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_deinit((gnutls_x509_crt_t) cert);
83 gnutls_global_deinit();
86 /*****************************************************
87 * Certificate field extraction routines
88 *****************************************************/
90 /* First, some internal service functions */
93 g_err(const char * tag, const char * from, int gnutls_err)
95 expand_string_message = string_sprintf("%s: %s fail: %s\n",
96 from, tag, gnutls_strerror(gnutls_err));
102 time_copy(time_t t, uschar * mod)
107 if (mod && Ustrcmp(mod, "int") == 0)
108 return string_sprintf("%u", (unsigned)t);
113 uschar * tz = to_tz(US"GMT0");
114 len = strftime(CS cp, len, "%b %e %T %Y %Z", gmtime(&t));
118 len = strftime(CS cp, len, "%b %e %T %Y %Z", localtime(&t));
119 return len > 0 ? cp : NULL;
124 /* Now the extractors, called from expand.c
127 mod Optional modifiers for the operator
130 Allocated string with extracted value
134 tls_cert_issuer(void * cert, uschar * mod)
140 if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz))
141 != GNUTLS_E_SHORT_MEMORY_BUFFER)
142 return g_err("gi0", __FUNCTION__, ret);
145 if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz)) < 0)
146 return g_err("gi1", __FUNCTION__, ret);
148 return mod ? tls_field_from_dn(cp, mod) : cp;
152 tls_cert_not_after(void * cert, uschar * mod)
155 gnutls_x509_crt_get_expiration_time((gnutls_x509_crt_t)cert),
160 tls_cert_not_before(void * cert, uschar * mod)
163 gnutls_x509_crt_get_activation_time((gnutls_x509_crt_t)cert),
168 tls_cert_serial_number(void * cert, uschar * mod)
170 uschar bin[50], txt[150];
171 size_t sz = sizeof(bin);
176 if ((ret = gnutls_x509_crt_get_serial((gnutls_x509_crt_t)cert,
178 return g_err("gs0", __FUNCTION__, ret);
180 for(dp = txt, sp = bin; sz; dp += 2, sp++, sz--)
181 sprintf(dp, "%.2x", *sp);
182 for(sp = txt; sp[0]=='0' && sp[1]; ) sp++; /* leading zeroes */
183 return string_copy(sp);
187 tls_cert_signature(void * cert, uschar * mod)
195 if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len))
196 != GNUTLS_E_SHORT_MEMORY_BUFFER)
197 return g_err("gs0", __FUNCTION__, ret);
199 cp1 = store_get(len*4+1);
200 if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len) != 0)
201 return g_err("gs1", __FUNCTION__, ret);
203 for(cp3 = cp2 = cp1+len; cp1 < cp2; cp3 += 3, cp1++)
204 sprintf(cp3, "%.2x ", *cp1);
211 tls_cert_signature_algorithm(void * cert, uschar * mod)
213 gnutls_sign_algorithm_t algo =
214 gnutls_x509_crt_get_signature_algorithm((gnutls_x509_crt_t)cert);
215 return algo < 0 ? NULL : string_copy(gnutls_sign_get_name(algo));
219 tls_cert_subject(void * cert, uschar * mod)
225 if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz))
226 != GNUTLS_E_SHORT_MEMORY_BUFFER)
227 return g_err("gs0", __FUNCTION__, ret);
230 if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz)) < 0)
231 return g_err("gs1", __FUNCTION__, ret);
233 return mod ? tls_field_from_dn(cp, mod) : cp;
237 tls_cert_version(void * cert, uschar * mod)
239 return string_sprintf("%d", gnutls_x509_crt_get_version(cert));
243 tls_cert_ext_by_oid(void * cert, uschar * oid, int idx)
252 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
253 oid, idx, cp1, &siz, &crit);
254 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
255 return g_err("ge0", __FUNCTION__, ret);
257 cp1 = store_get(siz*4 + 1);
259 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
260 oid, idx, cp1, &siz, &crit);
262 return g_err("ge1", __FUNCTION__, ret);
264 /* binary data, DER encoded */
266 /* just dump for now */
267 for(cp3 = cp2 = cp1+siz; cp1 < cp2; cp3 += 3, cp1++)
268 sprintf(cp3, "%.2x ", *cp1);
275 tls_cert_subject_altname(void * cert, uschar * mod)
277 uschar * list = NULL;
288 if (*mod == '>' && *++mod) sep = *mod++;
289 else if (Ustrcmp(mod, "dns")==0) { match = GNUTLS_SAN_DNSNAME; mod += 3; }
290 else if (Ustrcmp(mod, "uri")==0) { match = GNUTLS_SAN_URI; mod += 3; }
291 else if (Ustrcmp(mod, "mail")==0) { match = GNUTLS_SAN_RFC822NAME; mod += 4; }
298 for(index = 0;; index++)
301 switch(ret = gnutls_x509_crt_get_subject_alt_name(
302 (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL))
304 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
305 return list; /* no more elements; normal exit */
307 case GNUTLS_E_SHORT_MEMORY_BUFFER:
311 return g_err("gs0", __FUNCTION__, ret);
314 ele = store_get(siz+1);
315 if ((ret = gnutls_x509_crt_get_subject_alt_name(
316 (gnutls_x509_crt_t)cert, index, ele, &siz, NULL)) < 0)
317 return g_err("gs1", __FUNCTION__, ret);
320 if ( match != -1 && match != ret /* wrong type of SAN */
321 || Ustrlen(ele) != siz) /* contains a NUL */
325 case GNUTLS_SAN_DNSNAME: tag = US"DNS"; break;
326 case GNUTLS_SAN_URI: tag = US"URI"; break;
327 case GNUTLS_SAN_RFC822NAME: tag = US"MAIL"; break;
328 default: continue; /* ignore unrecognised types */
330 list = string_append_listele(list, sep,
331 match == -1 ? string_sprintf("%s=%s", tag, ele) : ele);
337 tls_cert_ocsp_uri(void * cert, uschar * mod)
339 #if GNUTLS_VERSION_NUMBER >= 0x030000
344 uschar * list = NULL;
347 if (*mod == '>' && *++mod) sep = *mod++;
349 for(index = 0;; index++)
351 ret = gnutls_x509_crt_get_authority_info_access((gnutls_x509_crt_t)cert,
352 index, GNUTLS_IA_OCSP_URI, &uri, NULL);
354 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
357 return g_err("gai", __FUNCTION__, ret);
359 list = string_append_listele(list, sep,
360 string_copyn(uri.data, uri.size));
366 expand_string_message =
367 string_sprintf("%s: OCSP support with GnuTLS requires version 3.0.0\n",
375 tls_cert_crl_uri(void * cert, uschar * mod)
381 uschar * list = NULL;
385 if (*mod == '>' && *++mod) sep = *mod++;
387 for(index = 0;; index++)
390 switch(ret = gnutls_x509_crt_get_crl_dist_points(
391 (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL, NULL))
393 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
395 case GNUTLS_E_SHORT_MEMORY_BUFFER:
398 return g_err("gc0", __FUNCTION__, ret);
401 ele = store_get(siz+1);
402 if ((ret = gnutls_x509_crt_get_crl_dist_points(
403 (gnutls_x509_crt_t)cert, index, ele, &siz, NULL, NULL)) < 0)
404 return g_err("gc1", __FUNCTION__, ret);
407 list = string_append_listele(list, sep, ele);
413 /*****************************************************
414 * Certificate operator routines
415 *****************************************************/
417 fingerprint(gnutls_x509_crt_t cert, gnutls_digest_algorithm_t algo)
425 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, NULL, &siz))
426 != GNUTLS_E_SHORT_MEMORY_BUFFER)
427 return g_err("gf0", __FUNCTION__, ret);
429 cp = store_get(siz*3+1);
430 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, cp, &siz)) < 0)
431 return g_err("gf1", __FUNCTION__, ret);
433 for (cp3 = cp2 = cp+siz; cp < cp2; cp++, cp3+=2)
434 sprintf(cp3, "%02X",*cp);
440 tls_cert_fprt_md5(void * cert)
442 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_MD5);
446 tls_cert_fprt_sha1(void * cert)
448 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA1);
452 tls_cert_fprt_sha256(void * cert)
454 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA256);
460 /* End of tlscert-gnu.c */