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