899014
From fb8665aebd79ea33cb255f578544e1738f5bbb58 Mon Sep 17 00:00:00 2001
899014
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
899014
Date: Thu, 2 Aug 2018 23:34:45 +0200
899014
Subject: [PATCH 1/2] Squashed commit of the following:
899014
MIME-Version: 1.0
899014
Content-Type: text/plain; charset=UTF-8
899014
Content-Transfer-Encoding: 8bit
899014
899014
commit b49f70ce0575b6b52a71b90fe0376dbf16f92c6b
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Mon Jan 22 14:12:37 2018 +0100
899014
899014
    Update system tests to detect MD5 disabled at runtime
899014
899014
commit 80ceffee4860c24baf70bc9a8653d92731eda2e4
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Thu Aug 2 14:53:54 2018 +0200
899014
899014
    Avoid warning about undefined parameters
899014
899014
commit e4ad4363e3d1acaac58456117579f02761f38fdc
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Wed Jun 20 19:31:19 2018 +0200
899014
899014
    Fix rndc-confgen default algorithm, report true algorithm in usage.
899014
899014
commit 7e629a351010cb75e0589ec361f720085675998c
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Fri Feb 23 21:21:30 2018 +0100
899014
899014
    Cleanup only if initialization was successful
899014
899014
commit 2101b948c77cbcbe07eb4a1e60f3e693b2245ec6
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Mon Feb 5 12:19:28 2018 +0100
899014
899014
    Ensure dst backend is initialized first even before hmac algorithms.
899014
899014
commit 7567c7edde7519115a9ae7e20818c835d3eb1ffe
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Mon Feb 5 12:17:54 2018 +0100
899014
899014
    Skip initialization of MD5 based algorithms if not available.
899014
899014
commit 5782137df6b45a6d900d5a1c250c1257227e917a
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Mon Feb 5 10:21:27 2018 +0100
899014
899014
    Change secalgs skipping to be more safe
899014
899014
commit f2d78729898182d2d19d5064de1bec9b66817159
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Wed Jan 31 18:26:11 2018 +0100
899014
899014
    Skip MD5 algorithm also in case of NULL name
899014
899014
commit 32a2ad4abc7aaca1c257730319ad3c27405d3407
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Wed Jan 31 11:38:12 2018 +0100
899014
899014
    Make MD5 behave like unknown algorithm in TSIG.
899014
899014
commit 13cd3f704dce568fdf24a567be5802b58ac6007b
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Tue Nov 28 20:14:37 2017 +0100
899014
899014
    Select token with most supported functions, instead of demanding it must support all functions
899014
899014
    Initialize PKCS#11 always until successfully initialized
899014
899014
commit a71df74abdca4fe63bcdf542b81a109cf1f495b4
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Mon Jan 22 16:17:44 2018 +0100
899014
899014
    Handle MD5 unavailability from DST
899014
899014
commit dd82cb263efa2753d3ee772972726ea08bcc639b
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Mon Jan 22 14:11:16 2018 +0100
899014
899014
    Check runtime flag from library and applications, fail gracefully.
899014
899014
commit c7b2f87f07ecae75b821a908e29f08a42371e32e
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Mon Jan 22 08:39:08 2018 +0100
899014
899014
    Modify libraries to use isc_md5_available() if PK11_MD5_DISABLE is not
899014
    defined.
899014
    TODO: pk11.c should accept slot without MD5 support.
899014
899014
commit 0b8e470ec636b9e350b5ec3203eb2b4091415fde
899014
Author: Petr Menšík <pemensik@redhat.com>
899014
Date:   Mon Jan 22 07:21:04 2018 +0100
899014
899014
    Add runtime detection whether MD5 is useable.
899014
---
899014
 bin/confgen/keygen.c              | 10 ++++-
899014
 bin/confgen/rndc-confgen.c        | 36 +++++-------------
899014
 bin/dig/dig.c                     |  7 ++--
899014
 bin/dig/dighost.c                 | 14 +++++--
899014
 bin/dnssec/dnssec-keygen.c        | 14 +++++++
899014
 bin/named/config.c                | 25 ++++++++++++-
899014
 bin/nsupdate/nsupdate.c           | 24 +++++++-----
899014
 bin/rndc/rndc.c                   |  3 +-
899014
 bin/tests/optional/hash_test.c    | 78 ++++++++++++++++++++-------------------
899014
 bin/tests/system/tkey/keycreate.c |  3 ++
899014
 bin/tests/system/tkey/keydelete.c | 18 ++++++---
899014
 lib/bind9/check.c                 | 10 +++++
899014
 lib/dns/dst_api.c                 | 23 ++++++++----
899014
 lib/dns/dst_internal.h            |  3 +-
899014
 lib/dns/dst_parse.c               | 18 +++++++--
899014
 lib/dns/hmac_link.c               | 20 +++-------
899014
 lib/dns/opensslrsa_link.c         |  6 +++
899014
 lib/dns/pkcs11rsa_link.c          | 33 +++++++++++++++--
899014
 lib/dns/rcode.c                   | 21 ++++++++++-
899014
 lib/dns/tests/rsa_test.c          | 29 ++++++++-------
899014
 lib/dns/tests/tsig_test.c         |  1 +
899014
 lib/dns/tkey.c                    |  9 +++++
899014
 lib/dns/tsec.c                    |  8 +++-
899014
 lib/dns/tsig.c                    | 17 +++++----
899014
 lib/isc/include/isc/md5.h         |  3 ++
899014
 lib/isc/md5.c                     | 59 +++++++++++++++++++++++++++++
899014
 lib/isc/pk11.c                    | 58 ++++++++++++++++++++---------
899014
 lib/isc/tests/hash_test.c         |  9 +++--
899014
 lib/isccc/cc.c                    | 42 +++++++++++++--------
899014
 29 files changed, 424 insertions(+), 177 deletions(-)
899014
899014
diff --git a/bin/confgen/keygen.c b/bin/confgen/keygen.c
899014
index 453c641dba..11cc54dd46 100644
899014
--- a/bin/confgen/keygen.c
899014
+++ b/bin/confgen/keygen.c
899014
@@ -22,6 +22,7 @@
899014
 #include <isc/entropy.h>
899014
 #include <isc/file.h>
899014
 #include <isc/keyboard.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/print.h>
899014
 #include <isc/result.h>
899014
@@ -73,7 +74,7 @@ alg_fromtext(const char *name) {
899014
 		p = &name[5];
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
-	if (strcasecmp(p, "md5") == 0)
899014
+	if (strcasecmp(p, "md5") == 0 && isc_md5_available())
899014
 		return DST_ALG_HMACMD5;
899014
 #endif
899014
 	if (strcasecmp(p, "sha1") == 0)
899014
@@ -132,6 +133,13 @@ generate_key(isc_mem_t *mctx, const char *randomfile, dns_secalg_t alg,
899014
 	switch (alg) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 	    case DST_ALG_HMACMD5:
899014
+		if (isc_md5_available() == ISC_FALSE) {
899014
+			fatal("unsupported algorithm %d\n", alg);
899014
+		} else if (keysize < 1 || keysize > 512) {
899014
+			fatal("keysize %d out of range (must be 1-512)\n",
899014
+			      keysize);
899014
+		}
899014
+		break;
899014
 #endif
899014
 	    case DST_ALG_HMACSHA1:
899014
 	    case DST_ALG_HMACSHA224:
899014
diff --git a/bin/confgen/rndc-confgen.c b/bin/confgen/rndc-confgen.c
899014
index 2925baf32f..d7d8418073 100644
899014
--- a/bin/confgen/rndc-confgen.c
899014
+++ b/bin/confgen/rndc-confgen.c
899014
@@ -35,6 +35,7 @@
899014
 #include <isc/file.h>
899014
 #include <isc/keyboard.h>
899014
 #include <isc/mem.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/net.h>
899014
 #include <isc/print.h>
899014
 #include <isc/result.h>
899014
@@ -62,7 +63,7 @@ const char *progname;
899014
 
899014
 isc_boolean_t verbose = ISC_FALSE;
899014
 
899014
-const char *keyfile, *keydef;
899014
+const char *keyfile, *keydef, *algdef;
899014
 
899014
 ISC_PLATFORM_NORETURN_PRE static void
899014
 usage(int status) ISC_PLATFORM_NORETURN_POST;
899014
@@ -70,13 +71,12 @@ usage(int status) ISC_PLATFORM_NORETURN_POST;
899014
 static void
899014
 usage(int status) {
899014
 
899014
-#ifndef PK11_MD5_DISABLE
899014
 	fprintf(stderr, "\
899014
 Usage:\n\
899014
  %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
899014
 [-s addr] [-t chrootdir] [-u user]\n\
899014
   -a:		 generate just the key clause and write it to keyfile (%s)\n\
899014
-  -A alg:	 algorithm (default hmac-md5)\n\
899014
+  -A alg:	 algorithm (default %s)\n\
899014
   -b bits:	 from 1 through 512, default 256; total length of the secret\n\
899014
   -c keyfile:	 specify an alternate key file (requires -a)\n\
899014
   -k keyname:	 the name as it will be used  in named.conf and rndc.conf\n\
899014
@@ -85,24 +85,7 @@ Usage:\n\
899014
   -s addr:	 the address to which rndc should connect\n\
899014
   -t chrootdir:	 write a keyfile in chrootdir as well (requires -a)\n\
899014
   -u user:	 set the keyfile owner to \"user\" (requires -a)\n",
899014
-		 progname, keydef);
899014
-#else
899014
-	fprintf(stderr, "\
899014
-Usage:\n\
899014
- %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
899014
-[-s addr] [-t chrootdir] [-u user]\n\
899014
-  -a:		 generate just the key clause and write it to keyfile (%s)\n\
899014
-  -A alg:	 algorithm (default hmac-sha256)\n\
899014
-  -b bits:	 from 1 through 512, default 256; total length of the secret\n\
899014
-  -c keyfile:	 specify an alternate key file (requires -a)\n\
899014
-  -k keyname:	 the name as it will be used  in named.conf and rndc.conf\n\
899014
-  -p port:	 the port named will listen on and rndc will connect to\n\
899014
-  -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
899014
-  -s addr:	 the address to which rndc should connect\n\
899014
-  -t chrootdir:	 write a keyfile in chrootdir as well (requires -a)\n\
899014
-  -u user:	 set the keyfile owner to \"user\" (requires -a)\n",
899014
-		 progname, keydef);
899014
-#endif
899014
+		 progname, keydef, algdef);
899014
 
899014
 	exit (status);
899014
 }
899014
@@ -138,13 +121,14 @@ main(int argc, char **argv) {
899014
 	progname = program;
899014
 
899014
 	keyname = DEFAULT_KEYNAME;
899014
-#ifndef PK11_MD5_DISABLE
899014
-	alg = DST_ALG_HMACMD5;
899014
-#else
899014
-	alg = DST_ALG_HMACSHA256;
899014
-#endif
899014
 	serveraddr = DEFAULT_SERVER;
899014
 	port = DEFAULT_PORT;
899014
+	alg = DST_ALG_HMACSHA256;
899014
+#ifndef PK11_MD5_DISABLE
899014
+	if (isc_md5_available())
899014
+		alg = DST_ALG_HMACMD5;
899014
+#endif
899014
+	algdef = alg_totext(alg);
899014
 
899014
 	isc_commandline_errprint = ISC_FALSE;
899014
 
899014
diff --git a/bin/dig/dig.c b/bin/dig/dig.c
899014
index d4808ada67..9dff7c8ecd 100644
899014
--- a/bin/dig/dig.c
899014
+++ b/bin/dig/dig.c
899014
@@ -17,6 +17,7 @@
899014
 #include <ctype.h>
899014
 
899014
 #include <isc/app.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/netaddr.h>
899014
 #include <isc/parseint.h>
899014
 #include <isc/platform.h>
899014
@@ -1757,10 +1758,10 @@ dash_option(char *option, char *next, dig_lookup_t **lookup,
899014
 			ptr = ptr2;
899014
 			ptr2 = ptr3;
899014
 		} else  {
899014
-#ifndef PK11_MD5_DISABLE
899014
-			hmacname = DNS_TSIG_HMACMD5_NAME;
899014
-#else
899014
 			hmacname = DNS_TSIG_HMACSHA256_NAME;
899014
+#ifndef PK11_MD5_DISABLE
899014
+			if (isc_md5_available())
899014
+				hmacname = DNS_TSIG_HMACMD5_NAME;
899014
 #endif
899014
 			digestbits = 0;
899014
 		}
899014
diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c
899014
index ecefc98453..94c428ed30 100644
899014
--- a/bin/dig/dighost.c
899014
+++ b/bin/dig/dighost.c
899014
@@ -77,6 +77,7 @@
899014
 #include <isc/hex.h>
899014
 #include <isc/lang.h>
899014
 #include <isc/log.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/netaddr.h>
899014
 #include <isc/netdb.h>
899014
 #include <isc/parseint.h>
899014
@@ -1243,9 +1244,10 @@ parse_hmac(const char *hmac) {
899014
 	digestbits = 0;
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
-	if (strcasecmp(buf, "hmac-md5") == 0) {
899014
+	if (strcasecmp(buf, "hmac-md5") == 0 && isc_md5_available()) {
899014
 		hmacname = DNS_TSIG_HMACMD5_NAME;
899014
-	} else if (strncasecmp(buf, "hmac-md5-", 9) == 0) {
899014
+	} else if (strncasecmp(buf, "hmac-md5-", 9) == 0 &&
899014
+		   isc_md5_available()) {
899014
 		hmacname = DNS_TSIG_HMACMD5_NAME;
899014
 		digestbits = parse_bits(&buf[9], "digest-bits [0..128]", 128);
899014
 	} else
899014
@@ -1365,7 +1367,13 @@ setup_file_key(void) {
899014
 	switch (dst_key_alg(dstkey)) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 	case DST_ALG_HMACMD5:
899014
-		hmacname = DNS_TSIG_HMACMD5_NAME;
899014
+		if (isc_md5_available()) {
899014
+			hmacname = DNS_TSIG_HMACMD5_NAME;
899014
+		} else {
899014
+			printf(";; Couldn't create key %s: bad algorithm\n",
899014
+			       keynametext);
899014
+			goto failure;
899014
+		}
899014
 		break;
899014
 #endif
899014
 	case DST_ALG_HMACSHA1:
899014
diff --git a/bin/dnssec/dnssec-keygen.c b/bin/dnssec/dnssec-keygen.c
899014
index 6fc3ab0979..fc04356ed4 100644
899014
--- a/bin/dnssec/dnssec-keygen.c
899014
+++ b/bin/dnssec/dnssec-keygen.c
899014
@@ -34,6 +34,7 @@
899014
 #include <isc/buffer.h>
899014
 #include <isc/commandline.h>
899014
 #include <isc/entropy.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/print.h>
899014
 #include <isc/region.h>
899014
@@ -560,6 +561,19 @@ main(int argc, char **argv) {
899014
 					"\"-a RSAMD5\"\n");
899014
 			INSIST(freeit == NULL);
899014
 			return (1);
899014
+		} else if (strcasecmp(algname, "HMAC-MD5") == 0) {
899014
+			if (isc_md5_available()) {
899014
+				alg = DST_ALG_HMACMD5;
899014
+			} else {
899014
+				fprintf(stderr,
899014
+					"The use of HMAC-MD5 was disabled\n");
899014
+				return (1);
899014
+			}
899014
+		} else if (strcasecmp(algname, "RSAMD5") == 0 &&
899014
+			   isc_md5_available() == ISC_FALSE) {
899014
+			fprintf(stderr, "The use of RSAMD5 was disabled\n");
899014
+			INSIST(freeit == NULL);
899014
+			return (1);
899014
 		} else if (strcasecmp(algname, "HMAC-MD5") == 0) {
899014
 			alg = DST_ALG_HMACMD5;
899014
 #else
899014
diff --git a/bin/named/config.c b/bin/named/config.c
899014
index 54bc37fff7..c50f759ddd 100644
899014
--- a/bin/named/config.c
899014
+++ b/bin/named/config.c
899014
@@ -17,6 +17,7 @@
899014
 
899014
 #include <isc/buffer.h>
899014
 #include <isc/log.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/parseint.h>
899014
 #include <isc/region.h>
899014
@@ -966,6 +967,21 @@ ns_config_getkeyalgorithm(const char *str, dns_name_t **name,
899014
 	return (ns_config_getkeyalgorithm2(str, name, NULL, digestbits));
899014
 }
899014
 
899014
+static inline int
899014
+algorithms_start() {
899014
+#ifndef PK11_MD5_DISABLE
899014
+	if (isc_md5_available() == ISC_FALSE) {
899014
+		int i = 0;
899014
+		while (algorithms[i].str != NULL &&
899014
+			algorithms[i].hmac == hmacmd5) {
899014
+			i++;
899014
+		}
899014
+		return i;
899014
+	}
899014
+#endif
899014
+	return 0;
899014
+}
899014
+
899014
 isc_result_t
899014
 ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
899014
 			   unsigned int *typep, isc_uint16_t *digestbits)
899014
@@ -975,7 +991,7 @@ ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
899014
 	isc_uint16_t bits;
899014
 	isc_result_t result;
899014
 
899014
-	for (i = 0; algorithms[i].str != NULL; i++) {
899014
+	for (i = algorithms_start(); algorithms[i].str != NULL; i++) {
899014
 		len = strlen(algorithms[i].str);
899014
 		if (strncasecmp(algorithms[i].str, str, len) == 0 &&
899014
 		    (str[len] == '\0' ||
899014
@@ -998,7 +1014,12 @@ ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
899014
 	if (name != NULL) {
899014
 		switch (algorithms[i].hmac) {
899014
 #ifndef PK11_MD5_DISABLE
899014
-		case hmacmd5: *name = dns_tsig_hmacmd5_name; break;
899014
+		case hmacmd5:
899014
+			if (isc_md5_available()) {
899014
+				*name = dns_tsig_hmacmd5_name; break;
899014
+			} else {
899014
+				return (ISC_R_NOTFOUND);
899014
+			}
899014
 #endif
899014
 		case hmacsha1: *name = dns_tsig_hmacsha1_name; break;
899014
 		case hmacsha224: *name = dns_tsig_hmacsha224_name; break;
899014
diff --git a/bin/nsupdate/nsupdate.c b/bin/nsupdate/nsupdate.c
899014
index 6967b49754..bb5d50038f 100644
899014
--- a/bin/nsupdate/nsupdate.c
899014
+++ b/bin/nsupdate/nsupdate.c
899014
@@ -29,6 +29,7 @@
899014
 #include <isc/hash.h>
899014
 #include <isc/lex.h>
899014
 #include <isc/log.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/parseint.h>
899014
 #include <isc/print.h>
899014
@@ -474,9 +475,10 @@ parse_hmac(dns_name_t **hmac, const char *hmacstr, size_t len,
899014
 	strlcpy(buf, hmacstr, ISC_MIN(len + 1, sizeof(buf)));
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
-	if (strcasecmp(buf, "hmac-md5") == 0) {
899014
+	if (strcasecmp(buf, "hmac-md5") == 0 && isc_md5_available()) {
899014
 		*hmac = DNS_TSIG_HMACMD5_NAME;
899014
-	} else if (strncasecmp(buf, "hmac-md5-", 9) == 0) {
899014
+	} else if (strncasecmp(buf, "hmac-md5-", 9) == 0 &&
899014
+		   isc_md5_available()) {
899014
 		*hmac = DNS_TSIG_HMACMD5_NAME;
899014
 		result = isc_parse_uint16(&digestbits, &buf[9], 10);
899014
 		if (result != ISC_R_SUCCESS || digestbits > 128) {
899014
@@ -589,10 +591,10 @@ setup_keystr(void) {
899014
 			exit(1);
899014
 		}
899014
 	} else {
899014
-#ifndef PK11_MD5_DISABLE
899014
-		hmacname = DNS_TSIG_HMACMD5_NAME;
899014
-#else
899014
 		hmacname = DNS_TSIG_HMACSHA256_NAME;
899014
+#ifndef PK11_MD5_DISABLE
899014
+		if (isc_md5_available())
899014
+			hmacname = DNS_TSIG_HMACMD5_NAME;
899014
 #endif
899014
 		name = keystr;
899014
 		n = s;
899014
@@ -729,7 +731,8 @@ setup_keyfile(isc_mem_t *mctx, isc_log_t *lctx) {
899014
 	switch (dst_key_alg(dstkey)) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 	case DST_ALG_HMACMD5:
899014
-		hmacname = DNS_TSIG_HMACMD5_NAME;
899014
+		if (isc_md5_available())
899014
+			hmacname = DNS_TSIG_HMACMD5_NAME;
899014
 		break;
899014
 #endif
899014
 	case DST_ALG_HMACSHA1:
899014
@@ -1604,12 +1607,13 @@ evaluate_key(char *cmdline) {
899014
 			return (STATUS_SYNTAX);
899014
 		}
899014
 		namestr = n + 1;
899014
-	} else
899014
-#ifndef PK11_MD5_DISABLE
899014
-		hmacname = DNS_TSIG_HMACMD5_NAME;
899014
-#else
899014
+	} else {
899014
 		hmacname = DNS_TSIG_HMACSHA256_NAME;
899014
+#ifndef PK11_MD5_DISABLE
899014
+		if (isc_md5_available())
899014
+			hmacname = DNS_TSIG_HMACMD5_NAME;
899014
 #endif
899014
+	}
899014
 
899014
 	isc_buffer_init(&b, namestr, strlen(namestr));
899014
 	isc_buffer_add(&b, strlen(namestr));
899014
diff --git a/bin/rndc/rndc.c b/bin/rndc/rndc.c
899014
index 5c29caf86b..617b06b4a1 100644
899014
--- a/bin/rndc/rndc.c
899014
+++ b/bin/rndc/rndc.c
899014
@@ -21,6 +21,7 @@
899014
 #include <isc/file.h>
899014
 #include <isc/log.h>
899014
 #include <isc/net.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/print.h>
899014
 #include <isc/random.h>
899014
@@ -634,7 +635,7 @@ parse_config(isc_mem_t *mctx, isc_log_t *log, const char *keyname,
899014
 	algorithmstr = cfg_obj_asstring(algorithmobj);
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
-	if (strcasecmp(algorithmstr, "hmac-md5") == 0)
899014
+	if (strcasecmp(algorithmstr, "hmac-md5") == 0 && isc_md5_available())
899014
 		algorithm = ISCCC_ALG_HMACMD5;
899014
 	else
899014
 #endif
899014
diff --git a/bin/tests/optional/hash_test.c b/bin/tests/optional/hash_test.c
899014
index bf2891ad4c..b5f0a1c5f5 100644
899014
--- a/bin/tests/optional/hash_test.c
899014
+++ b/bin/tests/optional/hash_test.c
899014
@@ -90,43 +90,47 @@ main(int argc, char **argv) {
899014
 	print_digest(s, "sha224", digest, ISC_SHA224_DIGESTLENGTH/4);
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
-	s = "abc";
899014
-	isc_md5_init(&md5;;
899014
-	memmove(buffer, s, strlen(s));
899014
-	isc_md5_update(&md5, buffer, strlen(s));
899014
-	isc_md5_final(&md5, digest);
899014
-	print_digest(s, "md5", digest, 4);
899014
-
899014
-	/*
899014
-	 * The 3 HMAC-MD5 examples from RFC2104
899014
-	 */
899014
-	s = "Hi There";
899014
-	memset(key, 0x0b, 16);
899014
-	isc_hmacmd5_init(&hmacmd5, key, 16);
899014
-	memmove(buffer, s, strlen(s));
899014
-	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
899014
-	isc_hmacmd5_sign(&hmacmd5, digest);
899014
-	print_digest(s, "hmacmd5", digest, 4);
899014
-
899014
-	s = "what do ya want for nothing?";
899014
-	strlcpy((char *)key, "Jefe", sizeof(key));
899014
-	isc_hmacmd5_init(&hmacmd5, key, 4);
899014
-	memmove(buffer, s, strlen(s));
899014
-	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
899014
-	isc_hmacmd5_sign(&hmacmd5, digest);
899014
-	print_digest(s, "hmacmd5", digest, 4);
899014
-
899014
-	s = "\335\335\335\335\335\335\335\335\335\335"
899014
-	    "\335\335\335\335\335\335\335\335\335\335"
899014
-	    "\335\335\335\335\335\335\335\335\335\335"
899014
-	    "\335\335\335\335\335\335\335\335\335\335"
899014
-	    "\335\335\335\335\335\335\335\335\335\335";
899014
-	memset(key, 0xaa, 16);
899014
-	isc_hmacmd5_init(&hmacmd5, key, 16);
899014
-	memmove(buffer, s, strlen(s));
899014
-	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
899014
-	isc_hmacmd5_sign(&hmacmd5, digest);
899014
-	print_digest(s, "hmacmd5", digest, 4);
899014
+	if (isc_md5_available()) {
899014
+		s = "abc";
899014
+		isc_md5_init(&md5;;
899014
+		memmove(buffer, s, strlen(s));
899014
+		isc_md5_update(&md5, buffer, strlen(s));
899014
+		isc_md5_final(&md5, digest);
899014
+		print_digest(s, "md5", digest, 4);
899014
+
899014
+		/*
899014
+		 * The 3 HMAC-MD5 examples from RFC2104
899014
+		 */
899014
+		s = "Hi There";
899014
+		memset(key, 0x0b, 16);
899014
+		isc_hmacmd5_init(&hmacmd5, key, 16);
899014
+		memmove(buffer, s, strlen(s));
899014
+		isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
899014
+		isc_hmacmd5_sign(&hmacmd5, digest);
899014
+		print_digest(s, "hmacmd5", digest, 4);
899014
+
899014
+		s = "what do ya want for nothing?";
899014
+		strlcpy((char *)key, "Jefe", sizeof(key));
899014
+		isc_hmacmd5_init(&hmacmd5, key, 4);
899014
+		memmove(buffer, s, strlen(s));
899014
+		isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
899014
+		isc_hmacmd5_sign(&hmacmd5, digest);
899014
+		print_digest(s, "hmacmd5", digest, 4);
899014
+
899014
+		s = "\335\335\335\335\335\335\335\335\335\335"
899014
+		    "\335\335\335\335\335\335\335\335\335\335"
899014
+		    "\335\335\335\335\335\335\335\335\335\335"
899014
+		    "\335\335\335\335\335\335\335\335\335\335"
899014
+		    "\335\335\335\335\335\335\335\335\335\335";
899014
+		memset(key, 0xaa, 16);
899014
+		isc_hmacmd5_init(&hmacmd5, key, 16);
899014
+		memmove(buffer, s, strlen(s));
899014
+		isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
899014
+		isc_hmacmd5_sign(&hmacmd5, digest);
899014
+		print_digest(s, "hmacmd5", digest, 4);
899014
+	} else {
899014
+		fprintf(stderr, "Skipping disabled MD5 algorithm\n");
899014
+	}
899014
 #endif
899014
 
899014
 	/*
899014
diff --git a/bin/tests/system/tkey/keycreate.c b/bin/tests/system/tkey/keycreate.c
899014
index 2a0ee94888..489f4390dc 100644
899014
--- a/bin/tests/system/tkey/keycreate.c
899014
+++ b/bin/tests/system/tkey/keycreate.c
899014
@@ -20,6 +20,7 @@
899014
 #include <isc/entropy.h>
899014
 #include <isc/hash.h>
899014
 #include <isc/log.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/print.h>
899014
 #include <isc/sockaddr.h>
899014
@@ -142,6 +143,8 @@ sendquery(isc_task_t *task, isc_event_t *event) {
899014
 	static char keystr[] = "0123456789ab";
899014
 
899014
 	isc_event_free(&event);
899014
+	if (isc_md5_available() == ISC_FALSE)
899014
+		CHECK("MD5 was disabled", ISC_R_NOTIMPLEMENTED);
899014
 
899014
 	result = ISC_R_FAILURE;
899014
 	if (inet_pton(AF_INET, "10.53.0.1", &inaddr) != 1)
899014
diff --git a/bin/tests/system/tkey/keydelete.c b/bin/tests/system/tkey/keydelete.c
899014
index 7057c318e4..36ee6c7d21 100644
899014
--- a/bin/tests/system/tkey/keydelete.c
899014
+++ b/bin/tests/system/tkey/keydelete.c
899014
@@ -225,12 +225,18 @@ main(int argc, char **argv) {
899014
 	result = dst_key_fromnamedfile(keyname, NULL, type, mctx, &dstkey);
899014
 	CHECK("dst_key_fromnamedfile", result);
899014
 #ifndef PK11_MD5_DISABLE
899014
-	result = dns_tsigkey_createfromkey(dst_key_name(dstkey),
899014
-					   DNS_TSIG_HMACMD5_NAME,
899014
-					   dstkey, ISC_TRUE, NULL, 0, 0,
899014
-					   mctx, ring, &tsigkey);
899014
-	dst_key_free(&dstkey);
899014
-	CHECK("dns_tsigkey_createfromkey", result);
899014
+	if (isc_md5_available()) {
899014
+		result = dns_tsigkey_createfromkey(dst_key_name(dstkey),
899014
+						   DNS_TSIG_HMACMD5_NAME,
899014
+						   dstkey, ISC_TRUE,
899014
+						   NULL, 0, 0,
899014
+						   mctx, ring, &tsigkey);
899014
+		dst_key_free(&dstkey);
899014
+		CHECK("dns_tsigkey_createfromkey", result);
899014
+	} else {
899014
+		dst_key_free(&dstkey);
899014
+		CHECK("MD5 was disabled", ISC_R_NOTIMPLEMENTED);
899014
+	}
899014
 #else
899014
 	dst_key_free(&dstkey);
899014
 	CHECK("MD5 was disabled", ISC_R_NOTIMPLEMENTED);
899014
diff --git a/lib/bind9/check.c b/lib/bind9/check.c
899014
index 3da83a7ae2..1a3d534799 100644
899014
--- a/lib/bind9/check.c
899014
+++ b/lib/bind9/check.c
899014
@@ -21,6 +21,7 @@
899014
 #include <isc/file.h>
899014
 #include <isc/hex.h>
899014
 #include <isc/log.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/netaddr.h>
899014
 #include <isc/parseint.h>
899014
@@ -2572,6 +2573,15 @@ bind9_check_key(const cfg_obj_t *key, isc_log_t *logctx) {
899014
 	}
899014
 
899014
 	algorithm = cfg_obj_asstring(algobj);
899014
+#ifndef PK11_MD5_DISABLE
899014
+	/* Skip hmac-md5* algorithms */
899014
+	if (isc_md5_available() == ISC_FALSE &&
899014
+	    strncasecmp(algorithm, "hmac-md5", 8) == 0) {
899014
+		cfg_obj_log(algobj, logctx, ISC_LOG_ERROR,
899014
+			    "disabled algorithm '%s'", algorithm);
899014
+		return (ISC_R_DISABLED);
899014
+	}
899014
+#endif
899014
 	for (i = 0; algorithms[i].name != NULL; i++) {
899014
 		len = strlen(algorithms[i].name);
899014
 		if (strncasecmp(algorithms[i].name, algorithm, len) == 0 &&
899014
diff --git a/lib/dns/dst_api.c b/lib/dns/dst_api.c
899014
index 4f3d6ac55c..dbece0ac56 100644
899014
--- a/lib/dns/dst_api.c
899014
+++ b/lib/dns/dst_api.c
899014
@@ -190,6 +190,12 @@ dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
899014
 	dst_result_register();
899014
 
899014
 	memset(dst_t_func, 0, sizeof(dst_t_func));
899014
+
899014
+#ifdef OPENSSL
899014
+	RETERR(dst__openssl_init(engine));
899014
+#elif PKCS11CRYPTO
899014
+	RETERR(dst__pkcs11_init(mctx, engine));
899014
+#endif
899014
 #ifndef PK11_MD5_DISABLE
899014
 	RETERR(dst__hmacmd5_init(&dst_t_func[DST_ALG_HMACMD5]));
899014
 #endif
899014
@@ -199,7 +205,6 @@ dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
899014
 	RETERR(dst__hmacsha384_init(&dst_t_func[DST_ALG_HMACSHA384]));
899014
 	RETERR(dst__hmacsha512_init(&dst_t_func[DST_ALG_HMACSHA512]));
899014
 #ifdef OPENSSL
899014
-	RETERR(dst__openssl_init(engine));
899014
 #ifndef PK11_MD5_DISABLE
899014
 	RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSAMD5],
899014
 				    DST_ALG_RSAMD5));
899014
@@ -233,14 +238,18 @@ dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
899014
 	RETERR(dst__openssleddsa_init(&dst_t_func[DST_ALG_ED448]));
899014
 #endif
899014
 #elif PKCS11CRYPTO
899014
-	RETERR(dst__pkcs11_init(mctx, engine));
899014
 #ifndef PK11_MD5_DISABLE
899014
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSAMD5]));
899014
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSAMD5],
899014
+				   DST_ALG_RSAMD5));
899014
 #endif
899014
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA1]));
899014
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_NSEC3RSASHA1]));
899014
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA256]));
899014
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA512]));
899014
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA1],
899014
+				   DST_ALG_RSASHA1));
899014
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_NSEC3RSASHA1],
899014
+				   DST_ALG_NSEC3RSASHA1));
899014
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA256],
899014
+				   DST_ALG_RSASHA256));
899014
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA512],
899014
+				   DST_ALG_RSASHA512));
899014
 #ifndef PK11_DSA_DISABLE
899014
 	RETERR(dst__pkcs11dsa_init(&dst_t_func[DST_ALG_DSA]));
899014
 	RETERR(dst__pkcs11dsa_init(&dst_t_func[DST_ALG_NSEC3DSA]));
899014
diff --git a/lib/dns/dst_internal.h b/lib/dns/dst_internal.h
899014
index 640519a5ba..deb7ed4e13 100644
899014
--- a/lib/dns/dst_internal.h
899014
+++ b/lib/dns/dst_internal.h
899014
@@ -245,7 +245,8 @@ isc_result_t dst__hmacsha384_init(struct dst_func **funcp);
899014
 isc_result_t dst__hmacsha512_init(struct dst_func **funcp);
899014
 isc_result_t dst__opensslrsa_init(struct dst_func **funcp,
899014
 				  unsigned char algorithm);
899014
-isc_result_t dst__pkcs11rsa_init(struct dst_func **funcp);
899014
+isc_result_t dst__pkcs11rsa_init(struct dst_func **funcp,
899014
+				 unsigned char algorithm);
899014
 #ifndef PK11_DSA_DISABLE
899014
 isc_result_t dst__openssldsa_init(struct dst_func **funcp);
899014
 isc_result_t dst__pkcs11dsa_init(struct dst_func **funcp);
899014
diff --git a/lib/dns/dst_parse.c b/lib/dns/dst_parse.c
899014
index b0e5c895c6..03f2b8ace8 100644
899014
--- a/lib/dns/dst_parse.c
899014
+++ b/lib/dns/dst_parse.c
899014
@@ -30,6 +30,7 @@
899014
 #include <isc/file.h>
899014
 #include <isc/fsaccess.h>
899014
 #include <isc/lex.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/print.h>
899014
 #include <isc/stdtime.h>
899014
@@ -393,6 +394,10 @@ check_data(const dst_private_t *priv, const unsigned int alg,
899014
 	switch (alg) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 	case DST_ALG_RSAMD5:
899014
+		if (isc_md5_available())
899014
+			return (check_rsa(priv, external));
899014
+		else
899014
+			return (DST_R_UNSUPPORTEDALG);
899014
 #endif
899014
 	case DST_ALG_RSASHA1:
899014
 	case DST_ALG_NSEC3RSASHA1:
899014
@@ -418,7 +423,10 @@ check_data(const dst_private_t *priv, const unsigned int alg,
899014
 		return (check_eddsa(priv, external));
899014
 #ifndef PK11_MD5_DISABLE
899014
 	case DST_ALG_HMACMD5:
899014
-		return (check_hmac_md5(priv, old));
899014
+		if (isc_md5_available())
899014
+			return (check_hmac_md5(priv, old));
899014
+		else
899014
+			return (DST_R_UNSUPPORTEDALG);
899014
 #endif
899014
 	case DST_ALG_HMACSHA1:
899014
 		return (check_hmac_sha(priv, HMACSHA1_NTAGS, alg));
899014
@@ -637,11 +645,13 @@ dst__privstruct_parse(dst_key_t *key, unsigned int alg, isc_lex_t *lex,
899014
 	}
899014
 
899014
 #ifdef PK11_MD5_DISABLE
899014
-	check = check_data(priv, alg == DST_ALG_RSA ? DST_ALG_RSASHA1 : alg,
899014
-			   ISC_TRUE, external);
899014
+	if (alg == DST_ALG_RSA)
899014
+		alg = DST_ALG_RSASHA1;
899014
 #else
899014
-	check = check_data(priv, alg, ISC_TRUE, external);
899014
+	if (isc_md5_available() == ISC_FALSE && alg == DST_ALG_RSA)
899014
+		alg = DST_ALG_RSASHA1;
899014
 #endif
899014
+	check = check_data(priv, alg, ISC_TRUE, external);
899014
 	if (check < 0) {
899014
 		ret = DST_R_INVALIDPRIVATEKEY;
899014
 		goto fail;
899014
diff --git a/lib/dns/hmac_link.c b/lib/dns/hmac_link.c
899014
index 59aa4705e5..21bfa44450 100644
899014
--- a/lib/dns/hmac_link.c
899014
+++ b/lib/dns/hmac_link.c
899014
@@ -338,25 +338,17 @@ static dst_func_t hmacmd5_functions = {
899014
 
899014
 isc_result_t
899014
 dst__hmacmd5_init(dst_func_t **funcp) {
899014
-#ifdef HAVE_FIPS_MODE
899014
 	/*
899014
-	 * Problems from OpenSSL are likely from FIPS mode
899014
+	 * Prevent use of incorrect crypto
899014
 	 */
899014
-	int fips_mode = FIPS_mode();
899014
-
899014
-	if (fips_mode != 0) {
899014
-		UNEXPECTED_ERROR(__FILE__, __LINE__,
899014
-				 "FIPS mode is %d: MD5 is only supported "
899014
-				 "if the value is 0.\n"
899014
-				 "Please disable either FIPS mode or MD5.",
899014
-				 fips_mode);
899014
+
899014
+#ifndef PK11_MD5_DISABLE
899014
+	if (isc_md5_available() == ISC_FALSE) {
899014
+		/* Intentionally skip initialization */
899014
+		return (ISC_R_SUCCESS);
899014
 	}
899014
 #endif
899014
 
899014
-	/*
899014
-	 * Prevent use of incorrect crypto
899014
-	 */
899014
-
899014
 	RUNTIME_CHECK(isc_md5_check(ISC_FALSE));
899014
 	RUNTIME_CHECK(isc_hmacmd5_check(0));
899014
 
899014
diff --git a/lib/dns/opensslrsa_link.c b/lib/dns/opensslrsa_link.c
899014
index f4847bbe74..126cebca19 100644
899014
--- a/lib/dns/opensslrsa_link.c
899014
+++ b/lib/dns/opensslrsa_link.c
899014
@@ -1801,6 +1801,12 @@ dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
899014
 
899014
 	if (*funcp == NULL) {
899014
 		switch (algorithm) {
899014
+#ifndef PK11_MD5_DISABLE
899014
+		case DST_ALG_RSAMD5:
899014
+			if (isc_md5_available())
899014
+				*funcp = &opensslrsa_functions;
899014
+			break;
899014
+#endif
899014
 		case DST_ALG_RSASHA256:
899014
 #if defined(HAVE_EVP_SHA256) || !USE_EVP
899014
 			*funcp = &opensslrsa_functions;
899014
diff --git a/lib/dns/pkcs11rsa_link.c b/lib/dns/pkcs11rsa_link.c
899014
index 56955203e9..af6008d4dd 100644
899014
--- a/lib/dns/pkcs11rsa_link.c
899014
+++ b/lib/dns/pkcs11rsa_link.c
899014
@@ -94,10 +94,15 @@ pkcs11rsa_createctx_sign(dst_key_t *key, dst_context_t *dctx) {
899014
 #endif
899014
 
899014
 	/*
899014
-	 * Reject incorrect RSA key lengths.
899014
+	 * Reject incorrect RSA key lengths or disabled algorithms.
899014
 	 */
899014
 	switch (dctx->key->key_alg) {
899014
 	case DST_ALG_RSAMD5:
899014
+#ifndef PK11_MD5_DISABLE
899014
+		if (isc_md5_available() == ISC_FALSE)
899014
+			return (ISC_R_FAILURE);
899014
+#endif
899014
+		/* FALLTHROUGH */
899014
 	case DST_ALG_RSASHA1:
899014
 	case DST_ALG_NSEC3RSASHA1:
899014
 		/* From RFC 3110 */
899014
@@ -634,6 +639,9 @@ pkcs11rsa_createctx(dst_key_t *key, dst_context_t *dctx) {
899014
 	switch (key->key_alg) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 	case DST_ALG_RSAMD5:
899014
+		if (isc_md5_available() == ISC_FALSE)
899014
+			return (ISC_R_FAILURE);
899014
+	
899014
 		mech.mechanism = CKM_MD5;
899014
 		break;
899014
 #endif
899014
@@ -790,6 +798,9 @@ pkcs11rsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
899014
 	switch (key->key_alg) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 	case DST_ALG_RSAMD5:
899014
+		if (isc_md5_available() == ISC_FALSE)
899014
+			return (ISC_R_FAILURE);
899014
+
899014
 		der = md5_der;
899014
 		derlen = sizeof(md5_der);
899014
 		hashlen = ISC_MD5_DIGESTLENGTH;
899014
@@ -1014,6 +1025,9 @@ pkcs11rsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
899014
 	switch (key->key_alg) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 	case DST_ALG_RSAMD5:
899014
+		if (isc_md5_available() == ISC_FALSE)
899014
+			return (ISC_R_FAILURE);
899014
+
899014
 		der = md5_der;
899014
 		derlen = sizeof(md5_der);
899014
 		hashlen = ISC_MD5_DIGESTLENGTH;
899014
@@ -2217,11 +2231,22 @@ static dst_func_t pkcs11rsa_functions = {
899014
 };
899014
 
899014
 isc_result_t
899014
-dst__pkcs11rsa_init(dst_func_t **funcp) {
899014
+dst__pkcs11rsa_init(dst_func_t **funcp, unsigned char algorithm) {
899014
 	REQUIRE(funcp != NULL);
899014
 
899014
-	if (*funcp == NULL)
899014
-		*funcp = &pkcs11rsa_functions;
899014
+	if (*funcp == NULL) {
899014
+		switch (algorithm) {
899014
+#ifndef PK11_MD5_DISABLE
899014
+			case DST_ALG_RSAMD5:
899014
+				if (isc_md5_available())
899014
+					*funcp = &pkcs11rsa_functions;
899014
+				break;
899014
+#endif
899014
+			default:
899014
+				*funcp = &pkcs11rsa_functions;
899014
+				break;
899014
+		}
899014
+	}
899014
 	return (ISC_R_SUCCESS);
899014
 }
899014
 
899014
diff --git a/lib/dns/rcode.c b/lib/dns/rcode.c
899014
index 937d8fc1ec..d1fa8d5870 100644
899014
--- a/lib/dns/rcode.c
899014
+++ b/lib/dns/rcode.c
899014
@@ -14,6 +14,7 @@
899014
 #include <ctype.h>
899014
 
899014
 #include <isc/buffer.h>
899014
+#include <isc/md5.h>
899014
 #include <isc/parseint.h>
899014
 #include <isc/print.h>
899014
 #include <isc/region.h>
899014
@@ -347,17 +348,33 @@ dns_cert_totext(dns_cert_t cert, isc_buffer_t *target) {
899014
 	return (dns_mnemonic_totext(cert, target, certs));
899014
 }
899014
 
899014
+static inline struct tbl *
899014
+secalgs_tbl_start() {
899014
+	struct tbl *algs = secalgs;
899014
+
899014
+#ifndef PK11_MD5_DISABLE
899014
+	if (isc_md5_available() == ISC_FALSE) {
899014
+		while (algs->name != NULL &&
899014
+		       algs->value == DNS_KEYALG_RSAMD5)
899014
+			++algs;
899014
+	}
899014
+#endif
899014
+	return algs;
899014
+}
899014
+
899014
 isc_result_t
899014
 dns_secalg_fromtext(dns_secalg_t *secalgp, isc_textregion_t *source) {
899014
 	unsigned int value;
899014
-	RETERR(dns_mnemonic_fromtext(&value, source, secalgs, 0xff));
899014
+
899014
+	RETERR(dns_mnemonic_fromtext(&value, source,
899014
+	                             secalgs_tbl_start(), 0xff));
899014
 	*secalgp = value;
899014
 	return (ISC_R_SUCCESS);
899014
 }
899014
 
899014
 isc_result_t
899014
 dns_secalg_totext(dns_secalg_t secalg, isc_buffer_t *target) {
899014
-	return (dns_mnemonic_totext(secalg, target, secalgs));
899014
+	return (dns_mnemonic_totext(secalg, target, secalgs_tbl_start()));
899014
 }
899014
 
899014
 void
899014
diff --git a/lib/dns/tests/rsa_test.c b/lib/dns/tests/rsa_test.c
899014
index 224cf5b475..44040dd8b7 100644
899014
--- a/lib/dns/tests/rsa_test.c
899014
+++ b/lib/dns/tests/rsa_test.c
899014
@@ -19,6 +19,7 @@
899014
 #include <stdio.h>
899014
 #include <string.h>
899014
 
899014
+#include <isc/md5.h>
899014
 #include <isc/util.h>
899014
 #include <isc/print.h>
899014
 
899014
@@ -225,23 +226,25 @@ ATF_TC_BODY(isc_rsa_verify, tc) {
899014
 	/* RSAMD5 */
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
-	key->key_alg = DST_ALG_RSAMD5;
899014
+	if (isc_md5_available()) {
899014
+		key->key_alg = DST_ALG_RSAMD5;
899014
 
899014
-	ret = dst_context_create3(key, mctx, DNS_LOGCATEGORY_DNSSEC,
899014
-				  ISC_FALSE, &ctx;;
899014
-	ATF_REQUIRE_EQ(ret, ISC_R_SUCCESS);
899014
+		ret = dst_context_create3(key, mctx, DNS_LOGCATEGORY_DNSSEC,
899014
+					  ISC_FALSE, &ctx;;
899014
+		ATF_REQUIRE_EQ(ret, ISC_R_SUCCESS);
899014
 
899014
-	r.base = d;
899014
-	r.length = 10;
899014
-	ret = dst_context_adddata(ctx, &r);
899014
-	ATF_REQUIRE_EQ(ret, ISC_R_SUCCESS);
899014
+		r.base = d;
899014
+		r.length = 10;
899014
+		ret = dst_context_adddata(ctx, &r);
899014
+		ATF_REQUIRE_EQ(ret, ISC_R_SUCCESS);
899014
 
899014
-	r.base = sigmd5;
899014
-	r.length = 256;
899014
-	ret = dst_context_verify(ctx, &r);
899014
-	ATF_REQUIRE_EQ(ret, ISC_R_SUCCESS);
899014
+		r.base = sigmd5;
899014
+		r.length = 256;
899014
+		ret = dst_context_verify(ctx, &r);
899014
+		ATF_REQUIRE_EQ(ret, ISC_R_SUCCESS);
899014
 
899014
-	dst_context_destroy(&ctx;;
899014
+		dst_context_destroy(&ctx;;
899014
+	}
899014
 #endif
899014
 
899014
 	/* RSASHA256 */
899014
diff --git a/lib/dns/tests/tsig_test.c b/lib/dns/tests/tsig_test.c
899014
index ee025c2387..c403d9954d 100644
899014
--- a/lib/dns/tests/tsig_test.c
899014
+++ b/lib/dns/tests/tsig_test.c
899014
@@ -14,6 +14,7 @@
899014
 #include <config.h>
899014
 #include <atf-c.h>
899014
 
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/print.h>
899014
 
899014
diff --git a/lib/dns/tkey.c b/lib/dns/tkey.c
899014
index d9f68e50b1..a8edde47b5 100644
899014
--- a/lib/dns/tkey.c
899014
+++ b/lib/dns/tkey.c
899014
@@ -242,6 +242,9 @@ compute_secret(isc_buffer_t *shared, isc_region_t *queryrandomness,
899014
 	unsigned char digests[32];
899014
 	unsigned int i;
899014
 
899014
+	if (isc_md5_available() == ISC_FALSE)
899014
+		return (ISC_R_NOTIMPLEMENTED);
899014
+
899014
 	isc_buffer_usedregion(shared, &r);
899014
 
899014
 	/*
899014
@@ -318,6 +321,12 @@ process_dhtkey(dns_message_t *msg, dns_name_t *signer, dns_name_t *name,
899014
 	}
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
+	if (isc_md5_available() == ISC_FALSE) {
899014
+		tkey_log("process_dhtkey: MD5 was disabled");
899014
+		tkeyout->error = dns_tsigerror_badalg;
899014
+		return (ISC_R_SUCCESS);
899014
+	}
899014
+
899014
 	if (!dns_name_equal(&tkeyin->algorithm, DNS_TSIG_HMACMD5_NAME)) {
899014
 		tkey_log("process_dhtkey: algorithms other than "
899014
 			 "hmac-md5 are not supported");
899014
diff --git a/lib/dns/tsec.c b/lib/dns/tsec.c
899014
index a367291f23..37baad7437 100644
899014
--- a/lib/dns/tsec.c
899014
+++ b/lib/dns/tsec.c
899014
@@ -11,6 +11,7 @@
899014
 
899014
 #include <config.h>
899014
 
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/util.h>
899014
 
899014
@@ -63,7 +64,12 @@ dns_tsec_create(isc_mem_t *mctx, dns_tsectype_t type, dst_key_t *key,
899014
 		switch (dst_key_alg(key)) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 		case DST_ALG_HMACMD5:
899014
-			algname = dns_tsig_hmacmd5_name;
899014
+			if (isc_md5_available()) {
899014
+				algname = dns_tsig_hmacmd5_name;
899014
+			} else {
899014
+				isc_mem_put(mctx, tsec, sizeof(*tsec));
899014
+				return (DNS_R_BADALG);
899014
+			}
899014
 			break;
899014
 #endif
899014
 		case DST_ALG_HMACSHA1:
899014
diff --git a/lib/dns/tsig.c b/lib/dns/tsig.c
899014
index bdcc581bc3..70805bb709 100644
899014
--- a/lib/dns/tsig.c
899014
+++ b/lib/dns/tsig.c
899014
@@ -270,7 +270,8 @@ dns_tsigkey_createfromkey(dns_name_t *name, dns_name_t *algorithm,
899014
 	(void)dns_name_downcase(&tkey->name, &tkey->name, NULL);
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
-	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME)) {
899014
+	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME) &&
899014
+	    isc_md5_available()) {
899014
 		tkey->algorithm = DNS_TSIG_HMACMD5_NAME;
899014
 		if (dstkey != NULL && dst_key_alg(dstkey) != DST_ALG_HMACMD5) {
899014
 			ret = DNS_R_BADALG;
899014
@@ -496,7 +497,8 @@ destroyring(dns_tsig_keyring_t *ring) {
899014
 static unsigned int
899014
 dst_alg_fromname(dns_name_t *algorithm) {
899014
 #ifndef PK11_MD5_DISABLE
899014
-	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME)) {
899014
+	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME) &&
899014
+	    isc_md5_available()) {
899014
 		return (DST_ALG_HMACMD5);
899014
 	} else
899014
 #endif
899014
@@ -680,7 +682,8 @@ dns_tsigkey_create(dns_name_t *name, dns_name_t *algorithm,
899014
 		REQUIRE(secret != NULL);
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
-	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME)) {
899014
+	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME) &&
899014
+	    isc_md5_available()) {
899014
 		if (secret != NULL) {
899014
 			isc_buffer_t b;
899014
 
899014
@@ -1280,7 +1283,7 @@ dns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,
899014
 		return (ret);
899014
 	if (
899014
 #ifndef PK11_MD5_DISABLE
899014
-	    alg == DST_ALG_HMACMD5 ||
899014
+	    (alg == DST_ALG_HMACMD5 && isc_md5_available()) ||
899014
 #endif
899014
 	    alg == DST_ALG_HMACSHA1 ||
899014
 	    alg == DST_ALG_HMACSHA224 || alg == DST_ALG_HMACSHA256 ||
899014
@@ -1449,7 +1452,7 @@ dns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,
899014
 
899014
 	if (
899014
 #ifndef PK11_MD5_DISABLE
899014
-	    alg == DST_ALG_HMACMD5 ||
899014
+	    (alg == DST_ALG_HMACMD5 && isc_md5_available()) ||
899014
 #endif
899014
 	    alg == DST_ALG_HMACSHA1 ||
899014
 	    alg == DST_ALG_HMACSHA224 || alg == DST_ALG_HMACSHA256 ||
899014
@@ -1590,7 +1593,7 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
899014
 			goto cleanup_querystruct;
899014
 		if (
899014
 #ifndef PK11_MD5_DISABLE
899014
-			alg == DST_ALG_HMACMD5 ||
899014
+			(alg == DST_ALG_HMACMD5 && isc_md5_available()) ||
899014
 #endif
899014
 			alg == DST_ALG_HMACSHA1 ||
899014
 			alg == DST_ALG_HMACSHA224 ||
899014
@@ -1769,7 +1772,7 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
899014
 			goto cleanup_context;
899014
 		if (
899014
 #ifndef PK11_MD5_DISABLE
899014
-			alg == DST_ALG_HMACMD5 ||
899014
+			(alg == DST_ALG_HMACMD5 && isc_md5_available()) ||
899014
 #endif
899014
 			alg == DST_ALG_HMACSHA1 ||
899014
 			alg == DST_ALG_HMACSHA224 ||
899014
diff --git a/lib/isc/include/isc/md5.h b/lib/isc/include/isc/md5.h
899014
index e5f46dd9c7..9d11f9f8b6 100644
899014
--- a/lib/isc/include/isc/md5.h
899014
+++ b/lib/isc/include/isc/md5.h
899014
@@ -89,6 +89,9 @@ isc_md5_final(isc_md5_t *ctx, unsigned char *digest);
899014
 isc_boolean_t
899014
 isc_md5_check(isc_boolean_t testing);
899014
 
899014
+isc_boolean_t
899014
+isc_md5_available(void);
899014
+
899014
 ISC_LANG_ENDDECLS
899014
 
899014
 #endif /* !PK11_MD5_DISABLE */
899014
diff --git a/lib/isc/md5.c b/lib/isc/md5.c
899014
index 740d863b1b..aefd16478f 100644
899014
--- a/lib/isc/md5.c
899014
+++ b/lib/isc/md5.c
899014
@@ -35,6 +35,7 @@
899014
 
899014
 #include <isc/assertions.h>
899014
 #include <isc/md5.h>
899014
+#include <isc/once.h>
899014
 #include <isc/platform.h>
899014
 #include <isc/safe.h>
899014
 #include <isc/string.h>
899014
@@ -53,6 +54,9 @@
899014
 #define EVP_MD_CTX_free(ptr) EVP_MD_CTX_cleanup(ptr)
899014
 #endif
899014
 
899014
+static isc_once_t available_once = ISC_ONCE_INIT;
899014
+static isc_boolean_t available = ISC_FALSE;
899014
+
899014
 void
899014
 isc_md5_init(isc_md5_t *ctx) {
899014
 	ctx->ctx = EVP_MD_CTX_new();
899014
@@ -84,8 +88,33 @@ isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
899014
 	ctx->ctx = NULL;
899014
 }
899014
 
899014
+static void
899014
+do_detect_available() {
899014
+	isc_md5_t local;
899014
+	isc_md5_t *ctx = &local;
899014
+	unsigned char digest[ISC_MD5_DIGESTLENGTH];
899014
+
899014
+	ctx->ctx = EVP_MD_CTX_new();
899014
+	RUNTIME_CHECK(ctx->ctx != NULL);
899014
+	available = ISC_TF(EVP_DigestInit(ctx->ctx, EVP_md5()) == 1);
899014
+	if (available)
899014
+		(void)EVP_DigestFinal(ctx->ctx, digest, NULL);
899014
+	EVP_MD_CTX_free(ctx->ctx);
899014
+	ctx->ctx = NULL;
899014
+}
899014
+
899014
+isc_boolean_t
899014
+isc_md5_available() {
899014
+	RUNTIME_CHECK(isc_once_do(&available_once, do_detect_available)
899014
+		      == ISC_R_SUCCESS);
899014
+	return available;
899014
+}
899014
+
899014
 #elif PKCS11CRYPTO
899014
 
899014
+static isc_once_t available_once = ISC_ONCE_INIT;
899014
+static isc_boolean_t available = ISC_FALSE;
899014
+
899014
 void
899014
 isc_md5_init(isc_md5_t *ctx) {
899014
 	CK_RV rv;
899014
@@ -128,6 +157,31 @@ isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
899014
 	pk11_return_session(ctx);
899014
 }
899014
 
899014
+static void
899014
+do_detect_available() {
899014
+	isc_md5_t local;
899014
+	isc_md5_t *ctx = &local;
899014
+	CK_RV rv;
899014
+	CK_MECHANISM mech = { CKM_MD5, NULL, 0 };
899014
+
899014
+	if (pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
899014
+				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS)
899014
+	{
899014
+		rv = pkcs_C_DigestInit(ctx->session, &mech);
899014
+		isc_md5_invalidate(ctx);
899014
+		available = (ISC_TF(rv == CKR_OK));
899014
+	} else {
899014
+		available = ISC_FALSE;
899014
+	}
899014
+}
899014
+
899014
+isc_boolean_t
899014
+isc_md5_available() {
899014
+	RUNTIME_CHECK(isc_once_do(&available_once, do_detect_available)
899014
+		      == ISC_R_SUCCESS);
899014
+	return available;
899014
+}
899014
+
899014
 #else
899014
 
899014
 static void
899014
@@ -337,6 +391,11 @@ isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
899014
 	memmove(digest, ctx->buf, 16);
899014
 	isc_safe_memwipe(ctx, sizeof(*ctx));	/* In case it's sensitive */
899014
 }
899014
+
899014
+isc_boolean_t
899014
+isc_md5_available() {
899014
+	return ISC_TRUE;
899014
+}
899014
 #endif
899014
 
899014
 /*
899014
diff --git a/lib/isc/pk11.c b/lib/isc/pk11.c
899014
index fc75a46154..48e1031974 100644
899014
--- a/lib/isc/pk11.c
899014
+++ b/lib/isc/pk11.c
899014
@@ -191,13 +191,12 @@ pk11_initialize(isc_mem_t *mctx, const char *engine) {
899014
 	LOCK(&alloclock);
899014
 	if ((mctx != NULL) && (pk11_mctx == NULL) && (allocsize == 0))
899014
 		isc_mem_attach(mctx, &pk11_mctx);
899014
+	UNLOCK(&alloclock);
899014
+
899014
+	LOCK(&sessionlock);
899014
 	if (initialized) {
899014
-		UNLOCK(&alloclock);
899014
-		return (ISC_R_SUCCESS);
899014
-	} else {
899014
-		LOCK(&sessionlock);
899014
-		initialized = ISC_TRUE;
899014
-		UNLOCK(&alloclock);
899014
+		result = ISC_R_SUCCESS;
899014
+		goto unlock;
899014
 	}
899014
 
899014
 	ISC_LIST_INIT(tokens);
899014
@@ -237,6 +236,7 @@ pk11_initialize(isc_mem_t *mctx, const char *engine) {
899014
 	}
899014
 #endif
899014
 #endif /* PKCS11CRYPTO */
899014
+	initialized = ISC_TRUE;
899014
 	result = ISC_R_SUCCESS;
899014
  unlock:
899014
 	UNLOCK(&sessionlock);
899014
@@ -273,9 +273,14 @@ pk11_finalize(void) {
899014
 		pk11_mem_put(token, sizeof(*token));
899014
 		token = next;
899014
 	}
899014
+	LOCK(&alloclock);
899014
 	if (pk11_mctx != NULL)
899014
 		isc_mem_detach(&pk11_mctx);
899014
+	UNLOCK(&alloclock);
899014
+
899014
+	LOCK(&sessionlock);
899014
 	initialized = ISC_FALSE;
899014
+	UNLOCK(&sessionlock);
899014
 	return (ret);
899014
 }
899014
 
899014
@@ -589,6 +594,8 @@ scan_slots(void) {
899014
 	pk11_token_t *token;
899014
 	unsigned int i;
899014
 	isc_boolean_t bad;
899014
+	unsigned int best_rsa_algorithms = 0;
899014
+	unsigned int best_digest_algorithms = 0;
899014
 
899014
 	slotCount = 0;
899014
 	PK11_FATALCHECK(pkcs_C_GetSlotList, (CK_FALSE, NULL_PTR, &slotCount));
899014
@@ -601,6 +608,8 @@ scan_slots(void) {
899014
 	PK11_FATALCHECK(pkcs_C_GetSlotList, (CK_FALSE, slotList, &slotCount));
899014
 
899014
 	for (i = 0; i < slotCount; i++) {
899014
+		unsigned int rsa_algorithms = 0;
899014
+		unsigned int digest_algorithms = 0;
899014
 		slot = slotList[i];
899014
 		PK11_TRACE2("slot#%u=0x%lx\n", i, slot);
899014
 
899014
@@ -640,11 +649,12 @@ scan_slots(void) {
899014
 		if ((rv != CKR_OK) ||
899014
 		    ((mechInfo.flags & CKF_SIGN) == 0) ||
899014
 		    ((mechInfo.flags & CKF_VERIFY) == 0)) {
899014
-#if !defined(PK11_MD5_DISABLE) && !defined(PK11_RSA_PKCS_REPLACE)
899014
-			bad = ISC_TRUE;
899014
-#endif
899014
 			PK11_TRACEM(CKM_MD5_RSA_PKCS);
899014
 		}
899014
+#if !defined(PK11_MD5_DISABLE) && !defined(PK11_RSA_PKCS_REPLACE)
899014
+		else
899014
+			++rsa_algorithms;
899014
+#endif
899014
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_SHA1_RSA_PKCS,
899014
 					     &mechInfo);
899014
 		if ((rv != CKR_OK) ||
899014
@@ -687,8 +697,14 @@ scan_slots(void) {
899014
 		if (bad)
899014
 			goto try_dsa;
899014
 		token->operations |= 1 << OP_RSA;
899014
-		if (best_rsa_token == NULL)
899014
+		if (best_rsa_token == NULL) {
899014
+			best_rsa_token = token;
899014
+			best_rsa_algorithms = rsa_algorithms;
899014
+		} else if (rsa_algorithms > best_rsa_algorithms) {
899014
+			pk11_mem_put(best_rsa_token, sizeof(*best_rsa_token));
899014
 			best_rsa_token = token;
899014
+			best_rsa_algorithms = rsa_algorithms;
899014
+		}
899014
 
899014
 	try_dsa:
899014
 		bad = ISC_FALSE;
899014
@@ -756,11 +772,12 @@ scan_slots(void) {
899014
 		bad = ISC_FALSE;
899014
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_MD5, &mechInfo);
899014
 		if ((rv != CKR_OK) || ((mechInfo.flags & CKF_DIGEST) == 0)) {
899014
-#ifndef PK11_MD5_DISABLE
899014
-			bad = ISC_TRUE;
899014
-#endif
899014
 			PK11_TRACEM(CKM_MD5);
899014
 		}
899014
+#ifndef PK11_MD5_DISABLE
899014
+		else
899014
+			++digest_algorithms;
899014
+#endif
899014
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_SHA_1, &mechInfo);
899014
 		if ((rv != CKR_OK) || ((mechInfo.flags & CKF_DIGEST) == 0)) {
899014
 			bad = ISC_TRUE;
899014
@@ -788,11 +805,12 @@ scan_slots(void) {
899014
 		}
899014
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_MD5_HMAC, &mechInfo);
899014
 		if ((rv != CKR_OK) || ((mechInfo.flags & CKF_SIGN) == 0)) {
899014
-#if !defined(PK11_MD5_DISABLE) && !defined(PK11_MD5_HMAC_REPLACE)
899014
-			bad = ISC_TRUE;
899014
-#endif
899014
 			PK11_TRACEM(CKM_MD5_HMAC);
899014
 		}
899014
+#if !defined(PK11_MD5_DISABLE) && !defined(PK11_MD5_HMAC_REPLACE)
899014
+		else
899014
+			++digest_algorithms;
899014
+#endif
899014
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_SHA_1_HMAC, &mechInfo);
899014
 		if ((rv != CKR_OK) || ((mechInfo.flags & CKF_SIGN) == 0)) {
899014
 #ifndef PK11_SHA_1_HMAC_REPLACE
899014
@@ -830,8 +848,14 @@ scan_slots(void) {
899014
 		}
899014
 		if (!bad) {
899014
 			token->operations |= 1 << OP_DIGEST;
899014
-			if (digest_token == NULL)
899014
+			if (digest_token == NULL) {
899014
+				digest_token = token;
899014
+				best_digest_algorithms = digest_algorithms;
899014
+			} else if (digest_algorithms > best_digest_algorithms) {
899014
+				pk11_mem_put(digest_token, sizeof(*digest_token));
899014
 				digest_token = token;
899014
+				best_digest_algorithms = digest_algorithms;
899014
+			}
899014
 		}
899014
 
899014
 		/* ECDSA requires digest */
899014
diff --git a/lib/isc/tests/hash_test.c b/lib/isc/tests/hash_test.c
899014
index 18759903be..6bc45b1ad3 100644
899014
--- a/lib/isc/tests/hash_test.c
899014
+++ b/lib/isc/tests/hash_test.c
899014
@@ -2008,7 +2008,8 @@ ATF_TP_ADD_TCS(tp) {
899014
 	 * various cryptographic hashes.
899014
 	 */
899014
 #ifndef PK11_MD5_DISABLE
899014
-	ATF_TP_ADD_TC(tp, md5_check);
899014
+	if (isc_md5_available())
899014
+		ATF_TP_ADD_TC(tp, md5_check);
899014
 #endif
899014
 	ATF_TP_ADD_TC(tp, sha1_check);
899014
 
899014
@@ -2016,7 +2017,8 @@ ATF_TP_ADD_TCS(tp) {
899014
 	ATF_TP_ADD_TC(tp, isc_hash_function_reverse);
899014
 	ATF_TP_ADD_TC(tp, isc_hash_initializer);
899014
 #ifndef PK11_MD5_DISABLE
899014
-	ATF_TP_ADD_TC(tp, isc_hmacmd5);
899014
+	if (isc_md5_available())
899014
+		ATF_TP_ADD_TC(tp, isc_hmacmd5);
899014
 #endif
899014
 	ATF_TP_ADD_TC(tp, isc_hmacsha1);
899014
 	ATF_TP_ADD_TC(tp, isc_hmacsha224);
899014
@@ -2024,7 +2026,8 @@ ATF_TP_ADD_TCS(tp) {
899014
 	ATF_TP_ADD_TC(tp, isc_hmacsha384);
899014
 	ATF_TP_ADD_TC(tp, isc_hmacsha512);
899014
 #ifndef PK11_MD5_DISABLE
899014
-	ATF_TP_ADD_TC(tp, isc_md5);
899014
+	if (isc_md5_available())
899014
+		ATF_TP_ADD_TC(tp, isc_md5);
899014
 #endif
899014
 	ATF_TP_ADD_TC(tp, isc_sha1);
899014
 	ATF_TP_ADD_TC(tp, isc_sha224);
899014
diff --git a/lib/isccc/cc.c b/lib/isccc/cc.c
899014
index 7225ab4a37..42b30466be 100644
899014
--- a/lib/isccc/cc.c
899014
+++ b/lib/isccc/cc.c
899014
@@ -270,11 +270,15 @@ sign(unsigned char *data, unsigned int length, unsigned char *hmac,
899014
 	switch (algorithm) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 	case ISCCC_ALG_HMACMD5:
899014
-		isc_hmacmd5_init(&ctx.hmd5, secret->rstart,
899014
-				 REGION_SIZE(*secret));
899014
-		isc_hmacmd5_update(&ctx.hmd5, data, length);
899014
-		isc_hmacmd5_sign(&ctx.hmd5, digest);
899014
-		source.rend = digest + ISC_MD5_DIGESTLENGTH;
899014
+		if (isc_md5_available()) {
899014
+			isc_hmacmd5_init(&ctx.hmd5, secret->rstart,
899014
+					 REGION_SIZE(*secret));
899014
+			isc_hmacmd5_update(&ctx.hmd5, data, length);
899014
+			isc_hmacmd5_sign(&ctx.hmd5, digest);
899014
+			source.rend = digest + ISC_MD5_DIGESTLENGTH;
899014
+		} else {
899014
+			return (ISC_R_FAILURE);
899014
+		}
899014
 		break;
899014
 #endif
899014
 
899014
@@ -348,14 +352,18 @@ isccc_cc_towire(isccc_sexpr_t *alist, isc_buffer_t **buffer,
899014
 {
899014
 	unsigned int hmac_base, signed_base;
899014
 	isc_result_t result;
899014
+	const isc_boolean_t md5 = ISC_TF(algorithm == ISCCC_ALG_HMACMD5);
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
+	if (md5 && isc_md5_available() == ISC_FALSE)
899014
+		return (ISC_R_NOTIMPLEMENTED);
899014
+
899014
 	result = isc_buffer_reserve(buffer,
899014
-				    4 + ((algorithm == ISCCC_ALG_HMACMD5) ?
899014
+				    4 + ((md5) ?
899014
 					 sizeof(auth_hmd5) :
899014
 					 sizeof(auth_hsha)));
899014
 #else
899014
-	if (algorithm == ISCCC_ALG_HMACMD5)
899014
+	if (md5)
899014
 		return (ISC_R_NOTIMPLEMENTED);
899014
 	result = isc_buffer_reserve(buffer, 4 + sizeof(auth_hsha));
899014
 #endif
899014
@@ -374,7 +382,7 @@ isccc_cc_towire(isccc_sexpr_t *alist, isc_buffer_t **buffer,
899014
 		 * we know what it is.
899014
 		 */
899014
 #ifndef PK11_MD5_DISABLE
899014
-		if (algorithm == ISCCC_ALG_HMACMD5) {
899014
+		if (md5) {
899014
 			hmac_base = (*buffer)->used + HMD5_OFFSET;
899014
 			isc_buffer_putmem(*buffer,
899014
 					  auth_hmd5, sizeof(auth_hmd5));
899014
@@ -440,7 +448,7 @@ verify(isccc_sexpr_t *alist, unsigned char *data, unsigned int length,
899014
 	if (!isccc_alist_alistp(_auth))
899014
 		return (ISC_R_FAILURE);
899014
 #ifndef PK11_MD5_DISABLE
899014
-	if (algorithm == ISCCC_ALG_HMACMD5)
899014
+	if (algorithm == ISCCC_ALG_HMACMD5 && isc_md5_available())
899014
 		hmac = isccc_alist_lookup(_auth, "hmd5");
899014
 	else
899014
 #endif
899014
@@ -455,12 +463,16 @@ verify(isccc_sexpr_t *alist, unsigned char *data, unsigned int length,
899014
 	switch (algorithm) {
899014
 #ifndef PK11_MD5_DISABLE
899014
 	case ISCCC_ALG_HMACMD5:
899014
-		isc_hmacmd5_init(&ctx.hmd5, secret->rstart,
899014
-				 REGION_SIZE(*secret));
899014
-		isc_hmacmd5_update(&ctx.hmd5, data, length);
899014
-		isc_hmacmd5_sign(&ctx.hmd5, digest);
899014
-		source.rend = digest + ISC_MD5_DIGESTLENGTH;
899014
-		break;
899014
+		if (isc_md5_available()) {
899014
+			isc_hmacmd5_init(&ctx.hmd5, secret->rstart,
899014
+					 REGION_SIZE(*secret));
899014
+			isc_hmacmd5_update(&ctx.hmd5, data, length);
899014
+			isc_hmacmd5_sign(&ctx.hmd5, digest);
899014
+			source.rend = digest + ISC_MD5_DIGESTLENGTH;
899014
+			break;
899014
+		} else {
899014
+			return (ISC_R_FAILURE);
899014
+		}
899014
 #endif
899014
 
899014
 	case ISCCC_ALG_HMACSHA1:
899014
-- 
899014
2.14.4
899014