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
/*
 * Copyright (C) 2015 Benjamin Fry <benjaminfry@me.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! negative cache proof for non-existence

use ::serialize::binary::*;
use ::error::*;
use ::rr::{Name, RecordType};
use ::rr::rdata::nsec3;

/// [RFC 4034, DNSSEC Resource Records, March 2005](https://tools.ietf.org/html/rfc4034#section-4)
///
/// ```text
/// 4.1.  NSEC RDATA Wire Format
///
///    The RDATA of the NSEC RR is as shown below:
///
///                         1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
///     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
///    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///    /                      Next Domain Name                         /
///    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///    /                       Type Bit Maps                           /
///    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///
/// 4.1.3.  Inclusion of Wildcard Names in NSEC RDATA
///
///    If a wildcard owner name appears in a zone, the wildcard label ("*")
///    is treated as a literal symbol and is treated the same as any other
///    owner name for the purposes of generating NSEC RRs.  Wildcard owner
///    names appear in the Next Domain Name field without any wildcard
///    expansion.  [RFC4035] describes the impact of wildcards on
///    authenticated denial of existence.
/// ```
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct NSEC { next_domain_name: Name, type_bit_maps: Vec<RecordType> }

impl NSEC {
  pub fn new(next_domain_name: Name, type_bit_maps: Vec<RecordType>) -> NSEC {
    NSEC { next_domain_name: next_domain_name, type_bit_maps: type_bit_maps }
  }

  /// [RFC 4034, DNSSEC Resource Records, March 2005](https://tools.ietf.org/html/rfc4034#section-4.1.1)
  ///
  /// ```text
  /// 4.1.1.  The Next Domain Name Field
  ///
  ///    The Next Domain field contains the next owner name (in the canonical
  ///    ordering of the zone) that has authoritative data or contains a
  ///    delegation point NS RRset; see Section 6.1 for an explanation of
  ///    canonical ordering.  The value of the Next Domain Name field in the
  ///    last NSEC record in the zone is the name of the zone apex (the owner
  ///    name of the zone's SOA RR).  This indicates that the owner name of
  ///    the NSEC RR is the last name in the canonical ordering of the zone.
  ///
  ///    A sender MUST NOT use DNS name compression on the Next Domain Name
  ///    field when transmitting an NSEC RR.
  ///
  ///    Owner names of RRsets for which the given zone is not authoritative
  ///    (such as glue records) MUST NOT be listed in the Next Domain Name
  ///    unless at least one authoritative RRset exists at the same owner
  ///    name.
  /// ```
  pub fn get_next_domain_name(&self) -> &Name { &self.next_domain_name }

  /// [RFC 4034, DNSSEC Resource Records, March 2005](https://tools.ietf.org/html/rfc4034#section-4.1.2)
  ///
  /// ```text
  /// 4.1.2.  The Type Bit Maps Field
  ///
  ///    The Type Bit Maps field identifies the RRset types that exist at the
  ///    NSEC RR's owner name.
  ///
  ///    A zone MUST NOT include an NSEC RR for any domain name that only
  ///    holds glue records.
  /// ```
  pub fn get_type_bit_maps(&self) -> &[RecordType] { &self.type_bit_maps }
}

pub fn read(decoder: &mut BinDecoder, rdata_length: u16) -> DecodeResult<NSEC> {
  let start_idx = decoder.index();

  let next_domain_name = try!(Name::read(decoder));

  let bit_map_len = rdata_length as usize - (decoder.index() - start_idx);
  let record_types = try!(nsec3::decode_type_bit_maps(decoder, bit_map_len));

  Ok(NSEC::new(next_domain_name, record_types))
}

pub fn emit(encoder: &mut BinEncoder, rdata: &NSEC) -> EncodeResult {
  let is_canonical_names = encoder.is_canonical_names();
  encoder.set_canonical_names(true);
  try!(rdata.get_next_domain_name().emit(encoder));
  try!(nsec3::encode_bit_maps(encoder, rdata.get_type_bit_maps()));
  encoder.set_canonical_names(is_canonical_names);

  Ok(())
}

#[test]
pub fn test() {
  use ::rr::RecordType;

  let rdata = NSEC::new(Name::new().label("www").label("example").label("com"),
                        vec![RecordType::A, RecordType::AAAA, RecordType::DS, RecordType::RRSIG]);

  let mut bytes = Vec::new();
  let mut encoder: BinEncoder = BinEncoder::new(&mut bytes);
  assert!(emit(&mut encoder, &rdata).is_ok());
  let bytes = encoder.as_bytes();

  println!("bytes: {:?}", bytes);

  let mut decoder: BinDecoder = BinDecoder::new(bytes);
  let read_rdata = read(&mut decoder, bytes.len() as u16);
  assert!(read_rdata.is_ok(), format!("error decoding: {:?}", read_rdata.unwrap_err()));
  assert_eq!(rdata, read_rdata.unwrap());
}