xref: /netbsd-src/external/bsd/libbind/dist/nameser/ns_verify.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*5bbd2a12Schristos /*	$NetBSD: ns_verify.c,v 1.1.1.2 2012/09/09 16:08:04 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos /*
4b5677b36Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos  * Copyright (c) 1999 by Internet Software Consortium, Inc.
6b5677b36Schristos  *
7b5677b36Schristos  * Permission to use, copy, modify, and distribute this software for any
8b5677b36Schristos  * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos  * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos  *
11b5677b36Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12b5677b36Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13b5677b36Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14b5677b36Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15b5677b36Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16b5677b36Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17b5677b36Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos  */
19b5677b36Schristos 
20b5677b36Schristos #ifndef lint
21b5677b36Schristos static const char rcsid[] = "Id: ns_verify.c,v 1.5 2006/03/09 23:57:56 marka Exp ";
22b5677b36Schristos #endif
23b5677b36Schristos 
24b5677b36Schristos /* Import. */
25b5677b36Schristos 
26b5677b36Schristos #include "port_before.h"
27b5677b36Schristos #include "fd_setsize.h"
28b5677b36Schristos 
29b5677b36Schristos #include <sys/types.h>
30b5677b36Schristos #include <sys/param.h>
31b5677b36Schristos 
32b5677b36Schristos #include <netinet/in.h>
33b5677b36Schristos #include <arpa/nameser.h>
34b5677b36Schristos #include <arpa/inet.h>
35b5677b36Schristos 
36b5677b36Schristos #include <errno.h>
37b5677b36Schristos #include <netdb.h>
38b5677b36Schristos #include <resolv.h>
39b5677b36Schristos #include <stdio.h>
40b5677b36Schristos #include <stdlib.h>
41b5677b36Schristos #include <string.h>
42b5677b36Schristos #include <time.h>
43b5677b36Schristos #include <unistd.h>
44b5677b36Schristos 
45b5677b36Schristos #include <isc/dst.h>
46b5677b36Schristos 
47b5677b36Schristos #include "port_after.h"
48b5677b36Schristos 
49b5677b36Schristos /* Private. */
50b5677b36Schristos 
51b5677b36Schristos #define BOUNDS_CHECK(ptr, count) \
52b5677b36Schristos 	do { \
53b5677b36Schristos 		if ((ptr) + (count) > eom) { \
54b5677b36Schristos 			return (NS_TSIG_ERROR_FORMERR); \
55b5677b36Schristos 		} \
56b5677b36Schristos 	} while (0)
57b5677b36Schristos 
58b5677b36Schristos /* Public. */
59b5677b36Schristos 
60b5677b36Schristos u_char *
ns_find_tsig(u_char * msg,u_char * eom)61b5677b36Schristos ns_find_tsig(u_char *msg, u_char *eom) {
62b5677b36Schristos 	HEADER *hp = (HEADER *)msg;
63b5677b36Schristos 	int n, type;
64b5677b36Schristos 	u_char *cp = msg, *start;
65b5677b36Schristos 
66b5677b36Schristos 	if (msg == NULL || eom == NULL || msg > eom)
67b5677b36Schristos 		return (NULL);
68b5677b36Schristos 
69b5677b36Schristos 	if (cp + HFIXEDSZ >= eom)
70b5677b36Schristos 		return (NULL);
71b5677b36Schristos 
72b5677b36Schristos 	if (hp->arcount == 0)
73b5677b36Schristos 		return (NULL);
74b5677b36Schristos 
75b5677b36Schristos 	cp += HFIXEDSZ;
76b5677b36Schristos 
77b5677b36Schristos 	n = ns_skiprr(cp, eom, ns_s_qd, ntohs(hp->qdcount));
78b5677b36Schristos 	if (n < 0)
79b5677b36Schristos 		return (NULL);
80b5677b36Schristos 	cp += n;
81b5677b36Schristos 
82b5677b36Schristos 	n = ns_skiprr(cp, eom, ns_s_an, ntohs(hp->ancount));
83b5677b36Schristos 	if (n < 0)
84b5677b36Schristos 		return (NULL);
85b5677b36Schristos 	cp += n;
86b5677b36Schristos 
87b5677b36Schristos 	n = ns_skiprr(cp, eom, ns_s_ns, ntohs(hp->nscount));
88b5677b36Schristos 	if (n < 0)
89b5677b36Schristos 		return (NULL);
90b5677b36Schristos 	cp += n;
91b5677b36Schristos 
92b5677b36Schristos 	n = ns_skiprr(cp, eom, ns_s_ar, ntohs(hp->arcount) - 1);
93b5677b36Schristos 	if (n < 0)
94b5677b36Schristos 		return (NULL);
95b5677b36Schristos 	cp += n;
96b5677b36Schristos 
97b5677b36Schristos 	start = cp;
98b5677b36Schristos 	n = dn_skipname(cp, eom);
99b5677b36Schristos 	if (n < 0)
100b5677b36Schristos 		return (NULL);
101b5677b36Schristos 	cp += n;
102b5677b36Schristos 	if (cp + INT16SZ >= eom)
103b5677b36Schristos 		return (NULL);
104b5677b36Schristos 
105b5677b36Schristos 	GETSHORT(type, cp);
106b5677b36Schristos 	if (type != ns_t_tsig)
107b5677b36Schristos 		return (NULL);
108b5677b36Schristos 	return (start);
109b5677b36Schristos }
110b5677b36Schristos 
111b5677b36Schristos /* ns_verify
112b5677b36Schristos  *
113b5677b36Schristos  * Parameters:
114b5677b36Schristos  *\li	statp		res stuff
115b5677b36Schristos  *\li	msg		received message
116b5677b36Schristos  *\li	msglen		length of message
117b5677b36Schristos  *\li	key		tsig key used for verifying.
118b5677b36Schristos  *\li	querysig	(response), the signature in the query
119b5677b36Schristos  *\li	querysiglen	(response), the length of the signature in the query
120b5677b36Schristos  *\li	sig		(query), a buffer to hold the signature
121b5677b36Schristos  *\li	siglen		(query), input - length of signature buffer
122b5677b36Schristos  *				 output - length of signature
123b5677b36Schristos  *
124b5677b36Schristos  * Errors:
125b5677b36Schristos  *\li	- bad input (-1)
126b5677b36Schristos  *\li	- invalid dns message (NS_TSIG_ERROR_FORMERR)
127b5677b36Schristos  *\li	- TSIG is not present (NS_TSIG_ERROR_NO_TSIG)
128b5677b36Schristos  *\li	- key doesn't match (-ns_r_badkey)
129b5677b36Schristos  *\li	- TSIG verification fails with BADKEY (-ns_r_badkey)
130b5677b36Schristos  *\li	- TSIG verification fails with BADSIG (-ns_r_badsig)
131b5677b36Schristos  *\li	- TSIG verification fails with BADTIME (-ns_r_badtime)
132b5677b36Schristos  *\li	- TSIG verification succeeds, error set to BAKEY (ns_r_badkey)
133b5677b36Schristos  *\li	- TSIG verification succeeds, error set to BADSIG (ns_r_badsig)
134b5677b36Schristos  *\li	- TSIG verification succeeds, error set to BADTIME (ns_r_badtime)
135b5677b36Schristos  */
136b5677b36Schristos int
ns_verify(u_char * msg,int * msglen,void * k,const u_char * querysig,int querysiglen,u_char * sig,int * siglen,time_t * timesigned,int nostrip)137b5677b36Schristos ns_verify(u_char *msg, int *msglen, void *k,
138b5677b36Schristos 	  const u_char *querysig, int querysiglen, u_char *sig, int *siglen,
139b5677b36Schristos 	  time_t *timesigned, int nostrip)
140b5677b36Schristos {
141b5677b36Schristos 	HEADER *hp = (HEADER *)msg;
142b5677b36Schristos 	DST_KEY *key = (DST_KEY *)k;
143b5677b36Schristos 	u_char *cp = msg, *eom;
144b5677b36Schristos 	char name[MAXDNAME], alg[MAXDNAME];
145b5677b36Schristos 	u_char *recstart, *rdatastart;
146b5677b36Schristos 	u_char *sigstart, *otherstart;
147b5677b36Schristos 	int n;
148b5677b36Schristos 	int error;
149b5677b36Schristos 	u_int16_t type, length;
150b5677b36Schristos 	u_int16_t fudge, sigfieldlen, otherfieldlen;
151b5677b36Schristos 
152b5677b36Schristos 	dst_init();
153b5677b36Schristos 	if (msg == NULL || msglen == NULL || *msglen < 0)
154b5677b36Schristos 		return (-1);
155b5677b36Schristos 
156b5677b36Schristos 	eom = msg + *msglen;
157b5677b36Schristos 
158b5677b36Schristos 	recstart = ns_find_tsig(msg, eom);
159b5677b36Schristos 	if (recstart == NULL)
160b5677b36Schristos 		return (NS_TSIG_ERROR_NO_TSIG);
161b5677b36Schristos 
162b5677b36Schristos 	cp = recstart;
163b5677b36Schristos 
164b5677b36Schristos 	/* Read the key name. */
165b5677b36Schristos 	n = dn_expand(msg, eom, cp, name, MAXDNAME);
166b5677b36Schristos 	if (n < 0)
167b5677b36Schristos 		return (NS_TSIG_ERROR_FORMERR);
168b5677b36Schristos 	cp += n;
169b5677b36Schristos 
170b5677b36Schristos 	/* Read the type. */
171b5677b36Schristos 	BOUNDS_CHECK(cp, 2*INT16SZ + INT32SZ + INT16SZ);
172b5677b36Schristos 	GETSHORT(type, cp);
173b5677b36Schristos 	if (type != ns_t_tsig)
174b5677b36Schristos 		return (NS_TSIG_ERROR_NO_TSIG);
175b5677b36Schristos 
176b5677b36Schristos 	/* Skip the class and TTL, save the length. */
177b5677b36Schristos 	cp += INT16SZ + INT32SZ;
178b5677b36Schristos 	GETSHORT(length, cp);
179b5677b36Schristos 	if (eom - cp != length)
180b5677b36Schristos 		return (NS_TSIG_ERROR_FORMERR);
181b5677b36Schristos 
182b5677b36Schristos 	/* Read the algorithm name. */
183b5677b36Schristos 	rdatastart = cp;
184b5677b36Schristos 	n = dn_expand(msg, eom, cp, alg, MAXDNAME);
185b5677b36Schristos 	if (n < 0)
186b5677b36Schristos 		return (NS_TSIG_ERROR_FORMERR);
187b5677b36Schristos 	if (ns_samename(alg, NS_TSIG_ALG_HMAC_MD5) != 1)
188b5677b36Schristos 		return (-ns_r_badkey);
189b5677b36Schristos 	cp += n;
190b5677b36Schristos 
191b5677b36Schristos 	/* Read the time signed and fudge. */
192b5677b36Schristos 	BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
193b5677b36Schristos 	cp += INT16SZ;
194b5677b36Schristos 	GETLONG((*timesigned), cp);
195b5677b36Schristos 	GETSHORT(fudge, cp);
196b5677b36Schristos 
197b5677b36Schristos 	/* Read the signature. */
198b5677b36Schristos 	BOUNDS_CHECK(cp, INT16SZ);
199b5677b36Schristos 	GETSHORT(sigfieldlen, cp);
200b5677b36Schristos 	BOUNDS_CHECK(cp, sigfieldlen);
201b5677b36Schristos 	sigstart = cp;
202b5677b36Schristos 	cp += sigfieldlen;
203b5677b36Schristos 
204b5677b36Schristos 	/* Skip id and read error. */
205b5677b36Schristos 	BOUNDS_CHECK(cp, 2*INT16SZ);
206b5677b36Schristos 	cp += INT16SZ;
207b5677b36Schristos 	GETSHORT(error, cp);
208b5677b36Schristos 
209b5677b36Schristos 	/* Parse the other data. */
210b5677b36Schristos 	BOUNDS_CHECK(cp, INT16SZ);
211b5677b36Schristos 	GETSHORT(otherfieldlen, cp);
212b5677b36Schristos 	BOUNDS_CHECK(cp, otherfieldlen);
213b5677b36Schristos 	otherstart = cp;
214b5677b36Schristos 	cp += otherfieldlen;
215b5677b36Schristos 
216b5677b36Schristos 	if (cp != eom)
217b5677b36Schristos 		return (NS_TSIG_ERROR_FORMERR);
218b5677b36Schristos 
219b5677b36Schristos 	/* Verify that the key used is OK. */
220b5677b36Schristos 	if (key != NULL) {
221b5677b36Schristos 		if (key->dk_alg != KEY_HMAC_MD5)
222b5677b36Schristos 			return (-ns_r_badkey);
223b5677b36Schristos 		if (error != ns_r_badsig && error != ns_r_badkey) {
224b5677b36Schristos 			if (ns_samename(key->dk_key_name, name) != 1)
225b5677b36Schristos 				return (-ns_r_badkey);
226b5677b36Schristos 		}
227b5677b36Schristos 	}
228b5677b36Schristos 
229b5677b36Schristos 	hp->arcount = htons(ntohs(hp->arcount) - 1);
230b5677b36Schristos 
231b5677b36Schristos 	/*
232b5677b36Schristos 	 * Do the verification.
233b5677b36Schristos 	 */
234b5677b36Schristos 
235b5677b36Schristos 	if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
236b5677b36Schristos 		void *ctx;
237b5677b36Schristos 		u_char buf[MAXDNAME];
238b5677b36Schristos 		u_char buf2[MAXDNAME];
239b5677b36Schristos 
240b5677b36Schristos 		/* Digest the query signature, if this is a response. */
241b5677b36Schristos 		dst_verify_data(SIG_MODE_INIT, key, &ctx, NULL, 0, NULL, 0);
242b5677b36Schristos 		if (querysiglen > 0 && querysig != NULL) {
243b5677b36Schristos 			u_int16_t len_n = htons(querysiglen);
244b5677b36Schristos 			dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
245b5677b36Schristos 					(u_char *)&len_n, INT16SZ, NULL, 0);
246b5677b36Schristos 			dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
247b5677b36Schristos 					querysig, querysiglen, NULL, 0);
248b5677b36Schristos 		}
249b5677b36Schristos 
250b5677b36Schristos  		/* Digest the message. */
251b5677b36Schristos 		dst_verify_data(SIG_MODE_UPDATE, key, &ctx, msg, recstart - msg,
252b5677b36Schristos 				NULL, 0);
253b5677b36Schristos 
254b5677b36Schristos 		/* Digest the key name. */
255b5677b36Schristos 		n = ns_name_pton(name, buf2, sizeof(buf2));
256b5677b36Schristos 		if (n < 0)
257b5677b36Schristos 			return (-1);
258b5677b36Schristos 		n = ns_name_ntol(buf2, buf, sizeof(buf));
259b5677b36Schristos 		if (n < 0)
260b5677b36Schristos 			return (-1);
261b5677b36Schristos 		dst_verify_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
262b5677b36Schristos 
263b5677b36Schristos 		/* Digest the class and TTL. */
264b5677b36Schristos 		dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
265b5677b36Schristos 				recstart + dn_skipname(recstart, eom) + INT16SZ,
266b5677b36Schristos 				INT16SZ + INT32SZ, NULL, 0);
267b5677b36Schristos 
268b5677b36Schristos 		/* Digest the algorithm. */
269b5677b36Schristos 		n = ns_name_pton(alg, buf2, sizeof(buf2));
270b5677b36Schristos 		if (n < 0)
271b5677b36Schristos 			return (-1);
272b5677b36Schristos 		n = ns_name_ntol(buf2, buf, sizeof(buf));
273b5677b36Schristos 		if (n < 0)
274b5677b36Schristos 			return (-1);
275b5677b36Schristos 		dst_verify_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
276b5677b36Schristos 
277b5677b36Schristos 		/* Digest the time signed and fudge. */
278b5677b36Schristos 		dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
279b5677b36Schristos 				rdatastart + dn_skipname(rdatastart, eom),
280b5677b36Schristos 				INT16SZ + INT32SZ + INT16SZ, NULL, 0);
281b5677b36Schristos 
282b5677b36Schristos 		/* Digest the error and other data. */
283b5677b36Schristos 		dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
284b5677b36Schristos 				otherstart - INT16SZ - INT16SZ,
285b5677b36Schristos 				otherfieldlen + INT16SZ + INT16SZ, NULL, 0);
286b5677b36Schristos 
287b5677b36Schristos 		n = dst_verify_data(SIG_MODE_FINAL, key, &ctx, NULL, 0,
288b5677b36Schristos 				    sigstart, sigfieldlen);
289b5677b36Schristos 
290b5677b36Schristos 		if (n < 0)
291b5677b36Schristos 			return (-ns_r_badsig);
292b5677b36Schristos 
293b5677b36Schristos 		if (sig != NULL && siglen != NULL) {
294b5677b36Schristos 			if (*siglen < sigfieldlen)
295b5677b36Schristos 				return (NS_TSIG_ERROR_NO_SPACE);
296b5677b36Schristos 			memcpy(sig, sigstart, sigfieldlen);
297b5677b36Schristos 			*siglen = sigfieldlen;
298b5677b36Schristos 		}
299b5677b36Schristos 	} else {
300b5677b36Schristos 		if (sigfieldlen > 0)
301b5677b36Schristos 			return (NS_TSIG_ERROR_FORMERR);
302b5677b36Schristos 		if (sig != NULL && siglen != NULL)
303b5677b36Schristos 			*siglen = 0;
304b5677b36Schristos 	}
305b5677b36Schristos 
306b5677b36Schristos 	/* Reset the counter, since we still need to check for badtime. */
307b5677b36Schristos 	hp->arcount = htons(ntohs(hp->arcount) + 1);
308b5677b36Schristos 
309b5677b36Schristos 	/* Verify the time. */
310b5677b36Schristos 	if (abs((*timesigned) - time(NULL)) > fudge)
311b5677b36Schristos 		return (-ns_r_badtime);
312b5677b36Schristos 
313b5677b36Schristos 	if (nostrip == 0) {
314b5677b36Schristos 		*msglen = recstart - msg;
315b5677b36Schristos 		hp->arcount = htons(ntohs(hp->arcount) - 1);
316b5677b36Schristos 	}
317b5677b36Schristos 
318b5677b36Schristos 	if (error != NOERROR)
319b5677b36Schristos 		return (error);
320b5677b36Schristos 
321b5677b36Schristos 	return (0);
322b5677b36Schristos }
323b5677b36Schristos 
324b5677b36Schristos int
ns_verify_tcp_init(void * k,const u_char * querysig,int querysiglen,ns_tcp_tsig_state * state)325b5677b36Schristos ns_verify_tcp_init(void *k, const u_char *querysig, int querysiglen,
326b5677b36Schristos 		   ns_tcp_tsig_state *state)
327b5677b36Schristos {
328b5677b36Schristos 	dst_init();
329b5677b36Schristos 	if (state == NULL || k == NULL || querysig == NULL || querysiglen < 0)
330b5677b36Schristos 		return (-1);
331b5677b36Schristos 	state->counter = -1;
332b5677b36Schristos 	state->key = k;
333b5677b36Schristos 	if (state->key->dk_alg != KEY_HMAC_MD5)
334b5677b36Schristos 		return (-ns_r_badkey);
335b5677b36Schristos 	if (querysiglen > (int)sizeof(state->sig))
336b5677b36Schristos 		return (-1);
337b5677b36Schristos 	memcpy(state->sig, querysig, querysiglen);
338b5677b36Schristos 	state->siglen = querysiglen;
339b5677b36Schristos 	return (0);
340b5677b36Schristos }
341b5677b36Schristos 
342b5677b36Schristos int
ns_verify_tcp(u_char * msg,int * msglen,ns_tcp_tsig_state * state,int required)343b5677b36Schristos ns_verify_tcp(u_char *msg, int *msglen, ns_tcp_tsig_state *state,
344b5677b36Schristos 	      int required)
345b5677b36Schristos {
346b5677b36Schristos 	HEADER *hp = (HEADER *)msg;
347b5677b36Schristos 	u_char *recstart, *sigstart;
348b5677b36Schristos 	unsigned int sigfieldlen, otherfieldlen;
349b5677b36Schristos 	u_char *cp, *eom, *cp2;
350b5677b36Schristos 	char name[MAXDNAME], alg[MAXDNAME];
351b5677b36Schristos 	u_char buf[MAXDNAME];
352b5677b36Schristos 	int n, type, length, fudge, error;
353b5677b36Schristos 	time_t timesigned;
354b5677b36Schristos 
355b5677b36Schristos 	if (msg == NULL || msglen == NULL || state == NULL)
356b5677b36Schristos 		return (-1);
357b5677b36Schristos 
358b5677b36Schristos 	eom = msg + *msglen;
359b5677b36Schristos 
360b5677b36Schristos 	state->counter++;
361b5677b36Schristos 	if (state->counter == 0)
362b5677b36Schristos 		return (ns_verify(msg, msglen, state->key,
363b5677b36Schristos 				  state->sig, state->siglen,
364b5677b36Schristos 				  state->sig, &state->siglen, &timesigned, 0));
365b5677b36Schristos 
366b5677b36Schristos 	if (state->siglen > 0) {
367b5677b36Schristos 		u_int16_t siglen_n = htons(state->siglen);
368b5677b36Schristos 
369b5677b36Schristos 		dst_verify_data(SIG_MODE_INIT, state->key, &state->ctx,
370b5677b36Schristos 				NULL, 0, NULL, 0);
371b5677b36Schristos 		dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
372b5677b36Schristos 				(u_char *)&siglen_n, INT16SZ, NULL, 0);
373b5677b36Schristos 		dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
374b5677b36Schristos 				state->sig, state->siglen, NULL, 0);
375b5677b36Schristos 		state->siglen = 0;
376b5677b36Schristos 	}
377b5677b36Schristos 
378b5677b36Schristos 	cp = recstart = ns_find_tsig(msg, eom);
379b5677b36Schristos 
380b5677b36Schristos 	if (recstart == NULL) {
381b5677b36Schristos 		if (required)
382b5677b36Schristos 			return (NS_TSIG_ERROR_NO_TSIG);
383b5677b36Schristos 		dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
384b5677b36Schristos 				msg, *msglen, NULL, 0);
385b5677b36Schristos 		return (0);
386b5677b36Schristos 	}
387b5677b36Schristos 
388b5677b36Schristos 	hp->arcount = htons(ntohs(hp->arcount) - 1);
389b5677b36Schristos 	dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
390b5677b36Schristos 			msg, recstart - msg, NULL, 0);
391b5677b36Schristos 
392b5677b36Schristos 	/* Read the key name. */
393b5677b36Schristos 	n = dn_expand(msg, eom, cp, name, MAXDNAME);
394b5677b36Schristos 	if (n < 0)
395b5677b36Schristos 		return (NS_TSIG_ERROR_FORMERR);
396b5677b36Schristos 	cp += n;
397b5677b36Schristos 
398b5677b36Schristos 	/* Read the type. */
399b5677b36Schristos 	BOUNDS_CHECK(cp, 2*INT16SZ + INT32SZ + INT16SZ);
400b5677b36Schristos 	GETSHORT(type, cp);
401b5677b36Schristos 	if (type != ns_t_tsig)
402b5677b36Schristos 		return (NS_TSIG_ERROR_NO_TSIG);
403b5677b36Schristos 
404b5677b36Schristos 	/* Skip the class and TTL, save the length. */
405b5677b36Schristos 	cp += INT16SZ + INT32SZ;
406b5677b36Schristos 	GETSHORT(length, cp);
407b5677b36Schristos 	if (eom - cp != length)
408b5677b36Schristos 		return (NS_TSIG_ERROR_FORMERR);
409b5677b36Schristos 
410b5677b36Schristos 	/* Read the algorithm name. */
411b5677b36Schristos 	n = dn_expand(msg, eom, cp, alg, MAXDNAME);
412b5677b36Schristos 	if (n < 0)
413b5677b36Schristos 		return (NS_TSIG_ERROR_FORMERR);
414b5677b36Schristos 	if (ns_samename(alg, NS_TSIG_ALG_HMAC_MD5) != 1)
415b5677b36Schristos 		return (-ns_r_badkey);
416b5677b36Schristos 	cp += n;
417b5677b36Schristos 
418b5677b36Schristos 	/* Verify that the key used is OK. */
419b5677b36Schristos 	if ((ns_samename(state->key->dk_key_name, name) != 1 ||
420b5677b36Schristos 	     state->key->dk_alg != KEY_HMAC_MD5))
421b5677b36Schristos 		return (-ns_r_badkey);
422b5677b36Schristos 
423b5677b36Schristos 	/* Read the time signed and fudge. */
424b5677b36Schristos 	BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
425b5677b36Schristos 	cp += INT16SZ;
426b5677b36Schristos 	GETLONG(timesigned, cp);
427b5677b36Schristos 	GETSHORT(fudge, cp);
428b5677b36Schristos 
429b5677b36Schristos 	/* Read the signature. */
430b5677b36Schristos 	BOUNDS_CHECK(cp, INT16SZ);
431b5677b36Schristos 	GETSHORT(sigfieldlen, cp);
432b5677b36Schristos 	BOUNDS_CHECK(cp, sigfieldlen);
433b5677b36Schristos 	sigstart = cp;
434b5677b36Schristos 	cp += sigfieldlen;
435b5677b36Schristos 
436b5677b36Schristos 	/* Skip id and read error. */
437b5677b36Schristos 	BOUNDS_CHECK(cp, 2*INT16SZ);
438b5677b36Schristos 	cp += INT16SZ;
439b5677b36Schristos 	GETSHORT(error, cp);
440b5677b36Schristos 
441b5677b36Schristos 	/* Parse the other data. */
442b5677b36Schristos 	BOUNDS_CHECK(cp, INT16SZ);
443b5677b36Schristos 	GETSHORT(otherfieldlen, cp);
444b5677b36Schristos 	BOUNDS_CHECK(cp, otherfieldlen);
445b5677b36Schristos 	cp += otherfieldlen;
446b5677b36Schristos 
447b5677b36Schristos 	if (cp != eom)
448b5677b36Schristos 		return (NS_TSIG_ERROR_FORMERR);
449b5677b36Schristos 
450b5677b36Schristos 	/*
451b5677b36Schristos 	 * Do the verification.
452b5677b36Schristos 	 */
453b5677b36Schristos 
454b5677b36Schristos 	/* Digest the time signed and fudge. */
455b5677b36Schristos 	cp2 = buf;
456b5677b36Schristos 	PUTSHORT(0, cp2);       /*%< Top 16 bits of time. */
457b5677b36Schristos 	PUTLONG(timesigned, cp2);
458b5677b36Schristos 	PUTSHORT(NS_TSIG_FUDGE, cp2);
459b5677b36Schristos 
460b5677b36Schristos 	dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
461b5677b36Schristos 			buf, cp2 - buf, NULL, 0);
462b5677b36Schristos 
463b5677b36Schristos 	n = dst_verify_data(SIG_MODE_FINAL, state->key, &state->ctx, NULL, 0,
464b5677b36Schristos 			    sigstart, sigfieldlen);
465b5677b36Schristos 	if (n < 0)
466b5677b36Schristos 		return (-ns_r_badsig);
467b5677b36Schristos 
468b5677b36Schristos 	if (sigfieldlen > sizeof(state->sig))
469b5677b36Schristos 		return (NS_TSIG_ERROR_NO_SPACE);
470b5677b36Schristos 
471b5677b36Schristos 	memcpy(state->sig, sigstart, sigfieldlen);
472b5677b36Schristos 	state->siglen = sigfieldlen;
473b5677b36Schristos 
474b5677b36Schristos 	/* Verify the time. */
475b5677b36Schristos 	if (abs(timesigned - time(NULL)) > fudge)
476b5677b36Schristos 		return (-ns_r_badtime);
477b5677b36Schristos 
478b5677b36Schristos 	*msglen = recstart - msg;
479b5677b36Schristos 
480b5677b36Schristos 	if (error != NOERROR)
481b5677b36Schristos 		return (error);
482b5677b36Schristos 
483b5677b36Schristos 	return (0);
484b5677b36Schristos }
485b5677b36Schristos 
486b5677b36Schristos /*! \file */
487