ustream-io-cyassl.c: fix client-mode connections
[oweals/openwrt-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 #include <openssl/x509v3.h>
24
25 /* Ciphersuite preference:
26  * - for server, no weak ciphers are used if you use an ECDSA key.
27  * - forward-secret (pfs), authenticated (AEAD) ciphers are at the top:
28  *      chacha20-poly1305, the fastest in software, 256-bits
29  *      aes128-gcm, 128-bits
30  *      aes256-gcm, 256-bits
31  * - key exchange: prefer ECDHE, then DHE (client only)
32  * - forward-secret ECDSA CBC ciphers (client-only)
33  * - forward-secret RSA CBC ciphers
34  * - non-pfs ciphers
35  *      aes128, aes256, 3DES(client only)
36  */
37
38 #ifdef WOLFSSL_SSL_H
39 # define top_ciphers                                                    \
40                                 "TLS13-CHACHA20-POLY1305-SHA256:"       \
41                                 "TLS13-AES128-GCM-SHA256:"              \
42                                 "TLS13-AES256-GCM-SHA384:"              \
43                                 ecdhe_aead_ciphers
44 #else
45 # define tls13_ciphersuites     "TLS_CHACHA20_POLY1305_SHA256:"         \
46                                 "TLS_AES_128_GCM_SHA256:"               \
47                                 "TLS_AES_256_GCM_SHA384"
48
49 # define top_ciphers                                                    \
50                                 ecdhe_aead_ciphers
51 #endif
52
53 #define ecdhe_aead_ciphers                                              \
54                                 "ECDHE-ECDSA-CHACHA20-POLY1305:"        \
55                                 "ECDHE-ECDSA-AES128-GCM-SHA256:"        \
56                                 "ECDHE-ECDSA-AES256-GCM-SHA384:"        \
57                                 "ECDHE-RSA-CHACHA20-POLY1305:"          \
58                                 "ECDHE-RSA-AES128-GCM-SHA256:"          \
59                                 "ECDHE-RSA-AES256-GCM-SHA384"
60
61 #define dhe_aead_ciphers                                                \
62                                 "DHE-RSA-CHACHA20-POLY1305:"            \
63                                 "DHE-RSA-AES128-GCM-SHA256:"            \
64                                 "DHE-RSA-AES256-GCM-SHA384"
65
66 #define ecdhe_ecdsa_cbc_ciphers                                         \
67                                 "ECDHE-ECDSA-AES128-SHA:"               \
68                                 "ECDHE-ECDSA-AES256-SHA"
69
70 #define ecdhe_rsa_cbc_ciphers                                           \
71                                 "ECDHE-RSA-AES128-SHA:"                 \
72                                 "ECDHE-RSA-AES256-SHA"
73
74 #define dhe_cbc_ciphers                                                 \
75                                 "DHE-RSA-AES128-SHA:"                   \
76                                 "DHE-RSA-AES256-SHA:"                   \
77                                 "DHE-DES-CBC3-SHA"
78
79 #define non_pfs_aes                                                     \
80                                 "AES128-GCM-SHA256:"                    \
81                                 "AES256-GCM-SHA384:"                    \
82                                 "AES128-SHA:"                           \
83                                 "AES256-SHA"
84
85 #define server_cipher_list                                              \
86                                 top_ciphers ":"                         \
87                                 ecdhe_rsa_cbc_ciphers ":"               \
88                                 non_pfs_aes
89
90 #define client_cipher_list                                              \
91                                 top_ciphers ":"                         \
92                                 dhe_aead_ciphers ":"                    \
93                                 ecdhe_ecdsa_cbc_ciphers ":"             \
94                                 ecdhe_rsa_cbc_ciphers ":"               \
95                                 dhe_cbc_ciphers ":"                     \
96                                 non_pfs_aes ":"                         \
97                                 "DES-CBC3-SHA"
98
99 __hidden struct ustream_ssl_ctx *
100 __ustream_ssl_context_new(bool server)
101 {
102         const void *m;
103         SSL_CTX *c;
104
105 #if OPENSSL_VERSION_NUMBER < 0x10100000L
106         static bool _init = false;
107
108         if (!_init) {
109                 SSL_load_error_strings();
110                 SSL_library_init();
111                 _init = true;
112         }
113 # define TLS_server_method SSLv23_server_method
114 # define TLS_client_method SSLv23_client_method
115 #endif
116
117         if (server) {
118                 m = TLS_server_method();
119         } else
120                 m = TLS_client_method();
121
122         c = SSL_CTX_new((void *) m);
123         if (!c)
124                 return NULL;
125
126         SSL_CTX_set_verify(c, SSL_VERIFY_NONE, NULL);
127         SSL_CTX_set_options(c, SSL_OP_NO_COMPRESSION | SSL_OP_SINGLE_ECDH_USE |
128                                SSL_OP_CIPHER_SERVER_PREFERENCE);
129 #if defined(SSL_CTX_set_ecdh_auto) && OPENSSL_VERSION_NUMBER < 0x10100000L
130         SSL_CTX_set_ecdh_auto(c, 1);
131 #elif OPENSSL_VERSION_NUMBER >= 0x10101000L
132         SSL_CTX_set_ciphersuites(c, tls13_ciphersuites);
133 #endif
134         if (server) {
135 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
136                 SSL_CTX_set_min_proto_version(c, TLS1_2_VERSION);
137 #else
138                 SSL_CTX_set_options(c, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 |
139                                        SSL_OP_NO_TLSv1_1);
140 #endif
141                 SSL_CTX_set_cipher_list(c, server_cipher_list);
142         } else {
143                 SSL_CTX_set_cipher_list(c, client_cipher_list);
144         }
145         SSL_CTX_set_quiet_shutdown(c, 1);
146
147         return (void *) c;
148 }
149
150 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
151 {
152         int ret;
153
154         ret = SSL_CTX_load_verify_locations((void *) ctx, file, NULL);
155         if (ret < 1)
156                 return -1;
157
158         return 0;
159 }
160
161 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
162 {
163         int ret;
164
165         ret = SSL_CTX_use_certificate_chain_file((void *) ctx, file);
166         if (ret < 1)
167                 ret = SSL_CTX_use_certificate_file((void *) ctx, file, SSL_FILETYPE_ASN1);
168
169         if (ret < 1)
170                 return -1;
171
172         return 0;
173 }
174
175 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
176 {
177         int ret;
178
179         ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_PEM);
180         if (ret < 1)
181                 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_ASN1);
182
183         if (ret < 1)
184                 return -1;
185
186         return 0;
187 }
188
189 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
190 {
191         SSL_CTX_free((void *) ctx);
192 }
193
194 void __ustream_ssl_session_free(void *ssl)
195 {
196         SSL_shutdown(ssl);
197         SSL_free(ssl);
198 }
199
200 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
201 {
202         us->error = ret;
203         uloop_timeout_set(&us->error_timer, 0);
204 }
205
206 #ifndef WOLFSSL_OPENSSL_H_
207
208 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
209 {
210         int ret;
211
212         if (!us->peer_cn)
213                 return false;
214
215         ret = X509_check_host(cert, us->peer_cn, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS, NULL);
216         return ret == 1;
217 }
218
219
220 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
221 {
222         void *ssl = us->ssl;
223         X509 *cert;
224         int res;
225
226         res = SSL_get_verify_result(ssl);
227         if (res != X509_V_OK) {
228                 if (us->notify_verify_error)
229                         us->notify_verify_error(us, res, X509_verify_cert_error_string(res));
230                 return;
231         }
232
233         cert = SSL_get_peer_certificate(ssl);
234         if (!cert)
235                 return;
236
237         us->valid_cert = true;
238         us->valid_cn = ustream_ssl_verify_cn(us, cert);
239         X509_free(cert);
240 }
241
242 #endif
243
244 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
245 {
246         void *ssl = us->ssl;
247         int r;
248
249         if (us->server)
250                 r = SSL_accept(ssl);
251         else
252                 r = SSL_connect(ssl);
253
254         if (r == 1) {
255 #ifndef WOLFSSL_OPENSSL_H_
256                 ustream_ssl_verify_cert(us);
257 #endif
258                 return U_SSL_OK;
259         }
260
261         r = SSL_get_error(ssl, r);
262         if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
263                 return U_SSL_PENDING;
264
265         ustream_ssl_error(us, r);
266         return U_SSL_ERROR;
267 }
268
269 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
270 {
271         void *ssl = us->ssl;
272         int ret = SSL_write(ssl, buf, len);
273
274         if (ret < 0) {
275                 int err = SSL_get_error(ssl, ret);
276                 if (err == SSL_ERROR_WANT_WRITE)
277                         return 0;
278
279                 ustream_ssl_error(us, err);
280                 return -1;
281         }
282
283         return ret;
284 }
285
286 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
287 {
288         int ret = SSL_read(us->ssl, buf, len);
289
290         if (ret < 0) {
291                 ret = SSL_get_error(us->ssl, ret);
292                 if (ret == SSL_ERROR_WANT_READ)
293                         return U_SSL_PENDING;
294
295                 ustream_ssl_error(us, ret);
296                 return U_SSL_ERROR;
297         }
298
299         return ret;
300 }
301