1 /* $NetBSD: tls_server.c,v 1.3 2010/06/17 18:18:16 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* tls_server 3 6 /* SUMMARY 7 /* server-side TLS engine 8 /* SYNOPSIS 9 /* #include <tls.h> 10 /* 11 /* TLS_APPL_STATE *tls_server_init(props) 12 /* const TLS_SERVER_INIT_PROPS *props; 13 /* 14 /* TLS_SESS_STATE *tls_server_start(props) 15 /* const TLS_SERVER_START_PROPS *props; 16 /* 17 /* void tls_server_stop(app_ctx, stream, failure, TLScontext) 18 /* TLS_APPL_STATE *app_ctx; 19 /* VSTREAM *stream; 20 /* int failure; 21 /* TLS_SESS_STATE *TLScontext; 22 /* DESCRIPTION 23 /* This module is the interface between Postfix TLS servers, 24 /* the OpenSSL library, and the TLS entropy and cache manager. 25 /* 26 /* tls_server_init() is called once when the SMTP server 27 /* initializes. 28 /* Certificate details are also decided during this phase, 29 /* so that peer-specific behavior is not possible. 30 /* 31 /* tls_server_start() activates the TLS feature for the VSTREAM 32 /* passed as argument. We assume that network buffers are flushed 33 /* and the TLS handshake can begin immediately. 34 /* 35 /* tls_server_stop() sends the "close notify" alert via 36 /* SSL_shutdown() to the peer and resets all connection specific 37 /* TLS data. As RFC2487 does not specify a separate shutdown, it 38 /* is assumed that the underlying TCP connection is shut down 39 /* immediately afterwards. Any further writes to the channel will 40 /* be discarded, and any further reads will report end-of-file. 41 /* If the failure flag is set, no SSL_shutdown() handshake is performed. 42 /* 43 /* Once the TLS connection is initiated, information about the TLS 44 /* state is available via the TLScontext structure: 45 /* .IP TLScontext->protocol 46 /* the protocol name (SSLv2, SSLv3, TLSv1), 47 /* .IP TLScontext->cipher_name 48 /* the cipher name (e.g. RC4/MD5), 49 /* .IP TLScontext->cipher_usebits 50 /* the number of bits actually used (e.g. 40), 51 /* .IP TLScontext->cipher_algbits 52 /* the number of bits the algorithm is based on (e.g. 128). 53 /* .PP 54 /* The last two values may differ from each other when export-strength 55 /* encryption is used. 56 /* 57 /* If the peer offered a certificate, part of the certificate data are 58 /* available as: 59 /* .IP TLScontext->peer_status 60 /* A bitmask field that records the status of the peer certificate 61 /* verification. One or more of TLS_CERT_FLAG_PRESENT and 62 /* TLS_CERT_FLAG_TRUSTED. 63 /* .IP TLScontext->peer_CN 64 /* Extracted CommonName of the peer, or zero-length string 65 /* when information could not be extracted. 66 /* .IP TLScontext->issuer_CN 67 /* Extracted CommonName of the issuer, or zero-length string 68 /* when information could not be extracted. 69 /* .IP TLScontext->peer_fingerprint 70 /* Fingerprint of the certificate, or zero-length string when no peer 71 /* certificate is available. 72 /* .PP 73 /* If no peer certificate is presented the peer_status is set to 0. 74 /* LICENSE 75 /* .ad 76 /* .fi 77 /* This software is free. You can do with it whatever you want. 78 /* The original author kindly requests that you acknowledge 79 /* the use of his software. 80 /* AUTHOR(S) 81 /* Originally written by: 82 /* Lutz Jaenicke 83 /* BTU Cottbus 84 /* Allgemeine Elektrotechnik 85 /* Universitaetsplatz 3-4 86 /* D-03044 Cottbus, Germany 87 /* 88 /* Updated by: 89 /* Wietse Venema 90 /* IBM T.J. Watson Research 91 /* P.O. Box 704 92 /* Yorktown Heights, NY 10598, USA 93 /* 94 /* Victor Duchovni 95 /* Morgan Stanley 96 /*--*/ 97 98 /* System library. */ 99 100 #include <sys_defs.h> 101 102 #ifdef USE_TLS 103 #include <unistd.h> 104 #include <string.h> 105 106 /* Utility library. */ 107 108 #include <mymalloc.h> 109 #include <vstring.h> 110 #include <vstream.h> 111 #include <dict.h> 112 #include <stringops.h> 113 #include <msg.h> 114 #include <hex_code.h> 115 116 /* Global library. */ 117 118 #include <mail_params.h> 119 120 /* TLS library. */ 121 122 #include <tls_mgr.h> 123 #define TLS_INTERNAL 124 #include <tls.h> 125 126 #define STR(x) vstring_str(x) 127 #define LEN(x) VSTRING_LEN(x) 128 129 /* Application-specific. */ 130 131 /* 132 * The session_id_context indentifies the service that created a session. 133 * This information is used to distinguish between multiple TLS-based 134 * servers running on the same server. We use the name of the mail system. 135 */ 136 static const char server_session_id_context[] = "Postfix/TLS"; 137 138 /* get_server_session_cb - callback to retrieve session from server cache */ 139 140 static SSL_SESSION *get_server_session_cb(SSL *ssl, unsigned char *session_id, 141 int session_id_length, 142 int *unused_copy) 143 { 144 const char *myname = "get_server_session_cb"; 145 TLS_SESS_STATE *TLScontext; 146 VSTRING *cache_id; 147 VSTRING *session_data = vstring_alloc(2048); 148 SSL_SESSION *session = 0; 149 150 if ((TLScontext = SSL_get_ex_data(ssl, TLScontext_index)) == 0) 151 msg_panic("%s: null TLScontext in session lookup callback", myname); 152 153 #define GEN_CACHE_ID(buf, id, len, service) \ 154 do { \ 155 buf = vstring_alloc(2 * (len) + 1 + strlen(service) + 3); \ 156 hex_encode(buf, (char *) (id), (len)); \ 157 vstring_sprintf_append(buf, "&s=%s", (service)); \ 158 } while (0) 159 160 161 GEN_CACHE_ID(cache_id, session_id, session_id_length, TLScontext->serverid); 162 163 if (TLScontext->log_level >= 2) 164 msg_info("%s: looking up session %s in %s cache", TLScontext->namaddr, 165 STR(cache_id), TLScontext->cache_type); 166 167 /* 168 * Load the session from cache and decode it. 169 */ 170 if (tls_mgr_lookup(TLScontext->cache_type, STR(cache_id), 171 session_data) == TLS_MGR_STAT_OK) { 172 session = tls_session_activate(STR(session_data), LEN(session_data)); 173 if (session && (TLScontext->log_level >= 2)) 174 msg_info("%s: reloaded session %s from %s cache", 175 TLScontext->namaddr, STR(cache_id), 176 TLScontext->cache_type); 177 } 178 179 /* 180 * Clean up. 181 */ 182 vstring_free(cache_id); 183 vstring_free(session_data); 184 185 return (session); 186 } 187 188 /* uncache_session - remove session from internal & external cache */ 189 190 static void uncache_session(SSL_CTX *ctx, TLS_SESS_STATE *TLScontext) 191 { 192 VSTRING *cache_id; 193 SSL_SESSION *session = SSL_get_session(TLScontext->con); 194 195 SSL_CTX_remove_session(ctx, session); 196 197 if (TLScontext->cache_type == 0) 198 return; 199 200 GEN_CACHE_ID(cache_id, session->session_id, session->session_id_length, 201 TLScontext->serverid); 202 203 if (TLScontext->log_level >= 2) 204 msg_info("%s: remove session %s from %s cache", TLScontext->namaddr, 205 STR(cache_id), TLScontext->cache_type); 206 207 tls_mgr_delete(TLScontext->cache_type, STR(cache_id)); 208 vstring_free(cache_id); 209 } 210 211 /* new_server_session_cb - callback to save session to server cache */ 212 213 static int new_server_session_cb(SSL *ssl, SSL_SESSION *session) 214 { 215 const char *myname = "new_server_session_cb"; 216 VSTRING *cache_id; 217 TLS_SESS_STATE *TLScontext; 218 VSTRING *session_data; 219 220 if ((TLScontext = SSL_get_ex_data(ssl, TLScontext_index)) == 0) 221 msg_panic("%s: null TLScontext in new session callback", myname); 222 223 GEN_CACHE_ID(cache_id, session->session_id, session->session_id_length, 224 TLScontext->serverid); 225 226 if (TLScontext->log_level >= 2) 227 msg_info("%s: save session %s to %s cache", TLScontext->namaddr, 228 STR(cache_id), TLScontext->cache_type); 229 230 /* 231 * Passivate and save the session state. 232 */ 233 session_data = tls_session_passivate(session); 234 if (session_data) 235 tls_mgr_update(TLScontext->cache_type, STR(cache_id), 236 STR(session_data), LEN(session_data)); 237 238 /* 239 * Clean up. 240 */ 241 if (session_data) 242 vstring_free(session_data); 243 vstring_free(cache_id); 244 SSL_SESSION_free(session); /* 200502 */ 245 246 return (1); 247 } 248 249 /* tls_server_init - initialize the server-side TLS engine */ 250 251 TLS_APPL_STATE *tls_server_init(const TLS_SERVER_INIT_PROPS *props) 252 { 253 SSL_CTX *server_ctx; 254 long off = 0; 255 int verify_flags = SSL_VERIFY_NONE; 256 int cachable; 257 int protomask; 258 TLS_APPL_STATE *app_ctx; 259 const EVP_MD *md_alg; 260 unsigned int md_len; 261 262 if (props->log_level >= 2) 263 msg_info("initializing the server-side TLS engine"); 264 265 /* 266 * Load (mostly cipher related) TLS-library internal main.cf parameters. 267 */ 268 tls_param_init(); 269 270 /* 271 * Detect mismatch between compile-time headers and run-time library. 272 */ 273 tls_check_version(); 274 275 /* 276 * Initialize the OpenSSL library by the book! To start with, we must 277 * initialize the algorithms. We want cleartext error messages instead of 278 * just error codes, so we load the error_strings. 279 */ 280 SSL_load_error_strings(); 281 OpenSSL_add_ssl_algorithms(); 282 283 /* 284 * First validate the protocols. If these are invalid, we can't continue. 285 */ 286 protomask = tls_protocol_mask(props->protocols); 287 if (protomask == TLS_PROTOCOL_INVALID) { 288 /* tls_protocol_mask() logs no warning. */ 289 msg_warn("Invalid TLS protocol list \"%s\": disabling TLS support", 290 props->protocols); 291 return (0); 292 } 293 294 /* 295 * Create an application data index for SSL objects, so that we can 296 * attach TLScontext information; this information is needed inside 297 * tls_verify_certificate_callback(). 298 */ 299 if (TLScontext_index < 0) { 300 if ((TLScontext_index = SSL_get_ex_new_index(0, 0, 0, 0, 0)) < 0) { 301 msg_warn("Cannot allocate SSL application data index: " 302 "disabling TLS support"); 303 return (0); 304 } 305 } 306 307 /* 308 * If the administrator specifies an unsupported digest algorithm, fail 309 * now, rather than in the middle of a TLS handshake. 310 */ 311 if ((md_alg = EVP_get_digestbyname(props->fpt_dgst)) == 0) { 312 msg_warn("Digest algorithm \"%s\" not found: disabling TLS support", 313 props->fpt_dgst); 314 return (0); 315 } 316 317 /* 318 * Sanity check: Newer shared libraries may use larger digests. 319 */ 320 if ((md_len = EVP_MD_size(md_alg)) > EVP_MAX_MD_SIZE) { 321 msg_warn("Digest algorithm \"%s\" output size %u too large:" 322 " disabling TLS support", props->fpt_dgst, md_len); 323 return (0); 324 } 325 326 /* 327 * Initialize the PRNG (Pseudo Random Number Generator) with some seed 328 * from external and internal sources. Don't enable TLS without some real 329 * entropy. 330 */ 331 if (tls_ext_seed(var_tls_daemon_rand_bytes) < 0) { 332 msg_warn("no entropy for TLS key generation: disabling TLS support"); 333 return (0); 334 } 335 tls_int_seed(); 336 337 /* 338 * The SSL/TLS specifications require the client to send a message in the 339 * oldest specification it understands with the highest level it 340 * understands in the message. Netscape communicator can still 341 * communicate with SSLv2 servers, so it sends out a SSLv2 client hello. 342 * To deal with it, our server must be SSLv2 aware (even if we don't like 343 * SSLv2), so we need to have the SSLv23 server here. If we want to limit 344 * the protocol level, we can add an option to not use SSLv2/v3/TLSv1 345 * later. 346 */ 347 ERR_clear_error(); 348 if ((server_ctx = SSL_CTX_new(SSLv23_server_method())) == 0) { 349 msg_warn("cannot allocate server SSL_CTX: disabling TLS support"); 350 tls_print_errors(); 351 return (0); 352 } 353 354 /* 355 * See the verify callback in tls_verify.c 356 */ 357 SSL_CTX_set_verify_depth(server_ctx, props->verifydepth + 1); 358 359 /* 360 * Protocol work-arounds, OpenSSL version dependent. 361 */ 362 off |= tls_bug_bits(); 363 SSL_CTX_set_options(server_ctx, off); 364 365 /* 366 * Global protocol selection. 367 */ 368 if (protomask != 0) 369 SSL_CTX_set_options(server_ctx, 370 ((protomask & TLS_PROTOCOL_TLSv1) ? SSL_OP_NO_TLSv1 : 0L) 371 | ((protomask & TLS_PROTOCOL_SSLv3) ? SSL_OP_NO_SSLv3 : 0L) 372 | ((protomask & TLS_PROTOCOL_SSLv2) ? SSL_OP_NO_SSLv2 : 0L)); 373 374 /* 375 * Set the call-back routine to debug handshake progress. 376 */ 377 if (props->log_level >= 2) 378 SSL_CTX_set_info_callback(server_ctx, tls_info_callback); 379 380 /* 381 * Load the CA public key certificates for both the server cert and for 382 * the verification of client certificates. As provided by OpenSSL we 383 * support two types of CA certificate handling: One possibility is to 384 * add all CA certificates to one large CAfile, the other possibility is 385 * a directory pointed to by CApath, containing separate files for each 386 * CA with softlinks named after the hash values of the certificate. The 387 * first alternative has the advantage that the file is opened and read 388 * at startup time, so that you don't have the hassle to maintain another 389 * copy of the CApath directory for chroot-jail. 390 */ 391 if (tls_set_ca_certificate_info(server_ctx, 392 props->CAfile, props->CApath) < 0) { 393 /* tls_set_ca_certificate_info() already logs a warning. */ 394 SSL_CTX_free(server_ctx); /* 200411 */ 395 return (0); 396 } 397 398 /* 399 * Load the server public key certificate and private key from file and 400 * check whether the cert matches the key. We can use RSA certificates 401 * ("cert") DSA certificates ("dcert") or ECDSA certificates ("eccert"). 402 * All three can be made available at the same time. The CA certificates 403 * for all three are handled in the same setup already finished. Which 404 * one is used depends on the cipher negotiated (that is: the first 405 * cipher listed by the client which does match the server). A client 406 * with RSA only (e.g. Netscape) will use the RSA certificate only. A 407 * client with openssl-library will use RSA first if not especially 408 * changed in the cipher setup. 409 */ 410 if (tls_set_my_certificate_key_info(server_ctx, 411 props->cert_file, 412 props->key_file, 413 props->dcert_file, 414 props->dkey_file, 415 props->eccert_file, 416 props->eckey_file) < 0) { 417 /* tls_set_my_certificate_key_info() already logs a warning. */ 418 SSL_CTX_free(server_ctx); /* 200411 */ 419 return (0); 420 } 421 422 /* 423 * According to the OpenSSL documentation, temporary RSA key is needed 424 * export ciphers are in use. We have to provide one, so well, we just do 425 * it. 426 */ 427 SSL_CTX_set_tmp_rsa_callback(server_ctx, tls_tmp_rsa_cb); 428 429 /* 430 * Diffie-Hellman key generation parameters can either be loaded from 431 * files (preferred) or taken from compiled in values. First, set the 432 * callback that will select the values when requested, then load the 433 * (possibly) available DH parameters from files. We are generous with 434 * the error handling, since we do have default values compiled in, so we 435 * will not abort but just log the error message. 436 */ 437 SSL_CTX_set_tmp_dh_callback(server_ctx, tls_tmp_dh_cb); 438 if (*props->dh1024_param_file != 0) 439 tls_set_dh_from_file(props->dh1024_param_file, 1024); 440 if (*props->dh512_param_file != 0) 441 tls_set_dh_from_file(props->dh512_param_file, 512); 442 443 /* 444 * Enable EECDH if available, errors are not fatal, we just keep going 445 * with any remaining key-exchange algorithms. 446 */ 447 (void) tls_set_eecdh_curve(server_ctx, props->eecdh_grade); 448 449 /* 450 * If we want to check client certificates, we have to indicate it in 451 * advance. By now we only allow to decide on a global basis. If we want 452 * to allow certificate based relaying, we must ask the client to provide 453 * one with SSL_VERIFY_PEER. The client now can decide, whether it 454 * provides one or not. We can enforce a failure of the negotiation with 455 * SSL_VERIFY_FAIL_IF_NO_PEER_CERT, if we do not allow a connection 456 * without one. In the "server hello" following the initialization by the 457 * "client hello" the server must provide a list of CAs it is willing to 458 * accept. Some clever clients will then select one from the list of 459 * available certificates matching these CAs. Netscape Communicator will 460 * present the list of certificates for selecting the one to be sent, or 461 * it will issue a warning, if there is no certificate matching the 462 * available CAs. 463 * 464 * With regard to the purpose of the certificate for relaying, we might like 465 * a later negotiation, maybe relaying would already be allowed for other 466 * reasons, but this would involve severe changes in the internal postfix 467 * logic, so we have to live with it the way it is. 468 */ 469 if (props->ask_ccert) 470 verify_flags = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE; 471 SSL_CTX_set_verify(server_ctx, verify_flags, 472 tls_verify_certificate_callback); 473 if (*props->CAfile) 474 SSL_CTX_set_client_CA_list(server_ctx, 475 SSL_load_client_CA_file(props->CAfile)); 476 477 /* 478 * Initialize our own TLS server handle, before diving into the details 479 * of TLS session cache management. 480 */ 481 app_ctx = tls_alloc_app_context(server_ctx); 482 483 /* 484 * The session cache is implemented by the tlsmgr(8) server. 485 * 486 * XXX 200502 Surprise: when OpenSSL purges an entry from the in-memory 487 * cache, it also attempts to purge the entry from the on-disk cache. 488 * This is undesirable, especially when we set the in-memory cache size 489 * to 1. For this reason we don't allow OpenSSL to purge on-disk cache 490 * entries, and leave it up to the tlsmgr process instead. Found by 491 * Victor Duchovni. 492 */ 493 494 if (tls_mgr_policy(props->cache_type, &cachable) != TLS_MGR_STAT_OK) 495 cachable = 0; 496 497 if (cachable || props->set_sessid) { 498 499 /* 500 * Initialize the session cache. 501 * 502 * With a large number of concurrent smtpd(8) processes, it is not a 503 * good idea to cache multiple large session objects in each process. 504 * We set the internal cache size to 1, and don't register a 505 * "remove_cb" so as to avoid deleting good sessions from the 506 * external cache prematurely (when the internal cache is full, 507 * OpenSSL removes sessions from the external cache also)! 508 * 509 * This makes SSL_CTX_remove_session() not useful for flushing broken 510 * sessions from the external cache, so we must delete them directly 511 * (not via a callback). 512 * 513 * Set a session id context to identify to what type of server process 514 * created a session. In our case, the context is simply the name of 515 * the mail system: "Postfix/TLS". 516 */ 517 SSL_CTX_sess_set_cache_size(server_ctx, 1); 518 SSL_CTX_set_session_id_context(server_ctx, 519 (void *) &server_session_id_context, 520 sizeof(server_session_id_context)); 521 SSL_CTX_set_session_cache_mode(server_ctx, 522 SSL_SESS_CACHE_SERVER | 523 SSL_SESS_CACHE_NO_AUTO_CLEAR); 524 if (cachable) { 525 app_ctx->cache_type = mystrdup(props->cache_type); 526 527 SSL_CTX_sess_set_get_cb(server_ctx, get_server_session_cb); 528 SSL_CTX_sess_set_new_cb(server_ctx, new_server_session_cb); 529 } 530 531 /* 532 * OpenSSL ignores timed-out sessions. We need to set the internal 533 * cache timeout at least as high as the external cache timeout. This 534 * applies even if no internal cache is used. 535 */ 536 SSL_CTX_set_timeout(server_ctx, props->scache_timeout); 537 } else { 538 539 /* 540 * If we have no external cache, disable all caching. No use wasting 541 * server memory resources with sessions they are unlikely to be able 542 * to reuse. 543 */ 544 SSL_CTX_set_session_cache_mode(server_ctx, SSL_SESS_CACHE_OFF); 545 } 546 547 return (app_ctx); 548 } 549 550 /* 551 * This is the actual startup routine for a new connection. We expect that 552 * the SMTP buffers are flushed and the "220 Ready to start TLS" was sent to 553 * the client, so that we can immediately start the TLS handshake process. 554 */ 555 TLS_SESS_STATE *tls_server_start(const TLS_SERVER_START_PROPS *props) 556 { 557 int sts; 558 TLS_SESS_STATE *TLScontext; 559 const SSL_CIPHER *cipher; 560 X509 *peer; 561 char buf[CCERT_BUFSIZ]; 562 const char *cipher_list; 563 TLS_APPL_STATE *app_ctx = props->ctx; 564 565 if (props->log_level >= 1) 566 msg_info("setting up TLS connection from %s", props->namaddr); 567 568 cipher_list = tls_set_ciphers(app_ctx, "TLS", props->cipher_grade, 569 props->cipher_exclusions); 570 if (cipher_list == 0) { 571 msg_warn("%s: %s: aborting TLS session", props->namaddr, 572 vstring_str(app_ctx->why)); 573 return (0); 574 } 575 if (props->log_level >= 2) 576 msg_info("%s: TLS cipher list \"%s\"", props->namaddr, cipher_list); 577 578 /* 579 * Allocate a new TLScontext for the new connection and get an SSL 580 * structure. Add the location of TLScontext to the SSL to later retrieve 581 * the information inside the tls_verify_certificate_callback(). 582 */ 583 TLScontext = tls_alloc_sess_context(props->log_level, props->namaddr); 584 TLScontext->cache_type = app_ctx->cache_type; 585 586 TLScontext->serverid = mystrdup(props->serverid); 587 TLScontext->am_server = 1; 588 589 ERR_clear_error(); 590 if ((TLScontext->con = (SSL *) SSL_new(app_ctx->ssl_ctx)) == 0) { 591 msg_warn("Could not allocate 'TLScontext->con' with SSL_new()"); 592 tls_print_errors(); 593 tls_free_context(TLScontext); 594 return (0); 595 } 596 if (!SSL_set_ex_data(TLScontext->con, TLScontext_index, TLScontext)) { 597 msg_warn("Could not set application data for 'TLScontext->con'"); 598 tls_print_errors(); 599 tls_free_context(TLScontext); 600 return (0); 601 } 602 603 /* 604 * The TLS connection is realized by a BIO_pair, so obtain the pair. 605 * 606 * XXX There is no need to store the internal_bio handle in the TLScontext 607 * structure. It will be attached to and destroyed with TLScontext->con. 608 * The network_bio, however, needs to be freed explicitly, so we need to 609 * store its handle in TLScontext. 610 */ 611 if (!BIO_new_bio_pair(&TLScontext->internal_bio, TLS_BIO_BUFSIZE, 612 &TLScontext->network_bio, TLS_BIO_BUFSIZE)) { 613 msg_warn("Could not obtain BIO_pair"); 614 tls_print_errors(); 615 tls_free_context(TLScontext); 616 return (0); 617 } 618 619 /* 620 * Before really starting anything, try to seed the PRNG a little bit 621 * more. 622 */ 623 tls_int_seed(); 624 (void) tls_ext_seed(var_tls_daemon_rand_bytes); 625 626 /* 627 * Initialize the SSL connection to accept state. This should not be 628 * necessary anymore since 0.9.3, but the call is still in the library 629 * and maintaining compatibility never hurts. 630 */ 631 SSL_set_accept_state(TLScontext->con); 632 633 /* 634 * Connect the SSL connection with the Postfix side of the BIO-pair for 635 * reading and writing. 636 */ 637 SSL_set_bio(TLScontext->con, TLScontext->internal_bio, 638 TLScontext->internal_bio); 639 640 /* 641 * If the debug level selected is high enough, all of the data is dumped: 642 * 3 will dump the SSL negotiation, 4 will dump everything. 643 * 644 * We do have an SSL_set_fd() and now suddenly a BIO_ routine is called? 645 * Well there is a BIO below the SSL routines that is automatically 646 * created for us, so we can use it for debugging purposes. 647 */ 648 if (props->log_level >= 3) 649 BIO_set_callback(SSL_get_rbio(TLScontext->con), tls_bio_dump_cb); 650 651 /* 652 * Start TLS negotiations. This process is a black box that invokes our 653 * call-backs for session caching and certificate verification. 654 * 655 * Error handling: If the SSL handhake fails, we print out an error message 656 * and remove all TLS state concerning this session. 657 */ 658 sts = tls_bio_accept(vstream_fileno(props->stream), props->timeout, 659 TLScontext); 660 if (sts <= 0) { 661 msg_info("SSL_accept error from %s: %d", props->namaddr, sts); 662 tls_print_errors(); 663 tls_free_context(TLScontext); 664 return (0); 665 } 666 /* Only loglevel==4 dumps everything */ 667 if (props->log_level < 4) 668 BIO_set_callback(SSL_get_rbio(TLScontext->con), 0); 669 670 /* 671 * The caller may want to know if this session was reused or if a new 672 * session was negotiated. 673 */ 674 TLScontext->session_reused = SSL_session_reused(TLScontext->con); 675 if (TLScontext->log_level >= 2 && TLScontext->session_reused) 676 msg_info("%s: Reusing old session", TLScontext->namaddr); 677 678 /* 679 * Let's see whether a peer certificate is available and what is the 680 * actual information. We want to save it for later use. 681 */ 682 peer = SSL_get_peer_certificate(TLScontext->con); 683 if (peer != NULL) { 684 TLScontext->peer_status |= TLS_CERT_FLAG_PRESENT; 685 if (SSL_get_verify_result(TLScontext->con) == X509_V_OK) 686 TLScontext->peer_status |= TLS_CERT_FLAG_TRUSTED; 687 688 if (props->log_level >= 2) { 689 X509_NAME_oneline(X509_get_subject_name(peer), 690 buf, sizeof(buf)); 691 msg_info("subject=%s", buf); 692 X509_NAME_oneline(X509_get_issuer_name(peer), 693 buf, sizeof(buf)); 694 msg_info("issuer=%s", buf); 695 } 696 TLScontext->peer_CN = tls_peer_CN(peer, TLScontext); 697 TLScontext->issuer_CN = tls_issuer_CN(peer, TLScontext); 698 TLScontext->peer_fingerprint = tls_fingerprint(peer, props->fpt_dgst); 699 700 if (props->log_level >= 1) { 701 msg_info("%s: %s: subject_CN=%s, issuer=%s, fingerprint=%s", 702 props->namaddr, 703 TLS_CERT_IS_TRUSTED(TLScontext) ? "Trusted" : "Untrusted", 704 TLScontext->peer_CN, TLScontext->issuer_CN, 705 TLScontext->peer_fingerprint); 706 } 707 X509_free(peer); 708 } else { 709 TLScontext->peer_CN = mystrdup(""); 710 TLScontext->issuer_CN = mystrdup(""); 711 TLScontext->peer_fingerprint = mystrdup(""); 712 } 713 714 /* 715 * Finally, collect information about protocol and cipher for logging 716 */ 717 TLScontext->protocol = SSL_get_version(TLScontext->con); 718 cipher = SSL_get_current_cipher(TLScontext->con); 719 TLScontext->cipher_name = SSL_CIPHER_get_name(cipher); 720 TLScontext->cipher_usebits = SSL_CIPHER_get_bits(cipher, 721 &(TLScontext->cipher_algbits)); 722 723 /* 724 * The TLS engine is active. Switch to the tls_timed_read/write() 725 * functions and make the TLScontext available to those functions. 726 */ 727 tls_stream_start(props->stream, TLScontext); 728 729 /* 730 * All the key facts in a single log entry. 731 */ 732 if (props->log_level >= 1) 733 msg_info("%s TLS connection established from %s: %s with cipher %s " 734 "(%d/%d bits)", !TLS_CERT_IS_PRESENT(TLScontext) ? "Anonymous" 735 : TLS_CERT_IS_TRUSTED(TLScontext) ? "Trusted" : "Untrusted", 736 props->namaddr, TLScontext->protocol, TLScontext->cipher_name, 737 TLScontext->cipher_usebits, TLScontext->cipher_algbits); 738 739 tls_int_seed(); 740 741 return (TLScontext); 742 } 743 744 #endif /* USE_TLS */ 745