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.
18 #include "port_before.h"
19 #include "fd_setsize.h"
21 #include <sys/types.h>
22 #include <sys/param.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #include <arpa/inet.h>
39 #include "port_after.h"
43 #define BOUNDS_CHECK(ptr, count) \
45 if ((ptr) + (count) > eom) { \
46 return (NS_TSIG_ERROR_FORMERR); \
53 ns_find_tsig(u_char
*msg
, u_char
*eom
) {
54 HEADER
*hp
= (HEADER
*)msg
;
56 u_char
*cp
= msg
, *start
;
58 if (msg
== NULL
|| eom
== NULL
|| msg
> eom
)
61 if (cp
+ HFIXEDSZ
>= eom
)
69 n
= ns_skiprr(cp
, eom
, ns_s_qd
, ntohs(hp
->qdcount
));
74 n
= ns_skiprr(cp
, eom
, ns_s_an
, ntohs(hp
->ancount
));
79 n
= ns_skiprr(cp
, eom
, ns_s_ns
, ntohs(hp
->nscount
));
84 n
= ns_skiprr(cp
, eom
, ns_s_ar
, ntohs(hp
->arcount
) - 1);
90 n
= dn_skipname(cp
, eom
);
94 if (cp
+ INT16SZ
>= eom
)
98 if (type
!= ns_t_tsig
)
107 *\li msg received message
108 *\li msglen length of message
109 *\li key tsig key used for verifying.
110 *\li querysig (response), the signature in the query
111 *\li querysiglen (response), the length of the signature in the query
112 *\li sig (query), a buffer to hold the signature
113 *\li siglen (query), input - length of signature buffer
114 * output - length of signature
117 *\li - bad input (-1)
118 *\li - invalid dns message (NS_TSIG_ERROR_FORMERR)
119 *\li - TSIG is not present (NS_TSIG_ERROR_NO_TSIG)
120 *\li - key doesn't match (-ns_r_badkey)
121 *\li - TSIG verification fails with BADKEY (-ns_r_badkey)
122 *\li - TSIG verification fails with BADSIG (-ns_r_badsig)
123 *\li - TSIG verification fails with BADTIME (-ns_r_badtime)
124 *\li - TSIG verification succeeds, error set to BAKEY (ns_r_badkey)
125 *\li - TSIG verification succeeds, error set to BADSIG (ns_r_badsig)
126 *\li - TSIG verification succeeds, error set to BADTIME (ns_r_badtime)
129 ns_verify(u_char
*msg
, int *msglen
, void *k
,
130 const u_char
*querysig
, int querysiglen
, u_char
*sig
, int *siglen
,
131 time_t *timesigned
, int nostrip
)
133 HEADER
*hp
= (HEADER
*)msg
;
134 DST_KEY
*key
= (DST_KEY
*)k
;
135 u_char
*cp
= msg
, *eom
;
136 char name
[MAXDNAME
], alg
[MAXDNAME
];
137 u_char
*recstart
, *rdatastart
;
138 u_char
*sigstart
, *otherstart
;
141 u_int16_t type
, length
;
142 u_int16_t fudge
, sigfieldlen
, otherfieldlen
;
145 if (msg
== NULL
|| msglen
== NULL
|| *msglen
< 0)
150 recstart
= ns_find_tsig(msg
, eom
);
151 if (recstart
== NULL
)
152 return (NS_TSIG_ERROR_NO_TSIG
);
156 /* Read the key name. */
157 n
= dn_expand(msg
, eom
, cp
, name
, MAXDNAME
);
159 return (NS_TSIG_ERROR_FORMERR
);
163 BOUNDS_CHECK(cp
, 2*INT16SZ
+ INT32SZ
+ INT16SZ
);
165 if (type
!= ns_t_tsig
)
166 return (NS_TSIG_ERROR_NO_TSIG
);
168 /* Skip the class and TTL, save the length. */
169 cp
+= INT16SZ
+ INT32SZ
;
170 GETSHORT(length
, cp
);
171 if (eom
- cp
!= length
)
172 return (NS_TSIG_ERROR_FORMERR
);
174 /* Read the algorithm name. */
176 n
= dn_expand(msg
, eom
, cp
, alg
, MAXDNAME
);
178 return (NS_TSIG_ERROR_FORMERR
);
179 if (ns_samename(alg
, NS_TSIG_ALG_HMAC_MD5
) != 1)
180 return (-ns_r_badkey
);
183 /* Read the time signed and fudge. */
184 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
186 GETLONG((*timesigned
), cp
);
189 /* Read the signature. */
190 BOUNDS_CHECK(cp
, INT16SZ
);
191 GETSHORT(sigfieldlen
, cp
);
192 BOUNDS_CHECK(cp
, sigfieldlen
);
196 /* Skip id and read error. */
197 BOUNDS_CHECK(cp
, 2*INT16SZ
);
201 /* Parse the other data. */
202 BOUNDS_CHECK(cp
, INT16SZ
);
203 GETSHORT(otherfieldlen
, cp
);
204 BOUNDS_CHECK(cp
, otherfieldlen
);
209 return (NS_TSIG_ERROR_FORMERR
);
211 /* Verify that the key used is OK. */
213 if (key
->dk_alg
!= KEY_HMAC_MD5
)
214 return (-ns_r_badkey
);
215 if (error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
216 if (ns_samename(key
->dk_key_name
, name
) != 1)
217 return (-ns_r_badkey
);
221 hp
->arcount
= htons(ntohs(hp
->arcount
) - 1);
224 * Do the verification.
227 if (key
!= NULL
&& error
!= ns_r_badsig
&& error
!= ns_r_badkey
) {
229 u_char buf
[MAXDNAME
];
230 u_char buf2
[MAXDNAME
];
232 /* Digest the query signature, if this is a response. */
233 dst_verify_data(SIG_MODE_INIT
, key
, &ctx
, NULL
, 0, NULL
, 0);
234 if (querysiglen
> 0 && querysig
!= NULL
) {
235 u_int16_t len_n
= htons(querysiglen
);
236 dst_verify_data(SIG_MODE_UPDATE
, key
, &ctx
,
237 (u_char
*)&len_n
, INT16SZ
, NULL
, 0);
238 dst_verify_data(SIG_MODE_UPDATE
, key
, &ctx
,
239 querysig
, querysiglen
, NULL
, 0);
242 /* Digest the message. */
243 dst_verify_data(SIG_MODE_UPDATE
, key
, &ctx
, msg
, recstart
- msg
,
246 /* Digest the key name. */
247 n
= ns_name_pton(name
, buf2
, sizeof(buf2
));
250 n
= ns_name_ntol(buf2
, buf
, sizeof(buf
));
253 dst_verify_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
255 /* Digest the class and TTL. */
256 dst_verify_data(SIG_MODE_UPDATE
, key
, &ctx
,
257 recstart
+ dn_skipname(recstart
, eom
) + INT16SZ
,
258 INT16SZ
+ INT32SZ
, NULL
, 0);
260 /* Digest the algorithm. */
261 n
= ns_name_pton(alg
, buf2
, sizeof(buf2
));
264 n
= ns_name_ntol(buf2
, buf
, sizeof(buf
));
267 dst_verify_data(SIG_MODE_UPDATE
, key
, &ctx
, buf
, n
, NULL
, 0);
269 /* Digest the time signed and fudge. */
270 dst_verify_data(SIG_MODE_UPDATE
, key
, &ctx
,
271 rdatastart
+ dn_skipname(rdatastart
, eom
),
272 INT16SZ
+ INT32SZ
+ INT16SZ
, NULL
, 0);
274 /* Digest the error and other data. */
275 dst_verify_data(SIG_MODE_UPDATE
, key
, &ctx
,
276 otherstart
- INT16SZ
- INT16SZ
,
277 otherfieldlen
+ INT16SZ
+ INT16SZ
, NULL
, 0);
279 n
= dst_verify_data(SIG_MODE_FINAL
, key
, &ctx
, NULL
, 0,
280 sigstart
, sigfieldlen
);
283 return (-ns_r_badsig
);
285 if (sig
!= NULL
&& siglen
!= NULL
) {
286 if (*siglen
< sigfieldlen
)
287 return (NS_TSIG_ERROR_NO_SPACE
);
288 memcpy(sig
, sigstart
, sigfieldlen
);
289 *siglen
= sigfieldlen
;
293 return (NS_TSIG_ERROR_FORMERR
);
294 if (sig
!= NULL
&& siglen
!= NULL
)
298 /* Reset the counter, since we still need to check for badtime. */
299 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
301 /* Verify the time. */
302 if (abs((*timesigned
) - time(NULL
)) > fudge
)
303 return (-ns_r_badtime
);
306 *msglen
= recstart
- msg
;
307 hp
->arcount
= htons(ntohs(hp
->arcount
) - 1);
310 if (error
!= NOERROR
)
317 ns_verify_tcp_init(void *k
, const u_char
*querysig
, int querysiglen
,
318 ns_tcp_tsig_state
*state
)
321 if (state
== NULL
|| k
== NULL
|| querysig
== NULL
|| querysiglen
< 0)
325 if (state
->key
->dk_alg
!= KEY_HMAC_MD5
)
326 return (-ns_r_badkey
);
327 if (querysiglen
> (int)sizeof(state
->sig
))
329 memcpy(state
->sig
, querysig
, querysiglen
);
330 state
->siglen
= querysiglen
;
335 ns_verify_tcp(u_char
*msg
, int *msglen
, ns_tcp_tsig_state
*state
,
338 HEADER
*hp
= (HEADER
*)msg
;
339 u_char
*recstart
, *sigstart
;
340 unsigned int sigfieldlen
, otherfieldlen
;
341 u_char
*cp
, *eom
, *cp2
;
342 char name
[MAXDNAME
], alg
[MAXDNAME
];
343 u_char buf
[MAXDNAME
];
344 int n
, type
, length
, fudge
, error
;
347 if (msg
== NULL
|| msglen
== NULL
|| state
== NULL
)
353 if (state
->counter
== 0)
354 return (ns_verify(msg
, msglen
, state
->key
,
355 state
->sig
, state
->siglen
,
356 state
->sig
, &state
->siglen
, ×igned
, 0));
358 if (state
->siglen
> 0) {
359 u_int16_t siglen_n
= htons(state
->siglen
);
361 dst_verify_data(SIG_MODE_INIT
, state
->key
, &state
->ctx
,
363 dst_verify_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
364 (u_char
*)&siglen_n
, INT16SZ
, NULL
, 0);
365 dst_verify_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
366 state
->sig
, state
->siglen
, NULL
, 0);
370 cp
= recstart
= ns_find_tsig(msg
, eom
);
372 if (recstart
== NULL
) {
374 return (NS_TSIG_ERROR_NO_TSIG
);
375 dst_verify_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
376 msg
, *msglen
, NULL
, 0);
380 hp
->arcount
= htons(ntohs(hp
->arcount
) - 1);
381 dst_verify_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
382 msg
, recstart
- msg
, NULL
, 0);
384 /* Read the key name. */
385 n
= dn_expand(msg
, eom
, cp
, name
, MAXDNAME
);
387 return (NS_TSIG_ERROR_FORMERR
);
391 BOUNDS_CHECK(cp
, 2*INT16SZ
+ INT32SZ
+ INT16SZ
);
393 if (type
!= ns_t_tsig
)
394 return (NS_TSIG_ERROR_NO_TSIG
);
396 /* Skip the class and TTL, save the length. */
397 cp
+= INT16SZ
+ INT32SZ
;
398 GETSHORT(length
, cp
);
399 if (eom
- cp
!= length
)
400 return (NS_TSIG_ERROR_FORMERR
);
402 /* Read the algorithm name. */
403 n
= dn_expand(msg
, eom
, cp
, alg
, MAXDNAME
);
405 return (NS_TSIG_ERROR_FORMERR
);
406 if (ns_samename(alg
, NS_TSIG_ALG_HMAC_MD5
) != 1)
407 return (-ns_r_badkey
);
410 /* Verify that the key used is OK. */
411 if ((ns_samename(state
->key
->dk_key_name
, name
) != 1 ||
412 state
->key
->dk_alg
!= KEY_HMAC_MD5
))
413 return (-ns_r_badkey
);
415 /* Read the time signed and fudge. */
416 BOUNDS_CHECK(cp
, INT16SZ
+ INT32SZ
+ INT16SZ
);
418 GETLONG(timesigned
, cp
);
421 /* Read the signature. */
422 BOUNDS_CHECK(cp
, INT16SZ
);
423 GETSHORT(sigfieldlen
, cp
);
424 BOUNDS_CHECK(cp
, sigfieldlen
);
428 /* Skip id and read error. */
429 BOUNDS_CHECK(cp
, 2*INT16SZ
);
433 /* Parse the other data. */
434 BOUNDS_CHECK(cp
, INT16SZ
);
435 GETSHORT(otherfieldlen
, cp
);
436 BOUNDS_CHECK(cp
, otherfieldlen
);
440 return (NS_TSIG_ERROR_FORMERR
);
443 * Do the verification.
446 /* Digest the time signed and fudge. */
448 PUTSHORT(0, cp2
); /*%< Top 16 bits of time. */
449 PUTLONG(timesigned
, cp2
);
450 PUTSHORT(NS_TSIG_FUDGE
, cp2
);
452 dst_verify_data(SIG_MODE_UPDATE
, state
->key
, &state
->ctx
,
453 buf
, cp2
- buf
, NULL
, 0);
455 n
= dst_verify_data(SIG_MODE_FINAL
, state
->key
, &state
->ctx
, NULL
, 0,
456 sigstart
, sigfieldlen
);
458 return (-ns_r_badsig
);
460 if (sigfieldlen
> sizeof(state
->sig
))
461 return (NS_TSIG_ERROR_NO_SPACE
);
463 memcpy(state
->sig
, sigstart
, sigfieldlen
);
464 state
->siglen
= sigfieldlen
;
466 /* Verify the time. */
467 if (abs(timesigned
- time(NULL
)) > fudge
)
468 return (-ns_r_badtime
);
470 *msglen
= recstart
- msg
;
472 if (error
!= NOERROR
)