Home | History | Annotate | Line # | Download | only in tls
      1 /*	$NetBSD: tls.h,v 1.7 2026/05/09 18:49:21 christos Exp $	*/
      2 
      3 #ifndef _TLS_H_INCLUDED_
      4 #define _TLS_H_INCLUDED_
      5 
      6 /*++
      7 /* NAME
      8 /*	tls 3h
      9 /* SUMMARY
     10 /*	libtls internal interfaces
     11 /* SYNOPSIS
     12 /*	#include <tls.h>
     13 /* DESCRIPTION
     14 /* .nf
     15 
     16  /*
     17   * Utility library.
     18   */
     19 #include <name_code.h>
     20 #include <argv.h>
     21 
     22  /*
     23   * TLS enforcement levels. Non-sentinel values may also be used to indicate
     24   * the actual security level of a session.
     25   *
     26   * XXX TLS_LEV_NOTFOUND no longer belongs in this list. The SMTP client will
     27   * have to use something else to report that policy table lookup failed.
     28   *
     29   * The order of levels matters, but we hide most of the details in macros.
     30   *
     31   * "dane" vs. "fingerprint", both must lie between "encrypt" and "verify".
     32   *
     33   * - With "may" and higher, TLS is enabled.
     34   *
     35   * - With "encrypt" and higher, TLS encryption must be applied.
     36   *
     37   * - Strictly above "encrypt", the peer certificate must match.
     38   *
     39   * - At "dane" and higher, the peer certificate must also be trusted. With
     40   * "dane" the trust may be self-asserted, so we only log trust verification
     41   * errors when TA associations are involved.
     42   */
     43 #define TLS_LEV_INVALID		-2	/* sentinel */
     44 #define TLS_LEV_NOTFOUND	-1	/* XXX not in policy table */
     45 #define TLS_LEV_NONE		0	/* plain-text only */
     46 #define TLS_LEV_MAY		1	/* wildcard */
     47 #define TLS_LEV_ENCRYPT		2	/* encrypted connection */
     48 #define TLS_LEV_FPRINT		3	/* "peer" CA-less verification */
     49 #define TLS_LEV_HALF_DANE	4	/* DANE TLSA MX host, insecure MX RR */
     50 #define TLS_LEV_DANE		5	/* Opportunistic TLSA policy */
     51 #define TLS_LEV_DANE_ONLY	6	/* Required TLSA policy */
     52 #define TLS_LEV_VERIFY		7	/* certificate verified */
     53 #define TLS_LEV_SECURE		8	/* "secure" verification */
     54 
     55 #define TLS_REQUIRED_BY_SECURITY_LEVEL(l) \
     56 				((l) > TLS_LEV_MAY)
     57 #define TLS_MUST_MATCH(l)	((l) > TLS_LEV_ENCRYPT)
     58 #define TLS_MUST_PKIX(l)	((l) >= TLS_LEV_VERIFY)
     59 #define TLS_OPPORTUNISTIC(l)	((l) == TLS_LEV_MAY || (l) == TLS_LEV_DANE)
     60 #define TLS_DANE_BASED(l)	\
     61 	((l) >= TLS_LEV_HALF_DANE && (l) <= TLS_LEV_DANE_ONLY)
     62 #define TLS_NEVER_SECURED(l)	((l) == TLS_LEV_HALF_DANE)
     63 
     64 extern int tls_level_lookup(const char *);
     65 extern const char *str_tls_level(int);
     66 
     67 #ifdef USE_TLS
     68 
     69  /*
     70   * OpenSSL library.
     71   */
     72 #include <openssl/lhash.h>
     73 #include <openssl/bn.h>
     74 #include <openssl/err.h>
     75 #include <openssl/pem.h>
     76 #include <openssl/x509.h>
     77 #include <openssl/x509v3.h>
     78 #include <openssl/rand.h>
     79 #include <openssl/crypto.h>		/* Legacy SSLEAY_VERSION_NUMBER */
     80 #include <openssl/evp.h>		/* New OpenSSL 3.0 EVP_PKEY APIs */
     81 #include <openssl/opensslv.h>		/* OPENSSL_VERSION_NUMBER */
     82 #include <openssl/ssl.h>
     83 #include <openssl/conf.h>
     84 #include <openssl/tls1.h>		/* TLS extensions */
     85 
     86  /* Appease indent(1) */
     87 #define x509_stack_t STACK_OF(X509)
     88 #define general_name_stack_t STACK_OF(GENERAL_NAME)
     89 #define ssl_cipher_stack_t STACK_OF(SSL_CIPHER)
     90 #define ssl_comp_stack_t STACK_OF(SSL_COMP)
     91 
     92 /*-
     93  * Official way to check minimum OpenSSL API version from 3.0 onward.
     94  * We simply define it false for all prior versions, where we typically also
     95  * need the patch level to determine API compatibility.
     96  */
     97 #ifndef OPENSSL_VERSION_PREREQ
     98 #define OPENSSL_VERSION_PREREQ(m,n) 0
     99 #endif
    100 
    101 #if (OPENSSL_VERSION_NUMBER < 0x1010100fUL)
    102 #error "OpenSSL releases prior to 1.1.1 are no longer supported"
    103 #endif
    104 
    105  /*-
    106   * Backwards compatibility with OpenSSL < 1.1.1a.
    107   *
    108   * In OpenSSL 1.1.1a the client-only interface SSL_get_server_tmp_key() was
    109   * updated to work on both the client and the server, and was renamed to
    110   * SSL_get_peer_tmp_key(), with the original name left behind as an alias.  We
    111   * use the new name when available.
    112   */
    113 #if OPENSSL_VERSION_NUMBER < 0x1010101fUL
    114 #undef SSL_get_signature_nid
    115 #define SSL_get_signature_nid(ssl, pnid) (NID_undef)
    116 #define tls_get_peer_dh_pubkey SSL_get_server_tmp_key
    117 #else
    118 #define tls_get_peer_dh_pubkey SSL_get_peer_tmp_key
    119 #endif
    120 
    121 #if OPENSSL_VERSION_PREREQ(3,0)
    122 #define TLS_PEEK_PEER_CERT(ssl) SSL_get0_peer_certificate(ssl)
    123 #define TLS_FREE_PEER_CERT(x)   ((void) 0)
    124 #define tls_set_bio_callback    BIO_set_callback_ex
    125 #else
    126 #define TLS_PEEK_PEER_CERT(ssl) SSL_get_peer_certificate(ssl)
    127 #define TLS_FREE_PEER_CERT(x)   X509_free(x)
    128 #define tls_set_bio_callback    BIO_set_callback
    129 #endif
    130 
    131 #if OPENSSL_VERSION_PREREQ(3,2)
    132 #define TLS_GROUP_NAME(ssl) SSL_get0_group_name(ssl)
    133 #elif OPENSSL_VERSION_PREREQ(3,0)
    134 #define TLS_GROUP_NAME(ssl) \
    135     SSL_group_to_name((ssl), SSL_get_negotiated_group(ssl))
    136 #else
    137 #define TLS_GROUP_NAME(ssl) ((const char *)0)
    138 #endif
    139 
    140 #if OPENSSL_VERSION_PREREQ(4,0)
    141 #define TLS_ADD1_HOST   SSL_add1_dnsname
    142 #define TLS_SET1_HOST   SSL_set1_dnsname
    143 #else
    144 #define TLS_ADD1_HOST   SSL_add1_host
    145 #define TLS_SET1_HOST   SSL_set1_host
    146 #endif
    147 
    148  /*
    149   * Utility library.
    150   */
    151 #include <vstream.h>
    152 #include <name_mask.h>
    153 #include <name_code.h>
    154 
    155  /*
    156   * TLS library.
    157   */
    158 #include <dns.h>
    159 
    160  /*
    161   * TLS role, presently for logging.
    162   */
    163 typedef enum {
    164     TLS_ROLE_CLIENT, TLS_ROLE_SERVER,
    165 } TLS_ROLE;
    166 
    167 typedef enum {
    168     TLS_USAGE_NEW, TLS_USAGE_USED,
    169 } TLS_USAGE;
    170 
    171  /*
    172   * Names of valid tlsmgr(8) session caches.
    173   */
    174 #define TLS_MGR_SCACHE_SMTPD	"smtpd"
    175 #define TLS_MGR_SCACHE_SMTP	"smtp"
    176 #define TLS_MGR_SCACHE_LMTP	"lmtp"
    177 
    178  /*
    179   * RFC 6698, 7671, 7672 DANE
    180   */
    181 #define TLS_DANE_TA	0		/* Match trust-anchor digests */
    182 #define TLS_DANE_EE	1		/* Match end-entity digests */
    183 
    184 #define TLS_DANE_CERT	0		/* Match the certificate digest */
    185 #define TLS_DANE_PKEY	1		/* Match the public key digest */
    186 
    187 #define TLS_DANE_FLAG_NORRS	(1<<0)	/* Nothing found in DNS */
    188 #define TLS_DANE_FLAG_EMPTY	(1<<1)	/* Nothing usable found in DNS */
    189 #define TLS_DANE_FLAG_ERROR	(1<<2)	/* TLSA record lookup error */
    190 
    191 #define tls_dane_unusable(dane)	((dane)->flags & TLS_DANE_FLAG_EMPTY)
    192 #define tls_dane_notfound(dane)	((dane)->flags & TLS_DANE_FLAG_NORRS)
    193 
    194 #define TLS_DANE_CACHE_TTL_MIN 1	/* A lot can happen in ~2 seconds */
    195 #define TLS_DANE_CACHE_TTL_MAX 100	/* Comparable to max_idle */
    196 
    197  /*
    198   * Certificate and public key digests (typically from TLSA RRs), grouped by
    199   * algorithm.
    200   */
    201 typedef struct TLS_TLSA {
    202     uint8_t usage;			/* DANE certificate usage */
    203     uint8_t selector;			/* DANE selector */
    204     uint8_t mtype;			/* Algorithm for this digest list */
    205     uint16_t length;			/* Length of associated data */
    206     unsigned char *data;		/* Associated data */
    207     struct TLS_TLSA *next;		/* Chain to next algorithm */
    208 } TLS_TLSA;
    209 
    210 typedef struct TLS_DANE {
    211     TLS_TLSA *tlsa;			/* TLSA records */
    212     char   *base_domain;		/* Base domain of TLSA RRset */
    213     int     flags;			/* Lookup status */
    214     time_t  expires;			/* Expiration time of this record */
    215     int     refs;			/* Reference count */
    216 } TLS_DANE;
    217 
    218  /*
    219   * tls_dane.c
    220   */
    221 extern int tls_dane_avail(void);
    222 extern void tls_dane_loglevel(const char *, const char *);
    223 extern void tls_dane_flush(void);
    224 extern TLS_DANE *tls_dane_alloc(void);
    225 extern void tls_tlsa_free(TLS_TLSA *);
    226 extern void tls_dane_free(TLS_DANE *);
    227 extern void tls_dane_add_fpt_digests(TLS_DANE *, int, const char *,
    228 				             const char *, int);
    229 extern TLS_DANE *tls_dane_resolve(unsigned, const char *, DNS_RR *, int);
    230 extern int tls_dane_load_trustfile(TLS_DANE *, const char *);
    231 
    232  /*
    233   * TLS session context, also used by the VSTREAM call-back routines for SMTP
    234   * input/output, and by OpenSSL call-back routines for key verification.
    235   *
    236   * Only some members are (read-only) accessible by the public.
    237   */
    238 #define CCERT_BUFSIZ	256
    239 
    240 typedef struct {
    241     /* Public, read-only. */
    242     char   *peer_CN;			/* Peer Common Name */
    243     char   *issuer_CN;			/* Issuer Common Name */
    244     char   *peer_sni;			/* SNI sent to or by the peer */
    245     char   *peer_cert_fprint;		/* ASCII certificate fingerprint */
    246     char   *peer_pkey_fprint;		/* ASCII public key fingerprint */
    247     int     level;			/* Effective security level */
    248     int     peer_status;		/* Certificate and match status */
    249     const char *protocol;
    250     const char *cipher_name;
    251     int     cipher_usebits;
    252     int     cipher_algbits;
    253     const char *kex_name;		/* shared key-exchange algorithm */
    254     const char *kex_curve;		/* shared key-exchange ECDHE curve */
    255     int     kex_bits;			/* shared FFDHE key exchange bits */
    256     int     ctos_rpk;			/* Did the client send an RPK? */
    257     int     stoc_rpk;			/* Did the server send an RPK? */
    258     const char *clnt_sig_name;		/* client's signature key algorithm */
    259     const char *clnt_sig_curve;		/* client's ECDSA curve name */
    260     int     clnt_sig_bits;		/* client's RSA signature key bits */
    261     const char *clnt_sig_dgst;		/* client's signature digest */
    262     const char *srvr_sig_name;		/* server's signature key algorithm */
    263     const char *srvr_sig_curve;		/* server's ECDSA curve name */
    264     int     srvr_sig_bits;		/* server's RSA signature key bits */
    265     const char *srvr_sig_dgst;		/* server's signature digest */
    266     int     rpt_reported;		/* Failure was reported with TLSRPT */
    267     /* Private. */
    268     SSL    *con;
    269     char   *cache_type;			/* tlsmgr(8) cache type if enabled */
    270     int     ticketed;			/* Issued (server) or cached (client) */
    271     char   *serverid;			/* unique server identifier */
    272     char   *namaddr;			/* nam[addr] for logging */
    273     int     log_mask;			/* What to log */
    274     int     session_reused;		/* this session was reused */
    275     int     am_server;			/* Are we an SSL server or client? */
    276     const char *mdalg;			/* default message digest algorithm */
    277     /* Built-in vs external SSL_accept/read/write/shutdown support. */
    278     VSTREAM *stream;			/* Blocking-mode SMTP session */
    279     /* DANE TLSA trust input and verification state */
    280     const TLS_DANE *dane;		/* DANE TLSA digests */
    281     const X509 *errorcert;		/* Error certificate closest to leaf */
    282     int     errordepth;			/* Chain depth of error cert */
    283     int     errorcode;			/* First error at error depth */
    284     int     must_fail;			/* Failed to load trust settings */
    285     char   *ffail_type;			/* Forced verification failure */
    286     /* End of Private members. */
    287 } TLS_SESS_STATE;
    288 
    289  /*
    290   * Peer status bits. TLS_CERT_FLAG_MATCHED implies TLS_CERT_FLAG_TRUSTED
    291   * only in the case of a hostname match.
    292   */
    293 #define TLS_CRED_FLAG_CERT		(1<<0)
    294 #define TLS_CERT_FLAG_ALTNAME		(1<<1)
    295 #define TLS_CERT_FLAG_TRUSTED		(1<<2)
    296 #define TLS_CERT_FLAG_MATCHED		(1<<3)
    297 #define TLS_CERT_FLAG_SECURED		(1<<4)
    298 #define TLS_CRED_FLAG_RPK		(1<<5)
    299 #define TLS_CRED_FLAG_ANY	(TLS_CRED_FLAG_CERT|TLS_CRED_FLAG_RPK)
    300 
    301 #define TLS_CRED_IS_PRESENT(c) ((c) && ((c)->peer_status&TLS_CRED_FLAG_ANY))
    302 #define TLS_CERT_IS_PRESENT(c) ((c) && ((c)->peer_status&TLS_CRED_FLAG_CERT))
    303 #define TLS_RPK_IS_PRESENT(c)  ((c) && ((c)->peer_status&TLS_CRED_FLAG_RPK))
    304 #define TLS_CERT_IS_ALTNAME(c) ((c) && ((c)->peer_status&TLS_CERT_FLAG_ALTNAME))
    305 #define TLS_CERT_IS_TRUSTED(c) ((c) && ((c)->peer_status&TLS_CERT_FLAG_TRUSTED))
    306 #define TLS_CERT_IS_MATCHED(c) ((c) && ((c)->peer_status&TLS_CERT_FLAG_MATCHED))
    307 #define TLS_CERT_IS_SECURED(c) ((c) && ((c)->peer_status&TLS_CERT_FLAG_SECURED))
    308 
    309  /*
    310   * Opaque client context handle.
    311   */
    312 typedef struct TLS_APPL_STATE TLS_APPL_STATE;
    313 
    314 #ifdef TLS_INTERNAL
    315 
    316  /*
    317   * Log mask details are internal to the library.
    318   */
    319 extern int tls_log_mask(const char *, const char *);
    320 
    321  /*
    322   * What to log.
    323   */
    324 #define TLS_LOG_NONE			(1<<0)
    325 #define TLS_LOG_SUMMARY			(1<<1)
    326 #define TLS_LOG_UNTRUSTED		(1<<2)
    327 #define TLS_LOG_PEERCERT		(1<<3)
    328 #define TLS_LOG_CERTMATCH		(1<<4)
    329 #define TLS_LOG_VERBOSE			(1<<5)
    330 #define TLS_LOG_CACHE			(1<<6)
    331 #define TLS_LOG_DEBUG			(1<<7)
    332 #define TLS_LOG_TLSPKTS			(1<<8)
    333 #define TLS_LOG_ALLPKTS			(1<<9)
    334 #define TLS_LOG_DANE			(1<<10)
    335 
    336  /*
    337   * Client and Server application contexts
    338   */
    339 struct TLS_APPL_STATE {
    340     SSL_CTX *ssl_ctx;
    341     SSL_CTX *sni_ctx;
    342     int     log_mask;
    343     char   *cache_type;
    344 };
    345 
    346  /*
    347   * tls_misc.c Application-context update and disposal.
    348   */
    349 extern void tls_update_app_logmask(TLS_APPL_STATE *, int);
    350 extern void tls_free_app_context(TLS_APPL_STATE *);
    351 
    352  /*
    353   * tls_misc.c
    354   */
    355 extern void tls_param_init(void);
    356 extern int tls_library_init(void);
    357 
    358  /*
    359   * Protocol selection.
    360   */
    361 #define TLS_PROTOCOL_INVALID	(~0)	/* All protocol bits masked */
    362 
    363 #ifdef SSL_TXT_SSLV2
    364 #define TLS_PROTOCOL_SSLv2	(1<<0)	/* SSLv2 */
    365 #else
    366 #define SSL_TXT_SSLV2		"SSLv2"
    367 #define TLS_PROTOCOL_SSLv2	0	/* Unknown */
    368 #undef  SSL_OP_NO_SSLv2
    369 #define SSL_OP_NO_SSLv2		0L	/* Noop */
    370 #endif
    371 
    372 #ifdef SSL_TXT_SSLV3
    373 #define TLS_PROTOCOL_SSLv3	(1<<1)	/* SSLv3 */
    374 #else
    375 #define SSL_TXT_SSLV3		"SSLv3"
    376 #define TLS_PROTOCOL_SSLv3	0	/* Unknown */
    377 #undef  SSL_OP_NO_SSLv3
    378 #define SSL_OP_NO_SSLv3		0L	/* Noop */
    379 #endif
    380 
    381 #ifdef SSL_TXT_TLSV1
    382 #define TLS_PROTOCOL_TLSv1	(1<<2)	/* TLSv1 */
    383 #else
    384 #define SSL_TXT_TLSV1		"TLSv1"
    385 #define TLS_PROTOCOL_TLSv1	0	/* Unknown */
    386 #undef  SSL_OP_NO_TLSv1
    387 #define SSL_OP_NO_TLSv1		0L	/* Noop */
    388 #endif
    389 
    390 #ifdef SSL_TXT_TLSV1_1
    391 #define TLS_PROTOCOL_TLSv1_1	(1<<3)	/* TLSv1_1 */
    392 #else
    393 #define SSL_TXT_TLSV1_1		"TLSv1.1"
    394 #define TLS_PROTOCOL_TLSv1_1	0	/* Unknown */
    395 #undef  SSL_OP_NO_TLSv1_1
    396 #define SSL_OP_NO_TLSv1_1	0L	/* Noop */
    397 #endif
    398 
    399 #ifdef SSL_TXT_TLSV1_2
    400 #define TLS_PROTOCOL_TLSv1_2	(1<<4)	/* TLSv1_2 */
    401 #else
    402 #define SSL_TXT_TLSV1_2		"TLSv1.2"
    403 #define TLS_PROTOCOL_TLSv1_2	0	/* Unknown */
    404 #undef  SSL_OP_NO_TLSv1_2
    405 #define SSL_OP_NO_TLSv1_2	0L	/* Noop */
    406 #endif
    407 
    408  /*
    409   * OpenSSL 1.1.1 does not define a TXT macro for TLS 1.3, so we roll our
    410   * own.
    411   */
    412 #define TLS_PROTOCOL_TXT_TLSV1_3	"TLSv1.3"
    413 
    414 #if defined(TLS1_3_VERSION) && defined(SSL_OP_NO_TLSv1_3)
    415 #define TLS_PROTOCOL_TLSv1_3	(1<<5)	/* TLSv1_3 */
    416 #else
    417 #define TLS_PROTOCOL_TLSv1_3	0	/* Unknown */
    418 #undef  SSL_OP_NO_TLSv1_3
    419 #define SSL_OP_NO_TLSv1_3	0L	/* Noop */
    420 #endif
    421 
    422 /*
    423  * Always used when defined, SMTP has no truncation attacks.
    424  */
    425 #ifndef SSL_OP_IGNORE_UNEXPECTED_EOF
    426 #define SSL_OP_IGNORE_UNEXPECTED_EOF    0L
    427 #endif
    428 
    429 #define TLS_KNOWN_PROTOCOLS \
    430 	( TLS_PROTOCOL_SSLv2 | TLS_PROTOCOL_SSLv3 | TLS_PROTOCOL_TLSv1 \
    431 	   | TLS_PROTOCOL_TLSv1_1 | TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3 )
    432 #define TLS_SSL_OP_PROTOMASK(m) \
    433 	    ((((m) & TLS_PROTOCOL_SSLv2) ? SSL_OP_NO_SSLv2 : 0L) \
    434 	     | (((m) & TLS_PROTOCOL_SSLv3) ? SSL_OP_NO_SSLv3 : 0L) \
    435 	     | (((m) & TLS_PROTOCOL_TLSv1) ? SSL_OP_NO_TLSv1 : 0L) \
    436 	     | (((m) & TLS_PROTOCOL_TLSv1_1) ? SSL_OP_NO_TLSv1_1 : 0L) \
    437 	     | (((m) & TLS_PROTOCOL_TLSv1_2) ? SSL_OP_NO_TLSv1_2 : 0L) \
    438 	     | (((m) & TLS_PROTOCOL_TLSv1_3) ? SSL_OP_NO_TLSv1_3 : 0L))
    439 
    440 /*
    441  * SSL options that are managed via dedicated Postfix features, rather than
    442  * just exposed via hex codes or named elements of tls_ssl_options.
    443  */
    444 #define TLS_SSL_OP_MANAGED_BITS \
    445 	(SSL_OP_CIPHER_SERVER_PREFERENCE | SSL_OP_IGNORE_UNEXPECTED_EOF | \
    446 	 TLS_SSL_OP_PROTOMASK(~0))
    447 
    448 extern int tls_proto_mask_lims(const char *, int *, int *);
    449 
    450  /*
    451   * Cipher grade selection.
    452   */
    453 #define TLS_CIPHER_NONE		0
    454 #define TLS_CIPHER_NULL		1
    455 #define TLS_CIPHER_EXPORT	2
    456 #define TLS_CIPHER_LOW		3
    457 #define TLS_CIPHER_MEDIUM	4
    458 #define TLS_CIPHER_HIGH		5
    459 
    460 extern const NAME_CODE tls_cipher_grade_table[];
    461 
    462 #define tls_cipher_grade(str) \
    463     name_code(tls_cipher_grade_table, NAME_CODE_FLAG_NONE, (str))
    464 #define str_tls_cipher_grade(gr) \
    465     str_name_code(tls_cipher_grade_table, (gr))
    466 
    467  /*
    468   * Cipher lists with exclusions.
    469   */
    470 extern const char *tls_set_ciphers(TLS_SESS_STATE *, const char *,
    471 				           const char *);
    472 
    473  /*
    474   * Populate TLS context with TLS 1.3-related signature parameters.
    475   */
    476 extern void tls_get_signature_params(TLS_SESS_STATE *);
    477 
    478 #endif					/* TLS_INTERNAL */
    479 
    480  /*
    481   * tls_client.c
    482   */
    483 typedef struct {
    484     const char *log_param;
    485     const char *log_level;
    486     int     verifydepth;
    487     const char *cache_type;
    488     const char *chain_files;
    489     const char *cert_file;
    490     const char *key_file;
    491     const char *dcert_file;
    492     const char *dkey_file;
    493     const char *eccert_file;
    494     const char *eckey_file;
    495     const char *CAfile;
    496     const char *CApath;
    497     const char *mdalg;			/* default message digest algorithm */
    498 } TLS_CLIENT_INIT_PROPS;
    499 
    500 typedef struct {
    501     TLS_APPL_STATE *ctx;
    502     VSTREAM *stream;
    503     int     fd;				/* Event-driven file descriptor */
    504     int     timeout;
    505     int     enable_rpk;			/* Solicit server raw public keys */
    506     int     tls_level;			/* Security level */
    507     const char *nexthop;		/* destination domain */
    508     const char *host;			/* MX hostname */
    509     const char *namaddr;		/* nam[addr] for logging */
    510     const char *sni;			/* optional SNI name when not DANE */
    511     const char *serverid;		/* Session cache key */
    512     const char *helo;			/* Server name from EHLO response */
    513     const char *protocols;		/* Enabled protocols */
    514     const char *cipher_grade;		/* Minimum cipher grade */
    515     const char *cipher_exclusions;	/* Ciphers to exclude */
    516     const ARGV *matchargv;		/* Cert match patterns */
    517     const char *mdalg;			/* default message digest algorithm */
    518     const TLS_DANE *dane;		/* DANE TLSA verification */
    519     struct TLSRPT_WRAPPER *tlsrpt;	/* RFC 8460 reporting */
    520     char   *ffail_type;			/* Forced verification failure */
    521 } TLS_CLIENT_START_PROPS;
    522 
    523 extern TLS_APPL_STATE *tls_client_init(const TLS_CLIENT_INIT_PROPS *);
    524 extern TLS_SESS_STATE *tls_client_start(const TLS_CLIENT_START_PROPS *);
    525 extern TLS_SESS_STATE *tls_client_post_connect(TLS_SESS_STATE *,
    526 				            const TLS_CLIENT_START_PROPS *);
    527 
    528 #define tls_client_stop(ctx, stream, timeout, failure, TLScontext) \
    529 	tls_session_stop(ctx, (stream), (timeout), (failure), (TLScontext))
    530 
    531 #define TLS_CLIENT_INIT_ARGS(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
    532     a10, a11, a12, a13, a14) \
    533     (((props)->a1), ((props)->a2), ((props)->a3), \
    534     ((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
    535     ((props)->a8), ((props)->a9), ((props)->a10), ((props)->a11), \
    536     ((props)->a12), ((props)->a13), ((props)->a14), (props))
    537 
    538 #define TLS_CLIENT_INIT(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
    539     a10, a11, a12, a13, a14) \
    540     tls_client_init(TLS_CLIENT_INIT_ARGS(props, a1, a2, a3, a4, a5, \
    541     a6, a7, a8, a9, a10, a11, a12, a13, a14))
    542 
    543 #define TLS_CLIENT_START(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
    544     a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) \
    545     tls_client_start((((props)->a1), ((props)->a2), ((props)->a3), \
    546     ((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
    547     ((props)->a8), ((props)->a9), ((props)->a10), ((props)->a11), \
    548     ((props)->a12), ((props)->a13), ((props)->a14), ((props)->a15), \
    549     ((props)->a16), ((props)->a17), ((props)->a18), ((props)->a19), \
    550     ((props)->a20), (props)))
    551 
    552  /*
    553   * tls_server.c
    554   */
    555 typedef struct {
    556     const char *log_param;
    557     const char *log_level;
    558     int     verifydepth;
    559     const char *cache_type;
    560     int     set_sessid;
    561     const char *chain_files;
    562     const char *cert_file;
    563     const char *key_file;
    564     const char *dcert_file;
    565     const char *dkey_file;
    566     const char *eccert_file;
    567     const char *eckey_file;
    568     const char *CAfile;
    569     const char *CApath;
    570     const char *protocols;
    571     const char *eecdh_grade;
    572     const char *dh1024_param_file;
    573     const char *dh512_param_file;
    574     int     ask_ccert;
    575     const char *mdalg;			/* default message digest algorithm */
    576 } TLS_SERVER_INIT_PROPS;
    577 
    578 typedef struct {
    579     TLS_APPL_STATE *ctx;		/* TLS application context */
    580     VSTREAM *stream;			/* Client stream */
    581     int     fd;				/* Event-driven file descriptor */
    582     int     timeout;			/* TLS handshake timeout */
    583     int     enable_rpk;			/* Solicit client raw public keys */
    584     int     requirecert;		/* Insist on client cert? */
    585     const char *serverid;		/* Server instance (salt cache key) */
    586     const char *namaddr;		/* Client nam[addr] for logging */
    587     const char *cipher_grade;
    588     const char *cipher_exclusions;
    589     const char *mdalg;			/* default message digest algorithm */
    590 } TLS_SERVER_START_PROPS;
    591 
    592 extern TLS_APPL_STATE *tls_server_init(const TLS_SERVER_INIT_PROPS *);
    593 extern TLS_SESS_STATE *tls_server_start(const TLS_SERVER_START_PROPS *props);
    594 extern TLS_SESS_STATE *tls_server_post_accept(TLS_SESS_STATE *);
    595 
    596 #define tls_server_stop(ctx, stream, timeout, failure, TLScontext) \
    597 	tls_session_stop(ctx, (stream), (timeout), (failure), (TLScontext))
    598 
    599 #define TLS_SERVER_INIT(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
    600     a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) \
    601     tls_server_init((((props)->a1), ((props)->a2), ((props)->a3), \
    602     ((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
    603     ((props)->a8), ((props)->a9), ((props)->a10), ((props)->a11), \
    604     ((props)->a12), ((props)->a13), ((props)->a14), ((props)->a15), \
    605     ((props)->a16), ((props)->a17), ((props)->a18), ((props)->a19), \
    606     ((props)->a20), (props)))
    607 
    608 #define TLS_SERVER_START(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
    609     a10, a11) \
    610     tls_server_start((((props)->a1), ((props)->a2), ((props)->a3), \
    611     ((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
    612     ((props)->a8), ((props)->a9), ((props)->a10), ((props)->a11), \
    613     (props)))
    614 
    615  /*
    616   * tls_session.c
    617   */
    618 extern void tls_session_stop(TLS_APPL_STATE *, VSTREAM *, int, int, TLS_SESS_STATE *);
    619 
    620  /*
    621   * tls_misc.c
    622   */
    623 extern const char *tls_compile_version(void);
    624 extern const char *tls_run_version(void);
    625 extern const char **tls_pkey_algorithms(void);
    626 extern void tls_log_summary(TLS_ROLE, TLS_USAGE, TLS_SESS_STATE *);
    627 extern void tls_pre_jail_init(TLS_ROLE);
    628 
    629 #ifdef TLS_INTERNAL
    630 
    631 #include <vstring.h>
    632 
    633 extern VSTRING *tls_session_passivate(SSL_SESSION *);
    634 extern SSL_SESSION *tls_session_activate(const char *, int);
    635 
    636  /*
    637   * tls_stream.c.
    638   */
    639 extern void tls_stream_start(VSTREAM *, TLS_SESS_STATE *);
    640 extern void tls_stream_stop(VSTREAM *);
    641 
    642  /*
    643   * tls_bio_ops.c: a generic multi-personality driver that retries SSL
    644   * operations until they are satisfied or until a hard error happens.
    645   * Because of its ugly multi-personality user interface we invoke it via
    646   * not-so-ugly single-personality wrappers.
    647   */
    648 extern int tls_bio(int, int, TLS_SESS_STATE *,
    649 		           int (*) (SSL *),	/* handshake */
    650 		           int (*) (SSL *, void *, int),	/* read */
    651 		           int (*) (SSL *, const void *, int),	/* write */
    652 		           void *, int);
    653 
    654 #define tls_bio_connect(fd, timeout, context) \
    655         tls_bio((fd), (timeout), (context), SSL_connect, \
    656 		NULL, NULL, NULL, 0)
    657 #define tls_bio_accept(fd, timeout, context) \
    658         tls_bio((fd), (timeout), (context), SSL_accept, \
    659 		NULL, NULL, NULL, 0)
    660 #define tls_bio_shutdown(fd, timeout, context) \
    661 	tls_bio((fd), (timeout), (context), SSL_shutdown, \
    662 		NULL, NULL, NULL, 0)
    663 #define tls_bio_read(fd, buf, len, timeout, context) \
    664 	tls_bio((fd), (timeout), (context), NULL, \
    665 		SSL_read, NULL, (buf), (len))
    666 #define tls_bio_write(fd, buf, len, timeout, context) \
    667 	tls_bio((fd), (timeout), (context), NULL, \
    668 		NULL, SSL_write, (buf), (len))
    669 
    670  /*
    671   * tls_dh.c
    672   */
    673 extern void tls_set_dh_from_file(const char *);
    674 extern void tls_tmp_dh(SSL_CTX *, int);
    675 extern void tls_auto_groups(SSL_CTX *, const char *, const char *);
    676 
    677  /*
    678   * tls_verify.c
    679   */
    680 extern char *tls_peer_CN(X509 *, const TLS_SESS_STATE *);
    681 extern char *tls_issuer_CN(X509 *, const TLS_SESS_STATE *);
    682 extern int tls_verify_certificate_callback(int, X509_STORE_CTX *);
    683 extern void tls_log_verify_error(TLS_SESS_STATE *, struct TLSRPT_WRAPPER *);
    684 
    685  /*
    686   * tls_dane.c
    687   */
    688 extern void tls_dane_log(TLS_SESS_STATE *);
    689 extern void tls_dane_digest_init(SSL_CTX *, const EVP_MD *);
    690 extern int tls_dane_enable(TLS_SESS_STATE *);
    691 extern TLS_TLSA *tlsa_prepend(TLS_TLSA *, uint8_t, uint8_t, uint8_t,
    692 			              const unsigned char *, uint16_t);
    693 
    694  /*
    695   * tls_fprint.c
    696   */
    697 extern const EVP_MD *tls_digest_byname(const char *, EVP_MD_CTX **);
    698 extern char *tls_digest_encode(const unsigned char *, int);
    699 extern char *tls_cert_fprint(X509 *, const char *);
    700 extern char *tls_pkey_fprint(EVP_PKEY *, const char *);
    701 extern char *tls_serverid_digest(TLS_SESS_STATE *,
    702 		              const TLS_CLIENT_START_PROPS *, const char *);
    703 
    704  /*
    705   * tls_certkey.c
    706   */
    707 extern int tls_set_ca_certificate_info(SSL_CTX *, const char *, const char *);
    708 extern int tls_load_pem_chain(SSL *, const char *, const char *);
    709 extern int tls_set_my_certificate_key_info(SSL_CTX *, /* All */ const char *,
    710 				       /* RSA */ const char *, const char *,
    711 				       /* DSA */ const char *, const char *,
    712 				    /* ECDSA */ const char *, const char *);
    713 
    714  /*
    715   * tls_misc.c
    716   */
    717 extern int TLScontext_index;
    718 
    719 extern TLS_APPL_STATE *tls_alloc_app_context(SSL_CTX *, SSL_CTX *, int);
    720 extern TLS_SESS_STATE *tls_alloc_sess_context(int, const char *);
    721 extern void tls_free_context(TLS_SESS_STATE *);
    722 extern void tls_check_version(void);
    723 extern long tls_bug_bits(void);
    724 extern void tls_print_errors(void);
    725 extern void tls_info_callback(const SSL *, int, int);
    726 
    727 #if OPENSSL_VERSION_PREREQ(3,0)
    728 extern long tls_bio_dump_cb(BIO *, int, const char *, size_t, int, long,
    729 			            int, size_t *);
    730 
    731 #else
    732 extern long tls_bio_dump_cb(BIO *, int, const char *, int, long, long);
    733 
    734 #endif
    735 extern const EVP_MD *tls_validate_digest(const char *);
    736 extern void tls_enable_client_rpk(SSL_CTX *, SSL *);
    737 extern void tls_enable_server_rpk(SSL_CTX *, SSL *);
    738 
    739  /*
    740   * tls_seed.c
    741   */
    742 extern void tls_int_seed(void);
    743 extern int tls_ext_seed(int);
    744 
    745 #endif					/* TLS_INTERNAL */
    746 
    747 /* LICENSE
    748 /* .ad
    749 /* .fi
    750 /*	The Secure Mailer license must be distributed with this software.
    751 /* AUTHOR(S)
    752 /*	Wietse Venema
    753 /*	IBM T.J. Watson Research
    754 /*	P.O. Box 704
    755 /*	Yorktown Heights, NY 10598, USA
    756 /*
    757 /*	Wietse Venema
    758 /*	Google, Inc.
    759 /*	111 8th Avenue
    760 /*	New York, NY 10011, USA
    761 /*
    762 /*	Victor Duchovni
    763 /*	Morgan Stanley
    764 /*--*/
    765 
    766 #endif					/* USE_TLS */
    767 #endif					/* _TLS_H_INCLUDED_ */
    768