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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! Error types and handling
//!
//! All errors include position information for debugging.
use crate::Tag;
/// Result type for ASN.1 operations
pub type Result<T> = core::result::Result<T, Error>;
/// ASN.1 error types
#[derive(Debug, Clone)]
pub enum Error {
/// Unexpected end of input
UnexpectedEof {
/// Position in input where EOF was encountered
position: usize,
},
/// Invalid tag encoding
InvalidTag {
/// Position in input where the invalid tag was found
position: usize,
/// The invalid byte value
byte: u8,
},
/// Invalid length encoding
InvalidLength {
/// Position in input where the invalid length was found
position: usize,
},
/// Length overflow during calculation
LengthOverflow,
/// Length exceeds configured maximum
LengthExceedsMaximum {
/// Position in input where the length was found
position: usize,
/// The length value that was too large
length: usize,
/// The configured maximum length
max_length: usize,
},
/// Maximum nesting depth exceeded
MaxDepthExceeded {
/// Position in input where the depth was exceeded
position: usize,
/// The configured maximum depth
max_depth: usize,
},
/// Unexpected tag (expected X, got Y)
UnexpectedTag {
/// Position in input where the unexpected tag was found
position: usize,
/// The tag that was expected
expected: Tag,
/// The tag that was actually found
actual: Tag,
},
/// Invalid encoding for type
#[cfg(feature = "std")]
InvalidEncoding {
/// Position in input where the invalid encoding was found
position: usize,
/// The tag of the invalid element
tag: Tag,
/// Description of why the encoding is invalid
reason: String,
},
#[cfg(not(feature = "std"))]
InvalidEncoding {
/// Position in input where the invalid encoding was found
position: usize,
/// The tag of the invalid element
tag: Tag,
/// Description of why the encoding is invalid
reason: &'static str,
},
/// Integer overflow when converting to native type
IntegerOverflow {
/// Position in input where the integer was found
position: usize,
},
/// Invalid OID encoding
InvalidOid {
/// Position in input where the OID was found
position: usize,
},
/// Invalid string encoding
#[cfg(feature = "std")]
InvalidString {
/// Position in input where the string was found
position: usize,
/// The type of string (e.g., "PrintableString", "UTF8String")
string_type: &'static str,
/// Description of why the string is invalid
reason: String,
},
#[cfg(not(feature = "std"))]
InvalidString {
/// Position in input where the string was found
position: usize,
/// The type of string (e.g., "PrintableString", "UTF8String")
string_type: &'static str,
/// Description of why the string is invalid
reason: &'static str,
},
/// Invalid time format
InvalidTime {
/// Position in input where the time was found
position: usize,
},
/// DER compliance violation
#[cfg(feature = "std")]
DerViolation {
/// Position in input where the violation was found
position: usize,
/// Description of the DER violation
reason: String,
},
#[cfg(not(feature = "std"))]
DerViolation {
/// Position in input where the violation was found
position: usize,
/// Description of the DER violation
reason: &'static str,
},
/// Indefinite length not allowed in DER
IndefiniteLengthInDer {
/// Position in input where indefinite length was found
position: usize,
},
/// CER compliance violation
#[cfg(feature = "std")]
CerViolation {
/// Position in input where the violation was found
position: usize,
/// Description of the CER violation
reason: String,
},
#[cfg(not(feature = "std"))]
CerViolation {
/// Position in input where the violation was found
position: usize,
/// Description of the CER violation
reason: &'static str,
},
/// Constructed encoder not properly finished
UnfinishedConstructed,
/// No constructed element to finish
NoConstructedToFinish,
/// Length too large to encode
LengthTooLarge,
/// Custom error
#[cfg(feature = "std")]
Custom(String),
#[cfg(not(feature = "std"))]
Custom(&'static str),
}
impl Error {
/// Get the position in the input where the error occurred, if available
pub fn position(&self) -> Option<usize> {
match self {
Error::UnexpectedEof { position }
| Error::InvalidTag { position, .. }
| Error::InvalidLength { position }
| Error::LengthExceedsMaximum { position, .. }
| Error::MaxDepthExceeded { position, .. }
| Error::UnexpectedTag { position, .. }
| Error::InvalidEncoding { position, .. }
| Error::IntegerOverflow { position }
| Error::InvalidOid { position }
| Error::InvalidString { position, .. }
| Error::InvalidTime { position }
| Error::DerViolation { position, .. }
| Error::IndefiniteLengthInDer { position }
| Error::CerViolation { position, .. } => Some(*position),
_ => None,
}
}
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::UnexpectedEof { position } => {
write!(f, "Unexpected end of input at position {}", position)
}
Error::InvalidTag { position, byte } => {
write!(
f,
"Invalid tag byte 0x{:02X} at position {}",
byte, position
)
}
Error::InvalidLength { position } => {
write!(f, "Invalid length encoding at position {}", position)
}
Error::LengthOverflow => {
write!(f, "Length calculation overflow")
}
Error::LengthExceedsMaximum {
position,
length,
max_length,
} => {
write!(
f,
"Length {} exceeds maximum {} at position {}",
length, max_length, position
)
}
Error::MaxDepthExceeded {
position,
max_depth,
} => {
write!(
f,
"Maximum nesting depth {} exceeded at position {}",
max_depth, position
)
}
Error::UnexpectedTag {
position,
expected,
actual,
} => {
write!(
f,
"Unexpected tag at position {}: expected {:?}, got {:?}",
position, expected, actual
)
}
Error::InvalidEncoding {
position,
tag,
reason,
} => {
write!(
f,
"Invalid encoding for {:?} at position {}: {}",
tag, position, reason
)
}
Error::IntegerOverflow { position } => {
write!(f, "Integer overflow at position {}", position)
}
Error::InvalidOid { position } => {
write!(f, "Invalid OID encoding at position {}", position)
}
Error::InvalidString {
position,
string_type,
reason,
} => {
write!(
f,
"Invalid {} at position {}: {}",
string_type, position, reason
)
}
Error::InvalidTime { position } => {
write!(f, "Invalid time format at position {}", position)
}
Error::DerViolation { position, reason } => {
write!(f, "DER violation at position {}: {}", position, reason)
}
Error::IndefiniteLengthInDer { position } => {
write!(
f,
"Indefinite length not allowed in DER at position {}",
position
)
}
Error::CerViolation { position, reason } => {
write!(f, "CER violation at position {}: {}", position, reason)
}
Error::UnfinishedConstructed => {
write!(f, "Constructed encoder not properly finished")
}
Error::NoConstructedToFinish => {
write!(f, "No constructed element to finish")
}
Error::LengthTooLarge => {
write!(f, "Length too large to encode")
}
Error::Custom(msg) => {
write!(f, "{}", msg)
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}