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"
24 #if !defined(HAVE_WOLFSSL)
25 #include <openssl/x509v3.h>
28 #if defined(HAVE_WOLFSSL) && defined(DEBUG)
29 #include <wolfssl/test.h>
32 /* Ciphersuite preference:
33 * - for server, no weak ciphers are used if you use an ECDSA key.
34 * - forward-secret (pfs), authenticated (AEAD) ciphers are at the top:
35 * chacha20-poly1305, the fastest in software, 256-bits
36 * aes128-gcm, 128-bits
37 * aes256-gcm, 256-bits
38 * - key exchange: prefer ECDHE, then DHE (client only)
39 * - forward-secret ECDSA CBC ciphers (client-only)
40 * - forward-secret RSA CBC ciphers
42 * aes128, aes256, 3DES(client only)
46 # define top_ciphers \
47 "TLS13-CHACHA20-POLY1305-SHA256:" \
48 "TLS13-AES128-GCM-SHA256:" \
49 "TLS13-AES256-GCM-SHA384:" \
52 # define tls13_ciphersuites "TLS_CHACHA20_POLY1305_SHA256:" \
53 "TLS_AES_128_GCM_SHA256:" \
54 "TLS_AES_256_GCM_SHA384"
56 # define top_ciphers \
60 #define ecdhe_aead_ciphers \
61 "ECDHE-ECDSA-CHACHA20-POLY1305:" \
62 "ECDHE-ECDSA-AES128-GCM-SHA256:" \
63 "ECDHE-ECDSA-AES256-GCM-SHA384:" \
64 "ECDHE-RSA-CHACHA20-POLY1305:" \
65 "ECDHE-RSA-AES128-GCM-SHA256:" \
66 "ECDHE-RSA-AES256-GCM-SHA384"
68 #define dhe_aead_ciphers \
69 "DHE-RSA-CHACHA20-POLY1305:" \
70 "DHE-RSA-AES128-GCM-SHA256:" \
71 "DHE-RSA-AES256-GCM-SHA384"
73 #define ecdhe_ecdsa_cbc_ciphers \
74 "ECDHE-ECDSA-AES128-SHA:" \
75 "ECDHE-ECDSA-AES256-SHA"
77 #define ecdhe_rsa_cbc_ciphers \
78 "ECDHE-RSA-AES128-SHA:" \
79 "ECDHE-RSA-AES256-SHA"
81 #define dhe_cbc_ciphers \
82 "DHE-RSA-AES128-SHA:" \
83 "DHE-RSA-AES256-SHA:" \
87 "AES128-GCM-SHA256:" \
88 "AES256-GCM-SHA384:" \
92 #define server_cipher_list \
94 ecdhe_rsa_cbc_ciphers ":" \
97 #define client_cipher_list \
99 dhe_aead_ciphers ":" \
100 ecdhe_ecdsa_cbc_ciphers ":" \
101 ecdhe_rsa_cbc_ciphers ":" \
102 dhe_cbc_ciphers ":" \
106 __hidden
struct ustream_ssl_ctx
*
107 __ustream_ssl_context_new(bool server
)
112 #if OPENSSL_VERSION_NUMBER < 0x10100000L
113 static bool _init
= false;
116 SSL_load_error_strings();
120 # ifndef TLS_server_method
121 # define TLS_server_method SSLv23_server_method
123 # ifndef TLS_client_method
124 # define TLS_client_method SSLv23_client_method
129 m
= TLS_server_method();
131 m
= TLS_client_method();
133 c
= SSL_CTX_new((void *) m
);
137 #if defined(HAVE_WOLFSSL)
139 SSL_CTX_set_verify(c
, SSL_VERIFY_NONE
, NULL
);
141 SSL_CTX_set_verify(c
, SSL_VERIFY_PEER
, NULL
);
143 SSL_CTX_set_verify(c
, SSL_VERIFY_NONE
, NULL
);
146 SSL_CTX_set_options(c
, SSL_OP_NO_COMPRESSION
| SSL_OP_SINGLE_ECDH_USE
|
147 SSL_OP_CIPHER_SERVER_PREFERENCE
);
148 #if defined(SSL_CTX_set_ecdh_auto) && OPENSSL_VERSION_NUMBER < 0x10100000L
149 SSL_CTX_set_ecdh_auto(c
, 1);
150 #elif OPENSSL_VERSION_NUMBER >= 0x10101000L
151 SSL_CTX_set_ciphersuites(c
, tls13_ciphersuites
);
154 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
155 SSL_CTX_set_min_proto_version(c
, TLS1_2_VERSION
);
157 SSL_CTX_set_options(c
, SSL_OP_NO_SSLv3
| SSL_OP_NO_TLSv1
|
160 #if defined(HAVE_WOLFSSL)
161 SSL_CTX_set_options(c
, SSL_AD_NO_RENEGOTIATION
);
163 SSL_CTX_set_options(c
, SSL_OP_NO_RENEGOTIATION
);
166 SSL_CTX_set_cipher_list(c
, server_cipher_list
);
168 SSL_CTX_set_cipher_list(c
, client_cipher_list
);
170 SSL_CTX_set_quiet_shutdown(c
, 1);
175 __hidden
int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
179 ret
= SSL_CTX_load_verify_locations((void *) ctx
, file
, NULL
);
186 __hidden
int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
190 ret
= SSL_CTX_use_certificate_chain_file((void *) ctx
, file
);
192 ret
= SSL_CTX_use_certificate_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
200 __hidden
int __ustream_ssl_set_key_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
204 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_PEM
);
206 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
214 __hidden
int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx
*ctx
, const char *ciphers
)
216 int ret
= SSL_CTX_set_cipher_list((void *) ctx
, ciphers
);
224 __hidden
int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx
*ctx
, bool require
)
226 int mode
= SSL_VERIFY_PEER
;
229 mode
= SSL_VERIFY_NONE
;
231 SSL_CTX_set_verify((void *) ctx
, mode
, NULL
);
236 __hidden
void __ustream_ssl_context_free(struct ustream_ssl_ctx
*ctx
)
238 SSL_CTX_free((void *) ctx
);
241 void __ustream_ssl_session_free(void *ssl
)
243 BIO
*bio
= SSL_get_wbio(ssl
);
244 struct bio_ctx
*ctx
= BIO_get_data(bio
);
249 BIO_meth_free(ctx
->meth
);
254 static void ustream_ssl_error(struct ustream_ssl
*us
, int ret
)
257 uloop_timeout_set(&us
->error_timer
, 0);
260 static bool ustream_ssl_verify_cn(struct ustream_ssl
*us
, X509
*cert
)
267 # ifndef WOLFSSL_OPENSSL_H_
268 ret
= X509_check_host(cert
, us
->peer_cn
, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
, NULL
);
270 ret
= wolfSSL_X509_check_host(cert
, us
->peer_cn
, 0, 0, NULL
);
275 static void ustream_ssl_verify_cert(struct ustream_ssl
*us
)
281 #if defined(HAVE_WOLFSSL) && defined(DEBUG)
285 res
= SSL_get_verify_result(ssl
);
286 if (res
!= X509_V_OK
) {
287 if (us
->notify_verify_error
)
288 us
->notify_verify_error(us
, res
, X509_verify_cert_error_string(res
));
292 cert
= SSL_get_peer_certificate(ssl
);
296 us
->valid_cert
= true;
297 us
->valid_cn
= ustream_ssl_verify_cn(us
, cert
);
303 static bool handle_wolfssl_asn_error(struct ustream_ssl
*us
, int r
)
310 case ASN_OBJECT_ID_E
:
314 case ASN_UNKNOWN_OID_E
:
316 case ASN_BEFORE_DATE_E
:
317 case ASN_AFTER_DATE_E
:
321 case ASN_SIG_CONFIRM_E
:
325 #if LIBWOLFSSL_VERSION_HEX < 0x05000000
330 case ASN_NO_PEM_HEADER
:
332 case ASN_NO_SIGNER_E
:
333 case ASN_CRL_CONFIRM_E
:
334 case ASN_CRL_NO_SIGNER_E
:
335 case ASN_OCSP_CONFIRM_E
:
336 case ASN_NAME_INVALID_E
:
339 case ASN_NO_KEYUSAGE
:
340 case ASN_COUNTRY_SIZE_E
:
341 case ASN_PATHLEN_SIZE_E
:
342 case ASN_PATHLEN_INV_E
:
343 case ASN_SELF_SIGNED_E
:
344 if (us
->notify_verify_error
)
345 us
->notify_verify_error(us
, r
, wc_GetErrorString(r
));
353 __hidden
enum ssl_conn_status
__ustream_ssl_connect(struct ustream_ssl
*us
)
363 r
= SSL_connect(ssl
);
366 ustream_ssl_verify_cert(us
);
370 r
= SSL_get_error(ssl
, r
);
371 if (r
== SSL_ERROR_WANT_READ
|| r
== SSL_ERROR_WANT_WRITE
)
372 return U_SSL_PENDING
;
375 if (handle_wolfssl_asn_error(us
, r
))
379 ustream_ssl_error(us
, r
);
383 __hidden
int __ustream_ssl_write(struct ustream_ssl
*us
, const char *buf
, int len
)
390 ret
= SSL_write(ssl
, buf
, len
);
393 int err
= SSL_get_error(ssl
, ret
);
394 if (err
== SSL_ERROR_WANT_WRITE
)
397 ustream_ssl_error(us
, err
);
404 __hidden
int __ustream_ssl_read(struct ustream_ssl
*us
, char *buf
, int len
)
410 ret
= SSL_read(us
->ssl
, buf
, len
);
413 ret
= SSL_get_error(us
->ssl
, ret
);
414 if (ret
== SSL_ERROR_WANT_READ
)
415 return U_SSL_PENDING
;
417 ustream_ssl_error(us
, ret
);