xref: /onnv-gate/usr/src/cmd/sendmail/src/tls.c (revision 11440:802724e2906a)
10Sstevel@tonic-gate /*
2*11440SJohn.Beck@Sun.COM  * Copyright (c) 2000-2006, 2008, 2009 Sendmail, Inc. and its suppliers.
30Sstevel@tonic-gate  *	All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
60Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
70Sstevel@tonic-gate  * the sendmail distribution.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  */
100Sstevel@tonic-gate 
110Sstevel@tonic-gate #include <sendmail.h>
120Sstevel@tonic-gate 
13*11440SJohn.Beck@Sun.COM SM_RCSID("@(#)$Id: tls.c,v 8.114 2009/08/10 15:11:09 ca Exp $")
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #if STARTTLS
160Sstevel@tonic-gate #  include <openssl/err.h>
170Sstevel@tonic-gate #  include <openssl/bio.h>
180Sstevel@tonic-gate #  include <openssl/pem.h>
190Sstevel@tonic-gate #  ifndef HASURANDOMDEV
200Sstevel@tonic-gate #   include <openssl/rand.h>
210Sstevel@tonic-gate #  endif /* ! HASURANDOMDEV */
220Sstevel@tonic-gate # if !TLS_NO_RSA
230Sstevel@tonic-gate static RSA *rsa_tmp = NULL;	/* temporary RSA key */
240Sstevel@tonic-gate static RSA *tmp_rsa_key __P((SSL *, int, int));
250Sstevel@tonic-gate # endif /* !TLS_NO_RSA */
260Sstevel@tonic-gate #  if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L
270Sstevel@tonic-gate static int	tls_verify_cb __P((X509_STORE_CTX *));
280Sstevel@tonic-gate #  else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
290Sstevel@tonic-gate static int	tls_verify_cb __P((X509_STORE_CTX *, void *));
300Sstevel@tonic-gate #  endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate # if OPENSSL_VERSION_NUMBER > 0x00907000L
330Sstevel@tonic-gate static int x509_verify_cb __P((int, X509_STORE_CTX *));
340Sstevel@tonic-gate # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L
370Sstevel@tonic-gate #  define CONST097
380Sstevel@tonic-gate # else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
390Sstevel@tonic-gate #  define CONST097 const
400Sstevel@tonic-gate # endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
410Sstevel@tonic-gate static void	apps_ssl_info_cb __P((CONST097 SSL *, int , int));
420Sstevel@tonic-gate static bool	tls_ok_f __P((char *, char *, int));
430Sstevel@tonic-gate static bool	tls_safe_f __P((char *, long, bool));
440Sstevel@tonic-gate static int	tls_verify_log __P((int, X509_STORE_CTX *, char *));
450Sstevel@tonic-gate 
460Sstevel@tonic-gate # if !NO_DH
470Sstevel@tonic-gate static DH *get_dh512 __P((void));
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static unsigned char dh512_p[] =
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
520Sstevel@tonic-gate 	0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
530Sstevel@tonic-gate 	0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
540Sstevel@tonic-gate 	0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
550Sstevel@tonic-gate 	0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
560Sstevel@tonic-gate 	0x47,0x74,0xE8,0x33
570Sstevel@tonic-gate };
580Sstevel@tonic-gate static unsigned char dh512_g[] =
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	0x02
610Sstevel@tonic-gate };
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static DH *
get_dh512()640Sstevel@tonic-gate get_dh512()
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	DH *dh = NULL;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if ((dh = DH_new()) == NULL)
690Sstevel@tonic-gate 		return NULL;
700Sstevel@tonic-gate 	dh->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL);
710Sstevel@tonic-gate 	dh->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL);
720Sstevel@tonic-gate 	if ((dh->p == NULL) || (dh->g == NULL))
730Sstevel@tonic-gate 		return NULL;
740Sstevel@tonic-gate 	return dh;
750Sstevel@tonic-gate }
760Sstevel@tonic-gate # endif /* !NO_DH */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate **  TLS_RAND_INIT -- initialize STARTTLS random generator
810Sstevel@tonic-gate **
820Sstevel@tonic-gate **	Parameters:
830Sstevel@tonic-gate **		randfile -- name of file with random data
840Sstevel@tonic-gate **		logl -- loglevel
850Sstevel@tonic-gate **
860Sstevel@tonic-gate **	Returns:
870Sstevel@tonic-gate **		success/failure
880Sstevel@tonic-gate **
890Sstevel@tonic-gate **	Side Effects:
900Sstevel@tonic-gate **		initializes PRNG for tls library.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate # define MIN_RAND_BYTES	128	/* 1024 bits */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate # define RF_OK		0	/* randfile OK */
960Sstevel@tonic-gate # define RF_MISS	1	/* randfile == NULL || *randfile == '\0' */
970Sstevel@tonic-gate # define RF_UNKNOWN	2	/* unknown prefix for randfile */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate # define RI_NONE	0	/* no init yet */
1000Sstevel@tonic-gate # define RI_SUCCESS	1	/* init was successful */
1010Sstevel@tonic-gate # define RI_FAIL	2	/* init failed */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static bool	tls_rand_init __P((char *, int));
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static bool
tls_rand_init(randfile,logl)1060Sstevel@tonic-gate tls_rand_init(randfile, logl)
1070Sstevel@tonic-gate 	char *randfile;
1080Sstevel@tonic-gate 	int logl;
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate # ifndef HASURANDOMDEV
1110Sstevel@tonic-gate 	/* not required if /dev/urandom exists, OpenSSL does it internally */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	bool ok;
1140Sstevel@tonic-gate 	int randdef;
1150Sstevel@tonic-gate 	static int done = RI_NONE;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	**  initialize PRNG
1190Sstevel@tonic-gate 	*/
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	/* did we try this before? if yes: return old value */
1220Sstevel@tonic-gate 	if (done != RI_NONE)
1230Sstevel@tonic-gate 		return done == RI_SUCCESS;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	/* set default values */
1260Sstevel@tonic-gate 	ok = false;
1270Sstevel@tonic-gate 	done = RI_FAIL;
1280Sstevel@tonic-gate 	randdef = (randfile == NULL || *randfile == '\0') ? RF_MISS : RF_OK;
1290Sstevel@tonic-gate #   if EGD
1300Sstevel@tonic-gate 	if (randdef == RF_OK && sm_strncasecmp(randfile, "egd:", 4) == 0)
1310Sstevel@tonic-gate 	{
1320Sstevel@tonic-gate 		randfile += 4;
1330Sstevel@tonic-gate 		if (RAND_egd(randfile) < 0)
1340Sstevel@tonic-gate 		{
1350Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
1360Sstevel@tonic-gate 				  "STARTTLS: RAND_egd(%s) failed: random number generator not seeded",
1370Sstevel@tonic-gate 				   randfile);
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 		else
1400Sstevel@tonic-gate 			ok = true;
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 	else
1430Sstevel@tonic-gate #   endif /* EGD */
1440Sstevel@tonic-gate 	if (randdef == RF_OK && sm_strncasecmp(randfile, "file:", 5) == 0)
1450Sstevel@tonic-gate 	{
1460Sstevel@tonic-gate 		int fd;
1470Sstevel@tonic-gate 		long sff;
1480Sstevel@tonic-gate 		struct stat st;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 		randfile += 5;
1510Sstevel@tonic-gate 		sff = SFF_SAFEDIRPATH | SFF_NOWLINK
1520Sstevel@tonic-gate 		      | SFF_NOGWFILES | SFF_NOWWFILES
1530Sstevel@tonic-gate 		      | SFF_NOGRFILES | SFF_NOWRFILES
1540Sstevel@tonic-gate 		      | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT;
1550Sstevel@tonic-gate 		if (DontLockReadFiles)
1560Sstevel@tonic-gate 			sff |= SFF_NOLOCK;
1570Sstevel@tonic-gate 		if ((fd = safeopen(randfile, O_RDONLY, 0, sff)) >= 0)
1580Sstevel@tonic-gate 		{
1590Sstevel@tonic-gate 			if (fstat(fd, &st) < 0)
1600Sstevel@tonic-gate 			{
1610Sstevel@tonic-gate 				if (LogLevel > logl)
1620Sstevel@tonic-gate 					sm_syslog(LOG_ERR, NOQID,
1630Sstevel@tonic-gate 						  "STARTTLS: can't fstat(%s)",
1640Sstevel@tonic-gate 						  randfile);
1650Sstevel@tonic-gate 			}
1660Sstevel@tonic-gate 			else
1670Sstevel@tonic-gate 			{
1680Sstevel@tonic-gate 				bool use, problem;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 				use = true;
1710Sstevel@tonic-gate 				problem = false;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 				/* max. age of file: 10 minutes */
1740Sstevel@tonic-gate 				if (st.st_mtime + 600 < curtime())
1750Sstevel@tonic-gate 				{
1760Sstevel@tonic-gate 					use = bitnset(DBS_INSUFFICIENTENTROPY,
1770Sstevel@tonic-gate 						      DontBlameSendmail);
1780Sstevel@tonic-gate 					problem = true;
1790Sstevel@tonic-gate 					if (LogLevel > logl)
1800Sstevel@tonic-gate 						sm_syslog(LOG_ERR, NOQID,
1810Sstevel@tonic-gate 							  "STARTTLS: RandFile %s too old: %s",
1820Sstevel@tonic-gate 							  randfile,
1830Sstevel@tonic-gate 							  use ? "unsafe" :
1840Sstevel@tonic-gate 								"unusable");
1850Sstevel@tonic-gate 				}
1860Sstevel@tonic-gate 				if (use && st.st_size < MIN_RAND_BYTES)
1870Sstevel@tonic-gate 				{
1880Sstevel@tonic-gate 					use = bitnset(DBS_INSUFFICIENTENTROPY,
1890Sstevel@tonic-gate 						      DontBlameSendmail);
1900Sstevel@tonic-gate 					problem = true;
1910Sstevel@tonic-gate 					if (LogLevel > logl)
1920Sstevel@tonic-gate 						sm_syslog(LOG_ERR, NOQID,
1930Sstevel@tonic-gate 							  "STARTTLS: size(%s) < %d: %s",
1940Sstevel@tonic-gate 							  randfile,
1950Sstevel@tonic-gate 							  MIN_RAND_BYTES,
1960Sstevel@tonic-gate 							  use ? "unsafe" :
1970Sstevel@tonic-gate 								"unusable");
1980Sstevel@tonic-gate 				}
1990Sstevel@tonic-gate 				if (use)
2000Sstevel@tonic-gate 					ok = RAND_load_file(randfile, -1) >=
2010Sstevel@tonic-gate 					     MIN_RAND_BYTES;
2020Sstevel@tonic-gate 				if (use && !ok)
2030Sstevel@tonic-gate 				{
2040Sstevel@tonic-gate 					if (LogLevel > logl)
2050Sstevel@tonic-gate 						sm_syslog(LOG_WARNING, NOQID,
2060Sstevel@tonic-gate 							  "STARTTLS: RAND_load_file(%s) failed: random number generator not seeded",
2070Sstevel@tonic-gate 							  randfile);
2080Sstevel@tonic-gate 				}
2090Sstevel@tonic-gate 				if (problem)
2100Sstevel@tonic-gate 					ok = false;
2110Sstevel@tonic-gate 			}
2120Sstevel@tonic-gate 			if (ok || bitnset(DBS_INSUFFICIENTENTROPY,
2130Sstevel@tonic-gate 					  DontBlameSendmail))
2140Sstevel@tonic-gate 			{
2150Sstevel@tonic-gate 				/* add this even if fstat() failed */
2163544Sjbeck 				RAND_seed((void *) &st, sizeof(st));
2170Sstevel@tonic-gate 			}
2180Sstevel@tonic-gate 			(void) close(fd);
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 		else
2210Sstevel@tonic-gate 		{
2220Sstevel@tonic-gate 			if (LogLevel > logl)
2230Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
2240Sstevel@tonic-gate 					  "STARTTLS: Warning: safeopen(%s) failed",
2250Sstevel@tonic-gate 					  randfile);
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 	else if (randdef == RF_OK)
2290Sstevel@tonic-gate 	{
2300Sstevel@tonic-gate 		if (LogLevel > logl)
2310Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
2320Sstevel@tonic-gate 				  "STARTTLS: Error: no proper random file definition %s",
2330Sstevel@tonic-gate 				  randfile);
2340Sstevel@tonic-gate 		randdef = RF_UNKNOWN;
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 	if (randdef == RF_MISS)
2370Sstevel@tonic-gate 	{
2380Sstevel@tonic-gate 		if (LogLevel > logl)
2390Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
2400Sstevel@tonic-gate 				  "STARTTLS: Error: missing random file definition");
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 	if (!ok && bitnset(DBS_INSUFFICIENTENTROPY, DontBlameSendmail))
2430Sstevel@tonic-gate 	{
2440Sstevel@tonic-gate 		int i;
2450Sstevel@tonic-gate 		long r;
2460Sstevel@tonic-gate 		unsigned char buf[MIN_RAND_BYTES];
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		/* assert((MIN_RAND_BYTES % sizeof(long)) == 0); */
2490Sstevel@tonic-gate 		for (i = 0; i <= sizeof(buf) - sizeof(long); i += sizeof(long))
2500Sstevel@tonic-gate 		{
2510Sstevel@tonic-gate 			r = get_random();
2520Sstevel@tonic-gate 			(void) memcpy(buf + i, (void *) &r, sizeof(long));
2530Sstevel@tonic-gate 		}
2543544Sjbeck 		RAND_seed(buf, sizeof(buf));
2550Sstevel@tonic-gate 		if (LogLevel > logl)
2560Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
2570Sstevel@tonic-gate 				  "STARTTLS: Warning: random number generator not properly seeded");
2580Sstevel@tonic-gate 		ok = true;
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 	done = ok ? RI_SUCCESS : RI_FAIL;
2610Sstevel@tonic-gate 	return ok;
2620Sstevel@tonic-gate # else /* ! HASURANDOMDEV */
2630Sstevel@tonic-gate 	return true;
2640Sstevel@tonic-gate # endif /* ! HASURANDOMDEV */
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate **  INIT_TLS_LIBRARY -- Calls functions which setup TLS library for global use.
2680Sstevel@tonic-gate **
2690Sstevel@tonic-gate **	Parameters:
2700Sstevel@tonic-gate **		none.
2710Sstevel@tonic-gate **
2720Sstevel@tonic-gate **	Returns:
2730Sstevel@tonic-gate **		succeeded?
2740Sstevel@tonic-gate */
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate bool
init_tls_library()2770Sstevel@tonic-gate init_tls_library()
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	/* basic TLS initialization, ignore result for now */
2800Sstevel@tonic-gate 	SSL_library_init();
2810Sstevel@tonic-gate 	SSL_load_error_strings();
2820Sstevel@tonic-gate # if 0
2830Sstevel@tonic-gate 	/* this is currently a macro for SSL_library_init */
2840Sstevel@tonic-gate 	SSLeay_add_ssl_algorithms();
2850Sstevel@tonic-gate # endif /* 0 */
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	return tls_rand_init(RandFile, 7);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate **  TLS_SET_VERIFY -- request client certificate?
2910Sstevel@tonic-gate **
2920Sstevel@tonic-gate **	Parameters:
2930Sstevel@tonic-gate **		ctx -- TLS context
2940Sstevel@tonic-gate **		ssl -- TLS structure
2950Sstevel@tonic-gate **		vrfy -- require certificate?
2960Sstevel@tonic-gate **
2970Sstevel@tonic-gate **	Returns:
2980Sstevel@tonic-gate **		none.
2990Sstevel@tonic-gate **
3000Sstevel@tonic-gate **	Side Effects:
3010Sstevel@tonic-gate **		Sets verification state for TLS
3020Sstevel@tonic-gate **
3030Sstevel@tonic-gate # if TLS_VRFY_PER_CTX
3040Sstevel@tonic-gate **	Notice:
3050Sstevel@tonic-gate **		This is per TLS context, not per TLS structure;
3060Sstevel@tonic-gate **		the former is global, the latter per connection.
3070Sstevel@tonic-gate **		It would be nice to do this per connection, but this
3080Sstevel@tonic-gate **		doesn't work in the current TLS libraries :-(
3090Sstevel@tonic-gate # endif * TLS_VRFY_PER_CTX *
3100Sstevel@tonic-gate */
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate void
tls_set_verify(ctx,ssl,vrfy)3130Sstevel@tonic-gate tls_set_verify(ctx, ssl, vrfy)
3140Sstevel@tonic-gate 	SSL_CTX *ctx;
3150Sstevel@tonic-gate 	SSL *ssl;
3160Sstevel@tonic-gate 	bool vrfy;
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate # if !TLS_VRFY_PER_CTX
3190Sstevel@tonic-gate 	SSL_set_verify(ssl, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL);
3200Sstevel@tonic-gate # else /* !TLS_VRFY_PER_CTX */
3210Sstevel@tonic-gate 	SSL_CTX_set_verify(ctx, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE,
3220Sstevel@tonic-gate 			NULL);
3230Sstevel@tonic-gate # endif /* !TLS_VRFY_PER_CTX */
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate **  status in initialization
3280Sstevel@tonic-gate **  these flags keep track of the status of the initialization
3290Sstevel@tonic-gate **  i.e., whether a file exists (_EX) and whether it can be used (_OK)
3300Sstevel@tonic-gate **  [due to permissions]
3310Sstevel@tonic-gate */
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate # define TLS_S_NONE	0x00000000	/* none yet */
3340Sstevel@tonic-gate # define TLS_S_CERT_EX	0x00000001	/* cert file exists */
3350Sstevel@tonic-gate # define TLS_S_CERT_OK	0x00000002	/* cert file is ok */
3360Sstevel@tonic-gate # define TLS_S_KEY_EX	0x00000004	/* key file exists */
3370Sstevel@tonic-gate # define TLS_S_KEY_OK	0x00000008	/* key file is ok */
3380Sstevel@tonic-gate # define TLS_S_CERTP_EX	0x00000010	/* CA cert path exists */
3390Sstevel@tonic-gate # define TLS_S_CERTP_OK	0x00000020	/* CA cert path is ok */
3400Sstevel@tonic-gate # define TLS_S_CERTF_EX	0x00000040	/* CA cert file exists */
3410Sstevel@tonic-gate # define TLS_S_CERTF_OK	0x00000080	/* CA cert file is ok */
3420Sstevel@tonic-gate # define TLS_S_CRLF_EX	0x00000100	/* CRL file exists */
3430Sstevel@tonic-gate # define TLS_S_CRLF_OK	0x00000200	/* CRL file is ok */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate # if _FFR_TLS_1
3460Sstevel@tonic-gate #  define TLS_S_CERT2_EX	0x00001000	/* 2nd cert file exists */
3470Sstevel@tonic-gate #  define TLS_S_CERT2_OK	0x00002000	/* 2nd cert file is ok */
3480Sstevel@tonic-gate #  define TLS_S_KEY2_EX	0x00004000	/* 2nd key file exists */
3490Sstevel@tonic-gate #  define TLS_S_KEY2_OK	0x00008000	/* 2nd key file is ok */
3500Sstevel@tonic-gate # endif /* _FFR_TLS_1 */
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate # define TLS_S_DH_OK	0x00200000	/* DH cert is ok */
3530Sstevel@tonic-gate # define TLS_S_DHPAR_EX	0x00400000	/* DH param file exists */
3540Sstevel@tonic-gate # define TLS_S_DHPAR_OK	0x00800000	/* DH param file is ok to use */
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate /* Type of variable */
3570Sstevel@tonic-gate # define TLS_T_OTHER	0
3580Sstevel@tonic-gate # define TLS_T_SRV	1
3590Sstevel@tonic-gate # define TLS_T_CLT	2
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate **  TLS_OK_F -- can var be an absolute filename?
3630Sstevel@tonic-gate **
3640Sstevel@tonic-gate **	Parameters:
3650Sstevel@tonic-gate **		var -- filename
3660Sstevel@tonic-gate **		fn -- what is the filename used for?
3670Sstevel@tonic-gate **		type -- type of variable
3680Sstevel@tonic-gate **
3690Sstevel@tonic-gate **	Returns:
3700Sstevel@tonic-gate **		ok?
3710Sstevel@tonic-gate */
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate static bool
tls_ok_f(var,fn,type)3740Sstevel@tonic-gate tls_ok_f(var, fn, type)
3750Sstevel@tonic-gate 	char *var;
3760Sstevel@tonic-gate 	char *fn;
3770Sstevel@tonic-gate 	int type;
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	/* must be absolute pathname */
3800Sstevel@tonic-gate 	if (var != NULL && *var == '/')
3810Sstevel@tonic-gate 		return true;
3820Sstevel@tonic-gate 	if (LogLevel > 12)
3830Sstevel@tonic-gate 		sm_syslog(LOG_WARNING, NOQID, "STARTTLS: %s%s missing",
3840Sstevel@tonic-gate 			  type == TLS_T_SRV ? "Server" :
3850Sstevel@tonic-gate 			  (type == TLS_T_CLT ? "Client" : ""), fn);
3860Sstevel@tonic-gate 	return false;
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate **  TLS_SAFE_F -- is a file safe to use?
3900Sstevel@tonic-gate **
3910Sstevel@tonic-gate **	Parameters:
3920Sstevel@tonic-gate **		var -- filename
3930Sstevel@tonic-gate **		sff -- flags for safefile()
3940Sstevel@tonic-gate **		srv -- server side?
3950Sstevel@tonic-gate **
3960Sstevel@tonic-gate **	Returns:
3970Sstevel@tonic-gate **		ok?
3980Sstevel@tonic-gate */
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate static bool
tls_safe_f(var,sff,srv)4010Sstevel@tonic-gate tls_safe_f(var, sff, srv)
4020Sstevel@tonic-gate 	char *var;
4030Sstevel@tonic-gate 	long sff;
4040Sstevel@tonic-gate 	bool srv;
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	int ret;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if ((ret = safefile(var, RunAsUid, RunAsGid, RunAsUserName, sff,
4090Sstevel@tonic-gate 			    S_IRUSR, NULL)) == 0)
4100Sstevel@tonic-gate 		return true;
4110Sstevel@tonic-gate 	if (LogLevel > 7)
4120Sstevel@tonic-gate 		sm_syslog(LOG_WARNING, NOQID, "STARTTLS=%s: file %s unsafe: %s",
4130Sstevel@tonic-gate 			  srv ? "server" : "client", var, sm_errstring(ret));
4140Sstevel@tonic-gate 	return false;
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate **  TLS_OK_F -- macro to simplify calls to tls_ok_f
4190Sstevel@tonic-gate **
4200Sstevel@tonic-gate **	Parameters:
4210Sstevel@tonic-gate **		var -- filename
4220Sstevel@tonic-gate **		fn -- what is the filename used for?
4230Sstevel@tonic-gate **		req -- is the file required?
4240Sstevel@tonic-gate **		st -- status bit to set if ok
4250Sstevel@tonic-gate **		type -- type of variable
4260Sstevel@tonic-gate **
4270Sstevel@tonic-gate **	Side Effects:
4280Sstevel@tonic-gate **		uses r, ok; may change ok and status.
4290Sstevel@tonic-gate **
4300Sstevel@tonic-gate */
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate # define TLS_OK_F(var, fn, req, st, type) if (ok) \
4330Sstevel@tonic-gate 	{ \
4340Sstevel@tonic-gate 		r = tls_ok_f(var, fn, type); \
4350Sstevel@tonic-gate 		if (r) \
4360Sstevel@tonic-gate 			status |= st; \
4370Sstevel@tonic-gate 		else if (req) \
4380Sstevel@tonic-gate 			ok = false; \
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate **  TLS_UNR -- macro to return whether a file should be unreadable
4430Sstevel@tonic-gate **
4440Sstevel@tonic-gate **	Parameters:
4450Sstevel@tonic-gate **		bit -- flag to test
4460Sstevel@tonic-gate **		req -- flags
4470Sstevel@tonic-gate **
4480Sstevel@tonic-gate **	Returns:
4490Sstevel@tonic-gate **		0/SFF_NORFILES
4500Sstevel@tonic-gate */
4510Sstevel@tonic-gate # define TLS_UNR(bit, req)	(bitset(bit, req) ? SFF_NORFILES : 0)
4520Sstevel@tonic-gate # define TLS_OUNR(bit, req)	(bitset(bit, req) ? SFF_NOWRFILES : 0)
4530Sstevel@tonic-gate # define TLS_KEYSFF(req)	\
4540Sstevel@tonic-gate 	(bitnset(DBS_GROUPREADABLEKEYFILE, DontBlameSendmail) ?	\
4550Sstevel@tonic-gate 		TLS_OUNR(TLS_I_KEY_OUNR, req) :			\
4560Sstevel@tonic-gate 		TLS_UNR(TLS_I_KEY_UNR, req))
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate /*
4590Sstevel@tonic-gate **  TLS_SAFE_F -- macro to simplify calls to tls_safe_f
4600Sstevel@tonic-gate **
4610Sstevel@tonic-gate **	Parameters:
4620Sstevel@tonic-gate **		var -- filename
4630Sstevel@tonic-gate **		sff -- flags for safefile()
4640Sstevel@tonic-gate **		req -- is the file required?
4650Sstevel@tonic-gate **		ex -- does the file exist?
4660Sstevel@tonic-gate **		st -- status bit to set if ok
4670Sstevel@tonic-gate **		srv -- server side?
4680Sstevel@tonic-gate **
4690Sstevel@tonic-gate **	Side Effects:
4700Sstevel@tonic-gate **		uses r, ok, ex; may change ok and status.
4710Sstevel@tonic-gate **
4720Sstevel@tonic-gate */
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate # define TLS_SAFE_F(var, sff, req, ex, st, srv) if (ex && ok) \
4750Sstevel@tonic-gate 	{ \
4760Sstevel@tonic-gate 		r = tls_safe_f(var, sff, srv); \
4770Sstevel@tonic-gate 		if (r) \
4780Sstevel@tonic-gate 			status |= st;	\
4790Sstevel@tonic-gate 		else if (req) \
4800Sstevel@tonic-gate 			ok = false;	\
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate /*
4840Sstevel@tonic-gate **  INITTLS -- initialize TLS
4850Sstevel@tonic-gate **
4860Sstevel@tonic-gate **	Parameters:
4870Sstevel@tonic-gate **		ctx -- pointer to context
4880Sstevel@tonic-gate **		req -- requirements for initialization (see sendmail.h)
489*11440SJohn.Beck@Sun.COM **		options -- options
4900Sstevel@tonic-gate **		srv -- server side?
4910Sstevel@tonic-gate **		certfile -- filename of certificate
4920Sstevel@tonic-gate **		keyfile -- filename of private key
4930Sstevel@tonic-gate **		cacertpath -- path to CAs
4940Sstevel@tonic-gate **		cacertfile -- file with CA(s)
4950Sstevel@tonic-gate **		dhparam -- parameters for DH
4960Sstevel@tonic-gate **
4970Sstevel@tonic-gate **	Returns:
4980Sstevel@tonic-gate **		succeeded?
4990Sstevel@tonic-gate */
5000Sstevel@tonic-gate 
5011658Sjbeck /*
5021658Sjbeck **  The session_id_context identifies the service that created a session.
5031658Sjbeck **  This information is used to distinguish between multiple TLS-based
5041658Sjbeck **  servers running on the same server. We use the name of the mail system.
5051658Sjbeck **  Note: the session cache is not persistent.
5061658Sjbeck */
5071658Sjbeck 
5081658Sjbeck static char server_session_id_context[] = "sendmail8";
5091658Sjbeck 
5102197Sjbeck /* 0.9.8a and b have a problem with SSL_OP_TLS_BLOCK_PADDING_BUG */
5112197Sjbeck #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
5122197Sjbeck # define SM_SSL_OP_TLS_BLOCK_PADDING_BUG	1
5132197Sjbeck #else
5142197Sjbeck # define SM_SSL_OP_TLS_BLOCK_PADDING_BUG	0
5152197Sjbeck #endif
5162197Sjbeck 
5170Sstevel@tonic-gate bool
inittls(ctx,req,options,srv,certfile,keyfile,cacertpath,cacertfile,dhparam)518*11440SJohn.Beck@Sun.COM inittls(ctx, req, options, srv, certfile, keyfile, cacertpath, cacertfile, dhparam)
5190Sstevel@tonic-gate 	SSL_CTX **ctx;
5200Sstevel@tonic-gate 	unsigned long req;
521*11440SJohn.Beck@Sun.COM 	long options;
5220Sstevel@tonic-gate 	bool srv;
5230Sstevel@tonic-gate 	char *certfile, *keyfile, *cacertpath, *cacertfile, *dhparam;
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate # if !NO_DH
5260Sstevel@tonic-gate 	static DH *dh = NULL;
5270Sstevel@tonic-gate # endif /* !NO_DH */
5280Sstevel@tonic-gate 	int r;
5290Sstevel@tonic-gate 	bool ok;
530*11440SJohn.Beck@Sun.COM 	long sff, status;
5310Sstevel@tonic-gate 	char *who;
5320Sstevel@tonic-gate # if _FFR_TLS_1
5330Sstevel@tonic-gate 	char *cf2, *kf2;
5340Sstevel@tonic-gate # endif /* _FFR_TLS_1 */
5350Sstevel@tonic-gate #  if SM_CONF_SHM
5360Sstevel@tonic-gate 	extern int ShmId;
5370Sstevel@tonic-gate #  endif /* SM_CONF_SHM */
5380Sstevel@tonic-gate # if OPENSSL_VERSION_NUMBER > 0x00907000L
5390Sstevel@tonic-gate 	BIO *crl_file;
5400Sstevel@tonic-gate 	X509_CRL *crl;
5410Sstevel@tonic-gate 	X509_STORE *store;
5420Sstevel@tonic-gate # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
5432197Sjbeck #if SM_SSL_OP_TLS_BLOCK_PADDING_BUG
5442197Sjbeck 	long rt_version;
5452197Sjbeck 	STACK_OF(SSL_COMP) *comp_methods;
5462197Sjbeck #endif
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	status = TLS_S_NONE;
5490Sstevel@tonic-gate 	who = srv ? "server" : "client";
5500Sstevel@tonic-gate 	if (ctx == NULL)
5512197Sjbeck 	{
5520Sstevel@tonic-gate 		syserr("STARTTLS=%s, inittls: ctx == NULL", who);
5532197Sjbeck 		/* NOTREACHED */
5542197Sjbeck 		SM_ASSERT(ctx != NULL);
5552197Sjbeck 	}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/* already initialized? (we could re-init...) */
5580Sstevel@tonic-gate 	if (*ctx != NULL)
5590Sstevel@tonic-gate 		return true;
5600Sstevel@tonic-gate 	ok = true;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate # if _FFR_TLS_1
5630Sstevel@tonic-gate 	/*
5640Sstevel@tonic-gate 	**  look for a second filename: it must be separated by a ','
5650Sstevel@tonic-gate 	**  no blanks allowed (they won't be skipped).
5660Sstevel@tonic-gate 	**  we change a global variable here! this change will be undone
5670Sstevel@tonic-gate 	**  before return from the function but only if it returns true.
5680Sstevel@tonic-gate 	**  this isn't a problem since in a failure case this function
5690Sstevel@tonic-gate 	**  won't be called again with the same (overwritten) values.
5700Sstevel@tonic-gate 	**  otherwise each return must be replaced with a goto endinittls.
5710Sstevel@tonic-gate 	*/
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	cf2 = NULL;
5740Sstevel@tonic-gate 	kf2 = NULL;
5750Sstevel@tonic-gate 	if (certfile != NULL && (cf2 = strchr(certfile, ',')) != NULL)
5760Sstevel@tonic-gate 	{
5770Sstevel@tonic-gate 		*cf2++ = '\0';
5780Sstevel@tonic-gate 		if (keyfile != NULL && (kf2 = strchr(keyfile, ',')) != NULL)
5790Sstevel@tonic-gate 			*kf2++ = '\0';
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate # endif /* _FFR_TLS_1 */
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/*
5840Sstevel@tonic-gate 	**  Check whether files/paths are defined
5850Sstevel@tonic-gate 	*/
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	TLS_OK_F(certfile, "CertFile", bitset(TLS_I_CERT_EX, req),
5880Sstevel@tonic-gate 		 TLS_S_CERT_EX, srv ? TLS_T_SRV : TLS_T_CLT);
5890Sstevel@tonic-gate 	TLS_OK_F(keyfile, "KeyFile", bitset(TLS_I_KEY_EX, req),
5900Sstevel@tonic-gate 		 TLS_S_KEY_EX, srv ? TLS_T_SRV : TLS_T_CLT);
5910Sstevel@tonic-gate 	TLS_OK_F(cacertpath, "CACertPath", bitset(TLS_I_CERTP_EX, req),
5920Sstevel@tonic-gate 		 TLS_S_CERTP_EX, TLS_T_OTHER);
5930Sstevel@tonic-gate 	TLS_OK_F(cacertfile, "CACertFile", bitset(TLS_I_CERTF_EX, req),
5940Sstevel@tonic-gate 		 TLS_S_CERTF_EX, TLS_T_OTHER);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate # if OPENSSL_VERSION_NUMBER > 0x00907000L
5970Sstevel@tonic-gate 	TLS_OK_F(CRLFile, "CRLFile", bitset(TLS_I_CRLF_EX, req),
5980Sstevel@tonic-gate 		 TLS_S_CRLF_EX, TLS_T_OTHER);
5990Sstevel@tonic-gate # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate # if _FFR_TLS_1
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	**  if the second file is specified it must exist
6040Sstevel@tonic-gate 	**  XXX: it is possible here to define only one of those files
6050Sstevel@tonic-gate 	*/
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (cf2 != NULL)
6080Sstevel@tonic-gate 	{
6090Sstevel@tonic-gate 		TLS_OK_F(cf2, "CertFile", bitset(TLS_I_CERT_EX, req),
6100Sstevel@tonic-gate 			 TLS_S_CERT2_EX, srv ? TLS_T_SRV : TLS_T_CLT);
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 	if (kf2 != NULL)
6130Sstevel@tonic-gate 	{
6140Sstevel@tonic-gate 		TLS_OK_F(kf2, "KeyFile", bitset(TLS_I_KEY_EX, req),
6150Sstevel@tonic-gate 			 TLS_S_KEY2_EX, srv ? TLS_T_SRV : TLS_T_CLT);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate # endif /* _FFR_TLS_1 */
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/*
6200Sstevel@tonic-gate 	**  valid values for dhparam are (only the first char is checked)
6210Sstevel@tonic-gate 	**  none	no parameters: don't use DH
6220Sstevel@tonic-gate 	**  512		generate 512 bit parameters (fixed)
6230Sstevel@tonic-gate 	**  1024	generate 1024 bit parameters
6240Sstevel@tonic-gate 	**  /file/name	read parameters from /file/name
6250Sstevel@tonic-gate 	**  default is: 1024 for server, 512 for client (OK? XXX)
6260Sstevel@tonic-gate 	*/
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (bitset(TLS_I_TRY_DH, req))
6290Sstevel@tonic-gate 	{
6300Sstevel@tonic-gate 		if (dhparam != NULL)
6310Sstevel@tonic-gate 		{
6320Sstevel@tonic-gate 			char c = *dhparam;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 			if (c == '1')
6350Sstevel@tonic-gate 				req |= TLS_I_DH1024;
6360Sstevel@tonic-gate 			else if (c == '5')
6370Sstevel@tonic-gate 				req |= TLS_I_DH512;
6380Sstevel@tonic-gate 			else if (c != 'n' && c != 'N' && c != '/')
6390Sstevel@tonic-gate 			{
6400Sstevel@tonic-gate 				if (LogLevel > 12)
6410Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, NOQID,
6420Sstevel@tonic-gate 						  "STARTTLS=%s, error: illegal value '%s' for DHParam",
6430Sstevel@tonic-gate 						  who, dhparam);
6440Sstevel@tonic-gate 				dhparam = NULL;
6450Sstevel@tonic-gate 			}
6460Sstevel@tonic-gate 		}
6470Sstevel@tonic-gate 		if (dhparam == NULL)
648*11440SJohn.Beck@Sun.COM 		{
6490Sstevel@tonic-gate 			dhparam = srv ? "1" : "5";
650*11440SJohn.Beck@Sun.COM 			req |= (srv ? TLS_I_DH1024 : TLS_I_DH512);
651*11440SJohn.Beck@Sun.COM 		}
6520Sstevel@tonic-gate 		else if (*dhparam == '/')
6530Sstevel@tonic-gate 		{
6540Sstevel@tonic-gate 			TLS_OK_F(dhparam, "DHParameters",
6550Sstevel@tonic-gate 				 bitset(TLS_I_DHPAR_EX, req),
6560Sstevel@tonic-gate 				 TLS_S_DHPAR_EX, TLS_T_OTHER);
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 	if (!ok)
6600Sstevel@tonic-gate 		return ok;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	/* certfile etc. must be "safe". */
6630Sstevel@tonic-gate 	sff = SFF_REGONLY | SFF_SAFEDIRPATH | SFF_NOWLINK
6640Sstevel@tonic-gate 	     | SFF_NOGWFILES | SFF_NOWWFILES
6650Sstevel@tonic-gate 	     | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT;
6660Sstevel@tonic-gate 	if (DontLockReadFiles)
6670Sstevel@tonic-gate 		sff |= SFF_NOLOCK;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	TLS_SAFE_F(certfile, sff | TLS_UNR(TLS_I_CERT_UNR, req),
6700Sstevel@tonic-gate 		   bitset(TLS_I_CERT_EX, req),
6710Sstevel@tonic-gate 		   bitset(TLS_S_CERT_EX, status), TLS_S_CERT_OK, srv);
6720Sstevel@tonic-gate 	TLS_SAFE_F(keyfile, sff | TLS_KEYSFF(req),
6730Sstevel@tonic-gate 		   bitset(TLS_I_KEY_EX, req),
6740Sstevel@tonic-gate 		   bitset(TLS_S_KEY_EX, status), TLS_S_KEY_OK, srv);
6750Sstevel@tonic-gate 	TLS_SAFE_F(cacertfile, sff | TLS_UNR(TLS_I_CERTF_UNR, req),
6760Sstevel@tonic-gate 		   bitset(TLS_I_CERTF_EX, req),
6770Sstevel@tonic-gate 		   bitset(TLS_S_CERTF_EX, status), TLS_S_CERTF_OK, srv);
6780Sstevel@tonic-gate 	TLS_SAFE_F(dhparam, sff | TLS_UNR(TLS_I_DHPAR_UNR, req),
6790Sstevel@tonic-gate 		   bitset(TLS_I_DHPAR_EX, req),
6800Sstevel@tonic-gate 		   bitset(TLS_S_DHPAR_EX, status), TLS_S_DHPAR_OK, srv);
6810Sstevel@tonic-gate # if OPENSSL_VERSION_NUMBER > 0x00907000L
6820Sstevel@tonic-gate 	TLS_SAFE_F(CRLFile, sff | TLS_UNR(TLS_I_CRLF_UNR, req),
6830Sstevel@tonic-gate 		   bitset(TLS_I_CRLF_EX, req),
6840Sstevel@tonic-gate 		   bitset(TLS_S_CRLF_EX, status), TLS_S_CRLF_OK, srv);
6850Sstevel@tonic-gate # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
6860Sstevel@tonic-gate 	if (!ok)
6870Sstevel@tonic-gate 		return ok;
6880Sstevel@tonic-gate # if _FFR_TLS_1
6890Sstevel@tonic-gate 	if (cf2 != NULL)
6900Sstevel@tonic-gate 	{
6910Sstevel@tonic-gate 		TLS_SAFE_F(cf2, sff | TLS_UNR(TLS_I_CERT_UNR, req),
6920Sstevel@tonic-gate 			   bitset(TLS_I_CERT_EX, req),
6930Sstevel@tonic-gate 			   bitset(TLS_S_CERT2_EX, status), TLS_S_CERT2_OK, srv);
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 	if (kf2 != NULL)
6960Sstevel@tonic-gate 	{
6970Sstevel@tonic-gate 		TLS_SAFE_F(kf2, sff | TLS_KEYSFF(req),
6980Sstevel@tonic-gate 			   bitset(TLS_I_KEY_EX, req),
6990Sstevel@tonic-gate 			   bitset(TLS_S_KEY2_EX, status), TLS_S_KEY2_OK, srv);
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate # endif /* _FFR_TLS_1 */
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/* create a method and a new context */
7040Sstevel@tonic-gate 	if ((*ctx = SSL_CTX_new(srv ? SSLv23_server_method() :
7050Sstevel@tonic-gate 				      SSLv23_client_method())) == NULL)
7060Sstevel@tonic-gate 	{
7070Sstevel@tonic-gate 		if (LogLevel > 7)
7080Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
7090Sstevel@tonic-gate 				  "STARTTLS=%s, error: SSL_CTX_new(SSLv23_%s_method()) failed",
7100Sstevel@tonic-gate 				  who, who);
7110Sstevel@tonic-gate 		if (LogLevel > 9)
7120Sstevel@tonic-gate 			tlslogerr(who);
7130Sstevel@tonic-gate 		return false;
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate # if OPENSSL_VERSION_NUMBER > 0x00907000L
7170Sstevel@tonic-gate 	if (CRLFile != NULL)
7180Sstevel@tonic-gate 	{
7190Sstevel@tonic-gate 		/* get a pointer to the current certificate validation store */
7200Sstevel@tonic-gate 		store = SSL_CTX_get_cert_store(*ctx);	/* does not fail */
7210Sstevel@tonic-gate 		crl_file = BIO_new(BIO_s_file_internal());
7220Sstevel@tonic-gate 		if (crl_file != NULL)
7230Sstevel@tonic-gate 		{
7240Sstevel@tonic-gate 			if (BIO_read_filename(crl_file, CRLFile) >= 0)
7250Sstevel@tonic-gate 			{
7260Sstevel@tonic-gate 				crl = PEM_read_bio_X509_CRL(crl_file, NULL,
7270Sstevel@tonic-gate 							NULL, NULL);
7280Sstevel@tonic-gate 				BIO_free(crl_file);
7290Sstevel@tonic-gate 				X509_STORE_add_crl(store, crl);
7300Sstevel@tonic-gate 				X509_CRL_free(crl);
7310Sstevel@tonic-gate 				X509_STORE_set_flags(store,
7320Sstevel@tonic-gate 					X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
7330Sstevel@tonic-gate 				X509_STORE_set_verify_cb_func(store,
7340Sstevel@tonic-gate 						x509_verify_cb);
7350Sstevel@tonic-gate 			}
7360Sstevel@tonic-gate 			else
7370Sstevel@tonic-gate 			{
7380Sstevel@tonic-gate 				if (LogLevel > 9)
7390Sstevel@tonic-gate 				{
7400Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, NOQID,
7410Sstevel@tonic-gate 						  "STARTTLS=%s, error: PEM_read_bio_X509_CRL(%s)=failed",
7420Sstevel@tonic-gate 						  who, CRLFile);
7430Sstevel@tonic-gate 				}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 				/* avoid memory leaks */
7460Sstevel@tonic-gate 				BIO_free(crl_file);
7470Sstevel@tonic-gate 				return false;
7480Sstevel@tonic-gate 			}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 		}
7510Sstevel@tonic-gate 		else if (LogLevel > 9)
7520Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
7530Sstevel@tonic-gate 				  "STARTTLS=%s, error: BIO_new=failed", who);
7540Sstevel@tonic-gate 	}
7553544Sjbeck 	else
7563544Sjbeck 		store = NULL;
7570Sstevel@tonic-gate #  if _FFR_CRLPATH
7583544Sjbeck 	if (CRLPath != NULL && store != NULL)
7590Sstevel@tonic-gate 	{
7600Sstevel@tonic-gate 		X509_LOOKUP *lookup;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 		lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
7630Sstevel@tonic-gate 		if (lookup == NULL)
7640Sstevel@tonic-gate 		{
7650Sstevel@tonic-gate 			if (LogLevel > 9)
7660Sstevel@tonic-gate 			{
7670Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
7680Sstevel@tonic-gate 					  "STARTTLS=%s, error: X509_STORE_add_lookup(hash)=failed",
7690Sstevel@tonic-gate 					  who, CRLFile);
7700Sstevel@tonic-gate 			}
7710Sstevel@tonic-gate 			return false;
7720Sstevel@tonic-gate 		}
7730Sstevel@tonic-gate 		X509_LOOKUP_add_dir(lookup, CRLPath, X509_FILETYPE_PEM);
7740Sstevel@tonic-gate 		X509_STORE_set_flags(store,
7750Sstevel@tonic-gate 			X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate #  endif /* _FFR_CRLPATH */
7780Sstevel@tonic-gate # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate # if TLS_NO_RSA
7810Sstevel@tonic-gate 	/* turn off backward compatibility, required for no-rsa */
7820Sstevel@tonic-gate 	SSL_CTX_set_options(*ctx, SSL_OP_NO_SSLv2);
7830Sstevel@tonic-gate # endif /* TLS_NO_RSA */
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate # if !TLS_NO_RSA
7870Sstevel@tonic-gate 	/*
7880Sstevel@tonic-gate 	**  Create a temporary RSA key
7890Sstevel@tonic-gate 	**  XXX  Maybe we shouldn't create this always (even though it
7900Sstevel@tonic-gate 	**  is only at startup).
7910Sstevel@tonic-gate 	**  It is a time-consuming operation and it is not always necessary.
7920Sstevel@tonic-gate 	**  maybe we should do it only on demand...
7930Sstevel@tonic-gate 	*/
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (bitset(TLS_I_RSA_TMP, req)
7960Sstevel@tonic-gate #   if SM_CONF_SHM
7970Sstevel@tonic-gate 	    && ShmId != SM_SHM_NO_ID &&
7980Sstevel@tonic-gate 	    (rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL,
7990Sstevel@tonic-gate 					NULL)) == NULL
8000Sstevel@tonic-gate #   else /* SM_CONF_SHM */
8010Sstevel@tonic-gate 	    && 0	/* no shared memory: no need to generate key now */
8020Sstevel@tonic-gate #   endif /* SM_CONF_SHM */
8030Sstevel@tonic-gate 	   )
8040Sstevel@tonic-gate 	{
8050Sstevel@tonic-gate 		if (LogLevel > 7)
8060Sstevel@tonic-gate 		{
8070Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
8080Sstevel@tonic-gate 				  "STARTTLS=%s, error: RSA_generate_key failed",
8090Sstevel@tonic-gate 				  who);
8100Sstevel@tonic-gate 			if (LogLevel > 9)
8110Sstevel@tonic-gate 				tlslogerr(who);
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 		return false;
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate # endif /* !TLS_NO_RSA */
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	**  load private key
8190Sstevel@tonic-gate 	**  XXX change this for DSA-only version
8200Sstevel@tonic-gate 	*/
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (bitset(TLS_S_KEY_OK, status) &&
8230Sstevel@tonic-gate 	    SSL_CTX_use_PrivateKey_file(*ctx, keyfile,
8240Sstevel@tonic-gate 					 SSL_FILETYPE_PEM) <= 0)
8250Sstevel@tonic-gate 	{
8260Sstevel@tonic-gate 		if (LogLevel > 7)
8270Sstevel@tonic-gate 		{
8280Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
8290Sstevel@tonic-gate 				  "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed",
8300Sstevel@tonic-gate 				  who, keyfile);
8310Sstevel@tonic-gate 			if (LogLevel > 9)
8320Sstevel@tonic-gate 				tlslogerr(who);
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate 		if (bitset(TLS_I_USE_KEY, req))
8350Sstevel@tonic-gate 			return false;
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	/* get the certificate file */
8390Sstevel@tonic-gate 	if (bitset(TLS_S_CERT_OK, status) &&
8400Sstevel@tonic-gate 	    SSL_CTX_use_certificate_file(*ctx, certfile,
8410Sstevel@tonic-gate 					 SSL_FILETYPE_PEM) <= 0)
8420Sstevel@tonic-gate 	{
8430Sstevel@tonic-gate 		if (LogLevel > 7)
8440Sstevel@tonic-gate 		{
8450Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
8460Sstevel@tonic-gate 				  "STARTTLS=%s, error: SSL_CTX_use_certificate_file(%s) failed",
8470Sstevel@tonic-gate 				  who, certfile);
8480Sstevel@tonic-gate 			if (LogLevel > 9)
8490Sstevel@tonic-gate 				tlslogerr(who);
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 		if (bitset(TLS_I_USE_CERT, req))
8520Sstevel@tonic-gate 			return false;
8530Sstevel@tonic-gate 	}
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	/* check the private key */
8560Sstevel@tonic-gate 	if (bitset(TLS_S_KEY_OK, status) &&
8570Sstevel@tonic-gate 	    (r = SSL_CTX_check_private_key(*ctx)) <= 0)
8580Sstevel@tonic-gate 	{
8590Sstevel@tonic-gate 		/* Private key does not match the certificate public key */
8600Sstevel@tonic-gate 		if (LogLevel > 5)
8610Sstevel@tonic-gate 		{
8620Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
8630Sstevel@tonic-gate 				  "STARTTLS=%s, error: SSL_CTX_check_private_key failed(%s): %d",
8640Sstevel@tonic-gate 				  who, keyfile, r);
8650Sstevel@tonic-gate 			if (LogLevel > 9)
8660Sstevel@tonic-gate 				tlslogerr(who);
8670Sstevel@tonic-gate 		}
8680Sstevel@tonic-gate 		if (bitset(TLS_I_USE_KEY, req))
8690Sstevel@tonic-gate 			return false;
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate # if _FFR_TLS_1
8730Sstevel@tonic-gate 	/* XXX this code is pretty much duplicated from above! */
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/* load private key */
8760Sstevel@tonic-gate 	if (bitset(TLS_S_KEY2_OK, status) &&
8770Sstevel@tonic-gate 	    SSL_CTX_use_PrivateKey_file(*ctx, kf2, SSL_FILETYPE_PEM) <= 0)
8780Sstevel@tonic-gate 	{
8790Sstevel@tonic-gate 		if (LogLevel > 7)
8800Sstevel@tonic-gate 		{
8810Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
8820Sstevel@tonic-gate 				  "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed",
8830Sstevel@tonic-gate 				  who, kf2);
8840Sstevel@tonic-gate 			if (LogLevel > 9)
8850Sstevel@tonic-gate 				tlslogerr(who);
8860Sstevel@tonic-gate 		}
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	/* get the certificate file */
8900Sstevel@tonic-gate 	if (bitset(TLS_S_CERT2_OK, status) &&
8910Sstevel@tonic-gate 	    SSL_CTX_use_certificate_file(*ctx, cf2, SSL_FILETYPE_PEM) <= 0)
8920Sstevel@tonic-gate 	{
8930Sstevel@tonic-gate 		if (LogLevel > 7)
8940Sstevel@tonic-gate 		{
8950Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
8960Sstevel@tonic-gate 				  "STARTTLS=%s, error: SSL_CTX_use_certificate_file(%s) failed",
8970Sstevel@tonic-gate 				  who, cf2);
8980Sstevel@tonic-gate 			if (LogLevel > 9)
8990Sstevel@tonic-gate 				tlslogerr(who);
9000Sstevel@tonic-gate 		}
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	/* also check the private key */
9040Sstevel@tonic-gate 	if (bitset(TLS_S_KEY2_OK, status) &&
9050Sstevel@tonic-gate 	    (r = SSL_CTX_check_private_key(*ctx)) <= 0)
9060Sstevel@tonic-gate 	{
9070Sstevel@tonic-gate 		/* Private key does not match the certificate public key */
9080Sstevel@tonic-gate 		if (LogLevel > 5)
9090Sstevel@tonic-gate 		{
9100Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, NOQID,
9110Sstevel@tonic-gate 				  "STARTTLS=%s, error: SSL_CTX_check_private_key 2 failed: %d",
9120Sstevel@tonic-gate 				  who, r);
9130Sstevel@tonic-gate 			if (LogLevel > 9)
9140Sstevel@tonic-gate 				tlslogerr(who);
9150Sstevel@tonic-gate 		}
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate # endif /* _FFR_TLS_1 */
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/* SSL_CTX_set_quiet_shutdown(*ctx, 1); violation of standard? */
9202197Sjbeck 
9212197Sjbeck #if SM_SSL_OP_TLS_BLOCK_PADDING_BUG
9222197Sjbeck 
9232197Sjbeck 	/*
9242197Sjbeck 	**  In OpenSSL 0.9.8[ab], enabling zlib compression breaks the
9252197Sjbeck 	**  padding bug work-around, leading to false positives and
9262197Sjbeck 	**  failed connections. We may not interoperate with systems
9272197Sjbeck 	**  with the bug, but this is better than breaking on all 0.9.8[ab]
9282197Sjbeck 	**  systems that have zlib support enabled.
9292197Sjbeck 	**  Note: this checks the runtime version of the library, not
9302197Sjbeck 	**  just the compile time version.
9312197Sjbeck 	*/
9322197Sjbeck 
9332197Sjbeck 	rt_version = SSLeay();
9342197Sjbeck 	if (rt_version >= 0x00908000L && rt_version <= 0x0090802fL)
9352197Sjbeck 	{
9362197Sjbeck 		comp_methods = SSL_COMP_get_compression_methods();
9372197Sjbeck 		if (comp_methods != NULL && sk_SSL_COMP_num(comp_methods) > 0)
9382197Sjbeck 			options &= ~SSL_OP_TLS_BLOCK_PADDING_BUG;
9392197Sjbeck 	}
9402197Sjbeck #endif
9412197Sjbeck 	SSL_CTX_set_options(*ctx, options);
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate # if !NO_DH
9440Sstevel@tonic-gate 	/* Diffie-Hellman initialization */
9450Sstevel@tonic-gate 	if (bitset(TLS_I_TRY_DH, req))
9460Sstevel@tonic-gate 	{
9470Sstevel@tonic-gate 		if (bitset(TLS_S_DHPAR_OK, status))
9480Sstevel@tonic-gate 		{
9490Sstevel@tonic-gate 			BIO *bio;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 			if ((bio = BIO_new_file(dhparam, "r")) != NULL)
9520Sstevel@tonic-gate 			{
9530Sstevel@tonic-gate 				dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
9540Sstevel@tonic-gate 				BIO_free(bio);
9550Sstevel@tonic-gate 				if (dh == NULL && LogLevel > 7)
9560Sstevel@tonic-gate 				{
9570Sstevel@tonic-gate 					unsigned long err;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 					err = ERR_get_error();
9600Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, NOQID,
9610Sstevel@tonic-gate 						  "STARTTLS=%s, error: cannot read DH parameters(%s): %s",
9620Sstevel@tonic-gate 						  who, dhparam,
9630Sstevel@tonic-gate 						  ERR_error_string(err, NULL));
9640Sstevel@tonic-gate 					if (LogLevel > 9)
9650Sstevel@tonic-gate 						tlslogerr(who);
9660Sstevel@tonic-gate 				}
9670Sstevel@tonic-gate 			}
9680Sstevel@tonic-gate 			else
9690Sstevel@tonic-gate 			{
9700Sstevel@tonic-gate 				if (LogLevel > 5)
9710Sstevel@tonic-gate 				{
9720Sstevel@tonic-gate 					sm_syslog(LOG_WARNING, NOQID,
9730Sstevel@tonic-gate 						  "STARTTLS=%s, error: BIO_new_file(%s) failed",
9740Sstevel@tonic-gate 						  who, dhparam);
9750Sstevel@tonic-gate 					if (LogLevel > 9)
9760Sstevel@tonic-gate 						tlslogerr(who);
9770Sstevel@tonic-gate 				}
9780Sstevel@tonic-gate 			}
9790Sstevel@tonic-gate 		}
9800Sstevel@tonic-gate 		if (dh == NULL && bitset(TLS_I_DH1024, req))
9810Sstevel@tonic-gate 		{
9820Sstevel@tonic-gate 			DSA *dsa;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 			/* this takes a while! (7-130s on a 450MHz AMD K6-2) */
9850Sstevel@tonic-gate 			dsa = DSA_generate_parameters(1024, NULL, 0, NULL,
9860Sstevel@tonic-gate 						      NULL, 0, NULL);
9870Sstevel@tonic-gate 			dh = DSA_dup_DH(dsa);
9880Sstevel@tonic-gate 			DSA_free(dsa);
9890Sstevel@tonic-gate 		}
9900Sstevel@tonic-gate 		else
9910Sstevel@tonic-gate 		if (dh == NULL && bitset(TLS_I_DH512, req))
9920Sstevel@tonic-gate 			dh = get_dh512();
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		if (dh == NULL)
9950Sstevel@tonic-gate 		{
9960Sstevel@tonic-gate 			if (LogLevel > 9)
9970Sstevel@tonic-gate 			{
9980Sstevel@tonic-gate 				unsigned long err;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 				err = ERR_get_error();
10010Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
10020Sstevel@tonic-gate 					  "STARTTLS=%s, error: cannot read or set DH parameters(%s): %s",
10030Sstevel@tonic-gate 					  who, dhparam,
10040Sstevel@tonic-gate 					  ERR_error_string(err, NULL));
10050Sstevel@tonic-gate 			}
10060Sstevel@tonic-gate 			if (bitset(TLS_I_REQ_DH, req))
10070Sstevel@tonic-gate 				return false;
10080Sstevel@tonic-gate 		}
10090Sstevel@tonic-gate 		else
10100Sstevel@tonic-gate 		{
10110Sstevel@tonic-gate 			SSL_CTX_set_tmp_dh(*ctx, dh);
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 			/* important to avoid small subgroup attacks */
10140Sstevel@tonic-gate 			SSL_CTX_set_options(*ctx, SSL_OP_SINGLE_DH_USE);
10150Sstevel@tonic-gate 			if (LogLevel > 13)
10160Sstevel@tonic-gate 				sm_syslog(LOG_INFO, NOQID,
10170Sstevel@tonic-gate 					  "STARTTLS=%s, Diffie-Hellman init, key=%d bit (%c)",
10180Sstevel@tonic-gate 					  who, 8 * DH_size(dh), *dhparam);
10190Sstevel@tonic-gate 			DH_free(dh);
10200Sstevel@tonic-gate 		}
10210Sstevel@tonic-gate 	}
10220Sstevel@tonic-gate # endif /* !NO_DH */
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	/* XXX do we need this cache here? */
10260Sstevel@tonic-gate 	if (bitset(TLS_I_CACHE, req))
10271658Sjbeck 	{
10281658Sjbeck 		SSL_CTX_sess_set_cache_size(*ctx, 1);
10291658Sjbeck 		SSL_CTX_set_timeout(*ctx, 1);
10301658Sjbeck 		SSL_CTX_set_session_id_context(*ctx,
10311658Sjbeck 			(void *) &server_session_id_context,
10321658Sjbeck 			sizeof(server_session_id_context));
10331658Sjbeck 		(void) SSL_CTX_set_session_cache_mode(*ctx,
10341658Sjbeck 				SSL_SESS_CACHE_SERVER);
10351658Sjbeck 	}
10361658Sjbeck 	else
10371658Sjbeck 	{
10381658Sjbeck 		(void) SSL_CTX_set_session_cache_mode(*ctx,
10391658Sjbeck 				SSL_SESS_CACHE_OFF);
10401658Sjbeck 	}
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	/* load certificate locations and default CA paths */
10430Sstevel@tonic-gate 	if (bitset(TLS_S_CERTP_EX, status) && bitset(TLS_S_CERTF_EX, status))
10440Sstevel@tonic-gate 	{
10450Sstevel@tonic-gate 		if ((r = SSL_CTX_load_verify_locations(*ctx, cacertfile,
10460Sstevel@tonic-gate 						       cacertpath)) == 1)
10470Sstevel@tonic-gate 		{
10480Sstevel@tonic-gate # if !TLS_NO_RSA
10490Sstevel@tonic-gate 			if (bitset(TLS_I_RSA_TMP, req))
10500Sstevel@tonic-gate 				SSL_CTX_set_tmp_rsa_callback(*ctx, tmp_rsa_key);
10510Sstevel@tonic-gate # endif /* !TLS_NO_RSA */
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 			/*
10540Sstevel@tonic-gate 			**  We have to install our own verify callback:
10550Sstevel@tonic-gate 			**  SSL_VERIFY_PEER requests a client cert but even
10560Sstevel@tonic-gate 			**  though *FAIL_IF* isn't set, the connection
10570Sstevel@tonic-gate 			**  will be aborted if the client presents a cert
10580Sstevel@tonic-gate 			**  that is not "liked" (can't be verified?) by
10590Sstevel@tonic-gate 			**  the TLS library :-(
10600Sstevel@tonic-gate 			*/
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 			/*
10630Sstevel@tonic-gate 			**  XXX currently we could call tls_set_verify()
10640Sstevel@tonic-gate 			**  but we hope that that function will later on
10650Sstevel@tonic-gate 			**  only set the mode per connection.
10660Sstevel@tonic-gate 			*/
10670Sstevel@tonic-gate 			SSL_CTX_set_verify(*ctx,
10680Sstevel@tonic-gate 				bitset(TLS_I_NO_VRFY, req) ? SSL_VERIFY_NONE
10690Sstevel@tonic-gate 							   : SSL_VERIFY_PEER,
10700Sstevel@tonic-gate 				NULL);
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 			/* install verify callback */
10730Sstevel@tonic-gate 			SSL_CTX_set_cert_verify_callback(*ctx, tls_verify_cb,
10740Sstevel@tonic-gate 							 NULL);
10750Sstevel@tonic-gate 			SSL_CTX_set_client_CA_list(*ctx,
10760Sstevel@tonic-gate 				SSL_load_client_CA_file(cacertfile));
10770Sstevel@tonic-gate 		}
10780Sstevel@tonic-gate 		else
10790Sstevel@tonic-gate 		{
10800Sstevel@tonic-gate 			/*
10810Sstevel@tonic-gate 			**  can't load CA data; do we care?
10820Sstevel@tonic-gate 			**  the data is necessary to authenticate the client,
10830Sstevel@tonic-gate 			**  which in turn would be necessary
10840Sstevel@tonic-gate 			**  if we want to allow relaying based on it.
10850Sstevel@tonic-gate 			*/
10860Sstevel@tonic-gate 			if (LogLevel > 5)
10870Sstevel@tonic-gate 			{
10880Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
10890Sstevel@tonic-gate 					  "STARTTLS=%s, error: load verify locs %s, %s failed: %d",
10900Sstevel@tonic-gate 					  who, cacertpath, cacertfile, r);
10910Sstevel@tonic-gate 				if (LogLevel > 9)
10920Sstevel@tonic-gate 					tlslogerr(who);
10930Sstevel@tonic-gate 			}
10940Sstevel@tonic-gate 			if (bitset(TLS_I_VRFY_LOC, req))
10950Sstevel@tonic-gate 				return false;
10960Sstevel@tonic-gate 		}
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/* XXX: make this dependent on an option? */
11000Sstevel@tonic-gate 	if (tTd(96, 9))
11010Sstevel@tonic-gate 		SSL_CTX_set_info_callback(*ctx, apps_ssl_info_cb);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate # if _FFR_TLS_1
11040Sstevel@tonic-gate 	/* install our own cipher list */
11050Sstevel@tonic-gate 	if (CipherList != NULL && *CipherList != '\0')
11060Sstevel@tonic-gate 	{
11070Sstevel@tonic-gate 		if (SSL_CTX_set_cipher_list(*ctx, CipherList) <= 0)
11080Sstevel@tonic-gate 		{
11090Sstevel@tonic-gate 			if (LogLevel > 7)
11100Sstevel@tonic-gate 			{
11110Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
11120Sstevel@tonic-gate 					  "STARTTLS=%s, error: SSL_CTX_set_cipher_list(%s) failed, list ignored",
11130Sstevel@tonic-gate 					  who, CipherList);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 				if (LogLevel > 9)
11160Sstevel@tonic-gate 					tlslogerr(who);
11170Sstevel@tonic-gate 			}
11180Sstevel@tonic-gate 			/* failure if setting to this list is required? */
11190Sstevel@tonic-gate 		}
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate # endif /* _FFR_TLS_1 */
11220Sstevel@tonic-gate 	if (LogLevel > 12)
11230Sstevel@tonic-gate 		sm_syslog(LOG_INFO, NOQID, "STARTTLS=%s, init=%d", who, ok);
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate # if _FFR_TLS_1
11260Sstevel@tonic-gate #  if 0
11270Sstevel@tonic-gate 	/*
11280Sstevel@tonic-gate 	**  this label is required if we want to have a "clean" exit
11290Sstevel@tonic-gate 	**  see the comments above at the initialization of cf2
11300Sstevel@tonic-gate 	*/
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate     endinittls:
11330Sstevel@tonic-gate #  endif /* 0 */
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	/* undo damage to global variables */
11360Sstevel@tonic-gate 	if (cf2 != NULL)
11370Sstevel@tonic-gate 		*--cf2 = ',';
11380Sstevel@tonic-gate 	if (kf2 != NULL)
11390Sstevel@tonic-gate 		*--kf2 = ',';
11400Sstevel@tonic-gate # endif /* _FFR_TLS_1 */
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	return ok;
11430Sstevel@tonic-gate }
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate **  TLS_GET_INFO -- get information about TLS connection
11460Sstevel@tonic-gate **
11470Sstevel@tonic-gate **	Parameters:
11480Sstevel@tonic-gate **		ssl -- TLS connection structure
11490Sstevel@tonic-gate **		srv -- server or client
11500Sstevel@tonic-gate **		host -- hostname of other side
11510Sstevel@tonic-gate **		mac -- macro storage
11520Sstevel@tonic-gate **		certreq -- did we ask for a cert?
11530Sstevel@tonic-gate **
11540Sstevel@tonic-gate **	Returns:
11550Sstevel@tonic-gate **		result of authentication.
11560Sstevel@tonic-gate **
11570Sstevel@tonic-gate **	Side Effects:
11580Sstevel@tonic-gate **		sets macros: {cipher}, {tls_version}, {verify},
11590Sstevel@tonic-gate **		{cipher_bits}, {alg_bits}, {cert}, {cert_subject},
11600Sstevel@tonic-gate **		{cert_issuer}, {cn_subject}, {cn_issuer}
11610Sstevel@tonic-gate */
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate int
tls_get_info(ssl,srv,host,mac,certreq)11640Sstevel@tonic-gate tls_get_info(ssl, srv, host, mac, certreq)
11650Sstevel@tonic-gate 	SSL *ssl;
11660Sstevel@tonic-gate 	bool srv;
11670Sstevel@tonic-gate 	char *host;
11680Sstevel@tonic-gate 	MACROS_T *mac;
11690Sstevel@tonic-gate 	bool certreq;
11700Sstevel@tonic-gate {
11710Sstevel@tonic-gate 	SSL_CIPHER *c;
11720Sstevel@tonic-gate 	int b, r;
11730Sstevel@tonic-gate 	long verifyok;
11740Sstevel@tonic-gate 	char *s, *who;
11750Sstevel@tonic-gate 	char bitstr[16];
11760Sstevel@tonic-gate 	X509 *cert;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	c = SSL_get_current_cipher(ssl);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	/* cast is just workaround for compiler warning */
11810Sstevel@tonic-gate 	macdefine(mac, A_TEMP, macid("{cipher}"),
11820Sstevel@tonic-gate 		  (char *) SSL_CIPHER_get_name(c));
11830Sstevel@tonic-gate 	b = SSL_CIPHER_get_bits(c, &r);
11843544Sjbeck 	(void) sm_snprintf(bitstr, sizeof(bitstr), "%d", b);
11850Sstevel@tonic-gate 	macdefine(mac, A_TEMP, macid("{cipher_bits}"), bitstr);
11863544Sjbeck 	(void) sm_snprintf(bitstr, sizeof(bitstr), "%d", r);
11870Sstevel@tonic-gate 	macdefine(mac, A_TEMP, macid("{alg_bits}"), bitstr);
11880Sstevel@tonic-gate 	s = SSL_CIPHER_get_version(c);
11890Sstevel@tonic-gate 	if (s == NULL)
11900Sstevel@tonic-gate 		s = "UNKNOWN";
11910Sstevel@tonic-gate 	macdefine(mac, A_TEMP, macid("{tls_version}"), s);
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	who = srv ? "server" : "client";
11940Sstevel@tonic-gate 	cert = SSL_get_peer_certificate(ssl);
11950Sstevel@tonic-gate 	verifyok = SSL_get_verify_result(ssl);
11960Sstevel@tonic-gate 	if (LogLevel > 14)
11970Sstevel@tonic-gate 		sm_syslog(LOG_INFO, NOQID,
11980Sstevel@tonic-gate 			  "STARTTLS=%s, get_verify: %ld get_peer: 0x%lx",
11990Sstevel@tonic-gate 			  who, verifyok, (unsigned long) cert);
12000Sstevel@tonic-gate 	if (cert != NULL)
12010Sstevel@tonic-gate 	{
12020Sstevel@tonic-gate 		unsigned int n;
1203*11440SJohn.Beck@Sun.COM 		X509_NAME *subj, *issuer;
12040Sstevel@tonic-gate 		unsigned char md[EVP_MAX_MD_SIZE];
12050Sstevel@tonic-gate 		char buf[MAXNAME];
12060Sstevel@tonic-gate 
1207*11440SJohn.Beck@Sun.COM 		subj = X509_get_subject_name(cert);
1208*11440SJohn.Beck@Sun.COM 		issuer = X509_get_issuer_name(cert);
1209*11440SJohn.Beck@Sun.COM 		X509_NAME_oneline(subj, buf, sizeof(buf));
12100Sstevel@tonic-gate 		macdefine(mac, A_TEMP, macid("{cert_subject}"),
12110Sstevel@tonic-gate 			 xtextify(buf, "<>\")"));
1212*11440SJohn.Beck@Sun.COM 		X509_NAME_oneline(issuer, buf, sizeof(buf));
12130Sstevel@tonic-gate 		macdefine(mac, A_TEMP, macid("{cert_issuer}"),
12140Sstevel@tonic-gate 			 xtextify(buf, "<>\")"));
1215*11440SJohn.Beck@Sun.COM 
1216*11440SJohn.Beck@Sun.COM #define CHECK_X509_NAME(which)	\
1217*11440SJohn.Beck@Sun.COM 	do {	\
1218*11440SJohn.Beck@Sun.COM 		if (r == -1)	\
1219*11440SJohn.Beck@Sun.COM 		{		\
1220*11440SJohn.Beck@Sun.COM 			sm_strlcpy(buf, "BadCertificateUnknown", sizeof(buf)); \
1221*11440SJohn.Beck@Sun.COM 			if (LogLevel > 7)	\
1222*11440SJohn.Beck@Sun.COM 				sm_syslog(LOG_INFO, NOQID,	\
1223*11440SJohn.Beck@Sun.COM 					"STARTTLS=%s, relay=%.100s, field=%s, status=failed to extract CN",	\
1224*11440SJohn.Beck@Sun.COM 					who,	\
1225*11440SJohn.Beck@Sun.COM 					host == NULL ? "local" : host,	\
1226*11440SJohn.Beck@Sun.COM 					which);	\
1227*11440SJohn.Beck@Sun.COM 		}		\
1228*11440SJohn.Beck@Sun.COM 		else if ((size_t)r >= sizeof(buf) - 1)	\
1229*11440SJohn.Beck@Sun.COM 		{		\
1230*11440SJohn.Beck@Sun.COM 			sm_strlcpy(buf, "BadCertificateTooLong", sizeof(buf)); \
1231*11440SJohn.Beck@Sun.COM 			if (LogLevel > 7)	\
1232*11440SJohn.Beck@Sun.COM 				sm_syslog(LOG_INFO, NOQID,	\
1233*11440SJohn.Beck@Sun.COM 					"STARTTLS=%s, relay=%.100s, field=%s, status=CN too long",	\
1234*11440SJohn.Beck@Sun.COM 					who,	\
1235*11440SJohn.Beck@Sun.COM 					host == NULL ? "local" : host,	\
1236*11440SJohn.Beck@Sun.COM 					which);	\
1237*11440SJohn.Beck@Sun.COM 		}		\
1238*11440SJohn.Beck@Sun.COM 		else if ((size_t)r > strlen(buf))	\
1239*11440SJohn.Beck@Sun.COM 		{		\
1240*11440SJohn.Beck@Sun.COM 			sm_strlcpy(buf, "BadCertificateContainsNUL",	\
1241*11440SJohn.Beck@Sun.COM 				sizeof(buf));	\
1242*11440SJohn.Beck@Sun.COM 			if (LogLevel > 7)	\
1243*11440SJohn.Beck@Sun.COM 				sm_syslog(LOG_INFO, NOQID,	\
1244*11440SJohn.Beck@Sun.COM 					"STARTTLS=%s, relay=%.100s, field=%s, status=CN contains NUL",	\
1245*11440SJohn.Beck@Sun.COM 					who,	\
1246*11440SJohn.Beck@Sun.COM 					host == NULL ? "local" : host,	\
1247*11440SJohn.Beck@Sun.COM 					which);	\
1248*11440SJohn.Beck@Sun.COM 		}		\
1249*11440SJohn.Beck@Sun.COM 	} while (0)
1250*11440SJohn.Beck@Sun.COM 
1251*11440SJohn.Beck@Sun.COM 		r = X509_NAME_get_text_by_NID(subj, NID_commonName, buf,
1252*11440SJohn.Beck@Sun.COM 			sizeof buf);
1253*11440SJohn.Beck@Sun.COM 		CHECK_X509_NAME("cn_subject");
12540Sstevel@tonic-gate 		macdefine(mac, A_TEMP, macid("{cn_subject}"),
12550Sstevel@tonic-gate 			 xtextify(buf, "<>\")"));
1256*11440SJohn.Beck@Sun.COM 		r = X509_NAME_get_text_by_NID(issuer, NID_commonName, buf,
1257*11440SJohn.Beck@Sun.COM 			sizeof buf);
1258*11440SJohn.Beck@Sun.COM 		CHECK_X509_NAME("cn_issuer");
12590Sstevel@tonic-gate 		macdefine(mac, A_TEMP, macid("{cn_issuer}"),
12600Sstevel@tonic-gate 			 xtextify(buf, "<>\")"));
12610Sstevel@tonic-gate 		n = 0;
12620Sstevel@tonic-gate 		if (X509_digest(cert, EVP_md5(), md, &n) != 0 && n > 0)
12630Sstevel@tonic-gate 		{
12640Sstevel@tonic-gate 			char md5h[EVP_MAX_MD_SIZE * 3];
12650Sstevel@tonic-gate 			static const char hexcodes[] = "0123456789ABCDEF";
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 			SM_ASSERT((n * 3) + 2 < sizeof(md5h));
12680Sstevel@tonic-gate 			for (r = 0; r < (int) n; r++)
12690Sstevel@tonic-gate 			{
12700Sstevel@tonic-gate 				md5h[r * 3] = hexcodes[(md[r] & 0xf0) >> 4];
12710Sstevel@tonic-gate 				md5h[(r * 3) + 1] = hexcodes[(md[r] & 0x0f)];
12720Sstevel@tonic-gate 				md5h[(r * 3) + 2] = ':';
12730Sstevel@tonic-gate 			}
12740Sstevel@tonic-gate 			md5h[(n * 3) - 1] = '\0';
12750Sstevel@tonic-gate 			macdefine(mac, A_TEMP, macid("{cert_md5}"), md5h);
12760Sstevel@tonic-gate 		}
12770Sstevel@tonic-gate 		else
12780Sstevel@tonic-gate 			macdefine(mac, A_TEMP, macid("{cert_md5}"), "");
12790Sstevel@tonic-gate 	}
12800Sstevel@tonic-gate 	else
12810Sstevel@tonic-gate 	{
12820Sstevel@tonic-gate 		macdefine(mac, A_PERM, macid("{cert_subject}"), "");
12830Sstevel@tonic-gate 		macdefine(mac, A_PERM, macid("{cert_issuer}"), "");
12840Sstevel@tonic-gate 		macdefine(mac, A_PERM, macid("{cn_subject}"), "");
12850Sstevel@tonic-gate 		macdefine(mac, A_PERM, macid("{cn_issuer}"), "");
12860Sstevel@tonic-gate 		macdefine(mac, A_TEMP, macid("{cert_md5}"), "");
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 	switch (verifyok)
12890Sstevel@tonic-gate 	{
12900Sstevel@tonic-gate 	  case X509_V_OK:
12910Sstevel@tonic-gate 		if (cert != NULL)
12920Sstevel@tonic-gate 		{
12930Sstevel@tonic-gate 			s = "OK";
12940Sstevel@tonic-gate 			r = TLS_AUTH_OK;
12950Sstevel@tonic-gate 		}
12960Sstevel@tonic-gate 		else
12970Sstevel@tonic-gate 		{
12980Sstevel@tonic-gate 			s = certreq ? "NO" : "NOT",
12990Sstevel@tonic-gate 			r = TLS_AUTH_NO;
13000Sstevel@tonic-gate 		}
13010Sstevel@tonic-gate 		break;
13020Sstevel@tonic-gate 	  default:
13030Sstevel@tonic-gate 		s = "FAIL";
13040Sstevel@tonic-gate 		r = TLS_AUTH_FAIL;
13050Sstevel@tonic-gate 		break;
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 	macdefine(mac, A_PERM, macid("{verify}"), s);
13080Sstevel@tonic-gate 	if (cert != NULL)
13090Sstevel@tonic-gate 		X509_free(cert);
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	/* do some logging */
13120Sstevel@tonic-gate 	if (LogLevel > 8)
13130Sstevel@tonic-gate 	{
13140Sstevel@tonic-gate 		char *vers, *s1, *s2, *cbits, *algbits;
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 		vers = macget(mac, macid("{tls_version}"));
13170Sstevel@tonic-gate 		cbits = macget(mac, macid("{cipher_bits}"));
13180Sstevel@tonic-gate 		algbits = macget(mac, macid("{alg_bits}"));
13190Sstevel@tonic-gate 		s1 = macget(mac, macid("{verify}"));
13200Sstevel@tonic-gate 		s2 = macget(mac, macid("{cipher}"));
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 		/* XXX: maybe cut off ident info? */
13230Sstevel@tonic-gate 		sm_syslog(LOG_INFO, NOQID,
13240Sstevel@tonic-gate 			  "STARTTLS=%s, relay=%.100s, version=%.16s, verify=%.16s, cipher=%.64s, bits=%.6s/%.6s",
13250Sstevel@tonic-gate 			  who,
13260Sstevel@tonic-gate 			  host == NULL ? "local" : host,
13270Sstevel@tonic-gate 			  vers, s1, s2, /* sm_snprintf() can deal with NULL */
13280Sstevel@tonic-gate 			  algbits == NULL ? "0" : algbits,
13290Sstevel@tonic-gate 			  cbits == NULL ? "0" : cbits);
13300Sstevel@tonic-gate 		if (LogLevel > 11)
13310Sstevel@tonic-gate 		{
13320Sstevel@tonic-gate 			/*
13330Sstevel@tonic-gate 			**  Maybe run xuntextify on the strings?
13340Sstevel@tonic-gate 			**  That is easier to read but makes it maybe a bit
13350Sstevel@tonic-gate 			**  more complicated to figure out the right values
13360Sstevel@tonic-gate 			**  for the access map...
13370Sstevel@tonic-gate 			*/
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 			s1 = macget(mac, macid("{cert_subject}"));
13400Sstevel@tonic-gate 			s2 = macget(mac, macid("{cert_issuer}"));
13410Sstevel@tonic-gate 			sm_syslog(LOG_INFO, NOQID,
13420Sstevel@tonic-gate 				  "STARTTLS=%s, cert-subject=%.256s, cert-issuer=%.256s, verifymsg=%s",
13430Sstevel@tonic-gate 				  who, s1, s2,
13440Sstevel@tonic-gate 				  X509_verify_cert_error_string(verifyok));
13450Sstevel@tonic-gate 		}
13460Sstevel@tonic-gate 	}
13470Sstevel@tonic-gate 	return r;
13480Sstevel@tonic-gate }
13490Sstevel@tonic-gate /*
13500Sstevel@tonic-gate **  ENDTLS -- shutdown secure connection
13510Sstevel@tonic-gate **
13520Sstevel@tonic-gate **	Parameters:
13530Sstevel@tonic-gate **		ssl -- SSL connection information.
13540Sstevel@tonic-gate **		side -- server/client (for logging).
13550Sstevel@tonic-gate **
13560Sstevel@tonic-gate **	Returns:
13570Sstevel@tonic-gate **		success? (EX_* code)
13580Sstevel@tonic-gate */
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate int
endtls(ssl,side)13610Sstevel@tonic-gate endtls(ssl, side)
13620Sstevel@tonic-gate 	SSL *ssl;
13630Sstevel@tonic-gate 	char *side;
13640Sstevel@tonic-gate {
13650Sstevel@tonic-gate 	int ret = EX_OK;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	if (ssl != NULL)
13680Sstevel@tonic-gate 	{
13690Sstevel@tonic-gate 		int r;
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		if ((r = SSL_shutdown(ssl)) < 0)
13720Sstevel@tonic-gate 		{
13730Sstevel@tonic-gate 			if (LogLevel > 11)
13740Sstevel@tonic-gate 			{
13750Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
13760Sstevel@tonic-gate 					  "STARTTLS=%s, SSL_shutdown failed: %d",
13770Sstevel@tonic-gate 					  side, r);
13780Sstevel@tonic-gate 				tlslogerr(side);
13790Sstevel@tonic-gate 			}
13800Sstevel@tonic-gate 			ret = EX_SOFTWARE;
13810Sstevel@tonic-gate 		}
13820Sstevel@tonic-gate # if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 		/*
13850Sstevel@tonic-gate 		**  Bug in OpenSSL (at least up to 0.9.6b):
13860Sstevel@tonic-gate 		**  From: Lutz.Jaenicke@aet.TU-Cottbus.DE
13870Sstevel@tonic-gate 		**  Message-ID: <20010723152244.A13122@serv01.aet.tu-cottbus.de>
13880Sstevel@tonic-gate 		**  To: openssl-users@openssl.org
13890Sstevel@tonic-gate 		**  Subject: Re: SSL_shutdown() woes (fwd)
13900Sstevel@tonic-gate 		**
13910Sstevel@tonic-gate 		**  The side sending the shutdown alert first will
13920Sstevel@tonic-gate 		**  not care about the answer of the peer but will
13930Sstevel@tonic-gate 		**  immediately return with a return value of "0"
13940Sstevel@tonic-gate 		**  (ssl/s3_lib.c:ssl3_shutdown()). SSL_get_error will evaluate
13950Sstevel@tonic-gate 		**  the value of "0" and as the shutdown alert of the peer was
13960Sstevel@tonic-gate 		**  not received (actually, the program did not even wait for
13970Sstevel@tonic-gate 		**  the answer), an SSL_ERROR_SYSCALL is flagged, because this
13980Sstevel@tonic-gate 		**  is the default rule in case everything else does not apply.
13990Sstevel@tonic-gate 		**
14000Sstevel@tonic-gate 		**  For your server the problem is different, because it
14010Sstevel@tonic-gate 		**  receives the shutdown first (setting SSL_RECEIVED_SHUTDOWN),
14020Sstevel@tonic-gate 		**  then sends its response (SSL_SENT_SHUTDOWN), so for the
14030Sstevel@tonic-gate 		**  server the shutdown was successfull.
14040Sstevel@tonic-gate 		**
14050Sstevel@tonic-gate 		**  As is by know, you would have to call SSL_shutdown() once
14060Sstevel@tonic-gate 		**  and ignore an SSL_ERROR_SYSCALL returned. Then call
14070Sstevel@tonic-gate 		**  SSL_shutdown() again to actually get the server's response.
14080Sstevel@tonic-gate 		**
14090Sstevel@tonic-gate 		**  In the last discussion, Bodo Moeller concluded that a
14100Sstevel@tonic-gate 		**  rewrite of the shutdown code would be necessary, but
14110Sstevel@tonic-gate 		**  probably with another API, as the change would not be
14120Sstevel@tonic-gate 		**  compatible to the way it is now.  Things do not become
14130Sstevel@tonic-gate 		**  easier as other programs do not follow the shutdown
14140Sstevel@tonic-gate 		**  guidelines anyway, so that a lot error conditions and
14150Sstevel@tonic-gate 		**  compitibility issues would have to be caught.
14160Sstevel@tonic-gate 		**
14170Sstevel@tonic-gate 		**  For now the recommondation is to ignore the error message.
14180Sstevel@tonic-gate 		*/
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 		else if (r == 0)
14210Sstevel@tonic-gate 		{
14220Sstevel@tonic-gate 			if (LogLevel > 15)
14230Sstevel@tonic-gate 			{
14240Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
14250Sstevel@tonic-gate 					  "STARTTLS=%s, SSL_shutdown not done",
14260Sstevel@tonic-gate 					  side);
14270Sstevel@tonic-gate 				tlslogerr(side);
14280Sstevel@tonic-gate 			}
14290Sstevel@tonic-gate 			ret = EX_SOFTWARE;
14300Sstevel@tonic-gate 		}
14310Sstevel@tonic-gate # endif /* !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL */
14320Sstevel@tonic-gate 		SSL_free(ssl);
14330Sstevel@tonic-gate 		ssl = NULL;
14340Sstevel@tonic-gate 	}
14350Sstevel@tonic-gate 	return ret;
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate # if !TLS_NO_RSA
14390Sstevel@tonic-gate /*
14400Sstevel@tonic-gate **  TMP_RSA_KEY -- return temporary RSA key
14410Sstevel@tonic-gate **
14420Sstevel@tonic-gate **	Parameters:
14430Sstevel@tonic-gate **		s -- TLS connection structure
14440Sstevel@tonic-gate **		export --
14450Sstevel@tonic-gate **		keylength --
14460Sstevel@tonic-gate **
14470Sstevel@tonic-gate **	Returns:
14480Sstevel@tonic-gate **		temporary RSA key.
14490Sstevel@tonic-gate */
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate #   ifndef MAX_RSA_TMP_CNT
14520Sstevel@tonic-gate #    define MAX_RSA_TMP_CNT	1000	/* XXX better value? */
14530Sstevel@tonic-gate #   endif /* ! MAX_RSA_TMP_CNT */
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate /* ARGUSED0 */
14560Sstevel@tonic-gate static RSA *
tmp_rsa_key(s,export,keylength)14570Sstevel@tonic-gate tmp_rsa_key(s, export, keylength)
14580Sstevel@tonic-gate 	SSL *s;
14590Sstevel@tonic-gate 	int export;
14600Sstevel@tonic-gate 	int keylength;
14610Sstevel@tonic-gate {
14620Sstevel@tonic-gate #   if SM_CONF_SHM
14630Sstevel@tonic-gate 	extern int ShmId;
14640Sstevel@tonic-gate 	extern int *PRSATmpCnt;
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	if (ShmId != SM_SHM_NO_ID && rsa_tmp != NULL &&
14670Sstevel@tonic-gate 	    ++(*PRSATmpCnt) < MAX_RSA_TMP_CNT)
14680Sstevel@tonic-gate 		return rsa_tmp;
14690Sstevel@tonic-gate #   endif /* SM_CONF_SHM */
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	if (rsa_tmp != NULL)
14720Sstevel@tonic-gate 		RSA_free(rsa_tmp);
14730Sstevel@tonic-gate 	rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL, NULL);
14740Sstevel@tonic-gate 	if (rsa_tmp == NULL)
14750Sstevel@tonic-gate 	{
14760Sstevel@tonic-gate 		if (LogLevel > 0)
14770Sstevel@tonic-gate 			sm_syslog(LOG_ERR, NOQID,
14780Sstevel@tonic-gate 				  "STARTTLS=server, tmp_rsa_key: RSA_generate_key failed!");
14790Sstevel@tonic-gate 	}
14800Sstevel@tonic-gate 	else
14810Sstevel@tonic-gate 	{
14820Sstevel@tonic-gate #   if SM_CONF_SHM
14830Sstevel@tonic-gate #    if 0
14840Sstevel@tonic-gate 		/*
14850Sstevel@tonic-gate 		**  XXX we can't (yet) share the new key...
14860Sstevel@tonic-gate 		**	The RSA structure contains pointers hence it can't be
14870Sstevel@tonic-gate 		**	easily kept in shared memory.  It must be transformed
14880Sstevel@tonic-gate 		**	into a continous memory region first, then stored,
14890Sstevel@tonic-gate 		**	and later read out again (each time re-transformed).
14900Sstevel@tonic-gate 		*/
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 		if (ShmId != SM_SHM_NO_ID)
14930Sstevel@tonic-gate 			*PRSATmpCnt = 0;
14940Sstevel@tonic-gate #    endif /* 0 */
14950Sstevel@tonic-gate #   endif /* SM_CONF_SHM */
14960Sstevel@tonic-gate 		if (LogLevel > 9)
14970Sstevel@tonic-gate 			sm_syslog(LOG_ERR, NOQID,
14980Sstevel@tonic-gate 				  "STARTTLS=server, tmp_rsa_key: new temp RSA key");
14990Sstevel@tonic-gate 	}
15000Sstevel@tonic-gate 	return rsa_tmp;
15010Sstevel@tonic-gate }
15020Sstevel@tonic-gate # endif /* !TLS_NO_RSA */
15030Sstevel@tonic-gate /*
15040Sstevel@tonic-gate **  APPS_SSL_INFO_CB -- info callback for TLS connections
15050Sstevel@tonic-gate **
15060Sstevel@tonic-gate **	Parameters:
15070Sstevel@tonic-gate **		s -- TLS connection structure
15080Sstevel@tonic-gate **		where -- state in handshake
15090Sstevel@tonic-gate **		ret -- return code of last operation
15100Sstevel@tonic-gate **
15110Sstevel@tonic-gate **	Returns:
15120Sstevel@tonic-gate **		none.
15130Sstevel@tonic-gate */
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate static void
apps_ssl_info_cb(s,where,ret)15160Sstevel@tonic-gate apps_ssl_info_cb(s, where, ret)
15170Sstevel@tonic-gate 	CONST097 SSL *s;
15180Sstevel@tonic-gate 	int where;
15190Sstevel@tonic-gate 	int ret;
15200Sstevel@tonic-gate {
15210Sstevel@tonic-gate 	int w;
15220Sstevel@tonic-gate 	char *str;
15230Sstevel@tonic-gate 	BIO *bio_err = NULL;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	if (LogLevel > 14)
15260Sstevel@tonic-gate 		sm_syslog(LOG_INFO, NOQID,
15270Sstevel@tonic-gate 			  "STARTTLS: info_callback where=0x%x, ret=%d",
15280Sstevel@tonic-gate 			  where, ret);
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	w = where & ~SSL_ST_MASK;
15310Sstevel@tonic-gate 	if (bio_err == NULL)
15320Sstevel@tonic-gate 		bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	if (bitset(SSL_ST_CONNECT, w))
15350Sstevel@tonic-gate 		str = "SSL_connect";
15360Sstevel@tonic-gate 	else if (bitset(SSL_ST_ACCEPT, w))
15370Sstevel@tonic-gate 		str = "SSL_accept";
15380Sstevel@tonic-gate 	else
15390Sstevel@tonic-gate 		str = "undefined";
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate 	if (bitset(SSL_CB_LOOP, where))
15420Sstevel@tonic-gate 	{
15430Sstevel@tonic-gate 		if (LogLevel > 12)
15440Sstevel@tonic-gate 			sm_syslog(LOG_NOTICE, NOQID,
15450Sstevel@tonic-gate 				"STARTTLS: %s:%s",
15460Sstevel@tonic-gate 				str, SSL_state_string_long(s));
15470Sstevel@tonic-gate 	}
15480Sstevel@tonic-gate 	else if (bitset(SSL_CB_ALERT, where))
15490Sstevel@tonic-gate 	{
15500Sstevel@tonic-gate 		str = bitset(SSL_CB_READ, where) ? "read" : "write";
15510Sstevel@tonic-gate 		if (LogLevel > 12)
15520Sstevel@tonic-gate 			sm_syslog(LOG_NOTICE, NOQID,
15530Sstevel@tonic-gate 				"STARTTLS: SSL3 alert %s:%s:%s",
15540Sstevel@tonic-gate 				str, SSL_alert_type_string_long(ret),
15550Sstevel@tonic-gate 				SSL_alert_desc_string_long(ret));
15560Sstevel@tonic-gate 	}
15570Sstevel@tonic-gate 	else if (bitset(SSL_CB_EXIT, where))
15580Sstevel@tonic-gate 	{
15590Sstevel@tonic-gate 		if (ret == 0)
15600Sstevel@tonic-gate 		{
15610Sstevel@tonic-gate 			if (LogLevel > 7)
15620Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
15630Sstevel@tonic-gate 					"STARTTLS: %s:failed in %s",
15640Sstevel@tonic-gate 					str, SSL_state_string_long(s));
15650Sstevel@tonic-gate 		}
15660Sstevel@tonic-gate 		else if (ret < 0)
15670Sstevel@tonic-gate 		{
15680Sstevel@tonic-gate 			if (LogLevel > 7)
15690Sstevel@tonic-gate 				sm_syslog(LOG_WARNING, NOQID,
15700Sstevel@tonic-gate 					"STARTTLS: %s:error in %s",
15710Sstevel@tonic-gate 					str, SSL_state_string_long(s));
15720Sstevel@tonic-gate 		}
15730Sstevel@tonic-gate 	}
15740Sstevel@tonic-gate }
15750Sstevel@tonic-gate /*
15760Sstevel@tonic-gate **  TLS_VERIFY_LOG -- log verify error for TLS certificates
15770Sstevel@tonic-gate **
15780Sstevel@tonic-gate **	Parameters:
15790Sstevel@tonic-gate **		ok -- verify ok?
15800Sstevel@tonic-gate **		ctx -- x509 context
15810Sstevel@tonic-gate **
15820Sstevel@tonic-gate **	Returns:
15830Sstevel@tonic-gate **		0 -- fatal error
15840Sstevel@tonic-gate **		1 -- ok
15850Sstevel@tonic-gate */
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate static int
tls_verify_log(ok,ctx,name)15880Sstevel@tonic-gate tls_verify_log(ok, ctx, name)
15890Sstevel@tonic-gate 	int ok;
15900Sstevel@tonic-gate 	X509_STORE_CTX *ctx;
15910Sstevel@tonic-gate 	char *name;
15920Sstevel@tonic-gate {
15930Sstevel@tonic-gate 	SSL *ssl;
15940Sstevel@tonic-gate 	X509 *cert;
15950Sstevel@tonic-gate 	int reason, depth;
15960Sstevel@tonic-gate 	char buf[512];
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	cert = X509_STORE_CTX_get_current_cert(ctx);
15990Sstevel@tonic-gate 	reason = X509_STORE_CTX_get_error(ctx);
16000Sstevel@tonic-gate 	depth = X509_STORE_CTX_get_error_depth(ctx);
16010Sstevel@tonic-gate 	ssl = (SSL *) X509_STORE_CTX_get_ex_data(ctx,
16020Sstevel@tonic-gate 			SSL_get_ex_data_X509_STORE_CTX_idx());
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 	if (ssl == NULL)
16050Sstevel@tonic-gate 	{
16060Sstevel@tonic-gate 		/* internal error */
16070Sstevel@tonic-gate 		sm_syslog(LOG_ERR, NOQID,
16080Sstevel@tonic-gate 			  "STARTTLS: internal error: tls_verify_cb: ssl == NULL");
16090Sstevel@tonic-gate 		return 0;
16100Sstevel@tonic-gate 	}
16110Sstevel@tonic-gate 
16123544Sjbeck 	X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
16130Sstevel@tonic-gate 	sm_syslog(LOG_INFO, NOQID,
16140Sstevel@tonic-gate 		  "STARTTLS: %s cert verify: depth=%d %s, state=%d, reason=%s",
16150Sstevel@tonic-gate 		  name, depth, buf, ok, X509_verify_cert_error_string(reason));
16160Sstevel@tonic-gate 	return 1;
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate /*
16200Sstevel@tonic-gate **  TLS_VERIFY_CB -- verify callback for TLS certificates
16210Sstevel@tonic-gate **
16220Sstevel@tonic-gate **	Parameters:
16230Sstevel@tonic-gate **		ctx -- x509 context
16240Sstevel@tonic-gate **
16250Sstevel@tonic-gate **	Returns:
16260Sstevel@tonic-gate **		accept connection?
16270Sstevel@tonic-gate **		currently: always yes.
16280Sstevel@tonic-gate */
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate static int
16310Sstevel@tonic-gate #  if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L
tls_verify_cb(ctx)16320Sstevel@tonic-gate tls_verify_cb(ctx)
16330Sstevel@tonic-gate 	X509_STORE_CTX *ctx;
16340Sstevel@tonic-gate #  else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
16350Sstevel@tonic-gate tls_verify_cb(ctx, unused)
16360Sstevel@tonic-gate 	X509_STORE_CTX *ctx;
16370Sstevel@tonic-gate 	void *unused;
16380Sstevel@tonic-gate #  endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
16390Sstevel@tonic-gate {
16400Sstevel@tonic-gate 	int ok;
16410Sstevel@tonic-gate 
1642*11440SJohn.Beck@Sun.COM 	/*
1643*11440SJohn.Beck@Sun.COM 	**  man SSL_CTX_set_cert_verify_callback():
1644*11440SJohn.Beck@Sun.COM 	**  callback should return 1 to indicate verification success
1645*11440SJohn.Beck@Sun.COM 	**  and 0 to indicate verification failure.
1646*11440SJohn.Beck@Sun.COM 	*/
1647*11440SJohn.Beck@Sun.COM 
16480Sstevel@tonic-gate 	ok = X509_verify_cert(ctx);
1649*11440SJohn.Beck@Sun.COM 	if (ok <= 0)
16500Sstevel@tonic-gate 	{
16510Sstevel@tonic-gate 		if (LogLevel > 13)
16520Sstevel@tonic-gate 			return tls_verify_log(ok, ctx, "TLS");
16530Sstevel@tonic-gate 	}
1654*11440SJohn.Beck@Sun.COM 	return 1;
16550Sstevel@tonic-gate }
16560Sstevel@tonic-gate /*
16570Sstevel@tonic-gate **  TLSLOGERR -- log the errors from the TLS error stack
16580Sstevel@tonic-gate **
16590Sstevel@tonic-gate **	Parameters:
16600Sstevel@tonic-gate **		who -- server/client (for logging).
16610Sstevel@tonic-gate **
16620Sstevel@tonic-gate **	Returns:
16630Sstevel@tonic-gate **		none.
16640Sstevel@tonic-gate */
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate void
tlslogerr(who)16670Sstevel@tonic-gate tlslogerr(who)
16681658Sjbeck 	const char *who;
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	unsigned long l;
16710Sstevel@tonic-gate 	int line, flags;
16720Sstevel@tonic-gate 	unsigned long es;
16730Sstevel@tonic-gate 	char *file, *data;
16740Sstevel@tonic-gate 	char buf[256];
16750Sstevel@tonic-gate #  define CP (const char **)
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	es = CRYPTO_thread_id();
16780Sstevel@tonic-gate 	while ((l = ERR_get_error_line_data(CP &file, &line, CP &data, &flags))
16790Sstevel@tonic-gate 		!= 0)
16800Sstevel@tonic-gate 	{
16810Sstevel@tonic-gate 		sm_syslog(LOG_WARNING, NOQID,
16820Sstevel@tonic-gate 			  "STARTTLS=%s: %lu:%s:%s:%d:%s", who, es,
16830Sstevel@tonic-gate 			  ERR_error_string(l, buf),
16840Sstevel@tonic-gate 			  file, line,
16850Sstevel@tonic-gate 			  bitset(ERR_TXT_STRING, flags) ? data : "");
16860Sstevel@tonic-gate 	}
16870Sstevel@tonic-gate }
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate # if OPENSSL_VERSION_NUMBER > 0x00907000L
16900Sstevel@tonic-gate /*
16910Sstevel@tonic-gate **  X509_VERIFY_CB -- verify callback
16920Sstevel@tonic-gate **
16930Sstevel@tonic-gate **	Parameters:
16940Sstevel@tonic-gate **		ctx -- x509 context
16950Sstevel@tonic-gate **
16960Sstevel@tonic-gate **	Returns:
16970Sstevel@tonic-gate **		accept connection?
16980Sstevel@tonic-gate **		currently: always yes.
16990Sstevel@tonic-gate */
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate static int
x509_verify_cb(ok,ctx)17020Sstevel@tonic-gate x509_verify_cb(ok, ctx)
17030Sstevel@tonic-gate 	int ok;
17040Sstevel@tonic-gate 	X509_STORE_CTX *ctx;
17050Sstevel@tonic-gate {
17060Sstevel@tonic-gate 	if (ok == 0)
17070Sstevel@tonic-gate 	{
17080Sstevel@tonic-gate 		if (LogLevel > 13)
17090Sstevel@tonic-gate 			tls_verify_log(ok, ctx, "x509");
17100Sstevel@tonic-gate 		if (ctx->error == X509_V_ERR_UNABLE_TO_GET_CRL)
17110Sstevel@tonic-gate 		{
17120Sstevel@tonic-gate 			ctx->error = 0;
17130Sstevel@tonic-gate 			return 1;	/* override it */
17140Sstevel@tonic-gate 		}
17150Sstevel@tonic-gate 	}
17160Sstevel@tonic-gate 	return ok;
17170Sstevel@tonic-gate }
17180Sstevel@tonic-gate # endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
17190Sstevel@tonic-gate #endif /* STARTTLS */
1720