dernamespacenamespace derASN.1 DER (ITU-T X.690), encoded and read with no outside package (commons 0001-formats FR-009): every value a tag, a length and its content, as bytes; enough to build CMS structures (RFC 5652) and read the fields of an X.509 certificate (RFC 5280). Encoders give the one DER form (minimal lengths and integers, a SET OF sorted); a malformed input to an encoder (an OID that is not dotted numbers) is a programming error and throws a RangeError. Readers return a Result: what is not DER this reader knows is an error saying where. Taken from Stolos's src/archon/export/der.ts (S/MIME for emailed exports), with negative integers and integers read.
import { oid, readOid, readTlv } from "./der.ts";
const t = readTlv(oid("1.2.840.113549.1.7.1"));
if (t.kind === "ok") readOid(t.value.content); // { kind: "ok", value: "1.2.840.113549.1.7.1" }dernamespacenamespace derder.TAGconstno test imports itconst TAG: { readonly boolean: 1; readonly integer: 2; readonly bitString: 3; readonly octetString: 4; readonly null: 5; readonly oid: 6; readonly utf8String: 12; readonly printableString: 19; readonly ia5String: 22; readonly utcTime: 23; readonly generalizedTime: 24; readonly sequence: 48; readonly set: 49; }The universal tags used (class universal, with the constructed bit where it applies).
der.DerErrortypeno test imports ittype DerError = { readonly kind: "der"; readonly at: number; readonly message: string };Why bytes are not DER this reader knows: what, and at which byte.
der.Tlvtypeno test imports ittype Tlv = { readonly tag: number; readonly content: Uint8Array; readonly raw: Uint8Array };One element read: its tag byte, its content, and its whole encoding (to copy it into another structure).
der.encodeLengthfunctionno test imports itfunction encodeLength(n: number): Uint8Array<ArrayBufferLike>A DER length: the short form below 128, else 0x80 with the number of length bytes, then the bytes.
der.tlvfunctionno test imports itfunction tlv(tag: number, content: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>One element: the tag byte, the content's length, the content.
der.sequencefunctionno test imports itfunction sequence(...items: readonly Uint8Array<ArrayBufferLike>[]): Uint8Array<ArrayBufferLike>A SEQUENCE of these encodings, in order.
der.compareBytesfunctionno test imports itfunction compareBytes(a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>): numberTwo byte strings compared as DER sorts them: byte by byte, a prefix first.
der.setOffunctionno test imports itfunction setOf(...items: readonly Uint8Array<ArrayBufferLike>[]): Uint8Array<ArrayBufferLike>A SET OF these encodings, sorted as DER requires (X.690 §11.6).
der.octetStringfunctionno test imports itfunction octetString(b: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>An OCTET STRING.
der.bitStringfunctionno test imports itfunction bitString(b: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>A BIT STRING of whole bytes (no unused bits).
der.utf8Stringfunctionno test imports itfunction utf8String(s: string): Uint8Array<ArrayBufferLike>A UTF8String.
der.booleanfunctionno test imports itfunction boolean(v: boolean): Uint8Array<ArrayBufferLike>A BOOLEAN.
der.derNullfunctionno test imports itfunction derNull(): Uint8Array<ArrayBufferLike>NULL.
der.integerfunctionno test imports itfunction integer(v: number | bigint | Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>An INTEGER: a JavaScript integer or a bigint (negative too), or big-endian unsigned magnitude bytes; always in its minimal form.
der.oidfunctionno test imports itfunction oid(dotted: string): Uint8Array<ArrayBufferLike>An OBJECT IDENTIFIER from its dotted form (1.2.840.113549).
der.explicitfunctionno test imports itfunction explicit(n: number, inner: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>A context-specific EXPLICIT tag [n] around one encoding.
der.implicitfunctionno test imports itfunction implicit(n: number, content: Uint8Array<ArrayBufferLike>, constructed: boolean): Uint8Array<ArrayBufferLike>A context-specific IMPLICIT tag [n]: this content under the tag, constructed where the underlying type is.
der.algorithmfunctionno test imports itfunction algorithm(id: string, params?: Uint8Array<ArrayBufferLike> | undefined): Uint8Array<ArrayBufferLike>An AlgorithmIdentifier: SEQUENCE { algorithm OID, parameters ANY OPTIONAL }.
der.readTlvfunctionno test imports itfunction readTlv(bytes: Uint8Array<ArrayBufferLike>, offset?: number): Result<Tlv, DerError>The element that starts at offset: its tag, its content and its whole encoding; or why it is not DER this reader knows.
der.childrenfunctionno test imports itfunction children(content: Uint8Array<ArrayBufferLike>): Result<readonly Tlv[], DerError>The elements inside a constructed element's content, in order.
der.readOidfunctionno test imports itfunction readOid(content: Uint8Array<ArrayBufferLike>): Result<string, DerError>The dotted form of an OBJECT IDENTIFIER's content.
der.readIntegerfunctionno test imports itfunction readInteger(content: Uint8Array<ArrayBufferLike>): Result<bigint, DerError>The value of an INTEGER's content, as a bigint (two's complement).
TAGconstconst TAG: { readonly boolean: 1; readonly integer: 2; readonly bitString: 3; readonly octetString: 4; readonly null: 5; readonly oid: 6; readonly utf8String: 12; readonly printableString: 19; readonly ia5String: 22; readonly utcTime: 23; readonly generalizedTime: 24; readonly sequence: 48; readonly set: 49; }The universal tags used (class universal, with the constructed bit where it applies).
DerErrortypeno test imports ittype DerError = { readonly kind: "der"; readonly at: number; readonly message: string };Why bytes are not DER this reader knows: what, and at which byte.
Tlvtypeno test imports ittype Tlv = { readonly tag: number; readonly content: Uint8Array; readonly raw: Uint8Array };One element read: its tag byte, its content, and its whole encoding (to copy it into another structure).
encodeLengthfunctionfunction encodeLength(n: number): Uint8Array<ArrayBufferLike>A DER length: the short form below 128, else 0x80 with the number of length bytes, then the bytes.
tlvfunctionno test imports itfunction tlv(tag: number, content: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>One element: the tag byte, the content's length, the content.
sequencefunctionfunction sequence(...items: readonly Uint8Array<ArrayBufferLike>[]): Uint8Array<ArrayBufferLike>A SEQUENCE of these encodings, in order.
compareBytesfunctionno test imports itfunction compareBytes(a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>): numberTwo byte strings compared as DER sorts them: byte by byte, a prefix first.
setOffunctionfunction setOf(...items: readonly Uint8Array<ArrayBufferLike>[]): Uint8Array<ArrayBufferLike>A SET OF these encodings, sorted as DER requires (X.690 §11.6).
octetStringfunctionfunction octetString(b: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>An OCTET STRING.
bitStringfunctionfunction bitString(b: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>A BIT STRING of whole bytes (no unused bits).
utf8Stringfunctionfunction utf8String(s: string): Uint8Array<ArrayBufferLike>A UTF8String.
booleanfunctionfunction boolean(v: boolean): Uint8Array<ArrayBufferLike>A BOOLEAN.
derNullfunctionfunction derNull(): Uint8Array<ArrayBufferLike>NULL.
integerfunctionfunction integer(v: number | bigint | Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>An INTEGER: a JavaScript integer or a bigint (negative too), or big-endian unsigned magnitude bytes; always in its minimal form.
oidfunctionfunction oid(dotted: string): Uint8Array<ArrayBufferLike>An OBJECT IDENTIFIER from its dotted form (1.2.840.113549).
explicitfunctionfunction explicit(n: number, inner: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>A context-specific EXPLICIT tag [n] around one encoding.
implicitfunctionfunction implicit(n: number, content: Uint8Array<ArrayBufferLike>, constructed: boolean): Uint8Array<ArrayBufferLike>A context-specific IMPLICIT tag [n]: this content under the tag, constructed where the underlying type is.
algorithmfunctionfunction algorithm(id: string, params?: Uint8Array<ArrayBufferLike> | undefined): Uint8Array<ArrayBufferLike>An AlgorithmIdentifier: SEQUENCE { algorithm OID, parameters ANY OPTIONAL }.
readTlvfunctionfunction readTlv(bytes: Uint8Array<ArrayBufferLike>, offset?: number): Result<Tlv, DerError>The element that starts at offset: its tag, its content and its whole encoding; or why it is not DER this reader knows.
readOidfunctionfunction readOid(content: Uint8Array<ArrayBufferLike>): Result<string, DerError>The dotted form of an OBJECT IDENTIFIER's content.
readIntegerfunctionfunction readInteger(content: Uint8Array<ArrayBufferLike>): Result<bigint, DerError>The value of an INTEGER's content, as a bigint (two's complement).