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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::enveloped_data_content::EncryptedContentInfo;
use der::{
Decodable, DecodeValue, Decoder, Encodable, EncodeValue, Encoder, FixedTag, Length, Sequence,
Tag,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Version {
V0 = 0,
}
impl FixedTag for Version {
const TAG: Tag = Tag::Integer;
}
impl From<Version> for u8 {
fn from(version: Version) -> Self {
version as u8
}
}
impl TryFrom<u8> for Version {
type Error = der::Error;
fn try_from(byte: u8) -> der::Result<Version> {
match byte {
0 => Ok(Version::V0),
_ => Err(Self::TAG.value_error()),
}
}
}
impl<'a> DecodeValue<'a> for Version {
fn decode_value(decoder: &mut Decoder<'a>, length: Length) -> der::Result<Version> {
Version::try_from(u8::decode_value(decoder, length)?)
}
}
impl EncodeValue for Version {
fn value_len(&self) -> der::Result<Length> {
u8::from(*self).value_len()
}
fn encode_value(&self, encoder: &mut Encoder<'_>) -> der::Result<()> {
u8::from(*self).encode_value(encoder)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct EncryptedDataContent<'a> {
pub version: Version,
pub encrypted_content_info: EncryptedContentInfo<'a>,
}
impl<'a> Decodable<'a> for EncryptedDataContent<'a> {
fn decode(decoder: &mut Decoder<'a>) -> der::Result<EncryptedDataContent<'a>> {
decoder.sequence(|decoder| {
Ok(EncryptedDataContent {
version: decoder.decode()?,
encrypted_content_info: decoder.decode()?,
})
})
}
}
impl<'a> Sequence<'a> for EncryptedDataContent<'a> {
fn fields<F, T>(&self, f: F) -> der::Result<T>
where
F: FnOnce(&[&dyn Encodable]) -> der::Result<T>,
{
f(&[&self.version, &self.encrypted_content_info])
}
}