Blame SOURCES/bind99-CVE-2017-3142+3143.patch

d10948
diff --git a/lib/dns/dnssec.c b/lib/dns/dnssec.c
d10948
index 00a0080..336c4da 100644
d10948
--- a/lib/dns/dnssec.c
d10948
+++ b/lib/dns/dnssec.c
d10948
@@ -982,6 +982,8 @@ dns_dnssec_verifymessage(isc_buffer_t *source, dns_message_t *msg,
d10948
 	mctx = msg->mctx;
d10948
 
d10948
 	msg->verify_attempted = 1;
d10948
+	msg->verified_sig = 0;
d10948
+	msg->sig0status = dns_tsigerror_badsig;
d10948
 
d10948
 	if (is_response(msg)) {
d10948
 		if (msg->query.base == NULL)
d10948
@@ -1076,6 +1078,7 @@ dns_dnssec_verifymessage(isc_buffer_t *source, dns_message_t *msg,
d10948
 	}
d10948
 
d10948
 	msg->verified_sig = 1;
d10948
+	msg->sig0status = dns_rcode_noerror;
d10948
 
d10948
 	dst_context_destroy(&ctx;;
d10948
 	dns_rdata_freestruct(&sig);
d10948
diff --git a/lib/dns/message.c b/lib/dns/message.c
d10948
index 1417067..0621175 100644
d10948
--- a/lib/dns/message.c
d10948
+++ b/lib/dns/message.c
d10948
@@ -3052,12 +3052,19 @@ dns_message_signer(dns_message_t *msg, dns_name_t *signer) {
d10948
 
d10948
 		result = dns_rdata_tostruct(&rdata, &tsig, NULL);
d10948
 		INSIST(result == ISC_R_SUCCESS);
d10948
-		if (msg->tsigstatus != dns_rcode_noerror)
d10948
+		if (msg->verified_sig &&
d10948
+		    msg->tsigstatus == dns_rcode_noerror &&
d10948
+		    tsig.error == dns_rcode_noerror)
d10948
+		{
d10948
+			result = ISC_R_SUCCESS;
d10948
+		} else if ((!msg->verified_sig) ||
d10948
+			   (msg->tsigstatus != dns_rcode_noerror))
d10948
+		{
d10948
 			result = DNS_R_TSIGVERIFYFAILURE;
d10948
-		else if (tsig.error != dns_rcode_noerror)
d10948
+		} else {
d10948
+			INSIST(tsig.error != dns_rcode_noerror);
d10948
 			result = DNS_R_TSIGERRORSET;
d10948
-		else
d10948
-			result = ISC_R_SUCCESS;
d10948
+		}
d10948
 		dns_rdata_freestruct(&tsig);
d10948
 
d10948
 		if (msg->tsigkey == NULL) {
d10948
diff --git a/lib/dns/tsig.c b/lib/dns/tsig.c
d10948
index 3239bff..7b91d1e 100644
d10948
--- a/lib/dns/tsig.c
d10948
+++ b/lib/dns/tsig.c
d10948
@@ -941,11 +941,20 @@ dns_tsig_sign(dns_message_t *msg) {
d10948
 		isc_buffer_putuint48(&otherbuf, tsig.timesigned);
d10948
 	}
d10948
 
d10948
-	if (key->key != NULL && tsig.error != dns_tsigerror_badsig) {
d10948
+	if ((key->key != NULL) &&
d10948
+	    (tsig.error != dns_tsigerror_badsig) &&
d10948
+	    (tsig.error != dns_tsigerror_badkey))
d10948
+	{
d10948
 		unsigned char header[DNS_MESSAGE_HEADERLEN];
d10948
 		isc_buffer_t headerbuf;
d10948
 		isc_uint16_t digestbits;
d10948
 
d10948
+		/*
d10948
+		 * If it is a response, we assume that the request MAC
d10948
+		 * has validated at this point. This is why we include a
d10948
+		 * MAC length > 0 in the reply.
d10948
+		 */
d10948
+
d10948
 		ret = dst_context_create3(key->key, mctx,
d10948
 					  DNS_LOGCATEGORY_DNSSEC,
d10948
 					  ISC_TRUE, &ctx;;
d10948
@@ -953,7 +962,7 @@ dns_tsig_sign(dns_message_t *msg) {
d10948
 			return (ret);
d10948
 
d10948
 		/*
d10948
-		 * If this is a response, digest the query signature.
d10948
+		 * If this is a response, digest the request's MAC.
d10948
 		 */
d10948
 		if (response) {
d10948
 			dns_rdata_t querytsigrdata = DNS_RDATA_INIT;
d10948
@@ -1083,6 +1092,17 @@ dns_tsig_sign(dns_message_t *msg) {
d10948
 		dst_context_destroy(&ctx;;
d10948
 		digestbits = dst_key_getbits(key->key);
d10948
 		if (digestbits != 0) {
d10948
+			/*
d10948
+			 * XXXRAY: Is this correct? What is the
d10948
+			 * expected behavior when digestbits is not an
d10948
+			 * integral multiple of 8? It looks like bytes
d10948
+			 * should either be (digestbits/8) or
d10948
+			 * (digestbits+7)/8.
d10948
+			 *
d10948
+			 * In any case, for current algorithms,
d10948
+			 * digestbits are an integral multiple of 8, so
d10948
+			 * it has the same effect as (digestbits/8).
d10948
+			 */
d10948
 			unsigned int bytes = (digestbits + 1) / 8;
d10948
 			if (response && bytes < querytsig.siglen)
d10948
 				bytes = querytsig.siglen;
d10948
@@ -1196,6 +1216,8 @@ dns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,
d10948
 	REQUIRE(tsigkey == NULL || VALID_TSIG_KEY(tsigkey));
d10948
 
d10948
 	msg->verify_attempted = 1;
d10948
+	msg->verified_sig = 0;
d10948
+	msg->tsigstatus = dns_tsigerror_badsig;
d10948
 
d10948
 	if (msg->tcp_continuation) {
d10948
 		if (tsigkey == NULL || msg->querytsig == NULL)
d10948
@@ -1294,19 +1316,6 @@ dns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,
d10948
 	key = tsigkey->key;
d10948
 
d10948
 	/*
d10948
-	 * Is the time ok?
d10948
-	 */
d10948
-	if (now + msg->timeadjust > tsig.timesigned + tsig.fudge) {
d10948
-		msg->tsigstatus = dns_tsigerror_badtime;
d10948
-		tsig_log(msg->tsigkey, 2, "signature has expired");
d10948
-		return (DNS_R_CLOCKSKEW);
d10948
-	} else if (now + msg->timeadjust < tsig.timesigned - tsig.fudge) {
d10948
-		msg->tsigstatus = dns_tsigerror_badtime;
d10948
-		tsig_log(msg->tsigkey, 2, "signature is in the future");
d10948
-		return (DNS_R_CLOCKSKEW);
d10948
-	}
d10948
-
d10948
-	/*
d10948
 	 * Check digest length.
d10948
 	 */
d10948
 	alg = dst_key_alg(key);
d10948
@@ -1315,31 +1324,19 @@ dns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,
d10948
 		return (ret);
d10948
 	if (alg == DST_ALG_HMACMD5 || alg == DST_ALG_HMACSHA1 ||
d10948
 	    alg == DST_ALG_HMACSHA224 || alg == DST_ALG_HMACSHA256 ||
d10948
-	    alg == DST_ALG_HMACSHA384 || alg == DST_ALG_HMACSHA512) {
d10948
-		isc_uint16_t digestbits = dst_key_getbits(key);
d10948
+	    alg == DST_ALG_HMACSHA384 || alg == DST_ALG_HMACSHA512)
d10948
+	{
d10948
 		if (tsig.siglen > siglen) {
d10948
-			tsig_log(msg->tsigkey, 2, "signature length to big");
d10948
+			tsig_log(msg->tsigkey, 2, "signature length too big");
d10948
 			return (DNS_R_FORMERR);
d10948
 		}
d10948
 		if (tsig.siglen > 0 &&
d10948
-		    (tsig.siglen < 10 || tsig.siglen < ((siglen + 1) / 2))) {
d10948
+		    (tsig.siglen < 10 || tsig.siglen < ((siglen + 1) / 2)))
d10948
+		{
d10948
 			tsig_log(msg->tsigkey, 2,
d10948
 				 "signature length below minimum");
d10948
 			return (DNS_R_FORMERR);
d10948
 		}
d10948
-		if (tsig.siglen > 0 && digestbits != 0 &&
d10948
-		    tsig.siglen < ((digestbits + 1) / 8)) {
d10948
-			msg->tsigstatus = dns_tsigerror_badtrunc;
d10948
-			tsig_log(msg->tsigkey, 2,
d10948
-				 "truncated signature length too small");
d10948
-			return (DNS_R_TSIGVERIFYFAILURE);
d10948
-		}
d10948
-		if (tsig.siglen > 0 && digestbits == 0 &&
d10948
-		    tsig.siglen < siglen) {
d10948
-			msg->tsigstatus = dns_tsigerror_badtrunc;
d10948
-			tsig_log(msg->tsigkey, 2, "signature length too small");
d10948
-			return (DNS_R_TSIGVERIFYFAILURE);
d10948
-		}
d10948
 	}
d10948
 
d10948
 	if (tsig.siglen > 0) {
d10948
@@ -1451,34 +1448,92 @@ dns_tsig_verify(isc_buffer_t *source, dns_message_t *msg,
d10948
 
d10948
 		ret = dst_context_verify(ctx, &sig_r);
d10948
 		if (ret == DST_R_VERIFYFAILURE) {
d10948
-			msg->tsigstatus = dns_tsigerror_badsig;
d10948
 			ret = DNS_R_TSIGVERIFYFAILURE;
d10948
 			tsig_log(msg->tsigkey, 2,
d10948
 				 "signature failed to verify(1)");
d10948
 			goto cleanup_context;
d10948
-		} else if (ret != ISC_R_SUCCESS)
d10948
+		} else if (ret != ISC_R_SUCCESS) {
d10948
 			goto cleanup_context;
d10948
-
d10948
-		dst_context_destroy(&ctx;;
d10948
+		}
d10948
 	} else if (tsig.error != dns_tsigerror_badsig &&
d10948
 		   tsig.error != dns_tsigerror_badkey) {
d10948
-		msg->tsigstatus = dns_tsigerror_badsig;
d10948
 		tsig_log(msg->tsigkey, 2, "signature was empty");
d10948
 		return (DNS_R_TSIGVERIFYFAILURE);
d10948
 	}
d10948
 
d10948
-	msg->tsigstatus = dns_rcode_noerror;
d10948
+	/*
d10948
+	 * Here at this point, the MAC has been verified. Even if any of
d10948
+	 * the following code returns a TSIG error, the reply will be
d10948
+	 * signed and WILL always include the request MAC in the digest
d10948
+	 * computation.
d10948
+	 */
d10948
+
d10948
+	/*
d10948
+	 * Is the time ok?
d10948
+	 */
d10948
+	if (now + msg->timeadjust > tsig.timesigned + tsig.fudge) {
d10948
+		msg->tsigstatus = dns_tsigerror_badtime;
d10948
+		tsig_log(msg->tsigkey, 2, "signature has expired");
d10948
+		ret = DNS_R_CLOCKSKEW;
d10948
+		goto cleanup_context;
d10948
+	} else if (now + msg->timeadjust < tsig.timesigned - tsig.fudge) {
d10948
+		msg->tsigstatus = dns_tsigerror_badtime;
d10948
+		tsig_log(msg->tsigkey, 2, "signature is in the future");
d10948
+		ret = DNS_R_CLOCKSKEW;
d10948
+		goto cleanup_context;
d10948
+	}
d10948
+
d10948
+	if (
d10948
+#ifndef PK11_MD5_DISABLE
d10948
+	    alg == DST_ALG_HMACMD5 ||
d10948
+#endif
d10948
+	    alg == DST_ALG_HMACSHA1 ||
d10948
+	    alg == DST_ALG_HMACSHA224 || alg == DST_ALG_HMACSHA256 ||
d10948
+	    alg == DST_ALG_HMACSHA384 || alg == DST_ALG_HMACSHA512)
d10948
+	{
d10948
+		isc_uint16_t digestbits = dst_key_getbits(key);
d10948
+
d10948
+		/*
d10948
+		 * XXXRAY: Is this correct? What is the expected
d10948
+		 * behavior when digestbits is not an integral multiple
d10948
+		 * of 8? It looks like bytes should either be
d10948
+		 * (digestbits/8) or (digestbits+7)/8.
d10948
+		 *
d10948
+		 * In any case, for current algorithms, digestbits are
d10948
+		 * an integral multiple of 8, so it has the same effect
d10948
+		 * as (digestbits/8).
d10948
+		 */
d10948
+		if (tsig.siglen > 0 && digestbits != 0 &&
d10948
+		    tsig.siglen < ((digestbits + 1) / 8))
d10948
+		{
d10948
+			msg->tsigstatus = dns_tsigerror_badtrunc;
d10948
+			tsig_log(msg->tsigkey, 2,
d10948
+				 "truncated signature length too small");
d10948
+			ret = DNS_R_TSIGVERIFYFAILURE;
d10948
+			goto cleanup_context;
d10948
+		}
d10948
+		if (tsig.siglen > 0 && digestbits == 0 &&
d10948
+		    tsig.siglen < siglen)
d10948
+		{
d10948
+			msg->tsigstatus = dns_tsigerror_badtrunc;
d10948
+			tsig_log(msg->tsigkey, 2, "signature length too small");
d10948
+			ret = DNS_R_TSIGVERIFYFAILURE;
d10948
+			goto cleanup_context;
d10948
+		}
d10948
+	}
d10948
 
d10948
 	if (tsig.error != dns_rcode_noerror) {
d10948
+		msg->tsigstatus = tsig.error;
d10948
 		if (tsig.error == dns_tsigerror_badtime)
d10948
-			return (DNS_R_CLOCKSKEW);
d10948
+			ret = DNS_R_CLOCKSKEW;
d10948
 		else
d10948
-			return (DNS_R_TSIGERRORSET);
d10948
+			ret = DNS_R_TSIGERRORSET;
d10948
+		goto cleanup_context;
d10948
 	}
d10948
 
d10948
+	msg->tsigstatus = dns_rcode_noerror;
d10948
 	msg->verified_sig = 1;
d10948
-
d10948
-	return (ISC_R_SUCCESS);
d10948
+	ret = ISC_R_SUCCESS;
d10948
 
d10948
 cleanup_context:
d10948
 	if (ctx != NULL)
d10948
@@ -1503,6 +1558,8 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
d10948
 	isc_uint16_t addcount, id;
d10948
 	isc_boolean_t has_tsig = ISC_FALSE;
d10948
 	isc_mem_t *mctx;
d10948
+	unsigned int siglen;
d10948
+	unsigned int alg;
d10948
 
d10948
 	REQUIRE(source != NULL);
d10948
 	REQUIRE(msg != NULL);
d10948
@@ -1510,12 +1567,16 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
d10948
 	REQUIRE(msg->tcp_continuation == 1);
d10948
 	REQUIRE(msg->querytsig != NULL);
d10948
 
d10948
+	msg->verified_sig = 0;
d10948
+	msg->tsigstatus = dns_tsigerror_badsig;
d10948
+
d10948
 	if (!is_response(msg))
d10948
 		return (DNS_R_EXPECTEDRESPONSE);
d10948
 
d10948
 	mctx = msg->mctx;
d10948
 
d10948
 	tsigkey = dns_message_gettsigkey(msg);
d10948
+	key = tsigkey->key;
d10948
 
d10948
 	/*
d10948
 	 * Extract and parse the previous TSIG
d10948
@@ -1548,7 +1609,8 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
d10948
 		 * Do the key name and algorithm match that of the query?
d10948
 		 */
d10948
 		if (!dns_name_equal(keyname, &tsigkey->name) ||
d10948
-		    !dns_name_equal(&tsig.algorithm, &querytsig.algorithm)) {
d10948
+		    !dns_name_equal(&tsig.algorithm, &querytsig.algorithm))
d10948
+		{
d10948
 			msg->tsigstatus = dns_tsigerror_badkey;
d10948
 			ret = DNS_R_TSIGVERIFYFAILURE;
d10948
 			tsig_log(msg->tsigkey, 2,
d10948
@@ -1557,27 +1619,40 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
d10948
 		}
d10948
 
d10948
 		/*
d10948
-		 * Is the time ok?
d10948
+		 * Check digest length.
d10948
 		 */
d10948
-		isc_stdtime_get(&now;;
d10948
-
d10948
-		if (now + msg->timeadjust > tsig.timesigned + tsig.fudge) {
d10948
-			msg->tsigstatus = dns_tsigerror_badtime;
d10948
-			tsig_log(msg->tsigkey, 2, "signature has expired");
d10948
-			ret = DNS_R_CLOCKSKEW;
d10948
-			goto cleanup_querystruct;
d10948
-		} else if (now + msg->timeadjust <
d10948
-			   tsig.timesigned - tsig.fudge) {
d10948
-			msg->tsigstatus = dns_tsigerror_badtime;
d10948
-			tsig_log(msg->tsigkey, 2,
d10948
-				 "signature is in the future");
d10948
-			ret = DNS_R_CLOCKSKEW;
d10948
+		alg = dst_key_alg(key);
d10948
+		ret = dst_key_sigsize(key, &siglen);
d10948
+		if (ret != ISC_R_SUCCESS)
d10948
 			goto cleanup_querystruct;
d10948
+		if (
d10948
+#ifndef PK11_MD5_DISABLE
d10948
+			alg == DST_ALG_HMACMD5 ||
d10948
+#endif
d10948
+			alg == DST_ALG_HMACSHA1 ||
d10948
+			alg == DST_ALG_HMACSHA224 ||
d10948
+			alg == DST_ALG_HMACSHA256 ||
d10948
+			alg == DST_ALG_HMACSHA384 ||
d10948
+			alg == DST_ALG_HMACSHA512)
d10948
+		{
d10948
+			if (tsig.siglen > siglen) {
d10948
+				tsig_log(tsigkey, 2,
d10948
+					 "signature length too big");
d10948
+				ret = DNS_R_FORMERR;
d10948
+				goto cleanup_querystruct;
d10948
+			}
d10948
+			if (tsig.siglen > 0 &&
d10948
+			    (tsig.siglen < 10 ||
d10948
+			     tsig.siglen < ((siglen + 1) / 2)))
d10948
+			{
d10948
+				tsig_log(tsigkey, 2,
d10948
+					 "signature length below minimum");
d10948
+				ret = DNS_R_FORMERR;
d10948
+				goto cleanup_querystruct;
d10948
+			}
d10948
 		}
d10948
 	}
d10948
 
d10948
-	key = tsigkey->key;
d10948
-
d10948
 	if (msg->tsigctx == NULL) {
d10948
 		ret = dst_context_create3(key, mctx,
d10948
 					  DNS_LOGCATEGORY_DNSSEC,
d10948
@@ -1670,10 +1745,12 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
d10948
 		sig_r.length = tsig.siglen;
d10948
 		if (tsig.siglen == 0) {
d10948
 			if (tsig.error != dns_rcode_noerror) {
d10948
-				if (tsig.error == dns_tsigerror_badtime)
d10948
+				msg->tsigstatus = tsig.error;
d10948
+				if (tsig.error == dns_tsigerror_badtime) {
d10948
 					ret = DNS_R_CLOCKSKEW;
d10948
-				else
d10948
+				} else {
d10948
 					ret = DNS_R_TSIGERRORSET;
d10948
+				}
d10948
 			} else {
d10948
 				tsig_log(msg->tsigkey, 2,
d10948
 					 "signature is empty");
d10948
@@ -1684,29 +1761,111 @@ tsig_verify_tcp(isc_buffer_t *source, dns_message_t *msg) {
d10948
 
d10948
 		ret = dst_context_verify(msg->tsigctx, &sig_r);
d10948
 		if (ret == DST_R_VERIFYFAILURE) {
d10948
-			msg->tsigstatus = dns_tsigerror_badsig;
d10948
 			tsig_log(msg->tsigkey, 2,
d10948
 				 "signature failed to verify(2)");
d10948
 			ret = DNS_R_TSIGVERIFYFAILURE;
d10948
 			goto cleanup_context;
d10948
+		} else if (ret != ISC_R_SUCCESS) {
d10948
+			goto cleanup_context;
d10948
 		}
d10948
-		else if (ret != ISC_R_SUCCESS)
d10948
+
d10948
+		/*
d10948
+		 * Here at this point, the MAC has been verified. Even
d10948
+		 * if any of the following code returns a TSIG error,
d10948
+		 * the reply will be signed and WILL always include the
d10948
+		 * request MAC in the digest computation.
d10948
+		 */
d10948
+
d10948
+		/*
d10948
+		 * Is the time ok?
d10948
+		 */
d10948
+		isc_stdtime_get(&now;;
d10948
+
d10948
+		if (now + msg->timeadjust > tsig.timesigned + tsig.fudge) {
d10948
+			msg->tsigstatus = dns_tsigerror_badtime;
d10948
+			tsig_log(msg->tsigkey, 2, "signature has expired");
d10948
+			ret = DNS_R_CLOCKSKEW;
d10948
+			goto cleanup_context;
d10948
+		} else if (now + msg->timeadjust <
d10948
+			   tsig.timesigned - tsig.fudge)
d10948
+		{
d10948
+			msg->tsigstatus = dns_tsigerror_badtime;
d10948
+			tsig_log(msg->tsigkey, 2,
d10948
+				 "signature is in the future");
d10948
+			ret = DNS_R_CLOCKSKEW;
d10948
 			goto cleanup_context;
d10948
+		}
d10948
 
d10948
-		dst_context_destroy(&msg->tsigctx);
d10948
+		alg = dst_key_alg(key);
d10948
+		ret = dst_key_sigsize(key, &siglen);
d10948
+		if (ret != ISC_R_SUCCESS)
d10948
+			goto cleanup_context;
d10948
+		if (
d10948
+#ifndef PK11_MD5_DISABLE
d10948
+			alg == DST_ALG_HMACMD5 ||
d10948
+#endif
d10948
+			alg == DST_ALG_HMACSHA1 ||
d10948
+			alg == DST_ALG_HMACSHA224 ||
d10948
+			alg == DST_ALG_HMACSHA256 ||
d10948
+			alg == DST_ALG_HMACSHA384 ||
d10948
+			alg == DST_ALG_HMACSHA512)
d10948
+		{
d10948
+			isc_uint16_t digestbits = dst_key_getbits(key);
d10948
+
d10948
+			/*
d10948
+			 * XXXRAY: Is this correct? What is the
d10948
+			 * expected behavior when digestbits is not an
d10948
+			 * integral multiple of 8? It looks like bytes
d10948
+			 * should either be (digestbits/8) or
d10948
+			 * (digestbits+7)/8.
d10948
+			 *
d10948
+			 * In any case, for current algorithms,
d10948
+			 * digestbits are an integral multiple of 8, so
d10948
+			 * it has the same effect as (digestbits/8).
d10948
+			 */
d10948
+			if (tsig.siglen > 0 && digestbits != 0 &&
d10948
+			    tsig.siglen < ((digestbits + 1) / 8))
d10948
+			{
d10948
+				msg->tsigstatus = dns_tsigerror_badtrunc;
d10948
+				tsig_log(msg->tsigkey, 2,
d10948
+					 "truncated signature length "
d10948
+					 "too small");
d10948
+				ret = DNS_R_TSIGVERIFYFAILURE;
d10948
+				goto cleanup_context;
d10948
+			}
d10948
+			if (tsig.siglen > 0 && digestbits == 0 &&
d10948
+			    tsig.siglen < siglen)
d10948
+			{
d10948
+				msg->tsigstatus = dns_tsigerror_badtrunc;
d10948
+				tsig_log(msg->tsigkey, 2,
d10948
+					 "signature length too small");
d10948
+				ret = DNS_R_TSIGVERIFYFAILURE;
d10948
+				goto cleanup_context;
d10948
+			}
d10948
+		}
d10948
+
d10948
+		if (tsig.error != dns_rcode_noerror) {
d10948
+			msg->tsigstatus = tsig.error;
d10948
+			if (tsig.error == dns_tsigerror_badtime)
d10948
+				ret = DNS_R_CLOCKSKEW;
d10948
+			else
d10948
+				ret = DNS_R_TSIGERRORSET;
d10948
+			goto cleanup_context;
d10948
+		}
d10948
 	}
d10948
 
d10948
 	msg->tsigstatus = dns_rcode_noerror;
d10948
-	return (ISC_R_SUCCESS);
d10948
+	msg->verified_sig = 1;
d10948
+	ret = ISC_R_SUCCESS;
d10948
 
d10948
  cleanup_context:
d10948
-	dst_context_destroy(&msg->tsigctx);
d10948
+	if (msg->tsigctx != NULL)
d10948
+		dst_context_destroy(&msg->tsigctx);
d10948
 
d10948
  cleanup_querystruct:
d10948
 	dns_rdata_freestruct(&querytsig);
d10948
 
d10948
 	return (ret);
d10948
-
d10948
 }
d10948
 
d10948
 isc_result_t