ustream-openssl: fix BIO_method memory leak
[project/ustream-ssl.git] / ustream-openssl.c
1 /*
2 * ustream-ssl - library for SSL over ustream
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #include <string.h>
20 #include <ctype.h>
21 #include "ustream-ssl.h"
22 #include "ustream-internal.h"
23
24 #if !defined(HAVE_WOLFSSL)
25 #include <openssl/x509v3.h>
26 #endif
27
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
37 * - non-pfs ciphers
38 * aes128, aes256, 3DES(client only)
39 */
40
41 #ifdef WOLFSSL_SSL_H
42 # define top_ciphers \
43 "TLS13-CHACHA20-POLY1305-SHA256:" \
44 "TLS13-AES128-GCM-SHA256:" \
45 "TLS13-AES256-GCM-SHA384:" \
46 ecdhe_aead_ciphers
47 #else
48 # define tls13_ciphersuites "TLS_CHACHA20_POLY1305_SHA256:" \
49 "TLS_AES_128_GCM_SHA256:" \
50 "TLS_AES_256_GCM_SHA384"
51
52 # define top_ciphers \
53 ecdhe_aead_ciphers
54 #endif
55
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"
63
64 #define dhe_aead_ciphers \
65 "DHE-RSA-CHACHA20-POLY1305:" \
66 "DHE-RSA-AES128-GCM-SHA256:" \
67 "DHE-RSA-AES256-GCM-SHA384"
68
69 #define ecdhe_ecdsa_cbc_ciphers \
70 "ECDHE-ECDSA-AES128-SHA:" \
71 "ECDHE-ECDSA-AES256-SHA"
72
73 #define ecdhe_rsa_cbc_ciphers \
74 "ECDHE-RSA-AES128-SHA:" \
75 "ECDHE-RSA-AES256-SHA"
76
77 #define dhe_cbc_ciphers \
78 "DHE-RSA-AES128-SHA:" \
79 "DHE-RSA-AES256-SHA:" \
80 "DHE-DES-CBC3-SHA"
81
82 #define non_pfs_aes \
83 "AES128-GCM-SHA256:" \
84 "AES256-GCM-SHA384:" \
85 "AES128-SHA:" \
86 "AES256-SHA"
87
88 #define server_cipher_list \
89 top_ciphers ":" \
90 ecdhe_rsa_cbc_ciphers ":" \
91 non_pfs_aes
92
93 #define client_cipher_list \
94 top_ciphers ":" \
95 dhe_aead_ciphers ":" \
96 ecdhe_ecdsa_cbc_ciphers ":" \
97 ecdhe_rsa_cbc_ciphers ":" \
98 dhe_cbc_ciphers ":" \
99 non_pfs_aes ":" \
100 "DES-CBC3-SHA"
101
102 __hidden struct ustream_ssl_ctx *
103 __ustream_ssl_context_new(bool server)
104 {
105 const void *m;
106 SSL_CTX *c;
107
108 #if OPENSSL_VERSION_NUMBER < 0x10100000L
109 static bool _init = false;
110
111 if (!_init) {
112 SSL_load_error_strings();
113 SSL_library_init();
114 _init = true;
115 }
116 # ifndef TLS_server_method
117 # define TLS_server_method SSLv23_server_method
118 # endif
119 # ifndef TLS_client_method
120 # define TLS_client_method SSLv23_client_method
121 # endif
122 #endif
123
124 if (server) {
125 m = TLS_server_method();
126 } else
127 m = TLS_client_method();
128
129 c = SSL_CTX_new((void *) m);
130 if (!c)
131 return NULL;
132
133 SSL_CTX_set_verify(c, SSL_VERIFY_NONE, NULL);
134 SSL_CTX_set_options(c, SSL_OP_NO_COMPRESSION | SSL_OP_SINGLE_ECDH_USE |
135 SSL_OP_CIPHER_SERVER_PREFERENCE);
136 #if defined(SSL_CTX_set_ecdh_auto) && OPENSSL_VERSION_NUMBER < 0x10100000L
137 SSL_CTX_set_ecdh_auto(c, 1);
138 #elif OPENSSL_VERSION_NUMBER >= 0x10101000L
139 SSL_CTX_set_ciphersuites(c, tls13_ciphersuites);
140 #endif
141 if (server) {
142 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
143 SSL_CTX_set_min_proto_version(c, TLS1_2_VERSION);
144 #else
145 SSL_CTX_set_options(c, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 |
146 SSL_OP_NO_TLSv1_1);
147 #endif
148 SSL_CTX_set_cipher_list(c, server_cipher_list);
149 } else {
150 SSL_CTX_set_cipher_list(c, client_cipher_list);
151 }
152 SSL_CTX_set_quiet_shutdown(c, 1);
153
154 return (void *) c;
155 }
156
157 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
158 {
159 int ret;
160
161 ret = SSL_CTX_load_verify_locations((void *) ctx, file, NULL);
162 if (ret < 1)
163 return -1;
164
165 return 0;
166 }
167
168 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
169 {
170 int ret;
171
172 ret = SSL_CTX_use_certificate_chain_file((void *) ctx, file);
173 if (ret < 1)
174 ret = SSL_CTX_use_certificate_file((void *) ctx, file, SSL_FILETYPE_ASN1);
175
176 if (ret < 1)
177 return -1;
178
179 return 0;
180 }
181
182 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
183 {
184 int ret;
185
186 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_PEM);
187 if (ret < 1)
188 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_ASN1);
189
190 if (ret < 1)
191 return -1;
192
193 return 0;
194 }
195
196 __hidden int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx *ctx, const char *ciphers)
197 {
198 int ret = SSL_CTX_set_cipher_list((void *) ctx, ciphers);
199
200 if (ret == 0)
201 return -1;
202
203 return 0;
204 }
205
206 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
207 {
208 SSL_CTX_free((void *) ctx);
209 }
210
211 void __ustream_ssl_session_free(void *ssl)
212 {
213 BIO *bio = SSL_get_wbio(ssl);
214 struct bio_ctx *ctx = BIO_get_data(bio);
215
216 SSL_shutdown(ssl);
217 SSL_free(ssl);
218 if (ctx) {
219 BIO_meth_free(ctx->meth);
220 free(ctx);
221 }
222 }
223
224 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
225 {
226 us->error = ret;
227 uloop_timeout_set(&us->error_timer, 0);
228 }
229
230 #ifndef NO_X509_CHECK_HOST
231
232 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
233 {
234 int ret;
235
236 if (!us->peer_cn)
237 return false;
238
239 # ifndef WOLFSSL_OPENSSL_H_
240 ret = X509_check_host(cert, us->peer_cn, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS, NULL);
241 # else
242 ret = wolfSSL_X509_check_host(cert, us->peer_cn, 0, 0, NULL);
243 # endif
244 return ret == 1;
245 }
246
247 #endif
248
249 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
250 {
251 void *ssl = us->ssl;
252 X509 *cert;
253 int res;
254
255 res = SSL_get_verify_result(ssl);
256 if (res != X509_V_OK) {
257 if (us->notify_verify_error)
258 us->notify_verify_error(us, res, X509_verify_cert_error_string(res));
259 return;
260 }
261
262 cert = SSL_get_peer_certificate(ssl);
263 if (!cert)
264 return;
265
266 us->valid_cert = true;
267 #ifndef NO_X509_CHECK_HOST
268 us->valid_cn = ustream_ssl_verify_cn(us, cert);
269 #endif
270 X509_free(cert);
271 }
272
273
274 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
275 {
276 void *ssl = us->ssl;
277 int r;
278
279 ERR_clear_error();
280
281 if (us->server)
282 r = SSL_accept(ssl);
283 else
284 r = SSL_connect(ssl);
285
286 if (r == 1) {
287 ustream_ssl_verify_cert(us);
288 return U_SSL_OK;
289 }
290
291 r = SSL_get_error(ssl, r);
292 if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
293 return U_SSL_PENDING;
294
295 ustream_ssl_error(us, r);
296 return U_SSL_ERROR;
297 }
298
299 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
300 {
301 void *ssl = us->ssl;
302 int ret;
303
304 ERR_clear_error();
305
306 ret = SSL_write(ssl, buf, len);
307
308 if (ret < 0) {
309 int err = SSL_get_error(ssl, ret);
310 if (err == SSL_ERROR_WANT_WRITE)
311 return 0;
312
313 ustream_ssl_error(us, err);
314 return -1;
315 }
316
317 return ret;
318 }
319
320 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
321 {
322 int ret;
323
324 ERR_clear_error();
325
326 ret = SSL_read(us->ssl, buf, len);
327
328 if (ret < 0) {
329 ret = SSL_get_error(us->ssl, ret);
330 if (ret == SSL_ERROR_WANT_READ)
331 return U_SSL_PENDING;
332
333 ustream_ssl_error(us, ret);
334 return U_SSL_ERROR;
335 }
336
337 return ret;
338 }