]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/validator/validator.c
Upgrade Unbound to 1.6.4. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / validator / validator.c
1 /*
2  * validator/validator.c - secure validator DNS query response module
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 a module that performs validation of DNS queries.
40  * According to RFC 4034.
41  */
42 #include "config.h"
43 #include "validator/validator.h"
44 #include "validator/val_anchor.h"
45 #include "validator/val_kcache.h"
46 #include "validator/val_kentry.h"
47 #include "validator/val_utils.h"
48 #include "validator/val_nsec.h"
49 #include "validator/val_nsec3.h"
50 #include "validator/val_neg.h"
51 #include "validator/val_sigcrypt.h"
52 #include "validator/autotrust.h"
53 #include "services/cache/dns.h"
54 #include "util/data/dname.h"
55 #include "util/module.h"
56 #include "util/log.h"
57 #include "util/net_help.h"
58 #include "util/regional.h"
59 #include "util/config_file.h"
60 #include "util/fptr_wlist.h"
61 #include "sldns/rrdef.h"
62 #include "sldns/wire2str.h"
63 #include "sldns/str2wire.h"
64
65 /* forward decl for cache response and normal super inform calls of a DS */
66 static void process_ds_response(struct module_qstate* qstate, 
67         struct val_qstate* vq, int id, int rcode, struct dns_msg* msg, 
68         struct query_info* qinfo, struct sock_list* origin);
69
70 /** fill up nsec3 key iterations config entry */
71 static int
72 fill_nsec3_iter(struct val_env* ve, char* s, int c)
73 {
74         char* e;
75         int i;
76         free(ve->nsec3_keysize);
77         free(ve->nsec3_maxiter);
78         ve->nsec3_keysize = (size_t*)calloc(sizeof(size_t), (size_t)c);
79         ve->nsec3_maxiter = (size_t*)calloc(sizeof(size_t), (size_t)c);
80         if(!ve->nsec3_keysize || !ve->nsec3_maxiter) {
81                 log_err("out of memory");
82                 return 0;
83         }
84         for(i=0; i<c; i++) {
85                 ve->nsec3_keysize[i] = (size_t)strtol(s, &e, 10);
86                 if(s == e) {
87                         log_err("cannot parse: %s", s);
88                         return 0;
89                 }
90                 s = e;
91                 ve->nsec3_maxiter[i] = (size_t)strtol(s, &e, 10);
92                 if(s == e) {
93                         log_err("cannot parse: %s", s);
94                         return 0;
95                 }
96                 s = e;
97                 if(i>0 && ve->nsec3_keysize[i-1] >= ve->nsec3_keysize[i]) {
98                         log_err("nsec3 key iterations not ascending: %d %d",
99                                 (int)ve->nsec3_keysize[i-1], 
100                                 (int)ve->nsec3_keysize[i]);
101                         return 0;
102                 }
103                 verbose(VERB_ALGO, "validator nsec3cfg keysz %d mxiter %d",
104                         (int)ve->nsec3_keysize[i], (int)ve->nsec3_maxiter[i]);
105         }
106         return 1;
107 }
108
109 /** apply config settings to validator */
110 static int
111 val_apply_cfg(struct module_env* env, struct val_env* val_env, 
112         struct config_file* cfg)
113 {
114         int c;
115         val_env->bogus_ttl = (uint32_t)cfg->bogus_ttl;
116         if(!env->anchors)
117                 env->anchors = anchors_create();
118         if(!env->anchors) {
119                 log_err("out of memory");
120                 return 0;
121         }
122         if(!val_env->kcache)
123                 val_env->kcache = key_cache_create(cfg);
124         if(!val_env->kcache) {
125                 log_err("out of memory");
126                 return 0;
127         }
128         env->key_cache = val_env->kcache;
129         if(!anchors_apply_cfg(env->anchors, cfg)) {
130                 log_err("validator: error in trustanchors config");
131                 return 0;
132         }
133         val_env->date_override = cfg->val_date_override;
134         val_env->skew_min = cfg->val_sig_skew_min;
135         val_env->skew_max = cfg->val_sig_skew_max;
136         c = cfg_count_numbers(cfg->val_nsec3_key_iterations);
137         if(c < 1 || (c&1)) {
138                 log_err("validator: unparseable or odd nsec3 key "
139                         "iterations: %s", cfg->val_nsec3_key_iterations);
140                 return 0;
141         }
142         val_env->nsec3_keyiter_count = c/2;
143         if(!fill_nsec3_iter(val_env, cfg->val_nsec3_key_iterations, c/2)) {
144                 log_err("validator: cannot apply nsec3 key iterations");
145                 return 0;
146         }
147         if(!val_env->neg_cache)
148                 val_env->neg_cache = val_neg_create(cfg,
149                         val_env->nsec3_maxiter[val_env->nsec3_keyiter_count-1]);
150         if(!val_env->neg_cache) {
151                 log_err("out of memory");
152                 return 0;
153         }
154         env->neg_cache = val_env->neg_cache;
155         return 1;
156 }
157
158 #ifdef USE_ECDSA_EVP_WORKAROUND
159 void ecdsa_evp_workaround_init(void);
160 #endif
161 int
162 val_init(struct module_env* env, int id)
163 {
164         struct val_env* val_env = (struct val_env*)calloc(1,
165                 sizeof(struct val_env));
166         if(!val_env) {
167                 log_err("malloc failure");
168                 return 0;
169         }
170         env->modinfo[id] = (void*)val_env;
171         env->need_to_validate = 1;
172         lock_basic_init(&val_env->bogus_lock);
173         lock_protect(&val_env->bogus_lock, &val_env->num_rrset_bogus,
174                 sizeof(val_env->num_rrset_bogus));
175 #ifdef USE_ECDSA_EVP_WORKAROUND
176         ecdsa_evp_workaround_init();
177 #endif
178         if(!val_apply_cfg(env, val_env, env->cfg)) {
179                 log_err("validator: could not apply configuration settings.");
180                 return 0;
181         }
182
183         return 1;
184 }
185
186 void
187 val_deinit(struct module_env* env, int id)
188 {
189         struct val_env* val_env;
190         if(!env || !env->modinfo[id])
191                 return;
192         val_env = (struct val_env*)env->modinfo[id];
193         lock_basic_destroy(&val_env->bogus_lock);
194         anchors_delete(env->anchors);
195         env->anchors = NULL;
196         key_cache_delete(val_env->kcache);
197         neg_cache_delete(val_env->neg_cache);
198         free(val_env->nsec3_keysize);
199         free(val_env->nsec3_maxiter);
200         free(val_env);
201         env->modinfo[id] = NULL;
202 }
203
204 /** fill in message structure */
205 static struct val_qstate*
206 val_new_getmsg(struct module_qstate* qstate, struct val_qstate* vq)
207 {
208         if(!qstate->return_msg || qstate->return_rcode != LDNS_RCODE_NOERROR) {
209                 /* create a message to verify */
210                 verbose(VERB_ALGO, "constructing reply for validation");
211                 vq->orig_msg = (struct dns_msg*)regional_alloc(qstate->region,
212                         sizeof(struct dns_msg));
213                 if(!vq->orig_msg)
214                         return NULL;
215                 vq->orig_msg->qinfo = qstate->qinfo;
216                 vq->orig_msg->rep = (struct reply_info*)regional_alloc(
217                         qstate->region, sizeof(struct reply_info));
218                 if(!vq->orig_msg->rep)
219                         return NULL;
220                 memset(vq->orig_msg->rep, 0, sizeof(struct reply_info));
221                 vq->orig_msg->rep->flags = (uint16_t)(qstate->return_rcode&0xf)
222                         |BIT_QR|BIT_RA|(qstate->query_flags|(BIT_CD|BIT_RD));
223                 vq->orig_msg->rep->qdcount = 1;
224         } else {
225                 vq->orig_msg = qstate->return_msg;
226         }
227         vq->qchase = qstate->qinfo;
228         /* chase reply will be an edited (sub)set of the orig msg rrset ptrs */
229         vq->chase_reply = regional_alloc_init(qstate->region, 
230                 vq->orig_msg->rep, 
231                 sizeof(struct reply_info) - sizeof(struct rrset_ref));
232         if(!vq->chase_reply)
233                 return NULL;
234         if(vq->orig_msg->rep->rrset_count > RR_COUNT_MAX)
235                 return NULL; /* protect against integer overflow */
236         vq->chase_reply->rrsets = regional_alloc_init(qstate->region,
237                 vq->orig_msg->rep->rrsets, sizeof(struct ub_packed_rrset_key*)
238                         * vq->orig_msg->rep->rrset_count);
239         if(!vq->chase_reply->rrsets)
240                 return NULL;
241         vq->rrset_skip = 0;
242         return vq;
243 }
244
245 /** allocate new validator query state */
246 static struct val_qstate*
247 val_new(struct module_qstate* qstate, int id)
248 {
249         struct val_qstate* vq = (struct val_qstate*)regional_alloc(
250                 qstate->region, sizeof(*vq));
251         log_assert(!qstate->minfo[id]);
252         if(!vq)
253                 return NULL;
254         memset(vq, 0, sizeof(*vq));
255         qstate->minfo[id] = vq;
256         vq->state = VAL_INIT_STATE;
257         return val_new_getmsg(qstate, vq);
258 }
259
260 /**
261  * Exit validation with an error status
262  * 
263  * @param qstate: query state
264  * @param id: validator id.
265  * @return false, for use by caller to return to stop processing.
266  */
267 static int
268 val_error(struct module_qstate* qstate, int id)
269 {
270         qstate->ext_state[id] = module_error;
271         qstate->return_rcode = LDNS_RCODE_SERVFAIL;
272         return 0;
273 }
274
275 /** 
276  * Check to see if a given response needs to go through the validation
277  * process. Typical reasons for this routine to return false are: CD bit was
278  * on in the original request, or the response is a kind of message that 
279  * is unvalidatable (i.e., SERVFAIL, REFUSED, etc.)
280  *
281  * @param qstate: query state.
282  * @param ret_rc: rcode for this message (if noerror - examine ret_msg).
283  * @param ret_msg: return msg, can be NULL; look at rcode instead.
284  * @return true if the response could use validation (although this does not
285  *         mean we can actually validate this response).
286  */
287 static int
288 needs_validation(struct module_qstate* qstate, int ret_rc, 
289         struct dns_msg* ret_msg)
290 {
291         int rcode;
292
293         /* If the CD bit is on in the original request, then you could think
294          * that we don't bother to validate anything.
295          * But this is signalled internally with the valrec flag.
296          * User queries are validated with BIT_CD to make our cache clean
297          * so that bogus messages get retried by the upstream also for
298          * downstream validators that set BIT_CD.
299          * For DNS64 bit_cd signals no dns64 processing, but we want to
300          * provide validation there too */
301         /*
302         if(qstate->query_flags & BIT_CD) {
303                 verbose(VERB_ALGO, "not validating response due to CD bit");
304                 return 0;
305         }
306         */
307         if(qstate->is_valrec) {
308                 verbose(VERB_ALGO, "not validating response, is valrec"
309                         "(validation recursion lookup)");
310                 return 0;
311         }
312
313         if(ret_rc != LDNS_RCODE_NOERROR || !ret_msg)
314                 rcode = ret_rc;
315         else    rcode = (int)FLAGS_GET_RCODE(ret_msg->rep->flags);
316
317         if(rcode != LDNS_RCODE_NOERROR && rcode != LDNS_RCODE_NXDOMAIN) {
318                 if(verbosity >= VERB_ALGO) {
319                         char rc[16];
320                         rc[0]=0;
321                         (void)sldns_wire2str_rcode_buf(rcode, rc, sizeof(rc));
322                         verbose(VERB_ALGO, "cannot validate non-answer, rcode %s", rc);
323                 }
324                 return 0;
325         }
326
327         /* cannot validate positive RRSIG response. (negatives can) */
328         if(qstate->qinfo.qtype == LDNS_RR_TYPE_RRSIG &&
329                 rcode == LDNS_RCODE_NOERROR && ret_msg &&
330                 ret_msg->rep->an_numrrsets > 0) {
331                 verbose(VERB_ALGO, "cannot validate RRSIG, no sigs on sigs.");
332                 return 0;
333         }
334         return 1;
335 }
336
337 /**
338  * Check to see if the response has already been validated.
339  * @param ret_msg: return msg, can be NULL
340  * @return true if the response has already been validated
341  */
342 static int
343 already_validated(struct dns_msg* ret_msg)
344 {
345         /* validate unchecked, and re-validate bogus messages */
346         if (ret_msg && ret_msg->rep->security > sec_status_bogus)
347         {
348                 verbose(VERB_ALGO, "response has already been validated: %s",
349                         sec_status_to_string(ret_msg->rep->security));
350                 return 1;
351         }
352         return 0;
353 }
354
355 /**
356  * Generate a request for DNS data.
357  *
358  * @param qstate: query state that is the parent.
359  * @param id: module id.
360  * @param name: what name to query for.
361  * @param namelen: length of name.
362  * @param qtype: query type.
363  * @param qclass: query class.
364  * @param flags: additional flags, such as the CD bit (BIT_CD), or 0.
365  * @param newq: If the subquery is newly created, it is returned,
366  *      otherwise NULL is returned
367  * @param detached: true if this qstate should not attach to the subquery
368  * @return false on alloc failure.
369  */
370 static int
371 generate_request(struct module_qstate* qstate, int id, uint8_t* name, 
372         size_t namelen, uint16_t qtype, uint16_t qclass, uint16_t flags, 
373         struct module_qstate** newq, int detached)
374 {
375         struct val_qstate* vq = (struct val_qstate*)qstate->minfo[id];
376         struct query_info ask;
377         int valrec;
378         ask.qname = name;
379         ask.qname_len = namelen;
380         ask.qtype = qtype;
381         ask.qclass = qclass;
382         ask.local_alias = NULL;
383         log_query_info(VERB_ALGO, "generate request", &ask);
384         /* enable valrec flag to avoid recursion to the same validation
385          * routine, this lookup is simply a lookup. DLVs need validation */
386         if(qtype == LDNS_RR_TYPE_DLV)
387                 valrec = 0;
388         else valrec = 1;
389         if(detached) {
390                 struct mesh_state* sub = NULL;
391                 fptr_ok(fptr_whitelist_modenv_add_sub(
392                         qstate->env->add_sub));
393                 if(!(*qstate->env->add_sub)(qstate, &ask, 
394                         (uint16_t)(BIT_RD|flags), 0, valrec, newq, &sub)){
395                         log_err("Could not generate request: out of memory");
396                         return 0;
397                 }
398         }
399         else {
400                 fptr_ok(fptr_whitelist_modenv_attach_sub(
401                         qstate->env->attach_sub));
402                 if(!(*qstate->env->attach_sub)(qstate, &ask, 
403                         (uint16_t)(BIT_RD|flags), 0, valrec, newq)){
404                         log_err("Could not generate request: out of memory");
405                         return 0;
406                 }
407         }
408         /* newq; validator does not need state created for that
409          * query, and its a 'normal' for iterator as well */
410         if(*newq) {
411                 /* add our blacklist to the query blacklist */
412                 sock_list_merge(&(*newq)->blacklist, (*newq)->region,
413                         vq->chain_blacklist);
414         }
415         qstate->ext_state[id] = module_wait_subquery;
416         return 1;
417 }
418
419 /**
420  * Generate, send and detach key tag signaling query.
421  *
422  * @param qstate: query state.
423  * @param id: module id.
424  * @param ta: trust anchor, locked.
425  * @return false on a processing error.
426  */
427 static int
428 generate_keytag_query(struct module_qstate* qstate, int id,
429         struct trust_anchor* ta)
430 {
431         /* 3 bytes for "_ta", 5 bytes per tag (4 bytes + "-") */
432 #define MAX_LABEL_TAGS (LDNS_MAX_LABELLEN-3)/5
433         size_t i, numtag;
434         uint16_t tags[MAX_LABEL_TAGS];
435         char tagstr[LDNS_MAX_LABELLEN+1] = "_ta"; /* +1 for NULL byte */
436         size_t tagstr_left = sizeof(tagstr) - strlen(tagstr);
437         char* tagstr_pos = tagstr + strlen(tagstr);
438         uint8_t dnamebuf[LDNS_MAX_DOMAINLEN+1]; /* +1 for label length byte */
439         size_t dnamebuf_len = sizeof(dnamebuf);
440         uint8_t* keytagdname;
441         struct module_qstate* newq = NULL;
442         enum module_ext_state ext_state = qstate->ext_state[id];
443
444         numtag = anchor_list_keytags(ta, tags, MAX_LABEL_TAGS);
445         if(numtag == 0)
446                 return 0;
447
448         for(i=0; i<numtag; i++) {
449                 /* Buffer can't overflow; numtag is limited to tags that fit in
450                  * the buffer. */
451                 snprintf(tagstr_pos, tagstr_left, "-%04x", (unsigned)tags[i]);
452                 tagstr_left -= strlen(tagstr_pos);
453                 tagstr_pos += strlen(tagstr_pos);
454         }
455
456         sldns_str2wire_dname_buf_origin(tagstr, dnamebuf, &dnamebuf_len,
457                 ta->name, ta->namelen);
458         if(!(keytagdname = (uint8_t*)regional_alloc_init(qstate->region,
459                 dnamebuf, dnamebuf_len))) {
460                 log_err("could not generate key tag query: out of memory");
461                 return 0;
462         }
463
464         log_nametypeclass(VERB_ALGO, "keytag query", keytagdname,
465                 LDNS_RR_TYPE_NULL, ta->dclass);
466         if(!generate_request(qstate, id, keytagdname, dnamebuf_len,
467                 LDNS_RR_TYPE_NULL, ta->dclass, 0, &newq, 1)) {
468                 log_err("failed to generate key tag signaling request");
469                 return 0;
470         }
471
472         /* Not interrested in subquery response. Restore the ext_state,
473          * that might be changed by generate_request() */
474         qstate->ext_state[id] = ext_state;
475
476         return 1;
477 }
478
479 /**
480  * Prime trust anchor for use.
481  * Generate and dispatch a priming query for the given trust anchor.
482  * The trust anchor can be DNSKEY or DS and does not have to be signed.
483  *
484  * @param qstate: query state.
485  * @param vq: validator query state.
486  * @param id: module id.
487  * @param toprime: what to prime.
488  * @return false on a processing error.
489  */
490 static int
491 prime_trust_anchor(struct module_qstate* qstate, struct val_qstate* vq,
492         int id, struct trust_anchor* toprime)
493 {
494         struct module_qstate* newq = NULL;
495         int ret = generate_request(qstate, id, toprime->name, toprime->namelen,
496                 LDNS_RR_TYPE_DNSKEY, toprime->dclass, BIT_CD, &newq, 0);
497
498         if(newq && qstate->env->cfg->trust_anchor_signaling &&
499                 !generate_keytag_query(qstate, id, toprime)) {
500                 log_err("keytag signaling query failed");
501                 return 0;
502         }
503
504         if(!ret) {
505                 log_err("Could not prime trust anchor: out of memory");
506                 return 0;
507         }
508         /* ignore newq; validator does not need state created for that
509          * query, and its a 'normal' for iterator as well */
510         vq->wait_prime_ta = 1; /* to elicit PRIME_RESP_STATE processing 
511                 from the validator inform_super() routine */
512         /* store trust anchor name for later lookup when prime returns */
513         vq->trust_anchor_name = regional_alloc_init(qstate->region,
514                 toprime->name, toprime->namelen);
515         vq->trust_anchor_len = toprime->namelen;
516         vq->trust_anchor_labs = toprime->namelabs;
517         if(!vq->trust_anchor_name) {
518                 log_err("Could not prime trust anchor: out of memory");
519                 return 0;
520         }
521         return 1;
522 }
523
524 /**
525  * Validate if the ANSWER and AUTHORITY sections contain valid rrsets.
526  * They must be validly signed with the given key.
527  * Tries to validate ADDITIONAL rrsets as well, but only to check them.
528  * Allows unsigned CNAME after a DNAME that expands the DNAME.
529  * 
530  * Note that by the time this method is called, the process of finding the
531  * trusted DNSKEY rrset that signs this response must already have been
532  * completed.
533  * 
534  * @param qstate: query state.
535  * @param env: module env for verify.
536  * @param ve: validator env for verify.
537  * @param qchase: query that was made.
538  * @param chase_reply: answer to validate.
539  * @param key_entry: the key entry, which is trusted, and which matches
540  *      the signer of the answer. The key entry isgood().
541  * @return false if any of the rrsets in the an or ns sections of the message 
542  *      fail to verify. The message is then set to bogus.
543  */
544 static int
545 validate_msg_signatures(struct module_qstate* qstate, struct module_env* env,
546         struct val_env* ve, struct query_info* qchase,
547         struct reply_info* chase_reply, struct key_entry_key* key_entry)
548 {
549         uint8_t* sname;
550         size_t i, slen;
551         struct ub_packed_rrset_key* s;
552         enum sec_status sec;
553         int dname_seen = 0;
554         char* reason = NULL;
555
556         /* validate the ANSWER section */
557         for(i=0; i<chase_reply->an_numrrsets; i++) {
558                 s = chase_reply->rrsets[i];
559                 /* Skip the CNAME following a (validated) DNAME.
560                  * Because of the normalization routines in the iterator, 
561                  * there will always be an unsigned CNAME following a DNAME 
562                  * (unless qtype=DNAME). */
563                 if(dname_seen && ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) {
564                         dname_seen = 0;
565                         /* CNAME was synthesized by our own iterator */
566                         /* since the DNAME verified, mark the CNAME as secure */
567                         ((struct packed_rrset_data*)s->entry.data)->security =
568                                 sec_status_secure;
569                         ((struct packed_rrset_data*)s->entry.data)->trust =
570                                 rrset_trust_validated;
571                         continue;
572                 }
573
574                 /* Verify the answer rrset */
575                 sec = val_verify_rrset_entry(env, ve, s, key_entry, &reason);
576                 /* If the (answer) rrset failed to validate, then this 
577                  * message is BAD. */
578                 if(sec != sec_status_secure) {
579                         log_nametypeclass(VERB_QUERY, "validator: response "
580                                 "has failed ANSWER rrset:", s->rk.dname,
581                                 ntohs(s->rk.type), ntohs(s->rk.rrset_class));
582                         errinf(qstate, reason);
583                         if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME)
584                                 errinf(qstate, "for CNAME");
585                         else if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME)
586                                 errinf(qstate, "for DNAME");
587                         errinf_origin(qstate, qstate->reply_origin);
588                         chase_reply->security = sec_status_bogus;
589                         return 0;
590                 }
591
592                 /* Notice a DNAME that should be followed by an unsigned 
593                  * CNAME. */
594                 if(qchase->qtype != LDNS_RR_TYPE_DNAME && 
595                         ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME) {
596                         dname_seen = 1;
597                 }
598         }
599
600         /* validate the AUTHORITY section */
601         for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+
602                 chase_reply->ns_numrrsets; i++) {
603                 s = chase_reply->rrsets[i];
604                 sec = val_verify_rrset_entry(env, ve, s, key_entry, &reason);
605                 /* If anything in the authority section fails to be secure, 
606                  * we have a bad message. */
607                 if(sec != sec_status_secure) {
608                         log_nametypeclass(VERB_QUERY, "validator: response "
609                                 "has failed AUTHORITY rrset:", s->rk.dname,
610                                 ntohs(s->rk.type), ntohs(s->rk.rrset_class));
611                         errinf(qstate, reason);
612                         errinf_origin(qstate, qstate->reply_origin);
613                         errinf_rrset(qstate, s);
614                         chase_reply->security = sec_status_bogus;
615                         return 0;
616                 }
617         }
618
619         /* If set, the validator should clean the additional section of
620          * secure messages. */
621         if(!env->cfg->val_clean_additional)
622                 return 1;
623         /* attempt to validate the ADDITIONAL section rrsets */
624         for(i=chase_reply->an_numrrsets+chase_reply->ns_numrrsets; 
625                 i<chase_reply->rrset_count; i++) {
626                 s = chase_reply->rrsets[i];
627                 /* only validate rrs that have signatures with the key */
628                 /* leave others unchecked, those get removed later on too */
629                 val_find_rrset_signer(s, &sname, &slen);
630                 if(sname && query_dname_compare(sname, key_entry->name)==0)
631                         (void)val_verify_rrset_entry(env, ve, s, key_entry,
632                                 &reason);
633                 /* the additional section can fail to be secure, 
634                  * it is optional, check signature in case we need
635                  * to clean the additional section later. */
636         }
637
638         return 1;
639 }
640
641 /**
642  * Detect wrong truncated response (say from BIND 9.6.1 that is forwarding
643  * and saw the NS record without signatures from a referral).
644  * The positive response has a mangled authority section.
645  * Remove that authority section and the additional section.
646  * @param rep: reply
647  * @return true if a wrongly truncated response.
648  */
649 static int
650 detect_wrongly_truncated(struct reply_info* rep)
651 {
652         size_t i;
653         /* only NS in authority, and it is bogus */
654         if(rep->ns_numrrsets != 1 || rep->an_numrrsets == 0)
655                 return 0;
656         if(ntohs(rep->rrsets[ rep->an_numrrsets ]->rk.type) != LDNS_RR_TYPE_NS)
657                 return 0;
658         if(((struct packed_rrset_data*)rep->rrsets[ rep->an_numrrsets ]
659                 ->entry.data)->security == sec_status_secure)
660                 return 0;
661         /* answer section is present and secure */
662         for(i=0; i<rep->an_numrrsets; i++) {
663                 if(((struct packed_rrset_data*)rep->rrsets[ i ]
664                         ->entry.data)->security != sec_status_secure)
665                         return 0;
666         }
667         verbose(VERB_ALGO, "truncating to minimal response");
668         return 1;
669 }
670
671 /**
672  * For messages that are not referrals, if the chase reply contains an
673  * unsigned NS record in the authority section it could have been
674  * inserted by a (BIND) forwarder that thinks the zone is insecure, and
675  * that has an NS record without signatures in cache.  Remove the NS
676  * record since the reply does not hinge on that record (in the authority
677  * section), but do not remove it if it removes the last record from the
678  * answer+authority sections.
679  * @param chase_reply: the chased reply, we have a key for this contents,
680  *      so we should have signatures for these rrsets and not having
681  *      signatures means it will be bogus.
682  * @param orig_reply: original reply, remove NS from there as well because
683  *      we cannot mark the NS record as DNSSEC valid because it is not
684  *      validated by signatures.
685  */
686 static void
687 remove_spurious_authority(struct reply_info* chase_reply,
688         struct reply_info* orig_reply)
689 {
690         size_t i, found = 0;
691         int remove = 0;
692         /* if no answer and only 1 auth RRset, do not remove that one */
693         if(chase_reply->an_numrrsets == 0 && chase_reply->ns_numrrsets == 1)
694                 return;
695         /* search authority section for unsigned NS records */
696         for(i = chase_reply->an_numrrsets;
697                 i < chase_reply->an_numrrsets+chase_reply->ns_numrrsets; i++) {
698                 struct packed_rrset_data* d = (struct packed_rrset_data*)
699                         chase_reply->rrsets[i]->entry.data;
700                 if(ntohs(chase_reply->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS
701                         && d->rrsig_count == 0) {
702                         found = i;
703                         remove = 1;
704                         break;
705                 }
706         }
707         /* see if we found the entry */
708         if(!remove) return;
709         log_rrset_key(VERB_ALGO, "Removing spurious unsigned NS record "
710                 "(likely inserted by forwarder)", chase_reply->rrsets[found]);
711
712         /* find rrset in orig_reply */
713         for(i = orig_reply->an_numrrsets;
714                 i < orig_reply->an_numrrsets+orig_reply->ns_numrrsets; i++) {
715                 if(ntohs(orig_reply->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS
716                         && query_dname_compare(orig_reply->rrsets[i]->rk.dname,
717                                 chase_reply->rrsets[found]->rk.dname) == 0) {
718                         /* remove from orig_msg */
719                         val_reply_remove_auth(orig_reply, i);
720                         break;
721                 }
722         }
723         /* remove rrset from chase_reply */
724         val_reply_remove_auth(chase_reply, found);
725 }
726
727 /**
728  * Given a "positive" response -- a response that contains an answer to the
729  * question, and no CNAME chain, validate this response. 
730  *
731  * The answer and authority RRsets must already be verified as secure.
732  * 
733  * @param env: module env for verify.
734  * @param ve: validator env for verify.
735  * @param qchase: query that was made.
736  * @param chase_reply: answer to that query to validate.
737  * @param kkey: the key entry, which is trusted, and which matches
738  *      the signer of the answer. The key entry isgood().
739  */
740 static void
741 validate_positive_response(struct module_env* env, struct val_env* ve,
742         struct query_info* qchase, struct reply_info* chase_reply,
743         struct key_entry_key* kkey)
744 {
745         uint8_t* wc = NULL;
746         int wc_NSEC_ok = 0;
747         int nsec3s_seen = 0;
748         size_t i;
749         struct ub_packed_rrset_key* s;
750
751         /* validate the ANSWER section - this will be the answer itself */
752         for(i=0; i<chase_reply->an_numrrsets; i++) {
753                 s = chase_reply->rrsets[i];
754
755                 /* Check to see if the rrset is the result of a wildcard 
756                  * expansion. If so, an additional check will need to be 
757                  * made in the authority section. */
758                 if(!val_rrset_wildcard(s, &wc)) {
759                         log_nametypeclass(VERB_QUERY, "Positive response has "
760                                 "inconsistent wildcard sigs:", s->rk.dname,
761                                 ntohs(s->rk.type), ntohs(s->rk.rrset_class));
762                         chase_reply->security = sec_status_bogus;
763                         return;
764                 }
765         }
766
767         /* validate the AUTHORITY section as well - this will generally be 
768          * the NS rrset (which could be missing, no problem) */
769         for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+
770                 chase_reply->ns_numrrsets; i++) {
771                 s = chase_reply->rrsets[i];
772
773                 /* If this is a positive wildcard response, and we have a 
774                  * (just verified) NSEC record, try to use it to 1) prove 
775                  * that qname doesn't exist and 2) that the correct wildcard 
776                  * was used. */
777                 if(wc != NULL && ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) {
778                         if(val_nsec_proves_positive_wildcard(s, qchase, wc)) {
779                                 wc_NSEC_ok = 1;
780                         }
781                         /* if not, continue looking for proof */
782                 }
783
784                 /* Otherwise, if this is a positive wildcard response and 
785                  * we have NSEC3 records */
786                 if(wc != NULL && ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
787                         nsec3s_seen = 1;
788                 }
789         }
790
791         /* If this was a positive wildcard response that we haven't already
792          * proven, and we have NSEC3 records, try to prove it using the NSEC3
793          * records. */
794         if(wc != NULL && !wc_NSEC_ok && nsec3s_seen) {
795                 enum sec_status sec = nsec3_prove_wildcard(env, ve, 
796                         chase_reply->rrsets+chase_reply->an_numrrsets,
797                         chase_reply->ns_numrrsets, qchase, kkey, wc);
798                 if(sec == sec_status_insecure) {
799                         verbose(VERB_ALGO, "Positive wildcard response is "
800                                 "insecure");
801                         chase_reply->security = sec_status_insecure;
802                         return;
803                 } else if(sec == sec_status_secure)
804                         wc_NSEC_ok = 1;
805         }
806
807         /* If after all this, we still haven't proven the positive wildcard
808          * response, fail. */
809         if(wc != NULL && !wc_NSEC_ok) {
810                 verbose(VERB_QUERY, "positive response was wildcard "
811                         "expansion and did not prove original data "
812                         "did not exist");
813                 chase_reply->security = sec_status_bogus;
814                 return;
815         }
816
817         verbose(VERB_ALGO, "Successfully validated positive response");
818         chase_reply->security = sec_status_secure;
819 }
820
821 /** 
822  * Validate a NOERROR/NODATA signed response -- a response that has a
823  * NOERROR Rcode but no ANSWER section RRsets. This consists of making 
824  * certain that the authority section NSEC/NSEC3s proves that the qname 
825  * does exist and the qtype doesn't.
826  *
827  * The answer and authority RRsets must already be verified as secure.
828  *
829  * @param env: module env for verify.
830  * @param ve: validator env for verify.
831  * @param qchase: query that was made.
832  * @param chase_reply: answer to that query to validate.
833  * @param kkey: the key entry, which is trusted, and which matches
834  *      the signer of the answer. The key entry isgood().
835  */
836 static void
837 validate_nodata_response(struct module_env* env, struct val_env* ve,
838         struct query_info* qchase, struct reply_info* chase_reply,
839         struct key_entry_key* kkey)
840 {
841         /* Since we are here, there must be nothing in the ANSWER section to
842          * validate. */
843         /* (Note: CNAME/DNAME responses will not directly get here --
844          * instead, they are chased down into individual CNAME validations,
845          * and at the end of the cname chain a POSITIVE, or CNAME_NOANSWER 
846          * validation.) */
847         
848         /* validate the AUTHORITY section */
849         int has_valid_nsec = 0; /* If true, then the NODATA has been proven.*/
850         uint8_t* ce = NULL; /* for wildcard nodata responses. This is the 
851                                 proven closest encloser. */
852         uint8_t* wc = NULL; /* for wildcard nodata responses. wildcard nsec */
853         int nsec3s_seen = 0; /* nsec3s seen */
854         struct ub_packed_rrset_key* s; 
855         size_t i;
856
857         for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+
858                 chase_reply->ns_numrrsets; i++) {
859                 s = chase_reply->rrsets[i];
860                 /* If we encounter an NSEC record, try to use it to prove 
861                  * NODATA.
862                  * This needs to handle the ENT NODATA case. */
863                 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) {
864                         if(nsec_proves_nodata(s, qchase, &wc)) {
865                                 has_valid_nsec = 1;
866                                 /* sets wc-encloser if wildcard applicable */
867                         } 
868                         if(val_nsec_proves_name_error(s, qchase->qname)) {
869                                 ce = nsec_closest_encloser(qchase->qname, s);
870                         }
871                         if(val_nsec_proves_insecuredelegation(s, qchase)) {
872                                 verbose(VERB_ALGO, "delegation is insecure");
873                                 chase_reply->security = sec_status_insecure;
874                                 return;
875                         }
876                 } else if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
877                         nsec3s_seen = 1;
878                 }
879         }
880
881         /* check to see if we have a wildcard NODATA proof. */
882
883         /* The wildcard NODATA is 1 NSEC proving that qname does not exist 
884          * (and also proving what the closest encloser is), and 1 NSEC 
885          * showing the matching wildcard, which must be *.closest_encloser. */
886         if(wc && !ce)
887                 has_valid_nsec = 0;
888         else if(wc && ce) {
889                 if(query_dname_compare(wc, ce) != 0) {
890                         has_valid_nsec = 0;
891                 }
892         }
893         
894         if(!has_valid_nsec && nsec3s_seen) {
895                 enum sec_status sec = nsec3_prove_nodata(env, ve, 
896                         chase_reply->rrsets+chase_reply->an_numrrsets,
897                         chase_reply->ns_numrrsets, qchase, kkey);
898                 if(sec == sec_status_insecure) {
899                         verbose(VERB_ALGO, "NODATA response is insecure");
900                         chase_reply->security = sec_status_insecure;
901                         return;
902                 } else if(sec == sec_status_secure)
903                         has_valid_nsec = 1;
904         }
905
906         if(!has_valid_nsec) {
907                 verbose(VERB_QUERY, "NODATA response failed to prove NODATA "
908                         "status with NSEC/NSEC3");
909                 if(verbosity >= VERB_ALGO)
910                         log_dns_msg("Failed NODATA", qchase, chase_reply);
911                 chase_reply->security = sec_status_bogus;
912                 return;
913         }
914
915         verbose(VERB_ALGO, "successfully validated NODATA response.");
916         chase_reply->security = sec_status_secure;
917 }
918
919 /** 
920  * Validate a NAMEERROR signed response -- a response that has a NXDOMAIN
921  * Rcode. 
922  * This consists of making certain that the authority section NSEC proves 
923  * that the qname doesn't exist and the covering wildcard also doesn't exist..
924  * 
925  * The answer and authority RRsets must have already been verified as secure.
926  *
927  * @param env: module env for verify.
928  * @param ve: validator env for verify.
929  * @param qchase: query that was made.
930  * @param chase_reply: answer to that query to validate.
931  * @param kkey: the key entry, which is trusted, and which matches
932  *      the signer of the answer. The key entry isgood().
933  * @param rcode: adjusted RCODE, in case of RCODE/proof mismatch leniency.
934  */
935 static void
936 validate_nameerror_response(struct module_env* env, struct val_env* ve,
937         struct query_info* qchase, struct reply_info* chase_reply,
938         struct key_entry_key* kkey, int* rcode)
939 {
940         int has_valid_nsec = 0;
941         int has_valid_wnsec = 0;
942         int nsec3s_seen = 0;
943         struct ub_packed_rrset_key* s; 
944         size_t i;
945
946         for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+
947                 chase_reply->ns_numrrsets; i++) {
948                 s = chase_reply->rrsets[i];
949                 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) {
950                         if(val_nsec_proves_name_error(s, qchase->qname))
951                                 has_valid_nsec = 1;
952                         if(val_nsec_proves_no_wc(s, qchase->qname, 
953                                 qchase->qname_len))
954                                 has_valid_wnsec = 1;
955                         if(val_nsec_proves_insecuredelegation(s, qchase)) {
956                                 verbose(VERB_ALGO, "delegation is insecure");
957                                 chase_reply->security = sec_status_insecure;
958                                 return;
959                         }
960                 } else if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3)
961                         nsec3s_seen = 1;
962         }
963
964         if((!has_valid_nsec || !has_valid_wnsec) && nsec3s_seen) {
965                 /* use NSEC3 proof, both answer and auth rrsets, in case
966                  * NSEC3s end up in the answer (due to qtype=NSEC3 or so) */
967                 chase_reply->security = nsec3_prove_nameerror(env, ve,
968                         chase_reply->rrsets, chase_reply->an_numrrsets+
969                         chase_reply->ns_numrrsets, qchase, kkey);
970                 if(chase_reply->security != sec_status_secure) {
971                         verbose(VERB_QUERY, "NameError response failed nsec, "
972                                 "nsec3 proof was %s", sec_status_to_string(
973                                 chase_reply->security));
974                         return;
975                 }
976                 has_valid_nsec = 1;
977                 has_valid_wnsec = 1;
978         }
979
980         /* If the message fails to prove either condition, it is bogus. */
981         if(!has_valid_nsec) {
982                 verbose(VERB_QUERY, "NameError response has failed to prove: "
983                           "qname does not exist");
984                 chase_reply->security = sec_status_bogus;
985                 /* Be lenient with RCODE in NSEC NameError responses */
986                 validate_nodata_response(env, ve, qchase, chase_reply, kkey);
987                 if (chase_reply->security == sec_status_secure)
988                         *rcode = LDNS_RCODE_NOERROR;
989                 return;
990         }
991
992         if(!has_valid_wnsec) {
993                 verbose(VERB_QUERY, "NameError response has failed to prove: "
994                           "covering wildcard does not exist");
995                 chase_reply->security = sec_status_bogus;
996                 /* Be lenient with RCODE in NSEC NameError responses */
997                 validate_nodata_response(env, ve, qchase, chase_reply, kkey);
998                 if (chase_reply->security == sec_status_secure)
999                         *rcode = LDNS_RCODE_NOERROR;
1000                 return;
1001         }
1002
1003         /* Otherwise, we consider the message secure. */
1004         verbose(VERB_ALGO, "successfully validated NAME ERROR response.");
1005         chase_reply->security = sec_status_secure;
1006 }
1007
1008 /** 
1009  * Given a referral response, validate rrsets and take least trusted rrset
1010  * as the current validation status.
1011  * 
1012  * Note that by the time this method is called, the process of finding the
1013  * trusted DNSKEY rrset that signs this response must already have been
1014  * completed.
1015  * 
1016  * @param chase_reply: answer to validate.
1017  */
1018 static void
1019 validate_referral_response(struct reply_info* chase_reply)
1020 {
1021         size_t i;
1022         enum sec_status s;
1023         /* message security equals lowest rrset security */
1024         chase_reply->security = sec_status_secure;
1025         for(i=0; i<chase_reply->rrset_count; i++) {
1026                 s = ((struct packed_rrset_data*)chase_reply->rrsets[i]
1027                         ->entry.data)->security;
1028                 if(s < chase_reply->security)
1029                         chase_reply->security = s;
1030         }
1031         verbose(VERB_ALGO, "validated part of referral response as %s",
1032                 sec_status_to_string(chase_reply->security));
1033 }
1034
1035 /** 
1036  * Given an "ANY" response -- a response that contains an answer to a
1037  * qtype==ANY question, with answers. This does no checking that all 
1038  * types are present.
1039  * 
1040  * NOTE: it may be possible to get parent-side delegation point records
1041  * here, which won't all be signed. Right now, this routine relies on the
1042  * upstream iterative resolver to not return these responses -- instead
1043  * treating them as referrals.
1044  * 
1045  * NOTE: RFC 4035 is silent on this issue, so this may change upon
1046  * clarification. Clarification draft -05 says to not check all types are
1047  * present.
1048  * 
1049  * Note that by the time this method is called, the process of finding the
1050  * trusted DNSKEY rrset that signs this response must already have been
1051  * completed.
1052  * 
1053  * @param env: module env for verify.
1054  * @param ve: validator env for verify.
1055  * @param qchase: query that was made.
1056  * @param chase_reply: answer to that query to validate.
1057  * @param kkey: the key entry, which is trusted, and which matches
1058  *      the signer of the answer. The key entry isgood().
1059  */
1060 static void
1061 validate_any_response(struct module_env* env, struct val_env* ve,
1062         struct query_info* qchase, struct reply_info* chase_reply,
1063         struct key_entry_key* kkey)
1064 {
1065         /* all answer and auth rrsets already verified */
1066         /* but check if a wildcard response is given, then check NSEC/NSEC3
1067          * for qname denial to see if wildcard is applicable */
1068         uint8_t* wc = NULL;
1069         int wc_NSEC_ok = 0;
1070         int nsec3s_seen = 0;
1071         size_t i;
1072         struct ub_packed_rrset_key* s;
1073
1074         if(qchase->qtype != LDNS_RR_TYPE_ANY) {
1075                 log_err("internal error: ANY validation called for non-ANY");
1076                 chase_reply->security = sec_status_bogus;
1077                 return;
1078         }
1079
1080         /* validate the ANSWER section - this will be the answer itself */
1081         for(i=0; i<chase_reply->an_numrrsets; i++) {
1082                 s = chase_reply->rrsets[i];
1083
1084                 /* Check to see if the rrset is the result of a wildcard 
1085                  * expansion. If so, an additional check will need to be 
1086                  * made in the authority section. */
1087                 if(!val_rrset_wildcard(s, &wc)) {
1088                         log_nametypeclass(VERB_QUERY, "Positive ANY response"
1089                                 " has inconsistent wildcard sigs:", 
1090                                 s->rk.dname, ntohs(s->rk.type), 
1091                                 ntohs(s->rk.rrset_class));
1092                         chase_reply->security = sec_status_bogus;
1093                         return;
1094                 }
1095         }
1096
1097         /* if it was a wildcard, check for NSEC/NSEC3s in both answer
1098          * and authority sections (NSEC may be moved to the ANSWER section) */
1099         if(wc != NULL)
1100           for(i=0; i<chase_reply->an_numrrsets+chase_reply->ns_numrrsets; 
1101                 i++) {
1102                 s = chase_reply->rrsets[i];
1103
1104                 /* If this is a positive wildcard response, and we have a 
1105                  * (just verified) NSEC record, try to use it to 1) prove 
1106                  * that qname doesn't exist and 2) that the correct wildcard 
1107                  * was used. */
1108                 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) {
1109                         if(val_nsec_proves_positive_wildcard(s, qchase, wc)) {
1110                                 wc_NSEC_ok = 1;
1111                         }
1112                         /* if not, continue looking for proof */
1113                 }
1114
1115                 /* Otherwise, if this is a positive wildcard response and 
1116                  * we have NSEC3 records */
1117                 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1118                         nsec3s_seen = 1;
1119                 }
1120         }
1121
1122         /* If this was a positive wildcard response that we haven't already
1123          * proven, and we have NSEC3 records, try to prove it using the NSEC3
1124          * records. */
1125         if(wc != NULL && !wc_NSEC_ok && nsec3s_seen) {
1126                 /* look both in answer and auth section for NSEC3s */
1127                 enum sec_status sec = nsec3_prove_wildcard(env, ve, 
1128                         chase_reply->rrsets,
1129                         chase_reply->an_numrrsets+chase_reply->ns_numrrsets, 
1130                         qchase, kkey, wc);
1131                 if(sec == sec_status_insecure) {
1132                         verbose(VERB_ALGO, "Positive ANY wildcard response is "
1133                                 "insecure");
1134                         chase_reply->security = sec_status_insecure;
1135                         return;
1136                 } else if(sec == sec_status_secure)
1137                         wc_NSEC_ok = 1;
1138         }
1139
1140         /* If after all this, we still haven't proven the positive wildcard
1141          * response, fail. */
1142         if(wc != NULL && !wc_NSEC_ok) {
1143                 verbose(VERB_QUERY, "positive ANY response was wildcard "
1144                         "expansion and did not prove original data "
1145                         "did not exist");
1146                 chase_reply->security = sec_status_bogus;
1147                 return;
1148         }
1149
1150         verbose(VERB_ALGO, "Successfully validated positive ANY response");
1151         chase_reply->security = sec_status_secure;
1152 }
1153
1154 /**
1155  * Validate CNAME response, or DNAME+CNAME.
1156  * This is just like a positive proof, except that this is about a 
1157  * DNAME+CNAME. Possible wildcard proof.
1158  * Difference with positive proof is that this routine refuses 
1159  * wildcarded DNAMEs.
1160  * 
1161  * The answer and authority rrsets must already be verified as secure.
1162  * 
1163  * @param env: module env for verify.
1164  * @param ve: validator env for verify.
1165  * @param qchase: query that was made.
1166  * @param chase_reply: answer to that query to validate.
1167  * @param kkey: the key entry, which is trusted, and which matches
1168  *      the signer of the answer. The key entry isgood().
1169  */
1170 static void
1171 validate_cname_response(struct module_env* env, struct val_env* ve,
1172         struct query_info* qchase, struct reply_info* chase_reply,
1173         struct key_entry_key* kkey)
1174 {
1175         uint8_t* wc = NULL;
1176         int wc_NSEC_ok = 0;
1177         int nsec3s_seen = 0;
1178         size_t i;
1179         struct ub_packed_rrset_key* s;
1180
1181         /* validate the ANSWER section - this will be the CNAME (+DNAME) */
1182         for(i=0; i<chase_reply->an_numrrsets; i++) {
1183                 s = chase_reply->rrsets[i];
1184
1185                 /* Check to see if the rrset is the result of a wildcard 
1186                  * expansion. If so, an additional check will need to be 
1187                  * made in the authority section. */
1188                 if(!val_rrset_wildcard(s, &wc)) {
1189                         log_nametypeclass(VERB_QUERY, "Cname response has "
1190                                 "inconsistent wildcard sigs:", s->rk.dname,
1191                                 ntohs(s->rk.type), ntohs(s->rk.rrset_class));
1192                         chase_reply->security = sec_status_bogus;
1193                         return;
1194                 }
1195                 
1196                 /* Refuse wildcarded DNAMEs rfc 4597. 
1197                  * Do not follow a wildcarded DNAME because 
1198                  * its synthesized CNAME expansion is underdefined */
1199                 if(qchase->qtype != LDNS_RR_TYPE_DNAME && 
1200                         ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME && wc) {
1201                         log_nametypeclass(VERB_QUERY, "cannot validate a "
1202                                 "wildcarded DNAME:", s->rk.dname, 
1203                                 ntohs(s->rk.type), ntohs(s->rk.rrset_class));
1204                         chase_reply->security = sec_status_bogus;
1205                         return;
1206                 }
1207
1208                 /* If we have found a CNAME, stop looking for one.
1209                  * The iterator has placed the CNAME chain in correct
1210                  * order. */
1211                 if (ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) {
1212                         break;
1213                 }
1214         }
1215
1216         /* AUTHORITY section */
1217         for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+
1218                 chase_reply->ns_numrrsets; i++) {
1219                 s = chase_reply->rrsets[i];
1220
1221                 /* If this is a positive wildcard response, and we have a 
1222                  * (just verified) NSEC record, try to use it to 1) prove 
1223                  * that qname doesn't exist and 2) that the correct wildcard 
1224                  * was used. */
1225                 if(wc != NULL && ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) {
1226                         if(val_nsec_proves_positive_wildcard(s, qchase, wc)) {
1227                                 wc_NSEC_ok = 1;
1228                         }
1229                         /* if not, continue looking for proof */
1230                 }
1231
1232                 /* Otherwise, if this is a positive wildcard response and 
1233                  * we have NSEC3 records */
1234                 if(wc != NULL && ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1235                         nsec3s_seen = 1;
1236                 }
1237         }
1238
1239         /* If this was a positive wildcard response that we haven't already
1240          * proven, and we have NSEC3 records, try to prove it using the NSEC3
1241          * records. */
1242         if(wc != NULL && !wc_NSEC_ok && nsec3s_seen) {
1243                 enum sec_status sec = nsec3_prove_wildcard(env, ve, 
1244                         chase_reply->rrsets+chase_reply->an_numrrsets,
1245                         chase_reply->ns_numrrsets, qchase, kkey, wc);
1246                 if(sec == sec_status_insecure) {
1247                         verbose(VERB_ALGO, "wildcard CNAME response is "
1248                                 "insecure");
1249                         chase_reply->security = sec_status_insecure;
1250                         return;
1251                 } else if(sec == sec_status_secure)
1252                         wc_NSEC_ok = 1;
1253         }
1254
1255         /* If after all this, we still haven't proven the positive wildcard
1256          * response, fail. */
1257         if(wc != NULL && !wc_NSEC_ok) {
1258                 verbose(VERB_QUERY, "CNAME response was wildcard "
1259                         "expansion and did not prove original data "
1260                         "did not exist");
1261                 chase_reply->security = sec_status_bogus;
1262                 return;
1263         }
1264
1265         verbose(VERB_ALGO, "Successfully validated CNAME response");
1266         chase_reply->security = sec_status_secure;
1267 }
1268
1269 /**
1270  * Validate CNAME NOANSWER response, no more data after a CNAME chain.
1271  * This can be a NODATA or a NAME ERROR case, but not both at the same time.
1272  * We don't know because the rcode has been set to NOERROR by the CNAME.
1273  * 
1274  * The answer and authority rrsets must already be verified as secure.
1275  * 
1276  * @param env: module env for verify.
1277  * @param ve: validator env for verify.
1278  * @param qchase: query that was made.
1279  * @param chase_reply: answer to that query to validate.
1280  * @param kkey: the key entry, which is trusted, and which matches
1281  *      the signer of the answer. The key entry isgood().
1282  */
1283 static void
1284 validate_cname_noanswer_response(struct module_env* env, struct val_env* ve,
1285         struct query_info* qchase, struct reply_info* chase_reply,
1286         struct key_entry_key* kkey)
1287 {
1288         int nodata_valid_nsec = 0; /* If true, then NODATA has been proven.*/
1289         uint8_t* ce = NULL; /* for wildcard nodata responses. This is the 
1290                                 proven closest encloser. */
1291         uint8_t* wc = NULL; /* for wildcard nodata responses. wildcard nsec */
1292         int nxdomain_valid_nsec = 0; /* if true, namerror has been proven */
1293         int nxdomain_valid_wnsec = 0;
1294         int nsec3s_seen = 0; /* nsec3s seen */
1295         struct ub_packed_rrset_key* s; 
1296         size_t i;
1297
1298         /* the AUTHORITY section */
1299         for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+
1300                 chase_reply->ns_numrrsets; i++) {
1301                 s = chase_reply->rrsets[i];
1302
1303                 /* If we encounter an NSEC record, try to use it to prove 
1304                  * NODATA. This needs to handle the ENT NODATA case. 
1305                  * Also try to prove NAMEERROR, and absence of a wildcard */
1306                 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) {
1307                         if(nsec_proves_nodata(s, qchase, &wc)) {
1308                                 nodata_valid_nsec = 1;
1309                                 /* set wc encloser if wildcard applicable */
1310                         } 
1311                         if(val_nsec_proves_name_error(s, qchase->qname)) {
1312                                 ce = nsec_closest_encloser(qchase->qname, s);
1313                                 nxdomain_valid_nsec = 1;
1314                         }
1315                         if(val_nsec_proves_no_wc(s, qchase->qname, 
1316                                 qchase->qname_len))
1317                                 nxdomain_valid_wnsec = 1;
1318                         if(val_nsec_proves_insecuredelegation(s, qchase)) {
1319                                 verbose(VERB_ALGO, "delegation is insecure");
1320                                 chase_reply->security = sec_status_insecure;
1321                                 return;
1322                         }
1323                 } else if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) {
1324                         nsec3s_seen = 1;
1325                 }
1326         }
1327
1328         /* check to see if we have a wildcard NODATA proof. */
1329
1330         /* The wildcard NODATA is 1 NSEC proving that qname does not exists 
1331          * (and also proving what the closest encloser is), and 1 NSEC 
1332          * showing the matching wildcard, which must be *.closest_encloser. */
1333         if(wc && !ce)
1334                 nodata_valid_nsec = 0;
1335         else if(wc && ce) {
1336                 if(query_dname_compare(wc, ce) != 0) {
1337                         nodata_valid_nsec = 0;
1338                 }
1339         }
1340         if(nxdomain_valid_nsec && !nxdomain_valid_wnsec) {
1341                 /* name error is missing wildcard denial proof */
1342                 nxdomain_valid_nsec = 0;
1343         }
1344         
1345         if(nodata_valid_nsec && nxdomain_valid_nsec) {
1346                 verbose(VERB_QUERY, "CNAMEchain to noanswer proves that name "
1347                         "exists and not exists, bogus");
1348                 chase_reply->security = sec_status_bogus;
1349                 return;
1350         }
1351         if(!nodata_valid_nsec && !nxdomain_valid_nsec && nsec3s_seen) {
1352                 int nodata;
1353                 enum sec_status sec = nsec3_prove_nxornodata(env, ve, 
1354                         chase_reply->rrsets+chase_reply->an_numrrsets,
1355                         chase_reply->ns_numrrsets, qchase, kkey, &nodata);
1356                 if(sec == sec_status_insecure) {
1357                         verbose(VERB_ALGO, "CNAMEchain to noanswer response "
1358                                 "is insecure");
1359                         chase_reply->security = sec_status_insecure;
1360                         return;
1361                 } else if(sec == sec_status_secure) {
1362                         if(nodata)
1363                                 nodata_valid_nsec = 1;
1364                         else    nxdomain_valid_nsec = 1;
1365                 }
1366         }
1367
1368         if(!nodata_valid_nsec && !nxdomain_valid_nsec) {
1369                 verbose(VERB_QUERY, "CNAMEchain to noanswer response failed "
1370                         "to prove status with NSEC/NSEC3");
1371                 if(verbosity >= VERB_ALGO)
1372                         log_dns_msg("Failed CNAMEnoanswer", qchase, chase_reply);
1373                 chase_reply->security = sec_status_bogus;
1374                 return;
1375         }
1376
1377         if(nodata_valid_nsec)
1378                 verbose(VERB_ALGO, "successfully validated CNAME chain to a "
1379                         "NODATA response.");
1380         else    verbose(VERB_ALGO, "successfully validated CNAME chain to a "
1381                         "NAMEERROR response.");
1382         chase_reply->security = sec_status_secure;
1383 }
1384
1385 /** 
1386  * Process init state for validator.
1387  * Process the INIT state. First tier responses start in the INIT state.
1388  * This is where they are vetted for validation suitability, and the initial
1389  * key search is done.
1390  * 
1391  * Currently, events the come through this routine will be either promoted
1392  * to FINISHED/CNAME_RESP (no validation needed), FINDKEY (next step to
1393  * validation), or will be (temporarily) retired and a new priming request
1394  * event will be generated.
1395  *
1396  * @param qstate: query state.
1397  * @param vq: validator query state.
1398  * @param ve: validator shared global environment.
1399  * @param id: module id.
1400  * @return true if the event should be processed further on return, false if
1401  *         not.
1402  */
1403 static int
1404 processInit(struct module_qstate* qstate, struct val_qstate* vq, 
1405         struct val_env* ve, int id)
1406 {
1407         uint8_t* lookup_name;
1408         size_t lookup_len;
1409         struct trust_anchor* anchor;
1410         enum val_classification subtype = val_classify_response(
1411                 qstate->query_flags, &qstate->qinfo, &vq->qchase, 
1412                 vq->orig_msg->rep, vq->rrset_skip);
1413         if(vq->restart_count > VAL_MAX_RESTART_COUNT) {
1414                 verbose(VERB_ALGO, "restart count exceeded");
1415                 return val_error(qstate, id);
1416         }
1417         verbose(VERB_ALGO, "validator classification %s", 
1418                 val_classification_to_string(subtype));
1419         if(subtype == VAL_CLASS_REFERRAL && 
1420                 vq->rrset_skip < vq->orig_msg->rep->rrset_count) {
1421                 /* referral uses the rrset name as qchase, to find keys for
1422                  * that rrset */
1423                 vq->qchase.qname = vq->orig_msg->rep->
1424                         rrsets[vq->rrset_skip]->rk.dname;
1425                 vq->qchase.qname_len = vq->orig_msg->rep->
1426                         rrsets[vq->rrset_skip]->rk.dname_len;
1427                 vq->qchase.qtype = ntohs(vq->orig_msg->rep->
1428                         rrsets[vq->rrset_skip]->rk.type);
1429                 vq->qchase.qclass = ntohs(vq->orig_msg->rep->
1430                         rrsets[vq->rrset_skip]->rk.rrset_class);
1431         }
1432         lookup_name = vq->qchase.qname;
1433         lookup_len = vq->qchase.qname_len;
1434         /* for type DS look at the parent side for keys/trustanchor */
1435         /* also for NSEC not at apex */
1436         if(vq->qchase.qtype == LDNS_RR_TYPE_DS ||
1437                 (vq->qchase.qtype == LDNS_RR_TYPE_NSEC && 
1438                  vq->orig_msg->rep->rrset_count > vq->rrset_skip &&
1439                  ntohs(vq->orig_msg->rep->rrsets[vq->rrset_skip]->rk.type) ==
1440                  LDNS_RR_TYPE_NSEC &&
1441                  !(vq->orig_msg->rep->rrsets[vq->rrset_skip]->
1442                  rk.flags&PACKED_RRSET_NSEC_AT_APEX))) {
1443                 dname_remove_label(&lookup_name, &lookup_len);
1444         }
1445
1446         val_mark_indeterminate(vq->chase_reply, qstate->env->anchors, 
1447                 qstate->env->rrset_cache, qstate->env);
1448         vq->key_entry = NULL;
1449         vq->empty_DS_name = NULL;
1450         vq->ds_rrset = 0;
1451         anchor = anchors_lookup(qstate->env->anchors, 
1452                 lookup_name, lookup_len, vq->qchase.qclass);
1453
1454         /* Determine the signer/lookup name */
1455         val_find_signer(subtype, &vq->qchase, vq->orig_msg->rep, 
1456                 vq->rrset_skip, &vq->signer_name, &vq->signer_len);
1457         if(vq->signer_name != NULL &&
1458                 !dname_subdomain_c(lookup_name, vq->signer_name)) {
1459                 log_nametypeclass(VERB_ALGO, "this signer name is not a parent "
1460                         "of lookupname, omitted", vq->signer_name, 0, 0);
1461                 vq->signer_name = NULL;
1462         }
1463         if(vq->signer_name == NULL) {
1464                 log_nametypeclass(VERB_ALGO, "no signer, using", lookup_name,
1465                         0, 0);
1466         } else {
1467                 lookup_name = vq->signer_name;
1468                 lookup_len = vq->signer_len;
1469                 log_nametypeclass(VERB_ALGO, "signer is", lookup_name, 0, 0);
1470         }
1471
1472         /* for NXDOMAIN it could be signed by a parent of the trust anchor */
1473         if(subtype == VAL_CLASS_NAMEERROR && vq->signer_name &&
1474                 anchor && dname_strict_subdomain_c(anchor->name, lookup_name)){
1475                 lock_basic_unlock(&anchor->lock);
1476                 anchor = anchors_lookup(qstate->env->anchors, 
1477                         lookup_name, lookup_len, vq->qchase.qclass);
1478                 if(!anchor) { /* unsigned parent denies anchor*/
1479                         verbose(VERB_QUERY, "unsigned parent zone denies"
1480                                 " trust anchor, indeterminate");
1481                         vq->chase_reply->security = sec_status_indeterminate;
1482                         vq->state = VAL_FINISHED_STATE;
1483                         return 1;
1484                 }
1485                 verbose(VERB_ALGO, "trust anchor NXDOMAIN by signed parent");
1486         } else if(subtype == VAL_CLASS_POSITIVE &&
1487                 qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY &&
1488                 query_dname_compare(lookup_name, qstate->qinfo.qname) == 0) {
1489                 /* is a DNSKEY so lookup a bit higher since we want to
1490                  * get it from a parent or from trustanchor */
1491                 dname_remove_label(&lookup_name, &lookup_len);
1492         }
1493
1494         if(vq->rrset_skip > 0 || subtype == VAL_CLASS_CNAME ||
1495                 subtype == VAL_CLASS_REFERRAL) {
1496                 /* extract this part of orig_msg into chase_reply for
1497                  * the eventual VALIDATE stage */
1498                 val_fill_reply(vq->chase_reply, vq->orig_msg->rep, 
1499                         vq->rrset_skip, lookup_name, lookup_len, 
1500                         vq->signer_name);
1501                 if(verbosity >= VERB_ALGO)
1502                         log_dns_msg("chased extract", &vq->qchase, 
1503                                 vq->chase_reply);
1504         }
1505
1506         vq->key_entry = key_cache_obtain(ve->kcache, lookup_name, lookup_len,
1507                 vq->qchase.qclass, qstate->region, *qstate->env->now);
1508
1509         /* there is no key(from DLV) and no trust anchor */
1510         if(vq->key_entry == NULL && anchor == NULL) {
1511                 /*response isn't under a trust anchor, so we cannot validate.*/
1512                 vq->chase_reply->security = sec_status_indeterminate;
1513                 /* go to finished state to cache this result */
1514                 vq->state = VAL_FINISHED_STATE;
1515                 return 1;
1516         }
1517         /* if not key, or if keyentry is *above* the trustanchor, i.e.
1518          * the keyentry is based on another (higher) trustanchor */
1519         else if(vq->key_entry == NULL || (anchor &&
1520                 dname_strict_subdomain_c(anchor->name, vq->key_entry->name))) {
1521                 /* trust anchor is an 'unsigned' trust anchor */
1522                 if(anchor && anchor->numDS == 0 && anchor->numDNSKEY == 0) {
1523                         vq->chase_reply->security = sec_status_insecure;
1524                         val_mark_insecure(vq->chase_reply, anchor->name, 
1525                                 qstate->env->rrset_cache, qstate->env);
1526                         lock_basic_unlock(&anchor->lock);
1527                         vq->dlv_checked=1; /* skip DLV check */
1528                         /* go to finished state to cache this result */
1529                         vq->state = VAL_FINISHED_STATE;
1530                         return 1;
1531                 }
1532                 /* fire off a trust anchor priming query. */
1533                 verbose(VERB_DETAIL, "prime trust anchor");
1534                 if(!prime_trust_anchor(qstate, vq, id, anchor)) {
1535                         lock_basic_unlock(&anchor->lock);
1536                         return val_error(qstate, id);
1537                 }
1538                 lock_basic_unlock(&anchor->lock);
1539                 /* and otherwise, don't continue processing this event.
1540                  * (it will be reactivated when the priming query returns). */
1541                 vq->state = VAL_FINDKEY_STATE;
1542                 return 0;
1543         }
1544         if(anchor) {
1545                 lock_basic_unlock(&anchor->lock);
1546         }
1547
1548         if(key_entry_isnull(vq->key_entry)) {
1549                 /* response is under a null key, so we cannot validate
1550                  * However, we do set the status to INSECURE, since it is 
1551                  * essentially proven insecure. */
1552                 vq->chase_reply->security = sec_status_insecure;
1553                 val_mark_insecure(vq->chase_reply, vq->key_entry->name, 
1554                         qstate->env->rrset_cache, qstate->env);
1555                 /* go to finished state to cache this result */
1556                 vq->state = VAL_FINISHED_STATE;
1557                 return 1;
1558         } else if(key_entry_isbad(vq->key_entry)) {
1559                 /* key is bad, chain is bad, reply is bogus */
1560                 errinf_dname(qstate, "key for validation", vq->key_entry->name);
1561                 errinf(qstate, "is marked as invalid");
1562                 if(key_entry_get_reason(vq->key_entry)) {
1563                         errinf(qstate, "because of a previous");
1564                         errinf(qstate, key_entry_get_reason(vq->key_entry));
1565                 }
1566                 /* no retries, stop bothering the authority until timeout */
1567                 vq->restart_count = VAL_MAX_RESTART_COUNT;
1568                 vq->chase_reply->security = sec_status_bogus;
1569                 vq->state = VAL_FINISHED_STATE;
1570                 return 1;
1571         }
1572
1573         /* otherwise, we have our "closest" cached key -- continue 
1574          * processing in the next state. */
1575         vq->state = VAL_FINDKEY_STATE;
1576         return 1;
1577 }
1578
1579 /**
1580  * Process the FINDKEY state. Generally this just calculates the next name
1581  * to query and either issues a DS or a DNSKEY query. It will check to see
1582  * if the correct key has already been reached, in which case it will
1583  * advance the event to the next state.
1584  *
1585  * @param qstate: query state.
1586  * @param vq: validator query state.
1587  * @param id: module id.
1588  * @return true if the event should be processed further on return, false if
1589  *         not.
1590  */
1591 static int
1592 processFindKey(struct module_qstate* qstate, struct val_qstate* vq, int id)
1593 {
1594         uint8_t* target_key_name, *current_key_name;
1595         size_t target_key_len;
1596         int strip_lab;
1597         struct module_qstate* newq = NULL;
1598
1599         log_query_info(VERB_ALGO, "validator: FindKey", &vq->qchase);
1600         /* We know that state.key_entry is not 0 or bad key -- if it were,
1601          * then previous processing should have directed this event to 
1602          * a different state. 
1603          * It could be an isnull key, which signals that a DLV was just
1604          * done and the DNSKEY after the DLV failed with dnssec-retry state
1605          * and the DNSKEY has to be performed again. */
1606         log_assert(vq->key_entry && !key_entry_isbad(vq->key_entry));
1607         if(key_entry_isnull(vq->key_entry)) {
1608                 if(!generate_request(qstate, id, vq->ds_rrset->rk.dname, 
1609                         vq->ds_rrset->rk.dname_len, LDNS_RR_TYPE_DNSKEY, 
1610                         vq->qchase.qclass, BIT_CD, &newq, 0)) {
1611                         log_err("mem error generating DNSKEY request");
1612                         return val_error(qstate, id);
1613                 }
1614                 return 0;
1615         }
1616
1617         target_key_name = vq->signer_name;
1618         target_key_len = vq->signer_len;
1619         if(!target_key_name) {
1620                 target_key_name = vq->qchase.qname;
1621                 target_key_len = vq->qchase.qname_len;
1622         }
1623
1624         current_key_name = vq->key_entry->name;
1625
1626         /* If our current key entry matches our target, then we are done. */
1627         if(query_dname_compare(target_key_name, current_key_name) == 0) {
1628                 vq->state = VAL_VALIDATE_STATE;
1629                 return 1;
1630         }
1631
1632         if(vq->empty_DS_name) {
1633                 /* if the last empty nonterminal/emptyDS name we detected is
1634                  * below the current key, use that name to make progress
1635                  * along the chain of trust */
1636                 if(query_dname_compare(target_key_name, 
1637                         vq->empty_DS_name) == 0) {
1638                         /* do not query for empty_DS_name again */
1639                         verbose(VERB_ALGO, "Cannot retrieve DS for signature");
1640                         errinf(qstate, "no signatures");
1641                         errinf_origin(qstate, qstate->reply_origin);
1642                         vq->chase_reply->security = sec_status_bogus;
1643                         vq->state = VAL_FINISHED_STATE;
1644                         return 1;
1645                 }
1646                 current_key_name = vq->empty_DS_name;
1647         }
1648
1649         log_nametypeclass(VERB_ALGO, "current keyname", current_key_name,
1650                 LDNS_RR_TYPE_DNSKEY, LDNS_RR_CLASS_IN);
1651         log_nametypeclass(VERB_ALGO, "target keyname", target_key_name,
1652                 LDNS_RR_TYPE_DNSKEY, LDNS_RR_CLASS_IN);
1653         /* assert we are walking down the DNS tree */
1654         if(!dname_subdomain_c(target_key_name, current_key_name)) {
1655                 verbose(VERB_ALGO, "bad signer name");
1656                 vq->chase_reply->security = sec_status_bogus;
1657                 vq->state = VAL_FINISHED_STATE;
1658                 return 1;
1659         }
1660         /* so this value is >= -1 */
1661         strip_lab = dname_count_labels(target_key_name) - 
1662                 dname_count_labels(current_key_name) - 1;
1663         log_assert(strip_lab >= -1);
1664         verbose(VERB_ALGO, "striplab %d", strip_lab);
1665         if(strip_lab > 0) {
1666                 dname_remove_labels(&target_key_name, &target_key_len, 
1667                         strip_lab);
1668         }
1669         log_nametypeclass(VERB_ALGO, "next keyname", target_key_name,
1670                 LDNS_RR_TYPE_DNSKEY, LDNS_RR_CLASS_IN);
1671
1672         /* The next step is either to query for the next DS, or to query 
1673          * for the next DNSKEY. */
1674         if(vq->ds_rrset)
1675                 log_nametypeclass(VERB_ALGO, "DS RRset", vq->ds_rrset->rk.dname, LDNS_RR_TYPE_DS, LDNS_RR_CLASS_IN);
1676         else verbose(VERB_ALGO, "No DS RRset");
1677
1678         if(vq->ds_rrset && query_dname_compare(vq->ds_rrset->rk.dname,
1679                 vq->key_entry->name) != 0) {
1680                 if(!generate_request(qstate, id, vq->ds_rrset->rk.dname, 
1681                         vq->ds_rrset->rk.dname_len, LDNS_RR_TYPE_DNSKEY, 
1682                         vq->qchase.qclass, BIT_CD, &newq, 0)) {
1683                         log_err("mem error generating DNSKEY request");
1684                         return val_error(qstate, id);
1685                 }
1686                 return 0;
1687         }
1688
1689         if(!vq->ds_rrset || query_dname_compare(vq->ds_rrset->rk.dname,
1690                 target_key_name) != 0) {
1691                 /* check if there is a cache entry : pick up an NSEC if
1692                  * there is no DS, check if that NSEC has DS-bit unset, and
1693                  * thus can disprove the secure delegation we seek.
1694                  * We can then use that NSEC even in the absence of a SOA
1695                  * record that would be required by the iterator to supply
1696                  * a completely protocol-correct response. 
1697                  * Uses negative cache for NSEC3 lookup of DS responses. */
1698                 /* only if cache not blacklisted, of course */
1699                 struct dns_msg* msg;
1700                 if(!qstate->blacklist && !vq->chain_blacklist &&
1701                         (msg=val_find_DS(qstate->env, target_key_name, 
1702                         target_key_len, vq->qchase.qclass, qstate->region,
1703                         vq->key_entry->name)) ) {
1704                         verbose(VERB_ALGO, "Process cached DS response");
1705                         process_ds_response(qstate, vq, id, LDNS_RCODE_NOERROR,
1706                                 msg, &msg->qinfo, NULL);
1707                         return 1; /* continue processing ds-response results */
1708                 }
1709                 if(!generate_request(qstate, id, target_key_name, 
1710                         target_key_len, LDNS_RR_TYPE_DS, vq->qchase.qclass,
1711                         BIT_CD, &newq, 0)) {
1712                         log_err("mem error generating DS request");
1713                         return val_error(qstate, id);
1714                 }
1715                 return 0;
1716         }
1717
1718         /* Otherwise, it is time to query for the DNSKEY */
1719         if(!generate_request(qstate, id, vq->ds_rrset->rk.dname, 
1720                 vq->ds_rrset->rk.dname_len, LDNS_RR_TYPE_DNSKEY, 
1721                 vq->qchase.qclass, BIT_CD, &newq, 0)) {
1722                 log_err("mem error generating DNSKEY request");
1723                 return val_error(qstate, id);
1724         }
1725
1726         return 0;
1727 }
1728
1729 /**
1730  * Process the VALIDATE stage, the init and findkey stages are finished,
1731  * and the right keys are available to validate the response.
1732  * Or, there are no keys available, in order to invalidate the response.
1733  *
1734  * After validation, the status is recorded in the message and rrsets,
1735  * and finished state is started.
1736  *
1737  * @param qstate: query state.
1738  * @param vq: validator query state.
1739  * @param ve: validator shared global environment.
1740  * @param id: module id.
1741  * @return true if the event should be processed further on return, false if
1742  *         not.
1743  */
1744 static int
1745 processValidate(struct module_qstate* qstate, struct val_qstate* vq, 
1746         struct val_env* ve, int id)
1747 {
1748         enum val_classification subtype;
1749         int rcode;
1750
1751         if(!vq->key_entry) {
1752                 verbose(VERB_ALGO, "validate: no key entry, failed");
1753                 return val_error(qstate, id);
1754         }
1755
1756         /* This is the default next state. */
1757         vq->state = VAL_FINISHED_STATE;
1758
1759         /* Unsigned responses must be underneath a "null" key entry.*/
1760         if(key_entry_isnull(vq->key_entry)) {
1761                 verbose(VERB_DETAIL, "Verified that %sresponse is INSECURE",
1762                         vq->signer_name?"":"unsigned ");
1763                 vq->chase_reply->security = sec_status_insecure;
1764                 val_mark_insecure(vq->chase_reply, vq->key_entry->name, 
1765                         qstate->env->rrset_cache, qstate->env);
1766                 key_cache_insert(ve->kcache, vq->key_entry, qstate);
1767                 return 1;
1768         }
1769
1770         if(key_entry_isbad(vq->key_entry)) {
1771                 log_nametypeclass(VERB_DETAIL, "Could not establish a chain "
1772                         "of trust to keys for", vq->key_entry->name,
1773                         LDNS_RR_TYPE_DNSKEY, vq->key_entry->key_class);
1774                 vq->chase_reply->security = sec_status_bogus;
1775                 errinf(qstate, "while building chain of trust");
1776                 if(vq->restart_count >= VAL_MAX_RESTART_COUNT)
1777                         key_cache_insert(ve->kcache, vq->key_entry, qstate);
1778                 return 1;
1779         }
1780
1781         /* signerName being null is the indicator that this response was 
1782          * unsigned */
1783         if(vq->signer_name == NULL) {
1784                 log_query_info(VERB_ALGO, "processValidate: state has no "
1785                         "signer name", &vq->qchase);
1786                 verbose(VERB_DETAIL, "Could not establish validation of "
1787                           "INSECURE status of unsigned response.");
1788                 errinf(qstate, "no signatures");
1789                 errinf_origin(qstate, qstate->reply_origin);
1790                 vq->chase_reply->security = sec_status_bogus;
1791                 return 1;
1792         }
1793         subtype = val_classify_response(qstate->query_flags, &qstate->qinfo,
1794                 &vq->qchase, vq->orig_msg->rep, vq->rrset_skip);
1795         if(subtype != VAL_CLASS_REFERRAL)
1796                 remove_spurious_authority(vq->chase_reply, vq->orig_msg->rep);
1797
1798         /* check signatures in the message; 
1799          * answer and authority must be valid, additional is only checked. */
1800         if(!validate_msg_signatures(qstate, qstate->env, ve, &vq->qchase, 
1801                 vq->chase_reply, vq->key_entry)) {
1802                 /* workaround bad recursor out there that truncates (even
1803                  * with EDNS4k) to 512 by removing RRSIG from auth section
1804                  * for positive replies*/
1805                 if((subtype == VAL_CLASS_POSITIVE || subtype == VAL_CLASS_ANY
1806                         || subtype == VAL_CLASS_CNAME) &&
1807                         detect_wrongly_truncated(vq->orig_msg->rep)) {
1808                         /* truncate the message some more */
1809                         vq->orig_msg->rep->ns_numrrsets = 0;
1810                         vq->orig_msg->rep->ar_numrrsets = 0;
1811                         vq->orig_msg->rep->rrset_count = 
1812                                 vq->orig_msg->rep->an_numrrsets;
1813                         vq->chase_reply->ns_numrrsets = 0;
1814                         vq->chase_reply->ar_numrrsets = 0;
1815                         vq->chase_reply->rrset_count = 
1816                                 vq->chase_reply->an_numrrsets;
1817                         qstate->errinf = NULL;
1818                 }
1819                 else {
1820                         verbose(VERB_DETAIL, "Validate: message contains "
1821                                 "bad rrsets");
1822                         return 1;
1823                 }
1824         }
1825
1826         switch(subtype) {
1827                 case VAL_CLASS_POSITIVE:
1828                         verbose(VERB_ALGO, "Validating a positive response");
1829                         validate_positive_response(qstate->env, ve,
1830                                 &vq->qchase, vq->chase_reply, vq->key_entry);
1831                         verbose(VERB_DETAIL, "validate(positive): %s",
1832                                 sec_status_to_string(
1833                                 vq->chase_reply->security));
1834                         break;
1835
1836                 case VAL_CLASS_NODATA:
1837                         verbose(VERB_ALGO, "Validating a nodata response");
1838                         validate_nodata_response(qstate->env, ve,
1839                                 &vq->qchase, vq->chase_reply, vq->key_entry);
1840                         verbose(VERB_DETAIL, "validate(nodata): %s",
1841                                 sec_status_to_string(
1842                                 vq->chase_reply->security));
1843                         break;
1844
1845                 case VAL_CLASS_NAMEERROR:
1846                         rcode = (int)FLAGS_GET_RCODE(vq->orig_msg->rep->flags);
1847                         verbose(VERB_ALGO, "Validating a nxdomain response");
1848                         validate_nameerror_response(qstate->env, ve, 
1849                                 &vq->qchase, vq->chase_reply, vq->key_entry, &rcode);
1850                         verbose(VERB_DETAIL, "validate(nxdomain): %s",
1851                                 sec_status_to_string(
1852                                 vq->chase_reply->security));
1853                         FLAGS_SET_RCODE(vq->orig_msg->rep->flags, rcode);
1854                         FLAGS_SET_RCODE(vq->chase_reply->flags, rcode);
1855                         break;
1856
1857                 case VAL_CLASS_CNAME:
1858                         verbose(VERB_ALGO, "Validating a cname response");
1859                         validate_cname_response(qstate->env, ve,
1860                                 &vq->qchase, vq->chase_reply, vq->key_entry);
1861                         verbose(VERB_DETAIL, "validate(cname): %s",
1862                                 sec_status_to_string(
1863                                 vq->chase_reply->security));
1864                         break;
1865
1866                 case VAL_CLASS_CNAMENOANSWER:
1867                         verbose(VERB_ALGO, "Validating a cname noanswer "
1868                                 "response");
1869                         validate_cname_noanswer_response(qstate->env, ve,
1870                                 &vq->qchase, vq->chase_reply, vq->key_entry);
1871                         verbose(VERB_DETAIL, "validate(cname_noanswer): %s",
1872                                 sec_status_to_string(
1873                                 vq->chase_reply->security));
1874                         break;
1875
1876                 case VAL_CLASS_REFERRAL:
1877                         verbose(VERB_ALGO, "Validating a referral response");
1878                         validate_referral_response(vq->chase_reply);
1879                         verbose(VERB_DETAIL, "validate(referral): %s",
1880                                 sec_status_to_string(
1881                                 vq->chase_reply->security));
1882                         break;
1883
1884                 case VAL_CLASS_ANY:
1885                         verbose(VERB_ALGO, "Validating a positive ANY "
1886                                 "response");
1887                         validate_any_response(qstate->env, ve, &vq->qchase, 
1888                                 vq->chase_reply, vq->key_entry);
1889                         verbose(VERB_DETAIL, "validate(positive_any): %s",
1890                                 sec_status_to_string(
1891                                 vq->chase_reply->security));
1892                         break;
1893
1894                 default:
1895                         log_err("validate: unhandled response subtype: %d",
1896                                 subtype);
1897         }
1898         if(vq->chase_reply->security == sec_status_bogus) {
1899                 if(subtype == VAL_CLASS_POSITIVE)
1900                         errinf(qstate, "wildcard");
1901                 else errinf(qstate, val_classification_to_string(subtype));
1902                 errinf(qstate, "proof failed");
1903                 errinf_origin(qstate, qstate->reply_origin);
1904         }
1905
1906         return 1;
1907 }
1908
1909 /**
1910  * Init DLV check.
1911  * DLV is going to be decommissioned, but the code is still here for some time.
1912  *
1913  * Called when a query is determined by other trust anchors to be insecure
1914  * (or indeterminate).  Then we look if there is a key in the DLV.
1915  * Performs aggressive negative cache check to see if there is no key.
1916  * Otherwise, spawns a DLV query, and changes to the DLV wait state.
1917  *
1918  * @param qstate: query state.
1919  * @param vq: validator query state.
1920  * @param ve: validator shared global environment.
1921  * @param id: module id.
1922  * @return  true if there is no DLV.
1923  *      false: processing is finished for the validator operate().
1924  *      This function may exit in three ways:
1925  *         o    no DLV (aggressive cache), so insecure. (true)
1926  *         o    error - stop processing (false)
1927  *         o    DLV lookup was started, stop processing (false)
1928  */
1929 static int
1930 val_dlv_init(struct module_qstate* qstate, struct val_qstate* vq, 
1931         struct val_env* ve, int id)
1932 {
1933         uint8_t* nm;
1934         size_t nm_len;
1935         struct module_qstate* newq = NULL;
1936         /* there must be a DLV configured */
1937         log_assert(qstate->env->anchors->dlv_anchor);
1938         /* this bool is true to avoid looping in the DLV checks */
1939         log_assert(vq->dlv_checked);
1940
1941         /* init the DLV lookup variables */
1942         vq->dlv_lookup_name = NULL;
1943         vq->dlv_lookup_name_len = 0;
1944         vq->dlv_insecure_at = NULL;
1945         vq->dlv_insecure_at_len = 0;
1946
1947         /* Determine the name for which we want to lookup DLV.
1948          * This name is for the current message, or 
1949          * for the current RRset for CNAME, referral subtypes.
1950          * If there is a signer, use that, otherwise the domain name */
1951         if(vq->signer_name) {
1952                 nm = vq->signer_name;
1953                 nm_len = vq->signer_len;
1954         } else {
1955                 /* use qchase */
1956                 nm = vq->qchase.qname;
1957                 nm_len = vq->qchase.qname_len;
1958                 if(vq->qchase.qtype == LDNS_RR_TYPE_DS)
1959                         dname_remove_label(&nm, &nm_len);
1960         }
1961         log_nametypeclass(VERB_ALGO, "DLV init look", nm, LDNS_RR_TYPE_DS,
1962                 vq->qchase.qclass);
1963         log_assert(nm && nm_len);
1964         /* sanity check: no DLV lookups below the DLV anchor itself.
1965          * Like, an securely insecure delegation there makes no sense. */
1966         if(dname_subdomain_c(nm, qstate->env->anchors->dlv_anchor->name)) {
1967                 verbose(VERB_ALGO, "DLV lookup within DLV repository denied");
1968                 return 1;
1969         }
1970         /* concat name (minus root label) + dlv name */
1971         vq->dlv_lookup_name_len = nm_len - 1 + 
1972                 qstate->env->anchors->dlv_anchor->namelen;
1973         vq->dlv_lookup_name = regional_alloc(qstate->region, 
1974                 vq->dlv_lookup_name_len);
1975         if(!vq->dlv_lookup_name) {
1976                 log_err("Out of memory preparing DLV lookup");
1977                 return val_error(qstate, id);
1978         }
1979         memmove(vq->dlv_lookup_name, nm, nm_len-1);
1980         memmove(vq->dlv_lookup_name+nm_len-1, 
1981                 qstate->env->anchors->dlv_anchor->name, 
1982                 qstate->env->anchors->dlv_anchor->namelen);
1983         log_nametypeclass(VERB_ALGO, "DLV name", vq->dlv_lookup_name, 
1984                 LDNS_RR_TYPE_DLV, vq->qchase.qclass);
1985
1986         /* determine where the insecure point was determined, the DLV must 
1987          * be equal or below that to continue building the trust chain 
1988          * down. May be NULL if no trust chain was built yet */
1989         nm = NULL;
1990         if(vq->key_entry && key_entry_isnull(vq->key_entry)) {
1991                 nm = vq->key_entry->name;
1992                 nm_len = vq->key_entry->namelen;
1993         }
1994         if(nm) {
1995                 vq->dlv_insecure_at_len = nm_len - 1 +
1996                         qstate->env->anchors->dlv_anchor->namelen;
1997                 vq->dlv_insecure_at = regional_alloc(qstate->region,
1998                         vq->dlv_insecure_at_len);
1999                 if(!vq->dlv_insecure_at) {
2000                         log_err("Out of memory preparing DLV lookup");
2001                         return val_error(qstate, id);
2002                 }
2003                 memmove(vq->dlv_insecure_at, nm, nm_len-1);
2004                 memmove(vq->dlv_insecure_at+nm_len-1, 
2005                         qstate->env->anchors->dlv_anchor->name, 
2006                         qstate->env->anchors->dlv_anchor->namelen);
2007                 log_nametypeclass(VERB_ALGO, "insecure_at", 
2008                         vq->dlv_insecure_at, 0, vq->qchase.qclass);
2009         }
2010
2011         /* If we can find the name in the aggressive negative cache,
2012          * give up; insecure is the answer */
2013         while(val_neg_dlvlookup(ve->neg_cache, vq->dlv_lookup_name,
2014                 vq->dlv_lookup_name_len, vq->qchase.qclass,
2015                 qstate->env->rrset_cache, *qstate->env->now)) {
2016                 /* go up */
2017                 dname_remove_label(&vq->dlv_lookup_name, 
2018                         &vq->dlv_lookup_name_len);
2019                 /* too high? */
2020                 if(!dname_subdomain_c(vq->dlv_lookup_name,
2021                         qstate->env->anchors->dlv_anchor->name)) {
2022                         verbose(VERB_ALGO, "ask above dlv repo");
2023                         return 1; /* Above the repo is insecure */
2024                 }
2025                 /* above chain of trust? */
2026                 if(vq->dlv_insecure_at && !dname_subdomain_c(
2027                         vq->dlv_lookup_name, vq->dlv_insecure_at)) {
2028                         verbose(VERB_ALGO, "ask above insecure endpoint");
2029                         return 1;
2030                 }
2031         }
2032
2033         /* perform a lookup for the DLV; with validation */
2034         vq->state = VAL_DLVLOOKUP_STATE;
2035         if(!generate_request(qstate, id, vq->dlv_lookup_name, 
2036                 vq->dlv_lookup_name_len, LDNS_RR_TYPE_DLV,
2037                 vq->qchase.qclass, 0, &newq, 0)) {
2038                 return val_error(qstate, id);
2039         }
2040
2041         /* Find the closest encloser DLV from the repository.
2042          * then that is used to build another chain of trust 
2043          * This may first require a query 'too low' that has NSECs in
2044          * the answer, from which we determine the closest encloser DLV. 
2045          * When determine the closest encloser, skip empty nonterminals,
2046          * since we want a nonempty node in the DLV repository. */
2047
2048         return 0;
2049 }
2050
2051 /**
2052  * The Finished state. The validation status (good or bad) has been determined.
2053  *
2054  * @param qstate: query state.
2055  * @param vq: validator query state.
2056  * @param ve: validator shared global environment.
2057  * @param id: module id.
2058  * @return true if the event should be processed further on return, false if
2059  *         not.
2060  */
2061 static int
2062 processFinished(struct module_qstate* qstate, struct val_qstate* vq, 
2063         struct val_env* ve, int id)
2064 {
2065         enum val_classification subtype = val_classify_response(
2066                 qstate->query_flags, &qstate->qinfo, &vq->qchase, 
2067                 vq->orig_msg->rep, vq->rrset_skip);
2068
2069         /* if the result is insecure or indeterminate and we have not 
2070          * checked the DLV yet, check the DLV */
2071         if((vq->chase_reply->security == sec_status_insecure ||
2072                 vq->chase_reply->security == sec_status_indeterminate) &&
2073                 qstate->env->anchors->dlv_anchor && !vq->dlv_checked) {
2074                 vq->dlv_checked = 1;
2075                 if(!val_dlv_init(qstate, vq, ve, id))
2076                         return 0;
2077         }
2078
2079         /* store overall validation result in orig_msg */
2080         if(vq->rrset_skip == 0)
2081                 vq->orig_msg->rep->security = vq->chase_reply->security;
2082         else if(subtype != VAL_CLASS_REFERRAL ||
2083                 vq->rrset_skip < vq->orig_msg->rep->an_numrrsets + 
2084                 vq->orig_msg->rep->ns_numrrsets) {
2085                 /* ignore sec status of additional section if a referral 
2086                  * type message skips there and
2087                  * use the lowest security status as end result. */
2088                 if(vq->chase_reply->security < vq->orig_msg->rep->security)
2089                         vq->orig_msg->rep->security = 
2090                                 vq->chase_reply->security;
2091         }
2092
2093         if(subtype == VAL_CLASS_REFERRAL) {
2094                 /* for a referral, move to next unchecked rrset and check it*/
2095                 vq->rrset_skip = val_next_unchecked(vq->orig_msg->rep, 
2096                         vq->rrset_skip);
2097                 if(vq->rrset_skip < vq->orig_msg->rep->rrset_count) {
2098                         /* and restart for this rrset */
2099                         verbose(VERB_ALGO, "validator: go to next rrset");
2100                         vq->chase_reply->security = sec_status_unchecked;
2101                         vq->dlv_checked = 0; /* can do DLV for this RR */
2102                         vq->state = VAL_INIT_STATE;
2103                         return 1;
2104                 }
2105                 /* referral chase is done */
2106         }
2107         if(vq->chase_reply->security != sec_status_bogus &&
2108                 subtype == VAL_CLASS_CNAME) {
2109                 /* chase the CNAME; process next part of the message */
2110                 if(!val_chase_cname(&vq->qchase, vq->orig_msg->rep, 
2111                         &vq->rrset_skip)) {
2112                         verbose(VERB_ALGO, "validator: failed to chase CNAME");
2113                         vq->orig_msg->rep->security = sec_status_bogus;
2114                 } else {
2115                         /* restart process for new qchase at rrset_skip */
2116                         log_query_info(VERB_ALGO, "validator: chased to",
2117                                 &vq->qchase);
2118                         vq->chase_reply->security = sec_status_unchecked;
2119                         vq->dlv_checked = 0; /* can do DLV for this RR */
2120                         vq->state = VAL_INIT_STATE;
2121                         return 1;
2122                 }
2123         }
2124
2125         if(vq->orig_msg->rep->security == sec_status_secure) {
2126                 /* If the message is secure, check that all rrsets are
2127                  * secure (i.e. some inserted RRset for CNAME chain with
2128                  * a different signer name). And drop additional rrsets
2129                  * that are not secure (if clean-additional option is set) */
2130                 /* this may cause the msg to be marked bogus */
2131                 val_check_nonsecure(qstate->env, vq->orig_msg->rep);
2132                 if(vq->orig_msg->rep->security == sec_status_secure) {
2133                         log_query_info(VERB_DETAIL, "validation success", 
2134                                 &qstate->qinfo);
2135                 }
2136         }
2137
2138         /* if the result is bogus - set message ttl to bogus ttl to avoid
2139          * endless bogus revalidation */
2140         if(vq->orig_msg->rep->security == sec_status_bogus) {
2141                 /* see if we can try again to fetch data */
2142                 if(vq->restart_count < VAL_MAX_RESTART_COUNT) {
2143                         int restart_count = vq->restart_count+1;
2144                         verbose(VERB_ALGO, "validation failed, "
2145                                 "blacklist and retry to fetch data");
2146                         val_blacklist(&qstate->blacklist, qstate->region, 
2147                                 qstate->reply_origin, 0);
2148                         qstate->reply_origin = NULL;
2149                         qstate->errinf = NULL;
2150                         memset(vq, 0, sizeof(*vq));
2151                         vq->restart_count = restart_count;
2152                         vq->state = VAL_INIT_STATE;
2153                         verbose(VERB_ALGO, "pass back to next module");
2154                         qstate->ext_state[id] = module_restart_next;
2155                         return 0;
2156                 }
2157
2158                 vq->orig_msg->rep->ttl = ve->bogus_ttl;
2159                 vq->orig_msg->rep->prefetch_ttl = 
2160                         PREFETCH_TTL_CALC(vq->orig_msg->rep->ttl);
2161                 if(qstate->env->cfg->val_log_level >= 1 &&
2162                         !qstate->env->cfg->val_log_squelch) {
2163                         if(qstate->env->cfg->val_log_level < 2)
2164                                 log_query_info(0, "validation failure",
2165                                         &qstate->qinfo);
2166                         else {
2167                                 char* err = errinf_to_str(qstate);
2168                                 if(err) log_info("%s", err);
2169                                 free(err);
2170                         }
2171                 }
2172                 /*
2173                  * If set, the validator will not make messages bogus, instead
2174                  * indeterminate is issued, so that no clients receive SERVFAIL.
2175                  * This allows an operator to run validation 'shadow' without
2176                  * hurting responses to clients.
2177                  */
2178                 /* If we are in permissive mode, bogus gets indeterminate */
2179                 if(qstate->env->cfg->val_permissive_mode)
2180                         vq->orig_msg->rep->security = sec_status_indeterminate;
2181         }
2182
2183         /* store results in cache */
2184         if(qstate->query_flags&BIT_RD) {
2185                 /* if secure, this will override cache anyway, no need
2186                  * to check if from parentNS */
2187                 if(!qstate->no_cache_store) {
2188                         if(!dns_cache_store(qstate->env, &vq->orig_msg->qinfo,
2189                                 vq->orig_msg->rep, 0, qstate->prefetch_leeway, 0, NULL,
2190                                 qstate->query_flags)) {
2191                                 log_err("out of memory caching validator results");
2192                         }
2193                 }
2194         } else {
2195                 /* for a referral, store the verified RRsets */
2196                 /* and this does not get prefetched, so no leeway */
2197                 if(!dns_cache_store(qstate->env, &vq->orig_msg->qinfo,
2198                         vq->orig_msg->rep, 1, 0, 0, NULL,
2199                         qstate->query_flags)) {
2200                         log_err("out of memory caching validator results");
2201                 }
2202         }
2203         qstate->return_rcode = LDNS_RCODE_NOERROR;
2204         qstate->return_msg = vq->orig_msg;
2205         qstate->ext_state[id] = module_finished;
2206         return 0;
2207 }
2208
2209 /**
2210  * The DLVLookup state. Process DLV lookups.
2211  *
2212  * @param qstate: query state.
2213  * @param vq: validator query state.
2214  * @param ve: validator shared global environment.
2215  * @param id: module id.
2216  * @return true if the event should be processed further on return, false if
2217  *         not.
2218  */
2219 static int
2220 processDLVLookup(struct module_qstate* qstate, struct val_qstate* vq, 
2221         struct val_env* ve, int id)
2222 {
2223         struct module_qstate* newq = NULL;
2224         /* see if this we are ready to continue normal resolution */
2225         /* we may need more DLV lookups */
2226         if(vq->dlv_status==dlv_error)
2227                 verbose(VERB_ALGO, "DLV woke up with status dlv_error");
2228         else if(vq->dlv_status==dlv_success)
2229                 verbose(VERB_ALGO, "DLV woke up with status dlv_success");
2230         else if(vq->dlv_status==dlv_ask_higher)
2231                 verbose(VERB_ALGO, "DLV woke up with status dlv_ask_higher");
2232         else if(vq->dlv_status==dlv_there_is_no_dlv)
2233                 verbose(VERB_ALGO, "DLV woke up with status dlv_there_is_no_dlv");
2234         else    verbose(VERB_ALGO, "DLV woke up with status unknown");
2235
2236         if(vq->dlv_status == dlv_error) {
2237                 verbose(VERB_QUERY, "failed DLV lookup");
2238                 return val_error(qstate, id);
2239         } else if(vq->dlv_status == dlv_success) {
2240                 uint8_t* nm;
2241                 size_t nmlen;
2242                 /* chain continues with DNSKEY, continue in FINDKEY */
2243                 vq->state = VAL_FINDKEY_STATE;
2244
2245                 /* strip off the DLV suffix from the name; could result in . */
2246                 log_assert(dname_subdomain_c(vq->ds_rrset->rk.dname,
2247                         qstate->env->anchors->dlv_anchor->name));
2248                 nmlen = vq->ds_rrset->rk.dname_len -
2249                         qstate->env->anchors->dlv_anchor->namelen + 1;
2250                 nm = regional_alloc_init(qstate->region, 
2251                         vq->ds_rrset->rk.dname, nmlen);
2252                 if(!nm) {
2253                         log_err("Out of memory in DLVLook");
2254                         return val_error(qstate, id);
2255                 }
2256                 nm[nmlen-1] = 0;
2257
2258                 vq->ds_rrset->rk.dname = nm;
2259                 vq->ds_rrset->rk.dname_len = nmlen;
2260
2261                 /* create a nullentry for the key so the dnskey lookup
2262                  * can be retried after a validation failure for it */
2263                 vq->key_entry = key_entry_create_null(qstate->region,
2264                         nm, nmlen, vq->qchase.qclass, 0, 0);
2265                 if(!vq->key_entry) {
2266                         log_err("Out of memory in DLVLook");
2267                         return val_error(qstate, id);
2268                 }
2269
2270                 if(!generate_request(qstate, id, vq->ds_rrset->rk.dname, 
2271                         vq->ds_rrset->rk.dname_len, LDNS_RR_TYPE_DNSKEY, 
2272                         vq->qchase.qclass, BIT_CD, &newq, 0)) {
2273                         log_err("mem error generating DNSKEY request");
2274                         return val_error(qstate, id);
2275                 }
2276                 return 0;
2277         } else if(vq->dlv_status == dlv_there_is_no_dlv) {
2278                 /* continue with the insecure result we got */
2279                 vq->state = VAL_FINISHED_STATE;
2280                 return 1;
2281         } 
2282         log_assert(vq->dlv_status == dlv_ask_higher);
2283
2284         /* ask higher, make sure we stay in DLV repo, below dlv_at */
2285         if(!dname_subdomain_c(vq->dlv_lookup_name,
2286                 qstate->env->anchors->dlv_anchor->name)) {
2287                 /* just like, there is no DLV */
2288                 verbose(VERB_ALGO, "ask above dlv repo");
2289                 vq->state = VAL_FINISHED_STATE;
2290                 return 1;
2291         }
2292         if(vq->dlv_insecure_at && !dname_subdomain_c(vq->dlv_lookup_name,
2293                 vq->dlv_insecure_at)) {
2294                 /* already checked a chain lower than dlv_lookup_name */
2295                 verbose(VERB_ALGO, "ask above insecure endpoint");
2296                 log_nametypeclass(VERB_ALGO, "enpt", vq->dlv_insecure_at, 0, 0);
2297                 vq->state = VAL_FINISHED_STATE;
2298                 return 1;
2299         }
2300
2301         /* check negative cache before making new request */
2302         if(val_neg_dlvlookup(ve->neg_cache, vq->dlv_lookup_name,
2303                 vq->dlv_lookup_name_len, vq->qchase.qclass,
2304                 qstate->env->rrset_cache, *qstate->env->now)) {
2305                 /* does not exist, go up one (go higher). */
2306                 dname_remove_label(&vq->dlv_lookup_name, 
2307                         &vq->dlv_lookup_name_len);
2308                 /* limit number of labels, limited number of recursion */
2309                 return processDLVLookup(qstate, vq, ve, id);
2310         }
2311
2312         if(!generate_request(qstate, id, vq->dlv_lookup_name,
2313                 vq->dlv_lookup_name_len, LDNS_RR_TYPE_DLV, 
2314                 vq->qchase.qclass, 0, &newq, 0)) {
2315                 return val_error(qstate, id);
2316         }
2317
2318         return 0;
2319 }
2320
2321 /** 
2322  * Handle validator state.
2323  * If a method returns true, the next state is started. If false, then
2324  * processing will stop.
2325  * @param qstate: query state.
2326  * @param vq: validator query state.
2327  * @param ve: validator shared global environment.
2328  * @param id: module id.
2329  */
2330 static void
2331 val_handle(struct module_qstate* qstate, struct val_qstate* vq, 
2332         struct val_env* ve, int id)
2333 {
2334         int cont = 1;
2335         while(cont) {
2336                 verbose(VERB_ALGO, "val handle processing q with state %s",
2337                         val_state_to_string(vq->state));
2338                 switch(vq->state) {
2339                         case VAL_INIT_STATE:
2340                                 cont = processInit(qstate, vq, ve, id);
2341                                 break;
2342                         case VAL_FINDKEY_STATE: 
2343                                 cont = processFindKey(qstate, vq, id);
2344                                 break;
2345                         case VAL_VALIDATE_STATE: 
2346                                 cont = processValidate(qstate, vq, ve, id);
2347                                 break;
2348                         case VAL_FINISHED_STATE: 
2349                                 cont = processFinished(qstate, vq, ve, id);
2350                                 break;
2351                         case VAL_DLVLOOKUP_STATE: 
2352                                 cont = processDLVLookup(qstate, vq, ve, id);
2353                                 break;
2354                         default:
2355                                 log_warn("validator: invalid state %d",
2356                                         vq->state);
2357                                 cont = 0;
2358                                 break;
2359                 }
2360         }
2361 }
2362
2363 void
2364 val_operate(struct module_qstate* qstate, enum module_ev event, int id,
2365         struct outbound_entry* outbound)
2366 {
2367         struct val_env* ve = (struct val_env*)qstate->env->modinfo[id];
2368         struct val_qstate* vq = (struct val_qstate*)qstate->minfo[id];
2369         verbose(VERB_QUERY, "validator[module %d] operate: extstate:%s "
2370                 "event:%s", id, strextstate(qstate->ext_state[id]), 
2371                 strmodulevent(event));
2372         log_query_info(VERB_QUERY, "validator operate: query",
2373                 &qstate->qinfo);
2374         if(vq && qstate->qinfo.qname != vq->qchase.qname) 
2375                 log_query_info(VERB_QUERY, "validator operate: chased to",
2376                 &vq->qchase);
2377         (void)outbound;
2378         if(event == module_event_new || 
2379                 (event == module_event_pass && vq == NULL)) {
2380
2381                 /* pass request to next module, to get it */
2382                 verbose(VERB_ALGO, "validator: pass to next module");
2383                 qstate->ext_state[id] = module_wait_module;
2384                 return;
2385         }
2386         if(event == module_event_moddone) {
2387                 /* check if validation is needed */
2388                 verbose(VERB_ALGO, "validator: nextmodule returned");
2389
2390                 if(!needs_validation(qstate, qstate->return_rcode, 
2391                         qstate->return_msg)) {
2392                         /* no need to validate this */
2393                         if(qstate->return_msg)
2394                                 qstate->return_msg->rep->security =
2395                                         sec_status_indeterminate;
2396                         qstate->ext_state[id] = module_finished;
2397                         return;
2398                 }
2399                 if(already_validated(qstate->return_msg)) {
2400                         qstate->ext_state[id] = module_finished;
2401                         return;
2402                 }
2403                 /* qclass ANY should have validation result from spawned 
2404                  * queries. If we get here, it is bogus or an internal error */
2405                 if(qstate->qinfo.qclass == LDNS_RR_CLASS_ANY) {
2406                         verbose(VERB_ALGO, "cannot validate classANY: bogus");
2407                         if(qstate->return_msg)
2408                                 qstate->return_msg->rep->security =
2409                                         sec_status_bogus;
2410                         qstate->ext_state[id] = module_finished;
2411                         return;
2412                 }
2413                 /* create state to start validation */
2414                 qstate->ext_state[id] = module_error; /* override this */
2415                 if(!vq) {
2416                         vq = val_new(qstate, id);
2417                         if(!vq) {
2418                                 log_err("validator: malloc failure");
2419                                 qstate->ext_state[id] = module_error;
2420                                 return;
2421                         }
2422                 } else if(!vq->orig_msg) {
2423                         if(!val_new_getmsg(qstate, vq)) {
2424                                 log_err("validator: malloc failure");
2425                                 qstate->ext_state[id] = module_error;
2426                                 return;
2427                         }
2428                 }
2429                 val_handle(qstate, vq, ve, id);
2430                 return;
2431         }
2432         if(event == module_event_pass) {
2433                 qstate->ext_state[id] = module_error; /* override this */
2434                 /* continue processing, since val_env exists */
2435                 val_handle(qstate, vq, ve, id);
2436                 return;
2437         }
2438         log_err("validator: bad event %s", strmodulevent(event));
2439         qstate->ext_state[id] = module_error;
2440         return;
2441 }
2442
2443 /**
2444  * Evaluate the response to a priming request.
2445  *
2446  * @param dnskey_rrset: DNSKEY rrset (can be NULL if none) in prime reply.
2447  *      (this rrset is allocated in the wrong region, not the qstate).
2448  * @param ta: trust anchor.
2449  * @param qstate: qstate that needs key.
2450  * @param id: module id.
2451  * @return new key entry or NULL on allocation failure.
2452  *      The key entry will either contain a validated DNSKEY rrset, or
2453  *      represent a Null key (query failed, but validation did not), or a
2454  *      Bad key (validation failed).
2455  */
2456 static struct key_entry_key*
2457 primeResponseToKE(struct ub_packed_rrset_key* dnskey_rrset, 
2458         struct trust_anchor* ta, struct module_qstate* qstate, int id)
2459 {
2460         struct val_env* ve = (struct val_env*)qstate->env->modinfo[id];
2461         struct key_entry_key* kkey = NULL;
2462         enum sec_status sec = sec_status_unchecked;
2463         char* reason = NULL;
2464         int downprot = qstate->env->cfg->harden_algo_downgrade;
2465
2466         if(!dnskey_rrset) {
2467                 log_nametypeclass(VERB_OPS, "failed to prime trust anchor -- "
2468                         "could not fetch DNSKEY rrset", 
2469                         ta->name, LDNS_RR_TYPE_DNSKEY, ta->dclass);
2470                 if(qstate->env->cfg->harden_dnssec_stripped) {
2471                         errinf(qstate, "no DNSKEY rrset");
2472                         kkey = key_entry_create_bad(qstate->region, ta->name,
2473                                 ta->namelen, ta->dclass, BOGUS_KEY_TTL,
2474                                 *qstate->env->now);
2475                 } else  kkey = key_entry_create_null(qstate->region, ta->name,
2476                                 ta->namelen, ta->dclass, NULL_KEY_TTL,
2477                                 *qstate->env->now);
2478                 if(!kkey) {
2479                         log_err("out of memory: allocate fail prime key");
2480                         return NULL;
2481                 }
2482                 return kkey;
2483         }
2484         /* attempt to verify with trust anchor DS and DNSKEY */
2485         kkey = val_verify_new_DNSKEYs_with_ta(qstate->region, qstate->env, ve, 
2486                 dnskey_rrset, ta->ds_rrset, ta->dnskey_rrset, downprot,
2487                 &reason);
2488         if(!kkey) {
2489                 log_err("out of memory: verifying prime TA");
2490                 return NULL;
2491         }
2492         if(key_entry_isgood(kkey))
2493                 sec = sec_status_secure;
2494         else
2495                 sec = sec_status_bogus;
2496         verbose(VERB_DETAIL, "validate keys with anchor(DS): %s", 
2497                 sec_status_to_string(sec));
2498
2499         if(sec != sec_status_secure) {
2500                 log_nametypeclass(VERB_OPS, "failed to prime trust anchor -- "
2501                         "DNSKEY rrset is not secure", 
2502                         ta->name, LDNS_RR_TYPE_DNSKEY, ta->dclass);
2503                 /* NOTE: in this case, we should probably reject the trust 
2504                  * anchor for longer, perhaps forever. */
2505                 if(qstate->env->cfg->harden_dnssec_stripped) {
2506                         errinf(qstate, reason);
2507                         kkey = key_entry_create_bad(qstate->region, ta->name,
2508                                 ta->namelen, ta->dclass, BOGUS_KEY_TTL,
2509                                 *qstate->env->now);
2510                 } else  kkey = key_entry_create_null(qstate->region, ta->name,
2511                                 ta->namelen, ta->dclass, NULL_KEY_TTL,
2512                                 *qstate->env->now);
2513                 if(!kkey) {
2514                         log_err("out of memory: allocate null prime key");
2515                         return NULL;
2516                 }
2517                 return kkey;
2518         }
2519
2520         log_nametypeclass(VERB_DETAIL, "Successfully primed trust anchor", 
2521                 ta->name, LDNS_RR_TYPE_DNSKEY, ta->dclass);
2522         return kkey;
2523 }
2524
2525 /**
2526  * In inform supers, with the resulting message and rcode and the current
2527  * keyset in the super state, validate the DS response, returning a KeyEntry.
2528  *
2529  * @param qstate: query state that is validating and asked for a DS.
2530  * @param vq: validator query state
2531  * @param id: module id.
2532  * @param rcode: rcode result value.
2533  * @param msg: result message (if rcode is OK).
2534  * @param qinfo: from the sub query state, query info.
2535  * @param ke: the key entry to return. It returns
2536  *      is_bad if the DS response fails to validate, is_null if the
2537  *      DS response indicated an end to secure space, is_good if the DS
2538  *      validated. It returns ke=NULL if the DS response indicated that the
2539  *      request wasn't a delegation point.
2540  * @return 0 on servfail error (malloc failure).
2541  */
2542 static int
2543 ds_response_to_ke(struct module_qstate* qstate, struct val_qstate* vq,
2544         int id, int rcode, struct dns_msg* msg, struct query_info* qinfo,
2545         struct key_entry_key** ke)
2546 {
2547         struct val_env* ve = (struct val_env*)qstate->env->modinfo[id];
2548         char* reason = NULL;
2549         enum val_classification subtype;
2550         if(rcode != LDNS_RCODE_NOERROR) {
2551                 char rc[16];
2552                 rc[0]=0;
2553                 (void)sldns_wire2str_rcode_buf(rcode, rc, sizeof(rc));
2554                 /* errors here pretty much break validation */
2555                 verbose(VERB_DETAIL, "DS response was error, thus bogus");
2556                 errinf(qstate, rc);
2557                 errinf(qstate, "no DS");
2558                 goto return_bogus;
2559         }
2560
2561         subtype = val_classify_response(BIT_RD, qinfo, qinfo, msg->rep, 0);
2562         if(subtype == VAL_CLASS_POSITIVE) {
2563                 struct ub_packed_rrset_key* ds;
2564                 enum sec_status sec;
2565                 ds = reply_find_answer_rrset(qinfo, msg->rep);
2566                 /* If there was no DS rrset, then we have mis-classified 
2567                  * this message. */
2568                 if(!ds) {
2569                         log_warn("internal error: POSITIVE DS response was "
2570                                 "missing DS.");
2571                         errinf(qstate, "no DS record");
2572                         goto return_bogus;
2573                 }
2574                 /* Verify only returns BOGUS or SECURE. If the rrset is 
2575                  * bogus, then we are done. */
2576                 sec = val_verify_rrset_entry(qstate->env, ve, ds, 
2577                         vq->key_entry, &reason);
2578                 if(sec != sec_status_secure) {
2579                         verbose(VERB_DETAIL, "DS rrset in DS response did "
2580                                 "not verify");
2581                         errinf(qstate, reason);
2582                         goto return_bogus;
2583                 }
2584
2585                 /* If the DS rrset validates, we still have to make sure 
2586                  * that they are usable. */
2587                 if(!val_dsset_isusable(ds)) {
2588                         /* If they aren't usable, then we treat it like 
2589                          * there was no DS. */
2590                         *ke = key_entry_create_null(qstate->region, 
2591                                 qinfo->qname, qinfo->qname_len, qinfo->qclass, 
2592                                 ub_packed_rrset_ttl(ds), *qstate->env->now);
2593                         return (*ke) != NULL;
2594                 }
2595
2596                 /* Otherwise, we return the positive response. */
2597                 log_query_info(VERB_DETAIL, "validated DS", qinfo);
2598                 *ke = key_entry_create_rrset(qstate->region,
2599                         qinfo->qname, qinfo->qname_len, qinfo->qclass, ds,
2600                         NULL, *qstate->env->now);
2601                 return (*ke) != NULL;
2602         } else if(subtype == VAL_CLASS_NODATA || 
2603                 subtype == VAL_CLASS_NAMEERROR) {
2604                 /* NODATA means that the qname exists, but that there was 
2605                  * no DS.  This is a pretty normal case. */
2606                 time_t proof_ttl = 0;
2607                 enum sec_status sec;
2608
2609                 /* make sure there are NSECs or NSEC3s with signatures */
2610                 if(!val_has_signed_nsecs(msg->rep, &reason)) {
2611                         verbose(VERB_ALGO, "no NSECs: %s", reason);
2612                         errinf(qstate, reason);
2613                         goto return_bogus;
2614                 }
2615
2616                 /* For subtype Name Error.
2617                  * attempt ANS 2.8.1.0 compatibility where it sets rcode
2618                  * to nxdomain, but really this is an Nodata/Noerror response.
2619                  * Find and prove the empty nonterminal in that case */
2620
2621                 /* Try to prove absence of the DS with NSEC */
2622                 sec = val_nsec_prove_nodata_dsreply(
2623                         qstate->env, ve, qinfo, msg->rep, vq->key_entry, 
2624                         &proof_ttl, &reason);
2625                 switch(sec) {
2626                         case sec_status_secure:
2627                                 verbose(VERB_DETAIL, "NSEC RRset for the "
2628                                         "referral proved no DS.");
2629                                 *ke = key_entry_create_null(qstate->region, 
2630                                         qinfo->qname, qinfo->qname_len, 
2631                                         qinfo->qclass, proof_ttl,
2632                                         *qstate->env->now);
2633                                 return (*ke) != NULL;
2634                         case sec_status_insecure:
2635                                 verbose(VERB_DETAIL, "NSEC RRset for the "
2636                                   "referral proved not a delegation point");
2637                                 *ke = NULL;
2638                                 return 1;
2639                         case sec_status_bogus:
2640                                 verbose(VERB_DETAIL, "NSEC RRset for the "
2641                                         "referral did not prove no DS.");
2642                                 errinf(qstate, reason);
2643                                 goto return_bogus;
2644                         case sec_status_unchecked:
2645                         default:
2646                                 /* NSEC proof did not work, try next */
2647                                 break;
2648                 }
2649
2650                 sec = nsec3_prove_nods(qstate->env, ve, 
2651                         msg->rep->rrsets + msg->rep->an_numrrsets,
2652                         msg->rep->ns_numrrsets, qinfo, vq->key_entry, &reason);
2653                 switch(sec) {
2654                         case sec_status_insecure:
2655                                 /* case insecure also continues to unsigned
2656                                  * space.  If nsec3-iter-count too high or
2657                                  * optout, then treat below as unsigned */
2658                         case sec_status_secure:
2659                                 verbose(VERB_DETAIL, "NSEC3s for the "
2660                                         "referral proved no DS.");
2661                                 *ke = key_entry_create_null(qstate->region, 
2662                                         qinfo->qname, qinfo->qname_len, 
2663                                         qinfo->qclass, proof_ttl,
2664                                         *qstate->env->now);
2665                                 return (*ke) != NULL;
2666                         case sec_status_indeterminate:
2667                                 verbose(VERB_DETAIL, "NSEC3s for the "
2668                                   "referral proved no delegation");
2669                                 *ke = NULL;
2670                                 return 1;
2671                         case sec_status_bogus:
2672                                 verbose(VERB_DETAIL, "NSEC3s for the "
2673                                         "referral did not prove no DS.");
2674                                 errinf(qstate, reason);
2675                                 goto return_bogus;
2676                         case sec_status_unchecked:
2677                         default:
2678                                 /* NSEC3 proof did not work */
2679                                 break;
2680                 }
2681
2682                 /* Apparently, no available NSEC/NSEC3 proved NODATA, so 
2683                  * this is BOGUS. */
2684                 verbose(VERB_DETAIL, "DS %s ran out of options, so return "
2685                         "bogus", val_classification_to_string(subtype));
2686                 errinf(qstate, "no DS but also no proof of that");
2687                 goto return_bogus;
2688         } else if(subtype == VAL_CLASS_CNAME || 
2689                 subtype == VAL_CLASS_CNAMENOANSWER) {
2690                 /* if the CNAME matches the exact name we want and is signed
2691                  * properly, then also, we are sure that no DS exists there,
2692                  * much like a NODATA proof */
2693                 enum sec_status sec;
2694                 struct ub_packed_rrset_key* cname;
2695                 cname = reply_find_rrset_section_an(msg->rep, qinfo->qname,
2696                         qinfo->qname_len, LDNS_RR_TYPE_CNAME, qinfo->qclass);
2697                 if(!cname) {
2698                         errinf(qstate, "validator classified CNAME but no "
2699                                 "CNAME of the queried name for DS");
2700                         goto return_bogus;
2701                 }
2702                 if(((struct packed_rrset_data*)cname->entry.data)->rrsig_count
2703                         == 0) {
2704                         if(msg->rep->an_numrrsets != 0 && ntohs(msg->rep->
2705                                 rrsets[0]->rk.type)==LDNS_RR_TYPE_DNAME) {
2706                                 errinf(qstate, "DS got DNAME answer");
2707                         } else {
2708                                 errinf(qstate, "DS got unsigned CNAME answer");
2709                         }
2710                         goto return_bogus;
2711                 }
2712                 sec = val_verify_rrset_entry(qstate->env, ve, cname, 
2713                         vq->key_entry, &reason);
2714                 if(sec == sec_status_secure) {
2715                         verbose(VERB_ALGO, "CNAME validated, "
2716                                 "proof that DS does not exist");
2717                         /* and that it is not a referral point */
2718                         *ke = NULL;
2719                         return 1;
2720                 }
2721                 errinf(qstate, "CNAME in DS response was not secure.");
2722                 errinf(qstate, reason);
2723                 goto return_bogus;
2724         } else {
2725                 verbose(VERB_QUERY, "Encountered an unhandled type of "
2726                         "DS response, thus bogus.");
2727                 errinf(qstate, "no DS and");
2728                 if(FLAGS_GET_RCODE(msg->rep->flags) != LDNS_RCODE_NOERROR) {
2729                         char rc[16];
2730                         rc[0]=0;
2731                         (void)sldns_wire2str_rcode_buf((int)FLAGS_GET_RCODE(
2732                                 msg->rep->flags), rc, sizeof(rc));
2733                         errinf(qstate, rc);
2734                 } else  errinf(qstate, val_classification_to_string(subtype));
2735                 errinf(qstate, "message fails to prove that");
2736                 goto return_bogus;
2737         }
2738 return_bogus:
2739         *ke = key_entry_create_bad(qstate->region, qinfo->qname,
2740                 qinfo->qname_len, qinfo->qclass, 
2741                 BOGUS_KEY_TTL, *qstate->env->now);
2742         return (*ke) != NULL;
2743 }
2744
2745 /**
2746  * Process DS response. Called from inform_supers.
2747  * Because it is in inform_supers, the mesh itself is busy doing callbacks
2748  * for a state that is to be deleted soon; don't touch the mesh; instead
2749  * set a state in the super, as the super will be reactivated soon.
2750  * Perform processing to determine what state to set in the super.
2751  *
2752  * @param qstate: query state that is validating and asked for a DS.
2753  * @param vq: validator query state
2754  * @param id: module id.
2755  * @param rcode: rcode result value.
2756  * @param msg: result message (if rcode is OK).
2757  * @param qinfo: from the sub query state, query info.
2758  * @param origin: the origin of msg.
2759  */
2760 static void
2761 process_ds_response(struct module_qstate* qstate, struct val_qstate* vq,
2762         int id, int rcode, struct dns_msg* msg, struct query_info* qinfo,
2763         struct sock_list* origin)
2764 {
2765         struct key_entry_key* dske = NULL;
2766         uint8_t* olds = vq->empty_DS_name;
2767         vq->empty_DS_name = NULL;
2768         if(!ds_response_to_ke(qstate, vq, id, rcode, msg, qinfo, &dske)) {
2769                         log_err("malloc failure in process_ds_response");
2770                         vq->key_entry = NULL; /* make it error */
2771                         vq->state = VAL_VALIDATE_STATE;
2772                         return;
2773         }
2774         if(dske == NULL) {
2775                 vq->empty_DS_name = regional_alloc_init(qstate->region,
2776                         qinfo->qname, qinfo->qname_len);
2777                 if(!vq->empty_DS_name) {
2778                         log_err("malloc failure in empty_DS_name");
2779                         vq->key_entry = NULL; /* make it error */
2780                         vq->state = VAL_VALIDATE_STATE;
2781                         return;
2782                 }
2783                 vq->empty_DS_len = qinfo->qname_len;
2784                 vq->chain_blacklist = NULL;
2785                 /* ds response indicated that we aren't on a delegation point.
2786                  * Keep the forState.state on FINDKEY. */
2787         } else if(key_entry_isgood(dske)) {
2788                 vq->ds_rrset = key_entry_get_rrset(dske, qstate->region);
2789                 if(!vq->ds_rrset) {
2790                         log_err("malloc failure in process DS");
2791                         vq->key_entry = NULL; /* make it error */
2792                         vq->state = VAL_VALIDATE_STATE;
2793                         return;
2794                 }
2795                 vq->chain_blacklist = NULL; /* fresh blacklist for next part*/
2796                 /* Keep the forState.state on FINDKEY. */
2797         } else if(key_entry_isbad(dske) 
2798                 && vq->restart_count < VAL_MAX_RESTART_COUNT) {
2799                 vq->empty_DS_name = olds;
2800                 val_blacklist(&vq->chain_blacklist, qstate->region, origin, 1);
2801                 qstate->errinf = NULL;
2802                 vq->restart_count++;
2803         } else {
2804                 if(key_entry_isbad(dske)) {
2805                         errinf_origin(qstate, origin);
2806                         errinf_dname(qstate, "for DS", qinfo->qname);
2807                 }
2808                 /* NOTE: the reason for the DS to be not good (that is, 
2809                  * either bad or null) should have been logged by 
2810                  * dsResponseToKE. */
2811                 vq->key_entry = dske;
2812                 /* The FINDKEY phase has ended, so move on. */
2813                 vq->state = VAL_VALIDATE_STATE;
2814         }
2815 }
2816
2817 /**
2818  * Process DNSKEY response. Called from inform_supers.
2819  * Sets the key entry in the state.
2820  * Because it is in inform_supers, the mesh itself is busy doing callbacks
2821  * for a state that is to be deleted soon; don't touch the mesh; instead
2822  * set a state in the super, as the super will be reactivated soon.
2823  * Perform processing to determine what state to set in the super.
2824  *
2825  * @param qstate: query state that is validating and asked for a DNSKEY.
2826  * @param vq: validator query state
2827  * @param id: module id.
2828  * @param rcode: rcode result value.
2829  * @param msg: result message (if rcode is OK).
2830  * @param qinfo: from the sub query state, query info.
2831  * @param origin: the origin of msg.
2832  */
2833 static void
2834 process_dnskey_response(struct module_qstate* qstate, struct val_qstate* vq,
2835         int id, int rcode, struct dns_msg* msg, struct query_info* qinfo,
2836         struct sock_list* origin)
2837 {
2838         struct val_env* ve = (struct val_env*)qstate->env->modinfo[id];
2839         struct key_entry_key* old = vq->key_entry;
2840         struct ub_packed_rrset_key* dnskey = NULL;
2841         int downprot;
2842         char* reason = NULL;
2843
2844         if(rcode == LDNS_RCODE_NOERROR)
2845                 dnskey = reply_find_answer_rrset(qinfo, msg->rep);
2846
2847         if(dnskey == NULL) {
2848                 /* bad response */
2849                 verbose(VERB_DETAIL, "Missing DNSKEY RRset in response to "
2850                         "DNSKEY query.");
2851                 if(vq->restart_count < VAL_MAX_RESTART_COUNT) {
2852                         val_blacklist(&vq->chain_blacklist, qstate->region,
2853                                 origin, 1);
2854                         qstate->errinf = NULL;
2855                         vq->restart_count++;
2856                         return;
2857                 }
2858                 vq->key_entry = key_entry_create_bad(qstate->region, 
2859                         qinfo->qname, qinfo->qname_len, qinfo->qclass,
2860                         BOGUS_KEY_TTL, *qstate->env->now);
2861                 if(!vq->key_entry) {
2862                         log_err("alloc failure in missing dnskey response");
2863                         /* key_entry is NULL for failure in Validate */
2864                 }
2865                 errinf(qstate, "No DNSKEY record");
2866                 errinf_origin(qstate, origin);
2867                 errinf_dname(qstate, "for key", qinfo->qname);
2868                 vq->state = VAL_VALIDATE_STATE;
2869                 return;
2870         }
2871         if(!vq->ds_rrset) {
2872                 log_err("internal error: no DS rrset for new DNSKEY response");
2873                 vq->key_entry = NULL;
2874                 vq->state = VAL_VALIDATE_STATE;
2875                 return;
2876         }
2877         downprot = qstate->env->cfg->harden_algo_downgrade;
2878         vq->key_entry = val_verify_new_DNSKEYs(qstate->region, qstate->env,
2879                 ve, dnskey, vq->ds_rrset, downprot, &reason);
2880
2881         if(!vq->key_entry) {
2882                 log_err("out of memory in verify new DNSKEYs");
2883                 vq->state = VAL_VALIDATE_STATE;
2884                 return;
2885         }
2886         /* If the key entry isBad or isNull, then we can move on to the next
2887          * state. */
2888         if(!key_entry_isgood(vq->key_entry)) {
2889                 if(key_entry_isbad(vq->key_entry)) {
2890                         if(vq->restart_count < VAL_MAX_RESTART_COUNT) {
2891                                 val_blacklist(&vq->chain_blacklist, 
2892                                         qstate->region, origin, 1);
2893                                 qstate->errinf = NULL;
2894                                 vq->restart_count++;
2895                                 vq->key_entry = old;
2896                                 return;
2897                         }
2898                         verbose(VERB_DETAIL, "Did not match a DS to a DNSKEY, "
2899                                 "thus bogus.");
2900                         errinf(qstate, reason);
2901                         errinf_origin(qstate, origin);
2902                         errinf_dname(qstate, "for key", qinfo->qname);
2903                 }
2904                 vq->chain_blacklist = NULL;
2905                 vq->state = VAL_VALIDATE_STATE;
2906                 return;
2907         }
2908         vq->chain_blacklist = NULL;
2909         qstate->errinf = NULL;
2910
2911         /* The DNSKEY validated, so cache it as a trusted key rrset. */
2912         key_cache_insert(ve->kcache, vq->key_entry, qstate);
2913
2914         /* If good, we stay in the FINDKEY state. */
2915         log_query_info(VERB_DETAIL, "validated DNSKEY", qinfo);
2916 }
2917
2918 /**
2919  * Process prime response
2920  * Sets the key entry in the state.
2921  *
2922  * @param qstate: query state that is validating and primed a trust anchor.
2923  * @param vq: validator query state
2924  * @param id: module id.
2925  * @param rcode: rcode result value.
2926  * @param msg: result message (if rcode is OK).
2927  * @param origin: the origin of msg.
2928  */
2929 static void
2930 process_prime_response(struct module_qstate* qstate, struct val_qstate* vq,
2931         int id, int rcode, struct dns_msg* msg, struct sock_list* origin)
2932 {
2933         struct val_env* ve = (struct val_env*)qstate->env->modinfo[id];
2934         struct ub_packed_rrset_key* dnskey_rrset = NULL;
2935         struct trust_anchor* ta = anchor_find(qstate->env->anchors, 
2936                 vq->trust_anchor_name, vq->trust_anchor_labs,
2937                 vq->trust_anchor_len, vq->qchase.qclass);
2938         if(!ta) {
2939                 /* trust anchor revoked, restart with less anchors */
2940                 vq->state = VAL_INIT_STATE;
2941                 if(!vq->trust_anchor_name)
2942                         vq->state = VAL_VALIDATE_STATE; /* break a loop */
2943                 vq->trust_anchor_name = NULL;
2944                 return;
2945         }
2946         /* Fetch and validate the keyEntry that corresponds to the 
2947          * current trust anchor. */
2948         if(rcode == LDNS_RCODE_NOERROR) {
2949                 dnskey_rrset = reply_find_rrset_section_an(msg->rep,
2950                         ta->name, ta->namelen, LDNS_RR_TYPE_DNSKEY,
2951                         ta->dclass);
2952         }
2953
2954         if(ta->autr) {
2955                 if(!autr_process_prime(qstate->env, ve, ta, dnskey_rrset)) {
2956                         /* trust anchor revoked, restart with less anchors */
2957                         vq->state = VAL_INIT_STATE;
2958                         vq->trust_anchor_name = NULL;
2959                         return;
2960                 }
2961         }
2962         vq->key_entry = primeResponseToKE(dnskey_rrset, ta, qstate, id);
2963         lock_basic_unlock(&ta->lock);
2964         if(vq->key_entry) {
2965                 if(key_entry_isbad(vq->key_entry) 
2966                         && vq->restart_count < VAL_MAX_RESTART_COUNT) {
2967                         val_blacklist(&vq->chain_blacklist, qstate->region, 
2968                                 origin, 1);
2969                         qstate->errinf = NULL;
2970                         vq->restart_count++;
2971                         vq->key_entry = NULL;
2972                         vq->state = VAL_INIT_STATE;
2973                         return;
2974                 } 
2975                 vq->chain_blacklist = NULL;
2976                 errinf_origin(qstate, origin);
2977                 errinf_dname(qstate, "for trust anchor", ta->name);
2978                 /* store the freshly primed entry in the cache */
2979                 key_cache_insert(ve->kcache, vq->key_entry, qstate);
2980         }
2981
2982         /* If the result of the prime is a null key, skip the FINDKEY state.*/
2983         if(!vq->key_entry || key_entry_isnull(vq->key_entry) ||
2984                 key_entry_isbad(vq->key_entry)) {
2985                 vq->state = VAL_VALIDATE_STATE;
2986         }
2987         /* the qstate will be reactivated after inform_super is done */
2988 }
2989
2990 /**
2991  * Process DLV response. Called from inform_supers.
2992  * Because it is in inform_supers, the mesh itself is busy doing callbacks
2993  * for a state that is to be deleted soon; don't touch the mesh; instead
2994  * set a state in the super, as the super will be reactivated soon.
2995  * Perform processing to determine what state to set in the super.
2996  *
2997  * @param qstate: query state that is validating and asked for a DLV.
2998  * @param vq: validator query state
2999  * @param id: module id.
3000  * @param rcode: rcode result value.
3001  * @param msg: result message (if rcode is OK).
3002  * @param qinfo: from the sub query state, query info.
3003  */
3004 static void
3005 process_dlv_response(struct module_qstate* qstate, struct val_qstate* vq,
3006         int id, int rcode, struct dns_msg* msg, struct query_info* qinfo)
3007 {
3008         struct val_env* ve = (struct val_env*)qstate->env->modinfo[id];
3009
3010         verbose(VERB_ALGO, "process dlv response to super");
3011         if(rcode != LDNS_RCODE_NOERROR) {
3012                 /* lookup failed, set in vq to give up */
3013                 vq->dlv_status = dlv_error;
3014                 verbose(VERB_ALGO, "response is error");
3015                 return;
3016         }
3017         if(msg->rep->security != sec_status_secure) {
3018                 vq->dlv_status = dlv_error;
3019                 verbose(VERB_ALGO, "response is not secure, %s",
3020                         sec_status_to_string(msg->rep->security));
3021                 return;
3022         }
3023         /* was the lookup a success? validated DLV? */
3024         if(FLAGS_GET_RCODE(msg->rep->flags) == LDNS_RCODE_NOERROR &&
3025                 msg->rep->an_numrrsets == 1 &&
3026                 msg->rep->security == sec_status_secure &&
3027                 ntohs(msg->rep->rrsets[0]->rk.type) == LDNS_RR_TYPE_DLV &&
3028                 ntohs(msg->rep->rrsets[0]->rk.rrset_class) == qinfo->qclass &&
3029                 query_dname_compare(msg->rep->rrsets[0]->rk.dname, 
3030                         vq->dlv_lookup_name) == 0) {
3031                 /* yay! it is just like a DS */
3032                 vq->ds_rrset = (struct ub_packed_rrset_key*)
3033                         regional_alloc_init(qstate->region,
3034                         msg->rep->rrsets[0], sizeof(*vq->ds_rrset));
3035                 if(!vq->ds_rrset) {
3036                         log_err("out of memory in process_dlv");
3037                         return;
3038                 }
3039                 vq->ds_rrset->entry.key = vq->ds_rrset;
3040                 vq->ds_rrset->rk.dname = (uint8_t*)regional_alloc_init(
3041                         qstate->region, vq->ds_rrset->rk.dname, 
3042                         vq->ds_rrset->rk.dname_len);
3043                 if(!vq->ds_rrset->rk.dname) {
3044                         log_err("out of memory in process_dlv");
3045                         vq->dlv_status = dlv_error;
3046                         return;
3047                 }
3048                 vq->ds_rrset->entry.data = regional_alloc_init(qstate->region,
3049                         vq->ds_rrset->entry.data, 
3050                         packed_rrset_sizeof(vq->ds_rrset->entry.data));
3051                 if(!vq->ds_rrset->entry.data) {
3052                         log_err("out of memory in process_dlv");
3053                         vq->dlv_status = dlv_error;
3054                         return;
3055                 }
3056                 packed_rrset_ptr_fixup(vq->ds_rrset->entry.data);
3057                 /* make vq do a DNSKEY query next up */
3058                 vq->dlv_status = dlv_success;
3059                 return;
3060         }
3061         /* store NSECs into negative cache */
3062         val_neg_addreply(ve->neg_cache, msg->rep);
3063
3064         /* was the lookup a failure? 
3065          *   if we have to go up into the DLV for a higher DLV anchor
3066          *   then set this in the vq, so it can make queries when activated.
3067          * See if the NSECs indicate that we should look for higher DLV
3068          * or, that there is no DLV securely */
3069         if(!val_nsec_check_dlv(qinfo, msg->rep, &vq->dlv_lookup_name, 
3070                 &vq->dlv_lookup_name_len)) {
3071                 vq->dlv_status = dlv_error;
3072                 verbose(VERB_ALGO, "nsec error");
3073                 return;
3074         }
3075         if(!dname_subdomain_c(vq->dlv_lookup_name, 
3076                 qstate->env->anchors->dlv_anchor->name)) {
3077                 vq->dlv_status = dlv_there_is_no_dlv;
3078                 return;
3079         }
3080         vq->dlv_status = dlv_ask_higher;
3081 }
3082
3083 /* 
3084  * inform validator super.
3085  * 
3086  * @param qstate: query state that finished.
3087  * @param id: module id.
3088  * @param super: the qstate to inform.
3089  */
3090 void
3091 val_inform_super(struct module_qstate* qstate, int id,
3092         struct module_qstate* super)
3093 {
3094         struct val_qstate* vq = (struct val_qstate*)super->minfo[id];
3095         log_query_info(VERB_ALGO, "validator: inform_super, sub is",
3096                 &qstate->qinfo);
3097         log_query_info(VERB_ALGO, "super is", &super->qinfo);
3098         if(!vq) {
3099                 verbose(VERB_ALGO, "super: has no validator state");
3100                 return;
3101         }
3102         if(vq->wait_prime_ta) {
3103                 vq->wait_prime_ta = 0;
3104                 process_prime_response(super, vq, id, qstate->return_rcode,
3105                         qstate->return_msg, qstate->reply_origin);
3106                 return;
3107         }
3108         if(qstate->qinfo.qtype == LDNS_RR_TYPE_DS) {
3109                 process_ds_response(super, vq, id, qstate->return_rcode,
3110                         qstate->return_msg, &qstate->qinfo, 
3111                         qstate->reply_origin);
3112                 return;
3113         } else if(qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY) {
3114                 process_dnskey_response(super, vq, id, qstate->return_rcode,
3115                         qstate->return_msg, &qstate->qinfo,
3116                         qstate->reply_origin);
3117                 return;
3118         } else if(qstate->qinfo.qtype == LDNS_RR_TYPE_DLV) {
3119                 process_dlv_response(super, vq, id, qstate->return_rcode,
3120                         qstate->return_msg, &qstate->qinfo);
3121                 return;
3122         }
3123         log_err("internal error in validator: no inform_supers possible");
3124 }
3125
3126 void
3127 val_clear(struct module_qstate* qstate, int id)
3128 {
3129         if(!qstate)
3130                 return;
3131         /* everything is allocated in the region, so assign NULL */
3132         qstate->minfo[id] = NULL;
3133 }
3134
3135 size_t 
3136 val_get_mem(struct module_env* env, int id)
3137 {
3138         struct val_env* ve = (struct val_env*)env->modinfo[id];
3139         if(!ve)
3140                 return 0;
3141         return sizeof(*ve) + key_cache_get_mem(ve->kcache) + 
3142                 val_neg_get_mem(ve->neg_cache) +
3143                 sizeof(size_t)*2*ve->nsec3_keyiter_count;
3144 }
3145
3146 /**
3147  * The validator function block 
3148  */
3149 static struct module_func_block val_block = {
3150         "validator",
3151         &val_init, &val_deinit, &val_operate, &val_inform_super, &val_clear,
3152         &val_get_mem
3153 };
3154
3155 struct module_func_block* 
3156 val_get_funcblock(void)
3157 {
3158         return &val_block;
3159 }
3160
3161 const char* 
3162 val_state_to_string(enum val_state state)
3163 {
3164         switch(state) {
3165                 case VAL_INIT_STATE: return "VAL_INIT_STATE";
3166                 case VAL_FINDKEY_STATE: return "VAL_FINDKEY_STATE";
3167                 case VAL_VALIDATE_STATE: return "VAL_VALIDATE_STATE";
3168                 case VAL_FINISHED_STATE: return "VAL_FINISHED_STATE";
3169                 case VAL_DLVLOOKUP_STATE: return "VAL_DLVLOOKUP_STATE";
3170         }
3171         return "UNKNOWN VALIDATOR STATE";
3172 }
3173