Skip to content

Commit 6aa1058

Browse files
authored
der: add ErrorKind::OidInvalid (#273)
Adds an error variant for propagating OIDs as discussed in #272. It's a little awkward for a few reasons: - There's already a unit `ErrorKind::Oid` variant. Since that's a little ambiguous, there's also a TODO to rename that variant to the more specific `ErrorKind::OidMalformed` in the next breaking release - Since `const-oid` is an optional dependency, the presence of this variant is conditional upon that feature being enabled. Fortunately the whole enum is marked `#[non_exhaustive]` (which is what even allows adding new variants in a non-breaking way), so hopefully the conditional presence of this variant won't be too problematic for downstream users The other problem is the `ObjectIdentifier` type is somewhat large. On a 64-bit machine, this increases the size of the overall `Error` type (which wraps `ErrorKind` with additional context) from 32-bytes to 64-bytes. That's not great, but probably worth it for the additional context being able to propagate an OID as an error provides.
1 parent 4ff3df4 commit 6aa1058

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

der/src/error.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ pub use core::str::Utf8Error;
55
use crate::{Length, Tag};
66
use core::{convert::Infallible, fmt};
77

8+
#[cfg(feature = "oid")]
9+
use crate::ObjectIdentifier;
10+
811
/// Result type.
912
pub type Result<T> = core::result::Result<T, Error>;
1013

11-
/// Error type
14+
/// Error type.
1215
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1316
pub struct Error {
14-
/// Kind of error
17+
/// Kind of error.
1518
kind: ErrorKind,
1619

17-
/// Position inside of message where error occurred
20+
/// Position inside of message where error occurred.
1821
position: Option<Length>,
1922
}
2023

2124
impl Error {
22-
/// Create a new [`Error`]
25+
/// Create a new [`Error`].
2326
pub fn new(kind: ErrorKind, position: Length) -> Error {
2427
Error {
2528
kind,
@@ -100,40 +103,50 @@ impl std::error::Error for ErrorKind {}
100103
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
101104
#[non_exhaustive]
102105
pub enum ErrorKind {
103-
/// Operation failed due to previous error
106+
/// Operation failed due to previous error.
104107
Failed,
105108

106-
/// Incorrect length for a given field
109+
/// Incorrect length for a given field.
107110
Length {
108-
/// Tag type of the value being decoded
111+
/// Tag type of the value being decoded.
109112
tag: Tag,
110113
},
111114

112-
/// Message is not canonically encoded
115+
/// Message is not canonically encoded.
113116
Noncanonical,
114117

115118
/// Malformed OID
119+
// TODO(tarcieri): rename this to `OidMalformed` in next breaking release
116120
Oid,
117121

118-
/// Integer overflow occurred (library bug!)
122+
/// Invalid/unknown OID.
123+
///
124+
/// This can be used by DER message parsers to report problems with a
125+
/// specific OID in the event it prevents the parsing of a message.
126+
#[cfg(feature = "oid")]
127+
#[cfg_attr(docsrs, doc(cfg(feature = "oid")))]
128+
OidInvalid(ObjectIdentifier),
129+
130+
/// Integer overflow occurred (library bug!).
119131
Overflow,
120132

121-
/// Message is longer than this library's internal limits support
133+
/// Message is longer than this library's internal limits support.
122134
Overlength,
123135

124-
/// Undecoded trailing data at end of message
136+
/// Undecoded trailing data at end of message.
125137
TrailingData {
126-
/// Length of the decoded data
138+
/// Length of the decoded data.
127139
decoded: Length,
128140

129-
/// Total length of the remaining data left in the buffer
141+
/// Total length of the remaining data left in the buffer.
130142
remaining: Length,
131143
},
132144

133-
/// Unexpected end-of-message/nested field when decoding
145+
/// Unexpected end-of-message/nested field when decoding.
134146
Truncated,
135147

136-
/// Encoded message is shorter than the expected length
148+
/// Encoded message is shorter than the expected length.
149+
///
137150
/// (i.e. an `Encodable` impl on a particular type has a buggy `encoded_len`)
138151
Underlength {
139152
/// Expected length
@@ -143,30 +156,30 @@ pub enum ErrorKind {
143156
actual: Length,
144157
},
145158

146-
/// Unexpected tag
159+
/// Unexpected tag.
147160
UnexpectedTag {
148161
/// Tag the decoder was expecting (if there is a single such tag).
149162
///
150163
/// `None` if multiple tags are expected/allowed, but the `actual` tag
151164
/// does not match any of them.
152165
expected: Option<Tag>,
153166

154-
/// Actual tag encountered in the message
167+
/// Actual tag encountered in the message.
155168
actual: Tag,
156169
},
157170

158-
/// Unknown/unsupported tag
171+
/// Unknown/unsupported tag.
159172
UnknownTag {
160-
/// Raw byte value of the tag
173+
/// Raw byte value of the tag.
161174
byte: u8,
162175
},
163176

164-
/// UTF-8 errors
177+
/// UTF-8 errors.
165178
Utf8(Utf8Error),
166179

167-
/// Unexpected value
180+
/// Unexpected value.
168181
Value {
169-
/// Tag of the unexpected value
182+
/// Tag of the unexpected value.
170183
tag: Tag,
171184
},
172185
}
@@ -186,6 +199,8 @@ impl fmt::Display for ErrorKind {
186199
ErrorKind::Length { tag } => write!(f, "incorrect length for {}", tag),
187200
ErrorKind::Noncanonical => write!(f, "DER is not canonically encoded"),
188201
ErrorKind::Oid => write!(f, "malformed OID"),
202+
#[cfg(feature = "oid")]
203+
ErrorKind::OidInvalid(oid) => write!(f, "invalid/unknown OID: {}", oid),
189204
ErrorKind::Overflow => write!(f, "integer overflow"),
190205
ErrorKind::Overlength => write!(f, "DER message is too long"),
191206
ErrorKind::TrailingData { decoded, remaining } => {

0 commit comments

Comments
 (0)