3810d6acbb5498c65be563ae31f7cfc444767136
[oweals/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 <openssl/x509v3.h>
22 #include "ustream-ssl.h"
23 #include "ustream-internal.h"
24
25
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
32  * - CBC ciphers
33  *      aes128, aes256, 3DES(client only)
34  */
35
36 #ifdef WOLFSSL_SSL_H
37 # define top_ciphers                                                    \
38                                 "TLS13-CHACHA20-POLY1305-SHA256:"       \
39                                 "TLS13-AES128-GCM-SHA256:"              \
40                                 "TLS13-AES256-GCM-SHA384:"              \
41                                 ecdhe_ciphers
42 #else
43 # define tls13_ciphersuites     "TLS_CHACHA20_POLY1305_SHA256:"         \
44                                 "TLS_AES_128_GCM_SHA256:"               \
45                                 "TLS_AES_256_GCM_SHA384"
46
47 # define top_ciphers                                                    \
48                                 ecdhe_ciphers
49 #endif
50
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"
62
63 #define dhe_ciphers                                                     \
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:"                   \
69                                 "DHE-DES-CBC3-SHA"
70
71 #define non_pfs_aes                                                     \
72                                 "AES128-GCM-SHA256:"                    \
73                                 "AES256-GCM-SHA384:"                    \
74                                 "AES128-SHA:"                           \
75                                 "AES256-SHA"
76
77 #define server_cipher_list                                              \
78                                 top_ciphers ":"                         \
79                                 non_pfs_aes
80
81 #define client_cipher_list                                              \
82                                 top_ciphers ":"                         \
83                                 dhe_ciphers ":"                         \
84                                 non_pfs_aes ":"                         \
85                                 "DES-CBC3-SHA"
86
87 __hidden struct ustream_ssl_ctx *
88 __ustream_ssl_context_new(bool server)
89 {
90         const void *m;
91         SSL_CTX *c;
92
93 #if OPENSSL_VERSION_NUMBER < 0x10100000L
94         static bool _init = false;
95
96         if (!_init) {
97                 SSL_load_error_strings();
98                 SSL_library_init();
99                 _init = true;
100         }
101 # define TLS_server_method SSLv23_server_method
102 # define TLS_client_method SSLv23_client_method
103 #endif
104
105         if (server) {
106                 m = TLS_server_method();
107         } else
108                 m = TLS_client_method();
109
110         c = SSL_CTX_new((void *) m);
111         if (!c)
112                 return NULL;
113
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);
121 #endif
122         if (server) {
123 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
124                 SSL_CTX_set_min_proto_version(c, TLS1_2_VERSION);
125 #else
126                 SSL_CTX_set_options(c, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 |
127                                        SSL_OP_NO_TLSv1_1);
128 #endif
129                 SSL_CTX_set_cipher_list(c, server_cipher_list);
130         } else {
131                 SSL_CTX_set_cipher_list(c, client_cipher_list);
132         }
133         SSL_CTX_set_quiet_shutdown(c, 1);
134
135         return (void *) c;
136 }
137
138 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
139 {
140         int ret;
141
142         ret = SSL_CTX_load_verify_locations((void *) ctx, file, NULL);
143         if (ret < 1)
144                 return -1;
145
146         return 0;
147 }
148
149 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
150 {
151         int ret;
152
153         ret = SSL_CTX_use_certificate_chain_file((void *) ctx, file);
154         if (ret < 1)
155                 ret = SSL_CTX_use_certificate_file((void *) ctx, file, SSL_FILETYPE_ASN1);
156
157         if (ret < 1)
158                 return -1;
159
160         return 0;
161 }
162
163 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
164 {
165         int ret;
166
167         ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_PEM);
168         if (ret < 1)
169                 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_ASN1);
170
171         if (ret < 1)
172                 return -1;
173
174         return 0;
175 }
176
177 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
178 {
179         SSL_CTX_free((void *) ctx);
180 }
181
182 void __ustream_ssl_session_free(void *ssl)
183 {
184         SSL_shutdown(ssl);
185         SSL_free(ssl);
186 }
187
188 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
189 {
190         us->error = ret;
191         uloop_timeout_set(&us->error_timer, 0);
192 }
193
194 #ifndef CYASSL_OPENSSL_H_
195
196 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
197 {
198         int ret;
199
200         if (!us->peer_cn)
201                 return false;
202
203         ret = X509_check_host(cert, us->peer_cn, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS, NULL);
204         return ret == 1;
205 }
206
207
208 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
209 {
210         void *ssl = us->ssl;
211         X509 *cert;
212         int res;
213
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));
218                 return;
219         }
220
221         cert = SSL_get_peer_certificate(ssl);
222         if (!cert)
223                 return;
224
225         us->valid_cert = true;
226         us->valid_cn = ustream_ssl_verify_cn(us, cert);
227         X509_free(cert);
228 }
229
230 #endif
231
232 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
233 {
234         void *ssl = us->ssl;
235         int r;
236
237         if (us->server)
238                 r = SSL_accept(ssl);
239         else
240                 r = SSL_connect(ssl);
241
242         if (r == 1) {
243 #ifndef CYASSL_OPENSSL_H_
244                 ustream_ssl_verify_cert(us);
245 #endif
246                 return U_SSL_OK;
247         }
248
249         r = SSL_get_error(ssl, r);
250         if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
251                 return U_SSL_PENDING;
252
253         ustream_ssl_error(us, r);
254         return U_SSL_ERROR;
255 }
256
257 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
258 {
259         void *ssl = us->ssl;
260         int ret = SSL_write(ssl, buf, len);
261
262         if (ret < 0) {
263                 int err = SSL_get_error(ssl, ret);
264                 if (err == SSL_ERROR_WANT_WRITE)
265                         return 0;
266
267                 ustream_ssl_error(us, err);
268                 return -1;
269         }
270
271         return ret;
272 }
273
274 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
275 {
276         int ret = SSL_read(us->ssl, buf, len);
277
278         if (ret < 0) {
279                 ret = SSL_get_error(us->ssl, ret);
280                 if (ret == SSL_ERROR_WANT_READ)
281                         return U_SSL_PENDING;
282
283                 ustream_ssl_error(us, ret);
284                 return U_SSL_ERROR;
285         }
286
287         return ret;
288 }
289