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.
21 #include "ustream-ssl.h"
22 #include "ustream-internal.h"
23 #include <openssl/x509v3.h>
25 /* Ciphersuite preference:
26 * - for server, no weak ciphers are used if you use an ECDSA key.
27 * - forward-secret (pfs), authenticated (AEAD) ciphers are at the top:
28 * chacha20-poly1305, the fastest in software, 256-bits
29 * aes128-gcm, 128-bits
30 * aes256-gcm, 256-bits
31 * - key exchange: prefer ECDHE, then DHE (client only)
32 * - forward-secret ECDSA CBC ciphers (client-only)
33 * - forward-secret RSA CBC ciphers
35 * aes128, aes256, 3DES(client only)
39 # define top_ciphers \
40 "TLS13-CHACHA20-POLY1305-SHA256:" \
41 "TLS13-AES128-GCM-SHA256:" \
42 "TLS13-AES256-GCM-SHA384:" \
45 # define tls13_ciphersuites "TLS_CHACHA20_POLY1305_SHA256:" \
46 "TLS_AES_128_GCM_SHA256:" \
47 "TLS_AES_256_GCM_SHA384"
49 # define top_ciphers \
53 #define ecdhe_aead_ciphers \
54 "ECDHE-ECDSA-CHACHA20-POLY1305:" \
55 "ECDHE-ECDSA-AES128-GCM-SHA256:" \
56 "ECDHE-ECDSA-AES256-GCM-SHA384:" \
57 "ECDHE-RSA-CHACHA20-POLY1305:" \
58 "ECDHE-RSA-AES128-GCM-SHA256:" \
59 "ECDHE-RSA-AES256-GCM-SHA384"
61 #define dhe_aead_ciphers \
62 "DHE-RSA-CHACHA20-POLY1305:" \
63 "DHE-RSA-AES128-GCM-SHA256:" \
64 "DHE-RSA-AES256-GCM-SHA384"
66 #define ecdhe_ecdsa_cbc_ciphers \
67 "ECDHE-ECDSA-AES128-SHA:" \
68 "ECDHE-ECDSA-AES256-SHA"
70 #define ecdhe_rsa_cbc_ciphers \
71 "ECDHE-RSA-AES128-SHA:" \
72 "ECDHE-RSA-AES256-SHA"
74 #define dhe_cbc_ciphers \
75 "DHE-RSA-AES128-SHA:" \
76 "DHE-RSA-AES256-SHA:" \
80 "AES128-GCM-SHA256:" \
81 "AES256-GCM-SHA384:" \
85 #define server_cipher_list \
87 ecdhe_rsa_cbc_ciphers ":" \
90 #define client_cipher_list \
92 dhe_aead_ciphers ":" \
93 ecdhe_ecdsa_cbc_ciphers ":" \
94 ecdhe_rsa_cbc_ciphers ":" \
99 __hidden struct ustream_ssl_ctx *
100 __ustream_ssl_context_new(bool server)
105 #if OPENSSL_VERSION_NUMBER < 0x10100000L
106 static bool _init = false;
109 SSL_load_error_strings();
113 # ifndef TLS_server_method
114 # define TLS_server_method SSLv23_server_method
116 # ifndef TLS_client_method
117 # define TLS_client_method SSLv23_client_method
122 m = TLS_server_method();
124 m = TLS_client_method();
126 c = SSL_CTX_new((void *) m);
130 SSL_CTX_set_verify(c, SSL_VERIFY_NONE, NULL);
131 SSL_CTX_set_options(c, SSL_OP_NO_COMPRESSION | SSL_OP_SINGLE_ECDH_USE |
132 SSL_OP_CIPHER_SERVER_PREFERENCE);
133 #if defined(SSL_CTX_set_ecdh_auto) && OPENSSL_VERSION_NUMBER < 0x10100000L
134 SSL_CTX_set_ecdh_auto(c, 1);
135 #elif OPENSSL_VERSION_NUMBER >= 0x10101000L
136 SSL_CTX_set_ciphersuites(c, tls13_ciphersuites);
139 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
140 SSL_CTX_set_min_proto_version(c, TLS1_2_VERSION);
142 SSL_CTX_set_options(c, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 |
145 SSL_CTX_set_cipher_list(c, server_cipher_list);
147 SSL_CTX_set_cipher_list(c, client_cipher_list);
149 SSL_CTX_set_quiet_shutdown(c, 1);
154 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
158 ret = SSL_CTX_load_verify_locations((void *) ctx, file, NULL);
165 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
169 ret = SSL_CTX_use_certificate_chain_file((void *) ctx, file);
171 ret = SSL_CTX_use_certificate_file((void *) ctx, file, SSL_FILETYPE_ASN1);
179 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
183 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_PEM);
185 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_ASN1);
193 __hidden int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx *ctx, const char *ciphers)
195 int ret = SSL_CTX_set_cipher_list((void *) ctx, ciphers);
203 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
205 SSL_CTX_free((void *) ctx);
208 void __ustream_ssl_session_free(void *ssl)
214 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
217 uloop_timeout_set(&us->error_timer, 0);
220 #ifndef NO_X509_CHECK_HOST
222 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
229 # ifndef WOLFSSL_OPENSSL_H_
230 ret = X509_check_host(cert, us->peer_cn, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS, NULL);
232 ret = wolfSSL_X509_check_host(cert, us->peer_cn, 0, 0, NULL);
239 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
245 res = SSL_get_verify_result(ssl);
246 if (res != X509_V_OK) {
247 if (us->notify_verify_error)
248 us->notify_verify_error(us, res, X509_verify_cert_error_string(res));
252 cert = SSL_get_peer_certificate(ssl);
256 us->valid_cert = true;
257 #ifndef NO_X509_CHECK_HOST
258 us->valid_cn = ustream_ssl_verify_cn(us, cert);
264 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
274 r = SSL_connect(ssl);
277 ustream_ssl_verify_cert(us);
281 r = SSL_get_error(ssl, r);
282 if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
283 return U_SSL_PENDING;
285 ustream_ssl_error(us, r);
289 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
296 ret = SSL_write(ssl, buf, len);
299 int err = SSL_get_error(ssl, ret);
300 if (err == SSL_ERROR_WANT_WRITE)
303 ustream_ssl_error(us, err);
310 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
316 ret = SSL_read(us->ssl, buf, len);
319 ret = SSL_get_error(us->ssl, ret);
320 if (ret == SSL_ERROR_WANT_READ)
321 return U_SSL_PENDING;
323 ustream_ssl_error(us, ret);