1 | use crate::error::InvalidMessage; |
2 | use crate::msgs::codec; |
3 | use crate::msgs::codec::{Codec, Reader}; |
4 | |
5 | use alloc::vec::Vec; |
6 | use core::fmt; |
7 | |
8 | use pki_types::CertificateDer; |
9 | use zeroize::Zeroize; |
10 | |
11 | /// An externally length'd payload |
12 | #[derive (Clone, Eq, PartialEq)] |
13 | pub struct Payload(pub Vec<u8>); |
14 | |
15 | impl Codec for Payload { |
16 | fn encode(&self, bytes: &mut Vec<u8>) { |
17 | bytes.extend_from_slice(&self.0); |
18 | } |
19 | |
20 | fn read(r: &mut Reader) -> Result<Self, InvalidMessage> { |
21 | Ok(Self::read(r)) |
22 | } |
23 | } |
24 | |
25 | impl Payload { |
26 | pub fn new(bytes: impl Into<Vec<u8>>) -> Self { |
27 | Self(bytes.into()) |
28 | } |
29 | |
30 | pub fn empty() -> Self { |
31 | Self::new(bytes:Vec::new()) |
32 | } |
33 | |
34 | pub fn read(r: &mut Reader) -> Self { |
35 | Self(r.rest().to_vec()) |
36 | } |
37 | } |
38 | |
39 | impl<'a> Codec for CertificateDer<'a> { |
40 | fn encode(&self, bytes: &mut Vec<u8>) { |
41 | codec::u24(self.as_ref().len() as u32).encode(bytes); |
42 | bytes.extend(self.as_ref()); |
43 | } |
44 | |
45 | fn read(r: &mut Reader) -> Result<Self, InvalidMessage> { |
46 | let len: usize = codec::u24::read(r)?.0 as usize; |
47 | let mut sub: Reader<'_> = r.sub(length:len)?; |
48 | let body: Vec = sub.rest().to_vec(); |
49 | Ok(Self::from(body)) |
50 | } |
51 | } |
52 | |
53 | impl fmt::Debug for Payload { |
54 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
55 | hex(f, &self.0) |
56 | } |
57 | } |
58 | |
59 | /// An arbitrary, unknown-content, u24-length-prefixed payload |
60 | #[derive (Clone, Eq, PartialEq)] |
61 | pub(crate) struct PayloadU24(pub(crate) Vec<u8>); |
62 | |
63 | impl PayloadU24 { |
64 | pub(crate) fn new(bytes: Vec<u8>) -> Self { |
65 | Self(bytes) |
66 | } |
67 | } |
68 | |
69 | impl Codec for PayloadU24 { |
70 | fn encode(&self, bytes: &mut Vec<u8>) { |
71 | codec::u24(self.0.len() as u32).encode(bytes); |
72 | bytes.extend_from_slice(&self.0); |
73 | } |
74 | |
75 | fn read(r: &mut Reader) -> Result<Self, InvalidMessage> { |
76 | let len: usize = codec::u24::read(r)?.0 as usize; |
77 | let mut sub: Reader<'_> = r.sub(length:len)?; |
78 | let body: Vec = sub.rest().to_vec(); |
79 | Ok(Self(body)) |
80 | } |
81 | } |
82 | |
83 | impl fmt::Debug for PayloadU24 { |
84 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
85 | hex(f, &self.0) |
86 | } |
87 | } |
88 | |
89 | /// An arbitrary, unknown-content, u16-length-prefixed payload |
90 | #[derive (Clone, Eq, PartialEq)] |
91 | pub struct PayloadU16(pub Vec<u8>); |
92 | |
93 | impl PayloadU16 { |
94 | pub fn new(bytes: Vec<u8>) -> Self { |
95 | Self(bytes) |
96 | } |
97 | |
98 | pub fn empty() -> Self { |
99 | Self::new(bytes:Vec::new()) |
100 | } |
101 | |
102 | pub fn encode_slice(slice: &[u8], bytes: &mut Vec<u8>) { |
103 | (slice.len() as u16).encode(bytes); |
104 | bytes.extend_from_slice(slice); |
105 | } |
106 | } |
107 | |
108 | impl Codec for PayloadU16 { |
109 | fn encode(&self, bytes: &mut Vec<u8>) { |
110 | Self::encode_slice(&self.0, bytes); |
111 | } |
112 | |
113 | fn read(r: &mut Reader) -> Result<Self, InvalidMessage> { |
114 | let len: usize = u16::read(r)? as usize; |
115 | let mut sub: Reader<'_> = r.sub(length:len)?; |
116 | let body: Vec = sub.rest().to_vec(); |
117 | Ok(Self(body)) |
118 | } |
119 | } |
120 | |
121 | impl fmt::Debug for PayloadU16 { |
122 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
123 | hex(f, &self.0) |
124 | } |
125 | } |
126 | |
127 | /// An arbitrary, unknown-content, u8-length-prefixed payload |
128 | #[derive (Clone, Eq, PartialEq)] |
129 | pub struct PayloadU8(pub(crate) Vec<u8>); |
130 | |
131 | impl PayloadU8 { |
132 | pub(crate) fn encode_slice(slice: &[u8], bytes: &mut Vec<u8>) { |
133 | (slice.len() as u8).encode(bytes); |
134 | bytes.extend_from_slice(slice); |
135 | } |
136 | |
137 | pub(crate) fn new(bytes: Vec<u8>) -> Self { |
138 | Self(bytes) |
139 | } |
140 | |
141 | pub(crate) fn empty() -> Self { |
142 | Self(Vec::new()) |
143 | } |
144 | } |
145 | |
146 | impl Codec for PayloadU8 { |
147 | fn encode(&self, bytes: &mut Vec<u8>) { |
148 | (self.0.len() as u8).encode(bytes); |
149 | bytes.extend_from_slice(&self.0); |
150 | } |
151 | |
152 | fn read(r: &mut Reader) -> Result<Self, InvalidMessage> { |
153 | let len: usize = u8::read(r)? as usize; |
154 | let mut sub: Reader<'_> = r.sub(length:len)?; |
155 | let body: Vec = sub.rest().to_vec(); |
156 | Ok(Self(body)) |
157 | } |
158 | } |
159 | |
160 | impl Zeroize for PayloadU8 { |
161 | fn zeroize(&mut self) { |
162 | self.0.zeroize(); |
163 | } |
164 | } |
165 | |
166 | impl fmt::Debug for PayloadU8 { |
167 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
168 | hex(f, &self.0) |
169 | } |
170 | } |
171 | |
172 | // Format an iterator of u8 into a hex string |
173 | pub(super) fn hex<'a>( |
174 | f: &mut fmt::Formatter<'_>, |
175 | payload: impl IntoIterator<Item = &'a u8>, |
176 | ) -> fmt::Result { |
177 | for b: &u8 in payload { |
178 | write!(f, " {:02x}" , b)?; |
179 | } |
180 | Ok(()) |
181 | } |
182 | |