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