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