JWKS structure and choosing a key by kid
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.
| Member | Purpose | When missing |
|---|---|---|
| kty | Key type (RSA, EC, OKP, oct) | Required; the key cannot be read |
| kid | Key identifier, unique in the set | Key selection may fail when there are several keys |
| alg | Signature or encryption algorithm | The caller must pin an allow-list |
| use | sig or enc | The purpose is lost, inviting mixed use |
| key_ops | sign, verify, encrypt and so on | Only use is available |
| x5c | X.509 certificate chain (Base64 DER) | Verify with the key alone |
| x5t#S256 | SHA-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.
- Read kid and alg from the token header. Without kid, continue only when there is exactly one key.
- Find the same kid in the JWKS. Two keys with one kid make the choice undefined, so it must fail.
- Check that kty and crv match the alg. An ES256 token must not be checked with a P-384 key.
- Check the algorithm against the server's allow-list. This is where none and HS256 substitution is stopped.
- 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.
- After publishing the new key, wait at least one token lifetime before removing the old one.
- Never reuse a kid. A new key under an old kid collides with the cached public key.
- During rotation two keys of the same alg coexist, so duplicate kid checks matter most then.
- If x5c ships with the key, check that the certificate key matches the JWK. This tool marks that mismatch critical.
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
- kid is empty, or two keys share one kid.
- use is enc while alg is a signature algorithm such as RS256.
- An EC key carries RS256, so alg and kty disagree.
- Private key fields (d, p, q) are present.
- An oct symmetric key is published, exposing the secret.
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 toolThis 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.