1#![allow(unknown_lints)]
2#![allow(non_local_definitions)] use crate::{Class, Tag};
5use alloc::str;
6use alloc::string;
7#[cfg(not(feature = "std"))]
8use alloc::string::{String, ToString};
9use core::fmt;
10use displaydoc::Display;
11use nom::error::{ErrorKind, FromExternalError, ParseError};
12use nom::{IResult, Input};
13#[cfg(feature = "std")]
14use std::io;
15use thiserror::Error;
16
17#[derive(Clone, Copy, Debug, Display, PartialEq, Eq, Error)]
18pub enum DerConstraint {
20 IndefiniteLength,
22 Constructed,
24 NotConstructed,
26 MissingTimeZone,
28 MissingSeconds,
30 UnusedBitsNotZero,
32 InvalidBoolean,
34 IntegerEmpty,
36 IntegerLeadingZeroes,
38 IntegerLeadingFF,
40}
41
42#[derive(Clone, Debug, Display, PartialEq, Eq, Error)]
45pub enum Error {
46 BerTypeError,
48 BerValueError,
50 InvalidLength,
52 InvalidValue { tag: Tag, msg: String },
54 InvalidTag,
56 UnknownTag(u32),
58 UnexpectedTag { expected: Option<Tag>, actual: Tag },
60 UnexpectedClass {
62 expected: Option<Class>,
63 actual: Class,
64 },
65
66 IndefiniteLengthUnexpected,
68
69 ConstructExpected,
71 ConstructUnexpected,
73
74 IntegerTooLarge,
76 IntegerNegative,
78 BerMaxDepth,
80
81 StringInvalidCharset,
83 InvalidDateTime,
85
86 DerConstraintFailed(DerConstraint),
88
89 LifetimeError,
91 Unsupported,
93
94 Incomplete(nom::Needed),
96
97 NomError(ErrorKind),
99}
100
101#[derive(Debug, Error, PartialEq, Eq)]
102pub struct BerError<I: Input> {
103 input: I,
105 inner_error: InnerError,
107}
108
109impl<I: Input> BerError<I> {
110 #[inline]
112 pub const fn new(input: I, inner: InnerError) -> Self {
113 Self {
114 input,
115 inner_error: inner,
116 }
117 }
118
119 pub const fn input(&self) -> &I {
121 &self.input
122 }
123
124 pub fn inner(&self) -> &InnerError {
126 &self.inner_error
127 }
128
129 #[inline]
130 pub const fn nom_err(input: I, inner_error: InnerError) -> nom::Err<Self> {
131 nom::Err::Error(Self { input, inner_error })
132 }
133
134 pub fn convert(input: I) -> impl Fn(InnerError) -> nom::Err<Self>
141 where
142 I: Clone,
143 {
144 move |inner_error| Self::nom_err(input.clone(), inner_error)
145 }
146
147 pub fn convert_into<E: From<Self>>(input: I) -> impl Fn(InnerError) -> nom::Err<E>
151 where
152 I: Clone,
153 {
154 move |inner_error| nom::Err::Error(Self::new(input.clone(), inner_error).into())
155 }
156
157 #[inline]
159 pub const fn invalid_value(input: I, tag: Tag, msg: String) -> Self {
160 Self::new(input, InnerError::InvalidValue { tag, msg })
161 }
162
163 #[inline]
171 pub const fn unexpected_tag(input: I, expected: Option<Tag>, actual: Tag) -> Self {
172 Self::new(input, InnerError::UnexpectedTag { expected, actual })
173 }
174
175 #[inline]
177 pub const fn unexpected_class(input: I, expected: Option<Class>, actual: Class) -> Self {
178 Self::new(input, InnerError::UnexpectedClass { expected, actual })
179 }
180
181 #[inline]
183 pub const fn incomplete(input: I, n: nom::Needed) -> Self {
184 Self {
185 input,
186 inner_error: InnerError::Incomplete(n),
187 }
188 }
189}
190
191impl<'i> BerError<crate::Input<'i>> {
192 pub const fn err_input<'a: 'i, 'r>(input: &'r crate::Input<'a>, inner: InnerError) -> Self {
194 BerError::new(input.const_clone(), inner)
195 }
196
197 pub const fn nom_err_input<'a: 'i, 'r>(
199 input: &'r crate::Input<'a>,
200 inner: InnerError,
201 ) -> nom::Err<Self> {
202 nom::Err::Error(BerError::new(input.const_clone(), inner))
203 }
204
205 pub fn consume(self) -> (crate::Input<'i>, InnerError) {
207 (self.input, self.inner_error)
208 }
209}
210
211impl<I: Input + fmt::Display> fmt::Display for BerError<I> {
212 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213 write!(f, "BerError: input={} error={}", self.input(), self.inner())
214 }
215}
216
217impl<I: Input> ParseError<I> for BerError<I> {
218 fn from_error_kind(input: I, kind: ErrorKind) -> Self {
219 Self {
220 input,
221 inner_error: InnerError::Nom(kind),
222 }
223 }
224
225 fn append(_: I, _z: ErrorKind, other: Self) -> Self {
226 other
228 }
229}
230
231impl From<Error> for BerError<crate::Input<'_>> {
232 fn from(value: Error) -> Self {
233 let inner = InnerError::from(value);
234 BerError::new(crate::Input::from_slice(&[]), inner)
235 }
236}
237
238impl<'a, 'b> From<(crate::Input<'b>, Error)> for BerError<crate::Input<'a>>
239where
240 'b: 'a,
241{
242 fn from(value: (crate::Input<'b>, Error)) -> Self {
243 Self {
244 input: value.0,
245 inner_error: InnerError::from(value.1),
246 }
247 }
248}
249
250impl<'a, 'b> From<(crate::Input<'b>, BerError<crate::Input<'b>>)> for BerError<crate::Input<'a>>
251where
252 'b: 'a,
253{
254 fn from(value: (crate::Input<'b>, BerError<crate::Input<'b>>)) -> Self {
255 value.1
257 }
262}
263
264#[derive(Clone, Debug, Display, PartialEq, Eq, Error)]
267#[non_exhaustive]
268pub enum InnerError {
269 BerTypeError,
271 BerValueError,
273 InvalidLength,
275 InvalidValue { tag: Tag, msg: String },
277 InvalidTag,
279 UnknownTag(u32),
281 UnexpectedTag { expected: Option<Tag>, actual: Tag },
283 UnexpectedClass {
285 expected: Option<Class>,
286 actual: Class,
287 },
288
289 IndefiniteLengthUnexpected,
291
292 ConstructExpected,
294 ConstructUnexpected,
296
297 IntegerTooLarge,
299 IntegerNegative,
301 BerMaxDepth,
303
304 StringInvalidCharset,
306 InvalidDateTime,
308
309 DerConstraintFailed(DerConstraint),
311
312 Nom(ErrorKind),
314
315 LifetimeError,
317 Unsupported,
319
320 Incomplete(nom::Needed),
322}
323
324impl InnerError {
325 pub fn invalid_value(tag: Tag, msg: &str) -> Self {
326 Self::InvalidValue {
327 tag,
328 msg: msg.to_string(),
329 }
330 }
331}
332
333impl From<str::Utf8Error> for InnerError {
334 fn from(_: str::Utf8Error) -> Self {
335 InnerError::StringInvalidCharset
336 }
337}
338
339impl From<string::FromUtf8Error> for InnerError {
340 fn from(_: string::FromUtf8Error) -> Self {
341 InnerError::StringInvalidCharset
342 }
343}
344
345impl From<string::FromUtf16Error> for InnerError {
346 fn from(_: string::FromUtf16Error) -> Self {
347 InnerError::StringInvalidCharset
348 }
349}
350
351impl From<Error> for InnerError {
352 fn from(value: Error) -> Self {
353 match value {
354 Error::BerTypeError => Self::BerTypeError,
355 Error::BerValueError => Self::BerValueError,
356 Error::InvalidLength => Self::InvalidLength,
357 Error::InvalidValue { tag, msg } => Self::InvalidValue { tag, msg },
358 Error::InvalidTag => Self::InvalidTag,
359 Error::UnknownTag(tag) => Self::UnknownTag(tag),
360 Error::UnexpectedTag { expected, actual } => Self::UnexpectedTag { expected, actual },
361 Error::UnexpectedClass { expected, actual } => {
362 Self::UnexpectedClass { expected, actual }
363 }
364 Error::IndefiniteLengthUnexpected => Self::IndefiniteLengthUnexpected,
365 Error::ConstructExpected => Self::ConstructExpected,
366 Error::ConstructUnexpected => Self::ConstructUnexpected,
367 Error::IntegerTooLarge => Self::IntegerTooLarge,
368 Error::IntegerNegative => Self::IntegerNegative,
369 Error::BerMaxDepth => Self::BerMaxDepth,
370 Error::StringInvalidCharset => Self::StringInvalidCharset,
371 Error::InvalidDateTime => Self::InvalidDateTime,
372 Error::DerConstraintFailed(der_constraint) => Self::DerConstraintFailed(der_constraint),
373 Error::LifetimeError => Self::LifetimeError,
374 Error::Unsupported => Self::Unsupported,
375 Error::Incomplete(needed) => Self::Incomplete(needed),
376 Error::NomError(error_kind) => Self::Nom(error_kind),
377 }
378 }
379}
380
381impl Error {
382 #[inline]
384 pub const fn invalid_value(tag: Tag, msg: String) -> Self {
385 Self::InvalidValue { tag, msg }
386 }
387
388 #[inline]
390 pub const fn unexpected_class(expected: Option<Class>, actual: Class) -> Self {
391 Self::UnexpectedClass { expected, actual }
392 }
393
394 #[inline]
396 pub const fn unexpected_tag(expected: Option<Tag>, actual: Tag) -> Self {
397 Self::UnexpectedTag { expected, actual }
398 }
399
400 pub fn from_nom_berr(e: nom::Err<BerError<crate::Input<'_>>>) -> Self {
402 match e {
403 nom::Err::Incomplete(n) => Self::Incomplete(n),
404 nom::Err::Error(e) | nom::Err::Failure(e) => e.inner_error.into(),
405 }
406 }
407}
408
409impl<I> ParseError<I> for Error {
410 fn from_error_kind(_input: I, kind: ErrorKind) -> Self {
411 Error::NomError(kind)
412 }
413 fn append(_input: I, kind: ErrorKind, _other: Self) -> Self {
414 Error::NomError(kind)
415 }
416}
417
418impl From<Error> for nom::Err<Error> {
419 fn from(e: Error) -> Self {
420 nom::Err::Error(e)
421 }
422}
423
424impl From<str::Utf8Error> for Error {
425 fn from(_: str::Utf8Error) -> Self {
426 Error::StringInvalidCharset
427 }
428}
429
430impl From<string::FromUtf8Error> for Error {
431 fn from(_: string::FromUtf8Error) -> Self {
432 Error::StringInvalidCharset
433 }
434}
435
436impl From<string::FromUtf16Error> for Error {
437 fn from(_: string::FromUtf16Error) -> Self {
438 Error::StringInvalidCharset
439 }
440}
441
442impl From<nom::Err<Error>> for Error {
443 fn from(e: nom::Err<Error>) -> Self {
444 match e {
445 nom::Err::Incomplete(n) => Self::Incomplete(n),
446 nom::Err::Error(e) | nom::Err::Failure(e) => e,
447 }
448 }
449}
450
451impl<I: Input> From<BerError<I>> for Error {
452 fn from(value: BerError<I>) -> Self {
453 Self::from(value.inner_error)
454 }
455}
456
457impl From<InnerError> for Error {
458 fn from(value: InnerError) -> Self {
459 match value {
460 InnerError::BerTypeError => Self::BerTypeError,
461 InnerError::BerValueError => Self::BerValueError,
462 InnerError::InvalidLength => Self::InvalidLength,
463 InnerError::InvalidValue { tag, msg } => Self::InvalidValue { tag, msg },
464 InnerError::InvalidTag => Self::InvalidTag,
465 InnerError::UnknownTag(t) => Self::UnknownTag(t),
466 InnerError::UnexpectedTag { expected, actual } => {
467 Self::UnexpectedTag { expected, actual }
468 }
469 InnerError::UnexpectedClass { expected, actual } => {
470 Self::unexpected_class(expected, actual)
471 }
472 InnerError::IndefiniteLengthUnexpected => Self::IndefiniteLengthUnexpected,
473 InnerError::ConstructExpected => Self::ConstructExpected,
474 InnerError::ConstructUnexpected => Self::ConstructUnexpected,
475 InnerError::IntegerTooLarge => Self::IntegerTooLarge,
476 InnerError::IntegerNegative => Self::IntegerNegative,
477 InnerError::BerMaxDepth => Self::BerMaxDepth,
478 InnerError::StringInvalidCharset => Self::StringInvalidCharset,
479 InnerError::InvalidDateTime => Self::InvalidDateTime,
480 InnerError::DerConstraintFailed(der_constraint) => {
481 Self::DerConstraintFailed(der_constraint)
482 }
483 InnerError::Nom(error_kind) => Self::NomError(error_kind),
484 InnerError::LifetimeError => Self::LifetimeError,
485 InnerError::Unsupported => Self::Unsupported,
486 InnerError::Incomplete(n) => Self::Incomplete(n),
487 }
488 }
489}
490
491impl<I, E> FromExternalError<I, E> for Error {
492 fn from_external_error(_input: I, kind: ErrorKind, _e: E) -> Error {
493 Error::NomError(kind)
494 }
495}
496
497pub fn from_nom_error<E, F>(e: nom::Err<E>) -> F
499where
500 F: From<E> + From<Error>,
501{
502 match e {
503 nom::Err::Error(e) | nom::Err::Failure(e) => F::from(e),
504 nom::Err::Incomplete(n) => F::from(Error::Incomplete(n)),
505 }
506}
507
508pub fn from_nom_bererror<'a, E, F>(input: crate::Input<'a>, e: nom::Err<E>) -> F
512where
513 F: Sized + From<(crate::Input<'a>, E)> + From<Error>,
514{
515 match e {
516 nom::Err::Error(e) | nom::Err::Failure(e) => F::from((input, e)),
517 nom::Err::Incomplete(n) => F::from(Error::Incomplete(n)),
518 }
519}
520
521pub type ParseResult<'a, T, E = Error> = IResult<&'a [u8], T, E>;
523
524pub type Result<T, E = Error> = core::result::Result<T, E>;
526
527#[cfg(feature = "std")]
529#[derive(Debug, Error)]
530pub enum SerializeError {
531 #[error("ASN.1 error: {0:?}")]
532 ASN1Error(#[from] Error),
533
534 #[error("Invalid Class {class:}")]
535 InvalidClass { class: u8 },
536
537 #[error("Invalid Length")]
538 InvalidLength,
539
540 #[error("I/O error: {0:?}")]
541 IOError(#[from] io::Error),
542}
543
544#[cfg(feature = "std")]
545pub type SerializeResult<T> = std::result::Result<T, SerializeError>;