Coverage Report

Created: 2023-06-07 06:26

/src/unbound/validator/val_nsec3.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * validator/val_nsec3.c - validator NSEC3 denial of existence functions.
3
 *
4
 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5
 *
6
 * This software is open source.
7
 * 
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 
12
 * Redistributions of source code must retain the above copyright notice,
13
 * this list of conditions and the following disclaimer.
14
 * 
15
 * Redistributions in binary form must reproduce the above copyright notice,
16
 * this list of conditions and the following disclaimer in the documentation
17
 * and/or other materials provided with the distribution.
18
 * 
19
 * Neither the name of the NLNET LABS nor the names of its contributors may
20
 * be used to endorse or promote products derived from this software without
21
 * specific prior written permission.
22
 * 
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
 */
35
36
/**
37
 * \file
38
 *
39
 * This file contains helper functions for the validator module.
40
 * The functions help with NSEC3 checking, the different NSEC3 proofs
41
 * for denial of existence, and proofs for presence of types.
42
 */
43
#include "config.h"
44
#include <ctype.h>
45
#include "validator/val_nsec3.h"
46
#include "validator/val_secalgo.h"
47
#include "validator/validator.h"
48
#include "validator/val_kentry.h"
49
#include "services/cache/rrset.h"
50
#include "util/regional.h"
51
#include "util/rbtree.h"
52
#include "util/module.h"
53
#include "util/net_help.h"
54
#include "util/data/packed_rrset.h"
55
#include "util/data/dname.h"
56
#include "util/data/msgreply.h"
57
/* we include nsec.h for the bitmap_has_type function */
58
#include "validator/val_nsec.h"
59
#include "sldns/sbuffer.h"
60
61
/** 
62
 * This function we get from ldns-compat or from base system 
63
 * it returns the number of data bytes stored at the target, or <0 on error.
64
 */
65
int sldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
66
  char *target, size_t targsize);
67
/** 
68
 * This function we get from ldns-compat or from base system 
69
 * it returns the number of data bytes stored at the target, or <0 on error.
70
 */
71
int sldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, 
72
  uint8_t *target, size_t targsize);
73
74
/**
75
 * Closest encloser (ce) proof results
76
 * Contains the ce and the next-closer (nc) proof.
77
 */
78
struct ce_response {
79
  /** the closest encloser name */
80
  uint8_t* ce;
81
  /** length of ce */
82
  size_t ce_len;
83
  /** NSEC3 record that proved ce. rrset */
84
  struct ub_packed_rrset_key* ce_rrset;
85
  /** NSEC3 record that proved ce. rr number */
86
  int ce_rr;
87
  /** NSEC3 record that proved nc. rrset */
88
  struct ub_packed_rrset_key* nc_rrset;
89
  /** NSEC3 record that proved nc. rr*/
90
  int nc_rr;
91
};
92
93
/**
94
 * Filter conditions for NSEC3 proof
95
 * Used to iterate over the applicable NSEC3 RRs.
96
 */
97
struct nsec3_filter {
98
  /** Zone name, only NSEC3 records for this zone are considered */
99
  uint8_t* zone;
100
  /** length of the zonename */
101
  size_t zone_len;
102
  /** the list of NSEC3s to filter; array */
103
  struct ub_packed_rrset_key** list;
104
  /** number of rrsets in list */
105
  size_t num;
106
  /** class of records for the NSEC3, only this class applies */
107
  uint16_t fclass;
108
};
109
110
/** return number of rrs in an rrset */
111
static size_t
112
rrset_get_count(struct ub_packed_rrset_key* rrset)
113
0
{
114
0
        struct packed_rrset_data* d = (struct packed_rrset_data*)
115
0
          rrset->entry.data;
116
0
        if(!d) return 0;
117
0
        return d->count;
118
0
}
119
120
/** return if nsec3 RR has unknown flags */
121
static int
122
nsec3_unknown_flags(struct ub_packed_rrset_key* rrset, int r)
123
0
{
124
0
        struct packed_rrset_data* d = (struct packed_rrset_data*)
125
0
          rrset->entry.data;
126
0
  log_assert(d && r < (int)d->count);
127
0
  if(d->rr_len[r] < 2+2)
128
0
    return 0; /* malformed */
129
0
  return (int)(d->rr_data[r][2+1] & NSEC3_UNKNOWN_FLAGS);
130
0
}
131
132
int
133
nsec3_has_optout(struct ub_packed_rrset_key* rrset, int r)
134
0
{
135
0
        struct packed_rrset_data* d = (struct packed_rrset_data*)
136
0
          rrset->entry.data;
137
0
  log_assert(d && r < (int)d->count);
138
0
  if(d->rr_len[r] < 2+2)
139
0
    return 0; /* malformed */
140
0
  return (int)(d->rr_data[r][2+1] & NSEC3_OPTOUT);
141
0
}
142
143
/** return nsec3 RR algorithm */
144
static int
145
nsec3_get_algo(struct ub_packed_rrset_key* rrset, int r)
146
0
{
147
0
        struct packed_rrset_data* d = (struct packed_rrset_data*)
148
0
          rrset->entry.data;
149
0
  log_assert(d && r < (int)d->count);
150
0
  if(d->rr_len[r] < 2+1)
151
0
    return 0; /* malformed */
152
0
  return (int)(d->rr_data[r][2+0]);
153
0
}
154
155
/** return if nsec3 RR has known algorithm */
156
static int
157
nsec3_known_algo(struct ub_packed_rrset_key* rrset, int r)
158
0
{
159
0
        struct packed_rrset_data* d = (struct packed_rrset_data*)
160
0
          rrset->entry.data;
161
0
  log_assert(d && r < (int)d->count);
162
0
  if(d->rr_len[r] < 2+1)
163
0
    return 0; /* malformed */
164
0
  switch(d->rr_data[r][2+0]) {
165
0
    case NSEC3_HASH_SHA1:
166
0
      return 1;
167
0
  }
168
0
  return 0;
169
0
}
170
171
/** return nsec3 RR iteration count */
172
static size_t
173
nsec3_get_iter(struct ub_packed_rrset_key* rrset, int r)
174
0
{
175
0
  uint16_t i;
176
0
        struct packed_rrset_data* d = (struct packed_rrset_data*)
177
0
          rrset->entry.data;
178
0
  log_assert(d && r < (int)d->count);
179
0
  if(d->rr_len[r] < 2+4)
180
0
    return 0; /* malformed */
181
0
  memmove(&i, d->rr_data[r]+2+2, sizeof(i));
182
0
  i = ntohs(i);
183
0
  return (size_t)i;
184
0
}
185
186
/** return nsec3 RR salt */
187
static int
188
nsec3_get_salt(struct ub_packed_rrset_key* rrset, int r,
189
  uint8_t** salt, size_t* saltlen)
190
0
{
191
0
        struct packed_rrset_data* d = (struct packed_rrset_data*)
192
0
          rrset->entry.data;
193
0
  log_assert(d && r < (int)d->count);
194
0
  if(d->rr_len[r] < 2+5) {
195
0
    *salt = 0;
196
0
    *saltlen = 0;
197
0
    return 0; /* malformed */
198
0
  }
199
0
  *saltlen = (size_t)d->rr_data[r][2+4];
200
0
  if(d->rr_len[r] < 2+5+(size_t)*saltlen) {
201
0
    *salt = 0;
202
0
    *saltlen = 0;
203
0
    return 0; /* malformed */
204
0
  }
205
0
  *salt = d->rr_data[r]+2+5;
206
0
  return 1;
207
0
}
208
209
int nsec3_get_params(struct ub_packed_rrset_key* rrset, int r,
210
  int* algo, size_t* iter, uint8_t** salt, size_t* saltlen)
211
0
{
212
0
  if(!nsec3_known_algo(rrset, r) || nsec3_unknown_flags(rrset, r))
213
0
    return 0;
214
0
  if(!nsec3_get_salt(rrset, r, salt, saltlen))
215
0
    return 0;
216
0
  *algo = nsec3_get_algo(rrset, r);
217
0
  *iter = nsec3_get_iter(rrset, r);
218
0
  return 1;
219
0
}
220
221
int
222
nsec3_get_nextowner(struct ub_packed_rrset_key* rrset, int r,
223
  uint8_t** next, size_t* nextlen)
224
0
{
225
0
  size_t saltlen;
226
0
        struct packed_rrset_data* d = (struct packed_rrset_data*)
227
0
          rrset->entry.data;
228
0
  log_assert(d && r < (int)d->count);
229
0
  if(d->rr_len[r] < 2+5) {
230
0
    *next = 0;
231
0
    *nextlen = 0;
232
0
    return 0; /* malformed */
233
0
  }
234
0
  saltlen = (size_t)d->rr_data[r][2+4];
235
0
  if(d->rr_len[r] < 2+5+saltlen+1) {
236
0
    *next = 0;
237
0
    *nextlen = 0;
238
0
    return 0; /* malformed */
239
0
  }
240
0
  *nextlen = (size_t)d->rr_data[r][2+5+saltlen];
241
0
  if(d->rr_len[r] < 2+5+saltlen+1+*nextlen) {
242
0
    *next = 0;
243
0
    *nextlen = 0;
244
0
    return 0; /* malformed */
245
0
  }
246
0
  *next = d->rr_data[r]+2+5+saltlen+1;
247
0
  return 1;
248
0
}
249
250
size_t nsec3_hash_to_b32(uint8_t* hash, size_t hashlen, uint8_t* zone,
251
  size_t zonelen, uint8_t* buf, size_t max)
252
0
{
253
  /* write b32 of name, leave one for length */
254
0
  int ret;
255
0
  if(max < hashlen*2+1) /* quick approx of b32, as if hexb16 */
256
0
    return 0;
257
0
  ret = sldns_b32_ntop_extended_hex(hash, hashlen, (char*)buf+1, max-1);
258
0
  if(ret < 1) 
259
0
    return 0;
260
0
  buf[0] = (uint8_t)ret; /* length of b32 label */
261
0
  ret++;
262
0
  if(max - ret < zonelen)
263
0
    return 0;
264
0
  memmove(buf+ret, zone, zonelen);
265
0
  return zonelen+(size_t)ret;
266
0
}
267
268
size_t nsec3_get_nextowner_b32(struct ub_packed_rrset_key* rrset, int r,
269
  uint8_t* buf, size_t max)
270
0
{
271
0
  uint8_t* nm, *zone;
272
0
  size_t nmlen, zonelen;
273
0
  if(!nsec3_get_nextowner(rrset, r, &nm, &nmlen))
274
0
    return 0;
275
  /* append zone name; the owner name must be <b32>.zone */
276
0
  zone = rrset->rk.dname;
277
0
  zonelen = rrset->rk.dname_len;
278
0
  dname_remove_label(&zone, &zonelen);
279
0
  return nsec3_hash_to_b32(nm, nmlen, zone, zonelen, buf, max);
280
0
}
281
282
int
283
nsec3_has_type(struct ub_packed_rrset_key* rrset, int r, uint16_t type)
284
0
{
285
0
  uint8_t* bitmap;
286
0
  size_t bitlen, skiplen;
287
0
        struct packed_rrset_data* d = (struct packed_rrset_data*)
288
0
          rrset->entry.data;
289
0
  log_assert(d && r < (int)d->count);
290
0
  skiplen = 2+4;
291
  /* skip salt */
292
0
  if(d->rr_len[r] < skiplen+1)
293
0
    return 0; /* malformed, too short */
294
0
  skiplen += 1+(size_t)d->rr_data[r][skiplen]; 
295
  /* skip next hashed owner */
296
0
  if(d->rr_len[r] < skiplen+1)
297
0
    return 0; /* malformed, too short */
298
0
  skiplen += 1+(size_t)d->rr_data[r][skiplen]; 
299
0
  if(d->rr_len[r] < skiplen)
300
0
    return 0; /* malformed, too short */
301
0
  bitlen = d->rr_len[r] - skiplen;
302
0
  bitmap = d->rr_data[r]+skiplen;
303
0
  return nsecbitmap_has_type_rdata(bitmap, bitlen, type);
304
0
}
305
  
306
/** 
307
 * Iterate through NSEC3 list, per RR 
308
 * This routine gives the next RR in the list (or sets rrset null). 
309
 * Usage:
310
 *
311
 * size_t rrsetnum;
312
 * int rrnum;
313
 * struct ub_packed_rrset_key* rrset;
314
 * for(rrset=filter_first(filter, &rrsetnum, &rrnum); rrset; 
315
 *  rrset=filter_next(filter, &rrsetnum, &rrnum))
316
 *    do_stuff;
317
 * 
318
 * Also filters out 
319
 *  o unknown flag NSEC3s
320
 *  o unknown algorithm NSEC3s.
321
 * @param filter: nsec3 filter structure.
322
 * @param rrsetnum: in/out rrset number to look at.
323
 * @param rrnum: in/out rr number in rrset to look at.
324
 * @returns ptr to the next rrset (or NULL at end).
325
 */
326
static struct ub_packed_rrset_key*
327
filter_next(struct nsec3_filter* filter, size_t* rrsetnum, int* rrnum)
328
0
{
329
0
  size_t i;
330
0
  int r;
331
0
  uint8_t* nm;
332
0
  size_t nmlen;
333
0
  if(!filter->zone) /* empty list */
334
0
    return NULL;
335
0
  for(i=*rrsetnum; i<filter->num; i++) {
336
    /* see if RRset qualifies */
337
0
    if(ntohs(filter->list[i]->rk.type) != LDNS_RR_TYPE_NSEC3 ||
338
0
      ntohs(filter->list[i]->rk.rrset_class) != 
339
0
      filter->fclass) 
340
0
      continue;
341
    /* check RRset zone */
342
0
    nm = filter->list[i]->rk.dname;
343
0
    nmlen = filter->list[i]->rk.dname_len;
344
0
    dname_remove_label(&nm, &nmlen);
345
0
    if(query_dname_compare(nm, filter->zone) != 0)
346
0
      continue;
347
0
    if(i == *rrsetnum)
348
0
      r = (*rrnum) + 1; /* continue at next RR */
349
0
    else  r = 0;   /* new RRset start at first RR */
350
0
    for(; r < (int)rrset_get_count(filter->list[i]); r++) {
351
      /* skip unknown flags, algo */
352
0
      if(nsec3_unknown_flags(filter->list[i], r) ||
353
0
        !nsec3_known_algo(filter->list[i], r))
354
0
        continue;
355
      /* this one is a good target */
356
0
      *rrsetnum = i;
357
0
      *rrnum = r;
358
0
      return filter->list[i];
359
0
    }
360
0
  }
361
0
  return NULL;
362
0
}
363
364
/**
365
 * Start iterating over NSEC3 records.
366
 * @param filter: the filter structure, must have been filter_init-ed.
367
 * @param rrsetnum: can be undefined on call, initialised.
368
 * @param rrnum: can be undefined on call, initialised.
369
 * @return first rrset of an NSEC3, together with rrnum this points to
370
 *  the first RR to examine. Is NULL on empty list.
371
 */
372
static struct ub_packed_rrset_key*
373
filter_first(struct nsec3_filter* filter, size_t* rrsetnum, int* rrnum)
374
0
{
375
0
  *rrsetnum = 0;
376
0
  *rrnum = -1;
377
0
  return filter_next(filter, rrsetnum, rrnum);
378
0
}
379
380
/** see if at least one RR is known (flags, algo) */
381
static int
382
nsec3_rrset_has_known(struct ub_packed_rrset_key* s)
383
0
{
384
0
  int r;
385
0
  for(r=0; r < (int)rrset_get_count(s); r++) {
386
0
    if(!nsec3_unknown_flags(s, r) && nsec3_known_algo(s, r))
387
0
      return 1;
388
0
  }
389
0
  return 0;
390
0
}
391
392
/** 
393
 * Initialize the filter structure.
394
 * Finds the zone by looking at available NSEC3 records and best match.
395
 *  (skips the unknown flag and unknown algo NSEC3s).
396
 *
397
 * @param filter: nsec3 filter structure.
398
 * @param list: list of rrsets, an array of them.
399
 * @param num: number of rrsets in list.
400
 * @param qinfo: 
401
 *  query name to match a zone for.
402
 *  query type (if DS a higher zone must be chosen)
403
 *  qclass, to filter NSEC3s with.
404
 */
405
static void
406
filter_init(struct nsec3_filter* filter, struct ub_packed_rrset_key** list,
407
  size_t num, struct query_info* qinfo)
408
0
{
409
0
  size_t i;
410
0
  uint8_t* nm;
411
0
  size_t nmlen;
412
0
  filter->zone = NULL;
413
0
  filter->zone_len = 0;
414
0
  filter->list = list;
415
0
  filter->num = num;
416
0
  filter->fclass = qinfo->qclass;
417
0
  for(i=0; i<num; i++) {
418
    /* ignore other stuff in the list */
419
0
    if(ntohs(list[i]->rk.type) != LDNS_RR_TYPE_NSEC3 ||
420
0
      ntohs(list[i]->rk.rrset_class) != qinfo->qclass) 
421
0
      continue;
422
    /* skip unknown flags, algo */
423
0
    if(!nsec3_rrset_has_known(list[i]))
424
0
      continue;
425
426
    /* since NSEC3s are base32.zonename, we can find the zone
427
     * name by stripping off the first label of the record */
428
0
    nm = list[i]->rk.dname;
429
0
    nmlen = list[i]->rk.dname_len;
430
0
    dname_remove_label(&nm, &nmlen);
431
    /* if we find a domain that can prove about the qname,
432
     * and if this domain is closer to the qname */
433
0
    if(dname_subdomain_c(qinfo->qname, nm) && (!filter->zone ||
434
0
      dname_subdomain_c(nm, filter->zone))) {
435
      /* for a type DS do not accept a zone equal to qname*/
436
0
      if(qinfo->qtype == LDNS_RR_TYPE_DS && 
437
0
        query_dname_compare(qinfo->qname, nm) == 0 &&
438
0
        !dname_is_root(qinfo->qname))
439
0
        continue;
440
0
      filter->zone = nm;
441
0
      filter->zone_len = nmlen;
442
0
    }
443
0
  }
444
0
}
445
446
/**
447
 * Find max iteration count using config settings and key size
448
 * @param ve: validator environment with iteration count config settings.
449
 * @param bits: key size
450
 * @return max iteration count
451
 */
452
static size_t
453
get_max_iter(struct val_env* ve, size_t bits)
454
0
{
455
0
  int i;
456
0
  log_assert(ve->nsec3_keyiter_count > 0);
457
  /* round up to nearest config keysize, linear search, keep it small */
458
0
  for(i=0; i<ve->nsec3_keyiter_count; i++) {
459
0
    if(bits <= ve->nsec3_keysize[i])
460
0
      return ve->nsec3_maxiter[i];
461
0
  }
462
  /* else, use value for biggest key */
463
0
  return ve->nsec3_maxiter[ve->nsec3_keyiter_count-1];
464
0
}
465
466
/** 
467
 * Determine if any of the NSEC3 rrs iteration count is too high, from key.
468
 * @param ve: validator environment with iteration count config settings.
469
 * @param filter: what NSEC3s to loop over.
470
 * @param kkey: key entry used for verification; used for iteration counts.
471
 * @return 1 if some nsec3s are above the max iteration count.
472
 */
473
static int
474
nsec3_iteration_count_high(struct val_env* ve, struct nsec3_filter* filter, 
475
  struct key_entry_key* kkey)
476
0
{
477
0
  size_t rrsetnum;
478
0
  int rrnum;
479
0
  struct ub_packed_rrset_key* rrset;
480
  /* first determine the max number of iterations */
481
0
  size_t bits = key_entry_keysize(kkey);
482
0
  size_t max_iter = get_max_iter(ve, bits);
483
0
  verbose(VERB_ALGO, "nsec3: keysize %d bits, max iterations %d",
484
0
    (int)bits, (int)max_iter);
485
486
0
  for(rrset=filter_first(filter, &rrsetnum, &rrnum); rrset; 
487
0
    rrset=filter_next(filter, &rrsetnum, &rrnum)) {
488
0
    if(nsec3_get_iter(rrset, rrnum) > max_iter)
489
0
      return 1;
490
0
  }
491
0
  return 0;
492
0
}
493
494
/* nsec3_cache_compare for rbtree */
495
int
496
nsec3_hash_cmp(const void* c1, const void* c2) 
497
0
{
498
0
  struct nsec3_cached_hash* h1 = (struct nsec3_cached_hash*)c1;
499
0
  struct nsec3_cached_hash* h2 = (struct nsec3_cached_hash*)c2;
500
0
  uint8_t* s1, *s2;
501
0
  size_t s1len, s2len;
502
0
  int c = query_dname_compare(h1->dname, h2->dname);
503
0
  if(c != 0)
504
0
    return c;
505
  /* compare parameters */
506
  /* if both malformed, its equal, robustness */
507
0
  if(nsec3_get_algo(h1->nsec3, h1->rr) !=
508
0
    nsec3_get_algo(h2->nsec3, h2->rr)) {
509
0
    if(nsec3_get_algo(h1->nsec3, h1->rr) <
510
0
      nsec3_get_algo(h2->nsec3, h2->rr))
511
0
      return -1;
512
0
    return 1;
513
0
  }
514
0
  if(nsec3_get_iter(h1->nsec3, h1->rr) !=
515
0
    nsec3_get_iter(h2->nsec3, h2->rr)) {
516
0
    if(nsec3_get_iter(h1->nsec3, h1->rr) <
517
0
      nsec3_get_iter(h2->nsec3, h2->rr))
518
0
      return -1;
519
0
    return 1;
520
0
  }
521
0
  (void)nsec3_get_salt(h1->nsec3, h1->rr, &s1, &s1len);
522
0
  (void)nsec3_get_salt(h2->nsec3, h2->rr, &s2, &s2len);
523
0
  if(s1len == 0 && s2len == 0)
524
0
    return 0;
525
0
  if(!s1) return -1;
526
0
  if(!s2) return 1;
527
0
  if(s1len != s2len) {
528
0
    if(s1len < s2len)
529
0
      return -1;
530
0
    return 1;
531
0
  }
532
0
  return memcmp(s1, s2, s1len);
533
0
}
534
535
size_t
536
nsec3_get_hashed(sldns_buffer* buf, uint8_t* nm, size_t nmlen, int algo, 
537
  size_t iter, uint8_t* salt, size_t saltlen, uint8_t* res, size_t max)
538
0
{
539
0
  size_t i, hash_len;
540
  /* prepare buffer for first iteration */
541
0
  sldns_buffer_clear(buf);
542
0
  sldns_buffer_write(buf, nm, nmlen);
543
0
  query_dname_tolower(sldns_buffer_begin(buf));
544
0
  sldns_buffer_write(buf, salt, saltlen);
545
0
  sldns_buffer_flip(buf);
546
0
  hash_len = nsec3_hash_algo_size_supported(algo);
547
0
  if(hash_len == 0) {
548
0
    log_err("nsec3 hash of unknown algo %d", algo);
549
0
    return 0;
550
0
  }
551
0
  if(hash_len > max)
552
0
    return 0;
553
0
  if(!secalgo_nsec3_hash(algo, (unsigned char*)sldns_buffer_begin(buf),
554
0
    sldns_buffer_limit(buf), (unsigned char*)res))
555
0
    return 0;
556
0
  for(i=0; i<iter; i++) {
557
0
    sldns_buffer_clear(buf);
558
0
    sldns_buffer_write(buf, res, hash_len);
559
0
    sldns_buffer_write(buf, salt, saltlen);
560
0
    sldns_buffer_flip(buf);
561
0
    if(!secalgo_nsec3_hash(algo,
562
0
      (unsigned char*)sldns_buffer_begin(buf),
563
0
      sldns_buffer_limit(buf), (unsigned char*)res))
564
0
      return 0;
565
0
  }
566
0
  return hash_len;
567
0
}
568
569
/** perform hash of name */
570
static int
571
nsec3_calc_hash(struct regional* region, sldns_buffer* buf, 
572
  struct nsec3_cached_hash* c)
573
0
{
574
0
  int algo = nsec3_get_algo(c->nsec3, c->rr);
575
0
  size_t iter = nsec3_get_iter(c->nsec3, c->rr);
576
0
  uint8_t* salt;
577
0
  size_t saltlen, i;
578
0
  if(!nsec3_get_salt(c->nsec3, c->rr, &salt, &saltlen))
579
0
    return -1;
580
  /* prepare buffer for first iteration */
581
0
  sldns_buffer_clear(buf);
582
0
  sldns_buffer_write(buf, c->dname, c->dname_len);
583
0
  query_dname_tolower(sldns_buffer_begin(buf));
584
0
  sldns_buffer_write(buf, salt, saltlen);
585
0
  sldns_buffer_flip(buf);
586
0
  c->hash_len = nsec3_hash_algo_size_supported(algo);
587
0
  if(c->hash_len == 0) {
588
0
    log_err("nsec3 hash of unknown algo %d", algo);
589
0
    return -1;
590
0
  }
591
0
  c->hash = (uint8_t*)regional_alloc(region, c->hash_len);
592
0
  if(!c->hash)
593
0
    return 0;
594
0
  (void)secalgo_nsec3_hash(algo, (unsigned char*)sldns_buffer_begin(buf),
595
0
    sldns_buffer_limit(buf), (unsigned char*)c->hash);
596
0
  for(i=0; i<iter; i++) {
597
0
    sldns_buffer_clear(buf);
598
0
    sldns_buffer_write(buf, c->hash, c->hash_len);
599
0
    sldns_buffer_write(buf, salt, saltlen);
600
0
    sldns_buffer_flip(buf);
601
0
    (void)secalgo_nsec3_hash(algo,
602
0
      (unsigned char*)sldns_buffer_begin(buf),
603
0
      sldns_buffer_limit(buf), (unsigned char*)c->hash);
604
0
  }
605
0
  return 1;
606
0
}
607
608
/** perform b32 encoding of hash */
609
static int
610
nsec3_calc_b32(struct regional* region, sldns_buffer* buf, 
611
  struct nsec3_cached_hash* c)
612
0
{
613
0
  int r;
614
0
  sldns_buffer_clear(buf);
615
0
  r = sldns_b32_ntop_extended_hex(c->hash, c->hash_len,
616
0
    (char*)sldns_buffer_begin(buf), sldns_buffer_limit(buf));
617
0
  if(r < 1) {
618
0
    log_err("b32_ntop_extended_hex: error in encoding: %d", r);
619
0
    return 0;
620
0
  }
621
0
  c->b32_len = (size_t)r;
622
0
  c->b32 = regional_alloc_init(region, sldns_buffer_begin(buf), 
623
0
    c->b32_len);
624
0
  if(!c->b32)
625
0
    return 0;
626
0
  return 1;
627
0
}
628
629
int
630
nsec3_hash_name(rbtree_type* table, struct regional* region, sldns_buffer* buf,
631
  struct ub_packed_rrset_key* nsec3, int rr, uint8_t* dname, 
632
  size_t dname_len, struct nsec3_cached_hash** hash)
633
0
{
634
0
  struct nsec3_cached_hash* c;
635
0
  struct nsec3_cached_hash looki;
636
#ifdef UNBOUND_DEBUG
637
  rbnode_type* n;
638
#endif
639
0
  int r;
640
0
  looki.node.key = &looki;
641
0
  looki.nsec3 = nsec3;
642
0
  looki.rr = rr;
643
0
  looki.dname = dname;
644
0
  looki.dname_len = dname_len;
645
  /* lookup first in cache */
646
0
  c = (struct nsec3_cached_hash*)rbtree_search(table, &looki);
647
0
  if(c) {
648
0
    *hash = c;
649
0
    return 1;
650
0
  }
651
  /* create a new entry */
652
0
  c = (struct nsec3_cached_hash*)regional_alloc(region, sizeof(*c));
653
0
  if(!c) return 0;
654
0
  c->node.key = c;
655
0
  c->nsec3 = nsec3;
656
0
  c->rr = rr;
657
0
  c->dname = dname;
658
0
  c->dname_len = dname_len;
659
0
  r = nsec3_calc_hash(region, buf, c);
660
0
  if(r != 1)
661
0
    return r;
662
0
  r = nsec3_calc_b32(region, buf, c);
663
0
  if(r != 1)
664
0
    return r;
665
#ifdef UNBOUND_DEBUG
666
  n =
667
#else
668
0
  (void)
669
0
#endif
670
0
  rbtree_insert(table, &c->node);
671
0
  log_assert(n); /* cannot be duplicate, just did lookup */
672
0
  *hash = c;
673
0
  return 1;
674
0
}
675
676
/**
677
 * compare a label lowercased
678
 */
679
static int
680
label_compare_lower(uint8_t* lab1, uint8_t* lab2, size_t lablen)
681
0
{
682
0
  size_t i;
683
0
  for(i=0; i<lablen; i++) {
684
0
    if(tolower((unsigned char)*lab1) != tolower((unsigned char)*lab2)) {
685
0
      if(tolower((unsigned char)*lab1) < tolower((unsigned char)*lab2))
686
0
        return -1;
687
0
      return 1;
688
0
    }
689
0
    lab1++;
690
0
    lab2++;
691
0
  }
692
0
  return 0;
693
0
}
694
695
/**
696
 * Compare a hashed name with the owner name of an NSEC3 RRset.
697
 * @param flt: filter with zone name.
698
 * @param hash: the hashed name.
699
 * @param s: rrset with owner name.
700
 * @return true if matches exactly, false if not.
701
 */
702
static int
703
nsec3_hash_matches_owner(struct nsec3_filter* flt, 
704
  struct nsec3_cached_hash* hash, struct ub_packed_rrset_key* s)
705
0
{
706
0
  uint8_t* nm = s->rk.dname;
707
  /* compare, does hash of name based on params in this NSEC3
708
   * match the owner name of this NSEC3? 
709
   * name must be: <hashlength>base32 . zone name 
710
   * so; first label must not be root label (not zero length),
711
   * and match the b32 encoded hash length, 
712
   * and the label content match the b32 encoded hash
713
   * and the rest must be the zone name.
714
   */
715
0
  if(hash->b32_len != 0 && (size_t)nm[0] == hash->b32_len &&
716
0
    label_compare_lower(nm+1, hash->b32, hash->b32_len) == 0 &&
717
0
    query_dname_compare(nm+(size_t)nm[0]+1, flt->zone) == 0) {
718
0
    return 1;
719
0
  }
720
0
  return 0;
721
0
}
722
723
/**
724
 * Find matching NSEC3
725
 * Find the NSEC3Record that matches a hash of a name.
726
 * @param env: module environment with temporary region and buffer.
727
 * @param flt: the NSEC3 RR filter, contains zone name and RRs.
728
 * @param ct: cached hashes table.
729
 * @param nm: name to look for.
730
 * @param nmlen: length of name.
731
 * @param rrset: nsec3 that matches is returned here.
732
 * @param rr: rr number in nsec3 rrset that matches.
733
 * @return true if a matching NSEC3 is found, false if not.
734
 */
735
static int
736
find_matching_nsec3(struct module_env* env, struct nsec3_filter* flt,
737
  rbtree_type* ct, uint8_t* nm, size_t nmlen, 
738
  struct ub_packed_rrset_key** rrset, int* rr)
739
0
{
740
0
  size_t i_rs;
741
0
  int i_rr;
742
0
  struct ub_packed_rrset_key* s;
743
0
  struct nsec3_cached_hash* hash = NULL;
744
0
  int r;
745
746
  /* this loop skips other-zone and unknown NSEC3s, also non-NSEC3 RRs */
747
0
  for(s=filter_first(flt, &i_rs, &i_rr); s; 
748
0
    s=filter_next(flt, &i_rs, &i_rr)) {
749
    /* get name hashed for this NSEC3 RR */
750
0
    r = nsec3_hash_name(ct, env->scratch, env->scratch_buffer,
751
0
      s, i_rr, nm, nmlen, &hash);
752
0
    if(r == 0) {
753
0
      log_err("nsec3: malloc failure");
754
0
      break; /* alloc failure */
755
0
    } else if(r != 1)
756
0
      continue; /* malformed NSEC3 */
757
0
    else if(nsec3_hash_matches_owner(flt, hash, s)) {
758
0
      *rrset = s; /* rrset with this name */
759
0
      *rr = i_rr; /* matches hash with these parameters */
760
0
      return 1;
761
0
    }
762
0
  }
763
0
  *rrset = NULL;
764
0
  *rr = 0;
765
0
  return 0;
766
0
}
767
768
int
769
nsec3_covers(uint8_t* zone, struct nsec3_cached_hash* hash,
770
  struct ub_packed_rrset_key* rrset, int rr, sldns_buffer* buf)
771
0
{
772
0
  uint8_t* next, *owner;
773
0
  size_t nextlen;
774
0
  int len;
775
0
  if(!nsec3_get_nextowner(rrset, rr, &next, &nextlen))
776
0
    return 0; /* malformed RR proves nothing */
777
778
  /* check the owner name is a hashed value . apex
779
   * base32 encoded values must have equal length. 
780
   * hash_value and next hash value must have equal length. */
781
0
  if(nextlen != hash->hash_len || hash->hash_len==0||hash->b32_len==0|| 
782
0
    (size_t)*rrset->rk.dname != hash->b32_len ||
783
0
    query_dname_compare(rrset->rk.dname+1+
784
0
      (size_t)*rrset->rk.dname, zone) != 0)
785
0
    return 0; /* bad lengths or owner name */
786
787
  /* This is the "normal case: owner < next and owner < hash < next */
788
0
  if(label_compare_lower(rrset->rk.dname+1, hash->b32, 
789
0
    hash->b32_len) < 0 && 
790
0
    memcmp(hash->hash, next, nextlen) < 0)
791
0
    return 1;
792
793
  /* convert owner name from text to binary */
794
0
  sldns_buffer_clear(buf);
795
0
  owner = sldns_buffer_begin(buf);
796
0
  len = sldns_b32_pton_extended_hex((char*)rrset->rk.dname+1, 
797
0
    hash->b32_len, owner, sldns_buffer_limit(buf));
798
0
  if(len<1)
799
0
    return 0; /* bad owner name in some way */
800
0
  if((size_t)len != hash->hash_len || (size_t)len != nextlen)
801
0
    return 0; /* wrong length */
802
803
  /* this is the end of zone case: next <= owner && 
804
   *  (hash > owner || hash < next) 
805
   * this also covers the only-apex case of next==owner.
806
   */
807
0
  if(memcmp(next, owner, nextlen) <= 0 &&
808
0
    ( memcmp(hash->hash, owner, nextlen) > 0 ||
809
0
      memcmp(hash->hash, next, nextlen) < 0)) {
810
0
    return 1;
811
0
  }
812
0
  return 0;
813
0
}
814
815
/**
816
 * findCoveringNSEC3
817
 * Given a name, find a covering NSEC3 from among a list of NSEC3s.
818
 *
819
 * @param env: module environment with temporary region and buffer.
820
 * @param flt: the NSEC3 RR filter, contains zone name and RRs.
821
 * @param ct: cached hashes table.
822
 * @param nm: name to check if covered.
823
 * @param nmlen: length of name.
824
 * @param rrset: covering NSEC3 rrset is returned here.
825
 * @param rr: rr of cover is returned here.
826
 * @return true if a covering NSEC3 is found, false if not.
827
 */
828
static int
829
find_covering_nsec3(struct module_env* env, struct nsec3_filter* flt,
830
        rbtree_type* ct, uint8_t* nm, size_t nmlen, 
831
  struct ub_packed_rrset_key** rrset, int* rr)
832
0
{
833
0
  size_t i_rs;
834
0
  int i_rr;
835
0
  struct ub_packed_rrset_key* s;
836
0
  struct nsec3_cached_hash* hash = NULL;
837
0
  int r;
838
839
  /* this loop skips other-zone and unknown NSEC3s, also non-NSEC3 RRs */
840
0
  for(s=filter_first(flt, &i_rs, &i_rr); s; 
841
0
    s=filter_next(flt, &i_rs, &i_rr)) {
842
    /* get name hashed for this NSEC3 RR */
843
0
    r = nsec3_hash_name(ct, env->scratch, env->scratch_buffer,
844
0
      s, i_rr, nm, nmlen, &hash);
845
0
    if(r == 0) {
846
0
      log_err("nsec3: malloc failure");
847
0
      break; /* alloc failure */
848
0
    } else if(r != 1)
849
0
      continue; /* malformed NSEC3 */
850
0
    else if(nsec3_covers(flt->zone, hash, s, i_rr, 
851
0
      env->scratch_buffer)) {
852
0
      *rrset = s; /* rrset with this name */
853
0
      *rr = i_rr; /* covers hash with these parameters */
854
0
      return 1;
855
0
    }
856
0
  }
857
0
  *rrset = NULL;
858
0
  *rr = 0;
859
0
  return 0;
860
0
}
861
862
/**
863
 * findClosestEncloser
864
 * Given a name and a list of NSEC3s, find the candidate closest encloser.
865
 * This will be the first ancestor of 'name' (including itself) to have a
866
 * matching NSEC3 RR.
867
 * @param env: module environment with temporary region and buffer.
868
 * @param flt: the NSEC3 RR filter, contains zone name and RRs.
869
 * @param ct: cached hashes table.
870
 * @param qinfo: query that is verified for.
871
 * @param ce: closest encloser information is returned in here.
872
 * @return true if a closest encloser candidate is found, false if not.
873
 */
874
static int
875
nsec3_find_closest_encloser(struct module_env* env, struct nsec3_filter* flt, 
876
  rbtree_type* ct, struct query_info* qinfo, struct ce_response* ce)
877
0
{
878
0
  uint8_t* nm = qinfo->qname;
879
0
  size_t nmlen = qinfo->qname_len;
880
881
  /* This scans from longest name to shortest, so the first match 
882
   * we find is the only viable candidate. */
883
884
  /* (David:) FIXME: modify so that the NSEC3 matching the zone apex need 
885
   * not be present. (Mark Andrews idea).
886
   * (Wouter:) But make sure you check for DNAME bit in zone apex,
887
   * if the NSEC3 you find is the only NSEC3 in the zone, then this
888
   * may be the case. */
889
890
0
  while(dname_subdomain_c(nm, flt->zone)) {
891
0
    if(find_matching_nsec3(env, flt, ct, nm, nmlen, 
892
0
      &ce->ce_rrset, &ce->ce_rr)) {
893
0
      ce->ce = nm;
894
0
      ce->ce_len = nmlen;
895
0
      return 1;
896
0
    }
897
0
    dname_remove_label(&nm, &nmlen);
898
0
  }
899
0
  return 0;
900
0
}
901
902
/**
903
 * Given a qname and its proven closest encloser, calculate the "next
904
 * closest" name. Basically, this is the name that is one label longer than
905
 * the closest encloser that is still a subdomain of qname.
906
 *
907
 * @param qname: query name.
908
 * @param qnamelen: length of qname.
909
 * @param ce: closest encloser
910
 * @param nm: result name.
911
 * @param nmlen: length of nm.
912
 */
913
static void
914
next_closer(uint8_t* qname, size_t qnamelen, uint8_t* ce, 
915
  uint8_t** nm, size_t* nmlen)
916
0
{
917
0
  int strip = dname_count_labels(qname) - dname_count_labels(ce) -1;
918
0
  *nm = qname;
919
0
  *nmlen = qnamelen;
920
0
  if(strip>0)
921
0
    dname_remove_labels(nm, nmlen, strip);
922
0
}
923
924
/**
925
 * proveClosestEncloser
926
 * Given a List of nsec3 RRs, find and prove the closest encloser to qname.
927
 * @param env: module environment with temporary region and buffer.
928
 * @param flt: the NSEC3 RR filter, contains zone name and RRs.
929
 * @param ct: cached hashes table.
930
 * @param qinfo: query that is verified for.
931
 * @param prove_does_not_exist: If true, then if the closest encloser 
932
 *  turns out to be qname, then null is returned.
933
 *  If set true, and the return value is true, then you can be 
934
 *  certain that the ce.nc_rrset and ce.nc_rr are set properly.
935
 * @param ce: closest encloser information is returned in here.
936
 * @return bogus if no closest encloser could be proven.
937
 *  secure if a closest encloser could be proven, ce is set.
938
 *  insecure if the closest-encloser candidate turns out to prove
939
 *    that an insecure delegation exists above the qname.
940
 */
941
static enum sec_status
942
nsec3_prove_closest_encloser(struct module_env* env, struct nsec3_filter* flt, 
943
  rbtree_type* ct, struct query_info* qinfo, int prove_does_not_exist,
944
  struct ce_response* ce)
945
0
{
946
0
  uint8_t* nc;
947
0
  size_t nc_len;
948
  /* robust: clean out ce, in case it gets abused later */
949
0
  memset(ce, 0, sizeof(*ce));
950
951
0
  if(!nsec3_find_closest_encloser(env, flt, ct, qinfo, ce)) {
952
0
    verbose(VERB_ALGO, "nsec3 proveClosestEncloser: could "
953
0
      "not find a candidate for the closest encloser.");
954
0
    return sec_status_bogus;
955
0
  }
956
0
  log_nametypeclass(VERB_ALGO, "ce candidate", ce->ce, 0, 0);
957
958
0
  if(query_dname_compare(ce->ce, qinfo->qname) == 0) {
959
0
    if(prove_does_not_exist) {
960
0
      verbose(VERB_ALGO, "nsec3 proveClosestEncloser: "
961
0
        "proved that qname existed, bad");
962
0
      return sec_status_bogus;
963
0
    }
964
    /* otherwise, we need to nothing else to prove that qname 
965
     * is its own closest encloser. */
966
0
    return sec_status_secure;
967
0
  }
968
969
  /* If the closest encloser is actually a delegation, then the 
970
   * response should have been a referral. If it is a DNAME, then 
971
   * it should have been a DNAME response. */
972
0
  if(nsec3_has_type(ce->ce_rrset, ce->ce_rr, LDNS_RR_TYPE_NS) &&
973
0
    !nsec3_has_type(ce->ce_rrset, ce->ce_rr, LDNS_RR_TYPE_SOA)) {
974
0
    if(!nsec3_has_type(ce->ce_rrset, ce->ce_rr, LDNS_RR_TYPE_DS)) {
975
0
      verbose(VERB_ALGO, "nsec3 proveClosestEncloser: "
976
0
        "closest encloser is insecure delegation");
977
0
      return sec_status_insecure;
978
0
    }
979
0
    verbose(VERB_ALGO, "nsec3 proveClosestEncloser: closest "
980
0
      "encloser was a delegation, bad");
981
0
    return sec_status_bogus;
982
0
  }
983
0
  if(nsec3_has_type(ce->ce_rrset, ce->ce_rr, LDNS_RR_TYPE_DNAME)) {
984
0
    verbose(VERB_ALGO, "nsec3 proveClosestEncloser: closest "
985
0
      "encloser was a DNAME, bad");
986
0
    return sec_status_bogus;
987
0
  }
988
  
989
  /* Otherwise, we need to show that the next closer name is covered. */
990
0
  next_closer(qinfo->qname, qinfo->qname_len, ce->ce, &nc, &nc_len);
991
0
  if(!find_covering_nsec3(env, flt, ct, nc, nc_len, 
992
0
    &ce->nc_rrset, &ce->nc_rr)) {
993
0
    verbose(VERB_ALGO, "nsec3: Could not find proof that the "
994
0
              "candidate encloser was the closest encloser");
995
0
    return sec_status_bogus;
996
0
  }
997
0
  return sec_status_secure;
998
0
}
999
1000
/** allocate a wildcard for the closest encloser */
1001
static uint8_t*
1002
nsec3_ce_wildcard(struct regional* region, uint8_t* ce, size_t celen,
1003
  size_t* len)
1004
0
{
1005
0
  uint8_t* nm;
1006
0
  if(celen > LDNS_MAX_DOMAINLEN - 2)
1007
0
    return 0; /* too long */
1008
0
  nm = (uint8_t*)regional_alloc(region, celen+2);
1009
0
  if(!nm) {
1010
0
    log_err("nsec3 wildcard: out of memory");
1011
0
    return 0; /* alloc failure */
1012
0
  }
1013
0
  nm[0] = 1;
1014
0
  nm[1] = (uint8_t)'*'; /* wildcard label */
1015
0
  memmove(nm+2, ce, celen);
1016
0
  *len = celen+2;
1017
0
  return nm;
1018
0
}
1019
1020
/** Do the name error proof */
1021
static enum sec_status
1022
nsec3_do_prove_nameerror(struct module_env* env, struct nsec3_filter* flt, 
1023
  rbtree_type* ct, struct query_info* qinfo)
1024
0
{
1025
0
  struct ce_response ce;
1026
0
  uint8_t* wc;
1027
0
  size_t wclen;
1028
0
  struct ub_packed_rrset_key* wc_rrset;
1029
0
  int wc_rr;
1030
0
  enum sec_status sec;
1031
1032
  /* First locate and prove the closest encloser to qname. We will 
1033
   * use the variant that fails if the closest encloser turns out 
1034
   * to be qname. */
1035
0
  sec = nsec3_prove_closest_encloser(env, flt, ct, qinfo, 1, &ce);
1036
0
  if(sec != sec_status_secure) {
1037
0
    if(sec == sec_status_bogus)
1038
0
      verbose(VERB_ALGO, "nsec3 nameerror proof: failed "
1039
0
        "to prove a closest encloser");
1040
0
    else  verbose(VERB_ALGO, "nsec3 nameerror proof: closest "
1041
0
        "nsec3 is an insecure delegation");
1042
0
    return sec;
1043
0
  }
1044
0
  log_nametypeclass(VERB_ALGO, "nsec3 nameerror: proven ce=", ce.ce,0,0);
1045
1046
  /* At this point, we know that qname does not exist. Now we need 
1047
   * to prove that the wildcard does not exist. */
1048
0
  log_assert(ce.ce);
1049
0
  wc = nsec3_ce_wildcard(env->scratch, ce.ce, ce.ce_len, &wclen);
1050
0
  if(!wc || !find_covering_nsec3(env, flt, ct, wc, wclen, 
1051
0
    &wc_rrset, &wc_rr)) {
1052
0
    verbose(VERB_ALGO, "nsec3 nameerror proof: could not prove "
1053
0
      "that the applicable wildcard did not exist.");
1054
0
    return sec_status_bogus;
1055
0
  }
1056
1057
0
  if(ce.nc_rrset && nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1058
0
    verbose(VERB_ALGO, "nsec3 nameerror proof: nc has optout");
1059
0
    return sec_status_insecure;
1060
0
  }
1061
0
  return sec_status_secure;
1062
0
}
1063
1064
enum sec_status
1065
nsec3_prove_nameerror(struct module_env* env, struct val_env* ve,
1066
  struct ub_packed_rrset_key** list, size_t num,
1067
  struct query_info* qinfo, struct key_entry_key* kkey)
1068
0
{
1069
0
  rbtree_type ct;
1070
0
  struct nsec3_filter flt;
1071
1072
0
  if(!list || num == 0 || !kkey || !key_entry_isgood(kkey))
1073
0
    return sec_status_bogus; /* no valid NSEC3s, bogus */
1074
0
  rbtree_init(&ct, &nsec3_hash_cmp); /* init names-to-hash cache */
1075
0
  filter_init(&flt, list, num, qinfo); /* init RR iterator */
1076
0
  if(!flt.zone)
1077
0
    return sec_status_bogus; /* no RRs */
1078
0
  if(nsec3_iteration_count_high(ve, &flt, kkey))
1079
0
    return sec_status_insecure; /* iteration count too high */
1080
0
  log_nametypeclass(VERB_ALGO, "start nsec3 nameerror proof, zone", 
1081
0
    flt.zone, 0, 0);
1082
0
  return nsec3_do_prove_nameerror(env, &flt, &ct, qinfo);
1083
0
}
1084
1085
/* 
1086
 * No code to handle qtype=NSEC3 specially. 
1087
 * This existed in early drafts, but was later (-05) removed.
1088
 */
1089
1090
/** Do the nodata proof */
1091
static enum sec_status
1092
nsec3_do_prove_nodata(struct module_env* env, struct nsec3_filter* flt, 
1093
  rbtree_type* ct, struct query_info* qinfo)
1094
0
{
1095
0
  struct ce_response ce;
1096
0
  uint8_t* wc;
1097
0
  size_t wclen;
1098
0
  struct ub_packed_rrset_key* rrset;
1099
0
  int rr;
1100
0
  enum sec_status sec;
1101
1102
0
  if(find_matching_nsec3(env, flt, ct, qinfo->qname, qinfo->qname_len, 
1103
0
    &rrset, &rr)) {
1104
    /* cases 1 and 2 */
1105
0
    if(nsec3_has_type(rrset, rr, qinfo->qtype)) {
1106
0
      verbose(VERB_ALGO, "proveNodata: Matching NSEC3 "
1107
0
        "proved that type existed, bogus");
1108
0
      return sec_status_bogus;
1109
0
    } else if(nsec3_has_type(rrset, rr, LDNS_RR_TYPE_CNAME)) {
1110
0
      verbose(VERB_ALGO, "proveNodata: Matching NSEC3 "
1111
0
        "proved that a CNAME existed, bogus");
1112
0
      return sec_status_bogus;
1113
0
    }
1114
1115
    /* 
1116
     * If type DS: filter_init zone find already found a parent
1117
     *   zone, so this nsec3 is from a parent zone. 
1118
     *   o can be not a delegation (unusual query for normal name,
1119
     *    no DS anyway, but we can verify that).
1120
     *   o can be a delegation (which is the usual DS check).
1121
     *   o may not have the SOA bit set (only the top of the
1122
     *    zone, which must have been above the name, has that).
1123
     *    Except for the root; which is checked by itself.
1124
     *
1125
     * If not type DS: matching nsec3 must not be a delegation.
1126
     */
1127
0
    if(qinfo->qtype == LDNS_RR_TYPE_DS && qinfo->qname_len != 1 
1128
0
      && nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA) &&
1129
0
      !dname_is_root(qinfo->qname)) {
1130
0
      verbose(VERB_ALGO, "proveNodata: apex NSEC3 "
1131
0
        "abused for no DS proof, bogus");
1132
0
      return sec_status_bogus;
1133
0
    } else if(qinfo->qtype != LDNS_RR_TYPE_DS && 
1134
0
      nsec3_has_type(rrset, rr, LDNS_RR_TYPE_NS) &&
1135
0
      !nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA)) {
1136
0
      if(!nsec3_has_type(rrset, rr, LDNS_RR_TYPE_DS)) {
1137
0
        verbose(VERB_ALGO, "proveNodata: matching "
1138
0
          "NSEC3 is insecure delegation");
1139
0
        return sec_status_insecure;
1140
0
      }
1141
0
      verbose(VERB_ALGO, "proveNodata: matching "
1142
0
        "NSEC3 is a delegation, bogus");
1143
0
      return sec_status_bogus;
1144
0
    }
1145
0
    return sec_status_secure;
1146
0
  }
1147
1148
  /* For cases 3 - 5, we need the proven closest encloser, and it 
1149
   * can't match qname. Although, at this point, we know that it 
1150
   * won't since we just checked that. */
1151
0
  sec = nsec3_prove_closest_encloser(env, flt, ct, qinfo, 1, &ce);
1152
0
  if(sec == sec_status_bogus) {
1153
0
    verbose(VERB_ALGO, "proveNodata: did not match qname, "
1154
0
              "nor found a proven closest encloser.");
1155
0
    return sec_status_bogus;
1156
0
  } else if(sec==sec_status_insecure && qinfo->qtype!=LDNS_RR_TYPE_DS){
1157
0
    verbose(VERB_ALGO, "proveNodata: closest nsec3 is insecure "
1158
0
              "delegation.");
1159
0
    return sec_status_insecure;
1160
0
  }
1161
1162
  /* Case 3: removed */
1163
1164
  /* Case 4: */
1165
0
  log_assert(ce.ce);
1166
0
  wc = nsec3_ce_wildcard(env->scratch, ce.ce, ce.ce_len, &wclen);
1167
0
  if(wc && find_matching_nsec3(env, flt, ct, wc, wclen, &rrset, &rr)) {
1168
    /* found wildcard */
1169
0
    if(nsec3_has_type(rrset, rr, qinfo->qtype)) {
1170
0
      verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1171
0
        "wildcard had qtype, bogus");
1172
0
      return sec_status_bogus;
1173
0
    } else if(nsec3_has_type(rrset, rr, LDNS_RR_TYPE_CNAME)) {
1174
0
      verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1175
0
        "wildcard had a CNAME, bogus");
1176
0
      return sec_status_bogus;
1177
0
    }
1178
0
    if(qinfo->qtype == LDNS_RR_TYPE_DS && qinfo->qname_len != 1 
1179
0
      && nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA)) {
1180
0
      verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1181
0
        "wildcard for no DS proof has a SOA, bogus");
1182
0
      return sec_status_bogus;
1183
0
    } else if(qinfo->qtype != LDNS_RR_TYPE_DS && 
1184
0
      nsec3_has_type(rrset, rr, LDNS_RR_TYPE_NS) &&
1185
0
      !nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA)) {
1186
0
      verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1187
0
        "wildcard is a delegation, bogus");
1188
0
      return sec_status_bogus;
1189
0
    }
1190
    /* everything is peachy keen, except for optout spans */
1191
0
    if(ce.nc_rrset && nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1192
0
      verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1193
0
        "wildcard is in optout range, insecure");
1194
0
      return sec_status_insecure;
1195
0
    }
1196
0
    return sec_status_secure;
1197
0
  }
1198
1199
  /* Case 5: */
1200
  /* Due to forwarders, cnames, and other collating effects, we
1201
   * can see the ordinary unsigned data from a zone beneath an
1202
   * insecure delegation under an optout here */
1203
0
  if(!ce.nc_rrset) {
1204
0
    verbose(VERB_ALGO, "nsec3 nodata proof: no next closer nsec3");
1205
0
    return sec_status_bogus;
1206
0
  }
1207
1208
  /* We need to make sure that the covering NSEC3 is opt-out. */
1209
0
  log_assert(ce.nc_rrset);
1210
0
  if(!nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1211
0
    if(qinfo->qtype == LDNS_RR_TYPE_DS)
1212
0
      verbose(VERB_ALGO, "proveNodata: covering NSEC3 was not "
1213
0
      "opt-out in an opt-out DS NOERROR/NODATA case.");
1214
0
    else verbose(VERB_ALGO, "proveNodata: could not find matching "
1215
0
      "NSEC3, nor matching wildcard, nor optout NSEC3 "
1216
0
      "-- no more options, bogus.");
1217
0
    return sec_status_bogus;
1218
0
  }
1219
  /* RFC5155 section 9.2: if nc has optout then no AD flag set */
1220
0
  return sec_status_insecure;
1221
0
}
1222
1223
enum sec_status
1224
nsec3_prove_nodata(struct module_env* env, struct val_env* ve,
1225
  struct ub_packed_rrset_key** list, size_t num,
1226
  struct query_info* qinfo, struct key_entry_key* kkey)
1227
0
{
1228
0
  rbtree_type ct;
1229
0
  struct nsec3_filter flt;
1230
1231
0
  if(!list || num == 0 || !kkey || !key_entry_isgood(kkey))
1232
0
    return sec_status_bogus; /* no valid NSEC3s, bogus */
1233
0
  rbtree_init(&ct, &nsec3_hash_cmp); /* init names-to-hash cache */
1234
0
  filter_init(&flt, list, num, qinfo); /* init RR iterator */
1235
0
  if(!flt.zone)
1236
0
    return sec_status_bogus; /* no RRs */
1237
0
  if(nsec3_iteration_count_high(ve, &flt, kkey))
1238
0
    return sec_status_insecure; /* iteration count too high */
1239
0
  return nsec3_do_prove_nodata(env, &flt, &ct, qinfo);
1240
0
}
1241
1242
enum sec_status
1243
nsec3_prove_wildcard(struct module_env* env, struct val_env* ve,
1244
        struct ub_packed_rrset_key** list, size_t num,
1245
  struct query_info* qinfo, struct key_entry_key* kkey, uint8_t* wc)
1246
0
{
1247
0
  rbtree_type ct;
1248
0
  struct nsec3_filter flt;
1249
0
  struct ce_response ce;
1250
0
  uint8_t* nc;
1251
0
  size_t nc_len;
1252
0
  size_t wclen;
1253
0
  (void)dname_count_size_labels(wc, &wclen);
1254
1255
0
  if(!list || num == 0 || !kkey || !key_entry_isgood(kkey))
1256
0
    return sec_status_bogus; /* no valid NSEC3s, bogus */
1257
0
  rbtree_init(&ct, &nsec3_hash_cmp); /* init names-to-hash cache */
1258
0
  filter_init(&flt, list, num, qinfo); /* init RR iterator */
1259
0
  if(!flt.zone)
1260
0
    return sec_status_bogus; /* no RRs */
1261
0
  if(nsec3_iteration_count_high(ve, &flt, kkey))
1262
0
    return sec_status_insecure; /* iteration count too high */
1263
1264
  /* We know what the (purported) closest encloser is by just 
1265
   * looking at the supposed generating wildcard. 
1266
   * The *. has already been removed from the wc name.
1267
   */
1268
0
  memset(&ce, 0, sizeof(ce));
1269
0
  ce.ce = wc;
1270
0
  ce.ce_len = wclen;
1271
1272
  /* Now we still need to prove that the original data did not exist.
1273
   * Otherwise, we need to show that the next closer name is covered. */
1274
0
  next_closer(qinfo->qname, qinfo->qname_len, ce.ce, &nc, &nc_len);
1275
0
  if(!find_covering_nsec3(env, &flt, &ct, nc, nc_len, 
1276
0
    &ce.nc_rrset, &ce.nc_rr)) {
1277
0
    verbose(VERB_ALGO, "proveWildcard: did not find a covering "
1278
0
      "NSEC3 that covered the next closer name.");
1279
0
    return sec_status_bogus;
1280
0
  }
1281
0
  if(ce.nc_rrset && nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1282
0
    verbose(VERB_ALGO, "proveWildcard: NSEC3 optout");
1283
0
    return sec_status_insecure;
1284
0
  }
1285
0
  return sec_status_secure;
1286
0
}
1287
1288
/** test if list is all secure */
1289
static int
1290
list_is_secure(struct module_env* env, struct val_env* ve, 
1291
  struct ub_packed_rrset_key** list, size_t num,
1292
  struct key_entry_key* kkey, char** reason, sldns_ede_code *reason_bogus,
1293
  struct module_qstate* qstate)
1294
0
{
1295
0
  struct packed_rrset_data* d;
1296
0
  size_t i;
1297
0
  for(i=0; i<num; i++) {
1298
0
    d = (struct packed_rrset_data*)list[i]->entry.data;
1299
0
    if(list[i]->rk.type != htons(LDNS_RR_TYPE_NSEC3))
1300
0
      continue;
1301
0
    if(d->security == sec_status_secure)
1302
0
      continue;
1303
0
    rrset_check_sec_status(env->rrset_cache, list[i], *env->now);
1304
0
    if(d->security == sec_status_secure)
1305
0
      continue;
1306
0
    d->security = val_verify_rrset_entry(env, ve, list[i], kkey,
1307
0
      reason, reason_bogus, LDNS_SECTION_AUTHORITY, qstate);
1308
0
    if(d->security != sec_status_secure) {
1309
0
      verbose(VERB_ALGO, "NSEC3 did not verify");
1310
0
      return 0;
1311
0
    }
1312
0
    rrset_update_sec_status(env->rrset_cache, list[i], *env->now);
1313
0
  }
1314
0
  return 1;
1315
0
}
1316
1317
enum sec_status
1318
nsec3_prove_nods(struct module_env* env, struct val_env* ve,
1319
  struct ub_packed_rrset_key** list, size_t num,
1320
  struct query_info* qinfo, struct key_entry_key* kkey, char** reason,
1321
  sldns_ede_code* reason_bogus, struct module_qstate* qstate)
1322
0
{
1323
0
  rbtree_type ct;
1324
0
  struct nsec3_filter flt;
1325
0
  struct ce_response ce;
1326
0
  struct ub_packed_rrset_key* rrset;
1327
0
  int rr;
1328
0
  log_assert(qinfo->qtype == LDNS_RR_TYPE_DS);
1329
1330
0
  if(!list || num == 0 || !kkey || !key_entry_isgood(kkey)) {
1331
0
    *reason = "no valid NSEC3s";
1332
0
    return sec_status_bogus; /* no valid NSEC3s, bogus */
1333
0
  }
1334
0
  if(!list_is_secure(env, ve, list, num, kkey, reason, reason_bogus, qstate)) {
1335
0
    *reason = "not all NSEC3 records secure";
1336
0
    return sec_status_bogus; /* not all NSEC3 records secure */
1337
0
  }
1338
0
  rbtree_init(&ct, &nsec3_hash_cmp); /* init names-to-hash cache */
1339
0
  filter_init(&flt, list, num, qinfo); /* init RR iterator */
1340
0
  if(!flt.zone) {
1341
0
    *reason = "no NSEC3 records";
1342
0
    return sec_status_bogus; /* no RRs */
1343
0
  }
1344
0
  if(nsec3_iteration_count_high(ve, &flt, kkey))
1345
0
    return sec_status_insecure; /* iteration count too high */
1346
1347
  /* Look for a matching NSEC3 to qname -- this is the normal 
1348
   * NODATA case. */
1349
0
  if(find_matching_nsec3(env, &flt, &ct, qinfo->qname, qinfo->qname_len, 
1350
0
    &rrset, &rr)) {
1351
    /* If the matching NSEC3 has the SOA bit set, it is from 
1352
     * the wrong zone (the child instead of the parent). If 
1353
     * it has the DS bit set, then we were lied to. */
1354
0
    if(nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA) && 
1355
0
      qinfo->qname_len != 1) {
1356
0
      verbose(VERB_ALGO, "nsec3 provenods: NSEC3 is from"
1357
0
        " child zone, bogus");
1358
0
      *reason = "NSEC3 from child zone";
1359
0
      return sec_status_bogus;
1360
0
    } else if(nsec3_has_type(rrset, rr, LDNS_RR_TYPE_DS)) {
1361
0
      verbose(VERB_ALGO, "nsec3 provenods: NSEC3 has qtype"
1362
0
        " DS, bogus");
1363
0
      *reason = "NSEC3 has DS in bitmap";
1364
0
      return sec_status_bogus;
1365
0
    }
1366
    /* If the NSEC3 RR doesn't have the NS bit set, then 
1367
     * this wasn't a delegation point. */
1368
0
    if(!nsec3_has_type(rrset, rr, LDNS_RR_TYPE_NS))
1369
0
      return sec_status_indeterminate;
1370
    /* Otherwise, this proves no DS. */
1371
0
    return sec_status_secure;
1372
0
  }
1373
1374
  /* Otherwise, we are probably in the opt-out case. */
1375
0
  if(nsec3_prove_closest_encloser(env, &flt, &ct, qinfo, 1, &ce)
1376
0
    != sec_status_secure) {
1377
    /* an insecure delegation *above* the qname does not prove
1378
     * anything about this qname exactly, and bogus is bogus */
1379
0
    verbose(VERB_ALGO, "nsec3 provenods: did not match qname, "
1380
0
              "nor found a proven closest encloser.");
1381
0
    *reason = "no NSEC3 closest encloser";
1382
0
    return sec_status_bogus;
1383
0
  }
1384
1385
  /* robust extra check */
1386
0
  if(!ce.nc_rrset) {
1387
0
    verbose(VERB_ALGO, "nsec3 nods proof: no next closer nsec3");
1388
0
    *reason = "no NSEC3 next closer";
1389
0
    return sec_status_bogus;
1390
0
  }
1391
1392
  /* we had the closest encloser proof, then we need to check that the
1393
   * covering NSEC3 was opt-out -- the proveClosestEncloser step already
1394
   * checked to see if the closest encloser was a delegation or DNAME.
1395
   */
1396
0
  log_assert(ce.nc_rrset);
1397
0
  if(!nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1398
0
    verbose(VERB_ALGO, "nsec3 provenods: covering NSEC3 was not "
1399
0
      "opt-out in an opt-out DS NOERROR/NODATA case.");
1400
0
    *reason = "covering NSEC3 was not opt-out in an opt-out "
1401
0
      "DS NOERROR/NODATA case";
1402
0
    return sec_status_bogus;
1403
0
  }
1404
  /* RFC5155 section 9.2: if nc has optout then no AD flag set */
1405
0
  return sec_status_insecure;
1406
0
}
1407
1408
enum sec_status
1409
nsec3_prove_nxornodata(struct module_env* env, struct val_env* ve,
1410
  struct ub_packed_rrset_key** list, size_t num, 
1411
  struct query_info* qinfo, struct key_entry_key* kkey, int* nodata)
1412
0
{
1413
0
  enum sec_status sec, secnx;
1414
0
  rbtree_type ct;
1415
0
  struct nsec3_filter flt;
1416
0
  *nodata = 0;
1417
1418
0
  if(!list || num == 0 || !kkey || !key_entry_isgood(kkey))
1419
0
    return sec_status_bogus; /* no valid NSEC3s, bogus */
1420
0
  rbtree_init(&ct, &nsec3_hash_cmp); /* init names-to-hash cache */
1421
0
  filter_init(&flt, list, num, qinfo); /* init RR iterator */
1422
0
  if(!flt.zone)
1423
0
    return sec_status_bogus; /* no RRs */
1424
0
  if(nsec3_iteration_count_high(ve, &flt, kkey))
1425
0
    return sec_status_insecure; /* iteration count too high */
1426
1427
  /* try nxdomain and nodata after another, while keeping the
1428
   * hash cache intact */
1429
1430
0
  secnx = nsec3_do_prove_nameerror(env, &flt, &ct, qinfo);
1431
0
  if(secnx==sec_status_secure)
1432
0
    return sec_status_secure;
1433
0
  sec = nsec3_do_prove_nodata(env, &flt, &ct, qinfo);
1434
0
  if(sec==sec_status_secure) {
1435
0
    *nodata = 1;
1436
0
  } else if(sec == sec_status_insecure) {
1437
0
    *nodata = 1;
1438
0
  } else if(secnx == sec_status_insecure) {
1439
0
    sec = sec_status_insecure;
1440
0
  }
1441
0
  return sec;
1442
0
}