1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Jeremy Harris 2014 */
7 /* This module provides TLS (aka SSL) support for Exim using the OpenSSL
8 library. It is #included into the tls.c file when that library is used.
14 #include <openssl/lhash.h>
15 #include <openssl/ssl.h>
16 #include <openssl/err.h>
17 #include <openssl/rand.h>
18 #include <openssl/x509v3.h>
21 /*****************************************************
22 * Export/import a certificate, binary/printable
23 *****************************************************/
25 tls_export_cert(uschar * buf, size_t buflen, void * cert)
27 BIO * bp = BIO_new(BIO_s_mem());
30 if ((fail = PEM_write_bio_X509(bp, (X509 *)cert) ? 0 : 1))
31 log_write(0, LOG_MAIN, "TLS error in certificate export: %s",
32 ERR_error_string(ERR_get_error(), NULL));
40 if ((n = BIO_gets(bp, cp, (int)buflen)) <= 0) break;
43 cp[-2] = '\\'; cp[-1] = 'n'; /* newline->"\n" */
44 } /* compat with string_printing() */
53 tls_import_cert(const uschar * buf, void ** cert)
55 void * reset_point = store_get(0);
56 const uschar * cp = string_unprinting(US buf);
61 bp = BIO_new_mem_buf(US cp, -1);
62 if (!(x = PEM_read_bio_X509(bp, NULL, 0, NULL)))
64 log_write(0, LOG_MAIN, "TLS error in certificate import: %s",
65 ERR_error_string(ERR_get_error(), NULL));
71 store_reset(reset_point);
76 tls_free_cert(void * cert)
78 X509_free((X509 *)cert);
82 /*****************************************************
83 * Certificate field extraction routines
84 *****************************************************/
86 /* First, some internal service functions */
91 expand_string_message = US"allocation failure";
96 bio_string_copy(BIO * bp, int len)
99 len = len > 0 ? (int) BIO_get_mem_data(bp, &cp) : 0;
100 cp = string_copyn(cp, len);
106 bio_string_time_to_int(BIO * bp, int len)
110 len = len > 0 ? (int) BIO_get_mem_data(bp, &cp) : 0;
111 /*XXX %Z might be glibc-specific? */
112 (void) strptime(CS cp, "%b%t%e%t%T%t%Y%t%Z", &t);
114 /*XXX timegm might not be portable? */
115 return string_sprintf("%u", (unsigned) timegm(&t));
119 asn1_time_copy(const ASN1_TIME * time, uschar * mod)
121 BIO * bp = BIO_new(BIO_s_mem());
124 if (!bp) return badalloc();
126 len = ASN1_TIME_print(bp, time);
127 return mod && Ustrcmp(mod, "int") == 0
128 ? bio_string_time_to_int(bp, len)
129 : bio_string_copy(bp, len);
133 x509_name_copy(X509_NAME * name)
135 BIO * bp = BIO_new(BIO_s_mem());
138 if (!bp) return badalloc();
141 X509_NAME_print_ex(bp, name, 0, XN_FLAG_RFC2253) >= 0
143 return bio_string_copy(bp, len_good);
147 /* Now the extractors, called from expand.c
150 mod Optional modifiers for the operator
153 Allocated string with extracted value
157 tls_cert_issuer(void * cert, uschar * mod)
159 uschar * cp = x509_name_copy(X509_get_issuer_name((X509 *)cert));
160 return mod ? tls_field_from_dn(cp, mod) : cp;
164 tls_cert_not_before(void * cert, uschar * mod)
166 return asn1_time_copy(X509_get_notBefore((X509 *)cert), mod);
170 tls_cert_not_after(void * cert, uschar * mod)
172 return asn1_time_copy(X509_get_notAfter((X509 *)cert), mod);
176 tls_cert_serial_number(void * cert, uschar * mod)
179 BIO * bp = BIO_new(BIO_s_mem());
182 if (!bp) return badalloc();
184 len = i2a_ASN1_INTEGER(bp, X509_get_serialNumber((X509 *)cert));
185 if (len < sizeof(txt))
186 BIO_read(bp, txt, len);
190 return string_copynlc(txt, len); /* lowercase */
194 tls_cert_signature(void * cert, uschar * mod)
197 BIO * bp = BIO_new(BIO_s_mem());
199 if (!bp) return badalloc();
201 if (X509_print_ex(bp, (X509 *)cert, 0,
202 X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION | X509_FLAG_NO_SERIAL |
203 X509_FLAG_NO_SIGNAME | X509_FLAG_NO_ISSUER | X509_FLAG_NO_VALIDITY |
204 X509_FLAG_NO_SUBJECT | X509_FLAG_NO_PUBKEY | X509_FLAG_NO_EXTENSIONS |
205 /* X509_FLAG_NO_SIGDUMP is the missing one */
206 X509_FLAG_NO_AUX) == 1)
208 long len = BIO_get_mem_data(bp, &cp);
209 cp = string_copyn(cp, len);
216 tls_cert_signature_algorithm(void * cert, uschar * mod)
218 return string_copy(US OBJ_nid2ln(X509_get_signature_type((X509 *)cert)));
222 tls_cert_subject(void * cert, uschar * mod)
224 uschar * cp = x509_name_copy(X509_get_subject_name((X509 *)cert));
225 return mod ? tls_field_from_dn(cp, mod) : cp;
229 tls_cert_version(void * cert, uschar * mod)
231 return string_sprintf("%d", X509_get_version((X509 *)cert));
235 tls_cert_ext_by_oid(void * cert, uschar * oid, int idx)
237 int nid = OBJ_create(CS oid, "", "");
238 int nidx = X509_get_ext_by_NID((X509 *)cert, nid, idx);
239 X509_EXTENSION * ex = X509_get_ext((X509 *)cert, nidx);
240 ASN1_OCTET_STRING * adata = X509_EXTENSION_get_data(ex);
241 BIO * bp = BIO_new(BIO_s_mem());
247 if (!bp) return badalloc();
249 M_ASN1_OCTET_STRING_print(bp, adata);
250 /* binary data, DER encoded */
252 /* just dump for now */
253 len = BIO_get_mem_data(bp, &cp1);
254 cp3 = cp2 = store_get(len*3+1);
258 sprintf(CS cp2, "%.2x ", *cp1++);
268 tls_cert_subject_altname(void * cert, uschar * mod)
270 uschar * list = NULL;
271 STACK_OF(GENERAL_NAME) * san = (STACK_OF(GENERAL_NAME) *)
272 X509_get_ext_d2i((X509 *)cert, NID_subject_alt_name, NULL, NULL);
279 if (!san) return NULL;
283 if (*mod == '>' && *++mod) sep = *mod++;
284 else if (Ustrcmp(mod, "dns")==0) { match = GEN_DNS; mod += 3; }
285 else if (Ustrcmp(mod, "uri")==0) { match = GEN_URI; mod += 3; }
286 else if (Ustrcmp(mod, "mail")==0) { match = GEN_EMAIL; mod += 4; }
293 while (sk_GENERAL_NAME_num(san) > 0)
295 GENERAL_NAME * namePart = sk_GENERAL_NAME_pop(san);
296 if (match != -1 && match != namePart->type)
298 switch (namePart->type)
302 ele = ASN1_STRING_data(namePart->d.dNSName);
303 len = ASN1_STRING_length(namePart->d.dNSName);
307 ele = ASN1_STRING_data(namePart->d.uniformResourceIdentifier);
308 len = ASN1_STRING_length(namePart->d.uniformResourceIdentifier);
312 ele = ASN1_STRING_data(namePart->d.rfc822Name);
313 len = ASN1_STRING_length(namePart->d.rfc822Name);
316 continue; /* ignore unrecognised types */
318 if (ele[len]) /* not nul-terminated */
319 ele = string_copyn(ele, len);
321 if (strnlen(CS ele, len) == len) /* ignore any with embedded nul */
322 list = string_append_listele(list, sep,
323 match == -1 ? string_sprintf("%s=%s", tag, ele) : ele);
326 sk_GENERAL_NAME_free(san);
331 tls_cert_ocsp_uri(void * cert, uschar * mod)
333 STACK_OF(ACCESS_DESCRIPTION) * ads = (STACK_OF(ACCESS_DESCRIPTION) *)
334 X509_get_ext_d2i((X509 *)cert, NID_info_access, NULL, NULL);
335 int adsnum = sk_ACCESS_DESCRIPTION_num(ads);
338 uschar * list = NULL;
341 if (*mod == '>' && *++mod) sep = *mod++;
343 for (i = 0; i < adsnum; i++)
345 ACCESS_DESCRIPTION * ad = sk_ACCESS_DESCRIPTION_value(ads, i);
347 if (ad && OBJ_obj2nid(ad->method) == NID_ad_OCSP)
348 list = string_append_listele(list, sep,
349 ASN1_STRING_data(ad->location->d.ia5));
355 tls_cert_crl_uri(void * cert, uschar * mod)
357 STACK_OF(DIST_POINT) * dps = (STACK_OF(DIST_POINT) *)
358 X509_get_ext_d2i((X509 *)cert, NID_crl_distribution_points,
361 int dpsnum = sk_DIST_POINT_num(dps);
364 uschar * list = NULL;
367 if (*mod == '>' && *++mod) sep = *mod++;
369 if (dps) for (i = 0; i < dpsnum; i++)
370 if ((dp = sk_DIST_POINT_value(dps, i)))
372 STACK_OF(GENERAL_NAME) * names = dp->distpoint->name.fullname;
374 int nnum = sk_GENERAL_NAME_num(names);
377 for (j = 0; j < nnum; j++)
378 if ( (np = sk_GENERAL_NAME_value(names, j))
379 && np->type == GEN_URI
381 list = string_append_listele(list, sep,
382 ASN1_STRING_data(np->d.uniformResourceIdentifier));
389 /*****************************************************
390 * Certificate operator routines
391 *****************************************************/
393 fingerprint(X509 * cert, const EVP_MD * fdig)
397 uschar md[EVP_MAX_MD_SIZE];
400 if (!X509_digest(cert,fdig,md,&n))
402 expand_string_message = US"tls_cert_fprt: out of mem\n";
405 cp = store_get(n*2+1);
406 for (j = 0; j < (int)n; j++) sprintf(CS cp+2*j, "%02X", md[j]);
411 tls_cert_fprt_md5(void * cert)
413 return fingerprint((X509 *)cert, EVP_md5());
417 tls_cert_fprt_sha1(void * cert)
419 return fingerprint((X509 *)cert, EVP_sha1());
423 tls_cert_fprt_sha256(void * cert)
425 return fingerprint((X509 *)cert, EVP_sha256());
431 /* End of tlscert-openssl.c */