2 * ustream-ssl - library for SSL over ustream
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
19 #include <sys/types.h>
25 #include "ustream-ssl.h"
26 #include "ustream-internal.h"
28 static int urandom_fd = -1;
30 static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
32 struct ustream *s = ctx;
39 sbuf = ustream_get_read_buf(s, &slen);
44 return MBEDTLS_ERR_SSL_WANT_READ;
46 memcpy(buf, sbuf, slen);
47 ustream_consume(s, slen);
52 static int s_ustream_write(void *ctx, const unsigned char *buf, size_t len)
54 struct ustream *s = ctx;
57 ret = ustream_write(s, (const char *) buf, len, false);
58 if (ret < 0 || s->write_error)
59 return MBEDTLS_ERR_NET_SEND_FAILED;
64 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
66 mbedtls_ssl_set_bio(ssl, conn, s_ustream_write, s_ustream_read, NULL);
69 static bool urandom_init(void)
74 urandom_fd = open("/dev/urandom", O_RDONLY);
81 static int _urandom(void *ctx, unsigned char *out, size_t len)
83 if (read(urandom_fd, out, len) < 0)
84 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
89 #define AES_GCM_CIPHERS(v) \
90 MBEDTLS_TLS_##v##_WITH_AES_128_GCM_SHA256, \
91 MBEDTLS_TLS_##v##_WITH_AES_256_GCM_SHA384
93 #define AES_CBC_CIPHERS(v) \
94 MBEDTLS_TLS_##v##_WITH_AES_128_CBC_SHA, \
95 MBEDTLS_TLS_##v##_WITH_AES_256_CBC_SHA
97 #define AES_CIPHERS(v) \
101 static const int default_ciphersuites_server[] =
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),
112 static const int default_ciphersuites_client[] =
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,
125 MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
130 __hidden struct ustream_ssl_ctx *
131 __ustream_ssl_context_new(bool server)
133 struct ustream_ssl_ctx *ctx;
134 mbedtls_ssl_config *conf;
140 ctx = calloc(1, sizeof(*ctx));
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);
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);
156 mbedtls_ssl_config_init(conf);
158 ep = server ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT;
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);
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);
170 mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_client);
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);
180 static void ustream_ssl_update_own_cert(struct ustream_ssl_ctx *ctx)
182 if (!ctx->cert.version)
186 mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->cert, NULL);
190 if (!ctx->key.pk_info)
194 mbedtls_ssl_conf_ca_chain(&ctx->conf, ctx->cert.next, NULL);
195 mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->key);
198 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
202 ret = mbedtls_x509_crt_parse_file(&ctx->ca_cert, file);
206 mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->ca_cert, NULL);
207 mbedtls_ssl_conf_authmode(&ctx->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
211 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
215 ret = mbedtls_x509_crt_parse_file(&ctx->cert, file);
219 ustream_ssl_update_own_cert(ctx);
223 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
227 ret = mbedtls_pk_parse_keyfile(&ctx->key, file, NULL);
231 ustream_ssl_update_own_cert(ctx);
235 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
237 #if defined(MBEDTLS_SSL_CACHE_C)
238 mbedtls_ssl_cache_free(&ctx->cache);
240 mbedtls_pk_free(&ctx->key);
241 mbedtls_x509_crt_free(&ctx->ca_cert);
242 mbedtls_x509_crt_free(&ctx->cert);
243 mbedtls_ssl_config_free(&ctx->conf);
247 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
250 uloop_timeout_set(&us->error_timer, 0);
253 static bool ssl_do_wait(int ret)
256 case MBEDTLS_ERR_SSL_WANT_READ:
257 case MBEDTLS_ERR_SSL_WANT_WRITE:
264 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
267 const char *msg = NULL;
271 r = mbedtls_ssl_get_verify_result(ssl);
272 cn_mismatch = r & MBEDTLS_X509_BADCERT_CN_MISMATCH;
273 r &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
275 if (r & MBEDTLS_X509_BADCERT_EXPIRED)
276 msg = "certificate has expired";
277 else if (r & MBEDTLS_X509_BADCERT_REVOKED)
278 msg = "certificate has been revoked";
279 else if (r & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
280 msg = "certificate is self-signed or not signed by a trusted CA";
282 msg = "unknown error";
285 if (us->notify_verify_error)
286 us->notify_verify_error(us, r, msg);
294 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
299 r = mbedtls_ssl_handshake(ssl);
301 ustream_ssl_verify_cert(us);
306 return U_SSL_PENDING;
308 ustream_ssl_error(us, r);
312 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
315 int done = 0, ret = 0;
317 while (done != len) {
318 ret = mbedtls_ssl_write(ssl, (const unsigned char *) buf + done, len - done);
321 if (ssl_do_wait(ret))
324 ustream_ssl_error(us, ret);
334 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
336 int ret = mbedtls_ssl_read(us->ssl, (unsigned char *) buf, len);
339 if (ssl_do_wait(ret))
340 return U_SSL_PENDING;
342 if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
345 ustream_ssl_error(us, ret);
352 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
354 mbedtls_ssl_context *ssl;
356 ssl = calloc(1, sizeof(*ssl));
360 mbedtls_ssl_init(ssl);
362 if (mbedtls_ssl_setup(ssl, &ctx->conf)) {
370 __hidden void __ustream_ssl_session_free(void *ssl)
372 mbedtls_ssl_free(ssl);