2 * libratbox: a library used by ircd-ratbox and other things
3 * openssl.c: openssl related code
5 * Copyright (C) 2007-2008 ircd-ratbox development team
6 * Copyright (C) 2007-2008 Aaron Sethman <androsyn@ratbox.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23 * $Id: commio.c 24808 2008-01-02 08:17:05Z androsyn $
26 #include <libratbox_config.h>
27 #include <ratbox_lib.h>
31 #include <commio-int.h>
32 #include <commio-ssl.h>
33 #include <openssl/ssl.h>
34 #include <openssl/dh.h>
35 #include <openssl/err.h>
36 #include <openssl/evp.h>
37 #include <openssl/rand.h>
39 static SSL_CTX
*ssl_server_ctx
;
40 static SSL_CTX
*ssl_client_ctx
;
41 static int libratbox_index
= -1;
46 unsigned long t_err
, err
= 0;
47 err
= ERR_get_error();
51 while((t_err
= ERR_get_error()) > 0)
58 rb_ssl_shutdown(rb_fde_t
*F
)
61 if(F
== NULL
|| F
->ssl
== NULL
)
63 SSL_set_shutdown((SSL
*) F
->ssl
, SSL_RECEIVED_SHUTDOWN
);
65 for(i
= 0; i
< 4; i
++)
67 if(SSL_shutdown((SSL
*) F
->ssl
))
71 SSL_free((SSL
*) F
->ssl
);
75 rb_ssl_handshake_count(rb_fde_t
*F
)
77 return F
->handshake_count
;
81 rb_ssl_clear_handshake_count(rb_fde_t
*F
)
83 F
->handshake_count
= 0;
87 rb_ssl_timeout(rb_fde_t
*F
, void *notused
)
89 lrb_assert(F
->accept
!= NULL
);
90 F
->accept
->callback(F
, RB_ERR_TIMEOUT
, NULL
, 0, F
->accept
->data
);
95 rb_ssl_info_callback(SSL
* ssl
, int where
, int ret
)
97 if(where
& SSL_CB_HANDSHAKE_START
)
99 rb_fde_t
*F
= SSL_get_ex_data(ssl
, libratbox_index
);
102 F
->handshake_count
++;
107 rb_setup_ssl_cb(rb_fde_t
*F
)
109 SSL_set_ex_data(F
->ssl
, libratbox_index
, (char *)F
);
110 SSL_set_info_callback((SSL
*) F
->ssl
, (void (*)(const SSL
*,int,int))rb_ssl_info_callback
);
114 rb_ssl_tryaccept(rb_fde_t
*F
, void *data
)
117 lrb_assert(F
->accept
!= NULL
);
119 struct acceptdata
*ad
;
121 if(!SSL_is_init_finished((SSL
*) F
->ssl
))
123 if((ssl_err
= SSL_accept((SSL
*) F
->ssl
)) <= 0)
125 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
127 case SSL_ERROR_WANT_READ
:
128 case SSL_ERROR_WANT_WRITE
:
129 if(ssl_err
== SSL_ERROR_WANT_WRITE
)
130 flags
= RB_SELECT_WRITE
;
132 flags
= RB_SELECT_READ
;
133 F
->ssl_errno
= get_last_err();
134 rb_setselect(F
, flags
, rb_ssl_tryaccept
, NULL
);
136 case SSL_ERROR_SYSCALL
:
137 F
->accept
->callback(F
, RB_ERROR
, NULL
, 0, F
->accept
->data
);
140 F
->ssl_errno
= get_last_err();
141 F
->accept
->callback(F
, RB_ERROR_SSL
, NULL
, 0, F
->accept
->data
);
147 rb_settimeout(F
, 0, NULL
, NULL
);
148 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
, NULL
, NULL
);
152 ad
->callback(F
, RB_OK
, (struct sockaddr
*)&ad
->S
, ad
->addrlen
, ad
->data
);
159 rb_ssl_accept_common(rb_fde_t
*new_F
)
162 if((ssl_err
= SSL_accept((SSL
*) new_F
->ssl
)) <= 0)
164 switch (ssl_err
= SSL_get_error((SSL
*) new_F
->ssl
, ssl_err
))
166 case SSL_ERROR_SYSCALL
:
167 if(rb_ignore_errno(errno
))
168 case SSL_ERROR_WANT_READ
:
169 case SSL_ERROR_WANT_WRITE
:
171 new_F
->ssl_errno
= get_last_err();
172 rb_setselect(new_F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
173 rb_ssl_tryaccept
, NULL
);
177 new_F
->ssl_errno
= get_last_err();
178 new_F
->accept
->callback(new_F
, RB_ERROR_SSL
, NULL
, 0, new_F
->accept
->data
);
184 rb_ssl_tryaccept(new_F
, NULL
);
189 rb_ssl_start_accepted(rb_fde_t
*new_F
, ACCB
* cb
, void *data
, int timeout
)
191 new_F
->type
|= RB_FD_SSL
;
192 new_F
->ssl
= SSL_new(ssl_server_ctx
);
193 new_F
->accept
= rb_malloc(sizeof(struct acceptdata
));
195 new_F
->accept
->callback
= cb
;
196 new_F
->accept
->data
= data
;
197 rb_settimeout(new_F
, timeout
, rb_ssl_timeout
, NULL
);
199 new_F
->accept
->addrlen
= 0;
200 SSL_set_fd((SSL
*) new_F
->ssl
, rb_get_fd(new_F
));
201 rb_setup_ssl_cb(new_F
);
202 rb_ssl_accept_common(new_F
);
209 rb_ssl_accept_setup(rb_fde_t
*F
, rb_fde_t
*new_F
, struct sockaddr
*st
, int addrlen
)
211 new_F
->type
|= RB_FD_SSL
;
212 new_F
->ssl
= SSL_new(ssl_server_ctx
);
213 new_F
->accept
= rb_malloc(sizeof(struct acceptdata
));
215 new_F
->accept
->callback
= F
->accept
->callback
;
216 new_F
->accept
->data
= F
->accept
->data
;
217 rb_settimeout(new_F
, 10, rb_ssl_timeout
, NULL
);
218 memcpy(&new_F
->accept
->S
, st
, addrlen
);
219 new_F
->accept
->addrlen
= addrlen
;
221 SSL_set_fd((SSL
*) new_F
->ssl
, rb_get_fd(new_F
));
222 rb_setup_ssl_cb(new_F
);
223 rb_ssl_accept_common(new_F
);
227 rb_ssl_read_or_write(int r_or_w
, rb_fde_t
*F
, void *rbuf
, const void *wbuf
, size_t count
)
234 ret
= (ssize_t
) SSL_read(ssl
, rbuf
, (int)count
);
236 ret
= (ssize_t
) SSL_write(ssl
, wbuf
, (int)count
);
240 switch (SSL_get_error(ssl
, ret
))
242 case SSL_ERROR_WANT_READ
:
244 return RB_RW_SSL_NEED_READ
;
245 case SSL_ERROR_WANT_WRITE
:
247 return RB_RW_SSL_NEED_WRITE
;
248 case SSL_ERROR_ZERO_RETURN
:
250 case SSL_ERROR_SYSCALL
:
251 err
= get_last_err();
255 return RB_RW_IO_ERROR
;
259 err
= get_last_err();
265 errno
= EIO
; /* not great but... */
266 return RB_RW_SSL_ERROR
;
268 return RB_RW_IO_ERROR
;
274 rb_ssl_read(rb_fde_t
*F
, void *buf
, size_t count
)
276 return rb_ssl_read_or_write(0, F
, buf
, NULL
, count
);
280 rb_ssl_write(rb_fde_t
*F
, const void *buf
, size_t count
)
282 return rb_ssl_read_or_write(1, F
, NULL
, buf
, count
);
286 verify_accept_all_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
292 get_ssl_error(unsigned long err
)
294 static char buf
[512];
296 ERR_error_string_n(err
, buf
, sizeof buf
);
304 char libratbox_data
[] = "libratbox data";
305 SSL_load_error_strings();
307 libratbox_index
= SSL_get_ex_new_index(0, libratbox_data
, NULL
, NULL
, NULL
);
308 ssl_server_ctx
= SSL_CTX_new(SSLv23_server_method());
309 if(ssl_server_ctx
== NULL
)
311 rb_lib_log("rb_init_openssl: Unable to initialize OpenSSL server context: %s",
312 get_ssl_error(ERR_get_error()));
315 /* Disable SSLv2, make the client use our settings */
316 SSL_CTX_set_options(ssl_server_ctx
, SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
| SSL_OP_CIPHER_SERVER_PREFERENCE
317 #ifdef SSL_OP_SINGLE_DH_USE
318 | SSL_OP_SINGLE_DH_USE
320 #ifdef SSL_OP_NO_TICKET
324 SSL_CTX_set_verify(ssl_server_ctx
, SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
, verify_accept_all_cb
);
325 SSL_CTX_set_session_cache_mode(ssl_server_ctx
, SSL_SESS_CACHE_OFF
);
326 SSL_CTX_set_cipher_list(ssl_server_ctx
, "kEECDH+HIGH:kEDH+HIGH:HIGH:!RC4:!aNULL");
328 /* Set ECDHE on OpenSSL 1.00+, but make sure it's actually available because redhat are dicks
329 and bastardise their OpenSSL for stupid reasons... */
330 #if (OPENSSL_VERSION_NUMBER >= 0x10000000) && defined(NID_secp384r1)
331 EC_KEY
*key
= EC_KEY_new_by_curve_name(NID_secp384r1
);
333 SSL_CTX_set_tmp_ecdh(ssl_server_ctx
, key
);
336 #ifdef SSL_OP_SINGLE_ECDH_USE
337 SSL_CTX_set_options(ssl_server_ctx
, SSL_OP_SINGLE_ECDH_USE
);
341 ssl_client_ctx
= SSL_CTX_new(TLSv1_client_method());
343 if(ssl_client_ctx
== NULL
)
345 rb_lib_log("rb_init_openssl: Unable to initialize OpenSSL client context: %s",
346 get_ssl_error(ERR_get_error()));
350 #ifdef SSL_OP_NO_TICKET
351 SSL_CTX_set_options(ssl_client_ctx
, SSL_OP_NO_TICKET
);
359 rb_setup_ssl_server(const char *cert
, const char *keyfile
, const char *dhfile
)
365 rb_lib_log("rb_setup_ssl_server: No certificate file");
368 if(!SSL_CTX_use_certificate_chain_file(ssl_server_ctx
, cert
) || !SSL_CTX_use_certificate_chain_file(ssl_client_ctx
, cert
))
370 err
= ERR_get_error();
371 rb_lib_log("rb_setup_ssl_server: Error loading certificate file [%s]: %s", cert
,
378 rb_lib_log("rb_setup_ssl_server: No key file");
383 if(!SSL_CTX_use_PrivateKey_file(ssl_server_ctx
, keyfile
, SSL_FILETYPE_PEM
) || !SSL_CTX_use_PrivateKey_file(ssl_client_ctx
, keyfile
, SSL_FILETYPE_PEM
))
385 err
= ERR_get_error();
386 rb_lib_log("rb_setup_ssl_server: Error loading keyfile [%s]: %s", keyfile
,
393 /* DH parameters aren't necessary, but they are nice..if they didn't pass one..that is their problem */
394 BIO
*bio
= BIO_new_file(dhfile
, "r");
397 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
400 err
= ERR_get_error();
402 ("rb_setup_ssl_server: Error loading DH params file [%s]: %s",
403 dhfile
, get_ssl_error(err
));
408 SSL_CTX_set_tmp_dh(ssl_server_ctx
, dh
);
412 err
= ERR_get_error();
413 rb_lib_log("rb_setup_ssl_server: Error loading DH params file [%s]: %s",
414 dhfile
, get_ssl_error(err
));
421 rb_ssl_listen(rb_fde_t
*F
, int backlog
, int defer_accept
)
425 result
= rb_listen(F
, backlog
, defer_accept
);
426 F
->type
= RB_FD_SOCKET
| RB_FD_LISTEN
| RB_FD_SSL
;
439 rb_ssl_connect_realcb(rb_fde_t
*F
, int status
, struct ssl_connect
*sconn
)
441 F
->connect
->callback
= sconn
->callback
;
442 F
->connect
->data
= sconn
->data
;
444 rb_connect_callback(F
, status
);
448 rb_ssl_tryconn_timeout_cb(rb_fde_t
*F
, void *data
)
450 rb_ssl_connect_realcb(F
, RB_ERR_TIMEOUT
, data
);
454 rb_ssl_tryconn_cb(rb_fde_t
*F
, void *data
)
456 struct ssl_connect
*sconn
= data
;
458 if(!SSL_is_init_finished((SSL
*) F
->ssl
))
460 if((ssl_err
= SSL_connect((SSL
*) F
->ssl
)) <= 0)
462 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
464 case SSL_ERROR_SYSCALL
:
465 if(rb_ignore_errno(errno
))
466 case SSL_ERROR_WANT_READ
:
467 case SSL_ERROR_WANT_WRITE
:
469 F
->ssl_errno
= get_last_err();
470 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
471 rb_ssl_tryconn_cb
, sconn
);
475 F
->ssl_errno
= get_last_err();
476 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
482 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
488 rb_ssl_tryconn(rb_fde_t
*F
, int status
, void *data
)
490 struct ssl_connect
*sconn
= data
;
494 rb_ssl_connect_realcb(F
, status
, sconn
);
498 F
->type
|= RB_FD_SSL
;
499 F
->ssl
= SSL_new(ssl_client_ctx
);
500 SSL_set_fd((SSL
*) F
->ssl
, F
->fd
);
502 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
503 if((ssl_err
= SSL_connect((SSL
*) F
->ssl
)) <= 0)
505 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
507 case SSL_ERROR_SYSCALL
:
508 if(rb_ignore_errno(errno
))
509 case SSL_ERROR_WANT_READ
:
510 case SSL_ERROR_WANT_WRITE
:
512 F
->ssl_errno
= get_last_err();
513 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
514 rb_ssl_tryconn_cb
, sconn
);
518 F
->ssl_errno
= get_last_err();
519 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
525 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
530 rb_connect_tcp_ssl(rb_fde_t
*F
, struct sockaddr
*dest
,
531 struct sockaddr
*clocal
, int socklen
, CNCB
* callback
, void *data
, int timeout
)
533 struct ssl_connect
*sconn
;
537 sconn
= rb_malloc(sizeof(struct ssl_connect
));
539 sconn
->callback
= callback
;
540 sconn
->timeout
= timeout
;
541 rb_connect_tcp(F
, dest
, clocal
, socklen
, rb_ssl_tryconn
, sconn
, timeout
);
546 rb_ssl_start_connected(rb_fde_t
*F
, CNCB
* callback
, void *data
, int timeout
)
548 struct ssl_connect
*sconn
;
553 sconn
= rb_malloc(sizeof(struct ssl_connect
));
555 sconn
->callback
= callback
;
556 sconn
->timeout
= timeout
;
557 F
->connect
= rb_malloc(sizeof(struct conndata
));
558 F
->connect
->callback
= callback
;
559 F
->connect
->data
= data
;
560 F
->type
|= RB_FD_SSL
;
561 F
->ssl
= SSL_new(ssl_client_ctx
);
563 SSL_set_fd((SSL
*) F
->ssl
, F
->fd
);
565 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
566 if((ssl_err
= SSL_connect((SSL
*) F
->ssl
)) <= 0)
568 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
570 case SSL_ERROR_SYSCALL
:
571 if(rb_ignore_errno(errno
))
572 case SSL_ERROR_WANT_READ
:
573 case SSL_ERROR_WANT_WRITE
:
575 F
->ssl_errno
= get_last_err();
576 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
577 rb_ssl_tryconn_cb
, sconn
);
581 F
->ssl_errno
= get_last_err();
582 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
588 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
593 rb_init_prng(const char *path
, prng_seed_t seed_type
)
595 if(seed_type
== RB_PRNG_DEFAULT
)
600 return RAND_status();
603 return RAND_status();
608 if(RAND_load_file(path
, -1) == -1)
620 return RAND_status();
624 rb_get_random(void *buf
, size_t length
)
628 if((ret
= RAND_bytes(buf
, length
)) == 0)
630 /* remove the error from the queue */
637 rb_get_pseudo_random(void *buf
, size_t length
)
640 ret
= RAND_pseudo_bytes(buf
, length
);
647 rb_get_ssl_strerror(rb_fde_t
*F
)
649 return get_ssl_error(F
->ssl_errno
);
653 rb_get_ssl_certfp(rb_fde_t
*F
, uint8_t certfp
[RB_SSL_CERTFP_LEN
])
661 cert
= SSL_get_peer_certificate((SSL
*) F
->ssl
);
664 res
= SSL_get_verify_result((SSL
*) F
->ssl
);
667 res
== X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
||
668 res
== X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
||
669 res
== X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
||
670 res
== X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
)
672 unsigned int certfp_length
= RB_SSL_CERTFP_LEN
;
673 X509_digest(cert
, EVP_sha1(), certfp
, &certfp_length
);
684 rb_supports_ssl(void)
690 rb_get_ssl_info(char *buf
, size_t len
)
692 rb_snprintf(buf
, len
, "Using SSL: %s compiled: 0x%lx, library 0x%lx",
693 SSLeay_version(SSLEAY_VERSION
),
694 (long)OPENSSL_VERSION_NUMBER
, SSLeay());
698 #endif /* HAVE_OPESSL */