opyeb jwkslab

jwkslab · JWKS basics

JWKS structure and choosing a key by kid

A JWKS (JSON Web Key Set) is a standard JSON document that publishes several public keys. The shape is simple; most incidents come from the order in which kid and alg are read.

What a JWKS does

A JWKS (JSON Web Key Set) bundles the public keys used to verify signatures into one document. An OIDC issuer usually serves it at a path such as /.well-known/jwks.json, and a verifier reads the kid from the token header to pick the matching key. Services that rotate keys publish the new key first, switch signing to it, keep both for a while, and then remove the old one.

The document is an object whose only required member is the keys array. Each entry is a JWK, and the parameters it needs depend on kty.

{"keys":[{"kty":"RSA","kid":"2024-rsa","use":"sig","alg":"RS256","n":"...","e":"AQAB"}]}

Members that identify a key

kty is the key type. JOSE defines RSA, EC, OKP and oct, and this tool reads all four. kid is the identifier the issuer assigns; it must be unique inside one JWKS. alg names the algorithm for signatures made with the key, use says whether the key is for signatures (sig) or encryption (enc), and key_ops lists the permitted operations.

MemberPurposeWhen missing
ktyKey type (RSA, EC, OKP, oct)Required; the key cannot be read
kidKey identifier, unique in the setKey selection may fail when there are several keys
algSignature or encryption algorithmThe caller must pin an allow-list
usesig or encThe purpose is lost, inviting mixed use
key_opssign, verify, encrypt and so onOnly use is available
x5cX.509 certificate chain (Base64 DER)Verify with the key alone
x5t#S256SHA-256 digest of x5c[0] (Base64URL)Key selection can be slower

How a verifier picks a key

A typical JWT library reads kid and alg from the token header and looks for the same kid in the JWKS. It then checks that the key's kty and alg match the header, and that the algorithm is on its allow-list. Skipping that order and trusting the key's alg invites algorithm-confusion attacks.

  1. Read kid and alg from the token header. Without kid, continue only when there is exactly one key.
  2. Find the same kid in the JWKS. Two keys with one kid make the choice undefined, so it must fail.
  3. Check that kty and crv match the alg. An ES256 token must not be checked with a P-384 key.
  4. Check the algorithm against the server's allow-list. This is where none and HS256 substitution is stopped.
  5. Verify the signature with the public key. On failure, refetch the JWKS once in case a rotation just happened.

Traps that rotation leaves behind

Rotation means adding a new key, switching signing to it, and removing the old key after enough time. Removing the old key the moment signing changes breaks tokens that have not expired. Leaving it for too long lets tokens forged with a retired key pass.

If you cache a JWKS, define both the cache lifetime and the refetch policy on a kid miss. Most failures right after a rotation come from one of those two.

Commonly broken key sets

The next article maps each JWK parameter to the part of a PEM it becomes, and explains why conversion is not just a Base64 swap.

Questions

How is a JWKS different from a JWK?

A JWK is one JSON object describing one key. A JWKS is a document that holds those objects in a keys array. Because a JWKS publishes several keys, it needs kid to tell them apart.

Is kid required?

The specification does not require it, but with more than one key it is effectively necessary. A verifier normally selects by kid and alg; without one it may use the first key or fail.

Can a JWKS contain a private key?

The format allows it, but a private key must never be published. Issuers really do leak one by serialising the wrong object, and such a set has to be withdrawn and the key rotated immediately.

Paste a JWKS and duplicate kid values and alg mismatches are marked for you.

Open the tool

This article follows RFC 7517 (JWK), RFC 7518 (JWA), RFC 7638 (JWK thumbprint), RFC 5280 (X.509), RFC 5958 (PKCS#8), RFC 5915 (SEC1) and RFC 8037 (OKP). Every example key is synthetic and was made for this site.