1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2021 - 2022 */
6 /* Copyright (c) Jeremy Harris 2014 - 2018 */
7 /* SPDX-License-Identifier: GPL-2.0-or-later */
9 /* This file provides TLS/SSL support for Exim using the GnuTLS library,
10 one of the available supported implementations. This file is #included into
11 tls.c when USE_GNUTLS has been set.
14 #include <gnutls/gnutls.h>
15 /* needed for cert checks in verification and DN extraction: */
16 #include <gnutls/x509.h>
17 /* needed to disable PKCS11 autoload unless requested */
18 #if GNUTLS_VERSION_NUMBER >= 0x020c00
19 # include <gnutls/pkcs11.h>
23 /*****************************************************
24 * Export/import a certificate, binary/printable
25 ******************************************************
26 Return: boolean success
30 tls_export_cert(uschar * buf, size_t buflen, void * cert)
33 rmark reset_point = store_mark();
37 if ((fail = gnutls_x509_crt_export((gnutls_x509_crt_t)cert,
38 GNUTLS_X509_FMT_PEM, buf, &sz)))
40 log_write(0, LOG_MAIN, "TLS error in certificate export: %s",
41 gnutls_strerror(fail));
44 if ((cp = string_printing(buf)) != buf)
46 Ustrncpy(buf, cp, buflen);
50 store_reset(reset_point);
54 /* On error, NULL out the destination */
56 tls_import_cert(const uschar * buf, void ** cert)
58 rmark reset_point = store_mark();
60 gnutls_x509_crt_t crt = *(gnutls_x509_crt_t *)cert;
64 gnutls_x509_crt_deinit(crt);
68 gnutls_x509_crt_init(&crt);
70 datum.data = string_unprinting(US buf);
71 datum.size = Ustrlen(datum.data);
72 if ((rc = gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM)))
74 log_write(0, LOG_MAIN, "TLS error in certificate import: %s",
79 store_reset(reset_point);
84 tls_free_cert(void ** cert)
86 gnutls_x509_crt_t crt = *(gnutls_x509_crt_t *)cert;
89 gnutls_x509_crt_deinit(crt);
90 gnutls_global_deinit();
95 /*****************************************************
96 * Certificate field extraction routines
97 *****************************************************/
99 /* First, some internal service functions */
102 g_err(const char * tag, const char * from, int gnutls_err)
104 expand_string_message = string_sprintf("%s: %s fail: %s\n",
105 from, tag, gnutls_strerror(gnutls_err));
111 time_copy(time_t t, uschar * mod)
116 if (mod && Ustrcmp(mod, "int") == 0)
117 return string_sprintf("%u", (unsigned)t);
119 cp = store_get(len, GET_UNTAINTED);
120 if (f.timestamps_utc)
122 uschar * tz = to_tz(US"GMT0");
123 len = strftime(CS cp, len, "%b %e %T %Y %Z", gmtime(&t));
127 len = strftime(CS cp, len, "%b %e %T %Y %Z", localtime(&t));
128 return len > 0 ? cp : NULL;
133 /* Now the extractors, called from expand.c
136 mod Optional modifiers for the operator
139 Allocated string with extracted value
143 tls_cert_issuer(void * cert, uschar * mod)
149 if ((ret = gnutls_x509_crt_get_issuer_dn(cert, CS cp, &siz))
150 != GNUTLS_E_SHORT_MEMORY_BUFFER)
151 return g_err("gi0", __FUNCTION__, ret);
153 cp = store_get(siz, GET_TAINTED);
154 if ((ret = gnutls_x509_crt_get_issuer_dn(cert, CS cp, &siz)) < 0)
155 return g_err("gi1", __FUNCTION__, ret);
157 return mod ? tls_field_from_dn(cp, mod) : cp;
161 tls_cert_not_after(void * cert, uschar * mod)
164 gnutls_x509_crt_get_expiration_time((gnutls_x509_crt_t)cert),
169 tls_cert_not_before(void * cert, uschar * mod)
172 gnutls_x509_crt_get_activation_time((gnutls_x509_crt_t)cert),
177 tls_cert_serial_number(void * cert, uschar * mod)
179 uschar bin[50], txt[150];
181 size_t sz = sizeof(bin);
184 if ((ret = gnutls_x509_crt_get_serial((gnutls_x509_crt_t)cert,
186 return g_err("gs0", __FUNCTION__, ret);
188 for(uschar * dp = txt; sz; sz--)
189 dp += sprintf(CS dp, "%.2x", *sp++);
190 for(sp = txt; sp[0]=='0' && sp[1]; ) sp++; /* leading zeroes */
191 return string_copy(sp);
195 tls_cert_signature(void * cert, uschar * mod)
203 if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, CS cp1, &len))
204 != GNUTLS_E_SHORT_MEMORY_BUFFER)
205 return g_err("gs0", __FUNCTION__, ret);
207 cp1 = store_get(len*4+1, GET_TAINTED);
208 if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, CS cp1, &len) != 0)
209 return g_err("gs1", __FUNCTION__, ret);
211 for(cp3 = cp2 = cp1+len; cp1 < cp2; cp1++)
212 cp3 += sprintf(CS cp3, "%.2x ", *cp1);
219 tls_cert_signature_algorithm(void * cert, uschar * mod)
221 gnutls_sign_algorithm_t algo =
222 gnutls_x509_crt_get_signature_algorithm((gnutls_x509_crt_t)cert);
223 return algo < 0 ? NULL : string_copy(US gnutls_sign_get_name(algo));
227 tls_cert_subject(void * cert, uschar * mod)
233 if ((ret = gnutls_x509_crt_get_dn(cert, CS cp, &siz))
234 != GNUTLS_E_SHORT_MEMORY_BUFFER)
235 return g_err("gs0", __FUNCTION__, ret);
237 cp = store_get(siz, GET_TAINTED);
238 if ((ret = gnutls_x509_crt_get_dn(cert, CS cp, &siz)) < 0)
239 return g_err("gs1", __FUNCTION__, ret);
241 return mod ? tls_field_from_dn(cp, mod) : cp;
245 tls_cert_version(void * cert, uschar * mod)
247 return string_sprintf("%d", gnutls_x509_crt_get_version(cert));
251 tls_cert_ext_by_oid(void * cert, uschar * oid, int idx)
260 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
261 CS oid, idx, CS cp1, &siz, &crit);
262 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
263 return g_err("ge0", __FUNCTION__, ret);
265 cp1 = store_get(siz*4 + 1, GET_TAINTED);
267 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
268 CS oid, idx, CS cp1, &siz, &crit);
270 return g_err("ge1", __FUNCTION__, ret);
272 /* binary data, DER encoded */
274 /* just dump for now */
275 for(cp3 = cp2 = cp1+siz; cp1 < cp2; cp1++)
276 cp3 += sprintf(CS cp3, "%.2x ", *cp1);
283 tls_cert_subject_altname(void * cert, uschar * mod)
285 gstring * list = NULL;
293 if (mod) while (*mod)
295 if (*mod == '>' && *++mod) sep = *mod++;
296 else if (Ustrncmp(mod, "dns", 3)==0) { match = GNUTLS_SAN_DNSNAME; mod += 3; }
297 else if (Ustrncmp(mod, "uri", 3)==0) { match = GNUTLS_SAN_URI; mod += 3; }
298 else if (Ustrncmp(mod, "mail", 4)==0) { match = GNUTLS_SAN_RFC822NAME; mod += 4; }
305 for (int index = 0;; index++)
308 switch(ret = gnutls_x509_crt_get_subject_alt_name(
309 (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL))
311 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
312 return string_from_gstring(list); /* no more elements; normal exit */
314 case GNUTLS_E_SHORT_MEMORY_BUFFER:
318 return g_err("gs0", __FUNCTION__, ret);
321 ele = store_get(siz+1, GET_TAINTED);
322 if ((ret = gnutls_x509_crt_get_subject_alt_name(
323 (gnutls_x509_crt_t)cert, index, ele, &siz, NULL)) < 0)
324 return g_err("gs1", __FUNCTION__, ret);
327 if ( match != -1 && match != ret /* wrong type of SAN */
328 || Ustrlen(ele) != siz) /* contains a NUL */
332 case GNUTLS_SAN_DNSNAME: tag = US"DNS"; break;
333 case GNUTLS_SAN_URI: tag = US"URI"; break;
334 case GNUTLS_SAN_RFC822NAME: tag = US"MAIL"; break;
335 default: continue; /* ignore unrecognised types */
337 list = string_append_listele(list, sep,
338 match == -1 ? string_sprintf("%s=%s", tag, ele) : ele);
344 tls_cert_ocsp_uri(void * cert, uschar * mod)
346 #if GNUTLS_VERSION_NUMBER >= 0x030000
350 gstring * list = NULL;
353 if (*mod == '>' && *++mod) sep = *mod++;
355 for (int index = 0;; index++)
357 ret = gnutls_x509_crt_get_authority_info_access((gnutls_x509_crt_t)cert,
358 index, GNUTLS_IA_OCSP_URI, &uri, NULL);
360 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
361 return string_from_gstring(list);
363 return g_err("gai", __FUNCTION__, ret);
365 list = string_append_listele_n(list, sep, uri.data, uri.size);
371 expand_string_message =
372 string_sprintf("%s: OCSP support with GnuTLS requires version 3.0.0\n",
380 tls_cert_crl_uri(void * cert, uschar * mod)
384 gstring * list = NULL;
388 if (*mod == '>' && *++mod) sep = *mod++;
390 for (int index = 0;; index++)
393 switch(ret = gnutls_x509_crt_get_crl_dist_points(
394 (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL, NULL))
396 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
397 return string_from_gstring(list);
398 case GNUTLS_E_SHORT_MEMORY_BUFFER:
401 return g_err("gc0", __FUNCTION__, ret);
404 ele = store_get(siz, GET_TAINTED);
405 if ((ret = gnutls_x509_crt_get_crl_dist_points(
406 (gnutls_x509_crt_t)cert, index, ele, &siz, NULL, NULL)) < 0)
407 return g_err("gc1", __FUNCTION__, ret);
409 list = string_append_listele_n(list, sep, ele, siz);
415 /*****************************************************
416 * Certificate operator routines
417 *****************************************************/
419 tls_cert_der_b64(void * cert)
425 if ( (fail = gnutls_x509_crt_export((gnutls_x509_crt_t)cert,
426 GNUTLS_X509_FMT_DER, cp, &len)) != GNUTLS_E_SHORT_MEMORY_BUFFER
427 || !(cp = store_get((int)len, GET_TAINTED), TRUE) /* tainted */
428 || (fail = gnutls_x509_crt_export((gnutls_x509_crt_t)cert,
429 GNUTLS_X509_FMT_DER, cp, &len))
432 log_write(0, LOG_MAIN, "TLS error in certificate export: %s",
433 gnutls_strerror(fail));
436 return b64encode(CUS cp, (int)len);
441 fingerprint(gnutls_x509_crt_t cert, gnutls_digest_algorithm_t algo)
448 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, NULL, &siz))
449 != GNUTLS_E_SHORT_MEMORY_BUFFER)
450 return g_err("gf0", __FUNCTION__, ret);
452 cp = store_get(siz*3+1, GET_TAINTED);
453 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, cp, &siz)) < 0)
454 return g_err("gf1", __FUNCTION__, ret);
456 for (uschar * cp3 = cp2 = cp+siz; cp < cp2; cp++)
457 cp3 += sprintf(CS cp3, "%02X", *cp);
463 tls_cert_fprt_md5(void * cert)
465 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_MD5);
469 tls_cert_fprt_sha1(void * cert)
471 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA1);
475 tls_cert_fprt_sha256(void * cert)
477 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA256);
483 /* End of tlscert-gnu.c */