| 1 |
|
| 2 |
|
| 3 |
use crate::{Header, Length, Result, SliceWriter, Tagged, Writer}; |
| 4 |
use core::marker::PhantomData; |
| 5 |
|
| 6 |
#[cfg(feature = "alloc")] |
| 7 |
use alloc::{ |
| 8 |
borrow::{Cow, ToOwned}, |
| 9 |
boxed::Box, |
| 10 |
vec::Vec, |
| 11 |
}; |
| 12 |
|
| 13 |
#[cfg(feature = "pem")] |
| 14 |
use { |
| 15 |
crate::PemWriter, |
| 16 |
alloc::string::String, |
| 17 |
pem_rfc7468::{self as pem, LineEnding, PemLabel}, |
| 18 |
}; |
| 19 |
|
| 20 |
#[cfg(any(feature = "alloc", feature = "pem"))] |
| 21 |
use crate::ErrorKind; |
| 22 |
|
| 23 |
#[cfg(doc)] |
| 24 |
use crate::{FixedTag, Tag}; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#[diagnostic::on_unimplemented( |
| 55 |
note = "Consider adding impls of `EncodeValue` and `FixedTag` to `{Self}`" |
| 56 |
)] |
| 57 |
pub trait Encode { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
fn encoded_len(&self) -> Result<Length>; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
fn encode(&self, writer: &mut impl Writer) -> Result<()>; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8]> { |
| 76 |
let mut writer = SliceWriter::new(buf); |
| 77 |
self.encode(&mut writer)?; |
| 78 |
writer.finish() |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
#[cfg(feature = "alloc")] |
| 87 |
fn encode_to_vec(&self, buf: &mut Vec<u8>) -> Result<Length> { |
| 88 |
let expected_len = usize::try_from(self.encoded_len()?)?; |
| 89 |
let initial_len = buf.len(); |
| 90 |
buf.resize(initial_len + expected_len, 0u8); |
| 91 |
|
| 92 |
let buf_slice = &mut buf[initial_len..]; |
| 93 |
let actual_len = self.encode_to_slice(buf_slice)?.len(); |
| 94 |
|
| 95 |
if expected_len != actual_len { |
| 96 |
return Err(ErrorKind::Incomplete { |
| 97 |
expected_len: expected_len.try_into()?, |
| 98 |
actual_len: actual_len.try_into()?, |
| 99 |
} |
| 100 |
.into()); |
| 101 |
} |
| 102 |
|
| 103 |
actual_len.try_into() |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
#[cfg(feature = "alloc")] |
| 111 |
fn to_der(&self) -> Result<Vec<u8>> { |
| 112 |
let mut buf = Vec::new(); |
| 113 |
self.encode_to_vec(&mut buf)?; |
| 114 |
Ok(buf) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
impl<T> Encode for T |
| 119 |
where |
| 120 |
T: EncodeValue + Tagged + ?Sized, |
| 121 |
{ |
| 122 |
fn encoded_len(&self) -> Result<Length> { |
| 123 |
self.value_len().and_then(|len| len.for_tlv(self.tag())) |
| 124 |
} |
| 125 |
|
| 126 |
fn encode(&self, writer: &mut impl Writer) -> Result<()> { |
| 127 |
self.header()?.encode(writer)?; |
| 128 |
self.encode_value(writer) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
impl<T> Encode for PhantomData<T> |
| 135 |
where |
| 136 |
T: ?Sized, |
| 137 |
{ |
| 138 |
fn encoded_len(&self) -> Result<Length> { |
| 139 |
Ok(Length::ZERO) |
| 140 |
} |
| 141 |
|
| 142 |
fn encode(&self, _writer: &mut impl Writer) -> Result<()> { |
| 143 |
Ok(()) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
#[cfg(feature = "pem")] |
| 152 |
#[diagnostic::on_unimplemented( |
| 153 |
note = "`EncodePem` is auto-impl'd for types which impl both `Encode` and `PemLabel`" |
| 154 |
)] |
| 155 |
pub trait EncodePem: Encode + PemLabel { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
fn to_pem(&self, line_ending: LineEnding) -> Result<String>; |
| 161 |
} |
| 162 |
|
| 163 |
#[cfg(feature = "pem")] |
| 164 |
impl<T> EncodePem for T |
| 165 |
where |
| 166 |
T: Encode + PemLabel + ?Sized, |
| 167 |
{ |
| 168 |
fn to_pem(&self, line_ending: LineEnding) -> Result<String> { |
| 169 |
let der_len = usize::try_from(self.encoded_len()?)?; |
| 170 |
let pem_len = pem::encapsulated_len(Self::PEM_LABEL, line_ending, der_len)?; |
| 171 |
|
| 172 |
let mut buf = vec![0u8; pem_len]; |
| 173 |
let mut writer = PemWriter::new(Self::PEM_LABEL, line_ending, &mut buf)?; |
| 174 |
self.encode(&mut writer)?; |
| 175 |
|
| 176 |
let actual_len = writer.finish()?; |
| 177 |
buf.truncate(actual_len); |
| 178 |
Ok(String::from_utf8(buf)?) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
pub trait EncodeValue { |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
fn header(&self) -> Result<Header> |
| 226 |
where |
| 227 |
Self: Tagged, |
| 228 |
{ |
| 229 |
Ok(Header::new(self.tag(), self.value_len()?)) |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
fn value_len(&self) -> Result<Length>; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
fn encode_value(&self, writer: &mut impl Writer) -> Result<()>; |
| 245 |
} |
| 246 |
|
| 247 |
#[cfg(feature = "alloc")] |
| 248 |
impl<T> EncodeValue for Box<T> |
| 249 |
where |
| 250 |
T: EncodeValue, |
| 251 |
{ |
| 252 |
fn value_len(&self) -> Result<Length> { |
| 253 |
T::value_len(self) |
| 254 |
} |
| 255 |
fn encode_value(&self, writer: &mut impl Writer) -> Result<()> { |
| 256 |
T::encode_value(self, writer) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
#[cfg(feature = "alloc")] |
| 261 |
impl<T> EncodeValue for Cow<'_, T> |
| 262 |
where |
| 263 |
T: ToOwned + ?Sized, |
| 264 |
for<'a> &'a T: EncodeValue, |
| 265 |
{ |
| 266 |
fn value_len(&self) -> Result<Length> { |
| 267 |
self.as_ref().value_len() |
| 268 |
} |
| 269 |
|
| 270 |
fn encode_value(&self, writer: &mut impl Writer) -> Result<()> { |
| 271 |
self.as_ref().encode_value(writer) |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
pub(crate) fn encode_value_to_slice<'a, T>(buf: &'a mut [u8], value: &T) -> Result<&'a [u8]> |
| 277 |
where |
| 278 |
T: EncodeValue, |
| 279 |
{ |
| 280 |
let mut writer = SliceWriter::new(buf); |
| 281 |
value.encode_value(&mut writer)?; |
| 282 |
writer.finish() |
| 283 |
} |