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 /* Ciphersuite preference:
29 * - for server, no weak ciphers are used if you use an ECDSA key.
30 * - forward-secret (pfs), authenticated (AEAD) ciphers are at the top:
31 * chacha20-poly1305, the fastest in software, 256-bits
32 * aes128-gcm, 128-bits
33 * aes256-gcm, 256-bits
34 * - key exchange: prefer ECDHE, then DHE (client only)
35 * - forward-secret ECDSA CBC ciphers (client-only)
36 * - forward-secret RSA CBC ciphers
38 * aes128, aes256, 3DES(client only)
42 # define top_ciphers \
43 "TLS13-CHACHA20-POLY1305-SHA256:" \
44 "TLS13-AES128-GCM-SHA256:" \
45 "TLS13-AES256-GCM-SHA384:" \
48 # define tls13_ciphersuites "TLS_CHACHA20_POLY1305_SHA256:" \
49 "TLS_AES_128_GCM_SHA256:" \
50 "TLS_AES_256_GCM_SHA384"
52 # define top_ciphers \
56 #define ecdhe_aead_ciphers \
57 "ECDHE-ECDSA-CHACHA20-POLY1305:" \
58 "ECDHE-ECDSA-AES128-GCM-SHA256:" \
59 "ECDHE-ECDSA-AES256-GCM-SHA384:" \
60 "ECDHE-RSA-CHACHA20-POLY1305:" \
61 "ECDHE-RSA-AES128-GCM-SHA256:" \
62 "ECDHE-RSA-AES256-GCM-SHA384"
64 #define dhe_aead_ciphers \
65 "DHE-RSA-CHACHA20-POLY1305:" \
66 "DHE-RSA-AES128-GCM-SHA256:" \
67 "DHE-RSA-AES256-GCM-SHA384"
69 #define ecdhe_ecdsa_cbc_ciphers \
70 "ECDHE-ECDSA-AES128-SHA:" \
71 "ECDHE-ECDSA-AES256-SHA"
73 #define ecdhe_rsa_cbc_ciphers \
74 "ECDHE-RSA-AES128-SHA:" \
75 "ECDHE-RSA-AES256-SHA"
77 #define dhe_cbc_ciphers \
78 "DHE-RSA-AES128-SHA:" \
79 "DHE-RSA-AES256-SHA:" \
83 "AES128-GCM-SHA256:" \
84 "AES256-GCM-SHA384:" \
88 #define server_cipher_list \
90 ecdhe_rsa_cbc_ciphers ":" \
93 #define client_cipher_list \
95 dhe_aead_ciphers ":" \
96 ecdhe_ecdsa_cbc_ciphers ":" \
97 ecdhe_rsa_cbc_ciphers ":" \
102 __hidden
struct ustream_ssl_ctx
*
103 __ustream_ssl_context_new(bool server
)
108 #if OPENSSL_VERSION_NUMBER < 0x10100000L
109 static bool _init
= false;
112 SSL_load_error_strings();
116 # ifndef TLS_server_method
117 # define TLS_server_method SSLv23_server_method
119 # ifndef TLS_client_method
120 # define TLS_client_method SSLv23_client_method
125 m
= TLS_server_method();
127 m
= TLS_client_method();
129 c
= SSL_CTX_new((void *) m
);
133 #if defined(HAVE_WOLFSSL)
135 SSL_CTX_set_verify(c
, SSL_VERIFY_NONE
, NULL
);
137 SSL_CTX_set_verify(c
, SSL_VERIFY_PEER
, NULL
);
139 SSL_CTX_set_verify(c
, SSL_VERIFY_NONE
, NULL
);
142 SSL_CTX_set_options(c
, SSL_OP_NO_COMPRESSION
| SSL_OP_SINGLE_ECDH_USE
|
143 SSL_OP_CIPHER_SERVER_PREFERENCE
);
144 #if defined(SSL_CTX_set_ecdh_auto) && OPENSSL_VERSION_NUMBER < 0x10100000L
145 SSL_CTX_set_ecdh_auto(c
, 1);
146 #elif OPENSSL_VERSION_NUMBER >= 0x10101000L
147 SSL_CTX_set_ciphersuites(c
, tls13_ciphersuites
);
150 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
151 SSL_CTX_set_min_proto_version(c
, TLS1_2_VERSION
);
153 SSL_CTX_set_options(c
, SSL_OP_NO_SSLv3
| SSL_OP_NO_TLSv1
|
156 SSL_CTX_set_cipher_list(c
, server_cipher_list
);
158 SSL_CTX_set_cipher_list(c
, client_cipher_list
);
160 SSL_CTX_set_quiet_shutdown(c
, 1);
165 __hidden
int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
169 ret
= SSL_CTX_load_verify_locations((void *) ctx
, file
, NULL
);
176 __hidden
int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
180 ret
= SSL_CTX_use_certificate_chain_file((void *) ctx
, file
);
182 ret
= SSL_CTX_use_certificate_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
190 __hidden
int __ustream_ssl_set_key_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
194 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_PEM
);
196 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
204 __hidden
int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx
*ctx
, const char *ciphers
)
206 int ret
= SSL_CTX_set_cipher_list((void *) ctx
, ciphers
);
214 __hidden
int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx
*ctx
, bool require
)
216 int mode
= SSL_VERIFY_PEER
;
219 mode
= SSL_VERIFY_NONE
;
221 SSL_CTX_set_verify((void *) ctx
, mode
, NULL
);
226 __hidden
void __ustream_ssl_context_free(struct ustream_ssl_ctx
*ctx
)
228 SSL_CTX_free((void *) ctx
);
231 void __ustream_ssl_session_free(void *ssl
)
233 BIO
*bio
= SSL_get_wbio(ssl
);
234 struct bio_ctx
*ctx
= BIO_get_data(bio
);
239 BIO_meth_free(ctx
->meth
);
244 static void ustream_ssl_error(struct ustream_ssl
*us
, int ret
)
247 uloop_timeout_set(&us
->error_timer
, 0);
250 #ifndef NO_X509_CHECK_HOST
252 static bool ustream_ssl_verify_cn(struct ustream_ssl
*us
, X509
*cert
)
259 # ifndef WOLFSSL_OPENSSL_H_
260 ret
= X509_check_host(cert
, us
->peer_cn
, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
, NULL
);
262 ret
= wolfSSL_X509_check_host(cert
, us
->peer_cn
, 0, 0, NULL
);
269 static void ustream_ssl_verify_cert(struct ustream_ssl
*us
)
275 res
= SSL_get_verify_result(ssl
);
276 if (res
!= X509_V_OK
) {
277 if (us
->notify_verify_error
)
278 us
->notify_verify_error(us
, res
, X509_verify_cert_error_string(res
));
282 cert
= SSL_get_peer_certificate(ssl
);
286 us
->valid_cert
= true;
287 #ifndef NO_X509_CHECK_HOST
288 us
->valid_cn
= ustream_ssl_verify_cn(us
, cert
);
294 static bool handle_wolfssl_asn_error(struct ustream_ssl
*us
, int r
)
301 case ASN_OBJECT_ID_E
:
305 case ASN_UNKNOWN_OID_E
:
307 case ASN_BEFORE_DATE_E
:
308 case ASN_AFTER_DATE_E
:
312 case ASN_SIG_CONFIRM_E
:
319 case ASN_NO_PEM_HEADER
:
321 case ASN_NO_SIGNER_E
:
322 case ASN_CRL_CONFIRM_E
:
323 case ASN_CRL_NO_SIGNER_E
:
324 case ASN_OCSP_CONFIRM_E
:
325 case ASN_NAME_INVALID_E
:
328 case ASN_NO_KEYUSAGE
:
329 case ASN_COUNTRY_SIZE_E
:
330 case ASN_PATHLEN_SIZE_E
:
331 case ASN_PATHLEN_INV_E
:
332 case ASN_SELF_SIGNED_E
:
333 if (us
->notify_verify_error
)
334 us
->notify_verify_error(us
, r
, wc_GetErrorString(r
));
342 __hidden
enum ssl_conn_status
__ustream_ssl_connect(struct ustream_ssl
*us
)
352 r
= SSL_connect(ssl
);
355 ustream_ssl_verify_cert(us
);
359 r
= SSL_get_error(ssl
, r
);
360 if (r
== SSL_ERROR_WANT_READ
|| r
== SSL_ERROR_WANT_WRITE
)
361 return U_SSL_PENDING
;
364 if (handle_wolfssl_asn_error(us
, r
))
368 ustream_ssl_error(us
, r
);
372 __hidden
int __ustream_ssl_write(struct ustream_ssl
*us
, const char *buf
, int len
)
379 ret
= SSL_write(ssl
, buf
, len
);
382 int err
= SSL_get_error(ssl
, ret
);
383 if (err
== SSL_ERROR_WANT_WRITE
)
386 ustream_ssl_error(us
, err
);
393 __hidden
int __ustream_ssl_read(struct ustream_ssl
*us
, char *buf
, int len
)
399 ret
= SSL_read(us
->ssl
, buf
, len
);
402 ret
= SSL_get_error(us
->ssl
, ret
);
403 if (ret
== SSL_ERROR_WANT_READ
)
404 return U_SSL_PENDING
;
406 ustream_ssl_error(us
, ret
);