ustream-openssl: fix wolfSSL includes
[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 SSL_shutdown(ssl);
214 SSL_free(ssl);
215 }
216
217 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
218 {
219 us->error = ret;
220 uloop_timeout_set(&us->error_timer, 0);
221 }
222
223 #ifndef NO_X509_CHECK_HOST
224
225 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
226 {
227 int ret;
228
229 if (!us->peer_cn)
230 return false;
231
232 # ifndef WOLFSSL_OPENSSL_H_
233 ret = X509_check_host(cert, us->peer_cn, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS, NULL);
234 # else
235 ret = wolfSSL_X509_check_host(cert, us->peer_cn, 0, 0, NULL);
236 # endif
237 return ret == 1;
238 }
239
240 #endif
241
242 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
243 {
244 void *ssl = us->ssl;
245 X509 *cert;
246 int res;
247
248 res = SSL_get_verify_result(ssl);
249 if (res != X509_V_OK) {
250 if (us->notify_verify_error)
251 us->notify_verify_error(us, res, X509_verify_cert_error_string(res));
252 return;
253 }
254
255 cert = SSL_get_peer_certificate(ssl);
256 if (!cert)
257 return;
258
259 us->valid_cert = true;
260 #ifndef NO_X509_CHECK_HOST
261 us->valid_cn = ustream_ssl_verify_cn(us, cert);
262 #endif
263 X509_free(cert);
264 }
265
266
267 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
268 {
269 void *ssl = us->ssl;
270 int r;
271
272 ERR_clear_error();
273
274 if (us->server)
275 r = SSL_accept(ssl);
276 else
277 r = SSL_connect(ssl);
278
279 if (r == 1) {
280 ustream_ssl_verify_cert(us);
281 return U_SSL_OK;
282 }
283
284 r = SSL_get_error(ssl, r);
285 if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
286 return U_SSL_PENDING;
287
288 ustream_ssl_error(us, r);
289 return U_SSL_ERROR;
290 }
291
292 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
293 {
294 void *ssl = us->ssl;
295 int ret;
296
297 ERR_clear_error();
298
299 ret = SSL_write(ssl, buf, len);
300
301 if (ret < 0) {
302 int err = SSL_get_error(ssl, ret);
303 if (err == SSL_ERROR_WANT_WRITE)
304 return 0;
305
306 ustream_ssl_error(us, err);
307 return -1;
308 }
309
310 return ret;
311 }
312
313 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
314 {
315 int ret;
316
317 ERR_clear_error();
318
319 ret = SSL_read(us->ssl, buf, len);
320
321 if (ret < 0) {
322 ret = SSL_get_error(us->ssl, ret);
323 if (ret == SSL_ERROR_WANT_READ)
324 return U_SSL_PENDING;
325
326 ustream_ssl_error(us, ret);
327 return U_SSL_ERROR;
328 }
329
330 return ret;
331 }