From d3147417c55cb1277cac42034198a3011c8f5cfb Mon Sep 17 00:00:00 2001
From: Matthijs Mekking <matthijs@isc.org>
Date: Tue, 17 May 2022 12:02:43 +0200
Subject: [PATCH] Require valid key for dst_key functions

Make sure that the key structure is valid when calling the following
functions:
- dst_key_setexternal
- dst_key_isexternal
- dst_key_setmodified
- dst_key_ismodified

This commit is adapted because 9.16 has a different approach
of deconsting the variable.

(cherry picked from commit 888ec4e0d407a9333017d6997a2be81a69658e1f)
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/d3147417c55cb1277cac42034198a3011c8f5cfb
---
 lib/dns/dst_api.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/lib/dns/dst_api.c b/lib/dns/dst_api.c
index e5a52aea37..f5741a1af4 100644
--- a/lib/dns/dst_api.c
+++ b/lib/dns/dst_api.c
@@ -482,16 +482,22 @@ dst_key_tofile(const dst_key_t *key, int type, const char *directory) {
 
 void
 dst_key_setexternal(dst_key_t *key, bool value) {
+	REQUIRE(VALID_KEY(key));
+
 	key->external = value;
 }
 
 bool
 dst_key_isexternal(dst_key_t *key) {
+	REQUIRE(VALID_KEY(key));
+
 	return (key->external);
 }
 
 void
 dst_key_setmodified(dst_key_t *key, bool value) {
+	REQUIRE(VALID_KEY(key));
+
 	isc_mutex_lock(&key->mdlock);
 	key->modified = value;
 	isc_mutex_unlock(&key->mdlock);
@@ -500,10 +506,15 @@ dst_key_setmodified(dst_key_t *key, bool value) {
 bool
 dst_key_ismodified(const dst_key_t *key) {
 	bool modified;
+	dst_key_t *k;
 
-	isc_mutex_lock(&(((dst_key_t *)key)->mdlock));
+	REQUIRE(VALID_KEY(key));
+
+	DE_CONST(key, k);
+
+	isc_mutex_lock(&k->mdlock);
 	modified = key->modified;
-	isc_mutex_unlock(&(((dst_key_t *)key)->mdlock));
+	isc_mutex_unlock(&k->mdlock);
 
 	return (modified);
 }
-- 
2.27.0

