]>
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 | 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); | |
545 | default: | |
546 | X509_free(peer_cert); | |
547 | return len; | |
548 | } | |
549 | } | |
0763237d | 550 | |
73470f0b AJ |
551 | int |
552 | rb_get_ssl_certfp_file(const char *const filename, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) | |
553 | { | |
554 | FILE *const fp = fopen(filename, "r"); | |
555 | if (fp == NULL) | |
556 | return -1; | |
557 | ||
558 | X509 *const cert = PEM_read_X509(fp, NULL, NULL, NULL); | |
559 | if (cert == NULL) | |
560 | { | |
561 | fclose(fp); | |
562 | return 0; | |
db137867 | 563 | } |
c1725bda | 564 | |
73470f0b | 565 | int len = make_certfp(cert, certfp, method); |
add3f90b | 566 | |
73470f0b AJ |
567 | X509_free(cert); |
568 | fclose(fp); | |
add3f90b | 569 | |
73470f0b AJ |
570 | return len; |
571 | } | |
add3f90b | 572 | |
73470f0b AJ |
573 | void |
574 | rb_get_ssl_info(char *const buf, const size_t len) | |
575 | { | |
576 | #ifdef LRB_SSL_FULL_VERSION_INFO | |
577 | if(LRB_SSL_VNUM_RUNTIME == LRB_SSL_VNUM_COMPILETIME) | |
578 | (void) snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", | |
579 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME); | |
580 | else | |
581 | (void) snprintf(buf, len, "OpenSSL: compiled (0x%lx, %s), library (0x%lx, %s)", | |
582 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME, | |
583 | LRB_SSL_VNUM_RUNTIME, LRB_SSL_VTEXT_RUNTIME); | |
584 | #else | |
585 | (void) snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s", | |
586 | LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_RUNTIME); | |
587 | #endif | |
db137867 AC |
588 | } |
589 | ||
73470f0b AJ |
590 | const char * |
591 | rb_ssl_get_cipher(rb_fde_t *const F) | |
db137867 | 592 | { |
73470f0b AJ |
593 | if(F == NULL || F->ssl == NULL) |
594 | return NULL; | |
aa4737a0 | 595 | |
73470f0b | 596 | static char buf[512]; |
aa4737a0 | 597 | |
73470f0b AJ |
598 | const char *const version = SSL_get_version(SSL_P(F)); |
599 | const char *const cipher = SSL_get_cipher_name(SSL_P(F)); | |
600 | ||
601 | (void) snprintf(buf, sizeof buf, "%s, %s", version, cipher); | |
602 | ||
603 | return buf; | |
db137867 AC |
604 | } |
605 | ||
73470f0b AJ |
606 | ssize_t |
607 | rb_ssl_read(rb_fde_t *const F, void *const buf, const size_t count) | |
db137867 | 608 | { |
73470f0b AJ |
609 | return rb_ssl_read_or_write(0, F, buf, NULL, count); |
610 | } | |
611 | ||
612 | ssize_t | |
613 | rb_ssl_write(rb_fde_t *const F, const void *const buf, const size_t count) | |
614 | { | |
615 | return rb_ssl_read_or_write(1, F, NULL, buf, count); | |
616 | } | |
617 | ||
618 | ||
619 | ||
620 | /* | |
621 | * Internal library-agnostic code | |
622 | */ | |
db137867 AC |
623 | |
624 | static void | |
73470f0b | 625 | rb_ssl_connect_realcb(rb_fde_t *const F, const int status, struct ssl_connect *const sconn) |
db137867 | 626 | { |
73470f0b AJ |
627 | lrb_assert(F->connect != NULL); |
628 | ||
db137867 AC |
629 | F->connect->callback = sconn->callback; |
630 | F->connect->data = sconn->data; | |
73470f0b | 631 | |
db137867 | 632 | rb_connect_callback(F, status); |
73470f0b | 633 | rb_free(sconn); |
db137867 AC |
634 | } |
635 | ||
636 | static void | |
73470f0b | 637 | rb_ssl_timeout_cb(rb_fde_t *const F, void *const data) |
db137867 | 638 | { |
73470f0b AJ |
639 | lrb_assert(F->accept != NULL); |
640 | lrb_assert(F->accept->callback != NULL); | |
641 | ||
642 | F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data); | |
db137867 AC |
643 | } |
644 | ||
645 | static void | |
73470f0b | 646 | rb_ssl_tryconn_timeout_cb(rb_fde_t *const F, void *const data) |
db137867 | 647 | { |
73470f0b | 648 | rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); |
db137867 AC |
649 | } |
650 | ||
651 | static void | |
73470f0b | 652 | rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data) |
db137867 | 653 | { |
73470f0b AJ |
654 | lrb_assert(F != NULL); |
655 | ||
656 | struct ssl_connect *const sconn = data; | |
657 | ||
db137867 AC |
658 | if(status != RB_OK) |
659 | { | |
660 | rb_ssl_connect_realcb(F, status, sconn); | |
661 | return; | |
662 | } | |
663 | ||
664 | F->type |= RB_FD_SSL; | |
db137867 | 665 | |
73470f0b AJ |
666 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
667 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT); | |
668 | rb_ssl_connect_common(F, sconn); | |
db137867 AC |
669 | } |
670 | ||
db137867 | 671 | |
3202e249 | 672 | |
73470f0b AJ |
673 | /* |
674 | * External library-agnostic code | |
675 | */ | |
db137867 AC |
676 | |
677 | int | |
73470f0b | 678 | rb_supports_ssl(void) |
db137867 | 679 | { |
73470f0b | 680 | return 1; |
db137867 AC |
681 | } |
682 | ||
73470f0b AJ |
683 | unsigned int |
684 | rb_ssl_handshake_count(rb_fde_t *const F) | |
db137867 | 685 | { |
73470f0b | 686 | return F->handshake_count; |
db137867 AC |
687 | } |
688 | ||
73470f0b AJ |
689 | void |
690 | rb_ssl_clear_handshake_count(rb_fde_t *const F) | |
db137867 | 691 | { |
73470f0b | 692 | F->handshake_count = 0; |
db137867 AC |
693 | } |
694 | ||
73470f0b AJ |
695 | void |
696 | rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout) | |
03469187 | 697 | { |
73470f0b | 698 | F->type |= RB_FD_SSL; |
03469187 | 699 | |
73470f0b AJ |
700 | F->accept = rb_malloc(sizeof(struct acceptdata)); |
701 | F->accept->callback = cb; | |
702 | F->accept->data = data; | |
703 | F->accept->addrlen = 0; | |
704 | (void) memset(&F->accept->S, 0x00, sizeof F->accept->S); | |
03469187 | 705 | |
73470f0b AJ |
706 | rb_settimeout(F, timeout, rb_ssl_timeout_cb, NULL); |
707 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_IN); | |
708 | rb_ssl_accept_common(F, NULL); | |
03469187 SA |
709 | } |
710 | ||
73470f0b AJ |
711 | void |
712 | rb_ssl_accept_setup(rb_fde_t *const srv_F, rb_fde_t *const cli_F, struct sockaddr *const st, const int addrlen) | |
7247337a | 713 | { |
73470f0b | 714 | cli_F->type |= RB_FD_SSL; |
e3760ba7 | 715 | |
73470f0b AJ |
716 | cli_F->accept = rb_malloc(sizeof(struct acceptdata)); |
717 | cli_F->accept->callback = srv_F->accept->callback; | |
718 | cli_F->accept->data = srv_F->accept->data; | |
719 | cli_F->accept->addrlen = (rb_socklen_t) addrlen; | |
720 | (void) memset(&cli_F->accept->S, 0x00, sizeof cli_F->accept->S); | |
721 | (void) memcpy(&cli_F->accept->S, st, (size_t) addrlen); | |
7247337a | 722 | |
73470f0b AJ |
723 | rb_settimeout(cli_F, 10, rb_ssl_timeout_cb, NULL); |
724 | rb_ssl_init_fd(cli_F, RB_FD_TLS_DIRECTION_IN); | |
725 | rb_ssl_accept_common(cli_F, NULL); | |
7247337a JT |
726 | } |
727 | ||
03469187 | 728 | int |
73470f0b | 729 | rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept) |
03469187 | 730 | { |
73470f0b | 731 | int result = rb_listen(F, backlog, defer_accept); |
03469187 | 732 | |
73470f0b | 733 | F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; |
03469187 | 734 | |
73470f0b | 735 | return result; |
db137867 AC |
736 | } |
737 | ||
030272f3 | 738 | void |
73470f0b AJ |
739 | rb_connect_tcp_ssl(rb_fde_t *const F, struct sockaddr *const dest, struct sockaddr *const clocal, |
740 | CNCB *const callback, void *const data, const int timeout) | |
030272f3 | 741 | { |
73470f0b AJ |
742 | if(F == NULL) |
743 | return; | |
744 | ||
745 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); | |
746 | sconn->data = data; | |
747 | sconn->callback = callback; | |
748 | sconn->timeout = timeout; | |
749 | ||
750 | rb_connect_tcp(F, dest, clocal, rb_ssl_tryconn, sconn, timeout); | |
030272f3 VY |
751 | } |
752 | ||
73470f0b AJ |
753 | void |
754 | rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data, const int timeout) | |
833b2f9c | 755 | { |
73470f0b AJ |
756 | if(F == NULL) |
757 | return; | |
833b2f9c | 758 | |
73470f0b AJ |
759 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); |
760 | sconn->data = data; | |
761 | sconn->callback = callback; | |
762 | sconn->timeout = timeout; | |
31145ea0 | 763 | |
73470f0b AJ |
764 | F->connect = rb_malloc(sizeof(struct conndata)); |
765 | F->connect->callback = callback; | |
766 | F->connect->data = data; | |
767 | F->type |= RB_FD_SSL; | |
31145ea0 | 768 | |
73470f0b AJ |
769 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
770 | rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT); | |
771 | rb_ssl_connect_common(F, sconn); | |
833b2f9c | 772 | } |
030272f3 | 773 | |
b1f05493 | 774 | #endif /* HAVE_OPENSSL */ |