domain 0.12.0

A DNS library for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! A collection of utility functions.

use super::context::{Config, Error, ValidationState};
use super::group::ValidatedGroup;
use super::nsec::{nsec3_for_not_exists_no_ce, nsec_for_not_exists};
use super::nsec::{Nsec3Cache, Nsec3NXStateNoCE, NsecNXState};
use crate::base::iana::{Class, ExtendedErrorCode};
use crate::base::name::Label;
use crate::base::opt::ExtendedError;
use crate::base::rdata::ComposeRecordData;
use crate::base::wire::Composer;
use crate::base::{
    Message, MessageBuilder, Name, NameBuilder, ParsedName, Record,
    RecordSectionBuilder, Rtype, StaticCompressor, ToName, Ttl,
};
use crate::dep::octseq::{OctetsFrom, OctetsInto};
use crate::rdata::dnssec::Timestamp;
use crate::rdata::{AllRecordData, Dname, Rrsig};
use bytes::Bytes;
use std::cmp::min;
use std::vec::Vec;

//----------- Helper functions -----------------------------------------------

/// Go through the answer section and resolve qname as much as possible
/// using the available CNAME and DNAME records. Return the final name
/// and validation state and well as an optional extended error.
pub async fn do_cname_dname(
    qname: Name<Bytes>,
    qclass: Class,
    qtype: Rtype,
    answers: &mut [ValidatedGroup],
    authorities: &mut [ValidatedGroup],
    nsec3_cache: &Nsec3Cache,
    config: &Config,
) -> (Name<Bytes>, ValidationState, Option<ExtendedError<Vec<u8>>>) {
    let mut name = qname;
    let mut count = 0;
    let mut maybe_secure = ValidationState::Secure;
    'name_loop: loop {
        for g in answers.iter() {
            if g.class() != qclass {
                continue;
            }
            let rtype = g.rtype();
            if rtype == Rtype::CNAME && rtype == qtype {
                // A CNAME requested, do not process them here.
                continue;
            }

            let rr_set = g.rr_set();
            if rr_set.len() != 1 {
                // totest, RRset with more than one CNAME or DNAME.
                // just skip this RRset. This is not an error because we
                // don't know the type of the record yet.
                continue;
            }

            if let AllRecordData::Cname(cname) = rr_set[0].data() {
                if g.owner() != name {
                    continue;
                }
                if let Some(ce) = g.closest_encloser() {
                    let (check, state, ede) = check_not_exists_for_wildcard(
                        &name,
                        authorities,
                        &g.signer_name(),
                        &ce,
                        nsec3_cache,
                        config,
                    )
                    .await;
                    if check {
                        maybe_secure = map_maybe_secure(state, maybe_secure);
                    // Just continue.
                    } else {
                        // totest, CNAME from wildcard with bad non-existance
                        // proof
                        // Report failure
                        return (name, ValidationState::Bogus, ede);
                    }
                }
                name = cname.cname().to_name();
                maybe_secure = map_maybe_secure(g.state(), maybe_secure);
                count += 1;
                if count > config.max_cname_dname() {
                    let ede = make_ede(
                        ExtendedErrorCode::DNSSEC_BOGUS,
                        "too many DNAME/CNAME records in sequence",
                    );
                    return (name, ValidationState::Bogus, ede);
                }
                continue 'name_loop;
            }

            if let AllRecordData::Dname(dname) = rr_set[0].data() {
                let owner = g.owner();
                if !name.ends_with(&owner) {
                    // This DNAME record is not suitable for the current name.
                    continue;
                }
                if owner == name {
                    // It cannot be an exact match.
                    continue;
                }

                if let Some(_ce) = g.closest_encloser() {
                    // totest, DNAME from wildcard
                    // wildcard DNAMEs are undefined.
                    let ede = make_ede(
                        ExtendedErrorCode::DNSSEC_BOGUS,
                        "DNAME from wildcard",
                    );
                    return (owner, ValidationState::Bogus, ede);
                }

                name = match map_dname(&owner, dname, &name) {
                    Ok(name) => name,
                    Err(_) => {
                        let ede = make_ede(
                            ExtendedErrorCode::DNSSEC_BOGUS,
                            "Failed to expand DNAME",
                        );
                        return (owner, ValidationState::Bogus, ede);
                    }
                };
                maybe_secure = map_maybe_secure(g.state(), maybe_secure);
                count += 1;
                if count > config.max_cname_dname() {
                    // totest, loop with too many DNAME records
                    let ede = make_ede(
                        ExtendedErrorCode::DNSSEC_BOGUS,
                        "too many DNAME/CNAME records in sequence",
                    );
                    return (owner, ValidationState::Bogus, ede);
                }
                continue 'name_loop;
            }

            // Just continue, not a CNAME or DNAME.
        }

        // No match CNAME or DNAME found.
        break;
    }

    (name, maybe_secure, None)
}

/// Apply a DNAME to a target name.
pub fn map_dname(
    target: &Name<Bytes>,
    dname: &Dname<ParsedName<Bytes>>,
    name: &Name<Bytes>,
) -> Result<Name<Bytes>, Error> {
    let mut tmp_name = name.clone();
    let mut new_name = NameBuilder::new_bytes();
    let target_labels = target.label_count();
    while tmp_name.label_count() > target_labels {
        new_name.append_label(tmp_name.first().as_slice())?;
        tmp_name = tmp_name.parent().expect("should not fail");
    }
    let name = new_name.append_origin(dname.dname())?;
    Ok(name)
}

/// Compute the TTL for a signature. Take the original_ttl and the
/// remaining signature lifetime into account.
pub fn ttl_for_sig(
    sig: &Record<Name<Bytes>, Rrsig<Bytes, Name<Bytes>>>,
) -> Ttl {
    let ttl = sig.ttl();
    let orig_ttl = sig.data().original_ttl();
    let ttl = min(ttl, orig_ttl);

    let until_expired =
        sig.data().expiration().into_int() - Timestamp::now().into_int();
    let expire_ttl = Ttl::from_secs(until_expired);
    min(ttl, expire_ttl)
}

/// Try to find an RRset that answers qname, qclass, qtype. Return None
/// is nothing was found. Otherwise, return the validation state,
/// the signer name, optionally the closest encloser if the answer came
/// from an expanded wildcard and optionally an extended error.
#[allow(clippy::type_complexity)]
pub fn get_answer_state(
    qname: &Name<Bytes>,
    qclass: Class,
    qtype: Rtype,
    groups: &mut [ValidatedGroup],
) -> Option<(
    ValidationState,
    Name<Bytes>,
    Option<Name<Bytes>>,
    Option<ExtendedError<Vec<u8>>>,
)> {
    for g in groups.iter() {
        if g.class() != qclass {
            continue;
        }
        if g.rtype() != qtype {
            continue;
        }
        if g.owner() != qname {
            continue;
        }
        return Some((
            g.state(),
            g.signer_name(),
            g.closest_encloser(),
            g.ede(),
        ));
    }
    None
}

/// Try to find a SOA record that is a parent of  qname and qclass. Return None
/// is nothing was found. Otherwise, return the validation state,
/// and the signer name. Optionally return an extended error.
#[allow(clippy::type_complexity)]
pub fn get_soa_state(
    qname: &Name<Bytes>,
    qclass: Class,
    groups: &mut [ValidatedGroup],
) -> (
    Option<(ValidationState, Name<Bytes>)>,
    Option<ExtendedError<Vec<u8>>>,
) {
    let mut ede = None;
    for g in groups.iter() {
        if g.rtype() != Rtype::SOA {
            continue;
        }
        if g.class() != qclass {
            // totest, SOA with wrong class
            if ede.is_none() {
                ede = make_ede(
                    ExtendedErrorCode::DNSSEC_BOGUS,
                    "SOA with wrong class",
                );
            }
            continue;
        }
        if !qname.ends_with(&g.owner()) {
            // totest, SOA with wrong name
            ede = make_ede(
                ExtendedErrorCode::DNSSEC_BOGUS,
                "SOA with wrong name",
            );
            continue;
        }
        return (Some((g.state(), g.signer_name())), g.ede());
    }
    (None, ede)
}

/// Keep track if the current validation result is still secure or if it
/// has been downgraded to insecure or indeterminate.
pub fn map_maybe_secure(
    result: ValidationState,
    maybe_secure: ValidationState,
) -> ValidationState {
    if let ValidationState::Secure = result {
        maybe_secure
    } else {
        result
    }
}

/// For a target name and a closest encloser get the name that is just one
/// label longer than the closest encloser but still a suffix of the
/// target name.
fn get_child_of_ce(target: &Name<Bytes>, ce: &Name<Bytes>) -> Name<Bytes> {
    let ce_label_count = ce.label_count();
    let mut name = target.clone();
    while name.label_count() > ce_label_count + 1 {
        name = name.parent().expect("should not fail");
    }
    if name.label_count() == ce_label_count + 1 {
        // name is the child we are looking for.
        return name;
    }

    // Something weird.
    panic!("Get child of closest encloser(ce), maybe target is not a decendent of ce?");
}

/// Check that target name does not exist. This typically happens if there
/// is an answer but the answer is an expanded wildcard. In that case the
/// closest encloser is know. What is needed is to check that target name does
/// not exist using either NSEC or NSEC3 records.
pub async fn check_not_exists_for_wildcard(
    target: &Name<Bytes>,
    group: &mut [ValidatedGroup],
    signer_name: &Name<Bytes>,
    closest_encloser: &Name<Bytes>,
    nsec3_cache: &Nsec3Cache,
    config: &Config,
) -> (bool, ValidationState, Option<ExtendedError<Vec<u8>>>) {
    let (state, ede) = nsec_for_not_exists(target, group, signer_name);
    match state {
        NsecNXState::Exists => {
            // The name actually exists.
            return (false, ValidationState::Bogus, ede);
        }
        NsecNXState::DoesNotExist(ce) => {
            // Make sure that the wildcard that was used for the
            // answer matches the closest encloser we got from the NSEC.
            if *closest_encloser == ce {
                // It checks out, we have a secure wildcard.
                return (true, ValidationState::Secure, None);
            }

            // totest, expanded wildcard does not match NSEC CE
            // Failure.
            let ede = make_ede(
                ExtendedErrorCode::DNSSEC_BOGUS,
                "wildcard does not match NSEC dereived closest encloser",
            );
            return (false, ValidationState::Bogus, ede);
        }
        NsecNXState::Nothing => (), // Continue with NSEC3
    }

    let child_of_ce = get_child_of_ce(target, closest_encloser);

    let (state, ede) = nsec3_for_not_exists_no_ce(
        &child_of_ce,
        group,
        signer_name,
        nsec3_cache,
        config,
    )
    .await;
    match state {
        Nsec3NXStateNoCE::DoesNotExist => {
            // It checks out, we have a secure wildcard.
            return (true, ValidationState::Secure, None);
        }
        Nsec3NXStateNoCE::DoesNotExistInsecure => {
            // Non-existance proof is insecure.
            return (true, ValidationState::Insecure, None);
        }
        Nsec3NXStateNoCE::Bogus => {
            return (false, ValidationState::Bogus, ede)
        }
        Nsec3NXStateNoCE::Nothing => (), // Continue.
    }

    // Failure, no suitable NSEC or NSEC3 record found.
    (false, ValidationState::Bogus, ede)
}

/// Prepend the wildcard label to a closest encloser.
pub fn star_closest_encloser(ce: &Name<Bytes>) -> Result<Name<Bytes>, Error> {
    let mut star_name = NameBuilder::new_bytes();
    star_name.append_label(Label::wildcard().as_ref())?;
    let star_name = star_name.append_origin(&ce)?;
    Ok(star_name)
}

/// Helper function to create an EDNS(0) extended error option.
pub fn make_ede(
    code: ExtendedErrorCode,
    reason: &str,
) -> Option<ExtendedError<Vec<u8>>> {
    // Assume that the only reason this case fail is a string that
    // is way too long. Just return None.
    ExtendedError::new_with_str(code, reason).ok()
}

/// Create a new DNS message based on the original message that fixes any
/// TTL issues and leaves out duplicate records.
pub fn rebuild_msg<OutOcts>(
    msg: &Message<Bytes>,
    answers: &[ValidatedGroup],
    authorities: &[ValidatedGroup],
) -> Result<Message<OutOcts>, Error>
where
    OutOcts: AsRef<[u8]> + OctetsFrom<Vec<u8>>,
{
    let mut target =
        MessageBuilder::from_target(StaticCompressor::new(Vec::new()))
            .expect("Vec is expected to have enough space");

    let source = msg;

    *target.header_mut() = msg.header();

    let source = source.question();
    let mut target = target.question();
    for rr in source {
        target.push(rr?).expect("should not fail");
    }
    let source = source.answer()?;
    let mut target = target.answer();
    add_list_to_section(answers, &mut target);

    let source = source.next_section()?.expect("section should be present");
    let mut target = target.authority();
    add_list_to_section(authorities, &mut target);

    let source = source.next_section()?.expect("section should be present");
    let mut target = target.additional();
    for rr in source {
        let rr = rr?;
        let rr = rr
            .into_record::<AllRecordData<_, ParsedName<_>>>()?
            .expect("record expected");
        target.push(rr).expect("should not fail");
    }

    let result = target.as_builder().clone();
    let msg = Message::<OutOcts>::from_octets(
        match result.finish().into_target().try_octets_into() {
            Ok(o) => o,
            Err(_) => {
                return Err(Error::OctetsConversion);
            }
        },
    )
    .expect("Message should be able to parse output from MessageBuilder");
    Ok(msg)
}

/// Add a list of `ValidatedGroup` objects to a section of a new DNS message.
fn add_list_to_section<Section, Target>(
    list: &[ValidatedGroup],
    section: &mut Section,
) where
    Target: Composer,
    Section: RecordSectionBuilder<Target>,
{
    for vg in list {
        let adjust_ttl = vg.adjust_ttl();
        for rr in vg.rr_set() {
            add_rr_to_section(&rr, adjust_ttl, section);
        }
        for rr in vg.sig_set() {
            add_rr_to_section(&rr, adjust_ttl, section);
        }
        for rr in vg.extra_set() {
            add_rr_to_section(&rr, adjust_ttl, section);
        }
    }
}

/// Add a record to a section of a new DNS message. If needed, adjust the TTL.
fn add_rr_to_section<RecData, Section, Target>(
    rr: &Record<Name<Bytes>, RecData>,
    adjust_ttl: Option<Ttl>,
    section: &mut Section,
) where
    RecData: Clone + ComposeRecordData,
    Target: Composer,
    Section: RecordSectionBuilder<Target>,
{
    if let Some(ttl) = adjust_ttl {
        let mut rr = rr.clone();
        rr.set_ttl(min(rr.ttl(), ttl));
        section.push(rr).expect("should not fail");
    } else {
        section.push(rr).expect("should not fail");
    }
}