X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/c03fae8adf341e26f2c4c8f068a60a72d8504963..86ede124f0ce622b4f73e05504abc11fece021e3:/src/src/tlscert-gnu.c diff --git a/src/src/tlscert-gnu.c b/src/src/tlscert-gnu.c index 3261c4e8d..a09fda0b9 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 - 2018 */ /* This file provides TLS/SSL support for Exim using the GnuTLS library, one of the available supported implementations. This file is #included into @@ -20,21 +20,24 @@ tls.c when USE_GNUTLS has been set. /***************************************************** * Export/import a certificate, binary/printable -*****************************************************/ -int +****************************************************** +Return: boolean success +*/ + +BOOL tls_export_cert(uschar * buf, size_t buflen, void * cert) { size_t sz = buflen; -void * reset_point = store_get(0); -int fail; -uschar * cp; +rmark reset_point = store_mark(); +BOOL fail; +const uschar * cp; 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; + return FALSE; } if ((cp = string_printing(buf)) != buf) { @@ -43,40 +46,48 @@ if ((cp = string_printing(buf)) != buf) fail = 1; } store_reset(reset_point); -return fail; +return !fail; } -int +/* On error, NULL out the destination */ +BOOL tls_import_cert(const uschar * buf, void ** cert) { -void * reset_point = store_get(0); +rmark reset_point = store_mark(); gnutls_datum_t datum; -gnutls_x509_crt_t crt; -int fail = 0; +gnutls_x509_crt_t crt = *(gnutls_x509_crt_t *)cert; +int rc; + +if (crt) + gnutls_x509_crt_deinit(crt); +else + gnutls_global_init(); -gnutls_global_init(); gnutls_x509_crt_init(&crt); datum.data = string_unprinting(US buf); datum.size = Ustrlen(datum.data); -if ((fail = gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM))) +if ((rc = 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; + gnutls_strerror(rc)); + crt = NULL; } -else - *cert = (void *)crt; - +*cert = (void *)crt; store_reset(reset_point); -return fail; +return rc != 0; } void -tls_free_cert(void * cert) +tls_free_cert(void ** cert) { -gnutls_x509_crt_deinit((gnutls_x509_crt_t) cert); -gnutls_global_deinit(); +gnutls_x509_crt_t crt = *(gnutls_x509_crt_t *)cert; +if (crt) + { + gnutls_x509_crt_deinit(crt); + gnutls_global_deinit(); + *cert = NULL; + } } /***************************************************** @@ -98,15 +109,20 @@ 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, FALSE); +if (f.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; } @@ -128,12 +144,12 @@ uschar * cp = NULL; int ret; size_t siz = 0; -if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz)) +if ((ret = gnutls_x509_crt_get_issuer_dn(cert, CS cp, &siz)) != GNUTLS_E_SHORT_MEMORY_BUFFER) return g_err("gi0", __FUNCTION__, ret); -cp = store_get(siz); -if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz)) < 0) +cp = store_get(siz, TRUE); +if ((ret = gnutls_x509_crt_get_issuer_dn(cert, CS cp, &siz)) < 0) return g_err("gi1", __FUNCTION__, ret); return mod ? tls_field_from_dn(cp, mod) : cp; @@ -159,17 +175,16 @@ uschar * tls_cert_serial_number(void * cert, uschar * mod) { uschar bin[50], txt[150]; +uschar * sp = bin; 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); -for(dp = txt, sp = bin; sz; dp += 2, sp++, sz--) - sprintf(dp, "%.2x", *sp); +for(uschar * dp = txt; sz; sz--) + dp += sprintf(CS dp, "%.2x", *sp++); for(sp = txt; sp[0]=='0' && sp[1]; ) sp++; /* leading zeroes */ return string_copy(sp); } @@ -177,22 +192,22 @@ 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; int ret; -if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len)) +if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, CS cp1, &len)) != GNUTLS_E_SHORT_MEMORY_BUFFER) return g_err("gs0", __FUNCTION__, ret); -cp1 = store_get(len*4+1); -if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len) != 0) +cp1 = store_get(len*4+1, TRUE); +if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, CS cp1, &len) != 0) return g_err("gs1", __FUNCTION__, ret); -for(cp3 = cp2 = cp1+len; cp1 < cp2; cp3 += 3, cp1++) - sprintf(cp3, "%.2x ", *cp1); +for(cp3 = cp2 = cp1+len; cp1 < cp2; cp1++) + cp3 += sprintf(CS cp3, "%.2x ", *cp1); cp3[-1]= '\0'; return cp2; @@ -203,7 +218,7 @@ tls_cert_signature_algorithm(void * cert, uschar * mod) { gnutls_sign_algorithm_t algo = gnutls_x509_crt_get_signature_algorithm((gnutls_x509_crt_t)cert); -return algo < 0 ? NULL : string_copy(gnutls_sign_get_name(algo)); +return algo < 0 ? NULL : string_copy(US gnutls_sign_get_name(algo)); } uschar * @@ -213,12 +228,12 @@ uschar * cp = NULL; int ret; size_t siz = 0; -if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz)) +if ((ret = gnutls_x509_crt_get_dn(cert, CS cp, &siz)) != GNUTLS_E_SHORT_MEMORY_BUFFER) return g_err("gs0", __FUNCTION__, ret); -cp = store_get(siz); -if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz)) < 0) +cp = store_get(siz, TRUE); +if ((ret = gnutls_x509_crt_get_dn(cert, CS cp, &siz)) < 0) return g_err("gs1", __FUNCTION__, ret); return mod ? tls_field_from_dn(cp, mod) : cp; @@ -241,22 +256,22 @@ unsigned int crit; int ret; ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert, - oid, idx, cp1, &siz, &crit); + CS oid, idx, CS cp1, &siz, &crit); if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER) return g_err("ge0", __FUNCTION__, ret); -cp1 = store_get(siz*4 + 1); +cp1 = store_get(siz*4 + 1, TRUE); ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert, - oid, idx, cp1, &siz, &crit); + CS oid, idx, CS cp1, &siz, &crit); if (ret < 0) return g_err("ge1", __FUNCTION__, ret); /* binary data, DER encoded */ /* just dump for now */ -for(cp3 = cp2 = cp1+siz; cp1 < cp2; cp3 += 3, cp1++) - sprintf(cp3, "%.2x ", *cp1); +for(cp3 = cp2 = cp1+siz; cp1 < cp2; cp1++) + cp3 += sprintf(CS cp3, "%.2x ", *cp1); cp3[-1]= '\0'; return cp2; @@ -265,8 +280,7 @@ return cp2; uschar * tls_cert_subject_altname(void * cert, uschar * mod) { -uschar * list = NULL; -int index; +gstring * list = NULL; size_t siz; int ret; uschar sep = '\n'; @@ -286,14 +300,14 @@ while (mod) break; } -for(index = 0;; index++) +for (int index = 0;; index++) { siz = 0; switch(ret = gnutls_x509_crt_get_subject_alt_name( (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL)) { case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE: - return list; /* no more elements; normal exit */ + return string_from_gstring(list); /* no more elements; normal exit */ case GNUTLS_E_SHORT_MEMORY_BUFFER: break; @@ -302,7 +316,7 @@ for(index = 0;; index++) return g_err("gs0", __FUNCTION__, ret); } - ele = store_get(siz+1); + ele = store_get(siz+1, TRUE); if ((ret = gnutls_x509_crt_get_subject_alt_name( (gnutls_x509_crt_t)cert, index, ele, &siz, NULL)) < 0) return g_err("gs1", __FUNCTION__, ret); @@ -314,11 +328,11 @@ for(index = 0;; index++) switch (ret) { case GNUTLS_SAN_DNSNAME: tag = US"DNS"; break; - case GNUTLS_SAN_URI: tag = US"URI"; break; + case GNUTLS_SAN_URI: tag = US"URI"; break; case GNUTLS_SAN_RFC822NAME: tag = US"MAIL"; break; default: continue; /* ignore unrecognised types */ } - list = string_append_listele(list, sep, + list = string_append_listele(list, sep, match == -1 ? string_sprintf("%s=%s", tag, ele) : ele); } /*NOTREACHED*/ @@ -331,30 +345,28 @@ tls_cert_ocsp_uri(void * cert, uschar * mod) gnutls_datum_t uri; int ret; uschar sep = '\n'; -int index; -uschar * list = NULL; +gstring * list = NULL; if (mod) if (*mod == '>' && *++mod) sep = *mod++; -for(index = 0;; index++) +for (int index = 0;; index++) { ret = gnutls_x509_crt_get_authority_info_access((gnutls_x509_crt_t)cert, index, GNUTLS_IA_OCSP_URI, &uri, NULL); if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) - return list; + return string_from_gstring(list); if (ret < 0) return g_err("gai", __FUNCTION__, ret); - list = string_append_listele(list, sep, - string_copyn(uri.data, uri.size)); + list = string_append_listele_n(list, sep, uri.data, uri.size); } /*NOTREACHED*/ #else -expand_string_message = +expand_string_message = string_sprintf("%s: OCSP support with GnuTLS requires version 3.0.0\n", __FUNCTION__); return NULL; @@ -366,36 +378,33 @@ uschar * tls_cert_crl_uri(void * cert, uschar * mod) { int ret; -size_t siz; uschar sep = '\n'; -int index; -uschar * list = NULL; +gstring * list = NULL; uschar * ele; if (mod) if (*mod == '>' && *++mod) sep = *mod++; -for(index = 0;; index++) +for (int index = 0;; index++) { - siz = 0; + size_t siz = 0; switch(ret = gnutls_x509_crt_get_crl_dist_points( (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL, NULL)) { case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE: - return list; + return string_from_gstring(list); case GNUTLS_E_SHORT_MEMORY_BUFFER: break; default: return g_err("gc0", __FUNCTION__, ret); } - ele = store_get(siz+1); + ele = store_get(siz, TRUE); if ((ret = gnutls_x509_crt_get_crl_dist_points( (gnutls_x509_crt_t)cert, index, ele, &siz, NULL, NULL)) < 0) return g_err("gc1", __FUNCTION__, ret); - ele[siz] = '\0'; - list = string_append_listele(list, sep, ele); + list = string_append_listele_n(list, sep, ele, siz); } /*NOTREACHED*/ } @@ -404,6 +413,28 @@ for(index = 0;; index++) /***************************************************** * Certificate operator routines *****************************************************/ +uschar * +tls_cert_der_b64(void * cert) +{ +size_t len = 0; +uschar * cp = NULL; +int fail; + +if ( (fail = gnutls_x509_crt_export((gnutls_x509_crt_t)cert, + GNUTLS_X509_FMT_DER, cp, &len)) != GNUTLS_E_SHORT_MEMORY_BUFFER + || !(cp = store_get((int)len, TRUE), TRUE) /* tainted */ + || (fail = gnutls_x509_crt_export((gnutls_x509_crt_t)cert, + GNUTLS_X509_FMT_DER, cp, &len)) + ) + { + log_write(0, LOG_MAIN, "TLS error in certificate export: %s", + gnutls_strerror(fail)); + return NULL; + } +return b64encode(CUS cp, (int)len); +} + + static uschar * fingerprint(gnutls_x509_crt_t cert, gnutls_digest_algorithm_t algo) { @@ -411,18 +442,17 @@ int ret; size_t siz = 0; uschar * cp; uschar * cp2; -uschar * cp3; if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, NULL, &siz)) != GNUTLS_E_SHORT_MEMORY_BUFFER) return g_err("gf0", __FUNCTION__, ret); -cp = store_get(siz*3+1); +cp = store_get(siz*3+1, TRUE); if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, cp, &siz)) < 0) return g_err("gf1", __FUNCTION__, ret); -for (cp3 = cp2 = cp+siz; cp < cp2; cp++, cp3+=2) - sprintf(cp3, "%02X",*cp); +for (uschar * cp3 = cp2 = cp+siz; cp < cp2; cp++) + cp3 += sprintf(CS cp3, "%02X", *cp); return cp2; }