918839
diff --git a/lib/dns/include/dns/message.h b/lib/dns/include/dns/message.h
918839
index a6862fa..d999e75 100644
918839
--- a/lib/dns/include/dns/message.h
918839
+++ b/lib/dns/include/dns/message.h
918839
@@ -210,6 +210,8 @@ struct dns_message {
918839
 	unsigned int			verify_attempted : 1;
918839
 	unsigned int			free_query : 1;
918839
 	unsigned int			free_saved : 1;
918839
+	unsigned int			tkey : 1;
918839
+	unsigned int			rdclass_set : 1;
918839
 
918839
 	unsigned int			opt_reserved;
918839
 	unsigned int			sig_reserved;
918839
@@ -1374,6 +1376,15 @@ dns_message_buildopt(dns_message_t *msg, dns_rdataset_t **opt,
918839
  * \li	 other.
918839
  */
918839
 
918839
+void
918839
+dns_message_setclass(dns_message_t *msg, dns_rdataclass_t rdclass);
918839
+/*%<
918839
+ * Set the expected class of records in the response.
918839
+ *
918839
+ * Requires:
918839
+ * \li   msg be a valid message with parsing intent.
918839
+ */
918839
+
918839
 ISC_LANG_ENDDECLS
918839
 
918839
 #endif /* DNS_MESSAGE_H */
918839
diff --git a/lib/dns/message.c b/lib/dns/message.c
918839
index 53efc5a..73def73 100644
918839
--- a/lib/dns/message.c
918839
+++ b/lib/dns/message.c
918839
@@ -436,6 +436,8 @@ msginit(dns_message_t *m) {
918839
 	m->saved.base = NULL;
918839
 	m->saved.length = 0;
918839
 	m->free_saved = 0;
918839
+	m->tkey = 0;
918839
+	m->rdclass_set = 0;
918839
 	m->querytsig = NULL;
918839
 }
918839
 
918839
@@ -1086,13 +1088,19 @@ getquestions(isc_buffer_t *source, dns_message_t *msg, dns_decompress_t *dctx,
918839
 		 * If this class is different than the one we already read,
918839
 		 * this is an error.
918839
 		 */
918839
-		if (msg->state == DNS_SECTION_ANY) {
918839
-			msg->state = DNS_SECTION_QUESTION;
918839
+		if (msg->rdclass_set == 0) {
918839
 			msg->rdclass = rdclass;
918839
+			msg->rdclass_set = 1;
918839
 		} else if (msg->rdclass != rdclass)
918839
 			DO_FORMERR;
918839
 
918839
 		/*
918839
+		 * Is this a TKEY query?
918839
+		 */
918839
+		if (rdtype == dns_rdatatype_tkey)
918839
+			msg->tkey = 1;
918839
+
918839
+		/*
918839
 		 * Can't ask the same question twice.
918839
 		 */
918839
 		result = dns_message_find(name, rdclass, rdtype, 0, NULL);
918839
@@ -1236,12 +1244,12 @@ getsection(isc_buffer_t *source, dns_message_t *msg, dns_decompress_t *dctx,
918839
 		 * If there was no question section, we may not yet have
918839
 		 * established a class.  Do so now.
918839
 		 */
918839
-		if (msg->state == DNS_SECTION_ANY &&
918839
+		if (msg->rdclass_set == 0 &&
918839
 		    rdtype != dns_rdatatype_opt &&	/* class is UDP SIZE */
918839
 		    rdtype != dns_rdatatype_tsig &&	/* class is ANY */
918839
 		    rdtype != dns_rdatatype_tkey) {	/* class is undefined */
918839
 			msg->rdclass = rdclass;
918839
-			msg->state = DNS_SECTION_QUESTION;
918839
+			msg->rdclass_set = 1;
918839
 		}
918839
 
918839
 		/*
918839
@@ -1251,7 +1259,7 @@ getsection(isc_buffer_t *source, dns_message_t *msg, dns_decompress_t *dctx,
918839
 		if (msg->opcode != dns_opcode_update
918839
 		    && rdtype != dns_rdatatype_tsig
918839
 		    && rdtype != dns_rdatatype_opt
918839
-		    && rdtype != dns_rdatatype_dnskey /* in a TKEY query */
918839
+		    && rdtype != dns_rdatatype_key /* in a TKEY query */
918839
 		    && rdtype != dns_rdatatype_sig /* SIG(0) */
918839
 		    && rdtype != dns_rdatatype_tkey /* Win2000 TKEY */
918839
 		    && msg->rdclass != dns_rdataclass_any
918839
@@ -1259,6 +1267,16 @@ getsection(isc_buffer_t *source, dns_message_t *msg, dns_decompress_t *dctx,
918839
 			DO_FORMERR;
918839
 
918839
 		/*
918839
+		 * If this is not a TKEY query/response then the KEY
918839
+		 * record's class needs to match.
918839
+		 */
918839
+		if (msg->opcode != dns_opcode_update && !msg->tkey &&
918839
+		    rdtype == dns_rdatatype_key &&
918839
+		    msg->rdclass != dns_rdataclass_any &&
918839
+		    msg->rdclass != rdclass)
918839
+			DO_FORMERR;
918839
+
918839
+		/*
918839
 		 * Special type handling for TSIG, OPT, and TKEY.
918839
 		 */
918839
 		if (rdtype == dns_rdatatype_tsig) {
918839
@@ -1372,6 +1390,10 @@ getsection(isc_buffer_t *source, dns_message_t *msg, dns_decompress_t *dctx,
918839
 				skip_name_search = ISC_TRUE;
918839
 				skip_type_search = ISC_TRUE;
918839
 				issigzero = ISC_TRUE;
918839
+			} else {
918839
+				if (msg->rdclass != dns_rdataclass_any &&
918839
+				    msg->rdclass != rdclass)
918839
+					DO_FORMERR;
918839
 			}
918839
 		} else
918839
 			covers = 0;
918839
@@ -1610,6 +1632,7 @@ dns_message_parse(dns_message_t *msg, isc_buffer_t *source,
918839
 	msg->counts[DNS_SECTION_ADDITIONAL] = isc_buffer_getuint16(source);
918839
 
918839
 	msg->header_ok = 1;
918839
+	msg->state = DNS_SECTION_QUESTION;
918839
 
918839
 	/*
918839
 	 * -1 means no EDNS.
918839
@@ -3550,3 +3573,15 @@ dns_message_buildopt(dns_message_t *message, dns_rdataset_t **rdatasetp,
918839
 		dns_message_puttemprdatalist(message, &rdatalist);
918839
 	return (result);
918839
 }
918839
+
918839
+void
918839
+dns_message_setclass(dns_message_t *msg, dns_rdataclass_t rdclass) {
918839
+
918839
+	REQUIRE(DNS_MESSAGE_VALID(msg));
918839
+	REQUIRE(msg->from_to_wire == DNS_MESSAGE_INTENTPARSE);
918839
+	REQUIRE(msg->state == DNS_SECTION_ANY);
918839
+	REQUIRE(msg->rdclass_set == 0);
918839
+
918839
+	msg->rdclass = rdclass;
918839
+	msg->rdclass_set = 1;
918839
+}
918839
diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c
918839
index aa23b11..d220986 100644
918839
--- a/lib/dns/resolver.c
918839
+++ b/lib/dns/resolver.c
918839
@@ -6964,6 +6964,8 @@ resquery_response(isc_task_t *task, isc_event_t *event) {
918839
 			goto done;
918839
 	}
918839
 
918839
+	dns_message_setclass(message, fctx->res->rdclass);
918839
+
918839
 	result = dns_message_parse(message, &devent->buffer, 0);
918839
 	if (result != ISC_R_SUCCESS) {
918839
 		switch (result) {
918839
@@ -7036,6 +7038,12 @@ resquery_response(isc_task_t *task, isc_event_t *event) {
918839
 	 */
918839
 	log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
918839
 
918839
+	if (message->rdclass != fctx->res->rdclass) {
918839
+		resend = ISC_TRUE;
918839
+		FCTXTRACE("bad class");
918839
+		goto done;
918839
+	}
918839
+
918839
 	/*
918839
 	 * Process receive opt record.
918839
 	 */
918839
diff --git a/lib/dns/xfrin.c b/lib/dns/xfrin.c
918839
index 9ad8960..938373a 100644
918839
--- a/lib/dns/xfrin.c
918839
+++ b/lib/dns/xfrin.c
918839
@@ -1241,6 +1241,8 @@ xfrin_recv_done(isc_task_t *task, isc_event_t *ev) {
918839
 	msg->tsigctx = xfr->tsigctx;
918839
 	xfr->tsigctx = NULL;
918839
 
918839
+	dns_message_setclass(msg, xfr->rdclass);
918839
+
918839
 	if (xfr->nmsg > 0)
918839
 		msg->tcp_continuation = 1;
918839