xref: /netbsd-src/external/ibm-public/postfix/dist/src/tls/tls.h (revision 9fb66d812c00ebfb445c0b47dea128f32aa6fe96)
1 /*	$NetBSD: tls.h,v 1.3 2020/03/18 19:05: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(l)		((l) > TLS_LEV_MAY)
56 #define TLS_MUST_MATCH(l)	((l) > TLS_LEV_ENCRYPT)
57 #define TLS_MUST_TRUST(l)	((l) >= TLS_LEV_HALF_DANE)
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/opensslv.h>		/* OPENSSL_VERSION_NUMBER */
81 #include <openssl/ssl.h>
82 
83  /* Appease indent(1) */
84 #define x509_stack_t STACK_OF(X509)
85 #define general_name_stack_t STACK_OF(GENERAL_NAME)
86 #define ssl_cipher_stack_t STACK_OF(SSL_CIPHER)
87 #define ssl_comp_stack_t STACK_OF(SSL_COMP)
88 
89 #if (OPENSSL_VERSION_NUMBER < 0x1000200fUL)
90 #error "OpenSSL releases prior to 1.0.2 are no longer supported"
91 #endif
92 
93  /* Backwards compatibility with OpenSSL < 1.1.0 */
94 #if OPENSSL_VERSION_NUMBER < 0x10100000L
95 #define OpenSSL_version_num SSLeay
96 #define OpenSSL_version SSLeay_version
97 #define OPENSSL_VERSION SSLEAY_VERSION
98 #define X509_STORE_up_ref(store) \
99 	CRYPTO_add(&((store)->references), 1, CRYPTO_LOCK_X509)
100 #define X509_up_ref(x) \
101 	CRYPTO_add(&((x)->references), 1, CRYPTO_LOCK_X509)
102 #define EVP_PKEY_up_ref(k) \
103 	CRYPTO_add(&((k)->references), 1, CRYPTO_LOCK_EVP_PKEY)
104 #define X509_STORE_CTX_get0_cert(ctx) ((ctx)->cert)
105 #define X509_STORE_CTX_get0_untrusted(ctx) ((ctx)->untrusted)
106 #define X509_STORE_CTX_set0_untrusted X509_STORE_CTX_set_chain
107 #define X509_STORE_CTX_set0_trusted_stack X509_STORE_CTX_trusted_stack
108 #define ASN1_STRING_get0_data ASN1_STRING_data
109 #define X509_getm_notBefore X509_get_notBefore
110 #define X509_getm_notAfter X509_get_notAfter
111 #define TLS_method SSLv23_method
112 #define TLS_client_method SSLv23_client_method
113 #define TLS_server_method SSLv23_server_method
114 #endif
115 
116  /* Backwards compatibility with OpenSSL < 1.1.1 */
117 #if OPENSSL_VERSION_NUMBER < 0x1010100fUL
118 #define SSL_CTX_set_num_tickets(ctx, num) ((void)0)
119 #endif
120 
121  /*-
122   * Backwards compatibility with OpenSSL < 1.1.1a.
123   *
124   * In OpenSSL 1.1.1a the client-only interface SSL_get_server_tmp_key() was
125   * updated to work on both the client and the server, and was renamed to
126   * SSL_get_peer_tmp_key(), with the original name left behind as an alias.  We
127   * use the new name when available.
128   */
129 #if OPENSSL_VERSION_NUMBER < 0x1010101fUL
130 #undef SSL_get_signature_nid
131 #define SSL_get_signature_nid(ssl, pnid) (NID_undef)
132 #define tls_get_peer_dh_pubkey SSL_get_server_tmp_key
133 #else
134 #define tls_get_peer_dh_pubkey SSL_get_peer_tmp_key
135 #endif
136 
137  /*
138   * Utility library.
139   */
140 #include <vstream.h>
141 #include <name_mask.h>
142 #include <name_code.h>
143 
144  /*
145   * TLS library.
146   */
147 #include <dns.h>
148 
149  /*
150   * TLS role, presently for logging.
151   */
152 typedef enum {
153     TLS_ROLE_CLIENT, TLS_ROLE_SERVER,
154 } TLS_ROLE;
155 
156 typedef enum {
157     TLS_USAGE_NEW, TLS_USAGE_USED,
158 } TLS_USAGE;
159 
160  /*
161   * Names of valid tlsmgr(8) session caches.
162   */
163 #define TLS_MGR_SCACHE_SMTPD	"smtpd"
164 #define TLS_MGR_SCACHE_SMTP	"smtp"
165 #define TLS_MGR_SCACHE_LMTP	"lmtp"
166 
167  /*
168   * RFC 6698, 7671, 7672 DANE
169   */
170 #define TLS_DANE_TA	0		/* Match trust-anchor digests */
171 #define TLS_DANE_EE	1		/* Match end-entity digests */
172 
173 #define TLS_DANE_CERT	0		/* Match the certificate digest */
174 #define TLS_DANE_PKEY	1		/* Match the public key digest */
175 
176 #define TLS_DANE_FLAG_NORRS	(1<<0)	/* Nothing found in DNS */
177 #define TLS_DANE_FLAG_EMPTY	(1<<1)	/* Nothing usable found in DNS */
178 #define TLS_DANE_FLAG_ERROR	(1<<2)	/* TLSA record lookup error */
179 
180 #define tls_dane_unusable(dane)	((dane)->flags & TLS_DANE_FLAG_EMPTY)
181 #define tls_dane_notfound(dane)	((dane)->flags & TLS_DANE_FLAG_NORRS)
182 
183 #define TLS_DANE_CACHE_TTL_MIN 1	/* A lot can happen in ~2 seconds */
184 #define TLS_DANE_CACHE_TTL_MAX 100	/* Comparable to max_idle */
185 
186  /*
187   * Certificate and public key digests (typically from TLSA RRs), grouped by
188   * algorithm.
189   */
190 typedef struct TLS_TLSA {
191     char   *mdalg;			/* Algorithm for this digest list */
192     ARGV   *certs;			/* Complete certificate digests */
193     ARGV   *pkeys;			/* SubjectPublicKeyInfo digests */
194     struct TLS_TLSA *next;		/* Chain to next algorithm */
195 } TLS_TLSA;
196 
197  /*
198   * Linked list of full X509 trust-anchor certs.
199   */
200 typedef struct TLS_CERTS {
201     X509   *cert;
202     struct TLS_CERTS *next;
203 } TLS_CERTS;
204 
205  /*
206   * Linked list of full EVP_PKEY trust-anchor public keys.
207   */
208 typedef struct TLS_PKEYS {
209     EVP_PKEY *pkey;
210     struct TLS_PKEYS *next;
211 } TLS_PKEYS;
212 
213 typedef struct TLS_DANE {
214     TLS_TLSA *ta;			/* Trust-anchor cert/pubkey digests */
215     TLS_TLSA *ee;			/* End-entity cert/pubkey digests */
216     TLS_CERTS *certs;			/* Full trust-anchor certificates */
217     TLS_PKEYS *pkeys;			/* Full trust-anchor public keys */
218     char   *base_domain;		/* Base domain of TLSA RRset */
219     int     flags;			/* Lookup status */
220     time_t  expires;			/* Expiration time of this record */
221     int     refs;			/* Reference count */
222 } TLS_DANE;
223 
224 #define TLS_DANE_HASTA(d)	((d) ? (d)->ta : 0)
225 #define TLS_DANE_HASEE(d)	((d) ? (d)->ee : 0)
226 
227  /*
228   * tls_dane.c
229   */
230 extern int tls_dane_avail(void);
231 extern void tls_dane_flush(void);
232 extern void tls_dane_verbose(int);
233 extern TLS_DANE *tls_dane_alloc(void);
234 extern void tls_dane_add_ee_digests(TLS_DANE *, const char *, const char *,
235 				            const char *);
236 extern void tls_dane_free(TLS_DANE *);
237 extern TLS_DANE *tls_dane_resolve(unsigned, const char *, DNS_RR *, int);
238 extern int tls_dane_load_trustfile(TLS_DANE *, const char *);
239 
240  /*
241   * TLS session context, also used by the VSTREAM call-back routines for SMTP
242   * input/output, and by OpenSSL call-back routines for key verification.
243   *
244   * Only some members are (read-only) accessible by the public.
245   */
246 #define CCERT_BUFSIZ	256
247 
248 typedef struct {
249     /* Public, read-only. */
250     char   *peer_CN;			/* Peer Common Name */
251     char   *issuer_CN;			/* Issuer Common Name */
252     char   *peer_sni;			/* SNI sent to or by the peer */
253     char   *peer_cert_fprint;		/* ASCII certificate fingerprint */
254     char   *peer_pkey_fprint;		/* ASCII public key fingerprint */
255     int     peer_status;		/* Certificate and match status */
256     const char *protocol;
257     const char *cipher_name;
258     int     cipher_usebits;
259     int     cipher_algbits;
260     const char *kex_name;		/* shared key-exchange algorithm */
261     const char *kex_curve;		/* shared key-exchange ECDHE curve */
262     int     kex_bits;			/* shared FFDHE key exchange bits */
263     const char *clnt_sig_name;		/* client's signature key algorithm */
264     const char *clnt_sig_curve;		/* client's ECDSA curve name */
265     int     clnt_sig_bits;		/* client's RSA signature key bits */
266     const char *clnt_sig_dgst;		/* client's signature digest */
267     const char *srvr_sig_name;		/* server's signature key algorithm */
268     const char *srvr_sig_curve;		/* server's ECDSA curve name */
269     int     srvr_sig_bits;		/* server's RSA signature key bits */
270     const char *srvr_sig_dgst;		/* server's signature digest */
271     /* Private. */
272     SSL    *con;
273     char   *cache_type;			/* tlsmgr(8) cache type if enabled */
274     int     ticketed;			/* Session ticket issued */
275     char   *serverid;			/* unique server identifier */
276     char   *namaddr;			/* nam[addr] for logging */
277     int     log_mask;			/* What to log */
278     int     session_reused;		/* this session was reused */
279     int     am_server;			/* Are we an SSL server or client? */
280     const char *mdalg;			/* default message digest algorithm */
281     /* Built-in vs external SSL_accept/read/write/shutdown support. */
282     VSTREAM *stream;			/* Blocking-mode SMTP session */
283     /* DANE TLSA trust input and verification state */
284     const TLS_DANE *dane;		/* DANE TLSA digests */
285     int     errordepth;			/* Chain depth of error cert */
286     int     tadepth;			/* Chain depth of trust anchor */
287     int     errorcode;			/* First error at error depth */
288     X509   *errorcert;			/* Error certificate closest to leaf */
289     x509_stack_t *untrusted;		/* Certificate chain fodder */
290     x509_stack_t *trusted;		/* Internal root CA list */
291 } TLS_SESS_STATE;
292 
293  /*
294   * Peer status bits. TLS_CERT_FLAG_MATCHED implies TLS_CERT_FLAG_TRUSTED
295   * only in the case of a hostname match.
296   */
297 #define TLS_CERT_FLAG_PRESENT		(1<<0)
298 #define TLS_CERT_FLAG_ALTNAME		(1<<1)
299 #define TLS_CERT_FLAG_TRUSTED		(1<<2)
300 #define TLS_CERT_FLAG_MATCHED		(1<<3)
301 #define TLS_CERT_FLAG_SECURED		(1<<4)
302 
303 #define TLS_CERT_IS_PRESENT(c) ((c) && ((c)->peer_status&TLS_CERT_FLAG_PRESENT))
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 
335  /*
336   * Client and Server application contexts
337   */
338 struct TLS_APPL_STATE {
339     SSL_CTX *ssl_ctx;
340     SSL_CTX *sni_ctx;
341     int     log_mask;
342     char   *cache_type;
343 };
344 
345  /*
346   * tls_misc.c Application-context update and disposal.
347   */
348 extern void tls_update_app_logmask(TLS_APPL_STATE *, int);
349 extern void tls_free_app_context(TLS_APPL_STATE *);
350 
351  /*
352   * tls_misc.c
353   */
354 extern void tls_param_init(void);
355 
356  /*
357   * Protocol selection.
358   */
359 #define TLS_PROTOCOL_INVALID	(~0)	/* All protocol bits masked */
360 
361 #ifdef SSL_TXT_SSLV2
362 #define TLS_PROTOCOL_SSLv2	(1<<0)	/* SSLv2 */
363 #else
364 #define SSL_TXT_SSLV2		"SSLv2"
365 #define TLS_PROTOCOL_SSLv2	0	/* Unknown */
366 #undef  SSL_OP_NO_SSLv2
367 #define SSL_OP_NO_SSLv2		0L	/* Noop */
368 #endif
369 
370 #ifdef SSL_TXT_SSLV3
371 #define TLS_PROTOCOL_SSLv3	(1<<1)	/* SSLv3 */
372 #else
373 #define SSL_TXT_SSLV3		"SSLv3"
374 #define TLS_PROTOCOL_SSLv3	0	/* Unknown */
375 #undef  SSL_OP_NO_SSLv3
376 #define SSL_OP_NO_SSLv3		0L	/* Noop */
377 #endif
378 
379 #ifdef SSL_TXT_TLSV1
380 #define TLS_PROTOCOL_TLSv1	(1<<2)	/* TLSv1 */
381 #else
382 #define SSL_TXT_TLSV1		"TLSv1"
383 #define TLS_PROTOCOL_TLSv1	0	/* Unknown */
384 #undef  SSL_OP_NO_TLSv1
385 #define SSL_OP_NO_TLSv1		0L	/* Noop */
386 #endif
387 
388 #ifdef SSL_TXT_TLSV1_1
389 #define TLS_PROTOCOL_TLSv1_1	(1<<3)	/* TLSv1_1 */
390 #else
391 #define SSL_TXT_TLSV1_1		"TLSv1.1"
392 #define TLS_PROTOCOL_TLSv1_1	0	/* Unknown */
393 #undef  SSL_OP_NO_TLSv1_1
394 #define SSL_OP_NO_TLSv1_1	0L	/* Noop */
395 #endif
396 
397 #ifdef SSL_TXT_TLSV1_2
398 #define TLS_PROTOCOL_TLSv1_2	(1<<4)	/* TLSv1_2 */
399 #else
400 #define SSL_TXT_TLSV1_2		"TLSv1.2"
401 #define TLS_PROTOCOL_TLSv1_2	0	/* Unknown */
402 #undef  SSL_OP_NO_TLSv1_2
403 #define SSL_OP_NO_TLSv1_2	0L	/* Noop */
404 #endif
405 
406  /*
407   * OpenSSL 1.1.1 does not define a TXT macro for TLS 1.3, so we roll our
408   * own.
409   */
410 #define TLS_PROTOCOL_TXT_TLSV1_3	"TLSv1.3"
411 
412 #if defined(TLS1_3_VERSION) && defined(SSL_OP_NO_TLSv1_3)
413 #define TLS_PROTOCOL_TLSv1_3	(1<<5)	/* TLSv1_3 */
414 #else
415 #define TLS_PROTOCOL_TLSv1_3	0	/* Unknown */
416 #undef  SSL_OP_NO_TLSv1_3
417 #define SSL_OP_NO_TLSv1_3	0L	/* Noop */
418 #endif
419 
420 #define TLS_KNOWN_PROTOCOLS \
421 	( TLS_PROTOCOL_SSLv2 | TLS_PROTOCOL_SSLv3 | TLS_PROTOCOL_TLSv1 \
422 	   | TLS_PROTOCOL_TLSv1_1 | TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3 )
423 #define TLS_SSL_OP_PROTOMASK(m) \
424 	    ((((m) & TLS_PROTOCOL_SSLv2) ? SSL_OP_NO_SSLv2 : 0L) \
425 	     | (((m) & TLS_PROTOCOL_SSLv3) ? SSL_OP_NO_SSLv3 : 0L) \
426 	     | (((m) & TLS_PROTOCOL_TLSv1) ? SSL_OP_NO_TLSv1 : 0L) \
427 	     | (((m) & TLS_PROTOCOL_TLSv1_1) ? SSL_OP_NO_TLSv1_1 : 0L) \
428 	     | (((m) & TLS_PROTOCOL_TLSv1_2) ? SSL_OP_NO_TLSv1_2 : 0L) \
429 	     | (((m) & TLS_PROTOCOL_TLSv1_3) ? SSL_OP_NO_TLSv1_3 : 0L))
430 
431 /*
432  * SSL options that are managed via dedicated Postfix features, rather than
433  * just exposed via hex codes or named elements of tls_ssl_options.
434  */
435 #define TLS_SSL_OP_MANAGED_BITS \
436 	(SSL_OP_CIPHER_SERVER_PREFERENCE | TLS_SSL_OP_PROTOMASK(~0))
437 
438 extern int tls_protocol_mask(const char *);
439 
440  /*
441   * Cipher grade selection.
442   */
443 #define TLS_CIPHER_NONE		0
444 #define TLS_CIPHER_NULL		1
445 #define TLS_CIPHER_EXPORT	2
446 #define TLS_CIPHER_LOW		3
447 #define TLS_CIPHER_MEDIUM	4
448 #define TLS_CIPHER_HIGH		5
449 
450 extern const NAME_CODE tls_cipher_grade_table[];
451 
452 #define tls_cipher_grade(str) \
453     name_code(tls_cipher_grade_table, NAME_CODE_FLAG_NONE, (str))
454 #define str_tls_cipher_grade(gr) \
455     str_name_code(tls_cipher_grade_table, (gr))
456 
457  /*
458   * Cipher lists with exclusions.
459   */
460 extern const char *tls_set_ciphers(TLS_SESS_STATE *, const char *,
461 				           const char *);
462 
463  /*
464   * Populate TLS context with TLS 1.3-related signature parameters.
465   */
466 extern void tls_get_signature_params(TLS_SESS_STATE *);
467 
468 #endif					/* TLS_INTERNAL */
469 
470  /*
471   * tls_client.c
472   */
473 typedef struct {
474     const char *log_param;
475     const char *log_level;
476     int     verifydepth;
477     const char *cache_type;
478     const char *chain_files;
479     const char *cert_file;
480     const char *key_file;
481     const char *dcert_file;
482     const char *dkey_file;
483     const char *eccert_file;
484     const char *eckey_file;
485     const char *CAfile;
486     const char *CApath;
487     const char *mdalg;			/* default message digest algorithm */
488 } TLS_CLIENT_INIT_PROPS;
489 
490 typedef struct {
491     TLS_APPL_STATE *ctx;
492     VSTREAM *stream;
493     int     fd;				/* Event-driven file descriptor */
494     int     timeout;
495     int     tls_level;			/* Security level */
496     const char *nexthop;		/* destination domain */
497     const char *host;			/* MX hostname */
498     const char *namaddr;		/* nam[addr] for logging */
499     const char *sni;			/* optional SNI name when not DANE */
500     const char *serverid;		/* Session cache key */
501     const char *helo;			/* Server name from EHLO response */
502     const char *protocols;		/* Enabled protocols */
503     const char *cipher_grade;		/* Minimum cipher grade */
504     const char *cipher_exclusions;	/* Ciphers to exclude */
505     const ARGV *matchargv;		/* Cert match patterns */
506     const char *mdalg;			/* default message digest algorithm */
507     const TLS_DANE *dane;		/* DANE TLSA verification */
508 } TLS_CLIENT_START_PROPS;
509 
510 extern TLS_APPL_STATE *tls_client_init(const TLS_CLIENT_INIT_PROPS *);
511 extern TLS_SESS_STATE *tls_client_start(const TLS_CLIENT_START_PROPS *);
512 extern TLS_SESS_STATE *tls_client_post_connect(TLS_SESS_STATE *,
513 				            const TLS_CLIENT_START_PROPS *);
514 
515 #define tls_client_stop(ctx, stream, timeout, failure, TLScontext) \
516 	tls_session_stop(ctx, (stream), (timeout), (failure), (TLScontext))
517 
518 #define TLS_CLIENT_INIT_ARGS(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
519     a10, a11, a12, a13, a14) \
520     (((props)->a1), ((props)->a2), ((props)->a3), \
521     ((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
522     ((props)->a8), ((props)->a9), ((props)->a10), ((props)->a11), \
523     ((props)->a12), ((props)->a13), ((props)->a14), (props))
524 
525 #define TLS_CLIENT_INIT(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
526     a10, a11, a12, a13, a14) \
527     tls_client_init(TLS_CLIENT_INIT_ARGS(props, a1, a2, a3, a4, a5, \
528     a6, a7, a8, a9, a10, a11, a12, a13, a14))
529 
530 #define TLS_CLIENT_START(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
531     a10, a11, a12, a13, a14, a15, a16, a17) \
532     tls_client_start((((props)->a1), ((props)->a2), ((props)->a3), \
533     ((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
534     ((props)->a8), ((props)->a9), ((props)->a10), ((props)->a11), \
535     ((props)->a12), ((props)->a13), ((props)->a14), ((props)->a15), \
536     ((props)->a16), ((props)->a17), (props)))
537 
538  /*
539   * tls_server.c
540   */
541 typedef struct {
542     const char *log_param;
543     const char *log_level;
544     int     verifydepth;
545     const char *cache_type;
546     int     set_sessid;
547     const char *chain_files;
548     const char *cert_file;
549     const char *key_file;
550     const char *dcert_file;
551     const char *dkey_file;
552     const char *eccert_file;
553     const char *eckey_file;
554     const char *CAfile;
555     const char *CApath;
556     const char *protocols;
557     const char *eecdh_grade;
558     const char *dh1024_param_file;
559     const char *dh512_param_file;
560     int     ask_ccert;
561     const char *mdalg;			/* default message digest algorithm */
562 } TLS_SERVER_INIT_PROPS;
563 
564 typedef struct {
565     TLS_APPL_STATE *ctx;		/* TLS application context */
566     VSTREAM *stream;			/* Client stream */
567     int     fd;				/* Event-driven file descriptor */
568     int     timeout;			/* TLS handshake timeout */
569     int     requirecert;		/* Insist on client cert? */
570     const char *serverid;		/* Server instance (salt cache key) */
571     const char *namaddr;		/* Client nam[addr] for logging */
572     const char *cipher_grade;
573     const char *cipher_exclusions;
574     const char *mdalg;			/* default message digest algorithm */
575 } TLS_SERVER_START_PROPS;
576 
577 extern TLS_APPL_STATE *tls_server_init(const TLS_SERVER_INIT_PROPS *);
578 extern TLS_SESS_STATE *tls_server_start(const TLS_SERVER_START_PROPS *props);
579 extern TLS_SESS_STATE *tls_server_post_accept(TLS_SESS_STATE *);
580 
581 #define tls_server_stop(ctx, stream, timeout, failure, TLScontext) \
582 	tls_session_stop(ctx, (stream), (timeout), (failure), (TLScontext))
583 
584 #define TLS_SERVER_INIT(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
585     a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) \
586     tls_server_init((((props)->a1), ((props)->a2), ((props)->a3), \
587     ((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
588     ((props)->a8), ((props)->a9), ((props)->a10), ((props)->a11), \
589     ((props)->a12), ((props)->a13), ((props)->a14), ((props)->a15), \
590     ((props)->a16), ((props)->a17), ((props)->a18), ((props)->a19), \
591     ((props)->a20), (props)))
592 
593 #define TLS_SERVER_START(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) \
594     tls_server_start((((props)->a1), ((props)->a2), ((props)->a3), \
595     ((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
596     ((props)->a8), ((props)->a9), ((props)->a10), (props)))
597 
598  /*
599   * tls_session.c
600   */
601 extern void tls_session_stop(TLS_APPL_STATE *, VSTREAM *, int, int, TLS_SESS_STATE *);
602 
603  /*
604   * tls_misc.c
605   */
606 extern const char *tls_compile_version(void);
607 extern const char *tls_run_version(void);
608 extern const char **tls_pkey_algorithms(void);
609 extern void tls_log_summary(TLS_ROLE, TLS_USAGE, TLS_SESS_STATE *);
610 extern void tls_pre_jail_init(TLS_ROLE);
611 
612 #ifdef TLS_INTERNAL
613 
614 #include <vstring.h>
615 
616 extern VSTRING *tls_session_passivate(SSL_SESSION *);
617 extern SSL_SESSION *tls_session_activate(const char *, int);
618 
619  /*
620   * tls_stream.c.
621   */
622 extern void tls_stream_start(VSTREAM *, TLS_SESS_STATE *);
623 extern void tls_stream_stop(VSTREAM *);
624 
625  /*
626   * tls_bio_ops.c: a generic multi-personality driver that retries SSL
627   * operations until they are satisfied or until a hard error happens.
628   * Because of its ugly multi-personality user interface we invoke it via
629   * not-so-ugly single-personality wrappers.
630   */
631 extern int tls_bio(int, int, TLS_SESS_STATE *,
632 		           int (*) (SSL *),	/* handshake */
633 		           int (*) (SSL *, void *, int),	/* read */
634 		           int (*) (SSL *, const void *, int),	/* write */
635 		           void *, int);
636 
637 #define tls_bio_connect(fd, timeout, context) \
638         tls_bio((fd), (timeout), (context), SSL_connect, \
639 		NULL, NULL, NULL, 0)
640 #define tls_bio_accept(fd, timeout, context) \
641         tls_bio((fd), (timeout), (context), SSL_accept, \
642 		NULL, NULL, NULL, 0)
643 #define tls_bio_shutdown(fd, timeout, context) \
644 	tls_bio((fd), (timeout), (context), SSL_shutdown, \
645 		NULL, NULL, NULL, 0)
646 #define tls_bio_read(fd, buf, len, timeout, context) \
647 	tls_bio((fd), (timeout), (context), NULL, \
648 		SSL_read, NULL, (buf), (len))
649 #define tls_bio_write(fd, buf, len, timeout, context) \
650 	tls_bio((fd), (timeout), (context), NULL, \
651 		NULL, SSL_write, (buf), (len))
652 
653  /*
654   * tls_dh.c
655   */
656 extern void tls_set_dh_from_file(const char *, int);
657 extern DH *tls_tmp_dh_cb(SSL *, int, int);
658 extern void tls_set_eecdh_curve(SSL_CTX *, const char *);
659 extern void tls_auto_eecdh_curves(SSL_CTX *, const char *);
660 
661  /*
662   * tls_rsa.c
663   */
664 extern RSA *tls_tmp_rsa_cb(SSL *, int, int);
665 
666  /*
667   * tls_verify.c
668   */
669 extern char *tls_peer_CN(X509 *, const TLS_SESS_STATE *);
670 extern char *tls_issuer_CN(X509 *, const TLS_SESS_STATE *);
671 extern const char *tls_dns_name(const GENERAL_NAME *, const TLS_SESS_STATE *);
672 extern int tls_verify_certificate_callback(int, X509_STORE_CTX *);
673 extern void tls_log_verify_error(TLS_SESS_STATE *);
674 
675  /*
676   * tls_dane.c
677   */
678 extern int tls_dane_match(TLS_SESS_STATE *, int, X509 *, int);
679 extern void tls_dane_set_callback(SSL_CTX *, TLS_SESS_STATE *);
680 
681  /*
682   * tls_fprint.c
683   */
684 extern char *tls_digest_encode(const unsigned char *, int);
685 extern char *tls_data_fprint(const char *, int, const char *);
686 extern char *tls_cert_fprint(X509 *, const char *);
687 extern char *tls_pkey_fprint(X509 *, const char *);
688 extern char *tls_serverid_digest(const TLS_CLIENT_START_PROPS *, long,
689 				         const char *);
690 
691  /*
692   * tls_certkey.c
693   */
694 extern int tls_set_ca_certificate_info(SSL_CTX *, const char *, const char *);
695 extern int tls_load_pem_chain(SSL *, const char *, const char *);
696 extern int tls_set_my_certificate_key_info(SSL_CTX *, /* All */ const char *,
697 				       /* RSA */ const char *, const char *,
698 				       /* DSA */ const char *, const char *,
699 				    /* ECDSA */ const char *, const char *);
700 
701  /*
702   * tls_misc.c
703   */
704 extern int TLScontext_index;
705 
706 extern TLS_APPL_STATE *tls_alloc_app_context(SSL_CTX *, SSL_CTX *, int);
707 extern TLS_SESS_STATE *tls_alloc_sess_context(int, const char *);
708 extern void tls_free_context(TLS_SESS_STATE *);
709 extern void tls_check_version(void);
710 extern long tls_bug_bits(void);
711 extern void tls_print_errors(void);
712 extern void tls_info_callback(const SSL *, int, int);
713 extern long tls_bio_dump_cb(BIO *, int, const char *, int, long, long);
714 extern int tls_validate_digest(const char *);
715 
716  /*
717   * tls_seed.c
718   */
719 extern void tls_int_seed(void);
720 extern int tls_ext_seed(int);
721 
722 #endif					/* TLS_INTERNAL */
723 
724 /* LICENSE
725 /* .ad
726 /* .fi
727 /*	The Secure Mailer license must be distributed with this software.
728 /* AUTHOR(S)
729 /*	Wietse Venema
730 /*	IBM T.J. Watson Research
731 /*	P.O. Box 704
732 /*	Yorktown Heights, NY 10598, USA
733 /*
734 /*	Wietse Venema
735 /*	Google, Inc.
736 /*	111 8th Avenue
737 /*	New York, NY 10011, USA
738 /*
739 /*	Victor Duchovni
740 /*	Morgan Stanley
741 /*--*/
742 
743 #endif					/* USE_TLS */
744 #endif					/* _TLS_H_INCLUDED_ */
745