s/qmail 4.3.20
Next generation secure email transport
Loading...
Searching...
No Matches
tls_remote.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "ucspissl.h"
3#include "fmt.h"
4#include "stralloc.h"
5#include "str.h"
6#include "byte.h"
7#include "case.h"
8#include "dns.h"
9#include "constmap.h"
10#include "tls_remote.h"
11#include "tls_errors.h"
12
24/* Caution: OpenSSL's X509_pubkey_digest() does not work as expected.
25 I've included now: X509_pkey_digest() and X509_cert_digest() (as makro) */
26
27#define X509_cert_digest X509_digest
28
29int tls_certkey(SSL_CTX *ctx,const char *cert,const char *key,char *ppwd)
30{
31 if (!cert) return 0;
32
33 if (SSL_CTX_use_certificate_chain_file(ctx,cert) != 1)
34 return -1;
35
36 if (!key) key = cert;
37
38 if (ppwd) SSL_CTX_set_default_passwd_cb_userdata(ctx,ppwd);
39
40 if (SSL_CTX_use_PrivateKey_file(ctx,key,SSL_FILETYPE_PEM) != 1)
41 return -2;
42
43 if (SSL_CTX_check_private_key(ctx) != 1)
44 return -3;
45
46 return 0;
47}
48
49int tls_conn(SSL *ssl,int smtpfd)
50{
51 SSL_set_options(ssl,SSL_OP_NO_SSLv2);
52 SSL_set_options(ssl,SSL_OP_NO_SSLv3);
53 return SSL_set_fd(ssl,smtpfd);
54}
55
56int tls_checkpeer(SSL *ssl,X509 *cert,const stralloc host,const int flag,const int verify)
57{
58 char buf[SSL_NAME_LEN];
59 char *dnsname = 0;
60 int dname = 0;
61 int num;
62 int len;
63 int fflag;
64 int i;
65 int rc = 0;
66
67 fflag = flag;
68 if (flag > 20) fflag = flag - 20;
69 if (flag > 10) fflag = flag - 10;
70
71 /* X.509 CA DN/SAN name validation against DNS */
72
73 if (host.len && fflag > 4) {
74 GENERAL_NAMES *extensions = X509_get_ext_d2i(cert,NID_subject_alt_name,0,0);
75 num = sk_GENERAL_NAME_num(extensions); /* num = 0, if no SAN extensions */
76
77 for (i = 0; i < num; ++i) {
78 const GENERAL_NAME *ext = sk_GENERAL_NAME_value(extensions,i);
79 if (ext->type == GEN_DNS) {
80 if (OBJ_sn2nid((const char*)ext->d.ia5) != V_ASN1_IA5STRING) continue;
81 dnsname = (char *)ASN1_STRING_get0_data(ext->d.ia5);
82 len = ASN1_STRING_length(ext->d.ia5);
83 dname = 1;
84 }
85 }
86
87 if (!dname) {
88 X509_NAME_get_text_by_NID(X509_get_subject_name(cert),NID_commonName,buf,sizeof(buf));
89 buf[SSL_NAME_LEN - 1] = 0;
90 dnsname = buf;
91 len = SSL_NAME_LEN - 1;
92 }
93
94 switch (fflag) {
95 case 5: if (dnsname[0] == '*' && dnsname[1] == '.')
96 if (case_diffrs(dnsname + 1,host.s)) return -3;
97 if (case_diffrs(dnsname,host.s)) return -3;
98 rc = 3; break;
99 case 6: if (case_diffs(dnsname,host.s)) return -3;
100 rc = 2; break;
101 }
102 }
103
104 /* X.509 CA Verification: root CA must be available */
105
106 if (fflag > 3 && verify > -2) {
107 if (SSL_get_verify_result(ssl) != X509_V_OK) return -2;
108 else rc = 1;
109 }
110
111 return rc;
112}
113
114int tls_checkcrl(SSL *ssl) // not implemented yet
115{
116
117 return 0;
118}
119
120int dig_ascii(char *digascii,const char *digest,const int len)
121{
122 static const char hextab[] = "0123456789abcdef";
123 int j;
124
125 for (j = 0; j < len; j++) {
126 digascii[2 * j] = hextab[(unsigned char)digest[j] >> 4];
127 digascii[2 * j + 1] = hextab[(unsigned char)digest[j] & 0x0f];
128 }
129 digascii[2 * len] = '\0';
130
131 return (2 * j); // 2*len
132}
133
134/* X509_pkey_digest() takes the same args as X509_digest();
135 however returning the correct hash of pubkey in md.
136 Subjects keys are restricted to 2048 byte in size.
137 Return codes: 1: sucess, 0: failed. */
138
139int X509_pkey_digest(const X509 *cert,const EVP_MD *type,unsigned char *md,unsigned int *dlen)
140{
141 unsigned int len = 0;
142 unsigned int size = 2048;
143 unsigned char *buf;
144 unsigned char *buf2;
145 unsigned char buffer[size]; // avoid malloc
146
147/* Following Viktor's suggestion */
148
149 if (!X509_get0_pubkey_bitstr(cert)) return 0; // no Subject public key
150
151 len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert),0);
152 if (len > size) return 0;
153 buf2 = buf = buffer;
154 i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert),(unsigned char **)&buf2);
155 if (buf2 - buf != len) return 0;
156
157 if (!EVP_Digest(buf,len,md,dlen,type,0)) return 0; // OpenSSL voodoo
158 return 1;
159}
160
161/* Return codes: -5: nullified TLSA digest, ignored
162 -4: no X.509 cert (fatal), -3: matching error (deferred),
163 -2: unsupported type, -1: weird TLSA record
164 0: No X.509 cert; seen: usage++; */
165
166const char tlsa0[512] = { 512 * 0 };
167
168int tlsa_check(const STACK_OF(X509) *certs,const stralloc host,const unsigned long p)
169{
170 const EVP_MD *methodsha256 = EVP_sha256();
171 const EVP_MD *methodsha512 = EVP_sha512();
172 stralloc out = {0};
173 stralloc sa = {0};
174 stralloc cn = {0};
175 unsigned char digest[EVP_MAX_MD_SIZE];
176 unsigned int dlen = 0;
177 unsigned int n = 0;
178 int i = 0;
179 int r;
180 char port[FMT_ULONG];
181 uint16 type;
182 uint16 selector;
183 uint16 usage;
184
185// construct TLSA FQDN -- simple procedure; returning Usage
186
187 if (host.len < 2) return 0;
188 if (!stralloc_copyb(&sa,"_",1)) temp_nomem();
189 port[fmt_ulong(port,p)] = 0;
190 if (!stralloc_cats(&sa,port)) temp_nomem();
191 if (!stralloc_cats(&sa,"._tcp.")) temp_nomem();
192 if (!stralloc_cats(&sa,host.s)) temp_nomem();
193
194 if (dns_cname(&cn,&sa) > 0) // query name could be a cname
195 { if (dns_tlsa(&out,&cn) <= 0) return 0; }
196 else
197 { if (dns_tlsa(&out,&sa) <= 0) return 0; }
198 if (out.len < 5) return -1;
199
200 /* https://www.openssl.org/docs/man3.0/man3/X509_digest.html (1.1.1):
201 "The len parameter, if not NULL, points to a place where the digest size will be stored."
202 [sigh]
203 */
204 do {
205 if (!byte_diff(tlsa0,out.len - i - 3,out.s + i + 3)) return -5; // TLSA digst = 00000 ...
206 usage = (unsigned char) out.s[i]; // Usage: PKIX-TA [0], PKIX-EE [1], DANE-TA [2], DANE-EE [3]
207 selector = (unsigned char) out.s[i + 1]; // Selector: 0 = Cert, 1 = SPKI
208 type = (unsigned char) out.s[i + 2]; // Type: 0/1/2 = [Cert|SPKI]/SHA256/SHA512
209
210 unsigned len = sk_X509_num(certs);
211 for (n = 0; n < len; n++) {
212 X509 *cert = sk_X509_value(certs,n);
213 if (type == 1) {
214 if (selector == 0) r = X509_cert_digest(cert,methodsha256,digest,&dlen);
215 if (selector == 1) r = X509_pkey_digest(cert,methodsha256,digest,&dlen);
216 } else if (type == 2) {
217 if (selector == 0) r = X509_cert_digest(cert,methodsha512,digest,&dlen);
218 if (selector == 1) r = X509_pkey_digest(cert,methodsha512,digest,&dlen);
219 } else
220 return -2;
221
222 if (!byte_diff(digest,dlen,out.s + i + 3)) return ++usage;
223 }
224
225 i += (dlen + 3);
226 } while (i < out.len - 4);
227
228 return -3;
229}
230
231int tls_fingerprint(X509 *cert,const char *fingerprint,int dlen)
232{
233 const EVP_MD *methodsha1 = EVP_sha1();
234 const EVP_MD *methodsha224 = EVP_sha224();
235 const EVP_MD *methodsha256 = EVP_sha256();
236 const EVP_MD *methodsha512 = EVP_sha512();
237 unsigned char digest[EVP_MAX_MD_SIZE];
238 unsigned char digascii[257];
239 unsigned int len;
240
241 switch (dlen) { /* fetch digest from cert; len = bitlength/8 */
242 case 40: if (!X509_digest(cert,methodsha1,digest,&len)) return -2;
243 case 56: if (!X509_digest(cert,methodsha224,digest,&len)) return -2;
244 case 64: if (!X509_digest(cert,methodsha256,digest,&len)) return -2;
245 case 128: if (!X509_digest(cert,methodsha512,digest,&len)) return -2;
246 default: return -3;
247 }
248
249 len = dig_ascii(digascii,digest,len);
250 if (!str_diffn(digascii,fingerprint,len)) return 1;
251
252 return 0;
253}
254
255int tls_exit(SSL *ssl)
256{
257 if (SSL_shutdown(ssl) == 0)
258 SSL_shutdown(ssl);
259
260 return 0;
261}
262
276int tls_destination(const stralloc hostname)
277{
278 int i;
279 stralloc tlshost = {0};
280 stralloc tlsdest = {0};
281
282 if (!stralloc_copy(&tlshost,&hostname)) temp_nomem();
283 if (!stralloc_0(&tlshost)) temp_nomem();
284
285// Host rules
286
287 if (!stralloc_copys(&tlsdest,"!")) temp_nomem();
288 if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem();
289 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return -1;
290
291 if (!stralloc_copys(&tlsdest,"?")) temp_nomem();
292 if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem();
293 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 9;
294
295 if (!stralloc_copys(&tlsdest,"/")) temp_nomem();
296 if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem();
297 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 8;
298
299 if (!stralloc_copys(&tlsdest,"%")) temp_nomem(); // CERT + hash
300 if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem();
301 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 7;
302
303 if (!stralloc_copys(&tlsdest,"=")) temp_nomem(); // CERT + FQDN
304 if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem();
305 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 6;
306
307 if (!stralloc_copys(&tlsdest,"~")) temp_nomem(); // CERT + Wild
308 if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem();
309 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 5;
310
311// Domain rules
312
313 for (i = 0; i < tlshost.len; ++i) // TLS fallthru
314 if ((i == 0) || (tlshost.s[i] == '.')) {
315 if (!stralloc_copys(&tlsdest,"?")) temp_nomem();
316 if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem();
317 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 9;
318 }
319
320 for (i = 0; i < tlshost.len; ++i) // no TLSA
321 if ((i == 0) || (tlshost.s[i] == '.')) {
322 if (!stralloc_copys(&tlsdest,"/")) temp_nomem();
323 if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem();
324 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 8;
325 }
326
327 for (i = 0; i < tlshost.len; ++i) // CERT + Wild
328 if ((i == 0) || (tlshost.s[i] == '.')) {
329 if (!stralloc_copys(&tlsdest,"~")) temp_nomem();
330 if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem();
331 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 5;
332 }
333
334 for (i = 0; i < tlshost.len; ++i) // CERT - generic
335 if ((i == 0) || (tlshost.s[i] == '.')) {
336 if (!stralloc_copys(&tlsdest,"")) temp_nomem();
337 if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem();
338 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 4;
339 }
340
341 for (i = 0; i < tlshost.len; ++i) // ADH per host/domain
342 if ((i == 0) || (tlshost.s[i] == '.')) {
343 if (!stralloc_copys(&tlsdest,"-")) temp_nomem();
344 if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem();
345 if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 2;
346 }
347
348// General rules (mandatory TLS)
349
350 tlsdestinfo = 0;
351 if (constmap(&maptlsdestinations,"/*",2)) return 8; // no TLSA
352 if (constmap(&maptlsdestinations,"=*",2)) return 6; // CERT + FQDN
353 if (constmap(&maptlsdestinations,"~*",2)) return 5; // CERT + Wild
354 if (constmap(&maptlsdestinations,"+*",2)) return 4; // CERT
355 if (constmap(&maptlsdestinations,"-*",2)) return 2; // ADH
356
357// Fall thru rules (optional TLS)
358
359 if (constmap(&maptlsdestinations,"?",1)) return 9; // fallback to no TLS
360 if (constmap(&maptlsdestinations,"*",1)) return 3; // CERT
361 if (constmap(&maptlsdestinations,"-",1)) return 1; // ADH
362
363 return 0;
364}
365
366int tls_domaincerts(const stralloc domainname)
367{
368 int i;
369 tlsdomaininfo = 0; // extern
370
371/* Our Certs - per domain */
372
373 if (domainname.len)
374 for (i = 0; i < domainname.len; ++i)
375 if ((i == 0) || (domainname.s[i] == '.'))
376 if ((tlsdomaininfo = constmap(&mapdomaincerts,domainname.s + i,domainname.len - i))) return 2;
377
378/* Standard Cert (if any) */
379
380 if ((tlsdomaininfo = constmap(&mapdomaincerts,"*",1))) return 1;
381
382 return 0;
383}
char num[FMT_ULONG]
Definition: chkspawn.c:8
int stralloc_copys(stralloc *, char const *)
int dns_tlsa(stralloc *out, const stralloc *fqdn)
Definition: dns_tlsa.c:41
stralloc out
Definition: dnscname.c:12
stralloc sa
Definition: dnscname.c:11
stralloc key
Definition: fastforward.c:116
uint32 dlen
Definition: fastforward.c:117
char buf[100+FMT_ULONG]
Definition: hier.c:11
void p(char *, char *, int, int, int)
Definition: install.c:49
char host[256]
Definition: hostname.c:5
void usage()
Definition: qmail-dkim.cpp:60
stralloc selector
Definition: qmail-dksign.c:228
unsigned long port
Definition: qmail-ldapam.c:63
char * ext
Definition: qmail-local.c:60
stralloc hostname
Definition: qmail-local.c:75
char buf2[BUFFER_SMALL]
Definition: qmail-qmtpd.c:85
unsigned long size
Definition: qmail-qread.c:56
int smtpfd
Definition: qmail-remote.c:292
SSL_CTX * ctx
Definition: qmail-remote.c:108
SSL * ssl
Definition: qmail-remote.c:107
int j
Definition: qmail-send.c:926
stralloc tlsdest
Definition: qmail-smtpam.c:285
stralloc dnsname
Definition: spf.c:27
void temp_nomem(void)
Definition: qmail-ldapam.c:65
int tls_exit(SSL *ssl)
Definition: tls_remote.c:255
const char tlsa0[512]
Definition: tls_remote.c:166
int tls_domaincerts(const stralloc domainname)
Definition: tls_remote.c:366
int tls_fingerprint(X509 *cert, const char *fingerprint, int dlen)
Definition: tls_remote.c:231
int tls_checkcrl(SSL *ssl)
Definition: tls_remote.c:114
int X509_pkey_digest(const X509 *cert, const EVP_MD *type, unsigned char *md, unsigned int *dlen)
Definition: tls_remote.c:139
int tls_conn(SSL *ssl, int smtpfd)
Definition: tls_remote.c:49
int tls_destination(const stralloc hostname)
tls_destination
Definition: tls_remote.c:276
int tlsa_check(const STACK_OF(X509) *certs, const stralloc host, const unsigned long p)
Definition: tls_remote.c:168
int dig_ascii(char *digascii, const char *digest, const int len)
Definition: tls_remote.c:120
int tls_certkey(SSL_CTX *ctx, const char *cert, const char *key, char *ppwd)
Definition: tls_remote.c:29
int tls_checkpeer(SSL *ssl, X509 *cert, const stralloc host, const int flag, const int verify)
Definition: tls_remote.c:56
#define X509_cert_digest
Definition: tls_remote.c:27
char * tlsdomaininfo
Definition: qmail-remote.c:435
struct constmap maptlsdestinations
Definition: qmail-remote.c:440
struct constmap mapdomaincerts
Definition: qmail-remote.c:438
char * tlsdestinfo
Definition: qmail-remote.c:434
#define SSL_NAME_LEN
Definition: ucspitls.h:14