01dd58
From eff6dcb62f3cea6df0a848c2220a49bc02cb4a0e 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
ad7b3b
Subject: [PATCH] FIPS code changes
899014
MIME-Version: 1.0
899014
Content-Type: text/plain; charset=UTF-8
899014
Content-Transfer-Encoding: 8bit
899014
ad7b3b
Squashed commit of the following:
ad7b3b
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
---
1e4169
 bin/confgen/keygen.c              | 10 +++-
1e4169
 bin/confgen/rndc-confgen.c        | 32 ++++---------
1e4169
 bin/dig/dig.c                     |  7 +--
1e4169
 bin/dig/dighost.c                 | 14 ++++--
1e4169
 bin/dnssec/dnssec-keygen.c        | 14 ++++++
1e4169
 bin/named/config.c                | 25 +++++++++-
1e4169
 bin/nsupdate/nsupdate.c           | 24 ++++++----
899014
 bin/rndc/rndc.c                   |  3 +-
1e4169
 bin/tests/optional/hash_test.c    | 78 ++++++++++++++++---------------
899014
 bin/tests/system/tkey/keycreate.c |  3 ++
1e4169
 bin/tests/system/tkey/keydelete.c | 17 ++++---
1e4169
 lib/bind9/check.c                 | 10 ++++
1e4169
 lib/dns/dst_api.c                 | 23 ++++++---
899014
 lib/dns/dst_internal.h            |  3 +-
1e4169
 lib/dns/dst_parse.c               | 18 +++++--
1e4169
 lib/dns/hmac_link.c               | 18 ++-----
899014
 lib/dns/opensslrsa_link.c         |  6 +++
1e4169
 lib/dns/pkcs11rsa_link.c          | 33 +++++++++++--
1e4169
 lib/dns/rcode.c                   | 21 ++++++++-
1e4169
 lib/dns/tests/rsa_test.c          |  4 ++
899014
 lib/dns/tests/tsig_test.c         |  1 +
1e4169
 lib/dns/tkey.c                    |  9 ++++
899014
 lib/dns/tsec.c                    |  8 +++-
1e4169
 lib/dns/tsig.c                    | 17 ++++---
899014
 lib/isc/include/isc/md5.h         |  3 ++
1e4169
 lib/isc/md5.c                     | 59 +++++++++++++++++++++++
1e4169
 lib/isc/pk11.c                    | 44 +++++++++++------
1e4169
 lib/isc/tests/hash_test.c         |  9 ++++
1e4169
 lib/isccc/cc.c                    | 42 +++++++++++------
1e4169
 29 files changed, 400 insertions(+), 155 deletions(-)
899014
899014
diff --git a/bin/confgen/keygen.c b/bin/confgen/keygen.c
ad7b3b
index 8931ad5..5015abb 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:
ad7b3b
+		if (!isc_md5_available()) {
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
ad7b3b
index 5ca3d76..6b7790a 100644
899014
--- a/bin/confgen/rndc-confgen.c
899014
+++ b/bin/confgen/rndc-confgen.c
ad7b3b
@@ -36,6 +36,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>
ad7b3b
@@ -63,7 +64,7 @@ const char *progname;
899014
 
ad7b3b
 bool verbose = 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;
ad7b3b
@@ -71,13 +72,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\
ad7b3b
@@ -86,24 +86,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
 }
ad7b3b
@@ -139,11 +122,12 @@ 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
ad7b3b
 	alg = DST_ALG_HMACSHA256;
899014
+#ifndef PK11_MD5_DISABLE
899014
+	if (isc_md5_available())
899014
+		alg = DST_ALG_HMACMD5;
ad7b3b
 #endif
899014
+	algdef = alg_totext(alg);
ad7b3b
 	serveraddr = DEFAULT_SERVER;
ad7b3b
 	port = DEFAULT_PORT;
899014
 
899014
diff --git a/bin/dig/dig.c b/bin/dig/dig.c
01dd58
index 706299e..aaf22e7 100644
899014
--- a/bin/dig/dig.c
899014
+++ b/bin/dig/dig.c
ad7b3b
@@ -20,6 +20,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>
01dd58
@@ -1774,10 +1775,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
01dd58
index 93e5b40..afd2700 100644
899014
--- a/bin/dig/dighost.c
899014
+++ b/bin/dig/dighost.c
ad7b3b
@@ -80,6 +80,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>
ad7b3b
@@ -1246,9 +1247,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
ad7b3b
@@ -1368,7 +1370,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
ad7b3b
index 1476d0d..f5c9316 100644
899014
--- a/bin/dnssec/dnssec-keygen.c
899014
+++ b/bin/dnssec/dnssec-keygen.c
ad7b3b
@@ -36,6 +36,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>
ad7b3b
@@ -562,6 +563,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 &&
ad7b3b
+			   !isc_md5_available()) {
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
01dd58
index 32c454a..dff826b 100644
899014
--- a/bin/named/config.c
899014
+++ b/bin/named/config.c
ad7b3b
@@ -18,6 +18,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>
01dd58
@@ -974,6 +975,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
ad7b3b
+	if (!isc_md5_available()) {
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,
ad7b3b
 			   unsigned int *typep, uint16_t *digestbits)
01dd58
@@ -983,7 +999,7 @@ ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
ad7b3b
 	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' ||
01dd58
@@ -1006,7 +1022,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
2a4663
index de60313..bbb3936 100644
899014
--- a/bin/nsupdate/nsupdate.c
899014
+++ b/bin/nsupdate/nsupdate.c
ad7b3b
@@ -31,6 +31,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>
2a4663
 #include <isc/portset.h>
2a4663
@@ -477,9 +478,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) {
2a4663
@@ -592,10 +594,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;
2a4663
@@ -732,7 +734,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:
2a4663
@@ -1637,12 +1640,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
ad7b3b
index 9eb0ce0..8083654 100644
899014
--- a/bin/rndc/rndc.c
899014
+++ b/bin/rndc/rndc.c
ad7b3b
@@ -23,6 +23,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>
ad7b3b
@@ -636,7 +637,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
ad7b3b
index bf2891a..b5f0a1c 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
ad7b3b
index 5a00f86..653c951 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);
ad7b3b
+	if (!isc_md5_available())
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
ad7b3b
index bde66a4..70a40c3 100644
899014
--- a/bin/tests/system/tkey/keydelete.c
899014
+++ b/bin/tests/system/tkey/keydelete.c
ad7b3b
@@ -225,12 +225,17 @@ 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,
ad7b3b
-					   dstkey, 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,
ad7b3b
+						   dstkey, true, 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
01dd58
index ec0ab6d..e0803d4 100644
899014
--- a/lib/bind9/check.c
899014
+++ b/lib/bind9/check.c
ad7b3b
@@ -23,6 +23,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>
01dd58
@@ -2618,6 +2619,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 */
ad7b3b
+	if (!isc_md5_available() &&
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
1e4169
index e3c47a9..320c0f8 100644
899014
--- a/lib/dns/dst_api.c
899014
+++ b/lib/dns/dst_api.c
ad7b3b
@@ -192,6 +192,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
ad7b3b
@@ -201,7 +207,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));
ad7b3b
@@ -235,14 +240,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
ad7b3b
index 6ee796c..3e55d44 100644
899014
--- a/lib/dns/dst_internal.h
899014
+++ b/lib/dns/dst_internal.h
ad7b3b
@@ -250,7 +250,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
ad7b3b
index f31c33d..87023a6 100644
899014
--- a/lib/dns/dst_parse.c
899014
+++ b/lib/dns/dst_parse.c
ad7b3b
@@ -33,6 +33,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>
ad7b3b
@@ -396,6 +397,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:
ad7b3b
@@ -421,7 +426,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));
ad7b3b
@@ -640,11 +648,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,
ad7b3b
-			   true, external);
899014
+	if (alg == DST_ALG_RSA)
899014
+		alg = DST_ALG_RSASHA1;
899014
 #else
ad7b3b
-	check = check_data(priv, alg, true, external);
ad7b3b
+	if (!isc_md5_available() && alg == DST_ALG_RSA)
899014
+		alg = DST_ALG_RSASHA1;
899014
 #endif
ad7b3b
+	check = check_data(priv, alg, 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
1e4169
index 3b6579b..4bdce2f 100644
899014
--- a/lib/dns/hmac_link.c
899014
+++ b/lib/dns/hmac_link.c
ad7b3b
@@ -340,20 +340,10 @@ static dst_func_t hmacmd5_functions = {
899014
 
899014
 isc_result_t
899014
 dst__hmacmd5_init(dst_func_t **funcp) {
899014
-#ifdef HAVE_FIPS_MODE
ad7b3b
-	/*
899014
-	 * Problems from OpenSSL are likely from FIPS mode
ad7b3b
-	 */
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);
ad7b3b
-	}
ad7b3b
-#endif
899014
+
ad7b3b
+	/* Intentionally skip initialization */
ad7b3b
+	if (!isc_md5_available())
899014
+		return (ISC_R_SUCCESS);
899014
 
1e4169
 #if PK11_FLAVOR != PK11_UTIMACO_FLAVOR
ad7b3b
 	/*
899014
diff --git a/lib/dns/opensslrsa_link.c b/lib/dns/opensslrsa_link.c
1e4169
index ec35f50..c80fabe 100644
899014
--- a/lib/dns/opensslrsa_link.c
899014
+++ b/lib/dns/opensslrsa_link.c
1e4169
@@ -1812,6 +1812,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
1e4169
index 096c1a8..6c280bf 100644
899014
--- a/lib/dns/pkcs11rsa_link.c
899014
+++ b/lib/dns/pkcs11rsa_link.c
ad7b3b
@@ -96,10 +96,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
ad7b3b
+		if (!isc_md5_available())
899014
+			return (ISC_R_FAILURE);
899014
+#endif
899014
+		/* FALLTHROUGH */
899014
 	case DST_ALG_RSASHA1:
899014
 	case DST_ALG_NSEC3RSASHA1:
899014
 		/* From RFC 3110 */
1e4169
@@ -641,6 +646,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:
ad7b3b
+		if (!isc_md5_available())
899014
+			return (ISC_R_FAILURE);
899014
+	
899014
 		mech.mechanism = CKM_MD5;
899014
 		break;
899014
 #endif
1e4169
@@ -799,6 +807,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:
ad7b3b
+		if (!isc_md5_available())
899014
+			return (ISC_R_FAILURE);
899014
+
899014
 		der = md5_der;
899014
 		derlen = sizeof(md5_der);
899014
 		hashlen = ISC_MD5_DIGESTLENGTH;
1e4169
@@ -1024,6 +1035,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:
ad7b3b
+		if (!isc_md5_available())
899014
+			return (ISC_R_FAILURE);
899014
+
899014
 		der = md5_der;
899014
 		derlen = sizeof(md5_der);
899014
 		hashlen = ISC_MD5_DIGESTLENGTH;
1e4169
@@ -2231,11 +2245,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
1e4169
index 9c42c50..f51d548 100644
899014
--- a/lib/dns/rcode.c
899014
+++ b/lib/dns/rcode.c
ad7b3b
@@ -16,6 +16,7 @@
ad7b3b
 #include <stdbool.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>
1e4169
@@ -357,17 +358,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
ad7b3b
+	if (!isc_md5_available()) {
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
01dd58
index f9ac6d0..241e17e 100644
899014
--- a/lib/dns/tests/rsa_test.c
899014
+++ b/lib/dns/tests/rsa_test.c
01dd58
@@ -27,6 +27,7 @@
1e4169
 #define UNIT_TESTING
1e4169
 #include <cmocka.h>
899014
 
899014
+#include <isc/md5.h>
899014
 #include <isc/util.h>
899014
 #include <isc/print.h>
899014
 
01dd58
@@ -248,6 +249,8 @@ isc_rsa_verify_test(void **state) {
899014
 	/* RSAMD5 */
899014
 
899014
 #ifndef PK11_MD5_DISABLE
899014
+	if (isc_md5_available()) {
1e4169
+		/* wrong indentation is kept for diff minimization */
1e4169
 	key->key_alg = DST_ALG_RSAMD5;
1e4169
 
1e4169
 	ret = dst_context_create3(key, mctx, DNS_LOGCATEGORY_DNSSEC,
01dd58
@@ -265,6 +268,7 @@ isc_rsa_verify_test(void **state) {
1e4169
 	assert_int_equal(ret, ISC_R_SUCCESS);
1e4169
 
1e4169
 	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
01dd58
index 11d011a..feb2068 100644
899014
--- a/lib/dns/tests/tsig_test.c
899014
+++ b/lib/dns/tests/tsig_test.c
01dd58
@@ -25,6 +25,7 @@
1e4169
 #define UNIT_TESTING
1e4169
 #include <cmocka.h>
899014
 
899014
+#include <isc/md5.h>
899014
 #include <isc/mem.h>
899014
 #include <isc/print.h>
1e4169
 #include <isc/util.h>
899014
diff --git a/lib/dns/tkey.c b/lib/dns/tkey.c
1e4169
index 89cfc79..d07364a 100644
899014
--- a/lib/dns/tkey.c
899014
+++ b/lib/dns/tkey.c
ad7b3b
@@ -245,6 +245,9 @@ compute_secret(isc_buffer_t *shared, isc_region_t *queryrandomness,
899014
 	unsigned char digests[32];
899014
 	unsigned int i;
899014
 
ad7b3b
+	if (!isc_md5_available())
899014
+		return (ISC_R_NOTIMPLEMENTED);
899014
+
899014
 	isc_buffer_usedregion(shared, &r);
899014
 
899014
 	/*
ad7b3b
@@ -321,6 +324,12 @@ process_dhtkey(dns_message_t *msg, dns_name_t *signer, dns_name_t *name,
899014
 	}
899014
 
899014
 #ifndef PK11_MD5_DISABLE
ad7b3b
+	if (!isc_md5_available()) {
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
1e4169
index 9d8ead4..0c82f65 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
1e4169
index 58c1104..00ee1e1 100644
899014
--- a/lib/dns/tsig.c
899014
+++ b/lib/dns/tsig.c
ad7b3b
@@ -273,7 +273,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;
ad7b3b
@@ -499,7 +500,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
ad7b3b
@@ -683,7 +685,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
 
1e4169
@@ -1291,7 +1294,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 ||
1e4169
@@ -1460,7 +1463,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 ||
1e4169
@@ -1601,7 +1604,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 ||
1e4169
@@ -1780,7 +1783,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
ad7b3b
index 4d29398..e3f5cec 100644
899014
--- a/lib/isc/include/isc/md5.h
899014
+++ b/lib/isc/include/isc/md5.h
ad7b3b
@@ -91,6 +91,9 @@ isc_md5_final(isc_md5_t *ctx, unsigned char *digest);
ad7b3b
 bool
ad7b3b
 isc_md5_check(bool testing);
899014
 
ad7b3b
+bool
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
2a4663
index 249f3da..628a414 100644
899014
--- a/lib/isc/md5.c
899014
+++ b/lib/isc/md5.c
ad7b3b
@@ -37,6 +37,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>
2a4663
@@ -54,6 +55,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;
ad7b3b
+static bool available = false;
899014
+
899014
 void
899014
 isc_md5_init(isc_md5_t *ctx) {
899014
 	ctx->ctx = EVP_MD_CTX_new();
2a4663
@@ -85,8 +89,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);
ad7b3b
+	available = (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
+
ad7b3b
+bool
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;
ad7b3b
+static bool available = false;
899014
+
899014
 void
899014
 isc_md5_init(isc_md5_t *ctx) {
899014
 	CK_RV rv;
2a4663
@@ -129,6 +158,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
+
ad7b3b
+	if (pk11_get_session(ctx, OP_DIGEST, true, false,
ad7b3b
+				       false, NULL, 0) == ISC_R_SUCCESS)
899014
+	{
899014
+		rv = pkcs_C_DigestInit(ctx->session, &mech);
899014
+		isc_md5_invalidate(ctx);
ad7b3b
+		available = (rv == CKR_OK);
899014
+	} else {
ad7b3b
+		available = false;
899014
+	}
899014
+}
899014
+
ad7b3b
+bool
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
2a4663
@@ -338,6 +392,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
+
ad7b3b
+bool
899014
+isc_md5_available() {
ad7b3b
+	return true;
899014
+}
899014
 #endif
899014
 
899014
 /*
899014
diff --git a/lib/isc/pk11.c b/lib/isc/pk11.c
01dd58
index 0d5b009..7809e7b 100644
899014
--- a/lib/isc/pk11.c
899014
+++ b/lib/isc/pk11.c
ad7b3b
@@ -197,8 +197,6 @@ pk11_initialize(isc_mem_t *mctx, const char *engine) {
ad7b3b
 	UNLOCK(&alloclock);
899014
 	if (initialized) {
ad7b3b
 		goto unlock;
899014
-	} else {
ad7b3b
-		initialized = true;
899014
 	}
899014
 
899014
 	ISC_LIST_INIT(tokens);
01dd58
@@ -238,6 +236,7 @@ pk11_initialize(isc_mem_t *mctx, const char *engine) {
899014
 	}
899014
 #endif
899014
 #endif /* PKCS11CRYPTO */
01dd58
+	initialized = true;
899014
  unlock:
01dd58
 	UNLOCK(&sessionlock);
01dd58
 	return (result);
ad7b3b
@@ -589,6 +588,8 @@ scan_slots(void) {
899014
 	pk11_token_t *token;
899014
 	unsigned int i;
ad7b3b
 	bool 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));
ad7b3b
@@ -601,6 +602,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
 
ad7b3b
@@ -640,11 +643,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)
ad7b3b
-			bad = 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) ||
ad7b3b
@@ -687,8 +691,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) {
ad7b3b
 			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));
ad7b3b
+			best_rsa_token = token;
899014
+			best_rsa_algorithms = rsa_algorithms;
899014
+		}
899014
 
899014
 	try_dsa:
ad7b3b
 		bad = false;
ad7b3b
@@ -756,11 +766,12 @@ scan_slots(void) {
ad7b3b
 		bad = 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
ad7b3b
-			bad = 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)) {
ad7b3b
 			bad = true;
ad7b3b
@@ -788,11 +799,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)
ad7b3b
-			bad = 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
ad7b3b
@@ -830,8 +842,14 @@ scan_slots(void) {
899014
 		}
899014
 		if (!bad) {
899014
 			token->operations |= 1 << OP_DIGEST;
899014
-			if (digest_token == NULL)
899014
+			if (digest_token == NULL) {
ad7b3b
 				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));
ad7b3b
+				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
01dd58
index 31ced94..421131e 100644
899014
--- a/lib/isc/tests/hash_test.c
899014
+++ b/lib/isc/tests/hash_test.c
01dd58
@@ -775,6 +775,9 @@ isc_md5_test(void **state) {
1e4169
 
1e4169
 	UNUSED(state);
1e4169
 
1e4169
+	if (!isc_md5_available())
1e4169
+		return;
1e4169
+
1e4169
 	/*
1e4169
 	 * These are the various test vectors.  All of these are passed
1e4169
 	 * through the hash function and the results are compared to the
01dd58
@@ -1630,6 +1633,9 @@ isc_hmacmd5_test(void **state) {
1e4169
 
1e4169
 	UNUSED(state);
1e4169
 
1e4169
+	if (!isc_md5_available())
1e4169
+		return;
1e4169
+
1e4169
 	/*
1e4169
 	 * These are the various test vectors.  All of these are passed
1e4169
 	 * through the hash function and the results are compared to the
01dd58
@@ -1940,6 +1946,9 @@ static void
1e4169
 md5_check_test(void **state) {
1e4169
 	UNUSED(state);
1e4169
 
1e4169
+	if (!isc_md5_available())
1e4169
+		return;
1e4169
+
1e4169
 	assert_true(isc_md5_check(false));
1e4169
 	assert_false(isc_md5_check(true));
899014
 
899014
diff --git a/lib/isccc/cc.c b/lib/isccc/cc.c
ad7b3b
index c2740cb..c314d76 100644
899014
--- a/lib/isccc/cc.c
899014
+++ b/lib/isccc/cc.c
ad7b3b
@@ -272,11 +272,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
 
ad7b3b
@@ -350,14 +354,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;
ad7b3b
+	const bool md5 = (algorithm == ISCCC_ALG_HMACMD5);
899014
 
899014
 #ifndef PK11_MD5_DISABLE
ad7b3b
+	if (md5 && !isc_md5_available())
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
ad7b3b
@@ -376,7 +384,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));
ad7b3b
@@ -442,7 +450,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
ad7b3b
@@ -457,12 +465,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
-- 
1e4169
2.20.1
899014