hickory_proto/rr/dnssec/trust_anchor.rs
1/*
2 * Copyright (C) 2015 Benjamin Fry <benjaminfry@me.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Allows for the root trust_anchor to either be added to or replaced for dns_sec validation.
18
19use std::default::Default;
20
21use crate::rr::dnssec::PublicKey;
22
23const ROOT_ANCHOR_ORIG: &[u8] = include_bytes!("roots/19036.rsa");
24const ROOT_ANCHOR_2018: &[u8] = include_bytes!("roots/20326.rsa");
25
26/// The root set of trust anchors for validating DNSSEC, anything in this set will be trusted
27#[derive(Clone)]
28pub struct TrustAnchor {
29 // TODO: these should also store some information, or more specifically, metadata from the signed
30 // public certificate.
31 pkeys: Vec<Vec<u8>>,
32}
33
34impl Default for TrustAnchor {
35 fn default() -> Self {
36 Self {
37 pkeys: vec![ROOT_ANCHOR_ORIG.to_owned(), ROOT_ANCHOR_2018.to_owned()],
38 }
39 }
40}
41
42impl TrustAnchor {
43 /// Creates a new empty trust anchor set
44 pub fn new() -> Self {
45 Self { pkeys: vec![] }
46 }
47
48 /// determines if the key is in the trust anchor set with the raw dnskey bytes
49 ///
50 /// # Arguments
51 ///
52 /// * `other_key` - The raw dnskey in bytes
53 pub fn contains_dnskey_bytes(&self, other_key: &[u8]) -> bool {
54 self.pkeys.iter().any(|k| other_key == k.as_slice())
55 }
56
57 /// determines if the key is in the trust anchor set
58 pub fn contains<P: PublicKey>(&self, other_key: &P) -> bool {
59 self.contains_dnskey_bytes(other_key.public_bytes())
60 }
61
62 /// inserts the trust_anchor to the trusted chain
63 pub fn insert_trust_anchor<P: PublicKey>(&mut self, public_key: &P) {
64 if !self.contains(public_key) {
65 self.pkeys.push(public_key.public_bytes().to_vec())
66 }
67 }
68
69 /// get the trust anchor at the specified index
70 pub fn get(&self, idx: usize) -> &[u8] {
71 &self.pkeys[idx]
72 }
73
74 /// number of keys in trust_anchor
75 pub fn len(&self) -> usize {
76 self.pkeys.len()
77 }
78
79 /// returns true if there are no keys in the trust_anchor
80 pub fn is_empty(&self) -> bool {
81 self.pkeys.is_empty()
82 }
83}
84
85#[test]
86fn test_kjqmt7v() {
87 let trust = TrustAnchor::default();
88 assert_eq!(trust.get(0), ROOT_ANCHOR_ORIG);
89 assert!(trust.contains_dnskey_bytes(ROOT_ANCHOR_ORIG));
90}