1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
use ::Asn1DerError;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DerValue {
pub data: Vec<u8>
}
impl DerValue {
pub fn deserialize<'a>(mut source: impl Iterator<Item = &'a u8>, len: impl Into<usize>)
-> Result<Self, Asn1DerError>
{
let mut data_buf = Vec::new();
for _ in 0..len.into() {
data_buf.push(*source.next().ok_or(Asn1DerError::LengthMismatch)?);
}
Ok(data_buf.into())
}
pub fn serialized_len(&self) -> usize {
self.data.len()
}
pub fn serialize<'a>(&self, mut buf: impl Iterator<Item = &'a mut u8>)
-> Result<usize, Asn1DerError>
{
for b in self.data.iter() {
*buf.next().ok_or(Asn1DerError::LengthMismatch)? = *b
}
Ok(self.data.len())
}
pub fn compute_serialized_len(payload_len: usize) -> usize {
payload_len
}
}
impl From<Vec<u8>> for DerValue {
fn from(data: Vec<u8>) -> Self {
DerValue { data }
}
}
impl From<DerValue> for Vec<u8> {
fn from(value: DerValue) -> Self {
value.data
}
}