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