Coverage Report

Created: 2025-11-16 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/asn1-rs-0.7.0/src/error.rs
Line
Count
Source
1
#![allow(unknown_lints)]
2
#![allow(non_local_definitions)] // false positive for displaydoc::Display: https://github.com/yaahc/displaydoc/issues/46
3
4
use crate::{Class, Tag};
5
use alloc::str;
6
use alloc::string;
7
#[cfg(not(feature = "std"))]
8
use alloc::string::String;
9
use displaydoc::Display;
10
use nom::error::{ErrorKind, FromExternalError, ParseError};
11
use nom::IResult;
12
#[cfg(feature = "std")]
13
use std::io;
14
use thiserror::Error;
15
16
#[derive(Clone, Copy, Debug, Display, PartialEq, Eq, Error)]
17
/// Error types for DER constraints
18
pub enum DerConstraint {
19
    /// Indefinite length not allowed
20
    IndefiniteLength,
21
    /// Object must not be constructed
22
    Constructed,
23
    /// Object must be constructed
24
    NotConstructed,
25
    /// DateTime object is missing timezone
26
    MissingTimeZone,
27
    /// DateTime object is missing seconds
28
    MissingSeconds,
29
    /// Bitstring unused bits must be set to zero
30
    UnusedBitsNotZero,
31
    /// Boolean value must be 0x00 of 0xff
32
    InvalidBoolean,
33
    /// Integer must not be empty
34
    IntegerEmpty,
35
    /// Leading zeroes in Integer encoding
36
    IntegerLeadingZeroes,
37
    /// Leading 0xff in negative Integer encoding
38
    IntegerLeadingFF,
39
}
40
41
/// The error type for operations of the [`FromBer`](crate::FromBer),
42
/// [`FromDer`](crate::FromDer), and associated traits.
43
#[derive(Clone, Debug, Display, PartialEq, Eq, Error)]
44
pub enum Error {
45
    /// BER object does not have the expected type
46
    BerTypeError,
47
    /// BER object does not have the expected value
48
    BerValueError,
49
    /// Invalid Length
50
    InvalidLength,
51
    /// Invalid Value when parsing object with tag {tag:?} {msg:}
52
    InvalidValue { tag: Tag, msg: String },
53
    /// Invalid Tag
54
    InvalidTag,
55
    /// Unknown tag: {0:?}
56
    UnknownTag(u32),
57
    /// Unexpected Tag (expected: {expected:?}, actual: {actual:?})
58
    UnexpectedTag { expected: Option<Tag>, actual: Tag },
59
    /// Unexpected Class (expected: {expected:?}, actual: {actual:?})
60
    UnexpectedClass {
61
        expected: Option<Class>,
62
        actual: Class,
63
    },
64
65
    /// Indefinite length not allowed
66
    IndefiniteLengthUnexpected,
67
68
    /// DER object was expected to be constructed (and found to be primitive)
69
    ConstructExpected,
70
    /// DER object was expected to be primitive (and found to be constructed)
71
    ConstructUnexpected,
72
73
    /// Integer too large to fit requested type
74
    IntegerTooLarge,
75
    /// BER integer is negative, while an unsigned integer was requested
76
    IntegerNegative,
77
    /// BER recursive parsing reached maximum depth
78
    BerMaxDepth,
79
80
    /// Invalid encoding or forbidden characters in string
81
    StringInvalidCharset,
82
    /// Invalid Date or Time
83
    InvalidDateTime,
84
85
    /// DER Failed constraint: {0:?}
86
    DerConstraintFailed(DerConstraint),
87
88
    /// Requesting borrowed data from a temporary object
89
    LifetimeError,
90
    /// Feature is not yet implemented
91
    Unsupported,
92
93
    /// incomplete data, missing: {0:?}
94
    Incomplete(nom::Needed),
95
96
    /// nom error: {0:?}
97
    NomError(ErrorKind),
98
}
99
100
impl Error {
101
    /// Build an error from the provided invalid value
102
    #[inline]
103
0
    pub const fn invalid_value(tag: Tag, msg: String) -> Self {
104
0
        Self::InvalidValue { tag, msg }
105
0
    }
Unexecuted instantiation: <asn1_rs::error::Error>::invalid_value
Unexecuted instantiation: <asn1_rs::error::Error>::invalid_value
106
107
    /// Build an error from the provided unexpected class
108
    #[inline]
109
0
    pub const fn unexpected_class(expected: Option<Class>, actual: Class) -> Self {
110
0
        Self::UnexpectedClass { expected, actual }
111
0
    }
Unexecuted instantiation: <asn1_rs::error::Error>::unexpected_class
Unexecuted instantiation: <asn1_rs::error::Error>::unexpected_class
112
113
    /// Build an error from the provided unexpected tag
114
    #[inline]
115
0
    pub const fn unexpected_tag(expected: Option<Tag>, actual: Tag) -> Self {
116
0
        Self::UnexpectedTag { expected, actual }
117
0
    }
Unexecuted instantiation: <asn1_rs::error::Error>::unexpected_tag
Unexecuted instantiation: <asn1_rs::error::Error>::unexpected_tag
118
}
119
120
impl<'a> ParseError<&'a [u8]> for Error {
121
0
    fn from_error_kind(_input: &'a [u8], kind: ErrorKind) -> Self {
122
0
        Error::NomError(kind)
123
0
    }
124
0
    fn append(_input: &'a [u8], kind: ErrorKind, _other: Self) -> Self {
125
0
        Error::NomError(kind)
126
0
    }
127
}
128
129
impl From<Error> for nom::Err<Error> {
130
0
    fn from(e: Error) -> Self {
131
0
        nom::Err::Error(e)
132
0
    }
133
}
134
135
impl From<str::Utf8Error> for Error {
136
0
    fn from(_: str::Utf8Error) -> Self {
137
0
        Error::StringInvalidCharset
138
0
    }
139
}
140
141
impl From<string::FromUtf8Error> for Error {
142
0
    fn from(_: string::FromUtf8Error) -> Self {
143
0
        Error::StringInvalidCharset
144
0
    }
145
}
146
147
impl From<string::FromUtf16Error> for Error {
148
0
    fn from(_: string::FromUtf16Error) -> Self {
149
0
        Error::StringInvalidCharset
150
0
    }
151
}
152
153
impl From<nom::Err<Error>> for Error {
154
0
    fn from(e: nom::Err<Error>) -> Self {
155
0
        match e {
156
0
            nom::Err::Incomplete(n) => Self::Incomplete(n),
157
0
            nom::Err::Error(e) | nom::Err::Failure(e) => e,
158
        }
159
0
    }
160
}
161
162
impl<I, E> FromExternalError<I, E> for Error {
163
0
    fn from_external_error(_input: I, kind: ErrorKind, _e: E) -> Error {
164
0
        Error::NomError(kind)
165
0
    }
Unexecuted instantiation: <asn1_rs::error::Error as nom::error::FromExternalError<&[u8], asn1_rs::error::Error>>::from_external_error
Unexecuted instantiation: <asn1_rs::error::Error as nom::error::FromExternalError<_, _>>::from_external_error
166
}
167
168
/// Flatten all `nom::Err` variants error into a single error type
169
0
pub fn from_nom_error<E, F>(e: nom::Err<E>) -> F
170
0
where
171
0
    F: From<E> + From<Error>,
172
{
173
0
    match e {
174
0
        nom::Err::Error(e) | nom::Err::Failure(e) => F::from(e),
175
0
        nom::Err::Incomplete(n) => F::from(Error::Incomplete(n)),
176
    }
177
0
}
Unexecuted instantiation: asn1_rs::error::from_nom_error::<asn1_rs::error::Error, x509_parser::error::X509Error>
Unexecuted instantiation: asn1_rs::error::from_nom_error::<x509_parser::error::X509Error, x509_parser::error::X509Error>
Unexecuted instantiation: asn1_rs::error::from_nom_error::<_, _>
178
179
/// Holds the result of BER/DER serialization functions
180
pub type ParseResult<'a, T, E = Error> = IResult<&'a [u8], T, E>;
181
182
/// A specialized `Result` type for all operations from this crate.
183
pub type Result<T, E = Error> = core::result::Result<T, E>;
184
185
/// The error type for serialization operations of the [`ToDer`](crate::ToDer) trait.
186
#[cfg(feature = "std")]
187
#[derive(Debug, Error)]
188
pub enum SerializeError {
189
    #[error("ASN.1 error: {0:?}")]
190
    ASN1Error(#[from] Error),
191
192
    #[error("Invalid Class {class:}")]
193
    InvalidClass { class: u8 },
194
195
    #[error("Invalid Length")]
196
    InvalidLength,
197
198
    #[error("I/O error: {0:?}")]
199
    IOError(#[from] io::Error),
200
}
201
202
#[cfg(feature = "std")]
203
/// Holds the result of BER/DER encoding functions
204
pub type SerializeResult<T> = std::result::Result<T, SerializeError>;