24
24
* XXX TLS_LEV_NOTFOUND no longer belongs in this list. The SMTP client will
25
25
* have to use something else to report that policy table lookup failed.
27
* The order of levels matters, but we hide most of the details in macros.
29
* "dane" vs. "fingerprint", both must lie between "encrypt" and "verify".
31
* - With "may" and higher, TLS is enabled.
33
* - With "encrypt" and higher, TLS encryption must be applied.
35
* - Strictly above "encrypt", the peer certificate must match.
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.
27
41
#define TLS_LEV_INVALID -2 /* sentinel */
28
42
#define TLS_LEV_NOTFOUND -1 /* XXX not in policy table */
30
44
#define TLS_LEV_MAY 1 /* wildcard */
31
45
#define TLS_LEV_ENCRYPT 2 /* encrypted connection */
32
46
#define TLS_LEV_FPRINT 3 /* "peer" CA-less verification */
33
#define TLS_LEV_VERIFY 4 /* certificate verified */
34
#define TLS_LEV_SECURE 5 /* "secure" verification */
47
#define TLS_LEV_DANE 4 /* Opportunistic TLSA policy */
48
#define TLS_LEV_DANE_ONLY 5 /* Required TLSA policy */
49
#define TLS_LEV_VERIFY 6 /* certificate verified */
50
#define TLS_LEV_SECURE 7 /* "secure" verification */
52
#define TLS_REQUIRED(l) ((l) > TLS_LEV_MAY)
53
#define TLS_MUST_MATCH(l) ((l) > TLS_LEV_ENCRYPT)
54
#define TLS_MUST_TRUST(l) ((l) >= TLS_LEV_DANE)
55
#define TLS_MUST_PKIX(l) ((l) >= TLS_LEV_VERIFY)
36
57
extern const NAME_CODE tls_level_table[];
52
73
#include <openssl/rand.h>
53
74
#include <openssl/ssl.h>
55
#if (OPENSSL_VERSION_NUMBER < 0x00905100L)
56
#error "need OpenSSL version 0.9.5 or later"
76
/* Appease indent(1) */
77
#define x509_stack_t STACK_OF(X509)
78
#define x509_extension_stack_t STACK_OF(X509_EXTENSION)
79
#define general_name_stack_t STACK_OF(GENERAL_NAME)
80
#define ssl_cipher_stack_t STACK_OF(SSL_CIPHER)
81
#define ssl_comp_stack_t STACK_OF(SSL_COMP)
83
#if (OPENSSL_VERSION_NUMBER < 0x00090700f)
84
#error "need OpenSSL version 0.9.7 or later"
87
/* SSL_CIPHER_get_name() got constified in 0.9.7g */
88
#if OPENSSL_VERSION_NUMBER >= 0x0090707fL /* constification */
89
#define SSL_CIPHER_const const
91
#define SSL_CIPHER_const
94
/* d2i_X509() got constified in 0.9.8a */
95
#if OPENSSL_VERSION_NUMBER >= 0x0090801fL
96
#define D2I_const const
71
114
#define TLS_MGR_SCACHE_LMTP "lmtp"
119
#define TLS_DANE_TA 0 /* Match trust-anchor digests */
120
#define TLS_DANE_EE 1 /* Match end-entity digests */
122
#define TLS_DANE_CERT 0 /* Match the certificate digest */
123
#define TLS_DANE_PKEY 1 /* Match the public key digest */
125
#define TLS_DANE_FLAG_NORRS (1<<0) /* Nothing found in DNS */
126
#define TLS_DANE_FLAG_EMPTY (1<<1) /* Nothing usable found in DNS */
127
#define TLS_DANE_FLAG_ERROR (1<<2) /* TLSA record lookup error */
129
#define tls_dane_unusable(dane) ((dane)->flags & TLS_DANE_FLAG_EMPTY)
130
#define tls_dane_notfound(dane) ((dane)->flags & TLS_DANE_FLAG_NORRS)
132
#define TLS_DANE_CACHE_TTL_MIN 1 /* A lot can happen in ~2 seconds */
133
#define TLS_DANE_CACHE_TTL_MAX 100 /* Comparable to max_idle */
136
* Certificate and public key digests (typically from TLSA RRs), grouped by
139
typedef struct TLS_TLSA {
140
char *mdalg; /* Algorithm for this digest list */
141
ARGV *certs; /* Complete certificate digests */
142
ARGV *pkeys; /* SubjectPublicKeyInfo digests */
143
struct TLS_TLSA *next; /* Chain to next algorithm */
147
* Linked list of full X509 trust-anchor certs.
149
typedef struct TLS_CERTS {
151
struct TLS_CERTS *next;
155
* Linked list of full EVP_PKEY trust-anchor public keys.
157
typedef struct TLS_PKEYS {
159
struct TLS_PKEYS *next;
162
typedef struct TLS_DANE {
163
TLS_TLSA *ta; /* Trust-anchor cert/pubkey digests */
164
TLS_TLSA *ee; /* End-entity cert/pubkey digests */
165
TLS_CERTS *certs; /* Full trust-anchor certificates */
166
TLS_PKEYS *pkeys; /* Full trust-anchor public keys */
167
char *base_domain; /* Base domain of TLSA RRset */
168
int flags; /* Conflate cert and pkey digests */
169
time_t expires; /* Expiration time of this record */
170
int refs; /* Reference count */
173
#define TLS_DANE_HASTA(d) ((d) ? (d)->ta : 0)
174
#define TLS_DANE_HASEE(d) ((d) ? (d)->ee : 0)
179
extern int tls_dane_avail(void);
180
extern void tls_dane_flush(void);
181
extern void tls_dane_verbose(int);
182
extern TLS_DANE *tls_dane_alloc(void);
183
extern void tls_dane_add_ee_digests(TLS_DANE *, const char *, const char *,
185
extern void tls_dane_free(TLS_DANE *);
186
extern TLS_DANE *tls_dane_resolve(unsigned, const char *, DNS_RR *, int);
187
extern int tls_dane_load_trustfile(TLS_DANE *, const char *);
74
190
* TLS session context, also used by the VSTREAM call-back routines for SMTP
75
191
* input/output, and by OpenSSL call-back routines for key verification.
94
210
char *cache_type; /* tlsmgr(8) cache type if enabled */
211
int ticketed; /* Session ticket issued */
95
212
char *serverid; /* unique server identifier */
96
213
char *namaddr; /* nam[addr] for logging */
97
214
int log_mask; /* What to log */
98
215
int session_reused; /* this session was reused */
99
216
int am_server; /* Are we an SSL server or client? */
217
const char *mdalg; /* default message digest algorithm */
100
218
/* Built-in vs external SSL_accept/read/write/shutdown support. */
101
char *fpt_dgst; /* Certificate fingerprint digest */
102
219
VSTREAM *stream; /* Blocking-mode SMTP session */
220
/* RFC 6698 DANE trust input and verification state */
221
const TLS_DANE *dane; /* DANE TLSA digests */
222
int errordepth; /* Chain depth of error cert */
223
int tadepth; /* Chain depth of trust anchor */
224
int errorcode; /* First error at error depth */
225
X509 *errorcert; /* Error certificate closest to leaf */
226
x509_stack_t *untrusted; /* Certificate chain fodder */
227
x509_stack_t *trusted; /* Internal root CA list */
103
228
} TLS_SESS_STATE;
187
314
#undef SSL_OP_NO_TLSv1_2
188
315
#define SSL_OP_NO_TLSv1_2 0L /* Noop */
190
#define TLS_KNOWN_PROTOCOLS \
317
#define TLS_KNOWN_PROTOCOLS \
191
318
( TLS_PROTOCOL_SSLv2 | TLS_PROTOCOL_SSLv3 | TLS_PROTOCOL_TLSv1 \
192
319
| TLS_PROTOCOL_TLSv1_1 | TLS_PROTOCOL_TLSv1_2 )
320
#define TLS_SSL_OP_PROTOMASK(m) \
321
((((m) & TLS_PROTOCOL_SSLv2) ? SSL_OP_NO_SSLv2 : 0L) \
322
| (((m) & TLS_PROTOCOL_SSLv3) ? SSL_OP_NO_SSLv3 : 0L) \
323
| (((m) & TLS_PROTOCOL_TLSv1) ? SSL_OP_NO_TLSv1 : 0L) \
324
| (((m) & TLS_PROTOCOL_TLSv1_1) ? SSL_OP_NO_TLSv1_1 : 0L) \
325
| (((m) & TLS_PROTOCOL_TLSv1_2) ? SSL_OP_NO_TLSv1_2 : 0L))
328
* SSL options that are managed via dedicated Postfix features, rather than
329
* just exposed via hex codes or named elements of tls_ssl_options.
331
#define TLS_SSL_OP_MANAGED_BITS \
332
(SSL_OP_CIPHER_SERVER_PREFERENCE | TLS_SSL_OP_PROTOMASK(~0))
194
334
extern int tls_protocol_mask(const char *);
246
386
const char *host; /* MX hostname */
247
387
const char *namaddr; /* nam[addr] for logging */
248
388
const char *serverid; /* Session cache key */
389
const char *helo; /* Server name from EHLO response */
249
390
const char *protocols; /* Enabled protocols */
250
391
const char *cipher_grade; /* Minimum cipher grade */
251
392
const char *cipher_exclusions; /* Ciphers to exclude */
252
393
const ARGV *matchargv; /* Cert match patterns */
253
const char *fpt_dgst; /* Fingerprint digest algorithm */
394
const char *mdalg; /* default message digest algorithm */
395
const TLS_DANE *dane; /* RFC 6698 verification */
254
396
} TLS_CLIENT_START_PROPS;
256
398
extern TLS_APPL_STATE *tls_client_init(const TLS_CLIENT_INIT_PROPS *);
267
409
((props)->a12), ((props)->a13), (props)))
269
411
#define TLS_CLIENT_START(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
270
a10, a11, a12, a13) \
412
a10, a11, a12, a13, a14, a15) \
271
413
tls_client_start((((props)->a1), ((props)->a2), ((props)->a3), \
272
414
((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
273
415
((props)->a8), ((props)->a9), ((props)->a10), ((props)->a11), \
274
((props)->a12), ((props)->a13), (props)))
416
((props)->a12), ((props)->a13), ((props)->a14), ((props)->a15), (props)))
309
450
const char *namaddr; /* Client nam[addr] for logging */
310
451
const char *cipher_grade;
311
452
const char *cipher_exclusions;
312
const char *fpt_dgst; /* Fingerprint digest algorithm */
453
const char *mdalg; /* default message digest algorithm */
313
454
} TLS_SERVER_START_PROPS;
315
456
extern TLS_APPL_STATE *tls_server_init(const TLS_SERVER_INIT_PROPS *);
320
461
tls_session_stop(ctx, (stream), (timeout), (failure), (TLScontext))
322
463
#define TLS_SERVER_INIT(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, \
323
a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) \
464
a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) \
324
465
tls_server_init((((props)->a1), ((props)->a2), ((props)->a3), \
325
466
((props)->a4), ((props)->a5), ((props)->a6), ((props)->a7), \
326
467
((props)->a8), ((props)->a9), ((props)->a10), ((props)->a11), \
327
468
((props)->a12), ((props)->a13), ((props)->a14), ((props)->a15), \
328
((props)->a16), ((props)->a17), ((props)->a18), ((props)->a19), \
329
((props)->a20), (props)))
469
((props)->a16), ((props)->a17), ((props)->a18), ((props)->a19), (props)))
331
471
#define TLS_SERVER_START(props, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) \
332
472
tls_server_start((((props)->a1), ((props)->a2), ((props)->a3), \
397
537
extern char *tls_peer_CN(X509 *, const TLS_SESS_STATE *);
398
538
extern char *tls_issuer_CN(X509 *, const TLS_SESS_STATE *);
399
539
extern const char *tls_dns_name(const GENERAL_NAME *, const TLS_SESS_STATE *);
400
extern char *tls_fingerprint(X509 *, const char *);
540
extern int tls_verify_certificate_callback(int, X509_STORE_CTX *);
541
extern void tls_log_verify_error(TLS_SESS_STATE *);
546
extern int tls_dane_match(TLS_SESS_STATE *, int, X509 *, int);
547
extern void tls_dane_set_callback(SSL_CTX *, TLS_SESS_STATE *);
552
extern char *tls_digest_encode(const unsigned char *, int);
553
extern char *tls_data_fprint(const char *, int, const char *);
554
extern char *tls_cert_fprint(X509 *, const char *);
401
555
extern char *tls_pkey_fprint(X509 *, const char *);
402
extern int tls_verify_certificate_callback(int, X509_STORE_CTX *);
556
extern char *tls_serverid_digest(const TLS_CLIENT_START_PROPS *, long,
423
578
extern void tls_print_errors(void);
424
579
extern void tls_info_callback(const SSL *, int, int);
425
580
extern long tls_bio_dump_cb(BIO *, int, const char *, int, long, long);
581
extern int tls_validate_digest(const char *);