xref: /netbsd-src/external/ibm-public/postfix/dist/src/tls/tls_client.c (revision c48c605c14fd8622b523d1d6a3f0c0bad133ea89)
1 /*	$NetBSD: tls_client.c,v 1.13 2023/12/23 20:30:45 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 
load_clnt_session(TLS_SESS_STATE * TLScontext)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 
new_client_session_cb(SSL * ssl,SSL_SESSION * session)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 
uncache_session(SSL_CTX * ctx,TLS_SESS_STATE * TLScontext)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 
verify_extract_name(TLS_SESS_STATE * TLScontext,X509 * peercert,const TLS_CLIENT_START_PROPS * props)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 	TLScontext->peer_status |= TLS_CERT_FLAG_TRUSTED;
330 	if (TLScontext->must_fail) {
331 	    msg_panic("%s: cert valid despite trust init failure",
332 		      TLScontext->namaddr);
333 	} else if (TLS_MUST_MATCH(TLScontext->level)) {
334 
335 	    /*
336 	     * Fully secured only if not insecure like half-dane.  We use
337 	     * TLS_CERT_FLAG_MATCHED to satisfy policy, but
338 	     * TLS_CERT_FLAG_SECURED to log the effective security.
339 	     *
340 	     * Would ideally also exclude "verify" (as opposed to "secure")
341 	     * here, because that can be subject to insecure MX indirection,
342 	     * but that's rather incompatible (and not even the case with
343 	     * explicitly chosen non-default match patterns).  Users have
344 	     * been warned.
345 	     */
346 	    if (!TLS_NEVER_SECURED(TLScontext->level))
347 		TLScontext->peer_status |= TLS_CERT_FLAG_SECURED;
348 	    TLScontext->peer_status |= TLS_CERT_FLAG_MATCHED;
349 
350 	    if (verbose) {
351 		const char *peername = SSL_get0_peername(TLScontext->con);
352 
353 		if (peername)
354 		    msg_info("%s: matched peername: %s",
355 			     TLScontext->namaddr, peername);
356 		tls_dane_log(TLScontext);
357 	    }
358 	}
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 
add_namechecks(TLS_SESS_STATE * TLScontext,const TLS_CLIENT_START_PROPS * props)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 
tls_auth_enable(TLS_SESS_STATE * TLScontext,const TLS_CLIENT_START_PROPS * props)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 
tls_client_init(const TLS_CLIENT_INIT_PROPS * props)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      * Initialize the OpenSSL library, possibly loading its configuration
647      * file.
648      */
649     if (tls_library_init() == 0)
650 	return (0);
651 
652     /*
653      * Create an application data index for SSL objects, so that we can
654      * attach TLScontext information; this information is needed inside
655      * tls_verify_certificate_callback().
656      */
657     if (TLScontext_index < 0) {
658 	if ((TLScontext_index = SSL_get_ex_new_index(0, 0, 0, 0, 0)) < 0) {
659 	    msg_warn("Cannot allocate SSL application data index: "
660 		     "disabling TLS support");
661 	    return (0);
662 	}
663     }
664 
665     /*
666      * If the administrator specifies an unsupported digest algorithm, fail
667      * now, rather than in the middle of a TLS handshake.
668      */
669     if ((fpt_alg = tls_validate_digest(props->mdalg)) == 0) {
670 	msg_warn("disabling TLS support");
671 	return (0);
672     }
673 
674     /*
675      * Initialize the PRNG (Pseudo Random Number Generator) with some seed
676      * from external and internal sources. Don't enable TLS without some real
677      * entropy.
678      */
679     if (tls_ext_seed(var_tls_daemon_rand_bytes) < 0) {
680 	msg_warn("no entropy for TLS key generation: disabling TLS support");
681 	return (0);
682     }
683     tls_int_seed();
684 
685     /*
686      * The SSL/TLS specifications require the client to send a message in the
687      * oldest specification it understands with the highest level it
688      * understands in the message. RFC2487 is only specified for TLSv1, but
689      * we want to be as compatible as possible, so we will start off with a
690      * SSLv2 greeting allowing the best we can offer: TLSv1. We can restrict
691      * this with the options setting later, anyhow.
692      */
693     ERR_clear_error();
694     client_ctx = SSL_CTX_new(TLS_client_method());
695     if (client_ctx == 0) {
696 	msg_warn("cannot allocate client SSL_CTX: disabling TLS support");
697 	tls_print_errors();
698 	return (0);
699     }
700 #ifdef SSL_SECOP_PEER
701     /* Backwards compatible security as a base for opportunistic TLS. */
702     SSL_CTX_set_security_level(client_ctx, 0);
703 #endif
704 
705     /*
706      * See the verify callback in tls_verify.c
707      */
708     SSL_CTX_set_verify_depth(client_ctx, props->verifydepth + 1);
709 
710     /*
711      * This is a prerequisite for enabling DANE support in OpenSSL, but not a
712      * commitment to use DANE, thus suitable for both DANE and non-DANE TLS
713      * connections.  Indeed we need this not just for DANE, but aslo for
714      * fingerprint and "tafile" support.  Since it just allocates memory, it
715      * should never fail except when we're likely to fail anyway.  Rather
716      * than try to run with crippled TLS support, just give up using TLS.
717      */
718     if (SSL_CTX_dane_enable(client_ctx) <= 0) {
719 	msg_warn("OpenSSL DANE initialization failed: disabling TLS support");
720 	tls_print_errors();
721 	return (0);
722     }
723     tls_dane_digest_init(client_ctx, fpt_alg);
724 
725     /*
726      * Presently we use TLS only with SMTP where truncation attacks are not
727      * possible as a result of application framing.  If we ever use TLS in
728      * some other application protocol where truncation could be relevant,
729      * we'd need to disable truncation detection conditionally, or explicitly
730      * clear the option in that code path.
731      */
732     off |= SSL_OP_IGNORE_UNEXPECTED_EOF;
733 
734     /*
735      * Protocol selection is destination dependent, so we delay the protocol
736      * selection options to the per-session SSL object.
737      */
738     off |= tls_bug_bits();
739     SSL_CTX_set_options(client_ctx, off);
740 
741     /*
742      * Set the call-back routine for verbose logging.
743      */
744     if (log_mask & TLS_LOG_DEBUG)
745 	SSL_CTX_set_info_callback(client_ctx, tls_info_callback);
746 
747     /*
748      * Load the CA public key certificates for both the client cert and for
749      * the verification of server certificates. As provided by OpenSSL we
750      * support two types of CA certificate handling: One possibility is to
751      * add all CA certificates to one large CAfile, the other possibility is
752      * a directory pointed to by CApath, containing separate files for each
753      * CA with softlinks named after the hash values of the certificate. The
754      * first alternative has the advantage that the file is opened and read
755      * at startup time, so that you don't have the hassle to maintain another
756      * copy of the CApath directory for chroot-jail.
757      */
758     if (tls_set_ca_certificate_info(client_ctx,
759 				    props->CAfile, props->CApath) < 0) {
760 	/* tls_set_ca_certificate_info() already logs a warning. */
761 	SSL_CTX_free(client_ctx);		/* 200411 */
762 	return (0);
763     }
764 
765     /*
766      * We do not need a client certificate, so the certificates are only
767      * loaded (and checked) if supplied. A clever client would handle
768      * multiple client certificates and decide based on the list of
769      * acceptable CAs, sent by the server, which certificate to submit.
770      * OpenSSL does however not do this and also has no call-back hooks to
771      * easily implement it.
772      *
773      * Load the client public key certificate and private key from file and
774      * check whether the cert matches the key. We can use RSA certificates
775      * ("cert") DSA certificates ("dcert") or ECDSA certificates ("eccert").
776      * All three can be made available at the same time. The CA certificates
777      * for all three are handled in the same setup already finished. Which
778      * one is used depends on the cipher negotiated (that is: the first
779      * cipher listed by the client which does match the server). The client
780      * certificate is presented after the server chooses the session cipher,
781      * so we will just present the right cert for the chosen cipher (if it
782      * uses certificates).
783      */
784     if (tls_set_my_certificate_key_info(client_ctx,
785 					props->chain_files,
786 					props->cert_file,
787 					props->key_file,
788 					props->dcert_file,
789 					props->dkey_file,
790 					props->eccert_file,
791 					props->eckey_file) < 0) {
792 	/* tls_set_my_certificate_key_info() already logs a warning. */
793 	SSL_CTX_free(client_ctx);		/* 200411 */
794 	return (0);
795     }
796 
797     /*
798      * With OpenSSL 1.0.2 and later the client EECDH curve list becomes
799      * configurable with the preferred curve negotiated via the supported
800      * curves extension.  With OpenSSL 3.0 and TLS 1.3, the same applies
801      * to the FFDHE groups which become part of a unified "groups" list.
802      */
803     tls_auto_groups(client_ctx, var_tls_eecdh_auto, var_tls_ffdhe_auto);
804 
805     /*
806      * Finally, the setup for the server certificate checking, done "by the
807      * book".
808      */
809     SSL_CTX_set_verify(client_ctx, SSL_VERIFY_NONE,
810 		       tls_verify_certificate_callback);
811 
812     /*
813      * Initialize the session cache.
814      *
815      * Since the client does not search an internal cache, we simply disable it.
816      * It is only useful for expiring old sessions, but we do that in the
817      * tlsmgr(8).
818      *
819      * This makes SSL_CTX_remove_session() not useful for flushing broken
820      * sessions from the external cache, so we must delete them directly (not
821      * via a callback).
822      */
823     if (tls_mgr_policy(props->cache_type, &cachable,
824 		       &scache_timeout) != TLS_MGR_STAT_OK)
825 	scache_timeout = 0;
826     if (scache_timeout <= 0)
827 	cachable = 0;
828 
829     /*
830      * Allocate an application context, and populate with mandatory protocol
831      * and cipher data.
832      */
833     app_ctx = tls_alloc_app_context(client_ctx, 0, log_mask);
834 
835     /*
836      * The external session cache is implemented by the tlsmgr(8) process.
837      */
838     if (cachable) {
839 
840 	app_ctx->cache_type = mystrdup(props->cache_type);
841 
842 	/*
843 	 * OpenSSL does not use callbacks to load sessions from a client
844 	 * cache, so we must invoke that function directly. Apparently,
845 	 * OpenSSL does not provide a way to pass session names from here to
846 	 * call-back routines that do session lookup.
847 	 *
848 	 * OpenSSL can, however, automatically save newly created sessions for
849 	 * us by callback (we create the session name in the call-back
850 	 * function).
851 	 *
852 	 * XXX gcc 2.95 can't compile #ifdef .. #endif in the expansion of
853 	 * SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE |
854 	 * SSL_SESS_CACHE_NO_AUTO_CLEAR.
855 	 */
856 #ifndef SSL_SESS_CACHE_NO_INTERNAL_STORE
857 #define SSL_SESS_CACHE_NO_INTERNAL_STORE 0
858 #endif
859 
860 	SSL_CTX_set_session_cache_mode(client_ctx,
861 				       SSL_SESS_CACHE_CLIENT |
862 				       SSL_SESS_CACHE_NO_INTERNAL_STORE |
863 				       SSL_SESS_CACHE_NO_AUTO_CLEAR);
864 	SSL_CTX_sess_set_new_cb(client_ctx, new_client_session_cb);
865 
866 	/*
867 	 * OpenSSL ignores timed-out sessions. We need to set the internal
868 	 * cache timeout at least as high as the external cache timeout. This
869 	 * applies even if no internal cache is used.  We set the session to
870 	 * twice the cache lifetime.  This way a session always lasts longer
871 	 * than its lifetime in the cache.
872 	 */
873 	SSL_CTX_set_timeout(client_ctx, 2 * scache_timeout);
874     }
875     return (app_ctx);
876 }
877 
878  /*
879   * This is the actual startup routine for the connection. We expect that the
880   * buffers are flushed and the "220 Ready to start TLS" was received by us,
881   * so that we can immediately start the TLS handshake process.
882   */
tls_client_start(const TLS_CLIENT_START_PROPS * props)883 TLS_SESS_STATE *tls_client_start(const TLS_CLIENT_START_PROPS *props)
884 {
885     int     sts;
886     int     protomask;
887     int     min_proto;
888     int     max_proto;
889     const char *cipher_list;
890     SSL_SESSION *session = 0;
891     TLS_SESS_STATE *TLScontext;
892     TLS_APPL_STATE *app_ctx = props->ctx;
893     int     log_mask = app_ctx->log_mask;
894 
895     /*
896      * When certificate verification is required, log trust chain validation
897      * errors even when disabled by default for opportunistic sessions. For
898      * DANE this only applies when using trust-anchor associations.
899      */
900     if (TLS_MUST_MATCH(props->tls_level))
901 	log_mask |= TLS_LOG_UNTRUSTED;
902 
903     if (log_mask & TLS_LOG_VERBOSE)
904 	msg_info("setting up TLS connection to %s", props->namaddr);
905 
906     /*
907      * First make sure we have valid protocol and cipher parameters
908      *
909      * Per-session protocol restrictions must be applied to the SSL connection,
910      * as restrictions in the global context cannot be cleared.
911      */
912     protomask = tls_proto_mask_lims(props->protocols, &min_proto, &max_proto);
913     if (protomask == TLS_PROTOCOL_INVALID) {
914 	/* tls_protocol_mask() logs no warning. */
915 	msg_warn("%s: Invalid TLS protocol list \"%s\": aborting TLS session",
916 		 props->namaddr, props->protocols);
917 	return (0);
918     }
919 
920     /*
921      * Though RFC7672 set the floor at SSLv3, we really can and should
922      * require TLS 1.0, since e.g. we send SNI, which is a TLS 1.0 extension.
923      * No DANE domains have been observed to support only SSLv3.
924      *
925      * XXX: Would be nice to make that TLS 1.2 at some point.  Users can choose
926      * to exclude TLS 1.0 and TLS 1.1 if they find they don't run into any
927      * problems doing that.
928      */
929     if (TLS_DANE_BASED(props->tls_level))
930 	protomask |= TLS_PROTOCOL_SSLv2 | TLS_PROTOCOL_SSLv3;
931 
932     /*
933      * Allocate a new TLScontext for the new connection and get an SSL
934      * structure. Add the location of TLScontext to the SSL to later retrieve
935      * the information inside the tls_verify_certificate_callback().
936      *
937      * If session caching was enabled when TLS was initialized, the cache type
938      * is stored in the client SSL context.
939      */
940     TLScontext = tls_alloc_sess_context(log_mask, props->namaddr);
941     TLScontext->cache_type = app_ctx->cache_type;
942     TLScontext->level = props->tls_level;
943 
944     if ((TLScontext->con = SSL_new(app_ctx->ssl_ctx)) == NULL) {
945 	msg_warn("Could not allocate 'TLScontext->con' with SSL_new()");
946 	tls_print_errors();
947 	tls_free_context(TLScontext);
948 	return (0);
949     }
950 
951     /*
952      * Per session cipher selection for sessions with mandatory encryption
953      *
954      * The cipherlist is applied to the global SSL context, since it is likely
955      * to stay the same between connections, so we make use of a 1-element
956      * cache to return the same result for identical inputs.
957      */
958     cipher_list = tls_set_ciphers(TLScontext, props->cipher_grade,
959 				  props->cipher_exclusions);
960     if (cipher_list == 0) {
961 	/* already warned */
962 	tls_free_context(TLScontext);
963 	return (0);
964     }
965     if (log_mask & TLS_LOG_VERBOSE)
966 	msg_info("%s: TLS cipher list \"%s\"", props->namaddr, cipher_list);
967 
968     TLScontext->stream = props->stream;
969     TLScontext->mdalg = props->mdalg;
970 
971     /* Alias DANE digest info from props */
972     TLScontext->dane = props->dane;
973 
974     if (!SSL_set_ex_data(TLScontext->con, TLScontext_index, TLScontext)) {
975 	msg_warn("Could not set application data for 'TLScontext->con'");
976 	tls_print_errors();
977 	tls_free_context(TLScontext);
978 	return (0);
979     }
980 #define CARP_VERSION(which) do { \
981         if (which##_proto != 0) \
982             msg_warn("%s: error setting %simum TLS version to: 0x%04x", \
983                      TLScontext->namaddr, #which, which##_proto); \
984         else \
985             msg_warn("%s: error clearing %simum TLS version", \
986                      TLScontext->namaddr, #which); \
987     } while (0)
988 
989     /*
990      * Apply session protocol restrictions.
991      */
992     if (protomask != 0)
993 	SSL_set_options(TLScontext->con, TLS_SSL_OP_PROTOMASK(protomask));
994     if (!SSL_set_min_proto_version(TLScontext->con, min_proto))
995 	CARP_VERSION(min);
996     if (!SSL_set_max_proto_version(TLScontext->con, max_proto))
997 	CARP_VERSION(max);
998 
999     /*
1000      * When applicable, configure DNS-based or synthetic (fingerprint or
1001      * local trust anchor) DANE authentication, enable an appropriate SNI
1002      * name and peer name matching.
1003      *
1004      * NOTE, this can change the effective security level, and needs to happen
1005      * early.
1006      */
1007     if (!tls_auth_enable(TLScontext, props)) {
1008 	tls_free_context(TLScontext);
1009 	return (0);
1010     }
1011 
1012     /*
1013      * Try to convey the configured TLSA records for this connection to the
1014      * OpenSSL library.  If none are "usable", we'll fall back to "encrypt"
1015      * when authentication is not mandatory, otherwise we must arrange to
1016      * ensure authentication failure.
1017      */
1018     if (TLScontext->dane && TLScontext->dane->tlsa) {
1019 	int     usable = tls_dane_enable(TLScontext);
1020 	int     must_fail = usable <= 0;
1021 
1022 	if (usable == 0) {
1023 	    switch (TLScontext->level) {
1024 	    case TLS_LEV_HALF_DANE:
1025 	    case TLS_LEV_DANE:
1026 		msg_warn("%s: all TLSA records unusable, fallback to "
1027 			 "unauthenticated TLS", TLScontext->namaddr);
1028 		must_fail = 0;
1029 		TLScontext->level = TLS_LEV_ENCRYPT;
1030 		break;
1031 
1032 	    case TLS_LEV_FPRINT:
1033 		msg_warn("%s: all fingerprints unusable", TLScontext->namaddr);
1034 		break;
1035 	    case TLS_LEV_DANE_ONLY:
1036 		msg_warn("%s: all TLSA records unusable", TLScontext->namaddr);
1037 		break;
1038 	    case TLS_LEV_SECURE:
1039 	    case TLS_LEV_VERIFY:
1040 		msg_warn("%s: all trust anchors unusable", TLScontext->namaddr);
1041 		break;
1042 	    }
1043 	}
1044 	TLScontext->must_fail |= must_fail;
1045     }
1046 
1047     /*
1048      * We compute the policy digest after we compute the SNI name in
1049      * tls_auth_enable() and possibly update the TLScontext security level.
1050      *
1051      * OpenSSL will ignore cached sessions that use the wrong protocol. So we do
1052      * not need to filter out cached sessions with the "wrong" protocol,
1053      * rather OpenSSL will simply negotiate a new session.
1054      *
1055      * We salt the session lookup key with the protocol list, so that sessions
1056      * found in the cache are plausibly acceptable.
1057      *
1058      * By the time a TLS client is negotiating ciphers it has already offered to
1059      * re-use a session, it is too late to renege on the offer. So we must
1060      * not attempt to re-use sessions whose ciphers are too weak. We salt the
1061      * session lookup key with the cipher list, so that sessions found in the
1062      * cache are always acceptable.
1063      *
1064      * With DANE, (more generally any TLScontext where we specified explicit
1065      * trust-anchor or end-entity certificates) the verification status of
1066      * the SSL session depends on the specified list.  Since we verify the
1067      * certificate only during the initial handshake, we must segregate
1068      * sessions with different TA lists.  Note, that TA re-verification is
1069      * not possible with cached sessions, since these don't hold the complete
1070      * peer trust chain.  Therefore, we compute a digest of the sorted TA
1071      * parameters and append it to the serverid.
1072      */
1073     TLScontext->serverid =
1074 	tls_serverid_digest(TLScontext, props, cipher_list);
1075 
1076     /*
1077      * When authenticating the peer, use 80-bit plus OpenSSL security level
1078      *
1079      * XXX: We should perhaps use security level 1 also for mandatory
1080      * encryption, with only "may" tolerating weaker algorithms.  But that
1081      * could mean no TLS 1.0 with OpenSSL >= 3.0 and encrypt, unless I get my
1082      * patch in on time to conditionally re-enable SHA1 at security level 1,
1083      * and we add code to make it so.
1084      *
1085      * That said, with "encrypt", we could reasonably require TLS 1.2?
1086      */
1087     if (TLS_MUST_MATCH(TLScontext->level))
1088 	SSL_set_security_level(TLScontext->con, 1);
1089 
1090     /*
1091      * XXX To avoid memory leaks we must always call SSL_SESSION_free() after
1092      * calling SSL_set_session(), regardless of whether or not the session
1093      * will be reused.
1094      */
1095     if (TLScontext->cache_type) {
1096 	session = load_clnt_session(TLScontext);
1097 	if (session) {
1098 	    SSL_set_session(TLScontext->con, session);
1099 	    SSL_SESSION_free(session);		/* 200411 */
1100 	}
1101     }
1102 
1103     /*
1104      * Before really starting anything, try to seed the PRNG a little bit
1105      * more.
1106      */
1107     tls_int_seed();
1108     (void) tls_ext_seed(var_tls_daemon_rand_bytes);
1109 
1110     /*
1111      * Connect the SSL connection with the network socket.
1112      */
1113     if (SSL_set_fd(TLScontext->con, props->stream == 0 ? props->fd :
1114 		   vstream_fileno(props->stream)) != 1) {
1115 	msg_info("SSL_set_fd error to %s", props->namaddr);
1116 	tls_print_errors();
1117 	uncache_session(app_ctx->ssl_ctx, TLScontext);
1118 	tls_free_context(TLScontext);
1119 	return (0);
1120     }
1121 
1122     /*
1123      * If the debug level selected is high enough, all of the data is dumped:
1124      * TLS_LOG_TLSPKTS will dump the SSL negotiation, TLS_LOG_ALLPKTS will
1125      * dump everything.
1126      *
1127      * We do have an SSL_set_fd() and now suddenly a BIO_ routine is called?
1128      * Well there is a BIO below the SSL routines that is automatically
1129      * created for us, so we can use it for debugging purposes.
1130      */
1131     if (log_mask & TLS_LOG_TLSPKTS)
1132 	tls_set_bio_callback(SSL_get_rbio(TLScontext->con), tls_bio_dump_cb);
1133 
1134     /*
1135      * If we don't trigger the handshake in the library, leave control over
1136      * SSL_connect/read/write/etc with the application.
1137      */
1138     if (props->stream == 0)
1139 	return (TLScontext);
1140 
1141     /*
1142      * Turn on non-blocking I/O so that we can enforce timeouts on network
1143      * I/O.
1144      */
1145     non_blocking(vstream_fileno(props->stream), NON_BLOCKING);
1146 
1147     /*
1148      * Start TLS negotiations. This process is a black box that invokes our
1149      * call-backs for certificate verification.
1150      *
1151      * Error handling: If the SSL handshake fails, we print out an error message
1152      * and remove all TLS state concerning this session.
1153      */
1154     sts = tls_bio_connect(vstream_fileno(props->stream), props->timeout,
1155 			  TLScontext);
1156     if (sts <= 0) {
1157 	if (ERR_peek_error() != 0) {
1158 	    msg_info("SSL_connect error to %s: %d", props->namaddr, sts);
1159 	    tls_print_errors();
1160 	} else if (errno != 0) {
1161 	    msg_info("SSL_connect error to %s: %m", props->namaddr);
1162 	} else {
1163 	    msg_info("SSL_connect error to %s: lost connection",
1164 		     props->namaddr);
1165 	}
1166 	uncache_session(app_ctx->ssl_ctx, TLScontext);
1167 	tls_free_context(TLScontext);
1168 	return (0);
1169     }
1170     return (tls_client_post_connect(TLScontext, props));
1171 }
1172 
1173 /* tls_client_post_connect - post-handshake processing */
1174 
tls_client_post_connect(TLS_SESS_STATE * TLScontext,const TLS_CLIENT_START_PROPS * props)1175 TLS_SESS_STATE *tls_client_post_connect(TLS_SESS_STATE *TLScontext,
1176 				        const TLS_CLIENT_START_PROPS *props)
1177 {
1178     const SSL_CIPHER *cipher;
1179     X509   *peercert;
1180 
1181     /* Turn off packet dump if only dumping the handshake */
1182     if ((TLScontext->log_mask & TLS_LOG_ALLPKTS) == 0)
1183 	tls_set_bio_callback(SSL_get_rbio(TLScontext->con), 0);
1184 
1185     /*
1186      * The caller may want to know if this session was reused or if a new
1187      * session was negotiated.
1188      */
1189     TLScontext->session_reused = SSL_session_reused(TLScontext->con);
1190     if ((TLScontext->log_mask & TLS_LOG_CACHE) && TLScontext->session_reused)
1191 	msg_info("%s: Reusing old session", TLScontext->namaddr);
1192 
1193     /*
1194      * Do peername verification if requested and extract useful information
1195      * from the certificate for later use.
1196      */
1197     if ((peercert = TLS_PEEK_PEER_CERT(TLScontext->con)) != 0) {
1198 	TLScontext->peer_status |= TLS_CERT_FLAG_PRESENT;
1199 
1200 	/*
1201 	 * Peer name or fingerprint verification as requested.
1202 	 * Unconditionally set peer_CN, issuer_CN and peer_cert_fprint. Check
1203 	 * fingerprint first, and avoid logging verified as untrusted in the
1204 	 * call to verify_extract_name().
1205 	 */
1206 	TLScontext->peer_cert_fprint = tls_cert_fprint(peercert, props->mdalg);
1207 	TLScontext->peer_pkey_fprint = tls_pkey_fprint(peercert, props->mdalg);
1208 	verify_extract_name(TLScontext, peercert, props);
1209 
1210 	if (TLScontext->log_mask &
1211 	    (TLS_LOG_CERTMATCH | TLS_LOG_VERBOSE | TLS_LOG_PEERCERT))
1212 	    msg_info("%s: subject_CN=%s, issuer_CN=%s, "
1213 		     "fingerprint=%s, pkey_fingerprint=%s", props->namaddr,
1214 		     TLScontext->peer_CN, TLScontext->issuer_CN,
1215 		     TLScontext->peer_cert_fprint,
1216 		     TLScontext->peer_pkey_fprint);
1217     } else {
1218 	TLScontext->issuer_CN = mystrdup("");
1219 	TLScontext->peer_CN = mystrdup("");
1220 	TLScontext->peer_cert_fprint = mystrdup("");
1221 	TLScontext->peer_pkey_fprint = mystrdup("");
1222     }
1223 
1224     /*
1225      * Finally, collect information about protocol and cipher for logging
1226      */
1227     TLScontext->protocol = SSL_get_version(TLScontext->con);
1228     cipher = SSL_get_current_cipher(TLScontext->con);
1229     TLScontext->cipher_name = SSL_CIPHER_get_name(cipher);
1230     TLScontext->cipher_usebits = SSL_CIPHER_get_bits(cipher,
1231 					     &(TLScontext->cipher_algbits));
1232 
1233     /*
1234      * The TLS engine is active. Switch to the tls_timed_read/write()
1235      * functions and make the TLScontext available to those functions.
1236      */
1237     if (TLScontext->stream != 0)
1238 	tls_stream_start(props->stream, TLScontext);
1239 
1240     /*
1241      * With the handshake done, extract TLS 1.3 signature metadata.
1242      */
1243     tls_get_signature_params(TLScontext);
1244 
1245     if (TLScontext->log_mask & TLS_LOG_SUMMARY)
1246 	tls_log_summary(TLS_ROLE_CLIENT, TLS_USAGE_NEW, TLScontext);
1247 
1248     tls_int_seed();
1249 
1250     return (TLScontext);
1251 }
1252 
1253 #endif					/* USE_TLS */
1254