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 const char libratbox_ciphers
[] = "kEECDH+HIGH:kEDH+HIGH:HIGH:!RC4:!aNULL";
306 SSL_load_error_strings();
308 libratbox_index
= SSL_get_ex_new_index(0, libratbox_data
, NULL
, NULL
, NULL
);
310 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
311 ssl_server_ctx
= SSL_CTX_new(SSLv23_server_method());
313 ssl_server_ctx
= SSL_CTX_new(TLS_server_method());
316 if(ssl_server_ctx
== NULL
)
318 rb_lib_log("rb_init_openssl: Unable to initialize OpenSSL server context: %s",
319 get_ssl_error(ERR_get_error()));
323 long server_options
= SSL_CTX_get_options(ssl_server_ctx
);
325 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
326 server_options
|= SSL_OP_NO_SSLv2
;
327 server_options
|= SSL_OP_NO_SSLv3
;
330 #ifdef SSL_OP_SINGLE_DH_USE
331 server_options
|= SSL_OP_SINGLE_DH_USE
;
334 #ifdef SSL_OP_SINGLE_ECDH_USE
335 server_options
|= SSL_OP_SINGLE_ECDH_USE
;
338 #ifdef SSL_OP_NO_TICKET
339 server_options
|= SSL_OP_NO_TICKET
;
342 server_options
|= SSL_OP_CIPHER_SERVER_PREFERENCE
;
344 SSL_CTX_set_options(ssl_server_ctx
, server_options
);
345 SSL_CTX_set_verify(ssl_server_ctx
, SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
, verify_accept_all_cb
);
346 SSL_CTX_set_session_cache_mode(ssl_server_ctx
, SSL_SESS_CACHE_OFF
);
347 SSL_CTX_set_cipher_list(ssl_server_ctx
, libratbox_ciphers
);
349 /* Set ECDHE on OpenSSL 1.00+, but make sure it's actually available because redhat are dicks
350 and bastardise their OpenSSL for stupid reasons... */
351 #if (OPENSSL_VERSION_NUMBER >= 0x10000000L) && defined(NID_secp384r1)
352 EC_KEY
*key
= EC_KEY_new_by_curve_name(NID_secp384r1
);
354 SSL_CTX_set_tmp_ecdh(ssl_server_ctx
, key
);
359 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
360 ssl_client_ctx
= SSL_CTX_new(TLSv1_client_method());
362 ssl_client_ctx
= SSL_CTX_new(TLS_client_method());
365 if(ssl_client_ctx
== NULL
)
367 rb_lib_log("rb_init_openssl: Unable to initialize OpenSSL client context: %s",
368 get_ssl_error(ERR_get_error()));
372 #ifdef SSL_OP_NO_TICKET
373 SSL_CTX_set_options(ssl_client_ctx
, SSL_OP_NO_TICKET
);
376 SSL_CTX_set_cipher_list(ssl_client_ctx
, libratbox_ciphers
);
383 rb_setup_ssl_server(const char *cert
, const char *keyfile
, const char *dhfile
)
389 rb_lib_log("rb_setup_ssl_server: No certificate file");
392 if(!SSL_CTX_use_certificate_chain_file(ssl_server_ctx
, cert
) || !SSL_CTX_use_certificate_chain_file(ssl_client_ctx
, cert
))
394 err
= ERR_get_error();
395 rb_lib_log("rb_setup_ssl_server: Error loading certificate file [%s]: %s", cert
,
402 rb_lib_log("rb_setup_ssl_server: No key file");
407 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
))
409 err
= ERR_get_error();
410 rb_lib_log("rb_setup_ssl_server: Error loading keyfile [%s]: %s", keyfile
,
417 /* DH parameters aren't necessary, but they are nice..if they didn't pass one..that is their problem */
418 BIO
*bio
= BIO_new_file(dhfile
, "r");
421 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
424 err
= ERR_get_error();
426 ("rb_setup_ssl_server: Error loading DH params file [%s]: %s",
427 dhfile
, get_ssl_error(err
));
432 SSL_CTX_set_tmp_dh(ssl_server_ctx
, dh
);
436 err
= ERR_get_error();
437 rb_lib_log("rb_setup_ssl_server: Error loading DH params file [%s]: %s",
438 dhfile
, get_ssl_error(err
));
445 rb_ssl_listen(rb_fde_t
*F
, int backlog
, int defer_accept
)
449 result
= rb_listen(F
, backlog
, defer_accept
);
450 F
->type
= RB_FD_SOCKET
| RB_FD_LISTEN
| RB_FD_SSL
;
463 rb_ssl_connect_realcb(rb_fde_t
*F
, int status
, struct ssl_connect
*sconn
)
465 F
->connect
->callback
= sconn
->callback
;
466 F
->connect
->data
= sconn
->data
;
468 rb_connect_callback(F
, status
);
472 rb_ssl_tryconn_timeout_cb(rb_fde_t
*F
, void *data
)
474 rb_ssl_connect_realcb(F
, RB_ERR_TIMEOUT
, data
);
478 rb_ssl_tryconn_cb(rb_fde_t
*F
, void *data
)
480 struct ssl_connect
*sconn
= data
;
482 if(!SSL_is_init_finished((SSL
*) F
->ssl
))
484 if((ssl_err
= SSL_connect((SSL
*) F
->ssl
)) <= 0)
486 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
488 case SSL_ERROR_SYSCALL
:
489 if(rb_ignore_errno(errno
))
490 case SSL_ERROR_WANT_READ
:
491 case SSL_ERROR_WANT_WRITE
:
493 F
->ssl_errno
= get_last_err();
494 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
495 rb_ssl_tryconn_cb
, sconn
);
499 F
->ssl_errno
= get_last_err();
500 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
506 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
512 rb_ssl_tryconn(rb_fde_t
*F
, int status
, void *data
)
514 struct ssl_connect
*sconn
= data
;
518 rb_ssl_connect_realcb(F
, status
, sconn
);
522 F
->type
|= RB_FD_SSL
;
523 F
->ssl
= SSL_new(ssl_client_ctx
);
524 SSL_set_fd((SSL
*) F
->ssl
, F
->fd
);
526 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
527 if((ssl_err
= SSL_connect((SSL
*) F
->ssl
)) <= 0)
529 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
531 case SSL_ERROR_SYSCALL
:
532 if(rb_ignore_errno(errno
))
533 case SSL_ERROR_WANT_READ
:
534 case SSL_ERROR_WANT_WRITE
:
536 F
->ssl_errno
= get_last_err();
537 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
538 rb_ssl_tryconn_cb
, sconn
);
542 F
->ssl_errno
= get_last_err();
543 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
549 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
554 rb_connect_tcp_ssl(rb_fde_t
*F
, struct sockaddr
*dest
,
555 struct sockaddr
*clocal
, int socklen
, CNCB
* callback
, void *data
, int timeout
)
557 struct ssl_connect
*sconn
;
561 sconn
= rb_malloc(sizeof(struct ssl_connect
));
563 sconn
->callback
= callback
;
564 sconn
->timeout
= timeout
;
565 rb_connect_tcp(F
, dest
, clocal
, socklen
, rb_ssl_tryconn
, sconn
, timeout
);
570 rb_ssl_start_connected(rb_fde_t
*F
, CNCB
* callback
, void *data
, int timeout
)
572 struct ssl_connect
*sconn
;
577 sconn
= rb_malloc(sizeof(struct ssl_connect
));
579 sconn
->callback
= callback
;
580 sconn
->timeout
= timeout
;
581 F
->connect
= rb_malloc(sizeof(struct conndata
));
582 F
->connect
->callback
= callback
;
583 F
->connect
->data
= data
;
584 F
->type
|= RB_FD_SSL
;
585 F
->ssl
= SSL_new(ssl_client_ctx
);
587 SSL_set_fd((SSL
*) F
->ssl
, F
->fd
);
589 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
590 if((ssl_err
= SSL_connect((SSL
*) F
->ssl
)) <= 0)
592 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
594 case SSL_ERROR_SYSCALL
:
595 if(rb_ignore_errno(errno
))
596 case SSL_ERROR_WANT_READ
:
597 case SSL_ERROR_WANT_WRITE
:
599 F
->ssl_errno
= get_last_err();
600 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
601 rb_ssl_tryconn_cb
, sconn
);
605 F
->ssl_errno
= get_last_err();
606 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
612 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
617 rb_init_prng(const char *path
, prng_seed_t seed_type
)
619 if(seed_type
== RB_PRNG_DEFAULT
)
624 return RAND_status();
627 return RAND_status();
632 if(RAND_load_file(path
, -1) == -1)
644 return RAND_status();
648 rb_get_random(void *buf
, size_t length
)
652 if((ret
= RAND_bytes(buf
, length
)) == 0)
654 /* remove the error from the queue */
661 rb_get_pseudo_random(void *buf
, size_t length
)
664 ret
= RAND_pseudo_bytes(buf
, length
);
671 rb_get_ssl_strerror(rb_fde_t
*F
)
673 return get_ssl_error(F
->ssl_errno
);
677 rb_get_ssl_certfp(rb_fde_t
*F
, uint8_t certfp
[RB_SSL_CERTFP_LEN
])
685 cert
= SSL_get_peer_certificate((SSL
*) F
->ssl
);
688 res
= SSL_get_verify_result((SSL
*) F
->ssl
);
691 res
== X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
||
692 res
== X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
||
693 res
== X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
||
694 res
== X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
)
696 unsigned int certfp_length
= RB_SSL_CERTFP_LEN
;
697 X509_digest(cert
, EVP_sha1(), certfp
, &certfp_length
);
708 rb_supports_ssl(void)
714 rb_get_ssl_info(char *buf
, size_t len
)
716 rb_snprintf(buf
, len
, "Using SSL: %s compiled: 0x%lx, library 0x%lx",
717 SSLeay_version(SSLEAY_VERSION
),
718 (long)OPENSSL_VERSION_NUMBER
, SSLeay());
722 #endif /* HAVE_OPESSL */