]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * librb: a library used by ircd-ratbox and other things | |
3 | * openssl.c: OpenSSL backend | |
4 | * | |
5 | * Copyright (C) 2007-2008 ircd-ratbox development team | |
6 | * Copyright (C) 2007-2008 Aaron Sethman <androsyn@ratbox.org> | |
7 | * Copyright (C) 2015-2016 Aaron Jones <aaronmdjones@gmail.com> | |
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. | |
18 | * | |
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 | * | |
24 | */ | |
25 | ||
26 | #include <librb_config.h> | |
27 | #include <rb_lib.h> | |
28 | ||
29 | #ifdef HAVE_OPENSSL | |
30 | ||
31 | #include <commio-int.h> | |
32 | #include <commio-ssl.h> | |
33 | ||
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 | ||
58 | ||
59 | ||
60 | /* | |
61 | * Internal OpenSSL-specific code | |
62 | */ | |
63 | ||
64 | static unsigned long | |
65 | rb_ssl_last_err(void) | |
66 | { | |
67 | unsigned long err_saved, err = 0; | |
68 | ||
69 | while((err_saved = ERR_get_error()) != 0) | |
70 | err = err_saved; | |
71 | ||
72 | return err; | |
73 | } | |
74 | ||
75 | static void | |
76 | rb_ssl_init_fd(rb_fde_t *const F, const rb_fd_tls_direction dir) | |
77 | { | |
78 | (void) rb_ssl_last_err(); | |
79 | ||
80 | F->ssl = SSL_new(ssl_ctx); | |
81 | ||
82 | if(F->ssl == NULL) | |
83 | { | |
84 | rb_lib_log("%s: SSL_new: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
85 | rb_close(F); | |
86 | return; | |
87 | } | |
88 | ||
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 | } | |
98 | ||
99 | SSL_set_fd(SSL_P(F), rb_get_fd(F)); | |
100 | } | |
101 | ||
102 | static void | |
103 | rb_ssl_accept_common(rb_fde_t *const F, void *const data) | |
104 | { | |
105 | lrb_assert(F != NULL); | |
106 | lrb_assert(F->accept != NULL); | |
107 | lrb_assert(F->accept->callback != NULL); | |
108 | lrb_assert(F->ssl != NULL); | |
109 | ||
110 | (void) rb_ssl_last_err(); | |
111 | ||
112 | int ret = SSL_do_handshake(SSL_P(F)); | |
113 | int err = SSL_get_error(SSL_P(F), ret); | |
114 | ||
115 | if(ret == 1) | |
116 | { | |
117 | F->handshake_count++; | |
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; | |
138 | } | |
139 | ||
140 | errno = EIO; | |
141 | F->ssl_errno = (unsigned long) err; | |
142 | F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data); | |
143 | } | |
144 | ||
145 | static void | |
146 | rb_ssl_connect_common(rb_fde_t *const F, void *const data) | |
147 | { | |
148 | lrb_assert(F != NULL); | |
149 | lrb_assert(F->ssl != NULL); | |
150 | ||
151 | (void) rb_ssl_last_err(); | |
152 | ||
153 | int ret = SSL_do_handshake(SSL_P(F)); | |
154 | int err = SSL_get_error(SSL_P(F), ret); | |
155 | ||
156 | if(ret == 1) | |
157 | { | |
158 | F->handshake_count++; | |
159 | ||
160 | rb_settimeout(F, 0, NULL, NULL); | |
161 | rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL); | |
162 | ||
163 | rb_ssl_connect_realcb(F, RB_OK, data); | |
164 | ||
165 | return; | |
166 | } | |
167 | if(ret == -1 && err == SSL_ERROR_WANT_READ) | |
168 | { | |
169 | rb_setselect(F, RB_SELECT_READ, rb_ssl_connect_common, data); | |
170 | return; | |
171 | } | |
172 | if(ret == -1 && err == SSL_ERROR_WANT_WRITE) | |
173 | { | |
174 | rb_setselect(F, RB_SELECT_WRITE, rb_ssl_connect_common, data); | |
175 | return; | |
176 | } | |
177 | ||
178 | errno = EIO; | |
179 | F->ssl_errno = (unsigned long) err; | |
180 | rb_ssl_connect_realcb(F, RB_ERROR_SSL, data); | |
181 | } | |
182 | ||
183 | static const char * | |
184 | rb_ssl_strerror(const unsigned long err) | |
185 | { | |
186 | static char errbuf[512]; | |
187 | ||
188 | ERR_error_string_n(err, errbuf, sizeof errbuf); | |
189 | ||
190 | return errbuf; | |
191 | } | |
192 | ||
193 | static int | |
194 | verify_accept_all_cb(const int preverify_ok, X509_STORE_CTX *const x509_ctx) | |
195 | { | |
196 | return 1; | |
197 | } | |
198 | ||
199 | static ssize_t | |
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) | |
201 | { | |
202 | ssize_t ret; | |
203 | unsigned long err; | |
204 | ||
205 | (void) rb_ssl_last_err(); | |
206 | ||
207 | if(r_or_w == 0) | |
208 | ret = (ssize_t) SSL_read(SSL_P(F), rbuf, (int)count); | |
209 | else | |
210 | ret = (ssize_t) SSL_write(SSL_P(F), wbuf, (int)count); | |
211 | ||
212 | if(ret < 0) | |
213 | { | |
214 | switch(SSL_get_error(SSL_P(F), ret)) | |
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: | |
225 | err = rb_ssl_last_err(); | |
226 | if(err == 0) | |
227 | { | |
228 | F->ssl_errno = 0; | |
229 | return RB_RW_IO_ERROR; | |
230 | } | |
231 | break; | |
232 | default: | |
233 | err = rb_ssl_last_err(); | |
234 | break; | |
235 | } | |
236 | ||
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 | ||
248 | static int | |
249 | make_certfp(X509 *const cert, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method) | |
250 | { | |
251 | unsigned int hashlen = 0; | |
252 | const EVP_MD *md_type = NULL; | |
253 | const ASN1_ITEM *item = NULL; | |
254 | void *data = NULL; | |
255 | ||
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 | } | |
291 | ||
292 | if(ASN1_item_digest(item, md_type, data, certfp, &hashlen) != 1) | |
293 | { | |
294 | rb_lib_log("%s: ASN1_item_digest: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
295 | return 0; | |
296 | } | |
297 | ||
298 | return (int) hashlen; | |
299 | } | |
300 | ||
301 | ||
302 | ||
303 | /* | |
304 | * External OpenSSL-specific code | |
305 | */ | |
306 | ||
307 | void | |
308 | rb_ssl_shutdown(rb_fde_t *const F) | |
309 | { | |
310 | if(F == NULL || F->ssl == NULL) | |
311 | return; | |
312 | ||
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; | |
326 | } | |
327 | ||
328 | int | |
329 | rb_init_ssl(void) | |
330 | { | |
331 | #ifndef LRB_SSL_NO_EXPLICIT_INIT | |
332 | (void) SSL_library_init(); | |
333 | SSL_load_error_strings(); | |
334 | #endif | |
335 | ||
336 | rb_lib_log("%s: OpenSSL backend initialised", __func__); | |
337 | return 1; | |
338 | } | |
339 | ||
340 | int | |
341 | rb_setup_ssl_server(const char *const certfile, const char *keyfile, | |
342 | const char *const dhfile, const char *cipherlist) | |
343 | { | |
344 | if(certfile == NULL) | |
345 | { | |
346 | rb_lib_log("%s: no certificate file specified", __func__); | |
347 | return 0; | |
348 | } | |
349 | ||
350 | if(keyfile == NULL) | |
351 | keyfile = certfile; | |
352 | ||
353 | if(cipherlist == NULL) | |
354 | cipherlist = rb_default_ciphers; | |
355 | ||
356 | ||
357 | (void) rb_ssl_last_err(); | |
358 | ||
359 | #ifdef LRB_HAVE_TLS_METHOD_API | |
360 | SSL_CTX *const ssl_ctx_new = SSL_CTX_new(TLS_method()); | |
361 | #else | |
362 | SSL_CTX *const ssl_ctx_new = SSL_CTX_new(SSLv23_method()); | |
363 | #endif | |
364 | ||
365 | if(ssl_ctx_new == NULL) | |
366 | { | |
367 | rb_lib_log("%s: SSL_CTX_new: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
368 | return 0; | |
369 | } | |
370 | ||
371 | if(SSL_CTX_use_certificate_chain_file(ssl_ctx_new, certfile) != 1) | |
372 | { | |
373 | rb_lib_log("%s: SSL_CTX_use_certificate_chain_file ('%s'): %s", __func__, certfile, | |
374 | rb_ssl_strerror(rb_ssl_last_err())); | |
375 | ||
376 | SSL_CTX_free(ssl_ctx_new); | |
377 | return 0; | |
378 | } | |
379 | ||
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())); | |
384 | ||
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); | |
428 | #endif | |
429 | ||
430 | #ifndef LRB_HAVE_TLS_METHOD_API | |
431 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); | |
432 | #endif | |
433 | ||
434 | #ifdef SSL_OP_NO_TLSv1 | |
435 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_TLSv1); | |
436 | #endif | |
437 | ||
438 | #ifdef SSL_OP_NO_TICKET | |
439 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_TICKET); | |
440 | #endif | |
441 | ||
442 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE | |
443 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_CIPHER_SERVER_PREFERENCE); | |
444 | #endif | |
445 | ||
446 | #ifdef SSL_OP_SINGLE_DH_USE | |
447 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_DH_USE); | |
448 | #endif | |
449 | ||
450 | #ifdef SSL_OP_SINGLE_ECDH_USE | |
451 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_ECDH_USE); | |
452 | #endif | |
453 | ||
454 | #ifdef LRB_HAVE_TLS_ECDH_AUTO | |
455 | (void) SSL_CTX_set_ecdh_auto(ssl_ctx_new, 1); | |
456 | #endif | |
457 | ||
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); | |
467 | } | |
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 | |
473 | #endif | |
474 | ||
475 | ||
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 | } | |
485 | ||
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())); | |
497 | return 0; | |
498 | } | |
499 | ||
500 | rb_lib_log("%s: PRNG initialised", __func__); | |
501 | return 1; | |
502 | } | |
503 | ||
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())); | |
512 | return 0; | |
513 | } | |
514 | ||
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 | { | |
527 | if(F == NULL || F->ssl == NULL) | |
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))) | |
537 | { | |
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 | // fallthrough | |
546 | default: | |
547 | X509_free(peer_cert); | |
548 | return len; | |
549 | } | |
550 | } | |
551 | ||
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; | |
564 | } | |
565 | ||
566 | int len = make_certfp(cert, certfp, method); | |
567 | ||
568 | X509_free(cert); | |
569 | fclose(fp); | |
570 | ||
571 | return len; | |
572 | } | |
573 | ||
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 | |
589 | } | |
590 | ||
591 | const char * | |
592 | rb_ssl_get_cipher(rb_fde_t *const F) | |
593 | { | |
594 | if(F == NULL || F->ssl == NULL) | |
595 | return NULL; | |
596 | ||
597 | static char buf[512]; | |
598 | ||
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; | |
605 | } | |
606 | ||
607 | ssize_t | |
608 | rb_ssl_read(rb_fde_t *const F, void *const buf, const size_t count) | |
609 | { | |
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 | */ | |
624 | ||
625 | static void | |
626 | rb_ssl_connect_realcb(rb_fde_t *const F, const int status, struct ssl_connect *const sconn) | |
627 | { | |
628 | lrb_assert(F->connect != NULL); | |
629 | ||
630 | F->connect->callback = sconn->callback; | |
631 | F->connect->data = sconn->data; | |
632 | ||
633 | rb_connect_callback(F, status); | |
634 | rb_free(sconn); | |
635 | } | |
636 | ||
637 | static void | |
638 | rb_ssl_timeout_cb(rb_fde_t *const F, void *const data) | |
639 | { | |
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); | |
644 | } | |
645 | ||
646 | static void | |
647 | rb_ssl_tryconn_timeout_cb(rb_fde_t *const F, void *const data) | |
648 | { | |
649 | rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); | |
650 | } | |
651 | ||
652 | static void | |
653 | rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data) | |
654 | { | |
655 | lrb_assert(F != NULL); | |
656 | ||
657 | struct ssl_connect *const sconn = data; | |
658 | ||
659 | if(status != RB_OK) | |
660 | { | |
661 | rb_ssl_connect_realcb(F, status, sconn); | |
662 | return; | |
663 | } | |
664 | ||
665 | F->type |= RB_FD_SSL; | |
666 | ||
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); | |
670 | } | |
671 | ||
672 | ||
673 | ||
674 | /* | |
675 | * External library-agnostic code | |
676 | */ | |
677 | ||
678 | int | |
679 | rb_supports_ssl(void) | |
680 | { | |
681 | return 1; | |
682 | } | |
683 | ||
684 | unsigned int | |
685 | rb_ssl_handshake_count(rb_fde_t *const F) | |
686 | { | |
687 | return F->handshake_count; | |
688 | } | |
689 | ||
690 | void | |
691 | rb_ssl_clear_handshake_count(rb_fde_t *const F) | |
692 | { | |
693 | F->handshake_count = 0; | |
694 | } | |
695 | ||
696 | void | |
697 | rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout) | |
698 | { | |
699 | F->type |= RB_FD_SSL; | |
700 | ||
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); | |
706 | ||
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); | |
710 | } | |
711 | ||
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) | |
714 | { | |
715 | cli_F->type |= RB_FD_SSL; | |
716 | ||
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); | |
723 | ||
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); | |
727 | } | |
728 | ||
729 | int | |
730 | rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept) | |
731 | { | |
732 | int result = rb_listen(F, backlog, defer_accept); | |
733 | ||
734 | F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; | |
735 | ||
736 | return result; | |
737 | } | |
738 | ||
739 | void | |
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) | |
742 | { | |
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); | |
752 | } | |
753 | ||
754 | void | |
755 | rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data, const int timeout) | |
756 | { | |
757 | if(F == NULL) | |
758 | return; | |
759 | ||
760 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); | |
761 | sconn->data = data; | |
762 | sconn->callback = callback; | |
763 | sconn->timeout = timeout; | |
764 | ||
765 | F->connect = rb_malloc(sizeof(struct conndata)); | |
766 | F->connect->callback = callback; | |
767 | F->connect->data = data; | |
768 | F->type |= RB_FD_SSL; | |
769 | ||
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); | |
773 | } | |
774 | ||
775 | #endif /* HAVE_OPENSSL */ |