X.509 v3 certificates (RFC 5280) for Ed25519 and ECDSA P-256 keys — create, parse, verify, PEM.
Module x509 | Source packages/front/fw/src/crypto/utils/x509.js | Deps asn1, keyformat, pem, ed25519, ecc, sha256, bitArray | Worker-safe yes
Builds and reads certificates whose key and signature are Ed25519 (RFC 8410: pure
Ed25519 over the TBS bytes) or ECDSA P-256 with SHA-256 (RFC 5758:
ecdsa-with-SHA256, DER r, s, RFC 6979 deterministic k). It checks one
signature; it does not validate a certification path.
Resolve
const x509 = runtime.resolve('x509');
// Returns: { create, parse, verify, toPem, fromPem }
API
| Method | Signature | Returns |
|---|---|---|
create(spec, signer) |
(X509Spec, X509Signer) => Uint8Array | false |
The DER certificate; false on any malformed input |
parse(der) |
(Uint8Array) => X509Parsed | false |
The TBS fields |
verify(der, issuerPublicKey) |
(Uint8Array, {alg, key}) => boolean |
true when that key signed the certificate |
toPem(der) |
(Uint8Array) => string |
A CERTIFICATE PEM block |
fromPem(text) |
(string) => Uint8Array | false |
The DER of the first CERTIFICATE block |
X509Spec
| Field | Type | Notes |
|---|---|---|
subject |
{ CN?, O?, OU?, L?, ST?, C? } |
At least one attribute, 1..64 characters each (UTF8String); C is two upper-case letters (PrintableString). Encoded in key order |
issuer |
same | Defaults to subject: a self-signed certificate |
serial |
Uint8Array |
Unsigned big-endian, positive, at most 20 encoded octets (RFC 5280 § 4.1.2.2) |
notBefore, notAfter |
Date |
notBefore < notAfter; UTCTime before 2050, GeneralizedTime from 2050; milliseconds dropped |
publicKey |
{ alg: 'Ed25519', key } | { alg: 'ECDSA-P256', key } |
32 bytes, or the 65-byte SEC1 uncompressed point (checked on the curve) |
extensions |
object, optional | See below |
X509Signer
| Field | Type | Notes |
|---|---|---|
alg |
'Ed25519' | 'ECDSA-P256' |
The signature algorithm follows the signer's key |
privateKey |
Uint8Array |
Ed25519: 64 bytes (seed ‖ public key, as ed25519.keyPair returns it). ECDSA P-256: the 32-byte scalar |
publicKey |
Uint8Array, optional |
The issuer's public key; needed for authorityKeyIdentifier unless self-signed |
Extensions
| Key | Value | Critical |
|---|---|---|
basicConstraints |
{ ca: boolean, pathLen?: number } (pathLen only with ca) |
yes |
keyUsage |
names: digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment, keyAgreement, keyCertSign, cRLSign, encipherOnly, decipherOnly |
yes |
extKeyUsage |
dotted OIDs, e.g. 1.3.6.1.5.5.7.3.1 (serverAuth), 1.3.6.1.5.5.7.3.4 (emailProtection) |
no |
subjectAltName |
{ dns?: string[], email?: string[] } (ASCII) |
no |
subjectKeyIdentifier |
true |
no |
authorityKeyIdentifier |
true |
no |
Key identifiers are the leftmost 160 bits of SHA-256 of the subjectPublicKey bits (RFC 7093 § 2, method 1): fw has no SHA-1.
X509Parsed
{ version, serial, signatureAlgorithm, issuer, subject, notBefore, notAfter, publicKey, extensions, tbs, signature }.
signatureAlgorithm is 'Ed25519', 'ECDSA-SHA256' or the dotted OID; a name
attribute fw does not name appears under its dotted OID, a repeated one as an
array; publicKey is { alg, key } for the two supported keys and
{ alg: <oid>, key: null, spki } otherwise; extensions holds the six keys of the
table above (each with critical) and other: [{ oid, critical, value }].
Examples
const { x509, ed25519, ecc } = fw.runtime.resolveAll(['x509', 'ed25519', 'ecc']);
// A self-signed Ed25519 certificate
const kp = ed25519.keyPair(seed); // seed: 32 random bytes
const der = x509.create({
subject: { CN: 'signer.example', O: 'Example', C: 'FR' },
serial: Uint8Array.of(0x01),
notBefore: new Date(), notAfter: new Date(Date.now() + 365 * 86400e3),
publicKey: { alg: 'Ed25519', key: kp.publicKey },
extensions: { basicConstraints: { ca: false }, keyUsage: ['digitalSignature', 'nonRepudiation'] },
}, { alg: 'Ed25519', privateKey: kp.privateKey });
x509.verify(der, { alg: 'Ed25519', key: kp.publicKey }); // true
const cert = x509.parse(der); // cert.subject.CN === 'signer.example'
const pemText = x509.toPem(der); // -----BEGIN CERTIFICATE----- …
A CA issues a certificate by passing its own name as issuer and its own key as
signer (with signer.publicKey for authorityKeyIdentifier).
Worker Usage
const worker = fw.createWorker(
function ({ libs, args }) {
self.postMessage(libs.x509.parse(new Uint8Array(args[0])).subject);
},
{ dependencies: ['x509'], args: [Array.from(der)] } // args are JSON: pass bytes as an array
);
Notes
- Interop measured: certificates fw creates are read by
node:crypto'sX509Certificate(fields,ca,verify, issuance) andopenssl x509 -text;openssl verifyaccepts a fw CA and the leaf it issues; fw parses and verifies certificatesopenssl req -x509creates (Ed25519 and P-256). - Not claimed: path validation (chains, revocation, name constraints, validity against a clock), RSA keys, CSRs.
- No-throw: malformed input returns
falseand logs[crypto] INVALID: x509: ….