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.1.2.1 2004/03/09 09:17:37 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)
112 * msg received message
113 * msglen length of message
114 * key tsig key used for verifying.
115 * querysig (response), the signature in the query
116 * querysiglen (response), the length of the signature in the query
117 * sig (query), a buffer to hold the signature
118 * siglen (query), input - length of signature buffer
119 * output - length of signature
123 * - invalid dns message (NS_TSIG_ERROR_FORMERR)
124 * - TSIG is not present (NS_TSIG_ERROR_NO_TSIG)
125 * - key doesn't match (-ns_r_badkey)
126 * - TSIG verification fails with BADKEY (-ns_r_badkey)
127 * - TSIG verification fails with BADSIG (-ns_r_badsig)
128 * - TSIG verification fails with BADTIME (-ns_r_badtime)
129 * - TSIG verification succeeds, error set to BAKEY (ns_r_badkey)
130 * - TSIG verification succeeds, error set to BADSIG (ns_r_badsig)
131 * - TSIG verification succeeds, error set to BADTIME (ns_r_badtime)
134 ns_verify(u_char *msg, int *msglen, void *k,
135 const u_char *querysig, int querysiglen, u_char *sig, int *siglen,
136 time_t *timesigned, int nostrip)
138 HEADER *hp = (HEADER *)msg;
139 DST_KEY *key = (DST_KEY *)k;
140 u_char *cp = msg, *eom;
141 char name[MAXDNAME], alg[MAXDNAME];
142 u_char *recstart, *rdatastart;
143 u_char *sigstart, *otherstart;
146 u_int16_t type, length;
147 u_int16_t fudge, sigfieldlen, id, otherfieldlen;
150 if (msg == NULL || msglen == NULL || *msglen < 0)
155 recstart = ns_find_tsig(msg, eom);
156 if (recstart == NULL)
157 return (NS_TSIG_ERROR_NO_TSIG);
161 /* Read the key name. */
162 n = dn_expand(msg, eom, cp, name, MAXDNAME);
164 return (NS_TSIG_ERROR_FORMERR);
168 BOUNDS_CHECK(cp, 2*INT16SZ + INT32SZ + INT16SZ);
170 if (type != ns_t_tsig)
171 return (NS_TSIG_ERROR_NO_TSIG);
173 /* Skip the class and TTL, save the length. */
174 cp += INT16SZ + INT32SZ;
175 GETSHORT(length, cp);
176 if (eom - cp != length)
177 return (NS_TSIG_ERROR_FORMERR);
179 /* Read the algorithm name. */
181 n = dn_expand(msg, eom, cp, alg, MAXDNAME);
183 return (NS_TSIG_ERROR_FORMERR);
184 if (ns_samename(alg, NS_TSIG_ALG_HMAC_MD5) != 1)
185 return (-ns_r_badkey);
188 /* Read the time signed and fudge. */
189 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
191 GETLONG((*timesigned), cp);
194 /* Read the signature. */
195 BOUNDS_CHECK(cp, INT16SZ);
196 GETSHORT(sigfieldlen, cp);
197 BOUNDS_CHECK(cp, sigfieldlen);
201 /* Read the original id and error. */
202 BOUNDS_CHECK(cp, 2*INT16SZ);
206 /* Parse the other data. */
207 BOUNDS_CHECK(cp, INT16SZ);
208 GETSHORT(otherfieldlen, cp);
209 BOUNDS_CHECK(cp, otherfieldlen);
214 return (NS_TSIG_ERROR_FORMERR);
216 /* Verify that the key used is OK. */
218 if (key->dk_alg != KEY_HMAC_MD5)
219 return (-ns_r_badkey);
220 if (error != ns_r_badsig && error != ns_r_badkey) {
221 if (ns_samename(key->dk_key_name, name) != 1)
222 return (-ns_r_badkey);
226 hp->arcount = htons(ntohs(hp->arcount) - 1);
229 * Do the verification.
232 if (key != NULL && error != ns_r_badsig && error != ns_r_badkey) {
234 u_char buf[MAXDNAME];
235 u_char buf2[MAXDNAME];
237 /* Digest the query signature, if this is a response. */
238 dst_verify_data(SIG_MODE_INIT, key, &ctx, NULL, 0, NULL, 0);
239 if (querysiglen > 0 && querysig != NULL) {
240 u_int16_t len_n = htons(querysiglen);
241 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
242 (u_char *)&len_n, INT16SZ, NULL, 0);
243 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
244 querysig, querysiglen, NULL, 0);
247 /* Digest the message. */
248 dst_verify_data(SIG_MODE_UPDATE, key, &ctx, msg, recstart - msg,
251 /* Digest the key name. */
252 n = ns_name_pton(name, buf2, sizeof(buf2));
255 n = ns_name_ntol(buf2, buf, sizeof(buf));
258 dst_verify_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
260 /* Digest the class and TTL. */
261 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
262 recstart + dn_skipname(recstart, eom) + INT16SZ,
263 INT16SZ + INT32SZ, NULL, 0);
265 /* Digest the algorithm. */
266 n = ns_name_pton(alg, buf2, sizeof(buf2));
269 n = ns_name_ntol(buf2, buf, sizeof(buf));
272 dst_verify_data(SIG_MODE_UPDATE, key, &ctx, buf, n, NULL, 0);
274 /* Digest the time signed and fudge. */
275 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
276 rdatastart + dn_skipname(rdatastart, eom),
277 INT16SZ + INT32SZ + INT16SZ, NULL, 0);
279 /* Digest the error and other data. */
280 dst_verify_data(SIG_MODE_UPDATE, key, &ctx,
281 otherstart - INT16SZ - INT16SZ,
282 otherfieldlen + INT16SZ + INT16SZ, NULL, 0);
284 n = dst_verify_data(SIG_MODE_FINAL, key, &ctx, NULL, 0,
285 sigstart, sigfieldlen);
288 return (-ns_r_badsig);
290 if (sig != NULL && siglen != NULL) {
291 if (*siglen < sigfieldlen)
292 return (NS_TSIG_ERROR_NO_SPACE);
293 memcpy(sig, sigstart, sigfieldlen);
294 *siglen = sigfieldlen;
298 return (NS_TSIG_ERROR_FORMERR);
299 if (sig != NULL && siglen != NULL)
303 /* Reset the counter, since we still need to check for badtime. */
304 hp->arcount = htons(ntohs(hp->arcount) + 1);
306 /* Verify the time. */
307 if (abs((*timesigned) - time(NULL)) > fudge)
308 return (-ns_r_badtime);
311 *msglen = recstart - msg;
312 hp->arcount = htons(ntohs(hp->arcount) - 1);
315 if (error != NOERROR)
322 ns_verify_tcp_init(void *k, const u_char *querysig, int querysiglen,
323 ns_tcp_tsig_state *state)
326 if (state == NULL || k == NULL || querysig == NULL || querysiglen < 0)
330 if (state->key->dk_alg != KEY_HMAC_MD5)
331 return (-ns_r_badkey);
332 if (querysiglen > (int)sizeof(state->sig))
334 memcpy(state->sig, querysig, querysiglen);
335 state->siglen = querysiglen;
340 ns_verify_tcp(u_char *msg, int *msglen, ns_tcp_tsig_state *state,
343 HEADER *hp = (HEADER *)msg;
344 u_char *recstart, *rdatastart, *sigstart;
345 unsigned int sigfieldlen, otherfieldlen;
346 u_char *cp, *eom = msg + *msglen, *cp2;
347 char name[MAXDNAME], alg[MAXDNAME];
348 u_char buf[MAXDNAME];
349 int n, type, length, fudge, id, error;
352 if (msg == NULL || msglen == NULL || state == NULL)
356 if (state->counter == 0)
357 return (ns_verify(msg, msglen, state->key,
358 state->sig, state->siglen,
359 state->sig, &state->siglen, ×igned, 0));
361 if (state->siglen > 0) {
362 u_int16_t siglen_n = htons(state->siglen);
364 dst_verify_data(SIG_MODE_INIT, state->key, &state->ctx,
366 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
367 (u_char *)&siglen_n, INT16SZ, NULL, 0);
368 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
369 state->sig, state->siglen, NULL, 0);
373 cp = recstart = ns_find_tsig(msg, eom);
375 if (recstart == NULL) {
377 return (NS_TSIG_ERROR_NO_TSIG);
378 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
379 msg, *msglen, NULL, 0);
383 hp->arcount = htons(ntohs(hp->arcount) - 1);
384 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
385 msg, recstart - msg, NULL, 0);
387 /* Read the key name. */
388 n = dn_expand(msg, eom, cp, name, MAXDNAME);
390 return (NS_TSIG_ERROR_FORMERR);
394 BOUNDS_CHECK(cp, 2*INT16SZ + INT32SZ + INT16SZ);
396 if (type != ns_t_tsig)
397 return (NS_TSIG_ERROR_NO_TSIG);
399 /* Skip the class and TTL, save the length. */
400 cp += INT16SZ + INT32SZ;
401 GETSHORT(length, cp);
402 if (eom - cp != length)
403 return (NS_TSIG_ERROR_FORMERR);
405 /* Read the algorithm name. */
407 n = dn_expand(msg, eom, cp, alg, MAXDNAME);
409 return (NS_TSIG_ERROR_FORMERR);
410 if (ns_samename(alg, NS_TSIG_ALG_HMAC_MD5) != 1)
411 return (-ns_r_badkey);
414 /* Verify that the key used is OK. */
415 if ((ns_samename(state->key->dk_key_name, name) != 1 ||
416 state->key->dk_alg != KEY_HMAC_MD5))
417 return (-ns_r_badkey);
419 /* Read the time signed and fudge. */
420 BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ);
422 GETLONG(timesigned, cp);
425 /* Read the signature. */
426 BOUNDS_CHECK(cp, INT16SZ);
427 GETSHORT(sigfieldlen, cp);
428 BOUNDS_CHECK(cp, sigfieldlen);
432 /* Read the original id and error. */
433 BOUNDS_CHECK(cp, 2*INT16SZ);
437 /* Parse the other data. */
438 BOUNDS_CHECK(cp, INT16SZ);
439 GETSHORT(otherfieldlen, cp);
440 BOUNDS_CHECK(cp, otherfieldlen);
444 return (NS_TSIG_ERROR_FORMERR);
447 * Do the verification.
450 /* Digest the time signed and fudge. */
452 PUTSHORT(0, cp2); /* Top 16 bits of time. */
453 PUTLONG(timesigned, cp2);
454 PUTSHORT(NS_TSIG_FUDGE, cp2);
456 dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx,
457 buf, cp2 - buf, NULL, 0);
459 n = dst_verify_data(SIG_MODE_FINAL, state->key, &state->ctx, NULL, 0,
460 sigstart, sigfieldlen);
462 return (-ns_r_badsig);
464 if (sigfieldlen > sizeof(state->sig))
465 return (NS_TSIG_ERROR_NO_SPACE);
467 memcpy(state->sig, sigstart, sigfieldlen);
468 state->siglen = sigfieldlen;
470 /* Verify the time. */
471 if (abs(timesigned - time(NULL)) > fudge)
472 return (-ns_r_badtime);
474 *msglen = recstart - msg;
476 if (error != NOERROR)