]>
Commit | Line | Data |
---|---|---|
cd492e44 | 1 | /* |
fe037171 | 2 | * librb: a library used by ircd-ratbox and other things |
cd492e44 AC |
3 | * mbedtls.c: mbedtls related code |
4 | * | |
5 | * Copyright (C) 2007-2008 ircd-ratbox development team | |
6 | * Copyright (C) 2007-2008 Aaron Sethman <androsyn@ratbox.org> | |
7 | * Copyright (C) 2015 William Pitcock <nenolod@dereferenced.org> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
22 | * USA | |
23 | * | |
cd492e44 AC |
24 | */ |
25 | ||
fe037171 EM |
26 | #include <librb_config.h> |
27 | #include <rb_lib.h> | |
cd492e44 AC |
28 | #include <commio-int.h> |
29 | #include <commio-ssl.h> | |
30 | ||
31 | #ifdef HAVE_MBEDTLS | |
32 | ||
33 | #include "mbedtls/entropy.h" | |
34 | #include "mbedtls/ctr_drbg.h" | |
35 | #include "mbedtls/certs.h" | |
36 | #include "mbedtls/x509.h" | |
37 | #include "mbedtls/ssl.h" | |
38 | #include "mbedtls/net.h" | |
39 | #include "mbedtls/error.h" | |
40 | #include "mbedtls/debug.h" | |
41 | #include "mbedtls/dhm.h" | |
42 | #include "mbedtls/version.h" | |
43 | ||
44 | static mbedtls_x509_crt x509; | |
45 | static mbedtls_pk_context serv_pk; | |
46 | static mbedtls_dhm_context dh_params; | |
47 | static mbedtls_ctr_drbg_context ctr_drbg; | |
48 | static mbedtls_entropy_context entropy; | |
49 | static mbedtls_ssl_config serv_config; | |
50 | static mbedtls_ssl_config client_config; | |
51 | ||
52 | #define SSL_P(x) ((mbedtls_ssl_context *)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 | int r = mbedtls_ssl_close_notify(SSL_P(F)); | |
63 | if(r != MBEDTLS_ERR_SSL_WANT_READ && r != MBEDTLS_ERR_SSL_WANT_WRITE) | |
64 | break; | |
65 | } | |
66 | mbedtls_ssl_free(SSL_P(F)); | |
67 | rb_free(F->ssl); | |
68 | } | |
69 | ||
70 | unsigned int | |
71 | rb_ssl_handshake_count(rb_fde_t *F) | |
72 | { | |
73 | return F->handshake_count; | |
74 | } | |
75 | ||
76 | void | |
77 | rb_ssl_clear_handshake_count(rb_fde_t *F) | |
78 | { | |
79 | F->handshake_count = 0; | |
80 | } | |
81 | ||
82 | static void | |
83 | rb_ssl_timeout(rb_fde_t *F, void *notused) | |
84 | { | |
85 | lrb_assert(F->accept != NULL); | |
86 | F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data); | |
87 | } | |
88 | ||
89 | ||
90 | static int | |
91 | do_ssl_handshake(rb_fde_t *F, PF * callback, void *data) | |
92 | { | |
93 | int ret; | |
94 | int flags; | |
95 | ||
96 | ret = mbedtls_ssl_handshake(SSL_P(F)); | |
97 | if(ret < 0) | |
98 | { | |
539d912b AC |
99 | if (ret == -1 && rb_ignore_errno(errno)) |
100 | ret = MBEDTLS_ERR_SSL_WANT_READ; | |
101 | ||
cd492e44 AC |
102 | if((ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)) |
103 | { | |
104 | if(ret == MBEDTLS_ERR_SSL_WANT_READ) | |
105 | flags = RB_SELECT_READ; | |
106 | else | |
107 | flags = RB_SELECT_WRITE; | |
108 | rb_setselect(F, flags, callback, data); | |
109 | return 0; | |
110 | } | |
539d912b | 111 | |
cd492e44 AC |
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); | |
136 | ||
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 | static int | |
146 | rb_ssl_read_cb(void *opaque, unsigned char *buf, size_t size) | |
147 | { | |
539d912b | 148 | int ret; |
cd492e44 AC |
149 | rb_fde_t *F = opaque; |
150 | ||
539d912b AC |
151 | ret = read(F->fd, buf, size); |
152 | if (ret < 0 && rb_ignore_errno(errno)) | |
153 | return MBEDTLS_ERR_SSL_WANT_READ; | |
154 | ||
155 | return ret; | |
cd492e44 AC |
156 | } |
157 | ||
158 | static int | |
159 | rb_ssl_write_cb(void *opaque, const unsigned char *buf, size_t size) | |
160 | { | |
161 | rb_fde_t *F = opaque; | |
539d912b | 162 | int ret; |
cd492e44 | 163 | |
539d912b AC |
164 | ret = write(F->fd, buf, size); |
165 | if (ret < 0 && rb_ignore_errno(errno)) | |
166 | return MBEDTLS_ERR_SSL_WANT_WRITE; | |
167 | ||
168 | return ret; | |
cd492e44 AC |
169 | } |
170 | ||
171 | static void | |
172 | rb_ssl_setup_srv_context(rb_fde_t *F, mbedtls_ssl_context *ssl) | |
173 | { | |
174 | int ret; | |
175 | ||
176 | mbedtls_ssl_init(ssl); | |
177 | if ((ret = mbedtls_ssl_setup(ssl, &serv_config)) != 0) | |
178 | { | |
179 | rb_lib_log("rb_ssl_setup_srv_context: failed to set up ssl context: -0x%x", -ret); | |
180 | rb_close(F); | |
181 | return; | |
182 | } | |
183 | ||
184 | mbedtls_ssl_set_bio(ssl, F, rb_ssl_write_cb, rb_ssl_read_cb, NULL); | |
185 | } | |
186 | ||
187 | void | |
188 | rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout) | |
189 | { | |
190 | mbedtls_ssl_context *ssl; | |
191 | new_F->type |= RB_FD_SSL; | |
192 | ssl = new_F->ssl = rb_malloc(sizeof(mbedtls_ssl_context)); | |
193 | new_F->accept = rb_malloc(sizeof(struct acceptdata)); | |
194 | ||
195 | new_F->accept->callback = cb; | |
196 | new_F->accept->data = data; | |
197 | rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL); | |
198 | ||
199 | new_F->accept->addrlen = 0; | |
200 | ||
201 | rb_ssl_setup_srv_context(new_F, ssl); | |
202 | if(do_ssl_handshake(new_F, rb_ssl_tryaccept, NULL)) | |
203 | { | |
204 | struct acceptdata *ad = new_F->accept; | |
205 | new_F->accept = NULL; | |
539d912b | 206 | |
cd492e44 AC |
207 | ad->callback(new_F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data); |
208 | rb_free(ad); | |
209 | } | |
210 | } | |
211 | ||
212 | void | |
213 | rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen) | |
214 | { | |
215 | new_F->type |= RB_FD_SSL; | |
216 | new_F->ssl = rb_malloc(sizeof(mbedtls_ssl_context)); | |
217 | new_F->accept = rb_malloc(sizeof(struct acceptdata)); | |
218 | ||
219 | new_F->accept->callback = F->accept->callback; | |
220 | new_F->accept->data = F->accept->data; | |
221 | rb_settimeout(new_F, 10, rb_ssl_timeout, NULL); | |
222 | memcpy(&new_F->accept->S, st, addrlen); | |
223 | new_F->accept->addrlen = addrlen; | |
224 | ||
225 | rb_ssl_setup_srv_context(new_F, new_F->ssl); | |
226 | if(do_ssl_handshake(F, rb_ssl_tryaccept, NULL)) | |
227 | { | |
228 | struct acceptdata *ad = F->accept; | |
229 | F->accept = NULL; | |
539d912b | 230 | |
cd492e44 AC |
231 | ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data); |
232 | rb_free(ad); | |
233 | } | |
234 | } | |
235 | ||
236 | static ssize_t | |
237 | rb_ssl_read_or_write(int r_or_w, rb_fde_t *F, void *rbuf, const void *wbuf, size_t count) | |
238 | { | |
239 | ssize_t ret; | |
240 | ||
241 | if(r_or_w == 0) | |
242 | ret = mbedtls_ssl_read(F->ssl, rbuf, count); | |
243 | else | |
244 | ret = mbedtls_ssl_write(F->ssl, wbuf, count); | |
245 | ||
246 | if(ret < 0) | |
247 | { | |
248 | switch (ret) | |
249 | { | |
250 | case MBEDTLS_ERR_SSL_WANT_READ: | |
251 | return RB_RW_SSL_NEED_READ; | |
252 | case MBEDTLS_ERR_SSL_WANT_WRITE: | |
253 | return RB_RW_SSL_NEED_WRITE; | |
254 | default: | |
255 | F->ssl_errno = ret; | |
256 | errno = EIO; | |
257 | return RB_RW_IO_ERROR; | |
258 | } | |
259 | } | |
260 | ||
261 | return ret; | |
262 | } | |
263 | ||
264 | ssize_t | |
265 | rb_ssl_read(rb_fde_t *F, void *buf, size_t count) | |
266 | { | |
267 | return rb_ssl_read_or_write(0, F, buf, NULL, count); | |
268 | } | |
269 | ||
270 | ssize_t | |
271 | rb_ssl_write(rb_fde_t *F, const void *buf, size_t count) | |
272 | { | |
273 | return rb_ssl_read_or_write(1, F, NULL, buf, count); | |
274 | } | |
275 | ||
276 | int | |
277 | rb_init_ssl(void) | |
278 | { | |
279 | int ret; | |
280 | ||
281 | mbedtls_entropy_init(&entropy); | |
282 | mbedtls_ctr_drbg_init(&ctr_drbg); | |
283 | ||
284 | if((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)) != 0) | |
285 | { | |
286 | rb_lib_log("rb_init_prng: unable to initialize PRNG, mbedtls_ctr_drbg_seed() returned -0x%x", -ret); | |
287 | return 0; | |
288 | } | |
289 | ||
290 | mbedtls_ssl_config_init(&serv_config); | |
291 | ||
292 | if ((ret = mbedtls_ssl_config_defaults(&serv_config, | |
293 | MBEDTLS_SSL_IS_SERVER, | |
294 | MBEDTLS_SSL_TRANSPORT_STREAM, | |
295 | MBEDTLS_SSL_PRESET_DEFAULT)) != 0) | |
296 | { | |
297 | rb_lib_log("rb_init_ssl: unable to initialize default SSL parameters for server context: -0x%x", -ret); | |
298 | return 0; | |
299 | } | |
300 | ||
301 | mbedtls_ssl_conf_rng(&serv_config, mbedtls_ctr_drbg_random, &ctr_drbg); | |
302 | ||
303 | /***************************************************************************************************************/ | |
304 | ||
305 | mbedtls_ssl_config_init(&client_config); | |
306 | ||
307 | if ((ret = mbedtls_ssl_config_defaults(&client_config, | |
308 | MBEDTLS_SSL_IS_CLIENT, | |
309 | MBEDTLS_SSL_TRANSPORT_STREAM, | |
310 | MBEDTLS_SSL_PRESET_DEFAULT)) != 0) | |
311 | { | |
312 | rb_lib_log("rb_init_ssl: unable to initialize default SSL parameters for client context: -0x%x", -ret); | |
313 | return 0; | |
314 | } | |
315 | ||
539d912b | 316 | mbedtls_ssl_conf_rng(&client_config, mbedtls_ctr_drbg_random, &ctr_drbg); |
162a91d6 | 317 | mbedtls_ssl_conf_authmode(&client_config, MBEDTLS_SSL_VERIFY_NONE); |
cd492e44 AC |
318 | |
319 | return 1; | |
320 | } | |
321 | ||
322 | int | |
c1725bda | 323 | rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile, const char *cipher_list) |
cd492e44 AC |
324 | { |
325 | int ret; | |
326 | ||
327 | mbedtls_x509_crt_init(&x509); | |
328 | ret = mbedtls_x509_crt_parse_file(&x509, cert); | |
329 | if (ret != 0) | |
330 | { | |
539d912b | 331 | rb_lib_log("rb_setup_ssl_server: failed to parse certificate '%s': -0x%x", cert, -ret); |
cd492e44 AC |
332 | return 0; |
333 | } | |
334 | ||
335 | mbedtls_pk_init(&serv_pk); | |
336 | ret = mbedtls_pk_parse_keyfile(&serv_pk, keyfile, NULL); | |
337 | if (ret != 0) | |
338 | { | |
539d912b | 339 | rb_lib_log("rb_setup_ssl_server: failed to parse private key '%s': -0x%x", keyfile, -ret); |
cd492e44 AC |
340 | return 0; |
341 | } | |
342 | ||
343 | mbedtls_dhm_init(&dh_params); | |
344 | ret = mbedtls_dhm_parse_dhmfile(&dh_params, dhfile); | |
345 | if (ret != 0) | |
346 | { | |
539d912b | 347 | rb_lib_log("rb_setup_ssl_server: failed to parse DH parameters '%s': -0x%x", dhfile, -ret); |
cd492e44 AC |
348 | return 0; |
349 | } | |
350 | ||
351 | ret = mbedtls_ssl_conf_dh_param_ctx(&serv_config, &dh_params); | |
352 | if (ret != 0) | |
353 | { | |
354 | rb_lib_log("rb_setup_ssl_server: failed to set DH parameters on SSL config context: -0x%x", -ret); | |
355 | return 0; | |
356 | } | |
357 | ||
358 | if (x509.next) | |
493897d6 | 359 | { |
cd492e44 | 360 | mbedtls_ssl_conf_ca_chain(&serv_config, x509.next, NULL); |
493897d6 AC |
361 | mbedtls_ssl_conf_ca_chain(&client_config, x509.next, NULL); |
362 | } | |
cd492e44 AC |
363 | |
364 | if ((ret = mbedtls_ssl_conf_own_cert(&serv_config, &x509, &serv_pk)) != 0) | |
365 | { | |
366 | rb_lib_log("rb_setup_ssl_server: failed to set up own certificate: -0x%x", -ret); | |
367 | return 0; | |
368 | } | |
369 | ||
493897d6 AC |
370 | if ((ret = mbedtls_ssl_conf_own_cert(&client_config, &x509, &serv_pk)) != 0) |
371 | { | |
372 | rb_lib_log("rb_setup_ssl_server: failed to set up own certificate: -0x%x", -ret); | |
373 | return 0; | |
374 | } | |
375 | ||
c1725bda AC |
376 | /* XXX support cipher lists when added to mbedtls */ |
377 | ||
cd492e44 AC |
378 | return 1; |
379 | } | |
380 | ||
381 | int | |
382 | rb_ssl_listen(rb_fde_t *F, int backlog, int defer_accept) | |
383 | { | |
384 | int result; | |
385 | ||
386 | result = rb_listen(F, backlog, defer_accept); | |
387 | F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; | |
388 | ||
389 | return result; | |
390 | } | |
391 | ||
392 | struct ssl_connect | |
393 | { | |
394 | CNCB *callback; | |
395 | void *data; | |
396 | int timeout; | |
397 | }; | |
398 | ||
399 | static void | |
400 | rb_ssl_connect_realcb(rb_fde_t *F, int status, struct ssl_connect *sconn) | |
401 | { | |
402 | F->connect->callback = sconn->callback; | |
403 | F->connect->data = sconn->data; | |
404 | rb_free(sconn); | |
405 | rb_connect_callback(F, status); | |
406 | } | |
407 | ||
408 | static void | |
409 | rb_ssl_tryconn_timeout_cb(rb_fde_t *F, void *data) | |
410 | { | |
411 | rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); | |
412 | } | |
413 | ||
414 | static void | |
415 | rb_ssl_tryconn_cb(rb_fde_t *F, void *data) | |
416 | { | |
417 | struct ssl_connect *sconn = data; | |
418 | int ret; | |
419 | ||
420 | ret = do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn); | |
421 | ||
422 | switch (ret) | |
423 | { | |
424 | case -1: | |
425 | rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn); | |
426 | break; | |
427 | case 0: | |
428 | /* do_ssl_handshake does the rb_setselect stuff */ | |
429 | return; | |
430 | default: | |
431 | break; | |
432 | ||
433 | ||
434 | } | |
435 | rb_ssl_connect_realcb(F, RB_OK, sconn); | |
436 | } | |
437 | ||
438 | static void | |
439 | rb_ssl_setup_client_context(rb_fde_t *F, mbedtls_ssl_context *ssl) | |
440 | { | |
441 | int ret; | |
442 | ||
443 | mbedtls_ssl_init(ssl); | |
444 | if ((ret = mbedtls_ssl_setup(ssl, &client_config)) != 0) | |
445 | { | |
446 | rb_lib_log("rb_ssl_setup_client_context: failed to set up ssl context: -0x%x", -ret); | |
447 | rb_close(F); | |
448 | return; | |
449 | } | |
450 | ||
451 | mbedtls_ssl_set_bio(ssl, F, rb_ssl_write_cb, rb_ssl_read_cb, NULL); | |
452 | } | |
453 | ||
454 | static void | |
455 | rb_ssl_tryconn(rb_fde_t *F, int status, void *data) | |
456 | { | |
457 | struct ssl_connect *sconn = data; | |
458 | if(status != RB_OK) | |
459 | { | |
460 | rb_ssl_connect_realcb(F, status, sconn); | |
461 | return; | |
462 | } | |
463 | ||
464 | F->type |= RB_FD_SSL; | |
465 | ||
466 | ||
467 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); | |
468 | F->ssl = rb_malloc(sizeof(mbedtls_ssl_context)); | |
469 | rb_ssl_setup_client_context(F, F->ssl); | |
470 | ||
471 | do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn); | |
472 | } | |
473 | ||
474 | void | |
475 | rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest, | |
476 | struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout) | |
477 | { | |
478 | struct ssl_connect *sconn; | |
479 | if(F == NULL) | |
480 | return; | |
481 | ||
482 | sconn = rb_malloc(sizeof(struct ssl_connect)); | |
483 | sconn->data = data; | |
484 | sconn->callback = callback; | |
485 | sconn->timeout = timeout; | |
486 | rb_connect_tcp(F, dest, clocal, socklen, rb_ssl_tryconn, sconn, timeout); | |
487 | } | |
488 | ||
489 | void | |
490 | rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout) | |
491 | { | |
492 | struct ssl_connect *sconn; | |
493 | if(F == NULL) | |
494 | return; | |
495 | ||
496 | sconn = rb_malloc(sizeof(struct ssl_connect)); | |
497 | sconn->data = data; | |
498 | sconn->callback = callback; | |
499 | sconn->timeout = timeout; | |
500 | F->connect = rb_malloc(sizeof(struct conndata)); | |
501 | F->connect->callback = callback; | |
502 | F->connect->data = data; | |
503 | F->type |= RB_FD_SSL; | |
504 | F->ssl = rb_malloc(sizeof(mbedtls_ssl_context)); | |
505 | ||
506 | rb_ssl_setup_client_context(F, F->ssl); | |
507 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); | |
508 | ||
509 | do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn); | |
510 | } | |
511 | ||
512 | int | |
513 | rb_init_prng(const char *path, prng_seed_t seed_type) | |
514 | { | |
515 | return 1; | |
516 | } | |
517 | ||
518 | int | |
519 | rb_get_random(void *buf, size_t length) | |
520 | { | |
521 | if (mbedtls_ctr_drbg_random(&ctr_drbg, buf, length)) | |
522 | return 0; | |
523 | ||
524 | return 1; | |
525 | } | |
526 | ||
527 | const char * | |
528 | rb_get_ssl_strerror(rb_fde_t *F) | |
529 | { | |
530 | #ifdef MBEDTLS_ERROR_C | |
531 | static char errbuf[512]; | |
532 | mbedtls_strerror(F->ssl_errno, errbuf, sizeof errbuf); | |
533 | return errbuf; | |
534 | #else | |
535 | return "???"; | |
536 | #endif | |
537 | } | |
538 | ||
539 | int | |
e6bbb410 | 540 | rb_get_ssl_certfp(rb_fde_t *F, uint8_t certfp[RB_SSL_CERTFP_LEN], int method) |
cd492e44 AC |
541 | { |
542 | const mbedtls_x509_crt *peer_cert; | |
677d3cb1 | 543 | uint8_t hash[RB_SSL_CERTFP_LEN]; |
e6bbb410 | 544 | size_t hashlen; |
677d3cb1 | 545 | const mbedtls_md_info_t *md_info; |
e6bbb410 | 546 | mbedtls_md_type_t md_type; |
677d3cb1 | 547 | int ret; |
cd492e44 | 548 | |
e6bbb410 EM |
549 | switch (method) |
550 | { | |
551 | case RB_SSL_CERTFP_METH_SHA1: | |
552 | md_type = MBEDTLS_MD_SHA1; | |
553 | hashlen = RB_SSL_CERTFP_LEN_SHA1; | |
7da82465 | 554 | break; |
e6bbb410 EM |
555 | case RB_SSL_CERTFP_METH_SHA256: |
556 | md_type = MBEDTLS_MD_SHA256; | |
557 | hashlen = RB_SSL_CERTFP_LEN_SHA256; | |
7da82465 | 558 | break; |
e6bbb410 EM |
559 | case RB_SSL_CERTFP_METH_SHA512: |
560 | md_type = MBEDTLS_MD_SHA512; | |
561 | hashlen = RB_SSL_CERTFP_LEN_SHA512; | |
7da82465 | 562 | break; |
e6bbb410 EM |
563 | default: |
564 | return 0; | |
565 | } | |
566 | ||
cd492e44 AC |
567 | peer_cert = mbedtls_ssl_get_peer_cert(SSL_P(F)); |
568 | if (peer_cert == NULL) | |
569 | return 0; | |
570 | ||
e6bbb410 | 571 | md_info = mbedtls_md_info_from_type(md_type); |
677d3cb1 | 572 | if (md_info == NULL) |
cd492e44 | 573 | return 0; |
cd492e44 | 574 | |
677d3cb1 | 575 | if ((ret = mbedtls_md(md_info, peer_cert->raw.p, peer_cert->raw.len, hash)) != 0) |
cd492e44 | 576 | { |
677d3cb1 | 577 | rb_lib_log("rb_get_ssl_certfp: unable to get certfp for F: %p, -0x%x", -ret); |
cd492e44 AC |
578 | return 0; |
579 | } | |
580 | ||
e6bbb410 | 581 | memcpy(certfp, hash, hashlen); |
cd492e44 | 582 | |
7da82465 | 583 | return hashlen; |
cd492e44 AC |
584 | } |
585 | ||
586 | int | |
587 | rb_supports_ssl(void) | |
588 | { | |
589 | return 1; | |
590 | } | |
591 | ||
592 | void | |
593 | rb_get_ssl_info(char *buf, size_t len) | |
594 | { | |
595 | char version_str[512]; | |
596 | mbedtls_version_get_string(version_str); | |
597 | ||
5203cba5 | 598 | snprintf(buf, len, "MBEDTLS: compiled (%s), library(%s)", |
cd492e44 AC |
599 | MBEDTLS_VERSION_STRING, version_str); |
600 | } | |
601 | ||
833b2f9c AC |
602 | const char * |
603 | rb_ssl_get_cipher(rb_fde_t *F) | |
604 | { | |
605 | if(F == NULL || F->ssl == NULL) | |
606 | return NULL; | |
607 | return mbedtls_ssl_get_ciphersuite(SSL_P(F)); | |
608 | } | |
cd492e44 AC |
609 | |
610 | #endif /* HAVE_GNUTLS */ |