jose.jwk

index · jose

Overview

JSON Web Keys (RFC 7517) for the three algorithms lilush signs with: Ed25519 as an OKP key (RFC 8037), P-256 as an EC key (RFC 7518) and a shared secret as an oct key. A key is a plain table with the RFC's member names and base64url values; d or k carries the private part. material decodes and validates one, thumbprint names it (RFC 7638), and the set_ calls build and read a JWKS document.

Every base64url value is read strictly: only A-Za-z0-9-_, no padding. Every length is checked here, before any byte reaches the crypto bindings, which check none.

Functions

NameSignature
b64url_decodeb64url_decode(s, name) -> bytes, err
alg_ofalg_of(key) -> alg, err
materialmaterial(key) -> m, err
checkcheck(key) -> key, err
from_ed25519from_ed25519(seed, pub) -> key, err
from_eccfrom_ecc(key_obj) -> key, err
from_secretfrom_secret(secret) -> key, err
publicpublic(key) -> key, err
thumbprintthumbprint(key) -> thumbprint, err
encodeencode(key) -> json, err
parseparse(doc) -> key, err
set_newset_new(list) -> set, err
set_parseset_parse(doc) -> set, err
set_encodeset_encode(set) -> json, err
set_findset_find(set, kid) -> key, err

b64url_decode(s, name) -> bytes, err

Decode a strict base64url string

Accepts A-Za-z0-9-_ alone, with no padding, as RFC 7515 writes every segment and member. name opens the error, so a caller says which field was bad. The decoder in crypto accepts +, / and = as well, and this is the one gate in front of it.

alg_of(key) -> alg, err

Name the JWS algorithm a key signs with

EdDSA for an OKP key on Ed25519, ES256 for an EC key on P-256, HS256 for an oct key. The algorithm follows from kty and crv alone; a JWS header never picks it.

material(key) -> m, err

Decode a key's raw material after validating every member

Returns { alg, pub, priv } with raw bytes: pub is the 32-byte Ed25519 point or the 65-byte uncompressed P-256 point, priv the 32-byte seed, the 32-byte scalar or the secret, and nil when the key has no private part. The raw bytes never land on the key table, so encode cannot leak them. Checks: a supported kty and crv, every member strict base64url of the right length (a secret of 32 bytes or more), alg absent or the key's own, use absent or sig, kid absent or a string.

check(key) -> key, err

Validate a key table

from_ed25519(seed, pub) -> key, err

Build an OKP key from an Ed25519 seed

seed is the 32 bytes crypto.ssh_load_key returns. The public point is derived from it; a pub given that does not match is refused.

from_ecc(key_obj) -> key, err

Build an EC key from a crypto P-256 key object

Takes the { private, public, x, y } table crypto.ecc_generate_key and crypto.ecc_load_key return. A kid on the object is not copied: an ACME account's kid is its URL, which goes in a header and never beside a jwk (RFC 8555 §6.2).

from_secret(secret) -> key, err

Build an oct key from a shared secret

The secret must be 32 bytes or longer, the HS256 floor RFC 7518 §3.2 sets.

public(key) -> key, err

Take the public half of a key

A fresh table with kty, crv, x, y, kid, alg and use alone. An oct key has no public half and is refused.

thumbprint(key) -> thumbprint, err

Compute the RFC 7638 thumbprint of a key

base64url of the SHA-256 over the canonical JSON: the required members alone, in lexical order, with no whitespace. The private part plays no role, so a key and its public half share one thumbprint. The form is {"crv","kty","x"} for OKP, {"crv","kty","x","y"} for EC and {"k","kty"} for oct. The members are strict base64url, so the string needs no escaping.

encode(key) -> json, err

Serialize a key as a JWK document

Writes the known members alone, the private part included. Take public first for a key that is to be published.

parse(doc) -> key, err

Parse a JWK document

Returns a key table with the known members alone; any other member of the document is dropped.

set_new(list) -> set, err

Build a key set from a list of keys

A set is { keys, by_kid }: the keys in the list's order, and the keyed ones by kid. Every key is checked, and a kid seen twice is refused.

set_parse(doc) -> set, err

Parse a JWKS document

set_encode(set) -> json, err

Serialize a key set as a JWKS document

Takes a set or a plain list of keys. A JWKS is published, so a key with a private part is refused: take public first. An empty set is refused too, since cjson would write its keys as {}.

set_find(set, kid) -> key, err

Find a key in a set by kid

The error for a kid the set lacks is the exact string unknown kid, the signal a relying party refetches its JWKS on.