Expand description
§Red ASN1
A little library to build/parse ASN1 DER
§Examples
Parsing and building bool:
use red_asn1::Asn1Object;
assert_eq!(true, bool::parse(&[0x1, 0x1, 0xff]).unwrap().1);
assert_eq!(false, bool::parse(&[0x1, 0x1, 0x0]).unwrap().1);
assert_eq!(true.build(), vec![0x1, 0x1, 0xff]);
assert_eq!(false.build(), vec![0x1, 0x1, 0x0]);Parsing and building Integer:
use red_asn1::{Integer, Asn1Object};
assert_eq!(2, Integer::parse(&[0x2, 0x1, 0x2]).unwrap().1);
assert_eq!(2.build(), vec![0x2, 0x1, 0x2]);Parsing and building String:
use red_asn1::Asn1Object;
assert_eq!(
"John".to_string(),
String::parse(&[0x1b, 0x4, 0x4a, 0x6f, 0x68, 0x6e]).unwrap().1
);
assert_eq!(
"John".to_string().build(),
vec![0x1b, 0x4, 0x4a, 0x6f, 0x68, 0x6e]
);Creating custom sequences:
/*
Person ::= [APPLICATION 1] SEQUENCE {
name: [0] GeneralString,
age: [1] IMPLICIT Integer,
address: [2] GeneralString OPTIONAL,
}
*/
use red_asn1::*;
use red_asn1_derive::Sequence;
#[derive(Sequence, Default)]
#[seq(application_tag = 1)]
struct Person {
#[seq_field(context_tag = 0)]
pub name: GeneralString,
#[seq_field(context_tag = 1, implicit)]
pub age: Integer,
#[seq_field(context_tag = 2)]
pub address: Option<GeneralString>
}
let john = Person{
name: GeneralString::from("John").into(),
age: Integer::from(18).into(),
address: None
};
assert_eq!(
vec![
0x61, 0xd, 0x30, 0xb,
0xa0, 0x6, 0x1b, 0x4, 0x4a, 0x6f, 0x68, 0x6e, // "John"
0x80, 0x1, 0x12 // 18
]
, john.build()
);
let (_, rachel) = Person::parse(&[
0x61, 0x19, 0x30, 0x17,
0xa0, 0x8, 0x1b, 0x6, 0x52, 0x61, 0x63, 0x68, 0x65, 0x6c, // "Rachel"
0x80, 0x1, 0x1e, // 30
0xa2, 0x8, 0x1b, 0x6, 0x48, 0x61, 0x77, 0x61, 0x69, 0x69 // "Hawaii"
]).unwrap();
assert_eq!("Rachel", rachel.name);
assert_eq!(30, rachel.age);
assert_eq!(Some("Hawaii".to_string()), rachel.address);
Creating custom enum:
/*
PersonAttr ::= [APPLICATION 1] CHOICE {
name: [0] GeneralString,
age: [1] IMPLICIT Integer,
}
*/
use red_asn1::*;
use red_asn1_derive::Choice;
#[derive(Choice, Debug, PartialEq)]
#[choice(application_tag = 1)]
enum PersonAttr {
#[choice_field(context_tag = 0)]
GeneralString(GeneralString),
#[choice_field(context_tag = 1, implicit)]
Integer(Integer),
}
// Asn1Obj needs to implement default
impl Default for PersonAttr {
fn default() -> Self {
return Self::Integer(Integer::from(0));
}
}
let pa = PersonAttr::GeneralString(GeneralString::from("John").into());
assert_eq!(
vec![
0x61, 0x8, // application tag
0xa0, 0x6, 0x1b, 0x4, 0x4a, 0x6f, 0x68, 0x6e, // "John"
],
pa.build()
);
assert_eq!(
PersonAttr::parse(&[
0x61, 0x8, // application tag
0xa0, 0x6, 0x1b, 0x4, 0x4a, 0x6f, 0x68, 0x6e, // "John"
])
.unwrap()
.1,
pa
);
let pa = PersonAttr::Integer(Integer::from(77));
assert_eq!(
vec![
0x61, 0x3, // application tag
0x80, 0x1, 77, // "John"
],
pa.build()
);
assert_eq!(
PersonAttr::parse(&[
0x61, 0x3, // application tag
0x80, 0x1, 77, // "John"
])
.unwrap()
.1,
pa
);
§Implemented types
| ASN1 | red_asn1 type | Rust type |
|---|---|---|
| BOOLEAN | Boolean | bool |
| INTEGER | Integer | i128, i64, i32, i16, u32 |
| BIT STRING | BitSring | |
| OCTET STRING | OctetString | Vec<u8> |
| GeneralString | GeneralString | String |
| IA5String | IA5String | ascii::AsciiString |
| GeneralizedTime | GeneralizedTime | |
| SEQUENCE OF | SequenceOf | Vec<T: Asn1Object> |
| SET OF | SetOf | |
| SEQUENCE | struct with #[derive(Sequence, Default)] | |
| OPTIONAL | Optional | Option |
| CHOICE | enum with #[derive(Choice)] | |
Structs§
- BitString
- Class to build/parse BitSring ASN1
- Enumerated
- Generalized
Time - Class to build/parse GeneralizedTime ASN1
- Oid
- Class to build/parse Object Identifiers
- SetOf
- Tag
- Class to represent DER-ASN1 tags of the different types.
- VecInt
- Class to parse an integer of any size into a vector.
Enums§
- Error
- Error in ASN1-DER parse/build operations
- TagClass
- Enum with the different tag classes
- TagType
- Enum with the different tag types
Statics§
- BIT_
STRING_ TAG_ NUMBER - BOOLEAN_
TAG_ NUMBER - ENUMERATED_
TAG_ NUMBER - GENERALIZED_
TIME_ TAG_ NUMBER - GENERALSTRING_
TAG_ NUMBER - IA5STRING_
TAG_ NUMBER - INTEGER_
TAG_ NUMBER - OCTET_
STRING_ TAG_ NUMBER - SEQUENCE_
TAG_ NUMBER - SET_
OF_ TAG_ NUMBER
Traits§
- Asn1Int
- A trait to identify types that are ASN.1 integers
- Asn1
Object - A trait to allow objects to be built/parsed from ASN1-DER
Functions§
- build_
length - parse_
length - To parse the object value length from DER, should not be overwritten
- sequence_
tag - Tag of Sequence
- sequenceof_
tag - Tag of Sequence Of
- setof_
tag - Tag of Set Of
Type Aliases§
- Boolean
- Class to build/parse Boolean ASN1
- General
String - Class to build/parse GeneralString ASN1
- IA5String
- Class to build/parse IA5String ASN1
- Integer
- Class to build/parse Integer ASN1
- Octet
String - Class to build/parse OctetString ASN1
- Optional
- Class to build/parse SequenceOf ASN1
- Result
- Result that encapsulates the Error type of this library
- Sequence
Of - Class to build/parse SequenceOf ASN1