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