Expand description
Synta: ASN.1 parser, decoder, and encoder library
This library provides ASN.1 (Abstract Syntax Notation One) parsing, decoding, and encoding capabilities with support for DER (Distinguished Encoding Rules) and BER (Basic Encoding Rules).
§Features
- Safe and ergonomic API leveraging Rust’s type system
- Zero-copy parsing where possible
- DER, BER, and CER encoding support
- Derive macros for automatic trait implementations (with
derivefeature) - C FFI compatibility (with
ffifeature) no_stdsupport (withalloc)- Optional serde
Serialize/Deserializesupport (withserdefeature)
§Cargo features
| Feature | Description |
|---|---|
std | Enable standard library support (enabled by default). Without this, alloc is used. |
derive | Enable derive macros (Asn1Sequence, Asn1Set, Asn1Choice). |
serde | Enable serde::Serialize/Deserialize for all ASN.1 types. |
unchecked | Unsafe optimization: skip overflow and bounds checks in tag and length decoding. Only enable for fully trusted, pre-validated input. Malformed data may cause incorrect behaviour. |
§ASN.1 types
All standard ASN.1 types are re-exported at the crate root for convenient import. Two families of string and binary types are available:
§Owned types (heap-allocating)
Suitable for constructing ASN.1 structures programmatically or when the decoded value must outlive the input buffer.
| Rust type | ASN.1 type |
|---|---|
OctetString | OCTET STRING |
BitString | BIT STRING |
Utf8String | UTF8String |
PrintableString | PrintableString |
IA5String | IA5String |
Integer | INTEGER |
Boolean | BOOLEAN |
ObjectIdentifier | OBJECT IDENTIFIER |
§Zero-copy borrowed types
These types borrow directly from the decoder’s input buffer, avoiding allocation. Use them for parse-only workloads where the values do not need to outlive the encoded bytes.
| Rust type | ASN.1 type |
|---|---|
OctetStringRef | OCTET STRING |
BitStringRef | BIT STRING |
Utf8StringRef | UTF8String |
PrintableStringRef | PrintableString |
IA5StringRef | IA5String |
§Example
use synta::{Decoder, Encoding, Integer};
// Decode an integer
let data = &[0x02, 0x01, 0x2A]; // INTEGER 42
let mut decoder = Decoder::new(data, Encoding::Der);
let value: Integer = decoder.decode().unwrap();
assert_eq!(value.as_i64().unwrap(), 42);§Convenience traits: ToDer and FromDer
ToDer and FromDer are blanket-implemented on all types that
implement Encode / Decode. They let you encode or decode a
single value without constructing an Encoder or Decoder manually:
use synta::{Integer, ToDer, FromDer};
let n = Integer::from_i64(42);
let der = n.to_der().unwrap();
let n2 = Integer::from_der(&der).unwrap();
assert_eq!(n, n2);Zero-copy borrowed types (e.g. OctetStringRef<'a>) cannot be decoded
via FromDer because they carry a lifetime tied to the input buffer;
use Decoder directly for those cases.
§Serde support
Enable the serde feature to serialize and deserialize all ASN.1 types
with any serde-compatible format (JSON, CBOR, etc.):
[dependencies]
synta = { version = "0.1", features = ["serde"] }use synta::{Integer, ObjectIdentifier};
let i = Integer::from_i64(255);
let json = serde_json::to_string(&i).unwrap(); // "\"ff\""
let oid = ObjectIdentifier::new(&[1, 2, 840, 113549]).unwrap();
let json = serde_json::to_string(&oid).unwrap(); // "\"1.2.840.113549\""Re-exports§
pub use config::DecoderConfig;pub use error::Error;pub use error::Result;pub use format::format_asn1_bytes;pub use format::Asn1FormatMode;pub use hex::bytes_to_hex;pub use hex::hex_to_bytes;pub use length::Length;pub use tag::Tag;pub use tag::TagClass;pub use der::decoder::Decoder;pub use der::encoder::Encoder;pub use types::oid::ObjectIdentifier;pub use types::primitive::Boolean;pub use types::primitive::Enumerated;pub use types::primitive::Integer;pub use types::primitive::Null;pub use types::primitive::Real;pub use types::string::BitString;pub use types::string::BmpString;pub use types::string::GeneralString;pub use types::string::IA5String;pub use types::string::NumericString;pub use types::string::OctetString;pub use types::string::PrintableString;pub use types::string::TeletexString;pub use types::string::UniversalString;pub use types::string::Utf8String;pub use types::string::VisibleString;pub use types::string::BitStringRef;pub use types::string::IA5StringRef;pub use types::string::OctetStringRef;pub use types::string::PrintableStringRef;pub use types::string::Utf8StringRef;pub use types::time::expand_yy;pub use types::time::parse2;pub use types::time::parse4;pub use types::time::GeneralizedTime;pub use types::time::UtcTime;pub use types::constructed::Element;pub use types::constructed::Sequence;pub use types::constructed::SequenceOf;pub use types::constructed::Set;pub use types::constructed::SetOf;pub use types::constructed::SequenceOf as SequenceOfVec;pub use types::constructed::SetOf as SetOfVec;pub use types::tagged::ExplicitTag;pub use types::tagged::ImplicitTag;pub use types::raw::RawDer;pub use traits::convenience::FromDer;pub use traits::convenience::ToDer;pub use traits::decode::Decode;pub use traits::decode_implicit::DecodeImplicit;pub use traits::encode::Encode;pub use traits::tag_for_optional::TagForOptional;pub use traits::tagged::Tagged;
Modules§
- ber
- BER (Basic Encoding Rules) support
- config
- Decoder and encoder configuration
- der
- DER (Distinguished Encoding Rules) support
- error
- Error types and handling
- format
- ASN.1 formatting utilities
- hex
- Hexadecimal encoding and decoding utilities.
- length
- ASN.1 length encoding and decoding
- reader
- Reading utilities
- tag
- ASN.1 tag handling
- traits
- Core traits for ASN.1 encoding and decoding
- types
- ASN.1 type definitions.
- writer
- Writing utilities
Enums§
- Encoding
- ASN.1 encoding rules
Derive Macros§
- Asn1
Choice - Derive
Encode,Decode, andTaggedfor an ASN.1 CHOICE type. - Asn1
Sequence - Derive
Encode,Decode, andTaggedfor an ASN.1 SEQUENCE type. - Asn1Set
- Derive
Encode,Decode, andTaggedfor an ASN.1 SET type.