ustream-ssl: always check connection at the beginning of a session
[project/ustream-ssl.git] / ustream-polarssl.c
1 /*
2  * ustream-ssl - library for SSL over ustream
3  *
4  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23
24 #include "ustream-ssl.h"
25 #include "ustream-internal.h"
26
27 static int urandom_fd = -1;
28
29 static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
30 {
31         struct ustream *s = ctx;
32         char *sbuf;
33         int slen;
34
35         if (s->eof)
36                 return 0;
37
38         sbuf = ustream_get_read_buf(s, &slen);
39         if (slen > len)
40                 slen = len;
41
42         if (!slen)
43                 return POLARSSL_ERR_NET_WANT_READ;
44
45         memcpy(buf, sbuf, slen);
46         ustream_consume(s, slen);
47
48         return slen;
49 }
50
51 static int s_ustream_write(void *ctx, const unsigned char *buf, size_t len)
52 {
53         struct ustream *s = ctx;
54         int ret;
55
56         ret = ustream_write(s, (const char *) buf, len, false);
57         if (ret < 0 || s->write_error)
58                 return POLARSSL_ERR_NET_SEND_FAILED;
59
60         return ret;
61 }
62
63 __hidden void ustream_set_io(void *ctx, void *ssl, struct ustream *conn)
64 {
65         ssl_set_bio(ssl, s_ustream_read, conn, s_ustream_write, conn);
66 }
67
68 static bool urandom_init(void)
69 {
70         if (urandom_fd > -1)
71                 return true;
72
73         urandom_fd = open("/dev/urandom", O_RDONLY);
74         if (urandom_fd < 0)
75                 return false;
76
77         return true;
78 }
79
80 static int _urandom(void *ctx, unsigned char *out, size_t len)
81 {
82         read(urandom_fd, out, len);
83         return 0;
84 }
85
86 __hidden void * __ustream_ssl_context_new(bool server)
87 {
88         struct ustream_polarssl_ctx *uctx;
89
90         if (!urandom_init())
91                 return NULL;
92
93         uctx = calloc(1, sizeof(*uctx));
94         if (!uctx)
95                 return NULL;
96
97         uctx->server = server;
98 #ifdef USE_VERSION_1_3
99         pk_init(&uctx->key);
100 #else
101         rsa_init(&uctx->key, RSA_PKCS_V15, 0);
102 #endif
103
104         return uctx;
105 }
106
107 __hidden int __ustream_ssl_set_crt_file(void *ctx, const char *file)
108 {
109         struct ustream_polarssl_ctx *uctx = ctx;
110         int ret;
111
112 #ifdef USE_VERSION_1_3
113         ret = x509_crt_parse_file(&uctx->cert, file);
114 #else
115         ret = x509parse_crtfile(&uctx->cert, file);
116 #endif
117         if (ret)
118                 return -1;
119
120         return 0;
121 }
122
123 __hidden int __ustream_ssl_set_key_file(void *ctx, const char *file)
124 {
125         struct ustream_polarssl_ctx *uctx = ctx;
126         int ret;
127
128 #ifdef USE_VERSION_1_3
129         ret = pk_parse_keyfile(&uctx->key, file, NULL);
130 #else
131         ret = x509parse_keyfile(&uctx->key, file, NULL);
132 #endif
133         if (ret)
134                 return -1;
135
136         return 0;
137 }
138
139 __hidden void __ustream_ssl_context_free(void *ctx)
140 {
141         struct ustream_polarssl_ctx *uctx = ctx;
142
143 #ifdef USE_VERSION_1_3
144         pk_free(&uctx->key);
145         x509_crt_free(&uctx->cert);
146 #else
147         rsa_free(&uctx->key);
148         x509_free(&uctx->cert);
149 #endif
150         free(ctx);
151 }
152
153 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
154 {
155         us->error = ret;
156         uloop_timeout_set(&us->error_timer, 0);
157 }
158
159 static bool ssl_do_wait(int ret)
160 {
161         switch(ret) {
162         case POLARSSL_ERR_NET_WANT_READ:
163         case POLARSSL_ERR_NET_WANT_WRITE:
164                 return true;
165         default:
166                 return false;
167         }
168 }
169
170 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
171 {
172         void *ssl = us->ssl;
173         int r;
174
175         r = ssl_handshake(ssl);
176         if (r == 0)
177                 return U_SSL_OK;
178
179         if (ssl_do_wait(r))
180                 return U_SSL_PENDING;
181
182         ustream_ssl_error(us, r);
183         return U_SSL_ERROR;
184 }
185
186 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
187 {
188         void *ssl = us->ssl;
189         int ret = ssl_write(ssl, (const unsigned char *) buf, len);
190
191         if (ret < 0) {
192                 if (ssl_do_wait(ret))
193                         return 0;
194
195                 ustream_ssl_error(us, ret);
196                 return -1;
197         }
198
199         return ret;
200 }
201
202 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
203 {
204         int ret = ssl_read(us->ssl, (unsigned char *) buf, len);
205
206         if (ret < 0) {
207                 if (ssl_do_wait(ret))
208                         return U_SSL_PENDING;
209
210                 ustream_ssl_error(us, ret);
211                 return U_SSL_ERROR;
212         }
213
214         return ret;
215 }
216
217 static const int default_ciphersuites[] =
218 {
219 #if defined(POLARSSL_AES_C)
220 #if defined(POLARSSL_SHA2_C)
221     TLS_RSA_WITH_AES_256_CBC_SHA256,
222 #endif /* POLARSSL_SHA2_C */
223 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
224     TLS_RSA_WITH_AES_256_GCM_SHA384,
225 #endif /* POLARSSL_SHA2_C */
226     TLS_RSA_WITH_AES_256_CBC_SHA,
227 #endif
228 #if defined(POLARSSL_CAMELLIA_C)
229 #if defined(POLARSSL_SHA2_C)
230     TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
231 #endif /* POLARSSL_SHA2_C */
232     TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
233 #endif
234 #if defined(POLARSSL_AES_C)
235 #if defined(POLARSSL_SHA2_C)
236     TLS_RSA_WITH_AES_128_CBC_SHA256,
237 #endif /* POLARSSL_SHA2_C */
238 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
239     TLS_RSA_WITH_AES_128_GCM_SHA256,
240 #endif /* POLARSSL_SHA2_C */
241     TLS_RSA_WITH_AES_128_CBC_SHA,
242 #endif
243 #if defined(POLARSSL_CAMELLIA_C)
244 #if defined(POLARSSL_SHA2_C)
245     TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
246 #endif /* POLARSSL_SHA2_C */
247     TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
248 #endif
249 #if defined(POLARSSL_DES_C)
250     TLS_RSA_WITH_3DES_EDE_CBC_SHA,
251 #endif
252 #if defined(POLARSSL_ARC4_C)
253     TLS_RSA_WITH_RC4_128_SHA,
254     TLS_RSA_WITH_RC4_128_MD5,
255 #endif
256     0
257 };
258
259 __hidden void *__ustream_ssl_session_new(void *ctx)
260 {
261         struct ustream_polarssl_ctx *uctx = ctx;
262         ssl_context *ssl;
263         int ep, auth;
264
265         ssl = calloc(1, sizeof(ssl_context));
266         if (!ssl)
267                 return NULL;
268
269         if (ssl_init(ssl)) {
270                 free(ssl);
271                 return NULL;
272         }
273
274         if (uctx->server) {
275                 ep = SSL_IS_SERVER;
276                 auth = SSL_VERIFY_NONE;
277         } else {
278                 ep = SSL_IS_CLIENT;
279                 auth = SSL_VERIFY_OPTIONAL;
280         }
281
282         ssl_set_ciphersuites(ssl, default_ciphersuites);
283         ssl_set_endpoint(ssl, ep);
284         ssl_set_authmode(ssl, auth);
285         ssl_set_rng(ssl, _urandom, NULL);
286
287         if (uctx->server) {
288                 if (uctx->cert.next)
289                         ssl_set_ca_chain(ssl, uctx->cert.next, NULL, NULL);
290                 ssl_set_own_cert(ssl, &uctx->cert, &uctx->key);
291         }
292
293         ssl_session_reset(ssl);
294
295         return ssl;
296 }
297
298 __hidden void __ustream_ssl_session_free(void *ssl)
299 {
300         ssl_free(ssl);
301         free(ssl);
302 }