2 * libratbox: a library used by ircd-ratbox and other things
3 * gnutls.c: gnutls 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: gnutls.c 26296 2008-12-13 03:36:00Z androsyn $
26 #include <libratbox_config.h>
27 #include <ratbox_lib.h>
28 #include <commio-int.h>
29 #include <commio-ssl.h>
32 #include <gnutls/gnutls.h>
33 #include <gnutls/x509.h>
36 static gnutls_certificate_credentials x509
;
37 static gnutls_dh_params dh_params
;
41 #define SSL_P(x) *((gnutls_session_t *)F->ssl)
44 rb_ssl_shutdown(rb_fde_t
*F
)
47 if(F
== NULL
|| F
->ssl
== NULL
)
49 for(i
= 0; i
< 4; i
++)
51 if(gnutls_bye(SSL_P(F
), GNUTLS_SHUT_RDWR
) == GNUTLS_E_SUCCESS
)
54 gnutls_deinit(SSL_P(F
));
59 rb_ssl_handshake_count(rb_fde_t
*F
)
61 return F
->handshake_count
;
65 rb_ssl_clear_handshake_count(rb_fde_t
*F
)
67 F
->handshake_count
= 0;
71 rb_ssl_timeout(rb_fde_t
*F
, void *notused
)
73 lrb_assert(F
->accept
!= NULL
);
74 F
->accept
->callback(F
, RB_ERR_TIMEOUT
, NULL
, 0, F
->accept
->data
);
79 do_ssl_handshake(rb_fde_t
*F
, PF
* callback
)
84 ret
= gnutls_handshake(SSL_P(F
));
87 if((ret
== GNUTLS_E_INTERRUPTED
&& rb_ignore_errno(errno
)) || ret
== GNUTLS_E_AGAIN
)
89 if(gnutls_record_get_direction(SSL_P(F
)) == 0)
90 flags
= RB_SELECT_READ
;
92 flags
= RB_SELECT_WRITE
;
93 rb_setselect(F
, flags
, callback
, NULL
);
99 return 1; /* handshake is finished..go about life */
103 rb_ssl_tryaccept(rb_fde_t
*F
, void *data
)
106 struct acceptdata
*ad
;
108 lrb_assert(F
->accept
!= NULL
);
110 ret
= do_ssl_handshake(F
, rb_ssl_tryaccept
);
112 /* do_ssl_handshake does the rb_setselect */
118 rb_settimeout(F
, 0, NULL
, NULL
);
119 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
, NULL
, NULL
);
122 ad
->callback(F
, RB_OK
, (struct sockaddr
*)&ad
->S
, ad
->addrlen
, ad
->data
);
124 ad
->callback(F
, RB_ERROR_SSL
, NULL
, 0, ad
->data
);
130 rb_ssl_start_accepted(rb_fde_t
*new_F
, ACCB
* cb
, void *data
, int timeout
)
132 gnutls_session_t
*ssl
;
133 new_F
->type
|= RB_FD_SSL
;
134 ssl
= new_F
->ssl
= rb_malloc(sizeof(gnutls_session_t
));
135 new_F
->accept
= rb_malloc(sizeof(struct acceptdata
));
137 new_F
->accept
->callback
= cb
;
138 new_F
->accept
->data
= data
;
139 rb_settimeout(new_F
, timeout
, rb_ssl_timeout
, NULL
);
141 new_F
->accept
->addrlen
= 0;
143 gnutls_init(ssl
, GNUTLS_SERVER
);
144 gnutls_set_default_priority(*ssl
);
145 gnutls_credentials_set(*ssl
, GNUTLS_CRD_CERTIFICATE
, x509
);
146 gnutls_dh_set_prime_bits(*ssl
, 1024);
147 gnutls_transport_set_ptr(*ssl
, (gnutls_transport_ptr_t
) (long int)new_F
->fd
);
148 gnutls_certificate_server_set_request(*ssl
, GNUTLS_CERT_REQUEST
);
149 if(do_ssl_handshake(new_F
, rb_ssl_tryaccept
))
151 struct acceptdata
*ad
= new_F
->accept
;
152 new_F
->accept
= NULL
;
153 ad
->callback(new_F
, RB_OK
, (struct sockaddr
*)&ad
->S
, ad
->addrlen
, ad
->data
);
163 rb_ssl_accept_setup(rb_fde_t
*F
, rb_fde_t
*new_F
, struct sockaddr
*st
, int addrlen
)
165 new_F
->type
|= RB_FD_SSL
;
166 new_F
->ssl
= rb_malloc(sizeof(gnutls_session_t
));
167 new_F
->accept
= rb_malloc(sizeof(struct acceptdata
));
169 new_F
->accept
->callback
= F
->accept
->callback
;
170 new_F
->accept
->data
= F
->accept
->data
;
171 rb_settimeout(new_F
, 10, rb_ssl_timeout
, NULL
);
172 memcpy(&new_F
->accept
->S
, st
, addrlen
);
173 new_F
->accept
->addrlen
= addrlen
;
175 gnutls_init((gnutls_session_t
*) new_F
->ssl
, GNUTLS_SERVER
);
176 gnutls_set_default_priority(SSL_P(new_F
));
177 gnutls_credentials_set(SSL_P(new_F
), GNUTLS_CRD_CERTIFICATE
, x509
);
178 gnutls_dh_set_prime_bits(SSL_P(new_F
), 1024);
179 gnutls_transport_set_ptr(SSL_P(new_F
), (gnutls_transport_ptr_t
) (long int)rb_get_fd(new_F
));
180 gnutls_certificate_server_set_request(SSL_P(new_F
), GNUTLS_CERT_REQUEST
);
181 if(do_ssl_handshake(F
, rb_ssl_tryaccept
))
183 struct acceptdata
*ad
= F
->accept
;
185 ad
->callback(F
, RB_OK
, (struct sockaddr
*)&ad
->S
, ad
->addrlen
, ad
->data
);
194 rb_ssl_read_or_write(int r_or_w
, rb_fde_t
*F
, void *rbuf
, const void *wbuf
, size_t count
)
197 gnutls_session_t
*ssl
= F
->ssl
;
200 ret
= gnutls_record_recv(*ssl
, rbuf
, count
);
202 ret
= gnutls_record_send(*ssl
, wbuf
, count
);
209 case GNUTLS_E_INTERRUPTED
:
210 if(rb_ignore_errno(errno
))
212 if(gnutls_record_get_direction(*ssl
) == 0)
213 return RB_RW_SSL_NEED_READ
;
215 return RB_RW_SSL_NEED_WRITE
;
221 return RB_RW_IO_ERROR
;
228 rb_ssl_read(rb_fde_t
*F
, void *buf
, size_t count
)
230 return rb_ssl_read_or_write(0, F
, buf
, NULL
, count
);
234 rb_ssl_write(rb_fde_t
*F
, const void *buf
, size_t count
)
236 return rb_ssl_read_or_write(1, F
, NULL
, buf
, count
);
240 rb_gcry_random_seed(void *unused
)
242 gcry_fast_random_poll();
248 gnutls_global_init();
250 if(gnutls_certificate_allocate_credentials(&x509
) != GNUTLS_E_SUCCESS
)
252 rb_lib_log("rb_init_ssl: Unable to allocate SSL/TLS certificate credentials");
255 rb_event_addish("rb_gcry_random_seed", rb_gcry_random_seed
, NULL
, 300);
260 rb_free_datum_t(gnutls_datum_t
* d
)
266 static gnutls_datum_t
*
267 rb_load_file_into_datum_t(const char *file
)
270 gnutls_datum_t
*datum
;
271 struct stat fileinfo
;
272 if((f
= fopen(file
, "r")) == NULL
)
274 if(fstat(fileno(f
), &fileinfo
))
277 datum
= rb_malloc(sizeof(gnutls_datum_t
));
279 if(fileinfo
.st_size
> 131072) /* deal with retards */
280 datum
->size
= 131072;
282 datum
->size
= fileinfo
.st_size
;
284 datum
->data
= rb_malloc(datum
->size
+ 1);
285 fread(datum
->data
, datum
->size
, 1, f
);
291 rb_setup_ssl_server(const char *cert
, const char *keyfile
, const char *dhfile
)
294 gnutls_datum_t
*d_cert
, *d_key
;
297 rb_lib_log("rb_setup_ssl_server: No certificate file");
301 if((d_cert
= rb_load_file_into_datum_t(cert
)) == NULL
)
303 rb_lib_log("rb_setup_ssl_server: Error loading certificate: %s", strerror(errno
));
307 if((d_key
= rb_load_file_into_datum_t(keyfile
)) == NULL
)
309 rb_lib_log("rb_setup_ssl_server: Error loading key: %s", strerror(errno
));
315 gnutls_certificate_set_x509_key_mem(x509
, d_cert
, d_key
,
316 GNUTLS_X509_FMT_PEM
)) != GNUTLS_E_SUCCESS
)
318 rb_lib_log("rb_setup_ssl_server: Error loading certificate or key file: %s",
319 gnutls_strerror(ret
));
322 rb_free_datum_t(d_cert
);
323 rb_free_datum_t(d_key
);
327 if(gnutls_dh_params_init(&dh_params
) == GNUTLS_E_SUCCESS
)
329 gnutls_datum_t
*data
;
331 data
= rb_load_file_into_datum_t(dhfile
);
334 xret
= gnutls_dh_params_import_pkcs3(dh_params
, data
,
335 GNUTLS_X509_FMT_PEM
);
338 ("rb_setup_ssl_server: Error parsing DH file: %s\n",
339 gnutls_strerror(xret
));
340 rb_free_datum_t(data
);
342 gnutls_certificate_set_dh_params(x509
, dh_params
);
345 rb_lib_log("rb_setup_ssl_server: Unable to setup DH parameters");
351 rb_ssl_listen(rb_fde_t
*F
, int backlog
, int defer_accept
)
355 result
= listen(F
->fd
, backlog
, defer_accept
);
356 F
->type
= RB_FD_SOCKET
| RB_FD_LISTEN
| RB_FD_SSL
;
369 rb_ssl_connect_realcb(rb_fde_t
*F
, int status
, struct ssl_connect
*sconn
)
371 F
->connect
->callback
= sconn
->callback
;
372 F
->connect
->data
= sconn
->data
;
374 rb_connect_callback(F
, status
);
378 rb_ssl_tryconn_timeout_cb(rb_fde_t
*F
, void *data
)
380 rb_ssl_connect_realcb(F
, RB_ERR_TIMEOUT
, data
);
384 rb_ssl_tryconn_cb(rb_fde_t
*F
, void *data
)
386 struct ssl_connect
*sconn
= data
;
389 ret
= do_ssl_handshake(F
, rb_ssl_tryconn_cb
);
394 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
397 /* do_ssl_handshake does the rb_setselect stuff */
404 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
408 rb_ssl_tryconn(rb_fde_t
*F
, int status
, void *data
)
410 struct ssl_connect
*sconn
= data
;
413 rb_ssl_connect_realcb(F
, status
, sconn
);
417 F
->type
|= RB_FD_SSL
;
420 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
421 F
->ssl
= rb_malloc(sizeof(gnutls_session_t
));
422 gnutls_init(F
->ssl
, GNUTLS_CLIENT
);
423 gnutls_set_default_priority(SSL_P(F
));
424 gnutls_credentials_set(SSL_P(F
), GNUTLS_CRD_CERTIFICATE
, x509
);
425 gnutls_dh_set_prime_bits(SSL_P(F
), 1024);
426 gnutls_transport_set_ptr(SSL_P(F
), (gnutls_transport_ptr_t
) (long int)F
->fd
);
428 if(do_ssl_handshake(F
, rb_ssl_tryconn_cb
))
430 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
435 rb_connect_tcp_ssl(rb_fde_t
*F
, struct sockaddr
*dest
,
436 struct sockaddr
*clocal
, int socklen
, CNCB
* callback
, void *data
, int timeout
)
438 struct ssl_connect
*sconn
;
442 sconn
= rb_malloc(sizeof(struct ssl_connect
));
444 sconn
->callback
= callback
;
445 sconn
->timeout
= timeout
;
446 rb_connect_tcp(F
, dest
, clocal
, socklen
, rb_ssl_tryconn
, sconn
, timeout
);
451 rb_ssl_start_connected(rb_fde_t
*F
, CNCB
* callback
, void *data
, int timeout
)
453 struct ssl_connect
*sconn
;
457 sconn
= rb_malloc(sizeof(struct ssl_connect
));
459 sconn
->callback
= callback
;
460 sconn
->timeout
= timeout
;
461 F
->connect
= rb_malloc(sizeof(struct conndata
));
462 F
->connect
->callback
= callback
;
463 F
->connect
->data
= data
;
464 F
->type
|= RB_FD_SSL
;
465 F
->ssl
= rb_malloc(sizeof(gnutls_session_t
));
467 gnutls_init(F
->ssl
, GNUTLS_CLIENT
);
468 gnutls_set_default_priority(SSL_P(F
));
469 gnutls_credentials_set(SSL_P(F
), GNUTLS_CRD_CERTIFICATE
, x509
);
470 gnutls_dh_set_prime_bits(SSL_P(F
), 1024);
471 gnutls_transport_set_ptr(SSL_P(F
), (gnutls_transport_ptr_t
) (long int)F
->fd
);
473 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
475 if(do_ssl_handshake(F
, rb_ssl_tryconn_cb
))
477 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
482 rb_init_prng(const char *path
, prng_seed_t seed_type
)
484 gcry_fast_random_poll();
489 rb_get_random(void *buf
, size_t length
)
491 gcry_randomize(buf
, length
, GCRY_STRONG_RANDOM
);
496 rb_get_pseudo_random(void *buf
, size_t length
)
498 gcry_randomize(buf
, length
, GCRY_WEAK_RANDOM
);
503 rb_get_ssl_strerror(rb_fde_t
*F
)
505 return gnutls_strerror(F
->ssl_errno
);
509 rb_get_ssl_certfp(rb_fde_t
*F
, uint8_t certfp
[RB_SSL_CERTFP_LEN
])
511 gnutls_x509_crt_t cert
;
512 unsigned int cert_list_size
;
513 const gnutls_datum_t
*cert_list
;
514 uint8_t digest
[RB_SSL_CERTFP_LEN
* 2];
517 if (gnutls_certificate_type_get(SSL_P(F
)) != GNUTLS_CRT_X509
)
520 if (gnutls_x509_crt_init(&cert
) < 0)
524 cert_list
= gnutls_certificate_get_peers(SSL_P(F
), &cert_list_size
);
525 if (cert_list
== NULL
)
527 gnutls_x509_crt_deinit(cert
);
531 if (gnutls_x509_crt_import(cert
, &cert_list
[0], GNUTLS_X509_FMT_DER
) < 0)
533 gnutls_x509_crt_deinit(cert
);
537 if (gnutls_x509_crt_get_fingerprint(cert
, GNUTLS_DIG_SHA1
, digest
, &digest_size
) < 0)
539 gnutls_x509_crt_deinit(cert
);
543 memcpy(certfp
, digest
, RB_SSL_CERTFP_LEN
);
545 gnutls_x509_crt_deinit(cert
);
550 rb_supports_ssl(void)
556 rb_get_ssl_info(char *buf
, size_t len
)
558 rb_snprintf(buf
, len
, "GNUTLS: compiled (%s), library(%s)",
559 LIBGNUTLS_VERSION
, gnutls_check_version(NULL
));
563 #endif /* HAVE_GNUTLS */