ustream-ssl: add support for using a fd instead of ustream as backing
[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 <sys/random.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "ustream-ssl.h"
27 #include "ustream-internal.h"
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 ((size_t) slen > len)
40 slen = len;
41
42 if (!slen)
43 return MBEDTLS_ERR_SSL_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 MBEDTLS_ERR_NET_SEND_FAILED;
59
60 return ret;
61 }
62
63 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
64 {
65 mbedtls_ssl_set_bio(ssl, conn, s_ustream_write, s_ustream_read, NULL);
66 }
67
68 static int _random(void *ctx, unsigned char *out, size_t len)
69 {
70 ssize_t ret;
71
72 ret = getrandom(out, len, 0);
73 if (ret < 0 || (size_t)ret != len)
74 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
75
76 return 0;
77 }
78
79 #define AES_GCM_CIPHERS(v) \
80 MBEDTLS_TLS_##v##_WITH_AES_128_GCM_SHA256, \
81 MBEDTLS_TLS_##v##_WITH_AES_256_GCM_SHA384
82
83 #define AES_CBC_CIPHERS(v) \
84 MBEDTLS_TLS_##v##_WITH_AES_128_CBC_SHA, \
85 MBEDTLS_TLS_##v##_WITH_AES_256_CBC_SHA
86
87 #define AES_CIPHERS(v) \
88 AES_GCM_CIPHERS(v), \
89 AES_CBC_CIPHERS(v)
90
91 static const int default_ciphersuites_server[] =
92 {
93 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
94 AES_GCM_CIPHERS(ECDHE_ECDSA),
95 MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
96 AES_GCM_CIPHERS(ECDHE_RSA),
97 AES_CBC_CIPHERS(ECDHE_RSA),
98 AES_CIPHERS(RSA),
99 0
100 };
101
102 static const int default_ciphersuites_client[] =
103 {
104 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
105 AES_GCM_CIPHERS(ECDHE_ECDSA),
106 MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
107 AES_GCM_CIPHERS(ECDHE_RSA),
108 MBEDTLS_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
109 AES_GCM_CIPHERS(DHE_RSA),
110 AES_CBC_CIPHERS(ECDHE_ECDSA),
111 AES_CBC_CIPHERS(ECDHE_RSA),
112 AES_CBC_CIPHERS(DHE_RSA),
113 /* Removed in Mbed TLS 3.0.0 */
114 #ifdef MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
115 MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
116 #endif
117 AES_CIPHERS(RSA),
118 /* Removed in Mbed TLS 3.0.0 */
119 #ifdef MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA
120 MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
121 #endif
122 0
123 };
124
125
126 __hidden struct ustream_ssl_ctx *
127 __ustream_ssl_context_new(bool server)
128 {
129 struct ustream_ssl_ctx *ctx;
130 mbedtls_ssl_config *conf;
131 int ep;
132
133 ctx = calloc(1, sizeof(*ctx));
134 if (!ctx)
135 return NULL;
136
137 ctx->server = server;
138 mbedtls_pk_init(&ctx->key);
139 mbedtls_x509_crt_init(&ctx->cert);
140 mbedtls_x509_crt_init(&ctx->ca_cert);
141
142 #if defined(MBEDTLS_SSL_CACHE_C)
143 mbedtls_ssl_cache_init(&ctx->cache);
144 mbedtls_ssl_cache_set_timeout(&ctx->cache, 30 * 60);
145 mbedtls_ssl_cache_set_max_entries(&ctx->cache, 5);
146 #endif
147
148 conf = &ctx->conf;
149 mbedtls_ssl_config_init(conf);
150
151 ep = server ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT;
152
153 mbedtls_ssl_config_defaults(conf, ep, MBEDTLS_SSL_TRANSPORT_STREAM,
154 MBEDTLS_SSL_PRESET_DEFAULT);
155 mbedtls_ssl_conf_rng(conf, _random, NULL);
156
157 if (server) {
158 mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
159 mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_server);
160 mbedtls_ssl_conf_min_version(conf, MBEDTLS_SSL_MAJOR_VERSION_3,
161 MBEDTLS_SSL_MINOR_VERSION_3);
162 } else {
163 mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
164 mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_client);
165 }
166
167 #if defined(MBEDTLS_SSL_CACHE_C)
168 mbedtls_ssl_conf_session_cache(conf, &ctx->cache,
169 mbedtls_ssl_cache_get,
170 mbedtls_ssl_cache_set);
171 #endif
172 return ctx;
173 }
174
175 static void ustream_ssl_update_own_cert(struct ustream_ssl_ctx *ctx)
176 {
177 if (!ctx->cert.version)
178 return;
179
180 if (mbedtls_pk_get_type(&ctx->key) == MBEDTLS_PK_NONE)
181 return;
182
183 mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->key);
184 }
185
186 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
187 {
188 int ret;
189
190 ret = mbedtls_x509_crt_parse_file(&ctx->ca_cert, file);
191 if (ret)
192 return -1;
193
194 mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->ca_cert, NULL);
195 mbedtls_ssl_conf_authmode(&ctx->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
196 return 0;
197 }
198
199 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
200 {
201 int ret;
202
203 ret = mbedtls_x509_crt_parse_file(&ctx->cert, file);
204 if (ret)
205 return -1;
206
207 ustream_ssl_update_own_cert(ctx);
208 return 0;
209 }
210
211 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
212 {
213 int ret;
214
215 #if (MBEDTLS_VERSION_NUMBER >= 0x03000000)
216 ret = mbedtls_pk_parse_keyfile(&ctx->key, file, NULL, _random, NULL);
217 #else
218 ret = mbedtls_pk_parse_keyfile(&ctx->key, file, NULL);
219 #endif
220 if (ret)
221 return -1;
222
223 ustream_ssl_update_own_cert(ctx);
224 return 0;
225 }
226
227 __hidden int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx *ctx, const char *ciphers)
228 {
229 int *ciphersuites = NULL, *tmp, id;
230 char *cipherstr, *p, *last, c;
231 size_t len = 0;
232
233 if (ciphers == NULL)
234 return -1;
235
236 cipherstr = strdup(ciphers);
237
238 if (cipherstr == NULL)
239 return -1;
240
241 for (p = cipherstr, last = p;; p++) {
242 if (*p == ':' || *p == 0) {
243 c = *p;
244 *p = 0;
245
246 id = mbedtls_ssl_get_ciphersuite_id(last);
247
248 if (id != 0) {
249 tmp = realloc(ciphersuites, (len + 2) * sizeof(int));
250
251 if (tmp == NULL) {
252 free(ciphersuites);
253 free(cipherstr);
254
255 return -1;
256 }
257
258 ciphersuites = tmp;
259 ciphersuites[len++] = id;
260 ciphersuites[len] = 0;
261 }
262
263 if (c == 0)
264 break;
265
266 last = p + 1;
267 }
268
269 /*
270 * mbedTLS expects cipher names with dashes while many sources elsewhere
271 * like the Firefox wiki or Wireshark specify ciphers with underscores,
272 * so simply convert all underscores to dashes to accept both notations.
273 */
274 else if (*p == '_') {
275 *p = '-';
276 }
277 }
278
279 free(cipherstr);
280
281 if (len == 0)
282 return -1;
283
284 mbedtls_ssl_conf_ciphersuites(&ctx->conf, ciphersuites);
285 free(ctx->ciphersuites);
286
287 ctx->ciphersuites = ciphersuites;
288
289 return 0;
290 }
291
292 __hidden int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx *ctx, bool require)
293 {
294 int mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
295
296 if (!require)
297 mode = MBEDTLS_SSL_VERIFY_NONE;
298
299 mbedtls_ssl_conf_authmode(&ctx->conf, mode);
300
301 return 0;
302 }
303
304 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
305 {
306 #if defined(MBEDTLS_SSL_CACHE_C)
307 mbedtls_ssl_cache_free(&ctx->cache);
308 #endif
309 mbedtls_pk_free(&ctx->key);
310 mbedtls_x509_crt_free(&ctx->ca_cert);
311 mbedtls_x509_crt_free(&ctx->cert);
312 mbedtls_ssl_config_free(&ctx->conf);
313 free(ctx->ciphersuites);
314 free(ctx);
315 }
316
317 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
318 {
319 us->error = ret;
320 uloop_timeout_set(&us->error_timer, 0);
321 }
322
323 static bool ssl_do_wait(int ret)
324 {
325 switch(ret) {
326 case MBEDTLS_ERR_SSL_WANT_READ:
327 case MBEDTLS_ERR_SSL_WANT_WRITE:
328 return true;
329 default:
330 return false;
331 }
332 }
333
334 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
335 {
336 void *ssl = us->ssl;
337 const char *msg = NULL;
338 bool cn_mismatch;
339 int r;
340
341 r = mbedtls_ssl_get_verify_result(ssl);
342 cn_mismatch = r & MBEDTLS_X509_BADCERT_CN_MISMATCH;
343 r &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
344
345 if (r & MBEDTLS_X509_BADCERT_EXPIRED)
346 msg = "certificate has expired";
347 else if (r & MBEDTLS_X509_BADCERT_REVOKED)
348 msg = "certificate has been revoked";
349 else if (r & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
350 msg = "certificate is self-signed or not signed by a trusted CA";
351 else
352 msg = "unknown error";
353
354 if (r) {
355 if (us->notify_verify_error)
356 us->notify_verify_error(us, r, msg);
357 return;
358 }
359
360 if (!cn_mismatch)
361 us->valid_cn = true;
362 }
363
364 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
365 {
366 void *ssl = us->ssl;
367 int r;
368
369 r = mbedtls_ssl_handshake(ssl);
370 if (r == 0) {
371 ustream_ssl_verify_cert(us);
372 return U_SSL_OK;
373 }
374
375 if (ssl_do_wait(r))
376 return U_SSL_PENDING;
377
378 ustream_ssl_error(us, r);
379 return U_SSL_ERROR;
380 }
381
382 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
383 {
384 void *ssl = us->ssl;
385 int done = 0, ret = 0;
386
387 while (done != len) {
388 ret = mbedtls_ssl_write(ssl, (const unsigned char *) buf + done, len - done);
389
390 if (ret < 0) {
391 if (ssl_do_wait(ret))
392 return done;
393
394 ustream_ssl_error(us, ret);
395 return -1;
396 }
397
398 done += ret;
399 }
400
401 return done;
402 }
403
404 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
405 {
406 int ret = mbedtls_ssl_read(us->ssl, (unsigned char *) buf, len);
407
408 if (ret < 0) {
409 if (ssl_do_wait(ret))
410 return U_SSL_PENDING;
411
412 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
413 return 0;
414
415 ustream_ssl_error(us, ret);
416 return U_SSL_ERROR;
417 }
418
419 return ret;
420 }
421
422 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
423 {
424 mbedtls_ssl_context *ssl;
425
426 ssl = calloc(1, sizeof(*ssl));
427 if (!ssl)
428 return NULL;
429
430 mbedtls_ssl_init(ssl);
431
432 if (mbedtls_ssl_setup(ssl, &ctx->conf)) {
433 free(ssl);
434 return NULL;
435 }
436
437 return ssl;
438 }
439
440 __hidden void __ustream_ssl_session_free(void *ssl)
441 {
442 mbedtls_ssl_free(ssl);
443 free(ssl);
444 }