Expand description
§Abstract Syntax Notation 1 (ASN.1) Library
This crate contains types and functions for using ASN.1 values. This library has little to do with encoding or decoding such values according to the Basic, Canonical, Distinguished, Packed, Octet, XML, JSON, General String, or BACNet Encoding Rules. This library is purposely abstracted from any particular encoding so that ASN.1 values can be translated between different encodings. Other crates will use this library as a dependency to implement encoding and decoding for the types defined in this crate.
This crate is intended to be high-performance and opines toward more code in exchange for better performance, notably using a lot of SIMD and other optimizations. This is not a lightweight crate; it is intended to be a fast, feature-complete, meticulous, and well-tested ASN.1 crate.
§Feature Flags
atoi_simd- This will make integer parsing slightly faster, but only if you enable SIMD features at compile-time. See this crate’s documentation.faster-hex- Faster hex compilation. Not tested to see if it’s really faster.itoa- Faster integer printing, only in a few select cases like OIDs andDURATIONvalueslikely_stable- Helps branch prediction, thereby making the code generally a little fastersmallvec- UseSmallVec<[u8; 16]>to storeOBJECT IDENTIFIER,RELATIVE-OIDandBIT STRING. Experimentation has shown this to be slower for some strange reason, so it should probably stay turned off.
§Capabilities
All universal types are supported, including the newer TIME subtypes: DATE,
DATE-TIME, TIME-OF-DAY, and DURATION.
§SEQUENCE or SET parsing features
This is not a complete solution, but this library provides TagSelector for
defining sequences of rules for matching components in a SEQUENCE or SET
data types, and ComponentSpec for more data, such as the component’s name and
whether it is OPTIONAL.
§Context-Switching Types
This library defines EXTERNAL, EMBEDDED_PDV, and CHARACTER_STRING. All of
them can be printed.
§Display
All types defined in this crate implement Display, and when printed, they are
printed according to their ASN.1 abstract syntax (which may differ from how
they are encoded).
§Detailed Errors
ASN1Errors are very detailed, printable, and you should be able to figure out
whatever your problem is based on the output:
pub struct ASN1Error {
pub error_code: ASN1ErrorCode,
pub component_name: Option<String>,
pub tag: Option<Tag>,
pub length: Option<usize>,
pub constructed: Option<bool>,
pub value_preview: Option<String>,
pub bytes_read: Option<usize>, // The number of bytes into the IO read stream where this error appeared.
pub values_read: Option<usize>, // The number of ASN.1 values into the IO read stream where this error appeared.
pub err_source: Option<Box<dyn std::error::Error + 'static>>,
}Of course, they implement std::error::Error.
There is a fluent API for elegantly creating errors, which is used like so:
let e = ASN1Error::new(super::ASN1ErrorCode::malformed_value)
.with_tag(Tag::new(TagClass::UNIVERSAL, 10))
.with_bytes_read(10)
.with_values_read(5)
.with_component_name("chunky")
.with_construction(true)
.with_preview("fogqwirg")
.with_length(7)
.with_source(src)
;§ASN.1 Types and Values
ASN.1 abstract values can be represented via ASN1Value. This is a pretty large
and complex enum. You probably should not use it unless you are parsing ASN.1
abstract values. When decoding presentation syntax (such as BER or DER), you
probably should represent presentation values using the decoding libraries
concept of a “node” or “value,” whatever that might be.
pub enum ASN1Value {
BitStringValue(BIT_STRING),
BooleanValue(BOOLEAN),
ChoiceValue(Box<ASN1Value>),
EmbeddedPDVValue(EMBEDDED_PDV),
EnumeratedValue(ENUMERATED),
ExternalValue(EXTERNAL),
InstanceOfValue(INSTANCE_OF),
IntegerValue(INTEGER),
IRIValue(OID_IRI),
NullValue,
/// ...
}§Embedded and no-std Use
This library is not ideal for embedded use cases. It is intended to be complete and compliant, not lightweight. Every valid ASN.1 requirement and capability is expected to be supported correctly, and if it is not, please report it as a bug.
This library is not no-std and probably never will be, because so many things
in this library rely on std, and so few things outside of that are useful.
In embedded environments, it might be better for you to use encoding-specific, simpler libraries that do the bare minimum. This might be a suitable library for you in this case. Also consider that, since this library is licensed under an FLOSS license, you could just copy whatever code you need into your embedded project.
§serde Integration
I don’t think this crate will support integration with serde, unless this is
requested. Its intended design is for dependent crates to implement most of the
encoding-specific logic.
§Fuzz Testing
In the root of this monorepo, run:
cargo fuzz run bitstring
cargo fuzz run date -- -max_len=10
cargo fuzz run datetime -- -max_len=20
cargo fuzz run time_of_day -- -max_len=8
cargo fuzz run asn1utils -- -max_len=8
cargo fuzz run duration -- -max_len=32
cargo fuzz run gentime -- -max_len=35
cargo fuzz run utctime -- -max_len=20
cargo fuzz run oid -- -max_len=24These will run forever, so you will want to kill them by pressing Ctrl+C.
Re-exports§
pub use bitstring::*;pub use constants::*;pub use construction::*;pub use date::*;pub use datetime::*;pub use display::*;pub use duration::*;pub use error::*;pub use external::*;pub use gentime::*;pub use oid::*;pub use roid::*;pub use strings::*;pub use tag::*;pub use time_of_day::*;pub use utctime::*;pub use utils::*;
Modules§
- bitstring
- The
BIT STRINGtype - constants
- Various ASN.1 constants
- construction
- Encoding, decoding, and validation of constructed types such as
SETorSEQUENCE - date
- The
DATEtype - datetime
- The
DATE-TIMEtype - display
- Implementations of
Displayand functions for displaying / printing - duration
- The
DURATIONtype - error
- The
ASN1Errorerror type - external
- Context-switching types:
EXTERNAL,EMBEDDED PDV, andCharacterString - gentime
- The
GeneralizedTimetype - oid
- The
OBJECT IDENTIFIERtype - roid
- The
RELATIVE-OIDtype - strings
- Functions for comparing, normalizing, and validating string types
- tag
- ASN.1 tags
- time_
of_ day - The
TIME-OF-DAYtype - utctime
- The
UTCTimetype - utils
- Various utilities
Macros§
- bits
- Macro to define a bit string from raw bits.
- octs
- Create an
OCTET STRING - oid
- Convenience macro for creating object identifiers
- roid
- Convenience macro for creating relative object identifiers (ROIDs)
Structs§
- BMPString
- ASN.1
BMPString: Unicode code points encoded on two bytes each. - Fractional
Part - Decimal digits fractional part
- Named
Type - A Named Type, such as would appear in the component type lists in a
SETorSEQUENCE - TYPE_
IDENTIFIER - ASN.1
TYPE-IDENTIFIER - UTCOffset
- Coordinated Universal Time (UTC) Offset
- Universal
String - ASN.1
UniversalString: Unicode code points encoded on four bytes each
Enums§
- ASN1
Value - ASN.1 value
Constants§
- FALSE
- Alias to make
falselook like ASN.1 - TRUE
- Alias to make
truelook like ASN.1 - UNIV_
TAG_ BIT_ STRING - The
UNIVERSALtag number forBIT STRING - UNIV_
TAG_ BMP_ STRING - The
UNIVERSALtag number forBMPString - UNIV_
TAG_ BOOLEAN - The
UNIVERSALtag number forBOOLEAN - UNIV_
TAG_ CHARACTER_ STRING - The
UNIVERSALtag number forCharacterString - UNIV_
TAG_ DATE - The
UNIVERSALtag number forDATE - UNIV_
TAG_ DATE_ TIME - The
UNIVERSALtag number forDATE-TIME - UNIV_
TAG_ DURATION - The
UNIVERSALtag number forDURATION - UNIV_
TAG_ EMBEDDED_ PDV - The
UNIVERSALtag number forEMBEDDED PDV - UNIV_
TAG_ END_ OF_ CONTENT - The
UNIVERSALtag number forEND-OF-CONTENT - UNIV_
TAG_ ENUMERATED - The
UNIVERSALtag number forENUMERATED - UNIV_
TAG_ EXTERNAL - The
UNIVERSALtag number forEXTERNAL - UNIV_
TAG_ GENERALIZED_ TIME - The
UNIVERSALtag number forGeneralizedTime - UNIV_
TAG_ GENERAL_ STRING - The
UNIVERSALtag number forGeneralString - UNIV_
TAG_ GRAPHIC_ STRING - The
UNIVERSALtag number forGraphicString - UNIV_
TAG_ IA5_ STRING - The
UNIVERSALtag number forIA5String - UNIV_
TAG_ INSTANCE_ OF - The
UNIVERSALtag number forINSTANCE OF - UNIV_
TAG_ INTEGER - The
UNIVERSALtag number forINTEGER - UNIV_
TAG_ NULL - The
UNIVERSALtag number forNULL - UNIV_
TAG_ NUMERIC_ STRING - The
UNIVERSALtag number forNumericString - UNIV_
TAG_ OBJECT_ DESCRIPTOR - The
UNIVERSALtag number forObjectDescriptor - UNIV_
TAG_ OBJECT_ IDENTIFIER - The
UNIVERSALtag number forOBJECT IDENTIFIER - UNIV_
TAG_ OCTET_ STRING - The
UNIVERSALtag number forOCTET STRING - UNIV_
TAG_ OID_ IRI - The
UNIVERSALtag number forOID-IRI - UNIV_
TAG_ PRINTABLE_ STRING - The
UNIVERSALtag number forPrintableString - UNIV_
TAG_ REAL - The
UNIVERSALtag number forREAL - UNIV_
TAG_ RELATIVE_ OID - The
UNIVERSALtag number forRELATIVE-OID - UNIV_
TAG_ RELATIVE_ OID_ IRI - The
UNIVERSALtag number forRELATIVE-OID-IRI - UNIV_
TAG_ RESERVED_ 15 - The reserved
UNIVERSALtag number 15 - UNIV_
TAG_ SEQUENCE - The
UNIVERSALtag number forSEQUENCE - UNIV_
TAG_ SEQUENCE_ OF - The
UNIVERSALtag number forSEQUENCE OF - UNIV_
TAG_ SET - The
UNIVERSALtag number forSET - UNIV_
TAG_ SET_ OF - The
UNIVERSALtag number forSET OF - UNIV_
TAG_ T61_ STRING - The
UNIVERSALtag number forT61String/TeletexString - UNIV_
TAG_ TIME - The
UNIVERSALtag number forTIME - UNIV_
TAG_ TIME_ OF_ DAY - The
UNIVERSALtag number forTIME-OF-DAY - UNIV_
TAG_ UNIVERSAL_ STRING - The
UNIVERSALtag number forUniversalString - UNIV_
TAG_ UTC_ TIME - The
UNIVERSALtag number forUTCTime - UNIV_
TAG_ UTF8_ STRING - The
UNIVERSALtag number forUTF8String - UNIV_
TAG_ VIDEOTEX_ STRING - The
UNIVERSALtag number forVideotexString - UNIV_
TAG_ VISIBLE_ STRING - The
UNIVERSALtag number forVisibleString
Traits§
- ASN1
Codec - An ASN.1 Codec
- ISO8601
Timestampable - Something that can be converted into an ISO 8601 Timestamp
- X690
Known Size - Anything that, when encoded as the content octets (“value”) of an X.690 Tag-Length-Value (TLV), will be encoded on a number of octets that can be trivially calculated, and does not vary with the choice of concrete syntax (BER, CER, or DER). This is so a codec can know in advance how many bytes a value will take up and pre-allocate them.
- X690
Validate - Trait for a type whose X.690 content octets can be validated in such a way that holds true for all X.690 codecs.
Type Aliases§
- BIT_
INDEX - Index into an ASN.1
BIT STRING - BOOLEAN
- ASN.1
BOOLEAN - Byte
Slice - How this library represents borrowed “bytes”
- CHARACTER_
STRING - ASN.1
CharacterString - DURATION
- ASN.1
DURATION - EMBEDDED_
PDV - ASN.1
EMBEDDED PDV - ENUMERATED
- ASN.1
ENUMERATED - EXTERNAL
- ASN.1
EXTERNAL - General
String - ASN.1
GeneralString - Graphic
String - ASN.1
GraphicString - IA5String
- ASN.1
IA5String - INSTANCE_
OF - ASN.1
INSTANCE OF - INTEGER
- ASN.1
INTEGER - NULL
- An ASN.1
NULLvalue - Numeric
String - ASN.1
NumericString - OCTET_
STRING - An ASN.1
OCTET STRING - OID_ARC
- An arc within an ASN.1
OBJECT IDENTIFIERorRELATIVE-OID - OID_IRI
- ASN.1
OBJECT IDENTIFIERInternationalized Resource Identifier (OID-IRI) - OPTIONAL
- An alias to make
Option<>look more like ASN.1. - Object
Descriptor - ASN.1
ObjectDescriptor, which is defined as - Printable
String - ASN.1
PrintableString - REAL
- ASN.1
REAL - RELATIVE_
OID_ IRI - ASN.1
RELATIVE-OIDInternationalized Resource Identifier (Relative-OID-IRI) - SEQUENCE
- ASN.1
SEQUENCE - SEQUENCE_
OF - ASN.1
SEQUENCE OF - SET
- ASN.1
SET - SET_OF
- ASN.1
SET OF - T61String
- ASN.1
T61String/TeletexString - TIME
- ASN.1
TIME - Teletex
String - ASN.1
T61String/TeletexString - UTF8
String - ASN.1
UTF8String - Videotex
String - ASN.1
VideotexString - Visible
String - ASN.1
VisibleString