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