Debian Codegraph @

src/dns_types.rs

raw
1 //! DNS and DNSSEC types.
2
3 use std::str::{Chars, FromStr};
4
5 use {idna::domain_to_ascii, thiserror::Error};
6
7 use crate::check::StringCheck;
8
9 /// Errors when determining DNS information.
10 #[derive(Debug, Error)]
11 pub enum DnsTypeError {
12 #[error("Invalid DNS Algorithm")]
13 InvalidAlgorithm,
14 #[error("Invalid DNS Digest")]
15 InvalidDigest,
16 }
17
18 /// Information about DNSSEC Algorithm.
19 pub struct DnsAlgorithm {
20 pub number: u8,
21 pub mnemonic: &'static str,
22 pub zone_signing: bool,
23 pub transaction_signing: bool,
24 }
25
26 /// DNS Algorithm Variants.
27 pub enum DnsAlgorithmType {
28 DeleteDs(DnsAlgorithm),
29 RsaMd5(DnsAlgorithm),
30 DiffieHellman(DnsAlgorithm),
31 Dsa(DnsAlgorithm),
32 RsaSha1(DnsAlgorithm),
33 DsaNsec3Sha1(DnsAlgorithm),
34 RsaSha1Nsec3Sha1(DnsAlgorithm),
35 RsaSha256(DnsAlgorithm),
36 RsaSha512(DnsAlgorithm),
37 EccGost(DnsAlgorithm),
38 EcdsaP256Sha256(DnsAlgorithm),
39 EcdsaP384Sha384(DnsAlgorithm),
40 Ed25519(DnsAlgorithm),
41 Ed448(DnsAlgorithm),
42 PrivateDns(DnsAlgorithm),
43 PrivateOid(DnsAlgorithm),
44 }
45
46 impl DnsAlgorithmType {
47 /// Convert an algorithm number to a [DnsAlgorithmType].
48 pub fn from_number(number: u8) -> Result<Self, DnsTypeError> {
49 Ok(match number {
50 0 => Self::DeleteDs(DnsAlgorithm {
51 number: 0,
52 mnemonic: "DELETE",
53 zone_signing: false,
54 transaction_signing: false,
55 }),
56 1 => Self::RsaMd5(DnsAlgorithm {
57 number: 1,
58 mnemonic: "RSAMD5",
59 zone_signing: false,
60 transaction_signing: true,
61 }),
62 2 => Self::DiffieHellman(DnsAlgorithm {
63 number: 2,
64 mnemonic: "DH",
65 zone_signing: false,
66 transaction_signing: true,
67 }),
68 3 => Self::Dsa(DnsAlgorithm {
69 number: 3,
70 mnemonic: "DSA",
71 zone_signing: true,
72 transaction_signing: true,
73 }),
74 5 => Self::RsaSha1(DnsAlgorithm {
75 number: 5,
76 mnemonic: "RSASHA1",
77 zone_signing: true,
78 transaction_signing: true,
79 }),
80 6 => Self::DsaNsec3Sha1(DnsAlgorithm {
81 number: 6,
82 mnemonic: "DSA-NSEC3-SHA1",
83 zone_signing: true,
84 transaction_signing: true,
85 }),
86 7 => Self::RsaSha1Nsec3Sha1(DnsAlgorithm {
87 number: 7,
88 mnemonic: "RSA-NSEC3-SHA1",
89 zone_signing: true,
90 transaction_signing: true,
91 }),
92 8 => Self::RsaSha256(DnsAlgorithm {
93 number: 8,
94 mnemonic: "RSASHA256",
95 zone_signing: true,
96 transaction_signing: false,
97 }),
98 10 => Self::RsaSha512(DnsAlgorithm {
99 number: 10,
100 mnemonic: "RSASHA512",
101 zone_signing: true,
102 transaction_signing: false,
103 }),
104 12 => Self::EccGost(DnsAlgorithm {
105 number: 12,
106 mnemonic: "ECC-GOST",
107 zone_signing: true,
108 transaction_signing: false,
109 }),
110 13 => Self::EcdsaP256Sha256(DnsAlgorithm {
111 number: 13,
112 mnemonic: "ECDSAP256SHA256",
113 zone_signing: true,
114 transaction_signing: false,
115 }),
116 14 => Self::EcdsaP384Sha384(DnsAlgorithm {
117 number: 14,
118 mnemonic: "ECDSAP384SHA384",
119 zone_signing: true,
120 transaction_signing: false,
121 }),
122 15 => Self::Ed25519(DnsAlgorithm {
123 number: 15,
124 mnemonic: "ED25519",
125 zone_signing: true,
126 transaction_signing: false,
127 }),
128 16 => Self::Ed448(DnsAlgorithm {
129 number: 16,
130 mnemonic: "ED448",
131 zone_signing: true,
132 transaction_signing: false,
133 }),
134 253 => Self::PrivateDns(DnsAlgorithm {
135 number: 253,
136 mnemonic: "PRIVATEDNS",
137 zone_signing: true,
138 transaction_signing: true,
139 }),
140 254 => Self::PrivateOid(DnsAlgorithm {
141 number: 254,
142 mnemonic: "PRIVATEOID",
143 zone_signing: true,
144 transaction_signing: true,
145 }),
146 _ => return Err(DnsTypeError::InvalidAlgorithm),
147 })
148 }
149
150 fn algo(self) -> DnsAlgorithm {
151 match self {
152 Self::DeleteDs(a)
153 | Self::RsaMd5(a)
154 | Self::DiffieHellman(a)
155 | Self::Dsa(a)
156 | Self::RsaSha1(a)
157 | Self::DsaNsec3Sha1(a)
158 | Self::RsaSha1Nsec3Sha1(a)
159 | Self::RsaSha256(a)
160 | Self::RsaSha512(a)
161 | Self::EccGost(a)
162 | Self::EcdsaP256Sha256(a)
163 | Self::EcdsaP384Sha384(a)
164 | Self::Ed25519(a)
165 | Self::Ed448(a)
166 | Self::PrivateDns(a)
167 | Self::PrivateOid(a) => a,
168 }
169 }
170
171 /// Get the mnemonic for the algorithm number.
172 pub fn mnemonic(number: u8) -> Result<&'static str, DnsTypeError> {
173 let alg = Self::from_number(number)?;
174 Ok(alg.algo().mnemonic)
175 }
176
177 /// True if the DNS Algorithm can sign zones.
178 pub fn zone_signing(number: u8) -> Result<bool, DnsTypeError> {
179 let alg = Self::from_number(number)?;
180 Ok(alg.algo().zone_signing)
181 }
182 }
183
184 /// DNS Digest.
185 pub struct DnsDigest {
186 pub number: u8,
187 pub mnemonic: &'static str,
188 pub mandatory: bool,
189 }
190
191 /*
192
193 0 Reserved - [RFC3658]
194 1 SHA-1 MANDATORY [RFC3658]
195 2 SHA-256 MANDATORY [RFC4509]
196 3 GOST R 34.11-94 OPTIONAL [RFC5933]
197 4 SHA-384 OPTIONAL
198 */
199
200 /// DNS Digest Variants.
201 pub enum DnsDigestType {
202 Sha1(DnsDigest),
203 Sha256(DnsDigest),
204 Gost(DnsDigest),
205 Sha384(DnsDigest),
206 }
207
208 impl DnsDigestType {
209 /// Get the [DnsDigestType] from the protocol number.
210 pub fn from_number(number: u8) -> Result<Self, DnsTypeError> {
211 Ok(match number {
212 1 => Self::Sha1(DnsDigest {
213 number: 1,
214 mnemonic: "SHA1",
215 mandatory: true,
216 }),
217 2 => Self::Sha256(DnsDigest {
218 number: 2,
219 mnemonic: "SHA256",
220 mandatory: true,
221 }),
222 3 => Self::Gost(DnsDigest {
223 number: 3,
224 mnemonic: "GOST",
225 mandatory: false,
226 }),
227 4 => Self::Sha384(DnsDigest {
228 number: 4,
229 mnemonic: "SHA384",
230 mandatory: false,
231 }),
232 _ => return Err(DnsTypeError::InvalidDigest),
233 })
234 }
235
236 /// Get the mnemonic from the protocol number.
237 pub fn mnemonic(number: u8) -> Result<&'static str, DnsTypeError> {
238 let digest = Self::from_number(number)?;
239 Ok(match digest {
240 Self::Sha1(d) | Self::Sha256(d) | Self::Gost(d) | Self::Sha384(d) => d.mnemonic,
241 })
242 }
243 }
244
245 /// Error specific to processing of domain names.
246 #[derive(Debug, Error)]
247 pub enum DomainNameError {
248 #[error("Invalid Domain Name")]
249 InvalidDomainName,
250 #[error(transparent)]
251 IdnaError(#[from] idna::Errors),
252 }
253
254 /// Represents a Domain name.
255 #[derive(Debug, Clone)]
256 pub struct DomainName {
257 domain_name: String,
258 ascii: String,
259 }
260
261 impl DomainName {
262 /// Iterate over the characters of the domain name.
263 pub fn chars(&self) -> Chars<'_> {
264 self.domain_name.chars()
265 }
266
267 /// Is this domain name a TLD.
268 pub fn is_tld(&self) -> bool {
269 self.domain_name.is_tld()
270 }
271
272 /// Gets the ASCII version of the domain, which is different if this is an IDN.
273 pub fn to_ascii(&self) -> &str {
274 &self.ascii
275 }
276
277 /// Is this domain name an IDN.
278 pub fn is_idn(&self) -> bool {
279 !self.ascii.eq(&self.domain_name)
280 }
281
282 /// Is this the DNS root.
283 pub fn is_root(&self) -> bool {
284 self.domain_name.eq(".")
285 }
286
287 /// Get this domain name with a leading dot.
288 pub fn with_leading_dot(&self) -> String {
289 if !self.is_root() {
290 format!(".{}", self.domain_name)
291 } else {
292 self.domain_name.to_string()
293 }
294 }
295
296 /// Trim leading dot.
297 pub fn trim_leading_dot(&self) -> &str {
298 if !self.is_root() {
299 self.domain_name.trim_start_matches('.')
300 } else {
301 &self.domain_name
302 }
303 }
304 }
305
306 impl FromStr for DomainName {
307 type Err = DomainNameError;
308
309 /// Create a new DomainName from a string.
310 fn from_str(s: &str) -> Result<Self, Self::Err> {
311 let s = s.trim_end_matches('.');
312 if !s.is_unicode_domain_name() {
313 return Err(DomainNameError::InvalidDomainName);
314 }
315 let ascii = domain_to_ascii(s)?;
316 Ok(Self {
317 domain_name: s.to_string(),
318 ascii,
319 })
320 }
321 }
322
323 #[cfg(test)]
324 mod tests {
325 use super::*;
326
327 #[test]
328 fn domain_with_trailing_dot() {
329 // GIVEN
330 let input = "icann.org.";
331
332 // WHEN
333 let domain = DomainName::from_str(input).expect("domain_name");
334
335 // THEN
336 assert_eq!(domain.domain_name, "icann.org");
337 }
338
339 #[test]
340 fn domain_without_trailing_dot() {
341 // GIVEN
342 let input = "icann.org";
343
344 // WHEN
345 let domain = DomainName::from_str(input).expect("domain_name");
346
347 // THEN
348 assert_eq!(domain.domain_name, "icann.org");
349 }
350
351 #[test]
352 fn idn_with_trailing_dot() {
353 // GIVEN
354 let input = "example.com.";
355
356 // WHEN
357 let domain = DomainName::from_str(input).expect("domain_name");
358
359 // THEN
360 assert_eq!(domain.to_ascii(), "example.com");
361 }
362
363 #[test]
364 fn root_dot() {
365 // GIVEN
366 let input = ".";
367
368 // WHEN
369 let result = DomainName::from_str(input);
370
371 // THEN
372 assert!(result.is_err());
373 }
374 }