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