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/rand.h>
38 static SSL_CTX
*ssl_server_ctx
;
39 static SSL_CTX
*ssl_client_ctx
;
40 static int libratbox_index
= -1;
45 unsigned long t_err
, err
= 0;
46 err
= ERR_get_error();
50 while((t_err
= ERR_get_error()) > 0)
57 rb_ssl_shutdown(rb_fde_t
*F
)
60 if(F
== NULL
|| F
->ssl
== NULL
)
62 SSL_set_shutdown((SSL
*) F
->ssl
, SSL_RECEIVED_SHUTDOWN
);
64 for(i
= 0; i
< 4; i
++)
66 if(SSL_shutdown((SSL
*) F
->ssl
))
70 SSL_free((SSL
*) F
->ssl
);
74 rb_ssl_handshake_count(rb_fde_t
*F
)
76 return F
->handshake_count
;
80 rb_ssl_clear_handshake_count(rb_fde_t
*F
)
82 F
->handshake_count
= 0;
86 rb_ssl_timeout(rb_fde_t
*F
, void *notused
)
88 lrb_assert(F
->accept
!= NULL
);
89 F
->accept
->callback(F
, RB_ERR_TIMEOUT
, NULL
, 0, F
->accept
->data
);
94 rb_ssl_info_callback(SSL
* ssl
, int where
, int ret
)
96 if(where
& SSL_CB_HANDSHAKE_START
)
98 rb_fde_t
*F
= SSL_get_ex_data(ssl
, libratbox_index
);
101 F
->handshake_count
++;
106 rb_setup_ssl_cb(rb_fde_t
*F
)
108 SSL_set_ex_data(F
->ssl
, libratbox_index
, (char *)F
);
109 SSL_set_info_callback((SSL
*) F
->ssl
, (void (*)(const SSL
*,int,int))rb_ssl_info_callback
);
113 rb_ssl_tryaccept(rb_fde_t
*F
, void *data
)
116 lrb_assert(F
->accept
!= NULL
);
118 struct acceptdata
*ad
;
120 if(!SSL_is_init_finished((SSL
*) F
->ssl
))
122 if((ssl_err
= SSL_accept((SSL
*) F
->ssl
)) <= 0)
124 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
126 case SSL_ERROR_WANT_READ
:
127 case SSL_ERROR_WANT_WRITE
:
128 if(ssl_err
== SSL_ERROR_WANT_WRITE
)
129 flags
= RB_SELECT_WRITE
;
131 flags
= RB_SELECT_READ
;
132 F
->ssl_errno
= get_last_err();
133 rb_setselect(F
, flags
, rb_ssl_tryaccept
, NULL
);
135 case SSL_ERROR_SYSCALL
:
136 F
->accept
->callback(F
, RB_ERROR
, NULL
, 0, F
->accept
->data
);
139 F
->ssl_errno
= get_last_err();
140 F
->accept
->callback(F
, RB_ERROR_SSL
, NULL
, 0, F
->accept
->data
);
146 rb_settimeout(F
, 0, NULL
, NULL
);
147 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
, NULL
, NULL
);
151 ad
->callback(F
, RB_OK
, (struct sockaddr
*)&ad
->S
, ad
->addrlen
, ad
->data
);
158 rb_ssl_accept_common(rb_fde_t
*new_F
)
161 if((ssl_err
= SSL_accept((SSL
*) new_F
->ssl
)) <= 0)
163 switch (ssl_err
= SSL_get_error((SSL
*) new_F
->ssl
, ssl_err
))
165 case SSL_ERROR_SYSCALL
:
166 if(rb_ignore_errno(errno
))
167 case SSL_ERROR_WANT_READ
:
168 case SSL_ERROR_WANT_WRITE
:
170 new_F
->ssl_errno
= get_last_err();
171 rb_setselect(new_F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
172 rb_ssl_tryaccept
, NULL
);
176 new_F
->ssl_errno
= get_last_err();
177 new_F
->accept
->callback(new_F
, RB_ERROR_SSL
, NULL
, 0, new_F
->accept
->data
);
183 rb_ssl_tryaccept(new_F
, NULL
);
188 rb_ssl_start_accepted(rb_fde_t
*new_F
, ACCB
* cb
, void *data
, int timeout
)
190 new_F
->type
|= RB_FD_SSL
;
191 new_F
->ssl
= SSL_new(ssl_server_ctx
);
192 new_F
->accept
= rb_malloc(sizeof(struct acceptdata
));
194 new_F
->accept
->callback
= cb
;
195 new_F
->accept
->data
= data
;
196 rb_settimeout(new_F
, timeout
, rb_ssl_timeout
, NULL
);
198 new_F
->accept
->addrlen
= 0;
199 SSL_set_fd((SSL
*) new_F
->ssl
, rb_get_fd(new_F
));
200 rb_setup_ssl_cb(new_F
);
201 rb_ssl_accept_common(new_F
);
208 rb_ssl_accept_setup(rb_fde_t
*F
, rb_fde_t
*new_F
, struct sockaddr
*st
, int addrlen
)
210 new_F
->type
|= RB_FD_SSL
;
211 new_F
->ssl
= SSL_new(ssl_server_ctx
);
212 new_F
->accept
= rb_malloc(sizeof(struct acceptdata
));
214 new_F
->accept
->callback
= F
->accept
->callback
;
215 new_F
->accept
->data
= F
->accept
->data
;
216 rb_settimeout(new_F
, 10, rb_ssl_timeout
, NULL
);
217 memcpy(&new_F
->accept
->S
, st
, addrlen
);
218 new_F
->accept
->addrlen
= addrlen
;
220 SSL_set_fd((SSL
*) new_F
->ssl
, rb_get_fd(new_F
));
221 rb_setup_ssl_cb(new_F
);
222 rb_ssl_accept_common(new_F
);
226 rb_ssl_read_or_write(int r_or_w
, rb_fde_t
*F
, void *rbuf
, const void *wbuf
, size_t count
)
233 ret
= (ssize_t
) SSL_read(ssl
, rbuf
, (int)count
);
235 ret
= (ssize_t
) SSL_write(ssl
, wbuf
, (int)count
);
239 switch (SSL_get_error(ssl
, ret
))
241 case SSL_ERROR_WANT_READ
:
243 return RB_RW_SSL_NEED_READ
;
244 case SSL_ERROR_WANT_WRITE
:
246 return RB_RW_SSL_NEED_WRITE
;
247 case SSL_ERROR_ZERO_RETURN
:
249 case SSL_ERROR_SYSCALL
:
250 err
= get_last_err();
254 return RB_RW_IO_ERROR
;
258 err
= get_last_err();
264 errno
= EIO
; /* not great but... */
265 return RB_RW_SSL_ERROR
;
267 return RB_RW_IO_ERROR
;
273 rb_ssl_read(rb_fde_t
*F
, void *buf
, size_t count
)
275 return rb_ssl_read_or_write(0, F
, buf
, NULL
, count
);
279 rb_ssl_write(rb_fde_t
*F
, const void *buf
, size_t count
)
281 return rb_ssl_read_or_write(1, F
, NULL
, buf
, count
);
285 verify_accept_all_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
291 get_ssl_error(unsigned long err
)
293 static char buf
[512];
295 ERR_error_string_n(err
, buf
, sizeof buf
);
303 char libratbox_data
[] = "libratbox data";
304 SSL_load_error_strings();
306 libratbox_index
= SSL_get_ex_new_index(0, libratbox_data
, NULL
, NULL
, NULL
);
307 ssl_server_ctx
= SSL_CTX_new(SSLv23_server_method());
308 if(ssl_server_ctx
== NULL
)
310 rb_lib_log("rb_init_openssl: Unable to initialize OpenSSL server context: %s",
311 get_ssl_error(ERR_get_error()));
314 /* Disable SSLv2, make the client use our settings */
315 SSL_CTX_set_options(ssl_server_ctx
, SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
| SSL_OP_CIPHER_SERVER_PREFERENCE
316 #ifdef SSL_OP_SINGLE_DH_USE
317 | SSL_OP_SINGLE_DH_USE
320 SSL_CTX_set_verify(ssl_server_ctx
, SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
, verify_accept_all_cb
);
321 SSL_CTX_set_session_id_context(ssl_server_ctx
,
322 (const unsigned char *)"libratbox", 9);
323 SSL_CTX_set_cipher_list(ssl_server_ctx
, "EECDH+HIGH:EDH+HIGH:HIGH:!aNULL");
325 /* Set ECDHE on OpenSSL 1.00+, but make sure it's actually available because redhat are dicks
326 and bastardise their OpenSSL for stupid reasons... */
327 #if (OPENSSL_VERSION_NUMBER >= 0x10000000) && defined(NID_secp384r1)
328 EC_KEY
*key
= EC_KEY_new_by_curve_name(NID_secp384r1
);
330 SSL_CTX_set_tmp_ecdh(ssl_server_ctx
, key
);
333 #ifdef SSL_OP_SINGLE_ECDH_USE
334 SSL_CTX_set_options(ssl_server_ctx
, SSL_OP_SINGLE_ECDH_USE
);
338 ssl_client_ctx
= SSL_CTX_new(TLSv1_client_method());
340 if(ssl_client_ctx
== NULL
)
342 rb_lib_log("rb_init_openssl: Unable to initialize OpenSSL client context: %s",
343 get_ssl_error(ERR_get_error()));
351 rb_setup_ssl_server(const char *cert
, const char *keyfile
, const char *dhfile
)
357 rb_lib_log("rb_setup_ssl_server: No certificate file");
360 if(!SSL_CTX_use_certificate_chain_file(ssl_server_ctx
, cert
) || !SSL_CTX_use_certificate_chain_file(ssl_client_ctx
, cert
))
362 err
= ERR_get_error();
363 rb_lib_log("rb_setup_ssl_server: Error loading certificate file [%s]: %s", cert
,
370 rb_lib_log("rb_setup_ssl_server: No key file");
375 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
))
377 err
= ERR_get_error();
378 rb_lib_log("rb_setup_ssl_server: Error loading keyfile [%s]: %s", keyfile
,
385 /* DH parameters aren't necessary, but they are nice..if they didn't pass one..that is their problem */
386 BIO
*bio
= BIO_new_file(dhfile
, "r");
389 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
392 err
= ERR_get_error();
394 ("rb_setup_ssl_server: Error loading DH params file [%s]: %s",
395 dhfile
, get_ssl_error(err
));
400 SSL_CTX_set_tmp_dh(ssl_server_ctx
, dh
);
404 err
= ERR_get_error();
405 rb_lib_log("rb_setup_ssl_server: Error loading DH params file [%s]: %s",
406 dhfile
, get_ssl_error(err
));
413 rb_ssl_listen(rb_fde_t
*F
, int backlog
, int defer_accept
)
417 result
= rb_listen(F
, backlog
, defer_accept
);
418 F
->type
= RB_FD_SOCKET
| RB_FD_LISTEN
| RB_FD_SSL
;
431 rb_ssl_connect_realcb(rb_fde_t
*F
, int status
, struct ssl_connect
*sconn
)
433 F
->connect
->callback
= sconn
->callback
;
434 F
->connect
->data
= sconn
->data
;
436 rb_connect_callback(F
, status
);
440 rb_ssl_tryconn_timeout_cb(rb_fde_t
*F
, void *data
)
442 rb_ssl_connect_realcb(F
, RB_ERR_TIMEOUT
, data
);
446 rb_ssl_tryconn_cb(rb_fde_t
*F
, void *data
)
448 struct ssl_connect
*sconn
= data
;
450 if(!SSL_is_init_finished((SSL
*) F
->ssl
))
452 if((ssl_err
= SSL_connect((SSL
*) F
->ssl
)) <= 0)
454 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
456 case SSL_ERROR_SYSCALL
:
457 if(rb_ignore_errno(errno
))
458 case SSL_ERROR_WANT_READ
:
459 case SSL_ERROR_WANT_WRITE
:
461 F
->ssl_errno
= get_last_err();
462 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
463 rb_ssl_tryconn_cb
, sconn
);
467 F
->ssl_errno
= get_last_err();
468 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
474 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
480 rb_ssl_tryconn(rb_fde_t
*F
, int status
, void *data
)
482 struct ssl_connect
*sconn
= data
;
486 rb_ssl_connect_realcb(F
, status
, sconn
);
490 F
->type
|= RB_FD_SSL
;
491 F
->ssl
= SSL_new(ssl_client_ctx
);
492 SSL_set_fd((SSL
*) F
->ssl
, F
->fd
);
494 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
495 if((ssl_err
= SSL_connect((SSL
*) F
->ssl
)) <= 0)
497 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
499 case SSL_ERROR_SYSCALL
:
500 if(rb_ignore_errno(errno
))
501 case SSL_ERROR_WANT_READ
:
502 case SSL_ERROR_WANT_WRITE
:
504 F
->ssl_errno
= get_last_err();
505 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
506 rb_ssl_tryconn_cb
, sconn
);
510 F
->ssl_errno
= get_last_err();
511 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
517 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
522 rb_connect_tcp_ssl(rb_fde_t
*F
, struct sockaddr
*dest
,
523 struct sockaddr
*clocal
, int socklen
, CNCB
* callback
, void *data
, int timeout
)
525 struct ssl_connect
*sconn
;
529 sconn
= rb_malloc(sizeof(struct ssl_connect
));
531 sconn
->callback
= callback
;
532 sconn
->timeout
= timeout
;
533 rb_connect_tcp(F
, dest
, clocal
, socklen
, rb_ssl_tryconn
, sconn
, timeout
);
538 rb_ssl_start_connected(rb_fde_t
*F
, CNCB
* callback
, void *data
, int timeout
)
540 struct ssl_connect
*sconn
;
545 sconn
= rb_malloc(sizeof(struct ssl_connect
));
547 sconn
->callback
= callback
;
548 sconn
->timeout
= timeout
;
549 F
->connect
= rb_malloc(sizeof(struct conndata
));
550 F
->connect
->callback
= callback
;
551 F
->connect
->data
= data
;
552 F
->type
|= RB_FD_SSL
;
553 F
->ssl
= SSL_new(ssl_client_ctx
);
555 SSL_set_fd((SSL
*) F
->ssl
, F
->fd
);
557 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
558 if((ssl_err
= SSL_connect((SSL
*) F
->ssl
)) <= 0)
560 switch (ssl_err
= SSL_get_error((SSL
*) F
->ssl
, ssl_err
))
562 case SSL_ERROR_SYSCALL
:
563 if(rb_ignore_errno(errno
))
564 case SSL_ERROR_WANT_READ
:
565 case SSL_ERROR_WANT_WRITE
:
567 F
->ssl_errno
= get_last_err();
568 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
,
569 rb_ssl_tryconn_cb
, sconn
);
573 F
->ssl_errno
= get_last_err();
574 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
580 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
585 rb_init_prng(const char *path
, prng_seed_t seed_type
)
587 if(seed_type
== RB_PRNG_DEFAULT
)
592 return RAND_status();
595 return RAND_status();
600 if(RAND_egd(path
) == -1)
604 if(RAND_load_file(path
, -1) == -1)
616 return RAND_status();
620 rb_get_random(void *buf
, size_t length
)
624 if((ret
= RAND_bytes(buf
, length
)) == 0)
626 /* remove the error from the queue */
633 rb_get_pseudo_random(void *buf
, size_t length
)
636 ret
= RAND_pseudo_bytes(buf
, length
);
643 rb_get_ssl_strerror(rb_fde_t
*F
)
645 return get_ssl_error(F
->ssl_errno
);
649 rb_get_ssl_certfp(rb_fde_t
*F
, uint8_t certfp
[RB_SSL_CERTFP_LEN
])
657 cert
= SSL_get_peer_certificate((SSL
*) F
->ssl
);
660 res
= SSL_get_verify_result((SSL
*) F
->ssl
);
661 if(res
== X509_V_OK
||
662 res
== X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
||
663 res
== X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
||
664 res
== X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
)
666 memcpy(certfp
, cert
->sha1_hash
, RB_SSL_CERTFP_LEN
);
677 rb_supports_ssl(void)
683 rb_get_ssl_info(char *buf
, size_t len
)
685 rb_snprintf(buf
, len
, "Using SSL: %s compiled: 0x%lx, library 0x%lx",
686 SSLeay_version(SSLEAY_VERSION
),
687 (long)OPENSSL_VERSION_NUMBER
, SSLeay());
691 #endif /* HAVE_OPESSL */