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