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 __hidden
void __ustream_ssl_session_free(struct ustream_ssl
*us
)
250 BIO
*bio
= SSL_get_wbio(us
->ssl
);
253 SSL_shutdown(us
->ssl
);
259 ctx
= BIO_get_data(bio
);
261 BIO_meth_free(ctx
->meth
);
266 static void ustream_ssl_error(struct ustream_ssl
*us
, int ret
)
269 uloop_timeout_set(&us
->error_timer
, 0);
272 static bool ustream_ssl_verify_cn(struct ustream_ssl
*us
, X509
*cert
)
279 # ifndef WOLFSSL_OPENSSL_H_
280 ret
= X509_check_host(cert
, us
->peer_cn
, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
, NULL
);
282 ret
= wolfSSL_X509_check_host(cert
, us
->peer_cn
, 0, 0, NULL
);
287 static void ustream_ssl_verify_cert(struct ustream_ssl
*us
)
293 #if defined(HAVE_WOLFSSL) && defined(DEBUG)
297 res
= SSL_get_verify_result(ssl
);
298 if (res
!= X509_V_OK
) {
299 if (us
->notify_verify_error
)
300 us
->notify_verify_error(us
, res
, X509_verify_cert_error_string(res
));
304 cert
= SSL_get_peer_certificate(ssl
);
308 us
->valid_cert
= true;
309 us
->valid_cn
= ustream_ssl_verify_cn(us
, cert
);
315 static bool handle_wolfssl_asn_error(struct ustream_ssl
*us
, int r
)
322 case ASN_OBJECT_ID_E
:
326 case ASN_UNKNOWN_OID_E
:
328 case ASN_BEFORE_DATE_E
:
329 case ASN_AFTER_DATE_E
:
333 case ASN_SIG_CONFIRM_E
:
337 #if LIBWOLFSSL_VERSION_HEX < 0x05000000
342 case ASN_NO_PEM_HEADER
:
344 case ASN_NO_SIGNER_E
:
345 case ASN_CRL_CONFIRM_E
:
346 case ASN_CRL_NO_SIGNER_E
:
347 case ASN_OCSP_CONFIRM_E
:
348 case ASN_NAME_INVALID_E
:
351 case ASN_NO_KEYUSAGE
:
352 case ASN_COUNTRY_SIZE_E
:
353 case ASN_PATHLEN_SIZE_E
:
354 case ASN_PATHLEN_INV_E
:
355 case ASN_SELF_SIGNED_E
:
356 if (us
->notify_verify_error
)
357 us
->notify_verify_error(us
, r
, wc_GetErrorString(r
));
365 __hidden
enum ssl_conn_status
__ustream_ssl_connect(struct ustream_ssl
*us
)
375 r
= SSL_connect(ssl
);
378 ustream_ssl_verify_cert(us
);
382 r
= SSL_get_error(ssl
, r
);
383 if (r
== SSL_ERROR_WANT_READ
|| r
== SSL_ERROR_WANT_WRITE
)
384 return U_SSL_PENDING
;
387 if (handle_wolfssl_asn_error(us
, r
))
391 ustream_ssl_error(us
, r
);
395 __hidden
int __ustream_ssl_write(struct ustream_ssl
*us
, const char *buf
, int len
)
402 ret
= SSL_write(ssl
, buf
, len
);
405 int err
= SSL_get_error(ssl
, ret
);
406 if (err
== SSL_ERROR_WANT_WRITE
)
409 ustream_ssl_error(us
, err
);
416 __hidden
int __ustream_ssl_read(struct ustream_ssl
*us
, char *buf
, int len
)
422 ret
= SSL_read(us
->ssl
, buf
, len
);
425 ret
= SSL_get_error(us
->ssl
, ret
);
426 if (ret
== SSL_ERROR_WANT_READ
)
427 return U_SSL_PENDING
;
429 ustream_ssl_error(us
, ret
);
436 #ifndef WOLFSSL_SSL_H
438 debug_cb(BIO
*bio
, int cmd
, const char *argp
, size_t len
, int argi
, long argl
,
439 int ret
, size_t *processed
)
441 struct ustream_ssl_ctx
*ctx
= (void *)BIO_get_callback_arg(bio
);
446 if (cmd
!= (BIO_CB_WRITE
|BIO_CB_RETURN
))
450 cur_len
= BIO_get_mem_data(bio
, (void *)&str
);
454 sep
= memchr(str
, '\n', cur_len
);
458 cur_len
= sep
+ 1 - str
;
459 if (cur_len
>= (ssize_t
)sizeof(buf
))
460 cur_len
= sizeof(buf
) - 1;
462 cur_len
= BIO_read(bio
, buf
, cur_len
);
469 ctx
->debug_cb(ctx
->debug_cb_priv
, 1, buf
);
477 __hidden
void __ustream_ssl_set_debug(struct ustream_ssl_ctx
*ctx
, int level
,
478 ustream_ssl_debug_cb cb
, void *cb_priv
)
480 #ifndef WOLFSSL_SSL_H
482 ctx
->debug_bio
= BIO_new(BIO_s_mem());
485 ctx
->debug_cb_priv
= cb_priv
;
486 SSL_CTX_set_msg_callback(ctx
->ssl
, SSL_trace
);
487 SSL_CTX_set_msg_callback_arg(ctx
->ssl
, ctx
->debug_bio
);
489 BIO_set_callback_ex(ctx
->debug_bio
, debug_cb
);
490 BIO_set_callback_arg(ctx
->debug_bio
, (void *)ctx
);