1N/A/*
1N/A * Copyright (c) 2000-2006, 2008, 2009, 2011 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A *
1N/A */
1N/A
1N/A#include <sendmail.h>
1N/A
1N/ASM_RCSID("@(#)$Id: tls.c,v 8.118 2011/03/07 23:20:47 ca Exp $")
1N/A
1N/A#if STARTTLS
1N/A# include <openssl/err.h>
1N/A# include <openssl/bio.h>
1N/A# include <openssl/pem.h>
1N/A# ifndef HASURANDOMDEV
1N/A# include <openssl/rand.h>
1N/A# endif /* ! HASURANDOMDEV */
1N/A# if !TLS_NO_RSA
1N/Astatic RSA *rsa_tmp = NULL; /* temporary RSA key */
1N/Astatic RSA *tmp_rsa_key __P((SSL *, int, int));
1N/A# endif /* !TLS_NO_RSA */
1N/A# if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L
1N/Astatic int tls_verify_cb __P((X509_STORE_CTX *));
1N/A# else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
1N/Astatic int tls_verify_cb __P((X509_STORE_CTX *, void *));
1N/A# endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
1N/A
1N/A# if OPENSSL_VERSION_NUMBER > 0x00907000L
1N/Astatic int x509_verify_cb __P((int, X509_STORE_CTX *));
1N/A# endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
1N/A
1N/A# if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L
1N/A# define CONST097
1N/A# else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
1N/A# define CONST097 const
1N/A# endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
1N/Astatic void apps_ssl_info_cb __P((CONST097 SSL *, int , int));
1N/Astatic bool tls_ok_f __P((char *, char *, int));
1N/Astatic bool tls_safe_f __P((char *, long, bool));
1N/Astatic int tls_verify_log __P((int, X509_STORE_CTX *, char *));
1N/A
1N/A# if !NO_DH
1N/Astatic DH *get_dh512 __P((void));
1N/A
1N/Astatic unsigned char dh512_p[] =
1N/A{
1N/A 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
1N/A 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
1N/A 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
1N/A 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
1N/A 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
1N/A 0x47,0x74,0xE8,0x33
1N/A};
1N/Astatic unsigned char dh512_g[] =
1N/A{
1N/A 0x02
1N/A};
1N/A
1N/Astatic DH *
1N/Aget_dh512()
1N/A{
1N/A DH *dh = NULL;
1N/A
1N/A if ((dh = DH_new()) == NULL)
1N/A return NULL;
1N/A dh->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL);
1N/A dh->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL);
1N/A if ((dh->p == NULL) || (dh->g == NULL))
1N/A return NULL;
1N/A return dh;
1N/A}
1N/A# endif /* !NO_DH */
1N/A
1N/A
1N/A/*
1N/A** TLS_RAND_INIT -- initialize STARTTLS random generator
1N/A**
1N/A** Parameters:
1N/A** randfile -- name of file with random data
1N/A** logl -- loglevel
1N/A**
1N/A** Returns:
1N/A** success/failure
1N/A**
1N/A** Side Effects:
1N/A** initializes PRNG for tls library.
1N/A*/
1N/A
1N/A# define MIN_RAND_BYTES 128 /* 1024 bits */
1N/A
1N/A# define RF_OK 0 /* randfile OK */
1N/A# define RF_MISS 1 /* randfile == NULL || *randfile == '\0' */
1N/A# define RF_UNKNOWN 2 /* unknown prefix for randfile */
1N/A
1N/A# define RI_NONE 0 /* no init yet */
1N/A# define RI_SUCCESS 1 /* init was successful */
1N/A# define RI_FAIL 2 /* init failed */
1N/A
1N/Astatic bool tls_rand_init __P((char *, int));
1N/A
1N/Astatic bool
1N/Atls_rand_init(randfile, logl)
1N/A char *randfile;
1N/A int logl;
1N/A{
1N/A# ifndef HASURANDOMDEV
1N/A /* not required if /dev/urandom exists, OpenSSL does it internally */
1N/A
1N/A bool ok;
1N/A int randdef;
1N/A static int done = RI_NONE;
1N/A
1N/A /*
1N/A ** initialize PRNG
1N/A */
1N/A
1N/A /* did we try this before? if yes: return old value */
1N/A if (done != RI_NONE)
1N/A return done == RI_SUCCESS;
1N/A
1N/A /* set default values */
1N/A ok = false;
1N/A done = RI_FAIL;
1N/A randdef = (randfile == NULL || *randfile == '\0') ? RF_MISS : RF_OK;
1N/A# if EGD
1N/A if (randdef == RF_OK && sm_strncasecmp(randfile, "egd:", 4) == 0)
1N/A {
1N/A randfile += 4;
1N/A if (RAND_egd(randfile) < 0)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS: RAND_egd(%s) failed: random number generator not seeded",
1N/A randfile);
1N/A }
1N/A else
1N/A ok = true;
1N/A }
1N/A else
1N/A# endif /* EGD */
1N/A if (randdef == RF_OK && sm_strncasecmp(randfile, "file:", 5) == 0)
1N/A {
1N/A int fd;
1N/A long sff;
1N/A struct stat st;
1N/A
1N/A randfile += 5;
1N/A sff = SFF_SAFEDIRPATH | SFF_NOWLINK
1N/A | SFF_NOGWFILES | SFF_NOWWFILES
1N/A | SFF_NOGRFILES | SFF_NOWRFILES
1N/A | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT;
1N/A if (DontLockReadFiles)
1N/A sff |= SFF_NOLOCK;
1N/A if ((fd = safeopen(randfile, O_RDONLY, 0, sff)) >= 0)
1N/A {
1N/A if (fstat(fd, &st) < 0)
1N/A {
1N/A if (LogLevel > logl)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "STARTTLS: can't fstat(%s)",
1N/A randfile);
1N/A }
1N/A else
1N/A {
1N/A bool use, problem;
1N/A
1N/A use = true;
1N/A problem = false;
1N/A
1N/A /* max. age of file: 10 minutes */
1N/A if (st.st_mtime + 600 < curtime())
1N/A {
1N/A use = bitnset(DBS_INSUFFICIENTENTROPY,
1N/A DontBlameSendmail);
1N/A problem = true;
1N/A if (LogLevel > logl)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "STARTTLS: RandFile %s too old: %s",
1N/A randfile,
1N/A use ? "unsafe" :
1N/A "unusable");
1N/A }
1N/A if (use && st.st_size < MIN_RAND_BYTES)
1N/A {
1N/A use = bitnset(DBS_INSUFFICIENTENTROPY,
1N/A DontBlameSendmail);
1N/A problem = true;
1N/A if (LogLevel > logl)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "STARTTLS: size(%s) < %d: %s",
1N/A randfile,
1N/A MIN_RAND_BYTES,
1N/A use ? "unsafe" :
1N/A "unusable");
1N/A }
1N/A if (use)
1N/A ok = RAND_load_file(randfile, -1) >=
1N/A MIN_RAND_BYTES;
1N/A if (use && !ok)
1N/A {
1N/A if (LogLevel > logl)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS: RAND_load_file(%s) failed: random number generator not seeded",
1N/A randfile);
1N/A }
1N/A if (problem)
1N/A ok = false;
1N/A }
1N/A if (ok || bitnset(DBS_INSUFFICIENTENTROPY,
1N/A DontBlameSendmail))
1N/A {
1N/A /* add this even if fstat() failed */
1N/A RAND_seed((void *) &st, sizeof(st));
1N/A }
1N/A (void) close(fd);
1N/A }
1N/A else
1N/A {
1N/A if (LogLevel > logl)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS: Warning: safeopen(%s) failed",
1N/A randfile);
1N/A }
1N/A }
1N/A else if (randdef == RF_OK)
1N/A {
1N/A if (LogLevel > logl)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS: Error: no proper random file definition %s",
1N/A randfile);
1N/A randdef = RF_UNKNOWN;
1N/A }
1N/A if (randdef == RF_MISS)
1N/A {
1N/A if (LogLevel > logl)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS: Error: missing random file definition");
1N/A }
1N/A if (!ok && bitnset(DBS_INSUFFICIENTENTROPY, DontBlameSendmail))
1N/A {
1N/A int i;
1N/A long r;
1N/A unsigned char buf[MIN_RAND_BYTES];
1N/A
1N/A /* assert((MIN_RAND_BYTES % sizeof(long)) == 0); */
1N/A for (i = 0; i <= sizeof(buf) - sizeof(long); i += sizeof(long))
1N/A {
1N/A r = get_random();
1N/A (void) memcpy(buf + i, (void *) &r, sizeof(long));
1N/A }
1N/A RAND_seed(buf, sizeof(buf));
1N/A if (LogLevel > logl)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS: Warning: random number generator not properly seeded");
1N/A ok = true;
1N/A }
1N/A done = ok ? RI_SUCCESS : RI_FAIL;
1N/A return ok;
1N/A# else /* ! HASURANDOMDEV */
1N/A return true;
1N/A# endif /* ! HASURANDOMDEV */
1N/A}
1N/A/*
1N/A** INIT_TLS_LIBRARY -- Calls functions which setup TLS library for global use.
1N/A**
1N/A** Parameters:
1N/A** none.
1N/A**
1N/A** Returns:
1N/A** succeeded?
1N/A*/
1N/A
1N/Abool
1N/Ainit_tls_library()
1N/A{
1N/A /* basic TLS initialization, ignore result for now */
1N/A SSL_library_init();
1N/A SSL_load_error_strings();
1N/A# if 0
1N/A /* this is currently a macro for SSL_library_init */
1N/A SSLeay_add_ssl_algorithms();
1N/A# endif /* 0 */
1N/A
1N/A return tls_rand_init(RandFile, 7);
1N/A}
1N/A/*
1N/A** TLS_SET_VERIFY -- request client certificate?
1N/A**
1N/A** Parameters:
1N/A** ctx -- TLS context
1N/A** ssl -- TLS structure
1N/A** vrfy -- require certificate?
1N/A**
1N/A** Returns:
1N/A** none.
1N/A**
1N/A** Side Effects:
1N/A** Sets verification state for TLS
1N/A**
1N/A# if TLS_VRFY_PER_CTX
1N/A** Notice:
1N/A** This is per TLS context, not per TLS structure;
1N/A** the former is global, the latter per connection.
1N/A** It would be nice to do this per connection, but this
1N/A** doesn't work in the current TLS libraries :-(
1N/A# endif * TLS_VRFY_PER_CTX *
1N/A*/
1N/A
1N/Avoid
1N/Atls_set_verify(ctx, ssl, vrfy)
1N/A SSL_CTX *ctx;
1N/A SSL *ssl;
1N/A bool vrfy;
1N/A{
1N/A# if !TLS_VRFY_PER_CTX
1N/A SSL_set_verify(ssl, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL);
1N/A# else /* !TLS_VRFY_PER_CTX */
1N/A SSL_CTX_set_verify(ctx, vrfy ? SSL_VERIFY_PEER : SSL_VERIFY_NONE,
1N/A NULL);
1N/A# endif /* !TLS_VRFY_PER_CTX */
1N/A}
1N/A
1N/A/*
1N/A** status in initialization
1N/A** these flags keep track of the status of the initialization
1N/A** i.e., whether a file exists (_EX) and whether it can be used (_OK)
1N/A** [due to permissions]
1N/A*/
1N/A
1N/A# define TLS_S_NONE 0x00000000 /* none yet */
1N/A# define TLS_S_CERT_EX 0x00000001 /* cert file exists */
1N/A# define TLS_S_CERT_OK 0x00000002 /* cert file is ok */
1N/A# define TLS_S_KEY_EX 0x00000004 /* key file exists */
1N/A# define TLS_S_KEY_OK 0x00000008 /* key file is ok */
1N/A# define TLS_S_CERTP_EX 0x00000010 /* CA cert path exists */
1N/A# define TLS_S_CERTP_OK 0x00000020 /* CA cert path is ok */
1N/A# define TLS_S_CERTF_EX 0x00000040 /* CA cert file exists */
1N/A# define TLS_S_CERTF_OK 0x00000080 /* CA cert file is ok */
1N/A# define TLS_S_CRLF_EX 0x00000100 /* CRL file exists */
1N/A# define TLS_S_CRLF_OK 0x00000200 /* CRL file is ok */
1N/A
1N/A# if _FFR_TLS_1
1N/A# define TLS_S_CERT2_EX 0x00001000 /* 2nd cert file exists */
1N/A# define TLS_S_CERT2_OK 0x00002000 /* 2nd cert file is ok */
1N/A# define TLS_S_KEY2_EX 0x00004000 /* 2nd key file exists */
1N/A# define TLS_S_KEY2_OK 0x00008000 /* 2nd key file is ok */
1N/A# endif /* _FFR_TLS_1 */
1N/A
1N/A# define TLS_S_DH_OK 0x00200000 /* DH cert is ok */
1N/A# define TLS_S_DHPAR_EX 0x00400000 /* DH param file exists */
1N/A# define TLS_S_DHPAR_OK 0x00800000 /* DH param file is ok to use */
1N/A
1N/A/* Type of variable */
1N/A# define TLS_T_OTHER 0
1N/A# define TLS_T_SRV 1
1N/A# define TLS_T_CLT 2
1N/A
1N/A/*
1N/A** TLS_OK_F -- can var be an absolute filename?
1N/A**
1N/A** Parameters:
1N/A** var -- filename
1N/A** fn -- what is the filename used for?
1N/A** type -- type of variable
1N/A**
1N/A** Returns:
1N/A** ok?
1N/A*/
1N/A
1N/Astatic bool
1N/Atls_ok_f(var, fn, type)
1N/A char *var;
1N/A char *fn;
1N/A int type;
1N/A{
1N/A /* must be absolute pathname */
1N/A if (var != NULL && *var == '/')
1N/A return true;
1N/A if (LogLevel > 12)
1N/A sm_syslog(LOG_WARNING, NOQID, "STARTTLS: %s%s missing",
1N/A type == TLS_T_SRV ? "Server" :
1N/A (type == TLS_T_CLT ? "Client" : ""), fn);
1N/A return false;
1N/A}
1N/A/*
1N/A** TLS_SAFE_F -- is a file safe to use?
1N/A**
1N/A** Parameters:
1N/A** var -- filename
1N/A** sff -- flags for safefile()
1N/A** srv -- server side?
1N/A**
1N/A** Returns:
1N/A** ok?
1N/A*/
1N/A
1N/Astatic bool
1N/Atls_safe_f(var, sff, srv)
1N/A char *var;
1N/A long sff;
1N/A bool srv;
1N/A{
1N/A int ret;
1N/A
1N/A if ((ret = safefile(var, RunAsUid, RunAsGid, RunAsUserName, sff,
1N/A S_IRUSR, NULL)) == 0)
1N/A return true;
1N/A if (LogLevel > 7)
1N/A sm_syslog(LOG_WARNING, NOQID, "STARTTLS=%s: file %s unsafe: %s",
1N/A srv ? "server" : "client", var, sm_errstring(ret));
1N/A return false;
1N/A}
1N/A
1N/A/*
1N/A** TLS_OK_F -- macro to simplify calls to tls_ok_f
1N/A**
1N/A** Parameters:
1N/A** var -- filename
1N/A** fn -- what is the filename used for?
1N/A** req -- is the file required?
1N/A** st -- status bit to set if ok
1N/A** type -- type of variable
1N/A**
1N/A** Side Effects:
1N/A** uses r, ok; may change ok and status.
1N/A**
1N/A*/
1N/A
1N/A# define TLS_OK_F(var, fn, req, st, type) if (ok) \
1N/A { \
1N/A r = tls_ok_f(var, fn, type); \
1N/A if (r) \
1N/A status |= st; \
1N/A else if (req) \
1N/A ok = false; \
1N/A }
1N/A
1N/A/*
1N/A** TLS_UNR -- macro to return whether a file should be unreadable
1N/A**
1N/A** Parameters:
1N/A** bit -- flag to test
1N/A** req -- flags
1N/A**
1N/A** Returns:
1N/A** 0/SFF_NORFILES
1N/A*/
1N/A# define TLS_UNR(bit, req) (bitset(bit, req) ? SFF_NORFILES : 0)
1N/A# define TLS_OUNR(bit, req) (bitset(bit, req) ? SFF_NOWRFILES : 0)
1N/A# define TLS_KEYSFF(req) \
1N/A (bitnset(DBS_GROUPREADABLEKEYFILE, DontBlameSendmail) ? \
1N/A TLS_OUNR(TLS_I_KEY_OUNR, req) : \
1N/A TLS_UNR(TLS_I_KEY_UNR, req))
1N/A
1N/A/*
1N/A** TLS_SAFE_F -- macro to simplify calls to tls_safe_f
1N/A**
1N/A** Parameters:
1N/A** var -- filename
1N/A** sff -- flags for safefile()
1N/A** req -- is the file required?
1N/A** ex -- does the file exist?
1N/A** st -- status bit to set if ok
1N/A** srv -- server side?
1N/A**
1N/A** Side Effects:
1N/A** uses r, ok, ex; may change ok and status.
1N/A**
1N/A*/
1N/A
1N/A# define TLS_SAFE_F(var, sff, req, ex, st, srv) if (ex && ok) \
1N/A { \
1N/A r = tls_safe_f(var, sff, srv); \
1N/A if (r) \
1N/A status |= st; \
1N/A else if (req) \
1N/A ok = false; \
1N/A }
1N/A
1N/A/*
1N/A** INITTLS -- initialize TLS
1N/A**
1N/A** Parameters:
1N/A** ctx -- pointer to context
1N/A** req -- requirements for initialization (see sendmail.h)
1N/A** options -- options
1N/A** srv -- server side?
1N/A** certfile -- filename of certificate
1N/A** keyfile -- filename of private key
1N/A** cacertpath -- path to CAs
1N/A** cacertfile -- file with CA(s)
1N/A** dhparam -- parameters for DH
1N/A**
1N/A** Returns:
1N/A** succeeded?
1N/A*/
1N/A
1N/A/*
1N/A** The session_id_context identifies the service that created a session.
1N/A** This information is used to distinguish between multiple TLS-based
1N/A** servers running on the same server. We use the name of the mail system.
1N/A** Note: the session cache is not persistent.
1N/A*/
1N/A
1N/Astatic char server_session_id_context[] = "sendmail8";
1N/A
1N/A/* 0.9.8a and b have a problem with SSL_OP_TLS_BLOCK_PADDING_BUG */
1N/A#if (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
1N/A# define SM_SSL_OP_TLS_BLOCK_PADDING_BUG 1
1N/A#else
1N/A# define SM_SSL_OP_TLS_BLOCK_PADDING_BUG 0
1N/A#endif
1N/A
1N/Abool
1N/Ainittls(ctx, req, options, srv, certfile, keyfile, cacertpath, cacertfile, dhparam)
1N/A SSL_CTX **ctx;
1N/A unsigned long req;
1N/A long options;
1N/A bool srv;
1N/A char *certfile, *keyfile, *cacertpath, *cacertfile, *dhparam;
1N/A{
1N/A# if !NO_DH
1N/A static DH *dh = NULL;
1N/A# endif /* !NO_DH */
1N/A int r;
1N/A bool ok;
1N/A long sff, status;
1N/A char *who;
1N/A# if _FFR_TLS_1
1N/A char *cf2, *kf2;
1N/A# endif /* _FFR_TLS_1 */
1N/A# if SM_CONF_SHM
1N/A extern int ShmId;
1N/A# endif /* SM_CONF_SHM */
1N/A# if OPENSSL_VERSION_NUMBER > 0x00907000L
1N/A BIO *crl_file;
1N/A X509_CRL *crl;
1N/A X509_STORE *store;
1N/A# endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
1N/A#if SM_SSL_OP_TLS_BLOCK_PADDING_BUG
1N/A long rt_version;
1N/A STACK_OF(SSL_COMP) *comp_methods;
1N/A#endif
1N/A
1N/A status = TLS_S_NONE;
1N/A who = srv ? "server" : "client";
1N/A if (ctx == NULL)
1N/A {
1N/A syserr("STARTTLS=%s, inittls: ctx == NULL", who);
1N/A /* NOTREACHED */
1N/A SM_ASSERT(ctx != NULL);
1N/A }
1N/A
1N/A /* already initialized? (we could re-init...) */
1N/A if (*ctx != NULL)
1N/A return true;
1N/A ok = true;
1N/A
1N/A# if _FFR_TLS_1
1N/A /*
1N/A ** look for a second filename: it must be separated by a ','
1N/A ** no blanks allowed (they won't be skipped).
1N/A ** we change a global variable here! this change will be undone
1N/A ** before return from the function but only if it returns true.
1N/A ** this isn't a problem since in a failure case this function
1N/A ** won't be called again with the same (overwritten) values.
1N/A ** otherwise each return must be replaced with a goto endinittls.
1N/A */
1N/A
1N/A cf2 = NULL;
1N/A kf2 = NULL;
1N/A if (certfile != NULL && (cf2 = strchr(certfile, ',')) != NULL)
1N/A {
1N/A *cf2++ = '\0';
1N/A if (keyfile != NULL && (kf2 = strchr(keyfile, ',')) != NULL)
1N/A *kf2++ = '\0';
1N/A }
1N/A# endif /* _FFR_TLS_1 */
1N/A
1N/A /*
1N/A ** Check whether files/paths are defined
1N/A */
1N/A
1N/A TLS_OK_F(certfile, "CertFile", bitset(TLS_I_CERT_EX, req),
1N/A TLS_S_CERT_EX, srv ? TLS_T_SRV : TLS_T_CLT);
1N/A TLS_OK_F(keyfile, "KeyFile", bitset(TLS_I_KEY_EX, req),
1N/A TLS_S_KEY_EX, srv ? TLS_T_SRV : TLS_T_CLT);
1N/A TLS_OK_F(cacertpath, "CACertPath", bitset(TLS_I_CERTP_EX, req),
1N/A TLS_S_CERTP_EX, TLS_T_OTHER);
1N/A TLS_OK_F(cacertfile, "CACertFile", bitset(TLS_I_CERTF_EX, req),
1N/A TLS_S_CERTF_EX, TLS_T_OTHER);
1N/A
1N/A# if OPENSSL_VERSION_NUMBER > 0x00907000L
1N/A TLS_OK_F(CRLFile, "CRLFile", bitset(TLS_I_CRLF_EX, req),
1N/A TLS_S_CRLF_EX, TLS_T_OTHER);
1N/A# endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
1N/A
1N/A# if _FFR_TLS_1
1N/A /*
1N/A ** if the second file is specified it must exist
1N/A ** XXX: it is possible here to define only one of those files
1N/A */
1N/A
1N/A if (cf2 != NULL)
1N/A {
1N/A TLS_OK_F(cf2, "CertFile", bitset(TLS_I_CERT_EX, req),
1N/A TLS_S_CERT2_EX, srv ? TLS_T_SRV : TLS_T_CLT);
1N/A }
1N/A if (kf2 != NULL)
1N/A {
1N/A TLS_OK_F(kf2, "KeyFile", bitset(TLS_I_KEY_EX, req),
1N/A TLS_S_KEY2_EX, srv ? TLS_T_SRV : TLS_T_CLT);
1N/A }
1N/A# endif /* _FFR_TLS_1 */
1N/A
1N/A /*
1N/A ** valid values for dhparam are (only the first char is checked)
1N/A ** none no parameters: don't use DH
1N/A ** 512 generate 512 bit parameters (fixed)
1N/A ** 1024 generate 1024 bit parameters
1N/A ** /file/name read parameters from /file/name
1N/A ** default is: 1024 for server, 512 for client (OK? XXX)
1N/A */
1N/A
1N/A if (bitset(TLS_I_TRY_DH, req))
1N/A {
1N/A if (dhparam != NULL)
1N/A {
1N/A char c = *dhparam;
1N/A
1N/A if (c == '1')
1N/A req |= TLS_I_DH1024;
1N/A else if (c == '5')
1N/A req |= TLS_I_DH512;
1N/A else if (c != 'n' && c != 'N' && c != '/')
1N/A {
1N/A if (LogLevel > 12)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: illegal value '%s' for DHParam",
1N/A who, dhparam);
1N/A dhparam = NULL;
1N/A }
1N/A }
1N/A if (dhparam == NULL)
1N/A {
1N/A dhparam = srv ? "1" : "5";
1N/A req |= (srv ? TLS_I_DH1024 : TLS_I_DH512);
1N/A }
1N/A else if (*dhparam == '/')
1N/A {
1N/A TLS_OK_F(dhparam, "DHParameters",
1N/A bitset(TLS_I_DHPAR_EX, req),
1N/A TLS_S_DHPAR_EX, TLS_T_OTHER);
1N/A }
1N/A }
1N/A if (!ok)
1N/A return ok;
1N/A
1N/A /* certfile etc. must be "safe". */
1N/A sff = SFF_REGONLY | SFF_SAFEDIRPATH | SFF_NOWLINK
1N/A | SFF_NOGWFILES | SFF_NOWWFILES
1N/A | SFF_MUSTOWN | SFF_ROOTOK | SFF_OPENASROOT;
1N/A if (DontLockReadFiles)
1N/A sff |= SFF_NOLOCK;
1N/A
1N/A TLS_SAFE_F(certfile, sff | TLS_UNR(TLS_I_CERT_UNR, req),
1N/A bitset(TLS_I_CERT_EX, req),
1N/A bitset(TLS_S_CERT_EX, status), TLS_S_CERT_OK, srv);
1N/A TLS_SAFE_F(keyfile, sff | TLS_KEYSFF(req),
1N/A bitset(TLS_I_KEY_EX, req),
1N/A bitset(TLS_S_KEY_EX, status), TLS_S_KEY_OK, srv);
1N/A TLS_SAFE_F(cacertfile, sff | TLS_UNR(TLS_I_CERTF_UNR, req),
1N/A bitset(TLS_I_CERTF_EX, req),
1N/A bitset(TLS_S_CERTF_EX, status), TLS_S_CERTF_OK, srv);
1N/A TLS_SAFE_F(dhparam, sff | TLS_UNR(TLS_I_DHPAR_UNR, req),
1N/A bitset(TLS_I_DHPAR_EX, req),
1N/A bitset(TLS_S_DHPAR_EX, status), TLS_S_DHPAR_OK, srv);
1N/A# if OPENSSL_VERSION_NUMBER > 0x00907000L
1N/A TLS_SAFE_F(CRLFile, sff | TLS_UNR(TLS_I_CRLF_UNR, req),
1N/A bitset(TLS_I_CRLF_EX, req),
1N/A bitset(TLS_S_CRLF_EX, status), TLS_S_CRLF_OK, srv);
1N/A# endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
1N/A if (!ok)
1N/A return ok;
1N/A# if _FFR_TLS_1
1N/A if (cf2 != NULL)
1N/A {
1N/A TLS_SAFE_F(cf2, sff | TLS_UNR(TLS_I_CERT_UNR, req),
1N/A bitset(TLS_I_CERT_EX, req),
1N/A bitset(TLS_S_CERT2_EX, status), TLS_S_CERT2_OK, srv);
1N/A }
1N/A if (kf2 != NULL)
1N/A {
1N/A TLS_SAFE_F(kf2, sff | TLS_KEYSFF(req),
1N/A bitset(TLS_I_KEY_EX, req),
1N/A bitset(TLS_S_KEY2_EX, status), TLS_S_KEY2_OK, srv);
1N/A }
1N/A# endif /* _FFR_TLS_1 */
1N/A
1N/A /* create a method and a new context */
1N/A if ((*ctx = SSL_CTX_new(srv ? SSLv23_server_method() :
1N/A SSLv23_client_method())) == NULL)
1N/A {
1N/A if (LogLevel > 7)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: SSL_CTX_new(SSLv23_%s_method()) failed",
1N/A who, who);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A return false;
1N/A }
1N/A
1N/A# if OPENSSL_VERSION_NUMBER > 0x00907000L
1N/A if (CRLFile != NULL)
1N/A {
1N/A /* get a pointer to the current certificate validation store */
1N/A store = SSL_CTX_get_cert_store(*ctx); /* does not fail */
1N/A crl_file = BIO_new(BIO_s_file_internal());
1N/A if (crl_file != NULL)
1N/A {
1N/A if (BIO_read_filename(crl_file, CRLFile) >= 0)
1N/A {
1N/A crl = PEM_read_bio_X509_CRL(crl_file, NULL,
1N/A NULL, NULL);
1N/A BIO_free(crl_file);
1N/A X509_STORE_add_crl(store, crl);
1N/A X509_CRL_free(crl);
1N/A X509_STORE_set_flags(store,
1N/A X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
1N/A X509_STORE_set_verify_cb_func(store,
1N/A x509_verify_cb);
1N/A }
1N/A else
1N/A {
1N/A if (LogLevel > 9)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: PEM_read_bio_X509_CRL(%s)=failed",
1N/A who, CRLFile);
1N/A }
1N/A
1N/A /* avoid memory leaks */
1N/A BIO_free(crl_file);
1N/A return false;
1N/A }
1N/A
1N/A }
1N/A else if (LogLevel > 9)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: BIO_new=failed", who);
1N/A }
1N/A else
1N/A store = NULL;
1N/A# if _FFR_CRLPATH
1N/A if (CRLPath != NULL && store != NULL)
1N/A {
1N/A X509_LOOKUP *lookup;
1N/A
1N/A lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
1N/A if (lookup == NULL)
1N/A {
1N/A if (LogLevel > 9)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: X509_STORE_add_lookup(hash)=failed",
1N/A who, CRLFile);
1N/A }
1N/A return false;
1N/A }
1N/A X509_LOOKUP_add_dir(lookup, CRLPath, X509_FILETYPE_PEM);
1N/A X509_STORE_set_flags(store,
1N/A X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
1N/A }
1N/A# endif /* _FFR_CRLPATH */
1N/A# endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
1N/A
1N/A# if TLS_NO_RSA
1N/A /* turn off backward compatibility, required for no-rsa */
1N/A SSL_CTX_set_options(*ctx, SSL_OP_NO_SSLv2);
1N/A# endif /* TLS_NO_RSA */
1N/A
1N/A
1N/A# if !TLS_NO_RSA
1N/A /*
1N/A ** Create a temporary RSA key
1N/A ** XXX Maybe we shouldn't create this always (even though it
1N/A ** is only at startup).
1N/A ** It is a time-consuming operation and it is not always necessary.
1N/A ** maybe we should do it only on demand...
1N/A */
1N/A
1N/A if (bitset(TLS_I_RSA_TMP, req)
1N/A# if SM_CONF_SHM
1N/A && ShmId != SM_SHM_NO_ID &&
1N/A (rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL,
1N/A NULL)) == NULL
1N/A# else /* SM_CONF_SHM */
1N/A && 0 /* no shared memory: no need to generate key now */
1N/A# endif /* SM_CONF_SHM */
1N/A )
1N/A {
1N/A if (LogLevel > 7)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: RSA_generate_key failed",
1N/A who);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A return false;
1N/A }
1N/A# endif /* !TLS_NO_RSA */
1N/A
1N/A /*
1N/A ** load private key
1N/A ** XXX change this for DSA-only version
1N/A */
1N/A
1N/A if (bitset(TLS_S_KEY_OK, status) &&
1N/A SSL_CTX_use_PrivateKey_file(*ctx, keyfile,
1N/A SSL_FILETYPE_PEM) <= 0)
1N/A {
1N/A if (LogLevel > 7)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed",
1N/A who, keyfile);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A if (bitset(TLS_I_USE_KEY, req))
1N/A return false;
1N/A }
1N/A
1N/A /* get the certificate file */
1N/A if (bitset(TLS_S_CERT_OK, status) &&
1N/A SSL_CTX_use_certificate_file(*ctx, certfile,
1N/A SSL_FILETYPE_PEM) <= 0)
1N/A {
1N/A if (LogLevel > 7)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: SSL_CTX_use_certificate_file(%s) failed",
1N/A who, certfile);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A if (bitset(TLS_I_USE_CERT, req))
1N/A return false;
1N/A }
1N/A
1N/A /* check the private key */
1N/A if (bitset(TLS_S_KEY_OK, status) &&
1N/A (r = SSL_CTX_check_private_key(*ctx)) <= 0)
1N/A {
1N/A /* Private key does not match the certificate public key */
1N/A if (LogLevel > 5)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: SSL_CTX_check_private_key failed(%s): %d",
1N/A who, keyfile, r);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A if (bitset(TLS_I_USE_KEY, req))
1N/A return false;
1N/A }
1N/A
1N/A# if _FFR_TLS_1
1N/A /* XXX this code is pretty much duplicated from above! */
1N/A
1N/A /* load private key */
1N/A if (bitset(TLS_S_KEY2_OK, status) &&
1N/A SSL_CTX_use_PrivateKey_file(*ctx, kf2, SSL_FILETYPE_PEM) <= 0)
1N/A {
1N/A if (LogLevel > 7)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: SSL_CTX_use_PrivateKey_file(%s) failed",
1N/A who, kf2);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A }
1N/A
1N/A /* get the certificate file */
1N/A if (bitset(TLS_S_CERT2_OK, status) &&
1N/A SSL_CTX_use_certificate_file(*ctx, cf2, SSL_FILETYPE_PEM) <= 0)
1N/A {
1N/A if (LogLevel > 7)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: SSL_CTX_use_certificate_file(%s) failed",
1N/A who, cf2);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A }
1N/A
1N/A /* also check the private key */
1N/A if (bitset(TLS_S_KEY2_OK, status) &&
1N/A (r = SSL_CTX_check_private_key(*ctx)) <= 0)
1N/A {
1N/A /* Private key does not match the certificate public key */
1N/A if (LogLevel > 5)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: SSL_CTX_check_private_key 2 failed: %d",
1N/A who, r);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A }
1N/A# endif /* _FFR_TLS_1 */
1N/A
1N/A /* SSL_CTX_set_quiet_shutdown(*ctx, 1); violation of standard? */
1N/A
1N/A#if SM_SSL_OP_TLS_BLOCK_PADDING_BUG
1N/A
1N/A /*
1N/A ** In OpenSSL 0.9.8[ab], enabling zlib compression breaks the
1N/A ** padding bug work-around, leading to false positives and
1N/A ** failed connections. We may not interoperate with systems
1N/A ** with the bug, but this is better than breaking on all 0.9.8[ab]
1N/A ** systems that have zlib support enabled.
1N/A ** Note: this checks the runtime version of the library, not
1N/A ** just the compile time version.
1N/A */
1N/A
1N/A rt_version = SSLeay();
1N/A if (rt_version >= 0x00908000L && rt_version <= 0x0090802fL)
1N/A {
1N/A comp_methods = SSL_COMP_get_compression_methods();
1N/A if (comp_methods != NULL && sk_SSL_COMP_num(comp_methods) > 0)
1N/A options &= ~SSL_OP_TLS_BLOCK_PADDING_BUG;
1N/A }
1N/A#endif
1N/A SSL_CTX_set_options(*ctx, options);
1N/A
1N/A# if !NO_DH
1N/A /* Diffie-Hellman initialization */
1N/A if (bitset(TLS_I_TRY_DH, req))
1N/A {
1N/A if (bitset(TLS_S_DHPAR_OK, status))
1N/A {
1N/A BIO *bio;
1N/A
1N/A if ((bio = BIO_new_file(dhparam, "r")) != NULL)
1N/A {
1N/A dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1N/A BIO_free(bio);
1N/A if (dh == NULL && LogLevel > 7)
1N/A {
1N/A unsigned long err;
1N/A
1N/A err = ERR_get_error();
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: cannot read DH parameters(%s): %s",
1N/A who, dhparam,
1N/A ERR_error_string(err, NULL));
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A }
1N/A else
1N/A {
1N/A if (LogLevel > 5)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: BIO_new_file(%s) failed",
1N/A who, dhparam);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A }
1N/A }
1N/A if (dh == NULL && bitset(TLS_I_DH1024, req))
1N/A {
1N/A DSA *dsa;
1N/A
1N/A /* this takes a while! (7-130s on a 450MHz AMD K6-2) */
1N/A dsa = DSA_generate_parameters(1024, NULL, 0, NULL,
1N/A NULL, 0, NULL);
1N/A dh = DSA_dup_DH(dsa);
1N/A DSA_free(dsa);
1N/A }
1N/A else
1N/A if (dh == NULL && bitset(TLS_I_DH512, req))
1N/A dh = get_dh512();
1N/A
1N/A if (dh == NULL)
1N/A {
1N/A if (LogLevel > 9)
1N/A {
1N/A unsigned long err;
1N/A
1N/A err = ERR_get_error();
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: cannot read or set DH parameters(%s): %s",
1N/A who, dhparam,
1N/A ERR_error_string(err, NULL));
1N/A }
1N/A if (bitset(TLS_I_REQ_DH, req))
1N/A return false;
1N/A }
1N/A else
1N/A {
1N/A SSL_CTX_set_tmp_dh(*ctx, dh);
1N/A
1N/A /* important to avoid small subgroup attacks */
1N/A SSL_CTX_set_options(*ctx, SSL_OP_SINGLE_DH_USE);
1N/A if (LogLevel > 13)
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "STARTTLS=%s, Diffie-Hellman init, key=%d bit (%c)",
1N/A who, 8 * DH_size(dh), *dhparam);
1N/A DH_free(dh);
1N/A }
1N/A }
1N/A# endif /* !NO_DH */
1N/A
1N/A
1N/A /* XXX do we need this cache here? */
1N/A if (bitset(TLS_I_CACHE, req))
1N/A {
1N/A SSL_CTX_sess_set_cache_size(*ctx, 1);
1N/A SSL_CTX_set_timeout(*ctx, 1);
1N/A SSL_CTX_set_session_id_context(*ctx,
1N/A (void *) &server_session_id_context,
1N/A sizeof(server_session_id_context));
1N/A (void) SSL_CTX_set_session_cache_mode(*ctx,
1N/A SSL_SESS_CACHE_SERVER);
1N/A }
1N/A else
1N/A {
1N/A (void) SSL_CTX_set_session_cache_mode(*ctx,
1N/A SSL_SESS_CACHE_OFF);
1N/A }
1N/A
1N/A /* load certificate locations and default CA paths */
1N/A if (bitset(TLS_S_CERTP_EX, status) && bitset(TLS_S_CERTF_EX, status))
1N/A {
1N/A if ((r = SSL_CTX_load_verify_locations(*ctx, cacertfile,
1N/A cacertpath)) == 1)
1N/A {
1N/A# if !TLS_NO_RSA
1N/A if (bitset(TLS_I_RSA_TMP, req))
1N/A SSL_CTX_set_tmp_rsa_callback(*ctx, tmp_rsa_key);
1N/A# endif /* !TLS_NO_RSA */
1N/A
1N/A /*
1N/A ** We have to install our own verify callback:
1N/A ** SSL_VERIFY_PEER requests a client cert but even
1N/A ** though *FAIL_IF* isn't set, the connection
1N/A ** will be aborted if the client presents a cert
1N/A ** that is not "liked" (can't be verified?) by
1N/A ** the TLS library :-(
1N/A */
1N/A
1N/A /*
1N/A ** XXX currently we could call tls_set_verify()
1N/A ** but we hope that that function will later on
1N/A ** only set the mode per connection.
1N/A */
1N/A SSL_CTX_set_verify(*ctx,
1N/A bitset(TLS_I_NO_VRFY, req) ? SSL_VERIFY_NONE
1N/A : SSL_VERIFY_PEER,
1N/A NULL);
1N/A
1N/A /* install verify callback */
1N/A SSL_CTX_set_cert_verify_callback(*ctx, tls_verify_cb,
1N/A NULL);
1N/A SSL_CTX_set_client_CA_list(*ctx,
1N/A SSL_load_client_CA_file(cacertfile));
1N/A }
1N/A else
1N/A {
1N/A /*
1N/A ** can't load CA data; do we care?
1N/A ** the data is necessary to authenticate the client,
1N/A ** which in turn would be necessary
1N/A ** if we want to allow relaying based on it.
1N/A */
1N/A if (LogLevel > 5)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: load verify locs %s, %s failed: %d",
1N/A who, cacertpath, cacertfile, r);
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A if (bitset(TLS_I_VRFY_LOC, req))
1N/A return false;
1N/A }
1N/A }
1N/A
1N/A /* XXX: make this dependent on an option? */
1N/A if (tTd(96, 9))
1N/A SSL_CTX_set_info_callback(*ctx, apps_ssl_info_cb);
1N/A
1N/A# if _FFR_TLS_1
1N/A /* install our own cipher list */
1N/A if (CipherList != NULL && *CipherList != '\0')
1N/A {
1N/A if (SSL_CTX_set_cipher_list(*ctx, CipherList) <= 0)
1N/A {
1N/A if (LogLevel > 7)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, error: SSL_CTX_set_cipher_list(%s) failed, list ignored",
1N/A who, CipherList);
1N/A
1N/A if (LogLevel > 9)
1N/A tlslogerr(who);
1N/A }
1N/A /* failure if setting to this list is required? */
1N/A }
1N/A }
1N/A# endif /* _FFR_TLS_1 */
1N/A if (LogLevel > 12)
1N/A sm_syslog(LOG_INFO, NOQID, "STARTTLS=%s, init=%d", who, ok);
1N/A
1N/A# if _FFR_TLS_1
1N/A# if 0
1N/A /*
1N/A ** this label is required if we want to have a "clean" exit
1N/A ** see the comments above at the initialization of cf2
1N/A */
1N/A
1N/A endinittls:
1N/A# endif /* 0 */
1N/A
1N/A /* undo damage to global variables */
1N/A if (cf2 != NULL)
1N/A *--cf2 = ',';
1N/A if (kf2 != NULL)
1N/A *--kf2 = ',';
1N/A# endif /* _FFR_TLS_1 */
1N/A
1N/A return ok;
1N/A}
1N/A/*
1N/A** TLS_GET_INFO -- get information about TLS connection
1N/A**
1N/A** Parameters:
1N/A** ssl -- TLS connection structure
1N/A** srv -- server or client
1N/A** host -- hostname of other side
1N/A** mac -- macro storage
1N/A** certreq -- did we ask for a cert?
1N/A**
1N/A** Returns:
1N/A** result of authentication.
1N/A**
1N/A** Side Effects:
1N/A** sets macros: {cipher}, {tls_version}, {verify},
1N/A** {cipher_bits}, {alg_bits}, {cert}, {cert_subject},
1N/A** {cert_issuer}, {cn_subject}, {cn_issuer}
1N/A*/
1N/A
1N/Aint
1N/Atls_get_info(ssl, srv, host, mac, certreq)
1N/A SSL *ssl;
1N/A bool srv;
1N/A char *host;
1N/A MACROS_T *mac;
1N/A bool certreq;
1N/A{
1N/A const SSL_CIPHER *c;
1N/A int b, r;
1N/A long verifyok;
1N/A char *s, *who;
1N/A char bitstr[16];
1N/A X509 *cert;
1N/A
1N/A c = SSL_get_current_cipher(ssl);
1N/A
1N/A /* cast is just workaround for compiler warning */
1N/A macdefine(mac, A_TEMP, macid("{cipher}"),
1N/A (char *) SSL_CIPHER_get_name(c));
1N/A b = SSL_CIPHER_get_bits(c, &r);
1N/A (void) sm_snprintf(bitstr, sizeof(bitstr), "%d", b);
1N/A macdefine(mac, A_TEMP, macid("{cipher_bits}"), bitstr);
1N/A (void) sm_snprintf(bitstr, sizeof(bitstr), "%d", r);
1N/A macdefine(mac, A_TEMP, macid("{alg_bits}"), bitstr);
1N/A s = SSL_CIPHER_get_version(c);
1N/A if (s == NULL)
1N/A s = "UNKNOWN";
1N/A macdefine(mac, A_TEMP, macid("{tls_version}"), s);
1N/A
1N/A who = srv ? "server" : "client";
1N/A cert = SSL_get_peer_certificate(ssl);
1N/A verifyok = SSL_get_verify_result(ssl);
1N/A if (LogLevel > 14)
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "STARTTLS=%s, get_verify: %ld get_peer: 0x%lx",
1N/A who, verifyok, (unsigned long) cert);
1N/A if (cert != NULL)
1N/A {
1N/A unsigned int n;
1N/A X509_NAME *subj, *issuer;
1N/A unsigned char md[EVP_MAX_MD_SIZE];
1N/A char buf[MAXNAME];
1N/A
1N/A subj = X509_get_subject_name(cert);
1N/A issuer = X509_get_issuer_name(cert);
1N/A X509_NAME_oneline(subj, buf, sizeof(buf));
1N/A macdefine(mac, A_TEMP, macid("{cert_subject}"),
1N/A xtextify(buf, "<>\")"));
1N/A X509_NAME_oneline(issuer, buf, sizeof(buf));
1N/A macdefine(mac, A_TEMP, macid("{cert_issuer}"),
1N/A xtextify(buf, "<>\")"));
1N/A
1N/A# define LL_BADCERT 8
1N/A
1N/A#define CHECK_X509_NAME(which) \
1N/A do { \
1N/A if (r == -1) \
1N/A { \
1N/A sm_strlcpy(buf, "BadCertificateUnknown", sizeof(buf)); \
1N/A if (LogLevel > LL_BADCERT) \
1N/A sm_syslog(LOG_INFO, NOQID, \
1N/A "STARTTLS=%s, relay=%.100s, field=%s, status=failed to extract CN", \
1N/A who, \
1N/A host == NULL ? "local" : host, \
1N/A which); \
1N/A } \
1N/A else if ((size_t)r >= sizeof(buf) - 1) \
1N/A { \
1N/A sm_strlcpy(buf, "BadCertificateTooLong", sizeof(buf)); \
1N/A if (LogLevel > 7) \
1N/A sm_syslog(LOG_INFO, NOQID, \
1N/A "STARTTLS=%s, relay=%.100s, field=%s, status=CN too long", \
1N/A who, \
1N/A host == NULL ? "local" : host, \
1N/A which); \
1N/A } \
1N/A else if ((size_t)r > strlen(buf)) \
1N/A { \
1N/A sm_strlcpy(buf, "BadCertificateContainsNUL", \
1N/A sizeof(buf)); \
1N/A if (LogLevel > 7) \
1N/A sm_syslog(LOG_INFO, NOQID, \
1N/A "STARTTLS=%s, relay=%.100s, field=%s, status=CN contains NUL", \
1N/A who, \
1N/A host == NULL ? "local" : host, \
1N/A which); \
1N/A } \
1N/A } while (0)
1N/A
1N/A r = X509_NAME_get_text_by_NID(subj, NID_commonName, buf,
1N/A sizeof buf);
1N/A CHECK_X509_NAME("cn_subject");
1N/A macdefine(mac, A_TEMP, macid("{cn_subject}"),
1N/A xtextify(buf, "<>\")"));
1N/A r = X509_NAME_get_text_by_NID(issuer, NID_commonName, buf,
1N/A sizeof buf);
1N/A CHECK_X509_NAME("cn_issuer");
1N/A macdefine(mac, A_TEMP, macid("{cn_issuer}"),
1N/A xtextify(buf, "<>\")"));
1N/A n = 0;
1N/A if (X509_digest(cert, EVP_md5(), md, &n) != 0 && n > 0)
1N/A {
1N/A char md5h[EVP_MAX_MD_SIZE * 3];
1N/A static const char hexcodes[] = "0123456789ABCDEF";
1N/A
1N/A SM_ASSERT((n * 3) + 2 < sizeof(md5h));
1N/A for (r = 0; r < (int) n; r++)
1N/A {
1N/A md5h[r * 3] = hexcodes[(md[r] & 0xf0) >> 4];
1N/A md5h[(r * 3) + 1] = hexcodes[(md[r] & 0x0f)];
1N/A md5h[(r * 3) + 2] = ':';
1N/A }
1N/A md5h[(n * 3) - 1] = '\0';
1N/A macdefine(mac, A_TEMP, macid("{cert_md5}"), md5h);
1N/A }
1N/A else
1N/A macdefine(mac, A_TEMP, macid("{cert_md5}"), "");
1N/A }
1N/A else
1N/A {
1N/A macdefine(mac, A_PERM, macid("{cert_subject}"), "");
1N/A macdefine(mac, A_PERM, macid("{cert_issuer}"), "");
1N/A macdefine(mac, A_PERM, macid("{cn_subject}"), "");
1N/A macdefine(mac, A_PERM, macid("{cn_issuer}"), "");
1N/A macdefine(mac, A_TEMP, macid("{cert_md5}"), "");
1N/A }
1N/A switch (verifyok)
1N/A {
1N/A case X509_V_OK:
1N/A if (cert != NULL)
1N/A {
1N/A s = "OK";
1N/A r = TLS_AUTH_OK;
1N/A }
1N/A else
1N/A {
1N/A s = certreq ? "NO" : "NOT",
1N/A r = TLS_AUTH_NO;
1N/A }
1N/A break;
1N/A default:
1N/A s = "FAIL";
1N/A r = TLS_AUTH_FAIL;
1N/A break;
1N/A }
1N/A macdefine(mac, A_PERM, macid("{verify}"), s);
1N/A if (cert != NULL)
1N/A X509_free(cert);
1N/A
1N/A /* do some logging */
1N/A if (LogLevel > 8)
1N/A {
1N/A char *vers, *s1, *s2, *cbits, *algbits;
1N/A
1N/A vers = macget(mac, macid("{tls_version}"));
1N/A cbits = macget(mac, macid("{cipher_bits}"));
1N/A algbits = macget(mac, macid("{alg_bits}"));
1N/A s1 = macget(mac, macid("{verify}"));
1N/A s2 = macget(mac, macid("{cipher}"));
1N/A
1N/A /* XXX: maybe cut off ident info? */
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "STARTTLS=%s, relay=%.100s, version=%.16s, verify=%.16s, cipher=%.64s, bits=%.6s/%.6s",
1N/A who,
1N/A host == NULL ? "local" : host,
1N/A vers, s1, s2, /* sm_snprintf() can deal with NULL */
1N/A algbits == NULL ? "0" : algbits,
1N/A cbits == NULL ? "0" : cbits);
1N/A if (LogLevel > 11)
1N/A {
1N/A /*
1N/A ** Maybe run xuntextify on the strings?
1N/A ** That is easier to read but makes it maybe a bit
1N/A ** more complicated to figure out the right values
1N/A ** for the access map...
1N/A */
1N/A
1N/A s1 = macget(mac, macid("{cert_subject}"));
1N/A s2 = macget(mac, macid("{cert_issuer}"));
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "STARTTLS=%s, cert-subject=%.256s, cert-issuer=%.256s, verifymsg=%s",
1N/A who, s1, s2,
1N/A X509_verify_cert_error_string(verifyok));
1N/A }
1N/A }
1N/A return r;
1N/A}
1N/A/*
1N/A** ENDTLS -- shutdown secure connection
1N/A**
1N/A** Parameters:
1N/A** ssl -- SSL connection information.
1N/A** side -- server/client (for logging).
1N/A**
1N/A** Returns:
1N/A** success? (EX_* code)
1N/A*/
1N/A
1N/Aint
1N/Aendtls(ssl, side)
1N/A SSL *ssl;
1N/A char *side;
1N/A{
1N/A int ret = EX_OK;
1N/A
1N/A if (ssl != NULL)
1N/A {
1N/A int r;
1N/A
1N/A if ((r = SSL_shutdown(ssl)) < 0)
1N/A {
1N/A if (LogLevel > 11)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, SSL_shutdown failed: %d",
1N/A side, r);
1N/A tlslogerr(side);
1N/A }
1N/A ret = EX_SOFTWARE;
1N/A }
1N/A# if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL
1N/A
1N/A /*
1N/A ** Bug in OpenSSL (at least up to 0.9.6b):
1N/A ** From: Lutz.Jaenicke@aet.TU-Cottbus.DE
1N/A ** Message-ID: <20010723152244.A13122@serv01.aet.tu-cottbus.de>
1N/A ** To: openssl-users@openssl.org
1N/A ** Subject: Re: SSL_shutdown() woes (fwd)
1N/A **
1N/A ** The side sending the shutdown alert first will
1N/A ** not care about the answer of the peer but will
1N/A ** immediately return with a return value of "0"
1N/A ** (ssl/s3_lib.c:ssl3_shutdown()). SSL_get_error will evaluate
1N/A ** the value of "0" and as the shutdown alert of the peer was
1N/A ** not received (actually, the program did not even wait for
1N/A ** the answer), an SSL_ERROR_SYSCALL is flagged, because this
1N/A ** is the default rule in case everything else does not apply.
1N/A **
1N/A ** For your server the problem is different, because it
1N/A ** receives the shutdown first (setting SSL_RECEIVED_SHUTDOWN),
1N/A ** then sends its response (SSL_SENT_SHUTDOWN), so for the
1N/A ** server the shutdown was successfull.
1N/A **
1N/A ** As is by know, you would have to call SSL_shutdown() once
1N/A ** and ignore an SSL_ERROR_SYSCALL returned. Then call
1N/A ** SSL_shutdown() again to actually get the server's response.
1N/A **
1N/A ** In the last discussion, Bodo Moeller concluded that a
1N/A ** rewrite of the shutdown code would be necessary, but
1N/A ** probably with another API, as the change would not be
1N/A ** compatible to the way it is now. Things do not become
1N/A ** easier as other programs do not follow the shutdown
1N/A ** guidelines anyway, so that a lot error conditions and
1N/A ** compitibility issues would have to be caught.
1N/A **
1N/A ** For now the recommondation is to ignore the error message.
1N/A */
1N/A
1N/A else if (r == 0)
1N/A {
1N/A if (LogLevel > 15)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s, SSL_shutdown not done",
1N/A side);
1N/A tlslogerr(side);
1N/A }
1N/A ret = EX_SOFTWARE;
1N/A }
1N/A# endif /* !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER > 0x0090602fL */
1N/A SSL_free(ssl);
1N/A ssl = NULL;
1N/A }
1N/A return ret;
1N/A}
1N/A
1N/A# if !TLS_NO_RSA
1N/A/*
1N/A** TMP_RSA_KEY -- return temporary RSA key
1N/A**
1N/A** Parameters:
1N/A** s -- TLS connection structure
1N/A** export --
1N/A** keylength --
1N/A**
1N/A** Returns:
1N/A** temporary RSA key.
1N/A*/
1N/A
1N/A# ifndef MAX_RSA_TMP_CNT
1N/A# define MAX_RSA_TMP_CNT 1000 /* XXX better value? */
1N/A# endif /* ! MAX_RSA_TMP_CNT */
1N/A
1N/A/* ARGUSED0 */
1N/Astatic RSA *
1N/Atmp_rsa_key(s, export, keylength)
1N/A SSL *s;
1N/A int export;
1N/A int keylength;
1N/A{
1N/A# if SM_CONF_SHM
1N/A extern int ShmId;
1N/A extern int *PRSATmpCnt;
1N/A
1N/A if (ShmId != SM_SHM_NO_ID && rsa_tmp != NULL &&
1N/A ++(*PRSATmpCnt) < MAX_RSA_TMP_CNT)
1N/A return rsa_tmp;
1N/A# endif /* SM_CONF_SHM */
1N/A
1N/A if (rsa_tmp != NULL)
1N/A RSA_free(rsa_tmp);
1N/A rsa_tmp = RSA_generate_key(RSA_KEYLENGTH, RSA_F4, NULL, NULL);
1N/A if (rsa_tmp == NULL)
1N/A {
1N/A if (LogLevel > 0)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "STARTTLS=server, tmp_rsa_key: RSA_generate_key failed!");
1N/A }
1N/A else
1N/A {
1N/A# if SM_CONF_SHM
1N/A# if 0
1N/A /*
1N/A ** XXX we can't (yet) share the new key...
1N/A ** The RSA structure contains pointers hence it can't be
1N/A ** easily kept in shared memory. It must be transformed
1N/A ** into a continous memory region first, then stored,
1N/A ** and later read out again (each time re-transformed).
1N/A */
1N/A
1N/A if (ShmId != SM_SHM_NO_ID)
1N/A *PRSATmpCnt = 0;
1N/A# endif /* 0 */
1N/A# endif /* SM_CONF_SHM */
1N/A if (LogLevel > 9)
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "STARTTLS=server, tmp_rsa_key: new temp RSA key");
1N/A }
1N/A return rsa_tmp;
1N/A}
1N/A# endif /* !TLS_NO_RSA */
1N/A/*
1N/A** APPS_SSL_INFO_CB -- info callback for TLS connections
1N/A**
1N/A** Parameters:
1N/A** s -- TLS connection structure
1N/A** where -- state in handshake
1N/A** ret -- return code of last operation
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Astatic void
1N/Aapps_ssl_info_cb(s, where, ret)
1N/A CONST097 SSL *s;
1N/A int where;
1N/A int ret;
1N/A{
1N/A int w;
1N/A char *str;
1N/A BIO *bio_err = NULL;
1N/A
1N/A if (LogLevel > 14)
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "STARTTLS: info_callback where=0x%x, ret=%d",
1N/A where, ret);
1N/A
1N/A w = where & ~SSL_ST_MASK;
1N/A if (bio_err == NULL)
1N/A bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
1N/A
1N/A if (bitset(SSL_ST_CONNECT, w))
1N/A str = "SSL_connect";
1N/A else if (bitset(SSL_ST_ACCEPT, w))
1N/A str = "SSL_accept";
1N/A else
1N/A str = "undefined";
1N/A
1N/A if (bitset(SSL_CB_LOOP, where))
1N/A {
1N/A if (LogLevel > 12)
1N/A sm_syslog(LOG_NOTICE, NOQID,
1N/A "STARTTLS: %s:%s",
1N/A str, SSL_state_string_long(s));
1N/A }
1N/A else if (bitset(SSL_CB_ALERT, where))
1N/A {
1N/A str = bitset(SSL_CB_READ, where) ? "read" : "write";
1N/A if (LogLevel > 12)
1N/A sm_syslog(LOG_NOTICE, NOQID,
1N/A "STARTTLS: SSL3 alert %s:%s:%s",
1N/A str, SSL_alert_type_string_long(ret),
1N/A SSL_alert_desc_string_long(ret));
1N/A }
1N/A else if (bitset(SSL_CB_EXIT, where))
1N/A {
1N/A if (ret == 0)
1N/A {
1N/A if (LogLevel > 7)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS: %s:failed in %s",
1N/A str, SSL_state_string_long(s));
1N/A }
1N/A else if (ret < 0)
1N/A {
1N/A if (LogLevel > 7)
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS: %s:error in %s",
1N/A str, SSL_state_string_long(s));
1N/A }
1N/A }
1N/A}
1N/A/*
1N/A** TLS_VERIFY_LOG -- log verify error for TLS certificates
1N/A**
1N/A** Parameters:
1N/A** ok -- verify ok?
1N/A** ctx -- x509 context
1N/A**
1N/A** Returns:
1N/A** 0 -- fatal error
1N/A** 1 -- ok
1N/A*/
1N/A
1N/Astatic int
1N/Atls_verify_log(ok, ctx, name)
1N/A int ok;
1N/A X509_STORE_CTX *ctx;
1N/A char *name;
1N/A{
1N/A SSL *ssl;
1N/A X509 *cert;
1N/A int reason, depth;
1N/A char buf[512];
1N/A
1N/A cert = X509_STORE_CTX_get_current_cert(ctx);
1N/A reason = X509_STORE_CTX_get_error(ctx);
1N/A depth = X509_STORE_CTX_get_error_depth(ctx);
1N/A ssl = (SSL *) X509_STORE_CTX_get_ex_data(ctx,
1N/A SSL_get_ex_data_X509_STORE_CTX_idx());
1N/A
1N/A if (ssl == NULL)
1N/A {
1N/A /* internal error */
1N/A sm_syslog(LOG_ERR, NOQID,
1N/A "STARTTLS: internal error: tls_verify_cb: ssl == NULL");
1N/A return 0;
1N/A }
1N/A
1N/A X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf));
1N/A sm_syslog(LOG_INFO, NOQID,
1N/A "STARTTLS: %s cert verify: depth=%d %s, state=%d, reason=%s",
1N/A name, depth, buf, ok, X509_verify_cert_error_string(reason));
1N/A return 1;
1N/A}
1N/A
1N/A/*
1N/A** TLS_VERIFY_CB -- verify callback for TLS certificates
1N/A**
1N/A** Parameters:
1N/A** ctx -- x509 context
1N/A**
1N/A** Returns:
1N/A** accept connection?
1N/A** currently: always yes.
1N/A*/
1N/A
1N/Astatic int
1N/A# if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x00907000L
1N/Atls_verify_cb(ctx)
1N/A X509_STORE_CTX *ctx;
1N/A# else /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
1N/Atls_verify_cb(ctx, unused)
1N/A X509_STORE_CTX *ctx;
1N/A void *unused;
1N/A# endif /* !defined() || OPENSSL_VERSION_NUMBER < 0x00907000L */
1N/A{
1N/A int ok;
1N/A
1N/A /*
1N/A ** man SSL_CTX_set_cert_verify_callback():
1N/A ** callback should return 1 to indicate verification success
1N/A ** and 0 to indicate verification failure.
1N/A */
1N/A
1N/A ok = X509_verify_cert(ctx);
1N/A if (ok <= 0)
1N/A {
1N/A if (LogLevel > 13)
1N/A return tls_verify_log(ok, ctx, "TLS");
1N/A }
1N/A return 1;
1N/A}
1N/A/*
1N/A** TLSLOGERR -- log the errors from the TLS error stack
1N/A**
1N/A** Parameters:
1N/A** who -- server/client (for logging).
1N/A**
1N/A** Returns:
1N/A** none.
1N/A*/
1N/A
1N/Avoid
1N/Atlslogerr(who)
1N/A const char *who;
1N/A{
1N/A unsigned long l;
1N/A int line, flags;
1N/A unsigned long es;
1N/A char *file, *data;
1N/A char buf[256];
1N/A# define CP (const char **)
1N/A
1N/A es = CRYPTO_thread_id();
1N/A while ((l = ERR_get_error_line_data(CP &file, &line, CP &data, &flags))
1N/A != 0)
1N/A {
1N/A sm_syslog(LOG_WARNING, NOQID,
1N/A "STARTTLS=%s: %lu:%s:%s:%d:%s", who, es,
1N/A ERR_error_string(l, buf),
1N/A file, line,
1N/A bitset(ERR_TXT_STRING, flags) ? data : "");
1N/A }
1N/A}
1N/A
1N/A# if OPENSSL_VERSION_NUMBER > 0x00907000L
1N/A/*
1N/A** X509_VERIFY_CB -- verify callback
1N/A**
1N/A** Parameters:
1N/A** ctx -- x509 context
1N/A**
1N/A** Returns:
1N/A** accept connection?
1N/A** currently: always yes.
1N/A*/
1N/A
1N/Astatic int
1N/Ax509_verify_cb(ok, ctx)
1N/A int ok;
1N/A X509_STORE_CTX *ctx;
1N/A{
1N/A if (ok == 0)
1N/A {
1N/A if (LogLevel > 13)
1N/A tls_verify_log(ok, ctx, "x509");
1N/A if (ctx->error == X509_V_ERR_UNABLE_TO_GET_CRL)
1N/A {
1N/A ctx->error = 0;
1N/A return 1; /* override it */
1N/A }
1N/A }
1N/A return ok;
1N/A}
1N/A# endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */
1N/A#endif /* STARTTLS */