Skip to content

Commit fb0634f

Browse files
authored
der: fix derive optional OCTET/BIT STRING on Option<&[u8]> (#1737)
* der: add test: optional OCTET/BIT STRING * der: fix derive for Option<&[u8]> and other Asn1Type's * der_derive: cargo clippy
1 parent f66f4c5 commit fb0634f

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

der/tests/derive.rs

+15
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,19 @@ mod sequence {
441441
pub typed: &'a [u8],
442442
#[asn1(context_specific = "0")]
443443
pub context_specific: bool,
444+
444445
#[asn1(optional = "true")]
445446
pub optional: Option<bool>,
447+
448+
#[asn1(type = "OCTET STRING", optional = "true")]
449+
pub optional_octet_string: Option<&'a [u8]>,
450+
451+
#[asn1(type = "BIT STRING", optional = "true")]
452+
pub optional_bit_string: Option<&'a [u8]>,
453+
454+
#[asn1(optional = "true")]
455+
pub optional_oid: Option<ObjectIdentifier>,
456+
446457
#[asn1(default = "default_false_example")]
447458
pub default: bool,
448459
#[asn1(type = "BIT STRING", context_specific = "1")]
@@ -467,6 +478,9 @@ mod sequence {
467478
#[test]
468479
fn type_combinations_instance() {
469480
let obj = TypeCheckExpandedSequenceFieldAttributeCombinations {
481+
optional: Some(true),
482+
optional_octet_string: Some(&[0xAA, 0xBB]),
483+
optional_bit_string: Some(&[0xCC, 0xDD]),
470484
context_specific_optional: Some(true),
471485
typed_context_specific: &[0, 1],
472486
typed_context_specific_optional_bits: Some(&[2, 3]),
@@ -477,6 +491,7 @@ mod sequence {
477491
};
478492

479493
let der_encoded = obj.to_der().unwrap();
494+
480495
let obj_decoded =
481496
TypeCheckExpandedSequenceFieldAttributeCombinations::from_der(&der_encoded).unwrap();
482497
assert_eq!(obj, obj_decoded);

der_derive/src/asn1_type.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,13 @@ impl Asn1Type {
5454

5555
/// Get a `der::Decoder` object for a particular ASN.1 type
5656
pub fn decoder(self) -> TokenStream {
57-
match self {
58-
Asn1Type::BitString => quote!(::der::asn1::BitStringRef::decode(reader)?),
59-
Asn1Type::Ia5String => quote!(::der::asn1::Ia5StringRef::decode(reader)?),
60-
Asn1Type::GeneralizedTime => quote!(::der::asn1::GeneralizedTime::decode(reader)?),
61-
Asn1Type::OctetString => quote!(::der::asn1::OctetStringRef::decode(reader)?),
62-
Asn1Type::PrintableString => quote!(::der::asn1::PrintableStringRef::decode(reader)?),
63-
Asn1Type::TeletexString => quote!(::der::asn1::TeletexStringRef::decode(reader)?),
64-
Asn1Type::VideotexString => quote!(::der::asn1::VideotexStringRef::decode(reader)?),
65-
Asn1Type::UtcTime => quote!(::der::asn1::UtcTime::decode(reader)?),
66-
Asn1Type::Utf8String => quote!(::der::asn1::Utf8StringRef::decode(reader)?),
67-
}
57+
let type_path = self.type_path();
58+
quote!(#type_path::decode(reader)?)
59+
}
60+
/// Get a `der::Decoder` optional object for a particular ASN.1 type
61+
pub fn decoder_optional(self) -> TokenStream {
62+
let type_path = self.type_path();
63+
quote!(Option::<#type_path>::decode(reader)?)
6864
}
6965

7066
/// Get a `der::Encoder` object for a particular ASN.1 type

der_derive/src/attributes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ impl FieldAttrs {
299299
Option::<#type_params>::decode(reader)?.unwrap_or_else(#default),
300300
}
301301
})
302+
} else if self.is_optional() {
303+
self.asn1_type
304+
.map(|ty| ty.decoder_optional())
305+
.unwrap_or_else(|| quote!(reader.decode()?))
302306
} else {
303307
self.asn1_type
304308
.map(|ty| ty.decoder())

0 commit comments

Comments
 (0)