1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Jeremy Harris 2014 */
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;
58 gnutls_x509_crt_init(&crt);
60 datum.data = string_unprinting(US buf);
61 datum.size = Ustrlen(datum.data);
62 if ((fail = gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM)))
64 log_write(0, LOG_MAIN, "TLS error in certificate import: %s",
65 gnutls_strerror(fail));
71 store_reset(reset_point);
76 tls_free_cert(void * cert)
78 gnutls_x509_crt_deinit((gnutls_x509_crt_t) cert);
79 gnutls_global_deinit();
82 /*****************************************************
83 * Certificate field extraction routines
84 *****************************************************/
86 /* First, some internal service functions */
89 g_err(const char * tag, const char * from, int gnutls_err)
91 expand_string_message = string_sprintf("%s: %s fail: %s\n",
92 from, tag, gnutls_strerror(gnutls_err));
98 time_copy(time_t t, uschar * mod)
103 if (mod && Ustrcmp(mod, "int") == 0)
104 return string_sprintf("%u", (unsigned)t);
109 uschar * tz = to_tz(US"GMT0");
110 len = strftime(CS cp, len, "%b %e %T %Y %Z", gmtime(&t));
114 len = strftime(CS cp, len, "%b %e %T %Y %Z", localtime(&t));
115 return len > 0 ? cp : NULL;
120 /* Now the extractors, called from expand.c
123 mod Optional modifiers for the operator
126 Allocated string with extracted value
130 tls_cert_issuer(void * cert, uschar * mod)
136 if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz))
137 != GNUTLS_E_SHORT_MEMORY_BUFFER)
138 return g_err("gi0", __FUNCTION__, ret);
141 if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz)) < 0)
142 return g_err("gi1", __FUNCTION__, ret);
144 return mod ? tls_field_from_dn(cp, mod) : cp;
148 tls_cert_not_after(void * cert, uschar * mod)
151 gnutls_x509_crt_get_expiration_time((gnutls_x509_crt_t)cert),
156 tls_cert_not_before(void * cert, uschar * mod)
159 gnutls_x509_crt_get_activation_time((gnutls_x509_crt_t)cert),
164 tls_cert_serial_number(void * cert, uschar * mod)
166 uschar bin[50], txt[150];
167 size_t sz = sizeof(bin);
172 if ((ret = gnutls_x509_crt_get_serial((gnutls_x509_crt_t)cert,
174 return g_err("gs0", __FUNCTION__, ret);
176 for(dp = txt, sp = bin; sz; dp += 2, sp++, sz--)
177 sprintf(dp, "%.2x", *sp);
178 for(sp = txt; sp[0]=='0' && sp[1]; ) sp++; /* leading zeroes */
179 return string_copy(sp);
183 tls_cert_signature(void * cert, uschar * mod)
191 if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len))
192 != GNUTLS_E_SHORT_MEMORY_BUFFER)
193 return g_err("gs0", __FUNCTION__, ret);
195 cp1 = store_get(len*4+1);
196 if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len) != 0)
197 return g_err("gs1", __FUNCTION__, ret);
199 for(cp3 = cp2 = cp1+len; cp1 < cp2; cp3 += 3, cp1++)
200 sprintf(cp3, "%.2x ", *cp1);
207 tls_cert_signature_algorithm(void * cert, uschar * mod)
209 gnutls_sign_algorithm_t algo =
210 gnutls_x509_crt_get_signature_algorithm((gnutls_x509_crt_t)cert);
211 return algo < 0 ? NULL : string_copy(gnutls_sign_get_name(algo));
215 tls_cert_subject(void * cert, uschar * mod)
221 if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz))
222 != GNUTLS_E_SHORT_MEMORY_BUFFER)
223 return g_err("gs0", __FUNCTION__, ret);
226 if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz)) < 0)
227 return g_err("gs1", __FUNCTION__, ret);
229 return mod ? tls_field_from_dn(cp, mod) : cp;
233 tls_cert_version(void * cert, uschar * mod)
235 return string_sprintf("%d", gnutls_x509_crt_get_version(cert));
239 tls_cert_ext_by_oid(void * cert, uschar * oid, int idx)
248 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
249 oid, idx, cp1, &siz, &crit);
250 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
251 return g_err("ge0", __FUNCTION__, ret);
253 cp1 = store_get(siz*4 + 1);
255 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
256 oid, idx, cp1, &siz, &crit);
258 return g_err("ge1", __FUNCTION__, ret);
260 /* binary data, DER encoded */
262 /* just dump for now */
263 for(cp3 = cp2 = cp1+siz; cp1 < cp2; cp3 += 3, cp1++)
264 sprintf(cp3, "%.2x ", *cp1);
271 tls_cert_subject_altname(void * cert, uschar * mod)
273 uschar * list = NULL;
284 if (*mod == '>' && *++mod) sep = *mod++;
285 else if (Ustrcmp(mod, "dns")==0) { match = GNUTLS_SAN_DNSNAME; mod += 3; }
286 else if (Ustrcmp(mod, "uri")==0) { match = GNUTLS_SAN_URI; mod += 3; }
287 else if (Ustrcmp(mod, "mail")==0) { match = GNUTLS_SAN_RFC822NAME; mod += 4; }
294 for(index = 0;; index++)
297 switch(ret = gnutls_x509_crt_get_subject_alt_name(
298 (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL))
300 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
301 return list; /* no more elements; normal exit */
303 case GNUTLS_E_SHORT_MEMORY_BUFFER:
307 return g_err("gs0", __FUNCTION__, ret);
310 ele = store_get(siz+1);
311 if ((ret = gnutls_x509_crt_get_subject_alt_name(
312 (gnutls_x509_crt_t)cert, index, ele, &siz, NULL)) < 0)
313 return g_err("gs1", __FUNCTION__, ret);
316 if ( match != -1 && match != ret /* wrong type of SAN */
317 || Ustrlen(ele) != siz) /* contains a NUL */
321 case GNUTLS_SAN_DNSNAME: tag = US"DNS"; break;
322 case GNUTLS_SAN_URI: tag = US"URI"; break;
323 case GNUTLS_SAN_RFC822NAME: tag = US"MAIL"; break;
324 default: continue; /* ignore unrecognised types */
326 list = string_append_listele(list, sep,
327 match == -1 ? string_sprintf("%s=%s", tag, ele) : ele);
333 tls_cert_ocsp_uri(void * cert, uschar * mod)
335 #if GNUTLS_VERSION_NUMBER >= 0x030000
340 uschar * list = NULL;
343 if (*mod == '>' && *++mod) sep = *mod++;
345 for(index = 0;; index++)
347 ret = gnutls_x509_crt_get_authority_info_access((gnutls_x509_crt_t)cert,
348 index, GNUTLS_IA_OCSP_URI, &uri, NULL);
350 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
353 return g_err("gai", __FUNCTION__, ret);
355 list = string_append_listele(list, sep,
356 string_copyn(uri.data, uri.size));
362 expand_string_message =
363 string_sprintf("%s: OCSP support with GnuTLS requires version 3.0.0\n",
371 tls_cert_crl_uri(void * cert, uschar * mod)
377 uschar * list = NULL;
381 if (*mod == '>' && *++mod) sep = *mod++;
383 for(index = 0;; index++)
386 switch(ret = gnutls_x509_crt_get_crl_dist_points(
387 (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL, NULL))
389 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
391 case GNUTLS_E_SHORT_MEMORY_BUFFER:
394 return g_err("gc0", __FUNCTION__, ret);
397 ele = store_get(siz+1);
398 if ((ret = gnutls_x509_crt_get_crl_dist_points(
399 (gnutls_x509_crt_t)cert, index, ele, &siz, NULL, NULL)) < 0)
400 return g_err("gc1", __FUNCTION__, ret);
403 list = string_append_listele(list, sep, ele);
409 /*****************************************************
410 * Certificate operator routines
411 *****************************************************/
413 fingerprint(gnutls_x509_crt_t cert, gnutls_digest_algorithm_t algo)
421 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, NULL, &siz))
422 != GNUTLS_E_SHORT_MEMORY_BUFFER)
423 return g_err("gf0", __FUNCTION__, ret);
425 cp = store_get(siz*3+1);
426 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, cp, &siz)) < 0)
427 return g_err("gf1", __FUNCTION__, ret);
429 for (cp3 = cp2 = cp+siz; cp < cp2; cp++, cp3+=2)
430 sprintf(cp3, "%02X",*cp);
436 tls_cert_fprt_md5(void * cert)
438 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_MD5);
442 tls_cert_fprt_sha1(void * cert)
444 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA1);
448 tls_cert_fprt_sha256(void * cert)
450 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA256);
456 /* End of tlscert-gnu.c */