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