1 /* 2 * mod_nss - Network Security Services (NSS) 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 * Portions supporting mod_nss_ssl_conf_ciphersuites() (see end of file) 8 * Copyright 2001-2004 The Apache Software Foundation 9 */ 10 /* 11 * WARNING: EXPERIMENTAL code sketch; mod_nss is INCOMPLETE and UNTESTED 12 * 13 * NSS docs: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS 14 * 15 * NSS documentation is seriously lacking and man pages exist only for apps; 16 * be prepared to slog through organic piles of NSS library code 17 * 18 * Note: If session tickets are -not- disabled with 19 * ssl.openssl.ssl-conf-cmd = ("Options" => "-SessionTicket") 20 * NSS never rotates server ticket encryption key (STEK) while running. 21 * Therefore, if session tickets are enabled, lighttpd server should be 22 * restarted (by an external job) at least every 24 hours. Restarting 23 * lighttpd generates a new key that is shared by lighttpd workers. There 24 * is no mechanism implemented in lighttpd mod_nss to share STEK between 25 * independent lighttpd servers. ssl.stek-file is not used in mod_nss. 26 * 27 * NSS provides SSL_SetSessionTicketKeyPair(pubKey, privKey) to set RSA keys. 28 * However, to match behavior of other lighttpd TLS modules, it seems we want 29 * to set the private struct ssl_self_encrypt_keys in lib/ssl/sslsnce.c 30 * instead of the private struct ssl_self_encrypt_key_pair. 31 * sslSelfEncryptKeys ssl_self_encrypt_keys contains: 32 * PRUint8 keyName[SELF_ENCRYPT_KEY_NAME_LEN]; 33 * PK11SymKey *encKey; 34 * PK11SymKey *macKey; 35 * (see lib/ssl/sslsnce.c:GenerateSelfEncryptKeys()) 36 * PK11SymKey *masterSecret in ssl3CipherSpec in ssl3State in sslSessionID 37 * is private in lib/ssl/ssl3con.c 38 * 39 * XXX: due to limitations, consider disabling session tickets in mod_nss 40 * 41 * not implemented: 42 * - session ticket rotation (see comments above) 43 * - OCSP Must Staple detection 44 * - ssl.honor-cipher-order 45 * - ssl.verifyclient.depth 46 * - ssl.dh-file 47 * - ssl.ec-curve 48 * - ssl.openssl.ssl-conf-cmd Ciphersuite 49 * 50 * future: 51 * - consider SSL_AlertReceivedCallback() to set SSLAlertCallback 52 * in order to (optionally) log alerts, and to abort connection if fatal alert 53 * - consider using experimental API for cipher suite choice in lib/ssl/sslexp.h 54 * SSL_CipherSuiteOrderGet 55 * SSL_CipherSuiteOrderSet 56 * - detect CLOSE_NOTIFY from client 57 * - feature options 58 * - SSL_ENABLE_FALSE_START 59 * - SSL_ENABLE_DELEGATED_CREDENTIALS 60 * - SSL_ENABLE_0RTT_DATA 61 * - SSL_SUPPRESS_END_OF_EARLY_DATA 62 * - crypto option using FreeBL 63 * "A core element of NSS is FreeBL, a base library providing hash functions, 64 * big number calculations, and cryptographic algorithms." 65 * "Softoken is an NSS module that exposes most FreeBL functionality as a 66 * PKCS#11 module." 67 * "PR_GetRandomNoise - Produces a random value for use as a seed value for 68 * another random number generator." 69 */ 70 #include "first.h" 71 72 #include <sys/types.h> 73 #include <sys/stat.h> 74 #include <errno.h> 75 #include <fcntl.h> 76 #include <stdarg.h> 77 #include <stdlib.h> 78 #include <stdio.h> /* vsnprintf() */ 79 #include <string.h> 80 81 #if defined(__CYGWIN__) 82 #include <nspr/nspr.h> 83 #include <nspr/private/pprio.h> /* see mod_nss_io_ctor() comments */ 84 #include <nss/nss.h> 85 #include <nss/nssb64.h> 86 #include <nss/pk11pub.h> 87 #include <nss/secder.h> 88 #include <nss/secerr.h> 89 #include <nss/ssl.h> 90 #include <nss/sslproto.h> 91 #else 92 #include <nspr4/nspr.h> 93 #include <nspr4/private/pprio.h> /* see mod_nss_io_ctor() comments */ 94 #include <nss3/nss.h> 95 #include <nss3/nssb64.h> 96 #include <nss3/pk11pub.h> 97 #include <nss3/secder.h> 98 #include <nss3/secerr.h> 99 #include <nss3/ssl.h> 100 #include <nss3/sslproto.h> 101 #endif 102 103 #include "base.h" 104 #include "fdevent.h" 105 #include "http_header.h" 106 #include "http_kv.h" 107 #include "log.h" 108 #include "plugin.h" 109 #include "safe_memclear.h" 110 111 typedef struct { 112 /* SNI per host: with COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */ 113 char must_staple; 114 CERTCertificate *ssl_pemfile_x509; 115 SECKEYPrivateKey *ssl_pemfile_pkey; 116 SSLExtraServerCertData ssl_credex; 117 const buffer *ssl_stapling_file; 118 time_t ssl_stapling_loadts; 119 time_t ssl_stapling_nextts; 120 SECItemArray OCSPResponses; 121 SECItem OCSPResponse; 122 } plugin_cert; 123 124 typedef struct { 125 PRFileDesc *model; 126 SSLVersionRange protos; 127 PRBool ssl_compression; 128 int8_t ssl_session_ticket; 129 } plugin_ssl_ctx; 130 131 typedef struct { 132 plugin_cert *pc; 133 134 /*(used only during startup; not patched)*/ 135 unsigned char ssl_enabled; /* only interesting for setting up listening sockets. don't use at runtime */ 136 unsigned char ssl_honor_cipher_order; /* determine SSL cipher in server-preferred order, not client-order */ 137 unsigned char ssl_empty_fragments; 138 unsigned char ssl_use_sslv2; 139 unsigned char ssl_use_sslv3; 140 const buffer *ssl_cipher_list; 141 const buffer *ssl_ec_curve; 142 array *ssl_conf_cmd; 143 144 /*(copied from plugin_data for socket ssl_ctx config)*/ 145 unsigned char ssl_session_ticket; 146 unsigned char ssl_verifyclient; 147 unsigned char ssl_verifyclient_enforce; 148 unsigned char ssl_verifyclient_depth; 149 150 PRFileDesc *model; 151 SSLVersionRange protos; 152 PRBool ssl_compression; 153 } plugin_config_socket; /*(used at startup during configuration)*/ 154 155 typedef struct { 156 /* SNI per host: w/ COMP_SERVER_SOCKET, COMP_HTTP_SCHEME, COMP_HTTP_HOST */ 157 plugin_cert *pc; 158 CERTCertList *ssl_ca_file; 159 CERTCertList *ssl_ca_dn_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 } plugin_data; 178 179 static int ssl_is_init; 180 /* need assigned p->id for deep access of module handler_ctx for connection 181 * i.e. handler_ctx *hctx = con->plugin_ctx[plugin_data_singleton->id]; */ 182 static plugin_data *plugin_data_singleton; 183 #define LOCAL_SEND_BUFSIZE 16384 /* DEFAULT_MAX_RECORD_SIZE */ 184 static char *local_send_buffer; 185 186 typedef struct { 187 PRFileDesc *ssl; 188 request_st *r; 189 connection *con; 190 int8_t close_notify; 191 uint8_t alpn; 192 int8_t ssl_session_ticket; 193 int handshake; 194 size_t pending_write; 195 plugin_config conf; 196 int verify_status; 197 buffer *tmp_buf; 198 log_error_st *errh; 199 } handler_ctx; 200 201 202 static handler_ctx * 203 handler_ctx_init (void) 204 { 205 handler_ctx *hctx = calloc(1, sizeof(*hctx)); 206 force_assert(hctx); 207 return hctx; 208 } 209 210 211 static void mod_nss_io_dtor (PRFileDesc *ssl); 212 213 static void 214 handler_ctx_free (handler_ctx *hctx) 215 { 216 mod_nss_io_dtor(hctx->ssl); 217 free(hctx); 218 } 219 220 221 __attribute_cold__ 222 static void elog(log_error_st * const errh, 223 const char * const file, const int line, 224 const char * const msg) 225 { 226 /* error logging convenience function that decodes NSS result codes */ 227 const PRErrorCode rc = PR_GetError(); 228 const char *s = PR_ErrorToName(rc); 229 log_error(errh, file, line, "NSS: %s: (%s) %s", 230 msg, s ? s : "", PR_ErrorToString(rc, 0)); 231 } 232 233 234 __attribute_cold__ 235 __attribute_format__((__printf__, 4, 5)) 236 static void elogf(log_error_st * const errh, 237 const char * const file, const int line, 238 const char * const fmt, ...) 239 { 240 char msg[1024]; 241 va_list ap; 242 va_start(ap, fmt); 243 vsnprintf(msg, sizeof(msg), fmt, ap); 244 va_end(ap); 245 elog(errh, file, line, msg); 246 } 247 248 249 static PRFileDesc * 250 mod_nss_io_ctor (int fd, PRFileDesc *model, log_error_st *errh) 251 { 252 /* WTH? Why not a public PR_ImportTCPSocket() interface from NSPR? 253 * (and PR_GetInheritedFD() is not a great replacement interface to 254 * fudge NSPR_INHERIT_FDS in environment for each connection) 255 * 256 * #include <nspr4/private/pprio.h> 257 * Use internal routines to set up PRFileDesc. Perform actions underlying 258 * PR_ImportTCPSocket() to avoid excess work done by PR_ImportTCPSocket(), 259 * which includes closing the fd if there is a failure. Could pass 0 as fd 260 * to PR_AllocFileDesc() to avoid NSPR setting O_NONBLOCK since already set. 261 * Instead, employ simpler PR_CreateSocketPollFd() and change methods table, 262 * which handles _PR_ImplicitInitialization() (PR_AllocFileDesc() does not). 263 * (WTH?: PR_AllocFileDesc() has limit fd < FD_SETSIZE when XP_UNIX defined) 264 * Note: since bypassing PR_ImportTCPSocket() this might not work on Windows 265 * which expects prfd->secret->af to be set to AF_INET. 266 */ 267 #if defined(_WIN32) && !defined(__CYGWIN__) 268 PRFileDesc *prfd = PR_ImportTCPSocket(fd); 269 if (NULL == prfd) { 270 elog(errh, __FILE__, __LINE__, "PR_ImportTCPSocket()"); 271 return NULL; 272 } 273 #else 274 /*PRFileDesc *prfd = PR_AllocFileDesc(0, PR_GetTCPMethods());*/ 275 PRFileDesc *prfd = PR_CreateSocketPollFd(fd); 276 if (NULL == prfd) { 277 elog(errh, __FILE__, __LINE__, "PR_CreateSocketPollFd()"); 278 return NULL; 279 } 280 /*PR_ChangeFileDescNativeHandle(prfd, fd);*/ 281 prfd->methods = PR_GetTCPMethods(); 282 /*prfd->dtor = PR_FreeFileDesc();*/ /* PR_FreeFileDesc() is private */ 283 #endif 284 285 /* set prfd->secret->nonblocking flag */ 286 PRSocketOptionData data; 287 data.option = PR_SockOpt_Nonblocking; 288 data.value.non_blocking = PR_TRUE; 289 if (PR_SetSocketOption(prfd, &data) != PR_SUCCESS) { 290 elog(errh, __FILE__, __LINE__, "PR_SocketSetSocketOption()"); 291 PR_DestroySocketPollFd(prfd); /* PR_FreeFileDesc() is private */ 292 return NULL; 293 } 294 295 PRFileDesc *ssl = SSL_ImportFD(model, prfd); 296 if (NULL == ssl) { 297 elog(errh, __FILE__, __LINE__, "SSL_ImportFD()"); 298 PR_DestroySocketPollFd(prfd); /* PR_FreeFileDesc() is private */ 299 return NULL; 300 } 301 302 return ssl; 303 } 304 305 306 static void 307 mod_nss_io_detach (PRFileDesc *ssl) 308 { 309 #if 0 /* PR_PopIOLayer() forbids pop of PR_NSPR_IO_LAYER */ 310 PRFileDesc *prfd = PR_PopIOLayer(ssl, PR_NSPR_IO_LAYER); 311 if (prfd) { 312 PR_ChangeFileDescNativeHandle(prfd, -1); 313 PR_DestroySocketPollFd(prfd); /* PR_FreeFileDesc() is private */ 314 } 315 #else 316 /*(results in close(-1) and EBADF from PR_Close() in mod_nss_io_dtor())*/ 317 PRFileDesc *prfd = PR_GetIdentitiesLayer(ssl, PR_NSPR_IO_LAYER); 318 if (prfd) 319 PR_ChangeFileDescNativeHandle(prfd, -1); 320 #endif 321 } 322 323 324 static void 325 mod_nss_io_dtor (PRFileDesc *ssl) 326 { 327 if (NULL == ssl) return; 328 mod_nss_io_detach(ssl); 329 PR_Close(ssl); 330 } 331 332 333 static int 334 mod_nss_load_file (const char * const fn, SECItem * const d, log_error_st *errh) 335 { 336 off_t dlen = 512*1024*1024;/*(arbitrary limit: 512 MB file; expect < 1 MB)*/ 337 char *data = fdevent_load_file(fn, &dlen, errh, PORT_Alloc, PORT_Free); 338 if (NULL == data) return -1; 339 d->type = siBuffer; 340 d->data = (unsigned char *)data; 341 d->len = (unsigned int)dlen; 342 return 0; 343 } 344 345 346 static void 347 mod_nss_secitem_wipe (SECItem * const d) 348 { 349 /* safer than SECITEM_ZfreeItem() */ 350 if (NULL == d) return; 351 if (d->data) { 352 if (d->len) safe_memclear(d->data, d->len); /*safer than PORT_Memset()*/ 353 PORT_Free(d->data); /* safe_memclear() is safer than PORT_ZFree() */ 354 d->data = NULL; 355 } 356 d->len = 0; 357 } 358 359 360 INIT_FUNC(mod_nss_init) 361 { 362 plugin_data_singleton = (plugin_data *)calloc(1, sizeof(plugin_data)); 363 return plugin_data_singleton; 364 } 365 366 367 static int mod_nss_init_once_nss (void) 368 { 369 if (ssl_is_init) return 1; 370 ssl_is_init = 1; 371 372 /*PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);*//*implicit on first use*/ 373 374 if (!NSS_IsInitialized() && NSS_NoDB_Init(NULL) < 0) 375 return 0; 376 377 if (SSL_OptionSetDefault(SSL_ENABLE_SSL2, PR_FALSE) < 0) 378 return 0; 379 if (SSL_OptionSetDefault(SSL_ENABLE_SSL3, PR_FALSE) < 0) 380 return 0; 381 /* XXX: lighttpd is single threaded and so SSL_NO_LOCKS is desirable, 382 * but NSS crashes if SSL_NO_LOCKS option is set to PR_TRUE. 383 * (Crash in SSL3_SendAlert() call to PR_GetMonitorEntryCount() 384 * with NULL ptr to monitor (mon)) 385 * NSS lib/ssl/sslimpl.h macros such as ssl_HaveSSL3HandshakeLock(ss), 386 * plus some other .c files use macros without first checking if 387 * (!ss->opt.noLocks): PZ_InMonitor() PZ_InMonitor() PZ_InMonitor() */ 388 /*if (SSL_OptionSetDefault(SSL_NO_LOCKS, PR_TRUE) < 0)*/ 389 if (SSL_OptionSetDefault(SSL_NO_LOCKS, PR_FALSE) < 0) 390 return 0; 391 if (SSL_OptionSetDefault(SSL_NO_CACHE, PR_TRUE) < 0) 392 return 0; 393 if (SSL_OptionSetDefault(SSL_ENABLE_SESSION_TICKETS, PR_TRUE) < 0) 394 return 0; 395 if (SSL_OptionSetDefault(SSL_ENABLE_ALPN, PR_TRUE) < 0) 396 return 0; 397 if (SSL_OptionSetDefault(SSL_ENABLE_RENEGOTIATION, 398 SSL_RENEGOTIATE_NEVER) < 0) 399 return 0; 400 401 if (NSS_SetDomesticPolicy() < 0) 402 return 0; 403 404 local_send_buffer = malloc(LOCAL_SEND_BUFSIZE); 405 force_assert(NULL != local_send_buffer); 406 407 return 1; 408 } 409 410 411 static void mod_nss_free_nss (void) 412 { 413 if (!ssl_is_init) return; 414 415 NSS_Shutdown(); 416 417 free(local_send_buffer); 418 ssl_is_init = 0; 419 } 420 421 422 #define PEM_BEGIN "-----BEGIN " 423 #define PEM_END "-----END " 424 #define PEM_BEGIN_CERT "-----BEGIN CERTIFICATE-----" 425 #define PEM_END_CERT "-----END CERTIFICATE-----" 426 #define PEM_BEGIN_TRUSTED_CERT "-----BEGIN TRUSTED CERTIFICATE-----" 427 #define PEM_END_TRUSTED_CERT "-----END TRUSTED CERTIFICATE-----" 428 #define PEM_BEGIN_PKEY "-----BEGIN PRIVATE KEY-----" 429 #define PEM_END_PKEY "-----END PRIVATE KEY-----" 430 #define PEM_BEGIN_EC_PKEY "-----BEGIN EC PRIVATE KEY-----" 431 #define PEM_END_EC_PKEY "-----END EC PRIVATE KEY-----" 432 #define PEM_BEGIN_RSA_PKEY "-----BEGIN RSA PRIVATE KEY-----" 433 #define PEM_END_RSA_PKEY "-----END RSA PRIVATE KEY-----" 434 #define PEM_BEGIN_DSA_PKEY "-----BEGIN DSA PRIVATE KEY-----" 435 #define PEM_END_DSA_PKEY "-----END DSA PRIVATE KEY-----" 436 #define PEM_BEGIN_ANY_PKEY "-----BEGIN ANY PRIVATE KEY-----" 437 #define PEM_END_ANY_PKEY "-----END ANY PRIVATE KEY-----" 438 /* (not implemented: support to get password from user for encrypted key) */ 439 #define PEM_BEGIN_ENCRYPTED_PKEY "-----BEGIN ENCRYPTED PRIVATE KEY-----" 440 #define PEM_END_ENCRYPTED_PKEY "-----END ENCRYPTED PRIVATE KEY-----" 441 442 #define PEM_BEGIN_X509_CRL "-----BEGIN X509 CRL-----" 443 #define PEM_END_X509_CRL "-----END X509 CRL-----" 444 445 446 static CERTCertificateList * 447 mod_nss_load_pem_file (const char *fn, log_error_st *errh) 448 { 449 if (!mod_nss_init_once_nss()) return NULL; 450 451 SECItem f; 452 int rc = mod_nss_load_file(fn, &f, errh); 453 if (rc < 0) return NULL; 454 455 rc = -1; 456 CERTCertificateList *chain = NULL; 457 do { 458 int count = 0; 459 char *b = (char *)f.data; 460 for (; (b = strstr(b, PEM_BEGIN_CERT)); b += sizeof(PEM_BEGIN_CERT)-1) 461 ++count; 462 b = (char *)f.data; 463 for (; (b = strstr(b, PEM_BEGIN_TRUSTED_CERT)); 464 b += sizeof(PEM_BEGIN_TRUSTED_CERT)-1) 465 ++count; 466 if (0 == count) { 467 rc = 0; 468 break; 469 } 470 471 PLArenaPool *arena = PORT_NewArena(4096); 472 if (NULL == arena) 473 break; 474 475 chain = (CERTCertificateList *) 476 PORT_ArenaAlloc(arena, sizeof(CERTCertificateList)); 477 if (NULL == chain) { 478 PORT_FreeArena(arena, PR_FALSE); 479 break; 480 } 481 482 chain->arena = arena; 483 chain->len = count; 484 chain->certs = (SECItem *)PORT_ArenaAlloc(arena, count*sizeof(SECItem)); 485 if (NULL == chain->certs) 486 break; 487 488 int i = 0; 489 for (char *e = (char *)f.data; (b = strstr(e, PEM_BEGIN_CERT)); ++i) { 490 b += sizeof(PEM_BEGIN_CERT)-1; 491 if (*b == '\r') ++b; 492 if (*b == '\n') ++b; 493 e = strstr(b, PEM_END_CERT); 494 if (NULL == e) break; 495 uint32_t len = (uint32_t)(e - b); 496 e += sizeof(PEM_END_CERT)-1; 497 chain->certs[i].type = 0; 498 chain->certs[i].data = NULL; 499 chain->certs[i].len = 0; 500 if (NULL == NSSBase64_DecodeBuffer(arena, chain->certs+i, b, len)) 501 break; 502 } 503 for (char *e=(char *)f.data; (b=strstr(e,PEM_BEGIN_TRUSTED_CERT)); ++i){ 504 b += sizeof(PEM_BEGIN_TRUSTED_CERT)-1; 505 if (*b == '\r') ++b; 506 if (*b == '\n') ++b; 507 e = strstr(b, PEM_END_TRUSTED_CERT); 508 if (NULL == e) break; 509 uint32_t len = (uint32_t)(e - b); 510 e += sizeof(PEM_END_TRUSTED_CERT)-1; 511 chain->certs[i].type = 0; 512 chain->certs[i].data = NULL; 513 chain->certs[i].len = 0; 514 if (NULL == NSSBase64_DecodeBuffer(arena, chain->certs+i, b, len)) 515 break; 516 } 517 if (i == count) 518 rc = 0; 519 else 520 PORT_SetError(SEC_ERROR_IO); 521 } while (0); 522 523 mod_nss_secitem_wipe(&f); 524 525 if (rc < 0) { 526 elogf(errh, __FILE__, __LINE__, "error loading %s", fn); 527 if (chain) 528 CERT_DestroyCertificateList(chain); 529 return NULL; 530 } 531 532 return chain; 533 } 534 535 536 static CERTCertificate * 537 mod_nss_load_pem_crts (const char *fn, log_error_st *errh, CERTCertificateList **pchain) 538 { 539 *pchain = mod_nss_load_pem_file(fn, errh); 540 if (NULL == *pchain) return NULL; 541 542 CERTCertificate *cert = CERT_NewTempCertificate(NULL, (*pchain)->certs+0, 543 NULL, PR_FALSE, PR_TRUE); 544 if (NULL == cert) { 545 CERT_DestroyCertificateList(*pchain); 546 *pchain = NULL; 547 } 548 return cert; 549 } 550 551 static CERTCertList * 552 mod_nss_cert_list (CERTCertificateList *crts) 553 { 554 SECStatus rc = SECFailure; 555 CERTCertificate *cert = NULL; 556 CERTCertList *clist = CERT_NewCertList(); 557 if (NULL != clist) { 558 for (int i = 0; i < crts->len; ++i) { 559 cert = CERT_NewTempCertificate(NULL, crts->certs+i, 560 NULL, PR_FALSE, PR_TRUE); 561 if (NULL == cert) break; 562 rc = CERT_AddCertToListTail(clist, cert); 563 if (rc < 0) break; 564 } 565 } 566 567 if (rc < 0 || NULL == cert) { 568 if (cert) CERT_DestroyCertificate(cert); 569 if (clist) CERT_DestroyCertList(clist); 570 PORT_SetError(SEC_ERROR_NO_MEMORY); 571 return NULL; 572 } 573 574 return clist; 575 } 576 577 578 static CERTCertList * 579 mod_nss_load_config_crts (const char *fn, log_error_st *errh) 580 { 581 CERTCertificateList *crts = mod_nss_load_pem_file(fn, errh); 582 if (NULL == crts) return NULL; 583 584 CERTCertList *clist = NULL; 585 SECStatus rc = 586 CERT_ImportCAChainTrusted(crts->certs,crts->len,certUsageUserCertImport); 587 if (rc == SECSuccess) 588 clist = mod_nss_cert_list(crts); 589 else { 590 elogf(errh, __FILE__, __LINE__, "CERT_ImportCAChainTrusted() %s", fn); 591 CERT_DestroyCertificateList(crts); 592 return NULL; 593 } 594 595 CERT_DestroyCertificateList(crts); 596 return clist; 597 } 598 599 600 static CERTCertList * 601 mod_nss_load_config_dncrts (const char *fn, log_error_st *errh) 602 { 603 CERTCertificateList *crts = mod_nss_load_pem_file(fn, errh); 604 if (NULL == crts) return NULL; 605 606 CERTCertList *clist = mod_nss_cert_list(crts); 607 608 CERT_DestroyCertificateList(crts); 609 return clist; 610 } 611 612 613 static void 614 mod_nss_free_config_crls (CERTCertificateList *crls) 615 { 616 if (NULL == crls) return; 617 CERTCertDBHandle * const dbhandle = CERT_GetDefaultCertDB(); 618 for (int i = 0; i < crls->len; ++i) 619 CERT_UncacheCRL(dbhandle, crls->certs+i); 620 CERT_DestroyCertificateList(crls); 621 } 622 623 624 static CERTCertificateList * 625 mod_nss_load_config_crls (const char *fn, log_error_st *errh) 626 { 627 /*(similar start to other mod_nss_load_config_*())*/ 628 if (!mod_nss_init_once_nss()) return NULL; 629 630 SECItem f; 631 int rc = mod_nss_load_file(fn, &f, errh); 632 if (rc < 0) return NULL; 633 634 rc = -1; 635 CERTCertificateList *chain = NULL; 636 CERTCertDBHandle * const dbhandle = CERT_GetDefaultCertDB(); 637 do { 638 int count = 0; 639 char *b = (char *)f.data; 640 for (; (b = strstr(b, PEM_BEGIN_X509_CRL)); 641 b += sizeof(PEM_BEGIN_X509_CRL)-1) 642 ++count; 643 if (0 == count) { 644 rc = 0; 645 break; 646 } 647 648 PLArenaPool *arena = PORT_NewArena(4096); 649 if (NULL == arena) 650 break; 651 652 chain = (CERTCertificateList *) 653 PORT_ArenaAlloc(arena, sizeof(CERTCertificateList)); 654 if (NULL == chain) { 655 PORT_FreeArena(arena, PR_FALSE); 656 break; 657 } 658 659 chain->arena = arena; 660 chain->len = count; 661 chain->certs = (SECItem *)PORT_ArenaAlloc(arena, count*sizeof(SECItem)); 662 if (NULL == chain->certs) 663 break; 664 665 int i = 0; 666 for (char *e = (char *)f.data; (b = strstr(e,PEM_BEGIN_X509_CRL)); ++i){ 667 b += sizeof(PEM_BEGIN_X509_CRL)-1; 668 if (*b == '\r') ++b; 669 if (*b == '\n') ++b; 670 e = strstr(b, PEM_END_X509_CRL); 671 if (NULL == e) break; 672 uint32_t len = (uint32_t)(e - b); 673 e += sizeof(PEM_END_X509_CRL)-1; 674 chain->certs[i].type = 0; 675 chain->certs[i].data = NULL; 676 chain->certs[i].len = 0; 677 if (NULL == NSSBase64_DecodeBuffer(arena, chain->certs+i, b, len)) 678 break; 679 /* using ephemeral db, so cache CRL instead of CERT_ImportCRL() */ 680 if (CERT_CacheCRL(dbhandle, chain->certs+i) < 0) 681 break; 682 } 683 if (i == count) 684 rc = 0; 685 else 686 PORT_SetError(SEC_ERROR_IO); 687 } while (0); 688 689 mod_nss_secitem_wipe(&f); 690 691 if (rc < 0) { 692 elogf(errh, __FILE__, __LINE__, "error loading %s", fn); 693 if (chain) 694 CERT_DestroyCertificateList(chain); 695 return NULL; 696 } 697 698 return chain; 699 } 700 701 702 static SECItem * 703 mod_nss_cert_get_publicValue (SECKEYPublicKey *pubKey) 704 { 705 /*(lib/pkcs12/p12d.c:sec_pkcs12_get_public_value_and_type() private)*/ 706 /*(lib/pk11wrap/pk11akey.c:pk11_MakeIDFromPublicKey() private, hashes)*/ 707 /*(lib/crmf/crmfcont.c:crmf_get_public_value() public but incomplete)*/ 708 switch (pubKey->keyType) { 709 case dsaKey: return &pubKey->u.dsa.publicValue; 710 case dhKey: return &pubKey->u.dh.publicValue; 711 case rsaKey: return &pubKey->u.rsa.modulus; 712 case ecKey: return &pubKey->u.ec.publicValue; 713 default: return NULL; 714 } 715 } 716 717 718 static SECKEYPrivateKey * 719 mod_nss_load_config_pkey (const char *fn, CERTCertificate *cert, log_error_st *errh) 720 { 721 /* NSS does not provide convenient mechanisms to read PEM or DER private key 722 * instead expecting PKCS12-format, which is not the convention used by many 723 * other TLS modules */ 724 725 /*(similar start to other mod_nss_load_config_*())*/ 726 if (!mod_nss_init_once_nss()) return NULL; 727 728 SECItem f; 729 int rc = mod_nss_load_file(fn, &f, errh); 730 if (rc < 0) return NULL; 731 732 SECItem der = { 0, NULL, 0 }; 733 PK11SlotInfo *slot = NULL; 734 SECKEYPrivateKey *pkey = NULL; 735 SECStatus src = SECFailure; 736 do { 737 /*(expecting single private key in file, so first match)*/ 738 char *b, *e; 739 if ((b = strstr((char *)f.data, PEM_BEGIN_PKEY)) 740 && (e = strstr(b, PEM_END_PKEY))) 741 b += sizeof(PEM_BEGIN_PKEY)-1; 742 else if ((b = strstr((char *)f.data, PEM_BEGIN_EC_PKEY)) 743 && (e = strstr(b, PEM_END_EC_PKEY))) 744 b += sizeof(PEM_BEGIN_EC_PKEY)-1; 745 else if ((b = strstr((char *)f.data, PEM_BEGIN_RSA_PKEY)) 746 && (e = strstr(b, PEM_END_RSA_PKEY))) 747 b += sizeof(PEM_BEGIN_RSA_PKEY)-1; 748 else if ((b = strstr((char *)f.data, PEM_BEGIN_DSA_PKEY)) 749 && (e = strstr(b, PEM_END_DSA_PKEY))) 750 b += sizeof(PEM_BEGIN_DSA_PKEY)-1; 751 else if ((b = strstr((char *)f.data, PEM_BEGIN_ANY_PKEY)) 752 && (e = strstr(b, PEM_END_ANY_PKEY))) 753 b += sizeof(PEM_BEGIN_ANY_PKEY)-1; 754 else 755 break; 756 if (*b == '\r') ++b; 757 if (*b == '\n') ++b; 758 759 if (NULL == NSSBase64_DecodeBuffer(NULL, &der, b, (uint32_t)(e - b))) 760 break; 761 762 slot = PK11_GetInternalKeySlot(); 763 if (NULL == slot) break; 764 765 SECItem nickname = { 0, NULL, strlen(fn) }; 766 *(const unsigned char **)&nickname.data = (unsigned char *)fn; 767 unsigned int keyUsage = KU_ALL; /* XXX: limit to fewer flags? */ 768 PRBool isPerm = PR_FALSE; 769 PRBool isPrivate = PR_TRUE; 770 SECKEYPublicKey *pubKey = CERT_ExtractPublicKey(cert); 771 SECItem *pubValue = mod_nss_cert_get_publicValue(pubKey); 772 src = 773 PK11_ImportDERPrivateKeyInfoAndReturnKey(slot, &der, &nickname, 774 pubValue, isPerm, isPrivate, 775 keyUsage, &pkey, NULL); 776 /* nickname attribute has reference taken to data; 777 * data must persist longer than SECKEYPrivateKey */ 778 /* (pubValue data is of decoded type SEC_ASN1_INTEGER and is copied) */ 779 SECKEY_DestroyPublicKey(pubKey); 780 } while (0); 781 782 if (slot) PK11_FreeSlot(slot); 783 if (der.data) { 784 mod_nss_secitem_wipe(&der); 785 PORT_Free(der.data); 786 } 787 mod_nss_secitem_wipe(&f); 788 789 if (src < 0) { 790 elogf(errh, __FILE__, __LINE__, 791 "PK11_ImportDERPrivateKeyInfoAndReturnKey() %s", fn); 792 return NULL; 793 } 794 795 return pkey; 796 } 797 798 799 static void 800 mod_nss_free_config (server *srv, plugin_data * const p) 801 { 802 if (NULL != p->ssl_ctxs) { 803 PRFileDesc *global_model = p->ssl_ctxs->model; 804 /* free from $SERVER["socket"] (if not copy of global scope) */ 805 for (uint32_t i = 1; i < srv->config_context->used; ++i) { 806 plugin_ssl_ctx * const s = p->ssl_ctxs + i; 807 if (s->model && s->model != global_model) 808 PR_Close(s->model); 809 } 810 /* free from global scope */ 811 if (global_model) 812 PR_Close(global_model); 813 free(p->ssl_ctxs); 814 } 815 816 if (NULL == p->cvlist) return; 817 /* (init i to 0 if global context; to 1 to skip empty global context) */ 818 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 819 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 820 for (; -1 != cpv->k_id; ++cpv) { 821 switch (cpv->k_id) { 822 case 0: /* ssl.pemfile */ 823 if (cpv->vtype == T_CONFIG_LOCAL) { 824 plugin_cert *pc = cpv->v.v; 825 CERT_DestroyCertificate(pc->ssl_pemfile_x509); 826 SECKEY_DestroyPrivateKey(pc->ssl_pemfile_pkey); 827 CERTCertificateList *certChain; 828 *(const CERTCertificateList **)&certChain = 829 pc->ssl_credex.certChain; 830 CERT_DestroyCertificateList(certChain); 831 PORT_Free(pc->OCSPResponse.data); 832 //CERT_Destroy...(pc->ssl_credex.signedCertTimestamps); 833 //CERT_Destroy...(pc->ssl_credex.delegCred); 834 //CERT_Destroy...(pc->ssl_credex.delegCredPrivKey); 835 free(pc); 836 } 837 break; 838 case 2: /* ssl.ca-file */ 839 case 3: /* ssl.ca-dn-file */ 840 if (cpv->vtype == T_CONFIG_LOCAL) 841 CERT_DestroyCertList(cpv->v.v); 842 break; 843 case 4: /* ssl.ca-crl-file */ 844 if (cpv->vtype == T_CONFIG_LOCAL) 845 mod_nss_free_config_crls(cpv->v.v); 846 break; 847 default: 848 break; 849 } 850 } 851 } 852 } 853 854 855 FREE_FUNC(mod_nss_free) 856 { 857 plugin_data *p = p_d; 858 if (NULL == p->srv) return; 859 mod_nss_free_config(p->srv, p); 860 mod_nss_free_nss(); 861 } 862 863 864 static void 865 mod_nss_merge_config_cpv (plugin_config * const pconf, const config_plugin_value_t * const cpv) 866 { 867 switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 868 case 0: /* ssl.pemfile */ 869 if (cpv->vtype == T_CONFIG_LOCAL) 870 pconf->pc = cpv->v.v; 871 break; 872 case 1: /* ssl.privkey */ 873 break; 874 case 2: /* ssl.ca-file */ 875 if (cpv->vtype == T_CONFIG_LOCAL) 876 pconf->ssl_ca_file = cpv->v.v; 877 break; 878 case 3: /* ssl.ca-dn-file */ 879 if (cpv->vtype == T_CONFIG_LOCAL) 880 pconf->ssl_ca_dn_file = cpv->v.v; 881 break; 882 case 4: /* ssl.ca-crl-file */ 883 break; 884 case 5: /* ssl.read-ahead */ 885 pconf->ssl_read_ahead = (0 != cpv->v.u); 886 break; 887 case 6: /* ssl.disable-client-renegotiation */ 888 pconf->ssl_disable_client_renegotiation = (0 != cpv->v.u); 889 break; 890 case 7: /* ssl.verifyclient.activate */ 891 pconf->ssl_verifyclient = (0 != cpv->v.u); 892 break; 893 case 8: /* ssl.verifyclient.enforce */ 894 pconf->ssl_verifyclient_enforce = (0 != cpv->v.u); 895 break; 896 case 9: /* ssl.verifyclient.depth */ 897 pconf->ssl_verifyclient_depth = (unsigned char)cpv->v.shrt; 898 break; 899 case 10:/* ssl.verifyclient.username */ 900 pconf->ssl_verifyclient_username = cpv->v.b; 901 break; 902 case 11:/* ssl.verifyclient.exportcert */ 903 pconf->ssl_verifyclient_export_cert = (0 != cpv->v.u); 904 break; 905 case 12:/* ssl.acme-tls-1 */ 906 pconf->ssl_acme_tls_1 = cpv->v.b; 907 break; 908 case 13:/* ssl.stapling-file */ 909 break; 910 case 14:/* debug.log-ssl-noise */ 911 pconf->ssl_log_noise = (unsigned char)cpv->v.shrt; 912 break; 913 default:/* should not happen */ 914 return; 915 } 916 } 917 918 919 static void 920 mod_nss_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) 921 { 922 do { 923 mod_nss_merge_config_cpv(pconf, cpv); 924 } while ((++cpv)->k_id != -1); 925 } 926 927 928 static void 929 mod_nss_patch_config (request_st * const r, plugin_config * const pconf) 930 { 931 plugin_data * const p = plugin_data_singleton; 932 memcpy(pconf, &p->defaults, sizeof(plugin_config)); 933 for (int i = 1, used = p->nconfig; i < used; ++i) { 934 if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 935 mod_nss_merge_config(pconf, p->cvlist + p->cvlist[i].v.u2[0]); 936 } 937 } 938 939 940 static SECStatus 941 mod_nss_verify_cb (void *arg, PRFileDesc *ssl, PRBool checkSig, PRBool isServer) 942 { 943 handler_ctx * const hctx = arg; 944 if (!hctx->conf.ssl_verifyclient) return SECSuccess; 945 946 /* Notes 947 * trusted CAs in ssl.ca-file were loaded into default cert db at startup 948 * OCSP checking (querying OCSP Responder) is disabled by default 949 * CERT_EnableOCSPChecking() 950 * CERT_DisableOCSPChecking() 951 * cert_VerifyCertWithFlags() is not public, 952 * so unable to use CERT_VERIFYCERT_SKIP_OCSP 953 * hctx->verify_status is set here; not setting SSL_BadCertHook() 954 * XXX: not implemented (yet) here: hctx->conf.ssl_verifyclient_depth) 955 */ 956 957 CERTCertificate *peer = NULL; 958 959 #if 0 960 peer = SSL_PeerCertificate(ssl); 961 if (NULL == peer) 962 return (PORT_GetError() == SSL_ERROR_NO_CERTIFICATE) 963 ? SECSuccess 964 : SECFailure; 965 966 if (CERT_VerifyCert(CERT_GetDefaultCertDB(), peer, PR_TRUE, 967 certUsageSSLClient, (PRInt64)log_epoch_secs * 1000000, 968 SSL_RevealPinArg(ssl), NULL) < 0) 969 #else 970 if (SSL_AuthCertificate((void *)CERT_GetDefaultCertDB(), 971 ssl, checkSig, isServer) < 0) 972 #endif 973 { 974 hctx->verify_status = PORT_GetError(); 975 if (0 == hctx->verify_status) 976 hctx->verify_status = SEC_ERROR_UNTRUSTED_CERT; 977 } 978 979 if (hctx->verify_status == 0 && hctx->conf.ssl_ca_dn_file) { 980 /* verify that client cert is issued by CA in ssl.ca-dn-file 981 * if both ssl.ca-dn-file and ssl.ca-file were configured */ 982 if (NULL == peer) peer = SSL_PeerCertificate(ssl); 983 if (peer) { 984 CERTCertList * const certList = hctx->conf.ssl_ca_dn_file; 985 SECItem * const derIssuer = &peer->derIssuer; 986 CERTCertListNode *node = CERT_LIST_HEAD(certList); 987 for (; !CERT_LIST_END(node, certList); node = CERT_LIST_NEXT(node)){ 988 SECItem * const derSubject = &node->cert->derSubject; 989 if (SECITEM_CompareItem(derIssuer, derSubject) == SECEqual) 990 break; 991 } 992 if (CERT_LIST_END(node, certList)) 993 hctx->verify_status = SEC_ERROR_UNTRUSTED_CERT; 994 } 995 } 996 997 if (peer) CERT_DestroyCertificate(peer); 998 999 if (hctx->verify_status != 0 && hctx->conf.ssl_verifyclient_enforce) { 1000 PORT_SetError(SEC_ERROR_UNTRUSTED_CERT); 1001 return SECFailure; 1002 } 1003 1004 return SECSuccess; 1005 } 1006 1007 1008 static int 1009 mod_nss_reload_stapling_file (server *srv, plugin_cert *pc, const time_t cur_ts) 1010 { 1011 SECItem f; 1012 int rc = mod_nss_load_file(pc->ssl_stapling_file->ptr, &f, srv->errh); 1013 if (rc < 0) return rc; 1014 1015 /* NSS has the ability to include multiple OCSP responses for 1016 * certificate chain as allowed in TLSv1.3, but that is not utilized here. 1017 * If implemented, it will probably operate on a new directive, 1018 * e.g. ssl.stapling-pemfile 1019 */ 1020 1021 /* Note that the credentials structure should be read-only when in 1022 * use, thus when reloading, either the credentials structure must not 1023 * be in use by any sessions, or a new credentials structure should be 1024 * allocated for new sessions. 1025 * XXX: lighttpd is not threaded, so this is probably not an issue (?) 1026 */ 1027 1028 PORT_Free(pc->OCSPResponse.data); 1029 pc->OCSPResponse.data = f.data; 1030 pc->OCSPResponse.len = f.len; 1031 pc->OCSPResponses.items = &pc->OCSPResponse; 1032 pc->OCSPResponses.len = 1; 1033 pc->ssl_credex.stapledOCSPResponses = &pc->OCSPResponses; 1034 1035 /* NSS does not expose CERTOCSPSingleResponse member nextUpdate 1036 * to allow getting (PRTime) of nextUpdate from the OCSP response. 1037 * (PRTime is (PRInt64) of microseconds since epoch) 1038 * e.g. DER_GeneralizedTimeToTime(&nextUpdate, single->nextUpdate); 1039 * XXX: *not* implementing our own ASN.1 DER decoder for OCSP response 1040 * ssl.stapling-file will be reloaded hourly 1041 */ 1042 time_t nextupd = (time_t)-1; 1043 1044 pc->ssl_stapling_loadts = cur_ts; 1045 pc->ssl_stapling_nextts = nextupd; 1046 if (pc->ssl_stapling_nextts == (time_t)-1) { 1047 /* "Next Update" might not be provided by OCSP responder 1048 * Use 3600 sec (1 hour) in that case. */ 1049 /* retry in 1 hour if unable to determine Next Update */ 1050 pc->ssl_stapling_nextts = cur_ts + 3600; 1051 pc->ssl_stapling_loadts = 0; 1052 } 1053 1054 return 0; 1055 } 1056 1057 1058 static int 1059 mod_nss_refresh_stapling_file (server *srv, plugin_cert *pc, const time_t cur_ts) 1060 { 1061 if (pc->ssl_stapling_nextts >= 256 1062 && pc->ssl_stapling_nextts - 256 > cur_ts) 1063 return 0; /* skip check for refresh unless close to expire */ 1064 struct stat st; 1065 if (0 != stat(pc->ssl_stapling_file->ptr, &st) 1066 || st.st_mtime <= pc->ssl_stapling_loadts) { 1067 if (pc->ssl_stapling_nextts < cur_ts) { 1068 /* discard expired OCSP stapling response */ 1069 pc->ssl_credex.stapledOCSPResponses = NULL; 1070 if (pc->must_staple) { 1071 log_error(srv->errh, __FILE__, __LINE__, 1072 "certificate marked OCSP Must-Staple, " 1073 "but OCSP response expired from ssl.stapling-file %s", 1074 pc->ssl_stapling_file->ptr); 1075 } 1076 } 1077 return 0; 1078 } 1079 return mod_nss_reload_stapling_file(srv, pc, cur_ts); 1080 } 1081 1082 1083 static void 1084 mod_nss_refresh_stapling_files (server *srv, const plugin_data *p, const time_t cur_ts) 1085 { 1086 /* future: might construct array of (plugin_cert *) at startup 1087 * to avoid the need to search for them here */ 1088 for (int i = 0, used = p->nconfig; i < used; ++i) { 1089 const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1090 for (; cpv->k_id != -1; ++cpv) { 1091 if (cpv->k_id != 0) continue; /* k_id == 0 for ssl.pemfile */ 1092 if (cpv->vtype != T_CONFIG_LOCAL) continue; 1093 plugin_cert *pc = cpv->v.v; 1094 if (!buffer_string_is_empty(pc->ssl_stapling_file)) 1095 mod_nss_refresh_stapling_file(srv, pc, cur_ts); 1096 } 1097 } 1098 } 1099 1100 1101 static int 1102 mod_nss_crt_must_staple (CERTCertificate *crt) 1103 { 1104 /* Look for TLS features X.509 extension with value 5 1105 * RFC 7633 https://tools.ietf.org/html/rfc7633#appendix-A 1106 * 5 = OCSP Must-Staple (security mechanism) 1107 * 1108 * id-pe-tlsfeature 1.3.6.1.5.5.7.1.24 1109 * 1.3.6.1.5.5.7.1.24 = DER:30:03:02:01:05 1110 */ 1111 1112 int rc; 1113 1114 /* XXX: not implemented */ 1115 UNUSED(crt); 1116 rc = 0; 1117 1118 return rc; /* 1 if OCSP Must-Staple found; 0 if not */ 1119 } 1120 1121 1122 static void * 1123 network_nss_load_pemfile (server *srv, const buffer *pemfile, const buffer *privkey, const buffer *ssl_stapling_file) 1124 { 1125 CERTCertificateList *ssl_pemfile_chain; 1126 CERTCertificate *ssl_pemfile_x509 = 1127 mod_nss_load_pem_crts(pemfile->ptr, srv->errh, &ssl_pemfile_chain); 1128 if (NULL == ssl_pemfile_x509) 1129 return NULL; 1130 1131 SECKEYPrivateKey *pkey = 1132 mod_nss_load_config_pkey(privkey->ptr, ssl_pemfile_x509, srv->errh); 1133 if (NULL == pkey) { 1134 CERT_DestroyCertificate(ssl_pemfile_x509); 1135 if (ssl_pemfile_chain) CERT_DestroyCertificateList(ssl_pemfile_chain); 1136 return NULL; 1137 } 1138 1139 if (NULL == ssl_pemfile_chain) 1140 ssl_pemfile_chain = CERT_CertChainFromCert(ssl_pemfile_x509, 1141 certUsageSSLServer, 1142 PR_FALSE); 1143 1144 plugin_cert *pc = calloc(1, sizeof(plugin_cert)); 1145 force_assert(pc); 1146 pc->ssl_pemfile_pkey = pkey; 1147 pc->ssl_pemfile_x509 = ssl_pemfile_x509; 1148 pc->ssl_credex.certChain = ssl_pemfile_chain; 1149 pc->ssl_stapling_file= ssl_stapling_file; 1150 pc->ssl_stapling_loadts = 0; 1151 pc->ssl_stapling_nextts = 0; 1152 pc->OCSPResponse.type = 0; 1153 pc->OCSPResponse.data = NULL; 1154 pc->OCSPResponse.len = 0; 1155 pc->OCSPResponses.items = NULL; 1156 pc->OCSPResponses.len = 0; 1157 pc->must_staple = mod_nss_crt_must_staple(ssl_pemfile_x509); 1158 1159 if (!buffer_string_is_empty(pc->ssl_stapling_file)) { 1160 if (mod_nss_reload_stapling_file(srv, pc, log_epoch_secs) < 0) { 1161 /* continue without OCSP response if there is an error */ 1162 } 1163 } 1164 else if (pc->must_staple) { 1165 log_error(srv->errh, __FILE__, __LINE__, 1166 "certificate %s marked OCSP Must-Staple, " 1167 "but ssl.stapling-file not provided", pemfile->ptr); 1168 } 1169 1170 return pc; 1171 } 1172 1173 1174 static int 1175 mod_nss_acme_tls_1 (handler_ctx *hctx) 1176 { 1177 buffer * const b = hctx->tmp_buf; 1178 const buffer * const name = &hctx->r->uri.authority; 1179 log_error_st * const errh = hctx->r->conf.errh; 1180 1181 /* check if acme-tls/1 protocol is enabled (path to dir of cert(s) is set)*/ 1182 if (buffer_string_is_empty(hctx->conf.ssl_acme_tls_1)) 1183 return SECFailure; /*(should not happen)*/ 1184 buffer_copy_buffer(b, hctx->conf.ssl_acme_tls_1); 1185 buffer_append_slash(b); 1186 1187 /* check if SNI set server name (required for acme-tls/1 protocol) 1188 * and perform simple path checks for no '/' 1189 * and no leading '.' (e.g. ignore "." or ".." or anything beginning '.') */ 1190 if (buffer_string_is_empty(name)) return SECFailure; 1191 if (NULL != strchr(name->ptr, '/')) return SECFailure; 1192 if (name->ptr[0] == '.') return SECFailure; 1193 #if 0 1194 if (0 != http_request_host_policy(name, hctx->r->conf.http_parseopts, 443)) 1195 return SECFailure; 1196 #endif 1197 buffer_append_string_buffer(b, name); 1198 1199 /* cert and key load is similar to network_nss_load_pemfile() */ 1200 1201 uint32_t len = buffer_string_length(b); 1202 buffer_append_string_len(b, CONST_STR_LEN(".crt.pem")); 1203 1204 CERTCertificateList *ssl_pemfile_chain; 1205 CERTCertificate *ssl_pemfile_x509 = 1206 mod_nss_load_pem_crts(b->ptr, errh, &ssl_pemfile_chain); 1207 if (NULL == ssl_pemfile_x509) 1208 return SECFailure; 1209 1210 buffer_string_set_length(b, len); 1211 buffer_append_string_len(b, CONST_STR_LEN(".key.pem")); 1212 1213 SECKEYPrivateKey *pkey = 1214 mod_nss_load_config_pkey(b->ptr, ssl_pemfile_x509, errh); 1215 if (NULL == pkey) { 1216 CERT_DestroyCertificate(ssl_pemfile_x509); 1217 if (ssl_pemfile_chain) CERT_DestroyCertificateList(ssl_pemfile_chain); 1218 return SECFailure; 1219 } 1220 1221 /* use NSS deprecated functions to unconfigure an already-configured cert. 1222 * This is because SSL_ConfigServerCert() will replace an existing cert 1223 * of the same type, but not if an existing cert is of a different type */ 1224 if (hctx->conf.pc) { 1225 SSLKEAType certType = 1226 NSS_FindCertKEAType(hctx->conf.pc->ssl_pemfile_x509); 1227 SSL_ConfigSecureServerWithCertChain(hctx->ssl,NULL,NULL,NULL,certType); 1228 } 1229 1230 unsigned int dlen = 0; 1231 SSLExtraServerCertData *data = NULL; 1232 SSLExtraServerCertData d; 1233 if (ssl_pemfile_chain) { 1234 data = &d; 1235 dlen = sizeof(d); 1236 memset(&d, 0, sizeof(d)); 1237 d.certChain = ssl_pemfile_chain; 1238 } 1239 SECStatus rc = 1240 SSL_ConfigServerCert(hctx->ssl, ssl_pemfile_x509, pkey, data, dlen); 1241 1242 CERT_DestroyCertificate(ssl_pemfile_x509); 1243 SECKEY_DestroyPrivateKey(pkey); 1244 if (ssl_pemfile_chain) CERT_DestroyCertificateList(ssl_pemfile_chain); 1245 1246 if (hctx->conf.ssl_verifyclient) { 1247 /*(disable client certificate verification for "acme-tls/1")*/ 1248 hctx->conf.ssl_verifyclient = 0; 1249 SSL_OptionSet(hctx->ssl, SSL_REQUEST_CERTIFICATE, PR_FALSE); 1250 SSL_OptionSet(hctx->ssl, SSL_REQUIRE_CERTIFICATE, SSL_REQUIRE_NEVER); 1251 } 1252 1253 return rc; 1254 } 1255 1256 1257 enum { 1258 MOD_NSS_ALPN_HTTP11 = 1 1259 ,MOD_NSS_ALPN_HTTP10 = 2 1260 ,MOD_NSS_ALPN_H2 = 3 1261 ,MOD_NSS_ALPN_ACME_TLS_1 = 4 1262 }; 1263 1264 1265 static SECStatus 1266 mod_nss_alpn_select_cb (void *arg, PRFileDesc *ssl, 1267 const unsigned char *protos, unsigned int protosLen, 1268 unsigned char *protoOut, unsigned int *protoOutLen, 1269 unsigned int protoMaxOut) 1270 { 1271 /* https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids */ 1272 static const SECItem alpn[] = { 1273 { 0, (unsigned char *)CONST_STR_LEN("h2") } 1274 ,{ 0, (unsigned char *)CONST_STR_LEN("http/1.1") } 1275 ,{ 0, (unsigned char *)CONST_STR_LEN("http/1.0") } 1276 ,{ 0, (unsigned char *)CONST_STR_LEN("acme-tls/1") } 1277 }; 1278 UNUSED(ssl); 1279 /* reference: lib/ssl/sslsock.c:ssl_NextProtoNegoCallback() */ 1280 for (unsigned int i = 0; i < protosLen; i += 1 + protos[i]) { 1281 for (unsigned int j = 0; j < sizeof(alpn)/sizeof(*alpn); ++j) { 1282 if (protos[i] == alpn[j].len && i+1+protos[i] <= protosLen 1283 && 0 == PORT_Memcmp(protos+i+1, alpn[j].data, alpn[j].len)) { 1284 1285 if (protoMaxOut < alpn[j].len) { 1286 PORT_SetError(SEC_ERROR_OUTPUT_LEN); 1287 return SECFailure; 1288 } 1289 1290 handler_ctx *hctx = arg; 1291 switch (j) { /*(must match SECItem alpn[] above)*/ 1292 case 0: 1293 if (!hctx->r->conf.h2proto) continue; 1294 hctx->alpn = MOD_NSS_ALPN_H2; 1295 hctx->r->http_version = HTTP_VERSION_2; 1296 break; 1297 case 1: 1298 hctx->alpn = MOD_NSS_ALPN_HTTP11; 1299 break; 1300 case 2: 1301 hctx->alpn = MOD_NSS_ALPN_HTTP10; 1302 break; 1303 case 3: 1304 if (buffer_string_is_empty(hctx->conf.ssl_acme_tls_1)) 1305 continue; 1306 if (0 == mod_nss_acme_tls_1(hctx)) 1307 hctx->alpn = MOD_NSS_ALPN_ACME_TLS_1; 1308 else { 1309 log_error(hctx->r->conf.errh, __FILE__, __LINE__, 1310 "failed to set acme-tls/1 certificate for TLS" 1311 " server name %s",hctx->r->uri.authority.ptr); 1312 return SECFailure; 1313 } 1314 break; 1315 default: 1316 break; 1317 } 1318 1319 memcpy(protoOut, alpn[j].data, alpn[j].len); 1320 *protoOutLen = alpn[j].len; 1321 1322 return SECSuccess; 1323 } 1324 } 1325 } 1326 return SECSuccess; 1327 } 1328 1329 1330 static PRInt32 1331 mod_nss_SNI (PRFileDesc *ssl, const SECItem *srvNameArr, PRUint32 srvNameArrSize, 1332 void *arg) 1333 { 1334 if (0 == srvNameArrSize) /* should not happen */ 1335 return SSL_SNI_CURRENT_CONFIG_IS_USED; 1336 1337 handler_ctx * const hctx = (handler_ctx *)arg; 1338 request_st * const r = hctx->r; 1339 buffer_copy_string(&r->uri.scheme, "https"); 1340 PRUint32 i = 0; /* index into srvNameArr; always take first element */ 1341 const SECItem *sn = srvNameArr+i; 1342 1343 if (sn->len >= 1024) { /*(expecting < 256; TLSEXT_MAXLEN_host_name is 255)*/ 1344 log_error(r->conf.errh, __FILE__, __LINE__, 1345 "NSS: SNI name too long %.*s", (int)sn->len,(char *)sn->data); 1346 return SSL_SNI_SEND_ALERT; 1347 } 1348 1349 /* use SNI to patch mod_nss config and then reset COMP_HTTP_HOST */ 1350 buffer_copy_string_len(&r->uri.authority, (const char *)sn->data, sn->len); 1351 buffer_to_lower(&r->uri.authority); 1352 #if 0 1353 /*(r->uri.authority used below for configuration before request read; 1354 * revisit for h2)*/ 1355 if (0 != http_request_host_policy(&r->uri.authority, 1356 r->conf.http_parseopts, 443)) 1357 return SSL_SNI_SEND_ALERT; 1358 #endif 1359 1360 r->conditional_is_valid |= (1 << COMP_HTTP_SCHEME) 1361 | (1 << COMP_HTTP_HOST); 1362 1363 plugin_cert *pc = hctx->conf.pc; 1364 1365 mod_nss_patch_config(r, &hctx->conf); 1366 /* reset COMP_HTTP_HOST so that conditions re-run after request hdrs read */ 1367 /*(done in configfile-glue.c:config_cond_cache_reset() after request hdrs read)*/ 1368 /*config_cond_cache_reset_item(r, COMP_HTTP_HOST);*/ 1369 /*buffer_clear(&r->uri.authority);*/ 1370 1371 if (pc == hctx->conf.pc) 1372 return SSL_SNI_CURRENT_CONFIG_IS_USED; 1373 1374 /* use NSS deprecated functions to unconfigure an already-configured cert. 1375 * This is because SSL_ConfigServerCert() will replace an existing cert 1376 * of the same type, but not if an existing cert is of a different type */ 1377 SSLKEAType certType = 1378 NSS_FindCertKEAType(hctx->conf.pc->ssl_pemfile_x509); 1379 SSL_ConfigSecureServerWithCertChain(ssl, NULL, NULL, NULL, certType); 1380 SECStatus rc = 1381 SSL_ConfigServerCert(ssl, hctx->conf.pc->ssl_pemfile_x509, 1382 hctx->conf.pc->ssl_pemfile_pkey, 1383 &hctx->conf.pc->ssl_credex, 1384 sizeof(hctx->conf.pc->ssl_credex)); 1385 if (rc < 0) { 1386 elogf(r->conf.errh, __FILE__, __LINE__, 1387 "failed to set SNI certificate for TLS server name %s", 1388 r->uri.authority.ptr); 1389 return SSL_SNI_SEND_ALERT; 1390 } 1391 1392 if (hctx->conf.ssl_verifyclient) { 1393 /*(XXX: technically do not need to redo if it has not changed)*/ 1394 if (SSL_AuthCertificateHook(ssl, mod_nss_verify_cb, hctx) < 0) { 1395 elog(r->conf.errh, __FILE__, __LINE__, "SSL_AuthCertificateHook"); 1396 return SSL_SNI_SEND_ALERT; 1397 } 1398 CERTCertList * const certList = hctx->conf.ssl_ca_dn_file 1399 ? hctx->conf.ssl_ca_dn_file 1400 : hctx->conf.ssl_ca_file; 1401 if (NULL == certList) 1402 log_error(hctx->r->conf.errh, __FILE__, __LINE__, 1403 "NSS: can't verify client without ssl.ca-file " 1404 "for TLS server name %s", 1405 hctx->r->uri.authority.ptr); /*(might not be set yet if no SNI)*/ 1406 if (certList && SSL_SetTrustAnchors(ssl, certList) < 0) { 1407 elog(r->conf.errh, __FILE__, __LINE__, "SSL_SetTrustAnchors"); 1408 return SSL_SNI_SEND_ALERT; 1409 } 1410 SSL_OptionSet(ssl, SSL_REQUEST_CERTIFICATE, PR_TRUE); 1411 SSL_OptionSet(ssl, SSL_REQUIRE_CERTIFICATE, 1412 hctx->conf.ssl_verifyclient_enforce 1413 ? SSL_REQUIRE_ALWAYS 1414 : SSL_REQUIRE_NEVER); 1415 } 1416 else { 1417 SSL_OptionSet(ssl, SSL_REQUEST_CERTIFICATE, PR_FALSE); 1418 SSL_OptionSet(ssl, SSL_REQUIRE_CERTIFICATE, SSL_REQUIRE_NEVER); 1419 } 1420 1421 return (PRInt32)i; 1422 } 1423 1424 1425 static int 1426 mod_nss_ssl_conf_ciphersuites (server *srv, plugin_config_socket *s, buffer *ciphersuites, const buffer *cipherstring); 1427 1428 1429 static int 1430 mod_nss_ssl_conf_curves(server *srv, plugin_config_socket *s, const buffer *curvelist); 1431 1432 1433 static void 1434 mod_nss_ssl_conf_proto (server *srv, plugin_config_socket *s, const buffer *minb, const buffer *maxb); 1435 1436 1437 static int 1438 mod_nss_ssl_conf_cmd (server *srv, plugin_config_socket *s) 1439 { 1440 /* reference: 1441 * https://www.openssl.org/docs/man1.1.1/man3/SSL_CONF_cmd.html */ 1442 int rc = 0; 1443 buffer *cipherstring = NULL; 1444 buffer *ciphersuites = NULL; 1445 buffer *minb = NULL; 1446 buffer *maxb = NULL; 1447 buffer *curves = NULL; 1448 1449 for (size_t i = 0; i < s->ssl_conf_cmd->used; ++i) { 1450 data_string *ds = (data_string *)s->ssl_conf_cmd->data[i]; 1451 if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("CipherString"))) 1452 cipherstring = &ds->value; 1453 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Ciphersuites"))) 1454 ciphersuites = &ds->value; 1455 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Curves")) 1456 || buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Groups"))) 1457 curves = &ds->value; 1458 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("MaxProtocol"))) 1459 maxb = &ds->value; 1460 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("MinProtocol"))) 1461 minb = &ds->value; 1462 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Protocol"))) { 1463 /* openssl config for Protocol=... is complex and deprecated */ 1464 log_error(srv->errh, __FILE__, __LINE__, 1465 "NSS: ssl.openssl.ssl-conf-cmd %s ignored; " 1466 "use MinProtocol=... and MaxProtocol=... instead", 1467 ds->key.ptr); 1468 } 1469 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("Options"))) { 1470 for (char *v = ds->value.ptr, *e; *v; v = e) { 1471 while (*v == ' ' || *v == '\t' || *v == ',') ++v; 1472 int flag = 1; 1473 if (*v == '-') { 1474 flag = 0; 1475 ++v; 1476 } 1477 for (e = v; light_isalpha(*e); ++e) ; 1478 switch ((int)(e-v)) { 1479 case 11: 1480 if (buffer_eq_icase_ssn(v, "Compression", 11)) { 1481 s->ssl_compression = flag; 1482 continue; 1483 } 1484 break; 1485 case 13: 1486 if (buffer_eq_icase_ssn(v, "SessionTicket", 13)) { 1487 /*(translates to "%NO_TICKETS" priority str if !flag)*/ 1488 s->ssl_session_ticket = flag; 1489 continue; 1490 } 1491 break; 1492 case 16: 1493 if (buffer_eq_icase_ssn(v, "ServerPreference", 16)) { 1494 /*(translates to "%SERVER_PRECEDENCE" priority string)*/ 1495 s->ssl_honor_cipher_order = flag; 1496 continue; 1497 } 1498 break; 1499 default: 1500 break; 1501 } 1502 /* warn if not explicitly handled or ignored above */ 1503 if (!flag) --v; 1504 log_error(srv->errh, __FILE__, __LINE__, 1505 "NSS: ssl.openssl.ssl-conf-cmd Options %.*s " 1506 "ignored", (int)(e-v), v); 1507 } 1508 } 1509 #if 0 1510 else if (buffer_eq_icase_slen(&ds->key, CONST_STR_LEN("..."))) { 1511 } 1512 #endif 1513 else { 1514 /* warn if not explicitly handled or ignored above */ 1515 log_error(srv->errh, __FILE__, __LINE__, 1516 "NSS: ssl.openssl.ssl-conf-cmd %s ignored", 1517 ds->key.ptr); 1518 } 1519 } 1520 1521 if (minb || maxb) /*(if at least one was set)*/ 1522 mod_nss_ssl_conf_proto(srv, s, minb, maxb); 1523 1524 if (!mod_nss_ssl_conf_ciphersuites(srv, s, ciphersuites, cipherstring)) 1525 rc = -1; 1526 1527 if (curves) { 1528 if (!mod_nss_ssl_conf_curves(srv, s, curves)) 1529 rc = -1; 1530 } 1531 1532 return rc; 1533 } 1534 1535 1536 static int 1537 network_init_ssl (server *srv, plugin_config_socket *s, plugin_data *p) 1538 { 1539 UNUSED(p); 1540 1541 /* use PR_CreateSocketPollFd() for dummy; 1542 * PR_CreateIOLayerStub() was resulting in crashes 1543 * when SSL_ImportFD() attempted ssl_DefGetpeername() */ 1544 s->model = PR_CreateSocketPollFd(-1); 1545 if (NULL == s->model) return -1; 1546 s->model->methods = PR_GetTCPMethods(); 1547 PRFileDesc *model = SSL_ImportFD(NULL, s->model); 1548 if (NULL == model) return -1; 1549 s->model = model; 1550 1551 if (!buffer_string_is_empty(s->ssl_cipher_list)) { 1552 if (!mod_nss_ssl_conf_ciphersuites(srv,s,NULL,s->ssl_cipher_list)) 1553 return -1; 1554 } 1555 1556 if (!buffer_string_is_empty(s->ssl_ec_curve)) { 1557 if (!mod_nss_ssl_conf_curves(srv, s, s->ssl_ec_curve)) 1558 return -1; 1559 } 1560 1561 if (!s->ssl_use_sslv3 && !s->ssl_use_sslv2) 1562 mod_nss_ssl_conf_proto(srv, s, NULL, NULL); /* set default range */ 1563 1564 if (s->ssl_conf_cmd && s->ssl_conf_cmd->used) { 1565 if (0 != mod_nss_ssl_conf_cmd(srv, s)) return -1; 1566 } 1567 1568 if (s->ssl_use_sslv3) 1569 s->protos.min = SSL_LIBRARY_VERSION_3_0; 1570 if (s->ssl_use_sslv2) 1571 s->protos.min = SSL_LIBRARY_VERSION_2; 1572 1573 /* future: add additional configuration of s->model here 1574 * rather than in mod_nss_handle_con_accept() */ 1575 1576 if (SSL_OptionSet(model, SSL_SECURITY, PR_TRUE) < 0) { 1577 elog(srv->errh, __FILE__, __LINE__, "SSL_SECURITY"); 1578 return -1; 1579 } 1580 1581 if (SSL_VersionRangeSet(model, &s->protos)) { 1582 elog(srv->errh, __FILE__, __LINE__, "SSL_VersionRangeSet()"); 1583 return -1; 1584 } 1585 1586 if (s->protos.min == SSL_LIBRARY_VERSION_2 1587 && SSL_OptionSet(model, SSL_ENABLE_SSL2, PR_TRUE) < 0) { 1588 elog(srv->errh, __FILE__, __LINE__, "SSL_ENABLE_SSL2"); 1589 return -1; 1590 } 1591 1592 if (s->protos.min == SSL_LIBRARY_VERSION_3_0 1593 && SSL_OptionSet(model, SSL_ENABLE_SSL3, PR_TRUE) < 0) { 1594 elog(srv->errh, __FILE__, __LINE__, "SSL_ENABLE_SSL3"); 1595 return -1; 1596 } 1597 1598 if (!s->ssl_session_ticket 1599 && SSL_OptionSet(model, SSL_ENABLE_SESSION_TICKETS, PR_FALSE) < 0) { 1600 elog(srv->errh, __FILE__, __LINE__, "!SSL_ENABLE_SESSION_TICKETS"); 1601 return -1; 1602 } 1603 1604 if (SSL_OptionSet(model, SSL_ENABLE_DEFLATE, s->ssl_compression) < 0) { 1605 elog(srv->errh, __FILE__, __LINE__, "SSL_ENABLE_DEFLATE"); 1606 return HANDLER_ERROR; 1607 } 1608 1609 SSL_OptionSet(model, SSL_REQUEST_CERTIFICATE, PR_FALSE); 1610 SSL_OptionSet(model, SSL_REQUIRE_CERTIFICATE, SSL_REQUIRE_NEVER); 1611 1612 SECStatus rc = 1613 SSL_ConfigServerCert(model, s->pc->ssl_pemfile_x509, 1614 s->pc->ssl_pemfile_pkey, 1615 &s->pc->ssl_credex, 1616 sizeof(s->pc->ssl_credex)); 1617 if (rc < 0) { 1618 elogf(srv->errh, __FILE__, __LINE__, 1619 "failed to set default certificate for socket"); 1620 return -1; 1621 } 1622 1623 return 0; 1624 } 1625 1626 1627 static int 1628 mod_nss_set_defaults_sockets(server *srv, plugin_data *p) 1629 { 1630 static const config_plugin_keys_t cpk[] = { 1631 { CONST_STR_LEN("ssl.engine"), 1632 T_CONFIG_BOOL, 1633 T_CONFIG_SCOPE_CONNECTION } 1634 ,{ CONST_STR_LEN("ssl.cipher-list"), 1635 T_CONFIG_STRING, 1636 T_CONFIG_SCOPE_CONNECTION } 1637 ,{ CONST_STR_LEN("ssl.honor-cipher-order"), 1638 T_CONFIG_BOOL, 1639 T_CONFIG_SCOPE_CONNECTION } 1640 ,{ CONST_STR_LEN("ssl.dh-file"), 1641 T_CONFIG_STRING, 1642 T_CONFIG_SCOPE_CONNECTION } 1643 ,{ CONST_STR_LEN("ssl.ec-curve"), 1644 T_CONFIG_STRING, 1645 T_CONFIG_SCOPE_CONNECTION } 1646 ,{ CONST_STR_LEN("ssl.openssl.ssl-conf-cmd"), 1647 T_CONFIG_ARRAY_KVSTRING, 1648 T_CONFIG_SCOPE_CONNECTION } 1649 ,{ CONST_STR_LEN("ssl.pemfile"), /* included to process global scope */ 1650 T_CONFIG_STRING, 1651 T_CONFIG_SCOPE_CONNECTION } 1652 ,{ CONST_STR_LEN("ssl.empty-fragments"), 1653 T_CONFIG_BOOL, 1654 T_CONFIG_SCOPE_CONNECTION } 1655 ,{ CONST_STR_LEN("ssl.use-sslv2"), 1656 T_CONFIG_BOOL, 1657 T_CONFIG_SCOPE_CONNECTION } 1658 ,{ CONST_STR_LEN("ssl.use-sslv3"), 1659 T_CONFIG_BOOL, 1660 T_CONFIG_SCOPE_CONNECTION } 1661 ,{ CONST_STR_LEN("ssl.stek-file"), 1662 T_CONFIG_STRING, 1663 T_CONFIG_SCOPE_SERVER } 1664 ,{ NULL, 0, 1665 T_CONFIG_UNSET, 1666 T_CONFIG_SCOPE_UNSET } 1667 }; 1668 static const buffer default_ssl_cipher_list = { CONST_STR_LEN("HIGH"), 0 }; 1669 1670 p->ssl_ctxs = calloc(srv->config_context->used, sizeof(plugin_ssl_ctx)); 1671 force_assert(p->ssl_ctxs); 1672 1673 int rc = HANDLER_GO_ON; 1674 plugin_data_base srvplug; 1675 memset(&srvplug, 0, sizeof(srvplug)); 1676 plugin_data_base * const ps = &srvplug; 1677 if (!config_plugin_values_init(srv, ps, cpk, "mod_nss")) 1678 return HANDLER_ERROR; 1679 1680 plugin_config_socket defaults; 1681 memset(&defaults, 0, sizeof(defaults)); 1682 defaults.ssl_honor_cipher_order = 1; /* default server preference for PFS */ 1683 defaults.ssl_session_ticket = 1; /* enabled by default */ 1684 defaults.ssl_compression = 0; /* disable for security */ 1685 defaults.ssl_cipher_list = &default_ssl_cipher_list; 1686 1687 /* process and validate config directives for global and $SERVER["socket"] 1688 * (init i to 0 if global context; to 1 to skip empty global context) */ 1689 for (int i = !ps->cvlist[0].v.u2[1]; i < ps->nconfig; ++i) { 1690 config_cond_info cfginfo; 1691 config_get_config_cond_info(&cfginfo, (uint32_t)ps->cvlist[i].k_id); 1692 int is_socket_scope = (0 == i || cfginfo.comp == COMP_SERVER_SOCKET); 1693 int count_not_engine = 0; 1694 1695 plugin_config_socket conf; 1696 memcpy(&conf, &defaults, sizeof(conf)); 1697 1698 /*(preserve prior behavior; not inherited)*/ 1699 /*(forcing inheritance might break existing configs where SSL is enabled 1700 * by default in the global scope, but not $SERVER["socket"]=="*:80") */ 1701 conf.ssl_enabled = 0; 1702 1703 config_plugin_value_t *cpv = ps->cvlist + ps->cvlist[i].v.u2[0]; 1704 for (; -1 != cpv->k_id; ++cpv) { 1705 /* ignore ssl.pemfile (k_id=10); included to process global scope */ 1706 if (!is_socket_scope && cpv->k_id != 10) { 1707 log_error(srv->errh, __FILE__, __LINE__, 1708 "NSS: %s is valid only in global scope or " 1709 "$SERVER[\"socket\"] condition", cpk[cpv->k_id].k); 1710 continue; 1711 } 1712 ++count_not_engine; 1713 switch (cpv->k_id) { 1714 case 0: /* ssl.engine */ 1715 conf.ssl_enabled = (0 != cpv->v.u); 1716 --count_not_engine; 1717 break; 1718 case 1: /* ssl.cipher-list */ 1719 conf.ssl_cipher_list = cpv->v.b; 1720 break; 1721 case 2: /* ssl.honor-cipher-order */ 1722 conf.ssl_honor_cipher_order = (0 != cpv->v.u); 1723 break; 1724 case 3: /* ssl.dh-file */ 1725 log_error(srv->errh, __FILE__, __LINE__, 1726 "NSS: ignoring ssl.dh-file; not implemented" 1727 "obsoleted by RFC7919"); 1728 break; 1729 case 4: /* ssl.ec-curve */ 1730 conf.ssl_ec_curve = cpv->v.b; 1731 break; 1732 case 5: /* ssl.openssl.ssl-conf-cmd */ 1733 *(const array **)&conf.ssl_conf_cmd = cpv->v.a; 1734 break; 1735 case 6: /* ssl.pemfile */ 1736 /* ignore here; included to process global scope when 1737 * ssl.pemfile is set, but ssl.engine is not "enable" */ 1738 break; 1739 case 7: /* ssl.empty-fragments */ 1740 conf.ssl_empty_fragments = (0 != cpv->v.u); 1741 log_error(srv->errh, __FILE__, __LINE__, 1742 "NSS: ignoring ssl.empty-fragments; openssl-specific " 1743 "counter-measure against a SSL 3.0/TLS 1.0 protocol " 1744 "vulnerability affecting CBC ciphers, which cannot be handled" 1745 " by some broken (Microsoft) SSL implementations."); 1746 break; 1747 case 8: /* ssl.use-sslv2 */ 1748 conf.ssl_use_sslv2 = (0 != cpv->v.u); 1749 log_error(srv->errh, __FILE__, __LINE__, "NSS: " 1750 "ssl.use-sslv2 is deprecated and will soon be removed. " 1751 "Many modern TLS libraries no longer support SSLv2."); 1752 break; 1753 case 9: /* ssl.use-sslv3 */ 1754 conf.ssl_use_sslv3 = (0 != cpv->v.u); 1755 log_error(srv->errh, __FILE__, __LINE__, "NSS: " 1756 "ssl.use-sslv3 is deprecated and will soon be removed. " 1757 "Many modern TLS libraries no longer support SSLv3. " 1758 "If needed, use: " 1759 "ssl.openssl.ssl-conf-cmd = (\"MinProtocol\" => \"SSLv3\")"); 1760 break; 1761 case 10:/* ssl.stek-file */ 1762 log_error(srv->errh, __FILE__, __LINE__, "NSS: " 1763 "ssl.stek-file is not supported in mod_nss; ignoring."); 1764 break; 1765 default:/* should not happen */ 1766 break; 1767 } 1768 } 1769 if (HANDLER_GO_ON != rc) break; 1770 if (0 == i) memcpy(&defaults, &conf, sizeof(conf)); 1771 1772 if (0 != i && !conf.ssl_enabled) continue; 1773 1774 /* fill plugin_config_socket with global context then $SERVER["socket"] 1775 * only for directives directly in current $SERVER["socket"] condition*/ 1776 1777 /*conf.pc = p->defaults.pc;*/ 1778 conf.ssl_verifyclient = p->defaults.ssl_verifyclient; 1779 conf.ssl_verifyclient_enforce = p->defaults.ssl_verifyclient_enforce; 1780 conf.ssl_verifyclient_depth = p->defaults.ssl_verifyclient_depth; 1781 1782 int sidx = ps->cvlist[i].k_id; 1783 for (int j = !p->cvlist[0].v.u2[1]; j < p->nconfig; ++j) { 1784 if (p->cvlist[j].k_id != sidx) continue; 1785 /*if (0 == sidx) break;*//*(repeat to get ssl_pemfile,ssl_privkey)*/ 1786 cpv = p->cvlist + p->cvlist[j].v.u2[0]; 1787 for (; -1 != cpv->k_id; ++cpv) { 1788 ++count_not_engine; 1789 switch (cpv->k_id) { 1790 case 0: /* ssl.pemfile */ 1791 if (cpv->vtype == T_CONFIG_LOCAL) 1792 conf.pc = cpv->v.v; 1793 break; 1794 case 7: /* ssl.verifyclient.activate */ 1795 conf.ssl_verifyclient = (0 != cpv->v.u); 1796 break; 1797 case 8: /* ssl.verifyclient.enforce */ 1798 conf.ssl_verifyclient_enforce = (0 != cpv->v.u); 1799 break; 1800 case 9: /* ssl.verifyclient.depth */ 1801 conf.ssl_verifyclient_depth = (unsigned char)cpv->v.shrt; 1802 break; 1803 default: 1804 break; 1805 } 1806 } 1807 break; 1808 } 1809 1810 if (NULL == conf.pc) { 1811 if (0 == i && !conf.ssl_enabled) continue; 1812 if (0 != i) { 1813 /* inherit ssl settings from global scope 1814 * (if only ssl.engine = "enable" and no other ssl.* settings) 1815 * (This is for convenience when defining both IPv4 and IPv6 1816 * and desiring to inherit the ssl config from global context 1817 * without having to duplicate the directives)*/ 1818 if (count_not_engine 1819 || (conf.ssl_enabled && NULL == p->ssl_ctxs[0].model)) { 1820 log_error(srv->errh, __FILE__, __LINE__, 1821 "NSS: ssl.pemfile has to be set in same " 1822 "$SERVER[\"socket\"] scope as other ssl.* directives, " 1823 "unless only ssl.engine is set, inheriting ssl.* from " 1824 "global scope"); 1825 rc = HANDLER_ERROR; 1826 continue; 1827 } 1828 plugin_ssl_ctx * const s = p->ssl_ctxs + sidx; 1829 *s = *p->ssl_ctxs;/*(copy struct of ssl_ctx from global scope)*/ 1830 continue; 1831 } 1832 /* PEM file is required */ 1833 log_error(srv->errh, __FILE__, __LINE__, 1834 "NSS: ssl.pemfile has to be set when ssl.engine = \"enable\""); 1835 rc = HANDLER_ERROR; 1836 continue; 1837 } 1838 1839 /* (initialize once if module enabled) */ 1840 if (!mod_nss_init_once_nss()) { 1841 rc = HANDLER_ERROR; 1842 break; 1843 } 1844 1845 /* configure ssl_ctx for socket */ 1846 1847 /*conf.ssl_ctx = NULL;*//*(filled by network_init_ssl() even on error)*/ 1848 if (0 == network_init_ssl(srv, &conf, p)) { 1849 plugin_ssl_ctx * const s = p->ssl_ctxs + sidx; 1850 s->model = conf.model; 1851 s->protos = conf.protos; 1852 s->ssl_compression = conf.ssl_compression; 1853 s->ssl_session_ticket = conf.ssl_session_ticket; 1854 } 1855 else { 1856 if (conf.model) PR_Close(conf.model); 1857 rc = HANDLER_ERROR; 1858 } 1859 } 1860 1861 free(srvplug.cvlist); 1862 return rc; 1863 } 1864 1865 1866 SETDEFAULTS_FUNC(mod_nss_set_defaults) 1867 { 1868 static const config_plugin_keys_t cpk[] = { 1869 { CONST_STR_LEN("ssl.pemfile"), 1870 T_CONFIG_STRING, 1871 T_CONFIG_SCOPE_CONNECTION } 1872 ,{ CONST_STR_LEN("ssl.privkey"), 1873 T_CONFIG_STRING, 1874 T_CONFIG_SCOPE_CONNECTION } 1875 ,{ CONST_STR_LEN("ssl.ca-file"), 1876 T_CONFIG_STRING, 1877 T_CONFIG_SCOPE_CONNECTION } 1878 ,{ CONST_STR_LEN("ssl.ca-dn-file"), 1879 T_CONFIG_STRING, 1880 T_CONFIG_SCOPE_CONNECTION } 1881 ,{ CONST_STR_LEN("ssl.ca-crl-file"), 1882 T_CONFIG_STRING, 1883 T_CONFIG_SCOPE_CONNECTION } 1884 ,{ CONST_STR_LEN("ssl.read-ahead"), 1885 T_CONFIG_BOOL, 1886 T_CONFIG_SCOPE_CONNECTION } 1887 ,{ CONST_STR_LEN("ssl.disable-client-renegotiation"), 1888 T_CONFIG_BOOL, 1889 T_CONFIG_SCOPE_CONNECTION } 1890 ,{ CONST_STR_LEN("ssl.verifyclient.activate"), 1891 T_CONFIG_BOOL, 1892 T_CONFIG_SCOPE_CONNECTION } 1893 ,{ CONST_STR_LEN("ssl.verifyclient.enforce"), 1894 T_CONFIG_BOOL, 1895 T_CONFIG_SCOPE_CONNECTION } 1896 ,{ CONST_STR_LEN("ssl.verifyclient.depth"), 1897 T_CONFIG_SHORT, 1898 T_CONFIG_SCOPE_CONNECTION } 1899 ,{ CONST_STR_LEN("ssl.verifyclient.username"), 1900 T_CONFIG_STRING, 1901 T_CONFIG_SCOPE_CONNECTION } 1902 ,{ CONST_STR_LEN("ssl.verifyclient.exportcert"), 1903 T_CONFIG_BOOL, 1904 T_CONFIG_SCOPE_CONNECTION } 1905 ,{ CONST_STR_LEN("ssl.acme-tls-1"), 1906 T_CONFIG_STRING, 1907 T_CONFIG_SCOPE_CONNECTION } 1908 ,{ CONST_STR_LEN("ssl.stapling-file"), 1909 T_CONFIG_STRING, 1910 T_CONFIG_SCOPE_CONNECTION } 1911 ,{ CONST_STR_LEN("debug.log-ssl-noise"), 1912 T_CONFIG_SHORT, 1913 T_CONFIG_SCOPE_CONNECTION } 1914 ,{ NULL, 0, 1915 T_CONFIG_UNSET, 1916 T_CONFIG_SCOPE_UNSET } 1917 }; 1918 1919 plugin_data * const p = p_d; 1920 p->srv = srv; 1921 if (!config_plugin_values_init(srv, p, cpk, "mod_nss")) 1922 return HANDLER_ERROR; 1923 1924 /* process and validate config directives 1925 * (init i to 0 if global context; to 1 to skip empty global context) */ 1926 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 1927 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1928 config_plugin_value_t *pemfile = NULL; 1929 config_plugin_value_t *privkey = NULL; 1930 const buffer *ssl_stapling_file = NULL; 1931 for (; -1 != cpv->k_id; ++cpv) { 1932 switch (cpv->k_id) { 1933 case 0: /* ssl.pemfile */ 1934 if (!buffer_string_is_empty(cpv->v.b)) pemfile = cpv; 1935 break; 1936 case 1: /* ssl.privkey */ 1937 if (!buffer_string_is_empty(cpv->v.b)) privkey = cpv; 1938 break; 1939 case 2: /* ssl.ca-file */ 1940 if (!buffer_string_is_empty(cpv->v.b)) { 1941 CERTCertList *d = 1942 mod_nss_load_config_crts(cpv->v.b->ptr, srv->errh); 1943 if (d != NULL) { 1944 cpv->vtype = T_CONFIG_LOCAL; 1945 cpv->v.v = d; 1946 } 1947 else { 1948 log_error(srv->errh, __FILE__, __LINE__, 1949 "%s = %s", cpk[cpv->k_id].k, cpv->v.b->ptr); 1950 return HANDLER_ERROR; 1951 } 1952 } 1953 break; 1954 case 3: /* ssl.ca-dn-file */ 1955 if (!buffer_string_is_empty(cpv->v.b)) { 1956 CERTCertList *d = 1957 mod_nss_load_config_dncrts(cpv->v.b->ptr, srv->errh); 1958 if (d != NULL) { 1959 cpv->vtype = T_CONFIG_LOCAL; 1960 cpv->v.v = d; 1961 } 1962 else { 1963 log_error(srv->errh, __FILE__, __LINE__, 1964 "%s = %s", cpk[cpv->k_id].k, cpv->v.b->ptr); 1965 return HANDLER_ERROR; 1966 } 1967 } 1968 break; 1969 case 4: /* ssl.ca-crl-file */ 1970 if (!buffer_string_is_empty(cpv->v.b)) { 1971 CERTCertificateList *d = 1972 mod_nss_load_config_crls(cpv->v.b->ptr, srv->errh); 1973 if (d != NULL) { 1974 cpv->vtype = T_CONFIG_LOCAL; 1975 cpv->v.v = d; 1976 } 1977 else { 1978 log_error(srv->errh, __FILE__, __LINE__, 1979 "%s = %s", cpk[cpv->k_id].k, cpv->v.b->ptr); 1980 return HANDLER_ERROR; 1981 } 1982 } 1983 break; 1984 case 5: /* ssl.read-ahead */ 1985 case 6: /* ssl.disable-client-renegotiation */ 1986 case 7: /* ssl.verifyclient.activate */ 1987 case 8: /* ssl.verifyclient.enforce */ 1988 break; 1989 case 9: /* ssl.verifyclient.depth */ 1990 if (cpv->v.shrt > 255) { 1991 log_error(srv->errh, __FILE__, __LINE__, 1992 "NSS: %s is absurdly large (%hu); limiting to 255", 1993 cpk[cpv->k_id].k, cpv->v.shrt); 1994 cpv->v.shrt = 255; 1995 } 1996 break; 1997 case 10:/* ssl.verifyclient.username */ 1998 case 11:/* ssl.verifyclient.exportcert */ 1999 case 12:/* ssl.acme-tls-1 */ 2000 break; 2001 case 13:/* ssl.stapling-file */ 2002 ssl_stapling_file = cpv->v.b; 2003 break; 2004 case 14:/* debug.log-ssl-noise */ 2005 break; 2006 default:/* should not happen */ 2007 break; 2008 } 2009 } 2010 2011 if (pemfile) { 2012 if (NULL == privkey) privkey = pemfile; 2013 pemfile->v.v = 2014 network_nss_load_pemfile(srv, pemfile->v.b, privkey->v.b, 2015 ssl_stapling_file); 2016 if (pemfile->v.v) 2017 pemfile->vtype = T_CONFIG_LOCAL; 2018 else 2019 return HANDLER_ERROR; 2020 } 2021 } 2022 2023 p->defaults.ssl_verifyclient = 0; 2024 p->defaults.ssl_verifyclient_enforce = 1; 2025 p->defaults.ssl_verifyclient_depth = 9; 2026 p->defaults.ssl_verifyclient_export_cert = 0; 2027 p->defaults.ssl_disable_client_renegotiation = 1; 2028 p->defaults.ssl_read_ahead = 0; 2029 2030 /* initialize p->defaults from global config context */ 2031 if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 2032 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 2033 if (-1 != cpv->k_id) 2034 mod_nss_merge_config(&p->defaults, cpv); 2035 } 2036 2037 return mod_nss_set_defaults_sockets(srv, p); 2038 } 2039 2040 2041 /* local_send_buffer is a static buffer of size (LOCAL_SEND_BUFSIZE) 2042 * 2043 * buffer is allocated once, is NOT realloced (note: not thread-safe) 2044 * */ 2045 2046 /* copy small mem chunks into single large buffer 2047 * before PR_Write() to reduce number times write() called 2048 * underneath PR_Write() and potentially reduce number of packets 2049 * generated if TCP_NODELAY */ 2050 2051 2052 __attribute_cold__ 2053 static int 2054 mod_nss_write_err(connection *con, handler_ctx *hctx, size_t wr_len) 2055 { 2056 switch (PR_GetError()) { 2057 case PR_WOULD_BLOCK_ERROR: 2058 case PR_PENDING_INTERRUPT_ERROR: 2059 con->is_writable = -1; 2060 /* XXX: not handled: protocol might be blocked waiting on read */ 2061 /*if (0) con->is_readable = -1;*/ 2062 break; /* try again later */ 2063 case PR_CONNECT_RESET_ERROR: 2064 if (!hctx->conf.ssl_log_noise) return -1; 2065 __attribute_fallthrough__ 2066 default: 2067 elog(hctx->r->conf.errh, __FILE__, __LINE__, __func__); 2068 return -1; 2069 } 2070 2071 /* partial write; save attempted wr_len */ 2072 hctx->pending_write = wr_len; 2073 2074 return 0; /* try again later */ 2075 } 2076 2077 2078 __attribute_cold__ 2079 static int 2080 mod_nss_read_err(connection *con, handler_ctx *hctx) 2081 { 2082 switch (PR_GetError()) { 2083 case PR_WOULD_BLOCK_ERROR: 2084 case PR_PENDING_INTERRUPT_ERROR: 2085 /* XXX: not handled: protocol might be blocked waiting on write */ 2086 /*if (0) con->is_writable = -1;*/ 2087 con->is_readable = 0; 2088 return 0; 2089 case PR_CONNECT_ABORTED_ERROR: 2090 case PR_CONNECT_RESET_ERROR: 2091 if (!hctx->conf.ssl_log_noise) return -1; 2092 __attribute_fallthrough__ 2093 default: 2094 elog(hctx->errh, __FILE__, __LINE__, __func__); 2095 return -1; 2096 } 2097 } 2098 2099 2100 static int 2101 mod_nss_close_notify(handler_ctx *hctx); 2102 2103 2104 static int 2105 connection_write_cq_ssl (connection *con, chunkqueue *cq, off_t max_bytes) 2106 { 2107 handler_ctx *hctx = con->plugin_ctx[plugin_data_singleton->id]; 2108 PRFileDesc *ssl = hctx->ssl; 2109 log_error_st * const errh = hctx->errh; 2110 2111 if (0 != hctx->close_notify) return mod_nss_close_notify(hctx); 2112 2113 chunkqueue_remove_finished_chunks(cq); 2114 2115 /* future: for efficiency/performance might consider using NSS 2116 * PR_Writev() PR_TransmitFile() PR_SendFile() 2117 */ 2118 2119 while (max_bytes > 0 && !chunkqueue_is_empty(cq)) { 2120 char *data = local_send_buffer; 2121 uint32_t data_len = LOCAL_SEND_BUFSIZE < max_bytes 2122 ? LOCAL_SEND_BUFSIZE 2123 : (uint32_t)max_bytes; 2124 int wr; 2125 2126 if (0 != chunkqueue_peek_data(cq, &data, &data_len, errh)) return -1; 2127 2128 /*(if partial write occurred, expect that subsequent writes will have 2129 * at least that much data available from chunkqueue_peek_data(), which 2130 * is what should happen, but is not checked here)*/ 2131 size_t lim = hctx->pending_write; 2132 if (lim && data_len > lim) data_len = lim; 2133 hctx->pending_write = 0; 2134 2135 /* 2136 * XXX: above comments modified from mod_mbedtls; should be verified 2137 */ 2138 2139 int wr_total = 0; 2140 do { 2141 size_t wr_len = data_len; 2142 wr = PR_Write(ssl, data, (PRInt32)wr_len); 2143 if (wr <= 0) { 2144 if (wr_total) chunkqueue_mark_written(cq, wr_total); 2145 return mod_nss_write_err(con, hctx, wr_len); 2146 } 2147 wr_total += wr; 2148 data += wr; 2149 } while ((data_len -= wr)); 2150 chunkqueue_mark_written(cq, wr_total); 2151 max_bytes -= wr_total; 2152 } 2153 2154 return 0; 2155 } 2156 2157 2158 static void 2159 mod_nss_SSLHandshakeCallback (PRFileDesc *fd, void *arg) 2160 { 2161 UNUSED(fd); 2162 handler_ctx *hctx = arg; 2163 hctx->handshake = 1; 2164 } 2165 2166 2167 static int 2168 connection_read_cq_ssl (connection *con, chunkqueue *cq, off_t max_bytes) 2169 { 2170 handler_ctx *hctx = con->plugin_ctx[plugin_data_singleton->id]; 2171 2172 UNUSED(max_bytes); 2173 2174 if (0 != hctx->close_notify) return mod_nss_close_notify(hctx); 2175 2176 PRFileDesc *ssl = hctx->ssl; 2177 ssize_t len; 2178 char *mem = NULL; 2179 size_t mem_len = 0; 2180 do { 2181 int pend = SSL_DataPending(ssl); 2182 if (pend < 0) { 2183 len = pend; 2184 break; 2185 } 2186 mem_len = pend < 2048 ? 2048 : (uint32_t)pend; 2187 chunk * const ckpt = cq->last; 2188 mem = chunkqueue_get_memory(cq, &mem_len); 2189 2190 len = PR_Read(ssl, mem, (PRInt32)mem_len); 2191 if (len > 0) { 2192 chunkqueue_use_memory(cq, ckpt, len); 2193 con->bytes_read += len; 2194 } else { 2195 chunkqueue_use_memory(cq, ckpt, 0); 2196 } 2197 } while (len > 0); 2198 2199 if (hctx->alpn && hctx->handshake) { 2200 if (hctx->alpn == MOD_NSS_ALPN_ACME_TLS_1) { 2201 /* Once TLS handshake is complete, return -1 to result in 2202 * CON_STATE_ERROR so that socket connection is quickly closed */ 2203 return -1; 2204 } 2205 hctx->alpn = 0; 2206 } 2207 2208 if (len < 0) { 2209 return mod_nss_read_err(con, hctx); 2210 } else if (len == 0) { 2211 con->is_readable = 0; 2212 /* the other end closed the connection -> KEEP-ALIVE */ 2213 2214 return -2; 2215 } else { 2216 return 0; 2217 } 2218 } 2219 2220 2221 CONNECTION_FUNC(mod_nss_handle_con_accept) 2222 { 2223 server_socket *srv_sock = con->srv_socket; 2224 if (!srv_sock->is_ssl) return HANDLER_GO_ON; 2225 2226 plugin_data *p = p_d; 2227 handler_ctx * const hctx = handler_ctx_init(); 2228 request_st * const r = &con->request; 2229 hctx->r = r; 2230 hctx->con = con; 2231 hctx->tmp_buf = con->srv->tmp_buf; 2232 hctx->errh = r->conf.errh; 2233 con->plugin_ctx[p->id] = hctx; 2234 2235 plugin_ssl_ctx * const s = p->ssl_ctxs + srv_sock->sidx; 2236 hctx->ssl_session_ticket = s->ssl_session_ticket; 2237 2238 con->network_read = connection_read_cq_ssl; 2239 con->network_write = connection_write_cq_ssl; 2240 con->proto_default_port = 443; /* "https" */ 2241 mod_nss_patch_config(r, &hctx->conf); 2242 2243 hctx->ssl = mod_nss_io_ctor(con->fd, s->model, r->conf.errh); 2244 if (NULL == hctx->ssl) 2245 return HANDLER_ERROR; 2246 2247 /* future: move more config from here to config model in network_init_ssl(). 2248 * Callbacks need to be set here to be able to set callback arg to hctx */ 2249 2250 if (!hctx->conf.ssl_disable_client_renegotiation 2251 && SSL_OptionSet(hctx->ssl, SSL_ENABLE_RENEGOTIATION, 2252 SSL_RENEGOTIATE_REQUIRES_XTN) < 0) { 2253 elog(r->conf.errh, __FILE__, __LINE__, "SSL_ENABLE_RENEGOTIATION"); 2254 return HANDLER_ERROR; 2255 } 2256 2257 if (SSL_ResetHandshake(hctx->ssl, PR_TRUE) < 0) { 2258 elog(r->conf.errh, __FILE__, __LINE__, "SSL_ResetHandshake()"); 2259 return HANDLER_ERROR; 2260 } 2261 2262 if (SSL_HandshakeCallback(hctx->ssl, mod_nss_SSLHandshakeCallback, hctx)<0){ 2263 elog(r->conf.errh, __FILE__, __LINE__, "SSL_HandshakeCallback()"); 2264 return HANDLER_ERROR; 2265 } 2266 2267 if (SSL_SNISocketConfigHook(hctx->ssl, mod_nss_SNI, hctx) < 0) { 2268 elog(r->conf.errh, __FILE__, __LINE__, "SSL_SNISocketConfigHook()"); 2269 return HANDLER_ERROR; 2270 } 2271 2272 if (SSL_SetNextProtoCallback(hctx->ssl, mod_nss_alpn_select_cb, hctx) < 0) { 2273 elog(r->conf.errh, __FILE__, __LINE__, "SSL_SetNextProtoCallback()"); 2274 return HANDLER_ERROR; 2275 } 2276 2277 hctx->verify_status = -1; 2278 if (hctx->conf.ssl_verifyclient) { 2279 if (SSL_AuthCertificateHook(hctx->ssl, mod_nss_verify_cb, hctx) < 0) { 2280 elog(r->conf.errh, __FILE__, __LINE__, "SSL_AuthCertificateHook()"); 2281 return HANDLER_ERROR; 2282 } 2283 CERTCertList * const certList = hctx->conf.ssl_ca_dn_file 2284 ? hctx->conf.ssl_ca_dn_file 2285 : hctx->conf.ssl_ca_file; 2286 if (NULL == certList) 2287 log_error(hctx->r->conf.errh, __FILE__, __LINE__, 2288 "NSS: can't verify client without ssl.ca-file " 2289 "for TLS server name %s", 2290 hctx->r->uri.authority.ptr); /*(might not be set yet if no SNI)*/ 2291 if (certList && SSL_SetTrustAnchors(hctx->ssl, certList) < 0) { 2292 elog(r->conf.errh, __FILE__, __LINE__, "SSL_SetTrustAnchors()"); 2293 return HANDLER_ERROR; 2294 } 2295 SSL_OptionSet(hctx->ssl, SSL_REQUEST_CERTIFICATE, PR_TRUE); 2296 SSL_OptionSet(hctx->ssl, SSL_REQUIRE_CERTIFICATE, 2297 hctx->conf.ssl_verifyclient_enforce 2298 ? SSL_REQUIRE_ALWAYS 2299 : SSL_REQUIRE_NEVER); 2300 } 2301 else { 2302 SSL_OptionSet(hctx->ssl, SSL_REQUEST_CERTIFICATE, PR_FALSE); 2303 SSL_OptionSet(hctx->ssl, SSL_REQUIRE_CERTIFICATE, SSL_REQUIRE_NEVER); 2304 } 2305 2306 return HANDLER_GO_ON; 2307 } 2308 2309 2310 static void 2311 mod_nss_detach(handler_ctx *hctx) 2312 { 2313 /* step aside from further SSL processing 2314 * (note: additional data might be buffered/discarded by this layer) 2315 * (used after handle_connection_shut_wr hook) */ 2316 /* future: might restore prior network_read and network_write fn ptrs */ 2317 mod_nss_io_detach(hctx->ssl); 2318 hctx->con->is_ssl_sock = 0; 2319 /* if called after handle_connection_shut_wr hook, shutdown SHUT_WR */ 2320 if (-1 == hctx->close_notify) shutdown(hctx->con->fd, SHUT_WR); 2321 hctx->close_notify = 1; 2322 } 2323 2324 2325 CONNECTION_FUNC(mod_nss_handle_con_shut_wr) 2326 { 2327 plugin_data *p = p_d; 2328 handler_ctx *hctx = con->plugin_ctx[p->id]; 2329 if (NULL == hctx) return HANDLER_GO_ON; 2330 2331 hctx->close_notify = -2; 2332 if (hctx->handshake) { 2333 mod_nss_close_notify(hctx); 2334 } 2335 else { 2336 mod_nss_detach(hctx); 2337 } 2338 2339 return HANDLER_GO_ON; 2340 } 2341 2342 2343 static int 2344 mod_nss_close_notify (handler_ctx *hctx) 2345 { 2346 if (1 == hctx->close_notify) return -2; 2347 2348 /* note: this sends close_notify TLS alert and calls shutdown() on fd */ 2349 switch (PR_Shutdown(hctx->ssl, PR_SHUTDOWN_SEND)) { 2350 case PR_SUCCESS: 2351 mod_nss_detach(hctx); 2352 return -2; 2353 case PR_FAILURE: 2354 default: 2355 if (PR_GetError() != PR_NOT_CONNECTED_ERROR) 2356 elog(hctx->r->conf.errh, __FILE__, __LINE__, "PR_Shutdown()"); 2357 mod_nss_detach(hctx); 2358 return -1; 2359 } 2360 } 2361 2362 2363 CONNECTION_FUNC(mod_nss_handle_con_close) 2364 { 2365 plugin_data *p = p_d; 2366 handler_ctx *hctx = con->plugin_ctx[p->id]; 2367 if (NULL != hctx) { 2368 con->plugin_ctx[p->id] = NULL; 2369 if (1 != hctx->close_notify) 2370 mod_nss_close_notify(hctx); /*(one final try)*/ 2371 handler_ctx_free(hctx); 2372 } 2373 2374 return HANDLER_GO_ON; 2375 } 2376 2377 2378 __attribute_noinline__ 2379 static void 2380 https_add_ssl_client_cert (request_st * const r, CERTCertificate *peer) 2381 { 2382 char *pem = NSSBase64_EncodeItem(NULL, NULL, 0, &peer->derCert); 2383 if (NULL == pem) return; 2384 uint32_t len = 0; 2385 for (uint32_t i = 0; pem[i]; ++i) { 2386 if (pem[i] != '\r') pem[len++] = pem[i]; /*(translate \r\n to \n)*/ 2387 } 2388 buffer * const tb = r->tmp_buf; 2389 buffer_copy_string_len(tb, CONST_STR_LEN(PEM_BEGIN_CERT"\n")); 2390 buffer_append_string_len(tb, pem, len); 2391 buffer_append_string_len(tb,CONST_STR_LEN("\n"PEM_END_CERT"\n")); 2392 http_header_env_set(r, CONST_STR_LEN("SSL_CLIENT_CERT"), CONST_BUF_LEN(tb)); 2393 PORT_Free(pem); 2394 } 2395 2396 2397 static void 2398 https_add_ssl_client_subject (request_st * const r, CERTName * const subj) 2399 { 2400 /* add components of client Subject DN */ 2401 /* not complete list; NSS does not expose enough of lib/certdb/alg1485.c 2402 * for friendly names, though we could consider using CERT_GetOidString() 2403 * and CERT_RFC1485_EscapeAndQuote() */ 2404 static const 2405 struct { const char *tag; uint32_t tlen; char *(*fn)(const CERTName *); } 2406 comp[] = { 2407 { CONST_STR_LEN("CN"), CERT_GetCommonName }, 2408 { CONST_STR_LEN("ST"), CERT_GetStateName }, 2409 { CONST_STR_LEN("O"), CERT_GetOrgName }, 2410 { CONST_STR_LEN("OU"), CERT_GetOrgUnitName }, 2411 { CONST_STR_LEN("C"), CERT_GetCountryName }, 2412 { CONST_STR_LEN("L"), CERT_GetLocalityName }, 2413 { CONST_STR_LEN("UID"), CERT_GetCertUid }, 2414 { CONST_STR_LEN("emailAddress"), CERT_GetCertEmailAddress }, 2415 { CONST_STR_LEN("DC"), CERT_GetDomainComponentName }, 2416 }; 2417 buffer * const tb = r->tmp_buf; 2418 buffer_copy_string_len(tb, CONST_STR_LEN("SSL_CLIENT_S_DN_")); 2419 for (uint32_t i = 0; i < sizeof(comp)/sizeof(*comp); ++i) { 2420 char *s = comp[i].fn(subj); 2421 if (NULL == s) continue; 2422 2423 unsigned int n; 2424 unsigned char c; 2425 for (n = 0; (c = ((unsigned char *)s)[n]); ++n) { 2426 if (c < 32 || c == 127 || (c > 128 && c < 160)) s[n] = '?'; 2427 } 2428 2429 buffer_string_set_length(tb, sizeof("SSL_CLIENT_S_DN_")-1); 2430 buffer_append_string_len(tb, comp[i].tag, comp[i].tlen); 2431 http_header_env_set(r, CONST_BUF_LEN(tb), s, n); 2432 2433 PR_Free(s); 2434 } 2435 } 2436 2437 static void 2438 https_add_ssl_client_entries (request_st * const r, handler_ctx * const hctx) 2439 { 2440 PRFileDesc *ssl = hctx->ssl; 2441 CERTCertificate *crt = NULL; 2442 buffer * const tb = r->tmp_buf; 2443 2444 if (hctx->verify_status != -1) 2445 crt = SSL_PeerCertificate(ssl); 2446 if (NULL == crt) { /* || hctx->verify_status == -1) */ 2447 /*(e.g. no cert, or verify result not available)*/ 2448 http_header_env_set(r, 2449 CONST_STR_LEN("SSL_CLIENT_VERIFY"), 2450 CONST_STR_LEN("NONE")); 2451 return; 2452 } 2453 else if (0 != hctx->verify_status) { 2454 buffer_copy_string_len(tb, CONST_STR_LEN("FAILED:")); 2455 const char *s = PR_ErrorToName(hctx->verify_status); 2456 if (s) 2457 buffer_append_string_len(tb, s, strlen(s)); 2458 buffer_append_string_len(tb, CONST_STR_LEN(":")); 2459 s = PR_ErrorToString(hctx->verify_status, PR_LANGUAGE_I_DEFAULT); 2460 buffer_append_string_len(tb, s, strlen(s)); 2461 http_header_env_set(r, 2462 CONST_STR_LEN("SSL_CLIENT_VERIFY"), 2463 CONST_BUF_LEN(tb)); 2464 CERT_DestroyCertificate(crt); 2465 return; 2466 } 2467 else { 2468 http_header_env_set(r, 2469 CONST_STR_LEN("SSL_CLIENT_VERIFY"), 2470 CONST_STR_LEN("SUCCESS")); 2471 } 2472 2473 char *s = CERT_NameToAsciiInvertible(&crt->subject, CERT_N2A_STRICT); 2474 if (s) { 2475 http_header_env_set(r, 2476 CONST_STR_LEN("SSL_CLIENT_S_DN"), 2477 s, strlen(s)); 2478 PR_Free(s); 2479 } 2480 2481 https_add_ssl_client_subject(r, &crt->subject); 2482 2483 buffer_string_set_length(tb, 0); 2484 buffer_append_uint_hex_lc(tb, DER_GetInteger(&crt->serialNumber)); 2485 http_header_env_set(r, 2486 CONST_STR_LEN("SSL_CLIENT_M_SERIAL"), 2487 CONST_BUF_LEN(tb)); 2488 2489 if (!buffer_string_is_empty(hctx->conf.ssl_verifyclient_username)) { 2490 /* pick one of the exported values as "REMOTE_USER", for example 2491 * ssl.verifyclient.username = "SSL_CLIENT_S_DN_UID" 2492 * or 2493 * ssl.verifyclient.username = "SSL_CLIENT_S_DN_emailAddress" 2494 */ 2495 const buffer *varname = hctx->conf.ssl_verifyclient_username; 2496 const buffer *vb = http_header_env_get(r, CONST_BUF_LEN(varname)); 2497 if (vb) { /* same as http_auth.c:http_auth_setenv() */ 2498 http_header_env_set(r, 2499 CONST_STR_LEN("REMOTE_USER"), 2500 CONST_BUF_LEN(vb)); 2501 http_header_env_set(r, 2502 CONST_STR_LEN("AUTH_TYPE"), 2503 CONST_STR_LEN("SSL_CLIENT_VERIFY")); 2504 } 2505 } 2506 2507 if (hctx->conf.ssl_verifyclient_export_cert) 2508 https_add_ssl_client_cert(r, crt); 2509 2510 CERT_DestroyCertificate(crt); 2511 } 2512 2513 2514 static void 2515 http_cgi_ssl_env (request_st * const r, handler_ctx * const hctx) 2516 { 2517 PRFileDesc *ssl = hctx->ssl; 2518 2519 /* (quite a bit of work just to get protocol version) 2520 * (could not find better NSS interface) */ 2521 SSLChannelInfo inf; 2522 if (SSL_GetChannelInfo(ssl, &inf, sizeof(inf)) < 0) 2523 inf.protocolVersion = 0; 2524 2525 size_t n; 2526 const char *s = NULL; 2527 switch (inf.protocolVersion) { 2528 case SSL_LIBRARY_VERSION_TLS_1_3: s="TLSv1.3";n=sizeof("TLSv1.3")-1;break; 2529 case SSL_LIBRARY_VERSION_TLS_1_2: s="TLSv1.2";n=sizeof("TLSv1.2")-1;break; 2530 case SSL_LIBRARY_VERSION_TLS_1_1: s="TLSv1.1";n=sizeof("TLSv1.1")-1;break; 2531 case SSL_LIBRARY_VERSION_TLS_1_0: s="TLSv1.0";n=sizeof("TLSv1.0")-1;break; 2532 case SSL_LIBRARY_VERSION_3_0: s="SSLv3.0";n=sizeof("SSLv3.0")-1;break; 2533 case SSL_LIBRARY_VERSION_2: s="SSLv2.0";n=sizeof("SSLv2.0")-1;break; 2534 default: break; 2535 } 2536 if (s) http_header_env_set(r, CONST_STR_LEN("SSL_PROTOCOL"), s, n); 2537 2538 char *cipher; 2539 int algkeysize; 2540 int usekeysize; 2541 if (SSL_SecurityStatus(ssl, NULL, &cipher, &algkeysize, &usekeysize, 2542 NULL, NULL) < 0) 2543 return; 2544 2545 if (cipher) { 2546 n = strlen(cipher); 2547 http_header_env_set(r, CONST_STR_LEN("SSL_CIPHER"), cipher, n); 2548 PR_Free(cipher); 2549 } 2550 2551 /* SSL_CIPHER_ALGKEYSIZE - Number of cipher bits (possible) */ 2552 /* SSL_CIPHER_USEKEYSIZE - Number of cipher bits (actually used) */ 2553 char buf[LI_ITOSTRING_LENGTH]; 2554 http_header_env_set(r, CONST_STR_LEN("SSL_CIPHER_USEKEYSIZE"), 2555 buf, li_utostrn(buf, sizeof(buf), usekeysize)); 2556 http_header_env_set(r, CONST_STR_LEN("SSL_CIPHER_ALGKEYSIZE"), 2557 buf, li_utostrn(buf, sizeof(buf), algkeysize)); 2558 } 2559 2560 2561 REQUEST_FUNC(mod_nss_handle_request_env) 2562 { 2563 plugin_data *p = p_d; 2564 /* simple flag for request_env_patched */ 2565 if (r->plugin_ctx[p->id]) return HANDLER_GO_ON; 2566 handler_ctx *hctx = r->con->plugin_ctx[p->id]; 2567 if (NULL == hctx) return HANDLER_GO_ON; 2568 r->plugin_ctx[p->id] = (void *)(uintptr_t)1u; 2569 2570 http_cgi_ssl_env(r, hctx); 2571 if (hctx->conf.ssl_verifyclient) { 2572 https_add_ssl_client_entries(r, hctx); 2573 } 2574 2575 return HANDLER_GO_ON; 2576 } 2577 2578 2579 REQUEST_FUNC(mod_nss_handle_uri_raw) 2580 { 2581 /* mod_nss must be loaded prior to mod_auth 2582 * if mod_nss is configured to set REMOTE_USER based on client cert */ 2583 /* mod_nss must be loaded after mod_extforward 2584 * if mod_nss config is based on lighttpd.conf remote IP conditional 2585 * using remote IP address set by mod_extforward, *unless* PROXY protocol 2586 * is enabled with extforward.hap-PROXY = "enable", in which case the 2587 * reverse is true: mod_extforward must be loaded after mod_nss */ 2588 plugin_data *p = p_d; 2589 handler_ctx *hctx = r->con->plugin_ctx[p->id]; 2590 if (NULL == hctx) return HANDLER_GO_ON; 2591 2592 mod_nss_patch_config(r, &hctx->conf); 2593 if (hctx->conf.ssl_verifyclient) { 2594 mod_nss_handle_request_env(r, p); 2595 } 2596 2597 return HANDLER_GO_ON; 2598 } 2599 2600 2601 REQUEST_FUNC(mod_nss_handle_request_reset) 2602 { 2603 plugin_data *p = p_d; 2604 r->plugin_ctx[p->id] = NULL; /* simple flag for request_env_patched */ 2605 return HANDLER_GO_ON; 2606 } 2607 2608 2609 TRIGGER_FUNC(mod_nss_handle_trigger) { 2610 const plugin_data * const p = p_d; 2611 const time_t cur_ts = log_epoch_secs; 2612 if (cur_ts & 0x3f) return HANDLER_GO_ON; /*(continue once each 64 sec)*/ 2613 2614 mod_nss_refresh_stapling_files(srv, p, cur_ts); 2615 2616 return HANDLER_GO_ON; 2617 } 2618 2619 2620 int mod_nss_plugin_init (plugin *p); 2621 int mod_nss_plugin_init (plugin *p) 2622 { 2623 p->version = LIGHTTPD_VERSION_ID; 2624 p->name = "nss"; 2625 p->init = mod_nss_init; 2626 p->cleanup = mod_nss_free; 2627 p->priv_defaults= mod_nss_set_defaults; 2628 2629 p->handle_connection_accept = mod_nss_handle_con_accept; 2630 p->handle_connection_shut_wr = mod_nss_handle_con_shut_wr; 2631 p->handle_connection_close = mod_nss_handle_con_close; 2632 p->handle_uri_raw = mod_nss_handle_uri_raw; 2633 p->handle_request_env = mod_nss_handle_request_env; 2634 p->handle_request_reset = mod_nss_handle_request_reset; 2635 p->handle_trigger = mod_nss_handle_trigger; 2636 2637 return 0; 2638 } 2639 2640 2641 static int 2642 mod_nss_ssl_conf_curves(server *srv, plugin_config_socket *s, const buffer *curvelist) 2643 { 2644 log_error(srv->errh, __FILE__, __LINE__, 2645 "NSS: ignoring Curves/Groups; not implemented (%s)", 2646 curvelist->ptr); 2647 UNUSED(s); 2648 UNUSED(curvelist); 2649 2650 /* XXX: TODO: see ssl/sslt.h enum SSLNamedGroup */ 2651 2652 return 1; 2653 } 2654 2655 2656 static PRUint16 2657 mod_nss_ssl_conf_proto_val (server *srv, plugin_config_socket *s, const buffer *b, int max) 2658 { 2659 /* use of SSL v3 should be avoided, and SSL v2 is not supported here */ 2660 /*(choosing not to support s->ssl_use_sslv2 or SSL_LIBRARY_VERSION_2 here)*/ 2661 if (NULL == b) /* default: min TLSv1.2, max TLSv1.3 */ 2662 return max ? SSL_LIBRARY_VERSION_TLS_1_3 : SSL_LIBRARY_VERSION_TLS_1_2; 2663 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("None"))) /*"disable" limit*/ 2664 return max 2665 ? SSL_LIBRARY_VERSION_TLS_1_3 2666 : (s->ssl_use_sslv3 2667 ? SSL_LIBRARY_VERSION_3_0 2668 : SSL_LIBRARY_VERSION_TLS_1_0); 2669 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("SSLv3"))) 2670 return SSL_LIBRARY_VERSION_3_0; 2671 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("TLSv1.0"))) 2672 return SSL_LIBRARY_VERSION_TLS_1_0; 2673 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("TLSv1.1"))) 2674 return SSL_LIBRARY_VERSION_TLS_1_1; 2675 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("TLSv1.2"))) 2676 return SSL_LIBRARY_VERSION_TLS_1_2; 2677 else if (buffer_eq_icase_slen(b, CONST_STR_LEN("TLSv1.3"))) 2678 return SSL_LIBRARY_VERSION_TLS_1_3; 2679 else { 2680 if (buffer_eq_icase_slen(b, CONST_STR_LEN("DTLSv1")) 2681 || buffer_eq_icase_slen(b, CONST_STR_LEN("DTLSv1.2"))) 2682 log_error(srv->errh, __FILE__, __LINE__, 2683 "NSS: ssl.openssl.ssl-conf-cmd %s %s ignored", 2684 max ? "MaxProtocol" : "MinProtocol", b->ptr); 2685 else 2686 log_error(srv->errh, __FILE__, __LINE__, 2687 "NSS: ssl.openssl.ssl-conf-cmd %s %s invalid; ignored", 2688 max ? "MaxProtocol" : "MinProtocol", b->ptr); 2689 } 2690 return max ? SSL_LIBRARY_VERSION_TLS_1_3 : SSL_LIBRARY_VERSION_TLS_1_2; 2691 } 2692 2693 2694 static void 2695 mod_nss_ssl_conf_proto (server *srv, plugin_config_socket *s, const buffer *minb, const buffer *maxb) 2696 { 2697 s->protos.min = mod_nss_ssl_conf_proto_val(srv, s, minb, 0); 2698 s->protos.max = mod_nss_ssl_conf_proto_val(srv, s, maxb, 1); 2699 /* XXX: could check values against SSL_VersionRangeGetSupported() */ 2700 } 2701 2702 2703 /** 2704 * Apache mod_nss 2705 * https://pagure.io/mod_nss.git 2706 * 2707 * (with minor modifications to compile in lighttpd) 2708 */ 2709 2710 #define ap_log_error(APLOG_MARK, APLOG_INFO, rc, errh, ...) \ 2711 log_error(errh, __FILE__, __LINE__, __VA_ARGS__) 2712 typedef log_error_st server_rec; 2713 2714 2715 /* 2716 * mod_nss/nss_engine_cipher.h 2717 */ 2718 2719 /* Copyright 2001-2004 The Apache Software Foundation 2720 * 2721 * Licensed under the Apache License, Version 2.0 (the "License"); 2722 * you may not use this file except in compliance with the License. 2723 * You may obtain a copy of the License at 2724 * 2725 * http://www.apache.org/licenses/LICENSE-2.0 2726 * 2727 * Unless required by applicable law or agreed to in writing, software 2728 * distributed under the License is distributed on an "AS IS" BASIS, 2729 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2730 * See the License for the specific language governing permissions and 2731 * limitations under the License. 2732 */ 2733 2734 /* 2735 * Cipher definitions 2736 */ 2737 typedef struct 2738 { 2739 const char *name; /* The mod_nss cipher name */ 2740 PRInt32 num; /* The cipher id */ 2741 const char *openssl_name; /* The OpenSSL cipher name */ 2742 PRInt32 attr; /* cipher attributes: algorithms, etc */ 2743 PRInt32 version; /* protocol version valid for this cipher */ 2744 PRInt32 strength; /* LOW, MEDIUM, HIGH */ 2745 PRInt32 bits; /* bits of strength */ 2746 PRInt32 alg_bits; /* bits of the algorithm */ 2747 const char *alias; /* Other names, usually typos. Right now a 2748 single string but could be CSV */ 2749 } cipher_properties; 2750 2751 /* OpenSSL-compatible cipher attributes */ 2752 #define SSL_kRSA 0x00000001L 2753 #define SSL_aRSA 0x00000002L 2754 #define SSL_aDSS 0x00000004L 2755 #define SSL_DSS SSL_aDSS 2756 #define SSL_eNULL 0x00000008L 2757 #define SSL_DES 0x00000010L 2758 #define SSL_3DES 0x00000020L 2759 #define SSL_RC4 0x00000040L 2760 #define SSL_RC2 0x00000080L 2761 #define SSL_MD5 0x00000200L 2762 #define SSL_SHA1 0x00000400L 2763 #define SSL_SHA SSL_SHA1 2764 #define SSL_RSA (SSL_kRSA) 2765 #define SSL_kEDH 0x00000800L 2766 #define SSL_EDH (SSL_kDHE) 2767 #define SSL_aNULL 0x00001000L 2768 #define SSL_kECDHE 0x00002000L 2769 #define SSL_AECDH 0x00004000L 2770 #define SSL_aECDSA 0x00008000L 2771 #define SSL_kECDHr 0x00010000L 2772 #define SSL_kEECDH 0x00020000L 2773 #define SSL_ECDH (SSL_kECDHE|SSL_kECDHr|SSL_kEECDH) 2774 #define SSL_EECDH (SSL_kEECDH) 2775 #define SSL_ADH (SSL_kEDH) 2776 #define SSL_kDHE 0x00040000L 2777 #define SSL_DHE (SSL_kDHE) 2778 2779 /* cipher strength */ 2780 #define SSL_STRONG_NONE 0x00000001L 2781 #define SSL_NULL 0x00000002L 2782 #define SSL_EXPORT40 0x00000004L 2783 #define SSL_EXPORT56 0x00000008L 2784 #define SSL_LOW 0x00000010L 2785 #define SSL_MEDIUM 0x00000020L 2786 #define SSL_HIGH 0x00000040L 2787 2788 #define SSL_CHACHA20POLY1305 0x00080000L 2789 #define SSL_AES128 0x00400000L 2790 #define SSL_AES256 0x00800000L 2791 #define SSL_CAMELLIA128 0x01000000L 2792 #define SSL_CAMELLIA256 0x02000000L 2793 #define SSL_AES128GCM 0x04000000L 2794 #define SSL_AES256GCM 0x08000000L 2795 #define SSL_SHA256 0x10000000L 2796 #define SSL_SHA384 0x20000000L 2797 #define SSL_AEAD 0x40000000L 2798 2799 #define SSL_AES (SSL_AES128|SSL_AES256|SSL_AES128GCM|SSL_AES256GCM) 2800 #define SSL_CAMELLIA (SSL_CAMELLIA128|SSL_CAMELLIA256) 2801 2802 /* Protocols */ 2803 #define SSLV2 0x00000001L 2804 #define SSLV3 0x00000002L 2805 #define TLSV1 SSLV3 2806 #define TLSV1_2 0x00000004L 2807 #define TLSV1_3 0x00000005L 2808 2809 #if 0 2810 /* the table itself is defined in nss_engine_cipher.c */ 2811 #if 0 2812 #ifdef NSS_ENABLE_ECC 2813 # ifdef ENABLE_SHA384 2814 # define ciphernum 54 2815 # else 2816 # define ciphernum 49 2817 # endif 2818 #else 2819 #define ciphernum 20 2820 #endif 2821 #endif 2822 2823 extern int ciphernum; 2824 2825 /* function prototypes */ 2826 int nss_parse_ciphers(server_rec *s, char *ciphers, PRBool cipher_list[ciphernum]); 2827 int countciphers(PRBool cipher_state[ciphernum], int version); 2828 #endif 2829 2830 /* I chose an arbitrary cipher to test the existence for to handle older 2831 * versions of NSS, at least back to 3.15.1 2832 */ 2833 #ifndef TLS_NULL_WITH_NULL_NULL 2834 #define TLS_NULL_WITH_NULL_NULL SSL_NULL_WITH_NULL_NULL 2835 #define TLS_RSA_WITH_NULL_MD5 SSL_RSA_WITH_NULL_MD5 2836 #define TLS_RSA_WITH_NULL_SHA SSL_RSA_WITH_NULL_SHA 2837 #define TLS_RSA_EXPORT_WITH_RC4_40_MD5 SSL_RSA_EXPORT_WITH_RC4_40_MD5 2838 #define TLS_RSA_WITH_RC4_128_MD5 SSL_RSA_WITH_RC4_128_MD5 2839 #define TLS_RSA_WITH_RC4_128_SHA SSL_RSA_WITH_RC4_128_SHA 2840 #define TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5 2841 #define TLS_RSA_WITH_IDEA_CBC_SHA SSL_RSA_WITH_IDEA_CBC_SHA 2842 #define TLS_RSA_EXPORT_WITH_DES40_CBC_SHA SSL_RSA_EXPORT_WITH_DES40_CBC_SHA 2843 #define TLS_RSA_WITH_DES_CBC_SHA SSL_RSA_WITH_DES_CBC_SHA 2844 #define TLS_RSA_WITH_3DES_EDE_CBC_SHA SSL_RSA_WITH_3DES_EDE_CBC_SHA 2845 #endif 2846 2847 2848 /* 2849 * mod_nss/nss_engine_cipher.c 2850 */ 2851 2852 /* Copyright 2001-2004 The Apache Software Foundation 2853 * 2854 * Licensed under the Apache License, Version 2.0 (the "License"); 2855 * you may not use this file except in compliance with the License. 2856 * You may obtain a copy of the License at 2857 * 2858 * http://www.apache.org/licenses/LICENSE-2.0 2859 * 2860 * Unless required by applicable law or agreed to in writing, software 2861 * distributed under the License is distributed on an "AS IS" BASIS, 2862 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 2863 * See the License for the specific language governing permissions and 2864 * limitations under the License. 2865 */ 2866 #include <sys/types.h> 2867 #include <string.h> 2868 #include <strings.h> 2869 #include <stdio.h> 2870 #include <stdlib.h> 2871 #if defined(__CYGWIN__) 2872 #include <nss/sslproto.h> 2873 #else 2874 #include <nss3/sslproto.h> 2875 #endif 2876 2877 /* Cipher actions */ 2878 #define PERMANENTLY_DISABLE_CIPHER -1 /* !CIPHER */ 2879 #define SUBTRACT_CIPHER 0 /* -CIPHER */ 2880 #define ENABLE_CIPHER 1 /* CIPHER */ 2881 #define REORDER_CIPHER 2 /* +CIPHER */ 2882 2883 ///* ciphernum is defined in nss_engine_cipher.h */ 2884 static const cipher_properties ciphers_def[] = 2885 { 2886 {"rsa_null_md5", TLS_RSA_WITH_NULL_MD5, "NULL-MD5", SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_MD5, SSLV3, SSL_STRONG_NONE, 0, 0, NULL}, 2887 {"rsa_null_sha", TLS_RSA_WITH_NULL_SHA, "NULL-SHA", SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_SHA1, SSLV3, SSL_STRONG_NONE, 0, 0, NULL}, 2888 {"rsa_rc4_40_md5", TLS_RSA_EXPORT_WITH_RC4_40_MD5, "EXP-RC4-MD5", SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSLV3, SSL_EXPORT40, 40, 128, NULL}, 2889 {"rsa_rc4_128_md5", TLS_RSA_WITH_RC4_128_MD5, "RC4-MD5", SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5, SSLV3, SSL_MEDIUM, 128, 128, NULL}, 2890 {"rsa_rc4_128_sha", TLS_RSA_WITH_RC4_128_SHA, "RC4-SHA", SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA1, SSLV3, SSL_MEDIUM, 128, 128, NULL}, 2891 {"rsa_rc2_40_md5", TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5, "EXP-RC2-CBC-MD5", SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5, SSLV3, SSL_EXPORT40, 40, 128, NULL}, 2892 /* TLS_RSA_EXPORT_WITH_DES40_CBC_SHA not implemented 0x0008 */ 2893 {"rsa_des_sha", TLS_RSA_WITH_DES_CBC_SHA, "DES-CBC-SHA", SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1, SSLV3, SSL_LOW, 56, 56, NULL}, 2894 {"rsa_3des_sha", TLS_RSA_WITH_3DES_EDE_CBC_SHA, "DES-CBC3-SHA", SSL_kRSA|SSL_aRSA|SSL_3DES|SSL_SHA1, SSLV3, SSL_MEDIUM, 168, 168, NULL}, 2895 //#ifdef ENABLE_SERVER_DHE 2896 {"dhe_rsa_des_sha", TLS_DHE_RSA_WITH_DES_CBC_SHA, "EDH-RSA-DES-CBC-SHA", SSL_kDHE|SSL_aRSA|SSL_DES|SSL_SHA1, SSLV3, SSL_LOW, 56, 56, NULL}, 2897 //#endif 2898 {"rsa_aes_128_sha", TLS_RSA_WITH_AES_128_CBC_SHA, "AES128-SHA", SSL_kRSA|SSL_aRSA|SSL_AES128|SSL_SHA1, TLSV1, SSL_HIGH, 128, 128, NULL}, 2899 {"rsa_aes_256_sha", TLS_RSA_WITH_AES_256_CBC_SHA, "AES256-SHA", SSL_kRSA|SSL_aRSA|SSL_AES256|SSL_SHA1, TLSV1, SSL_HIGH, 256, 256, NULL}, 2900 {"null_sha_256", TLS_RSA_WITH_NULL_SHA256, "NULL-SHA256", SSL_kRSA|SSL_aRSA|SSL_eNULL|SSL_SHA256, TLSV1_2, SSL_STRONG_NONE, 0, 0, NULL}, 2901 {"aes_128_sha_256", TLS_RSA_WITH_AES_128_CBC_SHA256, "AES128-SHA256", SSL_kRSA|SSL_aRSA|SSL_AES128|SSL_SHA256, TLSV1_2, SSL_HIGH, 128, 128, NULL}, 2902 {"aes_256_sha_256", TLS_RSA_WITH_AES_256_CBC_SHA256, "AES256-SHA256", SSL_kRSA|SSL_aRSA|SSL_AES256|SSL_SHA256, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2903 {"camellia_128_sha", TLS_RSA_WITH_CAMELLIA_128_CBC_SHA, "CAMELLIA128-SHA", SSL_kRSA|SSL_aRSA|SSL_CAMELLIA128|SSL_SHA1, TLSV1, SSL_HIGH, 128, 128, "camelia_128_sha"}, 2904 {"rsa_des_56_sha", TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA, "EXP1024-DES-CBC-SHA", SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1, TLSV1, SSL_EXPORT56, 56, 56, NULL}, 2905 {"rsa_rc4_56_sha", TLS_RSA_EXPORT1024_WITH_RC4_56_SHA, "EXP1024-RC4-SHA", SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA1, TLSV1, SSL_EXPORT56, 56, 128, NULL}, 2906 {"camellia_256_sha", TLS_RSA_WITH_CAMELLIA_256_CBC_SHA, "CAMELLIA256-SHA", SSL_kRSA|SSL_aRSA|SSL_CAMELLIA256|SSL_SHA1, TLSV1, SSL_HIGH, 256, 256, "camelia_256_sha"}, 2907 //#ifdef ENABLE_GCM 2908 {"rsa_aes_128_gcm_sha_256", TLS_RSA_WITH_AES_128_GCM_SHA256, "AES128-GCM-SHA256", SSL_kRSA|SSL_aRSA|SSL_AES128GCM|SSL_AEAD, TLSV1_2, SSL_HIGH, 128, 128, NULL}, 2909 //#endif 2910 //#ifdef ENABLE_SHA384 2911 {"rsa_aes_256_gcm_sha_384", TLS_RSA_WITH_AES_256_GCM_SHA384, "AES256-GCM-SHA384", SSL_kRSA|SSL_aRSA|SSL_AES256GCM|SSL_AEAD, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2912 //#endif 2913 {"fips_3des_sha", SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA, "FIPS-DES-CBC3-SHA", SSL_kRSA|SSL_aRSA|SSL_3DES|SSL_SHA1, SSLV3, SSL_MEDIUM, 112, 168, NULL}, 2914 {"fips_des_sha", SSL_RSA_FIPS_WITH_DES_CBC_SHA, "FIPS-DES-CBC-SHA", SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1, SSLV3, SSL_LOW, 56, 56, NULL}, 2915 //#ifdef ENABLE_SERVER_DHE 2916 {"dhe_rsa_3des_sha", TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, "DHE-RSA-DES-CBC3-SHA", SSL_kDHE|SSL_aRSA|SSL_3DES|SSL_SHA1, TLSV1, SSL_MEDIUM, 112, 168, NULL}, 2917 {"dhe_rsa_aes_128_sha", TLS_DHE_RSA_WITH_AES_128_CBC_SHA, "DHE-RSA-AES128-SHA", SSL_kDHE|SSL_aRSA|SSL_AES128|SSL_SHA1, TLSV1, SSL_HIGH, 128, 128, NULL}, 2918 {"dhe_rsa_aes_256_sha", TLS_DHE_RSA_WITH_AES_256_CBC_SHA, "DHE-RSA-AES256-SHA", SSL_kDHE|SSL_aRSA|SSL_AES256|SSL_SHA1, TLSV1, SSL_HIGH, 256, 256, NULL}, 2919 {"dhe_rsa_camellia_128_sha", TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA, "DHE-RSA-CAMELLIA128-SHA", SSL_kDHE|SSL_aRSA|SSL_CAMELLIA128|SSL_SHA1, TLSV1, SSL_HIGH, 128, 128, NULL}, 2920 {"dhe_rsa_camellia_256_sha", TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA, "DHE-RSA-CAMELLIA256-SHA", SSL_kDHE|SSL_aRSA|SSL_CAMELLIA256|SSL_SHA1, TLSV1, SSL_HIGH, 256, 256, NULL}, 2921 {"dhe_rsa_aes_128_sha_256", TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, "DHE-RSA-AES128-SHA256", SSL_kDHE|SSL_aRSA|SSL_AES128|SSL_SHA256, TLSV1_2, SSL_HIGH, 128, 128, "dhe_rsa_aes_128_sha256"}, 2922 {"dhe_rsa_aes_256_sha_256", TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, "DHE-RSA-AES256-SHA256", SSL_kDHE|SSL_aRSA|SSL_AES256|SSL_SHA256, TLSV1_2, SSL_HIGH, 256, 256, "dhe_rsa_aes_256_sha256"}, 2923 //#ifdef ENABLE_GCM 2924 {"dhe_rsa_aes_128_gcm_sha_256", TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, "DHE-RSA-AES128-GCM-SHA256", SSL_kDHE|SSL_aRSA|SSL_AES128GCM|SSL_AEAD, TLSV1_2, SSL_HIGH, 128, 128, NULL}, 2925 //#endif 2926 //#ifdef ENABLE_SHA384 2927 {"dhe_rsa_aes_256_gcm_sha_384", TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, "DHE-RSA-AES256-GCM-SHA384", SSL_kDHE|SSL_aRSA|SSL_AES256GCM|SSL_AEAD, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2928 //#endif 2929 //#endif /* ENABLE_SERVER_DHE */ 2930 //#ifdef NSS_ENABLE_ECC 2931 {"ecdh_ecdsa_null_sha", TLS_ECDH_ECDSA_WITH_NULL_SHA, "ECDH-ECDSA-NULL-SHA", SSL_kECDHE|SSL_AECDH|SSL_eNULL|SSL_SHA1, TLSV1, SSL_STRONG_NONE, 0, 0, NULL}, 2932 {"ecdh_ecdsa_rc4_128_sha", TLS_ECDH_ECDSA_WITH_RC4_128_SHA, "ECDH-ECDSA-RC4-SHA", SSL_kECDHE|SSL_AECDH|SSL_RC4|SSL_SHA1, TLSV1, SSL_MEDIUM, 128, 128, NULL}, 2933 {"ecdh_ecdsa_3des_sha", TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, "ECDH-ECDSA-DES-CBC3-SHA", SSL_kECDHE|SSL_AECDH|SSL_3DES|SSL_SHA1, TLSV1, SSL_MEDIUM, 112, 168, NULL}, 2934 {"ecdh_ecdsa_aes_128_sha", TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, "ECDH-ECDSA-AES128-SHA", SSL_kECDHE|SSL_AECDH|SSL_AES128|SSL_SHA1, TLSV1, SSL_HIGH, 128, 128, NULL}, 2935 {"ecdh_ecdsa_aes_256_sha", TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, "ECDH-ECDSA-AES256-SHA", SSL_kECDHE|SSL_AECDH|SSL_AES256|SSL_SHA1, TLSV1, SSL_HIGH, 256, 256, NULL}, 2936 {"ecdhe_ecdsa_null_sha", TLS_ECDHE_ECDSA_WITH_NULL_SHA, "ECDHE-ECDSA-NULL-SHA", SSL_kEECDH|SSL_aECDSA|SSL_eNULL|SSL_SHA1, TLSV1, SSL_STRONG_NONE, 0, 0, NULL}, 2937 {"ecdhe_ecdsa_rc4_128_sha", TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "ECDHE-ECDSA-RC4-SHA", SSL_kEECDH|SSL_aECDSA|SSL_RC4|SSL_SHA1, TLSV1, SSL_MEDIUM, 128, 128, NULL}, 2938 {"ecdhe_ecdsa_3des_sha", TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, "ECDHE-ECDSA-DES-CBC3-SHA", SSL_kEECDH|SSL_aECDSA|SSL_3DES|SSL_SHA1, TLSV1, SSL_MEDIUM, 112, 168, NULL}, 2939 {"ecdhe_ecdsa_aes_128_sha", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "ECDHE-ECDSA-AES128-SHA", SSL_kEECDH|SSL_aECDSA|SSL_AES128|SSL_SHA1, TLSV1, SSL_HIGH, 128, 128, NULL}, 2940 {"ecdhe_ecdsa_aes_256_sha", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "ECDHE-ECDSA-AES256-SHA", SSL_kEECDH|SSL_aECDSA|SSL_AES256|SSL_SHA1, TLSV1, SSL_HIGH, 256, 256, NULL}, 2941 {"ecdh_rsa_null_sha", TLS_ECDH_RSA_WITH_NULL_SHA, "ECDH-RSA-NULL-SHA", SSL_kECDHr|SSL_AECDH|SSL_eNULL|SSL_SHA1, TLSV1, SSL_STRONG_NONE, 0, 0, NULL}, 2942 {"ecdh_rsa_128_sha", TLS_ECDH_RSA_WITH_RC4_128_SHA, "ECDH-RSA-RC4-SHA", SSL_kECDHr|SSL_AECDH|SSL_RC4|SSL_SHA1, TLSV1, SSL_MEDIUM, 128, 128, NULL}, 2943 {"ecdh_rsa_3des_sha", TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, "ECDH-RSA-DES-CBC3-SHA", SSL_kECDHr|SSL_AECDH|SSL_3DES|SSL_SHA1, TLSV1, SSL_MEDIUM, 112, 168, NULL}, 2944 {"ecdh_rsa_aes_128_sha", TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, "ECDH-RSA-AES128-SHA", SSL_kECDHr|SSL_AECDH|SSL_AES128|SSL_SHA1, TLSV1, SSL_HIGH, 128, 128, NULL}, 2945 {"ecdh_rsa_aes_256_sha", TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, "ECDH-RSA-AES256-SHA", SSL_kECDHr|SSL_AECDH|SSL_AES256|SSL_SHA1, TLSV1, SSL_HIGH, 256, 256, NULL}, 2946 {"ecdhe_rsa_null", TLS_ECDHE_RSA_WITH_NULL_SHA, "ECDHE-RSA-NULL-SHA", SSL_kEECDH|SSL_aRSA|SSL_eNULL|SSL_SHA1, TLSV1, SSL_STRONG_NONE, 0, 0, NULL}, 2947 {"ecdhe_rsa_rc4_128_sha", TLS_ECDHE_RSA_WITH_RC4_128_SHA, "ECDHE-RSA-RC4-SHA", SSL_kEECDH|SSL_aRSA|SSL_RC4|SSL_SHA1, TLSV1, SSL_MEDIUM, 128, 128, NULL}, 2948 {"ecdhe_rsa_3des_sha", TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "ECDHE-RSA-DES-CBC3-SHA", SSL_kEECDH|SSL_aRSA|SSL_3DES|SSL_SHA1, TLSV1, SSL_MEDIUM, 112, 168, NULL}, 2949 {"ecdhe_rsa_aes_128_sha", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "ECDHE-RSA-AES128-SHA", SSL_kEECDH|SSL_aRSA|SSL_AES128|SSL_SHA1, TLSV1, SSL_HIGH, 128, 128, NULL}, 2950 {"ecdhe_rsa_aes_256_sha", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "ECDHE-RSA-AES256-SHA", SSL_kEECDH|SSL_aRSA|SSL_AES256|SSL_SHA1, TLSV1, SSL_HIGH, 256, 256, NULL}, 2951 {"ecdh_anon_null_sha", TLS_ECDH_anon_WITH_NULL_SHA, "AECDH-NULL-SHA", SSL_kEECDH|SSL_aNULL|SSL_eNULL|SSL_SHA1, TLSV1, SSL_STRONG_NONE, 0, 0, NULL}, 2952 {"ecdh_anon_rc4_128sha", TLS_ECDH_anon_WITH_RC4_128_SHA, "AECDH-RC4-SHA", SSL_kEECDH|SSL_aNULL|SSL_RC4|SSL_SHA1, TLSV1, SSL_MEDIUM, 128, 128, NULL}, 2953 {"ecdh_anon_3des_sha", TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA, "AECDH-DES-CBC3-SHA", SSL_kEECDH|SSL_aNULL|SSL_3DES|SSL_SHA1, TLSV1, SSL_MEDIUM, 112, 168, NULL}, 2954 {"ecdh_anon_aes_128_sha", TLS_ECDH_anon_WITH_AES_128_CBC_SHA, "AECDH-AES128-SHA", SSL_kEECDH|SSL_aNULL|SSL_AES128|SSL_SHA1, TLSV1, SSL_HIGH, 128, 128, NULL}, 2955 {"ecdh_anon_aes_256_sha", TLS_ECDH_anon_WITH_AES_256_CBC_SHA, "AECDH-AES256-SHA", SSL_kEECDH|SSL_aNULL|SSL_AES256|SSL_SHA1, TLSV1, SSL_HIGH, 256, 256, NULL}, 2956 {"ecdhe_ecdsa_aes_128_sha_256", TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "ECDHE-ECDSA-AES128-SHA256", SSL_kEECDH|SSL_aECDSA|SSL_AES128|SSL_SHA256, TLSV1_2, SSL_HIGH, 128, 128, NULL}, 2957 {"ecdhe_rsa_aes_128_sha_256", TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "ECDHE-RSA-AES128-SHA256", SSL_kEECDH|SSL_aRSA|SSL_AES128|SSL_SHA256, TLSV1_2, SSL_HIGH, 128, 128, NULL}, 2958 //#ifdef ENABLE_GCM 2959 {"ecdhe_ecdsa_aes_128_gcm_sha_256", TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "ECDHE-ECDSA-AES128-GCM-SHA256", SSL_kEECDH|SSL_aECDSA|SSL_AES128GCM|SSL_AEAD, TLSV1_2, SSL_HIGH, 128, 128, NULL}, 2960 //#endif 2961 //#ifdef ENABLE_SHA384 2962 {"ecdhe_ecdsa_aes_256_sha_384", TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, "ECDHE-ECDSA-AES256-SHA384", SSL_kEECDH|SSL_aECDSA|SSL_AES256|SSL_SHA384, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2963 {"ecdhe_rsa_aes_256_sha_384", TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, "ECDHE-RSA-AES256-SHA384", SSL_kEECDH|SSL_aRSA|SSL_AES256|SSL_SHA384, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2964 {"ecdhe_ecdsa_aes_256_gcm_sha_384", TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "ECDHE-ECDSA-AES256-GCM-SHA384", SSL_kEECDH|SSL_aECDSA|SSL_AES256GCM|SSL_AEAD, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2965 {"ecdhe_rsa_aes_256_gcm_sha_384", TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "ECDHE-RSA-AES256-GCM-SHA384", SSL_kEECDH|SSL_aRSA|SSL_AES256GCM|SSL_AEAD, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2966 //#endif 2967 //#ifdef ENABLE_GCM 2968 {"ecdhe_rsa_aes_128_gcm_sha_256", TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "ECDHE-RSA-AES128-GCM-SHA256", SSL_kEECDH|SSL_aRSA|SSL_AES128GCM|SSL_AEAD, TLSV1_2, SSL_HIGH, 128, 128, NULL}, 2969 //#endif 2970 /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 is not implemented */ 2971 /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 is not implemented */ 2972 /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 is not implemented */ 2973 /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 is not implemented */ 2974 //#endif 2975 //#ifdef ENABLE_CHACHA20 2976 {"ecdhe_rsa_chacha20_poly1305_sha_256", TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "ECDHE-RSA-CHACHA20-POLY1305", SSL_kEECDH|SSL_aRSA|SSL_CHACHA20POLY1305|SSL_AEAD, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2977 {"ecdhe_ecdsa_chacha20_poly1305_sha_256", TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, "ECDHE-ECDSA-CHACHA20-POLY1305", SSL_kEECDH|SSL_aECDSA|SSL_CHACHA20POLY1305|SSL_AEAD, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2978 {"dhe_rsa_chacha20_poly1305_sha_256", TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "DHE-RSA-CHACHA20-POLY1305", SSL_kDHE|SSL_aRSA|SSL_CHACHA20POLY1305|SSL_AEAD, TLSV1_2, SSL_HIGH, 256, 256, NULL}, 2979 //#endif 2980 //#ifdef NSS_SUPPORTS_TLS_1_3 2981 /* Special TLS 1.3 cipher suites that really just specify AEAD 2982 * TLS 1.3 ciphers don't specify key exchange and authentication. 2983 */ 2984 {"aes_128_gcm_sha_256", TLS_AES_128_GCM_SHA256, "TLS-AES-128-GCM-SHA256", SSL_AES128GCM|SSL_AEAD, TLSV1_3, SSL_HIGH, 128, 128, NULL}, 2985 {"aes_256_gcm_sha_384", TLS_AES_256_GCM_SHA384, "TLS-AES-256-GCM-SHA384", SSL_AES256GCM|SSL_AEAD, TLSV1_3, SSL_HIGH, 256, 256, NULL}, 2986 {"chacha20_poly1305_sha_256", TLS_CHACHA20_POLY1305_SHA256, "TLS-CHACHA20-POLY1305_SHA256", SSL_CHACHA20POLY1305|SSL_AEAD, TLSV1_3, SSL_HIGH, 256, 256, NULL}, 2987 //#endif 2988 }; 2989 2990 #define CIPHERNUM sizeof(ciphers_def) / sizeof(cipher_properties) 2991 //static const int ciphernum = CIPHERNUM; 2992 #define ciphernum ((int)(CIPHERNUM)) 2993 2994 /* Some ciphers are optionally enabled in OpenSSL. For safety sake assume 2995 * they are not available. 2996 */ 2997 static const int skip_ciphers = 4; 2998 static const int ciphers_not_in_openssl[] = { 2999 SSL_RSA_FIPS_WITH_DES_CBC_SHA, 3000 SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA, 3001 TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA, 3002 TLS_RSA_EXPORT1024_WITH_RC4_56_SHA, 3003 }; 3004 3005 static int parse_nss_ciphers(server_rec *s, char *ciphers, PRBool cipher_list[ciphernum]); 3006 static int parse_openssl_ciphers(server_rec *s, char *ciphers, PRBool cipher_list[ciphernum]); 3007 3008 static 3009 int countciphers(PRBool cipher_state[ciphernum], int version) { 3010 int ciphercount = 0; 3011 int i = 0; 3012 3013 for (i = 0; i < ciphernum; i++) 3014 { 3015 if ((cipher_state[i] == PR_TRUE) && 3016 (ciphers_def[i].version & version)) { 3017 ciphercount++; 3018 } 3019 } 3020 3021 return ciphercount; 3022 } 3023 3024 3025 static 3026 int nss_parse_ciphers(server_rec *s, char *ciphers, PRBool cipher_list[ciphernum]) 3027 { 3028 UNUSED(s); 3029 int rv = 0; 3030 3031 /* If the string has a colon we use the OpenSSL style. If it has a 3032 * comma then NSS. If it has neither we try both. */ 3033 if (strchr(ciphers, ':')) { 3034 rv = parse_openssl_ciphers(s, ciphers, cipher_list); 3035 } else if (strchr(ciphers, ',')) { 3036 rv = parse_nss_ciphers(s, ciphers, cipher_list); 3037 } else { 3038 rv = parse_openssl_ciphers(s, ciphers, cipher_list); 3039 if (rv == 0 && 0 == countciphers(cipher_list, SSLV3|TLSV1|TLSV1_2|TLSV1_3)) { 3040 rv = parse_nss_ciphers(s, ciphers, cipher_list); 3041 } 3042 } 3043 if (0 == countciphers(cipher_list, SSLV3|TLSV1|TLSV1_2|TLSV1_3)) { 3044 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, 3045 "no cipher match"); 3046 } 3047 3048 return rv; 3049 } 3050 3051 3052 /* Given a set of ciphers perform a given action on the indexed value. 3053 * 3054 * This is needed because the + action doesn't do anything in the NSS 3055 * context. In OpenSSL it will re-order the cipher list. 3056 */ 3057 static void set_cipher_value(PRBool cipher_list[ciphernum], int index, int action) 3058 { 3059 int i; 3060 3061 if (action == REORDER_CIPHER) 3062 /* NSS doesn't allow ordering so do nothing */ 3063 return; 3064 3065 for (i = 0; i < skip_ciphers; i++) { 3066 if (ciphers_def[index].num == ciphers_not_in_openssl[i]) { 3067 cipher_list[index] = PERMANENTLY_DISABLE_CIPHER; 3068 return; 3069 } 3070 } 3071 3072 if (cipher_list[index] != PERMANENTLY_DISABLE_CIPHER) 3073 cipher_list[index] = action; 3074 } 3075 3076 3077 static int parse_openssl_ciphers(server_rec *s, char *ciphers, PRBool cipher_list[ciphernum]) 3078 { 3079 UNUSED(s); 3080 char * cipher; 3081 int i, action; 3082 PRBool merge = PR_FALSE; 3083 PRBool found = PR_FALSE; 3084 PRBool first = PR_TRUE; 3085 3086 cipher = ciphers; 3087 while (ciphers && (strlen(ciphers))) 3088 { 3089 while ((*cipher) && (isspace(*(uint8_t *)cipher))) 3090 ++cipher; 3091 3092 action = ENABLE_CIPHER; /* default to enable */ 3093 switch(*cipher) 3094 { 3095 case '+': 3096 /* Cipher ordering is not supported in NSS */ 3097 action = REORDER_CIPHER; 3098 cipher++; 3099 break; 3100 case '-': 3101 action = SUBTRACT_CIPHER; 3102 cipher++; 3103 break; 3104 case '!': 3105 action = PERMANENTLY_DISABLE_CIPHER; 3106 cipher++; 3107 break; 3108 default: 3109 /* Add the cipher */ 3110 break; 3111 } 3112 3113 if ((ciphers = strchr(cipher, ':'))) { 3114 *ciphers++ = '\0'; 3115 merge = PR_FALSE; 3116 found = PR_FALSE; 3117 } 3118 3119 if (!strcmp(cipher, "ALL")) { 3120 found = PR_TRUE; 3121 for (i=0; i<ciphernum; i++) { 3122 if (!(ciphers_def[i].attr & SSL_eNULL)) 3123 set_cipher_value(cipher_list, i, action); 3124 } 3125 } else if (!strcmp(cipher, "COMPLEMENTOFALL")) { 3126 found = PR_TRUE; 3127 for (i=0; i<ciphernum; i++) { 3128 if ((ciphers_def[i].attr & SSL_eNULL)) 3129 set_cipher_value(cipher_list, i, action); 3130 } 3131 } else if (!strcmp(cipher, "DEFAULT")) { 3132 /* In OpenSSL the default cipher list is 3133 * ALL:!aNULL:!eNULL:!SSLv2 3134 * So we need to disable all the NULL ciphers too. 3135 */ 3136 int mask = SSL_aNULL | SSL_eNULL; 3137 found = PR_TRUE; 3138 for (i=0; i < ciphernum; i++) { 3139 if (cipher_list[i] != PERMANENTLY_DISABLE_CIPHER) 3140 SSL_CipherPrefGetDefault(ciphers_def[i].num, 3141 &cipher_list[i]); 3142 if (PR_TRUE == first) { 3143 if (ciphers_def[i].attr & mask) { 3144 set_cipher_value(cipher_list, i, 3145 PERMANENTLY_DISABLE_CIPHER); 3146 } 3147 } 3148 } 3149 } else if (!strcmp(cipher, "COMPLEMENTOFDEFAULT")) { 3150 found = PR_TRUE; 3151 /* no-op. In OpenSSL this is the ADH ciphers */ 3152 } else if (!strcmp(cipher, "@STRENGTH")) { 3153 /* No cipher ordering in NSS */ 3154 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, 3155 "Cipher ordering is not supported in NSS"); 3156 return -1; 3157 } else { 3158 int amask = 0; 3159 int amaskaction = 0; 3160 int mask = 0; 3161 int strength = 0; 3162 int protocol = 0; 3163 char *c; 3164 PRBool candidate_list[ciphernum]; 3165 PRBool temp_list[ciphernum]; 3166 3167 for (i = 0; i < ciphernum; i++) { 3168 candidate_list[i] = 1; 3169 } 3170 3171 c = cipher; 3172 while (c && (strlen(c))) { 3173 amask = 0; 3174 amaskaction = 0; 3175 mask = 0; 3176 strength = 0; 3177 protocol = 0; 3178 for (i = 0; i < ciphernum; i++) { 3179 temp_list[i] = 0; 3180 } 3181 3182 if ((c = strchr(cipher, '+'))) { 3183 *c++ = '\0'; 3184 } 3185 3186 if (!strcmp(cipher, "RSA")) { 3187 mask |= SSL_RSA; 3188 } else if (!strcmp(cipher, "kRSA")) { 3189 mask |= SSL_kRSA; 3190 } else if (!strcmp(cipher, "aRSA")) { 3191 mask |= SSL_aRSA; 3192 } else if (!strcmp(cipher, "EDH")) { 3193 /* Normally this is kEDH:-ADH but since we don't 3194 * support ADH this is sufficient. 3195 */ 3196 mask |= SSL_kEDH; 3197 } else if (!strcmp(cipher, "DH")) { 3198 /* non-ephemeral DH. The ciphers are defined 3199 * but not implemented in OpenSSL so manage 3200 * this here. 3201 */ 3202 mask |= SSL_kDHE; 3203 #if 0 3204 } else if (!strcmp(cipher, "ADH")) { 3205 mask |= SSL_ADH; 3206 #endif 3207 } else if (!strcmp(cipher, "ECDH")) { 3208 mask |= SSL_ECDH; 3209 } else if (!strcmp(cipher, "EECDH")) { 3210 mask |= SSL_kEECDH; 3211 amask = SSL_aNULL; 3212 amaskaction = 1; /* filter anonymous out */ 3213 } else if (!strcmp(cipher, "AECDH")) { 3214 mask |= SSL_kEECDH; 3215 amask = SSL_aNULL; /* require anonymous */ 3216 amaskaction = 0; /* keep these */ 3217 } else if (!strcmp(cipher, "kECDH")) { 3218 mask |= SSL_kECDHE | SSL_kECDHr; 3219 } else if (!strcmp(cipher, "kECDHE")) { 3220 mask |= SSL_kECDHE; 3221 } else if (!strcmp(cipher, "kECDHr")) { 3222 mask |= SSL_kECDHr; 3223 } else if (!strcmp(cipher, "kEECDH")) { 3224 mask |= SSL_kEECDH; 3225 } else if (!strcmp(cipher, "AECDH")) { 3226 mask |= SSL_AECDH; 3227 } else if (!strcmp(cipher, "ECDSA")) { 3228 mask |= SSL_aECDSA; 3229 } else if (!strcmp(cipher, "aECDSA")) { 3230 mask |= SSL_aECDSA; 3231 } else if ((!strcmp(cipher, "NULL")) || (!strcmp(cipher, "eNULL"))) { 3232 mask |= SSL_eNULL; 3233 } else if (!strcmp(cipher, "aNULL")) { 3234 mask |= SSL_aNULL; 3235 } else if (!strcmp(cipher, "AES")) { 3236 mask |= SSL_AES; 3237 } else if (!strcmp(cipher, "AESGCM")) { 3238 mask |= SSL_AES128GCM|SSL_AES256GCM; 3239 } else if (!strcmp(cipher, "AES128")) { 3240 mask |= SSL_AES128|SSL_AES128GCM; 3241 } else if (!strcmp(cipher, "AES256")) { 3242 mask |= SSL_AES256|SSL_AES256GCM; 3243 } else if (!strcmp(cipher, "CHACHA20")) { 3244 mask |= SSL_CHACHA20POLY1305; 3245 } else if (!strcmp(cipher, "CAMELLIA")) { 3246 mask |= SSL_CAMELLIA128|SSL_CAMELLIA256; 3247 } else if (!strcmp(cipher, "CAMELLIA128")) { 3248 mask |= SSL_CAMELLIA128; 3249 } else if (!strcmp(cipher, "CAMELLIA256")) { 3250 mask |= SSL_CAMELLIA256; 3251 } else if (!strcmp(cipher, "3DES")) { 3252 mask |= SSL_3DES; 3253 } else if (!strcmp(cipher, "DES")) { 3254 mask |= SSL_DES; 3255 } else if (!strcmp(cipher, "RC4")) { 3256 mask |= SSL_RC4; 3257 } else if (!strcmp(cipher, "RC2")) { 3258 mask |= SSL_RC2; 3259 } else if (!strcmp(cipher, "MD5")) { 3260 mask |= SSL_MD5; 3261 } else if ((!strcmp(cipher, "SHA")) || (!strcmp(cipher, "SHA1"))) { 3262 mask |= SSL_SHA1; 3263 } else if (!strcmp(cipher, "SHA256")) { 3264 mask |= SSL_SHA256; 3265 } else if (!strcmp(cipher, "SHA384")) { 3266 mask |= SSL_SHA384; 3267 } else if (!strcmp(cipher, "SSLv2")) { 3268 /* no-op */ 3269 } else if (!strcmp(cipher, "SSLv3")) { 3270 protocol |= SSLV3; 3271 } else if (!strcmp(cipher, "TLSv1")) { 3272 protocol |= TLSV1; 3273 } else if (!strcmp(cipher, "TLSv1.2")) { 3274 protocol |= TLSV1_2; 3275 } else if (!strcmp(cipher, "TLSv1.3")) { 3276 protocol |= TLSV1_3; 3277 } else if (!strcmp(cipher, "HIGH")) { 3278 strength |= SSL_HIGH; 3279 } else if (!strcmp(cipher, "MEDIUM")) { 3280 strength |= SSL_MEDIUM; 3281 } else if (!strcmp(cipher, "LOW")) { 3282 strength |= SSL_LOW; 3283 } else if ((!strcmp(cipher, "EXPORT")) || (!strcmp(cipher, "EXP"))) { 3284 strength |= SSL_EXPORT40|SSL_EXPORT56; 3285 } else if (!strcmp(cipher, "EXPORT40")) { 3286 strength |= SSL_EXPORT40; 3287 } else if (!strcmp(cipher, "EXPORT56")) { 3288 strength |= SSL_EXPORT56; 3289 } 3290 3291 if (c) 3292 cipher = c; 3293 3294 /* If we have a mask, apply it. If not then perhaps they 3295 * provided a specific cipher to enable. 3296 */ 3297 if (mask || strength || protocol) { 3298 merge = PR_TRUE; 3299 found = PR_TRUE; 3300 for (i=0; i<ciphernum; i++) { 3301 if (((ciphers_def[i].attr & mask) || 3302 (ciphers_def[i].strength & strength) || 3303 (ciphers_def[i].version & protocol)) && 3304 (cipher_list[i] != PERMANENTLY_DISABLE_CIPHER)) { 3305 if (amask != 0) { 3306 PRBool match = PR_FALSE; 3307 if (ciphers_def[i].attr & amask) { 3308 match = PR_TRUE; 3309 } 3310 if (amaskaction && match) 3311 continue; 3312 if (!amaskaction && !match) 3313 continue; 3314 } 3315 #if 0 3316 /* Enable the NULL ciphers only if explicity 3317 * requested */ 3318 if (ciphers_def[i].attr & SSL_eNULL) { 3319 if (mask & SSL_eNULL) 3320 temp_list[i] = 1; 3321 } else 3322 #endif 3323 temp_list[i] = 1; 3324 } 3325 } 3326 /* Merge the temp list into the candidate list */ 3327 for (i=0; i<ciphernum; i++) { 3328 if (!(candidate_list[i] & temp_list[i])) { 3329 candidate_list[i] = 0; 3330 } 3331 } 3332 } else if (!strcmp(cipher, "FIPS")) { 3333 SSLCipherSuiteInfo suite; 3334 for (i=0; i<ciphernum;i++) { 3335 if (SSL_GetCipherSuiteInfo(ciphers_def[i].num, 3336 &suite, sizeof suite) == SECSuccess) { 3337 if (suite.isFIPS) 3338 set_cipher_value(cipher_list, i, action); 3339 } 3340 } 3341 } else { 3342 for (i=0; i<ciphernum; i++) { 3343 if (!strcmp(ciphers_def[i].openssl_name, cipher)) 3344 set_cipher_value(cipher_list, i, action); 3345 } 3346 } 3347 } /* while */ 3348 if (PR_TRUE == merge) { 3349 first = PR_FALSE; 3350 /* Merge the candidate list into the cipher list */ 3351 for (i=0; i<ciphernum; i++) { 3352 if (candidate_list[i]) 3353 set_cipher_value(cipher_list, i, action); 3354 } 3355 merge = PR_FALSE; 3356 found = PR_FALSE; 3357 } 3358 } 3359 3360 if (ciphers) 3361 cipher = ciphers; 3362 3363 } 3364 if (found && 0 == countciphers(cipher_list, SSLV3|TLSV1|TLSV1_2|TLSV1_3)) 3365 return 1; /* no matching ciphers */ 3366 return 0; 3367 } 3368 3369 3370 static int parse_nss_ciphers(server_rec *s, char *ciphers, PRBool cipher_list[ciphernum]) 3371 { 3372 UNUSED(s); 3373 char * cipher; 3374 PRBool found; 3375 int i, active; 3376 3377 cipher = ciphers; 3378 3379 while (ciphers && (strlen(ciphers))) 3380 { 3381 while ((*cipher) && (isspace(*(uint8_t *)cipher))) 3382 ++cipher; 3383 3384 switch(*cipher++) 3385 { 3386 case '+': 3387 active = PR_TRUE; 3388 break; 3389 case '-': 3390 active = PR_FALSE; 3391 break; 3392 default: 3393 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, 3394 "invalid cipher string %s. Format is +cipher1,-cipher2...", cipher - 1); 3395 return -1; 3396 } 3397 3398 if ((ciphers = strchr(cipher, ','))) { 3399 *ciphers++ = '\0'; 3400 } 3401 3402 found = PR_FALSE; 3403 3404 for (i = 0; i < ciphernum; i++) 3405 { 3406 if (!strcasecmp(cipher, ciphers_def[i].name)) 3407 { 3408 cipher_list[i] = active; 3409 found = PR_TRUE; 3410 break; 3411 } else if ((ciphers_def[i].alias != NULL) && 3412 (!strcasecmp(cipher, ciphers_def[i].alias))) 3413 { 3414 cipher_list[i] = active; 3415 found = PR_TRUE; 3416 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, 3417 "Deprecated cipher name %s, use %s instead.", 3418 cipher, ciphers_def[i].name); 3419 break; 3420 } 3421 } 3422 3423 if (found == PR_FALSE) { 3424 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, 3425 "Unknown cipher %s\n", cipher); 3426 } 3427 3428 if (ciphers) { 3429 cipher = ciphers; 3430 } 3431 } 3432 3433 return 0; 3434 } 3435 3436 3437 static int 3438 mod_nss_ssl_conf_ciphersuites (server *srv, plugin_config_socket *s, buffer *ciphersuites, const buffer *cipherstring) 3439 { 3440 if (ciphersuites) 3441 /* XXX: not implemented; 3442 * could manually add support for short list of TLSv1.3 suites */ 3443 log_error(srv->errh, __FILE__, __LINE__, 3444 "Ciphersuite support not implemented for %s", 3445 ciphersuites->ptr); 3446 3447 if (buffer_string_is_empty(cipherstring)) 3448 return 1; /* nothing to do */ 3449 3450 /* 3451 * Apache mod_nss 3452 * https://pagure.io/mod_nss.git 3453 * 3454 * modified from mod_nss/nss_engine_init.c:nss_init_ctx_cipher_suite() 3455 */ 3456 3457 PRBool cipher_state[ciphernum]; 3458 3459 /* Disable all NSS supported cipher suites. This is to prevent any new 3460 * NSS cipher suites from getting automatically and unintentionally 3461 * enabled as a result of the NSS_SetDomesticPolicy() call. This way, 3462 * only the ciphers explicitly specified in the server configuration can 3463 * ever be enabled. 3464 */ 3465 for (int i = 0; i < SSL_NumImplementedCiphers; ++i) 3466 SSL_CipherPrefSet(s->model, SSL_ImplementedCiphers[i], SSL_NOT_ALLOWED); 3467 3468 /* initialize all known ciphers to false */ 3469 for (int i = 0; i < ciphernum; ++i) 3470 cipher_state[i] = PR_FALSE; 3471 3472 char *ciphers = strdup(cipherstring->ptr);/*(string modified during parse)*/ 3473 if (NULL == ciphers) return 0; 3474 3475 if (nss_parse_ciphers(srv->errh, ciphers, cipher_state) == -1) 3476 return 0; 3477 3478 free(ciphers); 3479 3480 if (((s->protos.min && s->protos.min <= SSL_LIBRARY_VERSION_3_0) 3481 || s->ssl_use_sslv3) 3482 && countciphers(cipher_state, SSLV3) == 0) { 3483 ap_log_error(APLOG_MARK, APLOG_ERR, 0, srv->errh, 3484 "NSSCipherSuite: SSL3 is enabled but no SSL3 ciphers are enabled."); 3485 return 0; 3486 } 3487 3488 if (s->protos.max >= SSL_LIBRARY_VERSION_TLS_1_0 3489 && countciphers(cipher_state, TLSV1|TLSV1_2|TLSV1_3) == 0) { 3490 ap_log_error(APLOG_MARK, APLOG_ERR, 0, srv->errh, 3491 "NSSCipherSuite: TLS is enabled but no TLS ciphers are enabled."); 3492 return 0; 3493 } 3494 3495 /* Finally actually enable the selected ciphers */ 3496 for (int i = 0; i < ciphernum; ++i) 3497 SSL_CipherPrefSet(s->model, ciphers_def[i].num, 3498 cipher_state[i] == ENABLE_CIPHER 3499 ? SSL_ALLOWED 3500 : SSL_NOT_ALLOWED); 3501 3502 return 1; 3503 } 3504