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