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
);
60 bp
= BIO_new_mem_buf(US cp
, -1);
61 x
= PEM_read_bio_X509(bp
, NULL
, 0, NULL
);
68 store_reset(reset_point
);
73 tls_free_cert(void * cert
)
75 X509_free((X509
*)cert
);
78 /*****************************************************
79 * Certificate field extraction routines
80 *****************************************************/
82 bio_string_copy(BIO
* bp
, int len
)
85 len
= len
> 0 ? (int) BIO_get_mem_data(bp
, &cp
) : 0;
86 cp
= string_copyn(cp
, len
);
92 asn1_time_copy(const ASN1_TIME
* time
)
94 BIO
* bp
= BIO_new(BIO_s_mem());
95 int len
= ASN1_TIME_print(bp
, time
);
96 return bio_string_copy(bp
, len
);
100 x509_name_copy(X509_NAME
* name
)
102 BIO
* bp
= BIO_new(BIO_s_mem());
104 X509_NAME_print_ex(bp
, name
, 0, XN_FLAG_RFC2253
) >= 0
106 return bio_string_copy(bp
, len_good
);
112 tls_cert_issuer(void * cert
, uschar
* mod
)
114 uschar
* cp
= x509_name_copy(X509_get_issuer_name((X509
*)cert
));
115 return mod
? tls_field_from_dn(cp
, mod
) : cp
;
119 tls_cert_not_before(void * cert
, uschar
* mod
)
121 return asn1_time_copy(X509_get_notBefore((X509
*)cert
));
125 tls_cert_not_after(void * cert
, uschar
* mod
)
127 return asn1_time_copy(X509_get_notAfter((X509
*)cert
));
131 tls_cert_serial_number(void * cert
, uschar
* mod
)
134 BIO
* bp
= BIO_new(BIO_s_mem());
135 int len
= i2a_ASN1_INTEGER(bp
, X509_get_serialNumber((X509
*)cert
));
137 if (len
< sizeof(txt
))
138 BIO_read(bp
, txt
, len
);
142 return string_copynlc(txt
, len
); /* lowercase */
146 tls_cert_signature(void * cert
, uschar
* mod
)
148 BIO
* bp
= BIO_new(BIO_s_mem());
151 if (X509_print_ex(bp
, (X509
*)cert
, 0,
152 X509_FLAG_NO_HEADER
| X509_FLAG_NO_VERSION
| X509_FLAG_NO_SERIAL
|
153 X509_FLAG_NO_SIGNAME
| X509_FLAG_NO_ISSUER
| X509_FLAG_NO_VALIDITY
|
154 X509_FLAG_NO_SUBJECT
| X509_FLAG_NO_PUBKEY
| X509_FLAG_NO_EXTENSIONS
|
155 /* X509_FLAG_NO_SIGDUMP is the missing one */
156 X509_FLAG_NO_AUX
) == 1)
158 long len
= BIO_get_mem_data(bp
, &cp
);
159 cp
= string_copyn(cp
, len
);
166 tls_cert_signature_algorithm(void * cert
, uschar
* mod
)
168 return string_copy(US
OBJ_nid2ln(X509_get_signature_type((X509
*)cert
)));
172 tls_cert_subject(void * cert
, uschar
* mod
)
174 uschar
* cp
= x509_name_copy(X509_get_subject_name((X509
*)cert
));
175 return mod
? tls_field_from_dn(cp
, mod
) : cp
;
179 tls_cert_version(void * cert
, uschar
* mod
)
181 return string_sprintf("%d", X509_get_version((X509
*)cert
));
185 tls_cert_ext_by_oid(void * cert
, uschar
* oid
, int idx
)
187 int nid
= OBJ_create(CS oid
, "", "");
188 int nidx
= X509_get_ext_by_NID((X509
*)cert
, nid
, idx
);
189 X509_EXTENSION
* ex
= X509_get_ext((X509
*)cert
, nidx
);
190 ASN1_OCTET_STRING
* adata
= X509_EXTENSION_get_data(ex
);
191 BIO
* bp
= BIO_new(BIO_s_mem());
197 M_ASN1_OCTET_STRING_print(bp
, adata
);
198 /* binary data, DER encoded */
200 /* just dump for now */
201 len
= BIO_get_mem_data(bp
, &cp1
);
202 cp3
= cp2
= store_get(len
*3+1);
206 sprintf(CS cp2
, "%.2x ", *cp1
++);
216 tls_cert_subject_altname(void * cert
, uschar
* mod
)
218 uschar
* list
= NULL
;
219 STACK_OF(GENERAL_NAME
) * san
= (STACK_OF(GENERAL_NAME
) *)
220 X509_get_ext_d2i((X509
*)cert
, NID_subject_alt_name
, NULL
, NULL
);
226 if (!san
) return NULL
;
230 if (*mod
== '>' && *++mod
) sep
= *mod
++;
231 else if (Ustrcmp(mod
, "dns")==0) { match
= GEN_DNS
; mod
+= 3; }
232 else if (Ustrcmp(mod
, "uri")==0) { match
= GEN_URI
; mod
+= 3; }
233 else if (Ustrcmp(mod
, "mail")==0) { match
= GEN_EMAIL
; mod
+= 4; }
240 while (sk_GENERAL_NAME_num(san
) > 0)
242 GENERAL_NAME
* namePart
= sk_GENERAL_NAME_pop(san
);
243 if (match
!= -1 && match
!= namePart
->type
)
245 switch (namePart
->type
)
249 ele
= ASN1_STRING_data(namePart
->d
.dNSName
);
253 ele
= ASN1_STRING_data(namePart
->d
.uniformResourceIdentifier
);
257 ele
= ASN1_STRING_data(namePart
->d
.rfc822Name
);
260 continue; /* ignore unrecognised types */
262 list
= string_append_listele(list
, sep
,
263 match
== -1 ? string_sprintf("%s=%s", tag
, ele
) : ele
);
266 sk_GENERAL_NAME_free(san
);
271 tls_cert_ocsp_uri(void * cert
, uschar
* mod
)
273 STACK_OF(ACCESS_DESCRIPTION
) * ads
= (STACK_OF(ACCESS_DESCRIPTION
) *)
274 X509_get_ext_d2i((X509
*)cert
, NID_info_access
, NULL
, NULL
);
275 int adsnum
= sk_ACCESS_DESCRIPTION_num(ads
);
278 uschar
* list
= NULL
;
281 if (*mod
== '>' && *++mod
) sep
= *mod
++;
283 for (i
= 0; i
< adsnum
; i
++)
285 ACCESS_DESCRIPTION
* ad
= sk_ACCESS_DESCRIPTION_value(ads
, i
);
287 if (ad
&& OBJ_obj2nid(ad
->method
) == NID_ad_OCSP
)
288 list
= string_append_listele(list
, sep
,
289 ASN1_STRING_data(ad
->location
->d
.ia5
));
295 tls_cert_crl_uri(void * cert
, uschar
* mod
)
297 STACK_OF(DIST_POINT
) * dps
= (STACK_OF(DIST_POINT
) *)
298 X509_get_ext_d2i((X509
*)cert
, NID_crl_distribution_points
,
301 int dpsnum
= sk_DIST_POINT_num(dps
);
304 uschar
* list
= NULL
;
307 if (*mod
== '>' && *++mod
) sep
= *mod
++;
309 if (dps
) for (i
= 0; i
< dpsnum
; i
++)
310 if ((dp
= sk_DIST_POINT_value(dps
, i
)))
312 STACK_OF(GENERAL_NAME
) * names
= dp
->distpoint
->name
.fullname
;
314 int nnum
= sk_GENERAL_NAME_num(names
);
317 for (j
= 0; j
< nnum
; j
++)
318 if ( (np
= sk_GENERAL_NAME_value(names
, j
))
319 && np
->type
== GEN_URI
321 list
= string_append_listele(list
, sep
,
322 ASN1_STRING_data(np
->d
.uniformResourceIdentifier
));
329 /*****************************************************
330 * Certificate operator routines
331 *****************************************************/
333 fingerprint(X509
* cert
, const EVP_MD
* fdig
)
337 uschar md
[EVP_MAX_MD_SIZE
];
340 if (!X509_digest(cert
,fdig
,md
,&n
))
342 expand_string_message
= US
"tls_cert_fprt: out of mem\n";
345 cp
= store_get(n
*2+1);
346 for (j
= 0; j
< (int)n
; j
++) sprintf(CS cp
+2*j
, "%02X", md
[j
]);
351 tls_cert_fprt_md5(void * cert
)
353 return fingerprint((X509
*)cert
, EVP_md5());
357 tls_cert_fprt_sha1(void * cert
)
359 return fingerprint((X509
*)cert
, EVP_sha1());
363 tls_cert_fprt_sha256(void * cert
)
365 return fingerprint((X509
*)cert
, EVP_sha256());
371 /* End of tlscert-openssl.c */