Add support for A/AAAA/NS records
[dnssec-prover] / src / ser.rs
1 //! Serialization/Deserialization logic lives here
2
3 use alloc::vec::Vec;
4 use alloc::string::String;
5
6 use ring::signature;
7
8 use crate::rr::*;
9
10 pub(crate) fn read_u8(inp: &mut &[u8]) -> Result<u8, ()> {
11         let res = *inp.get(0).ok_or(())?;
12         *inp = &inp[1..];
13         Ok(res)
14 }
15 pub(crate) fn read_u16(inp: &mut &[u8]) -> Result<u16, ()> {
16         if inp.len() < 2 { return Err(()); }
17         let mut bytes = [0; 2];
18         bytes.copy_from_slice(&inp[..2]);
19         *inp = &inp[2..];
20         Ok(u16::from_be_bytes(bytes))
21 }
22 pub(crate) fn read_u32(inp: &mut &[u8]) -> Result<u32, ()> {
23         if inp.len() < 4 { return Err(()); }
24         let mut bytes = [0; 4];
25         bytes.copy_from_slice(&inp[..4]);
26         *inp = &inp[4..];
27         Ok(u32::from_be_bytes(bytes))
28 }
29
30 pub(crate) fn read_name(inp: &mut &[u8]) -> Result<Name, ()> {
31         let mut name = String::with_capacity(1024);
32         loop {
33                 let len = read_u8(inp)? as usize;
34                 if len == 0 {
35                         if name.is_empty() { name += "."; }
36                         break;
37                 }
38                 if inp.len() <= len { return Err(()); }
39                 name += core::str::from_utf8(&inp[..len]).map_err(|_| ())?;
40                 name += ".";
41                 *inp = &inp[len..];
42                 if name.len() > 1024 { return Err(()); }
43         }
44         Ok(name.try_into()?)
45 }
46
47 pub(crate) trait Writer { fn write(&mut self, buf: &[u8]); }
48 impl Writer for Vec<u8> { fn write(&mut self, buf: &[u8]) { self.extend_from_slice(buf); } }
49 impl Writer for ring::digest::Context { fn write(&mut self, buf: &[u8]) { self.update(buf); } }
50 pub(crate) fn write_name<W: Writer>(out: &mut W, name: &str) {
51         let canonical_name = name.to_ascii_lowercase();
52         if canonical_name == "." {
53                 out.write(&[0]);
54         } else {
55                 for label in canonical_name.split(".") {
56                         out.write(&(label.len() as u8).to_be_bytes());
57                         out.write(label.as_bytes());
58                 }
59         }
60 }
61 pub(crate) fn name_len(name: &Name) -> u16 {
62         if name.as_str() == "." {
63                 1
64         } else {
65                 let mut res = 0;
66                 for label in name.split(".") {
67                         res += 1 + label.len();
68                 }
69                 res as u16
70         }
71 }
72
73 pub(crate) fn parse_rr(inp: &mut &[u8]) -> Result<RR, ()> {
74         let name = read_name(inp)?;
75         let ty = read_u16(inp)?;
76         let class = read_u16(inp)?;
77         if class != 1 { return Err(()); } // We only support the INternet
78         let _ttl = read_u32(inp)?;
79         let data_len = read_u16(inp)? as usize;
80         if inp.len() < data_len { return Err(()); }
81         let data = &inp[..data_len];
82         *inp = &inp[data_len..];
83
84         match ty {
85                 A::TYPE => Ok(RR::A(A::read_from_data(name, data)?)),
86                 AAAA::TYPE => Ok(RR::AAAA(AAAA::read_from_data(name, data)?)),
87                 NS::TYPE => Ok(RR::NS(NS::read_from_data(name, data)?)),
88                 Txt::TYPE => {
89                         Ok(RR::Txt(Txt::read_from_data(name, data)?))
90                 }
91                 CName::TYPE => {
92                         Ok(RR::CName(CName::read_from_data(name, data)?))
93                 }
94                 TLSA::TYPE => {
95                         Ok(RR::TLSA(TLSA::read_from_data(name, data)?))
96                 },
97                 DnsKey::TYPE => {
98                         Ok(RR::DnsKey(DnsKey::read_from_data(name, data)?))
99                 },
100                 DS::TYPE => {
101                         Ok(RR::DS(DS::read_from_data(name, data)?))
102                 },
103                 RRSig::TYPE => {
104                         Ok(RR::RRSig(RRSig::read_from_data(name, data)?))
105                 },
106                 _ => Err(()),
107         }
108 }
109
110 pub(crate) fn bytes_to_rsa_pk<'a>(pubkey: &'a [u8])
111 -> Result<signature::RsaPublicKeyComponents<&'a [u8]>, ()> {
112         if pubkey.len() <= 3 { return Err(()); }
113
114         let mut pos = 0;
115         let exponent_length;
116         if pubkey[0] == 0 {
117                 exponent_length = ((pubkey[1] as usize) << 8) | (pubkey[2] as usize);
118                 pos += 3;
119         } else {
120                 exponent_length = pubkey[0] as usize;
121                 pos += 1;
122         }
123
124         if pubkey.len() <= pos + exponent_length { return Err(()); }
125         Ok(signature::RsaPublicKeyComponents {
126                 n: &pubkey[pos + exponent_length..],
127                 e: &pubkey[pos..pos + exponent_length]
128         })
129 }