Skip to content

Commit c9aa9e5

Browse files
authored
Added OctetStringEncoded to allow representing structures like OCSPResponse or X.509 Extensions that encode a TLV inside of an OCTET STRING (#339)
1 parent 5147cac commit c9aa9e5

File tree

6 files changed

+82
-15
lines changed

6 files changed

+82
-15
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "asn1"
3-
version = "0.12.2"
3+
version = "0.12.3"
44
authors = ["Alex Gaynor <[email protected]>"]
55
repository = "https://github.com/alex/rust-asn1"
66
keywords = ["asn1"]
@@ -17,7 +17,7 @@ derive = ["asn1_derive"]
1717

1818
[dependencies]
1919
chrono = { version = "0.4.20", default-features = false, features = ["alloc"] }
20-
asn1_derive = { path = "asn1_derive/", version = "0.12.2", optional = true }
20+
asn1_derive = { path = "asn1_derive/", version = "0.12.3", optional = true }
2121

2222
[dev-dependencies]
2323
libc = "0.2.11"

asn1_derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "asn1_derive"
3-
version = "0.12.2"
3+
version = "0.12.3"
44
authors = ["Alex Gaynor <[email protected]>"]
55
repository = "https://github.com/alex/rust-asn1"
66
license = "BSD-3-Clause"

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ pub use crate::parser::{
123123
pub use crate::tag::Tag;
124124
pub use crate::types::{
125125
Asn1Readable, Asn1Writable, BMPString, BigInt, BigUint, Choice1, Choice2, Choice3, Enumerated,
126-
GeneralizedTime, IA5String, Null, PrintableString, Sequence, SequenceOf, SequenceOfWriter,
127-
SequenceWriter, SetOf, SetOfWriter, SimpleAsn1Readable, SimpleAsn1Writable, Tlv,
128-
UniversalString, UtcTime, Utf8String, VisibleString,
126+
GeneralizedTime, IA5String, Null, OctetStringEncoded, PrintableString, Sequence, SequenceOf,
127+
SequenceOfWriter, SequenceWriter, SetOf, SetOfWriter, SimpleAsn1Readable, SimpleAsn1Writable,
128+
Tlv, UniversalString, UtcTime, Utf8String, VisibleString,
129129
};
130130
#[cfg(feature = "const-generics")]
131131
pub use crate::types::{Explicit, Implicit};

src/parser.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ mod tests {
344344
use crate::types::Asn1Readable;
345345
use crate::{
346346
BMPString, BigInt, BigUint, BitString, Choice1, Choice2, Choice3, Enumerated,
347-
GeneralizedTime, IA5String, ObjectIdentifier, OwnedBitString, ParseError, ParseErrorKind,
348-
ParseLocation, ParseResult, PrintableString, Sequence, SequenceOf, SetOf, Tag, Tlv,
349-
UniversalString, UtcTime, Utf8String, VisibleString,
347+
GeneralizedTime, IA5String, ObjectIdentifier, OctetStringEncoded, OwnedBitString,
348+
ParseError, ParseErrorKind, ParseLocation, ParseResult, PrintableString, Sequence,
349+
SequenceOf, SetOf, Tag, Tlv, UniversalString, UtcTime, Utf8String, VisibleString,
350350
};
351351
#[cfg(feature = "const-generics")]
352352
use crate::{Explicit, Implicit};
@@ -674,6 +674,26 @@ mod tests {
674674
]);
675675
}
676676

677+
#[test]
678+
fn test_octet_string_encoded() {
679+
assert_parses::<OctetStringEncoded<bool>>(&[
680+
(Ok(OctetStringEncoded::new(true)), b"\x04\x03\x01\x01\xff"),
681+
(Ok(OctetStringEncoded::new(false)), b"\x04\x03\x01\x01\x00"),
682+
(
683+
Err(ParseError::new(ParseErrorKind::UnexpectedTag {
684+
actual: Tag::primitive(0x03),
685+
})),
686+
b"\x03\x00",
687+
),
688+
(
689+
Err(ParseError::new(ParseErrorKind::UnexpectedTag {
690+
actual: Tag::primitive(0x02),
691+
})),
692+
b"\x04\x02\x02\x00",
693+
),
694+
])
695+
}
696+
677697
#[test]
678698
fn test_parse_int_i64() {
679699
assert_parses::<i64>(&[

src/types.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,39 @@ impl<'a> SimpleAsn1Writable for &'a [u8] {
179179
}
180180
}
181181

182+
/// Represents values that are encoded as an `OCTET STRING` containing an
183+
/// encoded TLV, of type `T`.
184+
#[derive(PartialEq, Debug)]
185+
pub struct OctetStringEncoded<T>(T);
186+
187+
impl<T> OctetStringEncoded<T> {
188+
pub fn new(v: T) -> OctetStringEncoded<T> {
189+
OctetStringEncoded(v)
190+
}
191+
192+
pub fn get(&self) -> &T {
193+
&self.0
194+
}
195+
196+
pub fn into_inner(self) -> T {
197+
self.0
198+
}
199+
}
200+
201+
impl<'a, T: Asn1Readable<'a>> SimpleAsn1Readable<'a> for OctetStringEncoded<T> {
202+
const TAG: Tag = Tag::primitive(0x04);
203+
fn parse_data(data: &'a [u8]) -> ParseResult<Self> {
204+
Ok(OctetStringEncoded::new(parse_single(data)?))
205+
}
206+
}
207+
208+
impl<T: Asn1Writable> SimpleAsn1Writable for OctetStringEncoded<T> {
209+
const TAG: Tag = Tag::primitive(0x04);
210+
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
211+
self.0.write(&mut Writer::new(dest))
212+
}
213+
}
214+
182215
/// Type for use with `Parser.read_element` and `Writer.write_element` for
183216
/// handling ASN.1 `PrintableString`. A `PrintableString` contains an `&str`
184217
/// with only valid characers.
@@ -1463,9 +1496,9 @@ impl<'a, T: Asn1Writable, const TAG: u32> SimpleAsn1Writable for Explicit<'a, T,
14631496
#[cfg(test)]
14641497
mod tests {
14651498
use crate::{
1466-
parse_single, BigInt, BigUint, Enumerated, GeneralizedTime, IA5String, ParseError,
1467-
ParseErrorKind, PrintableString, SequenceOf, SequenceOfWriter, SetOf, SetOfWriter, Tag,
1468-
Tlv, UtcTime, Utf8String, VisibleString,
1499+
parse_single, BigInt, BigUint, Enumerated, GeneralizedTime, IA5String, OctetStringEncoded,
1500+
ParseError, ParseErrorKind, PrintableString, SequenceOf, SequenceOfWriter, SetOf,
1501+
SetOfWriter, Tag, Tlv, UtcTime, Utf8String, VisibleString,
14691502
};
14701503
#[cfg(feature = "const-generics")]
14711504
use crate::{Explicit, Implicit};
@@ -1477,6 +1510,12 @@ mod tests {
14771510
#[cfg(feature = "std")]
14781511
use std::collections::hash_map::DefaultHasher;
14791512

1513+
#[test]
1514+
fn test_octet_string_encoded() {
1515+
assert_eq!(OctetStringEncoded::new(12).get(), &12);
1516+
assert_eq!(OctetStringEncoded::new(12).into_inner(), 12);
1517+
}
1518+
14801519
#[test]
14811520
fn test_printable_string_new() {
14821521
assert!(PrintableString::new("abc").is_some());

src/writer.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ mod tests {
208208
use crate::types::Asn1Writable;
209209
use crate::{
210210
parse_single, BMPString, BigInt, BigUint, BitString, Choice1, Choice2, Choice3, Enumerated,
211-
GeneralizedTime, IA5String, ObjectIdentifier, OwnedBitString, PrintableString, Sequence,
212-
SequenceOf, SequenceOfWriter, SequenceWriter, SetOf, SetOfWriter, Tlv, UniversalString,
213-
UtcTime, Utf8String, VisibleString, WriteError,
211+
GeneralizedTime, IA5String, ObjectIdentifier, OctetStringEncoded, OwnedBitString,
212+
PrintableString, Sequence, SequenceOf, SequenceOfWriter, SequenceWriter, SetOf,
213+
SetOfWriter, Tlv, UniversalString, UtcTime, Utf8String, VisibleString, WriteError,
214214
};
215215
#[cfg(feature = "const-generics")]
216216
use crate::{Explicit, Implicit};
@@ -266,6 +266,14 @@ mod tests {
266266
]);
267267
}
268268

269+
#[test]
270+
fn test_write_octet_string_encoded() {
271+
assert_writes::<OctetStringEncoded<bool>>(&[
272+
(OctetStringEncoded::new(true), b"\x04\x03\x01\x01\xff"),
273+
(OctetStringEncoded::new(false), b"\x04\x03\x01\x01\x00"),
274+
]);
275+
}
276+
269277
#[test]
270278
fn test_write_printable_string() {
271279
assert_writes::<PrintableString>(&[

0 commit comments

Comments
 (0)