1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Jeremy Harris 2014 - 2015 */
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
);
58 X509
* x
= *(X509
**)cert
;
63 bp
= BIO_new_mem_buf(US cp
, -1);
64 if (!(x
= PEM_read_bio_X509(bp
, NULL
, 0, NULL
)))
66 log_write(0, LOG_MAIN
, "TLS error in certificate import: %s",
67 ERR_error_string(ERR_get_error(), NULL
));
73 store_reset(reset_point
);
78 tls_free_cert(void * cert
)
80 X509_free((X509
*)cert
);
84 /*****************************************************
85 * Certificate field extraction routines
86 *****************************************************/
88 /* First, some internal service functions */
93 expand_string_message
= US
"allocation failure";
98 bio_string_copy(BIO
* bp
, int len
)
101 len
= len
> 0 ? (int) BIO_get_mem_data(bp
, &cp
) : 0;
102 cp
= string_copyn(cp
, len
);
108 asn1_time_copy(const ASN1_TIME
* asntime
, uschar
* mod
)
111 BIO
* bp
= BIO_new(BIO_s_mem());
116 len
= ASN1_TIME_print(bp
, asntime
);
117 len
= len
> 0 ? (int) BIO_get_mem_data(bp
, &s
) : 0;
119 if (mod
&& Ustrcmp(mod
, "raw") == 0) /* native ASN */
120 s
= string_copyn(s
, len
);
124 struct tm
* tm_p
= &tm
;
126 uschar
* tz
= to_tz(US
"GMT0"); /* need to call strptime with baseline TZ */
128 /* Parse OpenSSL ASN1_TIME_print output. A shame there seems to
129 be no other interface for the times.
132 /*XXX %Z might be glibc-specific? Solaris has it, at least*/
133 /*XXX should we switch to POSIX locale for this? */
135 if (!strptime(CCS s
, "%b %e %T %Y %Z", &tm
))
136 expand_string_message
= US
"failed time conversion";
140 time_t t
= mktime(&tm
); /* make the tm self-consistent */
142 if (mod
&& Ustrcmp(mod
, "int") == 0) /* seconds since epoch */
143 s
= string_sprintf("%u", t
);
147 if (!timestamps_utc
) /* decoded string in local TZ */
148 { /* shift to local TZ */
151 tm_p
= localtime(&t
);
153 /* "utc" is default, and rfc5280 says cert times should be Zulu */
155 /* convert to string in our format */
158 strftime(CS s
, (size_t)len
, "%b %e %T %Y %z", tm_p
);
170 x509_name_copy(X509_NAME
* name
)
172 BIO
* bp
= BIO_new(BIO_s_mem());
175 if (!bp
) return badalloc();
178 X509_NAME_print_ex(bp
, name
, 0, XN_FLAG_RFC2253
) >= 0
180 return bio_string_copy(bp
, len_good
);
184 /* Now the extractors, called from expand.c
187 mod Optional modifiers for the operator
190 Allocated string with extracted value
194 tls_cert_issuer(void * cert
, uschar
* mod
)
196 uschar
* cp
= x509_name_copy(X509_get_issuer_name((X509
*)cert
));
197 return mod
? tls_field_from_dn(cp
, mod
) : cp
;
201 tls_cert_not_before(void * cert
, uschar
* mod
)
203 return asn1_time_copy(X509_get_notBefore((X509
*)cert
), mod
);
207 tls_cert_not_after(void * cert
, uschar
* mod
)
209 return asn1_time_copy(X509_get_notAfter((X509
*)cert
), mod
);
213 tls_cert_serial_number(void * cert
, uschar
* mod
)
216 BIO
* bp
= BIO_new(BIO_s_mem());
219 if (!bp
) return badalloc();
221 len
= i2a_ASN1_INTEGER(bp
, X509_get_serialNumber((X509
*)cert
));
222 if (len
< sizeof(txt
))
223 BIO_read(bp
, txt
, len
);
227 return string_copynlc(txt
, len
); /* lowercase */
231 tls_cert_signature(void * cert
, uschar
* mod
)
234 BIO
* bp
= BIO_new(BIO_s_mem());
236 if (!bp
) return badalloc();
238 if (X509_print_ex(bp
, (X509
*)cert
, 0,
239 X509_FLAG_NO_HEADER
| X509_FLAG_NO_VERSION
| X509_FLAG_NO_SERIAL
|
240 X509_FLAG_NO_SIGNAME
| X509_FLAG_NO_ISSUER
| X509_FLAG_NO_VALIDITY
|
241 X509_FLAG_NO_SUBJECT
| X509_FLAG_NO_PUBKEY
| X509_FLAG_NO_EXTENSIONS
|
242 /* X509_FLAG_NO_SIGDUMP is the missing one */
243 X509_FLAG_NO_AUX
) == 1)
245 long len
= BIO_get_mem_data(bp
, &cp
);
247 /* Strip leading "Signature Algorithm" line */
248 while (*cp
&& *cp
!= '\n') { cp
++; len
--; }
250 cp
= string_copyn(cp
+1, len
-1);
257 tls_cert_signature_algorithm(void * cert
, uschar
* mod
)
260 BIO
* bp
= BIO_new(BIO_s_mem());
262 if (!bp
) return badalloc();
264 if (X509_print_ex(bp
, (X509
*)cert
, 0,
265 X509_FLAG_NO_HEADER
| X509_FLAG_NO_VERSION
| X509_FLAG_NO_SERIAL
|
266 /* X509_FLAG_NO_SIGNAME is the missing one */
267 X509_FLAG_NO_ISSUER
| X509_FLAG_NO_VALIDITY
|
268 X509_FLAG_NO_SUBJECT
| X509_FLAG_NO_PUBKEY
| X509_FLAG_NO_EXTENSIONS
|
269 X509_FLAG_NO_SIGDUMP
| X509_FLAG_NO_AUX
) == 1)
271 long len
= BIO_get_mem_data(bp
, &cp
);
273 /* Strip leading " Signature Algorithm: " and trailing newline */
274 while (*cp
&& *cp
!= ':') { cp
++; len
--; }
275 do { cp
++; len
--; } while (*cp
&& *cp
== ' ');
276 if (cp
[len
-1] == '\n') len
--;
278 cp
= string_copyn(cp
, len
);
285 tls_cert_subject(void * cert
, uschar
* mod
)
287 uschar
* cp
= x509_name_copy(X509_get_subject_name((X509
*)cert
));
288 return mod
? tls_field_from_dn(cp
, mod
) : cp
;
292 tls_cert_version(void * cert
, uschar
* mod
)
294 return string_sprintf("%d", X509_get_version((X509
*)cert
));
298 tls_cert_ext_by_oid(void * cert
, uschar
* oid
, int idx
)
300 int nid
= OBJ_create(CS oid
, "", "");
301 int nidx
= X509_get_ext_by_NID((X509
*)cert
, nid
, idx
);
302 X509_EXTENSION
* ex
= X509_get_ext((X509
*)cert
, nidx
);
303 ASN1_OCTET_STRING
* adata
= X509_EXTENSION_get_data(ex
);
304 BIO
* bp
= BIO_new(BIO_s_mem());
310 if (!bp
) return badalloc();
312 M_ASN1_OCTET_STRING_print(bp
, adata
);
313 /* binary data, DER encoded */
315 /* just dump for now */
316 len
= BIO_get_mem_data(bp
, &cp1
);
317 cp3
= cp2
= store_get(len
*3+1);
321 sprintf(CS cp2
, "%.2x ", *cp1
++);
331 tls_cert_subject_altname(void * cert
, uschar
* mod
)
333 uschar
* list
= NULL
;
334 STACK_OF(GENERAL_NAME
) * san
= (STACK_OF(GENERAL_NAME
) *)
335 X509_get_ext_d2i((X509
*)cert
, NID_subject_alt_name
, NULL
, NULL
);
342 if (!san
) return NULL
;
346 if (*mod
== '>' && *++mod
) osep
= *mod
++;
347 else if (Ustrncmp(mod
,"dns",3)==0) { match
= GEN_DNS
; mod
+= 3; }
348 else if (Ustrncmp(mod
,"uri",3)==0) { match
= GEN_URI
; mod
+= 3; }
349 else if (Ustrncmp(mod
,"mail",4)==0) { match
= GEN_EMAIL
; mod
+= 4; }
352 if (*mod
== ',') mod
++;
355 while (sk_GENERAL_NAME_num(san
) > 0)
357 GENERAL_NAME
* namePart
= sk_GENERAL_NAME_pop(san
);
358 if (match
!= -1 && match
!= namePart
->type
)
360 switch (namePart
->type
)
364 ele
= ASN1_STRING_data(namePart
->d
.dNSName
);
365 len
= ASN1_STRING_length(namePart
->d
.dNSName
);
369 ele
= ASN1_STRING_data(namePart
->d
.uniformResourceIdentifier
);
370 len
= ASN1_STRING_length(namePart
->d
.uniformResourceIdentifier
);
374 ele
= ASN1_STRING_data(namePart
->d
.rfc822Name
);
375 len
= ASN1_STRING_length(namePart
->d
.rfc822Name
);
378 continue; /* ignore unrecognised types */
380 if (ele
[len
]) /* not nul-terminated */
381 ele
= string_copyn(ele
, len
);
383 if (Ustrlen(ele
) == len
) /* ignore any with embedded nul */
384 list
= string_append_listele(list
, osep
,
385 match
== -1 ? string_sprintf("%s=%s", tag
, ele
) : ele
);
388 sk_GENERAL_NAME_free(san
);
393 tls_cert_ocsp_uri(void * cert
, uschar
* mod
)
395 STACK_OF(ACCESS_DESCRIPTION
) * ads
= (STACK_OF(ACCESS_DESCRIPTION
) *)
396 X509_get_ext_d2i((X509
*)cert
, NID_info_access
, NULL
, NULL
);
397 int adsnum
= sk_ACCESS_DESCRIPTION_num(ads
);
400 uschar
* list
= NULL
;
403 if (*mod
== '>' && *++mod
) sep
= *mod
++;
405 for (i
= 0; i
< adsnum
; i
++)
407 ACCESS_DESCRIPTION
* ad
= sk_ACCESS_DESCRIPTION_value(ads
, i
);
409 if (ad
&& OBJ_obj2nid(ad
->method
) == NID_ad_OCSP
)
411 uschar
* ele
= ASN1_STRING_data(ad
->location
->d
.ia5
);
412 int len
= ASN1_STRING_length(ad
->location
->d
.ia5
);
413 list
= string_append_listele_n(list
, sep
, ele
, len
);
416 sk_ACCESS_DESCRIPTION_free(ads
);
421 tls_cert_crl_uri(void * cert
, uschar
* mod
)
423 STACK_OF(DIST_POINT
) * dps
= (STACK_OF(DIST_POINT
) *)
424 X509_get_ext_d2i((X509
*)cert
, NID_crl_distribution_points
,
427 int dpsnum
= sk_DIST_POINT_num(dps
);
430 uschar
* list
= NULL
;
433 if (*mod
== '>' && *++mod
) sep
= *mod
++;
435 if (dps
) for (i
= 0; i
< dpsnum
; i
++)
436 if ((dp
= sk_DIST_POINT_value(dps
, i
)))
438 STACK_OF(GENERAL_NAME
) * names
= dp
->distpoint
->name
.fullname
;
440 int nnum
= sk_GENERAL_NAME_num(names
);
443 for (j
= 0; j
< nnum
; j
++)
444 if ( (np
= sk_GENERAL_NAME_value(names
, j
))
445 && np
->type
== GEN_URI
448 uschar
* ele
= ASN1_STRING_data(np
->d
.uniformResourceIdentifier
);
449 int len
= ASN1_STRING_length(np
->d
.uniformResourceIdentifier
);
450 list
= string_append_listele_n(list
, sep
, ele
, len
);
453 sk_DIST_POINT_free(dps
);
459 /*****************************************************
460 * Certificate operator routines
461 *****************************************************/
463 fingerprint(X509
* cert
, const EVP_MD
* fdig
)
467 uschar md
[EVP_MAX_MD_SIZE
];
470 if (!X509_digest(cert
,fdig
,md
,&n
))
472 expand_string_message
= US
"tls_cert_fprt: out of mem\n";
475 cp
= store_get(n
*2+1);
476 for (j
= 0; j
< (int)n
; j
++) sprintf(CS cp
+2*j
, "%02X", md
[j
]);
481 tls_cert_fprt_md5(void * cert
)
483 return fingerprint((X509
*)cert
, EVP_md5());
487 tls_cert_fprt_sha1(void * cert
)
489 return fingerprint((X509
*)cert
, EVP_sha1());
493 tls_cert_fprt_sha256(void * cert
)
495 return fingerprint((X509
*)cert
, EVP_sha256());
501 /* End of tlscert-openssl.c */