]>
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 | { | |
395 | FILE *const dhf = fopen(dhfile, "r"); | |
396 | DH *dhp = NULL; | |
397 | ||
398 | if(dhf == NULL) | |
399 | { | |
400 | rb_lib_log("%s: fopen ('%s'): %s", __func__, dhfile, strerror(errno)); | |
401 | } | |
402 | else if(PEM_read_DHparams(dhf, &dhp, NULL, NULL) == NULL) | |
403 | { | |
404 | rb_lib_log("%s: PEM_read_DHparams ('%s'): %s", __func__, dhfile, | |
405 | rb_ssl_strerror(rb_ssl_last_err())); | |
406 | fclose(dhf); | |
407 | } | |
408 | else | |
409 | { | |
410 | SSL_CTX_set_tmp_dh(ssl_ctx_new, dhp); | |
411 | DH_free(dhp); | |
412 | fclose(dhf); | |
413 | } | |
414 | } | |
415 | ||
416 | if(SSL_CTX_set_cipher_list(ssl_ctx_new, cipherlist) != 1) | |
417 | { | |
418 | rb_lib_log("%s: SSL_CTX_set_cipher_list: could not configure any ciphers", __func__); | |
419 | SSL_CTX_free(ssl_ctx_new); | |
420 | return 0; | |
421 | } | |
422 | ||
423 | SSL_CTX_set_session_cache_mode(ssl_ctx_new, SSL_SESS_CACHE_OFF); | |
424 | SSL_CTX_set_verify(ssl_ctx_new, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, verify_accept_all_cb); | |
425 | ||
426 | #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS | |
427 | (void) SSL_CTX_clear_options(ssl_ctx_new, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); | |
92404a1a | 428 | #endif |
a4c8c827 | 429 | |
73470f0b AJ |
430 | #ifndef LRB_HAVE_TLS_METHOD_API |
431 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); | |
92404a1a | 432 | #endif |
b6e799f5 | 433 | |
35cd2993 AJ |
434 | #ifdef SSL_OP_NO_TLSv1 |
435 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_TLSv1); | |
436 | #endif | |
437 | ||
92404a1a | 438 | #ifdef SSL_OP_NO_TICKET |
73470f0b | 439 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_TICKET); |
92404a1a | 440 | #endif |
b1f05493 | 441 | |
92404a1a | 442 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
73470f0b | 443 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_CIPHER_SERVER_PREFERENCE); |
92404a1a | 444 | #endif |
b1f05493 | 445 | |
73470f0b AJ |
446 | #ifdef SSL_OP_SINGLE_DH_USE |
447 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_DH_USE); | |
448 | #endif | |
cf12678b | 449 | |
73470f0b AJ |
450 | #ifdef SSL_OP_SINGLE_ECDH_USE |
451 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_ECDH_USE); | |
cf12678b | 452 | #endif |
db137867 | 453 | |
92404a1a | 454 | #ifdef LRB_HAVE_TLS_ECDH_AUTO |
73470f0b | 455 | (void) SSL_CTX_set_ecdh_auto(ssl_ctx_new, 1); |
92404a1a | 456 | #endif |
6b6a5799 | 457 | |
73470f0b AJ |
458 | #ifdef LRB_HAVE_TLS_SET_CURVES |
459 | (void) SSL_CTX_set1_curves_list(ssl_ctx_new, rb_default_curves); | |
460 | #else | |
461 | # if (OPENSSL_VERSION_NUMBER >= 0x10000000L) && !defined(OPENSSL_NO_ECDH) && defined(NID_secp384r1) | |
462 | EC_KEY *const ec_key = EC_KEY_new_by_curve_name(NID_secp384r1); | |
463 | if(ec_key != NULL) | |
464 | { | |
465 | SSL_CTX_set_tmp_ecdh(ssl_ctx_new, ec_key); | |
466 | EC_KEY_free(ec_key); | |
cf12678b | 467 | } |
73470f0b AJ |
468 | else |
469 | rb_lib_log("%s: EC_KEY_new_by_curve_name failed; will not enable ECDHE- ciphers", __func__); | |
470 | # else | |
471 | rb_lib_log("%s: OpenSSL built without ECDH support; will not enable ECDHE- ciphers", __func__); | |
472 | # endif | |
92404a1a | 473 | #endif |
db137867 AC |
474 | |
475 | ||
73470f0b AJ |
476 | if(ssl_ctx) |
477 | SSL_CTX_free(ssl_ctx); | |
478 | ||
479 | ssl_ctx = ssl_ctx_new; | |
480 | ||
481 | ||
482 | rb_lib_log("%s: TLS configuration successful", __func__); | |
483 | return 1; | |
484 | } | |
add3f90b | 485 | |
73470f0b AJ |
486 | int |
487 | rb_init_prng(const char *const path, prng_seed_t seed_type) | |
488 | { | |
489 | (void) rb_ssl_last_err(); | |
490 | ||
491 | if(seed_type == RB_PRNG_FILE && RAND_load_file(path, -1) < 0) | |
492 | rb_lib_log("%s: RAND_load_file: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
493 | ||
494 | if(RAND_status() != 1) | |
495 | { | |
496 | rb_lib_log("%s: RAND_status: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
db137867 AC |
497 | return 0; |
498 | } | |
499 | ||
73470f0b AJ |
500 | rb_lib_log("%s: PRNG initialised", __func__); |
501 | return 1; | |
502 | } | |
add3f90b | 503 | |
73470f0b AJ |
504 | int |
505 | rb_get_random(void *const buf, const size_t length) | |
506 | { | |
507 | (void) rb_ssl_last_err(); | |
508 | ||
509 | if(RAND_bytes(buf, (int) length) != 1) | |
510 | { | |
511 | rb_lib_log("%s: RAND_bytes: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
db137867 AC |
512 | return 0; |
513 | } | |
514 | ||
73470f0b AJ |
515 | return 1; |
516 | } | |
517 | ||
518 | const char * | |
519 | rb_get_ssl_strerror(rb_fde_t *const F) | |
520 | { | |
521 | return rb_ssl_strerror(F->ssl_errno); | |
522 | } | |
523 | ||
524 | int | |
525 | rb_get_ssl_certfp(rb_fde_t *const F, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) | |
526 | { | |
35cd2993 | 527 | if(F == NULL || F->ssl == NULL) |
73470f0b AJ |
528 | return 0; |
529 | ||
530 | X509 *const peer_cert = SSL_get_peer_certificate(SSL_P(F)); | |
531 | if(peer_cert == NULL) | |
532 | return 0; | |
533 | ||
534 | int len = 0; | |
535 | ||
536 | switch(SSL_get_verify_result(SSL_P(F))) | |
db137867 | 537 | { |
73470f0b AJ |
538 | case X509_V_OK: |
539 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: | |
540 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: | |
541 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | |
542 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: | |
543 | case X509_V_ERR_CERT_UNTRUSTED: | |
544 | len = make_certfp(peer_cert, certfp, method); | |
30053ab9 | 545 | // fallthrough |
73470f0b AJ |
546 | default: |
547 | X509_free(peer_cert); | |
548 | return len; | |
549 | } | |
550 | } | |
0763237d | 551 | |
73470f0b AJ |
552 | int |
553 | rb_get_ssl_certfp_file(const char *const filename, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) | |
554 | { | |
555 | FILE *const fp = fopen(filename, "r"); | |
556 | if (fp == NULL) | |
557 | return -1; | |
558 | ||
559 | X509 *const cert = PEM_read_X509(fp, NULL, NULL, NULL); | |
560 | if (cert == NULL) | |
561 | { | |
562 | fclose(fp); | |
563 | return 0; | |
db137867 | 564 | } |
c1725bda | 565 | |
73470f0b | 566 | int len = make_certfp(cert, certfp, method); |
add3f90b | 567 | |
73470f0b AJ |
568 | X509_free(cert); |
569 | fclose(fp); | |
add3f90b | 570 | |
73470f0b AJ |
571 | return len; |
572 | } | |
add3f90b | 573 | |
73470f0b AJ |
574 | void |
575 | rb_get_ssl_info(char *const buf, const size_t len) | |
576 | { | |
577 | #ifdef LRB_SSL_FULL_VERSION_INFO | |
578 | if(LRB_SSL_VNUM_RUNTIME == LRB_SSL_VNUM_COMPILETIME) | |
579 | (void) snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", | |
580 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME); | |
581 | else | |
582 | (void) snprintf(buf, len, "OpenSSL: compiled (0x%lx, %s), library (0x%lx, %s)", | |
583 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME, | |
584 | LRB_SSL_VNUM_RUNTIME, LRB_SSL_VTEXT_RUNTIME); | |
585 | #else | |
586 | (void) snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", | |
587 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_RUNTIME); | |
588 | #endif | |
db137867 AC |
589 | } |
590 | ||
73470f0b AJ |
591 | const char * |
592 | rb_ssl_get_cipher(rb_fde_t *const F) | |
db137867 | 593 | { |
73470f0b AJ |
594 | if(F == NULL || F->ssl == NULL) |
595 | return NULL; | |
aa4737a0 | 596 | |
73470f0b | 597 | static char buf[512]; |
aa4737a0 | 598 | |
73470f0b AJ |
599 | const char *const version = SSL_get_version(SSL_P(F)); |
600 | const char *const cipher = SSL_get_cipher_name(SSL_P(F)); | |
601 | ||
602 | (void) snprintf(buf, sizeof buf, "%s, %s", version, cipher); | |
603 | ||
604 | return buf; | |
db137867 AC |
605 | } |
606 | ||
73470f0b AJ |
607 | ssize_t |
608 | rb_ssl_read(rb_fde_t *const F, void *const buf, const size_t count) | |
db137867 | 609 | { |
73470f0b AJ |
610 | return rb_ssl_read_or_write(0, F, buf, NULL, count); |
611 | } | |
612 | ||
613 | ssize_t | |
614 | rb_ssl_write(rb_fde_t *const F, const void *const buf, const size_t count) | |
615 | { | |
616 | return rb_ssl_read_or_write(1, F, NULL, buf, count); | |
617 | } | |
618 | ||
619 | ||
620 | ||
621 | /* | |
622 | * Internal library-agnostic code | |
623 | */ | |
db137867 AC |
624 | |
625 | static void | |
73470f0b | 626 | rb_ssl_connect_realcb(rb_fde_t *const F, const int status, struct ssl_connect *const sconn) |
db137867 | 627 | { |
73470f0b AJ |
628 | lrb_assert(F->connect != NULL); |
629 | ||
db137867 AC |
630 | F->connect->callback = sconn->callback; |
631 | F->connect->data = sconn->data; | |
73470f0b | 632 | |
db137867 | 633 | rb_connect_callback(F, status); |
73470f0b | 634 | rb_free(sconn); |
db137867 AC |
635 | } |
636 | ||
637 | static void | |
8679c0fe | 638 | rb_ssl_timeout_cb(rb_fde_t *const F, void *const data __attribute__((unused))) |
db137867 | 639 | { |
73470f0b AJ |
640 | lrb_assert(F->accept != NULL); |
641 | lrb_assert(F->accept->callback != NULL); | |
642 | ||
643 | F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data); | |
db137867 AC |
644 | } |
645 | ||
646 | static void | |
73470f0b | 647 | rb_ssl_tryconn_timeout_cb(rb_fde_t *const F, void *const data) |
db137867 | 648 | { |
73470f0b | 649 | rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); |
db137867 AC |
650 | } |
651 | ||
652 | static void | |
73470f0b | 653 | rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data) |
db137867 | 654 | { |
73470f0b AJ |
655 | lrb_assert(F != NULL); |
656 | ||
657 | struct ssl_connect *const sconn = data; | |
658 | ||
db137867 AC |
659 | if(status != RB_OK) |
660 | { | |
661 | rb_ssl_connect_realcb(F, status, sconn); | |
662 | return; | |
663 | } | |
664 | ||
665 | F->type |= RB_FD_SSL; | |
db137867 | 666 | |
73470f0b AJ |
667 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
668 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT); | |
669 | rb_ssl_connect_common(F, sconn); | |
db137867 AC |
670 | } |
671 | ||
db137867 | 672 | |
3202e249 | 673 | |
73470f0b AJ |
674 | /* |
675 | * External library-agnostic code | |
676 | */ | |
db137867 AC |
677 | |
678 | int | |
73470f0b | 679 | rb_supports_ssl(void) |
db137867 | 680 | { |
73470f0b | 681 | return 1; |
db137867 AC |
682 | } |
683 | ||
73470f0b AJ |
684 | unsigned int |
685 | rb_ssl_handshake_count(rb_fde_t *const F) | |
db137867 | 686 | { |
73470f0b | 687 | return F->handshake_count; |
db137867 AC |
688 | } |
689 | ||
73470f0b AJ |
690 | void |
691 | rb_ssl_clear_handshake_count(rb_fde_t *const F) | |
db137867 | 692 | { |
73470f0b | 693 | F->handshake_count = 0; |
db137867 AC |
694 | } |
695 | ||
73470f0b AJ |
696 | void |
697 | rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout) | |
03469187 | 698 | { |
73470f0b | 699 | F->type |= RB_FD_SSL; |
03469187 | 700 | |
73470f0b AJ |
701 | F->accept = rb_malloc(sizeof(struct acceptdata)); |
702 | F->accept->callback = cb; | |
703 | F->accept->data = data; | |
704 | F->accept->addrlen = 0; | |
705 | (void) memset(&F->accept->S, 0x00, sizeof F->accept->S); | |
03469187 | 706 | |
73470f0b AJ |
707 | rb_settimeout(F, timeout, rb_ssl_timeout_cb, NULL); |
708 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_IN); | |
709 | rb_ssl_accept_common(F, NULL); | |
03469187 SA |
710 | } |
711 | ||
73470f0b AJ |
712 | void |
713 | rb_ssl_accept_setup(rb_fde_t *const srv_F, rb_fde_t *const cli_F, struct sockaddr *const st, const int addrlen) | |
7247337a | 714 | { |
73470f0b | 715 | cli_F->type |= RB_FD_SSL; |
e3760ba7 | 716 | |
73470f0b AJ |
717 | cli_F->accept = rb_malloc(sizeof(struct acceptdata)); |
718 | cli_F->accept->callback = srv_F->accept->callback; | |
719 | cli_F->accept->data = srv_F->accept->data; | |
720 | cli_F->accept->addrlen = (rb_socklen_t) addrlen; | |
721 | (void) memset(&cli_F->accept->S, 0x00, sizeof cli_F->accept->S); | |
722 | (void) memcpy(&cli_F->accept->S, st, (size_t) addrlen); | |
7247337a | 723 | |
73470f0b AJ |
724 | rb_settimeout(cli_F, 10, rb_ssl_timeout_cb, NULL); |
725 | rb_ssl_init_fd(cli_F, RB_FD_TLS_DIRECTION_IN); | |
726 | rb_ssl_accept_common(cli_F, NULL); | |
7247337a JT |
727 | } |
728 | ||
03469187 | 729 | int |
73470f0b | 730 | rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept) |
03469187 | 731 | { |
73470f0b | 732 | int result = rb_listen(F, backlog, defer_accept); |
03469187 | 733 | |
73470f0b | 734 | F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; |
03469187 | 735 | |
73470f0b | 736 | return result; |
db137867 AC |
737 | } |
738 | ||
030272f3 | 739 | void |
73470f0b AJ |
740 | rb_connect_tcp_ssl(rb_fde_t *const F, struct sockaddr *const dest, struct sockaddr *const clocal, |
741 | CNCB *const callback, void *const data, const int timeout) | |
030272f3 | 742 | { |
73470f0b AJ |
743 | if(F == NULL) |
744 | return; | |
745 | ||
746 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); | |
747 | sconn->data = data; | |
748 | sconn->callback = callback; | |
749 | sconn->timeout = timeout; | |
750 | ||
751 | rb_connect_tcp(F, dest, clocal, rb_ssl_tryconn, sconn, timeout); | |
030272f3 VY |
752 | } |
753 | ||
73470f0b AJ |
754 | void |
755 | rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data, const int timeout) | |
833b2f9c | 756 | { |
73470f0b AJ |
757 | if(F == NULL) |
758 | return; | |
833b2f9c | 759 | |
73470f0b AJ |
760 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); |
761 | sconn->data = data; | |
762 | sconn->callback = callback; | |
763 | sconn->timeout = timeout; | |
31145ea0 | 764 | |
73470f0b AJ |
765 | F->connect = rb_malloc(sizeof(struct conndata)); |
766 | F->connect->callback = callback; | |
767 | F->connect->data = data; | |
768 | F->type |= RB_FD_SSL; | |
31145ea0 | 769 | |
73470f0b AJ |
770 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
771 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT); | |
772 | rb_ssl_connect_common(F, sconn); | |
833b2f9c | 773 | } |
030272f3 | 774 | |
b1f05493 | 775 | #endif /* HAVE_OPENSSL */ |