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