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
)
109 struct ustream_ssl_ctx
*ctx
;
113 #if OPENSSL_VERSION_NUMBER < 0x10100000L
114 static bool _init
= false;
117 SSL_load_error_strings();
121 # ifndef TLS_server_method
122 # define TLS_server_method SSLv23_server_method
124 # ifndef TLS_client_method
125 # define TLS_client_method SSLv23_client_method
130 m
= TLS_server_method();
132 m
= TLS_client_method();
134 c
= SSL_CTX_new((void *) m
);
138 ctx
= calloc(1, sizeof(*ctx
));
141 #if defined(HAVE_WOLFSSL)
143 SSL_CTX_set_verify(c
, SSL_VERIFY_NONE
, NULL
);
145 SSL_CTX_set_verify(c
, SSL_VERIFY_PEER
, NULL
);
147 SSL_CTX_set_verify(c
, SSL_VERIFY_NONE
, NULL
);
150 SSL_CTX_set_options(c
, SSL_OP_NO_COMPRESSION
| SSL_OP_SINGLE_ECDH_USE
|
151 SSL_OP_CIPHER_SERVER_PREFERENCE
);
152 #if defined(SSL_CTX_set_ecdh_auto) && OPENSSL_VERSION_NUMBER < 0x10100000L
153 SSL_CTX_set_ecdh_auto(c
, 1);
154 #elif OPENSSL_VERSION_NUMBER >= 0x10101000L
155 SSL_CTX_set_ciphersuites(c
, tls13_ciphersuites
);
158 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
159 SSL_CTX_set_min_proto_version(c
, TLS1_2_VERSION
);
161 SSL_CTX_set_options(c
, SSL_OP_NO_SSLv3
| SSL_OP_NO_TLSv1
|
164 #if defined(HAVE_WOLFSSL)
165 SSL_CTX_set_options(c
, SSL_AD_NO_RENEGOTIATION
);
167 SSL_CTX_set_options(c
, SSL_OP_NO_RENEGOTIATION
);
170 SSL_CTX_set_cipher_list(c
, server_cipher_list
);
172 SSL_CTX_set_cipher_list(c
, client_cipher_list
);
174 SSL_CTX_set_quiet_shutdown(c
, 1);
179 __hidden
int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
183 ret
= SSL_CTX_load_verify_locations(ctx
->ssl
, file
, NULL
);
190 __hidden
int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
194 ret
= SSL_CTX_use_certificate_chain_file(ctx
->ssl
, file
);
196 ret
= SSL_CTX_use_certificate_file(ctx
->ssl
, file
, SSL_FILETYPE_ASN1
);
204 __hidden
int __ustream_ssl_set_key_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
208 ret
= SSL_CTX_use_PrivateKey_file(ctx
->ssl
, file
, SSL_FILETYPE_PEM
);
210 ret
= SSL_CTX_use_PrivateKey_file(ctx
->ssl
, file
, SSL_FILETYPE_ASN1
);
218 __hidden
int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx
*ctx
, const char *ciphers
)
220 int ret
= SSL_CTX_set_cipher_list(ctx
->ssl
, ciphers
);
228 __hidden
int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx
*ctx
, bool require
)
230 int mode
= SSL_VERIFY_PEER
;
233 mode
= SSL_VERIFY_NONE
;
235 SSL_CTX_set_verify(ctx
->ssl
, mode
, NULL
);
240 __hidden
void __ustream_ssl_context_free(struct ustream_ssl_ctx
*ctx
)
242 SSL_CTX_free(ctx
->ssl
);
244 BIO_free(ctx
->debug_bio
);
248 void __ustream_ssl_session_free(void *ssl
)
250 BIO
*bio
= SSL_get_wbio(ssl
);
251 struct bio_ctx
*ctx
= BIO_get_data(bio
);
256 BIO_meth_free(ctx
->meth
);
261 static void ustream_ssl_error(struct ustream_ssl
*us
, int ret
)
264 uloop_timeout_set(&us
->error_timer
, 0);
267 static bool ustream_ssl_verify_cn(struct ustream_ssl
*us
, X509
*cert
)
274 # ifndef WOLFSSL_OPENSSL_H_
275 ret
= X509_check_host(cert
, us
->peer_cn
, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
, NULL
);
277 ret
= wolfSSL_X509_check_host(cert
, us
->peer_cn
, 0, 0, NULL
);
282 static void ustream_ssl_verify_cert(struct ustream_ssl
*us
)
288 #if defined(HAVE_WOLFSSL) && defined(DEBUG)
292 res
= SSL_get_verify_result(ssl
);
293 if (res
!= X509_V_OK
) {
294 if (us
->notify_verify_error
)
295 us
->notify_verify_error(us
, res
, X509_verify_cert_error_string(res
));
299 cert
= SSL_get_peer_certificate(ssl
);
303 us
->valid_cert
= true;
304 us
->valid_cn
= ustream_ssl_verify_cn(us
, cert
);
310 static bool handle_wolfssl_asn_error(struct ustream_ssl
*us
, int r
)
317 case ASN_OBJECT_ID_E
:
321 case ASN_UNKNOWN_OID_E
:
323 case ASN_BEFORE_DATE_E
:
324 case ASN_AFTER_DATE_E
:
328 case ASN_SIG_CONFIRM_E
:
332 #if LIBWOLFSSL_VERSION_HEX < 0x05000000
337 case ASN_NO_PEM_HEADER
:
339 case ASN_NO_SIGNER_E
:
340 case ASN_CRL_CONFIRM_E
:
341 case ASN_CRL_NO_SIGNER_E
:
342 case ASN_OCSP_CONFIRM_E
:
343 case ASN_NAME_INVALID_E
:
346 case ASN_NO_KEYUSAGE
:
347 case ASN_COUNTRY_SIZE_E
:
348 case ASN_PATHLEN_SIZE_E
:
349 case ASN_PATHLEN_INV_E
:
350 case ASN_SELF_SIGNED_E
:
351 if (us
->notify_verify_error
)
352 us
->notify_verify_error(us
, r
, wc_GetErrorString(r
));
360 __hidden
enum ssl_conn_status
__ustream_ssl_connect(struct ustream_ssl
*us
)
370 r
= SSL_connect(ssl
);
373 ustream_ssl_verify_cert(us
);
377 r
= SSL_get_error(ssl
, r
);
378 if (r
== SSL_ERROR_WANT_READ
|| r
== SSL_ERROR_WANT_WRITE
)
379 return U_SSL_PENDING
;
382 if (handle_wolfssl_asn_error(us
, r
))
386 ustream_ssl_error(us
, r
);
390 __hidden
int __ustream_ssl_write(struct ustream_ssl
*us
, const char *buf
, int len
)
397 ret
= SSL_write(ssl
, buf
, len
);
400 int err
= SSL_get_error(ssl
, ret
);
401 if (err
== SSL_ERROR_WANT_WRITE
)
404 ustream_ssl_error(us
, err
);
411 __hidden
int __ustream_ssl_read(struct ustream_ssl
*us
, char *buf
, int len
)
417 ret
= SSL_read(us
->ssl
, buf
, len
);
420 ret
= SSL_get_error(us
->ssl
, ret
);
421 if (ret
== SSL_ERROR_WANT_READ
)
422 return U_SSL_PENDING
;
424 ustream_ssl_error(us
, ret
);
431 #ifndef WOLFSSL_SSL_H
433 debug_cb(BIO
*bio
, int cmd
, const char *argp
, size_t len
, int argi
, long argl
,
434 int ret
, size_t *processed
)
436 struct ustream_ssl_ctx
*ctx
= (void *)BIO_get_callback_arg(bio
);
441 if (cmd
!= (BIO_CB_WRITE
|BIO_CB_RETURN
))
445 cur_len
= BIO_get_mem_data(bio
, (void *)&str
);
449 sep
= memchr(str
, '\n', cur_len
);
453 cur_len
= sep
+ 1 - str
;
454 if (cur_len
>= (ssize_t
)sizeof(buf
))
455 cur_len
= sizeof(buf
) - 1;
457 cur_len
= BIO_read(bio
, buf
, cur_len
);
464 ctx
->debug_cb(ctx
->debug_cb_priv
, 1, buf
);
472 __hidden
void __ustream_ssl_set_debug(struct ustream_ssl_ctx
*ctx
, int level
,
473 ustream_ssl_debug_cb cb
, void *cb_priv
)
475 #ifndef WOLFSSL_SSL_H
477 ctx
->debug_bio
= BIO_new(BIO_s_mem());
480 ctx
->debug_cb_priv
= cb_priv
;
481 SSL_CTX_set_msg_callback(ctx
->ssl
, SSL_trace
);
482 SSL_CTX_set_msg_callback_arg(ctx
->ssl
, ctx
->debug_bio
);
484 BIO_set_callback_ex(ctx
->debug_bio
, debug_cb
);
485 BIO_set_callback_arg(ctx
->debug_bio
, (void *)ctx
);