]> jfr.im git - solanum.git/blob - libratbox/src/gnutls.c
libratbox/gnutls: call gnutls_rnd_refresh() to ensure our PRNG is initialized
[solanum.git] / libratbox / src / gnutls.c
1 /*
2 * libratbox: a library used by ircd-ratbox and other things
3 * gnutls.c: gnutls related code
4 *
5 * Copyright (C) 2007-2008 ircd-ratbox development team
6 * Copyright (C) 2007-2008 Aaron Sethman <androsyn@ratbox.org>
7 *
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.
12 *
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.
17 *
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
21 * USA
22 *
23 * $Id: gnutls.c 26296 2008-12-13 03:36:00Z androsyn $
24 */
25
26 #include <libratbox_config.h>
27 #include <ratbox_lib.h>
28 #include <commio-int.h>
29 #include <commio-ssl.h>
30 #ifdef HAVE_GNUTLS
31
32 #include <gnutls/gnutls.h>
33 #include <gnutls/x509.h>
34 #include <gnutls/crypto.h>
35
36 #if GNUTLS_VERSION_MAJOR < 3
37 # include <gcrypt.h>
38 #endif
39
40 static gnutls_certificate_credentials x509;
41 static gnutls_dh_params dh_params;
42
43 /* These are all used for getting GnuTLS to supply a client cert. */
44 #define MAX_CERTS 6
45 static unsigned int x509_cert_count;
46 static gnutls_x509_crt_t x509_cert[MAX_CERTS];
47 static gnutls_x509_privkey_t x509_key;
48 static int cert_callback(gnutls_session_t session, const gnutls_datum_t *req_ca_rdn, int nreqs,
49 const gnutls_pk_algorithm_t *sign_algos, int sign_algos_len, gnutls_retr_st *st);
50
51
52 #define SSL_P(x) *((gnutls_session_t *)F->ssl)
53
54 void
55 rb_ssl_shutdown(rb_fde_t *F)
56 {
57 int i;
58 if(F == NULL || F->ssl == NULL)
59 return;
60 for(i = 0; i < 4; i++)
61 {
62 if(gnutls_bye(SSL_P(F), GNUTLS_SHUT_RDWR) == GNUTLS_E_SUCCESS)
63 break;
64 }
65 gnutls_deinit(SSL_P(F));
66 rb_free(F->ssl);
67 }
68
69 unsigned int
70 rb_ssl_handshake_count(rb_fde_t *F)
71 {
72 return F->handshake_count;
73 }
74
75 void
76 rb_ssl_clear_handshake_count(rb_fde_t *F)
77 {
78 F->handshake_count = 0;
79 }
80
81 static void
82 rb_ssl_timeout(rb_fde_t *F, void *notused)
83 {
84 lrb_assert(F->accept != NULL);
85 F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data);
86 }
87
88
89 static int
90 do_ssl_handshake(rb_fde_t *F, PF * callback, void *data)
91 {
92 int ret;
93 int flags;
94
95 ret = gnutls_handshake(SSL_P(F));
96 if(ret < 0)
97 {
98 if((ret == GNUTLS_E_INTERRUPTED && rb_ignore_errno(errno)) || ret == GNUTLS_E_AGAIN)
99 {
100 if(gnutls_record_get_direction(SSL_P(F)) == 0)
101 flags = RB_SELECT_READ;
102 else
103 flags = RB_SELECT_WRITE;
104 rb_setselect(F, flags, callback, data);
105 return 0;
106 }
107 F->ssl_errno = ret;
108 return -1;
109 }
110 return 1; /* handshake is finished..go about life */
111 }
112
113 static void
114 rb_ssl_tryaccept(rb_fde_t *F, void *data)
115 {
116 int ret;
117 struct acceptdata *ad;
118
119 lrb_assert(F->accept != NULL);
120
121 ret = do_ssl_handshake(F, rb_ssl_tryaccept, NULL);
122
123 /* do_ssl_handshake does the rb_setselect */
124 if(ret == 0)
125 return;
126
127 ad = F->accept;
128 F->accept = NULL;
129 rb_settimeout(F, 0, NULL, NULL);
130 rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL);
131
132 if(ret > 0)
133 ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
134 else
135 ad->callback(F, RB_ERROR_SSL, NULL, 0, ad->data);
136
137 rb_free(ad);
138 }
139
140 void
141 rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout)
142 {
143 gnutls_session_t *ssl;
144 new_F->type |= RB_FD_SSL;
145 ssl = new_F->ssl = rb_malloc(sizeof(gnutls_session_t));
146 new_F->accept = rb_malloc(sizeof(struct acceptdata));
147
148 new_F->accept->callback = cb;
149 new_F->accept->data = data;
150 rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL);
151
152 new_F->accept->addrlen = 0;
153
154 gnutls_init(ssl, GNUTLS_SERVER);
155 gnutls_set_default_priority(*ssl);
156 gnutls_credentials_set(*ssl, GNUTLS_CRD_CERTIFICATE, x509);
157 gnutls_dh_set_prime_bits(*ssl, 1024);
158 gnutls_transport_set_ptr(*ssl, (gnutls_transport_ptr_t) (long int)new_F->fd);
159 gnutls_certificate_server_set_request(*ssl, GNUTLS_CERT_REQUEST);
160 if(do_ssl_handshake(new_F, rb_ssl_tryaccept, NULL))
161 {
162 struct acceptdata *ad = new_F->accept;
163 new_F->accept = NULL;
164 ad->callback(new_F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
165 rb_free(ad);
166 }
167
168 }
169
170
171
172
173 void
174 rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen)
175 {
176 new_F->type |= RB_FD_SSL;
177 new_F->ssl = rb_malloc(sizeof(gnutls_session_t));
178 new_F->accept = rb_malloc(sizeof(struct acceptdata));
179
180 new_F->accept->callback = F->accept->callback;
181 new_F->accept->data = F->accept->data;
182 rb_settimeout(new_F, 10, rb_ssl_timeout, NULL);
183 memcpy(&new_F->accept->S, st, addrlen);
184 new_F->accept->addrlen = addrlen;
185
186 gnutls_init((gnutls_session_t *) new_F->ssl, GNUTLS_SERVER);
187 gnutls_set_default_priority(SSL_P(new_F));
188 gnutls_credentials_set(SSL_P(new_F), GNUTLS_CRD_CERTIFICATE, x509);
189 gnutls_dh_set_prime_bits(SSL_P(new_F), 1024);
190 gnutls_transport_set_ptr(SSL_P(new_F), (gnutls_transport_ptr_t) (long int)rb_get_fd(new_F));
191 gnutls_certificate_server_set_request(SSL_P(new_F), GNUTLS_CERT_REQUEST);
192 if(do_ssl_handshake(F, rb_ssl_tryaccept, NULL))
193 {
194 struct acceptdata *ad = F->accept;
195 F->accept = NULL;
196 ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
197 rb_free(ad);
198 }
199 }
200
201
202
203
204 static ssize_t
205 rb_ssl_read_or_write(int r_or_w, rb_fde_t *F, void *rbuf, const void *wbuf, size_t count)
206 {
207 ssize_t ret;
208 gnutls_session_t *ssl = F->ssl;
209
210 if(r_or_w == 0)
211 ret = gnutls_record_recv(*ssl, rbuf, count);
212 else
213 ret = gnutls_record_send(*ssl, wbuf, count);
214
215 if(ret < 0)
216 {
217 switch (ret)
218 {
219 case GNUTLS_E_AGAIN:
220 case GNUTLS_E_INTERRUPTED:
221 if(rb_ignore_errno(errno))
222 {
223 if(gnutls_record_get_direction(*ssl) == 0)
224 return RB_RW_SSL_NEED_READ;
225 else
226 return RB_RW_SSL_NEED_WRITE;
227 break;
228 }
229 default:
230 F->ssl_errno = ret;
231 errno = EIO;
232 return RB_RW_IO_ERROR;
233 }
234 }
235 return ret;
236 }
237
238 ssize_t
239 rb_ssl_read(rb_fde_t *F, void *buf, size_t count)
240 {
241 return rb_ssl_read_or_write(0, F, buf, NULL, count);
242 }
243
244 ssize_t
245 rb_ssl_write(rb_fde_t *F, const void *buf, size_t count)
246 {
247 return rb_ssl_read_or_write(1, F, NULL, buf, count);
248 }
249
250 static void
251 rb_gcry_random_seed(void *unused)
252 {
253 #if GNUTLS_VERSION_MAJOR < 3
254 gcry_fast_random_poll();
255 #endif
256 }
257
258 int
259 rb_init_ssl(void)
260 {
261 gnutls_global_init();
262
263 if(gnutls_certificate_allocate_credentials(&x509) != GNUTLS_E_SUCCESS)
264 {
265 rb_lib_log("rb_init_ssl: Unable to allocate SSL/TLS certificate credentials");
266 return 0;
267 }
268
269 /* This should be changed to gnutls_certificate_set_retrieve_function2 once
270 * everyone in the world has upgraded to GnuTLS 3.
271 */
272 gnutls_certificate_client_set_retrieve_function(x509, cert_callback);
273
274 rb_event_addish("rb_gcry_random_seed", rb_gcry_random_seed, NULL, 300);
275 return 1;
276 }
277
278 /* We only have one certificate to authenticate with, as both client and server. Unfortunately,
279 * GnuTLS tries to be clever, and as client, will attempt to use a certificate that the server
280 * will trust. We usually use self-signed certs, though, so the result of this search is always
281 * nothing. Therefore, it uses no certificate to authenticate as a client. This is undesirable
282 * as it breaks fingerprint auth. Thus, we use this callback to force GnuTLS to always
283 * authenticate with our certificate at all times.
284 */
285 static int
286 cert_callback(gnutls_session_t session, const gnutls_datum_t *req_ca_rdn, int nreqs,
287 const gnutls_pk_algorithm_t *sign_algos, int sign_algos_len, gnutls_retr_st *st)
288 {
289 /* XXX - ugly hack. Tell GnuTLS to use the first (only) certificate we have for auth. */
290 st->type = GNUTLS_CRT_X509;
291 st->ncerts = x509_cert_count;
292 st->cert.x509 = x509_cert;
293 st->key.x509 = x509_key;
294
295 return 0;
296 }
297
298 static void
299 rb_free_datum_t(gnutls_datum_t * d)
300 {
301 rb_free(d->data);
302 rb_free(d);
303 }
304
305 static gnutls_datum_t *
306 rb_load_file_into_datum_t(const char *file)
307 {
308 FILE *f;
309 gnutls_datum_t *datum;
310 struct stat fileinfo;
311 if((f = fopen(file, "r")) == NULL)
312 return NULL;
313 if(fstat(fileno(f), &fileinfo))
314 return NULL;
315
316 datum = rb_malloc(sizeof(gnutls_datum_t));
317
318 if(fileinfo.st_size > 131072) /* deal with retards */
319 datum->size = 131072;
320 else
321 datum->size = fileinfo.st_size;
322
323 datum->data = rb_malloc(datum->size + 1);
324 fread(datum->data, datum->size, 1, f);
325 fclose(f);
326 return datum;
327 }
328
329 int
330 rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile)
331 {
332 int ret;
333 gnutls_datum_t *d_cert, *d_key;
334 if(cert == NULL)
335 {
336 rb_lib_log("rb_setup_ssl_server: No certificate file");
337 return 0;
338 }
339
340 if((d_cert = rb_load_file_into_datum_t(cert)) == NULL)
341 {
342 rb_lib_log("rb_setup_ssl_server: Error loading certificate: %s", strerror(errno));
343 return 0;
344 }
345
346 if((d_key = rb_load_file_into_datum_t(keyfile)) == NULL)
347 {
348 rb_lib_log("rb_setup_ssl_server: Error loading key: %s", strerror(errno));
349 return 0;
350 }
351
352 /* In addition to creating the certificate set, we also need to store our cert elsewhere
353 * so we can force GnuTLS to identify with it when acting as a client.
354 */
355 gnutls_x509_privkey_init(&x509_key);
356 if ((ret = gnutls_x509_privkey_import(x509_key, d_key, GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS)
357 {
358 rb_lib_log("rb_setup_ssl_server: Error loading key file: %s", gnutls_strerror(ret));
359 return 0;
360 }
361
362 x509_cert_count = MAX_CERTS;
363 if ((ret = gnutls_x509_crt_list_import(x509_cert, &x509_cert_count, d_cert, GNUTLS_X509_FMT_PEM,
364 GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED)) < 0)
365 {
366 rb_lib_log("rb_setup_ssl_server: Error loading certificate: %s", gnutls_strerror(ret));
367 return 0;
368 }
369 x509_cert_count = ret;
370
371 if((ret =
372 gnutls_certificate_set_x509_key_mem(x509, d_cert, d_key,
373 GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS)
374 {
375 rb_lib_log("rb_setup_ssl_server: Error loading certificate or key file: %s",
376 gnutls_strerror(ret));
377 return 0;
378 }
379
380 rb_free_datum_t(d_cert);
381 rb_free_datum_t(d_key);
382
383 if(dhfile != NULL)
384 {
385 if(gnutls_dh_params_init(&dh_params) == GNUTLS_E_SUCCESS)
386 {
387 gnutls_datum_t *data;
388 int xret;
389 data = rb_load_file_into_datum_t(dhfile);
390 if(data != NULL)
391 {
392 xret = gnutls_dh_params_import_pkcs3(dh_params, data,
393 GNUTLS_X509_FMT_PEM);
394 if(xret < 0)
395 rb_lib_log
396 ("rb_setup_ssl_server: Error parsing DH file: %s\n",
397 gnutls_strerror(xret));
398 rb_free_datum_t(data);
399 }
400 gnutls_certificate_set_dh_params(x509, dh_params);
401 }
402 else
403 rb_lib_log("rb_setup_ssl_server: Unable to setup DH parameters");
404 }
405 return 1;
406 }
407
408 int
409 rb_ssl_listen(rb_fde_t *F, int backlog, int defer_accept)
410 {
411 int result;
412
413 result = rb_listen(F->fd, backlog, defer_accept);
414 F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL;
415
416 return result;
417 }
418
419 struct ssl_connect
420 {
421 CNCB *callback;
422 void *data;
423 int timeout;
424 };
425
426 static void
427 rb_ssl_connect_realcb(rb_fde_t *F, int status, struct ssl_connect *sconn)
428 {
429 F->connect->callback = sconn->callback;
430 F->connect->data = sconn->data;
431 rb_free(sconn);
432 rb_connect_callback(F, status);
433 }
434
435 static void
436 rb_ssl_tryconn_timeout_cb(rb_fde_t *F, void *data)
437 {
438 rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data);
439 }
440
441 static void
442 rb_ssl_tryconn_cb(rb_fde_t *F, void *data)
443 {
444 struct ssl_connect *sconn = data;
445 int ret;
446
447 ret = do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn);
448
449 switch (ret)
450 {
451 case -1:
452 rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn);
453 break;
454 case 0:
455 /* do_ssl_handshake does the rb_setselect stuff */
456 return;
457 default:
458 break;
459
460
461 }
462 rb_ssl_connect_realcb(F, RB_OK, sconn);
463 }
464
465 static void
466 rb_ssl_tryconn(rb_fde_t *F, int status, void *data)
467 {
468 struct ssl_connect *sconn = data;
469 if(status != RB_OK)
470 {
471 rb_ssl_connect_realcb(F, status, sconn);
472 return;
473 }
474
475 F->type |= RB_FD_SSL;
476
477
478 rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
479 F->ssl = rb_malloc(sizeof(gnutls_session_t));
480 gnutls_init(F->ssl, GNUTLS_CLIENT);
481 gnutls_set_default_priority(SSL_P(F));
482 gnutls_credentials_set(SSL_P(F), GNUTLS_CRD_CERTIFICATE, x509);
483 gnutls_dh_set_prime_bits(SSL_P(F), 1024);
484 gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd);
485
486 do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn);
487 }
488
489 void
490 rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest,
491 struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout)
492 {
493 struct ssl_connect *sconn;
494 if(F == NULL)
495 return;
496
497 sconn = rb_malloc(sizeof(struct ssl_connect));
498 sconn->data = data;
499 sconn->callback = callback;
500 sconn->timeout = timeout;
501 rb_connect_tcp(F, dest, clocal, socklen, rb_ssl_tryconn, sconn, timeout);
502
503 }
504
505 void
506 rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout)
507 {
508 struct ssl_connect *sconn;
509 if(F == NULL)
510 return;
511
512 sconn = rb_malloc(sizeof(struct ssl_connect));
513 sconn->data = data;
514 sconn->callback = callback;
515 sconn->timeout = timeout;
516 F->connect = rb_malloc(sizeof(struct conndata));
517 F->connect->callback = callback;
518 F->connect->data = data;
519 F->type |= RB_FD_SSL;
520 F->ssl = rb_malloc(sizeof(gnutls_session_t));
521
522 gnutls_init(F->ssl, GNUTLS_CLIENT);
523 gnutls_set_default_priority(SSL_P(F));
524 gnutls_credentials_set(SSL_P(F), GNUTLS_CRD_CERTIFICATE, x509);
525 gnutls_dh_set_prime_bits(SSL_P(F), 1024);
526 gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd);
527
528 rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
529
530 do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn);
531 }
532
533 int
534 rb_init_prng(const char *path, prng_seed_t seed_type)
535 {
536 #if GNUTLS_VERSION_MAJOR < 3
537 gcry_fast_random_poll();
538 #else
539 gnutls_rnd_refresh();
540 #endif
541 return 1;
542 }
543
544 int
545 rb_get_random(void *buf, size_t length)
546 {
547 #if GNUTLS_VERSION_MAJOR < 3
548 gcry_randomize(buf, length, GCRY_STRONG_RANDOM);
549 #else
550 gnutls_rnd(GNUTLS_RND_KEY, buf, length);
551 #endif
552 return 1;
553 }
554
555 int
556 rb_get_pseudo_random(void *buf, size_t length)
557 {
558 #if GNUTLS_VERSION_MAJOR < 3
559 gcry_randomize(buf, length, GCRY_WEAK_RANDOM);
560 #else
561 gnutls_rnd(GNUTLS_RND_RANDOM, buf, length);
562 #endif
563 return 1;
564 }
565
566 const char *
567 rb_get_ssl_strerror(rb_fde_t *F)
568 {
569 return gnutls_strerror(F->ssl_errno);
570 }
571
572 int
573 rb_get_ssl_certfp(rb_fde_t *F, uint8_t certfp[RB_SSL_CERTFP_LEN])
574 {
575 gnutls_x509_crt_t cert;
576 unsigned int cert_list_size;
577 const gnutls_datum_t *cert_list;
578 uint8_t digest[RB_SSL_CERTFP_LEN * 2];
579 size_t digest_size;
580
581 if (gnutls_certificate_type_get(SSL_P(F)) != GNUTLS_CRT_X509)
582 return 0;
583
584 if (gnutls_x509_crt_init(&cert) < 0)
585 return 0;
586
587 cert_list_size = 0;
588 cert_list = gnutls_certificate_get_peers(SSL_P(F), &cert_list_size);
589 if (cert_list == NULL)
590 {
591 gnutls_x509_crt_deinit(cert);
592 return 0;
593 }
594
595 if (gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
596 {
597 gnutls_x509_crt_deinit(cert);
598 return 0;
599 }
600
601 if (gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_SHA1, digest, &digest_size) < 0)
602 {
603 gnutls_x509_crt_deinit(cert);
604 return 0;
605 }
606
607 memcpy(certfp, digest, RB_SSL_CERTFP_LEN);
608
609 gnutls_x509_crt_deinit(cert);
610 return 1;
611 }
612
613 int
614 rb_supports_ssl(void)
615 {
616 return 1;
617 }
618
619 void
620 rb_get_ssl_info(char *buf, size_t len)
621 {
622 rb_snprintf(buf, len, "GNUTLS: compiled (%s), library(%s)",
623 LIBGNUTLS_VERSION, gnutls_check_version(NULL));
624 }
625
626
627 #endif /* HAVE_GNUTLS */