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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::{ Result, SerdeAsn1DerError, misc::ReaderSource };
use serde::{
Deserialize,
de::{ DeserializeSeed, Visitor, SeqAccess }
};
use asn1_der::{
Source, Sink, DerObject, ErrorChain,
typed::{ DerTypeView, DerDecodable, Boolean, Integer, Null, OctetString, Sequence, Utf8String },
};
use std::io::Read;
struct SequenceReader<'a> {
sequence: Sequence<'a>,
pos: usize
}
impl<'a> SeqAccess<'a> for SequenceReader<'a> {
type Error = SerdeAsn1DerError;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
where T: DeserializeSeed<'a>
{
let object = match self.sequence.get(self.pos) {
Ok(object) => object,
Err(_) => return Ok(None)
};
self.pos += 1;
let mut deserializer = Deserializer{ object };
let next = seed.deserialize(&mut deserializer)?;
Ok(Some(next))
}
}
struct Deserializer<'a> {
object: DerObject<'a>
}
impl<'a, 'r> serde::de::Deserializer<'a> for &'r mut Deserializer<'a> {
type Error = SerdeAsn1DerError;
fn deserialize_any<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
match self.object.tag() {
Boolean::TAG => self.deserialize_bool(visitor),
Integer::TAG => {
let integer = Integer::load(self.object).propagate(e!("Failed to load integer"))?;
match integer.is_negative() {
true => self.deserialize_i128(visitor),
false => self.deserialize_u128(visitor)
}
},
Null::TAG => self.deserialize_option(visitor),
OctetString::TAG => self.deserialize_byte_buf(visitor),
Sequence::TAG => self.deserialize_seq(visitor),
Utf8String::TAG => self.deserialize_string(visitor),
_ => Err(eunsupported!("The object type is not supported by this implementation"))?
}
}
fn deserialize_bool<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let bool = bool::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_bool(bool)
}
fn deserialize_i8<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let i8 = i8::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_i8(i8)
}
fn deserialize_i16<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let i16 = i16::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_i16(i16)
}
fn deserialize_i32<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let i32 = i32::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_i32(i32)
}
fn deserialize_i64<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let i64 = i64::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_i64(i64)
}
fn deserialize_i128<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let i128 = i128::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_i128(i128)
}
fn deserialize_u8<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let u8 = u8::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_u8(u8)
}
fn deserialize_u16<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let u16 = u16::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_u16(u16)
}
fn deserialize_u32<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let u32 = u32::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_u32(u32)
}
fn deserialize_u64<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let u64 = u64::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_u64(u64)
}
fn deserialize_u128<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let u128 = u128::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_u128(u128)
}
fn deserialize_f32<V: Visitor<'a>>(self, _visitor: V) -> Result<V::Value> {
Err(eunsupported!("The object type is not supported by this implementation"))?
}
fn deserialize_f64<V: Visitor<'a>>(self, _visitor: V) -> Result<V::Value> {
Err(eunsupported!("The object type is not supported by this implementation"))?
}
fn deserialize_char<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let s = Utf8String::load(self.object).propagate(e!("Failed to load object"))?;
let c = s.get().chars().next().ok_or(einval!("Cannot read char from empty string object"))?;
visitor.visit_char(c)
}
fn deserialize_str<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let s = Utf8String::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_str(s.get())
}
fn deserialize_string<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let string = String::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_string(string)
}
fn deserialize_bytes<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let bytes = OctetString::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_bytes(bytes.get())
}
fn deserialize_byte_buf<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let bytes = Vec::<u8>::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_byte_buf(bytes)
}
fn deserialize_option<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
match self.object.tag() {
Null::TAG => visitor.visit_none(),
_ => visitor.visit_some(self)
}
}
fn deserialize_unit<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
Null::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_unit()
}
fn deserialize_unit_struct<V: Visitor<'a>>(self, _name: &'static str, visitor: V)
-> Result<V::Value>
{
self.deserialize_unit(visitor)
}
fn deserialize_newtype_struct<V: Visitor<'a>>(self, _name: &'static str, visitor: V)
-> Result<V::Value>
{
self.deserialize_any(visitor)
}
fn deserialize_seq<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
let sequence = Sequence::load(self.object).propagate(e!("Failed to load object"))?;
visitor.visit_seq(SequenceReader { sequence, pos: 0 })
}
fn deserialize_tuple<V: Visitor<'a>>(self, _len: usize, visitor: V) -> Result<V::Value> {
self.deserialize_seq(visitor)
}
fn deserialize_tuple_struct<V: Visitor<'a>>(self, _name: &'static str, _len: usize, visitor: V)
-> Result<V::Value>
{
self.deserialize_seq(visitor)
}
fn deserialize_map<V: Visitor<'a>>(self, _visitor: V) -> Result<V::Value> {
Err(eunsupported!("The object type is not supported by this implementation"))?
}
fn deserialize_struct<V: Visitor<'a>>(self, _name: &'static str,
_fields: &'static [&'static str], visitor: V) -> Result<V::Value>
{
self.deserialize_seq(visitor)
}
fn deserialize_enum<V: Visitor<'a>>(self, _name: &'static str,
_variants: &'static [&'static str], _visitor: V) -> Result<V::Value>
{
Err(eunsupported!("The object type is not supported by this implementation"))?
}
fn deserialize_identifier<V: Visitor<'a>>(self, _visitor: V) -> Result<V::Value> {
Err(eunsupported!("The object type is not supported by this implementation"))?
}
fn deserialize_ignored_any<V: Visitor<'a>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_unit()
}
}
pub fn from_bytes<'a, T: Deserialize<'a>>(bytes: &'a[u8]) -> Result<T> {
let object = DerObject::decode(bytes).propagate(e!("Failed to decode DER object"))?;
T::deserialize(&mut Deserializer{ object })
}
pub fn from_reader<'a, T: Deserialize<'a>>(reader: impl Read, backing: impl Sink + Into<&'a[u8]>)
-> Result<T>
{
from_source(ReaderSource(reader), backing)
}
pub fn from_source<'a, T: Deserialize<'a>>(mut source: impl Source,
backing: impl Sink + Into<&'a[u8]>) -> Result<T>
{
let object = DerObject::decode_from_source(&mut source, backing)
.propagate(e!("Failed to decode DER object"))?;
T::deserialize(&mut Deserializer{ object })
}