Schema

The schema family: a declarative ASN.1 structure-schema engine and the per-format parsers built on it. Every format, from X.509 certificates and CRLs through CMS, OCSP, timestamps, and PKCS#12 stores (all() enumerates the registered set), is a member that composes the shared engine and the shared PKIX sub-schemas (AlgorithmIdentifier, Name, Extension), so a structural rule (bounds-checked positional reads, optional / tagged field ordering, SET-OF uniqueness, fail-closed typed errors) is defined once in the engine and no format can reintroduce the class of bug it prevents.

parse is the orchestrator: hand it DER (or PEM) and it detects which format the bytes encode and routes to that member's parser. Each member is also reachable directly (pki.schema.x509.parse, pki.schema.crl.parse), and all() enumerates the registered formats.

pki.schema.all

since 0.1.7 stable
pki.schema.all() -> string[]

The names of every registered format, in detection order.

Example

pki.schema.all();  // -> ["cms", "tsp", "crmf", "cmp", "ocsp-request", "ocsp-response", "pkcs12", "pkcs8", "csr", "attrcert", "attrcert-v1", "crl", "x509"]

References

pki.schema.parse

since 0.1.7 stable
pki.schema.parse(input) -> parsed

Detect which PKI format input (a DER Buffer or a PEM string) encodes and route to that format's parser, returning the same structured object the format's own parse returns. Throws SchemaError("schema/unknown-format") when the bytes match no registered format; the underlying decode / structural errors of the matched format propagate unchanged.

Example

async function example() {
  var pair = await pki.key.generate("Ed25519");
  var der = await pki.x509.sign({ subject: "example.com", subjectPublicKey: await pki.key.export(pair.publicKey),
    notBefore: new Date("2026-01-01T00:00:00Z"), notAfter: new Date("2036-01-01T00:00:00Z") },
    { key: await pki.key.export(pair.privateKey) });
  var parsed = pki.schema.parse(der);  // cert -> the pki.schema.x509 shape
}
example();

References

pki.schema.detectFormat

since 0.3.8 stable
pki.schema.detectFormat(input) -> string | null

Detect which registered PKI format input (a DER Buffer or PEM string) encodes and return its name, one of pki.schema.all(), without parsing it, or null when the decoded bytes match no registered format. This is the detection half of pki.schema.parse, running the same authoritative FORMATS ordering, exposed for a caller (e.g. pki.inspect.any) that needs the format name instead of the parsed result. Input that does not decode as DER throws the same coercion / decode error parse throws.

Example

async function example() {
  var pair = await pki.key.generate("Ed25519");
  var der = await pki.x509.sign({ subject: "example.com", subjectPublicKey: await pki.key.export(pair.publicKey),
    notBefore: new Date("2026-01-01T00:00:00Z"), notAfter: new Date("2036-01-01T00:00:00Z") },
    { key: await pki.key.export(pair.privateKey) });
  pki.schema.detectFormat(der);  // "x509" | "crl" | "csr" | "cms" | ... | null
}
example();

References