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