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