1 /* 2 * ustream-ssl - library for SSL over ustream 3 * 4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org> 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/random.h> 21 #include <fcntl.h> 22 #include <unistd.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "ustream-ssl.h" 27 #include "ustream-internal.h" 28 #include <psa/crypto.h> 29 #include <mbedtls/debug.h> 30 31 #if (MBEDTLS_VERSION_NUMBER >= 0x03000000) 32 33 static void ssl_conf_min_version_tls12(mbedtls_ssl_config *conf) 34 { 35 mbedtls_ssl_conf_min_tls_version(conf, MBEDTLS_SSL_VERSION_TLS1_2); 36 } 37 38 static void ssl_conf_max_version_tls12(mbedtls_ssl_config *conf) 39 { 40 mbedtls_ssl_conf_max_tls_version(conf, MBEDTLS_SSL_VERSION_TLS1_2); 41 } 42 43 #else 44 45 static void ssl_conf_min_version_tls12(mbedtls_ssl_config *conf) 46 { 47 mbedtls_ssl_conf_min_version(conf, MBEDTLS_SSL_MAJOR_VERSION_3, 48 MBEDTLS_SSL_MINOR_VERSION_3); 49 } 50 51 static void ssl_conf_max_version_tls12(mbedtls_ssl_config *conf) 52 { 53 mbedtls_ssl_conf_max_version(conf, MBEDTLS_SSL_MAJOR_VERSION_3, 54 MBEDTLS_SSL_MINOR_VERSION_3); 55 } 56 57 #endif 58 59 static void debug_cb(void *ctx_p, int level, 60 const char *file, int line, 61 const char *str) 62 { 63 struct ustream_ssl_ctx *ctx = ctx_p; 64 const char *fstr; 65 char buf[512]; 66 int len; 67 68 if (!ctx->debug_cb) 69 return; 70 71 while ((fstr = strstr(file + 1, "library/")) != NULL) 72 file = fstr; 73 74 len = snprintf(buf, sizeof(buf), "%s:%04d: %s", file, line, str); 75 if (len >= (int)sizeof(buf)) 76 len = (int)sizeof(buf) - 1; 77 if (buf[len - 1] == '\n') 78 buf[len - 1] = 0; 79 ctx->debug_cb(ctx->debug_cb_priv, level, buf); 80 } 81 82 static int s_ustream_read(void *ctx, unsigned char *buf, size_t len) 83 { 84 struct ustream *s = ctx; 85 char *sbuf; 86 int slen; 87 88 if (s->eof) 89 return 0; 90 91 sbuf = ustream_get_read_buf(s, &slen); 92 if ((size_t) slen > len) 93 slen = len; 94 95 if (!slen) 96 return MBEDTLS_ERR_SSL_WANT_READ; 97 98 memcpy(buf, sbuf, slen); 99 ustream_consume(s, slen); 100 101 return slen; 102 } 103 104 static int s_ustream_write(void *ctx, const unsigned char *buf, size_t len) 105 { 106 struct ustream *s = ctx; 107 int ret; 108 109 ret = ustream_write(s, (const char *) buf, len, false); 110 if (ret < 0 || s->write_error) 111 return MBEDTLS_ERR_NET_SEND_FAILED; 112 113 return ret; 114 } 115 116 static int s_fd_read(void *ctx, unsigned char *buf, size_t len) 117 { 118 struct uloop_fd *ufd = ctx; 119 mbedtls_net_context net = { 120 .fd = ufd->fd 121 }; 122 123 return mbedtls_net_recv(&net, buf, len); 124 } 125 126 static int s_fd_write(void *ctx, const unsigned char *buf, size_t len) 127 { 128 struct uloop_fd *ufd = ctx; 129 mbedtls_net_context net = { 130 .fd = ufd->fd 131 }; 132 133 return mbedtls_net_send(&net, buf, len); 134 } 135 136 __hidden void ustream_set_io(struct ustream_ssl *us) 137 { 138 if (us->conn) 139 mbedtls_ssl_set_bio(us->ssl, us->conn, s_ustream_write, s_ustream_read, NULL); 140 else 141 mbedtls_ssl_set_bio(us->ssl, &us->fd, s_fd_write, s_fd_read, NULL); 142 } 143 144 static int _random(void *ctx, unsigned char *out, size_t len) 145 { 146 #ifdef linux 147 if (getrandom(out, len, 0) != (ssize_t) len) 148 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; 149 #else 150 static FILE *f; 151 152 if (!f) 153 f = fopen("/dev/urandom", "r"); 154 if (!f || fread(out, len, 1, f) != 1) 155 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; 156 #endif 157 158 return 0; 159 } 160 161 #define AES_GCM_CIPHERS(v) \ 162 MBEDTLS_TLS_##v##_WITH_AES_128_GCM_SHA256, \ 163 MBEDTLS_TLS_##v##_WITH_AES_256_GCM_SHA384 164 165 #define AES_CBC_CIPHERS(v) \ 166 MBEDTLS_TLS_##v##_WITH_AES_128_CBC_SHA, \ 167 MBEDTLS_TLS_##v##_WITH_AES_256_CBC_SHA 168 169 #define AES_CIPHERS(v) \ 170 AES_GCM_CIPHERS(v), \ 171 AES_CBC_CIPHERS(v) 172 173 static const int default_ciphersuites_server[] = 174 { 175 #ifdef MBEDTLS_SSL_PROTO_TLS1_3 176 MBEDTLS_TLS1_3_CHACHA20_POLY1305_SHA256, 177 MBEDTLS_TLS1_3_AES_256_GCM_SHA384, 178 MBEDTLS_TLS1_3_AES_128_GCM_SHA256, 179 MBEDTLS_TLS1_3_AES_128_CCM_SHA256, 180 MBEDTLS_TLS1_3_AES_128_CCM_8_SHA256, 181 #endif 182 183 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 184 AES_GCM_CIPHERS(ECDHE_ECDSA), 185 MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 186 AES_GCM_CIPHERS(ECDHE_RSA), 187 AES_CBC_CIPHERS(ECDHE_RSA), 188 AES_CIPHERS(RSA), 189 0 190 }; 191 192 static const int default_ciphersuites_client[] = 193 { 194 #ifdef MBEDTLS_SSL_PROTO_TLS1_3 195 MBEDTLS_TLS1_3_CHACHA20_POLY1305_SHA256, 196 MBEDTLS_TLS1_3_AES_256_GCM_SHA384, 197 MBEDTLS_TLS1_3_AES_128_GCM_SHA256, 198 MBEDTLS_TLS1_3_AES_128_CCM_SHA256, 199 MBEDTLS_TLS1_3_AES_128_CCM_8_SHA256, 200 #endif 201 202 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 203 AES_GCM_CIPHERS(ECDHE_ECDSA), 204 MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 205 AES_GCM_CIPHERS(ECDHE_RSA), 206 MBEDTLS_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 207 AES_GCM_CIPHERS(DHE_RSA), 208 AES_CBC_CIPHERS(ECDHE_ECDSA), 209 AES_CBC_CIPHERS(ECDHE_RSA), 210 AES_CBC_CIPHERS(DHE_RSA), 211 /* Removed in Mbed TLS 3.0.0 */ 212 #ifdef MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 213 MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, 214 #endif 215 AES_CIPHERS(RSA), 216 /* Removed in Mbed TLS 3.0.0 */ 217 #ifdef MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA 218 MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA, 219 #endif 220 0 221 }; 222 223 224 __hidden struct ustream_ssl_ctx * 225 __ustream_ssl_context_new(bool server) 226 { 227 struct ustream_ssl_ctx *ctx; 228 mbedtls_ssl_config *conf; 229 int ep; 230 231 #ifdef MBEDTLS_PSA_CRYPTO_C 232 static bool psa_init; 233 234 if (!psa_init && !psa_crypto_init()) 235 psa_init = true; 236 #endif 237 238 ctx = calloc(1, sizeof(*ctx)); 239 if (!ctx) 240 return NULL; 241 242 ctx->server = server; 243 mbedtls_pk_init(&ctx->key); 244 mbedtls_x509_crt_init(&ctx->cert); 245 mbedtls_x509_crt_init(&ctx->ca_cert); 246 247 #if defined(MBEDTLS_SSL_CACHE_C) 248 mbedtls_ssl_cache_init(&ctx->cache); 249 mbedtls_ssl_cache_set_timeout(&ctx->cache, 30 * 60); 250 mbedtls_ssl_cache_set_max_entries(&ctx->cache, 5); 251 #endif 252 253 conf = &ctx->conf; 254 mbedtls_ssl_config_init(conf); 255 256 ep = server ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT; 257 258 mbedtls_ssl_config_defaults(conf, ep, MBEDTLS_SSL_TRANSPORT_STREAM, 259 MBEDTLS_SSL_PRESET_DEFAULT); 260 mbedtls_ssl_conf_rng(conf, _random, NULL); 261 262 if (server) { 263 mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE); 264 mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_server); 265 ssl_conf_min_version_tls12(conf); 266 } else { 267 mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_OPTIONAL); 268 mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_client); 269 } 270 271 #if defined(MBEDTLS_SSL_CACHE_C) 272 mbedtls_ssl_conf_session_cache(conf, &ctx->cache, 273 mbedtls_ssl_cache_get, 274 mbedtls_ssl_cache_set); 275 #endif 276 return ctx; 277 } 278 279 static void ustream_ssl_update_own_cert(struct ustream_ssl_ctx *ctx) 280 { 281 if (!ctx->cert.version) 282 return; 283 284 if (mbedtls_pk_get_type(&ctx->key) == MBEDTLS_PK_NONE) 285 return; 286 287 mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->key); 288 } 289 290 __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file) 291 { 292 int ret; 293 294 ret = mbedtls_x509_crt_parse_file(&ctx->ca_cert, file); 295 if (ret) 296 return -1; 297 298 mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->ca_cert, NULL); 299 mbedtls_ssl_conf_authmode(&ctx->conf, MBEDTLS_SSL_VERIFY_OPTIONAL); 300 return 0; 301 } 302 303 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file) 304 { 305 int ret; 306 307 ret = mbedtls_x509_crt_parse_file(&ctx->cert, file); 308 if (ret) 309 return -1; 310 311 ustream_ssl_update_own_cert(ctx); 312 return 0; 313 } 314 315 __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file) 316 { 317 int ret; 318 319 #if (MBEDTLS_VERSION_NUMBER >= 0x03000000) 320 ret = mbedtls_pk_parse_keyfile(&ctx->key, file, NULL, _random, NULL); 321 #else 322 ret = mbedtls_pk_parse_keyfile(&ctx->key, file, NULL); 323 #endif 324 if (ret) 325 return -1; 326 327 ustream_ssl_update_own_cert(ctx); 328 return 0; 329 } 330 331 __hidden int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx *ctx, const char *ciphers) 332 { 333 int *ciphersuites = NULL, *tmp, id; 334 char *cipherstr, *p, *last, c; 335 size_t len = 0; 336 337 if (ciphers == NULL) 338 return -1; 339 340 cipherstr = strdup(ciphers); 341 342 if (cipherstr == NULL) 343 return -1; 344 345 for (p = cipherstr, last = p;; p++) { 346 if (*p == ':' || *p == 0) { 347 c = *p; 348 *p = 0; 349 350 id = mbedtls_ssl_get_ciphersuite_id(last); 351 352 if (id != 0) { 353 tmp = realloc(ciphersuites, (len + 2) * sizeof(int)); 354 355 if (tmp == NULL) { 356 free(ciphersuites); 357 free(cipherstr); 358 359 return -1; 360 } 361 362 ciphersuites = tmp; 363 ciphersuites[len++] = id; 364 ciphersuites[len] = 0; 365 } 366 367 if (c == 0) 368 break; 369 370 last = p + 1; 371 } 372 373 /* 374 * mbedTLS expects cipher names with dashes while many sources elsewhere 375 * like the Firefox wiki or Wireshark specify ciphers with underscores, 376 * so simply convert all underscores to dashes to accept both notations. 377 */ 378 else if (*p == '_') { 379 *p = '-'; 380 } 381 } 382 383 free(cipherstr); 384 385 if (len == 0) 386 return -1; 387 388 mbedtls_ssl_conf_ciphersuites(&ctx->conf, ciphersuites); 389 free(ctx->ciphersuites); 390 391 ctx->ciphersuites = ciphersuites; 392 393 return 0; 394 } 395 396 __hidden int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx *ctx, bool require) 397 { 398 int mode = MBEDTLS_SSL_VERIFY_OPTIONAL; 399 400 if (!require) 401 mode = MBEDTLS_SSL_VERIFY_NONE; 402 403 /* force TLS 1.2 when not requiring validation for now */ 404 if (!require && !ctx->server) 405 ssl_conf_max_version_tls12(&ctx->conf); 406 mbedtls_ssl_conf_authmode(&ctx->conf, mode); 407 408 return 0; 409 } 410 411 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx) 412 { 413 free(ctx->session_data); 414 #if defined(MBEDTLS_SSL_CACHE_C) 415 mbedtls_ssl_cache_free(&ctx->cache); 416 #endif 417 mbedtls_pk_free(&ctx->key); 418 mbedtls_x509_crt_free(&ctx->ca_cert); 419 mbedtls_x509_crt_free(&ctx->cert); 420 mbedtls_ssl_config_free(&ctx->conf); 421 free(ctx->ciphersuites); 422 free(ctx); 423 } 424 425 static void ustream_ssl_error(struct ustream_ssl *us, int ret) 426 { 427 us->error = ret; 428 uloop_timeout_set(&us->error_timer, 0); 429 } 430 431 #ifdef MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET 432 static void 433 __ustream_ssl_save_session(struct ustream_ssl *us) 434 { 435 struct ustream_ssl_ctx *ctx = us->ctx; 436 mbedtls_ssl_session sess; 437 438 if (ctx->server) 439 return; 440 441 free(ctx->session_data); 442 ctx->session_data = NULL; 443 444 mbedtls_ssl_session_init(&sess); 445 if (mbedtls_ssl_get_session(us->ssl, &sess) != 0) 446 return; 447 448 mbedtls_ssl_session_save(&sess, NULL, 0, &ctx->session_data_len); 449 ctx->session_data = malloc(ctx->session_data_len); 450 if (!ctx->session_data || 451 mbedtls_ssl_session_save(&sess, ctx->session_data, ctx->session_data_len, 452 &ctx->session_data_len)) 453 ctx->session_data_len = 0; 454 mbedtls_ssl_session_free(&sess); 455 } 456 #endif 457 458 static int ssl_check_return(struct ustream_ssl *us, int ret) 459 { 460 switch(ret) { 461 case MBEDTLS_ERR_SSL_WANT_READ: 462 case MBEDTLS_ERR_SSL_WANT_WRITE: 463 return U_SSL_PENDING; 464 #ifdef MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET 465 case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET: 466 __ustream_ssl_save_session(us); 467 return U_SSL_RETRY; 468 #endif 469 #ifdef MBEDTLS_ECP_RESTARTABLE 470 case MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS: 471 return U_SSL_RETRY; 472 #endif 473 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: 474 case MBEDTLS_ERR_NET_CONN_RESET: 475 return 0; 476 default: 477 ustream_ssl_error(us, ret); 478 return U_SSL_ERROR; 479 } 480 } 481 482 static void ustream_ssl_verify_cert(struct ustream_ssl *us) 483 { 484 void *ssl = us->ssl; 485 const char *msg = NULL; 486 bool cn_mismatch; 487 int r; 488 489 r = mbedtls_ssl_get_verify_result(ssl); 490 cn_mismatch = r & MBEDTLS_X509_BADCERT_CN_MISMATCH; 491 r &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH; 492 493 if (r & MBEDTLS_X509_BADCERT_EXPIRED) 494 msg = "certificate has expired"; 495 else if (r & MBEDTLS_X509_BADCERT_REVOKED) 496 msg = "certificate has been revoked"; 497 else if (r & MBEDTLS_X509_BADCERT_NOT_TRUSTED) 498 msg = "certificate is self-signed or not signed by a trusted CA"; 499 else 500 msg = "unknown error"; 501 502 if (r) { 503 if (us->notify_verify_error) 504 us->notify_verify_error(us, r, msg); 505 return; 506 } 507 508 /* mbedtls_ssl_get_verify_result() also returns 0 when no peer 509 * certificate was presented or verification was skipped, so require a 510 * peer certificate like the openssl backend does. 511 */ 512 if (mbedtls_ssl_get_peer_cert(ssl)) 513 us->valid_cert = true; 514 515 if (!cn_mismatch) 516 us->valid_cn = true; 517 } 518 519 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us) 520 { 521 void *ssl = us->ssl; 522 int r; 523 524 do { 525 r = mbedtls_ssl_handshake(ssl); 526 if (r == 0) { 527 ustream_ssl_verify_cert(us); 528 return U_SSL_OK; 529 } 530 531 r = ssl_check_return(us, r); 532 } while (r == U_SSL_RETRY); 533 534 return r; 535 } 536 537 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len) 538 { 539 void *ssl = us->ssl; 540 int done = 0, ret = 0; 541 542 while (done != len) { 543 ret = mbedtls_ssl_write(ssl, (const unsigned char *) buf + done, len - done); 544 if (ret < 0) { 545 ret = ssl_check_return(us, ret); 546 if (ret == U_SSL_RETRY) 547 continue; 548 549 if (ret == U_SSL_PENDING) 550 return done; 551 552 return -1; 553 } 554 555 done += ret; 556 } 557 558 return done; 559 } 560 561 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len) 562 { 563 int ret; 564 565 do { 566 ret = mbedtls_ssl_read(us->ssl, (unsigned char *) buf, len); 567 if (ret >= 0) 568 return ret; 569 570 ret = ssl_check_return(us, ret); 571 } while (ret == U_SSL_RETRY); 572 573 return ret; 574 } 575 576 __hidden void __ustream_ssl_set_debug(struct ustream_ssl_ctx *ctx, int level, 577 ustream_ssl_debug_cb cb, void *cb_priv) 578 { 579 ctx->debug_cb = cb; 580 ctx->debug_cb_priv = cb_priv; 581 mbedtls_ssl_conf_dbg(&ctx->conf, debug_cb, ctx); 582 #ifdef MBEDTLS_DEBUG_C 583 mbedtls_debug_set_threshold(level); 584 #endif 585 } 586 587 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx) 588 { 589 mbedtls_ssl_context *ssl; 590 mbedtls_ssl_session sess; 591 592 ssl = calloc(1, sizeof(*ssl)); 593 if (!ssl) 594 return NULL; 595 596 mbedtls_ssl_init(ssl); 597 598 if (mbedtls_ssl_setup(ssl, &ctx->conf)) { 599 free(ssl); 600 return NULL; 601 } 602 603 if (!ctx->session_data_len) 604 return ssl; 605 606 mbedtls_ssl_session_init(&sess); 607 if (mbedtls_ssl_session_load(&sess, ctx->session_data, ctx->session_data_len) == 0) 608 mbedtls_ssl_set_session(ssl, &sess); 609 610 return ssl; 611 } 612 613 __hidden void __ustream_ssl_session_free(struct ustream_ssl *us) 614 { 615 mbedtls_ssl_close_notify(us->ssl); 616 mbedtls_ssl_free(us->ssl); 617 free(us->ssl); 618 } 619
This page was automatically generated by LXR 0.3.1. • OpenWrt