]> jfr.im git - irc/rqf/shadowircd.git/blame - libratbox/src/gnutls.c
Copied libratbox and related stuff from shadowircd upstream.
[irc/rqf/shadowircd.git] / libratbox / src / gnutls.c
CommitLineData
af6f5d47
WP
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>
af6f5d47
WP
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 *
94b4fbf9 23 * $Id: gnutls.c 26092 2008-09-19 15:13:52Z androsyn $
af6f5d47
WP
24 */
25
26#include <libratbox_config.h>
27#include <ratbox_lib.h>
af6f5d47
WP
28#include <commio-int.h>
29#include <commio-ssl.h>
033be687
VY
30#ifdef HAVE_GNUTLS
31
af6f5d47 32#include <gnutls/gnutls.h>
033be687
VY
33#include <gcrypt.h>
34
35static gnutls_certificate_credentials x509;
36static gnutls_dh_params dh_params;
af6f5d47 37
033be687
VY
38
39
40#define SSL_P(x) *((gnutls_session_t *)F->ssl)
af6f5d47
WP
41
42void
94b4fbf9 43rb_ssl_shutdown(rb_fde_t *F)
af6f5d47 44{
033be687 45 int i;
af6f5d47
WP
46 if(F == NULL || F->ssl == NULL)
47 return;
94b4fbf9 48 for(i = 0; i < 4; i++)
033be687
VY
49 {
50 if(gnutls_bye(SSL_P(F), GNUTLS_SHUT_RDWR) == GNUTLS_E_SUCCESS)
51 break;
52 }
53 gnutls_deinit(SSL_P(F));
54 rb_free(F->ssl);
55}
af6f5d47 56
033be687
VY
57unsigned int
58rb_ssl_handshake_count(rb_fde_t *F)
59{
60 return F->handshake_count;
61}
62
63void
64rb_ssl_clear_handshake_count(rb_fde_t *F)
65{
66 F->handshake_count = 0;
af6f5d47
WP
67}
68
69static void
94b4fbf9 70rb_ssl_timeout(rb_fde_t *F, void *notused)
af6f5d47
WP
71{
72 lrb_assert(F->accept != NULL);
73 F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data);
74}
75
033be687 76
94b4fbf9
VY
77static int
78do_ssl_handshake(rb_fde_t *F, PF * callback)
033be687
VY
79{
80 int ret;
94b4fbf9 81 int flags;
033be687
VY
82
83 ret = gnutls_handshake(SSL_P(F));
84 if(ret < 0)
85 {
86 if((ret == GNUTLS_E_INTERRUPTED && rb_ignore_errno(errno)) || ret == GNUTLS_E_AGAIN)
87 {
88 if(gnutls_record_get_direction(SSL_P(F)) == 0)
89 flags = RB_SELECT_READ;
90 else
91 flags = RB_SELECT_WRITE;
94b4fbf9 92 rb_setselect(F, flags, callback, NULL);
033be687
VY
93 return 0;
94 }
95 F->ssl_errno = ret;
96 return -1;
94b4fbf9
VY
97 }
98 return 1; /* handshake is finished..go about life */
033be687
VY
99}
100
af6f5d47 101static void
94b4fbf9 102rb_ssl_tryaccept(rb_fde_t *F, void *data)
af6f5d47 103{
033be687 104 int ret;
af6f5d47
WP
105 struct acceptdata *ad;
106
033be687 107 lrb_assert(F->accept != NULL);
af6f5d47 108
033be687 109 ret = do_ssl_handshake(F, rb_ssl_tryaccept);
94b4fbf9
VY
110
111 switch (ret)
033be687 112 {
94b4fbf9
VY
113 case -1:
114 F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data);
115 break;
116 case 0:
117 /* do_ssl_handshake does the rb_setselect stuff */
118 return;
119 default:
120 break;
121
122
af6f5d47
WP
123 }
124 rb_settimeout(F, 0, NULL, NULL);
125 rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL);
94b4fbf9 126
af6f5d47
WP
127 ad = F->accept;
128 F->accept = NULL;
94b4fbf9 129 ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
af6f5d47 130 rb_free(ad);
033be687 131
af6f5d47
WP
132}
133
134void
94b4fbf9 135rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout)
af6f5d47 136{
033be687 137 gnutls_session_t *ssl;
af6f5d47 138 new_F->type |= RB_FD_SSL;
033be687 139 ssl = new_F->ssl = rb_malloc(sizeof(gnutls_session_t));
af6f5d47 140 new_F->accept = rb_malloc(sizeof(struct acceptdata));
94b4fbf9 141
af6f5d47
WP
142 new_F->accept->callback = cb;
143 new_F->accept->data = data;
144 rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL);
145
146 new_F->accept->addrlen = 0;
147
94b4fbf9 148 gnutls_init(ssl, GNUTLS_SERVER);
033be687
VY
149 gnutls_set_default_priority(*ssl);
150 gnutls_credentials_set(*ssl, GNUTLS_CRD_CERTIFICATE, x509);
151 gnutls_dh_set_prime_bits(*ssl, 1024);
152 gnutls_transport_set_ptr(*ssl, (gnutls_transport_ptr_t) (long int)new_F->fd);
153 if(do_ssl_handshake(new_F, rb_ssl_tryaccept))
af6f5d47 154 {
033be687 155 struct acceptdata *ad = new_F->accept;
af6f5d47 156 new_F->accept = NULL;
94b4fbf9
VY
157 ad->callback(new_F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
158 rb_free(ad);
af6f5d47 159 }
033be687 160
af6f5d47
WP
161}
162
033be687
VY
163
164
165
af6f5d47 166void
94b4fbf9 167rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen)
af6f5d47 168{
af6f5d47 169 new_F->type |= RB_FD_SSL;
033be687 170 new_F->ssl = rb_malloc(sizeof(gnutls_session_t));
af6f5d47
WP
171 new_F->accept = rb_malloc(sizeof(struct acceptdata));
172
173 new_F->accept->callback = F->accept->callback;
174 new_F->accept->data = F->accept->data;
175 rb_settimeout(new_F, 10, rb_ssl_timeout, NULL);
176 memcpy(&new_F->accept->S, st, addrlen);
177 new_F->accept->addrlen = addrlen;
178
94b4fbf9 179 gnutls_init((gnutls_session_t *) new_F->ssl, GNUTLS_SERVER);
033be687
VY
180 gnutls_set_default_priority(SSL_P(new_F));
181 gnutls_credentials_set(SSL_P(new_F), GNUTLS_CRD_CERTIFICATE, x509);
182 gnutls_dh_set_prime_bits(SSL_P(new_F), 1024);
4414eb3c 183 gnutls_transport_set_ptr(SSL_P(new_F), (gnutls_transport_ptr_t) (long int)rb_get_fd(new_F));
033be687 184 if(do_ssl_handshake(F, rb_ssl_tryaccept))
af6f5d47 185 {
033be687
VY
186 struct acceptdata *ad = F->accept;
187 F->accept = NULL;
94b4fbf9
VY
188 ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
189 rb_free(ad);
033be687
VY
190 }
191}
af6f5d47 192
af6f5d47 193
af6f5d47 194
af6f5d47
WP
195
196static ssize_t
94b4fbf9 197rb_ssl_read_or_write(int r_or_w, rb_fde_t *F, void *rbuf, const void *wbuf, size_t count)
af6f5d47
WP
198{
199 ssize_t ret;
033be687 200 gnutls_session_t *ssl = F->ssl;
af6f5d47
WP
201
202 if(r_or_w == 0)
033be687 203 ret = gnutls_record_recv(*ssl, rbuf, count);
af6f5d47 204 else
033be687 205 ret = gnutls_record_send(*ssl, wbuf, count);
af6f5d47
WP
206
207 if(ret < 0)
208 {
94b4fbf9 209 switch (ret)
af6f5d47
WP
210 {
211 case GNUTLS_E_AGAIN:
af6f5d47 212 case GNUTLS_E_INTERRUPTED:
033be687 213 if(rb_ignore_errno(errno))
af6f5d47 214 {
033be687
VY
215 if(gnutls_record_get_direction(*ssl) == 0)
216 return RB_RW_SSL_NEED_READ;
217 else
218 return RB_RW_SSL_NEED_WRITE;
219 break;
af6f5d47 220 }
af6f5d47 221 default:
033be687
VY
222 F->ssl_errno = ret;
223 errno = EIO;
224 return RB_RW_IO_ERROR;
af6f5d47 225 }
af6f5d47
WP
226 }
227 return ret;
228}
229
230ssize_t
94b4fbf9 231rb_ssl_read(rb_fde_t *F, void *buf, size_t count)
af6f5d47
WP
232{
233 return rb_ssl_read_or_write(0, F, buf, NULL, count);
234}
235
236ssize_t
94b4fbf9 237rb_ssl_write(rb_fde_t *F, const void *buf, size_t count)
af6f5d47
WP
238{
239 return rb_ssl_read_or_write(1, F, NULL, buf, count);
240}
241
033be687
VY
242static void
243rb_gcry_random_seed(void *unused)
244{
245 gcry_fast_random_poll();
246}
247
af6f5d47
WP
248int
249rb_init_ssl(void)
250{
af6f5d47 251 gnutls_global_init();
94b4fbf9 252
033be687 253 if(gnutls_certificate_allocate_credentials(&x509) != GNUTLS_E_SUCCESS)
af6f5d47 254 {
033be687
VY
255 rb_lib_log("rb_init_ssl: Unable to allocate SSL/TLS certificate credentials");
256 return 0;
af6f5d47 257 }
033be687
VY
258 rb_event_addish("rb_gcry_random_seed", rb_gcry_random_seed, NULL, 300);
259 return 1;
260}
af6f5d47 261
033be687 262static void
94b4fbf9 263rb_free_datum_t(gnutls_datum_t * d)
033be687
VY
264{
265 rb_free(d->data);
266 rb_free(d);
267}
af6f5d47 268
033be687
VY
269static gnutls_datum_t *
270rb_load_file_into_datum_t(const char *file)
271{
272 FILE *f;
273 gnutls_datum_t *datum;
274 struct stat fileinfo;
275 if((f = fopen(file, "r")) == NULL)
276 return NULL;
277 if(fstat(fileno(f), &fileinfo))
278 return NULL;
279
280 datum = rb_malloc(sizeof(gnutls_datum_t));
281
94b4fbf9 282 if(fileinfo.st_size > 131072) /* deal with retards */
033be687
VY
283 datum->size = 131072;
284 else
94b4fbf9
VY
285 datum->size = fileinfo.st_size;
286
033be687
VY
287 datum->data = rb_malloc(datum->size + 1);
288 fread(datum->data, datum->size, 1, f);
289 fclose(f);
94b4fbf9 290 return datum;
af6f5d47
WP
291}
292
293int
294rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile)
295{
033be687
VY
296 int ret;
297 gnutls_datum_t *d_cert, *d_key;
298 if(cert == NULL)
299 {
300 rb_lib_log("rb_setup_ssl_server: No certificate file");
301 return 0;
302 }
af6f5d47 303
033be687 304 if((d_cert = rb_load_file_into_datum_t(cert)) == NULL)
af6f5d47 305 {
033be687 306 rb_lib_log("rb_setup_ssl_server: Error loading certificate: %s", strerror(errno));
af6f5d47
WP
307 return 0;
308 }
309
033be687
VY
310 if((d_key = rb_load_file_into_datum_t(keyfile)) == NULL)
311 {
312 rb_lib_log("rb_setup_ssl_server: Error loading key: %s", strerror(errno));
313 return 0;
314 }
94b4fbf9
VY
315
316
317 if((ret =
318 gnutls_certificate_set_x509_key_mem(x509, d_cert, d_key,
319 GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS)
033be687 320 {
94b4fbf9
VY
321 rb_lib_log("rb_setup_ssl_server: Error loading certificate or key file: %s",
322 gnutls_strerror(ret));
033be687
VY
323 return 0;
324 }
325 rb_free_datum_t(d_cert);
326 rb_free_datum_t(d_key);
94b4fbf9 327
033be687
VY
328 if(dhfile != NULL)
329 {
330 if(gnutls_dh_params_init(&dh_params) == GNUTLS_E_SUCCESS)
331 {
332 gnutls_datum_t *data;
333 int xret;
334 data = rb_load_file_into_datum_t(dhfile);
335 if(data != NULL)
336 {
94b4fbf9
VY
337 xret = gnutls_dh_params_import_pkcs3(dh_params, data,
338 GNUTLS_X509_FMT_PEM);
033be687 339 if(xret < 0)
94b4fbf9
VY
340 rb_lib_log
341 ("rb_setup_ssl_server: Error parsing DH file: %s\n",
342 gnutls_strerror(xret));
033be687
VY
343 rb_free_datum_t(data);
344 }
345 gnutls_certificate_set_dh_params(x509, dh_params);
94b4fbf9
VY
346 }
347 else
033be687
VY
348 rb_lib_log("rb_setup_ssl_server: Unable to setup DH parameters");
349 }
af6f5d47
WP
350 return 1;
351}
352
353int
94b4fbf9 354rb_ssl_listen(rb_fde_t *F, int backlog)
af6f5d47
WP
355{
356 F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL;
357 return listen(F->fd, backlog);
358}
359
360struct ssl_connect
361{
362 CNCB *callback;
363 void *data;
364 int timeout;
365};
366
367static void
94b4fbf9 368rb_ssl_connect_realcb(rb_fde_t *F, int status, struct ssl_connect *sconn)
af6f5d47
WP
369{
370 F->connect->callback = sconn->callback;
371 F->connect->data = sconn->data;
372 rb_free(sconn);
373 rb_connect_callback(F, status);
374}
375
376static void
94b4fbf9 377rb_ssl_tryconn_timeout_cb(rb_fde_t *F, void *data)
af6f5d47
WP
378{
379 rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data);
380}
381
382static void
94b4fbf9 383rb_ssl_tryconn_cb(rb_fde_t *F, void *data)
af6f5d47
WP
384{
385 struct ssl_connect *sconn = data;
033be687
VY
386 int ret;
387
94b4fbf9
VY
388 ret = do_ssl_handshake(F, rb_ssl_tryconn_cb);
389
390 switch (ret)
391 {
392 case -1:
393 rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn);
394 break;
395 case 0:
396 /* do_ssl_handshake does the rb_setselect stuff */
397 return;
398 default:
399 break;
400
401
402 }
033be687 403 rb_ssl_connect_realcb(F, RB_OK, sconn);
af6f5d47
WP
404}
405
406static void
94b4fbf9 407rb_ssl_tryconn(rb_fde_t *F, int status, void *data)
af6f5d47 408{
af6f5d47 409 struct ssl_connect *sconn = data;
af6f5d47
WP
410 if(status != RB_OK)
411 {
412 rb_ssl_connect_realcb(F, status, sconn);
413 return;
414 }
415
416 F->type |= RB_FD_SSL;
417
94b4fbf9 418
af6f5d47 419 rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
033be687 420 F->ssl = rb_malloc(sizeof(gnutls_session_t));
94b4fbf9
VY
421 gnutls_init(F->ssl, GNUTLS_CLIENT);
422 gnutls_set_default_priority(SSL_P(F));
033be687
VY
423 gnutls_dh_set_prime_bits(SSL_P(F), 1024);
424 gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd);
425
426 if(do_ssl_handshake(F, rb_ssl_tryconn_cb))
af6f5d47
WP
427 {
428 rb_ssl_connect_realcb(F, RB_OK, sconn);
429 }
430}
431
432void
94b4fbf9 433rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest,
af6f5d47
WP
434 struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout)
435{
436 struct ssl_connect *sconn;
437 if(F == NULL)
438 return;
439
440 sconn = rb_malloc(sizeof(struct ssl_connect));
441 sconn->data = data;
442 sconn->callback = callback;
443 sconn->timeout = timeout;
444 rb_connect_tcp(F, dest, clocal, socklen, rb_ssl_tryconn, sconn, timeout);
033be687 445
af6f5d47
WP
446}
447
448void
94b4fbf9 449rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout)
af6f5d47 450{
af6f5d47 451 struct ssl_connect *sconn;
af6f5d47
WP
452 if(F == NULL)
453 return;
454
455 sconn = rb_malloc(sizeof(struct ssl_connect));
456 sconn->data = data;
457 sconn->callback = callback;
458 sconn->timeout = timeout;
459 F->connect = rb_malloc(sizeof(struct conndata));
460 F->connect->callback = callback;
461 F->connect->data = data;
462 F->type |= RB_FD_SSL;
033be687 463 F->ssl = rb_malloc(sizeof(gnutls_session_t));
94b4fbf9
VY
464
465 gnutls_init(F->ssl, GNUTLS_CLIENT);
466 gnutls_set_default_priority(SSL_P(F));
033be687
VY
467 gnutls_dh_set_prime_bits(SSL_P(F), 1024);
468 gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd);
94b4fbf9 469
af6f5d47 470 rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
033be687
VY
471
472 if(do_ssl_handshake(F, rb_ssl_tryconn_cb))
af6f5d47
WP
473 {
474 rb_ssl_connect_realcb(F, RB_OK, sconn);
475 }
476}
477
af6f5d47
WP
478int
479rb_init_prng(const char *path, prng_seed_t seed_type)
480{
033be687
VY
481 gcry_fast_random_poll();
482 return 1;
af6f5d47
WP
483}
484
485int
486rb_get_random(void *buf, size_t length)
487{
033be687
VY
488 gcry_randomize(buf, length, GCRY_STRONG_RANDOM);
489 return 1;
af6f5d47
WP
490}
491
4414eb3c
VY
492int
493rb_get_pseudo_random(void *buf, size_t length)
494{
495 gcry_randomize(buf, length, GCRY_WEAK_RANDOM);
496 return 1;
497}
af6f5d47
WP
498
499const char *
94b4fbf9 500rb_get_ssl_strerror(rb_fde_t *F)
af6f5d47
WP
501{
502 return gnutls_strerror(F->ssl_errno);
503}
504
505int
506rb_supports_ssl(void)
507{
508 return 1;
509}
510
511#endif /* HAVE_GNUTLS */