2 * validator/val_anchor.c - validator trust anchor storage.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains storage for the trust anchors for the validator.
43 #include "validator/val_anchor.h"
44 #include "validator/val_sigcrypt.h"
45 #include "validator/autotrust.h"
46 #include "util/data/packed_rrset.h"
47 #include "util/data/dname.h"
49 #include "util/net_help.h"
50 #include "util/config_file.h"
51 #include "ldns/sbuffer.h"
52 #include "ldns/rrdef.h"
53 #include "ldns/str2wire.h"
59 anchor_cmp(const void* k1, const void* k2)
62 struct trust_anchor* n1 = (struct trust_anchor*)k1;
63 struct trust_anchor* n2 = (struct trust_anchor*)k2;
64 /* no need to ntohs(class) because sort order is irrelevant */
65 if(n1->dclass != n2->dclass) {
66 if(n1->dclass < n2->dclass)
70 return dname_lab_cmp(n1->name, n1->namelabs, n2->name, n2->namelabs,
77 struct val_anchors* a = (struct val_anchors*)calloc(1, sizeof(*a));
80 a->tree = rbtree_create(anchor_cmp);
85 a->autr = autr_global_create();
90 lock_basic_init(&a->lock);
91 lock_protect(&a->lock, a, sizeof(*a));
92 lock_protect(&a->lock, a->autr, sizeof(*a->autr));
96 /** delete assembled rrset */
98 assembled_rrset_delete(struct ub_packed_rrset_key* pkey)
101 if(pkey->entry.data) {
102 struct packed_rrset_data* pd = (struct packed_rrset_data*)
109 free(pkey->rk.dname);
113 /** destroy locks in tree and delete autotrust anchors */
115 anchors_delfunc(rbnode_t* elem, void* ATTR_UNUSED(arg))
117 struct trust_anchor* ta = (struct trust_anchor*)elem;
120 autr_point_delete(ta);
122 struct ta_key* p, *np;
123 lock_basic_destroy(&ta->lock);
132 assembled_rrset_delete(ta->ds_rrset);
133 assembled_rrset_delete(ta->dnskey_rrset);
139 anchors_delete(struct val_anchors* anchors)
143 lock_unprotect(&anchors->lock, anchors->autr);
144 lock_unprotect(&anchors->lock, anchors);
145 lock_basic_destroy(&anchors->lock);
147 traverse_postorder(anchors->tree, anchors_delfunc, NULL);
149 autr_global_delete(anchors->autr);
154 anchors_init_parents_locked(struct val_anchors* anchors)
156 struct trust_anchor* node, *prev = NULL, *p;
158 /* nobody else can grab locks because we hold the main lock.
159 * Thus the previous items, after unlocked, are not deleted */
160 RBTREE_FOR(node, struct trust_anchor*, anchors->tree) {
161 lock_basic_lock(&node->lock);
163 if(!prev || prev->dclass != node->dclass) {
165 lock_basic_unlock(&node->lock);
168 (void)dname_lab_cmp(prev->name, prev->namelabs, node->name,
169 node->namelabs, &m); /* we know prev is smaller */
170 /* sort order like: . com. bla.com. zwb.com. net. */
171 /* find the previous, or parent-parent-parent */
172 for(p = prev; p; p = p->parent)
173 /* looking for name with few labels, a parent */
174 if(p->namelabs <= m) {
175 /* ==: since prev matched m, this is closest*/
176 /* <: prev matches more, but is not a parent,
177 * this one is a (grand)parent */
181 lock_basic_unlock(&node->lock);
186 /** initialise parent pointers in the tree */
188 init_parents(struct val_anchors* anchors)
190 lock_basic_lock(&anchors->lock);
191 anchors_init_parents_locked(anchors);
192 lock_basic_unlock(&anchors->lock);
196 anchor_find(struct val_anchors* anchors, uint8_t* name, int namelabs,
197 size_t namelen, uint16_t dclass)
199 struct trust_anchor key;
201 if(!name) return NULL;
204 key.namelabs = namelabs;
205 key.namelen = namelen;
207 lock_basic_lock(&anchors->lock);
208 n = rbtree_search(anchors->tree, &key);
210 lock_basic_lock(&((struct trust_anchor*)n->key)->lock);
212 lock_basic_unlock(&anchors->lock);
215 return (struct trust_anchor*)n->key;
218 /** create new trust anchor object */
219 static struct trust_anchor*
220 anchor_new_ta(struct val_anchors* anchors, uint8_t* name, int namelabs,
221 size_t namelen, uint16_t dclass, int lockit)
226 struct trust_anchor* ta = (struct trust_anchor*)malloc(
227 sizeof(struct trust_anchor));
230 memset(ta, 0, sizeof(*ta));
232 ta->name = memdup(name, namelen);
237 ta->namelabs = namelabs;
238 ta->namelen = namelen;
240 lock_basic_init(&ta->lock);
242 lock_basic_lock(&anchors->lock);
249 rbtree_insert(anchors->tree, &ta->node);
251 lock_basic_unlock(&anchors->lock);
253 log_assert(r != NULL);
257 /** find trustanchor key by exact data match */
258 static struct ta_key*
259 anchor_find_key(struct trust_anchor* ta, uint8_t* rdata, size_t rdata_len,
263 for(k = ta->keylist; k; k = k->next) {
264 if(k->type == type && k->len == rdata_len &&
265 memcmp(k->data, rdata, rdata_len) == 0)
271 /** create new trustanchor key */
272 static struct ta_key*
273 anchor_new_ta_key(uint8_t* rdata, size_t rdata_len, uint16_t type)
275 struct ta_key* k = (struct ta_key*)malloc(sizeof(*k));
278 memset(k, 0, sizeof(*k));
279 k->data = memdup(rdata, rdata_len);
290 * This routine adds a new RR to a trust anchor. The trust anchor may not
291 * exist yet, and is created if not. The RR can be DS or DNSKEY.
292 * This routine will also remove duplicates; storing them only once.
293 * @param anchors: anchor storage.
294 * @param name: name of trust anchor (wireformat)
295 * @param type: type or RR
296 * @param dclass: class of RR
297 * @param rdata: rdata wireformat, starting with rdlength.
298 * If NULL, nothing is stored, but an entry is created.
299 * @param rdata_len: length of rdata including rdlength.
300 * @return: NULL on error, else the trust anchor.
302 static struct trust_anchor*
303 anchor_store_new_key(struct val_anchors* anchors, uint8_t* name, uint16_t type,
304 uint16_t dclass, uint8_t* rdata, size_t rdata_len)
307 struct trust_anchor* ta;
310 namelabs = dname_count_size_labels(name, &namelen);
311 if(type != LDNS_RR_TYPE_DS && type != LDNS_RR_TYPE_DNSKEY) {
312 log_err("Bad type for trust anchor");
315 /* lookup or create trustanchor */
316 ta = anchor_find(anchors, name, namelabs, namelen, dclass);
318 ta = anchor_new_ta(anchors, name, namelabs, namelen, dclass, 1);
321 lock_basic_lock(&ta->lock);
324 lock_basic_unlock(&ta->lock);
327 /* look for duplicates */
328 if(anchor_find_key(ta, rdata, rdata_len, type)) {
329 lock_basic_unlock(&ta->lock);
332 k = anchor_new_ta_key(rdata, rdata_len, type);
334 lock_basic_unlock(&ta->lock);
338 if(type == LDNS_RR_TYPE_DS)
340 else ta->numDNSKEY++;
341 k->next = ta->keylist;
343 lock_basic_unlock(&ta->lock);
348 * Add new RR. It converts ldns RR to wire format.
349 * @param anchors: anchor storage.
350 * @param rr: the wirerr.
351 * @param rl: length of rr.
352 * @param dl: length of dname.
353 * @return NULL on error, else the trust anchor.
355 static struct trust_anchor*
356 anchor_store_new_rr(struct val_anchors* anchors, uint8_t* rr, size_t rl,
359 struct trust_anchor* ta;
360 if(!(ta=anchor_store_new_key(anchors, rr,
361 sldns_wirerr_get_type(rr, rl, dl),
362 sldns_wirerr_get_class(rr, rl, dl),
363 sldns_wirerr_get_rdatawl(rr, rl, dl),
364 sldns_wirerr_get_rdatalen(rr, rl, dl)+2))) {
367 log_nametypeclass(VERB_QUERY, "adding trusted key",
368 rr, sldns_wirerr_get_type(rr, rl, dl),
369 sldns_wirerr_get_class(rr, rl, dl));
374 * Insert insecure anchor
375 * @param anchors: anchor storage.
376 * @param str: the domain name.
377 * @return NULL on error, Else last trust anchor point
379 static struct trust_anchor*
380 anchor_insert_insecure(struct val_anchors* anchors, const char* str)
382 struct trust_anchor* ta;
383 size_t dname_len = 0;
384 uint8_t* nm = sldns_str2wire_dname(str, &dname_len);
386 log_err("parse error in domain name '%s'", str);
389 ta = anchor_store_new_key(anchors, nm, LDNS_RR_TYPE_DS,
390 LDNS_RR_CLASS_IN, NULL, 0);
396 anchor_store_str(struct val_anchors* anchors, sldns_buffer* buffer,
399 struct trust_anchor* ta;
400 uint8_t* rr = sldns_buffer_begin(buffer);
401 size_t len = sldns_buffer_capacity(buffer), dname_len = 0;
402 int status = sldns_str2wire_rr_buf(str, rr, &len, &dname_len,
403 0, NULL, 0, NULL, 0);
405 log_err("error parsing trust anchor %s: at %d: %s",
406 str, LDNS_WIREPARSE_OFFSET(status),
407 sldns_get_errorstr_parse(status));
410 if(!(ta=anchor_store_new_rr(anchors, rr, len, dname_len))) {
411 log_err("out of memory");
418 * Read a file with trust anchors
419 * @param anchors: anchor storage.
420 * @param buffer: parsing buffer.
421 * @param fname: string.
422 * @param onlyone: only one trust anchor allowed in file.
423 * @return NULL on error. Else last trust-anchor point.
425 static struct trust_anchor*
426 anchor_read_file(struct val_anchors* anchors, sldns_buffer* buffer,
427 const char* fname, int onlyone)
429 struct trust_anchor* ta = NULL, *tanew;
430 struct sldns_file_parse_state pst;
432 size_t len, dname_len;
433 uint8_t* rr = sldns_buffer_begin(buffer);
435 FILE* in = fopen(fname, "r");
437 log_err("error opening file %s: %s", fname, strerror(errno));
440 memset(&pst, 0, sizeof(pst));
441 pst.default_ttl = 3600;
444 len = sldns_buffer_capacity(buffer);
446 status = sldns_fp2wire_rr_buf(in, rr, &len, &dname_len, &pst);
447 if(len == 0) /* empty, $TTL, $ORIGIN */
450 log_err("parse error in %s:%d:%d : %s", fname,
451 pst.lineno, LDNS_WIREPARSE_OFFSET(status),
452 sldns_get_errorstr_parse(status));
456 if(sldns_wirerr_get_type(rr, len, dname_len) !=
457 LDNS_RR_TYPE_DS && sldns_wirerr_get_type(rr, len,
458 dname_len) != LDNS_RR_TYPE_DNSKEY) {
461 if(!(tanew=anchor_store_new_rr(anchors, rr, len, dname_len))) {
462 log_err("mem error at %s line %d", fname, pst.lineno);
466 if(onlyone && ta && ta != tanew) {
467 log_err("error at %s line %d: no multiple anchor "
468 "domains allowed (you can have multiple "
469 "keys, but they must have the same name).",
478 /* empty file is OK when multiple anchors are allowed */
479 if(!onlyone && !ta) return (struct trust_anchor*)1;
483 /** skip file to end of line */
485 skip_to_eol(FILE* in)
488 while((c = getc(in)) != EOF ) {
494 /** true for special characters in bind configs */
496 is_bind_special(int c)
509 * Read a keyword skipping bind comments; spaces, specials, restkeywords.
510 * The file is split into the following tokens:
511 * * special characters, on their own, rdlen=1, { } doublequote ;
512 * * whitespace becomes a single ' ' or tab. Newlines become spaces.
513 * * other words ('keywords')
514 * * comments are skipped if desired
515 * / / C++ style comment to end of line
517 * / * C style comment * /
518 * @param in: file to read from.
519 * @param buf: buffer, what is read is stored after current buffer position.
520 * Space is left in the buffer to write a terminating 0.
521 * @param line: line number is increased per line, for error reports.
522 * @param comments: if 0, comments are not possible and become text.
523 * if 1, comments are skipped entirely.
524 * In BIND files, this is when reading quoted strings, for example
525 * " base 64 text with / / in there "
526 * @return the number of character written to the buffer.
530 readkeyword_bindfile(FILE* in, sldns_buffer* buf, int* line, int comments)
534 while((c = getc(in)) != EOF ) {
535 if(comments && c == '#') { /* # blabla */
539 } else if(comments && c=='/' && numdone>0 && /* /_/ bla*/
540 sldns_buffer_read_u8_at(buf,
541 sldns_buffer_position(buf)-1) == '/') {
542 sldns_buffer_skip(buf, -1);
547 } else if(comments && c=='*' && numdone>0 && /* /_* bla *_/ */
548 sldns_buffer_read_u8_at(buf,
549 sldns_buffer_position(buf)-1) == '/') {
550 sldns_buffer_skip(buf, -1);
552 /* skip to end of comment */
553 while(c != EOF && (c=getc(in)) != EOF ) {
555 if((c=getc(in)) == '/')
563 /* not a comment, complete the keyword */
565 /* check same type */
566 if(isspace((unsigned char)c)) {
570 if(is_bind_special(c)) {
579 /* space for 1 char + 0 string terminator */
580 if(sldns_buffer_remaining(buf) < 2) {
581 fatal_exit("trusted-keys, %d, string too long", *line);
583 sldns_buffer_write_u8(buf, (uint8_t)c);
585 if(isspace((unsigned char)c)) {
586 /* collate whitespace into ' ' */
587 while((c = getc(in)) != EOF ) {
590 if(!isspace((unsigned char)c)) {
597 if(is_bind_special(c))
603 /** skip through file to { or ; */
605 skip_to_special(FILE* in, sldns_buffer* buf, int* line, int spec)
608 sldns_buffer_clear(buf);
609 while((rdlen=readkeyword_bindfile(in, buf, line, 1))) {
610 if(rdlen == 1 && isspace((unsigned char)*sldns_buffer_begin(buf))) {
611 sldns_buffer_clear(buf);
614 if(rdlen != 1 || *sldns_buffer_begin(buf) != (uint8_t)spec) {
615 sldns_buffer_write_u8(buf, 0);
616 log_err("trusted-keys, line %d, expected %c",
622 log_err("trusted-keys, line %d, expected %c got EOF", *line, spec);
627 * read contents of trusted-keys{ ... ; clauses and insert keys into storage.
628 * @param anchors: where to store keys
629 * @param buf: buffer to use
630 * @param line: line number in file
631 * @param in: file to read from.
632 * @return 0 on error.
635 process_bind_contents(struct val_anchors* anchors, sldns_buffer* buf,
638 /* loop over contents, collate strings before ; */
639 /* contents is (numbered): 0 1 2 3 4 5 6 7 8 */
640 /* name. 257 3 5 base64 base64 */
641 /* quoted value: 0 "111" 0 0 0 0 0 0 0 */
642 /* comments value: 1 "000" 1 1 1 "0 0 0 0" 1 */
648 sldns_buffer_clear(buf);
649 while((rdlen=readkeyword_bindfile(in, buf, line, comments))) {
650 if(rdlen == 1 && sldns_buffer_position(buf) == 1
651 && isspace((unsigned char)*sldns_buffer_begin(buf))) {
652 /* starting whitespace is removed */
653 sldns_buffer_clear(buf);
655 } else if(rdlen == 1 && sldns_buffer_current(buf)[-1] == '"') {
656 /* remove " from the string */
661 sldns_buffer_skip(buf, -1);
662 if(contnum > 0 && quoted) {
663 if(sldns_buffer_remaining(buf) < 8+1) {
664 log_err("line %d, too long", *line);
667 sldns_buffer_write(buf, " DNSKEY ", 8);
670 } else if(contnum > 0)
671 comments = !comments;
673 } else if(rdlen == 1 && sldns_buffer_current(buf)[-1] == ';') {
676 sldns_buffer_write_u8(buf, 0);
677 log_err("line %d, bad key", *line);
680 sldns_buffer_skip(buf, -1);
681 sldns_buffer_write_u8(buf, 0);
682 str = strdup((char*)sldns_buffer_begin(buf));
684 log_err("line %d, allocation failure", *line);
687 if(!anchor_store_str(anchors, buf, str)) {
688 log_err("line %d, bad key", *line);
693 sldns_buffer_clear(buf);
698 } else if(rdlen == 1 && sldns_buffer_current(buf)[-1] == '}') {
700 sldns_buffer_write_u8(buf, 0);
701 log_err("line %d, bad key before }", *line);
705 } else if(rdlen == 1 &&
706 isspace((unsigned char)sldns_buffer_current(buf)[-1])) {
707 /* leave whitespace here */
709 /* not space or whatnot, so actual content */
711 if(contnum == 1 && !quoted) {
712 if(sldns_buffer_remaining(buf) < 8+1) {
713 log_err("line %d, too long", *line);
716 sldns_buffer_write(buf, " DNSKEY ", 8);
721 log_err("line %d, EOF before }", *line);
726 * Read a BIND9 like file with trust anchors in named.conf format.
727 * @param anchors: anchor storage.
728 * @param buffer: parsing buffer.
729 * @param fname: string.
730 * @return false on error.
733 anchor_read_bind_file(struct val_anchors* anchors, sldns_buffer* buffer,
737 FILE* in = fopen(fname, "r");
740 log_err("error opening file %s: %s", fname, strerror(errno));
743 verbose(VERB_QUERY, "reading in bind-compat-mode: '%s'", fname);
744 /* scan for trusted-keys keyword, ignore everything else */
745 sldns_buffer_clear(buffer);
746 while((rdlen=readkeyword_bindfile(in, buffer, &line_nr, 1)) != 0) {
747 if(rdlen != 12 || strncmp((char*)sldns_buffer_begin(buffer),
748 "trusted-keys", 12) != 0) {
749 sldns_buffer_clear(buffer);
750 /* ignore everything but trusted-keys */
753 if(!skip_to_special(in, buffer, &line_nr, '{')) {
754 log_err("error in trusted key: \"%s\"", fname);
758 /* process contents */
759 if(!process_bind_contents(anchors, buffer, &line_nr, in)) {
760 log_err("error in trusted key: \"%s\"", fname);
764 if(!skip_to_special(in, buffer, &line_nr, ';')) {
765 log_err("error in trusted key: \"%s\"", fname);
769 sldns_buffer_clear(buffer);
776 * Read a BIND9 like files with trust anchors in named.conf format.
777 * Performs wildcard processing of name.
778 * @param anchors: anchor storage.
779 * @param buffer: parsing buffer.
780 * @param pat: pattern string. (can be wildcarded)
781 * @return false on error.
784 anchor_read_bind_file_wild(struct val_anchors* anchors, sldns_buffer* buffer,
791 if(!strchr(pat, '*') && !strchr(pat, '?') && !strchr(pat, '[') &&
792 !strchr(pat, '{') && !strchr(pat, '~')) {
793 return anchor_read_bind_file(anchors, buffer, pat);
795 verbose(VERB_QUERY, "wildcard found, processing %s", pat);
810 memset(&g, 0, sizeof(g));
811 r = glob(pat, flags, NULL, &g);
814 if(r == GLOB_NOMATCH) {
815 verbose(VERB_QUERY, "trusted-keys-file: "
816 "no matches for %s", pat);
818 } else if(r == GLOB_NOSPACE) {
819 log_err("wildcard trusted-keys-file %s: "
820 "pattern out of memory", pat);
821 } else if(r == GLOB_ABORTED) {
822 log_err("wildcard trusted-keys-file %s: expansion "
823 "aborted (%s)", pat, strerror(errno));
825 log_err("wildcard trusted-keys-file %s: expansion "
826 "failed (%s)", pat, strerror(errno));
828 /* ignore globs that yield no files */
831 /* process files found, if any */
832 for(i=0; i<(size_t)g.gl_pathc; i++) {
833 if(!anchor_read_bind_file(anchors, buffer, g.gl_pathv[i])) {
834 log_err("error reading wildcard "
835 "trusted-keys-file: %s", g.gl_pathv[i]);
842 #else /* not HAVE_GLOB */
843 return anchor_read_bind_file(anchors, buffer, pat);
844 #endif /* HAVE_GLOB */
848 * Assemble an rrset structure for the type
849 * @param ta: trust anchor.
850 * @param num: number of items to fetch from list.
851 * @param type: fetch only items of this type.
852 * @return rrset or NULL on error.
854 static struct ub_packed_rrset_key*
855 assemble_it(struct trust_anchor* ta, size_t num, uint16_t type)
857 struct ub_packed_rrset_key* pkey = (struct ub_packed_rrset_key*)
858 malloc(sizeof(*pkey));
859 struct packed_rrset_data* pd;
864 memset(pkey, 0, sizeof(*pkey));
865 pkey->rk.dname = memdup(ta->name, ta->namelen);
866 if(!pkey->rk.dname) {
871 pkey->rk.dname_len = ta->namelen;
872 pkey->rk.type = htons(type);
873 pkey->rk.rrset_class = htons(ta->dclass);
874 /* The rrset is build in an uncompressed way. This means it
875 * cannot be copied in the normal way. */
876 pd = (struct packed_rrset_data*)malloc(sizeof(*pd));
878 free(pkey->rk.dname);
882 memset(pd, 0, sizeof(*pd));
884 pd->trust = rrset_trust_ultimate;
885 pd->rr_len = (size_t*)malloc(num*sizeof(size_t));
888 free(pkey->rk.dname);
892 pd->rr_ttl = (time_t*)malloc(num*sizeof(time_t));
896 free(pkey->rk.dname);
900 pd->rr_data = (uint8_t**)malloc(num*sizeof(uint8_t*));
905 free(pkey->rk.dname);
911 for(tk = ta->keylist; tk; tk = tk->next) {
914 pd->rr_len[i] = tk->len;
915 /* reuse data ptr to allocation in talist */
916 pd->rr_data[i] = tk->data;
920 pkey->entry.data = (void*)pd;
925 * Assemble structures for the trust DS and DNSKEY rrsets.
926 * @param ta: trust anchor
927 * @return: false on error.
930 anchors_assemble(struct trust_anchor* ta)
933 ta->ds_rrset = assemble_it(ta, ta->numDS, LDNS_RR_TYPE_DS);
937 if(ta->numDNSKEY > 0) {
938 ta->dnskey_rrset = assemble_it(ta, ta->numDNSKEY,
939 LDNS_RR_TYPE_DNSKEY);
940 if(!ta->dnskey_rrset)
947 * Check DS algos for support, warn if not.
948 * @param ta: trust anchor
949 * @return number of DS anchors with unsupported algorithms.
952 anchors_ds_unsupported(struct trust_anchor* ta)
955 for(i=0; i<ta->numDS; i++) {
956 if(!ds_digest_algo_is_supported(ta->ds_rrset, i) ||
957 !ds_key_algo_is_supported(ta->ds_rrset, i))
964 * Check DNSKEY algos for support, warn if not.
965 * @param ta: trust anchor
966 * @return number of DNSKEY anchors with unsupported algorithms.
969 anchors_dnskey_unsupported(struct trust_anchor* ta)
972 for(i=0; i<ta->numDNSKEY; i++) {
973 if(!dnskey_algo_is_supported(ta->dnskey_rrset, i))
980 * Assemble the rrsets in the anchors, ready for use by validator.
981 * @param anchors: trust anchor storage.
982 * @return: false on error.
985 anchors_assemble_rrsets(struct val_anchors* anchors)
987 struct trust_anchor* ta;
988 struct trust_anchor* next;
990 lock_basic_lock(&anchors->lock);
991 ta=(struct trust_anchor*)rbtree_first(anchors->tree);
992 while((rbnode_t*)ta != RBTREE_NULL) {
993 next = (struct trust_anchor*)rbtree_next(&ta->node);
994 lock_basic_lock(&ta->lock);
995 if(ta->autr || (ta->numDS == 0 && ta->numDNSKEY == 0)) {
996 lock_basic_unlock(&ta->lock);
997 ta = next; /* skip */
1000 if(!anchors_assemble(ta)) {
1001 log_err("out of memory");
1002 lock_basic_unlock(&ta->lock);
1003 lock_basic_unlock(&anchors->lock);
1006 nods = anchors_ds_unsupported(ta);
1007 nokey = anchors_dnskey_unsupported(ta);
1009 log_nametypeclass(0, "warning: unsupported "
1010 "algorithm for trust anchor",
1011 ta->name, LDNS_RR_TYPE_DS, ta->dclass);
1014 log_nametypeclass(0, "warning: unsupported "
1015 "algorithm for trust anchor",
1016 ta->name, LDNS_RR_TYPE_DNSKEY, ta->dclass);
1018 if(nods == ta->numDS && nokey == ta->numDNSKEY) {
1020 dname_str(ta->name, b);
1021 log_warn("trust anchor %s has no supported algorithms,"
1022 " the anchor is ignored (check if you need to"
1023 " upgrade unbound and openssl)", b);
1024 (void)rbtree_delete(anchors->tree, &ta->node);
1025 lock_basic_unlock(&ta->lock);
1026 anchors_delfunc(&ta->node, NULL);
1030 lock_basic_unlock(&ta->lock);
1033 lock_basic_unlock(&anchors->lock);
1038 anchors_apply_cfg(struct val_anchors* anchors, struct config_file* cfg)
1040 struct config_strlist* f;
1042 sldns_buffer* parsebuf = sldns_buffer_new(65535);
1043 for(f = cfg->domain_insecure; f; f = f->next) {
1044 if(!f->str || f->str[0] == 0) /* empty "" */
1046 if(!anchor_insert_insecure(anchors, f->str)) {
1047 log_err("error in domain-insecure: %s", f->str);
1048 sldns_buffer_free(parsebuf);
1052 for(f = cfg->trust_anchor_file_list; f; f = f->next) {
1053 if(!f->str || f->str[0] == 0) /* empty "" */
1056 if(cfg->chrootdir && cfg->chrootdir[0] && strncmp(nm,
1057 cfg->chrootdir, strlen(cfg->chrootdir)) == 0)
1058 nm += strlen(cfg->chrootdir);
1059 if(!anchor_read_file(anchors, parsebuf, nm, 0)) {
1060 log_err("error reading trust-anchor-file: %s", f->str);
1061 sldns_buffer_free(parsebuf);
1065 for(f = cfg->trusted_keys_file_list; f; f = f->next) {
1066 if(!f->str || f->str[0] == 0) /* empty "" */
1069 if(cfg->chrootdir && cfg->chrootdir[0] && strncmp(nm,
1070 cfg->chrootdir, strlen(cfg->chrootdir)) == 0)
1071 nm += strlen(cfg->chrootdir);
1072 if(!anchor_read_bind_file_wild(anchors, parsebuf, nm)) {
1073 log_err("error reading trusted-keys-file: %s", f->str);
1074 sldns_buffer_free(parsebuf);
1078 for(f = cfg->trust_anchor_list; f; f = f->next) {
1079 if(!f->str || f->str[0] == 0) /* empty "" */
1081 if(!anchor_store_str(anchors, parsebuf, f->str)) {
1082 log_err("error in trust-anchor: \"%s\"", f->str);
1083 sldns_buffer_free(parsebuf);
1087 if(cfg->dlv_anchor_file && cfg->dlv_anchor_file[0] != 0) {
1088 struct trust_anchor* dlva;
1089 nm = cfg->dlv_anchor_file;
1090 if(cfg->chrootdir && cfg->chrootdir[0] && strncmp(nm,
1091 cfg->chrootdir, strlen(cfg->chrootdir)) == 0)
1092 nm += strlen(cfg->chrootdir);
1093 if(!(dlva = anchor_read_file(anchors, parsebuf,
1095 log_err("error reading dlv-anchor-file: %s",
1096 cfg->dlv_anchor_file);
1097 sldns_buffer_free(parsebuf);
1100 lock_basic_lock(&anchors->lock);
1101 anchors->dlv_anchor = dlva;
1102 lock_basic_unlock(&anchors->lock);
1104 for(f = cfg->dlv_anchor_list; f; f = f->next) {
1105 struct trust_anchor* dlva;
1106 if(!f->str || f->str[0] == 0) /* empty "" */
1108 if(!(dlva = anchor_store_str(
1109 anchors, parsebuf, f->str))) {
1110 log_err("error in dlv-anchor: \"%s\"", f->str);
1111 sldns_buffer_free(parsebuf);
1114 lock_basic_lock(&anchors->lock);
1115 anchors->dlv_anchor = dlva;
1116 lock_basic_unlock(&anchors->lock);
1118 /* do autr last, so that it sees what anchors are filled by other
1119 * means can can print errors about double config for the name */
1120 for(f = cfg->auto_trust_anchor_file_list; f; f = f->next) {
1121 if(!f->str || f->str[0] == 0) /* empty "" */
1124 if(cfg->chrootdir && cfg->chrootdir[0] && strncmp(nm,
1125 cfg->chrootdir, strlen(cfg->chrootdir)) == 0)
1126 nm += strlen(cfg->chrootdir);
1127 if(!autr_read_file(anchors, nm)) {
1128 log_err("error reading auto-trust-anchor-file: %s",
1130 sldns_buffer_free(parsebuf);
1134 /* first assemble, since it may delete useless anchors */
1135 anchors_assemble_rrsets(anchors);
1136 init_parents(anchors);
1137 sldns_buffer_free(parsebuf);
1138 if(verbosity >= VERB_ALGO) autr_debug_print(anchors);
1142 struct trust_anchor*
1143 anchors_lookup(struct val_anchors* anchors,
1144 uint8_t* qname, size_t qname_len, uint16_t qclass)
1146 struct trust_anchor key;
1147 struct trust_anchor* result;
1148 rbnode_t* res = NULL;
1149 key.node.key = &key;
1151 key.namelabs = dname_count_labels(qname);
1152 key.namelen = qname_len;
1153 key.dclass = qclass;
1154 lock_basic_lock(&anchors->lock);
1155 if(rbtree_find_less_equal(anchors->tree, &key, &res)) {
1157 result = (struct trust_anchor*)res;
1159 /* smaller element (or no element) */
1161 result = (struct trust_anchor*)res;
1162 if(!result || result->dclass != qclass) {
1163 lock_basic_unlock(&anchors->lock);
1166 /* count number of labels matched */
1167 (void)dname_lab_cmp(result->name, result->namelabs, key.name,
1169 while(result) { /* go up until qname is subdomain of stub */
1170 if(result->namelabs <= m)
1172 result = result->parent;
1176 lock_basic_lock(&result->lock);
1178 lock_basic_unlock(&anchors->lock);
1183 anchors_get_mem(struct val_anchors* anchors)
1185 struct trust_anchor *ta;
1186 size_t s = sizeof(*anchors);
1189 RBTREE_FOR(ta, struct trust_anchor*, anchors->tree) {
1190 s += sizeof(*ta) + ta->namelen;
1191 /* keys and so on */
1197 anchors_add_insecure(struct val_anchors* anchors, uint16_t c, uint8_t* nm)
1199 struct trust_anchor key;
1200 key.node.key = &key;
1202 key.namelabs = dname_count_size_labels(nm, &key.namelen);
1204 lock_basic_lock(&anchors->lock);
1205 if(rbtree_search(anchors->tree, &key)) {
1206 lock_basic_unlock(&anchors->lock);
1207 /* nothing to do, already an anchor or insecure point */
1210 if(!anchor_new_ta(anchors, nm, key.namelabs, key.namelen, c, 0)) {
1211 log_err("out of memory");
1212 lock_basic_unlock(&anchors->lock);
1215 /* no other contents in new ta, because it is insecure point */
1216 anchors_init_parents_locked(anchors);
1217 lock_basic_unlock(&anchors->lock);
1222 anchors_delete_insecure(struct val_anchors* anchors, uint16_t c,
1225 struct trust_anchor key;
1226 struct trust_anchor* ta;
1227 key.node.key = &key;
1229 key.namelabs = dname_count_size_labels(nm, &key.namelen);
1231 lock_basic_lock(&anchors->lock);
1232 if(!(ta=(struct trust_anchor*)rbtree_search(anchors->tree, &key))) {
1233 lock_basic_unlock(&anchors->lock);
1237 /* lock it to drive away other threads that use it */
1238 lock_basic_lock(&ta->lock);
1239 /* see if its really an insecure point */
1240 if(ta->keylist || ta->autr || ta->numDS || ta->numDNSKEY) {
1241 lock_basic_unlock(&anchors->lock);
1242 lock_basic_unlock(&ta->lock);
1243 /* its not an insecure point, do not remove it */
1247 /* remove from tree */
1248 (void)rbtree_delete(anchors->tree, &ta->node);
1249 anchors_init_parents_locked(anchors);
1250 lock_basic_unlock(&anchors->lock);
1252 /* actual free of data */
1253 lock_basic_unlock(&ta->lock);
1254 anchors_delfunc(&ta->node, NULL);