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