]>
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 |
73470f0b | 103 | rb_ssl_accept_common(rb_fde_t *const F, void *const data) |
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 AJ |
193 | static int |
194 | verify_accept_all_cb(const int preverify_ok, X509_STORE_CTX *const x509_ctx) | |
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 AJ |
292 | if(ASN1_item_digest(item, md_type, data, certfp, &hashlen) != 1) |
293 | return 0; | |
294 | ||
295 | return (int) hashlen; | |
7247337a JT |
296 | } |
297 | ||
73470f0b AJ |
298 | |
299 | ||
300 | /* | |
301 | * External OpenSSL-specific code | |
302 | */ | |
303 | ||
304 | void | |
305 | rb_ssl_shutdown(rb_fde_t *const F) | |
918d73d5 | 306 | { |
73470f0b AJ |
307 | if(F == NULL || F->ssl == NULL) |
308 | return; | |
918d73d5 | 309 | |
73470f0b AJ |
310 | (void) rb_ssl_last_err(); |
311 | ||
312 | for(int i = 0; i < 4; i++) | |
313 | { | |
314 | int ret = SSL_shutdown(SSL_P(F)); | |
315 | int err = SSL_get_error(SSL_P(F), ret); | |
316 | ||
317 | if(ret >= 0 || (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)) | |
318 | break; | |
319 | } | |
320 | ||
321 | SSL_free(SSL_P(F)); | |
322 | F->ssl = NULL; | |
918d73d5 JT |
323 | } |
324 | ||
db137867 AC |
325 | int |
326 | rb_init_ssl(void) | |
327 | { | |
73470f0b AJ |
328 | #ifndef LRB_SSL_NO_EXPLICIT_INIT |
329 | (void) SSL_library_init(); | |
cf12678b | 330 | SSL_load_error_strings(); |
73470f0b | 331 | #endif |
a4c8c827 | 332 | |
73470f0b | 333 | rb_lib_log("%s: OpenSSL backend initialised", __func__); |
cf12678b AJ |
334 | return 1; |
335 | } | |
336 | ||
337 | int | |
73470f0b AJ |
338 | rb_setup_ssl_server(const char *const certfile, const char *keyfile, |
339 | const char *const dhfile, const char *cipherlist) | |
cf12678b | 340 | { |
0fe9dd41 | 341 | if(certfile == NULL) |
cf12678b | 342 | { |
73470f0b | 343 | rb_lib_log("%s: no certificate file specified", __func__); |
cf12678b AJ |
344 | return 0; |
345 | } | |
346 | ||
347 | if(keyfile == NULL) | |
0fe9dd41 | 348 | keyfile = certfile; |
cf12678b | 349 | |
73470f0b AJ |
350 | if(cipherlist == NULL) |
351 | cipherlist = rb_default_ciphers; | |
352 | ||
353 | ||
354 | (void) rb_ssl_last_err(); | |
92404a1a | 355 | |
cf12678b | 356 | #ifdef LRB_HAVE_TLS_METHOD_API |
73470f0b | 357 | SSL_CTX *const ssl_ctx_new = SSL_CTX_new(TLS_method()); |
cf12678b | 358 | #else |
73470f0b | 359 | SSL_CTX *const ssl_ctx_new = SSL_CTX_new(SSLv23_method()); |
cf12678b | 360 | #endif |
73470f0b AJ |
361 | |
362 | if(ssl_ctx_new == NULL) | |
db137867 | 363 | { |
73470f0b | 364 | rb_lib_log("%s: SSL_CTX_new: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); |
92404a1a | 365 | return 0; |
db137867 | 366 | } |
a4c8c827 | 367 | |
73470f0b | 368 | if(SSL_CTX_use_certificate_chain_file(ssl_ctx_new, certfile) != 1) |
92404a1a | 369 | { |
73470f0b AJ |
370 | rb_lib_log("%s: SSL_CTX_use_certificate_chain_file ('%s'): %s", __func__, certfile, |
371 | rb_ssl_strerror(rb_ssl_last_err())); | |
add3f90b | 372 | |
73470f0b | 373 | SSL_CTX_free(ssl_ctx_new); |
92404a1a AJ |
374 | return 0; |
375 | } | |
a4c8c827 | 376 | |
73470f0b AJ |
377 | if(SSL_CTX_use_PrivateKey_file(ssl_ctx_new, keyfile, SSL_FILETYPE_PEM) != 1) |
378 | { | |
379 | rb_lib_log("%s: SSL_CTX_use_PrivateKey_file ('%s'): %s", __func__, keyfile, | |
380 | rb_ssl_strerror(rb_ssl_last_err())); | |
a4c8c827 | 381 | |
73470f0b AJ |
382 | SSL_CTX_free(ssl_ctx_new); |
383 | return 0; | |
384 | } | |
385 | ||
386 | if(dhfile == NULL) | |
387 | { | |
388 | rb_lib_log("%s: no DH parameters file specified", __func__); | |
389 | } | |
390 | else | |
391 | { | |
392 | FILE *const dhf = fopen(dhfile, "r"); | |
393 | DH *dhp = NULL; | |
394 | ||
395 | if(dhf == NULL) | |
396 | { | |
397 | rb_lib_log("%s: fopen ('%s'): %s", __func__, dhfile, strerror(errno)); | |
398 | } | |
399 | else if(PEM_read_DHparams(dhf, &dhp, NULL, NULL) == NULL) | |
400 | { | |
401 | rb_lib_log("%s: PEM_read_DHparams ('%s'): %s", __func__, dhfile, | |
402 | rb_ssl_strerror(rb_ssl_last_err())); | |
403 | fclose(dhf); | |
404 | } | |
405 | else | |
406 | { | |
407 | SSL_CTX_set_tmp_dh(ssl_ctx_new, dhp); | |
408 | DH_free(dhp); | |
409 | fclose(dhf); | |
410 | } | |
411 | } | |
412 | ||
413 | if(SSL_CTX_set_cipher_list(ssl_ctx_new, cipherlist) != 1) | |
414 | { | |
415 | rb_lib_log("%s: SSL_CTX_set_cipher_list: could not configure any ciphers", __func__); | |
416 | SSL_CTX_free(ssl_ctx_new); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | SSL_CTX_set_session_cache_mode(ssl_ctx_new, SSL_SESS_CACHE_OFF); | |
421 | SSL_CTX_set_verify(ssl_ctx_new, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, verify_accept_all_cb); | |
422 | ||
423 | #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS | |
424 | (void) SSL_CTX_clear_options(ssl_ctx_new, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS); | |
92404a1a | 425 | #endif |
a4c8c827 | 426 | |
73470f0b AJ |
427 | #ifndef LRB_HAVE_TLS_METHOD_API |
428 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); | |
92404a1a | 429 | #endif |
b6e799f5 | 430 | |
92404a1a | 431 | #ifdef SSL_OP_NO_TICKET |
73470f0b | 432 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_TICKET); |
92404a1a | 433 | #endif |
b1f05493 | 434 | |
92404a1a | 435 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
73470f0b | 436 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_CIPHER_SERVER_PREFERENCE); |
92404a1a | 437 | #endif |
b1f05493 | 438 | |
73470f0b AJ |
439 | #ifdef SSL_OP_SINGLE_DH_USE |
440 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_DH_USE); | |
441 | #endif | |
cf12678b | 442 | |
73470f0b AJ |
443 | #ifdef SSL_OP_SINGLE_ECDH_USE |
444 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_ECDH_USE); | |
cf12678b | 445 | #endif |
db137867 | 446 | |
92404a1a | 447 | #ifdef LRB_HAVE_TLS_ECDH_AUTO |
73470f0b | 448 | (void) SSL_CTX_set_ecdh_auto(ssl_ctx_new, 1); |
92404a1a | 449 | #endif |
6b6a5799 | 450 | |
73470f0b AJ |
451 | #ifdef LRB_HAVE_TLS_SET_CURVES |
452 | (void) SSL_CTX_set1_curves_list(ssl_ctx_new, rb_default_curves); | |
453 | #else | |
454 | # if (OPENSSL_VERSION_NUMBER >= 0x10000000L) && !defined(OPENSSL_NO_ECDH) && defined(NID_secp384r1) | |
455 | EC_KEY *const ec_key = EC_KEY_new_by_curve_name(NID_secp384r1); | |
456 | if(ec_key != NULL) | |
457 | { | |
458 | SSL_CTX_set_tmp_ecdh(ssl_ctx_new, ec_key); | |
459 | EC_KEY_free(ec_key); | |
cf12678b | 460 | } |
73470f0b AJ |
461 | else |
462 | rb_lib_log("%s: EC_KEY_new_by_curve_name failed; will not enable ECDHE- ciphers", __func__); | |
463 | # else | |
464 | rb_lib_log("%s: OpenSSL built without ECDH support; will not enable ECDHE- ciphers", __func__); | |
465 | # endif | |
92404a1a | 466 | #endif |
db137867 AC |
467 | |
468 | ||
73470f0b AJ |
469 | if(ssl_ctx) |
470 | SSL_CTX_free(ssl_ctx); | |
471 | ||
472 | ssl_ctx = ssl_ctx_new; | |
473 | ||
474 | ||
475 | rb_lib_log("%s: TLS configuration successful", __func__); | |
476 | return 1; | |
477 | } | |
add3f90b | 478 | |
73470f0b AJ |
479 | int |
480 | rb_init_prng(const char *const path, prng_seed_t seed_type) | |
481 | { | |
482 | (void) rb_ssl_last_err(); | |
483 | ||
484 | if(seed_type == RB_PRNG_FILE && RAND_load_file(path, -1) < 0) | |
485 | rb_lib_log("%s: RAND_load_file: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
486 | ||
487 | if(RAND_status() != 1) | |
488 | { | |
489 | rb_lib_log("%s: RAND_status: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
db137867 AC |
490 | return 0; |
491 | } | |
492 | ||
73470f0b AJ |
493 | rb_lib_log("%s: PRNG initialised", __func__); |
494 | return 1; | |
495 | } | |
add3f90b | 496 | |
73470f0b AJ |
497 | int |
498 | rb_get_random(void *const buf, const size_t length) | |
499 | { | |
500 | (void) rb_ssl_last_err(); | |
501 | ||
502 | if(RAND_bytes(buf, (int) length) != 1) | |
503 | { | |
504 | rb_lib_log("%s: RAND_bytes: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
db137867 AC |
505 | return 0; |
506 | } | |
507 | ||
73470f0b AJ |
508 | return 1; |
509 | } | |
510 | ||
511 | const char * | |
512 | rb_get_ssl_strerror(rb_fde_t *const F) | |
513 | { | |
514 | return rb_ssl_strerror(F->ssl_errno); | |
515 | } | |
516 | ||
517 | int | |
518 | rb_get_ssl_certfp(rb_fde_t *const F, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) | |
519 | { | |
520 | if(F->ssl == NULL) | |
521 | return 0; | |
522 | ||
523 | X509 *const peer_cert = SSL_get_peer_certificate(SSL_P(F)); | |
524 | if(peer_cert == NULL) | |
525 | return 0; | |
526 | ||
527 | int len = 0; | |
528 | ||
529 | switch(SSL_get_verify_result(SSL_P(F))) | |
db137867 | 530 | { |
73470f0b AJ |
531 | case X509_V_OK: |
532 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: | |
533 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: | |
534 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | |
535 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: | |
536 | case X509_V_ERR_CERT_UNTRUSTED: | |
537 | len = make_certfp(peer_cert, certfp, method); | |
538 | default: | |
539 | X509_free(peer_cert); | |
540 | return len; | |
541 | } | |
542 | } | |
0763237d | 543 | |
73470f0b AJ |
544 | int |
545 | rb_get_ssl_certfp_file(const char *const filename, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) | |
546 | { | |
547 | FILE *const fp = fopen(filename, "r"); | |
548 | if (fp == NULL) | |
549 | return -1; | |
550 | ||
551 | X509 *const cert = PEM_read_X509(fp, NULL, NULL, NULL); | |
552 | if (cert == NULL) | |
553 | { | |
554 | fclose(fp); | |
555 | return 0; | |
db137867 | 556 | } |
c1725bda | 557 | |
73470f0b | 558 | int len = make_certfp(cert, certfp, method); |
add3f90b | 559 | |
73470f0b AJ |
560 | X509_free(cert); |
561 | fclose(fp); | |
add3f90b | 562 | |
73470f0b AJ |
563 | return len; |
564 | } | |
add3f90b | 565 | |
73470f0b AJ |
566 | void |
567 | rb_get_ssl_info(char *const buf, const size_t len) | |
568 | { | |
569 | #ifdef LRB_SSL_FULL_VERSION_INFO | |
570 | if(LRB_SSL_VNUM_RUNTIME == LRB_SSL_VNUM_COMPILETIME) | |
571 | (void) snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", | |
572 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME); | |
573 | else | |
574 | (void) snprintf(buf, len, "OpenSSL: compiled (0x%lx, %s), library (0x%lx, %s)", | |
575 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME, | |
576 | LRB_SSL_VNUM_RUNTIME, LRB_SSL_VTEXT_RUNTIME); | |
577 | #else | |
578 | (void) snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", | |
579 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_RUNTIME); | |
580 | #endif | |
db137867 AC |
581 | } |
582 | ||
73470f0b AJ |
583 | const char * |
584 | rb_ssl_get_cipher(rb_fde_t *const F) | |
db137867 | 585 | { |
73470f0b AJ |
586 | if(F == NULL || F->ssl == NULL) |
587 | return NULL; | |
aa4737a0 | 588 | |
73470f0b | 589 | static char buf[512]; |
aa4737a0 | 590 | |
73470f0b AJ |
591 | const char *const version = SSL_get_version(SSL_P(F)); |
592 | const char *const cipher = SSL_get_cipher_name(SSL_P(F)); | |
593 | ||
594 | (void) snprintf(buf, sizeof buf, "%s, %s", version, cipher); | |
595 | ||
596 | return buf; | |
db137867 AC |
597 | } |
598 | ||
73470f0b AJ |
599 | ssize_t |
600 | rb_ssl_read(rb_fde_t *const F, void *const buf, const size_t count) | |
db137867 | 601 | { |
73470f0b AJ |
602 | return rb_ssl_read_or_write(0, F, buf, NULL, count); |
603 | } | |
604 | ||
605 | ssize_t | |
606 | rb_ssl_write(rb_fde_t *const F, const void *const buf, const size_t count) | |
607 | { | |
608 | return rb_ssl_read_or_write(1, F, NULL, buf, count); | |
609 | } | |
610 | ||
611 | ||
612 | ||
613 | /* | |
614 | * Internal library-agnostic code | |
615 | */ | |
db137867 AC |
616 | |
617 | static void | |
73470f0b | 618 | rb_ssl_connect_realcb(rb_fde_t *const F, const int status, struct ssl_connect *const sconn) |
db137867 | 619 | { |
73470f0b AJ |
620 | lrb_assert(F->connect != NULL); |
621 | ||
db137867 AC |
622 | F->connect->callback = sconn->callback; |
623 | F->connect->data = sconn->data; | |
73470f0b | 624 | |
db137867 | 625 | rb_connect_callback(F, status); |
73470f0b | 626 | rb_free(sconn); |
db137867 AC |
627 | } |
628 | ||
629 | static void | |
73470f0b | 630 | rb_ssl_timeout_cb(rb_fde_t *const F, void *const data) |
db137867 | 631 | { |
73470f0b AJ |
632 | lrb_assert(F->accept != NULL); |
633 | lrb_assert(F->accept->callback != NULL); | |
634 | ||
635 | F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data); | |
db137867 AC |
636 | } |
637 | ||
638 | static void | |
73470f0b | 639 | rb_ssl_tryconn_timeout_cb(rb_fde_t *const F, void *const data) |
db137867 | 640 | { |
73470f0b | 641 | rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); |
db137867 AC |
642 | } |
643 | ||
644 | static void | |
73470f0b | 645 | rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data) |
db137867 | 646 | { |
73470f0b AJ |
647 | lrb_assert(F != NULL); |
648 | ||
649 | struct ssl_connect *const sconn = data; | |
650 | ||
db137867 AC |
651 | if(status != RB_OK) |
652 | { | |
653 | rb_ssl_connect_realcb(F, status, sconn); | |
654 | return; | |
655 | } | |
656 | ||
657 | F->type |= RB_FD_SSL; | |
db137867 | 658 | |
73470f0b AJ |
659 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
660 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT); | |
661 | rb_ssl_connect_common(F, sconn); | |
db137867 AC |
662 | } |
663 | ||
db137867 | 664 | |
3202e249 | 665 | |
73470f0b AJ |
666 | /* |
667 | * External library-agnostic code | |
668 | */ | |
db137867 AC |
669 | |
670 | int | |
73470f0b | 671 | rb_supports_ssl(void) |
db137867 | 672 | { |
73470f0b | 673 | return 1; |
db137867 AC |
674 | } |
675 | ||
73470f0b AJ |
676 | unsigned int |
677 | rb_ssl_handshake_count(rb_fde_t *const F) | |
db137867 | 678 | { |
73470f0b | 679 | return F->handshake_count; |
db137867 AC |
680 | } |
681 | ||
73470f0b AJ |
682 | void |
683 | rb_ssl_clear_handshake_count(rb_fde_t *const F) | |
db137867 | 684 | { |
73470f0b | 685 | F->handshake_count = 0; |
db137867 AC |
686 | } |
687 | ||
73470f0b AJ |
688 | void |
689 | rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout) | |
03469187 | 690 | { |
73470f0b | 691 | F->type |= RB_FD_SSL; |
03469187 | 692 | |
73470f0b AJ |
693 | F->accept = rb_malloc(sizeof(struct acceptdata)); |
694 | F->accept->callback = cb; | |
695 | F->accept->data = data; | |
696 | F->accept->addrlen = 0; | |
697 | (void) memset(&F->accept->S, 0x00, sizeof F->accept->S); | |
03469187 | 698 | |
73470f0b AJ |
699 | rb_settimeout(F, timeout, rb_ssl_timeout_cb, NULL); |
700 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_IN); | |
701 | rb_ssl_accept_common(F, NULL); | |
03469187 SA |
702 | } |
703 | ||
73470f0b AJ |
704 | void |
705 | rb_ssl_accept_setup(rb_fde_t *const srv_F, rb_fde_t *const cli_F, struct sockaddr *const st, const int addrlen) | |
7247337a | 706 | { |
73470f0b | 707 | cli_F->type |= RB_FD_SSL; |
e3760ba7 | 708 | |
73470f0b AJ |
709 | cli_F->accept = rb_malloc(sizeof(struct acceptdata)); |
710 | cli_F->accept->callback = srv_F->accept->callback; | |
711 | cli_F->accept->data = srv_F->accept->data; | |
712 | cli_F->accept->addrlen = (rb_socklen_t) addrlen; | |
713 | (void) memset(&cli_F->accept->S, 0x00, sizeof cli_F->accept->S); | |
714 | (void) memcpy(&cli_F->accept->S, st, (size_t) addrlen); | |
7247337a | 715 | |
73470f0b AJ |
716 | rb_settimeout(cli_F, 10, rb_ssl_timeout_cb, NULL); |
717 | rb_ssl_init_fd(cli_F, RB_FD_TLS_DIRECTION_IN); | |
718 | rb_ssl_accept_common(cli_F, NULL); | |
7247337a JT |
719 | } |
720 | ||
03469187 | 721 | int |
73470f0b | 722 | rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept) |
03469187 | 723 | { |
73470f0b | 724 | int result = rb_listen(F, backlog, defer_accept); |
03469187 | 725 | |
73470f0b | 726 | F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; |
03469187 | 727 | |
73470f0b | 728 | return result; |
db137867 AC |
729 | } |
730 | ||
030272f3 | 731 | void |
73470f0b AJ |
732 | rb_connect_tcp_ssl(rb_fde_t *const F, struct sockaddr *const dest, struct sockaddr *const clocal, |
733 | CNCB *const callback, void *const data, const int timeout) | |
030272f3 | 734 | { |
73470f0b AJ |
735 | if(F == NULL) |
736 | return; | |
737 | ||
738 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); | |
739 | sconn->data = data; | |
740 | sconn->callback = callback; | |
741 | sconn->timeout = timeout; | |
742 | ||
743 | rb_connect_tcp(F, dest, clocal, rb_ssl_tryconn, sconn, timeout); | |
030272f3 VY |
744 | } |
745 | ||
73470f0b AJ |
746 | void |
747 | rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data, const int timeout) | |
833b2f9c | 748 | { |
73470f0b AJ |
749 | if(F == NULL) |
750 | return; | |
833b2f9c | 751 | |
73470f0b AJ |
752 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); |
753 | sconn->data = data; | |
754 | sconn->callback = callback; | |
755 | sconn->timeout = timeout; | |
31145ea0 | 756 | |
73470f0b AJ |
757 | F->connect = rb_malloc(sizeof(struct conndata)); |
758 | F->connect->callback = callback; | |
759 | F->connect->data = data; | |
760 | F->type |= RB_FD_SSL; | |
31145ea0 | 761 | |
73470f0b AJ |
762 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
763 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT); | |
764 | rb_ssl_connect_common(F, sconn); | |
833b2f9c | 765 | } |
030272f3 | 766 | |
b1f05493 | 767 | #endif /* HAVE_OPENSSL */ |