]>
Commit | Line | Data |
---|---|---|
2bd29df9 AB |
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. | |
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 | * | |
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> | |
7aa40f6d AC |
34 | #include <gnutls/crypto.h> |
35 | ||
36 | #if GNUTLS_VERSION_MAJOR < 3 | |
37 | # include <gcrypt.h> | |
38 | #endif | |
2bd29df9 AB |
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); | |
55abcbb2 | 131 | |
2bd29df9 AB |
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 | { | |
7aa40f6d | 253 | #if GNUTLS_VERSION_MAJOR < 3 |
2bd29df9 | 254 | gcry_fast_random_poll(); |
7aa40f6d | 255 | #endif |
2bd29df9 AB |
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 | |
c1725bda | 330 | rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile, const char *cipher_list) |
2bd29df9 AB |
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 | } | |
c1725bda AC |
405 | |
406 | /* XXX integrate gnutls_priority_init() */ | |
2bd29df9 AB |
407 | return 1; |
408 | } | |
409 | ||
410 | int | |
411 | rb_ssl_listen(rb_fde_t *F, int backlog, int defer_accept) | |
412 | { | |
413 | int result; | |
414 | ||
fab6f9e8 | 415 | result = rb_listen(F, backlog, defer_accept); |
2bd29df9 AB |
416 | F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; |
417 | ||
418 | return result; | |
419 | } | |
420 | ||
421 | struct ssl_connect | |
422 | { | |
423 | CNCB *callback; | |
424 | void *data; | |
425 | int timeout; | |
426 | }; | |
427 | ||
428 | static void | |
429 | rb_ssl_connect_realcb(rb_fde_t *F, int status, struct ssl_connect *sconn) | |
430 | { | |
431 | F->connect->callback = sconn->callback; | |
432 | F->connect->data = sconn->data; | |
433 | rb_free(sconn); | |
434 | rb_connect_callback(F, status); | |
435 | } | |
436 | ||
437 | static void | |
438 | rb_ssl_tryconn_timeout_cb(rb_fde_t *F, void *data) | |
439 | { | |
440 | rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); | |
441 | } | |
442 | ||
443 | static void | |
444 | rb_ssl_tryconn_cb(rb_fde_t *F, void *data) | |
445 | { | |
446 | struct ssl_connect *sconn = data; | |
447 | int ret; | |
448 | ||
449 | ret = do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn); | |
450 | ||
451 | switch (ret) | |
452 | { | |
453 | case -1: | |
454 | rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn); | |
455 | break; | |
456 | case 0: | |
457 | /* do_ssl_handshake does the rb_setselect stuff */ | |
458 | return; | |
459 | default: | |
460 | break; | |
461 | ||
462 | ||
463 | } | |
464 | rb_ssl_connect_realcb(F, RB_OK, sconn); | |
465 | } | |
466 | ||
467 | static void | |
468 | rb_ssl_tryconn(rb_fde_t *F, int status, void *data) | |
469 | { | |
470 | struct ssl_connect *sconn = data; | |
471 | if(status != RB_OK) | |
472 | { | |
473 | rb_ssl_connect_realcb(F, status, sconn); | |
474 | return; | |
475 | } | |
476 | ||
477 | F->type |= RB_FD_SSL; | |
478 | ||
479 | ||
480 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); | |
481 | F->ssl = rb_malloc(sizeof(gnutls_session_t)); | |
482 | gnutls_init(F->ssl, GNUTLS_CLIENT); | |
483 | gnutls_set_default_priority(SSL_P(F)); | |
484 | gnutls_credentials_set(SSL_P(F), GNUTLS_CRD_CERTIFICATE, x509); | |
485 | gnutls_dh_set_prime_bits(SSL_P(F), 1024); | |
486 | gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd); | |
487 | ||
488 | do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn); | |
489 | } | |
490 | ||
491 | void | |
492 | rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest, | |
493 | struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout) | |
494 | { | |
495 | struct ssl_connect *sconn; | |
496 | if(F == NULL) | |
497 | return; | |
498 | ||
499 | sconn = rb_malloc(sizeof(struct ssl_connect)); | |
500 | sconn->data = data; | |
501 | sconn->callback = callback; | |
502 | sconn->timeout = timeout; | |
503 | rb_connect_tcp(F, dest, clocal, socklen, rb_ssl_tryconn, sconn, timeout); | |
504 | ||
505 | } | |
506 | ||
507 | void | |
508 | rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout) | |
509 | { | |
510 | struct ssl_connect *sconn; | |
511 | if(F == NULL) | |
512 | return; | |
513 | ||
514 | sconn = rb_malloc(sizeof(struct ssl_connect)); | |
515 | sconn->data = data; | |
516 | sconn->callback = callback; | |
517 | sconn->timeout = timeout; | |
518 | F->connect = rb_malloc(sizeof(struct conndata)); | |
519 | F->connect->callback = callback; | |
520 | F->connect->data = data; | |
521 | F->type |= RB_FD_SSL; | |
522 | F->ssl = rb_malloc(sizeof(gnutls_session_t)); | |
523 | ||
524 | gnutls_init(F->ssl, GNUTLS_CLIENT); | |
525 | gnutls_set_default_priority(SSL_P(F)); | |
526 | gnutls_credentials_set(SSL_P(F), GNUTLS_CRD_CERTIFICATE, x509); | |
527 | gnutls_dh_set_prime_bits(SSL_P(F), 1024); | |
528 | gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd); | |
529 | ||
530 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); | |
531 | ||
532 | do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn); | |
533 | } | |
534 | ||
535 | int | |
536 | rb_init_prng(const char *path, prng_seed_t seed_type) | |
537 | { | |
7aa40f6d | 538 | #if GNUTLS_VERSION_MAJOR < 3 |
2bd29df9 | 539 | gcry_fast_random_poll(); |
4cb0a93d AC |
540 | #else |
541 | gnutls_rnd_refresh(); | |
7aa40f6d | 542 | #endif |
2bd29df9 AB |
543 | return 1; |
544 | } | |
545 | ||
546 | int | |
547 | rb_get_random(void *buf, size_t length) | |
548 | { | |
7aa40f6d | 549 | #if GNUTLS_VERSION_MAJOR < 3 |
2bd29df9 | 550 | gcry_randomize(buf, length, GCRY_STRONG_RANDOM); |
7aa40f6d AC |
551 | #else |
552 | gnutls_rnd(GNUTLS_RND_KEY, buf, length); | |
553 | #endif | |
2bd29df9 AB |
554 | return 1; |
555 | } | |
556 | ||
2bd29df9 AB |
557 | const char * |
558 | rb_get_ssl_strerror(rb_fde_t *F) | |
559 | { | |
560 | return gnutls_strerror(F->ssl_errno); | |
561 | } | |
562 | ||
563 | int | |
e6bbb410 | 564 | rb_get_ssl_certfp(rb_fde_t *F, uint8_t certfp[RB_SSL_CERTFP_LEN], int method) |
2bd29df9 AB |
565 | { |
566 | gnutls_x509_crt_t cert; | |
e6bbb410 | 567 | gnutls_digest_algorithm_t algo; |
2bd29df9 AB |
568 | unsigned int cert_list_size; |
569 | const gnutls_datum_t *cert_list; | |
570 | uint8_t digest[RB_SSL_CERTFP_LEN * 2]; | |
571 | size_t digest_size; | |
e6bbb410 | 572 | int len; |
2bd29df9 AB |
573 | |
574 | if (gnutls_certificate_type_get(SSL_P(F)) != GNUTLS_CRT_X509) | |
575 | return 0; | |
576 | ||
577 | if (gnutls_x509_crt_init(&cert) < 0) | |
578 | return 0; | |
579 | ||
580 | cert_list_size = 0; | |
581 | cert_list = gnutls_certificate_get_peers(SSL_P(F), &cert_list_size); | |
582 | if (cert_list == NULL) | |
583 | { | |
584 | gnutls_x509_crt_deinit(cert); | |
585 | return 0; | |
586 | } | |
587 | ||
588 | if (gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0) | |
589 | { | |
590 | gnutls_x509_crt_deinit(cert); | |
591 | return 0; | |
592 | } | |
593 | ||
e6bbb410 EM |
594 | switch(method) |
595 | { | |
596 | case RB_SSL_CERTFP_METH_SHA1: | |
597 | algo = GNUTLS_DIG_SHA1; | |
598 | len = RB_SSL_CERTFP_LEN_SHA1; | |
599 | break; | |
600 | case RB_SSL_CERTFP_METH_SHA256: | |
601 | algo = GNUTLS_DIG_SHA256; | |
602 | len = RB_SSL_CERTFP_LEN_SHA256; | |
603 | break; | |
604 | case RB_SSL_CERTFP_METH_SHA512: | |
605 | algo = GNUTLS_DIG_SHA512; | |
606 | len = RB_SSL_CERTFP_LEN_SHA512; | |
607 | break; | |
608 | default: | |
609 | return 0; | |
610 | } | |
611 | ||
612 | if (gnutls_x509_crt_get_fingerprint(cert, algo, digest, &digest_size) < 0) | |
2bd29df9 AB |
613 | { |
614 | gnutls_x509_crt_deinit(cert); | |
615 | return 0; | |
616 | } | |
617 | ||
e6bbb410 | 618 | memcpy(certfp, digest, len); |
2bd29df9 AB |
619 | |
620 | gnutls_x509_crt_deinit(cert); | |
e6bbb410 | 621 | return len; |
2bd29df9 AB |
622 | } |
623 | ||
624 | int | |
625 | rb_supports_ssl(void) | |
626 | { | |
627 | return 1; | |
628 | } | |
629 | ||
630 | void | |
631 | rb_get_ssl_info(char *buf, size_t len) | |
632 | { | |
55abcbb2 | 633 | rb_snprintf(buf, len, "GNUTLS: compiled (%s), library(%s)", |
2bd29df9 AB |
634 | LIBGNUTLS_VERSION, gnutls_check_version(NULL)); |
635 | } | |
55abcbb2 | 636 | |
833b2f9c AC |
637 | const char * |
638 | rb_ssl_get_cipher(rb_fde_t *F) | |
639 | { | |
640 | static char buf[1024]; | |
641 | ||
642 | rb_snprintf(buf, sizeof(buf), "%s-%s-%s-%s", | |
643 | gnutls_protocol_get_name(gnutls_protocol_get_version(SSL_P(F))), | |
644 | gnutls_kx_get_name(gnutls_kx_get(SSL_P(F))), | |
645 | gnutls_cipher_get_name(gnutls_cipher_get(SSL_P(F))), | |
646 | gnutls_mac_get_name(gnutls_mac_get(SSL_P(F)))); | |
647 | ||
648 | return buf; | |
649 | } | |
55abcbb2 | 650 | |
2bd29df9 | 651 | #endif /* HAVE_GNUTLS */ |