]>
Commit | Line | Data |
---|---|---|
db137867 | 1 | /* |
fe037171 | 2 | * librb: a library used by ircd-ratbox and other things |
73470f0b | 3 | * openssl.c: OpenSSL backend |
db137867 AC |
4 | * |
5 | * Copyright (C) 2007-2008 ircd-ratbox development team | |
6 | * Copyright (C) 2007-2008 Aaron Sethman <androsyn@ratbox.org> | |
73470f0b | 7 | * Copyright (C) 2015-2016 Aaron Jones <aaronmdjones@gmail.com> |
db137867 AC |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
55abcbb2 | 18 | * |
db137867 AC |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
22 | * USA | |
23 | * | |
db137867 AC |
24 | */ |
25 | ||
fe037171 EM |
26 | #include <librb_config.h> |
27 | #include <rb_lib.h> | |
db137867 AC |
28 | |
29 | #ifdef HAVE_OPENSSL | |
30 | ||
31 | #include <commio-int.h> | |
32 | #include <commio-ssl.h> | |
3ae24413 | 33 | |
73470f0b AJ |
34 | #include "openssl_ratbox.h" |
35 | ||
36 | typedef enum | |
37 | { | |
38 | RB_FD_TLS_DIRECTION_IN = 0, | |
39 | RB_FD_TLS_DIRECTION_OUT = 1 | |
40 | } rb_fd_tls_direction; | |
41 | ||
42 | #define SSL_P(x) ((SSL *)((x)->ssl)) | |
43 | ||
44 | ||
45 | ||
46 | static SSL_CTX *ssl_ctx = NULL; | |
47 | ||
48 | struct ssl_connect | |
49 | { | |
50 | CNCB *callback; | |
51 | void *data; | |
52 | int timeout; | |
53 | }; | |
54 | ||
55 | static const char *rb_ssl_strerror(unsigned long); | |
56 | static void rb_ssl_connect_realcb(rb_fde_t *, int, struct ssl_connect *); | |
57 | ||
db137867 | 58 | |
b1f05493 | 59 | |
5325f9d2 | 60 | /* |
73470f0b | 61 | * Internal OpenSSL-specific code |
5325f9d2 | 62 | */ |
db137867 | 63 | |
3202e249 | 64 | static unsigned long |
73470f0b | 65 | rb_ssl_last_err(void) |
db137867 | 66 | { |
73470f0b | 67 | unsigned long err_saved, err = 0; |
3202e249 | 68 | |
73470f0b AJ |
69 | while((err_saved = ERR_get_error()) != 0) |
70 | err = err_saved; | |
db137867 AC |
71 | |
72 | return err; | |
73 | } | |
74 | ||
73470f0b AJ |
75 | static void |
76 | rb_ssl_init_fd(rb_fde_t *const F, const rb_fd_tls_direction dir) | |
db137867 | 77 | { |
73470f0b AJ |
78 | (void) rb_ssl_last_err(); |
79 | ||
80 | F->ssl = SSL_new(ssl_ctx); | |
db137867 | 81 | |
73470f0b | 82 | if(F->ssl == NULL) |
db137867 | 83 | { |
73470f0b AJ |
84 | rb_lib_log("%s: SSL_new: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); |
85 | rb_close(F); | |
86 | return; | |
db137867 | 87 | } |
db137867 | 88 | |
73470f0b AJ |
89 | switch(dir) |
90 | { | |
91 | case RB_FD_TLS_DIRECTION_IN: | |
92 | SSL_set_accept_state(SSL_P(F)); | |
93 | break; | |
94 | case RB_FD_TLS_DIRECTION_OUT: | |
95 | SSL_set_connect_state(SSL_P(F)); | |
96 | break; | |
97 | } | |
c2ac22cc | 98 | |
73470f0b | 99 | SSL_set_fd(SSL_P(F), rb_get_fd(F)); |
c2ac22cc VY |
100 | } |
101 | ||
db137867 | 102 | static void |
8679c0fe | 103 | rb_ssl_accept_common(rb_fde_t *const F, void *const data __attribute__((unused))) |
db137867 | 104 | { |
73470f0b | 105 | lrb_assert(F != NULL); |
73d6283c | 106 | lrb_assert(F->accept != NULL); |
73470f0b AJ |
107 | lrb_assert(F->accept->callback != NULL); |
108 | lrb_assert(F->ssl != NULL); | |
db137867 | 109 | |
73470f0b | 110 | (void) rb_ssl_last_err(); |
db137867 | 111 | |
73470f0b AJ |
112 | int ret = SSL_do_handshake(SSL_P(F)); |
113 | int err = SSL_get_error(SSL_P(F), ret); | |
114 | ||
115 | if(ret == 1) | |
c2ac22cc | 116 | { |
c2ac22cc | 117 | F->handshake_count++; |
73470f0b AJ |
118 | |
119 | rb_settimeout(F, 0, NULL, NULL); | |
120 | rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL); | |
121 | ||
122 | struct acceptdata *const ad = F->accept; | |
123 | F->accept = NULL; | |
124 | ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data); | |
125 | rb_free(ad); | |
126 | ||
127 | return; | |
128 | } | |
129 | if(ret == -1 && err == SSL_ERROR_WANT_READ) | |
130 | { | |
131 | rb_setselect(F, RB_SELECT_READ, rb_ssl_accept_common, NULL); | |
132 | return; | |
133 | } | |
134 | if(ret == -1 && err == SSL_ERROR_WANT_WRITE) | |
135 | { | |
136 | rb_setselect(F, RB_SELECT_WRITE, rb_ssl_accept_common, NULL); | |
137 | return; | |
3202e249 | 138 | } |
c2ac22cc | 139 | |
73470f0b AJ |
140 | errno = EIO; |
141 | F->ssl_errno = (unsigned long) err; | |
142 | F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data); | |
c2ac22cc VY |
143 | } |
144 | ||
db137867 | 145 | static void |
73470f0b | 146 | rb_ssl_connect_common(rb_fde_t *const F, void *const data) |
db137867 | 147 | { |
73470f0b AJ |
148 | lrb_assert(F != NULL); |
149 | lrb_assert(F->ssl != NULL); | |
db137867 | 150 | |
73470f0b | 151 | (void) rb_ssl_last_err(); |
3202e249 | 152 | |
73470f0b AJ |
153 | int ret = SSL_do_handshake(SSL_P(F)); |
154 | int err = SSL_get_error(SSL_P(F), ret); | |
db137867 | 155 | |
73470f0b AJ |
156 | if(ret == 1) |
157 | { | |
158 | F->handshake_count++; | |
db137867 | 159 | |
73470f0b AJ |
160 | rb_settimeout(F, 0, NULL, NULL); |
161 | rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL); | |
c2ac22cc | 162 | |
73470f0b AJ |
163 | rb_ssl_connect_realcb(F, RB_OK, data); |
164 | ||
165 | return; | |
166 | } | |
167 | if(ret == -1 && err == SSL_ERROR_WANT_READ) | |
db137867 | 168 | { |
73470f0b AJ |
169 | rb_setselect(F, RB_SELECT_READ, rb_ssl_connect_common, data); |
170 | return; | |
db137867 | 171 | } |
73470f0b | 172 | if(ret == -1 && err == SSL_ERROR_WANT_WRITE) |
db137867 | 173 | { |
73470f0b AJ |
174 | rb_setselect(F, RB_SELECT_WRITE, rb_ssl_connect_common, data); |
175 | return; | |
db137867 | 176 | } |
73470f0b AJ |
177 | |
178 | errno = EIO; | |
179 | F->ssl_errno = (unsigned long) err; | |
180 | rb_ssl_connect_realcb(F, RB_ERROR_SSL, data); | |
db137867 AC |
181 | } |
182 | ||
73470f0b AJ |
183 | static const char * |
184 | rb_ssl_strerror(const unsigned long err) | |
c2ac22cc | 185 | { |
73470f0b | 186 | static char errbuf[512]; |
c2ac22cc | 187 | |
73470f0b | 188 | ERR_error_string_n(err, errbuf, sizeof errbuf); |
c2ac22cc | 189 | |
73470f0b | 190 | return errbuf; |
c2ac22cc VY |
191 | } |
192 | ||
73470f0b | 193 | static int |
8679c0fe | 194 | verify_accept_all_cb(const int preverify_ok __attribute__((unused)), X509_STORE_CTX *const x509_ctx __attribute__((unused))) |
db137867 | 195 | { |
73470f0b | 196 | return 1; |
db137867 AC |
197 | } |
198 | ||
199 | static ssize_t | |
73470f0b | 200 | rb_ssl_read_or_write(const int r_or_w, rb_fde_t *const F, void *const rbuf, const void *const wbuf, const size_t count) |
db137867 AC |
201 | { |
202 | ssize_t ret; | |
203 | unsigned long err; | |
73470f0b AJ |
204 | |
205 | (void) rb_ssl_last_err(); | |
db137867 AC |
206 | |
207 | if(r_or_w == 0) | |
73470f0b | 208 | ret = (ssize_t) SSL_read(SSL_P(F), rbuf, (int)count); |
db137867 | 209 | else |
73470f0b | 210 | ret = (ssize_t) SSL_write(SSL_P(F), wbuf, (int)count); |
db137867 AC |
211 | |
212 | if(ret < 0) | |
213 | { | |
73470f0b | 214 | switch(SSL_get_error(SSL_P(F), ret)) |
db137867 AC |
215 | { |
216 | case SSL_ERROR_WANT_READ: | |
217 | errno = EAGAIN; | |
218 | return RB_RW_SSL_NEED_READ; | |
219 | case SSL_ERROR_WANT_WRITE: | |
220 | errno = EAGAIN; | |
221 | return RB_RW_SSL_NEED_WRITE; | |
222 | case SSL_ERROR_ZERO_RETURN: | |
223 | return 0; | |
224 | case SSL_ERROR_SYSCALL: | |
73470f0b | 225 | err = rb_ssl_last_err(); |
db137867 AC |
226 | if(err == 0) |
227 | { | |
228 | F->ssl_errno = 0; | |
229 | return RB_RW_IO_ERROR; | |
230 | } | |
231 | break; | |
232 | default: | |
73470f0b | 233 | err = rb_ssl_last_err(); |
db137867 AC |
234 | break; |
235 | } | |
73470f0b | 236 | |
db137867 AC |
237 | F->ssl_errno = err; |
238 | if(err > 0) | |
239 | { | |
240 | errno = EIO; /* not great but... */ | |
241 | return RB_RW_SSL_ERROR; | |
242 | } | |
243 | return RB_RW_IO_ERROR; | |
244 | } | |
245 | return ret; | |
246 | } | |
247 | ||
73470f0b AJ |
248 | static int |
249 | make_certfp(X509 *const cert, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) | |
db137867 | 250 | { |
73470f0b AJ |
251 | unsigned int hashlen = 0; |
252 | const EVP_MD *md_type = NULL; | |
253 | const ASN1_ITEM *item = NULL; | |
254 | void *data = NULL; | |
db137867 | 255 | |
73470f0b AJ |
256 | switch(method) |
257 | { | |
258 | case RB_SSL_CERTFP_METH_CERT_SHA1: | |
259 | hashlen = RB_SSL_CERTFP_LEN_SHA1; | |
260 | md_type = EVP_sha1(); | |
261 | item = ASN1_ITEM_rptr(X509); | |
262 | data = cert; | |
263 | break; | |
264 | case RB_SSL_CERTFP_METH_CERT_SHA256: | |
265 | hashlen = RB_SSL_CERTFP_LEN_SHA256; | |
266 | md_type = EVP_sha256(); | |
267 | item = ASN1_ITEM_rptr(X509); | |
268 | data = cert; | |
269 | break; | |
270 | case RB_SSL_CERTFP_METH_CERT_SHA512: | |
271 | hashlen = RB_SSL_CERTFP_LEN_SHA512; | |
272 | md_type = EVP_sha512(); | |
273 | item = ASN1_ITEM_rptr(X509); | |
274 | data = cert; | |
275 | break; | |
276 | case RB_SSL_CERTFP_METH_SPKI_SHA256: | |
277 | hashlen = RB_SSL_CERTFP_LEN_SHA256; | |
278 | md_type = EVP_sha256(); | |
279 | item = ASN1_ITEM_rptr(X509_PUBKEY); | |
280 | data = X509_get_X509_PUBKEY(cert); | |
281 | break; | |
282 | case RB_SSL_CERTFP_METH_SPKI_SHA512: | |
283 | hashlen = RB_SSL_CERTFP_LEN_SHA512; | |
284 | md_type = EVP_sha512(); | |
285 | item = ASN1_ITEM_rptr(X509_PUBKEY); | |
286 | data = X509_get_X509_PUBKEY(cert); | |
287 | break; | |
288 | default: | |
289 | return 0; | |
290 | } | |
db137867 | 291 | |
73470f0b | 292 | if(ASN1_item_digest(item, md_type, data, certfp, &hashlen) != 1) |
35cd2993 AJ |
293 | { |
294 | rb_lib_log("%s: ASN1_item_digest: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
73470f0b | 295 | return 0; |
35cd2993 | 296 | } |
73470f0b AJ |
297 | |
298 | return (int) hashlen; | |
7247337a JT |
299 | } |
300 | ||
73470f0b AJ |
301 | |
302 | ||
303 | /* | |
304 | * External OpenSSL-specific code | |
305 | */ | |
306 | ||
307 | void | |
308 | rb_ssl_shutdown(rb_fde_t *const F) | |
918d73d5 | 309 | { |
73470f0b AJ |
310 | if(F == NULL || F->ssl == NULL) |
311 | return; | |
918d73d5 | 312 | |
73470f0b AJ |
313 | (void) rb_ssl_last_err(); |
314 | ||
315 | for(int i = 0; i < 4; i++) | |
316 | { | |
317 | int ret = SSL_shutdown(SSL_P(F)); | |
318 | int err = SSL_get_error(SSL_P(F), ret); | |
319 | ||
320 | if(ret >= 0 || (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)) | |
321 | break; | |
322 | } | |
323 | ||
324 | SSL_free(SSL_P(F)); | |
325 | F->ssl = NULL; | |
918d73d5 JT |
326 | } |
327 | ||
db137867 AC |
328 | int |
329 | rb_init_ssl(void) | |
330 | { | |
73470f0b AJ |
331 | #ifndef LRB_SSL_NO_EXPLICIT_INIT |
332 | (void) SSL_library_init(); | |
cf12678b | 333 | SSL_load_error_strings(); |
73470f0b | 334 | #endif |
a4c8c827 | 335 | |
73470f0b | 336 | rb_lib_log("%s: OpenSSL backend initialised", __func__); |
cf12678b AJ |
337 | return 1; |
338 | } | |
339 | ||
340 | int | |
73470f0b AJ |
341 | rb_setup_ssl_server(const char *const certfile, const char *keyfile, |
342 | const char *const dhfile, const char *cipherlist) | |
cf12678b | 343 | { |
0fe9dd41 | 344 | if(certfile == NULL) |
cf12678b | 345 | { |
73470f0b | 346 | rb_lib_log("%s: no certificate file specified", __func__); |
cf12678b AJ |
347 | return 0; |
348 | } | |
349 | ||
350 | if(keyfile == NULL) | |
0fe9dd41 | 351 | keyfile = certfile; |
cf12678b | 352 | |
73470f0b AJ |
353 | if(cipherlist == NULL) |
354 | cipherlist = rb_default_ciphers; | |
355 | ||
356 | ||
357 | (void) rb_ssl_last_err(); | |
92404a1a | 358 | |
cf12678b | 359 | #ifdef LRB_HAVE_TLS_METHOD_API |
73470f0b | 360 | SSL_CTX *const ssl_ctx_new = SSL_CTX_new(TLS_method()); |
cf12678b | 361 | #else |
73470f0b | 362 | SSL_CTX *const ssl_ctx_new = SSL_CTX_new(SSLv23_method()); |
cf12678b | 363 | #endif |
73470f0b AJ |
364 | |
365 | if(ssl_ctx_new == NULL) | |
db137867 | 366 | { |
73470f0b | 367 | rb_lib_log("%s: SSL_CTX_new: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); |
92404a1a | 368 | return 0; |
db137867 | 369 | } |
a4c8c827 | 370 | |
73470f0b | 371 | if(SSL_CTX_use_certificate_chain_file(ssl_ctx_new, certfile) != 1) |
92404a1a | 372 | { |
73470f0b AJ |
373 | rb_lib_log("%s: SSL_CTX_use_certificate_chain_file ('%s'): %s", __func__, certfile, |
374 | rb_ssl_strerror(rb_ssl_last_err())); | |
add3f90b | 375 | |
73470f0b | 376 | SSL_CTX_free(ssl_ctx_new); |
92404a1a AJ |
377 | return 0; |
378 | } | |
a4c8c827 | 379 | |
73470f0b AJ |
380 | if(SSL_CTX_use_PrivateKey_file(ssl_ctx_new, keyfile, SSL_FILETYPE_PEM) != 1) |
381 | { | |
382 | rb_lib_log("%s: SSL_CTX_use_PrivateKey_file ('%s'): %s", __func__, keyfile, | |
383 | rb_ssl_strerror(rb_ssl_last_err())); | |
a4c8c827 | 384 | |
73470f0b AJ |
385 | SSL_CTX_free(ssl_ctx_new); |
386 | return 0; | |
387 | } | |
388 | ||
389 | if(dhfile == NULL) | |
390 | { | |
391 | rb_lib_log("%s: no DH parameters file specified", __func__); | |
392 | } | |
393 | else | |
394 | { | |
8e9a7418 | 395 | BIO *const dhf = BIO_new_file(dhfile, "r"); |
396 | #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) | |
397 | EVP_PKEY *dhp = NULL; | |
398 | #else | |
73470f0b | 399 | DH *dhp = NULL; |
8e9a7418 | 400 | #endif |
73470f0b AJ |
401 | |
402 | if(dhf == NULL) | |
403 | { | |
8e9a7418 | 404 | rb_lib_log("%s: BIO_new_file ('%s'): %s", __func__, dhfile, |
405 | rb_ssl_strerror(rb_ssl_last_err())); | |
406 | } | |
407 | #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) | |
408 | else if(PEM_read_bio_Parameters(dhf, &dhp) == NULL) | |
409 | { | |
410 | rb_lib_log("%s: PEM_read_bio_Parameters ('%s'): %s", __func__, dhfile, | |
411 | rb_ssl_strerror(rb_ssl_last_err())); | |
73470f0b | 412 | } |
8e9a7418 | 413 | #else |
414 | else if(PEM_read_bio_DHparams(dhf, &dhp, NULL, NULL) == NULL) | |
73470f0b | 415 | { |
8e9a7418 | 416 | rb_lib_log("%s: PEM_read_bio_DHparams ('%s'): %s", __func__, dhfile, |
73470f0b | 417 | rb_ssl_strerror(rb_ssl_last_err())); |
73470f0b | 418 | } |
8e9a7418 | 419 | #endif |
73470f0b AJ |
420 | else |
421 | { | |
8e9a7418 | 422 | #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) |
423 | if(SSL_CTX_set0_tmp_dh_pkey(ssl_ctx_new, dhp) != 1) | |
424 | { | |
425 | rb_lib_log("%s: SSL_CTX_set0_tmp_dh_pkey ('%s'): %s", __func__, dhfile, | |
426 | rb_ssl_strerror(rb_ssl_last_err())); | |
427 | EVP_PKEY_free(dhp); | |
428 | } | |
429 | #else | |
73470f0b AJ |
430 | SSL_CTX_set_tmp_dh(ssl_ctx_new, dhp); |
431 | DH_free(dhp); | |
8e9a7418 | 432 | #endif |
73470f0b | 433 | } |
73470f0b | 434 | |
8e9a7418 | 435 | BIO_free(dhf); |
436 | } | |
a90f22c9 AJ |
437 | |
438 | int ret_old = SSL_CTX_set_cipher_list(ssl_ctx_new, cipherlist); | |
439 | ||
440 | if (ret_old != 1) | |
441 | { | |
442 | (void) rb_lib_log("%s: no valid old-style ciphersuites found " | |
443 | "in ssl_cipher_list; will use defaults", | |
444 | __func__); | |
445 | ||
446 | ret_old = SSL_CTX_set_cipher_list(ssl_ctx_new, rb_default_ciphers); | |
447 | } | |
448 | ||
449 | #ifndef LRB_HAVE_TLS13 | |
450 | int ret_new = 0; | |
451 | #else | |
452 | int ret_new = SSL_CTX_set_ciphersuites(ssl_ctx_new, cipherlist); | |
453 | ||
454 | if (ret_new != 1) | |
73470f0b | 455 | { |
a90f22c9 AJ |
456 | (void) rb_lib_log("%s: no valid new-style ciphersuites found " |
457 | "in ssl_cipher_list; will use defaults", | |
458 | __func__); | |
459 | ||
460 | ret_new = SSL_CTX_set_ciphersuites(ssl_ctx_new, rb_default_ciphers); | |
461 | } | |
462 | #endif | |
463 | ||
464 | if (ret_old != 1 && ret_new != 1) | |
465 | { | |
466 | rb_lib_log("%s: could not configure any ciphers", __func__); | |
73470f0b AJ |
467 | SSL_CTX_free(ssl_ctx_new); |
468 | return 0; | |
469 | } | |
470 | ||
a90f22c9 | 471 | |
73470f0b AJ |
472 | SSL_CTX_set_session_cache_mode(ssl_ctx_new, SSL_SESS_CACHE_OFF); |
473 | SSL_CTX_set_verify(ssl_ctx_new, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, verify_accept_all_cb); | |
474 | ||
475 | #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS | |
476 | (void) SSL_CTX_clear_options(ssl_ctx_new, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); | |
92404a1a | 477 | #endif |
a4c8c827 | 478 | |
73470f0b AJ |
479 | #ifndef LRB_HAVE_TLS_METHOD_API |
480 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); | |
92404a1a | 481 | #endif |
b6e799f5 | 482 | |
92404a1a | 483 | #ifdef SSL_OP_NO_TICKET |
73470f0b | 484 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_TICKET); |
92404a1a | 485 | #endif |
b1f05493 | 486 | |
92404a1a | 487 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
73470f0b | 488 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_CIPHER_SERVER_PREFERENCE); |
92404a1a | 489 | #endif |
b1f05493 | 490 | |
73470f0b AJ |
491 | #ifdef SSL_OP_SINGLE_DH_USE |
492 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_DH_USE); | |
493 | #endif | |
cf12678b | 494 | |
73470f0b AJ |
495 | #ifdef SSL_OP_SINGLE_ECDH_USE |
496 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_ECDH_USE); | |
cf12678b | 497 | #endif |
db137867 | 498 | |
92404a1a | 499 | #ifdef LRB_HAVE_TLS_ECDH_AUTO |
73470f0b | 500 | (void) SSL_CTX_set_ecdh_auto(ssl_ctx_new, 1); |
92404a1a | 501 | #endif |
6b6a5799 | 502 | |
73470f0b AJ |
503 | #ifdef LRB_HAVE_TLS_SET_CURVES |
504 | (void) SSL_CTX_set1_curves_list(ssl_ctx_new, rb_default_curves); | |
505 | #else | |
506 | # if (OPENSSL_VERSION_NUMBER >= 0x10000000L) && !defined(OPENSSL_NO_ECDH) && defined(NID_secp384r1) | |
507 | EC_KEY *const ec_key = EC_KEY_new_by_curve_name(NID_secp384r1); | |
508 | if(ec_key != NULL) | |
509 | { | |
510 | SSL_CTX_set_tmp_ecdh(ssl_ctx_new, ec_key); | |
511 | EC_KEY_free(ec_key); | |
cf12678b | 512 | } |
73470f0b AJ |
513 | else |
514 | rb_lib_log("%s: EC_KEY_new_by_curve_name failed; will not enable ECDHE- ciphers", __func__); | |
515 | # else | |
516 | rb_lib_log("%s: OpenSSL built without ECDH support; will not enable ECDHE- ciphers", __func__); | |
517 | # endif | |
92404a1a | 518 | #endif |
db137867 AC |
519 | |
520 | ||
73470f0b AJ |
521 | if(ssl_ctx) |
522 | SSL_CTX_free(ssl_ctx); | |
523 | ||
524 | ssl_ctx = ssl_ctx_new; | |
525 | ||
526 | ||
527 | rb_lib_log("%s: TLS configuration successful", __func__); | |
528 | return 1; | |
529 | } | |
add3f90b | 530 | |
73470f0b AJ |
531 | int |
532 | rb_init_prng(const char *const path, prng_seed_t seed_type) | |
533 | { | |
534 | (void) rb_ssl_last_err(); | |
535 | ||
536 | if(seed_type == RB_PRNG_FILE && RAND_load_file(path, -1) < 0) | |
537 | rb_lib_log("%s: RAND_load_file: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
538 | ||
539 | if(RAND_status() != 1) | |
540 | { | |
541 | rb_lib_log("%s: RAND_status: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
db137867 AC |
542 | return 0; |
543 | } | |
544 | ||
73470f0b AJ |
545 | rb_lib_log("%s: PRNG initialised", __func__); |
546 | return 1; | |
547 | } | |
add3f90b | 548 | |
73470f0b AJ |
549 | int |
550 | rb_get_random(void *const buf, const size_t length) | |
551 | { | |
552 | (void) rb_ssl_last_err(); | |
553 | ||
554 | if(RAND_bytes(buf, (int) length) != 1) | |
555 | { | |
556 | rb_lib_log("%s: RAND_bytes: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
db137867 AC |
557 | return 0; |
558 | } | |
559 | ||
73470f0b AJ |
560 | return 1; |
561 | } | |
562 | ||
563 | const char * | |
564 | rb_get_ssl_strerror(rb_fde_t *const F) | |
565 | { | |
566 | return rb_ssl_strerror(F->ssl_errno); | |
567 | } | |
568 | ||
569 | int | |
570 | rb_get_ssl_certfp(rb_fde_t *const F, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) | |
571 | { | |
35cd2993 | 572 | if(F == NULL || F->ssl == NULL) |
73470f0b AJ |
573 | return 0; |
574 | ||
575 | X509 *const peer_cert = SSL_get_peer_certificate(SSL_P(F)); | |
576 | if(peer_cert == NULL) | |
577 | return 0; | |
578 | ||
579 | int len = 0; | |
580 | ||
581 | switch(SSL_get_verify_result(SSL_P(F))) | |
db137867 | 582 | { |
73470f0b AJ |
583 | case X509_V_OK: |
584 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: | |
585 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: | |
586 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | |
587 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: | |
588 | case X509_V_ERR_CERT_UNTRUSTED: | |
589 | len = make_certfp(peer_cert, certfp, method); | |
30053ab9 | 590 | // fallthrough |
73470f0b AJ |
591 | default: |
592 | X509_free(peer_cert); | |
593 | return len; | |
594 | } | |
595 | } | |
0763237d | 596 | |
73470f0b AJ |
597 | int |
598 | rb_get_ssl_certfp_file(const char *const filename, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) | |
599 | { | |
600 | FILE *const fp = fopen(filename, "r"); | |
601 | if (fp == NULL) | |
602 | return -1; | |
603 | ||
604 | X509 *const cert = PEM_read_X509(fp, NULL, NULL, NULL); | |
605 | if (cert == NULL) | |
606 | { | |
607 | fclose(fp); | |
608 | return 0; | |
db137867 | 609 | } |
c1725bda | 610 | |
73470f0b | 611 | int len = make_certfp(cert, certfp, method); |
add3f90b | 612 | |
73470f0b AJ |
613 | X509_free(cert); |
614 | fclose(fp); | |
add3f90b | 615 | |
73470f0b AJ |
616 | return len; |
617 | } | |
add3f90b | 618 | |
73470f0b AJ |
619 | void |
620 | rb_get_ssl_info(char *const buf, const size_t len) | |
621 | { | |
622 | #ifdef LRB_SSL_FULL_VERSION_INFO | |
623 | if(LRB_SSL_VNUM_RUNTIME == LRB_SSL_VNUM_COMPILETIME) | |
624 | (void) snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", | |
625 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME); | |
626 | else | |
627 | (void) snprintf(buf, len, "OpenSSL: compiled (0x%lx, %s), library (0x%lx, %s)", | |
628 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME, | |
629 | LRB_SSL_VNUM_RUNTIME, LRB_SSL_VTEXT_RUNTIME); | |
630 | #else | |
631 | (void) snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", | |
632 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_RUNTIME); | |
633 | #endif | |
db137867 AC |
634 | } |
635 | ||
73470f0b AJ |
636 | const char * |
637 | rb_ssl_get_cipher(rb_fde_t *const F) | |
db137867 | 638 | { |
73470f0b AJ |
639 | if(F == NULL || F->ssl == NULL) |
640 | return NULL; | |
aa4737a0 | 641 | |
73470f0b | 642 | static char buf[512]; |
aa4737a0 | 643 | |
73470f0b AJ |
644 | const char *const version = SSL_get_version(SSL_P(F)); |
645 | const char *const cipher = SSL_get_cipher_name(SSL_P(F)); | |
646 | ||
647 | (void) snprintf(buf, sizeof buf, "%s, %s", version, cipher); | |
648 | ||
649 | return buf; | |
db137867 AC |
650 | } |
651 | ||
73470f0b AJ |
652 | ssize_t |
653 | rb_ssl_read(rb_fde_t *const F, void *const buf, const size_t count) | |
db137867 | 654 | { |
73470f0b AJ |
655 | return rb_ssl_read_or_write(0, F, buf, NULL, count); |
656 | } | |
657 | ||
658 | ssize_t | |
659 | rb_ssl_write(rb_fde_t *const F, const void *const buf, const size_t count) | |
660 | { | |
661 | return rb_ssl_read_or_write(1, F, NULL, buf, count); | |
662 | } | |
663 | ||
664 | ||
665 | ||
666 | /* | |
667 | * Internal library-agnostic code | |
668 | */ | |
db137867 AC |
669 | |
670 | static void | |
73470f0b | 671 | rb_ssl_connect_realcb(rb_fde_t *const F, const int status, struct ssl_connect *const sconn) |
db137867 | 672 | { |
73470f0b AJ |
673 | lrb_assert(F->connect != NULL); |
674 | ||
db137867 AC |
675 | F->connect->callback = sconn->callback; |
676 | F->connect->data = sconn->data; | |
73470f0b | 677 | |
db137867 | 678 | rb_connect_callback(F, status); |
73470f0b | 679 | rb_free(sconn); |
db137867 AC |
680 | } |
681 | ||
682 | static void | |
8679c0fe | 683 | rb_ssl_timeout_cb(rb_fde_t *const F, void *const data __attribute__((unused))) |
db137867 | 684 | { |
73470f0b AJ |
685 | lrb_assert(F->accept != NULL); |
686 | lrb_assert(F->accept->callback != NULL); | |
687 | ||
688 | F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data); | |
db137867 AC |
689 | } |
690 | ||
691 | static void | |
73470f0b | 692 | rb_ssl_tryconn_timeout_cb(rb_fde_t *const F, void *const data) |
db137867 | 693 | { |
73470f0b | 694 | rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); |
db137867 AC |
695 | } |
696 | ||
697 | static void | |
73470f0b | 698 | rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data) |
db137867 | 699 | { |
73470f0b AJ |
700 | lrb_assert(F != NULL); |
701 | ||
702 | struct ssl_connect *const sconn = data; | |
703 | ||
db137867 AC |
704 | if(status != RB_OK) |
705 | { | |
706 | rb_ssl_connect_realcb(F, status, sconn); | |
707 | return; | |
708 | } | |
709 | ||
710 | F->type |= RB_FD_SSL; | |
db137867 | 711 | |
73470f0b AJ |
712 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
713 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT); | |
714 | rb_ssl_connect_common(F, sconn); | |
db137867 AC |
715 | } |
716 | ||
db137867 | 717 | |
3202e249 | 718 | |
73470f0b AJ |
719 | /* |
720 | * External library-agnostic code | |
721 | */ | |
db137867 AC |
722 | |
723 | int | |
73470f0b | 724 | rb_supports_ssl(void) |
db137867 | 725 | { |
73470f0b | 726 | return 1; |
db137867 AC |
727 | } |
728 | ||
73470f0b AJ |
729 | unsigned int |
730 | rb_ssl_handshake_count(rb_fde_t *const F) | |
db137867 | 731 | { |
73470f0b | 732 | return F->handshake_count; |
db137867 AC |
733 | } |
734 | ||
73470f0b AJ |
735 | void |
736 | rb_ssl_clear_handshake_count(rb_fde_t *const F) | |
db137867 | 737 | { |
73470f0b | 738 | F->handshake_count = 0; |
db137867 AC |
739 | } |
740 | ||
73470f0b AJ |
741 | void |
742 | rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout) | |
03469187 | 743 | { |
73470f0b | 744 | F->type |= RB_FD_SSL; |
03469187 | 745 | |
73470f0b AJ |
746 | F->accept = rb_malloc(sizeof(struct acceptdata)); |
747 | F->accept->callback = cb; | |
748 | F->accept->data = data; | |
749 | F->accept->addrlen = 0; | |
750 | (void) memset(&F->accept->S, 0x00, sizeof F->accept->S); | |
03469187 | 751 | |
73470f0b AJ |
752 | rb_settimeout(F, timeout, rb_ssl_timeout_cb, NULL); |
753 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_IN); | |
754 | rb_ssl_accept_common(F, NULL); | |
03469187 SA |
755 | } |
756 | ||
73470f0b AJ |
757 | void |
758 | rb_ssl_accept_setup(rb_fde_t *const srv_F, rb_fde_t *const cli_F, struct sockaddr *const st, const int addrlen) | |
7247337a | 759 | { |
73470f0b | 760 | cli_F->type |= RB_FD_SSL; |
e3760ba7 | 761 | |
73470f0b AJ |
762 | cli_F->accept = rb_malloc(sizeof(struct acceptdata)); |
763 | cli_F->accept->callback = srv_F->accept->callback; | |
764 | cli_F->accept->data = srv_F->accept->data; | |
765 | cli_F->accept->addrlen = (rb_socklen_t) addrlen; | |
766 | (void) memset(&cli_F->accept->S, 0x00, sizeof cli_F->accept->S); | |
767 | (void) memcpy(&cli_F->accept->S, st, (size_t) addrlen); | |
7247337a | 768 | |
73470f0b AJ |
769 | rb_settimeout(cli_F, 10, rb_ssl_timeout_cb, NULL); |
770 | rb_ssl_init_fd(cli_F, RB_FD_TLS_DIRECTION_IN); | |
771 | rb_ssl_accept_common(cli_F, NULL); | |
7247337a JT |
772 | } |
773 | ||
03469187 | 774 | int |
73470f0b | 775 | rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept) |
03469187 | 776 | { |
73470f0b | 777 | int result = rb_listen(F, backlog, defer_accept); |
03469187 | 778 | |
73470f0b | 779 | F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; |
03469187 | 780 | |
73470f0b | 781 | return result; |
db137867 AC |
782 | } |
783 | ||
030272f3 | 784 | void |
73470f0b AJ |
785 | rb_connect_tcp_ssl(rb_fde_t *const F, struct sockaddr *const dest, struct sockaddr *const clocal, |
786 | CNCB *const callback, void *const data, const int timeout) | |
030272f3 | 787 | { |
73470f0b AJ |
788 | if(F == NULL) |
789 | return; | |
790 | ||
791 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); | |
792 | sconn->data = data; | |
793 | sconn->callback = callback; | |
794 | sconn->timeout = timeout; | |
795 | ||
796 | rb_connect_tcp(F, dest, clocal, rb_ssl_tryconn, sconn, timeout); | |
030272f3 VY |
797 | } |
798 | ||
73470f0b AJ |
799 | void |
800 | rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data, const int timeout) | |
833b2f9c | 801 | { |
73470f0b AJ |
802 | if(F == NULL) |
803 | return; | |
833b2f9c | 804 | |
73470f0b AJ |
805 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); |
806 | sconn->data = data; | |
807 | sconn->callback = callback; | |
808 | sconn->timeout = timeout; | |
31145ea0 | 809 | |
73470f0b AJ |
810 | F->connect = rb_malloc(sizeof(struct conndata)); |
811 | F->connect->callback = callback; | |
812 | F->connect->data = data; | |
813 | F->type |= RB_FD_SSL; | |
31145ea0 | 814 | |
73470f0b AJ |
815 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
816 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT); | |
817 | rb_ssl_connect_common(F, sconn); | |
833b2f9c | 818 | } |
030272f3 | 819 | |
b1f05493 | 820 | #endif /* HAVE_OPENSSL */ |