27025e
From eff6dcb62f3cea6df0a848c2220a49bc02cb4a0e Mon Sep 17 00:00:00 2001
e55890
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
e55890
Date: Thu, 2 Aug 2018 23:34:45 +0200
27025e
Subject: [PATCH] FIPS code changes
e55890
MIME-Version: 1.0
e55890
Content-Type: text/plain; charset=UTF-8
e55890
Content-Transfer-Encoding: 8bit
e55890
27025e
Squashed commit of the following:
27025e
e55890
commit b49f70ce0575b6b52a71b90fe0376dbf16f92c6b
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Mon Jan 22 14:12:37 2018 +0100
e55890
e55890
    Update system tests to detect MD5 disabled at runtime
e55890
e55890
commit 80ceffee4860c24baf70bc9a8653d92731eda2e4
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Thu Aug 2 14:53:54 2018 +0200
e55890
e55890
    Avoid warning about undefined parameters
e55890
e55890
commit e4ad4363e3d1acaac58456117579f02761f38fdc
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Wed Jun 20 19:31:19 2018 +0200
e55890
e55890
    Fix rndc-confgen default algorithm, report true algorithm in usage.
e55890
e55890
commit 7e629a351010cb75e0589ec361f720085675998c
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Fri Feb 23 21:21:30 2018 +0100
e55890
e55890
    Cleanup only if initialization was successful
e55890
e55890
commit 2101b948c77cbcbe07eb4a1e60f3e693b2245ec6
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Mon Feb 5 12:19:28 2018 +0100
e55890
e55890
    Ensure dst backend is initialized first even before hmac algorithms.
e55890
e55890
commit 7567c7edde7519115a9ae7e20818c835d3eb1ffe
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Mon Feb 5 12:17:54 2018 +0100
e55890
e55890
    Skip initialization of MD5 based algorithms if not available.
e55890
e55890
commit 5782137df6b45a6d900d5a1c250c1257227e917a
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Mon Feb 5 10:21:27 2018 +0100
e55890
e55890
    Change secalgs skipping to be more safe
e55890
e55890
commit f2d78729898182d2d19d5064de1bec9b66817159
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Wed Jan 31 18:26:11 2018 +0100
e55890
e55890
    Skip MD5 algorithm also in case of NULL name
e55890
e55890
commit 32a2ad4abc7aaca1c257730319ad3c27405d3407
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Wed Jan 31 11:38:12 2018 +0100
e55890
e55890
    Make MD5 behave like unknown algorithm in TSIG.
e55890
e55890
commit 13cd3f704dce568fdf24a567be5802b58ac6007b
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Tue Nov 28 20:14:37 2017 +0100
e55890
e55890
    Select token with most supported functions, instead of demanding it must support all functions
e55890
e55890
    Initialize PKCS#11 always until successfully initialized
e55890
e55890
commit a71df74abdca4fe63bcdf542b81a109cf1f495b4
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Mon Jan 22 16:17:44 2018 +0100
e55890
e55890
    Handle MD5 unavailability from DST
e55890
e55890
commit dd82cb263efa2753d3ee772972726ea08bcc639b
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Mon Jan 22 14:11:16 2018 +0100
e55890
e55890
    Check runtime flag from library and applications, fail gracefully.
e55890
e55890
commit c7b2f87f07ecae75b821a908e29f08a42371e32e
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Mon Jan 22 08:39:08 2018 +0100
e55890
e55890
    Modify libraries to use isc_md5_available() if PK11_MD5_DISABLE is not
e55890
    defined.
e55890
    TODO: pk11.c should accept slot without MD5 support.
e55890
e55890
commit 0b8e470ec636b9e350b5ec3203eb2b4091415fde
e55890
Author: Petr Menšík <pemensik@redhat.com>
e55890
Date:   Mon Jan 22 07:21:04 2018 +0100
e55890
e55890
    Add runtime detection whether MD5 is useable.
e55890
---
27025e
 bin/confgen/keygen.c              | 10 +++-
27025e
 bin/confgen/rndc-confgen.c        | 32 ++++---------
27025e
 bin/dig/dig.c                     |  7 +--
27025e
 bin/dig/dighost.c                 | 14 ++++--
27025e
 bin/dnssec/dnssec-keygen.c        | 14 ++++++
27025e
 bin/named/config.c                | 25 +++++++++-
27025e
 bin/nsupdate/nsupdate.c           | 24 ++++++----
e55890
 bin/rndc/rndc.c                   |  3 +-
27025e
 bin/tests/optional/hash_test.c    | 78 ++++++++++++++++---------------
e55890
 bin/tests/system/tkey/keycreate.c |  3 ++
27025e
 bin/tests/system/tkey/keydelete.c | 17 ++++---
27025e
 lib/bind9/check.c                 | 10 ++++
27025e
 lib/dns/dst_api.c                 | 23 ++++++---
e55890
 lib/dns/dst_internal.h            |  3 +-
27025e
 lib/dns/dst_parse.c               | 18 +++++--
27025e
 lib/dns/hmac_link.c               | 18 ++-----
e55890
 lib/dns/opensslrsa_link.c         |  6 +++
27025e
 lib/dns/pkcs11rsa_link.c          | 33 +++++++++++--
27025e
 lib/dns/rcode.c                   | 21 ++++++++-
27025e
 lib/dns/tests/rsa_test.c          |  4 ++
e55890
 lib/dns/tests/tsig_test.c         |  1 +
27025e
 lib/dns/tkey.c                    |  9 ++++
e55890
 lib/dns/tsec.c                    |  8 +++-
27025e
 lib/dns/tsig.c                    | 17 ++++---
e55890
 lib/isc/include/isc/md5.h         |  3 ++
27025e
 lib/isc/md5.c                     | 59 +++++++++++++++++++++++
27025e
 lib/isc/pk11.c                    | 44 +++++++++++------
27025e
 lib/isc/tests/hash_test.c         |  9 ++++
27025e
 lib/isccc/cc.c                    | 42 +++++++++++------
27025e
 29 files changed, 400 insertions(+), 155 deletions(-)
e55890
e55890
diff --git a/bin/confgen/keygen.c b/bin/confgen/keygen.c
27025e
index 8931ad5..5015abb 100644
e55890
--- a/bin/confgen/keygen.c
e55890
+++ b/bin/confgen/keygen.c
e55890
@@ -22,6 +22,7 @@
e55890
 #include <isc/entropy.h>
e55890
 #include <isc/file.h>
e55890
 #include <isc/keyboard.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/print.h>
e55890
 #include <isc/result.h>
e55890
@@ -73,7 +74,7 @@ alg_fromtext(const char *name) {
e55890
 		p = &name[5];
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	if (strcasecmp(p, "md5") == 0)
e55890
+	if (strcasecmp(p, "md5") == 0 && isc_md5_available())
e55890
 		return DST_ALG_HMACMD5;
e55890
 #endif
e55890
 	if (strcasecmp(p, "sha1") == 0)
e55890
@@ -132,6 +133,13 @@ generate_key(isc_mem_t *mctx, const char *randomfile, dns_secalg_t alg,
e55890
 	switch (alg) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	    case DST_ALG_HMACMD5:
27025e
+		if (!isc_md5_available()) {
e55890
+			fatal("unsupported algorithm %d\n", alg);
e55890
+		} else if (keysize < 1 || keysize > 512) {
e55890
+			fatal("keysize %d out of range (must be 1-512)\n",
e55890
+			      keysize);
e55890
+		}
e55890
+		break;
e55890
 #endif
e55890
 	    case DST_ALG_HMACSHA1:
e55890
 	    case DST_ALG_HMACSHA224:
e55890
diff --git a/bin/confgen/rndc-confgen.c b/bin/confgen/rndc-confgen.c
27025e
index 5ca3d76..6b7790a 100644
e55890
--- a/bin/confgen/rndc-confgen.c
e55890
+++ b/bin/confgen/rndc-confgen.c
27025e
@@ -36,6 +36,7 @@
e55890
 #include <isc/file.h>
e55890
 #include <isc/keyboard.h>
e55890
 #include <isc/mem.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/net.h>
e55890
 #include <isc/print.h>
e55890
 #include <isc/result.h>
27025e
@@ -63,7 +64,7 @@ const char *progname;
e55890
 
27025e
 bool verbose = false;
e55890
 
e55890
-const char *keyfile, *keydef;
e55890
+const char *keyfile, *keydef, *algdef;
e55890
 
e55890
 ISC_PLATFORM_NORETURN_PRE static void
e55890
 usage(int status) ISC_PLATFORM_NORETURN_POST;
27025e
@@ -71,13 +72,12 @@ usage(int status) ISC_PLATFORM_NORETURN_POST;
e55890
 static void
e55890
 usage(int status) {
e55890
 
e55890
-#ifndef PK11_MD5_DISABLE
e55890
 	fprintf(stderr, "\
e55890
 Usage:\n\
e55890
  %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
e55890
 [-s addr] [-t chrootdir] [-u user]\n\
e55890
   -a:		 generate just the key clause and write it to keyfile (%s)\n\
e55890
-  -A alg:	 algorithm (default hmac-md5)\n\
e55890
+  -A alg:	 algorithm (default %s)\n\
e55890
   -b bits:	 from 1 through 512, default 256; total length of the secret\n\
e55890
   -c keyfile:	 specify an alternate key file (requires -a)\n\
e55890
   -k keyname:	 the name as it will be used  in named.conf and rndc.conf\n\
27025e
@@ -86,24 +86,7 @@ Usage:\n\
e55890
   -s addr:	 the address to which rndc should connect\n\
e55890
   -t chrootdir:	 write a keyfile in chrootdir as well (requires -a)\n\
e55890
   -u user:	 set the keyfile owner to \"user\" (requires -a)\n",
e55890
-		 progname, keydef);
e55890
-#else
e55890
-	fprintf(stderr, "\
e55890
-Usage:\n\
e55890
- %s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
e55890
-[-s addr] [-t chrootdir] [-u user]\n\
e55890
-  -a:		 generate just the key clause and write it to keyfile (%s)\n\
e55890
-  -A alg:	 algorithm (default hmac-sha256)\n\
e55890
-  -b bits:	 from 1 through 512, default 256; total length of the secret\n\
e55890
-  -c keyfile:	 specify an alternate key file (requires -a)\n\
e55890
-  -k keyname:	 the name as it will be used  in named.conf and rndc.conf\n\
e55890
-  -p port:	 the port named will listen on and rndc will connect to\n\
e55890
-  -r randomfile: source of random data (use \"keyboard\" for key timing)\n\
e55890
-  -s addr:	 the address to which rndc should connect\n\
e55890
-  -t chrootdir:	 write a keyfile in chrootdir as well (requires -a)\n\
e55890
-  -u user:	 set the keyfile owner to \"user\" (requires -a)\n",
e55890
-		 progname, keydef);
e55890
-#endif
e55890
+		 progname, keydef, algdef);
e55890
 
e55890
 	exit (status);
e55890
 }
27025e
@@ -139,11 +122,12 @@ main(int argc, char **argv) {
e55890
 	progname = program;
e55890
 
e55890
 	keyname = DEFAULT_KEYNAME;
e55890
-#ifndef PK11_MD5_DISABLE
e55890
-	alg = DST_ALG_HMACMD5;
e55890
-#else
27025e
 	alg = DST_ALG_HMACSHA256;
e55890
+#ifndef PK11_MD5_DISABLE
e55890
+	if (isc_md5_available())
e55890
+		alg = DST_ALG_HMACMD5;
27025e
 #endif
e55890
+	algdef = alg_totext(alg);
27025e
 	serveraddr = DEFAULT_SERVER;
27025e
 	port = DEFAULT_PORT;
e55890
 
e55890
diff --git a/bin/dig/dig.c b/bin/dig/dig.c
27025e
index 706299e..aaf22e7 100644
e55890
--- a/bin/dig/dig.c
e55890
+++ b/bin/dig/dig.c
27025e
@@ -20,6 +20,7 @@
e55890
 #include <ctype.h>
e55890
 
e55890
 #include <isc/app.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/netaddr.h>
e55890
 #include <isc/parseint.h>
e55890
 #include <isc/platform.h>
27025e
@@ -1774,10 +1775,10 @@ dash_option(char *option, char *next, dig_lookup_t **lookup,
e55890
 			ptr = ptr2;
e55890
 			ptr2 = ptr3;
e55890
 		} else  {
e55890
-#ifndef PK11_MD5_DISABLE
e55890
-			hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
-#else
e55890
 			hmacname = DNS_TSIG_HMACSHA256_NAME;
e55890
+#ifndef PK11_MD5_DISABLE
e55890
+			if (isc_md5_available())
e55890
+				hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
 #endif
e55890
 			digestbits = 0;
e55890
 		}
e55890
diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c
27025e
index 93e5b40..afd2700 100644
e55890
--- a/bin/dig/dighost.c
e55890
+++ b/bin/dig/dighost.c
27025e
@@ -80,6 +80,7 @@
e55890
 #include <isc/hex.h>
e55890
 #include <isc/lang.h>
e55890
 #include <isc/log.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/netaddr.h>
e55890
 #include <isc/netdb.h>
e55890
 #include <isc/parseint.h>
27025e
@@ -1246,9 +1247,10 @@ parse_hmac(const char *hmac) {
e55890
 	digestbits = 0;
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	if (strcasecmp(buf, "hmac-md5") == 0) {
e55890
+	if (strcasecmp(buf, "hmac-md5") == 0 && isc_md5_available()) {
e55890
 		hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
-	} else if (strncasecmp(buf, "hmac-md5-", 9) == 0) {
e55890
+	} else if (strncasecmp(buf, "hmac-md5-", 9) == 0 &&
e55890
+		   isc_md5_available()) {
e55890
 		hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
 		digestbits = parse_bits(&buf[9], "digest-bits [0..128]", 128);
e55890
 	} else
27025e
@@ -1368,7 +1370,13 @@ setup_file_key(void) {
e55890
 	switch (dst_key_alg(dstkey)) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	case DST_ALG_HMACMD5:
e55890
-		hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
+		if (isc_md5_available()) {
e55890
+			hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
+		} else {
e55890
+			printf(";; Couldn't create key %s: bad algorithm\n",
e55890
+			       keynametext);
e55890
+			goto failure;
e55890
+		}
e55890
 		break;
e55890
 #endif
e55890
 	case DST_ALG_HMACSHA1:
e55890
diff --git a/bin/dnssec/dnssec-keygen.c b/bin/dnssec/dnssec-keygen.c
27025e
index 1476d0d..f5c9316 100644
e55890
--- a/bin/dnssec/dnssec-keygen.c
e55890
+++ b/bin/dnssec/dnssec-keygen.c
27025e
@@ -36,6 +36,7 @@
e55890
 #include <isc/buffer.h>
e55890
 #include <isc/commandline.h>
e55890
 #include <isc/entropy.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/print.h>
e55890
 #include <isc/region.h>
27025e
@@ -562,6 +563,19 @@ main(int argc, char **argv) {
e55890
 					"\"-a RSAMD5\"\n");
e55890
 			INSIST(freeit == NULL);
e55890
 			return (1);
e55890
+		} else if (strcasecmp(algname, "HMAC-MD5") == 0) {
e55890
+			if (isc_md5_available()) {
e55890
+				alg = DST_ALG_HMACMD5;
e55890
+			} else {
e55890
+				fprintf(stderr,
e55890
+					"The use of HMAC-MD5 was disabled\n");
e55890
+				return (1);
e55890
+			}
e55890
+		} else if (strcasecmp(algname, "RSAMD5") == 0 &&
27025e
+			   !isc_md5_available()) {
e55890
+			fprintf(stderr, "The use of RSAMD5 was disabled\n");
e55890
+			INSIST(freeit == NULL);
e55890
+			return (1);
e55890
 		} else if (strcasecmp(algname, "HMAC-MD5") == 0) {
e55890
 			alg = DST_ALG_HMACMD5;
e55890
 #else
e55890
diff --git a/bin/named/config.c b/bin/named/config.c
27025e
index 32c454a..dff826b 100644
e55890
--- a/bin/named/config.c
e55890
+++ b/bin/named/config.c
27025e
@@ -18,6 +18,7 @@
e55890
 
e55890
 #include <isc/buffer.h>
e55890
 #include <isc/log.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/parseint.h>
e55890
 #include <isc/region.h>
27025e
@@ -974,6 +975,21 @@ ns_config_getkeyalgorithm(const char *str, dns_name_t **name,
e55890
 	return (ns_config_getkeyalgorithm2(str, name, NULL, digestbits));
e55890
 }
e55890
 
e55890
+static inline int
e55890
+algorithms_start() {
e55890
+#ifndef PK11_MD5_DISABLE
27025e
+	if (!isc_md5_available()) {
e55890
+		int i = 0;
e55890
+		while (algorithms[i].str != NULL &&
e55890
+			algorithms[i].hmac == hmacmd5) {
e55890
+			i++;
e55890
+		}
e55890
+		return i;
e55890
+	}
e55890
+#endif
e55890
+	return 0;
e55890
+}
e55890
+
e55890
 isc_result_t
e55890
 ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
27025e
 			   unsigned int *typep, uint16_t *digestbits)
27025e
@@ -983,7 +999,7 @@ ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
27025e
 	uint16_t bits;
e55890
 	isc_result_t result;
e55890
 
e55890
-	for (i = 0; algorithms[i].str != NULL; i++) {
e55890
+	for (i = algorithms_start(); algorithms[i].str != NULL; i++) {
e55890
 		len = strlen(algorithms[i].str);
e55890
 		if (strncasecmp(algorithms[i].str, str, len) == 0 &&
e55890
 		    (str[len] == '\0' ||
27025e
@@ -1006,7 +1022,12 @@ ns_config_getkeyalgorithm2(const char *str, dns_name_t **name,
e55890
 	if (name != NULL) {
e55890
 		switch (algorithms[i].hmac) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-		case hmacmd5: *name = dns_tsig_hmacmd5_name; break;
e55890
+		case hmacmd5:
e55890
+			if (isc_md5_available()) {
e55890
+				*name = dns_tsig_hmacmd5_name; break;
e55890
+			} else {
e55890
+				return (ISC_R_NOTFOUND);
e55890
+			}
e55890
 #endif
e55890
 		case hmacsha1: *name = dns_tsig_hmacsha1_name; break;
e55890
 		case hmacsha224: *name = dns_tsig_hmacsha224_name; break;
e55890
diff --git a/bin/nsupdate/nsupdate.c b/bin/nsupdate/nsupdate.c
27025e
index de60313..bbb3936 100644
e55890
--- a/bin/nsupdate/nsupdate.c
e55890
+++ b/bin/nsupdate/nsupdate.c
27025e
@@ -31,6 +31,7 @@
e55890
 #include <isc/hash.h>
e55890
 #include <isc/lex.h>
e55890
 #include <isc/log.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/parseint.h>
27025e
 #include <isc/portset.h>
27025e
@@ -477,9 +478,10 @@ parse_hmac(dns_name_t **hmac, const char *hmacstr, size_t len,
e55890
 	strlcpy(buf, hmacstr, ISC_MIN(len + 1, sizeof(buf)));
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	if (strcasecmp(buf, "hmac-md5") == 0) {
e55890
+	if (strcasecmp(buf, "hmac-md5") == 0 && isc_md5_available()) {
e55890
 		*hmac = DNS_TSIG_HMACMD5_NAME;
e55890
-	} else if (strncasecmp(buf, "hmac-md5-", 9) == 0) {
e55890
+	} else if (strncasecmp(buf, "hmac-md5-", 9) == 0 &&
e55890
+		   isc_md5_available()) {
e55890
 		*hmac = DNS_TSIG_HMACMD5_NAME;
e55890
 		result = isc_parse_uint16(&digestbits, &buf[9], 10);
e55890
 		if (result != ISC_R_SUCCESS || digestbits > 128) {
27025e
@@ -592,10 +594,10 @@ setup_keystr(void) {
e55890
 			exit(1);
e55890
 		}
e55890
 	} else {
e55890
-#ifndef PK11_MD5_DISABLE
e55890
-		hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
-#else
e55890
 		hmacname = DNS_TSIG_HMACSHA256_NAME;
e55890
+#ifndef PK11_MD5_DISABLE
e55890
+		if (isc_md5_available())
e55890
+			hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
 #endif
e55890
 		name = keystr;
e55890
 		n = s;
27025e
@@ -732,7 +734,8 @@ setup_keyfile(isc_mem_t *mctx, isc_log_t *lctx) {
e55890
 	switch (dst_key_alg(dstkey)) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	case DST_ALG_HMACMD5:
e55890
-		hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
+		if (isc_md5_available())
e55890
+			hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
 		break;
e55890
 #endif
e55890
 	case DST_ALG_HMACSHA1:
27025e
@@ -1637,12 +1640,13 @@ evaluate_key(char *cmdline) {
e55890
 			return (STATUS_SYNTAX);
e55890
 		}
e55890
 		namestr = n + 1;
e55890
-	} else
e55890
-#ifndef PK11_MD5_DISABLE
e55890
-		hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
-#else
e55890
+	} else {
e55890
 		hmacname = DNS_TSIG_HMACSHA256_NAME;
e55890
+#ifndef PK11_MD5_DISABLE
e55890
+		if (isc_md5_available())
e55890
+			hmacname = DNS_TSIG_HMACMD5_NAME;
e55890
 #endif
e55890
+	}
e55890
 
e55890
 	isc_buffer_init(&b, namestr, strlen(namestr));
e55890
 	isc_buffer_add(&b, strlen(namestr));
e55890
diff --git a/bin/rndc/rndc.c b/bin/rndc/rndc.c
27025e
index 9eb0ce0..8083654 100644
e55890
--- a/bin/rndc/rndc.c
e55890
+++ b/bin/rndc/rndc.c
27025e
@@ -23,6 +23,7 @@
e55890
 #include <isc/file.h>
e55890
 #include <isc/log.h>
e55890
 #include <isc/net.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/print.h>
e55890
 #include <isc/random.h>
27025e
@@ -636,7 +637,7 @@ parse_config(isc_mem_t *mctx, isc_log_t *log, const char *keyname,
e55890
 	algorithmstr = cfg_obj_asstring(algorithmobj);
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	if (strcasecmp(algorithmstr, "hmac-md5") == 0)
e55890
+	if (strcasecmp(algorithmstr, "hmac-md5") == 0 && isc_md5_available())
e55890
 		algorithm = ISCCC_ALG_HMACMD5;
e55890
 	else
e55890
 #endif
e55890
diff --git a/bin/tests/optional/hash_test.c b/bin/tests/optional/hash_test.c
27025e
index bf2891a..b5f0a1c 100644
e55890
--- a/bin/tests/optional/hash_test.c
e55890
+++ b/bin/tests/optional/hash_test.c
e55890
@@ -90,43 +90,47 @@ main(int argc, char **argv) {
e55890
 	print_digest(s, "sha224", digest, ISC_SHA224_DIGESTLENGTH/4);
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	s = "abc";
e55890
-	isc_md5_init(&md5;;
e55890
-	memmove(buffer, s, strlen(s));
e55890
-	isc_md5_update(&md5, buffer, strlen(s));
e55890
-	isc_md5_final(&md5, digest);
e55890
-	print_digest(s, "md5", digest, 4);
e55890
-
e55890
-	/*
e55890
-	 * The 3 HMAC-MD5 examples from RFC2104
e55890
-	 */
e55890
-	s = "Hi There";
e55890
-	memset(key, 0x0b, 16);
e55890
-	isc_hmacmd5_init(&hmacmd5, key, 16);
e55890
-	memmove(buffer, s, strlen(s));
e55890
-	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
e55890
-	isc_hmacmd5_sign(&hmacmd5, digest);
e55890
-	print_digest(s, "hmacmd5", digest, 4);
e55890
-
e55890
-	s = "what do ya want for nothing?";
e55890
-	strlcpy((char *)key, "Jefe", sizeof(key));
e55890
-	isc_hmacmd5_init(&hmacmd5, key, 4);
e55890
-	memmove(buffer, s, strlen(s));
e55890
-	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
e55890
-	isc_hmacmd5_sign(&hmacmd5, digest);
e55890
-	print_digest(s, "hmacmd5", digest, 4);
e55890
-
e55890
-	s = "\335\335\335\335\335\335\335\335\335\335"
e55890
-	    "\335\335\335\335\335\335\335\335\335\335"
e55890
-	    "\335\335\335\335\335\335\335\335\335\335"
e55890
-	    "\335\335\335\335\335\335\335\335\335\335"
e55890
-	    "\335\335\335\335\335\335\335\335\335\335";
e55890
-	memset(key, 0xaa, 16);
e55890
-	isc_hmacmd5_init(&hmacmd5, key, 16);
e55890
-	memmove(buffer, s, strlen(s));
e55890
-	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
e55890
-	isc_hmacmd5_sign(&hmacmd5, digest);
e55890
-	print_digest(s, "hmacmd5", digest, 4);
e55890
+	if (isc_md5_available()) {
e55890
+		s = "abc";
e55890
+		isc_md5_init(&md5;;
e55890
+		memmove(buffer, s, strlen(s));
e55890
+		isc_md5_update(&md5, buffer, strlen(s));
e55890
+		isc_md5_final(&md5, digest);
e55890
+		print_digest(s, "md5", digest, 4);
e55890
+
e55890
+		/*
e55890
+		 * The 3 HMAC-MD5 examples from RFC2104
e55890
+		 */
e55890
+		s = "Hi There";
e55890
+		memset(key, 0x0b, 16);
e55890
+		isc_hmacmd5_init(&hmacmd5, key, 16);
e55890
+		memmove(buffer, s, strlen(s));
e55890
+		isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
e55890
+		isc_hmacmd5_sign(&hmacmd5, digest);
e55890
+		print_digest(s, "hmacmd5", digest, 4);
e55890
+
e55890
+		s = "what do ya want for nothing?";
e55890
+		strlcpy((char *)key, "Jefe", sizeof(key));
e55890
+		isc_hmacmd5_init(&hmacmd5, key, 4);
e55890
+		memmove(buffer, s, strlen(s));
e55890
+		isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
e55890
+		isc_hmacmd5_sign(&hmacmd5, digest);
e55890
+		print_digest(s, "hmacmd5", digest, 4);
e55890
+
e55890
+		s = "\335\335\335\335\335\335\335\335\335\335"
e55890
+		    "\335\335\335\335\335\335\335\335\335\335"
e55890
+		    "\335\335\335\335\335\335\335\335\335\335"
e55890
+		    "\335\335\335\335\335\335\335\335\335\335"
e55890
+		    "\335\335\335\335\335\335\335\335\335\335";
e55890
+		memset(key, 0xaa, 16);
e55890
+		isc_hmacmd5_init(&hmacmd5, key, 16);
e55890
+		memmove(buffer, s, strlen(s));
e55890
+		isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
e55890
+		isc_hmacmd5_sign(&hmacmd5, digest);
e55890
+		print_digest(s, "hmacmd5", digest, 4);
e55890
+	} else {
e55890
+		fprintf(stderr, "Skipping disabled MD5 algorithm\n");
e55890
+	}
e55890
 #endif
e55890
 
e55890
 	/*
e55890
diff --git a/bin/tests/system/tkey/keycreate.c b/bin/tests/system/tkey/keycreate.c
27025e
index 5a00f86..653c951 100644
e55890
--- a/bin/tests/system/tkey/keycreate.c
e55890
+++ b/bin/tests/system/tkey/keycreate.c
e55890
@@ -20,6 +20,7 @@
e55890
 #include <isc/entropy.h>
e55890
 #include <isc/hash.h>
e55890
 #include <isc/log.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/print.h>
e55890
 #include <isc/sockaddr.h>
e55890
@@ -142,6 +143,8 @@ sendquery(isc_task_t *task, isc_event_t *event) {
e55890
 	static char keystr[] = "0123456789ab";
e55890
 
e55890
 	isc_event_free(&event);
27025e
+	if (!isc_md5_available())
e55890
+		CHECK("MD5 was disabled", ISC_R_NOTIMPLEMENTED);
e55890
 
e55890
 	result = ISC_R_FAILURE;
e55890
 	if (inet_pton(AF_INET, "10.53.0.1", &inaddr) != 1)
e55890
diff --git a/bin/tests/system/tkey/keydelete.c b/bin/tests/system/tkey/keydelete.c
27025e
index bde66a4..70a40c3 100644
e55890
--- a/bin/tests/system/tkey/keydelete.c
e55890
+++ b/bin/tests/system/tkey/keydelete.c
27025e
@@ -225,12 +225,17 @@ main(int argc, char **argv) {
e55890
 	result = dst_key_fromnamedfile(keyname, NULL, type, mctx, &dstkey);
e55890
 	CHECK("dst_key_fromnamedfile", result);
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	result = dns_tsigkey_createfromkey(dst_key_name(dstkey),
e55890
-					   DNS_TSIG_HMACMD5_NAME,
27025e
-					   dstkey, true, NULL, 0, 0,
e55890
-					   mctx, ring, &tsigkey);
e55890
-	dst_key_free(&dstkey);
e55890
-	CHECK("dns_tsigkey_createfromkey", result);
e55890
+	if (isc_md5_available()) {
e55890
+		result = dns_tsigkey_createfromkey(dst_key_name(dstkey),
e55890
+						   DNS_TSIG_HMACMD5_NAME,
27025e
+						   dstkey, true, NULL, 0, 0,
e55890
+						   mctx, ring, &tsigkey);
e55890
+		dst_key_free(&dstkey);
e55890
+		CHECK("dns_tsigkey_createfromkey", result);
e55890
+	} else {
e55890
+		dst_key_free(&dstkey);
e55890
+		CHECK("MD5 was disabled", ISC_R_NOTIMPLEMENTED);
e55890
+	}
e55890
 #else
e55890
 	dst_key_free(&dstkey);
e55890
 	CHECK("MD5 was disabled", ISC_R_NOTIMPLEMENTED);
e55890
diff --git a/lib/bind9/check.c b/lib/bind9/check.c
27025e
index ec0ab6d..e0803d4 100644
e55890
--- a/lib/bind9/check.c
e55890
+++ b/lib/bind9/check.c
27025e
@@ -23,6 +23,7 @@
e55890
 #include <isc/file.h>
e55890
 #include <isc/hex.h>
e55890
 #include <isc/log.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/netaddr.h>
e55890
 #include <isc/parseint.h>
27025e
@@ -2618,6 +2619,15 @@ bind9_check_key(const cfg_obj_t *key, isc_log_t *logctx) {
e55890
 	}
e55890
 
e55890
 	algorithm = cfg_obj_asstring(algobj);
e55890
+#ifndef PK11_MD5_DISABLE
e55890
+	/* Skip hmac-md5* algorithms */
27025e
+	if (!isc_md5_available() &&
e55890
+	    strncasecmp(algorithm, "hmac-md5", 8) == 0) {
e55890
+		cfg_obj_log(algobj, logctx, ISC_LOG_ERROR,
e55890
+			    "disabled algorithm '%s'", algorithm);
e55890
+		return (ISC_R_DISABLED);
e55890
+	}
e55890
+#endif
e55890
 	for (i = 0; algorithms[i].name != NULL; i++) {
e55890
 		len = strlen(algorithms[i].name);
e55890
 		if (strncasecmp(algorithms[i].name, algorithm, len) == 0 &&
e55890
diff --git a/lib/dns/dst_api.c b/lib/dns/dst_api.c
27025e
index e3c47a9..320c0f8 100644
e55890
--- a/lib/dns/dst_api.c
e55890
+++ b/lib/dns/dst_api.c
27025e
@@ -192,6 +192,12 @@ dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
e55890
 	dst_result_register();
e55890
 
e55890
 	memset(dst_t_func, 0, sizeof(dst_t_func));
e55890
+
e55890
+#ifdef OPENSSL
e55890
+	RETERR(dst__openssl_init(engine));
e55890
+#elif PKCS11CRYPTO
e55890
+	RETERR(dst__pkcs11_init(mctx, engine));
e55890
+#endif
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	RETERR(dst__hmacmd5_init(&dst_t_func[DST_ALG_HMACMD5]));
e55890
 #endif
27025e
@@ -201,7 +207,6 @@ dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
e55890
 	RETERR(dst__hmacsha384_init(&dst_t_func[DST_ALG_HMACSHA384]));
e55890
 	RETERR(dst__hmacsha512_init(&dst_t_func[DST_ALG_HMACSHA512]));
e55890
 #ifdef OPENSSL
e55890
-	RETERR(dst__openssl_init(engine));
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSAMD5],
e55890
 				    DST_ALG_RSAMD5));
27025e
@@ -235,14 +240,18 @@ dst_lib_init2(isc_mem_t *mctx, isc_entropy_t *ectx,
e55890
 	RETERR(dst__openssleddsa_init(&dst_t_func[DST_ALG_ED448]));
e55890
 #endif
e55890
 #elif PKCS11CRYPTO
e55890
-	RETERR(dst__pkcs11_init(mctx, engine));
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSAMD5]));
e55890
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSAMD5],
e55890
+				   DST_ALG_RSAMD5));
e55890
 #endif
e55890
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA1]));
e55890
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_NSEC3RSASHA1]));
e55890
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA256]));
e55890
-	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA512]));
e55890
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA1],
e55890
+				   DST_ALG_RSASHA1));
e55890
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_NSEC3RSASHA1],
e55890
+				   DST_ALG_NSEC3RSASHA1));
e55890
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA256],
e55890
+				   DST_ALG_RSASHA256));
e55890
+	RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA512],
e55890
+				   DST_ALG_RSASHA512));
e55890
 #ifndef PK11_DSA_DISABLE
e55890
 	RETERR(dst__pkcs11dsa_init(&dst_t_func[DST_ALG_DSA]));
e55890
 	RETERR(dst__pkcs11dsa_init(&dst_t_func[DST_ALG_NSEC3DSA]));
e55890
diff --git a/lib/dns/dst_internal.h b/lib/dns/dst_internal.h
27025e
index 6ee796c..3e55d44 100644
e55890
--- a/lib/dns/dst_internal.h
e55890
+++ b/lib/dns/dst_internal.h
27025e
@@ -250,7 +250,8 @@ isc_result_t dst__hmacsha384_init(struct dst_func **funcp);
e55890
 isc_result_t dst__hmacsha512_init(struct dst_func **funcp);
e55890
 isc_result_t dst__opensslrsa_init(struct dst_func **funcp,
e55890
 				  unsigned char algorithm);
e55890
-isc_result_t dst__pkcs11rsa_init(struct dst_func **funcp);
e55890
+isc_result_t dst__pkcs11rsa_init(struct dst_func **funcp,
e55890
+				 unsigned char algorithm);
e55890
 #ifndef PK11_DSA_DISABLE
e55890
 isc_result_t dst__openssldsa_init(struct dst_func **funcp);
e55890
 isc_result_t dst__pkcs11dsa_init(struct dst_func **funcp);
e55890
diff --git a/lib/dns/dst_parse.c b/lib/dns/dst_parse.c
27025e
index f31c33d..87023a6 100644
e55890
--- a/lib/dns/dst_parse.c
e55890
+++ b/lib/dns/dst_parse.c
27025e
@@ -33,6 +33,7 @@
e55890
 #include <isc/file.h>
e55890
 #include <isc/fsaccess.h>
e55890
 #include <isc/lex.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/print.h>
e55890
 #include <isc/stdtime.h>
27025e
@@ -396,6 +397,10 @@ check_data(const dst_private_t *priv, const unsigned int alg,
e55890
 	switch (alg) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	case DST_ALG_RSAMD5:
e55890
+		if (isc_md5_available())
e55890
+			return (check_rsa(priv, external));
e55890
+		else
e55890
+			return (DST_R_UNSUPPORTEDALG);
e55890
 #endif
e55890
 	case DST_ALG_RSASHA1:
e55890
 	case DST_ALG_NSEC3RSASHA1:
27025e
@@ -421,7 +426,10 @@ check_data(const dst_private_t *priv, const unsigned int alg,
e55890
 		return (check_eddsa(priv, external));
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	case DST_ALG_HMACMD5:
e55890
-		return (check_hmac_md5(priv, old));
e55890
+		if (isc_md5_available())
e55890
+			return (check_hmac_md5(priv, old));
e55890
+		else
e55890
+			return (DST_R_UNSUPPORTEDALG);
e55890
 #endif
e55890
 	case DST_ALG_HMACSHA1:
e55890
 		return (check_hmac_sha(priv, HMACSHA1_NTAGS, alg));
27025e
@@ -640,11 +648,13 @@ dst__privstruct_parse(dst_key_t *key, unsigned int alg, isc_lex_t *lex,
e55890
 	}
e55890
 
e55890
 #ifdef PK11_MD5_DISABLE
e55890
-	check = check_data(priv, alg == DST_ALG_RSA ? DST_ALG_RSASHA1 : alg,
27025e
-			   true, external);
e55890
+	if (alg == DST_ALG_RSA)
e55890
+		alg = DST_ALG_RSASHA1;
e55890
 #else
27025e
-	check = check_data(priv, alg, true, external);
27025e
+	if (!isc_md5_available() && alg == DST_ALG_RSA)
e55890
+		alg = DST_ALG_RSASHA1;
e55890
 #endif
27025e
+	check = check_data(priv, alg, true, external);
e55890
 	if (check < 0) {
e55890
 		ret = DST_R_INVALIDPRIVATEKEY;
e55890
 		goto fail;
e55890
diff --git a/lib/dns/hmac_link.c b/lib/dns/hmac_link.c
27025e
index 3b6579b..4bdce2f 100644
e55890
--- a/lib/dns/hmac_link.c
e55890
+++ b/lib/dns/hmac_link.c
27025e
@@ -340,20 +340,10 @@ static dst_func_t hmacmd5_functions = {
e55890
 
e55890
 isc_result_t
e55890
 dst__hmacmd5_init(dst_func_t **funcp) {
e55890
-#ifdef HAVE_FIPS_MODE
27025e
-	/*
e55890
-	 * Problems from OpenSSL are likely from FIPS mode
27025e
-	 */
e55890
-	int fips_mode = FIPS_mode();
e55890
-
e55890
-	if (fips_mode != 0) {
e55890
-		UNEXPECTED_ERROR(__FILE__, __LINE__,
e55890
-				 "FIPS mode is %d: MD5 is only supported "
e55890
-				 "if the value is 0.\n"
e55890
-				 "Please disable either FIPS mode or MD5.",
e55890
-				 fips_mode);
27025e
-	}
27025e
-#endif
e55890
+
27025e
+	/* Intentionally skip initialization */
27025e
+	if (!isc_md5_available())
e55890
+		return (ISC_R_SUCCESS);
e55890
 
27025e
 #if PK11_FLAVOR != PK11_UTIMACO_FLAVOR
27025e
 	/*
e55890
diff --git a/lib/dns/opensslrsa_link.c b/lib/dns/opensslrsa_link.c
27025e
index ec35f50..c80fabe 100644
e55890
--- a/lib/dns/opensslrsa_link.c
e55890
+++ b/lib/dns/opensslrsa_link.c
27025e
@@ -1812,6 +1812,12 @@ dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
e55890
 
e55890
 	if (*funcp == NULL) {
e55890
 		switch (algorithm) {
e55890
+#ifndef PK11_MD5_DISABLE
e55890
+		case DST_ALG_RSAMD5:
e55890
+			if (isc_md5_available())
e55890
+				*funcp = &opensslrsa_functions;
e55890
+			break;
e55890
+#endif
e55890
 		case DST_ALG_RSASHA256:
e55890
 #if defined(HAVE_EVP_SHA256) || !USE_EVP
e55890
 			*funcp = &opensslrsa_functions;
e55890
diff --git a/lib/dns/pkcs11rsa_link.c b/lib/dns/pkcs11rsa_link.c
27025e
index 096c1a8..6c280bf 100644
e55890
--- a/lib/dns/pkcs11rsa_link.c
e55890
+++ b/lib/dns/pkcs11rsa_link.c
27025e
@@ -96,10 +96,15 @@ pkcs11rsa_createctx_sign(dst_key_t *key, dst_context_t *dctx) {
e55890
 #endif
e55890
 
e55890
 	/*
e55890
-	 * Reject incorrect RSA key lengths.
e55890
+	 * Reject incorrect RSA key lengths or disabled algorithms.
e55890
 	 */
e55890
 	switch (dctx->key->key_alg) {
e55890
 	case DST_ALG_RSAMD5:
e55890
+#ifndef PK11_MD5_DISABLE
27025e
+		if (!isc_md5_available())
e55890
+			return (ISC_R_FAILURE);
e55890
+#endif
e55890
+		/* FALLTHROUGH */
e55890
 	case DST_ALG_RSASHA1:
e55890
 	case DST_ALG_NSEC3RSASHA1:
e55890
 		/* From RFC 3110 */
27025e
@@ -641,6 +646,9 @@ pkcs11rsa_createctx(dst_key_t *key, dst_context_t *dctx) {
e55890
 	switch (key->key_alg) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	case DST_ALG_RSAMD5:
27025e
+		if (!isc_md5_available())
e55890
+			return (ISC_R_FAILURE);
e55890
+	
e55890
 		mech.mechanism = CKM_MD5;
e55890
 		break;
e55890
 #endif
27025e
@@ -799,6 +807,9 @@ pkcs11rsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
e55890
 	switch (key->key_alg) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	case DST_ALG_RSAMD5:
27025e
+		if (!isc_md5_available())
e55890
+			return (ISC_R_FAILURE);
e55890
+
e55890
 		der = md5_der;
e55890
 		derlen = sizeof(md5_der);
e55890
 		hashlen = ISC_MD5_DIGESTLENGTH;
27025e
@@ -1024,6 +1035,9 @@ pkcs11rsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
e55890
 	switch (key->key_alg) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	case DST_ALG_RSAMD5:
27025e
+		if (!isc_md5_available())
e55890
+			return (ISC_R_FAILURE);
e55890
+
e55890
 		der = md5_der;
e55890
 		derlen = sizeof(md5_der);
e55890
 		hashlen = ISC_MD5_DIGESTLENGTH;
27025e
@@ -2231,11 +2245,22 @@ static dst_func_t pkcs11rsa_functions = {
e55890
 };
e55890
 
e55890
 isc_result_t
e55890
-dst__pkcs11rsa_init(dst_func_t **funcp) {
e55890
+dst__pkcs11rsa_init(dst_func_t **funcp, unsigned char algorithm) {
e55890
 	REQUIRE(funcp != NULL);
e55890
 
e55890
-	if (*funcp == NULL)
e55890
-		*funcp = &pkcs11rsa_functions;
e55890
+	if (*funcp == NULL) {
e55890
+		switch (algorithm) {
e55890
+#ifndef PK11_MD5_DISABLE
e55890
+			case DST_ALG_RSAMD5:
e55890
+				if (isc_md5_available())
e55890
+					*funcp = &pkcs11rsa_functions;
e55890
+				break;
e55890
+#endif
e55890
+			default:
e55890
+				*funcp = &pkcs11rsa_functions;
e55890
+				break;
e55890
+		}
e55890
+	}
e55890
 	return (ISC_R_SUCCESS);
e55890
 }
e55890
 
e55890
diff --git a/lib/dns/rcode.c b/lib/dns/rcode.c
27025e
index 9c42c50..f51d548 100644
e55890
--- a/lib/dns/rcode.c
e55890
+++ b/lib/dns/rcode.c
27025e
@@ -16,6 +16,7 @@
27025e
 #include <stdbool.h>
e55890
 
e55890
 #include <isc/buffer.h>
e55890
+#include <isc/md5.h>
e55890
 #include <isc/parseint.h>
e55890
 #include <isc/print.h>
e55890
 #include <isc/region.h>
27025e
@@ -357,17 +358,33 @@ dns_cert_totext(dns_cert_t cert, isc_buffer_t *target) {
e55890
 	return (dns_mnemonic_totext(cert, target, certs));
e55890
 }
e55890
 
e55890
+static inline struct tbl *
e55890
+secalgs_tbl_start() {
e55890
+	struct tbl *algs = secalgs;
e55890
+
e55890
+#ifndef PK11_MD5_DISABLE
27025e
+	if (!isc_md5_available()) {
e55890
+		while (algs->name != NULL &&
e55890
+		       algs->value == DNS_KEYALG_RSAMD5)
e55890
+			++algs;
e55890
+	}
e55890
+#endif
e55890
+	return algs;
e55890
+}
e55890
+
e55890
 isc_result_t
e55890
 dns_secalg_fromtext(dns_secalg_t *secalgp, isc_textregion_t *source) {
e55890
 	unsigned int value;
e55890
-	RETERR(dns_mnemonic_fromtext(&value, source, secalgs, 0xff));
e55890
+
e55890
+	RETERR(dns_mnemonic_fromtext(&value, source,
e55890
+	                             secalgs_tbl_start(), 0xff));
e55890
 	*secalgp = value;
e55890
 	return (ISC_R_SUCCESS);
e55890
 }
e55890
 
e55890
 isc_result_t
e55890
 dns_secalg_totext(dns_secalg_t secalg, isc_buffer_t *target) {
e55890
-	return (dns_mnemonic_totext(secalg, target, secalgs));
e55890
+	return (dns_mnemonic_totext(secalg, target, secalgs_tbl_start()));
e55890
 }
e55890
 
e55890
 void
e55890
diff --git a/lib/dns/tests/rsa_test.c b/lib/dns/tests/rsa_test.c
27025e
index f9ac6d0..241e17e 100644
e55890
--- a/lib/dns/tests/rsa_test.c
e55890
+++ b/lib/dns/tests/rsa_test.c
27025e
@@ -27,6 +27,7 @@
27025e
 #define UNIT_TESTING
27025e
 #include <cmocka.h>
e55890
 
e55890
+#include <isc/md5.h>
e55890
 #include <isc/util.h>
e55890
 #include <isc/print.h>
e55890
 
27025e
@@ -248,6 +249,8 @@ isc_rsa_verify_test(void **state) {
e55890
 	/* RSAMD5 */
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
e55890
+	if (isc_md5_available()) {
27025e
+		/* wrong indentation is kept for diff minimization */
27025e
 	key->key_alg = DST_ALG_RSAMD5;
27025e
 
27025e
 	ret = dst_context_create3(key, mctx, DNS_LOGCATEGORY_DNSSEC,
27025e
@@ -265,6 +268,7 @@ isc_rsa_verify_test(void **state) {
27025e
 	assert_int_equal(ret, ISC_R_SUCCESS);
27025e
 
27025e
 	dst_context_destroy(&ctx;;
e55890
+	}
e55890
 #endif
e55890
 
e55890
 	/* RSASHA256 */
e55890
diff --git a/lib/dns/tests/tsig_test.c b/lib/dns/tests/tsig_test.c
27025e
index 11d011a..feb2068 100644
e55890
--- a/lib/dns/tests/tsig_test.c
e55890
+++ b/lib/dns/tests/tsig_test.c
27025e
@@ -25,6 +25,7 @@
27025e
 #define UNIT_TESTING
27025e
 #include <cmocka.h>
e55890
 
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/print.h>
27025e
 #include <isc/util.h>
e55890
diff --git a/lib/dns/tkey.c b/lib/dns/tkey.c
27025e
index 89cfc79..d07364a 100644
e55890
--- a/lib/dns/tkey.c
e55890
+++ b/lib/dns/tkey.c
27025e
@@ -245,6 +245,9 @@ compute_secret(isc_buffer_t *shared, isc_region_t *queryrandomness,
e55890
 	unsigned char digests[32];
e55890
 	unsigned int i;
e55890
 
27025e
+	if (!isc_md5_available())
e55890
+		return (ISC_R_NOTIMPLEMENTED);
e55890
+
e55890
 	isc_buffer_usedregion(shared, &r);
e55890
 
e55890
 	/*
27025e
@@ -321,6 +324,12 @@ process_dhtkey(dns_message_t *msg, dns_name_t *signer, dns_name_t *name,
e55890
 	}
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
27025e
+	if (!isc_md5_available()) {
e55890
+		tkey_log("process_dhtkey: MD5 was disabled");
e55890
+		tkeyout->error = dns_tsigerror_badalg;
e55890
+		return (ISC_R_SUCCESS);
e55890
+	}
e55890
+
e55890
 	if (!dns_name_equal(&tkeyin->algorithm, DNS_TSIG_HMACMD5_NAME)) {
e55890
 		tkey_log("process_dhtkey: algorithms other than "
e55890
 			 "hmac-md5 are not supported");
e55890
diff --git a/lib/dns/tsec.c b/lib/dns/tsec.c
27025e
index 9d8ead4..0c82f65 100644
e55890
--- a/lib/dns/tsec.c
e55890
+++ b/lib/dns/tsec.c
e55890
@@ -11,6 +11,7 @@
e55890
 
e55890
 #include <config.h>
e55890
 
e55890
+#include <isc/md5.h>
e55890
 #include <isc/mem.h>
e55890
 #include <isc/util.h>
e55890
 
e55890
@@ -63,7 +64,12 @@ dns_tsec_create(isc_mem_t *mctx, dns_tsectype_t type, dst_key_t *key,
e55890
 		switch (dst_key_alg(key)) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 		case DST_ALG_HMACMD5:
e55890
-			algname = dns_tsig_hmacmd5_name;
e55890
+			if (isc_md5_available()) {
e55890
+				algname = dns_tsig_hmacmd5_name;
e55890
+			} else {
e55890
+				isc_mem_put(mctx, tsec, sizeof(*tsec));
e55890
+				return (DNS_R_BADALG);
e55890
+			}
e55890
 			break;
e55890
 #endif
e55890
 		case DST_ALG_HMACSHA1:
e55890
diff --git a/lib/dns/tsig.c b/lib/dns/tsig.c
27025e
index 58c1104..00ee1e1 100644
e55890
--- a/lib/dns/tsig.c
e55890
+++ b/lib/dns/tsig.c
27025e
@@ -273,7 +273,8 @@ dns_tsigkey_createfromkey(dns_name_t *name, dns_name_t *algorithm,
e55890
 	(void)dns_name_downcase(&tkey->name, &tkey->name, NULL);
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME)) {
e55890
+	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME) &&
e55890
+	    isc_md5_available()) {
e55890
 		tkey->algorithm = DNS_TSIG_HMACMD5_NAME;
e55890
 		if (dstkey != NULL && dst_key_alg(dstkey) != DST_ALG_HMACMD5) {
e55890
 			ret = DNS_R_BADALG;
27025e
@@ -499,7 +500,8 @@ destroyring(dns_tsig_keyring_t *ring) {
e55890
 static unsigned int
e55890
 dst_alg_fromname(dns_name_t *algorithm) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME)) {
e55890
+	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME) &&
e55890
+	    isc_md5_available()) {
e55890
 		return (DST_ALG_HMACMD5);
e55890
 	} else
e55890
 #endif
27025e
@@ -683,7 +685,8 @@ dns_tsigkey_create(dns_name_t *name, dns_name_t *algorithm,
e55890
 		REQUIRE(secret != NULL);
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME)) {
e55890
+	if (dns_name_equal(algorithm, DNS_TSIG_HMACMD5_NAME) &&
e55890
+	    isc_md5_available()) {
e55890
 		if (secret != NULL) {
e55890
 			isc_buffer_t b;
e55890
 
27025e
@@ -1291,7 +1294,7 @@ dns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,
e55890
 		return (ret);
e55890
 	if (
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	    alg == DST_ALG_HMACMD5 ||
e55890
+	    (alg == DST_ALG_HMACMD5 && isc_md5_available()) ||
e55890
 #endif
e55890
 	    alg == DST_ALG_HMACSHA1 ||
e55890
 	    alg == DST_ALG_HMACSHA224 || alg == DST_ALG_HMACSHA256 ||
27025e
@@ -1460,7 +1463,7 @@ dns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,
e55890
 
e55890
 	if (
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	    alg == DST_ALG_HMACMD5 ||
e55890
+	    (alg == DST_ALG_HMACMD5 && isc_md5_available()) ||
e55890
 #endif
e55890
 	    alg == DST_ALG_HMACSHA1 ||
e55890
 	    alg == DST_ALG_HMACSHA224 || alg == DST_ALG_HMACSHA256 ||
27025e
@@ -1601,7 +1604,7 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
e55890
 			goto cleanup_querystruct;
e55890
 		if (
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-			alg == DST_ALG_HMACMD5 ||
e55890
+			(alg == DST_ALG_HMACMD5 && isc_md5_available()) ||
e55890
 #endif
e55890
 			alg == DST_ALG_HMACSHA1 ||
e55890
 			alg == DST_ALG_HMACSHA224 ||
27025e
@@ -1780,7 +1783,7 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
e55890
 			goto cleanup_context;
e55890
 		if (
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-			alg == DST_ALG_HMACMD5 ||
e55890
+			(alg == DST_ALG_HMACMD5 && isc_md5_available()) ||
e55890
 #endif
e55890
 			alg == DST_ALG_HMACSHA1 ||
e55890
 			alg == DST_ALG_HMACSHA224 ||
e55890
diff --git a/lib/isc/include/isc/md5.h b/lib/isc/include/isc/md5.h
27025e
index 4d29398..e3f5cec 100644
e55890
--- a/lib/isc/include/isc/md5.h
e55890
+++ b/lib/isc/include/isc/md5.h
27025e
@@ -91,6 +91,9 @@ isc_md5_final(isc_md5_t *ctx, unsigned char *digest);
27025e
 bool
27025e
 isc_md5_check(bool testing);
e55890
 
27025e
+bool
e55890
+isc_md5_available(void);
e55890
+
e55890
 ISC_LANG_ENDDECLS
e55890
 
e55890
 #endif /* !PK11_MD5_DISABLE */
e55890
diff --git a/lib/isc/md5.c b/lib/isc/md5.c
27025e
index 249f3da..628a414 100644
e55890
--- a/lib/isc/md5.c
e55890
+++ b/lib/isc/md5.c
27025e
@@ -37,6 +37,7 @@
e55890
 
e55890
 #include <isc/assertions.h>
e55890
 #include <isc/md5.h>
e55890
+#include <isc/once.h>
e55890
 #include <isc/platform.h>
e55890
 #include <isc/safe.h>
e55890
 #include <isc/string.h>
27025e
@@ -54,6 +55,9 @@
e55890
 #define EVP_MD_CTX_free(ptr) EVP_MD_CTX_cleanup(ptr)
e55890
 #endif
e55890
 
e55890
+static isc_once_t available_once = ISC_ONCE_INIT;
27025e
+static bool available = false;
e55890
+
e55890
 void
e55890
 isc_md5_init(isc_md5_t *ctx) {
e55890
 	ctx->ctx = EVP_MD_CTX_new();
27025e
@@ -85,8 +89,33 @@ isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
e55890
 	ctx->ctx = NULL;
e55890
 }
e55890
 
e55890
+static void
e55890
+do_detect_available() {
e55890
+	isc_md5_t local;
e55890
+	isc_md5_t *ctx = &local;
e55890
+	unsigned char digest[ISC_MD5_DIGESTLENGTH];
e55890
+
e55890
+	ctx->ctx = EVP_MD_CTX_new();
e55890
+	RUNTIME_CHECK(ctx->ctx != NULL);
27025e
+	available = (EVP_DigestInit(ctx->ctx, EVP_md5()) == 1);
e55890
+	if (available)
e55890
+		(void)EVP_DigestFinal(ctx->ctx, digest, NULL);
e55890
+	EVP_MD_CTX_free(ctx->ctx);
e55890
+	ctx->ctx = NULL;
e55890
+}
e55890
+
27025e
+bool
e55890
+isc_md5_available() {
e55890
+	RUNTIME_CHECK(isc_once_do(&available_once, do_detect_available)
e55890
+		      == ISC_R_SUCCESS);
e55890
+	return available;
e55890
+}
e55890
+
e55890
 #elif PKCS11CRYPTO
e55890
 
e55890
+static isc_once_t available_once = ISC_ONCE_INIT;
27025e
+static bool available = false;
e55890
+
e55890
 void
e55890
 isc_md5_init(isc_md5_t *ctx) {
e55890
 	CK_RV rv;
27025e
@@ -129,6 +158,31 @@ isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
e55890
 	pk11_return_session(ctx);
e55890
 }
e55890
 
e55890
+static void
e55890
+do_detect_available() {
e55890
+	isc_md5_t local;
e55890
+	isc_md5_t *ctx = &local;
e55890
+	CK_RV rv;
e55890
+	CK_MECHANISM mech = { CKM_MD5, NULL, 0 };
e55890
+
27025e
+	if (pk11_get_session(ctx, OP_DIGEST, true, false,
27025e
+				       false, NULL, 0) == ISC_R_SUCCESS)
e55890
+	{
e55890
+		rv = pkcs_C_DigestInit(ctx->session, &mech);
e55890
+		isc_md5_invalidate(ctx);
27025e
+		available = (rv == CKR_OK);
e55890
+	} else {
27025e
+		available = false;
e55890
+	}
e55890
+}
e55890
+
27025e
+bool
e55890
+isc_md5_available() {
e55890
+	RUNTIME_CHECK(isc_once_do(&available_once, do_detect_available)
e55890
+		      == ISC_R_SUCCESS);
e55890
+	return available;
e55890
+}
e55890
+
e55890
 #else
e55890
 
e55890
 static void
27025e
@@ -338,6 +392,11 @@ isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
e55890
 	memmove(digest, ctx->buf, 16);
e55890
 	isc_safe_memwipe(ctx, sizeof(*ctx));	/* In case it's sensitive */
e55890
 }
e55890
+
27025e
+bool
e55890
+isc_md5_available() {
27025e
+	return true;
e55890
+}
e55890
 #endif
e55890
 
e55890
 /*
e55890
diff --git a/lib/isc/pk11.c b/lib/isc/pk11.c
27025e
index 0d5b009..7809e7b 100644
e55890
--- a/lib/isc/pk11.c
e55890
+++ b/lib/isc/pk11.c
27025e
@@ -197,8 +197,6 @@ pk11_initialize(isc_mem_t *mctx, const char *engine) {
27025e
 	UNLOCK(&alloclock);
e55890
 	if (initialized) {
27025e
 		goto unlock;
e55890
-	} else {
27025e
-		initialized = true;
e55890
 	}
e55890
 
e55890
 	ISC_LIST_INIT(tokens);
27025e
@@ -238,6 +236,7 @@ pk11_initialize(isc_mem_t *mctx, const char *engine) {
e55890
 	}
e55890
 #endif
e55890
 #endif /* PKCS11CRYPTO */
27025e
+	initialized = true;
e55890
  unlock:
e55890
 	UNLOCK(&sessionlock);
27025e
 	return (result);
27025e
@@ -589,6 +588,8 @@ scan_slots(void) {
e55890
 	pk11_token_t *token;
e55890
 	unsigned int i;
27025e
 	bool bad;
e55890
+	unsigned int best_rsa_algorithms = 0;
e55890
+	unsigned int best_digest_algorithms = 0;
e55890
 
e55890
 	slotCount = 0;
e55890
 	PK11_FATALCHECK(pkcs_C_GetSlotList, (CK_FALSE, NULL_PTR, &slotCount));
27025e
@@ -601,6 +602,8 @@ scan_slots(void) {
e55890
 	PK11_FATALCHECK(pkcs_C_GetSlotList, (CK_FALSE, slotList, &slotCount));
e55890
 
e55890
 	for (i = 0; i < slotCount; i++) {
e55890
+		unsigned int rsa_algorithms = 0;
e55890
+		unsigned int digest_algorithms = 0;
e55890
 		slot = slotList[i];
e55890
 		PK11_TRACE2("slot#%u=0x%lx\n", i, slot);
e55890
 
27025e
@@ -640,11 +643,12 @@ scan_slots(void) {
e55890
 		if ((rv != CKR_OK) ||
e55890
 		    ((mechInfo.flags & CKF_SIGN) == 0) ||
e55890
 		    ((mechInfo.flags & CKF_VERIFY) == 0)) {
e55890
-#if !defined(PK11_MD5_DISABLE) && !defined(PK11_RSA_PKCS_REPLACE)
27025e
-			bad = true;
e55890
-#endif
e55890
 			PK11_TRACEM(CKM_MD5_RSA_PKCS);
e55890
 		}
e55890
+#if !defined(PK11_MD5_DISABLE) && !defined(PK11_RSA_PKCS_REPLACE)
e55890
+		else
e55890
+			++rsa_algorithms;
e55890
+#endif
e55890
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_SHA1_RSA_PKCS,
e55890
 					     &mechInfo);
e55890
 		if ((rv != CKR_OK) ||
27025e
@@ -687,8 +691,14 @@ scan_slots(void) {
e55890
 		if (bad)
e55890
 			goto try_dsa;
e55890
 		token->operations |= 1 << OP_RSA;
e55890
-		if (best_rsa_token == NULL)
e55890
+		if (best_rsa_token == NULL) {
27025e
 			best_rsa_token = token;
e55890
+			best_rsa_algorithms = rsa_algorithms;
e55890
+		} else if (rsa_algorithms > best_rsa_algorithms) {
e55890
+			pk11_mem_put(best_rsa_token, sizeof(*best_rsa_token));
27025e
+			best_rsa_token = token;
e55890
+			best_rsa_algorithms = rsa_algorithms;
e55890
+		}
e55890
 
e55890
 	try_dsa:
27025e
 		bad = false;
27025e
@@ -756,11 +766,12 @@ scan_slots(void) {
27025e
 		bad = false;
e55890
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_MD5, &mechInfo);
e55890
 		if ((rv != CKR_OK) || ((mechInfo.flags & CKF_DIGEST) == 0)) {
e55890
-#ifndef PK11_MD5_DISABLE
27025e
-			bad = true;
e55890
-#endif
e55890
 			PK11_TRACEM(CKM_MD5);
e55890
 		}
e55890
+#ifndef PK11_MD5_DISABLE
e55890
+		else
e55890
+			++digest_algorithms;
e55890
+#endif
e55890
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_SHA_1, &mechInfo);
e55890
 		if ((rv != CKR_OK) || ((mechInfo.flags & CKF_DIGEST) == 0)) {
27025e
 			bad = true;
27025e
@@ -788,11 +799,12 @@ scan_slots(void) {
e55890
 		}
e55890
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_MD5_HMAC, &mechInfo);
e55890
 		if ((rv != CKR_OK) || ((mechInfo.flags & CKF_SIGN) == 0)) {
e55890
-#if !defined(PK11_MD5_DISABLE) && !defined(PK11_MD5_HMAC_REPLACE)
27025e
-			bad = true;
e55890
-#endif
e55890
 			PK11_TRACEM(CKM_MD5_HMAC);
e55890
 		}
e55890
+#if !defined(PK11_MD5_DISABLE) && !defined(PK11_MD5_HMAC_REPLACE)
e55890
+		else
e55890
+			++digest_algorithms;
e55890
+#endif
e55890
 		rv = pkcs_C_GetMechanismInfo(slot, CKM_SHA_1_HMAC, &mechInfo);
e55890
 		if ((rv != CKR_OK) || ((mechInfo.flags & CKF_SIGN) == 0)) {
e55890
 #ifndef PK11_SHA_1_HMAC_REPLACE
27025e
@@ -830,8 +842,14 @@ scan_slots(void) {
e55890
 		}
e55890
 		if (!bad) {
e55890
 			token->operations |= 1 << OP_DIGEST;
e55890
-			if (digest_token == NULL)
e55890
+			if (digest_token == NULL) {
27025e
 				digest_token = token;
e55890
+				best_digest_algorithms = digest_algorithms;
e55890
+			} else if (digest_algorithms > best_digest_algorithms) {
e55890
+				pk11_mem_put(digest_token, sizeof(*digest_token));
27025e
+				digest_token = token;
e55890
+				best_digest_algorithms = digest_algorithms;
e55890
+			}
e55890
 		}
e55890
 
e55890
 		/* ECDSA requires digest */
e55890
diff --git a/lib/isc/tests/hash_test.c b/lib/isc/tests/hash_test.c
27025e
index 31ced94..421131e 100644
e55890
--- a/lib/isc/tests/hash_test.c
e55890
+++ b/lib/isc/tests/hash_test.c
27025e
@@ -775,6 +775,9 @@ isc_md5_test(void **state) {
27025e
 
27025e
 	UNUSED(state);
27025e
 
27025e
+	if (!isc_md5_available())
27025e
+		return;
27025e
+
27025e
 	/*
27025e
 	 * These are the various test vectors.  All of these are passed
27025e
 	 * through the hash function and the results are compared to the
27025e
@@ -1630,6 +1633,9 @@ isc_hmacmd5_test(void **state) {
27025e
 
27025e
 	UNUSED(state);
27025e
 
27025e
+	if (!isc_md5_available())
27025e
+		return;
27025e
+
27025e
 	/*
27025e
 	 * These are the various test vectors.  All of these are passed
27025e
 	 * through the hash function and the results are compared to the
27025e
@@ -1940,6 +1946,9 @@ static void
27025e
 md5_check_test(void **state) {
27025e
 	UNUSED(state);
27025e
 
27025e
+	if (!isc_md5_available())
27025e
+		return;
27025e
+
27025e
 	assert_true(isc_md5_check(false));
27025e
 	assert_false(isc_md5_check(true));
e55890
 
e55890
diff --git a/lib/isccc/cc.c b/lib/isccc/cc.c
27025e
index c2740cb..c314d76 100644
e55890
--- a/lib/isccc/cc.c
e55890
+++ b/lib/isccc/cc.c
27025e
@@ -272,11 +272,15 @@ sign(unsigned char *data, unsigned int length, unsigned char *hmac,
e55890
 	switch (algorithm) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	case ISCCC_ALG_HMACMD5:
e55890
-		isc_hmacmd5_init(&ctx.hmd5, secret->rstart,
e55890
-				 REGION_SIZE(*secret));
e55890
-		isc_hmacmd5_update(&ctx.hmd5, data, length);
e55890
-		isc_hmacmd5_sign(&ctx.hmd5, digest);
e55890
-		source.rend = digest + ISC_MD5_DIGESTLENGTH;
e55890
+		if (isc_md5_available()) {
e55890
+			isc_hmacmd5_init(&ctx.hmd5, secret->rstart,
e55890
+					 REGION_SIZE(*secret));
e55890
+			isc_hmacmd5_update(&ctx.hmd5, data, length);
e55890
+			isc_hmacmd5_sign(&ctx.hmd5, digest);
e55890
+			source.rend = digest + ISC_MD5_DIGESTLENGTH;
e55890
+		} else {
e55890
+			return (ISC_R_FAILURE);
e55890
+		}
e55890
 		break;
e55890
 #endif
e55890
 
27025e
@@ -350,14 +354,18 @@ isccc_cc_towire(isccc_sexpr_t *alist, isc_buffer_t **buffer,
e55890
 {
e55890
 	unsigned int hmac_base, signed_base;
e55890
 	isc_result_t result;
27025e
+	const bool md5 = (algorithm == ISCCC_ALG_HMACMD5);
e55890
 
e55890
 #ifndef PK11_MD5_DISABLE
27025e
+	if (md5 && !isc_md5_available())
e55890
+		return (ISC_R_NOTIMPLEMENTED);
e55890
+
e55890
 	result = isc_buffer_reserve(buffer,
e55890
-				    4 + ((algorithm == ISCCC_ALG_HMACMD5) ?
e55890
+				    4 + ((md5) ?
e55890
 					 sizeof(auth_hmd5) :
e55890
 					 sizeof(auth_hsha)));
e55890
 #else
e55890
-	if (algorithm == ISCCC_ALG_HMACMD5)
e55890
+	if (md5)
e55890
 		return (ISC_R_NOTIMPLEMENTED);
e55890
 	result = isc_buffer_reserve(buffer, 4 + sizeof(auth_hsha));
e55890
 #endif
27025e
@@ -376,7 +384,7 @@ isccc_cc_towire(isccc_sexpr_t *alist, isc_buffer_t **buffer,
e55890
 		 * we know what it is.
e55890
 		 */
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-		if (algorithm == ISCCC_ALG_HMACMD5) {
e55890
+		if (md5) {
e55890
 			hmac_base = (*buffer)->used + HMD5_OFFSET;
e55890
 			isc_buffer_putmem(*buffer,
e55890
 					  auth_hmd5, sizeof(auth_hmd5));
27025e
@@ -442,7 +450,7 @@ verify(isccc_sexpr_t *alist, unsigned char *data, unsigned int length,
e55890
 	if (!isccc_alist_alistp(_auth))
e55890
 		return (ISC_R_FAILURE);
e55890
 #ifndef PK11_MD5_DISABLE
e55890
-	if (algorithm == ISCCC_ALG_HMACMD5)
e55890
+	if (algorithm == ISCCC_ALG_HMACMD5 && isc_md5_available())
e55890
 		hmac = isccc_alist_lookup(_auth, "hmd5");
e55890
 	else
e55890
 #endif
27025e
@@ -457,12 +465,16 @@ verify(isccc_sexpr_t *alist, unsigned char *data, unsigned int length,
e55890
 	switch (algorithm) {
e55890
 #ifndef PK11_MD5_DISABLE
e55890
 	case ISCCC_ALG_HMACMD5:
e55890
-		isc_hmacmd5_init(&ctx.hmd5, secret->rstart,
e55890
-				 REGION_SIZE(*secret));
e55890
-		isc_hmacmd5_update(&ctx.hmd5, data, length);
e55890
-		isc_hmacmd5_sign(&ctx.hmd5, digest);
e55890
-		source.rend = digest + ISC_MD5_DIGESTLENGTH;
e55890
-		break;
e55890
+		if (isc_md5_available()) {
e55890
+			isc_hmacmd5_init(&ctx.hmd5, secret->rstart,
e55890
+					 REGION_SIZE(*secret));
e55890
+			isc_hmacmd5_update(&ctx.hmd5, data, length);
e55890
+			isc_hmacmd5_sign(&ctx.hmd5, digest);
e55890
+			source.rend = digest + ISC_MD5_DIGESTLENGTH;
e55890
+			break;
e55890
+		} else {
e55890
+			return (ISC_R_FAILURE);
e55890
+		}
e55890
 #endif
e55890
 
e55890
 	case ISCCC_ALG_HMACSHA1:
e55890
-- 
27025e
2.20.1
e55890