Skip to content

Commit c501837

Browse files
authored
Add custom error types support to the Decode and DecodeValue traits. (#1055)
1 parent ce2be74 commit c501837

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+410
-223
lines changed

Cargo.lock

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ x509-ocsp = { path = "./x509-ocsp" }
6464
p256 = { git = "https://github.com/RustCrypto/elliptic-curves.git" }
6565
# Pending a release of 0.11.0-pre.2
6666
whirlpool = { git = "https://github.com/RustCrypto/hashes.git" }
67+
68+
# https://github.com/RustCrypto/formats/pull/1055
69+
# https://github.com/RustCrypto/signatures/pull/809
70+
ecdsa = { git = "https://github.com/turbocool3r/signatures", branch = "custom-errors" }

crmf/src/pop.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ impl<'a> ::der::Choice<'a> for EncKeyWithIdChoice<'a> {
231231
}
232232
}
233233
impl<'a> ::der::Decode<'a> for EncKeyWithIdChoice<'a> {
234+
type Error = ::der::Error;
235+
234236
fn decode<R: ::der::Reader<'a>>(reader: &mut R) -> ::der::Result<Self> {
235237
let t = reader.peek_tag()?;
236238
if t == <Utf8StringRef<'a> as ::der::FixedTag>::TAG {

der/derive/src/choice.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ impl DeriveChoice {
9696
}
9797

9898
impl<#lifetime> ::der::Decode<#lifetime> for #ident<#lt_params> {
99+
type Error = ::der::Error;
100+
99101
fn decode<R: ::der::Reader<#lifetime>>(reader: &mut R) -> ::der::Result<Self> {
100102
use der::Reader as _;
101103
match reader.peek_tag()? {

der/derive/src/enumerated.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ impl DeriveEnumerated {
117117

118118
quote! {
119119
impl<#default_lifetime> ::der::DecodeValue<#default_lifetime> for #ident {
120+
type Error = ::der::Error;
121+
120122
fn decode_value<R: ::der::Reader<#default_lifetime>>(
121123
reader: &mut R,
122124
header: ::der::Header

der/derive/src/sequence.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ impl DeriveSequence {
8686

8787
quote! {
8888
impl #impl_generics ::der::DecodeValue<#lifetime> for #ident #ty_generics #where_clause {
89+
type Error = ::der::Error;
90+
8991
fn decode_value<R: ::der::Reader<#lifetime>>(
9092
reader: &mut R,
9193
header: ::der::Header,

der/src/asn1/any.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use crate::{
66
BytesRef, Choice, Decode, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind, Header, Length,
7-
Reader, Result, SliceReader, Tag, Tagged, ValueOrd, Writer,
7+
Reader, SliceReader, Tag, Tagged, ValueOrd, Writer,
88
};
99
use core::cmp::Ordering;
1010

@@ -40,7 +40,7 @@ impl<'a> AnyRef<'a> {
4040
};
4141

4242
/// Create a new [`AnyRef`] from the provided [`Tag`] and DER bytes.
43-
pub fn new(tag: Tag, bytes: &'a [u8]) -> Result<Self> {
43+
pub fn new(tag: Tag, bytes: &'a [u8]) -> Result<Self, Error> {
4444
let value = BytesRef::new(bytes).map_err(|_| ErrorKind::Length { tag })?;
4545
Ok(Self { tag, value })
4646
}
@@ -56,12 +56,12 @@ impl<'a> AnyRef<'a> {
5656
}
5757

5858
/// Attempt to decode this [`AnyRef`] type into the inner value.
59-
pub fn decode_as<T>(self) -> Result<T>
59+
pub fn decode_as<T>(self) -> Result<T, <T as DecodeValue<'a>>::Error>
6060
where
6161
T: Choice<'a> + DecodeValue<'a>,
6262
{
6363
if !T::can_decode(self.tag) {
64-
return Err(self.tag.unexpected_error(None));
64+
return Err(self.tag.unexpected_error(None).into());
6565
}
6666

6767
let header = Header {
@@ -71,7 +71,7 @@ impl<'a> AnyRef<'a> {
7171

7272
let mut decoder = SliceReader::new(self.value())?;
7373
let result = T::decode_value(&mut decoder, header)?;
74-
decoder.finish(result)
74+
Ok(decoder.finish(result)?)
7575
}
7676

7777
/// Is this value an ASN.1 `NULL` value?
@@ -81,14 +81,15 @@ impl<'a> AnyRef<'a> {
8181

8282
/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
8383
/// nested reader and calling the provided argument with it.
84-
pub fn sequence<F, T>(self, f: F) -> Result<T>
84+
pub fn sequence<F, T, E>(self, f: F) -> Result<T, E>
8585
where
86-
F: FnOnce(&mut SliceReader<'a>) -> Result<T>,
86+
F: FnOnce(&mut SliceReader<'a>) -> Result<T, E>,
87+
E: From<Error>,
8788
{
8889
self.tag.assert_eq(Tag::Sequence)?;
8990
let mut reader = SliceReader::new(self.value.as_slice())?;
9091
let result = f(&mut reader)?;
91-
reader.finish(result)
92+
Ok(reader.finish(result)?)
9293
}
9394
}
9495

@@ -99,14 +100,18 @@ impl<'a> Choice<'a> for AnyRef<'a> {
99100
}
100101

101102
impl<'a> Decode<'a> for AnyRef<'a> {
102-
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<AnyRef<'a>> {
103+
type Error = Error;
104+
105+
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<AnyRef<'a>, Error> {
103106
let header = Header::decode(reader)?;
104107
Self::decode_value(reader, header)
105108
}
106109
}
107110

108111
impl<'a> DecodeValue<'a> for AnyRef<'a> {
109-
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
112+
type Error = Error;
113+
114+
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Error> {
110115
Ok(Self {
111116
tag: header.tag,
112117
value: BytesRef::decode_value(reader, header)?,
@@ -115,11 +120,11 @@ impl<'a> DecodeValue<'a> for AnyRef<'a> {
115120
}
116121

117122
impl EncodeValue for AnyRef<'_> {
118-
fn value_len(&self) -> Result<Length> {
123+
fn value_len(&self) -> Result<Length, Error> {
119124
Ok(self.value.len())
120125
}
121126

122-
fn encode_value(&self, writer: &mut impl Writer) -> Result<()> {
127+
fn encode_value(&self, writer: &mut impl Writer) -> Result<(), Error> {
123128
writer.write(self.value())
124129
}
125130
}
@@ -131,7 +136,7 @@ impl Tagged for AnyRef<'_> {
131136
}
132137

133138
impl ValueOrd for AnyRef<'_> {
134-
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
139+
fn value_cmp(&self, other: &Self) -> Result<Ordering, Error> {
135140
self.value.der_cmp(&other.value)
136141
}
137142
}
@@ -145,7 +150,7 @@ impl<'a> From<AnyRef<'a>> for BytesRef<'a> {
145150
impl<'a> TryFrom<&'a [u8]> for AnyRef<'a> {
146151
type Error = Error;
147152

148-
fn try_from(bytes: &'a [u8]) -> Result<AnyRef<'a>> {
153+
fn try_from(bytes: &'a [u8]) -> Result<AnyRef<'a>, Error> {
149154
AnyRef::from_der(bytes)
150155
}
151156
}
@@ -175,7 +180,7 @@ mod allocating {
175180

176181
impl Any {
177182
/// Create a new [`Any`] from the provided [`Tag`] and DER bytes.
178-
pub fn new(tag: Tag, bytes: impl Into<Box<[u8]>>) -> Result<Self> {
183+
pub fn new(tag: Tag, bytes: impl Into<Box<[u8]>>) -> Result<Self, Error> {
179184
let value = BytesOwned::new(bytes)?;
180185

181186
// Ensure the tag and value are a valid `AnyRef`.
@@ -189,15 +194,15 @@ mod allocating {
189194
}
190195

191196
/// Attempt to decode this [`Any`] type into the inner value.
192-
pub fn decode_as<'a, T>(&'a self) -> Result<T>
197+
pub fn decode_as<'a, T>(&'a self) -> Result<T, <T as DecodeValue<'a>>::Error>
193198
where
194199
T: Choice<'a> + DecodeValue<'a>,
195200
{
196201
AnyRef::from(self).decode_as()
197202
}
198203

199204
/// Encode the provided type as an [`Any`] value.
200-
pub fn encode_from<T>(msg: &T) -> Result<Self>
205+
pub fn encode_from<T>(msg: &T) -> Result<Self, Error>
201206
where
202207
T: Tagged + EncodeValue,
203208
{
@@ -211,9 +216,10 @@ mod allocating {
211216

212217
/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
213218
/// nested reader and calling the provided argument with it.
214-
pub fn sequence<'a, F, T>(&'a self, f: F) -> Result<T>
219+
pub fn sequence<'a, F, T, E>(&'a self, f: F) -> Result<T, E>
215220
where
216-
F: FnOnce(&mut SliceReader<'a>) -> Result<T>,
221+
F: FnOnce(&mut SliceReader<'a>) -> Result<T, E>,
222+
E: From<Error>,
217223
{
218224
AnyRef::from(self).sequence(f)
219225
}
@@ -234,25 +240,29 @@ mod allocating {
234240
}
235241

236242
impl<'a> Decode<'a> for Any {
237-
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Self> {
243+
type Error = Error;
244+
245+
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Self, Error> {
238246
let header = Header::decode(reader)?;
239247
Self::decode_value(reader, header)
240248
}
241249
}
242250

243251
impl<'a> DecodeValue<'a> for Any {
244-
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
252+
type Error = Error;
253+
254+
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Error> {
245255
let value = reader.read_vec(header.length)?;
246256
Self::new(header.tag, value)
247257
}
248258
}
249259

250260
impl EncodeValue for Any {
251-
fn value_len(&self) -> Result<Length> {
261+
fn value_len(&self) -> Result<Length, Error> {
252262
Ok(self.value.len())
253263
}
254264

255-
fn encode_value(&self, writer: &mut impl Writer) -> Result<()> {
265+
fn encode_value(&self, writer: &mut impl Writer) -> Result<(), Error> {
256266
writer.write(self.value.as_slice())
257267
}
258268
}
@@ -271,7 +281,7 @@ mod allocating {
271281
}
272282

273283
impl ValueOrd for Any {
274-
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
284+
fn value_cmp(&self, other: &Self) -> Result<Ordering, Error> {
275285
self.value.der_cmp(&other.value)
276286
}
277287
}

der/src/asn1/bit_string.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ impl<'a> BitStringRef<'a> {
120120
impl_any_conversions!(BitStringRef<'a>, 'a);
121121

122122
impl<'a> DecodeValue<'a> for BitStringRef<'a> {
123+
type Error = Error;
124+
123125
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
124126
let header = Header {
125127
tag: header.tag,
@@ -309,6 +311,8 @@ mod allocating {
309311
impl_any_conversions!(BitString);
310312

311313
impl<'a> DecodeValue<'a> for BitString {
314+
type Error = Error;
315+
312316
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
313317
let inner_len = (header.length - Length::ONE)?;
314318
let unused_bits = reader.read_byte()?;
@@ -442,6 +446,8 @@ where
442446
T::Type: From<bool>,
443447
T::Type: core::ops::Shl<usize, Output = T::Type>,
444448
{
449+
type Error = Error;
450+
445451
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
446452
let position = reader.position();
447453
let bits = BitStringRef::decode_value(reader, header)?;

der/src/asn1/bmp_string.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ impl AsRef<[u8]> for BmpString {
9090
}
9191

9292
impl<'a> DecodeValue<'a> for BmpString {
93+
type Error = Error;
94+
9395
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
9496
Self::from_ucs2(reader.read_vec(header.length)?)
9597
}

der/src/asn1/boolean.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const TRUE_OCTET: u8 = 0b11111111;
1515
const FALSE_OCTET: u8 = 0b00000000;
1616

1717
impl<'a> DecodeValue<'a> for bool {
18+
type Error = Error;
19+
1820
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
1921
if header.length != Length::ONE {
2022
return Err(reader.error(ErrorKind::Length { tag: Self::TAG }));

0 commit comments

Comments
 (0)