1 /* $NetBSD: tls_client.c,v 1.12 2022/10/08 16:12:50 christos Exp $ */ 2 3 /*++ 4 /* NAME 5 /* tls_client 6 /* SUMMARY 7 /* client-side TLS engine 8 /* SYNOPSIS 9 /* #include <tls.h> 10 /* 11 /* TLS_APPL_STATE *tls_client_init(init_props) 12 /* const TLS_CLIENT_INIT_PROPS *init_props; 13 /* 14 /* TLS_SESS_STATE *tls_client_start(start_props) 15 /* const TLS_CLIENT_START_PROPS *start_props; 16 /* 17 /* TLS_SESS_STATE *tls_client_post_connect(TLScontext, start_props) 18 /* TLS_SESS_STATE *TLScontext; 19 /* const TLS_CLIENT_START_PROPS *start_props; 20 /* 21 /* void tls_client_stop(app_ctx, stream, failure, TLScontext) 22 /* TLS_APPL_STATE *app_ctx; 23 /* VSTREAM *stream; 24 /* int failure; 25 /* TLS_SESS_STATE *TLScontext; 26 /* DESCRIPTION 27 /* This module is the interface between Postfix TLS clients, 28 /* the OpenSSL library and the TLS entropy and cache manager. 29 /* 30 /* The SMTP client will attempt to verify the server hostname 31 /* against the names listed in the server certificate. When 32 /* a hostname match is required, the verification fails 33 /* on certificate verification or hostname mis-match errors. 34 /* When no hostname match is required, hostname verification 35 /* failures are logged but they do not affect the TLS handshake 36 /* or the SMTP session. 37 /* 38 /* The rules for peer name wild-card matching differ between 39 /* RFC 2818 (HTTP over TLS) and RFC 2830 (LDAP over TLS), while 40 /* RFC RFC3207 (SMTP over TLS) does not specify a rule at all. 41 /* Postfix uses a restrictive match algorithm. One asterisk 42 /* ('*') is allowed as the left-most component of a wild-card 43 /* certificate name; it matches the left-most component of 44 /* the peer hostname. 45 /* 46 /* Another area where RFCs aren't always explicit is the 47 /* handling of dNSNames in peer certificates. RFC 3207 (SMTP 48 /* over TLS) does not mention dNSNames. Postfix follows the 49 /* strict rules in RFC 2818 (HTTP over TLS), section 3.1: The 50 /* Subject Alternative Name/dNSName has precedence over 51 /* CommonName. If at least one dNSName is provided, Postfix 52 /* verifies those against the peer hostname and ignores the 53 /* CommonName, otherwise Postfix verifies the CommonName 54 /* against the peer hostname. 55 /* 56 /* tls_client_init() is called once when the SMTP client 57 /* initializes. 58 /* Certificate details are also decided during this phase, 59 /* so peer-specific certificate selection is not possible. 60 /* 61 /* tls_client_start() activates the TLS session over an established 62 /* stream. We expect that network buffers are flushed and 63 /* the TLS handshake can begin immediately. 64 /* 65 /* tls_client_stop() sends the "close notify" alert via 66 /* SSL_shutdown() to the peer and resets all connection specific 67 /* TLS data. As RFC2487 does not specify a separate shutdown, it 68 /* is assumed that the underlying TCP connection is shut down 69 /* immediately afterwards. Any further writes to the channel will 70 /* be discarded, and any further reads will report end-of-file. 71 /* If the failure flag is set, no SSL_shutdown() handshake is performed. 72 /* 73 /* Once the TLS connection is initiated, information about the TLS 74 /* state is available via the TLScontext structure: 75 /* .IP TLScontext->protocol 76 /* the protocol name (SSLv2, SSLv3, TLSv1), 77 /* .IP TLScontext->cipher_name 78 /* the cipher name (e.g. RC4/MD5), 79 /* .IP TLScontext->cipher_usebits 80 /* the number of bits actually used (e.g. 40), 81 /* .IP TLScontext->cipher_algbits 82 /* the number of bits the algorithm is based on (e.g. 128). 83 /* .PP 84 /* The last two values may differ from each other when export-strength 85 /* encryption is used. 86 /* 87 /* If the peer offered a certificate, part of the certificate data are 88 /* available as: 89 /* .IP TLScontext->peer_status 90 /* A bitmask field that records the status of the peer certificate 91 /* verification. This consists of one or more of TLS_CERT_FLAG_PRESENT, 92 /* TLS_CERT_FLAG_TRUSTED, TLS_CERT_FLAG_MATCHED and TLS_CERT_FLAG_SECURED. 93 /* .IP TLScontext->peer_CN 94 /* Extracted CommonName of the peer, or zero-length string if the 95 /* information could not be extracted. 96 /* .IP TLScontext->issuer_CN 97 /* Extracted CommonName of the issuer, or zero-length string if the 98 /* information could not be extracted. 99 /* .IP TLScontext->peer_cert_fprint 100 /* At the fingerprint security level, if the peer presented a certificate 101 /* the fingerprint of the certificate. 102 /* .PP 103 /* If no peer certificate is presented the peer_status is set to 0. 104 /* EVENT_DRIVEN APPLICATIONS 105 /* .ad 106 /* .fi 107 /* Event-driven programs manage multiple I/O channels. Such 108 /* programs cannot use the synchronous VSTREAM-over-TLS 109 /* implementation that the TLS library historically provides, 110 /* including tls_client_stop() and the underlying tls_stream(3) 111 /* and tls_bio_ops(3) routines. 112 /* 113 /* With the current TLS library implementation, this means 114 /* that an event-driven application is responsible for calling 115 /* and retrying SSL_connect(), SSL_read(), SSL_write() and 116 /* SSL_shutdown(). 117 /* 118 /* To maintain control over TLS I/O, an event-driven client 119 /* invokes tls_client_start() with a null VSTREAM argument and 120 /* with an fd argument that specifies the I/O file descriptor. 121 /* Then, tls_client_start() performs all the necessary 122 /* preparations before the TLS handshake and returns a partially 123 /* populated TLS context. The event-driven application is then 124 /* responsible for invoking SSL_connect(), and if successful, 125 /* for invoking tls_client_post_connect() to finish the work 126 /* that was started by tls_client_start(). In case of unrecoverable 127 /* failure, tls_client_post_connect() destroys the TLS context 128 /* and returns a null pointer value. 129 /* LICENSE 130 /* .ad 131 /* .fi 132 /* This software is free. You can do with it whatever you want. 133 /* The original author kindly requests that you acknowledge 134 /* the use of his software. 135 /* AUTHOR(S) 136 /* Originally written by: 137 /* Lutz Jaenicke 138 /* BTU Cottbus 139 /* Allgemeine Elektrotechnik 140 /* Universitaetsplatz 3-4 141 /* D-03044 Cottbus, Germany 142 /* 143 /* Updated by: 144 /* Wietse Venema 145 /* IBM T.J. Watson Research 146 /* P.O. Box 704 147 /* Yorktown Heights, NY 10598, USA 148 /* 149 /* Wietse Venema 150 /* Google, Inc. 151 /* 111 8th Avenue 152 /* New York, NY 10011, USA 153 /* 154 /* Victor Duchovni 155 /* Morgan Stanley 156 /*--*/ 157 158 /* System library. */ 159 160 #include <sys_defs.h> 161 162 #ifdef USE_TLS 163 #include <string.h> 164 165 #ifdef STRCASECMP_IN_STRINGS_H 166 #include <strings.h> 167 #endif 168 169 /* Utility library. */ 170 171 #include <argv.h> 172 #include <mymalloc.h> 173 #include <vstring.h> 174 #include <vstream.h> 175 #include <stringops.h> 176 #include <msg.h> 177 #include <iostuff.h> /* non-blocking */ 178 #include <midna_domain.h> 179 180 /* Global library. */ 181 182 #include <mail_params.h> 183 184 /* TLS library. */ 185 186 #include <tls_mgr.h> 187 #define TLS_INTERNAL 188 #include <tls.h> 189 190 /* Application-specific. */ 191 192 #define STR vstring_str 193 #define LEN VSTRING_LEN 194 195 /* load_clnt_session - load session from client cache (non-callback) */ 196 197 static SSL_SESSION *load_clnt_session(TLS_SESS_STATE *TLScontext) 198 { 199 const char *myname = "load_clnt_session"; 200 SSL_SESSION *session = 0; 201 VSTRING *session_data = vstring_alloc(2048); 202 203 /* 204 * Prepare the query. 205 */ 206 if (TLScontext->log_mask & TLS_LOG_CACHE) 207 /* serverid contains transport:addr:port information */ 208 msg_info("looking for session %s in %s cache", 209 TLScontext->serverid, TLScontext->cache_type); 210 211 /* 212 * We only get here if the cache_type is not empty. This code is not 213 * called unless caching is enabled and the cache_type is stored in the 214 * server SSL context. 215 */ 216 if (TLScontext->cache_type == 0) 217 msg_panic("%s: null client session cache type in session lookup", 218 myname); 219 220 /* 221 * Look up and activate the SSL_SESSION object. Errors are non-fatal, 222 * since caching is only an optimization. 223 */ 224 if (tls_mgr_lookup(TLScontext->cache_type, TLScontext->serverid, 225 session_data) == TLS_MGR_STAT_OK) { 226 session = tls_session_activate(STR(session_data), LEN(session_data)); 227 if (session) { 228 if (TLScontext->log_mask & TLS_LOG_CACHE) 229 /* serverid contains transport:addr:port information */ 230 msg_info("reloaded session %s from %s cache", 231 TLScontext->serverid, TLScontext->cache_type); 232 } 233 } 234 235 /* 236 * Clean up. 237 */ 238 vstring_free(session_data); 239 240 return (session); 241 } 242 243 /* new_client_session_cb - name new session and save it to client cache */ 244 245 static int new_client_session_cb(SSL *ssl, SSL_SESSION *session) 246 { 247 const char *myname = "new_client_session_cb"; 248 TLS_SESS_STATE *TLScontext; 249 VSTRING *session_data; 250 251 /* 252 * The cache name (if caching is enabled in tlsmgr(8)) and the cache ID 253 * string for this session are stored in the TLScontext. It cannot be 254 * null at this point. 255 */ 256 if ((TLScontext = SSL_get_ex_data(ssl, TLScontext_index)) == 0) 257 msg_panic("%s: null TLScontext in new session callback", myname); 258 259 /* 260 * We only get here if the cache_type is not empty. This callback is not 261 * set unless caching is enabled and the cache_type is stored in the 262 * server SSL context. 263 */ 264 if (TLScontext->cache_type == 0) 265 msg_panic("%s: null session cache type in new session callback", 266 myname); 267 268 if (TLScontext->log_mask & TLS_LOG_CACHE) 269 /* serverid contains transport:addr:port information */ 270 msg_info("save session %s to %s cache", 271 TLScontext->serverid, TLScontext->cache_type); 272 273 /* 274 * Passivate and save the session object. Errors are non-fatal, since 275 * caching is only an optimization. 276 */ 277 if ((session_data = tls_session_passivate(session)) != 0) { 278 tls_mgr_update(TLScontext->cache_type, TLScontext->serverid, 279 STR(session_data), LEN(session_data)); 280 vstring_free(session_data); 281 } 282 283 /* 284 * Clean up. 285 */ 286 SSL_SESSION_free(session); /* 200502 */ 287 288 return (1); 289 } 290 291 /* uncache_session - remove session from the external cache */ 292 293 static void uncache_session(SSL_CTX *ctx, TLS_SESS_STATE *TLScontext) 294 { 295 SSL_SESSION *session = SSL_get_session(TLScontext->con); 296 297 SSL_CTX_remove_session(ctx, session); 298 if (TLScontext->cache_type == 0 || TLScontext->serverid == 0) 299 return; 300 301 if (TLScontext->log_mask & TLS_LOG_CACHE) 302 /* serverid contains transport:addr:port information */ 303 msg_info("remove session %s from client cache", TLScontext->serverid); 304 305 tls_mgr_delete(TLScontext->cache_type, TLScontext->serverid); 306 } 307 308 /* verify_extract_name - verify peer name and extract peer information */ 309 310 static void verify_extract_name(TLS_SESS_STATE *TLScontext, X509 *peercert, 311 const TLS_CLIENT_START_PROPS *props) 312 { 313 int verbose; 314 315 verbose = TLScontext->log_mask & 316 (TLS_LOG_CERTMATCH | TLS_LOG_VERBOSE | TLS_LOG_PEERCERT); 317 318 /* 319 * On exit both peer_CN and issuer_CN should be set. 320 */ 321 TLScontext->issuer_CN = tls_issuer_CN(peercert, TLScontext); 322 TLScontext->peer_CN = tls_peer_CN(peercert, TLScontext); 323 324 /* 325 * Is the certificate trust chain trusted and matched? Any required name 326 * checks are now performed internally in OpenSSL. 327 */ 328 if (SSL_get_verify_result(TLScontext->con) == X509_V_OK) { 329 if (TLScontext->must_fail) { 330 msg_panic("%s: cert valid despite trust init failure", 331 TLScontext->namaddr); 332 } else if (TLS_MUST_MATCH(TLScontext->level)) { 333 334 /* 335 * Fully secured only if not insecure like half-dane. We use 336 * TLS_CERT_FLAG_MATCHED to satisfy policy, but 337 * TLS_CERT_FLAG_SECURED to log the effective security. 338 * 339 * Would ideally also exclude "verify" (as opposed to "secure") 340 * here, because that can be subject to insecure MX indirection, 341 * but that's rather incompatible (and not even the case with 342 * explicitly chosen non-default match patterns). Users have 343 * been warned. 344 */ 345 if (!TLS_NEVER_SECURED(TLScontext->level)) 346 TLScontext->peer_status |= TLS_CERT_FLAG_SECURED; 347 TLScontext->peer_status |= TLS_CERT_FLAG_MATCHED; 348 349 if (verbose) { 350 const char *peername = SSL_get0_peername(TLScontext->con); 351 352 if (peername) 353 msg_info("%s: matched peername: %s", 354 TLScontext->namaddr, peername); 355 tls_dane_log(TLScontext); 356 } 357 } else 358 TLScontext->peer_status |= TLS_CERT_FLAG_TRUSTED; 359 } 360 361 /* 362 * Give them a clue. Problems with trust chain verification are logged 363 * when the session is first negotiated, before the session is stored 364 * into the cache. We don't want mystery failures, so log the fact the 365 * real problem is to be found in the past. 366 */ 367 if (!TLS_CERT_IS_MATCHED(TLScontext) 368 && (TLScontext->log_mask & TLS_LOG_UNTRUSTED)) { 369 if (TLScontext->session_reused == 0) 370 tls_log_verify_error(TLScontext); 371 else 372 msg_info("%s: re-using session with untrusted certificate, " 373 "look for details earlier in the log", props->namaddr); 374 } 375 } 376 377 /* add_namechecks - tell OpenSSL what names to check */ 378 379 static void add_namechecks(TLS_SESS_STATE *TLScontext, 380 const TLS_CLIENT_START_PROPS *props) 381 { 382 SSL *ssl = TLScontext->con; 383 int namechecks_count = 0; 384 int i; 385 386 /* RFC6125: No part-label 'foo*bar.example.com' wildcards for SMTP */ 387 SSL_set_hostflags(ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); 388 389 for (i = 0; i < props->matchargv->argc; ++i) { 390 const char *name = props->matchargv->argv[i]; 391 const char *aname; 392 int match_subdomain = 0; 393 394 if (strcasecmp(name, "nexthop") == 0) { 395 name = props->nexthop; 396 } else if (strcasecmp(name, "dot-nexthop") == 0) { 397 name = props->nexthop; 398 match_subdomain = 1; 399 } else if (strcasecmp(name, "hostname") == 0) { 400 name = props->host; 401 } else { 402 if (*name == '.') { 403 if (*++name == 0) { 404 msg_warn("%s: ignoring invalid match name: \".\"", 405 TLScontext->namaddr); 406 continue; 407 } 408 match_subdomain = 1; 409 } 410 #ifndef NO_EAI 411 else { 412 413 /* 414 * Besides U+002E (full stop) IDNA2003 allows labels to be 415 * separated by any of the Unicode variants U+3002 416 * (ideographic full stop), U+FF0E (fullwidth full stop), and 417 * U+FF61 (halfwidth ideographic full stop). Their respective 418 * UTF-8 encodings are: E38082, EFBC8E and EFBDA1. 419 * 420 * IDNA2008 does not permit (upper) case and other variant 421 * differences in U-labels. The midna_domain_to_ascii() 422 * function, based on UTS46, normalizes such differences 423 * away. 424 * 425 * The IDNA to_ASCII conversion does not allow empty leading 426 * labels, so we handle these explicitly here. 427 */ 428 unsigned char *cp = (unsigned char *) name; 429 430 if ((cp[0] == 0xe3 && cp[1] == 0x80 && cp[2] == 0x82) 431 || (cp[0] == 0xef && cp[1] == 0xbc && cp[2] == 0x8e) 432 || (cp[0] == 0xef && cp[1] == 0xbd && cp[2] == 0xa1)) { 433 if (name[3]) { 434 name = name + 3; 435 match_subdomain = 1; 436 } 437 } 438 } 439 #endif 440 } 441 442 /* 443 * DNS subjectAltNames are required to be ASCII. 444 * 445 * Per RFC 6125 Section 6.4.4 Matching the CN-ID, follows the same rules 446 * (6.4.1, 6.4.2 and 6.4.3) that apply to subjectAltNames. In 447 * particular, 6.4.2 says that the reference identifier is coerced to 448 * ASCII, but no conversion is stated or implied for the CN-ID, so it 449 * seems it only matches if it is all ASCII. Otherwise, it is some 450 * other sort of name. 451 */ 452 #ifndef NO_EAI 453 if (!allascii(name) && (aname = midna_domain_to_ascii(name)) != 0) { 454 if (msg_verbose) 455 msg_info("%s asciified to %s", name, aname); 456 name = aname; 457 } 458 #endif 459 460 if (!match_subdomain) { 461 if (SSL_add1_host(ssl, name)) 462 ++namechecks_count; 463 else 464 msg_warn("%s: error loading match name: \"%s\"", 465 TLScontext->namaddr, name); 466 } else { 467 char *dot_name = concatenate(".", name, (char *) 0); 468 469 if (SSL_add1_host(ssl, dot_name)) 470 ++namechecks_count; 471 else 472 msg_warn("%s: error loading match name: \"%s\"", 473 TLScontext->namaddr, dot_name); 474 myfree(dot_name); 475 } 476 } 477 478 /* 479 * If we failed to add any names, OpenSSL will perform no namechecks, so 480 * we set the "must_fail" bit to avoid verification false-positives. 481 */ 482 if (namechecks_count == 0) { 483 msg_warn("%s: could not configure peer name checks", 484 TLScontext->namaddr); 485 TLScontext->must_fail = 1; 486 } 487 } 488 489 /* tls_auth_enable - set up TLS authentication */ 490 491 static int tls_auth_enable(TLS_SESS_STATE *TLScontext, 492 const TLS_CLIENT_START_PROPS *props) 493 { 494 const char *sni = 0; 495 496 if (props->sni && *props->sni) { 497 #ifndef NO_EAI 498 const char *aname; 499 500 #endif 501 502 /* 503 * MTA-STS policy plugin compatibility: with servername=hostname, 504 * Postfix must send the MX hostname (not CNAME expanded). 505 */ 506 if (strcmp(props->sni, "hostname") == 0) 507 sni = props->host; 508 else if (strcmp(props->sni, "nexthop") == 0) 509 sni = props->nexthop; 510 else 511 sni = props->sni; 512 513 /* 514 * The SSL_set_tlsext_host_name() documentation does not promise that 515 * every implementation will convert U-label form to A-label form. 516 */ 517 #ifndef NO_EAI 518 if (!allascii(sni) && (aname = midna_domain_to_ascii(sni)) != 0) { 519 if (msg_verbose) 520 msg_info("%s asciified to %s", sni, aname); 521 sni = aname; 522 } 523 #endif 524 } 525 switch (TLScontext->level) { 526 case TLS_LEV_HALF_DANE: 527 case TLS_LEV_DANE: 528 case TLS_LEV_DANE_ONLY: 529 530 /* 531 * With DANE sessions, send an SNI hint. We don't care whether the 532 * server reports finding a matching certificate or not, so no 533 * callback is required to process the server response. Our use of 534 * SNI is limited to giving servers that make use of SNI the best 535 * opportunity to find the certificate they promised via the 536 * associated TLSA RRs. 537 * 538 * Since the hostname is DNSSEC-validated, it must be a DNS FQDN and 539 * therefore valid for use with SNI. 540 */ 541 if (SSL_dane_enable(TLScontext->con, 0) <= 0) { 542 msg_warn("%s: error enabling DANE-based certificate validation", 543 TLScontext->namaddr); 544 tls_print_errors(); 545 return (0); 546 } 547 /* RFC7672 Section 3.1.1 specifies no name checks for DANE-EE(3) */ 548 SSL_dane_set_flags(TLScontext->con, DANE_FLAG_NO_DANE_EE_NAMECHECKS); 549 550 /* Per RFC7672 the SNI name is the TLSA base domain */ 551 sni = props->dane->base_domain; 552 add_namechecks(TLScontext, props); 553 break; 554 555 case TLS_LEV_FPRINT: 556 /* Synthetic DANE for fingerprint security */ 557 if (SSL_dane_enable(TLScontext->con, 0) <= 0) { 558 msg_warn("%s: error enabling fingerprint certificate validation", 559 props->namaddr); 560 tls_print_errors(); 561 return (0); 562 } 563 SSL_dane_set_flags(TLScontext->con, DANE_FLAG_NO_DANE_EE_NAMECHECKS); 564 break; 565 566 case TLS_LEV_SECURE: 567 case TLS_LEV_VERIFY: 568 if (TLScontext->dane != 0 && TLScontext->dane->tlsa != 0) { 569 /* Synthetic DANE for per-destination trust-anchors */ 570 if (SSL_dane_enable(TLScontext->con, NULL) <= 0) { 571 msg_warn("%s: error configuring local trust anchors", 572 props->namaddr); 573 tls_print_errors(); 574 return (0); 575 } 576 } 577 add_namechecks(TLScontext, props); 578 break; 579 default: 580 break; 581 } 582 583 if (sni) { 584 if (strlen(sni) > TLSEXT_MAXLEN_host_name) { 585 msg_warn("%s: ignoring too long SNI hostname: %.100s", 586 props->namaddr, sni); 587 return (0); 588 } 589 590 /* 591 * Failure to set a valid SNI hostname is a memory allocation error, 592 * and thus transient. Since we must not cache the session if we 593 * failed to send the SNI name, we have little choice but to abort. 594 */ 595 if (!SSL_set_tlsext_host_name(TLScontext->con, sni)) { 596 msg_warn("%s: error setting SNI hostname to: %s", props->namaddr, 597 sni); 598 return (0); 599 } 600 601 /* 602 * The saved value is not presently used client-side, but could later 603 * be logged if acked by the server (requires new client-side 604 * callback to detect the ack). For now this just maintains symmetry 605 * with the server code, where do record the received SNI for 606 * logging. 607 */ 608 TLScontext->peer_sni = mystrdup(sni); 609 if (TLScontext->log_mask & TLS_LOG_DEBUG) 610 msg_info("%s: SNI hostname: %s", props->namaddr, sni); 611 } 612 return (1); 613 } 614 615 /* tls_client_init - initialize client-side TLS engine */ 616 617 TLS_APPL_STATE *tls_client_init(const TLS_CLIENT_INIT_PROPS *props) 618 { 619 SSL_CTX *client_ctx; 620 TLS_APPL_STATE *app_ctx; 621 const EVP_MD *fpt_alg; 622 long off = 0; 623 int cachable; 624 int scache_timeout; 625 int log_mask; 626 627 /* 628 * Convert user loglevel to internal logmask. 629 */ 630 log_mask = tls_log_mask(props->log_param, props->log_level); 631 632 if (log_mask & TLS_LOG_VERBOSE) 633 msg_info("initializing the client-side TLS engine"); 634 635 /* 636 * Load (mostly cipher related) TLS-library internal main.cf parameters. 637 */ 638 tls_param_init(); 639 640 /* 641 * Detect mismatch between compile-time headers and run-time library. 642 */ 643 tls_check_version(); 644 645 /* 646 * Create an application data index for SSL objects, so that we can 647 * attach TLScontext information; this information is needed inside 648 * tls_verify_certificate_callback(). 649 */ 650 if (TLScontext_index < 0) { 651 if ((TLScontext_index = SSL_get_ex_new_index(0, 0, 0, 0, 0)) < 0) { 652 msg_warn("Cannot allocate SSL application data index: " 653 "disabling TLS support"); 654 return (0); 655 } 656 } 657 658 /* 659 * If the administrator specifies an unsupported digest algorithm, fail 660 * now, rather than in the middle of a TLS handshake. 661 */ 662 if ((fpt_alg = tls_validate_digest(props->mdalg)) == 0) { 663 msg_warn("disabling TLS support"); 664 return (0); 665 } 666 667 /* 668 * Initialize the PRNG (Pseudo Random Number Generator) with some seed 669 * from external and internal sources. Don't enable TLS without some real 670 * entropy. 671 */ 672 if (tls_ext_seed(var_tls_daemon_rand_bytes) < 0) { 673 msg_warn("no entropy for TLS key generation: disabling TLS support"); 674 return (0); 675 } 676 tls_int_seed(); 677 678 /* 679 * The SSL/TLS specifications require the client to send a message in the 680 * oldest specification it understands with the highest level it 681 * understands in the message. RFC2487 is only specified for TLSv1, but 682 * we want to be as compatible as possible, so we will start off with a 683 * SSLv2 greeting allowing the best we can offer: TLSv1. We can restrict 684 * this with the options setting later, anyhow. 685 */ 686 ERR_clear_error(); 687 client_ctx = SSL_CTX_new(TLS_client_method()); 688 if (client_ctx == 0) { 689 msg_warn("cannot allocate client SSL_CTX: disabling TLS support"); 690 tls_print_errors(); 691 return (0); 692 } 693 #ifdef SSL_SECOP_PEER 694 /* Backwards compatible security as a base for opportunistic TLS. */ 695 SSL_CTX_set_security_level(client_ctx, 0); 696 #endif 697 698 /* 699 * See the verify callback in tls_verify.c 700 */ 701 SSL_CTX_set_verify_depth(client_ctx, props->verifydepth + 1); 702 703 /* 704 * This is a prerequisite for enabling DANE support in OpenSSL, but not a 705 * commitment to use DANE, thus suitable for both DANE and non-DANE TLS 706 * connections. Indeed we need this not just for DANE, but aslo for 707 * fingerprint and "tafile" support. Since it just allocates memory, it 708 * should never fail except when we're likely to fail anyway. Rather 709 * than try to run with crippled TLS support, just give up using TLS. 710 */ 711 if (SSL_CTX_dane_enable(client_ctx) <= 0) { 712 msg_warn("OpenSSL DANE initialization failed: disabling TLS support"); 713 tls_print_errors(); 714 return (0); 715 } 716 tls_dane_digest_init(client_ctx, fpt_alg); 717 718 /* 719 * Protocol selection is destination dependent, so we delay the protocol 720 * selection options to the per-session SSL object. 721 */ 722 off |= tls_bug_bits(); 723 SSL_CTX_set_options(client_ctx, off); 724 725 /* 726 * Set the call-back routine for verbose logging. 727 */ 728 if (log_mask & TLS_LOG_DEBUG) 729 SSL_CTX_set_info_callback(client_ctx, tls_info_callback); 730 731 /* 732 * Load the CA public key certificates for both the client cert and for 733 * the verification of server certificates. As provided by OpenSSL we 734 * support two types of CA certificate handling: One possibility is to 735 * add all CA certificates to one large CAfile, the other possibility is 736 * a directory pointed to by CApath, containing separate files for each 737 * CA with softlinks named after the hash values of the certificate. The 738 * first alternative has the advantage that the file is opened and read 739 * at startup time, so that you don't have the hassle to maintain another 740 * copy of the CApath directory for chroot-jail. 741 */ 742 if (tls_set_ca_certificate_info(client_ctx, 743 props->CAfile, props->CApath) < 0) { 744 /* tls_set_ca_certificate_info() already logs a warning. */ 745 SSL_CTX_free(client_ctx); /* 200411 */ 746 return (0); 747 } 748 749 /* 750 * We do not need a client certificate, so the certificates are only 751 * loaded (and checked) if supplied. A clever client would handle 752 * multiple client certificates and decide based on the list of 753 * acceptable CAs, sent by the server, which certificate to submit. 754 * OpenSSL does however not do this and also has no call-back hooks to 755 * easily implement it. 756 * 757 * Load the client public key certificate and private key from file and 758 * check whether the cert matches the key. We can use RSA certificates 759 * ("cert") DSA certificates ("dcert") or ECDSA certificates ("eccert"). 760 * All three can be made available at the same time. The CA certificates 761 * for all three are handled in the same setup already finished. Which 762 * one is used depends on the cipher negotiated (that is: the first 763 * cipher listed by the client which does match the server). The client 764 * certificate is presented after the server chooses the session cipher, 765 * so we will just present the right cert for the chosen cipher (if it 766 * uses certificates). 767 */ 768 if (tls_set_my_certificate_key_info(client_ctx, 769 props->chain_files, 770 props->cert_file, 771 props->key_file, 772 props->dcert_file, 773 props->dkey_file, 774 props->eccert_file, 775 props->eckey_file) < 0) { 776 /* tls_set_my_certificate_key_info() already logs a warning. */ 777 SSL_CTX_free(client_ctx); /* 200411 */ 778 return (0); 779 } 780 781 /* 782 * With OpenSSL 1.0.2 and later the client EECDH curve list becomes 783 * configurable with the preferred curve negotiated via the supported 784 * curves extension. 785 */ 786 tls_auto_eecdh_curves(client_ctx, var_tls_eecdh_auto); 787 788 /* 789 * Finally, the setup for the server certificate checking, done "by the 790 * book". 791 */ 792 SSL_CTX_set_verify(client_ctx, SSL_VERIFY_NONE, 793 tls_verify_certificate_callback); 794 795 /* 796 * Initialize the session cache. 797 * 798 * Since the client does not search an internal cache, we simply disable it. 799 * It is only useful for expiring old sessions, but we do that in the 800 * tlsmgr(8). 801 * 802 * This makes SSL_CTX_remove_session() not useful for flushing broken 803 * sessions from the external cache, so we must delete them directly (not 804 * via a callback). 805 */ 806 if (tls_mgr_policy(props->cache_type, &cachable, 807 &scache_timeout) != TLS_MGR_STAT_OK) 808 scache_timeout = 0; 809 if (scache_timeout <= 0) 810 cachable = 0; 811 812 /* 813 * Allocate an application context, and populate with mandatory protocol 814 * and cipher data. 815 */ 816 app_ctx = tls_alloc_app_context(client_ctx, 0, log_mask); 817 818 /* 819 * The external session cache is implemented by the tlsmgr(8) process. 820 */ 821 if (cachable) { 822 823 app_ctx->cache_type = mystrdup(props->cache_type); 824 825 /* 826 * OpenSSL does not use callbacks to load sessions from a client 827 * cache, so we must invoke that function directly. Apparently, 828 * OpenSSL does not provide a way to pass session names from here to 829 * call-back routines that do session lookup. 830 * 831 * OpenSSL can, however, automatically save newly created sessions for 832 * us by callback (we create the session name in the call-back 833 * function). 834 * 835 * XXX gcc 2.95 can't compile #ifdef .. #endif in the expansion of 836 * SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE | 837 * SSL_SESS_CACHE_NO_AUTO_CLEAR. 838 */ 839 #ifndef SSL_SESS_CACHE_NO_INTERNAL_STORE 840 #define SSL_SESS_CACHE_NO_INTERNAL_STORE 0 841 #endif 842 843 SSL_CTX_set_session_cache_mode(client_ctx, 844 SSL_SESS_CACHE_CLIENT | 845 SSL_SESS_CACHE_NO_INTERNAL_STORE | 846 SSL_SESS_CACHE_NO_AUTO_CLEAR); 847 SSL_CTX_sess_set_new_cb(client_ctx, new_client_session_cb); 848 849 /* 850 * OpenSSL ignores timed-out sessions. We need to set the internal 851 * cache timeout at least as high as the external cache timeout. This 852 * applies even if no internal cache is used. We set the session to 853 * twice the cache lifetime. This way a session always lasts longer 854 * than its lifetime in the cache. 855 */ 856 SSL_CTX_set_timeout(client_ctx, 2 * scache_timeout); 857 } 858 return (app_ctx); 859 } 860 861 /* 862 * This is the actual startup routine for the connection. We expect that the 863 * buffers are flushed and the "220 Ready to start TLS" was received by us, 864 * so that we can immediately start the TLS handshake process. 865 */ 866 TLS_SESS_STATE *tls_client_start(const TLS_CLIENT_START_PROPS *props) 867 { 868 int sts; 869 int protomask; 870 int min_proto; 871 int max_proto; 872 const char *cipher_list; 873 SSL_SESSION *session = 0; 874 TLS_SESS_STATE *TLScontext; 875 TLS_APPL_STATE *app_ctx = props->ctx; 876 int log_mask = app_ctx->log_mask; 877 878 /* 879 * When certificate verification is required, log trust chain validation 880 * errors even when disabled by default for opportunistic sessions. For 881 * DANE this only applies when using trust-anchor associations. 882 */ 883 if (TLS_MUST_MATCH(props->tls_level)) 884 log_mask |= TLS_LOG_UNTRUSTED; 885 886 if (log_mask & TLS_LOG_VERBOSE) 887 msg_info("setting up TLS connection to %s", props->namaddr); 888 889 /* 890 * First make sure we have valid protocol and cipher parameters 891 * 892 * Per-session protocol restrictions must be applied to the SSL connection, 893 * as restrictions in the global context cannot be cleared. 894 */ 895 protomask = tls_proto_mask_lims(props->protocols, &min_proto, &max_proto); 896 if (protomask == TLS_PROTOCOL_INVALID) { 897 /* tls_protocol_mask() logs no warning. */ 898 msg_warn("%s: Invalid TLS protocol list \"%s\": aborting TLS session", 899 props->namaddr, props->protocols); 900 return (0); 901 } 902 903 /* 904 * Though RFC7672 set the floor at SSLv3, we really can and should 905 * require TLS 1.0, since e.g. we send SNI, which is a TLS 1.0 extension. 906 * No DANE domains have been observed to support only SSLv3. 907 * 908 * XXX: Would be nice to make that TLS 1.2 at some point. Users can choose 909 * to exclude TLS 1.0 and TLS 1.1 if they find they don't run into any 910 * problems doing that. 911 */ 912 if (TLS_DANE_BASED(props->tls_level)) 913 protomask |= TLS_PROTOCOL_SSLv2 | TLS_PROTOCOL_SSLv3; 914 915 /* 916 * Allocate a new TLScontext for the new connection and get an SSL 917 * structure. Add the location of TLScontext to the SSL to later retrieve 918 * the information inside the tls_verify_certificate_callback(). 919 * 920 * If session caching was enabled when TLS was initialized, the cache type 921 * is stored in the client SSL context. 922 */ 923 TLScontext = tls_alloc_sess_context(log_mask, props->namaddr); 924 TLScontext->cache_type = app_ctx->cache_type; 925 TLScontext->level = props->tls_level; 926 927 if ((TLScontext->con = SSL_new(app_ctx->ssl_ctx)) == NULL) { 928 msg_warn("Could not allocate 'TLScontext->con' with SSL_new()"); 929 tls_print_errors(); 930 tls_free_context(TLScontext); 931 return (0); 932 } 933 934 /* 935 * Per session cipher selection for sessions with mandatory encryption 936 * 937 * The cipherlist is applied to the global SSL context, since it is likely 938 * to stay the same between connections, so we make use of a 1-element 939 * cache to return the same result for identical inputs. 940 */ 941 cipher_list = tls_set_ciphers(TLScontext, props->cipher_grade, 942 props->cipher_exclusions); 943 if (cipher_list == 0) { 944 /* already warned */ 945 tls_free_context(TLScontext); 946 return (0); 947 } 948 if (log_mask & TLS_LOG_VERBOSE) 949 msg_info("%s: TLS cipher list \"%s\"", props->namaddr, cipher_list); 950 951 TLScontext->stream = props->stream; 952 TLScontext->mdalg = props->mdalg; 953 954 /* Alias DANE digest info from props */ 955 TLScontext->dane = props->dane; 956 957 if (!SSL_set_ex_data(TLScontext->con, TLScontext_index, TLScontext)) { 958 msg_warn("Could not set application data for 'TLScontext->con'"); 959 tls_print_errors(); 960 tls_free_context(TLScontext); 961 return (0); 962 } 963 #define CARP_VERSION(which) do { \ 964 if (which##_proto != 0) \ 965 msg_warn("%s: error setting %simum TLS version to: 0x%04x", \ 966 TLScontext->namaddr, #which, which##_proto); \ 967 else \ 968 msg_warn("%s: error clearing %simum TLS version", \ 969 TLScontext->namaddr, #which); \ 970 } while (0) 971 972 /* 973 * Apply session protocol restrictions. 974 */ 975 if (protomask != 0) 976 SSL_set_options(TLScontext->con, TLS_SSL_OP_PROTOMASK(protomask)); 977 if (!SSL_set_min_proto_version(TLScontext->con, min_proto)) 978 CARP_VERSION(min); 979 if (!SSL_set_max_proto_version(TLScontext->con, max_proto)) 980 CARP_VERSION(max); 981 982 /* 983 * When applicable, configure DNS-based or synthetic (fingerprint or 984 * local trust anchor) DANE authentication, enable an appropriate SNI 985 * name and peer name matching. 986 * 987 * NOTE, this can change the effective security level, and needs to happen 988 * early. 989 */ 990 if (!tls_auth_enable(TLScontext, props)) { 991 tls_free_context(TLScontext); 992 return (0); 993 } 994 995 /* 996 * Try to convey the configured TLSA records for this connection to the 997 * OpenSSL library. If none are "usable", we'll fall back to "encrypt" 998 * when authentication is not mandatory, otherwise we must arrange to 999 * ensure authentication failure. 1000 */ 1001 if (TLScontext->dane && TLScontext->dane->tlsa) { 1002 int usable = tls_dane_enable(TLScontext); 1003 int must_fail = usable <= 0; 1004 1005 if (usable == 0) { 1006 switch (TLScontext->level) { 1007 case TLS_LEV_HALF_DANE: 1008 case TLS_LEV_DANE: 1009 msg_warn("%s: all TLSA records unusable, fallback to " 1010 "unauthenticated TLS", TLScontext->namaddr); 1011 must_fail = 0; 1012 TLScontext->level = TLS_LEV_ENCRYPT; 1013 break; 1014 1015 case TLS_LEV_FPRINT: 1016 msg_warn("%s: all fingerprints unusable", TLScontext->namaddr); 1017 break; 1018 case TLS_LEV_DANE_ONLY: 1019 msg_warn("%s: all TLSA records unusable", TLScontext->namaddr); 1020 break; 1021 case TLS_LEV_SECURE: 1022 case TLS_LEV_VERIFY: 1023 msg_warn("%s: all trust anchors unusable", TLScontext->namaddr); 1024 break; 1025 } 1026 } 1027 TLScontext->must_fail |= must_fail; 1028 } 1029 1030 /* 1031 * We compute the policy digest after we compute the SNI name in 1032 * tls_auth_enable() and possibly update the TLScontext security level. 1033 * 1034 * OpenSSL will ignore cached sessions that use the wrong protocol. So we do 1035 * not need to filter out cached sessions with the "wrong" protocol, 1036 * rather OpenSSL will simply negotiate a new session. 1037 * 1038 * We salt the session lookup key with the protocol list, so that sessions 1039 * found in the cache are plausibly acceptable. 1040 * 1041 * By the time a TLS client is negotiating ciphers it has already offered to 1042 * re-use a session, it is too late to renege on the offer. So we must 1043 * not attempt to re-use sessions whose ciphers are too weak. We salt the 1044 * session lookup key with the cipher list, so that sessions found in the 1045 * cache are always acceptable. 1046 * 1047 * With DANE, (more generally any TLScontext where we specified explicit 1048 * trust-anchor or end-entity certificates) the verification status of 1049 * the SSL session depends on the specified list. Since we verify the 1050 * certificate only during the initial handshake, we must segregate 1051 * sessions with different TA lists. Note, that TA re-verification is 1052 * not possible with cached sessions, since these don't hold the complete 1053 * peer trust chain. Therefore, we compute a digest of the sorted TA 1054 * parameters and append it to the serverid. 1055 */ 1056 TLScontext->serverid = 1057 tls_serverid_digest(TLScontext, props, cipher_list); 1058 1059 /* 1060 * When authenticating the peer, use 80-bit plus OpenSSL security level 1061 * 1062 * XXX: We should perhaps use security level 1 also for mandatory 1063 * encryption, with only "may" tolerating weaker algorithms. But that 1064 * could mean no TLS 1.0 with OpenSSL >= 3.0 and encrypt, unless I get my 1065 * patch in on time to conditionally re-enable SHA1 at security level 1, 1066 * and we add code to make it so. 1067 * 1068 * That said, with "encrypt", we could reasonably require TLS 1.2? 1069 */ 1070 if (TLS_MUST_MATCH(TLScontext->level)) 1071 SSL_set_security_level(TLScontext->con, 1); 1072 1073 /* 1074 * XXX To avoid memory leaks we must always call SSL_SESSION_free() after 1075 * calling SSL_set_session(), regardless of whether or not the session 1076 * will be reused. 1077 */ 1078 if (TLScontext->cache_type) { 1079 session = load_clnt_session(TLScontext); 1080 if (session) { 1081 SSL_set_session(TLScontext->con, session); 1082 SSL_SESSION_free(session); /* 200411 */ 1083 } 1084 } 1085 1086 /* 1087 * Before really starting anything, try to seed the PRNG a little bit 1088 * more. 1089 */ 1090 tls_int_seed(); 1091 (void) tls_ext_seed(var_tls_daemon_rand_bytes); 1092 1093 /* 1094 * Connect the SSL connection with the network socket. 1095 */ 1096 if (SSL_set_fd(TLScontext->con, props->stream == 0 ? props->fd : 1097 vstream_fileno(props->stream)) != 1) { 1098 msg_info("SSL_set_fd error to %s", props->namaddr); 1099 tls_print_errors(); 1100 uncache_session(app_ctx->ssl_ctx, TLScontext); 1101 tls_free_context(TLScontext); 1102 return (0); 1103 } 1104 1105 /* 1106 * If the debug level selected is high enough, all of the data is dumped: 1107 * TLS_LOG_TLSPKTS will dump the SSL negotiation, TLS_LOG_ALLPKTS will 1108 * dump everything. 1109 * 1110 * We do have an SSL_set_fd() and now suddenly a BIO_ routine is called? 1111 * Well there is a BIO below the SSL routines that is automatically 1112 * created for us, so we can use it for debugging purposes. 1113 */ 1114 if (log_mask & TLS_LOG_TLSPKTS) 1115 tls_set_bio_callback(SSL_get_rbio(TLScontext->con), tls_bio_dump_cb); 1116 1117 /* 1118 * If we don't trigger the handshake in the library, leave control over 1119 * SSL_connect/read/write/etc with the application. 1120 */ 1121 if (props->stream == 0) 1122 return (TLScontext); 1123 1124 /* 1125 * Turn on non-blocking I/O so that we can enforce timeouts on network 1126 * I/O. 1127 */ 1128 non_blocking(vstream_fileno(props->stream), NON_BLOCKING); 1129 1130 /* 1131 * Start TLS negotiations. This process is a black box that invokes our 1132 * call-backs for certificate verification. 1133 * 1134 * Error handling: If the SSL handshake fails, we print out an error message 1135 * and remove all TLS state concerning this session. 1136 */ 1137 sts = tls_bio_connect(vstream_fileno(props->stream), props->timeout, 1138 TLScontext); 1139 if (sts <= 0) { 1140 if (ERR_peek_error() != 0) { 1141 msg_info("SSL_connect error to %s: %d", props->namaddr, sts); 1142 tls_print_errors(); 1143 } else if (errno != 0) { 1144 msg_info("SSL_connect error to %s: %m", props->namaddr); 1145 } else { 1146 msg_info("SSL_connect error to %s: lost connection", 1147 props->namaddr); 1148 } 1149 uncache_session(app_ctx->ssl_ctx, TLScontext); 1150 tls_free_context(TLScontext); 1151 return (0); 1152 } 1153 return (tls_client_post_connect(TLScontext, props)); 1154 } 1155 1156 /* tls_client_post_connect - post-handshake processing */ 1157 1158 TLS_SESS_STATE *tls_client_post_connect(TLS_SESS_STATE *TLScontext, 1159 const TLS_CLIENT_START_PROPS *props) 1160 { 1161 const SSL_CIPHER *cipher; 1162 X509 *peercert; 1163 1164 /* Turn off packet dump if only dumping the handshake */ 1165 if ((TLScontext->log_mask & TLS_LOG_ALLPKTS) == 0) 1166 tls_set_bio_callback(SSL_get_rbio(TLScontext->con), 0); 1167 1168 /* 1169 * The caller may want to know if this session was reused or if a new 1170 * session was negotiated. 1171 */ 1172 TLScontext->session_reused = SSL_session_reused(TLScontext->con); 1173 if ((TLScontext->log_mask & TLS_LOG_CACHE) && TLScontext->session_reused) 1174 msg_info("%s: Reusing old session", TLScontext->namaddr); 1175 1176 /* 1177 * Do peername verification if requested and extract useful information 1178 * from the certificate for later use. 1179 */ 1180 if ((peercert = TLS_PEEK_PEER_CERT(TLScontext->con)) != 0) { 1181 TLScontext->peer_status |= TLS_CERT_FLAG_PRESENT; 1182 1183 /* 1184 * Peer name or fingerprint verification as requested. 1185 * Unconditionally set peer_CN, issuer_CN and peer_cert_fprint. Check 1186 * fingerprint first, and avoid logging verified as untrusted in the 1187 * call to verify_extract_name(). 1188 */ 1189 TLScontext->peer_cert_fprint = tls_cert_fprint(peercert, props->mdalg); 1190 TLScontext->peer_pkey_fprint = tls_pkey_fprint(peercert, props->mdalg); 1191 verify_extract_name(TLScontext, peercert, props); 1192 1193 if (TLScontext->log_mask & 1194 (TLS_LOG_CERTMATCH | TLS_LOG_VERBOSE | TLS_LOG_PEERCERT)) 1195 msg_info("%s: subject_CN=%s, issuer_CN=%s, " 1196 "fingerprint=%s, pkey_fingerprint=%s", props->namaddr, 1197 TLScontext->peer_CN, TLScontext->issuer_CN, 1198 TLScontext->peer_cert_fprint, 1199 TLScontext->peer_pkey_fprint); 1200 } else { 1201 TLScontext->issuer_CN = mystrdup(""); 1202 TLScontext->peer_CN = mystrdup(""); 1203 TLScontext->peer_cert_fprint = mystrdup(""); 1204 TLScontext->peer_pkey_fprint = mystrdup(""); 1205 } 1206 1207 /* 1208 * Finally, collect information about protocol and cipher for logging 1209 */ 1210 TLScontext->protocol = SSL_get_version(TLScontext->con); 1211 cipher = SSL_get_current_cipher(TLScontext->con); 1212 TLScontext->cipher_name = SSL_CIPHER_get_name(cipher); 1213 TLScontext->cipher_usebits = SSL_CIPHER_get_bits(cipher, 1214 &(TLScontext->cipher_algbits)); 1215 1216 /* 1217 * The TLS engine is active. Switch to the tls_timed_read/write() 1218 * functions and make the TLScontext available to those functions. 1219 */ 1220 if (TLScontext->stream != 0) 1221 tls_stream_start(props->stream, TLScontext); 1222 1223 /* 1224 * With the handshake done, extract TLS 1.3 signature metadata. 1225 */ 1226 tls_get_signature_params(TLScontext); 1227 1228 if (TLScontext->log_mask & TLS_LOG_SUMMARY) 1229 tls_log_summary(TLS_ROLE_CLIENT, TLS_USAGE_NEW, TLScontext); 1230 1231 tls_int_seed(); 1232 1233 return (TLScontext); 1234 } 1235 1236 #endif /* USE_TLS */ 1237