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>
24 #include "ustream-ssl.h"
25 #include "ustream-internal.h"
27 static int urandom_fd = -1;
29 static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
31 struct ustream *s = ctx;
38 sbuf = ustream_get_read_buf(s, &slen);
43 return POLARSSL_ERR_NET_WANT_READ;
45 memcpy(buf, sbuf, slen);
46 ustream_consume(s, slen);
51 static int s_ustream_write(void *ctx, const unsigned char *buf, size_t len)
53 struct ustream *s = ctx;
56 ret = ustream_write(s, (const char *) buf, len, false);
57 if (ret < 0 || s->write_error)
58 return POLARSSL_ERR_NET_SEND_FAILED;
63 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
65 ssl_set_bio(ssl, s_ustream_read, conn, s_ustream_write, conn);
68 static bool urandom_init(void)
73 urandom_fd = open("/dev/urandom", O_RDONLY);
80 static int _urandom(void *ctx, unsigned char *out, size_t len)
82 if (read(urandom_fd, out, len) < 0)
83 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
88 __hidden struct ustream_ssl_ctx *
89 __ustream_ssl_context_new(bool server)
91 struct ustream_ssl_ctx *ctx;
96 ctx = calloc(1, sizeof(*ctx));
100 ctx->server = server;
101 #ifdef USE_VERSION_1_3
104 rsa_init(&ctx->key, RSA_PKCS_V15, 0);
110 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
114 #ifdef USE_VERSION_1_3
115 ret = x509_crt_parse_file(&ctx->ca_cert, file);
117 ret = x509parse_crtfile(&ctx->ca_cert, file);
125 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
129 #ifdef USE_VERSION_1_3
130 ret = x509_crt_parse_file(&ctx->cert, file);
132 ret = x509parse_crtfile(&ctx->cert, file);
140 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
144 #ifdef USE_VERSION_1_3
145 ret = pk_parse_keyfile(&ctx->key, file, NULL);
147 ret = x509parse_keyfile(&ctx->key, file, NULL);
155 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
157 #ifdef USE_VERSION_1_3
159 x509_crt_free(&ctx->cert);
162 x509_free(&ctx->cert);
167 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
170 uloop_timeout_set(&us->error_timer, 0);
173 static bool ssl_do_wait(int ret)
176 case POLARSSL_ERR_NET_WANT_READ:
177 case POLARSSL_ERR_NET_WANT_WRITE:
184 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
187 const char *msg = NULL;
191 r = ssl_get_verify_result(ssl);
192 cn_mismatch = r & BADCERT_CN_MISMATCH;
193 r &= ~BADCERT_CN_MISMATCH;
195 if (r & BADCERT_EXPIRED)
196 msg = "certificate has expired";
197 else if (r & BADCERT_REVOKED)
198 msg = "certificate has been revoked";
199 else if (r & BADCERT_NOT_TRUSTED)
200 msg = "certificate is self-signed or not signed by a trusted CA";
202 msg = "unknown error";
205 if (us->notify_verify_error)
206 us->notify_verify_error(us, r, msg);
214 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
219 r = ssl_handshake(ssl);
221 ustream_ssl_verify_cert(us);
226 return U_SSL_PENDING;
228 ustream_ssl_error(us, r);
232 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
235 int done = 0, ret = 0;
237 while (done != len) {
238 ret = ssl_write(ssl, (const unsigned char *) buf + done, len - done);
241 if (ssl_do_wait(ret))
244 ustream_ssl_error(us, ret);
254 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
256 int ret = ssl_read(us->ssl, (unsigned char *) buf, len);
259 if (ssl_do_wait(ret))
260 return U_SSL_PENDING;
262 ustream_ssl_error(us, ret);
269 static const int default_ciphersuites[] =
271 #if defined(POLARSSL_AES_C)
272 #if defined(POLARSSL_SHA2_C)
273 TLS_RSA_WITH_AES_256_CBC_SHA256,
274 #endif /* POLARSSL_SHA2_C */
275 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
276 TLS_RSA_WITH_AES_256_GCM_SHA384,
277 #endif /* POLARSSL_SHA2_C */
278 TLS_RSA_WITH_AES_256_CBC_SHA,
280 #if defined(POLARSSL_CAMELLIA_C)
281 #if defined(POLARSSL_SHA2_C)
282 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
283 #endif /* POLARSSL_SHA2_C */
284 TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
286 #if defined(POLARSSL_AES_C)
287 #if defined(POLARSSL_SHA2_C)
288 TLS_RSA_WITH_AES_128_CBC_SHA256,
289 #endif /* POLARSSL_SHA2_C */
290 #if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
291 TLS_RSA_WITH_AES_128_GCM_SHA256,
292 #endif /* POLARSSL_SHA2_C */
293 TLS_RSA_WITH_AES_128_CBC_SHA,
295 #if defined(POLARSSL_CAMELLIA_C)
296 #if defined(POLARSSL_SHA2_C)
297 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
298 #endif /* POLARSSL_SHA2_C */
299 TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
301 #if defined(POLARSSL_DES_C)
302 TLS_RSA_WITH_3DES_EDE_CBC_SHA,
304 #if defined(POLARSSL_ARC4_C)
305 TLS_RSA_WITH_RC4_128_SHA,
306 TLS_RSA_WITH_RC4_128_MD5,
311 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
317 ssl = calloc(1, sizeof(ssl_context));
328 auth = SSL_VERIFY_NONE;
331 auth = SSL_VERIFY_OPTIONAL;
334 ssl_set_ciphersuites(ssl, default_ciphersuites);
335 ssl_set_endpoint(ssl, ep);
336 ssl_set_authmode(ssl, auth);
337 ssl_set_rng(ssl, _urandom, NULL);
341 ssl_set_ca_chain(ssl, ctx->cert.next, NULL, NULL);
342 ssl_set_own_cert(ssl, &ctx->cert, &ctx->key);
344 ssl_set_ca_chain(ssl, &ctx->cert, NULL, NULL);
347 ssl_session_reset(ssl);
352 __hidden void __ustream_ssl_session_free(void *ssl)
358 __hidden void __ustream_ssl_update_peer_cn(struct ustream_ssl *us)
360 struct ustream_ssl_ctx *ctx = us->ctx;
362 ssl_set_ca_chain(us->ssl, &ctx->ca_cert, NULL, us->peer_cn);