structured-asn1 — borrowed ASN.1 BER/DER parsing in Rust
A Rust ASN.1 reader for BER and DER that borrows encoded values and preserves their exact byte ranges. Useful for format parsers that must verify digital signatures over the bytes received. Nested readers share depth, element, length and work limits; resource exhaustion is distinct from malformed input.
no_std + alloc by default · Rust 1.92+ · No cryptographic backend or I/O
Install
These snippets target the coordinated 0.2 API family. They require that version to be published on crates.io; check the version badge above before installing. For the older 0.1 release, use its versioned API guide.
[]
= "0.2"
Enable the optional std feature for a standard-library integration, or name
alloc explicitly for an alloc-only feature graph. See the
API reference for parser limits and value types.
Quick start
use Arc;
use ;
use ;
The value is a view into bytes. Choose Parser::ber for BER input, or use
Parser::new to supply encoding and parser-local Limits explicitly. Borrowed
values require the document to remain alive.
Scratch belonging to a parse
The source API adds Reader::execution() and the sealed memory::Work contract.
Use reader.execution().vector() (with Work imported) when temporary schema
processing belongs to the original parser session. Allocator growth, copies,
sorting and explicit charge_work then admit both the parser-local and current
aggregate work limits before execution. The borrowed scratch cannot outlive the
reader/account. Reader paths and diagnostic copies also reserve their local work
before allocation, while their containers charge aggregate work once.
Memory::vector() remains the independently owned result boundary. Its capacity
charge survives the parser; moving it does not reserve again. The default
OwnedVec<T> still uses this owner, while parser-bound scratch uses the additional
execution type parameter. These additions are not in crates.io 0.1.4.
Explicit INTEGER recovery
Reader::integer and read_integer are strict: X.690 §8.3.2 requires minimal
sign octets already under BER. The source version adds
Reader::integer_reading(&Element) for schema readers that retain a uniquely
readable value together with its nonminimal-encoding fault. This addition is
not in crates.io 0.1.3.
IntegerReading::as_bytes() keeps the original contents; value() borrows a
minimal suffix for bounded numeric conversion. fault() retains the original
located IntegerNotMinimal, and into_parts() moves that diagnostic without
an allocating clone. Recovery never repairs the received signed bytes or claims
BER/DER conformance. Primitive implicit tags remain supported; empty and
constructed contents remain errors. The producing Reader admits padding work
through both local and current aggregate ceilings before scanning, and diagnostic
storage remains charged until drop. Resource refusal never becomes a recovered
value without its fault.
OIDs and retained memory
OID identity uses its encoded bytes. Oid::text(&Memory) and
Arc::text(&Memory) materialize exact decimal text with fallible work and
capacity admission. Returned text retains its storage charge until released;
OID and arc values do not implement Display. Borrowed arc iteration leaves
its traversal admission to the caller.
The source version also adds OwnedOid::parse(text, &Memory) for strict
dotted-decimal identifiers with arbitrary-width arcs, and
OwnedOid::from_oid(oid, &Memory) for fallible retained copies. Its validated
immutable as_oid() view is constant-time and does not rescan the encoding.
The independent result keeps its capacity charge; these additions are not
available in the initial crates.io 0.1.0 release.
Owned vectors, maps and strings retain allocated capacity on their original
resource account. Container construction, growth and copies operate in that
declared domain. Map lookup/removal and try_iter(&Memory) may instead use a
current execution account; iteration prepays control buckets, including sparse
holes. Rebuilding a collection in another domain requires an explicit fallible
copy; retained allocations are never silently rebound.
The source uses allocator-api2 0.4.0 for vectors and hashbrown 0.17.1 for maps. Hashbrown's published allocator interface still requires allocator-api2 0.2.21; that interface forwards to the same account allocator. Both container families share capacity, work, growth-peak and rollback rules. These dependency updates are not in registry version 0.1.2.
Raw byte views and ordinary formatting/equality traits leave application-owned traversal and output storage to the caller. An explicit budget bounds admitted mechanism work; it does not automatically account arbitrary caller code.
Related crate
For X.509 certificate parsing and provider-neutral path validation, use structured-pkix. This crate reads encoding structure; it does not decide certificate trust or signature validity.
License and source
Licensed under Apache-2.0. Source, issues and contribution instructions are in signature-primitives.