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