eeabe42f0160c53392b8aa1340808d18c021fbdb
[project/ustream-ssl.git] / ustream-mbedtls.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 #include <string.h>
24
25 #include "ustream-ssl.h"
26 #include "ustream-internal.h"
27
28 static int urandom_fd = -1;
29
30 static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
31 {
32         struct ustream *s = ctx;
33         char *sbuf;
34         int slen;
35
36         if (s->eof)
37                 return 0;
38
39         sbuf = ustream_get_read_buf(s, &slen);
40         if (slen > len)
41                 slen = len;
42
43         if (!slen)
44                 return MBEDTLS_ERR_SSL_WANT_READ;
45
46         memcpy(buf, sbuf, slen);
47         ustream_consume(s, slen);
48
49         return slen;
50 }
51
52 static int s_ustream_write(void *ctx, const unsigned char *buf, size_t len)
53 {
54         struct ustream *s = ctx;
55         int ret;
56
57         ret = ustream_write(s, (const char *) buf, len, false);
58         if (ret < 0 || s->write_error)
59                 return MBEDTLS_ERR_NET_SEND_FAILED;
60
61         return ret;
62 }
63
64 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
65 {
66         mbedtls_ssl_set_bio(ssl, conn, s_ustream_write, s_ustream_read, NULL);
67 }
68
69 static bool urandom_init(void)
70 {
71         if (urandom_fd > -1)
72                 return true;
73
74         urandom_fd = open("/dev/urandom", O_RDONLY);
75         if (urandom_fd < 0)
76                 return false;
77
78         return true;
79 }
80
81 static int _urandom(void *ctx, unsigned char *out, size_t len)
82 {
83         if (read(urandom_fd, out, len) < 0)
84                 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
85
86         return 0;
87 }
88
89 static const int default_ciphersuites[] =
90 {
91 #if defined(MBEDTLS_AES_C)
92 #if defined(MBEDTLS_SHA2_C)
93         MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256,
94 #endif /* MBEDTLS_SHA2_C */
95 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_SHA4_C)
96         MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384,
97 #endif /* MBEDTLS_SHA2_C */
98         MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA,
99 #endif
100 #if defined(MBEDTLS_CAMELLIA_C)
101 #if defined(MBEDTLS_SHA2_C)
102         MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
103 #endif /* MBEDTLS_SHA2_C */
104         MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
105 #endif
106 #if defined(MBEDTLS_AES_C)
107 #if defined(MBEDTLS_SHA2_C)
108         MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256,
109 #endif /* MBEDTLS_SHA2_C */
110 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_SHA2_C)
111         MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256,
112 #endif /* MBEDTLS_SHA2_C */
113         MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA,
114 #endif
115 #if defined(MBEDTLS_CAMELLIA_C)
116 #if defined(MBEDTLS_SHA2_C)
117         MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
118 #endif /* MBEDTLS_SHA2_C */
119         MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
120 #endif
121 #if defined(MBEDTLS_DES_C)
122         MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
123 #endif
124         0
125 };
126
127 __hidden struct ustream_ssl_ctx *
128 __ustream_ssl_context_new(bool server)
129 {
130         struct ustream_ssl_ctx *ctx;
131         mbedtls_ssl_config *conf;
132         int ep;
133
134         if (!urandom_init())
135                 return NULL;
136
137         ctx = calloc(1, sizeof(*ctx));
138         if (!ctx)
139                 return NULL;
140
141         ctx->server = server;
142         mbedtls_pk_init(&ctx->key);
143         mbedtls_x509_crt_init(&ctx->cert);
144         mbedtls_x509_crt_init(&ctx->ca_cert);
145
146         conf = &ctx->conf;
147         mbedtls_ssl_config_init(conf);
148         mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites);
149
150         if (server)
151                 ep = MBEDTLS_SSL_IS_SERVER;
152         else
153                 ep = MBEDTLS_SSL_IS_CLIENT;
154
155         mbedtls_ssl_config_defaults(conf, ep, MBEDTLS_SSL_TRANSPORT_STREAM,
156                                     MBEDTLS_SSL_PRESET_DEFAULT);
157         mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
158         mbedtls_ssl_conf_rng(conf, _urandom, NULL);
159
160         return ctx;
161 }
162
163 static void ustream_ssl_update_own_cert(struct ustream_ssl_ctx *ctx)
164 {
165         if (!ctx->cert.version)
166                 return;
167
168         if (!ctx->server) {
169                 mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->cert, NULL);
170                 return;
171         }
172
173         if (!ctx->key.pk_info)
174                 return;
175
176         if (ctx->cert.next)
177                 mbedtls_ssl_conf_ca_chain(&ctx->conf, ctx->cert.next, NULL);
178         mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->key);
179 }
180
181 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
182 {
183         int ret;
184
185         ret = mbedtls_x509_crt_parse_file(&ctx->ca_cert, file);
186         if (ret)
187                 return -1;
188
189         mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->ca_cert, NULL);
190         mbedtls_ssl_conf_authmode(&ctx->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
191         return 0;
192 }
193
194 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
195 {
196         int ret;
197
198         ret = mbedtls_x509_crt_parse_file(&ctx->cert, file);
199         if (ret)
200                 return -1;
201
202         ustream_ssl_update_own_cert(ctx);
203         return 0;
204 }
205
206 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
207 {
208         int ret;
209
210         ret = mbedtls_pk_parse_keyfile(&ctx->key, file, NULL);
211         if (ret)
212                 return -1;
213
214         ustream_ssl_update_own_cert(ctx);
215         return 0;
216 }
217
218 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
219 {
220         mbedtls_pk_free(&ctx->key);
221         mbedtls_x509_crt_free(&ctx->ca_cert);
222         mbedtls_x509_crt_free(&ctx->cert);
223         mbedtls_ssl_config_free(&ctx->conf);
224         free(ctx);
225 }
226
227 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
228 {
229         us->error = ret;
230         uloop_timeout_set(&us->error_timer, 0);
231 }
232
233 static bool ssl_do_wait(int ret)
234 {
235         switch(ret) {
236         case MBEDTLS_ERR_SSL_WANT_READ:
237         case MBEDTLS_ERR_SSL_WANT_WRITE:
238                 return true;
239         default:
240                 return false;
241         }
242 }
243
244 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
245 {
246         void *ssl = us->ssl;
247         const char *msg = NULL;
248         bool cn_mismatch;
249         int r;
250
251         r = mbedtls_ssl_get_verify_result(ssl);
252         cn_mismatch = r & MBEDTLS_X509_BADCERT_CN_MISMATCH;
253         r &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
254
255         if (r & MBEDTLS_X509_BADCERT_EXPIRED)
256                 msg = "certificate has expired";
257         else if (r & MBEDTLS_X509_BADCERT_REVOKED)
258                 msg = "certificate has been revoked";
259         else if (r & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
260                 msg = "certificate is self-signed or not signed by a trusted CA";
261         else
262                 msg = "unknown error";
263
264         if (r) {
265                 if (us->notify_verify_error)
266                         us->notify_verify_error(us, r, msg);
267                 return;
268         }
269
270         if (!cn_mismatch)
271                 us->valid_cn = true;
272 }
273
274 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
275 {
276         void *ssl = us->ssl;
277         int r;
278
279         r = mbedtls_ssl_handshake(ssl);
280         if (r == 0) {
281                 ustream_ssl_verify_cert(us);
282                 return U_SSL_OK;
283         }
284
285         if (ssl_do_wait(r))
286                 return U_SSL_PENDING;
287
288         ustream_ssl_error(us, r);
289         return U_SSL_ERROR;
290 }
291
292 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
293 {
294         void *ssl = us->ssl;
295         int done = 0, ret = 0;
296
297         while (done != len) {
298                 ret = mbedtls_ssl_write(ssl, (const unsigned char *) buf + done, len - done);
299
300                 if (ret < 0) {
301                         if (ssl_do_wait(ret))
302                                 return done;
303
304                         ustream_ssl_error(us, ret);
305                         return -1;
306                 }
307
308                 done += ret;
309         }
310
311         return done;
312 }
313
314 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
315 {
316         int ret = mbedtls_ssl_read(us->ssl, (unsigned char *) buf, len);
317
318         if (ret < 0) {
319                 if (ssl_do_wait(ret))
320                         return U_SSL_PENDING;
321
322                 ustream_ssl_error(us, ret);
323                 return U_SSL_ERROR;
324         }
325
326         return ret;
327 }
328
329 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
330 {
331         mbedtls_ssl_context *ssl;
332
333         ssl = calloc(1, sizeof(*ssl));
334         if (!ssl)
335                 return NULL;
336
337         mbedtls_ssl_init(ssl);
338
339         if (mbedtls_ssl_setup(ssl, &ctx->conf)) {
340                 free(ssl);
341                 return NULL;
342         }
343
344         return ssl;
345 }
346
347 __hidden void __ustream_ssl_session_free(void *ssl)
348 {
349         mbedtls_ssl_free(ssl);
350         free(ssl);
351 }