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 <openssl/x509v3.h>
22 #include "ustream-ssl.h"
23 #include "ustream-internal.h"
26 /* Ciphersuite preference:
27 * - key exchange: prefer ECDHE, then DHE(client only), then RSA
28 * - prefer AEAD ciphers:
29 * chacha20-poly1305, the fastest in software, 256-bits
30 * aes128-gcm, 128-bits
31 * aes256-gcm, 256-bits
33 * aes128, aes256, 3DES(client only)
37 # define top_ciphers \
38 "TLS13-CHACHA20-POLY1305-SHA256:" \
39 "TLS13-AES128-GCM-SHA256:" \
40 "TLS13-AES256-GCM-SHA384:" \
43 # define tls13_ciphersuites "TLS_CHACHA20_POLY1305_SHA256:" \
44 "TLS_AES_128_GCM_SHA256:" \
45 "TLS_AES_256_GCM_SHA384"
47 # define top_ciphers \
51 #define ecdhe_ciphers \
52 "ECDHE-ECDSA-CHACHA20-POLY1305:" \
53 "ECDHE-ECDSA-AES128-GCM-SHA256:" \
54 "ECDHE-ECDSA-AES256-GCM-SHA384:" \
55 "ECDHE-ECDSA-AES128-SHA:" \
56 "ECDHE-ECDSA-AES256-SHA:" \
57 "ECDHE-RSA-CHACHA20-POLY1305:" \
58 "ECDHE-RSA-AES128-GCM-SHA256:" \
59 "ECDHE-RSA-AES256-GCM-SHA384:" \
60 "ECDHE-RSA-AES128-SHA:" \
61 "ECDHE-RSA-AES256-SHA"
64 "DHE-RSA-CHACHA20-POLY1305:" \
65 "DHE-RSA-AES128-GCM-SHA256:" \
66 "DHE-RSA-AES256-GCM-SHA384:" \
67 "DHE-RSA-AES128-SHA:" \
68 "DHE-RSA-AES256-SHA:" \
72 "AES128-GCM-SHA256:" \
73 "AES256-GCM-SHA384:" \
77 #define server_cipher_list \
81 #define client_cipher_list \
87 __hidden
struct ustream_ssl_ctx
*
88 __ustream_ssl_context_new(bool server
)
93 #if OPENSSL_VERSION_NUMBER < 0x10100000L
94 static bool _init
= false;
97 SSL_load_error_strings();
101 # define TLS_server_method SSLv23_server_method
102 # define TLS_client_method SSLv23_client_method
106 m
= TLS_server_method();
108 m
= TLS_client_method();
110 c
= SSL_CTX_new((void *) m
);
114 SSL_CTX_set_verify(c
, SSL_VERIFY_NONE
, NULL
);
115 SSL_CTX_set_options(c
, SSL_OP_NO_COMPRESSION
| SSL_OP_SINGLE_ECDH_USE
|
116 SSL_OP_CIPHER_SERVER_PREFERENCE
);
117 #if defined(SSL_CTX_set_ecdh_auto) && OPENSSL_VERSION_NUMBER < 0x10100000L
118 SSL_CTX_set_ecdh_auto(c
, 1);
119 #elif OPENSSL_VERSION_NUMBER >= 0x10101000L
120 SSL_CTX_set_ciphersuites(c
, tls13_ciphersuites
);
123 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
124 SSL_CTX_set_min_proto_version(c
, TLS1_2_VERSION
);
126 SSL_CTX_set_options(c
, SSL_OP_NO_SSLv3
| SSL_OP_NO_TLSv1
|
129 SSL_CTX_set_cipher_list(c
, server_cipher_list
);
131 SSL_CTX_set_cipher_list(c
, client_cipher_list
);
133 SSL_CTX_set_quiet_shutdown(c
, 1);
138 __hidden
int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
142 ret
= SSL_CTX_load_verify_locations((void *) ctx
, file
, NULL
);
149 __hidden
int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
153 ret
= SSL_CTX_use_certificate_chain_file((void *) ctx
, file
);
155 ret
= SSL_CTX_use_certificate_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
163 __hidden
int __ustream_ssl_set_key_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
167 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_PEM
);
169 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
177 __hidden
void __ustream_ssl_context_free(struct ustream_ssl_ctx
*ctx
)
179 SSL_CTX_free((void *) ctx
);
182 void __ustream_ssl_session_free(void *ssl
)
188 static void ustream_ssl_error(struct ustream_ssl
*us
, int ret
)
191 uloop_timeout_set(&us
->error_timer
, 0);
194 #ifndef CYASSL_OPENSSL_H_
196 static bool ustream_ssl_verify_cn(struct ustream_ssl
*us
, X509
*cert
)
203 ret
= X509_check_host(cert
, us
->peer_cn
, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
, NULL
);
208 static void ustream_ssl_verify_cert(struct ustream_ssl
*us
)
214 res
= SSL_get_verify_result(ssl
);
215 if (res
!= X509_V_OK
) {
216 if (us
->notify_verify_error
)
217 us
->notify_verify_error(us
, res
, X509_verify_cert_error_string(res
));
221 cert
= SSL_get_peer_certificate(ssl
);
225 us
->valid_cert
= true;
226 us
->valid_cn
= ustream_ssl_verify_cn(us
, cert
);
232 __hidden
enum ssl_conn_status
__ustream_ssl_connect(struct ustream_ssl
*us
)
240 r
= SSL_connect(ssl
);
243 #ifndef CYASSL_OPENSSL_H_
244 ustream_ssl_verify_cert(us
);
249 r
= SSL_get_error(ssl
, r
);
250 if (r
== SSL_ERROR_WANT_READ
|| r
== SSL_ERROR_WANT_WRITE
)
251 return U_SSL_PENDING
;
253 ustream_ssl_error(us
, r
);
257 __hidden
int __ustream_ssl_write(struct ustream_ssl
*us
, const char *buf
, int len
)
260 int ret
= SSL_write(ssl
, buf
, len
);
263 int err
= SSL_get_error(ssl
, ret
);
264 if (err
== SSL_ERROR_WANT_WRITE
)
267 ustream_ssl_error(us
, err
);
274 __hidden
int __ustream_ssl_read(struct ustream_ssl
*us
, char *buf
, int len
)
276 int ret
= SSL_read(us
->ssl
, buf
, len
);
279 ret
= SSL_get_error(us
->ssl
, ret
);
280 if (ret
== SSL_ERROR_WANT_READ
)
281 return U_SSL_PENDING
;
283 ustream_ssl_error(us
, ret
);