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
/*
 * 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.
 */
use std::convert::From;

use ::rr::dnssec::Algorithm;

#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
pub struct SupportedAlgorithms {
  // right now the number of Algorithms supported are fewer than 16..
  bit_map: u8
}

impl SupportedAlgorithms {
  pub fn new() -> Self {
    SupportedAlgorithms{ bit_map: 0 }
  }

  pub fn all() -> Self {
    SupportedAlgorithms{ bit_map: 0b00001111 }
  }

  fn pos(algorithm: Algorithm) -> u8 {
    // not using the values from the RFC's to keep the bit_map space condensed
    let bit_pos: u8 = match algorithm {
      Algorithm::RSASHA1 => 0,
      Algorithm::RSASHA256 => 1,
      Algorithm::RSASHA1NSEC3SHA1 => 2,
      Algorithm::RSASHA512 => 3,
      // ECDSAP256SHA256 => 4,
      // ECDSAP384SHA384 => 5,
    };

    assert!(bit_pos <= u8::max_value());
    1u8 << bit_pos
  }

  fn from_pos(pos: u8) -> Option<Algorithm> {
    // TODO: should build a code generator or possibly a macro for deriving these inversions
    match pos {
      0 => Some(Algorithm::RSASHA1),
      1 => Some(Algorithm::RSASHA256),
      2 => Some(Algorithm::RSASHA1NSEC3SHA1),
      3 => Some(Algorithm::RSASHA512),
      _ => None,
    }
  }

  pub fn set(&mut self, algorithm: Algorithm) {
    let bit_pos: u8 = Self::pos(algorithm);
    self.bit_map |= bit_pos;
  }

  pub fn has(&self, algorithm: Algorithm) -> bool {
    let bit_pos: u8 = Self::pos(algorithm);
    (bit_pos & self.bit_map) == bit_pos
  }

  pub fn iter(&self) -> SupportedAlgorithmsIter {
    SupportedAlgorithmsIter::new(self)
  }

  pub fn len(&self) -> u16 {
    // this is pretty much guaranteed to be less that u16::max_value()
    self.iter().count() as u16
  }
}

// impl<'a> SupportedAlgorithms {
//   pub fn iter(&self) -> SupportedAlgorithmsIter<'a> {
//     SupportedAlgorithmsIter::new(self)
//   }
// }

impl<'a> From<&'a [u8]> for SupportedAlgorithms {
  fn from(value: &'a [u8]) -> Self {
    let mut supported = SupportedAlgorithms::new();

    for a in value.iter().map(|i|Algorithm::from_u8(*i)) {
      if a.is_ok() {
        supported.set(a.unwrap());
      } else {
        warn!("unrecognized algorithm: {}", a.unwrap_err());
      }
    }


    supported
  }
}

impl<'a> From<&'a SupportedAlgorithms> for Vec<u8> {
  fn from(value: &'a SupportedAlgorithms) -> Vec<u8> {
    let mut bytes: Vec<u8> = Vec::with_capacity(8); // today this is less than 8

    for a in value.iter() {
      bytes.push(a.into());
    }

    bytes.shrink_to_fit();
    bytes
  }
}

pub struct SupportedAlgorithmsIter<'a> {
  algorithms: &'a SupportedAlgorithms,
  current: usize,
}

impl<'a> SupportedAlgorithmsIter<'a> {
  pub fn new(algorithms: &'a SupportedAlgorithms) -> Self {
    SupportedAlgorithmsIter{ algorithms: algorithms, current: 0 }
  }
}

impl<'a> Iterator for SupportedAlgorithmsIter<'a> {
  type Item = Algorithm;
  fn next(&mut self) -> Option<Self::Item> {
    // some quick bounds checking
    if self.current > u8::max_value() as usize { return None }

    while let Some(algorithm) = SupportedAlgorithms::from_pos(self.current as u8) {
      self.current += 1;
      if self.algorithms.has(algorithm) { return Some(algorithm) }
    }

    None
  }
}

#[test]
fn test_has() {
  let mut supported = SupportedAlgorithms::new();

  supported.set(Algorithm::RSASHA1);

  assert!(supported.has(Algorithm::RSASHA1));
  assert!(!supported.has(Algorithm::RSASHA1NSEC3SHA1));

  supported.set(Algorithm::RSASHA256);
  assert!(supported.has(Algorithm::RSASHA1));
  assert!(!supported.has(Algorithm::RSASHA1NSEC3SHA1));
  assert!(supported.has(Algorithm::RSASHA256));
}

#[test]
fn test_iterator() {
  let supported = SupportedAlgorithms::all();
  assert_eq!(supported.iter().count(), 4);

  // it just so happens that the iterator has a fixed order...
  let supported = SupportedAlgorithms::all();
  let mut iter = supported.iter();
  assert_eq!(iter.next(), Some(Algorithm::RSASHA1));
  assert_eq!(iter.next(), Some(Algorithm::RSASHA256));
  assert_eq!(iter.next(), Some(Algorithm::RSASHA1NSEC3SHA1));
  assert_eq!(iter.next(), Some(Algorithm::RSASHA512));

  let mut supported = SupportedAlgorithms::new();
  supported.set(Algorithm::RSASHA256);
  supported.set(Algorithm::RSASHA512);

  let mut iter = supported.iter();
  assert_eq!(iter.next(), Some(Algorithm::RSASHA256));
  assert_eq!(iter.next(), Some(Algorithm::RSASHA512));
}

#[test]
fn test_vec() {
  let supported = SupportedAlgorithms::all();
  let array: Vec<u8> = (&supported).into();
  let decoded: SupportedAlgorithms = (&array as &[_]).into();

  assert_eq!(supported, decoded);

  let mut supported = SupportedAlgorithms::new();
  supported.set(Algorithm::RSASHA256);
  supported.has(Algorithm::RSASHA256);
  supported.has(Algorithm::RSASHA1NSEC3SHA1);
  let array: Vec<u8> = (&supported).into();
  let decoded: SupportedAlgorithms = (&array as &[_]).into();

  assert_eq!(supported, decoded);
}