1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Jeremy Harris 2014 - 2016 */
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>
20 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
21 # define EXIM_HAVE_ASN1_MACROS
25 /*****************************************************
26 * Export/import a certificate, binary/printable
27 *****************************************************/
29 tls_export_cert(uschar
* buf
, size_t buflen
, void * cert
)
31 BIO
* bp
= BIO_new(BIO_s_mem());
34 if ((fail
= PEM_write_bio_X509(bp
, (X509
*)cert
) ? 0 : 1))
35 log_write(0, LOG_MAIN
, "TLS error in certificate export: %s",
36 ERR_error_string(ERR_get_error(), NULL
));
44 if ((n
= BIO_gets(bp
, cp
, (int)buflen
)) <= 0) break;
47 cp
[-2] = '\\'; cp
[-1] = 'n'; /* newline->"\n" */
48 } /* compat with string_printing() */
57 tls_import_cert(const uschar
* buf
, void ** cert
)
59 void * reset_point
= store_get(0);
60 const uschar
* cp
= string_unprinting(US buf
);
62 X509
* x
= *(X509
**)cert
;
67 bp
= BIO_new_mem_buf(US cp
, -1);
68 if (!(x
= PEM_read_bio_X509(bp
, NULL
, 0, NULL
)))
70 log_write(0, LOG_MAIN
, "TLS error in certificate import: %s",
71 ERR_error_string(ERR_get_error(), NULL
));
77 store_reset(reset_point
);
82 tls_free_cert(void ** cert
)
84 X509
* x
= *(X509
**)cert
;
93 /*****************************************************
94 * Certificate field extraction routines
95 *****************************************************/
97 /* First, some internal service functions */
102 expand_string_message
= US
"allocation failure";
107 bio_string_copy(BIO
* bp
, int len
)
110 len
= len
> 0 ? (int) BIO_get_mem_data(bp
, &cp
) : 0;
111 cp
= string_copyn(cp
, len
);
117 asn1_time_copy(const ASN1_TIME
* asntime
, uschar
* mod
)
120 BIO
* bp
= BIO_new(BIO_s_mem());
125 len
= ASN1_TIME_print(bp
, asntime
);
126 len
= len
> 0 ? (int) BIO_get_mem_data(bp
, CSS
&s
) : 0;
128 if (mod
&& Ustrcmp(mod
, "raw") == 0) /* native ASN */
129 s
= string_copyn(s
, len
);
133 struct tm
* tm_p
= &tm
;
135 uschar
* tz
= to_tz(US
"GMT0"); /* need to call strptime with baseline TZ */
137 /* Parse OpenSSL ASN1_TIME_print output. A shame there seems to
138 be no other interface for the times.
141 /*XXX %Z might be glibc-specific? Solaris has it, at least*/
142 /*XXX should we switch to POSIX locale for this? */
144 if (!len
|| !strptime(CCS s
, "%b %e %T %Y %Z", &tm
))
145 expand_string_message
= US
"failed time conversion";
149 time_t t
= mktime(&tm
); /* make the tm self-consistent */
151 if (mod
&& Ustrcmp(mod
, "int") == 0) /* seconds since epoch */
152 s
= string_sprintf("%u", t
);
156 if (!timestamps_utc
) /* decoded string in local TZ */
157 { /* shift to local TZ */
160 tm_p
= localtime(&t
);
162 /* "utc" is default, and rfc5280 says cert times should be Zulu */
164 /* convert to string in our format */
167 strftime(CS s
, (size_t)len
, "%b %e %T %Y %z", tm_p
);
179 x509_name_copy(X509_NAME
* name
)
181 BIO
* bp
= BIO_new(BIO_s_mem());
184 if (!bp
) return badalloc();
187 X509_NAME_print_ex(bp
, name
, 0, XN_FLAG_RFC2253
) >= 0
189 return bio_string_copy(bp
, len_good
);
193 /* Now the extractors, called from expand.c
196 mod Optional modifiers for the operator
199 Allocated string with extracted value
203 tls_cert_issuer(void * cert
, uschar
* mod
)
205 uschar
* cp
= x509_name_copy(X509_get_issuer_name((X509
*)cert
));
206 return mod
? tls_field_from_dn(cp
, mod
) : cp
;
210 tls_cert_not_before(void * cert
, uschar
* mod
)
212 return asn1_time_copy(X509_get_notBefore((X509
*)cert
), mod
);
216 tls_cert_not_after(void * cert
, uschar
* mod
)
218 return asn1_time_copy(X509_get_notAfter((X509
*)cert
), mod
);
222 tls_cert_serial_number(void * cert
, uschar
* mod
)
225 BIO
* bp
= BIO_new(BIO_s_mem());
228 if (!bp
) return badalloc();
230 len
= i2a_ASN1_INTEGER(bp
, X509_get_serialNumber((X509
*)cert
));
231 if (len
< sizeof(txt
))
232 BIO_read(bp
, txt
, len
);
236 return string_copynlc(txt
, len
); /* lowercase */
240 tls_cert_signature(void * cert
, uschar
* mod
)
243 BIO
* bp
= BIO_new(BIO_s_mem());
245 if (!bp
) return badalloc();
247 if (X509_print_ex(bp
, (X509
*)cert
, 0,
248 X509_FLAG_NO_HEADER
| X509_FLAG_NO_VERSION
| X509_FLAG_NO_SERIAL
|
249 X509_FLAG_NO_SIGNAME
| X509_FLAG_NO_ISSUER
| X509_FLAG_NO_VALIDITY
|
250 X509_FLAG_NO_SUBJECT
| X509_FLAG_NO_PUBKEY
| X509_FLAG_NO_EXTENSIONS
|
251 /* X509_FLAG_NO_SIGDUMP is the missing one */
252 X509_FLAG_NO_AUX
) == 1)
254 long len
= BIO_get_mem_data(bp
, &cp
);
256 /* Strip leading "Signature Algorithm" line */
257 while (*cp
&& *cp
!= '\n') { cp
++; len
--; }
259 cp
= string_copyn(cp
+1, len
-1);
266 tls_cert_signature_algorithm(void * cert
, uschar
* mod
)
269 BIO
* bp
= BIO_new(BIO_s_mem());
271 if (!bp
) return badalloc();
273 if (X509_print_ex(bp
, (X509
*)cert
, 0,
274 X509_FLAG_NO_HEADER
| X509_FLAG_NO_VERSION
| X509_FLAG_NO_SERIAL
|
275 /* X509_FLAG_NO_SIGNAME is the missing one */
276 X509_FLAG_NO_ISSUER
| X509_FLAG_NO_VALIDITY
|
277 X509_FLAG_NO_SUBJECT
| X509_FLAG_NO_PUBKEY
| X509_FLAG_NO_EXTENSIONS
|
278 X509_FLAG_NO_SIGDUMP
| X509_FLAG_NO_AUX
) == 1)
280 long len
= BIO_get_mem_data(bp
, &cp
);
282 /* Strip leading " Signature Algorithm: " and trailing newline */
283 while (*cp
&& *cp
!= ':') { cp
++; len
--; }
284 do { cp
++; len
--; } while (*cp
&& *cp
== ' ');
285 if (cp
[len
-1] == '\n') len
--;
287 cp
= string_copyn(cp
, len
);
294 tls_cert_subject(void * cert
, uschar
* mod
)
296 uschar
* cp
= x509_name_copy(X509_get_subject_name((X509
*)cert
));
297 return mod
? tls_field_from_dn(cp
, mod
) : cp
;
301 tls_cert_version(void * cert
, uschar
* mod
)
303 return string_sprintf("%d", X509_get_version((X509
*)cert
));
307 tls_cert_ext_by_oid(void * cert
, uschar
* oid
, int idx
)
309 int nid
= OBJ_create(CS oid
, "", "");
310 int nidx
= X509_get_ext_by_NID((X509
*)cert
, nid
, idx
);
311 X509_EXTENSION
* ex
= X509_get_ext((X509
*)cert
, nidx
);
312 ASN1_OCTET_STRING
* adata
= X509_EXTENSION_get_data(ex
);
313 BIO
* bp
= BIO_new(BIO_s_mem());
319 if (!bp
) return badalloc();
321 #ifdef EXIM_HAVE_ASN1_MACROS
322 ASN1_STRING_print(bp
, adata
);
324 M_ASN1_OCTET_STRING_print(bp
, adata
);
327 /* binary data, DER encoded */
328 /* just dump for now */
329 len
= BIO_get_mem_data(bp
, &cp1
);
330 cp3
= cp2
= store_get(len
*3+1);
334 sprintf(CS cp2
, "%.2x ", *cp1
++);
344 tls_cert_subject_altname(void * cert
, uschar
* mod
)
346 uschar
* list
= NULL
;
347 STACK_OF(GENERAL_NAME
) * san
= (STACK_OF(GENERAL_NAME
) *)
348 X509_get_ext_d2i((X509
*)cert
, NID_subject_alt_name
, NULL
, NULL
);
355 if (!san
) return NULL
;
359 if (*mod
== '>' && *++mod
) osep
= *mod
++;
360 else if (Ustrncmp(mod
,"dns",3)==0) { match
= GEN_DNS
; mod
+= 3; }
361 else if (Ustrncmp(mod
,"uri",3)==0) { match
= GEN_URI
; mod
+= 3; }
362 else if (Ustrncmp(mod
,"mail",4)==0) { match
= GEN_EMAIL
; mod
+= 4; }
365 if (*mod
== ',') mod
++;
368 while (sk_GENERAL_NAME_num(san
) > 0)
370 GENERAL_NAME
* namePart
= sk_GENERAL_NAME_pop(san
);
371 if (match
!= -1 && match
!= namePart
->type
)
373 switch (namePart
->type
)
377 ele
= ASN1_STRING_data(namePart
->d
.dNSName
);
378 len
= ASN1_STRING_length(namePart
->d
.dNSName
);
382 ele
= ASN1_STRING_data(namePart
->d
.uniformResourceIdentifier
);
383 len
= ASN1_STRING_length(namePart
->d
.uniformResourceIdentifier
);
387 ele
= ASN1_STRING_data(namePart
->d
.rfc822Name
);
388 len
= ASN1_STRING_length(namePart
->d
.rfc822Name
);
391 continue; /* ignore unrecognised types */
393 if (ele
[len
]) /* not nul-terminated */
394 ele
= string_copyn(ele
, len
);
396 if (Ustrlen(ele
) == len
) /* ignore any with embedded nul */
397 list
= string_append_listele(list
, osep
,
398 match
== -1 ? string_sprintf("%s=%s", tag
, ele
) : ele
);
401 sk_GENERAL_NAME_free(san
);
406 tls_cert_ocsp_uri(void * cert
, uschar
* mod
)
408 STACK_OF(ACCESS_DESCRIPTION
) * ads
= (STACK_OF(ACCESS_DESCRIPTION
) *)
409 X509_get_ext_d2i((X509
*)cert
, NID_info_access
, NULL
, NULL
);
410 int adsnum
= sk_ACCESS_DESCRIPTION_num(ads
);
413 uschar
* list
= NULL
;
416 if (*mod
== '>' && *++mod
) sep
= *mod
++;
418 for (i
= 0; i
< adsnum
; i
++)
420 ACCESS_DESCRIPTION
* ad
= sk_ACCESS_DESCRIPTION_value(ads
, i
);
422 if (ad
&& OBJ_obj2nid(ad
->method
) == NID_ad_OCSP
)
424 uschar
* ele
= ASN1_STRING_data(ad
->location
->d
.ia5
);
425 int len
= ASN1_STRING_length(ad
->location
->d
.ia5
);
426 list
= string_append_listele_n(list
, sep
, ele
, len
);
429 sk_ACCESS_DESCRIPTION_free(ads
);
434 tls_cert_crl_uri(void * cert
, uschar
* mod
)
436 STACK_OF(DIST_POINT
) * dps
= (STACK_OF(DIST_POINT
) *)
437 X509_get_ext_d2i((X509
*)cert
, NID_crl_distribution_points
,
440 int dpsnum
= sk_DIST_POINT_num(dps
);
443 uschar
* list
= NULL
;
446 if (*mod
== '>' && *++mod
) sep
= *mod
++;
448 if (dps
) for (i
= 0; i
< dpsnum
; i
++)
449 if ((dp
= sk_DIST_POINT_value(dps
, i
)))
451 STACK_OF(GENERAL_NAME
) * names
= dp
->distpoint
->name
.fullname
;
453 int nnum
= sk_GENERAL_NAME_num(names
);
456 for (j
= 0; j
< nnum
; j
++)
457 if ( (np
= sk_GENERAL_NAME_value(names
, j
))
458 && np
->type
== GEN_URI
461 uschar
* ele
= ASN1_STRING_data(np
->d
.uniformResourceIdentifier
);
462 int len
= ASN1_STRING_length(np
->d
.uniformResourceIdentifier
);
463 list
= string_append_listele_n(list
, sep
, ele
, len
);
466 sk_DIST_POINT_free(dps
);
472 /*****************************************************
473 * Certificate operator routines
474 *****************************************************/
476 tls_cert_der_b64(void * cert
)
478 BIO
* bp
= BIO_new(BIO_s_mem());
481 if (!i2d_X509_bio(bp
, (X509
*)cert
))
482 log_write(0, LOG_MAIN
, "TLS error in certificate export: %s",
483 ERR_error_string(ERR_get_error(), NULL
));
486 long len
= BIO_get_mem_data(bp
, &cp
);
487 cp
= b64encode(cp
, (int)len
);
496 fingerprint(X509
* cert
, const EVP_MD
* fdig
)
500 uschar md
[EVP_MAX_MD_SIZE
];
503 if (!X509_digest(cert
,fdig
,md
,&n
))
505 expand_string_message
= US
"tls_cert_fprt: out of mem\n";
508 cp
= store_get(n
*2+1);
509 for (j
= 0; j
< (int)n
; j
++) sprintf(CS cp
+2*j
, "%02X", md
[j
]);
514 tls_cert_fprt_md5(void * cert
)
516 return fingerprint((X509
*)cert
, EVP_md5());
520 tls_cert_fprt_sha1(void * cert
)
522 return fingerprint((X509
*)cert
, EVP_sha1());
526 tls_cert_fprt_sha256(void * cert
)
528 return fingerprint((X509
*)cert
, EVP_sha256());
534 /* End of tlscert-openssl.c */