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
//! Record data from [RFC 5155].
//!
//! This RFC defines the NSEC3 and NSEC3PARAM resource records.
//!
//! [RFC 5155]: https://tools.ietf.org/html/rfc5155

use std::fmt;
use bytes::BufMut;
use ::bits::charstr::CharStr;
use ::bits::compose::{Compose, Compress, Compressor};
use ::bits::parse::{Parse, ParseAll, ParseAllError, Parser, ShortBuf};
use ::bits::rdata::RtypeRecordData;
use ::iana::{Nsec3HashAlg, Rtype};
use ::master::scan::{CharSource, Scan, Scanner, ScanError, SyntaxError};
use ::utils::base32;
use super::rfc4034::{RtypeBitmap, RtypeBitmapError};


//------------ Nsec3 ---------------------------------------------------------

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Nsec3 {
    hash_algorithm: Nsec3HashAlg,
    flags: u8,
    iterations: u16,
    salt: CharStr,
    next_owner: CharStr,
    types: RtypeBitmap,
}

impl Nsec3 {
    pub fn new(
        hash_algorithm: Nsec3HashAlg,
        flags: u8,
        iterations: u16,
        salt: CharStr,
        next_owner: CharStr,
        types: RtypeBitmap
    ) -> Self {
        Nsec3 { hash_algorithm, flags, iterations, salt, next_owner, types }
    }

    pub fn hash_algorithm(&self) -> Nsec3HashAlg {
        self.hash_algorithm
    }

    pub fn flags(&self) -> u8 { 
        self.flags
    }

    pub fn opt_out(&self) -> bool {
        self.flags & 0x01 != 0
    }

    pub fn iterations(&self) -> u16 {
        self.iterations
    }

    pub fn salt(&self) -> &CharStr {
        &self.salt
    }

    pub fn next_owner(&self) -> &CharStr {
        &self.next_owner
    }

    pub fn types(&self) -> &RtypeBitmap {
        &self.types
    }
}


//--- ParseAll, Compose, Compress

impl ParseAll for Nsec3 {
    type Err = ParseNsec3Error;

    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
        if len < 6 {
            return Err(ShortBuf.into())
        }
        let start = parser.pos();
        let hash_algorithm = Nsec3HashAlg::parse(parser)?;
        let flags = u8::parse(parser)?;
        let iterations = u16:: parse(parser)?;
        let salt = CharStr::parse(parser)?;
        let next_owner = CharStr::parse(parser)?;
        let len = if parser.pos() > start + len {
            return Err(ShortBuf.into())
        }
        else {
            len - (parser.pos() - start)
        };
        let types = RtypeBitmap::parse_all(parser, len)?;
        Ok(Self::new(
            hash_algorithm, flags, iterations, salt, next_owner, types
        ))
    }
}

impl Compose for Nsec3 {
    fn compose_len(&self) -> usize {
        4 + self.salt.compose_len() + self.next_owner.compose_len() +
            self.types.compose_len()
    }

    fn compose<B: BufMut>(&self, buf: &mut B) {
        self.hash_algorithm.compose(buf);
        self.flags.compose(buf);
        self.iterations.compose(buf);
        self.salt.compose(buf);
        self.next_owner.compose(buf);
        self.types.compose(buf);
    }
}

impl Compress for Nsec3 {
    fn compress(&self, buf: &mut Compressor) -> Result<(), ShortBuf> {
        buf.compose(self)
    }
}


//------------ Scan and Display ----------------------------------------------

impl Scan for Nsec3 {
    fn scan<C: CharSource>(
        scanner: &mut Scanner<C>
    ) -> Result<Self, ScanError> {
        Ok(Self::new(
            Nsec3HashAlg::scan(scanner)?,
            u8::scan(scanner)?,
            u16::scan(scanner)?,
            scan_salt(scanner)?,
            scan_hash(scanner)?,
            RtypeBitmap::scan(scanner)?
        ))
    }
}

fn scan_salt<C: CharSource>(
    scanner: &mut Scanner<C>
) -> Result<CharStr, ScanError> {
    if let Ok(()) = scanner.skip_literal("-") {    
        Ok(CharStr::empty())
    }
    else {
        CharStr::scan_hex(scanner)
    }
}

fn scan_hash<C: CharSource>(
    scanner: &mut Scanner<C>
) -> Result<CharStr, ScanError> {
    scanner.scan_base32hex_phrase(|bytes| {
        CharStr::from_bytes(bytes).map_err(SyntaxError::content)
    })
}

impl fmt::Display for Nsec3 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {} {} ", self.hash_algorithm, self.flags,
               self.iterations)?;
        self.salt.display_hex(f)?;
        base32::display_hex(&self.next_owner, f)?;
        write!(f, " {}", self.types)
    }
}


//--- RtypeRecordData

impl RtypeRecordData for Nsec3 {
    const RTYPE: Rtype = Rtype::Nsec3;
}


//------------ Nsec3param ----------------------------------------------------


#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Nsec3param {
    hash_algorithm: Nsec3HashAlg,
    flags: u8,
    iterations: u16,
    salt: CharStr,
}

impl Nsec3param {
    pub fn new(
        hash_algorithm: Nsec3HashAlg,
        flags: u8,
        iterations: u16,
        salt: CharStr
    ) -> Self {
        Nsec3param { hash_algorithm, flags, iterations, salt } 
    }

    pub fn hash_algorithm(&self) -> Nsec3HashAlg {
        self.hash_algorithm
    }

    pub fn flags(&self) -> u8 {
        self.flags
    }

    pub fn iterations(&self) -> u16 {
        self.iterations
    }

    pub fn salt(&self) -> &CharStr {
        &self.salt
    }
}


//--- Parse, ParseAll, Compose, Compres

impl Parse for Nsec3param {
    type Err = ShortBuf;

    fn parse(parser: &mut Parser) -> Result<Self, Self::Err> {
        Ok(Self::new(
            Nsec3HashAlg::parse(parser)?,
            u8::parse(parser)?,
            u16::parse(parser)?,
            CharStr::parse(parser)?,
        ))
    }

    fn skip(parser: &mut Parser) -> Result<(), Self::Err> {
        parser.advance(4)?;
        CharStr::skip(parser)
    }
}

impl ParseAll for Nsec3param {
    type Err = ParseAllError;

    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
        if len < 5 {
            return Err(ParseAllError::ShortField)
        }
        Ok(Self::new(
            Nsec3HashAlg::parse(parser)?,
            u8::parse(parser)?,
            u16::parse(parser)?,
            CharStr::parse_all(parser, len - 4)?,
        ))
    }
}

impl Compose for Nsec3param {
    fn compose_len(&self) -> usize {
        4 + self.salt.compose_len()
    }

    fn compose<B: BufMut>(&self, buf: &mut B) {
        self.hash_algorithm.compose(buf);
        self.flags.compose(buf);
        self.iterations.compose(buf);
        self.salt.compose(buf);
    }
}

impl Compress for Nsec3param {
    fn compress(&self, buf: &mut Compressor) -> Result<(), ShortBuf> {
        buf.compose(self)
    }
}


//--- Scan and Display

impl Scan for Nsec3param {
    fn scan<C: CharSource>(
        scanner: &mut Scanner<C>
    ) -> Result<Self, ScanError> {
        Ok(Self::new(
            Nsec3HashAlg::scan(scanner)?,
            u8::scan(scanner)?,
            u16::scan(scanner)?,
            scan_salt(scanner)?
        ))
    }
}

impl fmt::Display for Nsec3param {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {} {} ", self.hash_algorithm, self.flags,
               self.iterations)?;
        self.salt.display_hex(f)
    }
}


//--- RtypeRecordData

impl RtypeRecordData for Nsec3param {
    const RTYPE: Rtype = Rtype::Nsec3param;
}


//------------ ParseNsec3Error -----------------------------------------------

#[derive(Clone, Copy, Debug, Eq, Fail, PartialEq)]
pub enum ParseNsec3Error {
    #[fail(display="short field")]
    ShortField,

    #[fail(display="invalid record type bitmap")]
    BadRtypeBitmap,
}

impl From<ShortBuf> for ParseNsec3Error {
    fn from(_: ShortBuf) -> Self {
        ParseNsec3Error::ShortField
    }
}

impl From<RtypeBitmapError> for ParseNsec3Error {
    fn from(err: RtypeBitmapError) -> Self {
        match err {
            RtypeBitmapError::ShortBuf => ParseNsec3Error::ShortField,
            RtypeBitmapError::BadRtypeBitmap => ParseNsec3Error::BadRtypeBitmap
        }
    }
}


//------------ parsed --------------------------------------------------------

pub mod parsed {
    pub use super::{Nsec3, Nsec3param};
}