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>
35 static gnutls_certificate_credentials x509
;
36 static gnutls_dh_params dh_params
;
40 #define SSL_P(x) *((gnutls_session_t *)F->ssl)
43 rb_ssl_shutdown(rb_fde_t
*F
)
46 if(F
== NULL
|| F
->ssl
== NULL
)
48 for(i
= 0; i
< 4; i
++)
50 if(gnutls_bye(SSL_P(F
), GNUTLS_SHUT_RDWR
) == GNUTLS_E_SUCCESS
)
53 gnutls_deinit(SSL_P(F
));
58 rb_ssl_handshake_count(rb_fde_t
*F
)
60 return F
->handshake_count
;
64 rb_ssl_clear_handshake_count(rb_fde_t
*F
)
66 F
->handshake_count
= 0;
70 rb_ssl_timeout(rb_fde_t
*F
, void *notused
)
72 lrb_assert(F
->accept
!= NULL
);
73 F
->accept
->callback(F
, RB_ERR_TIMEOUT
, NULL
, 0, F
->accept
->data
);
78 do_ssl_handshake(rb_fde_t
*F
, PF
* callback
)
83 ret
= gnutls_handshake(SSL_P(F
));
86 if((ret
== GNUTLS_E_INTERRUPTED
&& rb_ignore_errno(errno
)) || ret
== GNUTLS_E_AGAIN
)
88 if(gnutls_record_get_direction(SSL_P(F
)) == 0)
89 flags
= RB_SELECT_READ
;
91 flags
= RB_SELECT_WRITE
;
92 rb_setselect(F
, flags
, callback
, NULL
);
98 return 1; /* handshake is finished..go about life */
102 rb_ssl_tryaccept(rb_fde_t
*F
, void *data
)
105 struct acceptdata
*ad
;
107 lrb_assert(F
->accept
!= NULL
);
109 ret
= do_ssl_handshake(F
, rb_ssl_tryaccept
);
111 /* do_ssl_handshake does the rb_setselect */
117 rb_settimeout(F
, 0, NULL
, NULL
);
118 rb_setselect(F
, RB_SELECT_READ
| RB_SELECT_WRITE
, NULL
, NULL
);
121 ad
->callback(F
, RB_OK
, (struct sockaddr
*)&ad
->S
, ad
->addrlen
, ad
->data
);
123 ad
->callback(F
, RB_ERROR_SSL
, NULL
, 0, ad
->data
);
129 rb_ssl_start_accepted(rb_fde_t
*new_F
, ACCB
* cb
, void *data
, int timeout
)
131 gnutls_session_t
*ssl
;
132 new_F
->type
|= RB_FD_SSL
;
133 ssl
= new_F
->ssl
= rb_malloc(sizeof(gnutls_session_t
));
134 new_F
->accept
= rb_malloc(sizeof(struct acceptdata
));
136 new_F
->accept
->callback
= cb
;
137 new_F
->accept
->data
= data
;
138 rb_settimeout(new_F
, timeout
, rb_ssl_timeout
, NULL
);
140 new_F
->accept
->addrlen
= 0;
142 gnutls_init(ssl
, GNUTLS_SERVER
);
143 gnutls_set_default_priority(*ssl
);
144 gnutls_credentials_set(*ssl
, GNUTLS_CRD_CERTIFICATE
, x509
);
145 gnutls_dh_set_prime_bits(*ssl
, 1024);
146 gnutls_transport_set_ptr(*ssl
, (gnutls_transport_ptr_t
) (long int)new_F
->fd
);
147 if(do_ssl_handshake(new_F
, rb_ssl_tryaccept
))
149 struct acceptdata
*ad
= new_F
->accept
;
150 new_F
->accept
= NULL
;
151 ad
->callback(new_F
, RB_OK
, (struct sockaddr
*)&ad
->S
, ad
->addrlen
, ad
->data
);
161 rb_ssl_accept_setup(rb_fde_t
*F
, rb_fde_t
*new_F
, struct sockaddr
*st
, int addrlen
)
163 new_F
->type
|= RB_FD_SSL
;
164 new_F
->ssl
= rb_malloc(sizeof(gnutls_session_t
));
165 new_F
->accept
= rb_malloc(sizeof(struct acceptdata
));
167 new_F
->accept
->callback
= F
->accept
->callback
;
168 new_F
->accept
->data
= F
->accept
->data
;
169 rb_settimeout(new_F
, 10, rb_ssl_timeout
, NULL
);
170 memcpy(&new_F
->accept
->S
, st
, addrlen
);
171 new_F
->accept
->addrlen
= addrlen
;
173 gnutls_init((gnutls_session_t
*) new_F
->ssl
, GNUTLS_SERVER
);
174 gnutls_set_default_priority(SSL_P(new_F
));
175 gnutls_credentials_set(SSL_P(new_F
), GNUTLS_CRD_CERTIFICATE
, x509
);
176 gnutls_dh_set_prime_bits(SSL_P(new_F
), 1024);
177 gnutls_transport_set_ptr(SSL_P(new_F
), (gnutls_transport_ptr_t
) (long int)rb_get_fd(new_F
));
178 if(do_ssl_handshake(F
, rb_ssl_tryaccept
))
180 struct acceptdata
*ad
= F
->accept
;
182 ad
->callback(F
, RB_OK
, (struct sockaddr
*)&ad
->S
, ad
->addrlen
, ad
->data
);
191 rb_ssl_read_or_write(int r_or_w
, rb_fde_t
*F
, void *rbuf
, const void *wbuf
, size_t count
)
194 gnutls_session_t
*ssl
= F
->ssl
;
197 ret
= gnutls_record_recv(*ssl
, rbuf
, count
);
199 ret
= gnutls_record_send(*ssl
, wbuf
, count
);
206 case GNUTLS_E_INTERRUPTED
:
207 if(rb_ignore_errno(errno
))
209 if(gnutls_record_get_direction(*ssl
) == 0)
210 return RB_RW_SSL_NEED_READ
;
212 return RB_RW_SSL_NEED_WRITE
;
218 return RB_RW_IO_ERROR
;
225 rb_ssl_read(rb_fde_t
*F
, void *buf
, size_t count
)
227 return rb_ssl_read_or_write(0, F
, buf
, NULL
, count
);
231 rb_ssl_write(rb_fde_t
*F
, const void *buf
, size_t count
)
233 return rb_ssl_read_or_write(1, F
, NULL
, buf
, count
);
237 rb_gcry_random_seed(void *unused
)
239 gcry_fast_random_poll();
245 gnutls_global_init();
247 if(gnutls_certificate_allocate_credentials(&x509
) != GNUTLS_E_SUCCESS
)
249 rb_lib_log("rb_init_ssl: Unable to allocate SSL/TLS certificate credentials");
252 rb_event_addish("rb_gcry_random_seed", rb_gcry_random_seed
, NULL
, 300);
257 rb_free_datum_t(gnutls_datum_t
* d
)
263 static gnutls_datum_t
*
264 rb_load_file_into_datum_t(const char *file
)
267 gnutls_datum_t
*datum
;
268 struct stat fileinfo
;
269 if((f
= fopen(file
, "r")) == NULL
)
271 if(fstat(fileno(f
), &fileinfo
))
274 datum
= rb_malloc(sizeof(gnutls_datum_t
));
276 if(fileinfo
.st_size
> 131072) /* deal with retards */
277 datum
->size
= 131072;
279 datum
->size
= fileinfo
.st_size
;
281 datum
->data
= rb_malloc(datum
->size
+ 1);
282 fread(datum
->data
, datum
->size
, 1, f
);
288 rb_setup_ssl_server(const char *cert
, const char *keyfile
, const char *dhfile
)
291 gnutls_datum_t
*d_cert
, *d_key
;
294 rb_lib_log("rb_setup_ssl_server: No certificate file");
298 if((d_cert
= rb_load_file_into_datum_t(cert
)) == NULL
)
300 rb_lib_log("rb_setup_ssl_server: Error loading certificate: %s", strerror(errno
));
304 if((d_key
= rb_load_file_into_datum_t(keyfile
)) == NULL
)
306 rb_lib_log("rb_setup_ssl_server: Error loading key: %s", strerror(errno
));
312 gnutls_certificate_set_x509_key_mem(x509
, d_cert
, d_key
,
313 GNUTLS_X509_FMT_PEM
)) != GNUTLS_E_SUCCESS
)
315 rb_lib_log("rb_setup_ssl_server: Error loading certificate or key file: %s",
316 gnutls_strerror(ret
));
319 rb_free_datum_t(d_cert
);
320 rb_free_datum_t(d_key
);
324 if(gnutls_dh_params_init(&dh_params
) == GNUTLS_E_SUCCESS
)
326 gnutls_datum_t
*data
;
328 data
= rb_load_file_into_datum_t(dhfile
);
331 xret
= gnutls_dh_params_import_pkcs3(dh_params
, data
,
332 GNUTLS_X509_FMT_PEM
);
335 ("rb_setup_ssl_server: Error parsing DH file: %s\n",
336 gnutls_strerror(xret
));
337 rb_free_datum_t(data
);
339 gnutls_certificate_set_dh_params(x509
, dh_params
);
342 rb_lib_log("rb_setup_ssl_server: Unable to setup DH parameters");
348 rb_ssl_listen(rb_fde_t
*F
, int backlog
)
350 F
->type
= RB_FD_SOCKET
| RB_FD_LISTEN
| RB_FD_SSL
;
351 return listen(F
->fd
, backlog
);
362 rb_ssl_connect_realcb(rb_fde_t
*F
, int status
, struct ssl_connect
*sconn
)
364 F
->connect
->callback
= sconn
->callback
;
365 F
->connect
->data
= sconn
->data
;
367 rb_connect_callback(F
, status
);
371 rb_ssl_tryconn_timeout_cb(rb_fde_t
*F
, void *data
)
373 rb_ssl_connect_realcb(F
, RB_ERR_TIMEOUT
, data
);
377 rb_ssl_tryconn_cb(rb_fde_t
*F
, void *data
)
379 struct ssl_connect
*sconn
= data
;
382 ret
= do_ssl_handshake(F
, rb_ssl_tryconn_cb
);
387 rb_ssl_connect_realcb(F
, RB_ERROR_SSL
, sconn
);
390 /* do_ssl_handshake does the rb_setselect stuff */
397 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
401 rb_ssl_tryconn(rb_fde_t
*F
, int status
, void *data
)
403 struct ssl_connect
*sconn
= data
;
406 rb_ssl_connect_realcb(F
, status
, sconn
);
410 F
->type
|= RB_FD_SSL
;
413 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
414 F
->ssl
= rb_malloc(sizeof(gnutls_session_t
));
415 gnutls_init(F
->ssl
, GNUTLS_CLIENT
);
416 gnutls_set_default_priority(SSL_P(F
));
417 gnutls_dh_set_prime_bits(SSL_P(F
), 1024);
418 gnutls_transport_set_ptr(SSL_P(F
), (gnutls_transport_ptr_t
) (long int)F
->fd
);
420 if(do_ssl_handshake(F
, rb_ssl_tryconn_cb
))
422 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
427 rb_connect_tcp_ssl(rb_fde_t
*F
, struct sockaddr
*dest
,
428 struct sockaddr
*clocal
, int socklen
, CNCB
* callback
, void *data
, int timeout
)
430 struct ssl_connect
*sconn
;
434 sconn
= rb_malloc(sizeof(struct ssl_connect
));
436 sconn
->callback
= callback
;
437 sconn
->timeout
= timeout
;
438 rb_connect_tcp(F
, dest
, clocal
, socklen
, rb_ssl_tryconn
, sconn
, timeout
);
443 rb_ssl_start_connected(rb_fde_t
*F
, CNCB
* callback
, void *data
, int timeout
)
445 struct ssl_connect
*sconn
;
449 sconn
= rb_malloc(sizeof(struct ssl_connect
));
451 sconn
->callback
= callback
;
452 sconn
->timeout
= timeout
;
453 F
->connect
= rb_malloc(sizeof(struct conndata
));
454 F
->connect
->callback
= callback
;
455 F
->connect
->data
= data
;
456 F
->type
|= RB_FD_SSL
;
457 F
->ssl
= rb_malloc(sizeof(gnutls_session_t
));
459 gnutls_init(F
->ssl
, GNUTLS_CLIENT
);
460 gnutls_set_default_priority(SSL_P(F
));
461 gnutls_dh_set_prime_bits(SSL_P(F
), 1024);
462 gnutls_transport_set_ptr(SSL_P(F
), (gnutls_transport_ptr_t
) (long int)F
->fd
);
464 rb_settimeout(F
, sconn
->timeout
, rb_ssl_tryconn_timeout_cb
, sconn
);
466 if(do_ssl_handshake(F
, rb_ssl_tryconn_cb
))
468 rb_ssl_connect_realcb(F
, RB_OK
, sconn
);
473 rb_init_prng(const char *path
, prng_seed_t seed_type
)
475 gcry_fast_random_poll();
480 rb_get_random(void *buf
, size_t length
)
482 gcry_randomize(buf
, length
, GCRY_STRONG_RANDOM
);
487 rb_get_pseudo_random(void *buf
, size_t length
)
489 gcry_randomize(buf
, length
, GCRY_WEAK_RANDOM
);
494 rb_get_ssl_strerror(rb_fde_t
*F
)
496 return gnutls_strerror(F
->ssl_errno
);
500 rb_supports_ssl(void)
506 rb_get_ssl_info(char *buf
, size_t len
)
508 rb_snprintf(buf
, len
, "GNUTLS: compiled (%s), library(%s)",
509 LIBGNUTLS_VERSION
, gnutls_check_version(NULL
));
513 #endif /* HAVE_GNUTLS */