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