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 (gnutls_x509_crt_export((gnutls_x509_crt_t)cert,
33 GNUTLS_X509_FMT_PEM, buf, &sz))
35 if ((cp = string_printing(buf)) != buf)
37 Ustrncpy(buf, cp, buflen);
41 store_reset(reset_point);
46 tls_import_cert(const uschar * buf, void ** cert)
48 void * reset_point = store_get(0);
50 gnutls_x509_crt_t crt;
54 gnutls_x509_crt_init(&crt);
56 datum.data = string_unprinting(US buf);
57 datum.size = Ustrlen(datum.data);
58 if (gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM))
63 store_reset(reset_point);
68 tls_free_cert(void * cert)
70 gnutls_x509_crt_deinit((gnutls_x509_crt_t) cert);
71 gnutls_global_deinit();
74 /*****************************************************
75 * Certificate field extraction routines
76 *****************************************************/
78 g_err(const char * tag, const char * from, int gnutls_err)
80 expand_string_message = string_sprintf(stderr,
81 "%s: %s fail: %s\n", from, tag, gnutls_strerror(gnutls_err));
89 uschar * cp = store_get(32);
90 struct tm * tp = gmtime(&t);
91 size_t len = strftime(CS cp, 32, "%b %e %T %Y %Z", tp);
92 return len > 0 ? cp : NULL;
98 tls_cert_issuer(void * cert, uschar * mod)
101 size_t sz = sizeof(txt);
102 return ( gnutls_x509_crt_get_issuer_dn(cert, CS txt, &sz) == 0 )
103 ? string_copy(txt) : NULL;
107 tls_cert_not_after(void * cert, uschar * mod)
110 gnutls_x509_crt_get_expiration_time((gnutls_x509_crt_t)cert));
114 tls_cert_not_before(void * cert, uschar * mod)
117 gnutls_x509_crt_get_activation_time((gnutls_x509_crt_t)cert));
121 tls_cert_serial_number(void * cert, uschar * mod)
123 uschar bin[50], txt[150];
124 size_t sz = sizeof(bin);
128 if (gnutls_x509_crt_get_serial((gnutls_x509_crt_t)cert,
129 bin, &sz) || sz > sizeof(bin))
131 for(dp = txt, sp = bin; sz; dp += 2, sp++, sz--)
132 sprintf(dp, "%.2x", *sp);
133 for(sp = txt; sp[0]=='0' && sp[1]; ) sp++; /* leading zeroes */
134 return string_copy(sp);
138 tls_cert_signature(void * cert, uschar * mod)
146 if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len)) !=
147 GNUTLS_E_SHORT_MEMORY_BUFFER)
149 fprintf(stderr, "%s: gs0 fail: %s\n", __FUNCTION__, gnutls_strerror(ret));
153 cp1 = store_get(len*4+1);
155 if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len) != 0)
157 fprintf(stderr, "%s: gs1 fail\n", __FUNCTION__);
161 for(cp3 = cp2 = cp1+len; cp1 < cp2; cp3 += 3, cp1++)
162 sprintf(cp3, "%.2x ", *cp1);
169 tls_cert_signature_algorithm(void * cert, uschar * mod)
171 gnutls_sign_algorithm_t algo =
172 gnutls_x509_crt_get_signature_algorithm((gnutls_x509_crt_t)cert);
173 return algo < 0 ? NULL : string_copy(gnutls_sign_get_name(algo));
177 tls_cert_subject(void * cert, uschar * mod)
183 ret = gnutls_x509_crt_get_dn(cert, cp, &siz);
184 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
186 fprintf(stderr, "%s: gs0 fail: %s\n", __FUNCTION__, gnutls_strerror(ret));
192 ret = gnutls_x509_crt_get_dn(cert, cp, &siz);
195 fprintf(stderr, "%s: gs1 fail: %s\n", __FUNCTION__, gnutls_strerror(ret));
203 tls_cert_version(void * cert, uschar * mod)
205 return string_sprintf("%d", gnutls_x509_crt_get_version(cert));
209 tls_cert_ext_by_oid(void * cert, uschar * oid, int idx)
218 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
219 oid, idx, cp1, &siz, &crit);
220 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
221 return g_err("ge0", __FUNCTION__, ret);
223 cp1 = store_get(siz*4 + 1);
225 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
226 oid, idx, cp1, &siz, &crit);
228 return g_err("ge1", __FUNCTION__, ret);
230 /* binary data, DER encoded */
232 /* just dump for now */
233 for(cp3 = cp2 = cp1+siz; cp1 < cp2; cp3 += 3, cp1++)
234 sprintf(cp3, "%.2x ", *cp1);
241 tls_cert_subject_altname(void * cert, uschar * mod)
243 uschar * list = NULL;
254 if (*mod == '>' && *++mod) sep = *mod++;
255 else if (Ustrcmp(mod, "dns")==0) { match = GNUTLS_SAN_DNSNAME; mod += 3; }
256 else if (Ustrcmp(mod, "uri")==0) { match = GNUTLS_SAN_URI; mod += 3; }
257 else if (Ustrcmp(mod, "mail")==0) { match = GNUTLS_SAN_RFC822NAME; mod += 4; }
264 for(index = 0;; index++)
267 switch(ret = gnutls_x509_crt_get_subject_alt_name(
268 (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL))
270 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
271 return list; /* no more elements; normal exit */
273 case GNUTLS_E_SHORT_MEMORY_BUFFER:
277 return g_err("gs0", __FUNCTION__, ret);
280 ele = store_get(siz+1);
281 if ((ret = gnutls_x509_crt_get_subject_alt_name(
282 (gnutls_x509_crt_t)cert, index, ele, &siz, NULL)) < 0)
283 return g_err("gs1", __FUNCTION__, ret);
286 if (match != -1 && match != ret)
290 case GNUTLS_SAN_DNSNAME: tag = US"DNS"; break;
291 case GNUTLS_SAN_URI: tag = US"URI"; break;
292 case GNUTLS_SAN_RFC822NAME: tag = US"MAIL"; break;
293 default: continue; /* ignore unrecognised types */
295 list = string_append_listele(list, sep,
296 match == -1 ? string_sprintf("%s=%s", tag, ele) : ele);
302 tls_cert_ocsp_uri(void * cert, uschar * mod)
304 #if GNUTLS_VERSION_NUMBER >= 0x030000
309 uschar * list = NULL;
312 if (*mod == '>' && *++mod) sep = *mod++;
314 for(index = 0;; index++)
316 ret = gnutls_x509_crt_get_authority_info_access((gnutls_x509_crt_t)cert,
317 index, GNUTLS_IA_OCSP_URI, &uri, NULL);
319 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
322 return g_err("gai", __FUNCTION__, ret);
324 list = string_append_listele(list, sep,
325 string_copyn(uri.data, uri.size));
331 expand_string_message =
332 string_sprintf("%s: OCSP support with GnuTLS requires version 3.0.0\n",
340 tls_cert_crl_uri(void * cert, uschar * mod)
346 uschar * list = NULL;
350 if (*mod == '>' && *++mod) sep = *mod++;
352 for(index = 0;; index++)
355 switch(ret = gnutls_x509_crt_get_crl_dist_points(
356 (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL, NULL))
358 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
360 case GNUTLS_E_SHORT_MEMORY_BUFFER:
363 return g_err("gc0", __FUNCTION__, ret);
366 ele = store_get(siz+1);
367 if ((ret = gnutls_x509_crt_get_crl_dist_points(
368 (gnutls_x509_crt_t)cert, index, ele, &siz, NULL, NULL)) < 0)
369 return g_err("gc1", __FUNCTION__, ret);
372 list = string_append_listele(list, sep, ele);
378 /*****************************************************
379 * Certificate operator routines
380 *****************************************************/
382 fingerprint(gnutls_x509_crt_t cert, gnutls_digest_algorithm_t algo)
390 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, NULL, &siz))
391 != GNUTLS_E_SHORT_MEMORY_BUFFER)
392 return g_err("gf0", __FUNCTION__, ret);
394 cp = store_get(siz*3+1);
395 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, cp, &siz)) < 0)
396 return g_err("gf1", __FUNCTION__, ret);
398 for (cp3 = cp2 = cp+siz; cp < cp2; cp++, cp3+=2)
399 sprintf(cp3, "%02X",*cp);
405 tls_cert_fprt_md5(void * cert)
407 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_MD5);
411 tls_cert_fprt_sha1(void * cert)
413 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA1);
417 tls_cert_fprt_sha256(void * cert)
419 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA256);
425 /* End of tlscert-gnu.c */