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