micro509/der
DER reading and writing primitives.
The building blocks every other entrypoint is made of, for the cases the typed APIs do not cover:
- decoding a private extension's
extnValueinside anExtensionDecoder - encoding the bytes a
CustomExtensioncarries - inspecting a structure this library does not model
Reading accepts definite lengths and minimal length encodings, rejects the high-tag-number form, and applies a nesting-depth guard capped at DEFAULT_MAX_DER_DEPTH. BER-only constructs fail.
Readers and decoders take untrusted bytes, so each one comes as a pair:
decodeDerIntegerreturns aDecodeDerResultdecodeDerIntegerOrThrowthrows
Writers take typed input and throw, matching encodeName and pemEncode.
All operations are synchronous.
Functions
decodeDerBitString— Decodes a BIT STRING element (tag0x03) into aDerBitString.decodeDerBitStringOrThrow— Decodes a BIT STRING element (tag0x03) into aDerBitString.decodeDerBoolean— Decodes a BOOLEAN element (tag0x01).decodeDerBooleanOrThrow— Decodes a BOOLEAN element (tag0x01).decodeDerInteger— Decodes an INTEGER element (tag0x02) into anumber.decodeDerIntegerOrThrow— Decodes an INTEGER element (tag0x02) into anumber.decodeDerOctetString— Decodes an OCTET STRING element (tag0x04) into its payload bytes.decodeDerOctetStringOrThrow— Decodes an OCTET STRING element (tag0x04) into its payload bytes.decodeDerOid— Decodes an OBJECT IDENTIFIER element (tag0x06) into dotted-decimal form.decodeDerOidOrThrow— Decodes an OBJECT IDENTIFIER element (tag0x06) into dotted-decimal form.decodeDerString— Decodes a string element into text, dispatching on its tag.decodeDerStringOrThrow— Decodes a string element into text, dispatching on its tag.decodeDerTime— Decodes a UTCTime (tag0x17) or GeneralizedTime (tag0x18) element into aDate.decodeDerTimeOrThrow— Decodes a UTCTime (tag0x17) or GeneralizedTime (tag0x18) element into aDate.derChildren— Reads the direct children of a constructedparentwithinsource.derChildrenOrThrow— Reads the direct children of a constructedparentwithinsource.readDerElement— Reads one TLV element frombytesstarting atoffset.readDerElementOrThrow— Reads one TLV element frombytesstarting atoffset.readDerRoot— Reads the single top-level TLV element frombytes.readDerRootOrThrow— Reads the single top-level TLV element frombytes.readDerSequence— Reads a DER-encoded SEQUENCE frombytesand returns its direct children.readDerSequenceOrThrow— Reads a DER-encoded SEQUENCE frombytesand returns its direct children.hexToBytes— Converts a hex string (even or odd length) to aUint8Array.toHex— Converts raw bytes to a lowercase hex string with no separator.assertDerMaxDepth— Walks the full DER tree rooted inbytes.derBitString— Encodes a DER BIT STRING (tag0x03).derBmpString— Encodes a DER BMPString (tag0x1e) as big-endian UTF-16.derBoolean— Encodes a DER BOOLEAN (tag0x01):true→0xff,false→0x00.concatBytes— Concatenates multiple byte arrays into a singleUint8Array.derExplicitContext— Wraps a value in an explicit context-specific constructed tag (0xa0 + tag).derGeneralizedTime— Encodes aDateas a DER GeneralizedTime (tag0x18), formatYYYYMMDDHHMMSSZ.derIa5String— Encodes a DER IA5String (tag0x16).derImplicitConstructedContext— Wraps a value in an implicit context-specific constructed tag (0xa0 + tag).derImplicitPrimitiveContext— Wraps a value in an implicit context-specific primitive tag (0x80 + tag).derInteger— Encodes raw big-endian bytes as a DER INTEGER (tag0x02).derIntegerFromNumber— Encodes a non-negative JavaScriptnumberas a DER INTEGER.derNull— Produces a DER NULL element (tag0x05, zero-length value).derOid— Encodes a dotted-decimal OID string as a DER OBJECT IDENTIFIER (tag0x06).derOctetString— Wraps raw bytes in an OCTET STRING element (tag0x04).derPrintableString— Encodes a DER PrintableString (tag0x13).derSequence— Wraps concatenated children in a SEQUENCE (tag0x30).derSet— Wraps children in a SET (tag0x31) after DER-sorting them lexicographically by encoded bytes,derTime— Encodes aDateas the appropriate DER time type per RFC 5280.derTlv— Builds a complete DER TLV (tag-length-value) element:derUniversalString— Encodes a DER UniversalString (tag0x1c) as big-endian UTF-32.derUtcTime— Encodes aDateas a DER UTCTime (tag0x17), formatYYMMDDHHMMSSZ.derUtf8String— Encodes a DER UTF8String (tag0x0c).
Types
DecodeDerErrorCode— Machine-readable failure reason for the DER readers and decoders.DecodeDerFailure— Structured failure payload for DER reading and decoding.DecodeDerResult— Success-or-failure result from a DER reader or decoder.DerBitString— A BIT STRING payload and the number of unused trailing bits in its final byte.DerElement— A single parsed ASN.1 TLV element with byte-range metadata.ReadRootElementOptions— Options forreadRootElement.ReadSequenceChildrenOptions— Options forreadSequenceChildren.
Variables
DEFAULT_MAX_DER_DEPTH— Maximum nesting depth allowed when recursively walking a DER structure.