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