1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Jeremy Harris 2014 - 2015 */
7 /* This file provides TLS/SSL support for Exim using the GnuTLS library,
8 one of the available supported implementations. This file is #included into
9 tls.c when USE_GNUTLS has been set.
12 #include <gnutls/gnutls.h>
13 /* needed for cert checks in verification and DN extraction: */
14 #include <gnutls/x509.h>
15 /* needed to disable PKCS11 autoload unless requested */
16 #if GNUTLS_VERSION_NUMBER >= 0x020c00
17 # include <gnutls/pkcs11.h>
21 /*****************************************************
22 * Export/import a certificate, binary/printable
23 *****************************************************/
25 tls_export_cert(uschar
* buf
, size_t buflen
, void * cert
)
28 void * reset_point
= store_get(0);
32 if ((fail
= gnutls_x509_crt_export((gnutls_x509_crt_t
)cert
,
33 GNUTLS_X509_FMT_PEM
, buf
, &sz
)))
35 log_write(0, LOG_MAIN
, "TLS error in certificate export: %s",
36 gnutls_strerror(fail
));
39 if ((cp
= string_printing(buf
)) != buf
)
41 Ustrncpy(buf
, cp
, buflen
);
45 store_reset(reset_point
);
50 tls_import_cert(const uschar
* buf
, void ** cert
)
52 void * reset_point
= store_get(0);
54 gnutls_x509_crt_t crt
= *(gnutls_x509_crt_t
*)cert
;
58 gnutls_x509_crt_deinit(crt
);
62 gnutls_x509_crt_init(&crt
);
64 datum
.data
= string_unprinting(US buf
);
65 datum
.size
= Ustrlen(datum
.data
);
66 if ((fail
= gnutls_x509_crt_import(crt
, &datum
, GNUTLS_X509_FMT_PEM
)))
68 log_write(0, LOG_MAIN
, "TLS error in certificate import: %s",
69 gnutls_strerror(fail
));
75 store_reset(reset_point
);
80 tls_free_cert(void ** cert
)
82 gnutls_x509_crt_t crt
= *(gnutls_x509_crt_t
*)cert
;
85 gnutls_x509_crt_deinit(crt
);
86 gnutls_global_deinit();
91 /*****************************************************
92 * Certificate field extraction routines
93 *****************************************************/
95 /* First, some internal service functions */
98 g_err(const char * tag
, const char * from
, int gnutls_err
)
100 expand_string_message
= string_sprintf("%s: %s fail: %s\n",
101 from
, tag
, gnutls_strerror(gnutls_err
));
107 time_copy(time_t t
, uschar
* mod
)
112 if (mod
&& Ustrcmp(mod
, "int") == 0)
113 return string_sprintf("%u", (unsigned)t
);
118 uschar
* tz
= to_tz(US
"GMT0");
119 len
= strftime(CS cp
, len
, "%b %e %T %Y %Z", gmtime(&t
));
123 len
= strftime(CS cp
, len
, "%b %e %T %Y %Z", localtime(&t
));
124 return len
> 0 ? cp
: NULL
;
129 /* Now the extractors, called from expand.c
132 mod Optional modifiers for the operator
135 Allocated string with extracted value
139 tls_cert_issuer(void * cert
, uschar
* mod
)
145 if ((ret
= gnutls_x509_crt_get_issuer_dn(cert
, CS cp
, &siz
))
146 != GNUTLS_E_SHORT_MEMORY_BUFFER
)
147 return g_err("gi0", __FUNCTION__
, ret
);
150 if ((ret
= gnutls_x509_crt_get_issuer_dn(cert
, CS cp
, &siz
)) < 0)
151 return g_err("gi1", __FUNCTION__
, ret
);
153 return mod
? tls_field_from_dn(cp
, mod
) : cp
;
157 tls_cert_not_after(void * cert
, uschar
* mod
)
160 gnutls_x509_crt_get_expiration_time((gnutls_x509_crt_t
)cert
),
165 tls_cert_not_before(void * cert
, uschar
* mod
)
168 gnutls_x509_crt_get_activation_time((gnutls_x509_crt_t
)cert
),
173 tls_cert_serial_number(void * cert
, uschar
* mod
)
175 uschar bin
[50], txt
[150];
176 size_t sz
= sizeof(bin
);
181 if ((ret
= gnutls_x509_crt_get_serial((gnutls_x509_crt_t
)cert
,
183 return g_err("gs0", __FUNCTION__
, ret
);
185 for(dp
= txt
, sp
= bin
; sz
; sz
--)
186 dp
+= sprintf(CS dp
, "%.2x", *sp
++);
187 for(sp
= txt
; sp
[0]=='0' && sp
[1]; ) sp
++; /* leading zeroes */
188 return string_copy(sp
);
192 tls_cert_signature(void * cert
, uschar
* mod
)
200 if ((ret
= gnutls_x509_crt_get_signature((gnutls_x509_crt_t
)cert
, CS cp1
, &len
))
201 != GNUTLS_E_SHORT_MEMORY_BUFFER
)
202 return g_err("gs0", __FUNCTION__
, ret
);
204 cp1
= store_get(len
*4+1);
205 if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t
)cert
, CS cp1
, &len
) != 0)
206 return g_err("gs1", __FUNCTION__
, ret
);
208 for(cp3
= cp2
= cp1
+len
; cp1
< cp2
; cp1
++)
209 cp3
+= sprintf(CS cp3
, "%.2x ", *cp1
);
216 tls_cert_signature_algorithm(void * cert
, uschar
* mod
)
218 gnutls_sign_algorithm_t algo
=
219 gnutls_x509_crt_get_signature_algorithm((gnutls_x509_crt_t
)cert
);
220 return algo
< 0 ? NULL
: string_copy(US
gnutls_sign_get_name(algo
));
224 tls_cert_subject(void * cert
, uschar
* mod
)
230 if ((ret
= gnutls_x509_crt_get_dn(cert
, CS cp
, &siz
))
231 != GNUTLS_E_SHORT_MEMORY_BUFFER
)
232 return g_err("gs0", __FUNCTION__
, ret
);
235 if ((ret
= gnutls_x509_crt_get_dn(cert
, CS cp
, &siz
)) < 0)
236 return g_err("gs1", __FUNCTION__
, ret
);
238 return mod
? tls_field_from_dn(cp
, mod
) : cp
;
242 tls_cert_version(void * cert
, uschar
* mod
)
244 return string_sprintf("%d", gnutls_x509_crt_get_version(cert
));
248 tls_cert_ext_by_oid(void * cert
, uschar
* oid
, int idx
)
257 ret
= gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t
)cert
,
258 oid
, idx
, CS cp1
, &siz
, &crit
);
259 if (ret
!= GNUTLS_E_SHORT_MEMORY_BUFFER
)
260 return g_err("ge0", __FUNCTION__
, ret
);
262 cp1
= store_get(siz
*4 + 1);
264 ret
= gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t
)cert
,
265 oid
, idx
, CS cp1
, &siz
, &crit
);
267 return g_err("ge1", __FUNCTION__
, ret
);
269 /* binary data, DER encoded */
271 /* just dump for now */
272 for(cp3
= cp2
= cp1
+siz
; cp1
< cp2
; cp1
++)
273 cp3
+= sprintf(CS cp3
, "%.2x ", *cp1
);
280 tls_cert_subject_altname(void * cert
, uschar
* mod
)
282 uschar
* list
= NULL
;
283 int lsize
= 0, llen
= 0;
294 if (*mod
== '>' && *++mod
) sep
= *mod
++;
295 else if (Ustrcmp(mod
, "dns")==0) { match
= GNUTLS_SAN_DNSNAME
; mod
+= 3; }
296 else if (Ustrcmp(mod
, "uri")==0) { match
= GNUTLS_SAN_URI
; mod
+= 3; }
297 else if (Ustrcmp(mod
, "mail")==0) { match
= GNUTLS_SAN_RFC822NAME
; mod
+= 4; }
304 for(index
= 0;; index
++)
307 switch(ret
= gnutls_x509_crt_get_subject_alt_name(
308 (gnutls_x509_crt_t
)cert
, index
, NULL
, &siz
, NULL
))
310 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
:
311 return list
; /* no more elements; normal exit */
313 case GNUTLS_E_SHORT_MEMORY_BUFFER
:
317 return g_err("gs0", __FUNCTION__
, ret
);
320 ele
= store_get(siz
+1);
321 if ((ret
= gnutls_x509_crt_get_subject_alt_name(
322 (gnutls_x509_crt_t
)cert
, index
, ele
, &siz
, NULL
)) < 0)
323 return g_err("gs1", __FUNCTION__
, ret
);
326 if ( match
!= -1 && match
!= ret
/* wrong type of SAN */
327 || Ustrlen(ele
) != siz
) /* contains a NUL */
331 case GNUTLS_SAN_DNSNAME
: tag
= US
"DNS"; break;
332 case GNUTLS_SAN_URI
: tag
= US
"URI"; break;
333 case GNUTLS_SAN_RFC822NAME
: tag
= US
"MAIL"; break;
334 default: continue; /* ignore unrecognised types */
336 list
= string_append_listele(list
, &lsize
, &llen
, sep
,
337 match
== -1 ? string_sprintf("%s=%s", tag
, ele
) : ele
);
343 tls_cert_ocsp_uri(void * cert
, uschar
* mod
)
345 #if GNUTLS_VERSION_NUMBER >= 0x030000
350 uschar
* list
= NULL
;
351 int lsize
= 0, llen
= 0;
354 if (*mod
== '>' && *++mod
) sep
= *mod
++;
356 for(index
= 0;; index
++)
358 ret
= gnutls_x509_crt_get_authority_info_access((gnutls_x509_crt_t
)cert
,
359 index
, GNUTLS_IA_OCSP_URI
, &uri
, NULL
);
361 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
364 return g_err("gai", __FUNCTION__
, ret
);
366 list
= string_append_listele_n(list
, &lsize
, &llen
, sep
,
373 expand_string_message
=
374 string_sprintf("%s: OCSP support with GnuTLS requires version 3.0.0\n",
382 tls_cert_crl_uri(void * cert
, uschar
* mod
)
388 uschar
* list
= NULL
;
389 int lsize
= 0, llen
= 0;
393 if (*mod
== '>' && *++mod
) sep
= *mod
++;
395 for(index
= 0;; index
++)
398 switch(ret
= gnutls_x509_crt_get_crl_dist_points(
399 (gnutls_x509_crt_t
)cert
, index
, NULL
, &siz
, NULL
, NULL
))
401 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
:
403 case GNUTLS_E_SHORT_MEMORY_BUFFER
:
406 return g_err("gc0", __FUNCTION__
, ret
);
409 ele
= store_get(siz
);
410 if ((ret
= gnutls_x509_crt_get_crl_dist_points(
411 (gnutls_x509_crt_t
)cert
, index
, ele
, &siz
, NULL
, NULL
)) < 0)
412 return g_err("gc1", __FUNCTION__
, ret
);
414 list
= string_append_listele_n(list
, &lsize
, &llen
, sep
, ele
, siz
);
420 /*****************************************************
421 * Certificate operator routines
422 *****************************************************/
424 tls_cert_der_b64(void * cert
)
430 if ( (fail
= gnutls_x509_crt_export((gnutls_x509_crt_t
)cert
,
431 GNUTLS_X509_FMT_DER
, cp
, &len
)) != GNUTLS_E_SHORT_MEMORY_BUFFER
432 || !(cp
= store_get((int)len
))
433 || (fail
= gnutls_x509_crt_export((gnutls_x509_crt_t
)cert
,
434 GNUTLS_X509_FMT_DER
, cp
, &len
))
437 log_write(0, LOG_MAIN
, "TLS error in certificate export: %s",
438 gnutls_strerror(fail
));
441 return b64encode(cp
, (int)len
);
446 fingerprint(gnutls_x509_crt_t cert
, gnutls_digest_algorithm_t algo
)
454 if ((ret
= gnutls_x509_crt_get_fingerprint(cert
, algo
, NULL
, &siz
))
455 != GNUTLS_E_SHORT_MEMORY_BUFFER
)
456 return g_err("gf0", __FUNCTION__
, ret
);
458 cp
= store_get(siz
*3+1);
459 if ((ret
= gnutls_x509_crt_get_fingerprint(cert
, algo
, cp
, &siz
)) < 0)
460 return g_err("gf1", __FUNCTION__
, ret
);
462 for (cp3
= cp2
= cp
+siz
; cp
< cp2
; cp
++)
463 cp3
+= sprintf(CS cp3
, "%02X", *cp
);
469 tls_cert_fprt_md5(void * cert
)
471 return fingerprint((gnutls_x509_crt_t
)cert
, GNUTLS_DIG_MD5
);
475 tls_cert_fprt_sha1(void * cert
)
477 return fingerprint((gnutls_x509_crt_t
)cert
, GNUTLS_DIG_SHA1
);
481 tls_cert_fprt_sha256(void * cert
)
483 return fingerprint((gnutls_x509_crt_t
)cert
, GNUTLS_DIG_SHA256
);
489 /* End of tlscert-gnu.c */