]> git.ipfire.org Git - people/ms/dnsmasq.git/blame - src/dnssec.c
Fix FTBFS with Nettle-3.0.
[people/ms/dnsmasq.git] / src / dnssec.c
CommitLineData
8d41ebd8 1/* dnssec.c is Copyright (c) 2012 Giovanni Bajo <rasky@develer.com>
0fc2f313 2 and Copyright (c) 2012-2014 Simon Kelley
8d41ebd8
GB
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 dated June, 1991, or
7 (at your option) version 3 dated 29 June, 2007.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
e292e93d
GB
17
18#include "dnsmasq.h"
0fc2f313
SK
19
20#ifdef HAVE_DNSSEC
21
86bec2d3
SK
22#include <nettle/rsa.h>
23#include <nettle/dsa.h>
c152dc84
SK
24#ifndef NO_NETTLE_ECC
25# include <nettle/ecdsa.h>
26# include <nettle/ecc-curve.h>
27#endif
86bec2d3 28#include <nettle/nettle-meta.h>
063efb33
SK
29#include <nettle/bignum.h>
30
cdb755c5
SK
31/* Nettle-3.0 moved to a new API for DSA. We use a name that's defined in the new API
32 to detect Nettle-3, and invoke the backwards compatibility mode. */
33#ifdef dsa_params_init
34#include <nettle/dsa-compat.h>
35#endif
36
c3e0b9b6 37
e292e93d
GB
38#define SERIAL_UNDEF -100
39#define SERIAL_EQ 0
40#define SERIAL_LT -1
41#define SERIAL_GT 1
42
86bec2d3
SK
43/* http://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
44static char *ds_digest_name(int digest)
45{
46 switch (digest)
47 {
48 case 1: return "sha1";
49 case 2: return "sha256";
50 case 3: return "gosthash94";
51 case 4: return "sha384";
52 default: return NULL;
53 }
54}
55
56/* http://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
57static char *algo_digest_name(int algo)
58{
59 switch (algo)
60 {
61 case 1: return "md5";
62 case 3: return "sha1";
63 case 5: return "sha1";
64 case 6: return "sha1";
65 case 7: return "sha1";
66 case 8: return "sha256";
67 case 10: return "sha512";
68 case 12: return "gosthash94";
69 case 13: return "sha256";
70 case 14: return "sha384";
71 default: return NULL;
72 }
73}
74
75/* Find pointer to correct hash function in nettle library */
76static const struct nettle_hash *hash_find(char *name)
77{
78 int i;
79
80 if (!name)
81 return NULL;
82
83 for (i = 0; nettle_hashes[i]; i++)
84 {
85 if (strcmp(nettle_hashes[i]->name, name) == 0)
86 return nettle_hashes[i];
87 }
88
89 return NULL;
90}
91
92/* expand ctx and digest memory allocations if necessary and init hash function */
93static int hash_init(const struct nettle_hash *hash, void **ctxp, unsigned char **digestp)
94{
95 static void *ctx = NULL;
96 static unsigned char *digest = NULL;
97 static unsigned int ctx_sz = 0;
98 static unsigned int digest_sz = 0;
99
100 void *new;
101
102 if (ctx_sz < hash->context_size)
103 {
104 if (!(new = whine_malloc(hash->context_size)))
105 return 0;
106 if (ctx)
107 free(ctx);
108 ctx = new;
109 ctx_sz = hash->context_size;
110 }
111
112 if (digest_sz < hash->digest_size)
113 {
114 if (!(new = whine_malloc(hash->digest_size)))
115 return 0;
116 if (digest)
117 free(digest);
118 digest = new;
119 digest_sz = hash->digest_size;
120 }
121
122 *ctxp = ctx;
123 *digestp = digest;
124
125 hash->init(ctx);
126
127 return 1;
128}
129
cdb755c5
SK
130static int dnsmasq_rsa_verify(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
131 unsigned char *digest, int algo)
86bec2d3
SK
132{
133 unsigned char *p;
134 size_t exp_len;
135
136 static struct rsa_public_key *key = NULL;
137 static mpz_t sig_mpz;
138
139 if (key == NULL)
140 {
141 if (!(key = whine_malloc(sizeof(struct rsa_public_key))))
142 return 0;
143
144 nettle_rsa_public_key_init(key);
145 mpz_init(sig_mpz);
146 }
147
148 if ((key_len < 3) || !(p = blockdata_retrieve(key_data, key_len, NULL)))
149 return 0;
150
151 key_len--;
152 if ((exp_len = *p++) == 0)
153 {
154 GETSHORT(exp_len, p);
155 key_len -= 2;
156 }
157
158 if (exp_len >= key_len)
159 return 0;
160
161 key->size = key_len - exp_len;
162 mpz_import(key->e, exp_len, 1, 1, 0, 0, p);
163 mpz_import(key->n, key->size, 1, 1, 0, 0, p + exp_len);
164
165 mpz_import(sig_mpz, sig_len, 1, 1, 0, 0, sig);
166
167 switch (algo)
168 {
169 case 1:
170 return nettle_rsa_md5_verify_digest(key, digest, sig_mpz);
171 case 5: case 7:
172 return nettle_rsa_sha1_verify_digest(key, digest, sig_mpz);
173 case 8:
174 return nettle_rsa_sha256_verify_digest(key, digest, sig_mpz);
175 case 10:
176 return nettle_rsa_sha512_verify_digest(key, digest, sig_mpz);
177 }
178
179 return 0;
180}
181
cdb755c5
SK
182static int dnsmasq_dsa_verify(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
183 unsigned char *digest, int algo)
86bec2d3
SK
184{
185 unsigned char *p;
186 unsigned int t;
187
188 static struct dsa_public_key *key = NULL;
189 static struct dsa_signature *sig_struct;
190
191 if (key == NULL)
192 {
193 if (!(sig_struct = whine_malloc(sizeof(struct dsa_signature))) ||
194 !(key = whine_malloc(sizeof(struct dsa_public_key))))
195 return 0;
196
197 nettle_dsa_public_key_init(key);
198 nettle_dsa_signature_init(sig_struct);
199 }
200
201 if ((sig_len < 41) || !(p = blockdata_retrieve(key_data, key_len, NULL)))
202 return 0;
203
204 t = *p++;
205
206 if (key_len < (213 + (t * 24)))
207 return 0;
ebe95a83 208
86bec2d3
SK
209 mpz_import(key->q, 20, 1, 1, 0, 0, p); p += 20;
210 mpz_import(key->p, 64 + (t*8), 1, 1, 0, 0, p); p += 64 + (t*8);
211 mpz_import(key->g, 64 + (t*8), 1, 1, 0, 0, p); p += 64 + (t*8);
212 mpz_import(key->y, 64 + (t*8), 1, 1, 0, 0, p); p += 64 + (t*8);
213
214 mpz_import(sig_struct->r, 20, 1, 1, 0, 0, sig+1);
215 mpz_import(sig_struct->s, 20, 1, 1, 0, 0, sig+21);
216
217 (void)algo;
ebe95a83 218
86bec2d3
SK
219 return nettle_dsa_sha1_verify_digest(key, digest, sig_struct);
220}
221
c152dc84
SK
222#ifndef NO_NETTLE_ECC
223static int dnsmasq_ecdsa_verify(struct blockdata *key_data, unsigned int key_len,
224 unsigned char *sig, size_t sig_len,
ebe95a83
SK
225 unsigned char *digest, size_t digest_len, int algo)
226{
227 unsigned char *p;
228 unsigned int t;
229 struct ecc_point *key;
230
231 static struct ecc_point *key_256 = NULL, *key_384 = NULL;
232 static mpz_t x, y;
233 static struct dsa_signature *sig_struct;
234
235 if (!sig_struct)
236 {
237 if (!(sig_struct = whine_malloc(sizeof(struct dsa_signature))))
238 return 0;
239
240 nettle_dsa_signature_init(sig_struct);
241 mpz_init(x);
242 mpz_init(y);
243 }
244
245 switch (algo)
246 {
247 case 13:
248 if (!key_256)
249 {
250 if (!(key_256 = whine_malloc(sizeof(struct ecc_point))))
251 return 0;
252
253 nettle_ecc_point_init(key_256, &nettle_secp_256r1);
254 }
255
256 key = key_256;
257 t = 32;
258 break;
259
260 case 14:
261 if (!key_384)
262 {
263 if (!(key_384 = whine_malloc(sizeof(struct ecc_point))))
264 return 0;
265
266 nettle_ecc_point_init(key_384, &nettle_secp_384r1);
267 }
268
269 key = key_384;
270 t = 48;
271 break;
272
273 default:
274 return 0;
275 }
276
277 if (sig_len != 2*t || key_len != 2*t ||
278 (p = blockdata_retrieve(key_data, key_len, NULL)))
279 return 0;
280
281 mpz_import(x, t , 1, 1, 0, 0, p);
282 mpz_import(y, t , 1, 1, 0, 0, p + t);
283
284 if (!ecc_point_set(key, x, y))
285 return 0;
286
287 mpz_import(sig_struct->r, t, 1, 1, 0, 0, sig);
288 mpz_import(sig_struct->s, t, 1, 1, 0, 0, sig + t);
289
290 return nettle_ecdsa_verify(key, digest_len, digest, sig_struct);
291}
c152dc84
SK
292#endif
293
86bec2d3 294static int verify(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
ebe95a83 295 unsigned char *digest, size_t digest_len, int algo)
86bec2d3 296{
7b1eae4f
SK
297 (void)digest_len;
298
86bec2d3
SK
299 switch (algo)
300 {
301 case 1: case 5: case 7: case 8: case 10:
cdb755c5 302 return dnsmasq_rsa_verify(key_data, key_len, sig, sig_len, digest, algo);
86bec2d3
SK
303
304 case 3: case 6:
cdb755c5 305 return dnsmasq_dsa_verify(key_data, key_len, sig, sig_len, digest, algo);
c152dc84
SK
306
307#ifndef NO_NETTLE_ECC
ebe95a83
SK
308 case 13: case 14:
309 return dnsmasq_ecdsa_verify(key_data, key_len, sig, sig_len, digest, digest_len, algo);
c152dc84
SK
310#endif
311 }
86bec2d3
SK
312
313 return 0;
314}
315
0fc2f313
SK
316/* Convert from presentation format to wire format, in place.
317 Also map UC -> LC.
318 Note that using extract_name to get presentation format
319 then calling to_wire() removes compression and maps case,
320 thus generating names in canonical form.
321 Calling to_wire followed by from_wire is almost an identity,
322 except that the UC remains mapped to LC.
323*/
324static int to_wire(char *name)
7f0485cf 325{
0fc2f313
SK
326 unsigned char *l, *p, term;
327 int len;
328
329 for (l = (unsigned char*)name; *l != 0; l = p)
330 {
331 for (p = l; *p != '.' && *p != 0; p++)
332 if (*p >= 'A' && *p <= 'Z')
333 *p = *p - 'A' + 'a';
334
335 term = *p;
336
337 if ((len = p - l) != 0)
338 memmove(l+1, l, len);
339 *l = len;
340
341 p++;
342
343 if (term == 0)
344 *p = 0;
345 }
346
347 return l + 1 - (unsigned char *)name;
7f0485cf
GB
348}
349
0fc2f313
SK
350/* Note: no compression allowed in input. */
351static void from_wire(char *name)
13e435eb 352{
0fc2f313
SK
353 unsigned char *l;
354 int len;
13e435eb 355
0fc2f313 356 for (l = (unsigned char *)name; *l != 0; l += len+1)
13e435eb 357 {
0fc2f313
SK
358 len = *l;
359 memmove(l, l+1, len);
360 l[len] = '.';
13e435eb 361 }
7f0485cf 362
e3f14558 363 if ((char *)l != name)
bd9b3cf5 364 *(l-1) = 0;
13e435eb
GB
365}
366
5ada8885
SK
367/* Input in presentation format */
368static int count_labels(char *name)
369{
370 int i;
371
372 if (*name == 0)
373 return 0;
374
375 for (i = 0; *name; name++)
376 if (*name == '.')
377 i++;
378
379 return i+1;
380}
381
5f8e58f4
SK
382/* Implement RFC1982 wrapped compare for 32-bit numbers */
383static int serial_compare_32(unsigned long s1, unsigned long s2)
384{
385 if (s1 == s2)
386 return SERIAL_EQ;
0ca895f5 387
5f8e58f4
SK
388 if ((s1 < s2 && (s2 - s1) < (1UL<<31)) ||
389 (s1 > s2 && (s1 - s2) > (1UL<<31)))
390 return SERIAL_LT;
391 if ((s1 < s2 && (s2 - s1) > (1UL<<31)) ||
392 (s1 > s2 && (s1 - s2) < (1UL<<31)))
393 return SERIAL_GT;
394 return SERIAL_UNDEF;
395}
0852d76b 396
5f8e58f4
SK
397/* Check whether today/now is between date_start and date_end */
398static int check_date_range(unsigned long date_start, unsigned long date_end)
0852d76b 399{
e98bd52e
SK
400 unsigned long curtime;
401
402 /* Checking timestamps may be temporarily disabled */
403 if (option_bool(OPT_DNSSEC_TIME))
404 return 1;
405
406 curtime = time(0);
5f8e58f4
SK
407
408 /* We must explicitly check against wanted values, because of SERIAL_UNDEF */
409 return serial_compare_32(curtime, date_start) == SERIAL_GT
410 && serial_compare_32(curtime, date_end) == SERIAL_LT;
411}
f119ed38 412
5f8e58f4
SK
413static u16 *get_desc(int type)
414{
415 /* List of RRtypes which include domains in the data.
416 0 -> domain
417 integer -> no of plain bytes
418 -1 -> end
419
420 zero is not a valid RRtype, so the final entry is returned for
421 anything which needs no mangling.
422 */
423
424 static u16 rr_desc[] =
425 {
426 T_NS, 0, -1,
427 T_MD, 0, -1,
428 T_MF, 0, -1,
429 T_CNAME, 0, -1,
430 T_SOA, 0, 0, -1,
431 T_MB, 0, -1,
432 T_MG, 0, -1,
433 T_MR, 0, -1,
434 T_PTR, 0, -1,
435 T_MINFO, 0, 0, -1,
436 T_MX, 2, 0, -1,
437 T_RP, 0, 0, -1,
438 T_AFSDB, 2, 0, -1,
439 T_RT, 2, 0, -1,
440 T_SIG, 18, 0, -1,
441 T_PX, 2, 0, 0, -1,
442 T_NXT, 0, -1,
443 T_KX, 2, 0, -1,
444 T_SRV, 6, 0, -1,
445 T_DNAME, 0, -1,
5f8e58f4
SK
446 0, -1 /* wildcard/catchall */
447 };
448
449 u16 *p = rr_desc;
450
451 while (*p != type && *p != 0)
452 while (*p++ != (u16)-1);
f119ed38 453
5f8e58f4
SK
454 return p+1;
455}
0852d76b 456
5f8e58f4
SK
457/* Return bytes of canonicalised rdata, when the return value is zero, the remaining
458 data, pointed to by *p, should be used raw. */
459static int get_rdata(struct dns_header *header, size_t plen, unsigned char *end, char *buff,
460 unsigned char **p, u16 **desc)
461{
462 int d = **desc;
463
464 (*desc)++;
465
466 /* No more data needs mangling */
467 if (d == (u16)-1)
468 return 0;
469
470 if (d == 0 && extract_name(header, plen, p, buff, 1, 0))
471 /* domain-name, canonicalise */
472 return to_wire(buff);
473 else
474 {
475 /* plain data preceding a domain-name, don't run off the end of the data */
476 if ((end - *p) < d)
477 d = end - *p;
478
479 if (d != 0)
480 {
481 memcpy(buff, *p, d);
482 *p += d;
483 }
484
485 return d;
486 }
0852d76b
GB
487}
488
613ad15d
SK
489static int expand_workspace(unsigned char ***wkspc, int *sz, int new)
490{
491 unsigned char **p;
492 int new_sz = *sz;
493
494 if (new_sz > new)
495 return 1;
496
497 if (new >= 100)
498 return 0;
499
500 new_sz += 5;
501
502 if (!(p = whine_malloc((new_sz) * sizeof(unsigned char **))))
503 return 0;
504
505 if (*wkspc)
506 {
507 memcpy(p, *wkspc, *sz * sizeof(unsigned char **));
508 free(*wkspc);
509 }
510
511 *wkspc = p;
512 *sz = new_sz;
00a5b5d4
SK
513
514 return 1;
613ad15d
SK
515}
516
5f8e58f4
SK
517/* Bubble sort the RRset into the canonical order.
518 Note that the byte-streams from two RRs may get unsynced: consider
519 RRs which have two domain-names at the start and then other data.
520 The domain-names may have different lengths in each RR, but sort equal
521
522 ------------
523 |abcde|fghi|
524 ------------
525 |abcd|efghi|
526 ------------
527
528 leaving the following bytes as deciding the order. Hence the nasty left1 and left2 variables.
529*/
530
531static void sort_rrset(struct dns_header *header, size_t plen, u16 *rr_desc, int rrsetidx,
532 unsigned char **rrset, char *buff1, char *buff2)
c3e0b9b6 533{
5f8e58f4 534 int swap, quit, i;
0fc2f313 535
5f8e58f4
SK
536 do
537 {
538 for (swap = 0, i = 0; i < rrsetidx-1; i++)
539 {
540 int rdlen1, rdlen2, left1, left2, len1, len2, len, rc;
541 u16 *dp1, *dp2;
542 unsigned char *end1, *end2;
5107ace1
SK
543 /* Note that these have been determined to be OK previously,
544 so we don't need to check for NULL return here. */
5f8e58f4
SK
545 unsigned char *p1 = skip_name(rrset[i], header, plen, 10);
546 unsigned char *p2 = skip_name(rrset[i+1], header, plen, 10);
547
548 p1 += 8; /* skip class, type, ttl */
549 GETSHORT(rdlen1, p1);
550 end1 = p1 + rdlen1;
551
552 p2 += 8; /* skip class, type, ttl */
553 GETSHORT(rdlen2, p2);
554 end2 = p2 + rdlen2;
555
556 dp1 = dp2 = rr_desc;
557
1486a9c7 558 for (quit = 0, left1 = 0, left2 = 0, len1 = 0, len2 = 0; !quit;)
5f8e58f4 559 {
1486a9c7
SK
560 if (left1 != 0)
561 memmove(buff1, buff1 + len1 - left1, left1);
562
5f8e58f4
SK
563 if ((len1 = get_rdata(header, plen, end1, buff1 + left1, &p1, &dp1)) == 0)
564 {
565 quit = 1;
566 len1 = end1 - p1;
567 memcpy(buff1 + left1, p1, len1);
568 }
569 len1 += left1;
570
1486a9c7
SK
571 if (left2 != 0)
572 memmove(buff2, buff2 + len2 - left2, left2);
573
5f8e58f4
SK
574 if ((len2 = get_rdata(header, plen, end2, buff2 + left2, &p2, &dp2)) == 0)
575 {
576 quit = 1;
577 len2 = end2 - p2;
578 memcpy(buff2 + left2, p2, len2);
579 }
580 len2 += left2;
581
582 if (len1 > len2)
1486a9c7 583 left1 = len1 - len2, left2 = 0, len = len2;
5f8e58f4 584 else
1486a9c7 585 left2 = len2 - len1, left1 = 0, len = len1;
5f8e58f4 586
6fd6dacb 587 rc = (len == 0) ? 0 : memcmp(buff1, buff2, len);
5f8e58f4 588
4619d946 589 if (rc > 0 || (rc == 0 && quit && len1 > len2))
5f8e58f4
SK
590 {
591 unsigned char *tmp = rrset[i+1];
592 rrset[i+1] = rrset[i];
593 rrset[i] = tmp;
594 swap = quit = 1;
595 }
6fd6dacb
SK
596 else if (rc < 0)
597 quit = 1;
5f8e58f4
SK
598 }
599 }
600 } while (swap);
601}
c3e0b9b6 602
5f8e58f4
SK
603/* Validate a single RRset (class, type, name) in the supplied DNS reply
604 Return code:
605 STAT_SECURE if it validates.
5107ace1 606 STAT_SECURE_WILDCARD if it validates and is the result of wildcard expansion.
00a5b5d4 607 STAT_NO_SIG no RRsigs found.
87070192
SK
608 STAT_INSECURE RRset empty.
609 STAT_BOGUS signature is wrong, bad packet.
5f8e58f4
SK
610 STAT_NEED_KEY need DNSKEY to complete validation (name is returned in keyname)
611
612 if key is non-NULL, use that key, which has the algo and tag given in the params of those names,
613 otherwise find the key in the cache.
5107ace1
SK
614
615 name is unchanged on exit. keyname is used as workspace and trashed.
5f8e58f4
SK
616*/
617static int validate_rrset(time_t now, struct dns_header *header, size_t plen, int class,
e7829aef 618 int type, char *name, char *keyname, struct blockdata *key, int keylen, int algo_in, int keytag_in)
5f8e58f4
SK
619{
620 static unsigned char **rrset = NULL, **sigs = NULL;
621 static int rrset_sz = 0, sig_sz = 0;
0fc2f313 622
5f8e58f4 623 unsigned char *p;
5ada8885 624 int rrsetidx, sigidx, res, rdlen, j, name_labels;
5f8e58f4
SK
625 struct crec *crecp = NULL;
626 int type_covered, algo, labels, orig_ttl, sig_expiration, sig_inception, key_tag;
627 u16 *rr_desc = get_desc(type);
c3e0b9b6 628
5f8e58f4 629 if (!(p = skip_questions(header, plen)))
87070192 630 return STAT_BOGUS;
c3e0b9b6 631
5ada8885
SK
632 name_labels = count_labels(name); /* For 4035 5.3.2 check */
633
634 /* look for RRSIGs for this RRset and get pointers to each RR in the set. */
5f8e58f4
SK
635 for (rrsetidx = 0, sigidx = 0, j = ntohs(header->ancount) + ntohs(header->nscount);
636 j != 0; j--)
637 {
638 unsigned char *pstart, *pdata;
b98d22c1 639 int stype, sclass;
c3e0b9b6 640
5f8e58f4
SK
641 pstart = p;
642
643 if (!(res = extract_name(header, plen, &p, name, 0, 10)))
87070192 644 return STAT_BOGUS; /* bad packet */
5f8e58f4
SK
645
646 GETSHORT(stype, p);
647 GETSHORT(sclass, p);
b98d22c1 648 p += 4; /* TTL */
5f8e58f4
SK
649
650 pdata = p;
c3e0b9b6 651
5f8e58f4
SK
652 GETSHORT(rdlen, p);
653
e7829aef 654 if (!CHECK_LEN(header, p, plen, rdlen))
87070192 655 return STAT_BOGUS;
e7829aef 656
5f8e58f4
SK
657 if (res == 1 && sclass == class)
658 {
659 if (stype == type)
660 {
613ad15d 661 if (!expand_workspace(&rrset, &rrset_sz, rrsetidx))
87070192 662 return STAT_BOGUS;
613ad15d 663
5f8e58f4
SK
664 rrset[rrsetidx++] = pstart;
665 }
666
667 if (stype == T_RRSIG)
668 {
613ad15d 669 if (rdlen < 18)
87070192 670 return STAT_BOGUS; /* bad packet */
613ad15d
SK
671
672 GETSHORT(type_covered, p);
673
674 if (type_covered == type)
675 {
676 if (!expand_workspace(&sigs, &sig_sz, sigidx))
87070192 677 return STAT_BOGUS;
613ad15d
SK
678
679 sigs[sigidx++] = pdata;
680 }
681
682 p = pdata + 2; /* restore for ADD_RDLEN */
5f8e58f4
SK
683 }
684 }
613ad15d 685
5f8e58f4 686 if (!ADD_RDLEN(header, p, plen, rdlen))
87070192 687 return STAT_BOGUS;
5f8e58f4 688 }
c3e0b9b6 689
00a5b5d4
SK
690 /* RRset empty */
691 if (rrsetidx == 0)
5f8e58f4 692 return STAT_INSECURE;
00a5b5d4
SK
693
694 /* no RRSIGs */
695 if (sigidx == 0)
696 return STAT_NO_SIG;
5f8e58f4
SK
697
698 /* Sort RRset records into canonical order.
d387380a 699 Note that at this point keyname and daemon->workspacename buffs are
5f8e58f4 700 unused, and used as workspace by the sort. */
d387380a 701 sort_rrset(header, plen, rr_desc, rrsetidx, rrset, daemon->workspacename, keyname);
5f8e58f4
SK
702
703 /* Now try all the sigs to try and find one which validates */
704 for (j = 0; j <sigidx; j++)
705 {
d387380a 706 unsigned char *psav, *sig, *digest;
86bec2d3
SK
707 int i, wire_len, sig_len;
708 const struct nettle_hash *hash;
709 void *ctx;
d387380a 710 char *name_start;
5f8e58f4
SK
711 u32 nsigttl;
712
713 p = sigs[j];
5ada8885 714 GETSHORT(rdlen, p); /* rdlen >= 18 checked previously */
5f8e58f4
SK
715 psav = p;
716
5ada8885 717 p += 2; /* type_covered - already checked */
5f8e58f4
SK
718 algo = *p++;
719 labels = *p++;
720 GETLONG(orig_ttl, p);
e7829aef
SK
721 GETLONG(sig_expiration, p);
722 GETLONG(sig_inception, p);
5f8e58f4
SK
723 GETSHORT(key_tag, p);
724
5f8e58f4 725 if (!extract_name(header, plen, &p, keyname, 1, 0))
87070192 726 return STAT_BOGUS;
d387380a
SK
727
728 /* RFC 4035 5.3.1 says that the Signer's Name field MUST equal
729 the name of the zone containing the RRset. We can't tell that
730 for certain, but we can check that the RRset name is equal to
731 or encloses the signers name, which should be enough to stop
732 an attacker using signatures made with the key of an unrelated
733 zone he controls. Note that the root key is always allowed. */
734 if (*keyname != 0)
735 {
736 int failed = 0;
737
738 for (name_start = name; !hostname_isequal(name_start, keyname); )
739 if ((name_start = strchr(name_start, '.')))
740 name_start++; /* chop a label off and try again */
741 else
742 {
743 failed = 1;
744 break;
745 }
746
747 /* Bad sig, try another */
748 if (failed)
749 continue;
750 }
5f8e58f4 751
d387380a 752 /* Other 5.3.1 checks */
e7829aef
SK
753 if (!check_date_range(sig_inception, sig_expiration) ||
754 labels > name_labels ||
755 !(hash = hash_find(algo_digest_name(algo))) ||
756 !hash_init(hash, &ctx, &digest))
757 continue;
758
5f8e58f4
SK
759 /* OK, we have the signature record, see if the relevant DNSKEY is in the cache. */
760 if (!key && !(crecp = cache_find_by_name(NULL, keyname, now, F_DNSKEY)))
761 return STAT_NEED_KEY;
762
86bec2d3
SK
763 sig = p;
764 sig_len = rdlen - (p - psav);
e7829aef 765
5f8e58f4
SK
766 nsigttl = htonl(orig_ttl);
767
86bec2d3 768 hash->update(ctx, 18, psav);
5f8e58f4 769 wire_len = to_wire(keyname);
86bec2d3 770 hash->update(ctx, (unsigned int)wire_len, (unsigned char*)keyname);
5f8e58f4
SK
771 from_wire(keyname);
772
5f8e58f4
SK
773 for (i = 0; i < rrsetidx; ++i)
774 {
775 int seg;
776 unsigned char *end, *cp;
777 u16 len, *dp;
d387380a 778
5f8e58f4
SK
779 p = rrset[i];
780 if (!extract_name(header, plen, &p, name, 1, 10))
87070192 781 return STAT_BOGUS;
5ada8885 782
d387380a
SK
783 name_start = name;
784
5ada8885
SK
785 /* if more labels than in RRsig name, hash *.<no labels in rrsig labels field> 4035 5.3.2 */
786 if (labels < name_labels)
787 {
788 int k;
789 for (k = name_labels - labels; k != 0; k--)
790 while (*name_start != '.' && *name_start != 0)
791 name_start++;
792 name_start--;
793 *name_start = '*';
794 }
795
796 wire_len = to_wire(name_start);
86bec2d3
SK
797 hash->update(ctx, (unsigned int)wire_len, (unsigned char *)name_start);
798 hash->update(ctx, 4, p); /* class and type */
799 hash->update(ctx, 4, (unsigned char *)&nsigttl);
5f8e58f4
SK
800
801 p += 8; /* skip class, type, ttl */
802 GETSHORT(rdlen, p);
5ada8885 803 if (!CHECK_LEN(header, p, plen, rdlen))
87070192 804 return STAT_BOGUS;
5ada8885 805
5f8e58f4
SK
806 end = p + rdlen;
807
808 /* canonicalise rdata and calculate length of same, use name buffer as workspace */
809 cp = p;
810 dp = rr_desc;
811 for (len = 0; (seg = get_rdata(header, plen, end, name, &cp, &dp)) != 0; len += seg);
812 len += end - cp;
813 len = htons(len);
86bec2d3 814 hash->update(ctx, 2, (unsigned char *)&len);
5f8e58f4
SK
815
816 /* Now canonicalise again and digest. */
817 cp = p;
818 dp = rr_desc;
819 while ((seg = get_rdata(header, plen, end, name, &cp, &dp)))
86bec2d3 820 hash->update(ctx, seg, (unsigned char *)name);
5f8e58f4 821 if (cp != end)
86bec2d3 822 hash->update(ctx, end - cp, cp);
5f8e58f4 823 }
86bec2d3
SK
824
825 hash->digest(ctx, hash->digest_size, digest);
826
5ada8885
SK
827 /* namebuff used for workspace above, restore to leave unchanged on exit */
828 p = (unsigned char*)(rrset[0]);
829 extract_name(header, plen, &p, name, 1, 0);
830
5f8e58f4
SK
831 if (key)
832 {
833 if (algo_in == algo && keytag_in == key_tag &&
ebe95a83 834 verify(key, keylen, sig, sig_len, digest, hash->digest_size, algo))
5f8e58f4
SK
835 return STAT_SECURE;
836 }
837 else
838 {
839 /* iterate through all possible keys 4035 5.3.1 */
840 for (; crecp; crecp = cache_find_by_name(crecp, keyname, now, F_DNSKEY))
51ea3ca2
SK
841 if (crecp->addr.key.algo == algo &&
842 crecp->addr.key.keytag == key_tag &&
3f7483e8 843 crecp->uid == (unsigned int)class &&
ebe95a83 844 verify(crecp->addr.key.keydata, crecp->addr.key.keylen, sig, sig_len, digest, hash->digest_size, algo))
5107ace1 845 return (labels < name_labels) ? STAT_SECURE_WILDCARD : STAT_SECURE;
5f8e58f4
SK
846 }
847 }
848
849 return STAT_BOGUS;
850}
851
0fc2f313 852/* The DNS packet is expected to contain the answer to a DNSKEY query.
c3e0b9b6
SK
853 Put all DNSKEYs in the answer which are valid into the cache.
854 return codes:
87070192 855 STAT_INSECURE No DNSKEYs in reply.
c3e0b9b6 856 STAT_SECURE At least one valid DNSKEY found and in cache.
0fc2f313 857 STAT_BOGUS No DNSKEYs found, which can be validated with DS,
87070192 858 or self-sign for DNSKEY RRset is not valid, bad packet.
0fc2f313 859 STAT_NEED_DS DS records to validate a key not found, name in keyname
c3e0b9b6
SK
860*/
861int dnssec_validate_by_ds(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname, int class)
862{
0fc2f313 863 unsigned char *psave, *p = (unsigned char *)(header+1);
c3e0b9b6 864 struct crec *crecp, *recp1;
8d718cbb 865 int rc, j, qtype, qclass, ttl, rdlen, flags, algo, valid, keytag, type_covered;
c3e0b9b6 866 struct blockdata *key;
51ea3ca2 867 struct all_addr a;
c3e0b9b6 868
5f8e58f4
SK
869 if (ntohs(header->qdcount) != 1 ||
870 !extract_name(header, plen, &p, name, 1, 4))
87070192 871 return STAT_BOGUS;
5f8e58f4 872
c3e0b9b6
SK
873 GETSHORT(qtype, p);
874 GETSHORT(qclass, p);
875
87070192 876 if (qtype != T_DNSKEY || qclass != class)
f01d7be6 877 return STAT_BOGUS;
87070192
SK
878
879 if (ntohs(header->ancount) == 0)
880 return STAT_INSECURE;
c3e0b9b6 881
b8eac191 882 /* See if we have cached a DS record which validates this key */
0fc2f313
SK
883 if (!(crecp = cache_find_by_name(NULL, name, now, F_DS)))
884 {
885 strcpy(keyname, name);
886 return STAT_NEED_DS;
887 }
b8eac191
SK
888
889 /* If we've cached that DS provably doesn't exist, result must be INSECURE */
890 if (crecp->flags & F_NEG)
891 return STAT_INSECURE;
892
0fc2f313 893 /* NOTE, we need to find ONE DNSKEY which matches the DS */
e7829aef 894 for (valid = 0, j = ntohs(header->ancount); j != 0 && !valid; j--)
c3e0b9b6
SK
895 {
896 /* Ensure we have type, class TTL and length */
0fc2f313 897 if (!(rc = extract_name(header, plen, &p, name, 0, 10)))
87070192 898 return STAT_BOGUS; /* bad packet */
c3e0b9b6
SK
899
900 GETSHORT(qtype, p);
901 GETSHORT(qclass, p);
902 GETLONG(ttl, p);
903 GETSHORT(rdlen, p);
6f468103 904
0fc2f313 905 if (!CHECK_LEN(header, p, plen, rdlen) || rdlen < 4)
87070192 906 return STAT_BOGUS; /* bad packet */
0fc2f313 907
6f468103
SK
908 if (qclass != class || qtype != T_DNSKEY || rc == 2)
909 {
910 p += rdlen;
911 continue;
912 }
913
0fc2f313 914 psave = p;
c3e0b9b6 915
c3e0b9b6 916 GETSHORT(flags, p);
0fc2f313 917 if (*p++ != 3)
f01d7be6 918 return STAT_BOGUS;
c3e0b9b6 919 algo = *p++;
0fc2f313 920 keytag = dnskey_keytag(algo, flags, p, rdlen - 4);
e7829aef 921 key = NULL;
c3e0b9b6 922
e7829aef
SK
923 /* key must have zone key flag set */
924 if (flags & 0x100)
925 key = blockdata_alloc((char*)p, rdlen - 4);
c3e0b9b6 926
0fc2f313 927 p = psave;
e7829aef 928
0fc2f313 929 if (!ADD_RDLEN(header, p, plen, rdlen))
8d718cbb
SK
930 {
931 if (key)
932 blockdata_free(key);
87070192 933 return STAT_BOGUS; /* bad packet */
8d718cbb
SK
934 }
935
e7829aef
SK
936 /* No zone key flag or malloc failure */
937 if (!key)
0fc2f313
SK
938 continue;
939
940 for (recp1 = crecp; recp1; recp1 = cache_find_by_name(recp1, name, now, F_DS))
86bec2d3
SK
941 {
942 void *ctx;
943 unsigned char *digest, *ds_digest;
944 const struct nettle_hash *hash;
945
51ea3ca2
SK
946 if (recp1->addr.ds.algo == algo &&
947 recp1->addr.ds.keytag == keytag &&
3f7483e8 948 recp1->uid == (unsigned int)class &&
51ea3ca2 949 (hash = hash_find(ds_digest_name(recp1->addr.ds.digest))) &&
86bec2d3 950 hash_init(hash, &ctx, &digest))
0fc2f313 951
86bec2d3
SK
952 {
953 int wire_len = to_wire(name);
954
955 /* Note that digest may be different between DSs, so
956 we can't move this outside the loop. */
957 hash->update(ctx, (unsigned int)wire_len, (unsigned char *)name);
958 hash->update(ctx, (unsigned int)rdlen, psave);
959 hash->digest(ctx, hash->digest_size, digest);
960
961 from_wire(name);
962
824202ef
SK
963 if (recp1->addr.ds.keylen == (int)hash->digest_size &&
964 (ds_digest = blockdata_retrieve(recp1->addr.key.keydata, recp1->addr.ds.keylen, NULL)) &&
965 memcmp(ds_digest, digest, recp1->addr.ds.keylen) == 0 &&
f6a2b793 966 validate_rrset(now, header, plen, class, T_DNSKEY, name, keyname, key, rdlen - 4, algo, keytag) == STAT_SECURE)
86bec2d3 967 {
86bec2d3 968 valid = 1;
86bec2d3
SK
969 break;
970 }
971 }
972 }
e7829aef 973 blockdata_free(key);
c3e0b9b6 974 }
c3e0b9b6 975
0fc2f313
SK
976 if (valid)
977 {
8d718cbb 978 /* DNSKEY RRset determined to be OK, now cache it and the RRsigs that sign it. */
e7829aef
SK
979 cache_start_insert();
980
981 p = skip_questions(header, plen);
982
983 for (j = ntohs(header->ancount); j != 0; j--)
984 {
985 /* Ensure we have type, class TTL and length */
986 if (!(rc = extract_name(header, plen, &p, name, 0, 10)))
987 return STAT_INSECURE; /* bad packet */
988
989 GETSHORT(qtype, p);
990 GETSHORT(qclass, p);
991 GETLONG(ttl, p);
992 GETSHORT(rdlen, p);
8d718cbb
SK
993
994 if (!CHECK_LEN(header, p, plen, rdlen))
87070192 995 return STAT_BOGUS; /* bad packet */
e7829aef 996
8d718cbb 997 if (qclass == class && rc == 1)
e7829aef 998 {
8d718cbb 999 psave = p;
e7829aef 1000
8d718cbb
SK
1001 if (qtype == T_DNSKEY)
1002 {
1003 if (rdlen < 4)
87070192 1004 return STAT_BOGUS; /* bad packet */
8d718cbb
SK
1005
1006 GETSHORT(flags, p);
1007 if (*p++ != 3)
f01d7be6 1008 return STAT_BOGUS;
8d718cbb
SK
1009 algo = *p++;
1010 keytag = dnskey_keytag(algo, flags, p, rdlen - 4);
1011
1012 /* Cache needs to known class for DNSSEC stuff */
1013 a.addr.dnssec.class = class;
1014
1015 if ((key = blockdata_alloc((char*)p, rdlen - 4)))
1016 {
1017 if (!(recp1 = cache_insert(name, &a, now, ttl, F_FORWARD | F_DNSKEY | F_DNSSECOK)))
1018 blockdata_free(key);
1019 else
1020 {
1021 a.addr.keytag = keytag;
1022 log_query(F_KEYTAG | F_UPSTREAM, name, &a, "DNSKEY keytag %u");
1023
1024 recp1->addr.key.keylen = rdlen - 4;
1025 recp1->addr.key.keydata = key;
1026 recp1->addr.key.algo = algo;
1027 recp1->addr.key.keytag = keytag;
1028 recp1->addr.key.flags = flags;
8d718cbb
SK
1029 }
1030 }
1031 }
1032 else if (qtype == T_RRSIG)
1033 {
1034 /* RRSIG, cache if covers DNSKEY RRset */
1035 if (rdlen < 18)
87070192 1036 return STAT_BOGUS; /* bad packet */
8d718cbb
SK
1037
1038 GETSHORT(type_covered, p);
1039
1040 if (type_covered == T_DNSKEY)
1041 {
1042 a.addr.dnssec.class = class;
1043 a.addr.dnssec.type = type_covered;
1044
1045 algo = *p++;
1046 p += 13; /* labels, orig_ttl, expiration, inception */
1047 GETSHORT(keytag, p);
1048 if ((key = blockdata_alloc((char*)psave, rdlen)))
1049 {
1050 if (!(crecp = cache_insert(name, &a, now, ttl, F_FORWARD | F_DNSKEY | F_DS)))
1051 blockdata_free(key);
1052 else
1053 {
8d718cbb
SK
1054 crecp->addr.sig.keydata = key;
1055 crecp->addr.sig.keylen = rdlen;
1056 crecp->addr.sig.keytag = keytag;
1057 crecp->addr.sig.type_covered = type_covered;
1058 crecp->addr.sig.algo = algo;
1059 }
1060 }
1061 }
1062 }
e7829aef 1063
8d718cbb 1064 p = psave;
e7829aef 1065 }
8d718cbb 1066
e7829aef 1067 if (!ADD_RDLEN(header, p, plen, rdlen))
87070192 1068 return STAT_BOGUS; /* bad packet */
e7829aef
SK
1069 }
1070
0fc2f313
SK
1071 /* commit cache insert. */
1072 cache_end_insert();
1073 return STAT_SECURE;
1074 }
1075
1076 log_query(F_UPSTREAM, name, NULL, "BOGUS DNSKEY");
1077 return STAT_BOGUS;
c3e0b9b6 1078}
0fc2f313 1079
c3e0b9b6
SK
1080/* The DNS packet is expected to contain the answer to a DS query
1081 Put all DSs in the answer which are valid into the cache.
1082 return codes:
87070192 1083 STAT_INSECURE no DS in reply or not signed.
c3e0b9b6 1084 STAT_SECURE At least one valid DS found and in cache.
00a5b5d4 1085 STAT_NO_DS It's proved there's no DS here.
87070192 1086 STAT_BOGUS At least one DS found, which fails validation, bad packet.
c3e0b9b6
SK
1087 STAT_NEED_DNSKEY DNSKEY records to validate a DS not found, name in keyname
1088*/
1089
1090int dnssec_validate_ds(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname, int class)
1091{
51ea3ca2 1092 unsigned char *p = (unsigned char *)(header+1);
00a5b5d4 1093 int qtype, qclass, val, i, neganswer;
c3e0b9b6 1094
5f8e58f4 1095 if (ntohs(header->qdcount) != 1 ||
b8eac191 1096 !(p = skip_name(p, header, plen, 4)))
87070192 1097 return STAT_BOGUS;
8d718cbb 1098
c3e0b9b6
SK
1099 GETSHORT(qtype, p);
1100 GETSHORT(qclass, p);
1101
b47b04c8
SK
1102 if (qtype != T_DS || qclass != class)
1103 val = STAT_BOGUS;
1104 else
00a5b5d4
SK
1105 val = dnssec_validate_reply(now, header, plen, name, keyname, NULL, &neganswer);
1106
1107 if (val == STAT_NO_SIG)
1108 val = STAT_INSECURE;
51ea3ca2 1109
b8eac191
SK
1110 p = (unsigned char *)(header+1);
1111 extract_name(header, plen, &p, name, 1, 4);
1112 p += 4; /* qtype, qclass */
1113
00a5b5d4 1114 if (!(p = skip_section(p, ntohs(header->ancount), header, plen)))
87070192 1115 return STAT_BOGUS;
00a5b5d4 1116
0fc2f313 1117 if (val == STAT_BOGUS)
b8eac191 1118 log_query(F_UPSTREAM, name, NULL, "BOGUS DS");
b47b04c8 1119
00a5b5d4 1120 if ((val == STAT_SECURE || val == STAT_INSECURE) && neganswer)
b8eac191 1121 {
14db4212 1122 int rdlen, flags = F_FORWARD | F_DS | F_NEG;
b8eac191
SK
1123 unsigned long ttl, minttl = ULONG_MAX;
1124 struct all_addr a;
00a5b5d4
SK
1125
1126 if (RCODE(header) == NXDOMAIN)
1127 flags |= F_NXDOMAIN;
1128
1129 if (val == STAT_SECURE)
1130 flags |= F_DNSSECOK;
b8eac191
SK
1131
1132 for (i = ntohs(header->nscount); i != 0; i--)
1133 {
00a5b5d4 1134 if (!(p = skip_name(p, header, plen, 0)))
87070192 1135 return STAT_BOGUS;
b8eac191
SK
1136
1137 GETSHORT(qtype, p);
1138 GETSHORT(qclass, p);
1139 GETLONG(ttl, p);
1140 GETSHORT(rdlen, p);
1141
00a5b5d4 1142 if (!CHECK_LEN(header, p, plen, rdlen))
87070192 1143 return STAT_BOGUS; /* bad packet */
00a5b5d4
SK
1144
1145 if (qclass != class || qtype != T_SOA)
b8eac191
SK
1146 {
1147 p += rdlen;
1148 continue;
1149 }
1150
1151 if (ttl < minttl)
1152 minttl = ttl;
1153
1154 /* MNAME */
1155 if (!(p = skip_name(p, header, plen, 0)))
87070192 1156 return STAT_BOGUS;
b8eac191
SK
1157 /* RNAME */
1158 if (!(p = skip_name(p, header, plen, 20)))
87070192 1159 return STAT_BOGUS;
b8eac191
SK
1160 p += 16; /* SERIAL REFRESH RETRY EXPIRE */
1161
1162 GETLONG(ttl, p); /* minTTL */
1163 if (ttl < minttl)
1164 minttl = ttl;
00a5b5d4
SK
1165
1166 break;
b8eac191
SK
1167 }
1168
00a5b5d4
SK
1169 if (i != 0)
1170 {
1171 cache_start_insert();
1172
1173 a.addr.dnssec.class = class;
1174 cache_insert(name, &a, now, ttl, flags);
1175
1176 cache_end_insert();
1177 }
b8eac191 1178
00a5b5d4 1179 return (val == STAT_SECURE) ? STAT_NO_DS : STAT_INSECURE;
b8eac191
SK
1180 }
1181
51ea3ca2 1182 return val;
c3e0b9b6
SK
1183}
1184
c5f4ec7d
SK
1185/* 4034 6.1 */
1186static int hostname_cmp(const char *a, const char *b)
1187{
dbf72123
SK
1188 char *sa, *ea, *ca, *sb, *eb, *cb;
1189 unsigned char ac, bc;
1190
1191 sa = ea = (char *)a + strlen(a);
1192 sb = eb = (char *)b + strlen(b);
1193
c5f4ec7d
SK
1194 while (1)
1195 {
dbf72123
SK
1196 while (sa != a && *(sa-1) != '.')
1197 sa--;
c5f4ec7d 1198
dbf72123
SK
1199 while (sb != b && *(sb-1) != '.')
1200 sb--;
1201
1202 ca = sa;
1203 cb = sb;
1204
1205 while (1)
1206 {
1207 if (ca == ea)
1208 {
1209 if (cb == eb)
1210 break;
1211
1212 return -1;
1213 }
c5f4ec7d 1214
dbf72123
SK
1215 if (cb == eb)
1216 return 1;
1217
1218 ac = (unsigned char) *ca++;
1219 bc = (unsigned char) *cb++;
1220
1221 if (ac >= 'A' && ac <= 'Z')
1222 ac += 'a' - 'A';
1223 if (bc >= 'A' && bc <= 'Z')
1224 bc += 'a' - 'A';
1225
979cdf9b 1226 if (ac < bc)
dbf72123
SK
1227 return -1;
1228 else if (ac != bc)
1229 return 1;
1230 }
c5f4ec7d 1231
dbf72123
SK
1232
1233 if (sa == a)
c5f4ec7d 1234 {
dbf72123
SK
1235 if (sb == b)
1236 return 0;
c5f4ec7d 1237
dbf72123 1238 return -1;
c5f4ec7d
SK
1239 }
1240
dbf72123
SK
1241 if (sb == b)
1242 return 1;
c5f4ec7d 1243
dbf72123
SK
1244 ea = sa--;
1245 eb = sb--;
c5f4ec7d
SK
1246 }
1247}
1248
5107ace1
SK
1249/* Find all the NSEC or NSEC3 records in a reply.
1250 return an array of pointers to them. */
1251static int find_nsec_records(struct dns_header *header, size_t plen, unsigned char ***nsecsetp, int *nsecsetl, int class_reqd)
1252{
1253 static unsigned char **nsecset = NULL;
1254 static int nsecset_sz = 0;
1255
87070192 1256 int type_found = 0;
5107ace1
SK
1257 unsigned char *p = skip_questions(header, plen);
1258 int type, class, rdlen, i, nsecs_found;
1259
1260 /* Move to NS section */
1261 if (!p || !(p = skip_section(p, ntohs(header->ancount), header, plen)))
1262 return 0;
1263
1264 for (nsecs_found = 0, i = ntohs(header->nscount); i != 0; i--)
1265 {
1266 unsigned char *pstart = p;
1267
1268 if (!(p = skip_name(p, header, plen, 10)))
1269 return 0;
1270
1271 GETSHORT(type, p);
1272 GETSHORT(class, p);
1273 p += 4; /* TTL */
1274 GETSHORT(rdlen, p);
1275
1276 if (class == class_reqd && (type == T_NSEC || type == T_NSEC3))
1277 {
1278 /* No mixed NSECing 'round here, thankyouverymuch */
1279 if (type_found == T_NSEC && type == T_NSEC3)
1280 return 0;
1281 if (type_found == T_NSEC3 && type == T_NSEC)
1282 return 0;
1283
1284 type_found = type;
1285
613ad15d
SK
1286 if (!expand_workspace(&nsecset, &nsecset_sz, nsecs_found))
1287 return 0;
1288
5107ace1
SK
1289 nsecset[nsecs_found++] = pstart;
1290 }
613ad15d 1291
5107ace1
SK
1292 if (!ADD_RDLEN(header, p, plen, rdlen))
1293 return 0;
1294 }
1295
1296 *nsecsetp = nsecset;
1297 *nsecsetl = nsecs_found;
1298
1299 return type_found;
1300}
1301
24187530 1302static int prove_non_existence_nsec(struct dns_header *header, size_t plen, unsigned char **nsecs, int nsec_count,
5107ace1
SK
1303 char *workspace1, char *workspace2, char *name, int type)
1304{
1305 int i, rc, rdlen;
1306 unsigned char *p, *psave;
1307 int offset = (type & 0xff) >> 3;
1308 int mask = 0x80 >> (type & 0x07);
1309
1310 /* Find NSEC record that proves name doesn't exist */
1311 for (i = 0; i < nsec_count; i++)
1312 {
1313 p = nsecs[i];
1314 if (!extract_name(header, plen, &p, workspace1, 1, 10))
87070192 1315 return STAT_BOGUS;
5107ace1
SK
1316 p += 8; /* class, type, TTL */
1317 GETSHORT(rdlen, p);
1318 psave = p;
1319 if (!extract_name(header, plen, &p, workspace2, 1, 10))
87070192 1320 return STAT_BOGUS;
5107ace1
SK
1321
1322 rc = hostname_cmp(workspace1, name);
1323
1324 if (rc == 0)
1325 {
f01d7be6
SK
1326 /* 4035 para 5.4. Last sentence */
1327 if (type == T_NSEC || type == T_RRSIG)
1328 return STAT_SECURE;
1329
5107ace1
SK
1330 /* NSEC with the same name as the RR we're testing, check
1331 that the type in question doesn't appear in the type map */
1332 rdlen -= p - psave;
1333 /* rdlen is now length of type map, and p points to it */
1334
1335 while (rdlen >= 2)
1336 {
1337 if (!CHECK_LEN(header, p, plen, rdlen))
87070192 1338 return STAT_BOGUS;
5107ace1
SK
1339
1340 if (p[0] == type >> 8)
1341 {
1342 /* Does the NSEC say our type exists? */
a857daa3 1343 if (offset < p[1] && (p[offset+2] & mask) != 0)
5107ace1
SK
1344 return STAT_BOGUS;
1345
1346 break; /* finshed checking */
1347 }
1348
1349 rdlen -= p[1];
1350 p += p[1];
1351 }
1352
1353 return STAT_SECURE;
1354 }
1355 else if (rc == -1)
1356 {
1357 /* Normal case, name falls between NSEC name and next domain name,
1358 wrap around case, name falls between NSEC name (rc == -1) and end */
1359 if (hostname_cmp(workspace2, name) == 1 || hostname_cmp(workspace1, workspace2) == 1)
1360 return STAT_SECURE;
1361 }
1362 else
1363 {
1364 /* wrap around case, name falls between start and next domain name */
1365 if (hostname_cmp(workspace1, workspace2) == 1 && hostname_cmp(workspace2, name) == 1)
1366 return STAT_SECURE;
1367 }
1368 }
1369
1370 return STAT_BOGUS;
1371}
1372
1373/* return digest length, or zero on error */
1374static int hash_name(char *in, unsigned char **out, struct nettle_hash const *hash,
1375 unsigned char *salt, int salt_len, int iterations)
1376{
1377 void *ctx;
1378 unsigned char *digest;
1379 int i;
1380
1381 if (!hash_init(hash, &ctx, &digest))
1382 return 0;
1383
1384 hash->update(ctx, to_wire(in), (unsigned char *)in);
1385 hash->update(ctx, salt_len, salt);
1386 hash->digest(ctx, hash->digest_size, digest);
1387
1388 for(i = 0; i < iterations; i++)
1389 {
1390 hash->update(ctx, hash->digest_size, digest);
1391 hash->update(ctx, salt_len, salt);
1392 hash->digest(ctx, hash->digest_size, digest);
1393 }
1394
1395 from_wire(in);
1396
1397 *out = digest;
1398 return hash->digest_size;
1399}
1400
1401/* Decode base32 to first "." or end of string */
1402static int base32_decode(char *in, unsigned char *out)
1403{
a857daa3 1404 int oc, on, c, mask, i;
5107ace1
SK
1405 unsigned char *p = out;
1406
a857daa3 1407 for (c = *in, oc = 0, on = 0; c != 0 && c != '.'; c = *++in)
5107ace1 1408 {
5107ace1
SK
1409 if (c >= '0' && c <= '9')
1410 c -= '0';
1411 else if (c >= 'a' && c <= 'v')
1412 c -= 'a', c += 10;
1413 else if (c >= 'A' && c <= 'V')
1414 c -= 'A', c += 10;
1415 else
1416 return 0;
1417
1418 for (mask = 0x10, i = 0; i < 5; i++)
1419 {
a857daa3
SK
1420 if (c & mask)
1421 oc |= 1;
1422 mask = mask >> 1;
1423 if (((++on) & 7) == 0)
1424 *p++ = oc;
1425 oc = oc << 1;
5107ace1
SK
1426 }
1427 }
1428
1429 if ((on & 7) != 0)
1430 return 0;
1431
1432 return p - out;
1433}
1434
24187530 1435static int prove_non_existence_nsec3(struct dns_header *header, size_t plen, unsigned char **nsecs, int nsec_count,
5107ace1
SK
1436 char *workspace1, char *workspace2, char *name, int type)
1437{
a857daa3
SK
1438 unsigned char *salt, *p, *digest;
1439 int digest_len, i, iterations, salt_len, hash_len, base32_len, algo = 0;
5107ace1
SK
1440 struct nettle_hash const *hash;
1441 char *closest_encloser, *next_closest, *wildcard;
1442
1443 /* Look though the NSEC3 records to find the first one with
1444 an algorithm we support (currently only algo == 1).
1445
1446 Take the algo, iterations, and salt of that record
1447 as the ones we're going to use, and prune any
1448 that don't match. */
1449
1450 for (i = 0; i < nsec_count; i++)
1451 {
1452 if (!(p = skip_name(nsecs[i], header, plen, 15)))
87070192 1453 return STAT_BOGUS; /* bad packet */
5107ace1
SK
1454
1455 p += 10; /* type, class, TTL, rdlen */
1456 algo = *p++;
1457
1458 if (algo == 1)
1459 break; /* known algo */
1460 }
1461
1462 /* No usable NSEC3s */
1463 if (i == nsec_count)
1464 return STAT_BOGUS;
1465
1466 p++; /* flags */
1467 GETSHORT (iterations, p);
1468 salt_len = *p++;
1469 salt = p;
1470 if (!CHECK_LEN(header, salt, plen, salt_len))
87070192 1471 return STAT_BOGUS; /* bad packet */
5107ace1
SK
1472
1473 /* Now prune so we only have NSEC3 records with same iterations, salt and algo */
1474 for (i = 0; i < nsec_count; i++)
1475 {
1476 unsigned char *nsec3p = nsecs[i];
1477 int this_iter;
1478
1479 nsecs[i] = NULL; /* Speculative, will be restored if OK. */
1480
1481 if (!(p = skip_name(nsec3p, header, plen, 15)))
87070192 1482 return STAT_BOGUS; /* bad packet */
5107ace1
SK
1483
1484 p += 10; /* type, class, TTL, rdlen */
1485
1486 if (*p++ != algo)
1487 continue;
1488
1489 p++; /* flags */
1490
a857daa3 1491 GETSHORT(this_iter, p);
5107ace1
SK
1492 if (this_iter != iterations)
1493 continue;
1494
1495 if (salt_len != *p++)
1496 continue;
1497
1498 if (!CHECK_LEN(header, p, plen, salt_len))
87070192 1499 return STAT_BOGUS; /* bad packet */
5107ace1
SK
1500
1501 if (memcmp(p, salt, salt_len) != 0)
1502 continue;
1503
1504 /* All match, put the pointer back */
1505 nsecs[i] = nsec3p;
1506 }
1507
1508 /* Algo is checked as 1 above */
1509 if (!(hash = hash_find("sha1")))
87070192 1510 return STAT_BOGUS;
5107ace1
SK
1511
1512 /* Now, we need the "closest encloser NSEC3" */
1513 closest_encloser = name;
1514 next_closest = NULL;
1515
1516 do
1517 {
1518 if (*closest_encloser == '.')
1519 closest_encloser++;
c5f4ec7d 1520
a857daa3 1521 if ((digest_len = hash_name(closest_encloser, &digest, hash, salt, salt_len, iterations)) == 0)
87070192 1522 return STAT_BOGUS;
5107ace1
SK
1523
1524 for (i = 0; i < nsec_count; i++)
1525 if ((p = nsecs[i]))
1526 {
5107ace1 1527 if (!extract_name(header, plen, &p, workspace1, 1, 0) ||
a857daa3 1528 !(base32_len = base32_decode(workspace1, (unsigned char *)workspace2)))
87070192 1529 return STAT_BOGUS;
5107ace1 1530
a857daa3
SK
1531 if (digest_len == base32_len &&
1532 memcmp(digest, workspace2, digest_len) == 0)
5107ace1
SK
1533 break; /* Gotit */
1534 }
1535
1536 if (i != nsec_count)
1537 break;
1538
1539 next_closest = closest_encloser;
1540 }
1541 while ((closest_encloser = strchr(closest_encloser, '.')));
1542
1543 /* No usable NSEC3s */
1544 if (i == nsec_count)
1545 return STAT_BOGUS;
1546
1547 if (!next_closest)
1548 {
1549 /* We found an NSEC3 whose hashed name exactly matches the query, so
1550 Now we just need to check the type map. p points to the RR data for the record. */
a857daa3 1551 int rdlen;
5107ace1
SK
1552 unsigned char *psave;
1553 int offset = (type & 0xff) >> 3;
1554 int mask = 0x80 >> (type & 0x07);
1555
1556 p += 8; /* class, type, TTL */
1557 GETSHORT(rdlen, p);
1558 psave = p;
1559 p += 5 + salt_len; /* algo, flags, iterations, salt_len, salt */
1560 hash_len = *p++;
1561 if (!CHECK_LEN(header, p, plen, hash_len))
87070192 1562 return STAT_BOGUS; /* bad packet */
5107ace1
SK
1563 p += hash_len;
1564 rdlen -= p - psave;
1565
1566 while (rdlen >= 2)
1567 {
1568 if (!CHECK_LEN(header, p, plen, rdlen))
87070192 1569 return STAT_BOGUS;
5107ace1
SK
1570
1571 if (p[0] == type >> 8)
1572 {
a857daa3
SK
1573 /* Does the NSEC3 say our type exists? */
1574 if (offset < p[1] && (p[offset+2] & mask) != 0)
5107ace1
SK
1575 return STAT_BOGUS;
1576
1577 break; /* finshed checking */
1578 }
1579
1580 rdlen -= p[1];
1581 p += p[1];
1582 }
1583
1584 return STAT_SECURE;
1585 }
1586
24187530 1587 /* Look for NSEC3 that proves the non-existence of the next-closest encloser */
a857daa3 1588 if ((digest_len = hash_name(next_closest, &digest, hash, salt, salt_len, iterations)) == 0)
87070192 1589 return STAT_BOGUS;
5107ace1
SK
1590
1591 for (i = 0; i < nsec_count; i++)
1592 if ((p = nsecs[i]))
1593 {
a857daa3
SK
1594 if (!extract_name(header, plen, &p, workspace1, 1, 0) ||
1595 !(base32_len = base32_decode(workspace1, (unsigned char *)workspace2)))
87070192 1596 return STAT_BOGUS;
5107ace1
SK
1597
1598 p += 15 + salt_len; /* class, type, TTL, rdlen, algo, flags, iterations, salt_len, salt */
1599 hash_len = *p++; /* p now points to next hashed name */
1600
1601 if (!CHECK_LEN(header, p, plen, hash_len))
87070192 1602 return STAT_BOGUS;
5107ace1 1603
a857daa3 1604 if (digest_len == base32_len && hash_len == base32_len)
5107ace1 1605 {
a857daa3 1606 if (memcmp(workspace2, digest, digest_len) <= 0)
5107ace1
SK
1607 {
1608 /* Normal case, hash falls between NSEC3 name-hash and next domain name-hash,
1609 wrap around case, name-hash falls between NSEC3 name-hash and end */
a857daa3 1610 if (memcmp(p, digest, digest_len) > 0 || memcmp(workspace2, p, digest_len) > 0)
5107ace1
SK
1611 return STAT_SECURE;
1612 }
1613 else
1614 {
1615 /* wrap around case, name falls between start and next domain name */
a857daa3 1616 if (memcmp(workspace2, p, digest_len) > 0 && memcmp(p, digest, digest_len) > 0)
5107ace1
SK
1617 return STAT_SECURE;
1618 }
1619 }
1620 }
1621
1622 /* Finally, check that there's no seat of wildcard synthesis */
1623 if (!(wildcard = strchr(next_closest, '.')) || wildcard == next_closest)
1624 return STAT_BOGUS;
1625
1626 wildcard--;
1627 *wildcard = '*';
1628
a857daa3 1629 if ((digest_len = hash_name(wildcard, &digest, hash, salt, salt_len, iterations)) == 0)
87070192 1630 return STAT_BOGUS;
5107ace1
SK
1631
1632 for (i = 0; i < nsec_count; i++)
1633 if ((p = nsecs[i]))
1634 {
5107ace1 1635 if (!extract_name(header, plen, &p, workspace1, 1, 0) ||
a857daa3 1636 !(base32_len = base32_decode(workspace1, (unsigned char *)workspace2)))
87070192 1637 return STAT_BOGUS;
5107ace1
SK
1638
1639 p += 15 + salt_len; /* class, type, TTL, rdlen, algo, flags, iterations, salt_len, salt */
1640 hash_len = *p++; /* p now points to next hashed name */
1641
1642 if (!CHECK_LEN(header, p, plen, hash_len))
87070192 1643 return STAT_BOGUS;
5107ace1 1644
a857daa3 1645 if (digest_len == base32_len && hash_len == base32_len)
5107ace1 1646 {
a857daa3 1647 if (memcmp(workspace2, digest, digest_len) <= 0)
5107ace1
SK
1648 {
1649 /* Normal case, hash falls between NSEC3 name-hash and next domain name-hash,
1650 wrap around case, name-hash falls between NSEC3 name-hash and end */
a857daa3 1651 if (memcmp(p, digest, digest_len) > 0 || memcmp(workspace2, p, digest_len) > 0)
5107ace1
SK
1652 return STAT_SECURE;
1653 }
1654 else
1655 {
1656 /* wrap around case, name falls between start and next domain name */
a857daa3 1657 if (memcmp(workspace2, p, digest_len) > 0 && memcmp(p, digest, digest_len) > 0)
5107ace1
SK
1658 return STAT_SECURE;
1659 }
1660 }
1661 }
1662
1663 return STAT_BOGUS;
1664}
1665
0fc2f313 1666/* Validate all the RRsets in the answer and authority sections of the reply (4035:3.2.3) */
51ea3ca2 1667/* Returns are the same as validate_rrset, plus the class if the missing key is in *class */
00a5b5d4 1668int dnssec_validate_reply(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname, int *class, int *neganswer)
adca3e9c 1669{
00a5b5d4
SK
1670 unsigned char *ans_start, *qname, *p1, *p2, **nsecs;
1671 int type1, class1, rdlen1, type2, class2, rdlen2, qclass, qtype;
1fbe4d2f 1672 int i, j, rc, nsec_count, cname_count = CNAME_CHAIN;
00a5b5d4 1673 int nsec_type = 0, have_answer = 0;
adca3e9c 1674
00a5b5d4
SK
1675 if (neganswer)
1676 *neganswer = 0;
1677
87070192 1678 if (RCODE(header) == SERVFAIL || ntohs(header->qdcount) != 1)
e3ec15af
SK
1679 return STAT_BOGUS;
1680
87070192 1681 if (RCODE(header) != NXDOMAIN && RCODE(header) != NOERROR)
72ae2f3d 1682 return STAT_INSECURE;
00a5b5d4
SK
1683
1684 qname = p1 = (unsigned char *)(header+1);
c5f4ec7d 1685
00a5b5d4 1686 if (!extract_name(header, plen, &p1, name, 1, 4))
87070192 1687 return STAT_BOGUS;
00a5b5d4
SK
1688
1689 GETSHORT(qtype, p1);
1690 GETSHORT(qclass, p1);
1691 ans_start = p1;
9d1b22aa
SK
1692
1693 if (qtype == T_ANY)
1694 have_answer = 1;
00a5b5d4
SK
1695
1696 /* Can't validate an RRISG query */
1697 if (qtype == T_RRSIG)
0fc2f313 1698 return STAT_INSECURE;
00a5b5d4
SK
1699
1700 cname_loop:
1701 for (j = ntohs(header->ancount); j != 0; j--)
1702 {
1703 /* leave pointer to missing name in qname */
1704
1705 if (!(rc = extract_name(header, plen, &p1, name, 0, 10)))
87070192 1706 return STAT_BOGUS; /* bad packet */
00a5b5d4
SK
1707
1708 GETSHORT(type2, p1);
1709 GETSHORT(class2, p1);
1710 p1 += 4; /* TTL */
1711 GETSHORT(rdlen2, p1);
1712
1713 if (rc == 1 && qclass == class2)
1714 {
1715 /* Do we have an answer for the question? */
1716 if (type2 == qtype)
1717 {
1718 have_answer = 1;
1719 break;
1720 }
1721 else if (type2 == T_CNAME)
1722 {
1723 qname = p1;
1724
1725 /* looped CNAMES */
1726 if (!cname_count-- || !extract_name(header, plen, &p1, name, 1, 0))
87070192 1727 return STAT_BOGUS;
00a5b5d4
SK
1728
1729 p1 = ans_start;
1730 goto cname_loop;
1731 }
1732 }
1733
1734 if (!ADD_RDLEN(header, p1, plen, rdlen2))
87070192 1735 return STAT_BOGUS;
00a5b5d4 1736 }
0fc2f313 1737
00a5b5d4
SK
1738 if (neganswer && !have_answer)
1739 *neganswer = 1;
0575610f
SK
1740
1741 /* No data, therefore no sigs */
1742 if (ntohs(header->ancount) + ntohs(header->nscount) == 0)
1743 return STAT_NO_SIG;
00a5b5d4 1744
0fc2f313 1745 for (p1 = ans_start, i = 0; i < ntohs(header->ancount) + ntohs(header->nscount); i++)
adca3e9c 1746 {
0fc2f313 1747 if (!extract_name(header, plen, &p1, name, 1, 10))
87070192 1748 return STAT_BOGUS; /* bad packet */
0fc2f313
SK
1749
1750 GETSHORT(type1, p1);
1751 GETSHORT(class1, p1);
1752 p1 += 4; /* TTL */
1753 GETSHORT(rdlen1, p1);
1754
1755 /* Don't try and validate RRSIGs! */
1756 if (type1 != T_RRSIG)
1757 {
1758 /* Check if we've done this RRset already */
1759 for (p2 = ans_start, j = 0; j < i; j++)
1760 {
1761 if (!(rc = extract_name(header, plen, &p2, name, 0, 10)))
87070192 1762 return STAT_BOGUS; /* bad packet */
0fc2f313
SK
1763
1764 GETSHORT(type2, p2);
1765 GETSHORT(class2, p2);
1766 p2 += 4; /* TTL */
1767 GETSHORT(rdlen2, p2);
1768
1769 if (type2 == type1 && class2 == class1 && rc == 1)
1770 break; /* Done it before: name, type, class all match. */
1771
1772 if (!ADD_RDLEN(header, p2, plen, rdlen2))
87070192 1773 return STAT_BOGUS;
0fc2f313
SK
1774 }
1775
1776 /* Not done, validate now */
51ea3ca2 1777 if (j == i)
0fc2f313 1778 {
8d718cbb
SK
1779 int ttl, keytag, algo, digest, type_covered;
1780 unsigned char *psave;
1781 struct all_addr a;
1782 struct blockdata *key;
1783 struct crec *crecp;
1784
5107ace1
SK
1785 rc = validate_rrset(now, header, plen, class1, type1, name, keyname, NULL, 0, 0, 0);
1786
1787 if (rc == STAT_SECURE_WILDCARD)
1788 {
1789 /* An attacker replay a wildcard answer with a different
a857daa3 1790 answer and overlay a genuine RR. To prove this
5107ace1 1791 hasn't happened, the answer must prove that
a857daa3 1792 the gennuine record doesn't exist. Check that here. */
87070192
SK
1793 if (!nsec_type && !(nsec_type = find_nsec_records(header, plen, &nsecs, &nsec_count, class1)))
1794 return STAT_BOGUS; /* No NSECs or bad packet */
5107ace1
SK
1795
1796 if (nsec_type == T_NSEC)
24187530 1797 rc = prove_non_existence_nsec(header, plen, nsecs, nsec_count, daemon->workspacename, keyname, name, type1);
5107ace1 1798 else
24187530 1799 rc = prove_non_existence_nsec3(header, plen, nsecs, nsec_count, daemon->workspacename, keyname, name, type1);
5107ace1
SK
1800
1801 if (rc != STAT_SECURE)
1802 return rc;
1803 }
1804 else if (rc != STAT_SECURE)
51ea3ca2
SK
1805 {
1806 if (class)
1807 *class = class1; /* Class for DS or DNSKEY */
1808 return rc;
1809 }
5107ace1 1810
8d718cbb
SK
1811 /* Cache RRsigs in answer section, and if we just validated a DS RRset, cache it */
1812 cache_start_insert();
1813
1814 for (p2 = ans_start, j = 0; j < ntohs(header->ancount); j++)
51ea3ca2 1815 {
8d718cbb 1816 if (!(rc = extract_name(header, plen, &p2, name, 0, 10)))
87070192 1817 return STAT_BOGUS; /* bad packet */
51ea3ca2 1818
8d718cbb
SK
1819 GETSHORT(type2, p2);
1820 GETSHORT(class2, p2);
1821 GETLONG(ttl, p2);
1822 GETSHORT(rdlen2, p2);
1823
1824 if (!CHECK_LEN(header, p2, plen, rdlen2))
87070192 1825 return STAT_BOGUS; /* bad packet */
8d718cbb
SK
1826
1827 if (class2 == class1 && rc == 1)
1828 {
1829 psave = p2;
1830
1831 if (type1 == T_DS && type2 == T_DS)
51ea3ca2 1832 {
8d718cbb 1833 if (rdlen2 < 4)
87070192 1834 return STAT_BOGUS; /* bad packet */
8d718cbb 1835
51ea3ca2
SK
1836 GETSHORT(keytag, p2);
1837 algo = *p2++;
1838 digest = *p2++;
1839
1840 /* Cache needs to known class for DNSSEC stuff */
1841 a.addr.dnssec.class = class2;
1842
8d718cbb 1843 if ((key = blockdata_alloc((char*)p2, rdlen2 - 4)))
51ea3ca2 1844 {
8d718cbb
SK
1845 if (!(crecp = cache_insert(name, &a, now, ttl, F_FORWARD | F_DS | F_DNSSECOK)))
1846 blockdata_free(key);
1847 else
1848 {
1849 a.addr.keytag = keytag;
1850 log_query(F_KEYTAG | F_UPSTREAM, name, &a, "DS keytag %u");
1851 crecp->addr.ds.digest = digest;
1852 crecp->addr.ds.keydata = key;
1853 crecp->addr.ds.algo = algo;
1854 crecp->addr.ds.keytag = keytag;
8d718cbb
SK
1855 crecp->addr.ds.keylen = rdlen2 - 4;
1856 }
1857 }
1858 }
1859 else if (type2 == T_RRSIG)
1860 {
1861 if (rdlen2 < 18)
87070192 1862 return STAT_BOGUS; /* bad packet */
51ea3ca2 1863
8d718cbb
SK
1864 GETSHORT(type_covered, p2);
1865
1866 if (type_covered == type1 &&
1867 (type_covered == T_A || type_covered == T_AAAA ||
1868 type_covered == T_CNAME || type_covered == T_DS ||
1869 type_covered == T_DNSKEY || type_covered == T_PTR))
1870 {
1871 a.addr.dnssec.type = type_covered;
c8ca33f8 1872 a.addr.dnssec.class = class1;
8d718cbb
SK
1873
1874 algo = *p2++;
1875 p2 += 13; /* labels, orig_ttl, expiration, inception */
1876 GETSHORT(keytag, p2);
1877
1878 if ((key = blockdata_alloc((char*)psave, rdlen2)))
1879 {
1880 if (!(crecp = cache_insert(name, &a, now, ttl, F_FORWARD | F_DNSKEY | F_DS)))
1881 blockdata_free(key);
1882 else
1883 {
8d718cbb
SK
1884 crecp->addr.sig.keydata = key;
1885 crecp->addr.sig.keylen = rdlen2;
1886 crecp->addr.sig.keytag = keytag;
1887 crecp->addr.sig.type_covered = type_covered;
1888 crecp->addr.sig.algo = algo;
1889 }
1890 }
1891 }
51ea3ca2
SK
1892 }
1893
8d718cbb 1894 p2 = psave;
51ea3ca2
SK
1895 }
1896
8d718cbb 1897 if (!ADD_RDLEN(header, p2, plen, rdlen2))
87070192 1898 return STAT_BOGUS; /* bad packet */
51ea3ca2 1899 }
8d718cbb
SK
1900
1901 cache_end_insert();
0fc2f313
SK
1902 }
1903 }
adca3e9c 1904
0fc2f313 1905 if (!ADD_RDLEN(header, p1, plen, rdlen1))
87070192 1906 return STAT_BOGUS;
adca3e9c
GB
1907 }
1908
c5f4ec7d 1909 /* OK, all the RRsets validate, now see if we have a NODATA or NXDOMAIN reply */
00a5b5d4
SK
1910 if (have_answer)
1911 return STAT_SECURE;
1912
5107ace1 1913 /* NXDOMAIN or NODATA reply, prove that (name, class1, type1) can't exist */
5107ace1 1914 /* First marshall the NSEC records, if we've not done it previously */
87070192
SK
1915 if (!nsec_type && !(nsec_type = find_nsec_records(header, plen, &nsecs, &nsec_count, qclass)))
1916 return STAT_BOGUS; /* No NSECs */
00a5b5d4
SK
1917
1918 /* Get name of missing answer */
1919 if (!extract_name(header, plen, &qname, name, 1, 0))
87070192 1920 return STAT_BOGUS;
5107ace1
SK
1921
1922 if (nsec_type == T_NSEC)
00a5b5d4 1923 return prove_non_existence_nsec(header, plen, nsecs, nsec_count, daemon->workspacename, keyname, name, qtype);
5107ace1 1924 else
00a5b5d4 1925 return prove_non_existence_nsec3(header, plen, nsecs, nsec_count, daemon->workspacename, keyname, name, qtype);
e292e93d
GB
1926}
1927
00a5b5d4
SK
1928/* Chase the CNAME chain in the packet until the first record which _doesn't validate.
1929 Needed for proving answer in unsigned space.
1930 Return STAT_NEED_*
1931 STAT_BOGUS - error
1932 STAT_INSECURE - name of first non-secure record in name
1933*/
1934int dnssec_chase_cname(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname)
1935{
1936 unsigned char *p = (unsigned char *)(header+1);
c07d30dc 1937 int type, class, qclass, rdlen, j, rc;
1fbe4d2f 1938 int cname_count = CNAME_CHAIN;
00a5b5d4
SK
1939
1940 /* Get question */
1941 if (!extract_name(header, plen, &p, name, 1, 4))
1942 return STAT_BOGUS;
1943
c07d30dc 1944 p +=2; /* type */
00a5b5d4
SK
1945 GETSHORT(qclass, p);
1946
1947 while (1)
1948 {
1949 for (j = ntohs(header->ancount); j != 0; j--)
1950 {
1951 if (!(rc = extract_name(header, plen, &p, name, 0, 10)))
1952 return STAT_BOGUS; /* bad packet */
1953
1954 GETSHORT(type, p);
1955 GETSHORT(class, p);
1956 p += 4; /* TTL */
1957 GETSHORT(rdlen, p);
1958
1959 /* Not target, loop */
1960 if (rc == 2 || qclass != class)
1961 {
1962 if (!ADD_RDLEN(header, p, plen, rdlen))
1963 return STAT_BOGUS;
1964 continue;
1965 }
1966
1967 /* Got to end of CNAME chain. */
1968 if (type != T_CNAME)
1969 return STAT_INSECURE;
1970
1971 /* validate CNAME chain, return if insecure or need more data */
1972 rc = validate_rrset(now, header, plen, class, type, name, keyname, NULL, 0, 0, 0);
1973 if (rc != STAT_SECURE)
1974 {
1975 if (rc == STAT_NO_SIG)
1976 rc = STAT_INSECURE;
1977 return rc;
1978 }
1979
1980 /* Loop down CNAME chain/ */
1981 if (!cname_count-- ||
1982 !extract_name(header, plen, &p, name, 1, 0) ||
1983 !(p = skip_questions(header, plen)))
1984 return STAT_BOGUS;
1985
1986 break;
1987 }
1988
1989 /* End of CNAME chain */
1990 return STAT_INSECURE;
1991 }
1992}
1993
1994
3471f181 1995/* Compute keytag (checksum to quickly index a key). See RFC4034 */
0fc2f313 1996int dnskey_keytag(int alg, int flags, unsigned char *key, int keylen)
3471f181 1997{
75ffc9bf
GB
1998 if (alg == 1)
1999 {
2000 /* Algorithm 1 (RSAMD5) has a different (older) keytag calculation algorithm.
2001 See RFC4034, Appendix B.1 */
0fc2f313 2002 return key[keylen-4] * 256 + key[keylen-3];
75ffc9bf
GB
2003 }
2004 else
2005 {
1633e308 2006 unsigned long ac = flags + 0x300 + alg;
75ffc9bf
GB
2007 int i;
2008
0fc2f313
SK
2009 for (i = 0; i < keylen; ++i)
2010 ac += (i & 1) ? key[i] : key[i] << 8;
1633e308 2011
0fc2f313
SK
2012 ac += (ac >> 16) & 0xffff;
2013 return ac & 0xffff;
0304d28f 2014 }
3471f181 2015}
e292e93d 2016
5f8e58f4
SK
2017size_t dnssec_generate_query(struct dns_header *header, char *end, char *name, int class, int type, union mysockaddr *addr)
2018{
2019 unsigned char *p;
610e782a 2020 char *types = querystr("dnssec-query", type);
5f8e58f4
SK
2021
2022 if (addr->sa.sa_family == AF_INET)
2023 log_query(F_DNSSEC | F_IPV4, name, (struct all_addr *)&addr->in.sin_addr, types);
2024#ifdef HAVE_IPV6
2025 else
2026 log_query(F_DNSSEC | F_IPV6, name, (struct all_addr *)&addr->in6.sin6_addr, types);
2027#endif
2028
2029 header->qdcount = htons(1);
2030 header->ancount = htons(0);
2031 header->nscount = htons(0);
2032 header->arcount = htons(0);
e292e93d 2033
5f8e58f4
SK
2034 header->hb3 = HB3_RD;
2035 SET_OPCODE(header, QUERY);
5b3bf921
SK
2036 /* For debugging, set Checking Disabled, otherwise, have the upstream check too,
2037 this allows it to select auth servers when one is returning bad data. */
2038 header->hb4 = option_bool(OPT_DNSSEC_DEBUG) ? HB4_CD : 0;
5f8e58f4
SK
2039
2040 /* ID filled in later */
2041
2042 p = (unsigned char *)(header+1);
2043
2044 p = do_rfc1035_name(p, name);
2045 *p++ = 0;
2046 PUTSHORT(type, p);
2047 PUTSHORT(class, p);
2048
2049 return add_do_bit(header, p - (unsigned char *)header, end);
2050}
8a9be9e4 2051
613ad15d
SK
2052/* Go through a domain name, find "pointers" and fix them up based on how many bytes
2053 we've chopped out of the packet, or check they don't point into an elided part. */
2054static int check_name(unsigned char **namep, struct dns_header *header, size_t plen, int fixup, unsigned char **rrs, int rr_count)
2055{
2056 unsigned char *ansp = *namep;
2057
2058 while(1)
2059 {
2060 unsigned int label_type;
2061
2062 if (!CHECK_LEN(header, ansp, plen, 1))
2063 return 0;
2064
2065 label_type = (*ansp) & 0xc0;
2066
2067 if (label_type == 0xc0)
2068 {
2069 /* pointer for compression. */
00a5b5d4
SK
2070 unsigned int offset;
2071 int i;
613ad15d
SK
2072 unsigned char *p;
2073
2074 if (!CHECK_LEN(header, ansp, plen, 2))
2075 return 0;
2076
2077 offset = ((*ansp++) & 0x3f) << 8;
2078 offset |= *ansp++;
2079
2080 p = offset + (unsigned char *)header;
2081
2082 for (i = 0; i < rr_count; i++)
2083 if (p < rrs[i])
2084 break;
2085 else
2086 if (i & 1)
2087 offset -= rrs[i] - rrs[i-1];
2088
2089 /* does the pointer end up in an elided RR? */
2090 if (i & 1)
00a5b5d4 2091 return 0;
613ad15d
SK
2092
2093 /* No, scale the pointer */
2094 if (fixup)
2095 {
2096 ansp -= 2;
2097 *ansp++ = (offset >> 8) | 0xc0;
2098 *ansp++ = offset & 0xff;
2099 }
2100 break;
2101 }
2102 else if (label_type == 0x80)
2103 return 0; /* reserved */
2104 else if (label_type == 0x40)
2105 {
2106 /* Extended label type */
2107 unsigned int count;
2108
2109 if (!CHECK_LEN(header, ansp, plen, 2))
2110 return 0;
2111
2112 if (((*ansp++) & 0x3f) != 1)
2113 return 0; /* we only understand bitstrings */
2114
2115 count = *(ansp++); /* Bits in bitstring */
2116
2117 if (count == 0) /* count == 0 means 256 bits */
2118 ansp += 32;
2119 else
2120 ansp += ((count-1)>>3)+1;
2121 }
2122 else
2123 { /* label type == 0 Bottom six bits is length */
2124 unsigned int len = (*ansp++) & 0x3f;
2125
2126 if (!ADD_RDLEN(header, ansp, plen, len))
2127 return 0;
2128
2129 if (len == 0)
2130 break; /* zero length label marks the end. */
2131 }
2132 }
2133
2134 *namep = ansp;
2135
2136 return 1;
2137}
2138
2139/* Go through RRs and check or fixup the domain names contained within */
2140static int check_rrs(unsigned char *p, struct dns_header *header, size_t plen, int fixup, unsigned char **rrs, int rr_count)
2141{
2142 int i, type, class, rdlen;
00a5b5d4 2143 unsigned char *pp;
613ad15d 2144
50f86ce8 2145 for (i = 0; i < ntohs(header->ancount) + ntohs(header->nscount) + ntohs(header->arcount); i++)
613ad15d 2146 {
00a5b5d4
SK
2147 pp = p;
2148
2149 if (!(p = skip_name(p, header, plen, 10)))
2150 return 0;
613ad15d
SK
2151
2152 GETSHORT(type, p);
2153 GETSHORT(class, p);
2154 p += 4; /* TTL */
2155 GETSHORT(rdlen, p);
00a5b5d4 2156
613ad15d
SK
2157 if (type != T_NSEC && type != T_NSEC3 && type != T_RRSIG)
2158 {
00a5b5d4
SK
2159 /* fixup name of RR */
2160 if (!check_name(&pp, header, plen, fixup, rrs, rr_count))
2161 return 0;
2162
613ad15d
SK
2163 if (class == C_IN)
2164 {
2165 u16 *d;
14db4212
SK
2166
2167 for (pp = p, d = get_desc(type); *d != (u16)-1; d++)
613ad15d
SK
2168 {
2169 if (*d != 0)
2170 pp += *d;
2171 else if (!check_name(&pp, header, plen, fixup, rrs, rr_count))
2172 return 0;
2173 }
2174 }
2175 }
2176
2177 if (!ADD_RDLEN(header, p, plen, rdlen))
2178 return 0;
2179 }
2180
2181 return 1;
2182}
2183
2184
2185size_t filter_rrsigs(struct dns_header *header, size_t plen)
2186{
2187 static unsigned char **rrs;
2188 static int rr_sz = 0;
2189
2190 unsigned char *p = (unsigned char *)(header+1);
50f86ce8 2191 int i, rdlen, qtype, qclass, rr_found, chop_an, chop_ns, chop_ar;
613ad15d
SK
2192
2193 if (ntohs(header->qdcount) != 1 ||
2194 !(p = skip_name(p, header, plen, 4)))
2195 return plen;
2196
2197 GETSHORT(qtype, p);
2198 GETSHORT(qclass, p);
2199
2200 /* First pass, find pointers to start and end of all the records we wish to elide:
2201 records added for DNSSEC, unless explicity queried for */
50f86ce8
SK
2202 for (rr_found = 0, chop_ns = 0, chop_an = 0, chop_ar = 0, i = 0;
2203 i < ntohs(header->ancount) + ntohs(header->nscount) + ntohs(header->arcount);
2204 i++)
613ad15d
SK
2205 {
2206 unsigned char *pstart = p;
2207 int type, class;
2208
2209 if (!(p = skip_name(p, header, plen, 10)))
2210 return plen;
2211
2212 GETSHORT(type, p);
2213 GETSHORT(class, p);
2214 p += 4; /* TTL */
2215 GETSHORT(rdlen, p);
2216
2217 if ((type == T_NSEC || type == T_NSEC3 || type == T_RRSIG) &&
2218 (type != qtype || class != qclass))
2219 {
2220 if (!expand_workspace(&rrs, &rr_sz, rr_found + 1))
2221 return plen;
2222
2223 rrs[rr_found++] = pstart;
2224
2225 if (!ADD_RDLEN(header, p, plen, rdlen))
2226 return plen;
2227
2228 rrs[rr_found++] = p;
2229
2230 if (i < ntohs(header->ancount))
2231 chop_an++;
e6096e64 2232 else if (i < (ntohs(header->nscount) + ntohs(header->ancount)))
613ad15d 2233 chop_ns++;
50f86ce8
SK
2234 else
2235 chop_ar++;
613ad15d
SK
2236 }
2237 else if (!ADD_RDLEN(header, p, plen, rdlen))
2238 return plen;
2239 }
2240
2241 /* Nothing to do. */
2242 if (rr_found == 0)
2243 return plen;
2244
2245 /* Second pass, look for pointers in names in the records we're keeping and make sure they don't
2246 point to records we're going to elide. This is theoretically possible, but unlikely. If
2247 it happens, we give up and leave the answer unchanged. */
2248 p = (unsigned char *)(header+1);
2249
2250 /* question first */
2251 if (!check_name(&p, header, plen, 0, rrs, rr_found))
2252 return plen;
2253 p += 4; /* qclass, qtype */
2254
2255 /* Now answers and NS */
2256 if (!check_rrs(p, header, plen, 0, rrs, rr_found))
2257 return plen;
2258
2259 /* Third pass, elide records */
2260 for (p = rrs[0], i = 1; i < rr_found; i += 2)
2261 {
2262 unsigned char *start = rrs[i];
2263 unsigned char *end = (i != rr_found - 1) ? rrs[i+1] : ((unsigned char *)(header+1)) + plen;
2264
2265 memmove(p, start, end-start);
2266 p += end-start;
2267 }
2268
2269 plen = p - (unsigned char *)header;
2270 header->ancount = htons(ntohs(header->ancount) - chop_an);
2271 header->nscount = htons(ntohs(header->nscount) - chop_ns);
50f86ce8
SK
2272 header->arcount = htons(ntohs(header->arcount) - chop_ar);
2273
613ad15d
SK
2274 /* Fourth pass, fix up pointers in the remaining records */
2275 p = (unsigned char *)(header+1);
2276
2277 check_name(&p, header, plen, 1, rrs, rr_found);
2278 p += 4; /* qclass, qtype */
2279
2280 check_rrs(p, header, plen, 1, rrs, rr_found);
2281
2282 return plen;
2283}
2284
8a9be9e4
SK
2285unsigned char* hash_questions(struct dns_header *header, size_t plen, char *name)
2286{
2287 int q;
2288 unsigned int len;
2289 unsigned char *p = (unsigned char *)(header+1);
2290 const struct nettle_hash *hash;
2291 void *ctx;
2292 unsigned char *digest;
5f8e58f4 2293
8a9be9e4
SK
2294 if (!(hash = hash_find("sha1")) || !hash_init(hash, &ctx, &digest))
2295 return NULL;
2296
2297 for (q = ntohs(header->qdcount); q != 0; q--)
2298 {
2299 if (!extract_name(header, plen, &p, name, 1, 4))
7d23a66f 2300 break; /* bad packet */
8a9be9e4
SK
2301
2302 len = to_wire(name);
2303 hash->update(ctx, len, (unsigned char *)name);
2304 /* CRC the class and type as well */
2305 hash->update(ctx, 4, p);
2306
2307 p += 4;
2308 if (!CHECK_LEN(header, p, plen, 0))
7d23a66f 2309 break; /* bad packet */
8a9be9e4 2310 }
703c7ff4
SK
2311
2312 hash->digest(ctx, hash->digest_size, digest);
8a9be9e4
SK
2313 return digest;
2314}
2315
0fc2f313 2316#endif /* HAVE_DNSSEC */