openssl, wolfssl: match mbedTLS ciphersuite list
[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 <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 #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"
47
48 #define dhe_ciphers \
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:" \
54 "DHE-DES-CBC3-SHA"
55
56 #define non_pfs_aes \
57 "AES128-GCM-SHA256:" \
58 "AES256-GCM-SHA384:" \
59 "AES128-SHA:" \
60 "AES256-SHA"
61
62 #define server_cipher_list \
63 ecdhe_ciphers ":" \
64 non_pfs_aes
65
66 #define client_cipher_list \
67 ecdhe_ciphers ":" \
68 dhe_ciphers ":" \
69 non_pfs_aes ":" \
70 "DES-CBC3-SHA"
71
72 __hidden struct ustream_ssl_ctx *
73 __ustream_ssl_context_new(bool server)
74 {
75 const void *m;
76 SSL_CTX *c;
77
78 #if OPENSSL_VERSION_NUMBER < 0x10100000L
79 static bool _init = false;
80
81 if (!_init) {
82 SSL_load_error_strings();
83 SSL_library_init();
84 _init = true;
85 }
86 # define TLS_server_method TLSv1_2_server_method
87 # define TLS_client_method SSLv23_client_method
88 #endif
89
90 if (server) {
91 m = TLS_server_method();
92 } else
93 m = TLS_client_method();
94
95 c = SSL_CTX_new((void *) m);
96 if (!c)
97 return NULL;
98
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);
104 #endif
105 if (server) {
106 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
107 SSL_CTX_set_min_proto_version(c, TLS1_2_VERSION);
108 #endif
109 SSL_CTX_set_cipher_list(c, server_cipher_list);
110 } else {
111 SSL_CTX_set_cipher_list(c, client_cipher_list);
112 }
113 SSL_CTX_set_quiet_shutdown(c, 1);
114
115 return (void *) c;
116 }
117
118 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
119 {
120 int ret;
121
122 ret = SSL_CTX_load_verify_locations((void *) ctx, file, NULL);
123 if (ret < 1)
124 return -1;
125
126 return 0;
127 }
128
129 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
130 {
131 int ret;
132
133 ret = SSL_CTX_use_certificate_chain_file((void *) ctx, file);
134 if (ret < 1)
135 ret = SSL_CTX_use_certificate_file((void *) ctx, file, SSL_FILETYPE_ASN1);
136
137 if (ret < 1)
138 return -1;
139
140 return 0;
141 }
142
143 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
144 {
145 int ret;
146
147 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_PEM);
148 if (ret < 1)
149 ret = SSL_CTX_use_PrivateKey_file((void *) ctx, file, SSL_FILETYPE_ASN1);
150
151 if (ret < 1)
152 return -1;
153
154 return 0;
155 }
156
157 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
158 {
159 SSL_CTX_free((void *) ctx);
160 }
161
162 void __ustream_ssl_session_free(void *ssl)
163 {
164 SSL_shutdown(ssl);
165 SSL_free(ssl);
166 }
167
168 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
169 {
170 us->error = ret;
171 uloop_timeout_set(&us->error_timer, 0);
172 }
173
174 #ifndef CYASSL_OPENSSL_H_
175
176 static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
177 {
178 int ret;
179
180 if (!us->peer_cn)
181 return false;
182
183 ret = X509_check_host(cert, us->peer_cn, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS, NULL);
184 return ret == 1;
185 }
186
187
188 static void ustream_ssl_verify_cert(struct ustream_ssl *us)
189 {
190 void *ssl = us->ssl;
191 X509 *cert;
192 int res;
193
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));
198 return;
199 }
200
201 cert = SSL_get_peer_certificate(ssl);
202 if (!cert)
203 return;
204
205 us->valid_cert = true;
206 us->valid_cn = ustream_ssl_verify_cn(us, cert);
207 X509_free(cert);
208 }
209
210 #endif
211
212 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
213 {
214 void *ssl = us->ssl;
215 int r;
216
217 if (us->server)
218 r = SSL_accept(ssl);
219 else
220 r = SSL_connect(ssl);
221
222 if (r == 1) {
223 #ifndef CYASSL_OPENSSL_H_
224 ustream_ssl_verify_cert(us);
225 #endif
226 return U_SSL_OK;
227 }
228
229 r = SSL_get_error(ssl, r);
230 if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
231 return U_SSL_PENDING;
232
233 ustream_ssl_error(us, r);
234 return U_SSL_ERROR;
235 }
236
237 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
238 {
239 void *ssl = us->ssl;
240 int ret = SSL_write(ssl, buf, len);
241
242 if (ret < 0) {
243 int err = SSL_get_error(ssl, ret);
244 if (err == SSL_ERROR_WANT_WRITE)
245 return 0;
246
247 ustream_ssl_error(us, err);
248 return -1;
249 }
250
251 return ret;
252 }
253
254 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
255 {
256 int ret = SSL_read(us->ssl, buf, len);
257
258 if (ret < 0) {
259 ret = SSL_get_error(us->ssl, ret);
260 if (ret == SSL_ERROR_WANT_READ)
261 return U_SSL_PENDING;
262
263 ustream_ssl_error(us, ret);
264 return U_SSL_ERROR;
265 }
266
267 return ret;
268 }
269