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)
36 #define ecdhe_ciphers \
37 "ECDHE-ECDSA-CHACHA20-POLY1305:" \
38 "ECDHE-ECDSA-AES128-GCM-SHA256:" \
39 "ECDHE-ECDSA-AES256-GCM-SHA384:" \
40 "ECDHE-ECDSA-AES128-SHA:" \
41 "ECDHE-ECDSA-AES256-SHA:" \
42 "ECDHE-RSA-CHACHA20-POLY1305:" \
43 "ECDHE-RSA-AES128-GCM-SHA256:" \
44 "ECDHE-RSA-AES256-GCM-SHA384:" \
45 "ECDHE-RSA-AES128-SHA:" \
46 "ECDHE-RSA-AES256-SHA"
49 "DHE-RSA-CHACHA20-POLY1305:" \
50 "DHE-RSA-AES128-GCM-SHA256:" \
51 "DHE-RSA-AES256-GCM-SHA384:" \
52 "DHE-RSA-AES128-SHA:" \
53 "DHE-RSA-AES256-SHA:" \
57 "AES128-GCM-SHA256:" \
58 "AES256-GCM-SHA384:" \
62 #define server_cipher_list \
66 #define client_cipher_list \
72 __hidden
struct ustream_ssl_ctx
*
73 __ustream_ssl_context_new(bool server
)
78 #if OPENSSL_VERSION_NUMBER < 0x10100000L
79 static bool _init
= false;
82 SSL_load_error_strings();
86 # define TLS_server_method TLSv1_2_server_method
87 # define TLS_client_method SSLv23_client_method
91 m
= TLS_server_method();
93 m
= TLS_client_method();
95 c
= SSL_CTX_new((void *) m
);
99 SSL_CTX_set_verify(c
, SSL_VERIFY_NONE
, NULL
);
100 SSL_CTX_set_options(c
, SSL_OP_NO_COMPRESSION
| SSL_OP_SINGLE_ECDH_USE
|
101 SSL_OP_CIPHER_SERVER_PREFERENCE
);
102 #if defined(SSL_CTX_set_ecdh_auto) && OPENSSL_VERSION_NUMBER < 0x10100000L
103 SSL_CTX_set_ecdh_auto(c
, 1);
106 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
107 SSL_CTX_set_min_proto_version(c
, TLS1_2_VERSION
);
109 SSL_CTX_set_cipher_list(c
, server_cipher_list
);
111 SSL_CTX_set_cipher_list(c
, client_cipher_list
);
113 SSL_CTX_set_quiet_shutdown(c
, 1);
118 __hidden
int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
122 ret
= SSL_CTX_load_verify_locations((void *) ctx
, file
, NULL
);
129 __hidden
int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
133 ret
= SSL_CTX_use_certificate_chain_file((void *) ctx
, file
);
135 ret
= SSL_CTX_use_certificate_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
143 __hidden
int __ustream_ssl_set_key_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
147 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_PEM
);
149 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
157 __hidden
void __ustream_ssl_context_free(struct ustream_ssl_ctx
*ctx
)
159 SSL_CTX_free((void *) ctx
);
162 void __ustream_ssl_session_free(void *ssl
)
168 static void ustream_ssl_error(struct ustream_ssl
*us
, int ret
)
171 uloop_timeout_set(&us
->error_timer
, 0);
174 #ifndef CYASSL_OPENSSL_H_
176 static bool ustream_ssl_verify_cn(struct ustream_ssl
*us
, X509
*cert
)
183 ret
= X509_check_host(cert
, us
->peer_cn
, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
, NULL
);
188 static void ustream_ssl_verify_cert(struct ustream_ssl
*us
)
194 res
= SSL_get_verify_result(ssl
);
195 if (res
!= X509_V_OK
) {
196 if (us
->notify_verify_error
)
197 us
->notify_verify_error(us
, res
, X509_verify_cert_error_string(res
));
201 cert
= SSL_get_peer_certificate(ssl
);
205 us
->valid_cert
= true;
206 us
->valid_cn
= ustream_ssl_verify_cn(us
, cert
);
212 __hidden
enum ssl_conn_status
__ustream_ssl_connect(struct ustream_ssl
*us
)
220 r
= SSL_connect(ssl
);
223 #ifndef CYASSL_OPENSSL_H_
224 ustream_ssl_verify_cert(us
);
229 r
= SSL_get_error(ssl
, r
);
230 if (r
== SSL_ERROR_WANT_READ
|| r
== SSL_ERROR_WANT_WRITE
)
231 return U_SSL_PENDING
;
233 ustream_ssl_error(us
, r
);
237 __hidden
int __ustream_ssl_write(struct ustream_ssl
*us
, const char *buf
, int len
)
240 int ret
= SSL_write(ssl
, buf
, len
);
243 int err
= SSL_get_error(ssl
, ret
);
244 if (err
== SSL_ERROR_WANT_WRITE
)
247 ustream_ssl_error(us
, err
);
254 __hidden
int __ustream_ssl_read(struct ustream_ssl
*us
, char *buf
, int len
)
256 int ret
= SSL_read(us
->ssl
, buf
, len
);
259 ret
= SSL_get_error(us
->ssl
, ret
);
260 if (ret
== SSL_ERROR_WANT_READ
)
261 return U_SSL_PENDING
;
263 ustream_ssl_error(us
, ret
);