X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/25ba25448b55c2fd5ea9b1aeed82e02d59816a07..3386088d5af4d4c61faa12ae29560e2c5bd43304:/src/src/tlscert-gnu.c diff --git a/src/src/tlscert-gnu.c b/src/src/tlscert-gnu.c index 9b9c83d8b..dc290b8b7 100644 --- a/src/src/tlscert-gnu.c +++ b/src/src/tlscert-gnu.c @@ -2,7 +2,7 @@ * Exim - an Internet mail transport agent * *************************************************/ -/* Copyright (c) Jeremy Harris 2014 */ +/* Copyright (c) Jeremy Harris 2014 - 2015 */ /* This file provides TLS/SSL support for Exim using the GnuTLS library, one of the available supported implementations. This file is #included into @@ -26,12 +26,16 @@ tls_export_cert(uschar * buf, size_t buflen, void * cert) { size_t sz = buflen; void * reset_point = store_get(0); -int fail = 0; -uschar * cp; +int fail; +const uschar * cp; -if (gnutls_x509_crt_export((gnutls_x509_crt_t)cert, - GNUTLS_X509_FMT_PEM, buf, &sz)) +if ((fail = gnutls_x509_crt_export((gnutls_x509_crt_t)cert, + GNUTLS_X509_FMT_PEM, buf, &sz))) + { + log_write(0, LOG_MAIN, "TLS error in certificate export: %s", + gnutls_strerror(fail)); return 1; + } if ((cp = string_printing(buf)) != buf) { Ustrncpy(buf, cp, buflen); @@ -55,8 +59,12 @@ gnutls_x509_crt_init(&crt); datum.data = string_unprinting(US buf); datum.size = Ustrlen(datum.data); -if (gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM)) +if ((fail = gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM))) + { + log_write(0, LOG_MAIN, "TLS error in certificate import: %s", + gnutls_strerror(fail)); fail = 1; + } else *cert = (void *)crt; @@ -74,6 +82,9 @@ gnutls_global_deinit(); /***************************************************** * Certificate field extraction routines *****************************************************/ + +/* First, some internal service functions */ + static uschar * g_err(const char * tag, const char * from, int gnutls_err) { @@ -87,19 +98,33 @@ static uschar * time_copy(time_t t, uschar * mod) { uschar * cp; -struct tm * tp; -size_t len; +size_t len = 32; if (mod && Ustrcmp(mod, "int") == 0) return string_sprintf("%u", (unsigned)t); -cp = store_get(32); -tp = gmtime(&t); -len = strftime(CS cp, 32, "%b %e %T %Y %Z", tp); +cp = store_get(len); +if (timestamps_utc) + { + uschar * tz = to_tz(US"GMT0"); + len = strftime(CS cp, len, "%b %e %T %Y %Z", gmtime(&t)); + restore_tz(tz); + } +else + len = strftime(CS cp, len, "%b %e %T %Y %Z", localtime(&t)); return len > 0 ? cp : NULL; } + /**/ +/* Now the extractors, called from expand.c +Arguments: + cert The certificate + mod Optional modifiers for the operator + +Return: + Allocated string with extracted value +*/ uschar * tls_cert_issuer(void * cert, uschar * mod) @@ -142,10 +167,12 @@ uschar bin[50], txt[150]; size_t sz = sizeof(bin); uschar * sp; uschar * dp; +int ret; + +if ((ret = gnutls_x509_crt_get_serial((gnutls_x509_crt_t)cert, + bin, &sz))) + return g_err("gs0", __FUNCTION__, ret); -if (gnutls_x509_crt_get_serial((gnutls_x509_crt_t)cert, - bin, &sz) || sz > sizeof(bin)) - return NULL; for(dp = txt, sp = bin; sz; dp += 2, sp++, sz--) sprintf(dp, "%.2x", *sp); for(sp = txt; sp[0]=='0' && sp[1]; ) sp++; /* leading zeroes */ @@ -155,7 +182,7 @@ return string_copy(sp); uschar * tls_cert_signature(void * cert, uschar * mod) { -uschar * cp1; +uschar * cp1 = NULL; uschar * cp2; uschar * cp3; size_t len = 0; @@ -268,7 +295,7 @@ for(index = 0;; index++) { siz = 0; switch(ret = gnutls_x509_crt_get_subject_alt_name( - (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL)) + (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL)) { case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE: return list; /* no more elements; normal exit */ @@ -286,7 +313,8 @@ for(index = 0;; index++) return g_err("gs1", __FUNCTION__, ret); ele[siz] = '\0'; - if (match != -1 && match != ret) + if ( match != -1 && match != ret /* wrong type of SAN */ + || Ustrlen(ele) != siz) /* contains a NUL */ continue; switch (ret) {