Skip to content

v3.8.0

Latest

Choose a tag to compare

@benmcollins benmcollins released this 20 Nov 21:45
00e284e

LibJWT 3.8.0 is a feature release. A jwt_checker_t now has a maximum token size, 1 MiB by default. After a successful verify, an application can read the token it accepted. jwt_builder_setkey() now has a signer of its own, separate from jwt_builder_add_signature() signers, so the order of the two calls no longer matters. ECDH-ES Direct Key Agreement now works with A192CBC-HS384 and A256CBC-HS512. Keyring verification is faster and leaner. The json-c backend now treats a JSON null member as Jansson does, and both backends reject a non-array "keys". Read the behavior changes below before upgrading.

This release adds API and is ABI-compatible with 3.7.0. An nm diff shows 158 exported symbols, up from 155, with three added and none removed or changed. Per the libtool rules, the SONAME goes 18:5:4 → 19:0:5. The SONAME stays libjwt.so.14 and existing binaries keep working.

⚠️ Behavior changes

  • Tokens over 1 MiB are rejected by default (#378). jwt_checker_verify() and jwt_checker_verify_detached() reject a token longer than 1048576 bytes with Token too large, before they parse it. Before, only tokens of INT_MAX bytes or more were rejected. To accept larger tokens, raise the limit on the checker:
    jwt_checker_setmaxsize(checker, 4 * 1024 * 1024);
    • For jwt_checker_verify_detached() the payload counts: the token rebuilt with the payload attached must fit, and a payload whose own length exceeds the limit is refused before it is encoded. Base64url encoding makes a payload about 4/3 of its size, so with the default limit a detached payload over roughly 768 KiB now fails. An unencoded ("b64":false) payload counts at its own length.
    • JWE tokens (jwe_checker_t) and JWK/JWKS input are not subject to this limit.
  • jwt_builder_setkey() no longer re-keys an add_signature() signer (#361).
    • Called after jwt_builder_add_signature(), setkey() now adds a second signer and switches the builder to the General JSON serialization, as any second signer does. jwt_builder_generate() then returns a JSON document where it used to return a Compact token. Before, setkey() re-keyed the add_signature() signer: that signer kept its headers (such as its "kid") but signed with the setkey() key, and its own key was dropped without an error. To keep a single Compact signer, drop the add_signature() call. To change an add_signature() signer's key, pass that key to add_signature().
    • If the application then selects JWT_FORMAT_COMPACT, generation fails rather than leave an add_signature() signer out of the token. setkey() alone in Compact signs as before.
    • jwt_builder_setkey(builder, JWT_ALG_NONE, NULL) removes only the setkey() signer. It never turns an add_signature() signer into an unsigned one. With a JSON format and no other signer, generation now fails with No key set for JSON serialization (it was A JSON-serialized JWS cannot use "none").
    • A builder callback's config->key/config->alg now apply to the setkey() signer in the JSON serializations too; before, they applied only to Compact.
    • Without setkey(), a key set by the callback adds one more signature, placed before the add_signature() ones.
    • In a JSON form, generation fails if the callback clears config->key for a setkey() signer.
    • A builder callback that sets a key but leaves config->alg as JWT_ALG_NONE now gets a Compact token signed with the key's own "alg". It used to get an unsigned ("alg":"none") token, unless the builder had refused a key that was in error. This now matches the JSON forms and jwt_builder_setkey(builder, JWT_ALG_NONE, key). If you meant an unsigned token, leave config->key NULL in the callback.
  • JSON null members, non-array "keys" and non-string private members (#357). Most of these apply to both JSON backends:
    • A JWKS whose "keys" member is not an array (including null) now sets jwks_error() on the set (Invalid JWKS: "keys" is not an array) and adds no key. Before, Jansson loaded it as an empty set without an error, and json-c read a null one as a single JWK. Every CLI tool that loads a key file now stops with this error.
    • "alg":null and "x5c":null put a key in error. Since 3.6.3 both backends had read them as absent.
    • On every JSON and crypto backend, a private member (RSA d/p/q/dp/dq/qi, EC and OKP d, AKP priv) that is present but not a string puts the key in error with Invalid JWK: "<name>" is not a string. Before, GnuTLS and MbedTLS loaded such an EC or OKP key as a public key, which could verify, and OpenSSL failed it with a message of its own. Fix the JWK. Such a key still keeps its kid, alg, use and key_ops, as any key that fails to load does, so jwks_find_bykid() and messages that name the key still work.
    • Getting a member that is not an object or array as JWT_VALUE_JSON now returns JWT_VALUE_ERR_TYPE. Jansson returned JWT_VALUE_ERR_INVALID and json-c returned the scalar's JSON text.
    • json-c only: a null member is now present. Getters return JWT_VALUE_ERR_TYPE (was JWT_VALUE_ERR_NOEXIST), so code that treats JWT_VALUE_ERR_NOEXIST as "absent" now sees an error. Setters without replace return JWT_VALUE_ERR_EXIST. A null exp or nbf fails the claims check, a null claim satisfies jwt_checker_require(), and a JWE token with a null crit, zip, protected, unprotected, recipients, header, aad or encrypted_key now fails. The full list is under Library below.
  • Error text on the Jansson keyring path (#355). A Compact token that is not valid UTF-8 in its header, signature or base64url-encoded payload is still rejected against a keyring. The error now comes from the check it fails, such as JWS protected header is not valid JSON or Error decoding signature, instead of Token is not valid UTF-8. An unencoded "b64":false payload is covered under Library.
  • CLI message text (#365). Scripts that match the jwt-verify unknown-algorithm message need updating. Details are under CLI tools below.

✨ New API

  • jwt_checker_setmaxsize() (#378) sets the checker's maximum token size. It accepts 1 to INT_MAX - 1. Any other value returns non-zero, sets Invalid maximum token size on the checker and keeps the previous limit. A new checker starts at 1 MiB.
  • jwt_checker_verified_jwt() (#362) returns the jwt_t that the last jwt_checker_verify() or jwt_checker_verify_detached() accepted. Use it to read the claims and protected header. jwt_get_alg() on it gives the algorithm that verified it, even when the key has no "alg".
    • The checker owns the returned jwt_t; do not free it. It stays valid until the next verify call (even one that fails immediately) or jwt_checker_free(). A checker therefore now keeps the last token it accepted in memory until then.
    • After a failed verify, and from inside a verify or jti callback, it returns NULL.
    • For a General JWS JSON token, the jwt_t has an empty header and jwt_get_alg() returns JWT_ALG_INVAL, since each signature has a header of its own; use jwt_checker_sig_header_get() instead.
  • jwt_checker_sig_header_get() (#362) reads a protected-header member of one signature, by index, as jwt_header_get() does. It returns a value only for a signature that verified in an accepted token; otherwise it returns JWT_VALUE_ERR_INVALID. It never reads the unprotected "header", which is not integrity protected.

🔐 Library

  • ECDH-ES with A192CBC-HS384 and A256CBC-HS512 (#369). In Direct Key Agreement mode ("alg":"ECDH-ES"), the derived key is the content key, so these two enc values need 48 or 64 octets. Before, every backend ran a single round of the Concat KDF and refused anything longer than 32 octets. jwe_builder_generate() returned NULL and jwe_checker_decrypt() failed with ECDH-ES key agreement failed, even for valid tokens from other implementations.
    • The three backend copies of the KDF are now one shared implementation of RFC 7518 §4.6.2 in full.
    • ECDH-ES now encrypts and decrypts with every enc on OpenSSL, GnuTLS and MbedTLS. X25519 and X448 on GnuTLS still need GnuTLS 3.8.13 or later, as before.
    • Keys of 32 octets or less (A128CBC-HS256, the AES-GCM enc values and the ECDH-ES+A*KW KEKs) derive byte for byte as before. ECDH-ES+A*KW was not affected.
  • Compact tokens against a keyring (#355). A checker with a keyring no longer converts a Compact token into a Flattened JWS JSON document, serializes it and parses it again. It checks the token's own bytes, with the same per-signature checks a JSON signature gets.
    • On a 350 KB token under Jansson, a one-key ring now allocates about 0.85× what jwt_checker_setkey() does, down from about 2.5×. Each further candidate key costs only its own signature check.
    • With Jansson, a Compact token whose unencoded ("b64":false) payload is not valid UTF-8, attached or detached, now verifies against a keyring. It already did with json-c and with jwt_checker_setkey().
    • Verdicts and the jwt_checker_sig_*() results are otherwise unchanged.
  • Keyring scan in one pass (#356). A signature without "kid" is tried against every compatible key in the ring. That scan walked the key list from its head for each key, which is quadratic in the ring size. It is now linear: on a 10,000-key ring with no matching key, a signature takes a few milliseconds instead of about 0.4 s. Key order, the first key that verifies, the tried count and the error messages are unchanged. The jwt_checker_setkeyring() docs now describe the scan.
  • JSON null members on json-c (#357). json-c's lookup returns nothing both for a missing member and for one whose value is null. libjwt used that lookup to test whether a member was present, so the two JSON backends gave different answers for the same token, key or call. It now checks presence explicitly, and json-c gives Jansson's results:
    • Getters (jwt_claim_get(), jwt_header_get() and the builder's): a null member is JWT_VALUE_ERR_TYPE, not JWT_VALUE_ERR_NOEXIST.
    • Setters: setting over a null member without replace gives JWT_VALUE_ERR_EXIST.
    • Checker: a null exp or nbf fails the claims check, a null claim is present for jwt_checker_require(), and a null protected JWS parameter overlaps the same name in the unprotected header.
    • Builder: jwt_builder_setcrit() may name a null header parameter, an application's null typ is kept, and a null protected parameter overlaps a signature's unprotected one.
    • JWE:
      • A null protected crit or zip, and a null protected, unprotected, recipients, header, aad or encrypted_key member, are rejected as the wrong type.
      • A null header parameter counts for the disjointness rule.
      • A token with a null parameter in the shared or per-recipient unprotected header now decrypts; it used to fail with Error allocating memory.

🧰 CLI tools

  • jwt-verify
    • For an unknown -a/--algorithm value, the second error line now reads Use -l to see a list of supported algorithms, without the stray ) (#365).
    • jwt-verify(1) now documents that a token given on the command line is held to the library's 1 MiB default (#378). Stdin lines were already capped at 1 MiB.
  • jwt-generate: for an unknown -a/--algorithm value, one blank line now separates the error from the usage text (it was two), as for every other error (#365).
  • All tools: a key file whose "keys" is not an array (or is null) now fails to load with Invalid JWKS: "keys" is not an array (#357).

🏗️ Tests & docs

  • New test programs jwt_verified and jwt_maxsize, a large-keyring scan test, ECDH-ES round trips with every enc on every curve and backend, and the Wycheproof jwe_ec vectors for A192CBC-HS384 and A256CBC-HS512 (#362, #378, #356, #369).
  • The README and the Doxygen main page describe the setkey() signer and ECDH-ES with every enc. The jwt.h API docs cover the keyring scan, the size limit and the new getters. The jwt_t accessor docs no longer refer to a jwt_create() that does not exist.

Full Changelog: v3.7.0...v3.8.0