2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1999 by Internet Software Consortium, Inc.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 static const char rcsid[] = "$Id: ns_verify.c,v 1.2.18.3 2006/03/10 00:20:08 marka Exp $";
24 #include "port_before.h"
25 #include "fd_setsize.h"
27 #include <sys/types.h>
28 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <arpa/inet.h>
45 #include "port_after.h"
49 #define BOUNDS_CHECK(ptr, count) \
51 if ((ptr) + (count) > eom) { \
52 return (NS_TSIG_ERROR_FORMERR); \
59 ns_find_tsig(u_char *msg, u_char *eom) {
60 HEADER *hp = (HEADER *)msg;
62 u_char *cp = msg, *start;
64 if (msg == NULL || eom == NULL || msg > eom)
67 if (cp + HFIXEDSZ >= eom)
75 n = ns_skiprr(cp, eom, ns_s_qd, ntohs(hp->qdcount));
80 n = ns_skiprr(cp, eom, ns_s_an, ntohs(hp->ancount));
85 n = ns_skiprr(cp, eom, ns_s_ns, ntohs(hp->nscount));
90 n = ns_skiprr(cp, eom, ns_s_ar, ntohs(hp->arcount) - 1);
96 n = dn_skipname(cp, eom);
100 if (cp + INT16SZ >= eom)
104 if (type != ns_t_tsig)
113 *\li msg received message
114 *\li msglen length of message
115 *\li key tsig key used for verifying.
116 *\li querysig (response), the signature in the query
117 *\li querysiglen (response), the length of the signature in the query
118 *\li sig (query), a buffer to hold the signature
119 *\li siglen (query), input - length of signature buffer
120 * output - length of signature
123 *\li - bad input (-1)
124 *\li - invalid dns message (NS_TSIG_ERROR_FORMERR)
125 *\li - TSIG is not present (NS_TSIG_ERROR_NO_TSIG)
126 *\li - key doesn't match (-ns_r_badkey)
127 *\li - TSIG verification fails with BADKEY (-ns_r_badkey)
128 *\li - TSIG verification fails with BADSIG (-ns_r_badsig)
129 *\li - TSIG verification fails with BADTIME (-ns_r_badtime)
130 *\li - TSIG verification succeeds, error set to BAKEY (ns_r_badkey)
131 *\li - TSIG verification succeeds, error set to BADSIG (ns_r_badsig)
132 *\li - TSIG verification succeeds, error set to BADTIME (ns_r_badtime)
135 ns_verify(u_char *msg, int *msglen, void *k,
136 const u_char *querysig, int querysiglen, u_char *sig, int *siglen,
137 time_t *timesigned, int nostrip)
139 HEADER *hp = (HEADER *)msg;
140 DST_KEY *key = (DST_KEY *)k;
141 u_char *cp = msg, *eom;
142 char name[MAXDNAME], alg[MAXDNAME];
143 u_char *recstart, *rdatastart;
144 u_char *sigstart, *otherstart;
147 u_int16_t type, length;
148 u_int16_t fudge, sigfieldlen, otherfieldlen;
151 if (msg == NULL || msglen == NULL || *msglen < 0)
156 recstart = ns_find_tsig(msg, eom);
157 if (recstart == NULL)
158 return (NS_TSIG_ERROR_NO_TSIG);
162 /* Read the key name. */
163 n = dn_expand(msg, eom, cp, name, MAXDNAME);
165 return (NS_TSIG_ERROR_FORMERR);
169 BOUNDS_CHECK(cp, 2*INT16SZ + INT32SZ + INT16SZ);
171 if (type != ns_t_tsig)
172 return (NS_TSIG_ERROR_NO_TSIG);
174 /* Skip the class and TTL, save the length. */
175 cp += INT16SZ + INT32SZ;
176 GETSHORT(length, cp);
177 if (eom - cp != length)
178 return (NS_TSIG_ERROR_FORMERR);
180 /* Read the algorithm name. */
182 n = dn_expand(msg, eom, cp, alg, MAXDNAME);
184 return (NS_TSIG_ERROR_FORMERR);
185 if (ns_samename(alg, NS_TSIG_ALG_HMAC_MD5) != 1)
186 return (-ns_r_badkey);
189 /* Read the time signed and fudge. */
190 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
192 GETLONG((*timesigned), cp);
195 /* Read the signature. */
196 BOUNDS_CHECK(cp, INT16SZ);
197 GETSHORT(sigfieldlen, cp);
198 BOUNDS_CHECK(cp, sigfieldlen);
202 /* Skip id and read error. */
203 BOUNDS_CHECK(cp, 2*INT16SZ);
207 /* Parse the other data. */
208 BOUNDS_CHECK(cp, INT16SZ);
209 GETSHORT(otherfieldlen, cp);
210 BOUNDS_CHECK(cp, otherfieldlen);
215 return (NS_TSIG_ERROR_FORMERR);
217 /* Verify that the key used is OK. */
219 if (key->dk_alg != KEY_HMAC_MD5)
220 return (-ns_r_badkey);
221 if (error != ns_r_badsig && error != ns_r_badkey) {
222 if (ns_samename(key->dk_key_name, name) != 1)
223 return (-ns_r_badkey);
227 hp->arcount = htons(ntohs(hp->arcount) - 1);
230 * Do the verification.
233 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
235 u_char buf[MAXDNAME];
236 u_char buf2[MAXDNAME];
238 /* Digest the query signature, if this is a response. */
239 dst_verify_data(SIG_MODE_INIT, key, &ctx, NULL, 0, NULL, 0);
240 if (querysiglen > 0 && querysig != NULL) {
241 u_int16_t len_n = htons(querysiglen);
242 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
243 (u_char *)&len_n, INT16SZ, NULL, 0);
244 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
245 querysig, querysiglen, NULL, 0);
248 /* Digest the message. */
249 dst_verify_data(SIG_MODE_UPDATE, key, &ctx, msg, recstart - msg,
252 /* Digest the key name. */
253 n = ns_name_pton(name, buf2, sizeof(buf2));
256 n = ns_name_ntol(buf2, buf, sizeof(buf));
259 dst_verify_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
261 /* Digest the class and TTL. */
262 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
263 recstart + dn_skipname(recstart, eom) + INT16SZ,
264 INT16SZ + INT32SZ, NULL, 0);
266 /* Digest the algorithm. */
267 n = ns_name_pton(alg, buf2, sizeof(buf2));
270 n = ns_name_ntol(buf2, buf, sizeof(buf));
273 dst_verify_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
275 /* Digest the time signed and fudge. */
276 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
277 rdatastart + dn_skipname(rdatastart, eom),
278 INT16SZ + INT32SZ + INT16SZ, NULL, 0);
280 /* Digest the error and other data. */
281 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
282 otherstart - INT16SZ - INT16SZ,
283 otherfieldlen + INT16SZ + INT16SZ, NULL, 0);
285 n = dst_verify_data(SIG_MODE_FINAL, key, &ctx, NULL, 0,
286 sigstart, sigfieldlen);
289 return (-ns_r_badsig);
291 if (sig != NULL && siglen != NULL) {
292 if (*siglen < sigfieldlen)
293 return (NS_TSIG_ERROR_NO_SPACE);
294 memcpy(sig, sigstart, sigfieldlen);
295 *siglen = sigfieldlen;
299 return (NS_TSIG_ERROR_FORMERR);
300 if (sig != NULL && siglen != NULL)
304 /* Reset the counter, since we still need to check for badtime. */
305 hp->arcount = htons(ntohs(hp->arcount) + 1);
307 /* Verify the time. */
308 if (abs((*timesigned) - time(NULL)) > fudge)
309 return (-ns_r_badtime);
312 *msglen = recstart - msg;
313 hp->arcount = htons(ntohs(hp->arcount) - 1);
316 if (error != NOERROR)
323 ns_verify_tcp_init(void *k, const u_char *querysig, int querysiglen,
324 ns_tcp_tsig_state *state)
327 if (state == NULL || k == NULL || querysig == NULL || querysiglen < 0)
331 if (state->key->dk_alg != KEY_HMAC_MD5)
332 return (-ns_r_badkey);
333 if (querysiglen > (int)sizeof(state->sig))
335 memcpy(state->sig, querysig, querysiglen);
336 state->siglen = querysiglen;
341 ns_verify_tcp(u_char *msg, int *msglen, ns_tcp_tsig_state *state,
344 HEADER *hp = (HEADER *)msg;
345 u_char *recstart, *sigstart;
346 unsigned int sigfieldlen, otherfieldlen;
347 u_char *cp, *eom, *cp2;
348 char name[MAXDNAME], alg[MAXDNAME];
349 u_char buf[MAXDNAME];
350 int n, type, length, fudge, error;
353 if (msg == NULL || msglen == NULL || state == NULL)
359 if (state->counter == 0)
360 return (ns_verify(msg, msglen, state->key,
361 state->sig, state->siglen,
362 state->sig, &state->siglen, ×igned, 0));
364 if (state->siglen > 0) {
365 u_int16_t siglen_n = htons(state->siglen);
367 dst_verify_data(SIG_MODE_INIT, state->key, &state->ctx,
369 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
370 (u_char *)&siglen_n, INT16SZ, NULL, 0);
371 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
372 state->sig, state->siglen, NULL, 0);
376 cp = recstart = ns_find_tsig(msg, eom);
378 if (recstart == NULL) {
380 return (NS_TSIG_ERROR_NO_TSIG);
381 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
382 msg, *msglen, NULL, 0);
386 hp->arcount = htons(ntohs(hp->arcount) - 1);
387 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
388 msg, recstart - msg, NULL, 0);
390 /* Read the key name. */
391 n = dn_expand(msg, eom, cp, name, MAXDNAME);
393 return (NS_TSIG_ERROR_FORMERR);
397 BOUNDS_CHECK(cp, 2*INT16SZ + INT32SZ + INT16SZ);
399 if (type != ns_t_tsig)
400 return (NS_TSIG_ERROR_NO_TSIG);
402 /* Skip the class and TTL, save the length. */
403 cp += INT16SZ + INT32SZ;
404 GETSHORT(length, cp);
405 if (eom - cp != length)
406 return (NS_TSIG_ERROR_FORMERR);
408 /* Read the algorithm name. */
409 n = dn_expand(msg, eom, cp, alg, MAXDNAME);
411 return (NS_TSIG_ERROR_FORMERR);
412 if (ns_samename(alg, NS_TSIG_ALG_HMAC_MD5) != 1)
413 return (-ns_r_badkey);
416 /* Verify that the key used is OK. */
417 if ((ns_samename(state->key->dk_key_name, name) != 1 ||
418 state->key->dk_alg != KEY_HMAC_MD5))
419 return (-ns_r_badkey);
421 /* Read the time signed and fudge. */
422 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
424 GETLONG(timesigned, cp);
427 /* Read the signature. */
428 BOUNDS_CHECK(cp, INT16SZ);
429 GETSHORT(sigfieldlen, cp);
430 BOUNDS_CHECK(cp, sigfieldlen);
434 /* Skip id and read error. */
435 BOUNDS_CHECK(cp, 2*INT16SZ);
439 /* Parse the other data. */
440 BOUNDS_CHECK(cp, INT16SZ);
441 GETSHORT(otherfieldlen, cp);
442 BOUNDS_CHECK(cp, otherfieldlen);
446 return (NS_TSIG_ERROR_FORMERR);
449 * Do the verification.
452 /* Digest the time signed and fudge. */
454 PUTSHORT(0, cp2); /*%< Top 16 bits of time. */
455 PUTLONG(timesigned, cp2);
456 PUTSHORT(NS_TSIG_FUDGE, cp2);
458 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
459 buf, cp2 - buf, NULL, 0);
461 n = dst_verify_data(SIG_MODE_FINAL, state->key, &state->ctx, NULL, 0,
462 sigstart, sigfieldlen);
464 return (-ns_r_badsig);
466 if (sigfieldlen > sizeof(state->sig))
467 return (NS_TSIG_ERROR_NO_SPACE);
469 memcpy(state->sig, sigstart, sigfieldlen);
470 state->siglen = sigfieldlen;
472 /* Verify the time. */
473 if (abs(timesigned - time(NULL)) > fudge)
474 return (-ns_r_badtime);
476 *msglen = recstart - msg;
478 if (error != NOERROR)