]> jfr.im git - solanum.git/blob - librb/src/mbedtls.c
GNUTLS: Forward-port release/3.5 improvements
[solanum.git] / librb / src / mbedtls.c
1 /*
2 * librb: a library used by ircd-ratbox and other things
3 * mbedtls.c: ARM MbedTLS backend
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 * Copyright (C) 2016 Aaron Jones <aaronmdjones@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23 * USA
24 *
25 */
26
27 #include <librb_config.h>
28 #include <rb_lib.h>
29
30 #ifdef HAVE_MBEDTLS
31
32 #include <commio-int.h>
33 #include <commio-ssl.h>
34 #include <stdbool.h>
35
36 #include "mbedtls_ratbox.h"
37
38 typedef enum
39 {
40 RB_FD_TLS_DIRECTION_IN = 0,
41 RB_FD_TLS_DIRECTION_OUT = 1
42 } rb_fd_tls_direction;
43
44 #define RB_MAX_CIPHERSUITES 512
45
46 typedef struct
47 {
48 mbedtls_x509_crt crt;
49 mbedtls_pk_context key;
50 mbedtls_dhm_context dhp;
51 mbedtls_ssl_config server_cfg;
52 mbedtls_ssl_config client_cfg;
53 int suites[RB_MAX_CIPHERSUITES + 1];
54 size_t refcount;
55 } rb_mbedtls_cfg_context;
56
57 typedef struct
58 {
59 rb_mbedtls_cfg_context *cfg;
60 mbedtls_ssl_context ssl;
61 } rb_mbedtls_ssl_context;
62
63 #define SSL_C(x) ((rb_mbedtls_ssl_context *) (x)->ssl)->cfg
64 #define SSL_P(x) &((rb_mbedtls_ssl_context *) (x)->ssl)->ssl
65
66 static mbedtls_ctr_drbg_context ctr_drbg_ctx;
67 static mbedtls_entropy_context entropy_ctx;
68
69 static mbedtls_x509_crt dummy_ca_ctx;
70 static rb_mbedtls_cfg_context *rb_mbedtls_cfg = NULL;
71
72
73
74 struct ssl_connect
75 {
76 CNCB *callback;
77 void *data;
78 int timeout;
79 };
80
81 static const char *rb_ssl_strerror(int);
82 static void rb_ssl_connect_realcb(rb_fde_t *, int, struct ssl_connect *);
83
84 static int rb_sock_net_recv(void *, unsigned char *, size_t);
85 static int rb_sock_net_xmit(void *, const unsigned char *, size_t);
86
87
88
89 /*
90 * Internal MbedTLS-specific code
91 */
92
93 static void
94 rb_mbedtls_cfg_incref(rb_mbedtls_cfg_context *const cfg)
95 {
96 lrb_assert(cfg->refcount > 0);
97
98 cfg->refcount++;
99 }
100
101 static void
102 rb_mbedtls_cfg_decref(rb_mbedtls_cfg_context *const cfg)
103 {
104 if(cfg == NULL)
105 return;
106
107 lrb_assert(cfg->refcount > 0);
108
109 if((--cfg->refcount) > 0)
110 return;
111
112 mbedtls_ssl_config_free(&cfg->client_cfg);
113 mbedtls_ssl_config_free(&cfg->server_cfg);
114 mbedtls_dhm_free(&cfg->dhp);
115 mbedtls_pk_free(&cfg->key);
116 mbedtls_x509_crt_free(&cfg->crt);
117
118 rb_free(cfg);
119 }
120
121 static void
122 rb_ssl_init_fd(rb_fde_t *const F, const rb_fd_tls_direction dir)
123 {
124 rb_mbedtls_ssl_context *const mbed_ssl_ctx = rb_malloc(sizeof *mbed_ssl_ctx);
125
126 if(mbed_ssl_ctx == NULL)
127 {
128 rb_lib_log("%s: rb_malloc: allocation failure", __func__);
129 rb_close(F);
130 return;
131 }
132
133 mbedtls_ssl_config *mbed_config;
134
135 switch(dir)
136 {
137 case RB_FD_TLS_DIRECTION_IN:
138 mbed_config = &rb_mbedtls_cfg->server_cfg;
139 break;
140 case RB_FD_TLS_DIRECTION_OUT:
141 mbed_config = &rb_mbedtls_cfg->client_cfg;
142 break;
143 }
144
145 mbedtls_ssl_init(&mbed_ssl_ctx->ssl);
146
147 int ret;
148
149 if((ret = mbedtls_ssl_setup(&mbed_ssl_ctx->ssl, mbed_config)) != 0)
150 {
151 rb_lib_log("%s: ssl_setup: %s", __func__, rb_ssl_strerror(ret));
152 mbedtls_ssl_free(&mbed_ssl_ctx->ssl);
153 rb_free(mbed_ssl_ctx);
154 rb_close(F);
155 return;
156 }
157
158 mbedtls_ssl_set_bio(&mbed_ssl_ctx->ssl, F, rb_sock_net_xmit, rb_sock_net_recv, NULL);
159
160 rb_mbedtls_cfg_incref(rb_mbedtls_cfg);
161 mbed_ssl_ctx->cfg = rb_mbedtls_cfg;
162
163 F->ssl = mbed_ssl_ctx;
164 }
165
166 static rb_mbedtls_cfg_context *
167 rb_mbedtls_cfg_new(void)
168 {
169 rb_mbedtls_cfg_context *const cfg = rb_malloc(sizeof *cfg);
170
171 if(cfg == NULL)
172 return NULL;
173
174 mbedtls_x509_crt_init(&cfg->crt);
175 mbedtls_pk_init(&cfg->key);
176 mbedtls_dhm_init(&cfg->dhp);
177 mbedtls_ssl_config_init(&cfg->server_cfg);
178 mbedtls_ssl_config_init(&cfg->client_cfg);
179
180 (void) memset(cfg->suites, 0x00, sizeof cfg->suites);
181
182 cfg->refcount = 1;
183
184 int ret;
185
186 if((ret = mbedtls_ssl_config_defaults(&cfg->server_cfg,
187 MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM,
188 MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
189 {
190 rb_lib_log("%s: ssl_config_defaults (server): %s", __func__, rb_ssl_strerror(ret));
191 rb_mbedtls_cfg_decref(cfg);
192 return NULL;
193 }
194
195 if((ret = mbedtls_ssl_config_defaults(&cfg->client_cfg,
196 MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
197 MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
198 {
199 rb_lib_log("%s: ssl_config_defaults (client): %s", __func__, rb_ssl_strerror(ret));
200 rb_mbedtls_cfg_decref(cfg);
201 return NULL;
202 }
203
204 mbedtls_ssl_conf_rng(&cfg->server_cfg, mbedtls_ctr_drbg_random, &ctr_drbg_ctx);
205 mbedtls_ssl_conf_rng(&cfg->client_cfg, mbedtls_ctr_drbg_random, &ctr_drbg_ctx);
206
207 mbedtls_ssl_conf_ca_chain(&cfg->server_cfg, &dummy_ca_ctx, NULL);
208 mbedtls_ssl_conf_ca_chain(&cfg->client_cfg, &dummy_ca_ctx, NULL);
209
210 mbedtls_ssl_conf_authmode(&cfg->server_cfg, MBEDTLS_SSL_VERIFY_OPTIONAL);
211 mbedtls_ssl_conf_authmode(&cfg->client_cfg, MBEDTLS_SSL_VERIFY_NONE);
212
213 #ifdef MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE
214 mbedtls_ssl_conf_legacy_renegotiation(&cfg->client_cfg, MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE);
215 #endif
216
217 #ifdef MBEDTLS_SSL_SESSION_TICKETS_DISABLED
218 mbedtls_ssl_conf_session_tickets(&cfg->client_cfg, MBEDTLS_SSL_SESSION_TICKETS_DISABLED);
219 #endif
220
221 return cfg;
222 }
223
224 static void
225 rb_ssl_accept_common(rb_fde_t *const F, void *const data)
226 {
227 lrb_assert(F != NULL);
228 lrb_assert(F->accept != NULL);
229 lrb_assert(F->accept->callback != NULL);
230 lrb_assert(F->ssl != NULL);
231
232 const int ret = mbedtls_ssl_handshake(SSL_P(F));
233
234 switch(ret)
235 {
236 case 0:
237 F->handshake_count++;
238 break;
239 case MBEDTLS_ERR_SSL_WANT_READ:
240 rb_setselect(F, RB_SELECT_READ, rb_ssl_accept_common, NULL);
241 return;
242 case MBEDTLS_ERR_SSL_WANT_WRITE:
243 rb_setselect(F, RB_SELECT_WRITE, rb_ssl_accept_common, NULL);
244 return;
245 default:
246 errno = EIO;
247 F->ssl_errno = (unsigned long) -ret;
248 F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data);
249 return;
250 }
251
252 rb_settimeout(F, 0, NULL, NULL);
253 rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL);
254
255 struct acceptdata *const ad = F->accept;
256 F->accept = NULL;
257 ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
258 rb_free(ad);
259 }
260
261 static void
262 rb_ssl_connect_common(rb_fde_t *const F, void *const data)
263 {
264 lrb_assert(F != NULL);
265 lrb_assert(F->ssl != NULL);
266
267 const int ret = mbedtls_ssl_handshake(SSL_P(F));
268
269 switch(ret)
270 {
271 case 0:
272 F->handshake_count++;
273 break;
274 case MBEDTLS_ERR_SSL_WANT_READ:
275 rb_setselect(F, RB_SELECT_READ, rb_ssl_connect_common, data);
276 return;
277 case MBEDTLS_ERR_SSL_WANT_WRITE:
278 rb_setselect(F, RB_SELECT_WRITE, rb_ssl_connect_common, data);
279 return;
280 default:
281 errno = EIO;
282 F->ssl_errno = (unsigned long) -ret;
283 rb_ssl_connect_realcb(F, RB_ERROR_SSL, data);
284 return;
285 }
286
287 rb_ssl_connect_realcb(F, RB_OK, data);
288 }
289
290 static const char *
291 rb_ssl_strerror(const int err)
292 {
293 static char errbuf[512];
294
295 #ifdef MBEDTLS_ERROR_C
296 char mbed_errbuf[512];
297 mbedtls_strerror(err, mbed_errbuf, sizeof mbed_errbuf);
298 (void) snprintf(errbuf, sizeof errbuf, "-0x%x: %s", -err, mbed_errbuf);
299 #else
300 (void) snprintf(errbuf, sizeof errbuf, "-0x%x", -err);
301 #endif
302
303 return errbuf;
304 }
305
306 static int
307 rb_make_certfp(const mbedtls_x509_crt *const peer_cert, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method)
308 {
309 size_t hashlen = 0;
310 mbedtls_md_type_t md_type;
311 bool spki = false;
312
313 switch(method)
314 {
315 case RB_SSL_CERTFP_METH_CERT_SHA1:
316 md_type = MBEDTLS_MD_SHA1;
317 hashlen = RB_SSL_CERTFP_LEN_SHA1;
318 break;
319 case RB_SSL_CERTFP_METH_SPKI_SHA256:
320 spki = true;
321 case RB_SSL_CERTFP_METH_CERT_SHA256:
322 md_type = MBEDTLS_MD_SHA256;
323 hashlen = RB_SSL_CERTFP_LEN_SHA256;
324 break;
325 case RB_SSL_CERTFP_METH_SPKI_SHA512:
326 spki = true;
327 case RB_SSL_CERTFP_METH_CERT_SHA512:
328 md_type = MBEDTLS_MD_SHA512;
329 hashlen = RB_SSL_CERTFP_LEN_SHA512;
330 break;
331 default:
332 return 0;
333 }
334
335 const mbedtls_md_info_t *const md_info = mbedtls_md_info_from_type(md_type);
336 if(md_info == NULL)
337 return 0;
338
339 int ret;
340 void* data = peer_cert->raw.p;
341 size_t datalen = peer_cert->raw.len;
342
343 if(spki)
344 {
345 unsigned char der_pubkey[8192];
346 if((ret = mbedtls_pk_write_pubkey_der((mbedtls_pk_context *)&peer_cert->pk,
347 der_pubkey, sizeof der_pubkey)) < 0)
348 {
349 rb_lib_log("rb_get_ssl_certfp: pk_write_pubkey_der: %s", rb_ssl_strerror(ret));
350 return 0;
351 }
352 data = der_pubkey + (sizeof(der_pubkey) - (size_t)ret);
353 datalen = (size_t)ret;
354 }
355
356 if((ret = mbedtls_md(md_info, data, datalen, certfp)) != 0)
357 {
358 rb_lib_log("rb_get_ssl_certfp: mbedtls_md: %s", rb_ssl_strerror(ret));
359 return 0;
360 }
361
362 return (int) hashlen;
363 }
364
365
366
367 /*
368 * External MbedTLS-specific code
369 */
370
371 void
372 rb_ssl_shutdown(rb_fde_t *const F)
373 {
374 if(F == NULL || F->ssl == NULL)
375 return;
376
377 for(int i = 0; i < 4; i++)
378 {
379 int ret = mbedtls_ssl_close_notify(SSL_P(F));
380
381 if(ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
382 break;
383 }
384
385 mbedtls_ssl_free(SSL_P(F));
386 rb_mbedtls_cfg_decref(SSL_C(F));
387
388 rb_free(F->ssl);
389 F->ssl = NULL;
390 }
391
392 int
393 rb_init_ssl(void)
394 {
395 mbedtls_ctr_drbg_init(&ctr_drbg_ctx);
396 mbedtls_entropy_init(&entropy_ctx);
397
398 int ret;
399
400 if((ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx,
401 (const unsigned char *)rb_mbedtls_personal_str, sizeof(rb_mbedtls_personal_str))) != 0)
402 {
403 rb_lib_log("%s: ctr_drbg_seed: %s", __func__, rb_ssl_strerror(ret));
404 return 0;
405 }
406
407 if((ret = mbedtls_x509_crt_parse_der(&dummy_ca_ctx, rb_mbedtls_dummy_ca_certificate,
408 sizeof(rb_mbedtls_dummy_ca_certificate))) != 0)
409 {
410 rb_lib_log("%s: x509_crt_parse_der (Dummy CA): %s", __func__, rb_ssl_strerror(ret));
411 return 0;
412 }
413
414 rb_lib_log("%s: MbedTLS backend initialised", __func__);
415 return 1;
416 }
417
418 int
419 rb_setup_ssl_server(const char *const certfile, const char *keyfile,
420 const char *const dhfile, const char *const cipherlist)
421 {
422 if(certfile == NULL)
423 {
424 rb_lib_log("%s: no certificate file specified", __func__);
425 return 0;
426 }
427
428 if(keyfile == NULL)
429 keyfile = certfile;
430
431 rb_mbedtls_cfg_context *const newcfg = rb_mbedtls_cfg_new();
432
433 if(newcfg == NULL)
434 {
435 rb_lib_log("%s: rb_mbedtls_cfg_new: allocation failed", __func__);
436 return 0;
437 }
438
439 int ret;
440
441 if((ret = mbedtls_x509_crt_parse_file(&newcfg->crt, certfile)) != 0)
442 {
443 rb_lib_log("%s: x509_crt_parse_file ('%s'): %s", __func__, certfile, rb_ssl_strerror(ret));
444 rb_mbedtls_cfg_decref(newcfg);
445 return 0;
446 }
447 if((ret = mbedtls_pk_parse_keyfile(&newcfg->key, keyfile, NULL)) != 0)
448 {
449 rb_lib_log("%s: pk_parse_keyfile ('%s'): %s", __func__, keyfile, rb_ssl_strerror(ret));
450 rb_mbedtls_cfg_decref(newcfg);
451 return 0;
452 }
453 if((ret = mbedtls_ssl_conf_own_cert(&newcfg->server_cfg, &newcfg->crt, &newcfg->key)) != 0)
454 {
455 rb_lib_log("%s: ssl_conf_own_cert (server): %s", __func__, rb_ssl_strerror(ret));
456 rb_mbedtls_cfg_decref(newcfg);
457 return 0;
458 }
459 if((ret = mbedtls_ssl_conf_own_cert(&newcfg->client_cfg, &newcfg->crt, &newcfg->key)) != 0)
460 {
461 rb_lib_log("%s: ssl_conf_own_cert (client): %s", __func__, rb_ssl_strerror(ret));
462 rb_mbedtls_cfg_decref(newcfg);
463 return 0;
464 }
465
466
467 /* Absense of DH parameters does not matter with mbedTLS, as it comes with its own defaults
468 Thus, clients can still use DHE- ciphersuites, just over a weaker, common DH group
469 So, we do not consider failure to parse DH parameters as fatal */
470 if(dhfile == NULL)
471 {
472 rb_lib_log("%s: no DH parameters file specified", __func__);
473 }
474 else
475 {
476 if((ret = mbedtls_dhm_parse_dhmfile(&newcfg->dhp, dhfile)) != 0)
477 {
478 rb_lib_log("%s: dhm_parse_dhmfile ('%s'): %s", __func__, dhfile, rb_ssl_strerror(ret));
479 }
480 else if((ret = mbedtls_ssl_conf_dh_param_ctx(&newcfg->server_cfg, &newcfg->dhp)) != 0)
481 {
482 rb_lib_log("%s: ssl_conf_dh_param_ctx: %s", __func__, rb_ssl_strerror(ret));
483 }
484 }
485
486
487 const int *rb_ciphersuites = newcfg->suites;
488 size_t suites_count = 0;
489
490 if(cipherlist != NULL)
491 {
492 // The cipherlist is (const char *) -- we should not modify it
493 char *const cipherlist_dup = strdup(cipherlist);
494
495 if(cipherlist_dup != NULL)
496 {
497 char *cipher_str = cipherlist_dup;
498 char *cipher_idx;
499
500 do
501 {
502 // Arbitrary, but the same separator as OpenSSL uses
503 cipher_idx = strchr(cipher_str, ':');
504
505 // This could legitimately be NULL (last ciphersuite in the list)
506 if(cipher_idx != NULL)
507 *cipher_idx = '\0';
508
509 size_t cipher_len = strlen(cipher_str);
510 int cipher_idn = 0;
511
512 // All MbedTLS ciphersuite names begin with these 4 characters
513 if(cipher_len > 4 && strncmp(cipher_str, "TLS-", 4) == 0)
514 cipher_idn = mbedtls_ssl_get_ciphersuite_id(cipher_str);
515
516 // Prevent the same ciphersuite being added multiple times
517 for(size_t x = 0; cipher_idn != 0 && newcfg->suites[x] != 0; x++)
518 if(newcfg->suites[x] == cipher_idn)
519 cipher_idn = 0;
520
521 // Add the suite to the list
522 if(cipher_idn != 0)
523 newcfg->suites[suites_count++] = cipher_idn;
524
525 // Advance the string to the next entry
526 if(cipher_idx)
527 cipher_str = cipher_idx + 1;
528
529 } while(cipher_idx && suites_count < RB_MAX_CIPHERSUITES);
530
531 if(suites_count == 0)
532 rb_lib_log("%s: Ciphersuites provided, but could not parse any", __func__);
533
534 free(cipherlist_dup);
535 }
536 else
537 {
538 rb_lib_log("%s: strdup: %s", __func__, strerror(errno));
539 }
540 }
541 else
542 {
543 rb_lib_log("%s: No ciphersuite list provided", __func__);
544 }
545
546 if(suites_count == 0)
547 {
548 rb_lib_log("%s: Using default ciphersuites", __func__);
549
550 rb_ciphersuites = rb_mbedtls_ciphersuites;
551 suites_count = (sizeof(rb_mbedtls_ciphersuites) / sizeof(rb_mbedtls_ciphersuites[0])) - 1;
552 }
553
554 mbedtls_ssl_conf_ciphersuites(&newcfg->server_cfg, rb_ciphersuites);
555 mbedtls_ssl_conf_ciphersuites(&newcfg->client_cfg, rb_ciphersuites);
556 rb_lib_log("%s: Configured %zu ciphersuites", __func__, suites_count);
557
558
559 rb_mbedtls_cfg_decref(rb_mbedtls_cfg);
560 rb_mbedtls_cfg = newcfg;
561
562 rb_lib_log("%s: TLS configuration successful", __func__);
563 return 1;
564 }
565
566 int
567 rb_init_prng(const char *const path, prng_seed_t seed_type)
568 {
569 rb_lib_log("%s: Skipping PRNG initialisation; not required by MbedTLS backend", __func__);
570 return 1;
571 }
572
573 int
574 rb_get_random(void *const buf, const size_t length)
575 {
576 int ret;
577
578 if((ret = mbedtls_ctr_drbg_random(&ctr_drbg_ctx, buf, length)) != 0)
579 {
580 rb_lib_log("%s: ctr_drbg_random: %s", __func__, rb_ssl_strerror(ret));
581 return 0;
582 }
583
584 return 1;
585 }
586
587 const char *
588 rb_get_ssl_strerror(rb_fde_t *const F)
589 {
590 const int err = (int) F->ssl_errno;
591 return rb_ssl_strerror(-err);
592 }
593
594 int
595 rb_get_ssl_certfp(rb_fde_t *const F, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method)
596 {
597 const mbedtls_x509_crt *const peer_cert = mbedtls_ssl_get_peer_cert(SSL_P(F));
598
599 if(peer_cert == NULL)
600 return 0;
601
602 return rb_make_certfp(peer_cert, certfp, method);
603 }
604
605 int
606 rb_get_ssl_certfp_file(const char *const filename, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method)
607 {
608 mbedtls_x509_crt cert;
609
610 mbedtls_x509_crt_init(&cert);
611
612 const int ret = mbedtls_x509_crt_parse_file(&cert, filename);
613
614 if(ret != 0)
615 return -1;
616
617 const int len = rb_make_certfp(&cert, certfp, method);
618
619 mbedtls_x509_crt_free(&cert);
620
621 return len;
622 }
623 void
624 rb_get_ssl_info(char *const buf, const size_t len)
625 {
626 char version_str[512];
627
628 mbedtls_version_get_string(version_str);
629
630 (void) snprintf(buf, len, "ARM mbedTLS: compiled (v%s), library (v%s)",
631 MBEDTLS_VERSION_STRING, version_str);
632 }
633
634 const char *
635 rb_ssl_get_cipher(rb_fde_t *const F)
636 {
637 if(F == NULL || F->ssl == NULL)
638 return NULL;
639
640 static char buf[512];
641
642 const char *const version = mbedtls_ssl_get_version(SSL_P(F));
643 const char *const cipher = mbedtls_ssl_get_ciphersuite(SSL_P(F));
644
645 (void) snprintf(buf, sizeof buf, "%s, %s", version, cipher);
646
647 return buf;
648 }
649
650 ssize_t
651 rb_ssl_read(rb_fde_t *const F, void *const buf, const size_t count)
652 {
653 lrb_assert(F != NULL);
654 lrb_assert(F->ssl != NULL);
655
656 const int ret = mbedtls_ssl_read(SSL_P(F), buf, count);
657
658 if(ret >= 0)
659 return (ssize_t) ret;
660
661 switch(ret)
662 {
663 case MBEDTLS_ERR_SSL_WANT_READ:
664 errno = EAGAIN;
665 return RB_RW_SSL_NEED_READ;
666 case MBEDTLS_ERR_SSL_WANT_WRITE:
667 errno = EAGAIN;
668 return RB_RW_SSL_NEED_WRITE;
669 default:
670 errno = EIO;
671 F->ssl_errno = (unsigned long) -ret;
672 return RB_RW_SSL_ERROR;
673 }
674 }
675
676 ssize_t
677 rb_ssl_write(rb_fde_t *const F, const void *const buf, const size_t count)
678 {
679 lrb_assert(F != NULL);
680 lrb_assert(F->ssl != NULL);
681
682 const int ret = mbedtls_ssl_write(SSL_P(F), buf, count);
683
684 if(ret >= 0)
685 return (ssize_t) ret;
686
687 switch(ret)
688 {
689 case MBEDTLS_ERR_SSL_WANT_READ:
690 errno = EAGAIN;
691 return RB_RW_SSL_NEED_READ;
692 case MBEDTLS_ERR_SSL_WANT_WRITE:
693 errno = EAGAIN;
694 return RB_RW_SSL_NEED_WRITE;
695 default:
696 errno = EIO;
697 F->ssl_errno = (unsigned long) -ret;
698 return RB_RW_SSL_ERROR;
699 }
700 }
701
702
703
704 /*
705 * Internal library-agnostic code
706 */
707
708 static void
709 rb_ssl_connect_realcb(rb_fde_t *const F, const int status, struct ssl_connect *const sconn)
710 {
711 lrb_assert(F != NULL);
712 lrb_assert(F->connect != NULL);
713
714 F->connect->callback = sconn->callback;
715 F->connect->data = sconn->data;
716
717 rb_connect_callback(F, status);
718 rb_free(sconn);
719 }
720
721 static void
722 rb_ssl_timeout_cb(rb_fde_t *const F, void *const data)
723 {
724 lrb_assert(F->accept != NULL);
725 lrb_assert(F->accept->callback != NULL);
726
727 F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data);
728 }
729
730 static void
731 rb_ssl_tryconn_timeout_cb(rb_fde_t *const F, void *const data)
732 {
733 rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data);
734 }
735
736 static void
737 rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data)
738 {
739 lrb_assert(F != NULL);
740
741 struct ssl_connect *const sconn = data;
742
743 if(status != RB_OK)
744 {
745 rb_ssl_connect_realcb(F, status, sconn);
746 return;
747 }
748
749 F->type |= RB_FD_SSL;
750
751 rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
752 rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT);
753 rb_ssl_connect_common(F, sconn);
754 }
755
756 static int
757 rb_sock_net_recv(void *const context_ptr, unsigned char *const buf, const size_t count)
758 {
759 const int fd = rb_get_fd((rb_fde_t *)context_ptr);
760
761 const int ret = (int) read(fd, buf, count);
762
763 if(ret < 0 && rb_ignore_errno(errno))
764 return MBEDTLS_ERR_SSL_WANT_READ;
765
766 return ret;
767 }
768
769 static int
770 rb_sock_net_xmit(void *const context_ptr, const unsigned char *const buf, const size_t count)
771 {
772 const int fd = rb_get_fd((rb_fde_t *)context_ptr);
773
774 const int ret = (int) write(fd, buf, count);
775
776 if(ret < 0 && rb_ignore_errno(errno))
777 return MBEDTLS_ERR_SSL_WANT_WRITE;
778
779 return ret;
780 }
781
782
783
784 /*
785 * External library-agnostic code
786 */
787
788 int
789 rb_supports_ssl(void)
790 {
791 return 1;
792 }
793
794 unsigned int
795 rb_ssl_handshake_count(rb_fde_t *const F)
796 {
797 return F->handshake_count;
798 }
799
800 void
801 rb_ssl_clear_handshake_count(rb_fde_t *const F)
802 {
803 F->handshake_count = 0;
804 }
805
806 void
807 rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout)
808 {
809 F->type |= RB_FD_SSL;
810
811 F->accept = rb_malloc(sizeof(struct acceptdata));
812 F->accept->callback = cb;
813 F->accept->data = data;
814 F->accept->addrlen = 0;
815 (void) memset(&F->accept->S, 0x00, sizeof F->accept->S);
816
817 rb_settimeout(F, timeout, rb_ssl_timeout_cb, NULL);
818 rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_IN);
819 rb_ssl_accept_common(F, NULL);
820 }
821
822 void
823 rb_ssl_accept_setup(rb_fde_t *const srv_F, rb_fde_t *const cli_F, struct sockaddr *const st, const int addrlen)
824 {
825 cli_F->type |= RB_FD_SSL;
826
827 cli_F->accept = rb_malloc(sizeof(struct acceptdata));
828 cli_F->accept->callback = srv_F->accept->callback;
829 cli_F->accept->data = srv_F->accept->data;
830 cli_F->accept->addrlen = (rb_socklen_t) addrlen;
831 (void) memset(&cli_F->accept->S, 0x00, sizeof cli_F->accept->S);
832 (void) memcpy(&cli_F->accept->S, st, (size_t) addrlen);
833
834 rb_settimeout(cli_F, 10, rb_ssl_timeout_cb, NULL);
835 rb_ssl_init_fd(cli_F, RB_FD_TLS_DIRECTION_IN);
836 rb_ssl_accept_common(cli_F, NULL);
837 }
838
839 int
840 rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept)
841 {
842 int result = rb_listen(F, backlog, defer_accept);
843
844 F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL;
845
846 return result;
847 }
848
849 void
850 rb_connect_tcp_ssl(rb_fde_t *const F, struct sockaddr *const dest, struct sockaddr *const clocal,
851 CNCB *const callback, void *const data, const int timeout)
852 {
853 if(F == NULL)
854 return;
855
856 struct ssl_connect *const sconn = rb_malloc(sizeof *sconn);
857 sconn->data = data;
858 sconn->callback = callback;
859 sconn->timeout = timeout;
860
861 rb_connect_tcp(F, dest, clocal, rb_ssl_tryconn, sconn, timeout);
862 }
863
864 void
865 rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data, const int timeout)
866 {
867 if(F == NULL)
868 return;
869
870 struct ssl_connect *const sconn = rb_malloc(sizeof *sconn);
871 sconn->data = data;
872 sconn->callback = callback;
873 sconn->timeout = timeout;
874
875 F->connect = rb_malloc(sizeof(struct conndata));
876 F->connect->callback = callback;
877 F->connect->data = data;
878
879 F->type |= RB_FD_SSL;
880
881 rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
882 rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT);
883 rb_ssl_connect_common(F, sconn);
884 }
885
886 #endif /* HAVE_MBEDTLS */