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 POLARSSL_ERR_NET_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 POLARSSL_ERR_NET_SEND_FAILED;
64 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
66 ssl_set_bio(ssl, s_ustream_read, conn, s_ustream_write, conn);
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 POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
89 __hidden struct ustream_ssl_ctx *
90 __ustream_ssl_context_new(bool server)
92 struct ustream_ssl_ctx *ctx;
97 ctx = calloc(1, sizeof(*ctx));
101 ctx->server = server;
103 x509_crt_init(&ctx->ca_cert);
104 x509_crt_init(&ctx->cert);
109 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
113 ret = x509_crt_parse_file(&ctx->ca_cert, file);
120 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
124 ret = x509_crt_parse_file(&ctx->cert, file);
131 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
135 ret = pk_parse_keyfile(&ctx->key, file, NULL);
142 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
145 x509_crt_free(&ctx->ca_cert);
146 x509_crt_free(&ctx->cert);
150 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
153 uloop_timeout_set(&us->error_timer, 0);
156 static bool ssl_do_wait(int ret)
159 case POLARSSL_ERR_NET_WANT_READ:
160 case POLARSSL_ERR_NET_WANT_WRITE:
167 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
170 const char *msg = NULL;
174 r = ssl_get_verify_result(ssl);
175 cn_mismatch = r & BADCERT_CN_MISMATCH;
176 r &= ~BADCERT_CN_MISMATCH;
178 if (r & BADCERT_EXPIRED)
179 msg = "certificate has expired";
180 else if (r & BADCERT_REVOKED)
181 msg = "certificate has been revoked";
182 else if (r & BADCERT_NOT_TRUSTED)
183 msg = "certificate is self-signed or not signed by a trusted CA";
185 msg = "unknown error";
188 if (us->notify_verify_error)
189 us->notify_verify_error(us, r, msg);
197 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
202 r = ssl_handshake(ssl);
204 ustream_ssl_verify_cert(us);
209 return U_SSL_PENDING;
211 ustream_ssl_error(us, r);
215 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
218 int done = 0, ret = 0;
220 while (done != len) {
221 ret = ssl_write(ssl, (const unsigned char *) buf + done, len - done);
224 if (ssl_do_wait(ret))
227 ustream_ssl_error(us, ret);
237 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
239 int ret = ssl_read(us->ssl, (unsigned char *) buf, len);
242 if (ssl_do_wait(ret))
243 return U_SSL_PENDING;
245 if (ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
248 ustream_ssl_error(us, ret);
255 #define TLS_DEFAULT_CIPHERS \
256 TLS_CIPHER(AES_256_CBC_SHA256) \
257 TLS_CIPHER(AES_256_GCM_SHA384) \
258 TLS_CIPHER(AES_256_CBC_SHA) \
259 TLS_CIPHER(CAMELLIA_256_CBC_SHA256) \
260 TLS_CIPHER(CAMELLIA_256_CBC_SHA) \
261 TLS_CIPHER(AES_128_CBC_SHA256) \
262 TLS_CIPHER(AES_128_GCM_SHA256) \
263 TLS_CIPHER(AES_128_CBC_SHA) \
264 TLS_CIPHER(CAMELLIA_128_CBC_SHA256) \
265 TLS_CIPHER(CAMELLIA_128_CBC_SHA) \
266 TLS_CIPHER(3DES_EDE_CBC_SHA)
268 static const int default_ciphersuites_nodhe[] =
270 #define TLS_CIPHER(v) \
277 static const int default_ciphersuites[] =
279 #define TLS_CIPHER(v) \
280 TLS_DHE_RSA_WITH_##v, \
287 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
293 ssl = calloc(1, sizeof(ssl_context));
304 auth = SSL_VERIFY_NONE;
307 auth = SSL_VERIFY_OPTIONAL;
310 ssl_set_endpoint(ssl, ep);
311 ssl_set_authmode(ssl, auth);
312 ssl_set_rng(ssl, _urandom, NULL);
315 ssl_set_ciphersuites(ssl, default_ciphersuites_nodhe);
317 ssl_set_ca_chain(ssl, ctx->cert.next, NULL, NULL);
318 ssl_set_own_cert(ssl, &ctx->cert, &ctx->key);
320 ssl_set_ciphersuites(ssl, default_ciphersuites);
321 ssl_set_ca_chain(ssl, &ctx->ca_cert, NULL, NULL);
324 ssl_session_reset(ssl);
329 __hidden void __ustream_ssl_session_free(void *ssl)
335 __hidden void __ustream_ssl_update_peer_cn(struct ustream_ssl *us)
337 struct ustream_ssl_ctx *ctx = us->ctx;
339 ssl_set_ca_chain(us->ssl, &ctx->ca_cert, NULL, us->peer_cn);