mbedtls: Add support for a session cache
[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 #define TLS_DEFAULT_CIPHERS                     \
90     TLS_CIPHER(AES_256_CBC_SHA256)              \
91     TLS_CIPHER(AES_256_GCM_SHA384)              \
92     TLS_CIPHER(AES_256_CBC_SHA)                 \
93     TLS_CIPHER(CAMELLIA_256_CBC_SHA256)         \
94     TLS_CIPHER(CAMELLIA_256_CBC_SHA)            \
95     TLS_CIPHER(AES_128_CBC_SHA256)              \
96     TLS_CIPHER(AES_128_GCM_SHA256)              \
97     TLS_CIPHER(AES_128_CBC_SHA)                 \
98     TLS_CIPHER(CAMELLIA_128_CBC_SHA256)         \
99     TLS_CIPHER(CAMELLIA_128_CBC_SHA)            \
100     TLS_CIPHER(3DES_EDE_CBC_SHA)
101
102 static const int default_ciphersuites_nodhe[] =
103 {
104 #define TLS_CIPHER(v)                           \
105         MBEDTLS_TLS_RSA_WITH_##v,
106         TLS_DEFAULT_CIPHERS
107 #undef TLS_CIPHER
108         0
109 };
110
111 static const int default_ciphersuites[] =
112 {
113 #define TLS_CIPHER(v)                           \
114         MBEDTLS_TLS_DHE_RSA_WITH_##v,           \
115         MBEDTLS_TLS_RSA_WITH_##v,
116         TLS_DEFAULT_CIPHERS
117 #undef TLS_CIPHER
118         0
119 };
120
121
122 __hidden struct ustream_ssl_ctx *
123 __ustream_ssl_context_new(bool server)
124 {
125         struct ustream_ssl_ctx *ctx;
126         mbedtls_ssl_config *conf;
127         int ep;
128
129         if (!urandom_init())
130                 return NULL;
131
132         ctx = calloc(1, sizeof(*ctx));
133         if (!ctx)
134                 return NULL;
135
136         ctx->server = server;
137         mbedtls_pk_init(&ctx->key);
138         mbedtls_x509_crt_init(&ctx->cert);
139         mbedtls_x509_crt_init(&ctx->ca_cert);
140
141 #if defined(MBEDTLS_SSL_CACHE_C)
142         mbedtls_ssl_cache_init(&ctx->cache);
143         mbedtls_ssl_cache_set_timeout(&ctx->cache, 30 * 60);
144         mbedtls_ssl_cache_set_max_entries(&ctx->cache, 5);
145 #endif
146
147         conf = &ctx->conf;
148         mbedtls_ssl_config_init(conf);
149
150         if (server) {
151                 mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_nodhe);
152                 ep = MBEDTLS_SSL_IS_SERVER;
153         } else {
154                 mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites);
155                 ep = MBEDTLS_SSL_IS_CLIENT;
156         }
157
158         mbedtls_ssl_config_defaults(conf, ep, MBEDTLS_SSL_TRANSPORT_STREAM,
159                                     MBEDTLS_SSL_PRESET_DEFAULT);
160         mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
161         mbedtls_ssl_conf_rng(conf, _urandom, NULL);
162
163 #if defined(MBEDTLS_SSL_CACHE_C)
164         mbedtls_ssl_conf_session_cache(conf, &ctx->cache,
165                                        mbedtls_ssl_cache_get,
166                                        mbedtls_ssl_cache_set);
167 #endif
168         return ctx;
169 }
170
171 static void ustream_ssl_update_own_cert(struct ustream_ssl_ctx *ctx)
172 {
173         if (!ctx->cert.version)
174                 return;
175
176         if (!ctx->server) {
177                 mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->cert, NULL);
178                 return;
179         }
180
181         if (!ctx->key.pk_info)
182                 return;
183
184         if (ctx->cert.next)
185                 mbedtls_ssl_conf_ca_chain(&ctx->conf, ctx->cert.next, NULL);
186         mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->key);
187 }
188
189 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
190 {
191         int ret;
192
193         ret = mbedtls_x509_crt_parse_file(&ctx->ca_cert, file);
194         if (ret)
195                 return -1;
196
197         mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->ca_cert, NULL);
198         mbedtls_ssl_conf_authmode(&ctx->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
199         return 0;
200 }
201
202 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
203 {
204         int ret;
205
206         ret = mbedtls_x509_crt_parse_file(&ctx->cert, file);
207         if (ret)
208                 return -1;
209
210         ustream_ssl_update_own_cert(ctx);
211         return 0;
212 }
213
214 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
215 {
216         int ret;
217
218         ret = mbedtls_pk_parse_keyfile(&ctx->key, file, NULL);
219         if (ret)
220                 return -1;
221
222         ustream_ssl_update_own_cert(ctx);
223         return 0;
224 }
225
226 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
227 {
228 #if defined(MBEDTLS_SSL_CACHE_C)
229         mbedtls_ssl_cache_free(&ctx->cache);
230 #endif
231         mbedtls_pk_free(&ctx->key);
232         mbedtls_x509_crt_free(&ctx->ca_cert);
233         mbedtls_x509_crt_free(&ctx->cert);
234         mbedtls_ssl_config_free(&ctx->conf);
235         free(ctx);
236 }
237
238 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
239 {
240         us->error = ret;
241         uloop_timeout_set(&us->error_timer, 0);
242 }
243
244 static bool ssl_do_wait(int ret)
245 {
246         switch(ret) {
247         case MBEDTLS_ERR_SSL_WANT_READ:
248         case MBEDTLS_ERR_SSL_WANT_WRITE:
249                 return true;
250         default:
251                 return false;
252         }
253 }
254
255 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
256 {
257         void *ssl = us->ssl;
258         const char *msg = NULL;
259         bool cn_mismatch;
260         int r;
261
262         r = mbedtls_ssl_get_verify_result(ssl);
263         cn_mismatch = r & MBEDTLS_X509_BADCERT_CN_MISMATCH;
264         r &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
265
266         if (r & MBEDTLS_X509_BADCERT_EXPIRED)
267                 msg = "certificate has expired";
268         else if (r & MBEDTLS_X509_BADCERT_REVOKED)
269                 msg = "certificate has been revoked";
270         else if (r & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
271                 msg = "certificate is self-signed or not signed by a trusted CA";
272         else
273                 msg = "unknown error";
274
275         if (r) {
276                 if (us->notify_verify_error)
277                         us->notify_verify_error(us, r, msg);
278                 return;
279         }
280
281         if (!cn_mismatch)
282                 us->valid_cn = true;
283 }
284
285 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
286 {
287         void *ssl = us->ssl;
288         int r;
289
290         r = mbedtls_ssl_handshake(ssl);
291         if (r == 0) {
292                 ustream_ssl_verify_cert(us);
293                 return U_SSL_OK;
294         }
295
296         if (ssl_do_wait(r))
297                 return U_SSL_PENDING;
298
299         ustream_ssl_error(us, r);
300         return U_SSL_ERROR;
301 }
302
303 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
304 {
305         void *ssl = us->ssl;
306         int done = 0, ret = 0;
307
308         while (done != len) {
309                 ret = mbedtls_ssl_write(ssl, (const unsigned char *) buf + done, len - done);
310
311                 if (ret < 0) {
312                         if (ssl_do_wait(ret))
313                                 return done;
314
315                         ustream_ssl_error(us, ret);
316                         return -1;
317                 }
318
319                 done += ret;
320         }
321
322         return done;
323 }
324
325 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
326 {
327         int ret = mbedtls_ssl_read(us->ssl, (unsigned char *) buf, len);
328
329         if (ret < 0) {
330                 if (ssl_do_wait(ret))
331                         return U_SSL_PENDING;
332
333                 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
334                         return 0;
335
336                 ustream_ssl_error(us, ret);
337                 return U_SSL_ERROR;
338         }
339
340         return ret;
341 }
342
343 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
344 {
345         mbedtls_ssl_context *ssl;
346
347         ssl = calloc(1, sizeof(*ssl));
348         if (!ssl)
349                 return NULL;
350
351         mbedtls_ssl_init(ssl);
352
353         if (mbedtls_ssl_setup(ssl, &ctx->conf)) {
354                 free(ssl);
355                 return NULL;
356         }
357
358         return ssl;
359 }
360
361 __hidden void __ustream_ssl_session_free(void *ssl)
362 {
363         mbedtls_ssl_free(ssl);
364         free(ssl);
365 }