1 /* 2 * mod_mbedtls - mbedTLS support for lighttpd 3 * 4 * Copyright(c) 2020 Glenn Strauss gstrauss()gluelogic.com All rights reserved 5 * License: BSD 3-clause (same as lighttpd) 6 */ 7 /* 8 * reference: 9 * https://tls.mbed.org/high-level-design 10 * https://tls.mbed.org/tech-updates/blog/mbedtls-2.0-defaults-best-practices 11 * mbedTLS header files (mbedtls/ssl.h and others) are extremely well-documented 12 * https://tls.mbed.org/api/ (generated from mbedTLS headers and code) 13 * 14 * mbedTLS limitations: 15 * - mbedTLS does not currently support OCSP 16 * https://tls.mbed.org/discussions/feature-request/ocsp-stapling 17 * TLS/DTLS: OCSP Stapling support #880 18 * https://github.com/ARMmbed/mbedtls/issues/880 19 * Add support for writing OCSP requests and parsing OCSP responses #1197 20 * https://github.com/ARMmbed/mbedtls/issues/1197 21 * 22 * future possible enhancements to lighttpd mod_mbedtls: 23 * - session cache (though session tickets are implemented) 24 * sample code in mbedtls:programs/ssl/ssl_server2.c 25 * (and do not enable unless server.feature-flags ssl.session-cache enabled) 26 * 27 * Note: If session tickets are -not- disabled with 28 * ssl.openssl.ssl-conf-cmd = ("Options" => "-SessionTicket") 29 * mbedtls rotates the session ticket key according to 2x timeout set with 30 * mbedtls_ssl_ticket_setup() (currently 43200 s, so 24 hour ticket lifetime) 31 * This is fine for use with a single lighttpd instance, but with multiple 32 * lighttpd workers, no coordinated STEK (server ticket encryption key) 33 * rotation occurs unless ssl.stek-file is defined and maintained (preferred), 34 * or if some external job restarts lighttpd. Restarting lighttpd generates a 35 * new key that is shared by lighttpd workers for the lifetime of the new key. 36 * If the rotation period expires and lighttpd has not been restarted, and if 37 * ssl.stek-file is not in use, then lighttpd workers will generate new 38 * independent keys, making session tickets less effective for session 39 * resumption, since clients have a lower chance for future connections to 40 * reach the same lighttpd worker. However, things will still work, and a new 41 * session will be created if session resumption fails. Admins should plan to 42 * restart lighttpd at least every 12 hours if session tickets are enabled and 43 * multiple lighttpd workers are configured. Since that is likely disruptive, 44 * if multiple lighttpd workers are configured, ssl.stek-file should be 45 * defined and the file maintained externally. 46 */ 47 #include "first.h" 48 49 #include <sys/types.h> 50 #include <sys/stat.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <stdarg.h> 54 #include <stdlib.h> 55 #include <stdio.h> /* vsnprintf() */ 56 #include <string.h> 57 58 #include <mbedtls/version.h> 59 /*(compatibility while waiting for future mbedtls 3.x interfaces)*/ 60 #if MBEDTLS_VERSION_NUMBER < 0x03020000 /* mbedtls 3.02.0 */ 61 #ifndef MBEDTLS_ALLOW_PRIVATE_ACCESS 62 #define MBEDTLS_ALLOW_PRIVATE_ACCESS 63 #endif 64 #endif 65 #include <mbedtls/ctr_drbg.h> 66 #include <mbedtls/debug.h> 67 #include <mbedtls/dhm.h> 68 #include <mbedtls/error.h> 69 #include <mbedtls/entropy.h> 70 #include <mbedtls/oid.h> 71 #include <mbedtls/pem.h> 72 #include <mbedtls/ssl.h> 73 #if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.00.0 */ 74 #include <mbedtls/ssl_internal.h> /* struct mbedtls_ssl_transform */ 75 #endif 76 #include <mbedtls/x509.h> 77 #include <mbedtls/x509_crt.h> 78 #include <mbedtls/platform_util.h> /* mbedtls_platform_zeroize() */ 79 80 #if MBEDTLS_VERSION_NUMBER >= 0x02040000 /* mbedtls 2.04.0 */ 81 #include <mbedtls/net_sockets.h> 82 #else 83 #include <mbedtls/net.h> 84 #endif 85 86 #if defined(MBEDTLS_SSL_TICKET_C) 87 #include <mbedtls/ssl_ticket.h> 88 #endif 89 90 #ifndef MBEDTLS_X509_CRT_PARSE_C 91 #error "lighttpd requires that mbedtls be built with MBEDTLS_X509_CRT_PARSE_C" 92 #endif 93 94 #ifndef MBEDTLS_PRIVATE 95 #define MBEDTLS_PRIVATE(x) x 96 #endif 97 98 #include "base.h" 99 #include "ck.h" 100 #include "fdevent.h" 101 #include "http_header.h" 102 #include "http_kv.h" 103 #include "log.h" 104 #include "plugin.h" 105 106 typedef struct { 107 /* SNI per host: with COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */ 108 mbedtls_pk_context ssl_pemfile_pkey;/* parsed private key structure */ 109 mbedtls_x509_crt ssl_pemfile_x509; /* parsed public key structure */ 110 const buffer *ssl_pemfile; 111 const buffer *ssl_privkey; 112 int8_t need_chain; 113 } plugin_cert; 114 115 typedef struct { 116 mbedtls_ssl_config *ssl_ctx; /* context shared between mbedtls_ssl_CONTEXT structures */ 117 int *ciphersuites; 118 void *curves; 119 } plugin_ssl_ctx; 120 121 typedef struct { 122 mbedtls_ssl_config *ssl_ctx; /* output from network_init_ssl() */ 123 int *ciphersuites; /* output from network_init_ssl() */ 124 void *curves; /* output from network_init_ssl() */ 125 126 /*(used only during startup; not patched)*/ 127 unsigned char ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */ 128 unsigned char ssl_honor_cipher_order; /* determine SSL cipher in server-preferred order, not client-order */ 129 unsigned char ssl_empty_fragments; 130 unsigned char ssl_use_sslv2; 131 unsigned char ssl_use_sslv3; 132 const buffer *ssl_cipher_list; 133 const buffer *ssl_dh_file; 134 const buffer *ssl_ec_curve; 135 const buffer *ssl_acme_tls_1; 136 array *ssl_conf_cmd; 137 138 /*(copied from plugin_data for socket ssl_ctx config)*/ 139 plugin_cert *pc; 140 mbedtls_pk_context *ssl_pemfile_pkey; /* parsed private key structure */ 141 mbedtls_x509_crt *ssl_pemfile_x509; /* parsed public key structure */ 142 mbedtls_x509_crt *ssl_ca_file; 143 const buffer *ssl_pemfile; 144 const buffer *ssl_privkey; 145 unsigned char ssl_session_ticket; 146 unsigned char ssl_verifyclient; 147 unsigned char ssl_verifyclient_enforce; 148 unsigned char ssl_verifyclient_depth; 149 } plugin_config_socket; /*(used at startup during configuration)*/ 150 151 typedef struct { 152 /* SNI per host: w/ COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */ 153 plugin_cert *pc; 154 mbedtls_pk_context *ssl_pemfile_pkey; /* parsed private key structure */ 155 mbedtls_x509_crt *ssl_pemfile_x509; /* parsed public key structure */ 156 const buffer *ssl_pemfile; 157 mbedtls_x509_crt *ssl_ca_file; 158 mbedtls_x509_crt *ssl_ca_dn_file; 159 mbedtls_x509_crl *ssl_ca_crl_file; 160 161 unsigned char ssl_verifyclient; 162 unsigned char ssl_verifyclient_enforce; 163 unsigned char ssl_verifyclient_depth; 164 unsigned char ssl_verifyclient_export_cert; 165 unsigned char ssl_read_ahead; 166 unsigned char ssl_log_noise; 167 unsigned char ssl_disable_client_renegotiation; 168 const buffer *ssl_verifyclient_username; 169 const buffer *ssl_acme_tls_1; 170 } plugin_config; 171 172 typedef struct { 173 PLUGIN_DATA; 174 plugin_ssl_ctx *ssl_ctxs; 175 plugin_config defaults; 176 server *srv; 177 /* NIST counter-mode deterministic random byte generator */ 178 mbedtls_ctr_drbg_context ctr_drbg; 179 /* entropy collection and state management */ 180 mbedtls_entropy_context entropy; 181 #if defined(MBEDTLS_SSL_SESSION_TICKETS) 182 mbedtls_ssl_ticket_context ticket_ctx; 183 const char *ssl_stek_file; 184 #endif 185 } plugin_data; 186 187 static int ssl_is_init; 188 /* need assigned p->id for deep access of module handler_ctx for connection 189 * i.e. handler_ctx *hctx = con->plugin_ctx[plugin_data_singleton->id]; */ 190 static plugin_data *plugin_data_singleton; 191 #ifdef MBEDTLS_SSL_OUT_CONTENT_LEN 192 #define LOCAL_SEND_BUFSIZE MBEDTLS_SSL_OUT_CONTENT_LEN 193 #else 194 #define LOCAL_SEND_BUFSIZE MBEDTLS_SSL_MAX_CONTENT_LEN 195 #endif 196 static char *local_send_buffer; 197 198 typedef struct { 199 mbedtls_ssl_context ssl; /* mbedtls request/connection context */ 200 request_st *r; 201 connection *con; 202 int8_t close_notify; 203 unsigned short alpn; 204 int handshake_done; 205 size_t pending_write; 206 plugin_config conf; 207 buffer *tmp_buf; 208 log_error_st *errh; 209 mbedtls_ssl_config *ssl_ctx; 210 mbedtls_pk_context *acme_tls_1_pkey; 211 mbedtls_x509_crt *acme_tls_1_x509; 212 } handler_ctx; 213 214 215 static handler_ctx * 216 handler_ctx_init (void) 217 { 218 handler_ctx *hctx = calloc(1, sizeof(*hctx)); 219 force_assert(hctx); 220 return hctx; 221 } 222 223 224 static void 225 handler_ctx_free (handler_ctx *hctx) 226 { 227 mbedtls_ssl_free(&hctx->ssl); 228 if (hctx->acme_tls_1_pkey) { 229 mbedtls_pk_free(hctx->acme_tls_1_pkey); 230 free(hctx->acme_tls_1_pkey); 231 } 232 if (hctx->acme_tls_1_x509) { 233 mbedtls_x509_crt_free(hctx->acme_tls_1_x509); 234 free(hctx->acme_tls_1_x509); 235 } 236 free(hctx); 237 } 238 239 240 #ifdef MBEDTLS_ERROR_C 241 __attribute_cold__ 242 static void elog(log_error_st * const errh, 243 const char * const file, const int line, 244 const int rc, const char * const msg) 245 { 246 /* error logging convenience function that decodes mbedtls result codes */ 247 char buf[256]; 248 mbedtls_strerror(rc, buf, sizeof(buf)); 249 log_error(errh, file, line, "MTLS: %s: %s (-0x%04x)", msg, buf, -rc); 250 } 251 #else 252 #define elog(errh, file, line, rc, msg) \ 253 log_error((errh), (file), (line), "MTLS: %s: (-0x%04x)", (msg), -(rc)) 254 #endif 255 256 257 __attribute_cold__ 258 __attribute_format__((__printf__, 5, 6)) 259 static void elogf(log_error_st * const errh, 260 const char * const file, const int line, 261 const int rc, const char * const fmt, ...) 262 { 263 char msg[1024]; 264 va_list ap; 265 va_start(ap, fmt); 266 vsnprintf(msg, sizeof(msg), fmt, ap); 267 va_end(ap); 268 elog(errh, file, line, rc, msg); 269 } 270 271 272 #ifdef MBEDTLS_SSL_SESSION_TICKETS 273 274 #define TLSEXT_KEYNAME_LENGTH 16 275 #define TLSEXT_TICK_KEY_LENGTH 32 276 277 /* construct our own session ticket encryption key structure 278 * to store keys that are not yet active 279 * (mirror from mod_openssl, even though not all bits are used here) */ 280 typedef struct tlsext_ticket_key_st { 281 unix_time64_t active_ts; /* tickets not issued w/ key until activation ts*/ 282 unix_time64_t expire_ts; /* key not valid after expiration timestamp */ 283 unsigned char tick_key_name[TLSEXT_KEYNAME_LENGTH]; 284 unsigned char tick_hmac_key[TLSEXT_TICK_KEY_LENGTH]; 285 unsigned char tick_aes_key[TLSEXT_TICK_KEY_LENGTH]; 286 } tlsext_ticket_key_t; 287 288 static tlsext_ticket_key_t session_ticket_keys[1]; /* temp store until active */ 289 static unix_time64_t stek_rotate_ts; 290 291 292 static int 293 mod_mbedtls_session_ticket_key_file (const char *fn) 294 { 295 /* session ticket encryption key (STEK) 296 * 297 * STEK file should be stored in non-persistent storage, 298 * e.g. /dev/shm/lighttpd/stek-file (in memory) 299 * with appropriate permissions set to keep stek-file from being 300 * read by other users. Where possible, systems should also be 301 * configured without swap. 302 * 303 * admin should schedule an independent job to periodically 304 * generate new STEK up to 3 times during key lifetime 305 * (lighttpd stores up to 3 keys) 306 * 307 * format of binary file is: 308 * 4-byte - format version (always 0; for use if format changes) 309 * 4-byte - activation timestamp 310 * 4-byte - expiration timestamp 311 * 16-byte - session ticket key name 312 * 32-byte - session ticket HMAC encrpytion key 313 * 32-byte - session ticket AES encrpytion key 314 * 315 * STEK file can be created with a command such as: 316 * dd if=/dev/random bs=1 count=80 status=none | \ 317 * perl -e 'print pack("iii",0,time()+300,time()+86400),<>' \ 318 * > STEK-file.$$ && mv STEK-file.$$ STEK-file 319 * 320 * The above delays activation time by 5 mins (+300 sec) to allow file to 321 * be propagated to other machines. (admin must handle this independently) 322 * If STEK generation is performed immediately prior to starting lighttpd, 323 * admin should activate keys immediately (without +300). 324 */ 325 int buf[23]; /* 92 bytes */ 326 int rc = 0; /*(will retry on next check interval upon any error)*/ 327 if (0 != fdevent_load_file_bytes((char *)buf,(off_t)sizeof(buf),0,fn,NULL)) 328 return rc; 329 if (buf[0] == 0) { /*(format version 0)*/ 330 session_ticket_keys[0].active_ts = TIME64_CAST(buf[1]); 331 session_ticket_keys[0].expire_ts = TIME64_CAST(buf[2]); 332 #ifndef __COVERITY__ 333 memcpy(&session_ticket_keys[0].tick_key_name, buf+3, 80); 334 #else 335 memcpy(&session_ticket_keys[0].tick_key_name, 336 buf+3, TLSEXT_KEYNAME_LENGTH); 337 memcpy(&session_ticket_keys[0].tick_hmac_key, 338 buf+7, TLSEXT_TICK_KEY_LENGTH); 339 memcpy(&session_ticket_keys[0].tick_aes_key, 340 buf+15, TLSEXT_TICK_KEY_LENGTH); 341 #endif 342 rc = 1; 343 } 344 345 mbedtls_platform_zeroize(buf, sizeof(buf)); 346 return rc; 347 } 348 349 350 static void 351 mod_mbedtls_session_ticket_key_check (plugin_data *p, const unix_time64_t cur_ts) 352 { 353 if (NULL == p->ssl_stek_file) return; 354 355 struct stat st; 356 if (0 == stat(p->ssl_stek_file, &st) 357 && TIME64_CAST(st.st_mtime) > stek_rotate_ts 358 && mod_mbedtls_session_ticket_key_file(p->ssl_stek_file)) { 359 stek_rotate_ts = cur_ts; 360 } 361 362 tlsext_ticket_key_t *stek = session_ticket_keys; 363 if (stek->active_ts != 0 && stek->active_ts - 63 <= cur_ts) { 364 #if MBEDTLS_VERSION_NUMBER >= 0x03020000 /* mbedtls 3.02.0 */ 365 int rc = mbedtls_ssl_ticket_rotate(&p->ticket_ctx, 366 stek->tick_key_name, sizeof(stek->tick_key_name), 367 stek->tick_aes_key, sizeof(stek->tick_aes_key), 368 (uint32_t)(stek->expire_ts - stek->active_ts)); 369 if (0 != rc) 370 elog(p->srv->errh, __FILE__,__LINE__, rc, 371 "session ticket encryption key rotation failed"); 372 #else /*(mbedtls_allow_private_access at top of file for [3.0.0,3.2.0))*/ 373 /* expect to get newer ssl.stek-file prior to mbedtls detecting 374 * expiration and internally generating a new key. If not, then 375 * lifetime may be up to 2x specified lifetime until overwritten 376 * by mbedtls, but original key will be overwritten and discarded */ 377 mbedtls_ssl_ticket_context *ctx = &p->ticket_ctx; 378 ctx->ticket_lifetime = stek->expire_ts - stek->active_ts; 379 ctx->active = 1 - ctx->active; 380 mbedtls_ssl_ticket_key *key = ctx->keys + ctx->active; 381 /* set generation_time to cur_ts instead of stek->active_ts 382 * since ctx->active was updated */ 383 key->generation_time = cur_ts; 384 memcpy(key->name, stek->tick_key_name, sizeof(key->name)); 385 /* With GCM and CCM, same context can encrypt & decrypt */ 386 int rc = mbedtls_cipher_setkey(&key->ctx, stek->tick_aes_key, 387 mbedtls_cipher_get_key_bitlen(&key->ctx), 388 MBEDTLS_ENCRYPT); 389 if (0 != rc) { /* expire key immediately if error occurs */ 390 key->generation_time = cur_ts > (unix_time64_t)ctx->ticket_lifetime 391 ? cur_ts - ctx->ticket_lifetime - 1 392 : 0; 393 ctx->active = 1 - ctx->active; 394 } 395 #endif 396 mbedtls_platform_zeroize(stek, sizeof(tlsext_ticket_key_t)); 397 } 398 } 399 400 #endif /* MBEDTLS_SSL_SESSION_TICKETS */ 401 402 403 INIT_FUNC(mod_mbedtls_init) 404 { 405 plugin_data_singleton = (plugin_data *)calloc(1, sizeof(plugin_data)); 406 #if defined(MBEDTLS_SSL_SESSION_TICKETS) 407 mbedtls_ssl_ticket_init(&plugin_data_singleton->ticket_ctx); 408 #endif 409 return plugin_data_singleton; 410 } 411 412 413 static int mod_mbedtls_init_once_mbedtls (server *srv) 414 { 415 if (ssl_is_init) return 1; 416 ssl_is_init = 1; 417 418 plugin_data * const p = plugin_data_singleton; 419 mbedtls_ctr_drbg_init(&p->ctr_drbg); /* init empty NSIT random num gen */ 420 mbedtls_entropy_init(&p->entropy); /* init empty entropy collection struct 421 .. could add sources here too */ 422 423 int rc = /* init RNG */ 424 mbedtls_ctr_drbg_seed(&p->ctr_drbg, /* random number generator */ 425 mbedtls_entropy_func, /* default entropy func */ 426 &p->entropy, /* entropy context */ 427 NULL, 0); /* no personalization data */ 428 if (0 != rc) { 429 elog(srv->errh, __FILE__,__LINE__, rc, 430 "Init of random number generator failed"); 431 return 0; 432 } 433 434 local_send_buffer = malloc(LOCAL_SEND_BUFSIZE); 435 force_assert(NULL != local_send_buffer); 436 437 return 1; 438 } 439 440 441 static void mod_mbedtls_free_mbedtls (void) 442 { 443 if (!ssl_is_init) return; 444 445 #ifdef MBEDTLS_SSL_SESSION_TICKETS 446 mbedtls_platform_zeroize(session_ticket_keys, sizeof(session_ticket_keys)); 447 stek_rotate_ts = 0; 448 #endif 449 450 plugin_data * const p = plugin_data_singleton; 451 mbedtls_ctr_drbg_free(&p->ctr_drbg); 452 mbedtls_entropy_free(&p->entropy); 453 #if defined(MBEDTLS_SSL_SESSION_TICKETS) 454 mbedtls_ssl_ticket_free(&p->ticket_ctx); 455 #endif 456 457 free(local_send_buffer); 458 ssl_is_init = 0; 459 } 460 461 462 static void 463 mod_mbedtls_free_config (server *srv, plugin_data * const p) 464 { 465 if (NULL != p->ssl_ctxs) { 466 mbedtls_ssl_config * const ssl_ctx_global_scope = p->ssl_ctxs->ssl_ctx; 467 /* free ssl_ctx from $SERVER["socket"] (if not copy of global scope) */ 468 for (uint32_t i = 1; i < srv->config_context->used; ++i) { 469 plugin_ssl_ctx * const s = p->ssl_ctxs + i; 470 if (s->ssl_ctx && s->ssl_ctx != ssl_ctx_global_scope) { 471 mbedtls_ssl_config_free(s->ssl_ctx); 472 free(s->ciphersuites); 473 free(s->curves); 474 } 475 } 476 /* free ssl_ctx from global scope */ 477 if (ssl_ctx_global_scope) { 478 mbedtls_ssl_config_free(ssl_ctx_global_scope); 479 free(p->ssl_ctxs[0].ciphersuites); 480 free(p->ssl_ctxs[0].curves); 481 } 482 free(p->ssl_ctxs); 483 } 484 485 if (NULL == p->cvlist) return; 486 /* (init i to 0 if global context; to 1 to skip empty global context) */ 487 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 488 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 489 for (; -1 != cpv->k_id; ++cpv) { 490 switch (cpv->k_id) { 491 case 0: /* ssl.pemfile */ 492 if (cpv->vtype == T_CONFIG_LOCAL) { 493 plugin_cert *pc = cpv->v.v; 494 mbedtls_pk_free(&pc->ssl_pemfile_pkey); 495 mbedtls_x509_crt_free(&pc->ssl_pemfile_x509); 496 free(pc); 497 } 498 break; 499 case 2: /* ssl.ca-file */ 500 case 3: /* ssl.ca-dn-file */ 501 if (cpv->vtype == T_CONFIG_LOCAL) { 502 mbedtls_x509_crt *cacert = cpv->v.v; 503 mbedtls_x509_crt_free(cacert); 504 free(cacert); 505 } 506 break; 507 case 4: /* ssl.ca-crl-file */ 508 if (cpv->vtype == T_CONFIG_LOCAL) { 509 mbedtls_x509_crl *crl = cpv->v.v; 510 mbedtls_x509_crl_free(crl); 511 free(crl); 512 } 513 break; 514 default: 515 break; 516 } 517 } 518 } 519 } 520 521 522 FREE_FUNC(mod_mbedtls_free) 523 { 524 plugin_data *p = p_d; 525 if (NULL == p->srv) return; 526 mod_mbedtls_free_config(p->srv, p); 527 mod_mbedtls_free_mbedtls(); 528 } 529 530 531 static void 532 mod_mbedtls_merge_config_cpv (plugin_config * const pconf, const config_plugin_value_t * const cpv) 533 { 534 switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 535 case 0: /* ssl.pemfile */ 536 if (cpv->vtype == T_CONFIG_LOCAL) 537 pconf->pc = cpv->v.v; 538 break; 539 case 1: /* ssl.privkey */ 540 break; 541 case 2: /* ssl.ca-file */ 542 if (cpv->vtype == T_CONFIG_LOCAL) 543 pconf->ssl_ca_file = cpv->v.v; 544 break; 545 case 3: /* ssl.ca-dn-file */ 546 if (cpv->vtype == T_CONFIG_LOCAL) 547 pconf->ssl_ca_dn_file = cpv->v.v; 548 break; 549 case 4: /* ssl.ca-crl-file */ 550 if (cpv->vtype == T_CONFIG_LOCAL) 551 pconf->ssl_ca_dn_file = cpv->v.v; 552 break; 553 case 5: /* ssl.read-ahead */ 554 pconf->ssl_read_ahead = (0 != cpv->v.u); 555 break; 556 case 6: /* ssl.disable-client-renegotiation */ 557 pconf->ssl_disable_client_renegotiation = (0 != cpv->v.u); 558 break; 559 case 7: /* ssl.verifyclient.activate */ 560 pconf->ssl_verifyclient = (0 != cpv->v.u); 561 break; 562 case 8: /* ssl.verifyclient.enforce */ 563 pconf->ssl_verifyclient_enforce = (0 != cpv->v.u); 564 break; 565 case 9: /* ssl.verifyclient.depth */ 566 pconf->ssl_verifyclient_depth = (unsigned char)cpv->v.shrt; 567 break; 568 case 10:/* ssl.verifyclient.username */ 569 pconf->ssl_verifyclient_username = cpv->v.b; 570 break; 571 case 11:/* ssl.verifyclient.exportcert */ 572 pconf->ssl_verifyclient_export_cert = (0 != cpv->v.u); 573 break; 574 case 12:/* ssl.acme-tls-1 */ 575 pconf->ssl_acme_tls_1 = cpv->v.b; 576 break; 577 case 13:/* debug.log-ssl-noise */ 578 pconf->ssl_log_noise = (unsigned char)cpv->v.shrt; 579 break; 580 #if 0 /*(cpk->k_id remapped in mod_mbedtls_set_defaults())*/ 581 case 14:/* ssl.verifyclient.ca-file */ 582 case 15:/* ssl.verifyclient.ca-dn-file */ 583 case 16:/* ssl.verifyclient.ca-crl-file */ 584 break; 585 #endif 586 default:/* should not happen */ 587 return; 588 } 589 } 590 591 592 static void 593 mod_mbedtls_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) 594 { 595 do { 596 mod_mbedtls_merge_config_cpv(pconf, cpv); 597 } while ((++cpv)->k_id != -1); 598 } 599 600 601 static void 602 mod_mbedtls_patch_config (request_st * const r, plugin_config * const pconf) 603 { 604 plugin_data * const p = plugin_data_singleton; 605 memcpy(pconf, &p->defaults, sizeof(plugin_config)); 606 for (int i = 1, used = p->nconfig; i < used; ++i) { 607 if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 608 mod_mbedtls_merge_config(pconf, p->cvlist + p->cvlist[i].v.u2[0]); 609 } 610 } 611 612 613 __attribute_pure__ 614 static int 615 mod_mbedtls_crt_is_self_issued (const mbedtls_x509_crt * const crt) 616 { 617 const mbedtls_x509_buf * const issuer = &crt->issuer_raw; 618 const mbedtls_x509_buf * const subject = &crt->subject_raw; 619 return subject->len == issuer->len 620 && 0 == memcmp(issuer->p, subject->p, subject->len); 621 } 622 623 624 static int 625 mod_mbedtls_construct_crt_chain (mbedtls_x509_crt *leaf, mbedtls_x509_crt *store, log_error_st *errh) 626 { 627 /* Historically, openssl will use the cert chain in (SSL_CTX *) if a cert 628 * does not have a chain configured in (SSL *). While similar behavior 629 * could be achieved with mbedtls_x509_crt_parse_file(crt, ssl_ca_file->ptr) 630 * instead attempt to do better and build a proper, ordered cert chain. */ 631 632 if (leaf->next) return 0; /*(presume chain has already been provided)*/ 633 if (store == NULL) return 0;/*(unable to proceed; chain may be incomplete)*/ 634 635 /* attempt to construct certificate chain from certificate store */ 636 for (mbedtls_x509_crt *crt = leaf; crt; ) { 637 const mbedtls_x509_buf * const issuer = &crt->issuer_raw; 638 639 /*(walk entire store in case certs are not properly sorted)*/ 640 for (crt = store; crt; crt = crt->next) { 641 /* The raw issuer/subject data (DER) is used for quick comparison */ 642 /* (see comments in mod_mbedtls_verify_cb())*/ 643 const mbedtls_x509_buf * const subject = &crt->subject_raw; 644 if (issuer->len != subject->len 645 || 0 != memcmp(subject->p, issuer->p, issuer->len)) continue; 646 647 /* root cert is end condition; omit from chain of intermediates */ 648 if (mod_mbedtls_crt_is_self_issued(crt)) 649 return 0; 650 651 int rc = 652 #if MBEDTLS_VERSION_NUMBER >= 0x02110000 /* mbedtls 2.17.0 */ 653 /* save memory by eliding copy of already-loaded raw DER */ 654 mbedtls_x509_crt_parse_der_nocopy(leaf, crt->raw.p, crt->raw.len); 655 #else 656 mbedtls_x509_crt_parse_der(leaf, crt->raw.p, crt->raw.len); 657 #endif 658 if (0 != rc) { /*(failure not unexpected since already parsed)*/ 659 elog(errh, __FILE__, __LINE__, rc, "cert copy failed"); 660 return rc; 661 } 662 break; 663 } 664 } 665 666 return 0; /*(no error, though cert chain may or may not be complete)*/ 667 } 668 669 670 static int 671 mod_mbedtls_verify_cb (void *arg, mbedtls_x509_crt *crt, int depth, uint32_t *flags) 672 { 673 handler_ctx * const hctx = (handler_ctx *)arg; 674 675 if (depth > hctx->conf.ssl_verifyclient_depth) { 676 log_error(hctx->r->conf.errh, __FILE__, __LINE__, 677 "MTLS: client cert chain too long"); 678 *flags |= MBEDTLS_X509_BADCERT_OTHER; /* cert chain too long */ 679 } 680 else if (0 == depth && NULL != hctx->conf.ssl_ca_dn_file) { 681 /* verify that client cert is issued by CA in ssl.ca-dn-file 682 * if both ssl.ca-dn-file and ssl.ca-file were configured */ 683 /* The raw issuer/subject data (DER) is used for quick comparison. */ 684 const size_t len = crt->issuer_raw.len; 685 mbedtls_x509_crt *chain = hctx->conf.ssl_ca_dn_file; 686 do { 687 #if 0 /* x509_name_cmp() is not a public func in mbedtls */ 688 if (0 == x509_name_cmp(&crt->issuer, &chain->subject)) 689 break; 690 #else 691 if (len == chain->subject_raw.len 692 && 0 == memcmp(chain->subject_raw.p, crt->issuer_raw.p, len)) 693 break; 694 #endif 695 } while ((chain = chain->next)); 696 697 if (NULL == chain) 698 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED; 699 } 700 if (*flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED) { 701 log_error(hctx->r->conf.errh, __FILE__, __LINE__, 702 "MTLS: client cert not trusted"); 703 } 704 705 return 0; 706 } 707 708 709 enum { 710 MOD_MBEDTLS_ALPN_HTTP11 = 1 711 ,MOD_MBEDTLS_ALPN_HTTP10 = 2 712 ,MOD_MBEDTLS_ALPN_H2 = 3 713 ,MOD_MBEDTLS_ALPN_ACME_TLS_1 = 4 714 }; 715 716 #ifdef MBEDTLS_SSL_SERVER_NAME_INDICATION 717 #ifdef MBEDTLS_SSL_ALPN 718 719 static int 720 mod_mbedtls_acme_tls_1 (handler_ctx *hctx); 721 722 #endif 723 #endif 724 725 726 #if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.00.0 */ 727 #define MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO 728 #define MBEDTLS_ERR_SSL_DECODE_ERROR MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO 729 #endif 730 731 732 #ifdef MBEDTLS_SSL_SERVER_NAME_INDICATION 733 static int 734 mod_mbedtls_SNI (void *arg, mbedtls_ssl_context *ssl, const unsigned char *servername, size_t len) 735 { 736 handler_ctx * const hctx = (handler_ctx *) arg; 737 request_st * const r = hctx->r; 738 buffer_copy_string_len(&r->uri.scheme, CONST_STR_LEN("https")); 739 740 if (len >= 1024) { /*(expecting < 256; TLSEXT_MAXLEN_host_name is 255)*/ 741 log_error(r->conf.errh, __FILE__, __LINE__, 742 "MTLS: SNI name too long %.*s", (int)len, servername); 743 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; 744 } 745 746 /* use SNI to patch mod_mbedtls config and then reset COMP_HTTP_HOST */ 747 buffer_copy_string_len_lc(&r->uri.authority, (const char *)servername, len); 748 #if 0 749 /*(r->uri.authority used below for configuration before request read; 750 * revisit for h2)*/ 751 if (0 != http_request_host_policy(&r->uri.authority, 752 r->conf.http_parseopts, 443)) 753 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; 754 #endif 755 756 const buffer * const ssl_pemfile = hctx->conf.pc->ssl_pemfile; 757 758 r->conditional_is_valid |= (1 << COMP_HTTP_SCHEME) 759 | (1 << COMP_HTTP_HOST); 760 761 mod_mbedtls_patch_config(r, &hctx->conf); 762 /* reset COMP_HTTP_HOST so that conditions re-run after request hdrs read */ 763 /*(done in configfile-glue.c:config_cond_cache_reset() after request hdrs read)*/ 764 /*config_cond_cache_reset_item(r, COMP_HTTP_HOST);*/ 765 /*buffer_clear(&r->uri.authority);*/ 766 767 #ifdef MBEDTLS_SSL_ALPN 768 /* TLS-ALPN-01 (ALPN "acme-tls/1") requires SNI */ 769 if (hctx->alpn == MOD_MBEDTLS_ALPN_ACME_TLS_1) 770 return mod_mbedtls_acme_tls_1(hctx); 771 #endif 772 773 /*(compare strings as ssl.pemfile might repeat same file in lighttpd.conf 774 * and mod_mbedtls does not attempt to de-dup)*/ 775 if (!buffer_is_equal(hctx->conf.pc->ssl_pemfile, ssl_pemfile)) { 776 /* if needed, attempt to construct certificate chain for server cert */ 777 if (hctx->conf.pc->need_chain) { 778 hctx->conf.pc->need_chain = 0; /*(attempt once to complete chain)*/ 779 mbedtls_x509_crt *ssl_cred = &hctx->conf.pc->ssl_pemfile_x509; 780 mbedtls_x509_crt *store = hctx->conf.ssl_ca_file; 781 if (!mod_mbedtls_construct_crt_chain(ssl_cred, store, r->conf.errh)) 782 return MBEDTLS_ERR_SSL_INTERNAL_ERROR; 783 } 784 /* reconfigure to use SNI-specific cert */ 785 int rc = 786 mbedtls_ssl_set_hs_own_cert(ssl, 787 &hctx->conf.pc->ssl_pemfile_x509, 788 &hctx->conf.pc->ssl_pemfile_pkey); 789 if (0 != rc) { 790 elogf(r->conf.errh, __FILE__, __LINE__, rc, 791 "failed to set SNI certificate for TLS server name %s", 792 r->uri.authority.ptr); 793 return MBEDTLS_ERR_SSL_INTERNAL_ERROR; 794 } 795 } 796 797 return 0; 798 } 799 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ 800 801 802 static int 803 mod_mbedtls_conf_verify (handler_ctx *hctx) 804 { 805 if (NULL == hctx->conf.ssl_ca_file) { 806 log_error(hctx->r->conf.errh, __FILE__, __LINE__, 807 "MTLS: can't verify client without ssl.verifyclient.ca-file " 808 "for TLS server name %s", 809 hctx->r->uri.authority.ptr); 810 return MBEDTLS_ERR_SSL_INTERNAL_ERROR; 811 } 812 813 /* send ssl_ca_dn_file (if set) in client certificate request 814 * (later changed to ssl_ca_file before client certificate verification) */ 815 mbedtls_x509_crt *ca_certs = hctx->conf.ssl_ca_dn_file 816 ? hctx->conf.ssl_ca_dn_file 817 : hctx->conf.ssl_ca_file; 818 819 mbedtls_ssl_context * const ssl = &hctx->ssl; 820 mbedtls_ssl_set_hs_ca_chain(ssl, ca_certs, hctx->conf.ssl_ca_crl_file); 821 #if MBEDTLS_VERSION_NUMBER >= 0x02120000 /* mbedtls 2.18.0 */ 822 mbedtls_ssl_set_verify(ssl, mod_mbedtls_verify_cb, hctx); 823 #else 824 /* overwrite callback with hctx each time we enter here, before handshake 825 * (Some callbacks are on mbedtls_ssl_config, not mbedtls_ssl_context) 826 * (Not thread-safe if config (mbedtls_ssl_config *ssl_ctx) is shared) 827 * (XXX: there is probably a better way to do this...) */ 828 mbedtls_ssl_conf_verify(hctx->ssl_ctx, mod_mbedtls_verify_cb, hctx); 829 #endif 830 return 0; 831 } 832 833 834 /* mbedTLS interfaces are generally excellent. mbedTLS convenience interfaces 835 * to read CRLs, X509 certs, and private keys are uniformly paranoid about 836 * clearing memory. At the moment, stdio routines fopen(), fread(), fclose() 837 * are used for portability, but without setvbuf(stream, NULL, _IOLBF, 0), 838 * again for portability, since setvbuf() is not necessarily available. Since 839 * stdio buffers by default, use our own funcs to read files without buffering. 840 * mbedtls_pk_load_file() includes trailing '\0' in size when contents in PEM 841 * format, so do the same with the value returned from fdevent_load_file(). 842 */ 843 844 845 static int 846 mod_mbedtls_x509_crl_parse_file (mbedtls_x509_crl *chain, const char *fn) 847 { 848 int rc = MBEDTLS_ERR_X509_FILE_IO_ERROR; 849 off_t dlen = 512*1024*1024;/*(arbitrary limit: 512 MB file; expect < 1 MB)*/ 850 char *data = fdevent_load_file(fn, &dlen, NULL, malloc, free); 851 if (NULL == data) return rc; 852 853 rc = mbedtls_x509_crl_parse(chain, (unsigned char *)data, (size_t)dlen+1); 854 855 if (dlen) ck_memzero(data, (size_t)dlen); 856 free(data); 857 858 return rc; 859 } 860 861 862 #if MBEDTLS_VERSION_NUMBER >= 0x02170000 /* mbedtls 2.23.0 */ 863 864 static int 865 mod_mbedtls_x509_crt_ext_cb (void *p_ctx, 866 mbedtls_x509_crt const *crt, 867 mbedtls_x509_buf const *oid, 868 int critical, 869 const unsigned char *p, 870 const unsigned char *end) 871 { 872 UNUSED(p_ctx); 873 UNUSED(crt); 874 /* id-pe-acmeIdentifier 1.3.6.1.5.5.7.1.31 */ 875 static const unsigned char acmeIdentifier[] = MBEDTLS_OID_PKIX "\x01\x1f"; 876 if (0 == MBEDTLS_OID_CMP(acmeIdentifier, oid)) { 877 if (!critical) /* required by RFC 8737 */ 878 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS 879 + MBEDTLS_ERR_ASN1_INVALID_DATA; 880 /*(mbedtls_asn1_get_tag() should take first param as 881 * (const unsigned char **) so safe to cast away const)*/ 882 unsigned char *q; 883 *(const unsigned char **)&q = p; 884 size_t len; 885 int rc = mbedtls_asn1_get_tag(&q, end, &len, 886 MBEDTLS_ASN1_OCTET_STRING 887 |MBEDTLS_ASN1_PRIMITIVE); 888 if (0 != rc) 889 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + rc; 890 if (q + len != end) 891 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS 892 + MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; 893 if (len != 32) /* must be OCTET STRING (SIZE (32)) */ 894 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS 895 + MBEDTLS_ERR_ASN1_INVALID_LENGTH; 896 return 0; 897 } 898 return MBEDTLS_ERR_ASN1_UNEXPECTED_TAG; 899 } 900 901 static int 902 mod_mbedtls_x509_crt_parse_acme (mbedtls_x509_crt *chain, const char *fn) 903 { 904 /* similar to mod_mbedtls_x509_crt_parse_file(), but read single cert 905 * and special case to handle id-pe-acmeIdentifier OID */ 906 /* https://github.com/ARMmbed/mbedtls/issues/3241 */ 907 int rc = MBEDTLS_ERR_X509_FILE_IO_ERROR; 908 off_t dlen = 512*1024*1024;/*(arbitrary limit: 512 MB file; expect < 1 MB)*/ 909 char *data = fdevent_load_file(fn, &dlen, NULL, malloc, free); 910 if (NULL == data) return rc; 911 912 mbedtls_pem_context pem; 913 mbedtls_pem_init(&pem); 914 915 size_t use_len; 916 rc = mbedtls_pem_read_buffer(&pem, 917 "-----BEGIN CERTIFICATE-----", 918 "-----END CERTIFICATE-----", 919 (unsigned char *)data, NULL, 0, &use_len); 920 if (0 == rc) { 921 mbedtls_x509_crt_ext_cb_t cb = mod_mbedtls_x509_crt_ext_cb; 922 #if MBEDTLS_VERSION_NUMBER >= 0x03020000 /* mbedtls 3.02.0 */ 923 size_t buflen; 924 const unsigned char *buf = mbedtls_pem_get_buffer(&pem, &buflen); 925 #else 926 const unsigned char *buf = pem.MBEDTLS_PRIVATE(buf); 927 size_t buflen = pem.MBEDTLS_PRIVATE(buflen); 928 #endif 929 rc = mbedtls_x509_crt_parse_der_with_ext_cb(chain,buf,buflen,1,cb,NULL); 930 } 931 932 mbedtls_pem_free(&pem); 933 934 if (dlen) ck_memzero(data, (size_t)dlen); 935 free(data); 936 937 return rc; 938 } 939 940 #endif 941 942 943 static int 944 mod_mbedtls_x509_crt_parse_file (mbedtls_x509_crt *chain, const char *fn) 945 { 946 int rc = MBEDTLS_ERR_X509_FILE_IO_ERROR; 947 off_t dlen = 512*1024*1024;/*(arbitrary limit: 512 MB file; expect < 1 MB)*/ 948 char *data = fdevent_load_file(fn, &dlen, NULL, malloc, free); 949 if (NULL == data) return rc; 950 951 rc = mbedtls_x509_crt_parse(chain, (unsigned char *)data, (size_t)dlen+1); 952 953 if (dlen) ck_memzero(data, (size_t)dlen); 954 free(data); 955 956 return rc; 957 } 958 959 960 static int 961 mod_mbedtls_pk_parse_keyfile (mbedtls_pk_context *ctx, const char *fn, const char *pwd) 962 { 963 int rc = MBEDTLS_ERR_PK_FILE_IO_ERROR; 964 off_t dlen = 512*1024*1024;/*(arbitrary limit: 512 MB file; expect < 1 MB)*/ 965 char *data = fdevent_load_file(fn, &dlen, NULL, malloc, free); 966 if (NULL == data) return rc; 967 968 #if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.00.0 */ 969 plugin_data * const p = plugin_data_singleton; 970 rc = mbedtls_pk_parse_key(ctx, (unsigned char *)data, (size_t)dlen+1, 971 (const unsigned char *)pwd, 972 pwd ? strlen(pwd) : 0, 973 mbedtls_ctr_drbg_random, &p->ctr_drbg); 974 #else 975 rc = mbedtls_pk_parse_key(ctx, (unsigned char *)data, (size_t)dlen+1, 976 (const unsigned char *)pwd, 977 pwd ? strlen(pwd) : 0); 978 #endif 979 980 if (dlen) ck_memzero(data, (size_t)dlen); 981 free(data); 982 983 return rc; 984 } 985 986 987 static void * 988 network_mbedtls_load_pemfile (server *srv, const buffer *pemfile, const buffer *privkey) 989 { 990 mbedtls_x509_crt ssl_pemfile_x509; /* parsed public key structure */ 991 mbedtls_pk_context ssl_pemfile_pkey; /* parsed private key structure */ 992 int rc; 993 994 mbedtls_x509_crt_init(&ssl_pemfile_x509); /* init cert structure */ 995 rc = mod_mbedtls_x509_crt_parse_file(&ssl_pemfile_x509, pemfile->ptr); 996 if (0 != rc) { 997 elogf(srv->errh, __FILE__, __LINE__, rc, 998 "PEM file cert read failed (%s)", pemfile->ptr); 999 return NULL; 1000 } 1001 1002 mbedtls_pk_init(&ssl_pemfile_pkey); /* init private key context */ 1003 rc = mod_mbedtls_pk_parse_keyfile(&ssl_pemfile_pkey, privkey->ptr, NULL); 1004 if (0 != rc) { 1005 elogf(srv->errh, __FILE__, __LINE__, rc, 1006 "PEM file private key read failed %s", privkey->ptr); 1007 mbedtls_x509_crt_free(&ssl_pemfile_x509); 1008 return NULL; 1009 } 1010 1011 #if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.00.0 */ 1012 plugin_data * const p = plugin_data_singleton; 1013 rc = mbedtls_pk_check_pair(&ssl_pemfile_x509.pk, &ssl_pemfile_pkey, 1014 mbedtls_ctr_drbg_random, &p->ctr_drbg); 1015 #else 1016 rc = mbedtls_pk_check_pair(&ssl_pemfile_x509.pk, &ssl_pemfile_pkey); 1017 #endif 1018 if (0 != rc) { 1019 elogf(srv->errh, __FILE__, __LINE__, rc, 1020 "PEM cert and private key did not verify (%s) (%s)", 1021 pemfile->ptr, privkey->ptr); 1022 mbedtls_pk_free(&ssl_pemfile_pkey); 1023 mbedtls_x509_crt_free(&ssl_pemfile_x509); 1024 return NULL; 1025 } 1026 1027 plugin_cert *pc = malloc(sizeof(plugin_cert)); 1028 force_assert(pc); 1029 pc->ssl_pemfile_pkey = ssl_pemfile_pkey; 1030 pc->ssl_pemfile_x509 = ssl_pemfile_x509; 1031 pc->ssl_pemfile = pemfile; 1032 pc->ssl_privkey = privkey; 1033 pc->need_chain = (ssl_pemfile_x509.next == NULL 1034 && !mod_mbedtls_crt_is_self_issued(&ssl_pemfile_x509)); 1035 mbedtls_platform_zeroize(&ssl_pemfile_pkey, sizeof(ssl_pemfile_pkey)); 1036 return pc; 1037 } 1038 1039 1040 #ifdef MBEDTLS_SSL_ALPN 1041 1042 #ifdef MBEDTLS_SSL_SERVER_NAME_INDICATION 1043 static int 1044 mod_mbedtls_acme_tls_1 (handler_ctx *hctx) 1045 { 1046 buffer * const b = hctx->tmp_buf; 1047 const buffer * const name = &hctx->r->uri.authority; 1048 log_error_st * const errh = hctx->r->conf.errh; 1049 mbedtls_x509_crt *ssl_pemfile_x509 = NULL; 1050 mbedtls_pk_context *ssl_pemfile_pkey = NULL; 1051 size_t len; 1052 int rc = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; 1053 1054 /* check if acme-tls/1 protocol is enabled (path to dir of cert(s) is set)*/ 1055 if (!hctx->conf.ssl_acme_tls_1) 1056 return 0; /*(should not happen)*/ 1057 1058 /* check if SNI set server name (required for acme-tls/1 protocol) 1059 * and perform simple path checks for no '/' 1060 * and no leading '.' (e.g. ignore "." or ".." or anything beginning '.') */ 1061 if (buffer_is_blank(name)) return rc; 1062 if (NULL != strchr(name->ptr, '/')) return rc; 1063 if (name->ptr[0] == '.') return rc; 1064 #if 0 1065 if (0 != http_request_host_policy(name,hctx->r->conf.http_parseopts,443)) 1066 return rc; 1067 #endif 1068 buffer_copy_path_len2(b, BUF_PTR_LEN(hctx->conf.ssl_acme_tls_1), 1069 BUF_PTR_LEN(name)); 1070 len = buffer_clen(b); 1071 1072 do { 1073 buffer_append_string_len(b, CONST_STR_LEN(".crt.pem")); 1074 ssl_pemfile_x509 = malloc(sizeof(*ssl_pemfile_x509)); 1075 force_assert(ssl_pemfile_x509); 1076 mbedtls_x509_crt_init(ssl_pemfile_x509); /* init cert structure */ 1077 #if MBEDTLS_VERSION_NUMBER >= 0x02170000 /* mbedtls 2.23.0 */ 1078 rc = mod_mbedtls_x509_crt_parse_acme(ssl_pemfile_x509, b->ptr); 1079 #else /*(will fail; unable to handle id-pe-acmeIdentifier OID)*/ 1080 rc = mod_mbedtls_x509_crt_parse_file(ssl_pemfile_x509, b->ptr); 1081 #endif 1082 if (0 != rc) { 1083 elogf(errh, __FILE__, __LINE__, rc, 1084 "Failed to load acme-tls/1 pemfile: %s", b->ptr); 1085 break; 1086 } 1087 1088 buffer_truncate(b, len); /*(remove ".crt.pem")*/ 1089 buffer_append_string_len(b, CONST_STR_LEN(".key.pem")); 1090 ssl_pemfile_pkey = malloc(sizeof(*ssl_pemfile_pkey)); 1091 force_assert(ssl_pemfile_pkey); 1092 mbedtls_pk_init(ssl_pemfile_pkey); /* init private key context */ 1093 rc = mod_mbedtls_pk_parse_keyfile(ssl_pemfile_pkey, b->ptr, NULL); 1094 if (0 != rc) { 1095 elogf(errh, __FILE__, __LINE__, rc, 1096 "Failed to load acme-tls/1 pemfile: %s", b->ptr); 1097 break; 1098 } 1099 1100 rc = mbedtls_ssl_set_hs_own_cert(&hctx->ssl, 1101 ssl_pemfile_x509, ssl_pemfile_pkey); 1102 if (0 != rc) { 1103 elogf(errh, __FILE__, __LINE__, rc, 1104 "failed to set acme-tls/1 certificate for TLS server " 1105 "name %s", name->ptr); 1106 break; 1107 } 1108 1109 hctx->acme_tls_1_pkey = ssl_pemfile_pkey; /* save ptr and free later */ 1110 hctx->acme_tls_1_x509 = ssl_pemfile_x509; /* save ptr and free later */ 1111 return 0; 1112 1113 } while (0); 1114 1115 if (ssl_pemfile_pkey) { 1116 mbedtls_pk_free(ssl_pemfile_pkey); 1117 free(ssl_pemfile_pkey); 1118 } 1119 if (ssl_pemfile_x509) { 1120 mbedtls_x509_crt_free(ssl_pemfile_x509); 1121 free(ssl_pemfile_x509); 1122 } 1123 1124 return rc; 1125 } 1126 #endif 1127 1128 1129 static int 1130 mod_mbedtls_alpn_h2_policy (handler_ctx * const hctx) 1131 { 1132 /*(currently called after handshake has completed)*/ 1133 #if 0 /* SNI omitted by client when connecting to IP instead of to name */ 1134 if (buffer_is_blank(&hctx->r->uri.authority)) { 1135 log_error(hctx->errh, __FILE__, __LINE__, 1136 "SSL: error ALPN h2 without SNI"); 1137 return -1; 1138 } 1139 #endif 1140 #if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.00.0 */ 1141 if (hctx->ssl.major_ver == MBEDTLS_SSL_MAJOR_VERSION_3 1142 && hctx->ssl.minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) { 1143 log_error(hctx->errh, __FILE__, __LINE__, 1144 "SSL: error ALPN h2 requires TLSv1.2 or later"); 1145 return -1; 1146 } 1147 #else /*(mbedTLS 3.0.0 dropped support for TLSv1.1 and earlier)*/ 1148 UNUSED(hctx); 1149 #endif 1150 1151 return 0; 1152 } 1153 1154 1155 static int 1156 mod_mbedtls_alpn_selected (handler_ctx * const hctx, const char * const in) 1157 { 1158 const int n = (int)strlen(in); 1159 const int i = 0; 1160 unsigned short proto; 1161 1162 switch (n) { 1163 case 2: /* "h2" */ 1164 if (in[i] == 'h' && in[i+1] == '2') { 1165 proto = MOD_MBEDTLS_ALPN_H2; 1166 hctx->r->http_version = HTTP_VERSION_2; 1167 break; 1168 } 1169 return 0; 1170 case 8: /* "http/1.1" "http/1.0" */ 1171 if (0 == memcmp(in+i, "http/1.", 7)) { 1172 if (in[i+7] == '1') { 1173 proto = MOD_MBEDTLS_ALPN_HTTP11; 1174 break; 1175 } 1176 if (in[i+7] == '0') { 1177 proto = MOD_MBEDTLS_ALPN_HTTP10; 1178 break; 1179 } 1180 } 1181 return 0; 1182 case 10: /* "acme-tls/1" */ 1183 if (0 == memcmp(in+i, "acme-tls/1", 10)) { 1184 proto = MOD_MBEDTLS_ALPN_ACME_TLS_1; 1185 break; 1186 } 1187 return 0; 1188 default: 1189 return 0; 1190 } 1191 1192 hctx->alpn = proto; 1193 return 0; 1194 } 1195 1196 1197 #if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.00.0 */ 1198 #ifdef MBEDTLS_SSL_SERVER_NAME_INDICATION 1199 static int 1200 mod_mbedtls_alpn_select_cb (handler_ctx *hctx, const unsigned char *in, const unsigned int inlen) 1201 { 1202 /*(skip first two bytes which should match inlen-2)*/ 1203 for (unsigned int i = 2, n; i < inlen; i += n) { 1204 n = in[i++]; 1205 if (i+n > inlen || 0 == n) break; 1206 switch (n) { 1207 case 2: /* "h2" */ 1208 if (in[i] == 'h' && in[i+1] == '2') { 1209 if (!hctx->r->conf.h2proto) continue; 1210 hctx->alpn = MOD_MBEDTLS_ALPN_H2; 1211 hctx->r->http_version = HTTP_VERSION_2; 1212 return 0; 1213 } 1214 continue; 1215 case 8: /* "http/1.1" "http/1.0" */ 1216 if (0 == memcmp(in+i, "http/1.", 7)) { 1217 if (in[i+7] == '1') { 1218 hctx->alpn = MOD_MBEDTLS_ALPN_HTTP11; 1219 return 0; 1220 } 1221 if (in[i+7] == '0') { 1222 hctx->alpn = MOD_MBEDTLS_ALPN_HTTP10; 1223 return 0; 1224 } 1225 } 1226 continue; 1227 case 10: /* "acme-tls/1" */ 1228 if (0 == memcmp(in+i, "acme-tls/1", 10)) { 1229 hctx->alpn = MOD_MBEDTLS_ALPN_ACME_TLS_1; 1230 return 0; 1231 } 1232 continue; 1233 default: 1234 continue; 1235 } 1236 } 1237 1238 return 0; 1239 } 1240 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ 1241 #endif /* MBEDTLS_VERSION_NUMBER < 0x03000000 */ /* mbedtls 3.00.0 */ 1242 1243 #endif /* MBEDTLS_SSL_ALPN */ 1244 1245 1246 static int 1247 mod_mbedtls_ssl_conf_ciphersuites (server *srv, plugin_config_socket *s, buffer *ciphersuites, const buffer *cipherstring); 1248 1249 1250 static int 1251 mod_mbedtls_ssl_conf_curves(server *srv, plugin_config_socket *s, const buffer *curvelist); 1252 1253 1254 static void 1255 mod_mbedtls_ssl_conf_proto (server *srv, plugin_config_socket *s, const buffer *b, int max); 1256 1257 1258 static int 1259 mod_mbedtls_ssl_conf_cmd (server *srv, plugin_config_socket *s) 1260 { 1261 /* reference: 1262 * https://www.openssl.org/docs/man1.1.1/man3/SSL_CONF_cmd.html */ 1263 int rc = 0; 1264 buffer *cipherstring = NULL; 1265 buffer *ciphersuites = NULL; 1266 1267 for (size_t i = 0; i < s->ssl_conf_cmd->used; ++i) { 1268 data_string *ds = (data_string *)s->ssl_conf_cmd->data[i]; 1269 if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("CipherString"))) 1270 cipherstring = &ds->value; 1271 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Ciphersuites"))) 1272 ciphersuites = &ds->value; 1273 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Curves")) 1274 || buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Groups"))) { 1275 if (!mod_mbedtls_ssl_conf_curves(srv, s, &ds->value)) 1276 rc = -1; 1277 } 1278 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("MaxProtocol"))) 1279 mod_mbedtls_ssl_conf_proto(srv, s, &ds->value, 1); /* max */ 1280 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("MinProtocol"))) 1281 mod_mbedtls_ssl_conf_proto(srv, s, &ds->value, 0); /* min */ 1282 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Protocol"))) { 1283 /* openssl config for Protocol=... is complex and deprecated */ 1284 log_error(srv->errh, __FILE__, __LINE__, 1285 "MTLS: ssl.openssl.ssl-conf-cmd %s ignored; " 1286 "use MinProtocol=... and MaxProtocol=... instead", 1287 ds->key.ptr); 1288 } 1289 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Options"))) { 1290 for (char *v = ds->value.ptr, *e; *v; v = e) { 1291 while (*v == ' ' || *v == '\t' || *v == ',') ++v; 1292 int flag = 1; 1293 if (*v == '-') { 1294 flag = 0; 1295 ++v; 1296 } 1297 for (e = v; light_isalpha(*e); ++e) ; 1298 switch ((int)(e-v)) { 1299 case 11: 1300 if (buffer_eq_icase_ssn(v, "Compression", 11)) { 1301 /* mbedtls defaults to no record compression unless 1302 * mbedtls is built with MBEDTLS_ZLIB_SUPPORT, which 1303 * is deprecated and slated for removal*/ 1304 if (!flag) continue; 1305 } 1306 break; 1307 case 13: 1308 if (buffer_eq_icase_ssn(v, "SessionTicket", 13)) { 1309 s->ssl_session_ticket = flag; 1310 continue; 1311 } 1312 break; 1313 case 16: 1314 if (buffer_eq_icase_ssn(v, "ServerPreference", 16)) { 1315 /* Note: before mbedTLS 3.0.0, the server uses its own 1316 * preferences over the preference of the client unless 1317 * MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE defined! */ 1318 s->ssl_honor_cipher_order = flag; 1319 continue; 1320 } 1321 break; 1322 default: 1323 break; 1324 } 1325 /* warn if not explicitly handled or ignored above */ 1326 if (!flag) --v; 1327 log_error(srv->errh, __FILE__, __LINE__, 1328 "MTLS: ssl.openssl.ssl-conf-cmd Options %.*s ignored", 1329 (int)(e-v), v); 1330 } 1331 } 1332 #if 0 1333 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("..."))) { 1334 } 1335 #endif 1336 else { 1337 /* warn if not explicitly handled or ignored above */ 1338 log_error(srv->errh, __FILE__, __LINE__, 1339 "MTLS: ssl.openssl.ssl-conf-cmd %s ignored", 1340 ds->key.ptr); 1341 } 1342 } 1343 1344 if (!mod_mbedtls_ssl_conf_ciphersuites(srv, s, ciphersuites, cipherstring)) 1345 rc = -1; 1346 1347 return rc; 1348 } 1349 1350 1351 static int 1352 network_init_ssl (server *srv, plugin_config_socket *s, plugin_data *p) 1353 { 1354 int rc; 1355 1356 s->ssl_ctx = malloc(sizeof(mbedtls_ssl_config)); 1357 force_assert(s->ssl_ctx); 1358 mbedtls_ssl_config_init(s->ssl_ctx); 1359 1360 /* set the RNG in the ssl config context, using the default random func */ 1361 mbedtls_ssl_conf_rng(s->ssl_ctx, mbedtls_ctr_drbg_random, &p->ctr_drbg); 1362 1363 /* mbedtls defaults to disable client renegotiation 1364 * mbedtls defaults to no record compression unless mbedtls is built 1365 * with MBEDTLS_ZLIB_SUPPORT, which is deprecated and slated for removal 1366 * MBEDTLS_SSL_PRESET_SUITEB is stricter than MBEDTLS_SSL_PRESET_DEFAULT 1367 * (and is attempted to be supported in mod_mbedtls_ssl_conf_ciphersuites()) 1368 * explanation: https://github.com/ARMmbed/mbedtls/issues/1591 1369 * reference: RFC 6460 */ 1370 rc = mbedtls_ssl_config_defaults(s->ssl_ctx, 1371 MBEDTLS_SSL_IS_SERVER, 1372 MBEDTLS_SSL_TRANSPORT_STREAM, 1373 MBEDTLS_SSL_PRESET_DEFAULT); 1374 if (0 != rc) { 1375 elog(srv->errh, __FILE__,__LINE__, rc, 1376 "Init of ssl config context defaults failed"); 1377 return -1; 1378 } 1379 1380 /* mbedtls defaults minimum accepted SSL/TLS protocol version TLS v1.0 1381 * use of SSL v3 should be avoided, and SSL v2 is not supported */ 1382 #ifdef MBEDTLS_SSL_PROTO_SSL3 1383 if (s->ssl_use_sslv3) 1384 mbedtls_ssl_conf_min_version(s->ssl_ctx, MBEDTLS_SSL_MAJOR_VERSION_3, 1385 MBEDTLS_SSL_MINOR_VERSION_0); 1386 #endif 1387 1388 if (s->ssl_cipher_list) { 1389 if (!mod_mbedtls_ssl_conf_ciphersuites(srv,s,NULL,s->ssl_cipher_list)) 1390 return -1; 1391 } 1392 1393 if (s->ssl_dh_file) { 1394 mbedtls_dhm_context dhm; 1395 mbedtls_dhm_init(&dhm); 1396 rc = mbedtls_dhm_parse_dhmfile(&dhm, s->ssl_dh_file->ptr); 1397 if (0 != rc) 1398 elogf(srv->errh, __FILE__,__LINE__, rc, 1399 "mbedtls_dhm_parse_dhmfile() %s", s->ssl_dh_file->ptr); 1400 else { 1401 rc = mbedtls_ssl_conf_dh_param_ctx(s->ssl_ctx, &dhm); 1402 if (0 != rc) 1403 elogf(srv->errh, __FILE__,__LINE__, rc, 1404 "mbedtls_ssl_conf_dh_param_ctx() %s", s->ssl_dh_file->ptr); 1405 } 1406 mbedtls_dhm_free(&dhm); 1407 if (0 != rc) 1408 return -1; 1409 } 1410 1411 if (s->ssl_ec_curve) { 1412 if (!mod_mbedtls_ssl_conf_curves(srv, s, s->ssl_ec_curve)) 1413 return -1; 1414 } 1415 1416 /* if needed, attempt to construct certificate chain for server cert */ 1417 if (s->pc->need_chain) { 1418 s->pc->need_chain = 0; /*(attempt once to complete chain)*/ 1419 if (!mod_mbedtls_construct_crt_chain(s->ssl_pemfile_x509, 1420 s->ssl_ca_file, srv->errh)) 1421 return -1; 1422 } 1423 1424 rc = mbedtls_ssl_conf_own_cert(s->ssl_ctx, 1425 s->ssl_pemfile_x509, s->ssl_pemfile_pkey); 1426 if (0 != rc) { 1427 elogf(srv->errh, __FILE__, __LINE__, rc, 1428 "PEM cert and private key did not verify (%s) (%s)", 1429 s->ssl_pemfile->ptr, s->ssl_privkey->ptr); 1430 return -1; 1431 } 1432 1433 #ifdef MBEDTLS_SSL_ALPN 1434 /* https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids */ 1435 static const char *alpn_protos_http_acme[] = { 1436 "h2" 1437 ,"http/1.1" 1438 ,"http/1.0" 1439 ,"acme-tls/1" 1440 ,NULL 1441 }; 1442 static const char *alpn_protos_http[] = { 1443 "h2" 1444 ,"http/1.1" 1445 ,"http/1.0" 1446 ,NULL 1447 }; 1448 const char **alpn_protos = (s->ssl_acme_tls_1) 1449 ? alpn_protos_http_acme 1450 : alpn_protos_http; 1451 if (!srv->srvconf.h2proto) ++alpn_protos; 1452 rc = mbedtls_ssl_conf_alpn_protocols(s->ssl_ctx, alpn_protos); 1453 if (0 != rc) { 1454 elog(srv->errh, __FILE__, __LINE__, rc, "error setting ALPN protocols"); 1455 return -1; 1456 } 1457 #endif 1458 1459 if (!s->ssl_use_sslv3 && !s->ssl_use_sslv2) 1460 mod_mbedtls_ssl_conf_proto(srv, s, NULL, 0); /* min */ 1461 1462 if (s->ssl_conf_cmd && s->ssl_conf_cmd->used) { 1463 if (0 != mod_mbedtls_ssl_conf_cmd(srv, s)) return -1; 1464 } 1465 1466 #if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.00.0 */ 1467 int order = s->ssl_honor_cipher_order 1468 ? MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER 1469 : MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_CLIENT; 1470 mbedtls_ssl_conf_preference_order(s->ssl_ctx, order); 1471 #else 1472 /* server preference is used (default) unless mbedtls is built with 1473 * MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE defined (not default) */ 1474 #ifndef MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE 1475 if (!s->ssl_honor_cipher_order) 1476 log_error(srv->errh, __FILE__, __LINE__, 1477 "MTLS: " 1478 "ignoring ssl.honor-cipher-order; mbedtls uses server preference " 1479 "unless mbedtls built MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE"); 1480 #else 1481 if (s->ssl_honor_cipher_order) 1482 log_error(srv->errh, __FILE__, __LINE__, 1483 "MTLS: " 1484 "ignoring ssl.honor-cipher-order; mbedtls uses client preference " 1485 "since mbedtls built MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE"); 1486 #endif 1487 #endif 1488 1489 #if defined(MBEDTLS_SSL_SESSION_TICKETS) 1490 if (s->ssl_session_ticket && !p->ticket_ctx.ticket_lifetime) { /*init once*/ 1491 rc = mbedtls_ssl_ticket_setup(&p->ticket_ctx, mbedtls_ctr_drbg_random, 1492 &p->ctr_drbg, MBEDTLS_CIPHER_AES_256_GCM, 1493 43200); /* ticket timeout: 12 hours */ 1494 if (0 != rc) { 1495 elog(srv->errh, __FILE__,__LINE__, rc, 1496 "mbedtls_ssl_ticket_setup()"); 1497 return -1; 1498 } 1499 } 1500 1501 #if defined(MBEDTLS_SSL_TICKET_C) 1502 if (s->ssl_session_ticket) 1503 mbedtls_ssl_conf_session_tickets_cb(s->ssl_ctx, 1504 mbedtls_ssl_ticket_write, 1505 mbedtls_ssl_ticket_parse, 1506 &p->ticket_ctx); 1507 #endif 1508 #endif /* MBEDTLS_SSL_SESSION_TICKETS */ 1509 1510 return 0; 1511 } 1512 1513 1514 static int 1515 mod_mbedtls_set_defaults_sockets(server *srv, plugin_data *p) 1516 { 1517 static const config_plugin_keys_t cpk[] = { 1518 { CONST_STR_LEN("ssl.engine"), 1519 T_CONFIG_BOOL, 1520 T_CONFIG_SCOPE_CONNECTION } 1521 ,{ CONST_STR_LEN("ssl.cipher-list"), 1522 T_CONFIG_STRING, 1523 T_CONFIG_SCOPE_CONNECTION } 1524 ,{ CONST_STR_LEN("ssl.honor-cipher-order"), 1525 T_CONFIG_BOOL, 1526 T_CONFIG_SCOPE_CONNECTION } 1527 ,{ CONST_STR_LEN("ssl.dh-file"), 1528 T_CONFIG_STRING, 1529 T_CONFIG_SCOPE_CONNECTION } 1530 ,{ CONST_STR_LEN("ssl.ec-curve"), 1531 T_CONFIG_STRING, 1532 T_CONFIG_SCOPE_CONNECTION } 1533 ,{ CONST_STR_LEN("ssl.openssl.ssl-conf-cmd"), 1534 T_CONFIG_ARRAY_KVSTRING, 1535 T_CONFIG_SCOPE_CONNECTION } 1536 ,{ CONST_STR_LEN("ssl.pemfile"), /* included to process global scope */ 1537 T_CONFIG_STRING, 1538 T_CONFIG_SCOPE_CONNECTION } 1539 ,{ CONST_STR_LEN("ssl.empty-fragments"), 1540 T_CONFIG_BOOL, 1541 T_CONFIG_SCOPE_CONNECTION } 1542 ,{ CONST_STR_LEN("ssl.use-sslv2"), 1543 T_CONFIG_BOOL, 1544 T_CONFIG_SCOPE_CONNECTION } 1545 ,{ CONST_STR_LEN("ssl.use-sslv3"), 1546 T_CONFIG_BOOL, 1547 T_CONFIG_SCOPE_CONNECTION } 1548 ,{ CONST_STR_LEN("ssl.stek-file"), 1549 T_CONFIG_STRING, 1550 T_CONFIG_SCOPE_SERVER } 1551 ,{ NULL, 0, 1552 T_CONFIG_UNSET, 1553 T_CONFIG_SCOPE_UNSET } 1554 }; 1555 static const buffer default_ssl_cipher_list = { CONST_STR_LEN("HIGH"), 0 }; 1556 1557 p->ssl_ctxs = calloc(srv->config_context->used, sizeof(plugin_ssl_ctx)); 1558 force_assert(p->ssl_ctxs); 1559 1560 int rc = HANDLER_GO_ON; 1561 plugin_data_base srvplug; 1562 memset(&srvplug, 0, sizeof(srvplug)); 1563 plugin_data_base * const ps = &srvplug; 1564 if (!config_plugin_values_init(srv, ps, cpk, "mod_mbedtls")) 1565 return HANDLER_ERROR; 1566 1567 plugin_config_socket defaults; 1568 memset(&defaults, 0, sizeof(defaults)); 1569 #ifndef MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE 1570 defaults.ssl_honor_cipher_order = 1; 1571 #endif 1572 defaults.ssl_session_ticket = 1; /* enabled by default */ 1573 defaults.ssl_cipher_list = &default_ssl_cipher_list; 1574 1575 /* process and validate config directives for global and $SERVER["socket"] 1576 * (init i to 0 if global context; to 1 to skip empty global context) */ 1577 for (int i = !ps->cvlist[0].v.u2[1]; i < ps->nconfig; ++i) { 1578 config_cond_info cfginfo; 1579 config_get_config_cond_info(&cfginfo, (uint32_t)ps->cvlist[i].k_id); 1580 int is_socket_scope = (0 == i || cfginfo.comp == COMP_SERVER_SOCKET); 1581 int count_not_engine = 0; 1582 1583 plugin_config_socket conf; 1584 memcpy(&conf, &defaults, sizeof(conf)); 1585 1586 /*(preserve prior behavior; not inherited)*/ 1587 /*(forcing inheritance might break existing configs where SSL is enabled 1588 * by default in the global scope, but not $SERVER["socket"]=="*:80") */ 1589 conf.ssl_enabled = 0; 1590 1591 config_plugin_value_t *cpv = ps->cvlist + ps->cvlist[i].v.u2[0]; 1592 for (; -1 != cpv->k_id; ++cpv) { 1593 /* ignore ssl.pemfile (k_id=6); included to process global scope */ 1594 if (!is_socket_scope && cpv->k_id != 6) { 1595 log_error(srv->errh, __FILE__, __LINE__, 1596 "MTLS: %s is valid only in global scope or " 1597 "$SERVER[\"socket\"] condition", cpk[cpv->k_id].k); 1598 continue; 1599 } 1600 ++count_not_engine; 1601 switch (cpv->k_id) { 1602 case 0: /* ssl.engine */ 1603 conf.ssl_enabled = (0 != cpv->v.u); 1604 --count_not_engine; 1605 break; 1606 case 1: /* ssl.cipher-list */ 1607 if (!buffer_is_blank(cpv->v.b)) 1608 conf.ssl_cipher_list = cpv->v.b; 1609 break; 1610 case 2: /* ssl.honor-cipher-order */ 1611 conf.ssl_honor_cipher_order = (0 != cpv->v.u); 1612 break; 1613 case 3: /* ssl.dh-file */ 1614 if (!buffer_is_blank(cpv->v.b)) 1615 conf.ssl_dh_file = cpv->v.b; 1616 break; 1617 case 4: /* ssl.ec-curve */ 1618 if (!buffer_is_blank(cpv->v.b)) 1619 conf.ssl_ec_curve = cpv->v.b; 1620 break; 1621 case 5: /* ssl.openssl.ssl-conf-cmd */ 1622 *(const array **)&conf.ssl_conf_cmd = cpv->v.a; 1623 break; 1624 case 6: /* ssl.pemfile */ 1625 /* ignore here; included to process global scope when 1626 * ssl.pemfile is set, but ssl.engine is not "enable" */ 1627 break; 1628 case 7: /* ssl.empty-fragments */ 1629 conf.ssl_empty_fragments = (0 != cpv->v.u); 1630 log_error(srv->errh, __FILE__, __LINE__, 1631 "MTLS: ignoring ssl.empty-fragments; openssl-specific " 1632 "counter-measure against a SSL 3.0/TLS 1.0 protocol " 1633 "vulnerability affecting CBC ciphers, which cannot be handled" 1634 " by some broken (Microsoft) SSL implementations."); 1635 break; 1636 case 8: /* ssl.use-sslv2 */ 1637 conf.ssl_use_sslv2 = (0 != cpv->v.u); 1638 log_error(srv->errh, __FILE__, __LINE__, "MTLS: " 1639 "ssl.use-sslv2 is deprecated and will soon be removed. " 1640 "Many modern TLS libraries no longer support SSLv2."); 1641 break; 1642 case 9: /* ssl.use-sslv3 */ 1643 conf.ssl_use_sslv3 = (0 != cpv->v.u); 1644 log_error(srv->errh, __FILE__, __LINE__, "MTLS: " 1645 "ssl.use-sslv3 is deprecated and will soon be removed. " 1646 "Many modern TLS libraries no longer support SSLv3. " 1647 "If needed, use: " 1648 "ssl.openssl.ssl-conf-cmd = (\"MinProtocol\" => \"SSLv3\")"); 1649 break; 1650 case 10:/* ssl.stek-file */ 1651 #ifdef MBEDTLS_SSL_SESSION_TICKETS 1652 if (!buffer_is_blank(cpv->v.b)) 1653 p->ssl_stek_file = cpv->v.b->ptr; 1654 #else 1655 log_error(srv->errh, __FILE__, __LINE__, "MTLS: " 1656 "ssl.stek-file ignored; mbedtls library not built with " 1657 "support for SSL session tickets"); 1658 #endif 1659 break; 1660 default:/* should not happen */ 1661 break; 1662 } 1663 } 1664 if (HANDLER_GO_ON != rc) break; 1665 if (0 == i) memcpy(&defaults, &conf, sizeof(conf)); 1666 1667 if (0 != i && !conf.ssl_enabled) continue; 1668 1669 /* fill plugin_config_socket with global context then $SERVER["socket"] 1670 * only for directives directly in current $SERVER["socket"] condition*/ 1671 1672 /*conf.ssl_pemfile_pkey = p->defaults.ssl_pemfile_pkey;*/ 1673 /*conf.ssl_pemfile_x509 = p->defaults.ssl_pemfile_x509;*/ 1674 conf.ssl_verifyclient = p->defaults.ssl_verifyclient; 1675 conf.ssl_verifyclient_enforce = p->defaults.ssl_verifyclient_enforce; 1676 conf.ssl_verifyclient_depth = p->defaults.ssl_verifyclient_depth; 1677 conf.ssl_acme_tls_1 = p->defaults.ssl_acme_tls_1; 1678 1679 int sidx = ps->cvlist[i].k_id; 1680 for (int j = !p->cvlist[0].v.u2[1]; j < p->nconfig; ++j) { 1681 if (p->cvlist[j].k_id != sidx) continue; 1682 /*if (0 == sidx) break;*//*(repeat to get ssl_pemfile,ssl_privkey)*/ 1683 cpv = p->cvlist + p->cvlist[j].v.u2[0]; 1684 for (; -1 != cpv->k_id; ++cpv) { 1685 ++count_not_engine; 1686 switch (cpv->k_id) { 1687 case 0: /* ssl.pemfile */ 1688 if (cpv->vtype == T_CONFIG_LOCAL) { 1689 plugin_cert *pc = cpv->v.v; 1690 conf.pc = pc; 1691 conf.ssl_pemfile_pkey = &pc->ssl_pemfile_pkey; 1692 conf.ssl_pemfile_x509 = &pc->ssl_pemfile_x509; 1693 conf.ssl_pemfile = pc->ssl_pemfile; 1694 conf.ssl_privkey = pc->ssl_privkey; 1695 } 1696 break; 1697 case 2: /* ssl.ca-file */ 1698 if (cpv->vtype == T_CONFIG_LOCAL) 1699 conf.ssl_ca_file = cpv->v.v; 1700 break; 1701 case 7: /* ssl.verifyclient.activate */ 1702 conf.ssl_verifyclient = (0 != cpv->v.u); 1703 break; 1704 case 8: /* ssl.verifyclient.enforce */ 1705 conf.ssl_verifyclient_enforce = (0 != cpv->v.u); 1706 break; 1707 case 9: /* ssl.verifyclient.depth */ 1708 conf.ssl_verifyclient_depth = (unsigned char)cpv->v.shrt; 1709 break; 1710 case 12:/* ssl.acme-tls-1 */ 1711 conf.ssl_acme_tls_1 = cpv->v.b; 1712 break; 1713 #if 0 /*(cpk->k_id remapped in mod_mbedtls_set_defaults())*/ 1714 case 14:/* ssl.verifyclient.ca-file */ 1715 #endif 1716 default: 1717 break; 1718 } 1719 } 1720 break; 1721 } 1722 1723 if (NULL == conf.ssl_pemfile_x509) { 1724 if (0 == i && !conf.ssl_enabled) continue; 1725 if (0 != i) { 1726 /* inherit ssl settings from global scope 1727 * (if only ssl.engine = "enable" and no other ssl.* settings) 1728 * (This is for convenience when defining both IPv4 and IPv6 1729 * and desiring to inherit the ssl config from global context 1730 * without having to duplicate the directives)*/ 1731 if (count_not_engine 1732 || (conf.ssl_enabled && NULL == p->ssl_ctxs[0].ssl_ctx)) { 1733 log_error(srv->errh, __FILE__, __LINE__, 1734 "MTLS: ssl.pemfile has to be set in same " 1735 "$SERVER[\"socket\"] scope as other ssl.* directives, " 1736 "unless only ssl.engine is set, inheriting ssl.* from " 1737 "global scope"); 1738 rc = HANDLER_ERROR; 1739 continue; 1740 } 1741 plugin_ssl_ctx * const s = p->ssl_ctxs + sidx; 1742 *s = *p->ssl_ctxs;/*(copy struct of ssl_ctx from global scope)*/ 1743 continue; 1744 } 1745 /* PEM file is required */ 1746 log_error(srv->errh, __FILE__, __LINE__, 1747 "MTLS: ssl.pemfile has to be set when ssl.engine = \"enable\""); 1748 rc = HANDLER_ERROR; 1749 continue; 1750 } 1751 1752 /* (initialize once if module enabled) */ 1753 if (!mod_mbedtls_init_once_mbedtls(srv)) { 1754 rc = HANDLER_ERROR; 1755 break; 1756 } 1757 1758 /* configure ssl_ctx for socket */ 1759 1760 /*conf.ssl_ctx = NULL;*//*(filled by network_init_ssl() even on error)*/ 1761 if (0 == network_init_ssl(srv, &conf, p)) { 1762 plugin_ssl_ctx * const s = p->ssl_ctxs + sidx; 1763 s->ssl_ctx = conf.ssl_ctx; 1764 s->ciphersuites = conf.ciphersuites; 1765 s->curves = conf.curves; 1766 } 1767 else { 1768 mbedtls_ssl_config_free(conf.ssl_ctx); 1769 free(conf.ciphersuites); 1770 free(conf.curves); 1771 rc = HANDLER_ERROR; 1772 } 1773 } 1774 1775 #ifdef MBEDTLS_SSL_SESSION_TICKETS 1776 if (rc == HANDLER_GO_ON && ssl_is_init) 1777 mod_mbedtls_session_ticket_key_check(p, log_epoch_secs); 1778 #endif 1779 1780 free(srvplug.cvlist); 1781 return rc; 1782 } 1783 1784 1785 SETDEFAULTS_FUNC(mod_mbedtls_set_defaults) 1786 { 1787 static const config_plugin_keys_t cpk[] = { 1788 { CONST_STR_LEN("ssl.pemfile"), 1789 T_CONFIG_STRING, 1790 T_CONFIG_SCOPE_CONNECTION } 1791 ,{ CONST_STR_LEN("ssl.privkey"), 1792 T_CONFIG_STRING, 1793 T_CONFIG_SCOPE_CONNECTION } 1794 ,{ CONST_STR_LEN("ssl.ca-file"), 1795 T_CONFIG_STRING, 1796 T_CONFIG_SCOPE_CONNECTION } 1797 ,{ CONST_STR_LEN("ssl.ca-dn-file"), 1798 T_CONFIG_STRING, 1799 T_CONFIG_SCOPE_CONNECTION } 1800 ,{ CONST_STR_LEN("ssl.ca-crl-file"), 1801 T_CONFIG_STRING, 1802 T_CONFIG_SCOPE_CONNECTION } 1803 ,{ CONST_STR_LEN("ssl.read-ahead"), 1804 T_CONFIG_BOOL, 1805 T_CONFIG_SCOPE_CONNECTION } 1806 ,{ CONST_STR_LEN("ssl.disable-client-renegotiation"), 1807 T_CONFIG_BOOL, 1808 T_CONFIG_SCOPE_CONNECTION } 1809 ,{ CONST_STR_LEN("ssl.verifyclient.activate"), 1810 T_CONFIG_BOOL, 1811 T_CONFIG_SCOPE_CONNECTION } 1812 ,{ CONST_STR_LEN("ssl.verifyclient.enforce"), 1813 T_CONFIG_BOOL, 1814 T_CONFIG_SCOPE_CONNECTION } 1815 ,{ CONST_STR_LEN("ssl.verifyclient.depth"), 1816 T_CONFIG_SHORT, 1817 T_CONFIG_SCOPE_CONNECTION } 1818 ,{ CONST_STR_LEN("ssl.verifyclient.username"), 1819 T_CONFIG_STRING, 1820 T_CONFIG_SCOPE_CONNECTION } 1821 ,{ CONST_STR_LEN("ssl.verifyclient.exportcert"), 1822 T_CONFIG_BOOL, 1823 T_CONFIG_SCOPE_CONNECTION } 1824 ,{ CONST_STR_LEN("ssl.acme-tls-1"), 1825 T_CONFIG_STRING, 1826 T_CONFIG_SCOPE_CONNECTION } 1827 ,{ CONST_STR_LEN("debug.log-ssl-noise"), 1828 T_CONFIG_BOOL, 1829 T_CONFIG_SCOPE_CONNECTION } 1830 ,{ CONST_STR_LEN("ssl.verifyclient.ca-file"), 1831 T_CONFIG_STRING, 1832 T_CONFIG_SCOPE_CONNECTION } 1833 ,{ CONST_STR_LEN("ssl.verifyclient.ca-dn-file"), 1834 T_CONFIG_STRING, 1835 T_CONFIG_SCOPE_CONNECTION } 1836 ,{ CONST_STR_LEN("ssl.verifyclient.ca-crl-file"), 1837 T_CONFIG_STRING, 1838 T_CONFIG_SCOPE_CONNECTION } 1839 ,{ NULL, 0, 1840 T_CONFIG_UNSET, 1841 T_CONFIG_SCOPE_UNSET } 1842 }; 1843 1844 plugin_data * const p = p_d; 1845 p->srv = srv; 1846 if (!config_plugin_values_init(srv, p, cpk, "mod_mbedtls")) 1847 return HANDLER_ERROR; 1848 1849 /* process and validate config directives 1850 * (init i to 0 if global context; to 1 to skip empty global context) */ 1851 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 1852 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1853 config_plugin_value_t *pemfile = NULL; 1854 config_plugin_value_t *privkey = NULL; 1855 for (; -1 != cpv->k_id; ++cpv) { 1856 switch (cpv->k_id) { 1857 case 0: /* ssl.pemfile */ 1858 if (!buffer_is_blank(cpv->v.b)) pemfile = cpv; 1859 break; 1860 case 1: /* ssl.privkey */ 1861 if (!buffer_is_blank(cpv->v.b)) privkey = cpv; 1862 break; 1863 case 14:/* ssl.verifyclient.ca-file */ 1864 if (cpv->k_id == 14) cpv->k_id = 2; 1865 __attribute_fallthrough__ 1866 case 15:/* ssl.verifyclient.ca-dn-file */ 1867 if (cpv->k_id == 15) cpv->k_id = 3; 1868 __attribute_fallthrough__ 1869 case 2: /* ssl.ca-file */ 1870 case 3: /* ssl.ca-dn-file */ 1871 #if 0 /* defer; not necessary for pemfile parsing */ 1872 if (!mod_mbedtls_init_once_mbedtls(srv)) return HANDLER_ERROR; 1873 #endif 1874 if (!buffer_is_blank(cpv->v.b)) { 1875 mbedtls_x509_crt *cacert = calloc(1, sizeof(*cacert)); 1876 force_assert(cacert); 1877 mbedtls_x509_crt_init(cacert); 1878 int rc = 1879 mod_mbedtls_x509_crt_parse_file(cacert, cpv->v.b->ptr); 1880 if (0 == rc) { 1881 cpv->vtype = T_CONFIG_LOCAL; 1882 cpv->v.v = cacert; 1883 } 1884 else { 1885 elogf(srv->errh, __FILE__, __LINE__, rc, 1886 "%s = %s", cpk[cpv->k_id].k, cpv->v.b->ptr); 1887 mbedtls_x509_crt_free(cacert); 1888 free(cacert); 1889 return HANDLER_ERROR; 1890 } 1891 } 1892 break; 1893 case 16:/* ssl.verifyclient.ca-crl-file */ 1894 cpv->k_id = 4; 1895 __attribute_fallthrough__ 1896 case 4: /* ssl.ca-crl-file */ 1897 if (!buffer_is_blank(cpv->v.b)) { 1898 mbedtls_x509_crl *crl = malloc(sizeof(*crl)); 1899 force_assert(crl); 1900 mbedtls_x509_crl_init(crl); 1901 int rc = 1902 mod_mbedtls_x509_crl_parse_file(crl, cpv->v.b->ptr); 1903 if (0 == rc) { 1904 cpv->vtype = T_CONFIG_LOCAL; 1905 cpv->v.v = crl; 1906 } 1907 else { 1908 elogf(srv->errh, __FILE__, __LINE__, rc, 1909 "CRL file read failed (%s)", cpv->v.b->ptr); 1910 free(crl); 1911 return HANDLER_ERROR; 1912 } 1913 } 1914 break; 1915 case 5: /* ssl.read-ahead */ 1916 break; 1917 case 6: /* ssl.disable-client-renegotiation */ 1918 /* (force disabled, the default, if HTTP/2 enabled in server) */ 1919 if (srv->srvconf.h2proto) 1920 cpv->v.u = 1; /* disable client renegotiation */ 1921 break; 1922 case 7: /* ssl.verifyclient.activate */ 1923 case 8: /* ssl.verifyclient.enforce */ 1924 break; 1925 case 9: /* ssl.verifyclient.depth */ 1926 if (cpv->v.shrt > 255) { 1927 log_error(srv->errh, __FILE__, __LINE__, 1928 "MTLS: %s is absurdly large (%hu); limiting to 255", 1929 cpk[cpv->k_id].k, cpv->v.shrt); 1930 cpv->v.shrt = 255; 1931 } 1932 break; 1933 case 10:/* ssl.verifyclient.username */ 1934 if (buffer_is_blank(cpv->v.b)) 1935 cpv->v.b = NULL; 1936 break; 1937 case 11:/* ssl.verifyclient.exportcert */ 1938 break; 1939 case 12:/* ssl.acme-tls-1 */ 1940 if (buffer_is_blank(cpv->v.b)) 1941 cpv->v.b = NULL; 1942 break; 1943 case 13:/* debug.log-ssl-noise */ 1944 #if 0 /*(handled further above)*/ 1945 case 14:/* ssl.verifyclient.ca-file */ 1946 case 15:/* ssl.verifyclient.ca-dn-file */ 1947 case 16:/* ssl.verifyclient.ca-crl-file */ 1948 #endif 1949 break; 1950 default:/* should not happen */ 1951 break; 1952 } 1953 } 1954 1955 if (pemfile) { 1956 if (NULL == privkey) privkey = pemfile; 1957 pemfile->v.v = 1958 network_mbedtls_load_pemfile(srv, pemfile->v.b, privkey->v.b); 1959 if (pemfile->v.v) 1960 pemfile->vtype = T_CONFIG_LOCAL; 1961 else 1962 return HANDLER_ERROR; 1963 } 1964 } 1965 1966 p->defaults.ssl_verifyclient = 0; 1967 p->defaults.ssl_verifyclient_enforce = 1; 1968 p->defaults.ssl_verifyclient_depth = 9; 1969 p->defaults.ssl_verifyclient_export_cert = 0; 1970 p->defaults.ssl_disable_client_renegotiation = 1; 1971 p->defaults.ssl_read_ahead = 0; 1972 1973 /* initialize p->defaults from global config context */ 1974 if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 1975 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 1976 if (-1 != cpv->k_id) 1977 mod_mbedtls_merge_config(&p->defaults, cpv); 1978 } 1979 1980 #ifndef MBEDTLS_ERROR_C 1981 log_error(srv->errh, __FILE__, __LINE__, 1982 "MTLS: No error strings available. " 1983 "Compile mbedtls with MBEDTLS_ERROR_C to enable."); 1984 #endif 1985 1986 return mod_mbedtls_set_defaults_sockets(srv, p); 1987 } 1988 1989 1990 /* local_send_buffer is a static buffer of size (LOCAL_SEND_BUFSIZE) 1991 * 1992 * buffer is allocated once, is NOT realloced (note: not thread-safe) 1993 * */ 1994 1995 /* copy small mem chunks into single large buffer 1996 * before mbedtls_ssl_write() to reduce number times 1997 * write() called underneath mbedtls_ssl_write() and 1998 * potentially reduce number of packets generated if TCP_NODELAY */ 1999 2000 2001 __attribute_cold__ 2002 static int 2003 mod_mbedtls_ssl_write_err(connection *con, handler_ctx *hctx, int wr, size_t wr_len) 2004 { 2005 switch (wr) { 2006 case MBEDTLS_ERR_SSL_WANT_READ: 2007 con->is_readable = -1; 2008 break; /* try again later */ 2009 case MBEDTLS_ERR_SSL_WANT_WRITE: 2010 con->is_writable = -1; 2011 break; /* try again later */ 2012 case MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS: 2013 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: 2014 break; /* try again later */ 2015 case MBEDTLS_ERR_NET_CONN_RESET: 2016 if (hctx->conf.ssl_log_noise) 2017 elog(hctx->r->conf.errh, __FILE__, __LINE__, wr, 2018 "peer closed connection"); 2019 return -1; 2020 default: 2021 elog(hctx->r->conf.errh, __FILE__, __LINE__, wr, __func__); 2022 return -1; 2023 } 2024 2025 #if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.00.0 */ 2026 if (0 != hctx->ssl.out_left) /* partial write; save attempted wr_len */ 2027 #endif 2028 hctx->pending_write = wr_len; 2029 2030 return 0; /* try again later */ 2031 } 2032 2033 2034 static int 2035 mod_mbedtls_close_notify(handler_ctx *hctx); 2036 2037 2038 static int 2039 connection_write_cq_ssl (connection * const con, chunkqueue * const cq, off_t max_bytes) 2040 { 2041 handler_ctx * const hctx = con->plugin_ctx[plugin_data_singleton->id]; 2042 mbedtls_ssl_context * const ssl = &hctx->ssl; 2043 2044 if (hctx->pending_write) { 2045 int wr = (int)hctx->pending_write; 2046 #if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.00.0 */ 2047 if (0 != ssl->out_left) 2048 #endif 2049 { 2050 /*(would prefer mbedtls_ssl_flush_output() from ssl_internal.h)*/ 2051 size_t data_len = hctx->pending_write; 2052 wr = mbedtls_ssl_write(ssl, NULL, data_len); 2053 if (wr <= 0) 2054 return mod_mbedtls_ssl_write_err(con, hctx, wr, data_len); 2055 max_bytes -= wr; 2056 } 2057 hctx->pending_write = 0; 2058 chunkqueue_mark_written(cq, wr); 2059 } 2060 2061 if (__builtin_expect( (0 != hctx->close_notify), 0)) 2062 return mod_mbedtls_close_notify(hctx); 2063 2064 const int lim = mbedtls_ssl_get_max_out_record_payload(ssl); 2065 if (lim < 0) return mod_mbedtls_ssl_write_err(con, hctx, lim, 0); 2066 2067 log_error_st * const errh = hctx->errh; 2068 while (max_bytes > 0 && !chunkqueue_is_empty(cq)) { 2069 char *data = local_send_buffer; 2070 uint32_t data_len = LOCAL_SEND_BUFSIZE < max_bytes 2071 ? LOCAL_SEND_BUFSIZE 2072 : (uint32_t)max_bytes; 2073 int wr; 2074 2075 if (0 != chunkqueue_peek_data(cq, &data, &data_len, errh)) return -1; 2076 if (__builtin_expect( (0 == data_len), 0)) { 2077 chunkqueue_remove_finished_chunks(cq); 2078 continue; 2079 } 2080 2081 /* mbedtls_ssl_write() copies the data, up to max record size, but if 2082 * (temporarily) unable to write the entire record, it is documented 2083 * that the caller must call mbedtls_ssl_write() again, later, with the 2084 * same arguments. This appears to be because mbedtls_ssl_context does 2085 * not keep track of the original size of the caller data that 2086 * mbedtls_ssl_write() attempted to write (and may have transformed to 2087 * a different size). The func may return MBEDTLS_ERR_SSL_WANT_READ or 2088 * MBEDTLS_ERR_SSL_WANT_WRITE to indicate that the caller should wait 2089 * for the fd to be readable/writable before calling the func again, 2090 * which is why those (temporary) errors are returned instead of telling 2091 * the caller that the data was successfully copied. When the record is 2092 * written successfully, the return value is supposed to indicate the 2093 * number of (originally submitted) bytes written, but since that value 2094 * is unknown (not saved), the caller's len parameter is reflected back, 2095 * which is why the caller must call the func again with the same args. 2096 * Additionally, to be accurate, the size must fit into a record which 2097 * is why we restrict ourselves to sending max out record payload each 2098 * iteration. 2099 */ 2100 2101 int wr_total = 0; 2102 do { 2103 size_t wr_len = (data_len > (size_t)lim) ? (size_t)lim : data_len; 2104 wr = mbedtls_ssl_write(ssl, (const unsigned char *)data, wr_len); 2105 if (wr <= 0) { 2106 if (wr_total) chunkqueue_mark_written(cq, wr_total); 2107 return mod_mbedtls_ssl_write_err(con, hctx, wr, wr_len); 2108 } 2109 wr_total += wr; 2110 data += wr; 2111 } while ((data_len -= wr)); 2112 chunkqueue_mark_written(cq, wr_total); 2113 max_bytes -= wr_total; 2114 } 2115 2116 return 0; 2117 } 2118 2119 2120 #if MBEDTLS_VERSION_NUMBER >= 0x03020000 /* mbedtls 3.02.0 */ 2121 #define handshake_state(ssl) (ssl)->MBEDTLS_PRIVATE(state) 2122 #elif MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.00.0 */ 2123 #define handshake_state(ssl) (ssl)->MBEDTLS_PRIVATE(state) 2124 #else /* MBEDTLS_VERSION_NUMBER < 0x03000000 */ /* mbedtls 3.00.0 */ 2125 #define handshake_state(ssl) (ssl)->state 2126 #ifdef MBEDTLS_SSL_SERVER_NAME_INDICATION 2127 #ifdef MBEDTLS_SSL_ALPN 2128 static int ssl_parse_client_hello( mbedtls_ssl_context *ssl, handler_ctx *hctx ); 2129 #endif 2130 #endif 2131 #endif /* MBEDTLS_VERSION_NUMBER < 0x03000000 */ /* mbedtls 3.00.0 */ 2132 2133 2134 static int 2135 mod_mbedtls_ssl_handshake (handler_ctx *hctx) 2136 { 2137 int rc = 0; 2138 2139 /* overwrite callback with hctx each time we enter here, before handshake 2140 * (Some callbacks are on mbedtls_ssl_config, not mbedtls_ssl_context) 2141 * (Not thread-safe if config (mbedtls_ssl_config *ssl_ctx) is shared) 2142 * (XXX: there is probably a better way to do this...) */ 2143 #ifdef MBEDTLS_SSL_SERVER_NAME_INDICATION 2144 mbedtls_ssl_conf_sni(hctx->ssl_ctx, mod_mbedtls_SNI, hctx); 2145 #endif 2146 2147 if (handshake_state(&hctx->ssl) < MBEDTLS_SSL_SERVER_HELLO) { 2148 while (handshake_state(&hctx->ssl) != MBEDTLS_SSL_SERVER_HELLO 2149 && handshake_state(&hctx->ssl) != MBEDTLS_SSL_HANDSHAKE_OVER) { 2150 /* disable in mbedtls 3.0+ until alternative callbacks are available 2151 * https://github.com/ARMmbed/mbedtls/issues/5430 */ 2152 #if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.00.0 */ 2153 #ifdef MBEDTLS_SSL_SERVER_NAME_INDICATION 2154 #ifdef MBEDTLS_SSL_ALPN 2155 /* parse client_hello for ALPN extension prior to mbedtls handshake 2156 * in order to perform certificate selection in mod_mbedtls_SNI() */ 2157 if (hctx->conf.ssl_acme_tls_1) { 2158 rc = ssl_parse_client_hello(&hctx->ssl, hctx); 2159 if (0 != rc) break; 2160 } 2161 #endif 2162 #endif 2163 #endif /* MBEDTLS_VERSION_NUMBER < 0x03000000 */ /* mbedtls 3.00.0 */ 2164 rc = mbedtls_ssl_handshake_step(&hctx->ssl); 2165 if (0 != rc) break; 2166 } 2167 if (0 == rc 2168 && handshake_state(&hctx->ssl) == MBEDTLS_SSL_SERVER_HELLO) { 2169 #ifdef MBEDTLS_SSL_ALPN 2170 const char *alpn = mbedtls_ssl_get_alpn_protocol(&hctx->ssl); 2171 if (NULL != alpn) 2172 rc = mod_mbedtls_alpn_selected(hctx, alpn); 2173 #endif 2174 } 2175 } 2176 2177 if (0 == rc && hctx->conf.ssl_verifyclient /*(after SNI and ALPN)*/ 2178 && handshake_state(&hctx->ssl) >= MBEDTLS_SSL_SERVER_HELLO 2179 && handshake_state(&hctx->ssl) <= MBEDTLS_SSL_SERVER_HELLO_DONE 2180 && hctx->alpn != MOD_MBEDTLS_ALPN_ACME_TLS_1) { /*(not "acme-tls/1")*/ 2181 int mode = (hctx->conf.ssl_verifyclient_enforce) 2182 ? MBEDTLS_SSL_VERIFY_REQUIRED 2183 : MBEDTLS_SSL_VERIFY_OPTIONAL; 2184 mbedtls_ssl_set_hs_authmode(&hctx->ssl, mode); 2185 while (handshake_state(&hctx->ssl) != MBEDTLS_SSL_CERTIFICATE_REQUEST 2186 && handshake_state(&hctx->ssl) != MBEDTLS_SSL_HANDSHAKE_OVER) { 2187 rc = mbedtls_ssl_handshake_step(&hctx->ssl); 2188 if (0 != rc) break; 2189 } 2190 if (0 == rc 2191 && handshake_state(&hctx->ssl) == MBEDTLS_SSL_CERTIFICATE_REQUEST) { 2192 rc = mod_mbedtls_conf_verify(hctx); 2193 if (0 == rc) 2194 rc = mbedtls_ssl_handshake_step(&hctx->ssl); 2195 /* reconfigure CA trust chain after sending client certificate 2196 * request (if ssl_ca_dn_file is set), before client certificate 2197 * verification (MBEDTLS_SSL_CERTIFICATE_VERIFY) */ 2198 if (0 == rc && hctx->conf.ssl_ca_dn_file 2199 && handshake_state(&hctx->ssl)==MBEDTLS_SSL_SERVER_HELLO_DONE) { 2200 mbedtls_ssl_context * const ssl = &hctx->ssl; 2201 mbedtls_x509_crt *ca_certs = hctx->conf.ssl_ca_file; 2202 mbedtls_x509_crl *ca_crl = hctx->conf.ssl_ca_crl_file; 2203 mbedtls_ssl_set_hs_ca_chain(ssl, ca_certs, ca_crl); 2204 } 2205 } 2206 } 2207 2208 if (0 == rc && handshake_state(&hctx->ssl) != MBEDTLS_SSL_HANDSHAKE_OVER) { 2209 rc = mbedtls_ssl_handshake(&hctx->ssl); 2210 } 2211 2212 switch (rc) { 2213 case 0: 2214 hctx->handshake_done = 1; 2215 #ifdef MBEDTLS_SSL_ALPN 2216 if (hctx->alpn == MOD_MBEDTLS_ALPN_H2) { 2217 if (0 != mod_mbedtls_alpn_h2_policy(hctx)) 2218 return -1; 2219 } 2220 else if (hctx->alpn == MOD_MBEDTLS_ALPN_ACME_TLS_1) { 2221 /* Once TLS handshake is complete, return -1 to result in 2222 * CON_STATE_ERROR so that socket connection is quickly closed */ 2223 return -1; 2224 } 2225 hctx->alpn = 0; 2226 #endif 2227 return 1; /* continue reading */ 2228 case MBEDTLS_ERR_SSL_WANT_WRITE: 2229 hctx->con->is_writable = -1; 2230 __attribute_fallthrough__ 2231 case MBEDTLS_ERR_SSL_WANT_READ: 2232 hctx->con->is_readable = 0; 2233 return 0; 2234 case MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS: 2235 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: 2236 return 0; 2237 case MBEDTLS_ERR_SSL_CLIENT_RECONNECT: 2238 return -1; 2239 case MBEDTLS_ERR_NET_CONN_RESET: 2240 case MBEDTLS_ERR_SSL_CONN_EOF: 2241 if (!hctx->conf.ssl_log_noise) return -1; 2242 __attribute_fallthrough__ 2243 default: 2244 elog(hctx->r->conf.errh, __FILE__, __LINE__, rc, __func__); 2245 return -1; 2246 } 2247 } 2248 2249 2250 static int 2251 connection_read_cq_ssl (connection * const con, chunkqueue * const cq, off_t max_bytes) 2252 { 2253 handler_ctx * const hctx = con->plugin_ctx[plugin_data_singleton->id]; 2254 int len; 2255 char *mem = NULL; 2256 size_t mem_len = 0; 2257 2258 UNUSED(max_bytes); 2259 2260 if (__builtin_expect( (0 != hctx->close_notify), 0)) 2261 return mod_mbedtls_close_notify(hctx); 2262 2263 if (!hctx->handshake_done) { 2264 int rc = mod_mbedtls_ssl_handshake(hctx); 2265 if (1 != rc) return rc; /* !hctx->handshake_done; not done, or error */ 2266 } 2267 2268 do { 2269 len = mbedtls_ssl_get_bytes_avail(&hctx->ssl); 2270 mem_len = len < 2048 ? 2048 : (size_t)len; 2271 chunk * const ckpt = cq->last; 2272 mem = chunkqueue_get_memory(cq, &mem_len); 2273 2274 len = mbedtls_ssl_read(&hctx->ssl, (unsigned char *)mem, mem_len); 2275 if (len > 0) { 2276 chunkqueue_use_memory(cq, ckpt, len); 2277 con->bytes_read += len; 2278 } else { 2279 chunkqueue_use_memory(cq, ckpt, 0); 2280 } 2281 } while (len > 0 2282 && mbedtls_ssl_check_pending(&hctx->ssl)); 2283 2284 if (len < 0) { 2285 int rc = len; 2286 request_st * const r = &con->request; 2287 switch (rc) { 2288 case MBEDTLS_ERR_SSL_WANT_WRITE: 2289 con->is_writable = -1; 2290 __attribute_fallthrough__ 2291 case MBEDTLS_ERR_SSL_WANT_READ: 2292 con->is_readable = 0; 2293 return 0; 2294 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: 2295 case MBEDTLS_ERR_SSL_CONN_EOF: 2296 /* XXX: future: save state to avoid future read after response? */ 2297 con->is_readable = 0; 2298 r->keep_alive = 0; 2299 return -2; 2300 case MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS: 2301 case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS: 2302 return 0; 2303 case MBEDTLS_ERR_SSL_CLIENT_RECONNECT: 2304 return -1; 2305 case MBEDTLS_ERR_NET_CONN_RESET: 2306 if (!hctx->conf.ssl_log_noise) return -1; 2307 __attribute_fallthrough__ 2308 default: 2309 elog(hctx->errh, __FILE__, __LINE__, rc, "Reading mbedtls"); 2310 return -1; 2311 } 2312 } else if (len == 0) { 2313 con->is_readable = 0; 2314 /* the other end closed the connection -> KEEP-ALIVE */ 2315 2316 return -2; 2317 } else { 2318 return 0; 2319 } 2320 } 2321 2322 2323 static void 2324 mod_mbedtls_debug_cb(void *ctx, int level, 2325 const char *file, int line, 2326 const char *str) 2327 { 2328 if (level < (intptr_t)ctx) /* level */ 2329 log_error(plugin_data_singleton->srv->errh,file,line,"MTLS: %s",str); 2330 } 2331 2332 2333 CONNECTION_FUNC(mod_mbedtls_handle_con_accept) 2334 { 2335 const server_socket *srv_sock = con->srv_socket; 2336 if (!srv_sock->is_ssl) return HANDLER_GO_ON; 2337 2338 plugin_data *p = p_d; 2339 handler_ctx * const hctx = handler_ctx_init(); 2340 request_st * const r = &con->request; 2341 hctx->r = r; 2342 hctx->con = con; 2343 hctx->tmp_buf = con->srv->tmp_buf; 2344 hctx->errh = r->conf.errh; 2345 con->plugin_ctx[p->id] = hctx; 2346 buffer_blank(&r->uri.authority); 2347 2348 hctx->ssl_ctx = p->ssl_ctxs[srv_sock->sidx].ssl_ctx; 2349 mbedtls_ssl_init(&hctx->ssl); 2350 int rc = mbedtls_ssl_setup(&hctx->ssl, hctx->ssl_ctx); 2351 if (0 == rc) { 2352 con->network_read = connection_read_cq_ssl; 2353 con->network_write = connection_write_cq_ssl; 2354 con->proto_default_port = 443; /* "https" */ 2355 mod_mbedtls_patch_config(r, &hctx->conf); 2356 } 2357 else { 2358 elog(r->conf.errh, __FILE__, __LINE__, rc, "ssl_setup() failed"); 2359 return HANDLER_ERROR; 2360 } 2361 2362 mbedtls_ssl_set_bio(&hctx->ssl, (mbedtls_net_context *)&con->fd, 2363 mbedtls_net_send, mbedtls_net_recv, NULL); 2364 2365 /* (mbedtls_ssl_config *) is shared across multiple connections, which may 2366 * overlap, and so this debug setting is not reset upon connection close. 2367 * Once enabled, debug hook will remain so for this mbedtls_ssl_config */ 2368 if (hctx->conf.ssl_log_noise) {/* volume level for debug message callback */ 2369 #if MBEDTLS_VERSION_NUMBER >= 0x02000000 /* mbedtls 2.0.0 */ 2370 mbedtls_debug_set_threshold(hctx->conf.ssl_log_noise); 2371 #endif 2372 mbedtls_ssl_conf_dbg(hctx->ssl_ctx, mod_mbedtls_debug_cb, 2373 (void *)(intptr_t)hctx->conf.ssl_log_noise); 2374 } 2375 2376 /* (mbedtls_ssl_config *) is shared across multiple connections, which may 2377 * overlap, and so renegotiation setting is not reset upon connection close. 2378 * Once enabled, renegotiation will remain so for this mbedtls_ssl_config. 2379 * mbedtls defaults to disable client renegotiation 2380 * (MBEDTLS_LEGACY_SSL_RENEGOTIATION_DISABLED) 2381 * and it is recommended to leave it disabled (lighttpd mbedtls default) */ 2382 #ifdef MBEDTLS_LEGACY_SSL_RENEGOTIATION_ENABLED 2383 if (!hctx->conf.ssl_disable_client_renegotiation) 2384 mbedtls_legacy_ssl_conf_renegotiation(hctx->ssl_ctx, 2385 MBEDTLS_LEGACY_SSL_RENEGOTIATION_ENABLED); 2386 #endif 2387 2388 return HANDLER_GO_ON; 2389 } 2390 2391 2392 static void 2393 mod_mbedtls_detach(handler_ctx *hctx) 2394 { 2395 /* step aside from further SSL processing 2396 * (used after handle_connection_shut_wr hook) */ 2397 /* future: might restore prior network_read and network_write fn ptrs */ 2398 hctx->con->is_ssl_sock = 0; 2399 /* if called after handle_connection_shut_wr hook, shutdown SHUT_WR */ 2400 if (-1 == hctx->close_notify) shutdown(hctx->con->fd, SHUT_WR); 2401 hctx->close_notify = 1; 2402 } 2403 2404 2405 CONNECTION_FUNC(mod_mbedtls_handle_con_shut_wr) 2406 { 2407 plugin_data *p = p_d; 2408 handler_ctx *hctx = con->plugin_ctx[p->id]; 2409 if (NULL == hctx) return HANDLER_GO_ON; 2410 2411 hctx->close_notify = -2; 2412 if (hctx->handshake_done) { 2413 mod_mbedtls_close_notify(hctx); 2414 } 2415 else { 2416 mod_mbedtls_detach(hctx); 2417 } 2418 2419 return HANDLER_GO_ON; 2420 } 2421 2422 2423 static int 2424 mod_mbedtls_close_notify (handler_ctx *hctx) 2425 { 2426 if (1 == hctx->close_notify) return -2; 2427 2428 int rc = mbedtls_ssl_close_notify(&hctx->ssl); 2429 switch (rc) { 2430 case 0: 2431 mod_mbedtls_detach(hctx); 2432 return -2; 2433 case MBEDTLS_ERR_SSL_WANT_READ: 2434 case MBEDTLS_ERR_SSL_WANT_WRITE: 2435 return 0; 2436 default: 2437 elog(hctx->r->conf.errh, __FILE__, __LINE__, rc, 2438 "mbedtls_ssl_close_notify()"); 2439 __attribute_fallthrough__ 2440 case MBEDTLS_ERR_NET_CONN_RESET: 2441 mbedtls_ssl_session_reset(&hctx->ssl); 2442 mod_mbedtls_detach(hctx); 2443 return -1; 2444 } 2445 } 2446 2447 2448 CONNECTION_FUNC(mod_mbedtls_handle_con_close) 2449 { 2450 plugin_data *p = p_d; 2451 handler_ctx *hctx = con->plugin_ctx[p->id]; 2452 if (NULL != hctx) { 2453 con->plugin_ctx[p->id] = NULL; 2454 if (1 != hctx->close_notify) 2455 mod_mbedtls_close_notify(hctx); /*(one final try)*/ 2456 handler_ctx_free(hctx); 2457 } 2458 2459 return HANDLER_GO_ON; 2460 } 2461 2462 2463 #if defined(MBEDTLS_PEM_WRITE_C) 2464 __attribute_noinline__ 2465 static void 2466 https_add_ssl_client_cert (request_st * const r, const mbedtls_x509_crt * const peer) 2467 { 2468 #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n" 2469 #define PEM_END_CRT "-----END CERTIFICATE-----\n" 2470 unsigned char buf[4096]; 2471 size_t olen; 2472 if (0 == mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT, 2473 peer->raw.p, peer->raw.len, 2474 buf, sizeof(buf), &olen)) 2475 http_header_env_set(r, 2476 CONST_STR_LEN("SSL_CLIENT_CERT"), 2477 (char *)buf, olen); 2478 } 2479 #endif 2480 2481 2482 static void 2483 https_add_ssl_client_subject (request_st * const r, const mbedtls_x509_name *name) 2484 { 2485 /* add components of client Subject DN */ 2486 /* code block is similar to mbedtls_x509_dn_gets() */ 2487 /* code block specialized for creating env vars of Subject DN components 2488 * and splits multi-valued RDNs into separate env vars for attribute=value*/ 2489 size_t n = 0; 2490 const size_t prelen = sizeof("SSL_CLIENT_S_DN_")-1; 2491 char key[64] = "SSL_CLIENT_S_DN_"; 2492 char buf[512]; /* MBEDTLS_X509_MAX_DN_NAME_SIZE is (256) */ 2493 2494 for (; name != NULL; name = name->next) { 2495 if (!name->oid.p) 2496 continue; 2497 const char *short_name = NULL; 2498 if (0 != mbedtls_oid_get_attr_short_name(&name->oid, &short_name)) 2499 continue; 2500 const size_t len = strlen(short_name); 2501 if (prelen+len >= sizeof(key)) continue; 2502 memcpy(key+prelen, short_name, len); /*(not '\0'-terminated)*/ 2503 2504 if (n+2+len+1+name->val.len > sizeof(buf)) continue; 2505 buf[n++] = ','; /*(", " at beginning is skipped outside loop below)*/ 2506 buf[n++] = ' '; 2507 memcpy(buf+n, short_name, len); 2508 n += len; 2509 buf[n++] = '='; 2510 2511 for (size_t i = 0; i < name->val.len; ++i) { 2512 unsigned char c = name->val.p[i]; 2513 buf[n+i] = (c >= 32 && c != 127) ? c : '?'; 2514 } 2515 2516 http_header_env_set(r, key, prelen+len, buf+n, name->val.len); 2517 n += name->val.len; 2518 } 2519 2520 /* mbedtls_x509_dn_gets() is not used to construct DN because that func does 2521 * not support non-ASCII UTF-8. This func allows non-ASCII UTF-8 but does 2522 * not check for need to backslash-encode special chars. This func 2523 * *assumes* a trusted and validated cert which does contain any chars which 2524 * need to be backslash-encoded in the stringified DN, even if such chars 2525 * are allowed in ASN.1 DN. Above, CTLs are encoded as '?' above, even 2526 * though some are allowed if backslash-encoded. Multi-valued RDNs are not 2527 * combined with '+' above, as name->next_merged is private in mbedtls 3.0.0 2528 */ 2529 if (n > 2) 2530 http_header_env_set(r, CONST_STR_LEN("SSL_CLIENT_S_DN"), buf+2, n-2); 2531 } 2532 2533 2534 __attribute_cold__ 2535 static void 2536 https_add_ssl_client_verify_err (buffer * const b, uint32_t status) 2537 { 2538 #ifndef MBEDTLS_X509_REMOVE_INFO 2539 /* get failure string and translate newline to ':', removing last one */ 2540 char buf[512]; 2541 int n = mbedtls_x509_crt_verify_info(buf, sizeof(buf), "", status); 2542 if (n > 0) { 2543 for (char *nl = buf; NULL != (nl = strchr(nl, '\n')); ++nl) 2544 nl[0] = ('\0' == nl[1] ? (--n, '\0') : ':'); 2545 buffer_append_string_len(b, buf, n); 2546 } 2547 #else 2548 UNUSED(b); 2549 UNUSED(status); 2550 #endif 2551 } 2552 2553 2554 __attribute_noinline__ 2555 static void 2556 https_add_ssl_client_entries (request_st * const r, handler_ctx * const hctx) 2557 { 2558 /* Note: starting with mbedtls-2.17.0, peer cert is not available here if 2559 * MBEDTLS_SSL_KEEP_PEER_CERTIFICATE *is not* defined at compile time, 2560 * though default behavior is to have it defined. However, since earlier 2561 * versions do keep the cert, but not set this define, attempt to retrieve 2562 * the peer cert and check for NULL before using it. */ 2563 const mbedtls_x509_crt *crt = mbedtls_ssl_get_peer_cert(&hctx->ssl); 2564 buffer *vb = http_header_env_set_ptr(r, CONST_STR_LEN("SSL_CLIENT_VERIFY")); 2565 2566 uint32_t rc = (NULL != crt) 2567 ? mbedtls_ssl_get_verify_result(&hctx->ssl) 2568 : 0xFFFFFFFF; 2569 if (0xFFFFFFFF == rc) { /*(e.g. no cert, or verify result not available)*/ 2570 buffer_copy_string_len(vb, CONST_STR_LEN("NONE")); 2571 return; 2572 } 2573 else if (0 != rc) { 2574 buffer_copy_string_len(vb, CONST_STR_LEN("FAILED:")); 2575 https_add_ssl_client_verify_err(vb, rc); 2576 return; 2577 } 2578 else { 2579 buffer_copy_string_len(vb, CONST_STR_LEN("SUCCESS")); 2580 } 2581 2582 https_add_ssl_client_subject(r, &crt->subject); 2583 2584 /* mbedtls_x509_serial_gets() (inefficiently) formats to hex separated by 2585 * colons, but would differ from behavior of other lighttpd TLS modules */ 2586 size_t i = 0; /* skip leading 0's per Distinguished Encoding Rules (DER) */ 2587 while (i < crt->serial.len && crt->serial.p[i] == 0) ++i; 2588 if (i == crt->serial.len) --i; 2589 buffer_append_string_encoded_hex_uc( 2590 http_header_env_set_ptr(r, CONST_STR_LEN("SSL_CLIENT_M_SERIAL")), 2591 (char *)crt->serial.p+i, crt->serial.len-i); 2592 2593 if (hctx->conf.ssl_verifyclient_username) { 2594 /* pick one of the exported values as "REMOTE_USER", for example 2595 * ssl.verifyclient.username = "SSL_CLIENT_S_DN_UID" 2596 * or 2597 * ssl.verifyclient.username = "SSL_CLIENT_S_DN_emailAddress" 2598 */ 2599 const buffer *varname = hctx->conf.ssl_verifyclient_username; 2600 vb = http_header_env_get(r, BUF_PTR_LEN(varname)); 2601 if (vb) { /* same as mod_auth_api.c:http_auth_setenv() */ 2602 http_header_env_set(r, 2603 CONST_STR_LEN("REMOTE_USER"), 2604 BUF_PTR_LEN(vb)); 2605 http_header_env_set(r, 2606 CONST_STR_LEN("AUTH_TYPE"), 2607 CONST_STR_LEN("SSL_CLIENT_VERIFY")); 2608 } 2609 } 2610 2611 #if defined(MBEDTLS_PEM_WRITE_C) 2612 /* if (NULL != crt) (e.g. not PSK-based ciphersuite) */ 2613 if (hctx->conf.ssl_verifyclient_export_cert) 2614 https_add_ssl_client_cert(r, crt); 2615 #endif 2616 } 2617 2618 2619 #if MBEDTLS_VERSION_NUMBER < 0x03020000 /* mbedtls 3.02.0 */ 2620 #define mbedtls_ssl_get_ciphersuite_id_from_ssl(ssl) \ 2621 (ssl)->MBEDTLS_PRIVATE(session)->MBEDTLS_PRIVATE(ciphersuite) 2622 #define mbedtls_ssl_ciphersuite_get_name(info) \ 2623 (info)->MBEDTLS_PRIVATE(name) 2624 #endif 2625 2626 static void 2627 http_cgi_ssl_env (request_st * const r, handler_ctx * const hctx) 2628 { 2629 const char *s; 2630 2631 s = mbedtls_ssl_get_version(&hctx->ssl); 2632 http_header_env_set(r, CONST_STR_LEN("SSL_PROTOCOL"), s, strlen(s)); 2633 2634 const int ciphersuite_id = 2635 mbedtls_ssl_get_ciphersuite_id_from_ssl(&hctx->ssl); 2636 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = 2637 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id); 2638 if (__builtin_expect( (NULL == ciphersuite_info), 0)) return; 2639 2640 s = mbedtls_ssl_ciphersuite_get_name(ciphersuite_info); 2641 http_header_env_set(r, CONST_STR_LEN("SSL_CIPHER"), s, strlen(s)); 2642 2643 { 2644 /* SSL_CIPHER_ALGKEYSIZE - Number of cipher bits (possible) */ 2645 /* SSL_CIPHER_USEKEYSIZE - Number of cipher bits (actually used) */ 2646 #if MBEDTLS_VERSION_NUMBER >= 0x03020000 /* mbedtls 3.02.0 */ 2647 size_t algkeysize = 2648 mbedtls_ssl_ciphersuite_get_cipher_key_bitlen(ciphersuite_info); 2649 unsigned int usekeysize = algkeysize; /*(equivalent in modern ciphers)*/ 2650 #elif MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.00.0 */ 2651 #ifdef MBEDTLS_CIPHER_C 2652 /*(messy transition; ssl->transform is hidden in ssl_internal.h)*/ 2653 const mbedtls_cipher_info_t * const cipher_info = 2654 mbedtls_cipher_info_from_type( 2655 ciphersuite_info->MBEDTLS_PRIVATE(cipher)); 2656 if (__builtin_expect( (NULL == cipher_info), 0)) return; 2657 unsigned int algkeysize = cipher_info->MBEDTLS_PRIVATE(key_bitlen); 2658 #else 2659 if (1) return; 2660 #endif 2661 unsigned int usekeysize = algkeysize; /*(equivalent in modern ciphers)*/ 2662 #else 2663 /* XXX: is usekeysize correct? XXX: reaching into ssl_internal.h here */ 2664 unsigned int usekeysize = 2665 hctx->ssl.transform->cipher_ctx_enc.key_bitlen; 2666 #ifdef MBEDTLS_CIPHER_C 2667 unsigned int algkeysize = 2668 hctx->ssl.transform->cipher_ctx_enc.cipher_info->key_bitlen; 2669 #else 2670 unsigned int algkeysize = usekeysize; 2671 #endif 2672 #endif 2673 char buf[LI_ITOSTRING_LENGTH]; 2674 http_header_env_set(r, CONST_STR_LEN("SSL_CIPHER_ALGKEYSIZE"), 2675 buf, li_utostrn(buf, sizeof(buf), algkeysize)); 2676 http_header_env_set(r, CONST_STR_LEN("SSL_CIPHER_USEKEYSIZE"), 2677 buf, li_utostrn(buf, sizeof(buf), usekeysize)); 2678 } 2679 } 2680 2681 2682 REQUEST_FUNC(mod_mbedtls_handle_request_env) 2683 { 2684 plugin_data *p = p_d; 2685 /* simple flag for request_env_patched */ 2686 if (r->plugin_ctx[p->id]) return HANDLER_GO_ON; 2687 handler_ctx *hctx = r->con->plugin_ctx[p->id]; 2688 if (NULL == hctx) return HANDLER_GO_ON; 2689 r->plugin_ctx[p->id] = (void *)(uintptr_t)1u; 2690 2691 http_cgi_ssl_env(r, hctx); 2692 if (hctx->conf.ssl_verifyclient) { 2693 https_add_ssl_client_entries(r, hctx); 2694 } 2695 2696 return HANDLER_GO_ON; 2697 } 2698 2699 2700 REQUEST_FUNC(mod_mbedtls_handle_uri_raw) 2701 { 2702 /* mod_mbedtls must be loaded prior to mod_auth 2703 * if mod_mbedtls is configured to set REMOTE_USER based on client cert */ 2704 /* mod_mbedtls must be loaded after mod_extforward 2705 * if mod_mbedtls config is based on lighttpd.conf remote IP conditional 2706 * using remote IP address set by mod_extforward, *unless* PROXY protocol 2707 * is enabled with extforward.hap-PROXY = "enable", in which case the 2708 * reverse is true: mod_extforward must be loaded after mod_mbedtls */ 2709 plugin_data *p = p_d; 2710 handler_ctx *hctx = r->con->plugin_ctx[p->id]; 2711 if (NULL == hctx) return HANDLER_GO_ON; 2712 2713 mod_mbedtls_patch_config(r, &hctx->conf); 2714 if (hctx->conf.ssl_verifyclient) { 2715 mod_mbedtls_handle_request_env(r, p); 2716 } 2717 2718 return HANDLER_GO_ON; 2719 } 2720 2721 2722 REQUEST_FUNC(mod_mbedtls_handle_request_reset) 2723 { 2724 plugin_data *p = p_d; 2725 r->plugin_ctx[p->id] = NULL; /* simple flag for request_env_patched */ 2726 return HANDLER_GO_ON; 2727 } 2728 2729 2730 TRIGGER_FUNC(mod_mbedtls_handle_trigger) { 2731 plugin_data * const p = p_d; 2732 const unix_time64_t cur_ts = log_epoch_secs; 2733 if (cur_ts & 0x3f) return HANDLER_GO_ON; /*(continue once each 64 sec)*/ 2734 UNUSED(srv); 2735 UNUSED(p); 2736 2737 #ifdef MBEDTLS_SSL_SESSION_TICKETS 2738 mod_mbedtls_session_ticket_key_check(p, cur_ts); 2739 #endif 2740 2741 return HANDLER_GO_ON; 2742 } 2743 2744 2745 int mod_mbedtls_plugin_init (plugin *p); 2746 int mod_mbedtls_plugin_init (plugin *p) 2747 { 2748 p->version = LIGHTTPD_VERSION_ID; 2749 p->name = "mbedtls"; 2750 p->init = mod_mbedtls_init; 2751 p->cleanup = mod_mbedtls_free; 2752 p->priv_defaults= mod_mbedtls_set_defaults; 2753 2754 p->handle_connection_accept = mod_mbedtls_handle_con_accept; 2755 p->handle_connection_shut_wr = mod_mbedtls_handle_con_shut_wr; 2756 p->handle_connection_close = mod_mbedtls_handle_con_close; 2757 p->handle_uri_raw = mod_mbedtls_handle_uri_raw; 2758 p->handle_request_env = mod_mbedtls_handle_request_env; 2759 p->handle_request_reset = mod_mbedtls_handle_request_reset; 2760 p->handle_trigger = mod_mbedtls_handle_trigger; 2761 2762 return 0; 2763 } 2764 2765 2766 /* cipher suites (taken from mbedtls/ssl_ciphersuites.[ch]) */ 2767 2768 static const int suite_CHACHAPOLY_ephemeral[] = { 2769 /* Chacha-Poly ephemeral suites */ 2770 MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 2771 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 2772 MBEDTLS_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 2773 }; 2774 2775 static const int suite_AES_256_ephemeral[] = { 2776 /* All AES-256 ephemeral suites */ 2777 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 2778 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 2779 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, 2780 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM, 2781 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM, 2782 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 2783 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 2784 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, 2785 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 2786 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 2787 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA, 2788 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8, 2789 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM_8 2790 }; 2791 2792 static const int suite_CAMELLIA_256_ephemeral[] = { 2793 /* All CAMELLIA-256 ephemeral suites */ 2794 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384, 2795 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384, 2796 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384, 2797 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384, 2798 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384, 2799 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256, 2800 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA 2801 }; 2802 2803 static const int suite_ARIA_256_ephemeral[] = { 2804 /* All ARIA-256 ephemeral suites */ 2805 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384, 2806 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384, 2807 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384, 2808 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384, 2809 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384, 2810 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384 2811 }; 2812 2813 static const int suite_AES_128_ephemeral[] = { 2814 /* All AES-128 ephemeral suites */ 2815 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 2816 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 2817 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, 2818 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM, 2819 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM, 2820 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 2821 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 2822 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, 2823 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 2824 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 2825 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA, 2826 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, 2827 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM_8 2828 }; 2829 2830 static const int suite_CAMELLIA_128_ephemeral[] = { 2831 /* All CAMELLIA-128 ephemeral suites */ 2832 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256, 2833 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256, 2834 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256, 2835 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256, 2836 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256, 2837 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256, 2838 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA 2839 }; 2840 2841 static const int suite_ARIA_128_ephemeral[] = { 2842 /* All ARIA-128 ephemeral suites */ 2843 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256, 2844 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256, 2845 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256, 2846 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256, 2847 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256, 2848 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256 2849 }; 2850 2851 static const int suite_PSK_ephemeral[] = { 2852 /* The PSK ephemeral suites */ 2853 MBEDTLS_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, 2854 MBEDTLS_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256, 2855 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384, 2856 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM, 2857 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384, 2858 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384, 2859 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA, 2860 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA, 2861 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384, 2862 MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384, 2863 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384, 2864 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM_8, 2865 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384, 2866 MBEDTLS_TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384, 2867 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384, 2868 2869 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256, 2870 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM, 2871 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256, 2872 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256, 2873 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA, 2874 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA, 2875 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256, 2876 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256, 2877 MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256, 2878 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM_8, 2879 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256, 2880 MBEDTLS_TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256, 2881 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256 2882 }; 2883 2884 #if 0 2885 static const int suite_ECJPAKE[] = { 2886 /* The ECJPAKE suite */ 2887 MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8 2888 }; 2889 #endif 2890 2891 static const int suite_AES_256[] = { 2892 /* All AES-256 suites */ 2893 MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384, 2894 MBEDTLS_TLS_RSA_WITH_AES_256_CCM, 2895 MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256, 2896 MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA, 2897 MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, 2898 MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, 2899 MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, 2900 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, 2901 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, 2902 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, 2903 MBEDTLS_TLS_RSA_WITH_AES_256_CCM_8 2904 }; 2905 2906 static const int suite_CAMELLIA_256[] = { 2907 /* All CAMELLIA-256 suites */ 2908 MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384, 2909 MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256, 2910 MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA, 2911 MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384, 2912 MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384, 2913 MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384, 2914 MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 2915 }; 2916 2917 static const int suite_ARIA_256[] = { 2918 /* All ARIA-256 suites */ 2919 MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384, 2920 MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384, 2921 MBEDTLS_TLS_RSA_WITH_ARIA_256_GCM_SHA384, 2922 MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384, 2923 MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384, 2924 MBEDTLS_TLS_RSA_WITH_ARIA_256_CBC_SHA384 2925 }; 2926 2927 static const int suite_AES_128[] = { 2928 /* All AES-128 suites */ 2929 MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256, 2930 MBEDTLS_TLS_RSA_WITH_AES_128_CCM, 2931 MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256, 2932 MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA, 2933 MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, 2934 MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, 2935 MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, 2936 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, 2937 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, 2938 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, 2939 MBEDTLS_TLS_RSA_WITH_AES_128_CCM_8 2940 }; 2941 2942 static const int suite_CAMELLIA_128[] = { 2943 /* All CAMELLIA-128 suites */ 2944 MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256, 2945 MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256, 2946 MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA, 2947 MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256, 2948 MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256, 2949 MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256, 2950 MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 2951 }; 2952 2953 static const int suite_ARIA_128[] = { 2954 /* All ARIA-128 suites */ 2955 MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256, 2956 MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256, 2957 MBEDTLS_TLS_RSA_WITH_ARIA_128_GCM_SHA256, 2958 MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256, 2959 MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256, 2960 MBEDTLS_TLS_RSA_WITH_ARIA_128_CBC_SHA256 2961 }; 2962 2963 static const int suite_RSA_PSK[] = { 2964 /* The RSA PSK suites */ 2965 MBEDTLS_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256, 2966 MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384, 2967 MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384, 2968 MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA, 2969 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384, 2970 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384, 2971 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384, 2972 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384, 2973 2974 MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256, 2975 MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256, 2976 MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA, 2977 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256, 2978 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256, 2979 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256, 2980 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256 2981 }; 2982 2983 static const int suite_PSK[] = { 2984 /* The PSK suites */ 2985 MBEDTLS_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256, 2986 MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384, 2987 MBEDTLS_TLS_PSK_WITH_AES_256_CCM, 2988 MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384, 2989 MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA, 2990 MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384, 2991 MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384, 2992 MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8, 2993 MBEDTLS_TLS_PSK_WITH_ARIA_256_GCM_SHA384, 2994 MBEDTLS_TLS_PSK_WITH_ARIA_256_CBC_SHA384, 2995 2996 MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256, 2997 MBEDTLS_TLS_PSK_WITH_AES_128_CCM, 2998 MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256, 2999 MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA, 3000 MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256, 3001 MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256, 3002 MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8, 3003 MBEDTLS_TLS_PSK_WITH_ARIA_128_GCM_SHA256, 3004 MBEDTLS_TLS_PSK_WITH_ARIA_128_CBC_SHA256 3005 }; 3006 3007 #ifdef MBEDTLS_SSL_PROTO_TLS1 3008 static const int suite_3DES[] = { 3009 /* 3DES suites */ 3010 MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, 3011 MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 3012 MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, 3013 MBEDTLS_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA, 3014 MBEDTLS_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA, 3015 MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA, 3016 MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, 3017 MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, 3018 MBEDTLS_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA, 3019 MBEDTLS_TLS_PSK_WITH_3DES_EDE_CBC_SHA 3020 }; 3021 #endif 3022 3023 #ifdef MBEDTLS_SSL_PROTO_TLS1 3024 static const int suite_RC4[] = { 3025 /* RC4 suites */ 3026 MBEDTLS_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 3027 MBEDTLS_TLS_ECDHE_RSA_WITH_RC4_128_SHA, 3028 MBEDTLS_TLS_ECDHE_PSK_WITH_RC4_128_SHA, 3029 MBEDTLS_TLS_DHE_PSK_WITH_RC4_128_SHA, 3030 MBEDTLS_TLS_RSA_WITH_RC4_128_SHA, 3031 MBEDTLS_TLS_RSA_WITH_RC4_128_MD5, 3032 MBEDTLS_TLS_ECDH_RSA_WITH_RC4_128_SHA, 3033 MBEDTLS_TLS_ECDH_ECDSA_WITH_RC4_128_SHA, 3034 MBEDTLS_TLS_RSA_PSK_WITH_RC4_128_SHA, 3035 MBEDTLS_TLS_PSK_WITH_RC4_128_SHA 3036 }; 3037 #endif 3038 3039 #ifdef MBEDTLS_SSL_PROTO_SSL3 3040 static const int suite_weak[] = { 3041 /* Weak suites */ 3042 MBEDTLS_TLS_DHE_RSA_WITH_DES_CBC_SHA, 3043 MBEDTLS_TLS_RSA_WITH_DES_CBC_SHA 3044 }; 3045 #endif 3046 3047 static const int suite_null[] = { 3048 /* NULL suites */ 3049 MBEDTLS_TLS_ECDHE_ECDSA_WITH_NULL_SHA, 3050 MBEDTLS_TLS_ECDHE_RSA_WITH_NULL_SHA, 3051 MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA384, 3052 MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA256, 3053 MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA, 3054 MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA384, 3055 MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA256, 3056 MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA, 3057 3058 MBEDTLS_TLS_RSA_WITH_NULL_SHA256, 3059 MBEDTLS_TLS_RSA_WITH_NULL_SHA, 3060 MBEDTLS_TLS_RSA_WITH_NULL_MD5, 3061 MBEDTLS_TLS_ECDH_RSA_WITH_NULL_SHA, 3062 MBEDTLS_TLS_ECDH_ECDSA_WITH_NULL_SHA, 3063 MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA384, 3064 MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA256, 3065 MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA, 3066 MBEDTLS_TLS_PSK_WITH_NULL_SHA384, 3067 MBEDTLS_TLS_PSK_WITH_NULL_SHA256, 3068 MBEDTLS_TLS_PSK_WITH_NULL_SHA 3069 }; 3070 3071 /* TLSv1.2 cipher list (supported in mbedtls) 3072 * marked with minimum version MBEDTLS_SSL_MINOR_VERSION_3 in 3073 * ciphersuite_definitions[] and then sorted by ciphersuite_preference[] 3074 * from mbedtls library/ssl_ciphersuites.c */ 3075 static const int suite_TLSv12[] = { 3076 MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 3077 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 3078 MBEDTLS_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 3079 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 3080 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 3081 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, 3082 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM, 3083 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM, 3084 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 3085 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 3086 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, 3087 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8, 3088 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM_8, 3089 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384, 3090 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384, 3091 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384, 3092 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256, 3093 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384, 3094 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384, 3095 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384, 3096 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384, 3097 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384, 3098 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384, 3099 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 3100 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 3101 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, 3102 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM, 3103 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM, 3104 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 3105 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 3106 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, 3107 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, 3108 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM_8, 3109 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256, 3110 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256, 3111 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256, 3112 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256, 3113 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256, 3114 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256, 3115 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256, 3116 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256, 3117 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256, 3118 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256, 3119 MBEDTLS_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, 3120 MBEDTLS_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256, 3121 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384, 3122 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM, 3123 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384, 3124 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM_8, 3125 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384, 3126 MBEDTLS_TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384, 3127 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384, 3128 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256, 3129 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM, 3130 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256, 3131 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM_8, 3132 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256, 3133 MBEDTLS_TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256, 3134 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256, 3135 MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8, 3136 MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384, 3137 MBEDTLS_TLS_RSA_WITH_AES_256_CCM, 3138 MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256, 3139 MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, 3140 MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, 3141 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, 3142 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, 3143 MBEDTLS_TLS_RSA_WITH_AES_256_CCM_8, 3144 MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384, 3145 MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256, 3146 MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384, 3147 MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384, 3148 MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384, 3149 MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384, 3150 MBEDTLS_TLS_RSA_WITH_ARIA_256_GCM_SHA384, 3151 MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384, 3152 MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384, 3153 MBEDTLS_TLS_RSA_WITH_ARIA_256_CBC_SHA384, 3154 MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256, 3155 MBEDTLS_TLS_RSA_WITH_AES_128_CCM, 3156 MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256, 3157 MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, 3158 MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, 3159 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, 3160 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, 3161 MBEDTLS_TLS_RSA_WITH_AES_128_CCM_8, 3162 MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256, 3163 MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256, 3164 MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256, 3165 MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256, 3166 MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256, 3167 MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256, 3168 MBEDTLS_TLS_RSA_WITH_ARIA_128_GCM_SHA256, 3169 MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256, 3170 MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256, 3171 MBEDTLS_TLS_RSA_WITH_ARIA_128_CBC_SHA256, 3172 MBEDTLS_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256, 3173 MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384, 3174 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384, 3175 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384, 3176 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384, 3177 MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256, 3178 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256, 3179 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256, 3180 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256, 3181 MBEDTLS_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256, 3182 MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384, 3183 MBEDTLS_TLS_PSK_WITH_AES_256_CCM, 3184 MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384, 3185 MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8, 3186 MBEDTLS_TLS_PSK_WITH_ARIA_256_GCM_SHA384, 3187 MBEDTLS_TLS_PSK_WITH_ARIA_256_CBC_SHA384, 3188 MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256, 3189 MBEDTLS_TLS_PSK_WITH_AES_128_CCM, 3190 MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256, 3191 MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8, 3192 MBEDTLS_TLS_PSK_WITH_ARIA_128_GCM_SHA256, 3193 MBEDTLS_TLS_PSK_WITH_ARIA_128_CBC_SHA256 3194 }; 3195 3196 #ifdef MBEDTLS_SSL_PROTO_TLS1 3197 /* TLSv1.0 cipher list (supported in mbedtls) 3198 * marked with minimum version MBEDTLS_SSL_MINOR_VERSION_1 in 3199 * ciphersuite_definitions[] and then sorted by ciphersuite_preference[] 3200 * from mbedtls library/ssl_ciphersuites.c */ 3201 /* XXX: intentionally not including overlapping eNULL ciphers */ 3202 static const int suite_TLSv10[] = { 3203 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 3204 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 3205 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384, 3206 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384, 3207 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 3208 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 3209 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256, 3210 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256, 3211 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384, 3212 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384, 3213 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA, 3214 MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384, 3215 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384, 3216 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256, 3217 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256, 3218 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA, 3219 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256, 3220 MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256, 3221 MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, 3222 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, 3223 MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384, 3224 MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384, 3225 MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, 3226 MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, 3227 MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256, 3228 MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256, 3229 MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384, 3230 MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA, 3231 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384, 3232 MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256, 3233 MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA, 3234 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256, 3235 MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384, 3236 MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384, 3237 MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256, 3238 MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256, 3239 MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, 3240 MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 3241 MBEDTLS_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA, 3242 MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, 3243 MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, 3244 MBEDTLS_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA, 3245 MBEDTLS_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 3246 MBEDTLS_TLS_ECDHE_RSA_WITH_RC4_128_SHA, 3247 MBEDTLS_TLS_ECDHE_PSK_WITH_RC4_128_SHA, 3248 MBEDTLS_TLS_ECDH_RSA_WITH_RC4_128_SHA, 3249 MBEDTLS_TLS_ECDH_ECDSA_WITH_RC4_128_SHA, 3250 MBEDTLS_TLS_RSA_PSK_WITH_RC4_128_SHA 3251 }; 3252 #endif 3253 3254 #ifdef MBEDTLS_SSL_PROTO_SSL3 3255 /* SSLv3 cipher list (supported in mbedtls) 3256 * marked with minimum version MBEDTLS_SSL_MINOR_VERSION_0 in 3257 * ciphersuite_definitions[] and then sorted by ciphersuite_preference[] 3258 * from mbedtls library/ssl_ciphersuites.c */ 3259 /* XXX: intentionally not including overlapping eNULL ciphers */ 3260 static const int suite_SSLv3[] = { 3261 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA, 3262 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA, 3263 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA, 3264 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA, 3265 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA, 3266 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA, 3267 MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA, 3268 MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA, 3269 MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA, 3270 MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA, 3271 MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA, 3272 MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA, 3273 MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, 3274 MBEDTLS_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA, 3275 MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA, 3276 MBEDTLS_TLS_PSK_WITH_3DES_EDE_CBC_SHA, 3277 MBEDTLS_TLS_DHE_PSK_WITH_RC4_128_SHA, 3278 MBEDTLS_TLS_RSA_WITH_RC4_128_SHA, 3279 MBEDTLS_TLS_RSA_WITH_RC4_128_MD5, 3280 MBEDTLS_TLS_PSK_WITH_RC4_128_SHA, 3281 MBEDTLS_TLS_DHE_RSA_WITH_DES_CBC_SHA, 3282 MBEDTLS_TLS_RSA_WITH_DES_CBC_SHA 3283 }; 3284 #endif 3285 3286 /* HIGH cipher list (mapped from openssl list to mbedtls) */ 3287 static const int suite_HIGH[] = { 3288 MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 3289 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, 3290 MBEDTLS_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256, 3291 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 3292 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 3293 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, 3294 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM, 3295 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM, 3296 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 3297 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 3298 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, 3299 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 3300 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 3301 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA, 3302 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8, 3303 MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM_8, 3304 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384, 3305 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384, 3306 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256, 3307 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA, 3308 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384, 3309 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384, 3310 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384, 3311 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 3312 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 3313 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, 3314 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM, 3315 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM, 3316 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 3317 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 3318 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, 3319 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 3320 MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 3321 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA, 3322 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, 3323 MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CCM_8, 3324 MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256, 3325 MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256, 3326 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256, 3327 MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA, 3328 MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256, 3329 MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256, 3330 MBEDTLS_TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256, 3331 MBEDTLS_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256, 3332 MBEDTLS_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256, 3333 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384, 3334 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM, 3335 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384, 3336 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384, 3337 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA, 3338 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA, 3339 MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384, 3340 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384, 3341 MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CCM_8, 3342 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384, 3343 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256, 3344 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM, 3345 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256, 3346 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256, 3347 MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA, 3348 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA, 3349 MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256, 3350 MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256, 3351 MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CCM_8, 3352 MBEDTLS_TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256, 3353 MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384, 3354 MBEDTLS_TLS_RSA_WITH_AES_256_CCM, 3355 MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256, 3356 MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA, 3357 MBEDTLS_TLS_RSA_WITH_AES_256_CCM_8, 3358 MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256, 3359 MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA, 3360 MBEDTLS_TLS_RSA_WITH_ARIA_256_GCM_SHA384, 3361 MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256, 3362 MBEDTLS_TLS_RSA_WITH_AES_128_CCM, 3363 MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256, 3364 MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA, 3365 MBEDTLS_TLS_RSA_WITH_AES_128_CCM_8, 3366 MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256, 3367 MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA, 3368 MBEDTLS_TLS_RSA_WITH_ARIA_128_GCM_SHA256, 3369 MBEDTLS_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256, 3370 MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384, 3371 MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384, 3372 MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA, 3373 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384, 3374 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384, 3375 MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256, 3376 MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256, 3377 MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA, 3378 MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256, 3379 MBEDTLS_TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256, 3380 MBEDTLS_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256, 3381 MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384, 3382 MBEDTLS_TLS_PSK_WITH_AES_256_CCM, 3383 MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384, 3384 MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA, 3385 MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384, 3386 MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8, 3387 MBEDTLS_TLS_PSK_WITH_ARIA_256_GCM_SHA384, 3388 MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256, 3389 MBEDTLS_TLS_PSK_WITH_AES_128_CCM, 3390 MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256, 3391 MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA, 3392 MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256, 3393 MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8, 3394 MBEDTLS_TLS_PSK_WITH_ARIA_128_GCM_SHA256, 3395 }; 3396 3397 3398 /* true if RC4 or weak or NULL cipher suite 3399 * (These ciphersuites are excluded from openssl "DEFAULT") 3400 * This is a subset of ciphers excluded for mod_openssl "!aNULL:!eNULL:!EXP" */ 3401 static int 3402 mod_mbedtls_ssl_is_weak_ciphersuite (int id) 3403 { 3404 #ifdef MBEDTLS_SSL_PROTO_TLS1 3405 for (uint32_t i = 0; i < sizeof(suite_RC4)/sizeof(suite_RC4[0]); ++i) { 3406 if (id == suite_RC4[i]) return 1; 3407 } 3408 #endif 3409 #ifdef MBEDTLS_SSL_PROTO_SSL3 3410 for (uint32_t i = 0; i < sizeof(suite_weak)/sizeof(suite_weak[0]); ++i) { 3411 if (id == suite_weak[i]) return 1; 3412 } 3413 #endif 3414 for (uint32_t i = 0; i < sizeof(suite_null)/sizeof(suite_null[0]); ++i) { 3415 if (id == suite_null[i]) return 1; 3416 } 3417 return 0; 3418 } 3419 3420 3421 static int 3422 mod_mbedtls_ssl_DEFAULT_ciphersuite (server *srv, int *ids, int nids, int idsz) 3423 { 3424 /* obtain default ciphersuite list and filter out weak or NULL */ 3425 const int *dids = mbedtls_ssl_list_ciphersuites(); 3426 int i = 0; 3427 while (dids[i] != 0) ++i; 3428 3429 if (i >= idsz - (nids + 1)) { 3430 log_error(srv->errh, __FILE__, __LINE__, 3431 "MTLS: error: too many ciphersuites during list expand"); 3432 return -1; 3433 } 3434 3435 for (i = 0; dids[i] != 0; ++i) { 3436 if (!mod_mbedtls_ssl_is_weak_ciphersuite(dids[i])) 3437 ids[++nids] = dids[i]; 3438 } 3439 3440 return nids; 3441 } 3442 3443 3444 static int 3445 mod_mbedtls_ssl_append_ciphersuite (server *srv, int *ids, int nids, int idsz, const int *x, int xsz) 3446 { 3447 if (xsz >= idsz - (nids + 1)) { 3448 log_error(srv->errh, __FILE__, __LINE__, 3449 "MTLS: error: too many ciphersuites during list expand"); 3450 return -1; 3451 } 3452 3453 for (int i = 0; i < xsz; ++i) 3454 ids[++nids] = x[i]; 3455 3456 return nids; 3457 } 3458 3459 3460 static int 3461 mod_mbedtls_ssl_conf_ciphersuites (server *srv, plugin_config_socket *s, buffer *ciphersuites, const buffer *cipherstring) 3462 { 3463 /* reference: https://www.openssl.org/docs/man1.1.1/man1/ciphers.html 3464 * Attempt to parse *some* keywords from Ciphersuites and CipherString 3465 * !!! openssl uses a *different* naming scheme than does mbedTLS !!! 3466 * Since Ciphersuites in openssl takes only TLSv1.3 suites, and mbedTLS 3467 * does not currently support TLSv1.3, mapping of those names is not 3468 * currently provided. Note that CipherString does allow cipher suites to 3469 * be listed, and this code does not currently attempt to provide mapping */ 3470 3471 char n[128]; /*(most ciphersuite names are about 40 chars)*/ 3472 int ids[512]; 3473 int nids = -1; 3474 const int idsz = (int)(sizeof(ids)/sizeof(*ids)-1); 3475 int crt_profile_default = 0; 3476 3477 if (ciphersuites) { 3478 buffer *b = ciphersuites; 3479 buffer_to_upper(b); /*(ciphersuites are all uppercase (currently))*/ 3480 for (const char *e = b->ptr-1; e; ) { 3481 const char * const p = e+1; 3482 e = strchr(p, ':'); 3483 size_t len = e ? (size_t)(e - p) : strlen(p); 3484 if (len >= sizeof(n)) { 3485 log_error(srv->errh, __FILE__, __LINE__, 3486 "MTLS: skipped ciphersuite; too long: %.*s", 3487 (int)len, p); 3488 continue; 3489 } 3490 memcpy(n, p, len); 3491 n[len] = '\0'; 3492 3493 int id = mbedtls_ssl_get_ciphersuite_id(n); 3494 if (0 == id) { 3495 log_error(srv->errh, __FILE__, __LINE__, 3496 "MTLS: skipped ciphersuite; not recognized: %.*s", 3497 (int)len, n); 3498 continue; 3499 } 3500 3501 /* allow any ciphersuite if explicitly listed, even weak or eNULL */ 3502 #if 0 3503 if (mod_mbedtls_ssl_is_weak_ciphersuite(id)) { 3504 log_error(srv->errh, __FILE__, __LINE__, 3505 "MTLS: skipped ciphersuite; weak or NULL suite: %.*s", 3506 (int)len, n); 3507 continue; 3508 } 3509 #endif 3510 3511 if (nids >= idsz) { 3512 log_error(srv->errh, __FILE__, __LINE__, 3513 "MTLS: skipped ciphersuite; too many listed: %.*s", 3514 (int)len, n); 3515 continue; 3516 } 3517 3518 ids[++nids] = id; 3519 } 3520 } 3521 3522 /* XXX: openssl config for CipherString=... is excessively complex. 3523 * If there is a need to enable specific ciphersuites, then that 3524 * can be accomplished with mod_mbedtls by specifying the list in 3525 * Ciphersuites=... in the ssl.openssl.ssl-conf-cmd directive. 3526 * (Alternatively, build mbedtls with specific set of cipher suites 3527 * or modify mod_mbedtls code to specify the precise list). 3528 * 3529 * The tokens parsed here are a quick attempt to handle a few cases 3530 * 3531 * XXX: not done: could make a list of ciphers with bitflag of attributes 3532 * to make future combining easier */ 3533 if (cipherstring && !buffer_is_blank(cipherstring)) { 3534 const buffer *b = cipherstring; 3535 const char *e = b->ptr; 3536 3537 /* XXX: not done: no checking for duplication of ciphersuites 3538 * even if tokens overlap or are repeated */ 3539 3540 /* XXX: not done: might walk string and build up exclude list of !xxxxx 3541 * ciphersuites and walk string again, excluding as result list built */ 3542 3543 /* manually handle first token, since one-offs apply */ 3544 /* (openssl syntax NOT fully supported) */ 3545 int default_suite = 0; 3546 #define strncmp_const(s,cs) strncmp((s),(cs),sizeof(cs)-1) 3547 if (0 == strncmp_const(e, "!ALL") || 0 == strncmp_const(e, "-ALL")) { 3548 /* "!ALL" excluding all ciphers does not make sense; ignore */ 3549 e += sizeof("!ALL")-1; /* same as sizeof("-ALL")-1 */ 3550 } 3551 else if (0 == strncmp_const(e, "!DEFAULT") 3552 || 0 == strncmp_const(e, "-DEFAULT")) { 3553 /* "!DEFAULT" excluding default ciphers is empty list; no effect */ 3554 e += sizeof("!DEFAULT")-1; /* same as sizeof("-DEFAULT")-1 */ 3555 } 3556 else if (0 == strncmp_const(e, "DEFAULT")) { 3557 e += sizeof("DEFAULT")-1; 3558 default_suite = 1; 3559 } 3560 else if (0 == /* effectively the same as "DEFAULT" */ 3561 strncmp_const(e, "ALL:!COMPLEMENTOFDEFAULT:!eNULL")) { 3562 e += sizeof("ALL:!COMPLEMENTOFDEFAULT:!eNULL")-1; 3563 default_suite = 1; 3564 } 3565 else if (0 == strncmp_const(e, "SUITEB128") 3566 || 0 == strncmp_const(e, "SUITEB128ONLY") 3567 || 0 == strncmp_const(e, "SUITEB192")) { 3568 mbedtls_ssl_conf_cert_profile(s->ssl_ctx, 3569 &mbedtls_x509_crt_profile_suiteb); 3570 /* re-initialize mbedtls_ssl_config defaults */ 3571 #if MBEDTLS_VERSION_NUMBER < 0x03020000 /* mbedtls 3.02.0 */ 3572 mbedtls_mpi_free(&s->ssl_ctx->MBEDTLS_PRIVATE(dhm_P)); 3573 mbedtls_mpi_free(&s->ssl_ctx->MBEDTLS_PRIVATE(dhm_G)); 3574 #endif 3575 int rc = mbedtls_ssl_config_defaults(s->ssl_ctx, 3576 MBEDTLS_SSL_IS_SERVER, 3577 MBEDTLS_SSL_TRANSPORT_STREAM, 3578 MBEDTLS_SSL_PRESET_SUITEB); 3579 if (0 != rc) { 3580 elog(srv->errh, __FILE__,__LINE__, rc, 3581 "Init of ssl config context SUITEB defaults failed"); 3582 return 0; 3583 } 3584 e += (0 == strncmp_const(e, "SUITEB128ONLY")) 3585 ? sizeof("SUITEB128ONLY")-1 3586 : sizeof("SUITEB128")-1; 3587 if (*e) 3588 log_error(srv->errh, __FILE__, __LINE__, 3589 "MTLS: ignoring cipher string after SUITEB: %s", e); 3590 return 1; 3591 } 3592 else if (0 == strncmp_const(e, 3593 "ECDHE+AESGCM:ECDHE+AES256:CHACHA20:!SHA1:!SHA256:!SHA384") 3594 || 0 == strncmp_const(e, 3595 "EECDH+AESGCM:AES256+EECDH:CHACHA20:!SHA1:!SHA256:!SHA384")) { 3596 e += sizeof( 3597 "EECDH+AESGCM:AES256+EECDH:CHACHA20:!SHA1:!SHA256:!SHA384")-1; 3598 if (nids + 9 >= idsz) { 3599 log_error(srv->errh, __FILE__, __LINE__, 3600 "MTLS: error: too many ciphersuites during list expand"); 3601 return 0; 3602 } 3603 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384; 3604 ids[++nids] = MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384; 3605 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256; 3606 ids[++nids] = MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256; 3607 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM; 3608 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8; 3609 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256; 3610 ids[++nids] = MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256; 3611 ids[++nids] = MBEDTLS_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256; 3612 } 3613 3614 if (e != b->ptr && *e != ':' && *e != '\0') { 3615 log_error(srv->errh, __FILE__, __LINE__, 3616 "MTLS: error: missing support for cipher list: %s", b->ptr); 3617 return 0; 3618 } 3619 3620 if (default_suite) { 3621 crt_profile_default = 1; 3622 nids = 3623 mod_mbedtls_ssl_DEFAULT_ciphersuite(srv, ids, nids, idsz); 3624 if (-1 == nids) return 0; 3625 } 3626 3627 /* not handled: "ALL" is "DEFAULT" and "RC4" */ 3628 /* not handled: "COMPLEMENTOFALL" is "eNULL" */ 3629 3630 int rc = 1; 3631 if (e == b->ptr || *e == '\0') --e; /*initial condition for loop below*/ 3632 do { 3633 const char * const p = e+1; 3634 e = strchr(p, ':'); 3635 size_t len = e ? (size_t)(e - p) : strlen(p); 3636 if (0 == len) continue; 3637 if (len >= sizeof(n)) { 3638 log_error(srv->errh, __FILE__, __LINE__, 3639 "MTLS: skipped ciphersuite; too long: %.*s", 3640 (int)len, p); 3641 continue; 3642 } 3643 char c = (*p == '!' || *p == '-' || *p == '+') ? *p : 0; 3644 size_t nlen = c ? len-1 : len; 3645 memcpy(n, c ? p+1 : p, nlen); 3646 n[nlen] = '\0'; 3647 3648 /* not handled: !xxxxx -xxxxx and most +xxxxx */ 3649 if (c) { 3650 log_error(srv->errh, __FILE__, __LINE__, 3651 "MTLS: error: missing support for cipher list: %s", b->ptr); 3652 } 3653 3654 /* ignore @STRENGTH sorting and ignore @SECLEVEL=n */ 3655 char *a = strchr(n, '@'); 3656 if (a) { 3657 log_error(srv->errh, __FILE__, __LINE__, 3658 "MTLS: ignored %s in %.*s", a, (int)len, p); 3659 *a = '\0'; 3660 nlen = (size_t)(a - n); 3661 } 3662 3663 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("TLSv1.2"))) { 3664 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3665 suite_TLSv12, 3666 (int)(sizeof(suite_TLSv12)/sizeof(*suite_TLSv12))); 3667 if (-1 == nids) return 0; 3668 continue; 3669 } 3670 3671 #ifdef MBEDTLS_SSL_PROTO_TLS1 3672 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("TLSv1.0"))) { 3673 crt_profile_default = 1; 3674 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3675 suite_TLSv10, 3676 (int)(sizeof(suite_TLSv10)/sizeof(*suite_TLSv10))); 3677 if (-1 == nids) return 0; 3678 continue; 3679 } 3680 #endif 3681 3682 #ifdef MBEDTLS_SSL_PROTO_SSL3 3683 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("SSLv3"))) { 3684 crt_profile_default = 1; 3685 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3686 suite_SSLv3, 3687 (int)(sizeof(suite_SSLv3)/sizeof(*suite_SSLv3))); 3688 if (-1 == nids) return 0; 3689 continue; 3690 } 3691 #endif 3692 3693 /* handle a popular recommendations 3694 * ssl.cipher-list = "EECDH+AESGCM:EDH+AESGCM" 3695 * ssl.cipher-list = "AES256+EECDH:AES256+EDH" 3696 * which uses AES hardware acceleration built into popular CPUs */ 3697 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("ECDHE+AESGCM")) 3698 || buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("EECDH+AESGCM"))) { 3699 if (nids + 4 >= idsz) { 3700 log_error(srv->errh, __FILE__, __LINE__, 3701 "MTLS: error: too many ciphersuites during list expand"); 3702 return 0; 3703 } 3704 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384; 3705 ids[++nids] = MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384; 3706 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256; 3707 ids[++nids] = MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256; 3708 continue; 3709 } 3710 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("DHE+AESGCM")) 3711 || buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("EDH+AESGCM"))) { 3712 if (nids + 2 >= idsz) { 3713 log_error(srv->errh, __FILE__, __LINE__, 3714 "MTLS: error: too many ciphersuites during list expand"); 3715 return 0; 3716 } 3717 ids[++nids] = MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384; 3718 ids[++nids] = MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256; 3719 continue; 3720 } 3721 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("AES256+EECDH"))) { 3722 if (nids + 8 >= idsz) { 3723 log_error(srv->errh, __FILE__, __LINE__, 3724 "MTLS: error: too many ciphersuites during list expand"); 3725 return 0; 3726 } 3727 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384; 3728 ids[++nids] = MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384; 3729 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM; 3730 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384; 3731 ids[++nids] = MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384; 3732 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA; 3733 ids[++nids] = MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA; 3734 ids[++nids] = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8; 3735 continue; 3736 } 3737 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("AES256+EDH"))) { 3738 if (nids + 5 >= idsz) { 3739 log_error(srv->errh, __FILE__, __LINE__, 3740 "MTLS: error: too many ciphersuites during list expand"); 3741 return 0; 3742 } 3743 ids[++nids] = MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384; 3744 ids[++nids] = MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM; 3745 ids[++nids] = MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256; 3746 ids[++nids] = MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA; 3747 ids[++nids] = MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CCM_8; 3748 continue; 3749 } 3750 3751 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("HIGH"))) { 3752 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3753 suite_HIGH, 3754 (int)(sizeof(suite_HIGH)/sizeof(*suite_HIGH))); 3755 if (-1 == nids) return 0; 3756 continue; 3757 } 3758 3759 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("AES256")) 3760 || buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("AES"))) { 3761 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3762 suite_AES_256_ephemeral, 3763 (int)(sizeof(suite_AES_256_ephemeral) 3764 /sizeof(*suite_AES_256_ephemeral))); 3765 if (-1 == nids) return 0; 3766 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3767 suite_AES_256, 3768 (int)(sizeof(suite_AES_256)/sizeof(*suite_AES_256))); 3769 if (-1 == nids) return 0; 3770 /* XXX: not done: AES256 PSK suites */ 3771 if (nlen == sizeof("AES256")-1) continue; 3772 } 3773 3774 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("AES128")) 3775 || buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("AES"))) { 3776 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3777 suite_AES_128_ephemeral, 3778 (int)(sizeof(suite_AES_128_ephemeral) 3779 /sizeof(*suite_AES_128_ephemeral))); 3780 if (-1 == nids) return 0; 3781 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3782 suite_AES_128, 3783 (int)(sizeof(suite_AES_128)/sizeof(*suite_AES_128))); 3784 if (-1 == nids) return 0; 3785 /* XXX: not done: AES128 PSK suites */ 3786 continue; 3787 } 3788 3789 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("CAMELLIA256")) 3790 || buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("CAMELLIA"))) { 3791 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3792 suite_CAMELLIA_256_ephemeral, 3793 (int)(sizeof(suite_CAMELLIA_256_ephemeral) 3794 /sizeof(*suite_CAMELLIA_256_ephemeral))); 3795 if (-1 == nids) return 0; 3796 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3797 suite_CAMELLIA_256, 3798 (int)(sizeof(suite_CAMELLIA_256) 3799 /sizeof(*suite_CAMELLIA_256))); 3800 if (-1 == nids) return 0; 3801 /* XXX: not done: CAMELLIA256 PSK suites */ 3802 if (nlen == sizeof("CAMELLIA256")-1) continue; 3803 } 3804 3805 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("CAMELLIA128")) 3806 || buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("CAMELLIA"))) { 3807 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3808 suite_CAMELLIA_128_ephemeral, 3809 (int)(sizeof(suite_CAMELLIA_128_ephemeral) 3810 /sizeof(*suite_CAMELLIA_128_ephemeral))); 3811 if (-1 == nids) return 0; 3812 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3813 suite_CAMELLIA_128, 3814 (int)(sizeof(suite_CAMELLIA_128) 3815 /sizeof(*suite_CAMELLIA_128))); 3816 if (-1 == nids) return 0; 3817 /* XXX: not done: CAMELLIA128 PSK suites */ 3818 continue; 3819 } 3820 3821 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("ARIA256")) 3822 || buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("ARIA"))) { 3823 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3824 suite_ARIA_256_ephemeral, 3825 (int)(sizeof(suite_ARIA_256_ephemeral) 3826 /sizeof(*suite_ARIA_256_ephemeral))); 3827 if (-1 == nids) return 0; 3828 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3829 suite_ARIA_256, 3830 (int)(sizeof(suite_ARIA_256)/sizeof(*suite_ARIA_256))); 3831 if (-1 == nids) return 0; 3832 /* XXX: not done: ARIA256 PSK suites */ 3833 if (nlen == sizeof("ARIA256")-1) continue; 3834 } 3835 3836 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("ARIA128")) 3837 || buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("ARIA"))) { 3838 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3839 suite_ARIA_128_ephemeral, 3840 (int)(sizeof(suite_ARIA_128_ephemeral) 3841 /sizeof(*suite_ARIA_128_ephemeral))); 3842 if (-1 == nids) return 0; 3843 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3844 suite_ARIA_128, 3845 (int)(sizeof(suite_ARIA_128)/sizeof(*suite_ARIA_128))); 3846 if (-1 == nids) return 0; 3847 /* XXX: not done: ARIA128 PSK suites */ 3848 continue; 3849 } 3850 3851 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("CHACHA20"))) { 3852 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3853 suite_CHACHAPOLY_ephemeral, 3854 (int)(sizeof(suite_CHACHAPOLY_ephemeral) 3855 /sizeof(*suite_CHACHAPOLY_ephemeral))); 3856 if (-1 == nids) return 0; 3857 /* XXX: not done: CHACHA20 PSK suites */ 3858 continue; 3859 } 3860 3861 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("PSK"))) { 3862 /* XXX: intentionally not including overlapping eNULL ciphers */ 3863 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3864 suite_PSK_ephemeral, 3865 (int)(sizeof(suite_PSK_ephemeral) 3866 /sizeof(*suite_PSK_ephemeral))); 3867 if (-1 == nids) return 0; 3868 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3869 suite_RSA_PSK, 3870 (int)(sizeof(suite_RSA_PSK)/sizeof(*suite_RSA_PSK))); 3871 if (-1 == nids) return 0; 3872 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3873 suite_PSK, 3874 (int)(sizeof(suite_PSK)/sizeof(*suite_PSK))); 3875 if (-1 == nids) return 0; 3876 continue; 3877 } 3878 3879 #ifdef MBEDTLS_SSL_PROTO_TLS1 3880 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("3DES"))) { 3881 crt_profile_default = 1; 3882 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3883 suite_3DES, 3884 (int)(sizeof(suite_3DES)/sizeof(*suite_3DES))); 3885 if (-1 == nids) return 0; 3886 continue; 3887 } 3888 #endif 3889 3890 #ifdef MBEDTLS_SSL_PROTO_TLS1 3891 /* not recommended, but permitted if explicitly requested */ 3892 /* "RC4" is same as openssl "COMPLEMENTOFALL" */ 3893 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("RC4"))) { 3894 crt_profile_default = 1; 3895 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3896 suite_RC4, 3897 (int)(sizeof(suite_RC4)/sizeof(*suite_RC4))); 3898 if (-1 == nids) return 0; 3899 continue; 3900 } 3901 #endif 3902 3903 /* not recommended, but permitted if explicitly requested */ 3904 if (buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("NULL")) 3905 || buffer_eq_icase_ss(n, nlen, CONST_STR_LEN("eNULL"))) { 3906 crt_profile_default = 1; 3907 nids = mod_mbedtls_ssl_append_ciphersuite(srv, ids, nids, idsz, 3908 suite_null, 3909 (int)(sizeof(suite_null)/sizeof(*suite_null))); 3910 if (-1 == nids) return 0; 3911 continue; 3912 } 3913 3914 { 3915 log_error(srv->errh, __FILE__, __LINE__, 3916 "MTLS: error: missing support for cipher list: %.*s", 3917 (int)len, p); 3918 rc = 0; 3919 continue; 3920 } 3921 } while (e); 3922 if (0 == rc) return 0; 3923 } 3924 3925 if (-1 == nids) { 3926 /* Do not set a default if ssl.cipher-list was set (and we are 3927 * are processing ssl.openssl.ssl-conf-cmd, not ssl.cipher-list) */ 3928 if (cipherstring != s->ssl_cipher_list && s->ssl_cipher_list) 3929 return 1; 3930 3931 /* obtain default ciphersuite list and filter out RC4, weak, and NULL */ 3932 nids = 3933 mod_mbedtls_ssl_DEFAULT_ciphersuite(srv, ids, nids, 3934 sizeof(ids)/sizeof(*ids)); 3935 if (-1 == nids) return 0; 3936 } 3937 3938 if (nids >= idsz) { 3939 log_error(srv->errh, __FILE__, __LINE__, 3940 "MTLS: error: too many ciphersuites during list expand"); 3941 return 0; 3942 } 3943 ids[++nids] = 0; /* terminate list */ 3944 ++nids; 3945 3946 if (!crt_profile_default) 3947 mbedtls_ssl_conf_cert_profile(s->ssl_ctx, 3948 &mbedtls_x509_crt_profile_next); 3949 3950 /* ciphersuites list must be persistent for lifetime of mbedtls_ssl_config*/ 3951 s->ciphersuites = malloc(nids * sizeof(int)); 3952 force_assert(s->ciphersuites); 3953 memcpy(s->ciphersuites, ids, nids * sizeof(int)); 3954 3955 mbedtls_ssl_conf_ciphersuites(s->ssl_ctx, s->ciphersuites); 3956 return 1; 3957 } 3958 3959 3960 #if MBEDTLS_VERSION_NUMBER < 0x03010000 /* mbedtls 3.01.0 */ 3961 static int 3962 mod_mbedtls_ssl_append_curve (server *srv, mbedtls_ecp_group_id *ids, int nids, int idsz, const mbedtls_ecp_group_id id) 3963 { 3964 if (1 >= idsz - (nids + 1)) { 3965 log_error(srv->errh, __FILE__, __LINE__, 3966 "MTLS: error: too many curves during list expand"); 3967 return -1; 3968 } 3969 ids[++nids] = id; 3970 return nids; 3971 } 3972 3973 3974 static int 3975 mod_mbedtls_ssl_conf_curves(server *srv, plugin_config_socket *s, const buffer *curvelist) 3976 { 3977 mbedtls_ecp_group_id ids[512]; 3978 int nids = -1; 3979 const int idsz = (int)(sizeof(ids)/sizeof(*ids)-1); 3980 const mbedtls_ecp_curve_info * const curve_info = mbedtls_ecp_curve_list(); 3981 3982 const buffer * const b = curvelist; 3983 for (const char *e = b->ptr-1; e; ) { 3984 const char * const n = e+1; 3985 e = strchr(n, ':'); 3986 size_t len = e ? (size_t)(e - n) : strlen(n); 3987 /* similar to mbedtls_ecp_curve_info_from_name() */ 3988 const mbedtls_ecp_curve_info *info; 3989 for (info = curve_info; info->grp_id != MBEDTLS_ECP_DP_NONE; ++info) { 3990 if (0 == strncmp(info->name, n, len) && info->name[len] == '\0') 3991 break; 3992 } 3993 if (info->grp_id == MBEDTLS_ECP_DP_NONE) { 3994 log_error(srv->errh, __FILE__, __LINE__, 3995 "MTLS: unrecognized curve: %.*s; ignored", (int)len, n); 3996 continue; 3997 } 3998 3999 nids = mod_mbedtls_ssl_append_curve(srv, ids, nids, idsz, info->grp_id); 4000 if (-1 == nids) return 0; 4001 } 4002 4003 /* XXX: mod_openssl configures "prime256v1" if curve list not specified, 4004 * but mbedtls provides a list of supported curves if not explicitly set */ 4005 if (-1 == nids) return 1; /* empty list; no-op */ 4006 4007 ids[++nids] = MBEDTLS_ECP_DP_NONE; /* terminate list */ 4008 ++nids; 4009 4010 /* curves list must be persistent for lifetime of mbedtls_ssl_config */ 4011 s->curves = malloc(nids * sizeof(mbedtls_ecp_group_id)); 4012 force_assert(s->curves); 4013 memcpy(s->curves, ids, nids * sizeof(mbedtls_ecp_group_id)); 4014 4015 mbedtls_ssl_conf_curves(s->ssl_ctx, s->curves); 4016 return 1; 4017 } 4018 #else 4019 static int 4020 mod_mbedtls_ssl_append_curve (server *srv, uint16_t *ids, int nids, int idsz, const uint16_t id) 4021 { 4022 if (1 >= idsz - (nids + 1)) { 4023 log_error(srv->errh, __FILE__, __LINE__, 4024 "MTLS: error: too many curves during list expand"); 4025 return -1; 4026 } 4027 ids[++nids] = id; 4028 return nids; 4029 } 4030 4031 4032 static int 4033 mod_mbedtls_ssl_conf_curves(server *srv, plugin_config_socket *s, const buffer *curvelist) 4034 { 4035 uint16_t ids[512]; 4036 int nids = -1; 4037 const int idsz = (int)(sizeof(ids)/sizeof(*ids)-1); 4038 const mbedtls_ecp_curve_info * const curve_info = mbedtls_ecp_curve_list(); 4039 4040 const buffer * const b = curvelist; 4041 for (const char *e = b->ptr-1; e; ) { 4042 const char * const n = e+1; 4043 e = strchr(n, ':'); 4044 size_t len = e ? (size_t)(e - n) : strlen(n); 4045 /* similar to mbedtls_ecp_curve_info_from_name() */ 4046 const mbedtls_ecp_curve_info *info; 4047 for (info = curve_info; info->tls_id != 0; ++info) { 4048 if (0 == strncmp(info->name, n, len) && info->name[len] == '\0') 4049 break; 4050 } 4051 if (info->tls_id == 0) { 4052 log_error(srv->errh, __FILE__, __LINE__, 4053 "MTLS: unrecognized curve: %.*s; ignored", (int)len, n); 4054 continue; 4055 } 4056 4057 nids = mod_mbedtls_ssl_append_curve(srv, ids, nids, idsz, info->tls_id); 4058 if (-1 == nids) return 0; 4059 } 4060 4061 /* XXX: mod_openssl configures "prime256v1" if curve list not specified, 4062 * but mbedtls provides a list of supported curves if not explicitly set */ 4063 if (-1 == nids) return 1; /* empty list; no-op */ 4064 4065 ids[++nids] = 0; /* terminate list */ 4066 ++nids; 4067 4068 /* curves list must be persistent for lifetime of mbedtls_ssl_config */ 4069 s->curves = malloc(nids * sizeof(uint16_t)); 4070 force_assert(s->curves); 4071 memcpy(s->curves, ids, nids * sizeof(uint16_t)); 4072 4073 mbedtls_ssl_conf_groups(s->ssl_ctx, s->curves); 4074 return 1; 4075 } 4076 #endif /* MBEDTLS_VERSION_NUMBER >= 0x03010000 */ /* mbedtls 3.01.0 */ 4077 4078 4079 static void 4080 mod_mbedtls_ssl_conf_proto (server *srv, plugin_config_socket *s, const buffer *b, int max) 4081 { 4082 int v = MBEDTLS_SSL_MINOR_VERSION_3; /* default: TLS v1.2 */ 4083 if (NULL == b) /* default: min TLSv1.2, max TLSv1.3 */ 4084 #ifdef MBEDTLS_SSL_MINOR_VERSION_4 4085 v = max ? MBEDTLS_SSL_MINOR_VERSION_4 : MBEDTLS_SSL_MINOR_VERSION_3; 4086 #else 4087 v = MBEDTLS_SSL_MINOR_VERSION_3; 4088 #endif 4089 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("None"))) /*"disable" limit*/ 4090 v = max 4091 ? 4092 #ifdef MBEDTLS_SSL_MINOR_VERSION_4 4093 MBEDTLS_SSL_MINOR_VERSION_4 /* TLS v1.3 */ 4094 #else 4095 MBEDTLS_SSL_MINOR_VERSION_3 /* TLS v1.2 */ 4096 #endif 4097 : 4098 #if defined(MBEDTLS_SSL_MINOR_VERSION_0) \ 4099 || defined(MBEDTLS_SSL_MINOR_VERSION_1) 4100 s->ssl_use_sslv3 4101 ? 4102 #ifdef MBEDTLS_SSL_MINOR_VERSION_0 4103 MBEDTLS_SSL_MINOR_VERSION_0 /* SSL v3.0 */ 4104 #else 4105 MBEDTLS_SSL_MINOR_VERSION_1 /* TLS v1.0 */ 4106 #endif 4107 : MBEDTLS_SSL_MINOR_VERSION_1 /* TLS v1.0 */ 4108 #elif defined(MBEDTLS_SSL_MINOR_VERSION_2) 4109 MBEDTLS_SSL_MINOR_VERSION_2 /* TLS v1.1 */ 4110 #else 4111 MBEDTLS_SSL_MINOR_VERSION_3 /* TLS v1.2 */ 4112 #endif 4113 ; 4114 #ifdef MBEDTLS_SSL_MINOR_VERSION_0 4115 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("SSLv3"))) 4116 v = MBEDTLS_SSL_MINOR_VERSION_0; /* SSL v3.0 */ 4117 #endif 4118 #ifdef MBEDTLS_SSL_MINOR_VERSION_1 4119 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("TLSv1.0"))) 4120 v = MBEDTLS_SSL_MINOR_VERSION_1; /* TLS v1.0 */ 4121 #endif 4122 #ifdef MBEDTLS_SSL_MINOR_VERSION_2 4123 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("TLSv1.1"))) 4124 v = MBEDTLS_SSL_MINOR_VERSION_2; /* TLS v1.1 */ 4125 #endif 4126 #ifdef MBEDTLS_SSL_MINOR_VERSION_3 4127 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("TLSv1.2"))) 4128 v = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS v1.2 */ 4129 #endif 4130 #ifdef MBEDTLS_SSL_MINOR_VERSION_4 4131 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("TLSv1.3"))) 4132 v = MBEDTLS_SSL_MINOR_VERSION_4; /* TLS v1.3 */ 4133 #endif 4134 else { 4135 #ifndef MBEDTLS_SSL_MINOR_VERSION_4 4136 if (buffer_eq_icase_slen(b, CONST_STR_LEN("TLSv1.3"))) 4137 log_error(srv->errh, __FILE__, __LINE__, 4138 "MTLS: ssl.openssl.ssl-conf-cmd %s TLSv1.3 not supported " 4139 "by mod_mbedtls; using TLSv1.2", 4140 max ? "MaxProtocol" : "MinProtocol"); 4141 else 4142 #endif 4143 if (buffer_eq_icase_slen(b, CONST_STR_LEN("DTLSv1")) 4144 || buffer_eq_icase_slen(b, CONST_STR_LEN("DTLSv1.2"))) { 4145 log_error(srv->errh, __FILE__, __LINE__, 4146 "MTLS: ssl.openssl.ssl-conf-cmd %s %s ignored", 4147 max ? "MaxProtocol" : "MinProtocol", b->ptr); 4148 return; 4149 } 4150 else { 4151 log_error(srv->errh, __FILE__, __LINE__, 4152 "MTLS: ssl.openssl.ssl-conf-cmd %s %s invalid; ignored", 4153 max ? "MaxProtocol" : "MinProtocol", b->ptr); 4154 return; 4155 } 4156 } 4157 4158 max 4159 ? mbedtls_ssl_conf_max_version(s->ssl_ctx,MBEDTLS_SSL_MAJOR_VERSION_3,v) 4160 : mbedtls_ssl_conf_min_version(s->ssl_ctx,MBEDTLS_SSL_MAJOR_VERSION_3,v); 4161 } 4162 4163 #if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.00.0 */ 4164 #ifdef MBEDTLS_SSL_SERVER_NAME_INDICATION 4165 #ifdef MBEDTLS_SSL_ALPN 4166 /* 4167 * XXX: forked from mbedtls 4168 * 4169 * ssl_parse_client_hello() is forked and modified from mbedtls 4170 * library/ssl_srv.c:ssl_parse_client_hello() 4171 * due to limitations in mbedtls hooks. Other than fetching input, ssl is not 4172 * modified here so that it can be reprocessed during handshake. 4173 * 4174 * It would be beneficial to have a callback after parsing client hello and all 4175 * extensions, and before certificate selection. (SNI extension might occur 4176 * prior to ALPN extension, and a different certificate may be needed by 4177 * ALPN "acme-tls/1".) Alternatively, mbedtls could provide an API to clear 4178 * &ssl->handshake->sni_key_cert, rather than forcing ssl_append_key_cert() with 4179 * no other option. 4180 */ 4181 static int ssl_parse_client_hello( mbedtls_ssl_context *ssl, handler_ctx *hctx ) 4182 { 4183 int ret; 4184 size_t msg_len; 4185 unsigned char *buf; 4186 4187 /* 4188 * If renegotiating, then the input was read with mbedtls_ssl_read_record(), 4189 * otherwise read it ourselves manually in order to support SSLv2 4190 * ClientHello, which doesn't use the same record layer format. 4191 */ 4192 #if defined(MBEDTLS_SSL_RENEGOTIATION) 4193 if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE ) 4194 #endif 4195 { 4196 if( ( ret = mbedtls_ssl_fetch_input( ssl, 5 ) ) != 0 ) 4197 { 4198 /* No alert on a read error. */ 4199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); 4200 return( ret ); 4201 } 4202 } 4203 4204 buf = ssl->in_hdr; 4205 4206 /* 4207 * TLS Client Hello 4208 * 4209 * Record layer: 4210 * 0 . 0 message type 4211 * 1 . 2 protocol version 4212 * 3 . 11 DTLS: epoch + record sequence number 4213 * 3 . 4 message length 4214 */ 4215 if( buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE ) 4216 { 4217 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); 4218 } 4219 4220 #if defined(MBEDTLS_SSL_PROTO_DTLS) 4221 /*(not supported in lighttpd for now)*/ 4222 /*(mbedtls_ssl_in_hdr_len() and mbedtls_ssl_hs_hdr_len() are in 4223 * mbedtls/ssl_internal.h but simple enough to repeat here) */ 4224 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) 4225 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); 4226 #endif /* MBEDTLS_SSL_PROTO_DTLS */ 4227 4228 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; 4229 4230 #if defined(MBEDTLS_SSL_RENEGOTIATION) 4231 if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) 4232 { 4233 /* Set by mbedtls_ssl_read_record() */ 4234 msg_len = ssl->in_hslen; 4235 } 4236 else 4237 #endif 4238 { 4239 if( msg_len > MBEDTLS_SSL_IN_CONTENT_LEN ) 4240 { 4241 return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER ); 4242 } 4243 4244 if( ( ret = mbedtls_ssl_fetch_input( ssl, 4245 5 /*mbedtls_ssl_in_hdr_len( ssl )*/ + msg_len ) ) != 0 ) 4246 { 4247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); 4248 return( ret ); 4249 } 4250 } 4251 4252 buf = ssl->in_msg; 4253 4254 /* 4255 * Handshake layer: 4256 * 0 . 0 handshake type 4257 * 1 . 3 handshake length 4258 * 4 . 5 DTLS only: message seqence number 4259 * 6 . 8 DTLS only: fragment offset 4260 * 9 . 11 DTLS only: fragment length 4261 */ 4262 if( msg_len < 4 /*mbedtls_ssl_hs_hdr_len( ssl )*/ ) 4263 { 4264 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4265 } 4266 4267 if( buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) 4268 { 4269 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); 4270 } 4271 4272 /* We don't support fragmentation of ClientHello (yet?) */ 4273 if( buf[1] != 0 || 4274 msg_len != 4u /*mbedtls_ssl_hs_hdr_len( ssl )*/ + ( ( buf[2] << 8 ) | buf[3] ) ) 4275 { 4276 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4277 } 4278 4279 buf += 4; /* mbedtls_ssl_hs_hdr_len( ssl ); */ 4280 msg_len -= 4; /* mbedtls_ssl_hs_hdr_len( ssl ); */ 4281 4282 /* 4283 * ClientHello layer: 4284 * 0 . 1 protocol version 4285 * 2 . 33 random bytes (starting with 4 bytes of Unix time) 4286 * 34 . 35 session id length (1 byte) 4287 * 35 . 34+x session id 4288 * 35+x . 35+x DTLS only: cookie length (1 byte) 4289 * 36+x . .. DTLS only: cookie 4290 * .. . .. ciphersuite list length (2 bytes) 4291 * .. . .. ciphersuite list 4292 * .. . .. compression alg. list length (1 byte) 4293 * .. . .. compression alg. list 4294 * .. . .. extensions length (2 bytes, optional) 4295 * .. . .. extensions (optional) 4296 */ 4297 4298 /* 4299 * Minimal length (with everything empty and extensions omitted) is 4300 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can 4301 * read at least up to session id length without worrying. 4302 */ 4303 if( msg_len < 38 ) 4304 { 4305 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4306 } 4307 4308 /* 4309 * Check and save the protocol version 4310 */ 4311 int major_ver, minor_ver; 4312 mbedtls_ssl_read_version( &major_ver, &minor_ver, 4313 ssl->conf->transport, buf ); 4314 4315 /* 4316 * Check the session ID length and save session ID 4317 */ 4318 const size_t sess_len = buf[34]; 4319 4320 if( sess_len > sizeof( ssl->session_negotiate->id ) || 4321 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */ 4322 { 4323 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4324 } 4325 4326 /* 4327 * Check the cookie length and content 4328 */ 4329 const size_t ciph_offset = 35 + sess_len; 4330 4331 const size_t ciph_len = ( buf[ciph_offset + 0] << 8 ) 4332 | ( buf[ciph_offset + 1] ); 4333 4334 if( ciph_len < 2 || 4335 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */ 4336 ( ciph_len % 2 ) != 0 ) 4337 { 4338 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4339 } 4340 4341 /* 4342 * Check the compression algorithms length and pick one 4343 */ 4344 const size_t comp_offset = ciph_offset + 2 + ciph_len; 4345 4346 const size_t comp_len = buf[comp_offset]; 4347 4348 if( comp_len < 1 || 4349 comp_len > 16 || 4350 comp_len + comp_offset + 1 > msg_len ) 4351 { 4352 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4353 } 4354 4355 /* Do not parse the extensions if the protocol is SSLv3 */ 4356 #if defined(MBEDTLS_SSL_PROTO_SSL3) 4357 if( ( major_ver != 3 ) || ( minor_ver != 0 ) ) 4358 { 4359 #endif 4360 /* 4361 * Check the extension length 4362 */ 4363 const size_t ext_offset = comp_offset + 1 + comp_len; 4364 size_t ext_len; 4365 if( msg_len > ext_offset ) 4366 { 4367 if( msg_len < ext_offset + 2 ) 4368 { 4369 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4370 } 4371 4372 ext_len = ( buf[ext_offset + 0] << 8 ) 4373 | ( buf[ext_offset + 1] ); 4374 4375 if( msg_len != ext_offset + 2 + ext_len ) 4376 { 4377 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4378 } 4379 } 4380 else 4381 ext_len = 0; 4382 4383 unsigned char *ext = buf + ext_offset + 2; 4384 4385 while( ext_len != 0 ) 4386 { 4387 unsigned int ext_id; 4388 unsigned int ext_size; 4389 if ( ext_len < 4 ) { 4390 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4391 } 4392 ext_id = ( ( ext[0] << 8 ) | ( ext[1] ) ); 4393 ext_size = ( ( ext[2] << 8 ) | ( ext[3] ) ); 4394 4395 if( ext_size + 4 > ext_len ) 4396 { 4397 return( MBEDTLS_ERR_SSL_DECODE_ERROR ); 4398 } 4399 switch( ext_id ) 4400 { 4401 #if defined(MBEDTLS_SSL_ALPN) 4402 case MBEDTLS_TLS_EXT_ALPN: 4403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) ); 4404 4405 /*(lighttpd-specific)*/ 4406 ret = mod_mbedtls_alpn_select_cb(hctx, ext + 4, ext_size); 4407 if( ret != 0 ) 4408 return( ret ); 4409 break; 4410 #endif /* MBEDTLS_SSL_ALPN */ 4411 4412 default: 4413 break; 4414 } 4415 4416 ext_len -= 4 + ext_size; 4417 ext += 4 + ext_size; 4418 } 4419 #if defined(MBEDTLS_SSL_PROTO_SSL3) 4420 } 4421 #endif 4422 4423 return( 0 ); 4424 } 4425 #endif /* MBEDTLS_SSL_ALPN */ 4426 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ 4427 #endif /* MBEDTLS_VERSION_NUMBER < 0x03000000 */ /* mbedtls 3.00.0 */ 4428