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