]>
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 | return 0; | |
294 | ||
295 | return (int) hashlen; | |
296 | } | |
297 | ||
298 | ||
299 | ||
300 | /* | |
301 | * External OpenSSL-specific code | |
302 | */ | |
303 | ||
304 | void | |
305 | rb_ssl_shutdown(rb_fde_t *const F) | |
306 | { | |
307 | if(F == NULL || F->ssl == NULL) | |
308 | return; | |
309 | ||
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; | |
323 | } | |
324 | ||
325 | int | |
326 | rb_init_ssl(void) | |
327 | { | |
328 | #ifndef LRB_SSL_NO_EXPLICIT_INIT | |
329 | (void) SSL_library_init(); | |
330 | SSL_load_error_strings(); | |
331 | #endif | |
332 | ||
333 | rb_lib_log("%s: OpenSSL backend initialised", __func__); | |
334 | return 1; | |
335 | } | |
336 | ||
337 | int | |
338 | rb_setup_ssl_server(const char *const certfile, const char *keyfile, | |
339 | const char *const dhfile, const char *cipherlist) | |
340 | { | |
341 | if(certfile == NULL) | |
342 | { | |
343 | rb_lib_log("%s: no certificate file specified", __func__); | |
344 | return 0; | |
345 | } | |
346 | ||
347 | if(keyfile == NULL) | |
348 | keyfile = certfile; | |
349 | ||
350 | if(cipherlist == NULL) | |
351 | cipherlist = rb_default_ciphers; | |
352 | ||
353 | ||
354 | (void) rb_ssl_last_err(); | |
355 | ||
356 | #ifdef LRB_HAVE_TLS_METHOD_API | |
357 | SSL_CTX *const ssl_ctx_new = SSL_CTX_new(TLS_method()); | |
358 | #else | |
359 | SSL_CTX *const ssl_ctx_new = SSL_CTX_new(SSLv23_method()); | |
360 | #endif | |
361 | ||
362 | if(ssl_ctx_new == NULL) | |
363 | { | |
364 | rb_lib_log("%s: SSL_CTX_new: %s", __func__, rb_ssl_strerror(rb_ssl_last_err())); | |
365 | return 0; | |
366 | } | |
367 | ||
368 | if(SSL_CTX_use_certificate_chain_file(ssl_ctx_new, certfile) != 1) | |
369 | { | |
370 | rb_lib_log("%s: SSL_CTX_use_certificate_chain_file ('%s'): %s", __func__, certfile, | |
371 | rb_ssl_strerror(rb_ssl_last_err())); | |
372 | ||
373 | SSL_CTX_free(ssl_ctx_new); | |
374 | return 0; | |
375 | } | |
376 | ||
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())); | |
381 | ||
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); | |
425 | #endif | |
426 | ||
427 | #ifndef LRB_HAVE_TLS_METHOD_API | |
428 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); | |
429 | #endif | |
430 | ||
431 | #ifdef SSL_OP_NO_TICKET | |
432 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_TICKET); | |
433 | #endif | |
434 | ||
435 | #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE | |
436 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_CIPHER_SERVER_PREFERENCE); | |
437 | #endif | |
438 | ||
439 | #ifdef SSL_OP_SINGLE_DH_USE | |
440 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_DH_USE); | |
441 | #endif | |
442 | ||
443 | #ifdef SSL_OP_SINGLE_ECDH_USE | |
444 | (void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_ECDH_USE); | |
445 | #endif | |
446 | ||
447 | #ifdef LRB_HAVE_TLS_ECDH_AUTO | |
448 | (void) SSL_CTX_set_ecdh_auto(ssl_ctx_new, 1); | |
449 | #endif | |
450 | ||
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); | |
460 | } | |
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 | |
466 | #endif | |
467 | ||
468 | ||
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 | } | |
478 | ||
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())); | |
490 | return 0; | |
491 | } | |
492 | ||
493 | rb_lib_log("%s: PRNG initialised", __func__); | |
494 | return 1; | |
495 | } | |
496 | ||
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())); | |
505 | return 0; | |
506 | } | |
507 | ||
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))) | |
530 | { | |
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 | } | |
543 | ||
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; | |
556 | } | |
557 | ||
558 | int len = make_certfp(cert, certfp, method); | |
559 | ||
560 | X509_free(cert); | |
561 | fclose(fp); | |
562 | ||
563 | return len; | |
564 | } | |
565 | ||
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 | |
581 | } | |
582 | ||
583 | const char * | |
584 | rb_ssl_get_cipher(rb_fde_t *const F) | |
585 | { | |
586 | if(F == NULL || F->ssl == NULL) | |
587 | return NULL; | |
588 | ||
589 | static char buf[512]; | |
590 | ||
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; | |
597 | } | |
598 | ||
599 | ssize_t | |
600 | rb_ssl_read(rb_fde_t *const F, void *const buf, const size_t count) | |
601 | { | |
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 | */ | |
616 | ||
617 | static void | |
618 | rb_ssl_connect_realcb(rb_fde_t *const F, const int status, struct ssl_connect *const sconn) | |
619 | { | |
620 | lrb_assert(F->connect != NULL); | |
621 | ||
622 | F->connect->callback = sconn->callback; | |
623 | F->connect->data = sconn->data; | |
624 | ||
625 | rb_connect_callback(F, status); | |
626 | rb_free(sconn); | |
627 | } | |
628 | ||
629 | static void | |
630 | rb_ssl_timeout_cb(rb_fde_t *const F, void *const data) | |
631 | { | |
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); | |
636 | } | |
637 | ||
638 | static void | |
639 | rb_ssl_tryconn_timeout_cb(rb_fde_t *const F, void *const data) | |
640 | { | |
641 | rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); | |
642 | } | |
643 | ||
644 | static void | |
645 | rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data) | |
646 | { | |
647 | lrb_assert(F != NULL); | |
648 | ||
649 | struct ssl_connect *const sconn = data; | |
650 | ||
651 | if(status != RB_OK) | |
652 | { | |
653 | rb_ssl_connect_realcb(F, status, sconn); | |
654 | return; | |
655 | } | |
656 | ||
657 | F->type |= RB_FD_SSL; | |
658 | ||
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); | |
662 | } | |
663 | ||
664 | ||
665 | ||
666 | /* | |
667 | * External library-agnostic code | |
668 | */ | |
669 | ||
670 | int | |
671 | rb_supports_ssl(void) | |
672 | { | |
673 | return 1; | |
674 | } | |
675 | ||
676 | unsigned int | |
677 | rb_ssl_handshake_count(rb_fde_t *const F) | |
678 | { | |
679 | return F->handshake_count; | |
680 | } | |
681 | ||
682 | void | |
683 | rb_ssl_clear_handshake_count(rb_fde_t *const F) | |
684 | { | |
685 | F->handshake_count = 0; | |
686 | } | |
687 | ||
688 | void | |
689 | rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout) | |
690 | { | |
691 | F->type |= RB_FD_SSL; | |
692 | ||
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); | |
698 | ||
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); | |
702 | } | |
703 | ||
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) | |
706 | { | |
707 | cli_F->type |= RB_FD_SSL; | |
708 | ||
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); | |
715 | ||
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); | |
719 | } | |
720 | ||
721 | int | |
722 | rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept) | |
723 | { | |
724 | int result = rb_listen(F, backlog, defer_accept); | |
725 | ||
726 | F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; | |
727 | ||
728 | return result; | |
729 | } | |
730 | ||
731 | void | |
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) | |
734 | { | |
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); | |
744 | } | |
745 | ||
746 | void | |
747 | rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data, const int timeout) | |
748 | { | |
749 | if(F == NULL) | |
750 | return; | |
751 | ||
752 | struct ssl_connect *const sconn = rb_malloc(sizeof *sconn); | |
753 | sconn->data = data; | |
754 | sconn->callback = callback; | |
755 | sconn->timeout = timeout; | |
756 | ||
757 | F->connect = rb_malloc(sizeof(struct conndata)); | |
758 | F->connect->callback = callback; | |
759 | F->connect->data = data; | |
760 | F->type |= RB_FD_SSL; | |
761 | ||
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); | |
765 | } | |
766 | ||
767 | #endif /* HAVE_OPENSSL */ |