|
LibJWT 3.8.0
The C JSON Web Token Library +JWK +JWKS
|
Verify and validate JWT tokens. More...
Macros | |
| #define | jwt_checker_auto_t |
| A jwt_checker_t pointer that is freed automatically at scope exit. | |
Typedefs | |
| typedef struct jwt_checker | jwt_checker_t |
| Opaque Checker object. | |
Enumerations | |
| enum | jwt_verify_policy_t { JWT_VERIFY_POLICY_ANY = 0 , JWT_VERIFY_POLICY_ALL } |
| Multi-signature verification policy. More... | |
Functions | |
| jwt_checker_t * | jwt_checker_new (void) |
| Function to create a new checker instance. | |
| void | jwt_checker_free (jwt_checker_t *checker) |
| Frees a previously created checker object. | |
| int | jwt_checker_error (const jwt_checker_t *checker) |
| Checks error state of checker object. | |
| const char * | jwt_checker_error_msg (const jwt_checker_t *checker) |
| Get the error message contained in a checker object. | |
| void | jwt_checker_error_clear (jwt_checker_t *checker) |
| Clear error state in a checker object. | |
| int | jwt_checker_setkey (jwt_checker_t *checker, const jwt_alg_t alg, const jwk_item_t *key) |
| Sets a key and algorithm for a checker. | |
| int | jwt_checker_setkeyring (jwt_checker_t *checker, const jwk_set_t *keyring, jwt_verify_policy_t policy) |
| Set a keyring and policy for verification. | |
| int | jwt_checker_expect_typ (jwt_checker_t *checker, const char *typ) |
| Require a specific token media type ("typ" header). | |
| int | jwt_checker_setalgs (jwt_checker_t *checker, const jwt_alg_t *algs, size_t n) |
| Set an allowlist of acceptable algorithms. | |
| int | jwt_checker_require (jwt_checker_t *checker, const char **claims, unsigned int count) |
| Require that a set of claims is present. | |
| int | jwt_checker_setmaxsize (jwt_checker_t *checker, size_t size) |
| Set the maximum size of a token this checker verifies. | |
| int | jwt_checker_enable_embedded_jwk (jwt_checker_t *checker, jwk_thumbprint_alg_t alg, const char *expected_jkt) |
| Verify using the key embedded in the header, pinned by thumbprint. | |
| int | jwt_checker_enable_embedded_jwk_keyring (jwt_checker_t *checker, jwk_thumbprint_alg_t alg, const jwk_set_t *allowed) |
| Verify using the key embedded in the header, allowed by a keyring. | |
| int | jwt_checker_setcb (jwt_checker_t *checker, jwt_callback_t cb, void *ctx) |
| Set a callback for verifying tokens. | |
| int | jwt_checker_setjti (jwt_checker_t *checker, jwt_jti_check_cb_t cb, void *ctx) |
| Set a callback to verify the jti (JWT ID) claim (RFC-7519 Sec 4.1.7). | |
| void * | jwt_checker_getctx (jwt_checker_t *checker) |
| Retrieve the callback context that was previously set. | |
| int | jwt_checker_understands (jwt_checker_t *checker, const char *header) |
| Declare a critical header parameter as understood (RFC-7515 Sec 4.1.11). | |
| int | jwt_checker_verify (jwt_checker_t *checker, const char *token) |
| Verify a token. | |
| unsigned int | jwt_checker_sig_count (const jwt_checker_t *checker) |
| Number of signatures in the last verified token. | |
| int | jwt_checker_sig_verified (const jwt_checker_t *checker, unsigned int index) |
| Whether a given signature of the last token verified. | |
| const jwk_item_t * | jwt_checker_sig_key (const jwt_checker_t *checker, unsigned int index) |
| The key a given signature of the last token verified against. | |
| jwt_t * | jwt_checker_verified_jwt (jwt_checker_t *checker) |
| The token that the last verify accepted. | |
| jwt_value_error_t | jwt_checker_sig_header_get (jwt_checker_t *checker, unsigned int index, jwt_value_t *value) |
| Get a protected-header member of one verified signature. | |
| int | jwt_checker_verify_detached (jwt_checker_t *checker, const char *token, const unsigned char *payload, size_t len) |
| Verify a token whose payload was detached. | |
Verify and validate JWT tokens.
Validating a JWT involves decoding the Base64url parts of the JWT then verifying claims and the signature hash. The checker object allows you to configure how you want to perform these steps so you can easily process tokens with one simple call.
| #define jwt_checker_auto_t |
A jwt_checker_t pointer that is freed automatically at scope exit.
Declares a jwt_checker_t pointer carrying the GCC/Clang cleanup attribute so jwt_checker_free() is invoked on it automatically when the variable goes out of scope. Initialize it where you declare it.
| typedef struct jwt_checker jwt_checker_t |
Opaque Checker object.
| enum jwt_verify_policy_t |
Multi-signature verification policy.
Governs how jwt_checker_verify() decides on a JWS JSON Serialization that carries more than one signature (RFC-7515 Sec 7.2 leaves this to the application). It has no effect on a Compact or single-signature token.
| Enumerator | |
|---|---|
| JWT_VERIFY_POLICY_ANY | Accept if >=1 signature verifies |
| JWT_VERIFY_POLICY_ALL | Accept only if every signature does |
| int jwt_checker_enable_embedded_jwk | ( | jwt_checker_t * | checker, |
| jwk_thumbprint_alg_t | alg, | ||
| const char * | expected_jkt ) |
Verify using the key embedded in the header, pinned by thumbprint.
Enables taking the verification key from the token's protected-header "jwk" (a self-contained token, as used by DPoP and OpenID4VCI key proofs). Because that key is supplied by whoever made the token, it is never trusted on its own: it is accepted only if its JWK Thumbprint (alg) equals expected_jkt – the value you obtained out of band and are pinning to, such as the cnf.jkt of a presented access token (read with jwt_get_cnf()). The confirmed key is still held to the usual key-type/algorithm binding. Combine with jwt_checker_setalgs() to bound the acceptable algorithms.
The header key has to be a public key (RFC-7515 Sec 4.1.3). A "jwk" that is a private key, the signer's own included, or a symmetric ("oct") key is refused like one that does not match the pin: verification fails with "Embedded JWK is missing or not confirmed" and no signature is checked. So is one that merely carries a member a public JWK does not have ("d", "k" and the others that jwks_item_export() strips, whatever its value), or that is a JWK set (a "keys" member). Put the public JWK in the header, e.g. as jwks_item_export() gives it when its priv argument is zero; the key must carry its public member (a seed-only private key has no public form to embed).
Passing a NULL or empty expected_jkt fails: there is no "trust whatever is
embedded" mode. Applies to the Compact Serialization (what DPoP/OpenID4VCI use). Mutually exclusive with the keyring form; the last call wins.
"jwk" that was a private or a symmetric key was confirmed, and used to verify, like a public one (GHSA-vmf4-mvgm-7vpv).| checker | Pointer to a checker object |
| alg | The hash for the thumbprint comparison (see jwk_thumbprint_alg_t) |
| expected_jkt | The pinned base64url JWK Thumbprint the embedded key must match |
| int jwt_checker_enable_embedded_jwk_keyring | ( | jwt_checker_t * | checker, |
| jwk_thumbprint_alg_t | alg, | ||
| const jwk_set_t * | allowed ) |
Verify using the key embedded in the header, allowed by a keyring.
As jwt_checker_enable_embedded_jwk(), but the protected-header "jwk" is accepted only if its JWK Thumbprint (alg) matches that of some key in allowed (via jwks_find_bythumbprint()). A key in error, in the header or in allowed, has no thumbprint and so never matches. Use this when the set of acceptable holder keys is known in advance rather than pinned per token. The keyring is borrowed: the caller retains ownership and must keep it valid for the lifetime of the checker.
As with the pin, a header "jwk" that is a private or a symmetric key is refused, even if its thumbprint is that of a key in allowed. The keys in allowed may themselves be private keys: only their thumbprints are used.
Passing a NULL allowed fails. Mutually exclusive with the pinned form; the last call wins.
| checker | Pointer to a checker object |
| alg | The hash for the thumbprint comparison (see jwk_thumbprint_alg_t) |
| allowed | A JWKS of acceptable keys (borrowed, not freed by the checker) |
| int jwt_checker_error | ( | const jwt_checker_t * | checker | ) |
Checks error state of checker object.
| checker | Pointer to a checker object |
| void jwt_checker_error_clear | ( | jwt_checker_t * | checker | ) |
Clear error state in a checker object.
| checker | Pointer to a checker object |
| const char * jwt_checker_error_msg | ( | const jwt_checker_t * | checker | ) |
Get the error message contained in a checker object.
| checker | Pointer to a checker object |
| int jwt_checker_expect_typ | ( | jwt_checker_t * | checker, |
| const char * | typ ) |
Require a specific token media type ("typ" header).
When set, jwt_checker_verify() rejects a token whose "typ" header does not match typ. The comparison is case-insensitive and tolerates the optional application/ prefix (RFC 6838), so expect_typ(c, "at+jwt") accepts both "at+jwt" and "application/at+jwt". This is the standardized cross-JWT-confusion defense (📄 RFC-8725 §3.11).
| checker | Pointer to a checker object |
| typ | The required media type, or NULL to clear the requirement |
| void jwt_checker_free | ( | jwt_checker_t * | checker | ) |
Frees a previously created checker object.
| checker | Pointer to a checker object |
| void * jwt_checker_getctx | ( | jwt_checker_t * | checker | ) |
Retrieve the callback context that was previously set.
This is useful for accessing the context that was previously passed in the setcb function.
| checker | Pointer to a checker object |
| jwt_checker_t * jwt_checker_new | ( | void | ) |
Function to create a new checker instance.
| int jwt_checker_require | ( | jwt_checker_t * | checker, |
| const char ** | claims, | ||
| unsigned int | count ) |
Require that a set of claims is present.
When set, jwt_checker_verify() rejects a token that is missing any of the named claims, independent of any value match (a value check is a separate concern – see jwt_checker_claim_set()). LibJWT otherwise requires only the claims whose value it is told to compare (a pinned iss, sub or aud) and tolerates an absent exp, nbf or any other claim; this asserts the mandatory-claims discipline that profiles such as RFC 9068 (at+jwt) require – e.g. {"iss","exp","aud","sub","client_id","jti"}. The names are copied. Passing count as 0 (or claims as NULL) clears the requirement. A claim whose value is JSON null is present (before 3.8.0, json-c called it missing).
An 📄 RFC-7797 unencoded ("b64":false) payload carries no claims (RFC-7797 Sec 7), so any requirement rejects it (see jwt_checker_verify()).
| checker | Pointer to a checker object |
| claims | An array of claim names that must be present (copied) |
| count | The number of names in claims |
| int jwt_checker_setalgs | ( | jwt_checker_t * | checker, |
| const jwt_alg_t * | algs, | ||
| size_t | n ) |
Set an allowlist of acceptable algorithms.
Restricts the algorithms jwt_checker_verify() will accept to the given set, checked before any signature work (📄 RFC-8725). Useful when verifying against a keyring (jwt_checker_setkeyring()) where several algorithms are acceptable, e.g. {JWT_ALG_RS256, JWT_ALG_ES256}. A token whose "alg" is not in the set is rejected, which also blocks an alg:none downgrade. Passing n as 0 (or algs as NULL) clears the allowlist.
With no allowlist set (the default, or once cleared) any signed algorithm is accepted, but an unsigned ("alg":"none") token is not: accepting one is opt-in (📄 RFC-8725 §3.2). List JWT_ALG_NONE here (alone, or alongside the signed algorithms) to accept unsigned tokens; a signed-only set keeps them out, as it always has. This is the supported way to consume unsigned tokens on purpose, and the only one.
| checker | Pointer to a checker object |
| algs | An array of acceptable jwt_alg_t values (copied) |
| n | The number of entries in algs |
| int jwt_checker_setcb | ( | jwt_checker_t * | checker, |
| jwt_callback_t | cb, | ||
| void * | ctx ) |
Set a callback for verifying tokens.
When verifying a token, this callback will be run after jwt_t has been parsed, but before the token is verified (including signature verification). During this, the callback must only inspect the header and claims in the JWT, never change them: the checks that follow read the jwt_t as the callback leaves it, so deleting the exp claim, for example, lets an expired token pass.
Treat what the callback sees as unverified: the header and claims count as verified only once jwt_checker_verify() (or jwt_checker_verify_detached()) returns 0. Act on them (e.g. record them) only then, reading them from jwt_checker_verified_jwt() and jwt_checker_sig_header_get(), or act on the claims in a jti callback (jwt_checker_setjti()), which runs after the signature and claim checks. Keep no pointer into the jwt_t the callback receives: it may be freed as soon as the verify returns. In a JWS JSON Serialization with several signatures, each has its own header, and under JWT_VERIFY_POLICY_ANY the token is accepted if one of them verifies, so a header the callback saw may belong to a signature that did not. The callback is not told which signature it runs for, and one that the "typ" or algorithm policy skips gets no call, so its calls do not line up with the indexes of jwt_checker_sig_verified(): rely on such a header only under JWT_VERIFY_POLICY_ALL, where every signature of an accepted token verified.
The callback can also set the key and algorithm used to verify the signature. A key in error (jwks_item_error()) is never used: the signature it was set for does not verify. If the callback returns non-zero, then processing will stop and return an error, except as described next for a JWS JSON Serialization. A JWS JSON Serialization needs a key or keyring on the checker: one with neither fails it before the callback runs.
For a JWS JSON Serialization, and a Compact token checked against a keyring (which is verified as one), the callback runs once for each signature that meets the "typ" and algorithm policy (until an error such as an unsupported "crit" header ends the verification), just before that signature is verified. It sees that signature's protected header, but not its unprotected "header", and the token's claims. config->alg is the algorithm given to jwt_checker_setkey(), if any, or else that signature's own "alg", and the signature verifies only if its "alg" is the one in config->alg when the callback returns (for JWT_ALG_NONE, the key's "alg", if it has one, and nothing if it has none, just as on the single-key Compact path). A non-zero return only leaves that signature unverified, for the jwt_verify_policy_t to decide: under JWT_VERIFY_POLICY_ANY the token is still accepted if another of its signatures verifies, and under JWT_VERIFY_POLICY_ALL it is rejected. A Compact token has only the one signature, so it is rejected under either policy.
What config->key holds when the callback runs, and what leaving it NULL means:
The ctx value is also passed to the callback as part of the jwt_config_t struct.
| checker | Pointer to a checker object |
| cb | Pointer to a callback function |
| ctx | Pointer to data to pass to the callback function |
| int jwt_checker_setjti | ( | jwt_checker_t * | checker, |
| jwt_jti_check_cb_t | cb, | ||
| void * | ctx ) |
Set a callback to verify the jti (JWT ID) claim (RFC-7519 Sec 4.1.7).
When set, verification reads the token's "jti" claim and passes it to the callback, which returns 0 to accept or non-zero to reject the token. This is where an application implements replay protection against its own id pool (look the id up and consume it). When this callback is set, a token that has no "jti" claim is rejected.
It runs once per token, last: only after the signature check (for a JWS JSON Serialization, the jwt_verify_policy_t) and every claim check have passed, so the claims it sees are verified, and a non-zero return rejects the token under either policy. For a JWS JSON Serialization, and a Compact token checked against a keyring, the jwt_t it receives holds the claims but no header (jwt_header_get() fails), and jwt_get_alg() names the last signature's algorithm, whether or not that signature verified. Once jwt_checker_verify() returns 0, jwt_checker_verified_jwt() and jwt_checker_sig_header_get() give the claims, the protected headers and the verified algorithm.
The ctx is passed to the callback via the jwt_config_t structure.
| checker | Pointer to a checker object |
| cb | Pointer to a jti verification callback |
| ctx | Pointer to data to pass to the callback |
| int jwt_checker_setkey | ( | jwt_checker_t * | checker, |
| const jwt_alg_t | alg, | ||
| const jwk_item_t * | key ) |
Sets a key and algorithm for a checker.
See jwt_builder_setkey for detailed information.
A signature, of a Compact token or of a JWS JSON Serialization alike, verifies only if it uses exactly that algorithm (with JWT_ALG_NONE, the key's "alg", and a key with none verifies nothing), not merely one the key could check (e.g. RS512 or PS256 when pinned to RS256). A verify callback that sets config->alg pins that algorithm instead. The key is used whatever its "use" or "key_ops", unlike the keys of a keyring (see jwt_checker_setkeyring()), but a key that is in error (jwks_item_error()) is refused. The checker keeps the key or keyring it has, and one that has neither is not left unkeyed: it rejects every token, an unsigned one included, until it is given a key or keyring, its verify callback supplies the key, or this is called with a NULL key (which leaves it unkeyed; see below).
A NULL key (with JWT_ALG_NONE) also drops a keyring. Unless embedded-JWK verification is enabled (this does not turn it off), the checker is then unkeyed: it verifies no JWS JSON Serialization, and no Compact token unless a verify callback supplies a key. An unsigned ("alg":"none") Compact token is the one kind an unkeyed checker can still verify, but only once unsigned tokens are allowed by listing JWT_ALG_NONE in jwt_checker_setalgs(); otherwise it, too, is rejected (📄 RFC-8725 §3.2).
Called from a verify or jti callback, though, it only removes the key: a checker that was given a key or keyring still requires one, so every later token fails unless a callback supplies the key, until this is called with a NULL key outside a verify. It does not fail the token being verified, whose signature is still checked with config->key (a jti callback runs only after that check), though later signatures of the same JWS JSON Serialization find no key. To reject that token, the verify callback returns non-zero.
| checker | Pointer to a checker object |
| alg | A valid jwt_alg_t type |
| key | A JWK key object |
| int jwt_checker_setkeyring | ( | jwt_checker_t * | checker, |
| const jwk_set_t * | keyring, | ||
| jwt_verify_policy_t | policy ) |
Set a keyring and policy for verification.
Supplies a set of candidate keys (a JWKS) instead of the single key set by jwt_checker_setkey(), e.g. for key rotation, several issuers, or a JWS JSON Serialization with multiple signatures. Every token is verified against it: a JSON Serialization signature by signature, and a Compact token exactly as the equivalent one-signature (Flattened) JSON token.
Only ring keys that loaded and are meant for signatures are candidates: not a key in error (jwks_item_error()), nor one whose "use" is present and is not "sig", nor one whose "key_ops" is present and includes neither "verify" nor "sign" (e.g. a key-wrapping key). A key that is not a candidate is skipped as if it were not in the ring, and the other keys are used as usual. Every key that libjwt imports (jwks_load_fromkey()) or generates (jwks_generate()) is a candidate. A signature whose protected header names a kid is verified only against the candidate with that kid; if there is none, that signature does not verify (there is no fallback to the other keys). A kid that is not a string, in either header, fails the verification. A signature without a kid is tried against every compatible candidate, in ring order, until one verifies; that first key is the one jwt_checker_sig_key() reports.
Each candidate is held to the usual algorithm/key-type binding and, when the key declares an "alg", to exactly that algorithm. The "typ" and algorithm policy, the verify callback (run once for each signature that meets that policy, until an error such as an unsupported "crit" header ends the verification), the claim checks and the jti check all apply, and an unsigned ("alg":"none") token is never accepted. The callback may supply the key, which is then the only one tried, whatever its "use" or "key_ops" (unless it is in error: then none is); if it sets config->alg, a signature with another "alg" does not verify, and if it sets it to JWT_ALG_NONE each candidate is pinned to the candidate's own "alg" (a candidate without one then verifies nothing).
A ring key is not tied to an issuer: when the ring holds the keys of several issuers, a token without a kid verifies with whichever of them signed it. To bind an issuer to its keys, select the key from the token's iss in the verify callback and return non-zero for an unknown or missing iss (leaving the key NULL lets a token without a kid be tried against the whole ring), or use one checker and keyring per issuer.
Under JWT_VERIFY_POLICY_ANY (the default and the generalization of the single-key behavior) the token is accepted if at least one signature verifies; under JWT_VERIFY_POLICY_ALL every signature in the token must verify. A Compact token carries exactly one signature, so it meets either policy when that signature verifies. The keyring is borrowed: the caller retains ownership and must keep it valid for the lifetime of the checker. Setting a keyring and setting a single key are mutually exclusive; the last call wins. When embedded-JWK verification is enabled (jwt_checker_enable_embedded_jwk()), a Compact token is still verified with the confirmed header key rather than the keyring, while a JWS JSON Serialization is verified against the keyring.
A Compact token is checked against a keyring directly, over its own bytes: it is split and decoded once, as on the single-key path, and costs about as much. Each further candidate key adds only its own signature check. A JWS JSON Serialization is parsed as JSON first.
| checker | Pointer to a checker object |
| keyring | A JWKS of candidate keys (borrowed, not freed by the checker) |
| policy | A jwt_verify_policy_t value |
| int jwt_checker_setmaxsize | ( | jwt_checker_t * | checker, |
| size_t | size ) |
Set the maximum size of a token this checker verifies.
jwt_checker_verify() and jwt_checker_verify_detached() reject a token longer than this many bytes (not counting its terminating NUL) with the error "Token too large", before any of it is copied, decoded or parsed, and reading no more of it than the limit plus one byte. Every token is held to it: Compact and JWS JSON Serialization alike, leading whitespace included. For jwt_checker_verify_detached() it applies both to the detached token as given and to the token rebuilt with the payload attached, which is the one that is verified, so a large detached payload needs a higher limit too.
A new checker allows 1048576 bytes (1 MiB), far more than any real token needs. Lower it to match the tokens a service expects, or raise it if it genuinely handles larger ones. The limit stays until it is set again or the checker is freed; a new checker starts at the default.
size must be at least 1 and at most INT_MAX - 1, the longest token the library can handle. Anything else is refused: this returns non-zero with the error set in the checker, and the limit is left as it was.
| checker | Pointer to a checker object |
| size | The maximum token length in bytes |
| unsigned int jwt_checker_sig_count | ( | const jwt_checker_t * | checker | ) |
Number of signatures in the last verified token.
After jwt_checker_verify() or jwt_checker_verify_detached(), returns how many signatures the token carried: 1 for a Compact or Flattened token, N for a General one. It is 0 if verification failed before the signatures were parsed and, for a Compact token not checked against a keyring (see jwt_checker_setkeyring()), whenever the token was rejected. A non-zero count does not mean that the token was accepted (see jwt_checker_sig_verified()).
| checker | Pointer to a checker object |
| jwt_value_error_t jwt_checker_sig_header_get | ( | jwt_checker_t * | checker, |
| unsigned int | index, | ||
| jwt_value_t * | value ) |
Get a protected-header member of one verified signature.
After jwt_checker_verify() or jwt_checker_verify_detached() returns 0, reads a member of the protected header of signature index the way jwt_header_get() reads one from a jwt_t. Set value up with one of the jwt_set_GET_*() macros, e.g. jwt_set_GET_STR(&value, "kid"), or jwt_set_GET_JSON(&value, NULL) for the whole header. This is how to read each signature's header, including its "alg", in a General JWS JSON Serialization; for a Compact or Flattened token (index 0) it reads the same header as jwt_header_get() on jwt_checker_verified_jwt().
Only a signature that verified is readable, and only once the token was accepted: under JWT_VERIFY_POLICY_ANY a signature that did not verify is not, and after a failed verify none is, nor during one (from a verify or jti callback). A signature's unprotected "header" is never read.
As with jwt_checker_verified_jwt(), a JWT_VALUE_STR value points into data the checker owns until its next verify call or jwt_checker_free(); a JWT_VALUE_JSON value is a new string for the caller to free, as from jwt_header_get().
| checker | Pointer to a checker object |
| index | A signature index in [0, jwt_checker_sig_count()) |
| value | A jwt_value_t structure with relevant actions filled in |
checker or value is NULL, the last verify did not accept a token, or signature index does not exist or did not verify; otherwise as jwt_header_get() (e.g. JWT_VALUE_ERR_NOEXIST for an absent member). The value.error field matches the return value whenever value is not NULL. | const jwk_item_t * jwt_checker_sig_key | ( | const jwt_checker_t * | checker, |
| unsigned int | index ) |
The key a given signature of the last token verified against.
Like jwt_checker_sig_verified(), this reports the signature check alone: it returns a verified signature's key even if jwt_checker_verify() rejected the token.
| checker | Pointer to a checker object |
| index | A signature index in [0, jwt_checker_sig_count()) |
index, or NULL if it did not verify, it is an accepted unsigned token's (which has no key) or the index is out of range. It is borrowed, so do not free it: it belongs to the keyring, or to whoever supplied it (jwt_checker_setkey() or the verify callback), except that a key confirmed by embedded-JWK verification (always a public key) belongs to the checker, which keeps it only until the next jwt_checker_verify() or jwt_checker_verify_detached() call on checker (even one that fails at once, as on a NULL or empty token) or jwt_checker_free(). | int jwt_checker_sig_verified | ( | const jwt_checker_t * | checker, |
| unsigned int | index ) |
Whether a given signature of the last token verified.
After jwt_checker_verify() or jwt_checker_verify_detached(), reports per-signature results. Useful under JWT_VERIFY_POLICY_ANY to learn which signature(s) passed.
| checker | Pointer to a checker object |
| index | A signature index in [0, jwt_checker_sig_count()) |
index verified, 0 otherwise (including an out-of-range index). An unsigned token that an unkeyed checker accepted counts as verified, with no key. | int jwt_checker_understands | ( | jwt_checker_t * | checker, |
| const char * | header ) |
Declare a critical header parameter as understood (RFC-7515 Sec 4.1.11).
Per RFC 7515, if a token's crit (Critical) header parameter lists a header name, the recipient MUST understand and process that header or else reject the token. LibJWT understands no extension header parameters on its own, so by default any token carrying a crit header will fail verification.
Use this function to declare each extension header parameter that your application is prepared to handle (typically inspected in your verify callback). During verification, every name listed in crit must both be present in the header and have been declared here; otherwise the token is rejected.
| checker | Pointer to a checker object |
| header | Name of the critical header parameter the application understands |
| jwt_t * jwt_checker_verified_jwt | ( | jwt_checker_t * | checker | ) |
The token that the last verify accepted.
After jwt_checker_verify() or jwt_checker_verify_detached() returns 0, the checker keeps the token it accepted. Read it with jwt_claim_get(), jwt_header_get(), jwt_get_alg() and jwt_get_cnf():
Nothing is returned after a failed verify, even where jwt_checker_sig_verified() reports a signature as verified (e.g. the token expired, failed a claim check or did not meet JWT_VERIFY_POLICY_ALL), nor to a verify or jti callback, which runs while the token is being verified: use the jwt_t passed to the callback there.
A JWT_VALUE_STR read from the token (e.g. with jwt_claim_get()) points into it, so it lasts only as long as the token does. Do not free or modify the token.
| checker | Pointer to a checker object |
checker). It belongs to checker, which keeps it only until the next jwt_checker_verify() or jwt_checker_verify_detached() call on checker (even one that fails at once, as on a NULL or empty token) or jwt_checker_free(). | int jwt_checker_verify | ( | jwt_checker_t * | checker, |
| const char * | token ) |
Verify a token.
Accepts the Compact Serialization or the JWS JSON Serialization (RFC-7515 Sec 7.2), auto-detected by a { as the first byte after any leading space, tab, CR or LF.
An 📄 RFC-7797 unencoded ("b64":false) token is accepted only if "b64" is listed in its "crit" header. In a JSON token, "b64" and "crit" may appear only in protected headers (RFC-7515 Sec 4.1.11), and every signature must carry the same "b64" value (RFC-7797 Sec 3); a token that breaks either rule is rejected under any keyring policy.
An unencoded payload is never JWT claims (RFC-7797 Sec 7), so it is checked as an empty claim set: the default exp/nbf checks (validate if present) pass it, but a checker that pins iss, sub or aud (jwt_checker_claim_set()), requires claims (jwt_checker_require()) or checks jti (jwt_checker_setjti()) rejects it with the error Claims cannot be validated on an unencoded ("b64":false) payload (RFC 7797). Verify such documents with a separate checker that does none of these. Before 3.6.2, the pinned-value and required-claim checks were skipped for an unencoded payload.
These rules apply equally to jwt_checker_verify_detached().
A token longer than the checker's maximum size, 1 MiB unless changed with jwt_checker_setmaxsize(), is rejected with the error "Token too large" before it is parsed. Before 3.8.0 only a token of INT_MAX bytes or more was rejected that way.
Once this returns 0, jwt_checker_verified_jwt() gives the accepted token (its claims and, for a Compact or Flattened token, its protected header and verified algorithm) and jwt_checker_sig_header_get() each verified signature's protected header, until the next verify call on checker. This call releases the previous token first, whatever its outcome.
| checker | Pointer to a checker object |
| token | A string containing a token to be verified |
| int jwt_checker_verify_detached | ( | jwt_checker_t * | checker, |
| const char * | token, | ||
| const unsigned char * | payload, | ||
| size_t | len ) |
Verify a token whose payload was detached.
Verifies a token produced with jwt_builder_set_detached() (or any JWS with a detached payload), supplying the payload out-of-band. Works for both base64url and 📄 RFC-7797 unencoded ("b64":false) payloads; the signing input is reconstructed from payload according to the token's "b64" header. As with jwt_checker_verify(), an unencoded payload is accepted only if "b64" is present in the token's "crit" header (RFC 7797 §6).
A base64url payload is verified byte for byte, NULs included. An unencoded payload is spliced into the token as text, so it must not contain a NUL byte (jwt_builder_generate() will not sign one either): such a payload is rejected, never verified only up to the NUL. In the JWS JSON Serialization it must also be a valid JSON string, which with the Jansson JSON backend means valid UTF-8. A Compact token takes any other bytes, whether checked with a single key or against a keyring (before 3.8.0, with Jansson, a keyring refused a payload that was not valid UTF-8).
jwt_checker_verify() reads a base64url payload as a JWT claim set, though, so it must be a JSON object (see jwt_builder_setpayload()): a binary document can be signed base64url-encoded, but not verified.
The checker's maximum token size (see jwt_checker_setmaxsize()) applies to token as given and to the token rebuilt with the payload attached (in its base64url form, about 4/3 of len, for a base64url payload), so the payload counts toward it. Either one over the limit, or a len over it on its own, fails with "Token too large" (since 3.8.0).
Any error already set in checker is cleared on entry and every failure sets one, so jwt_checker_error() and jwt_checker_error_msg() always reflect this call. Likewise the token the last verify accepted is released on entry, and on success jwt_checker_verified_jwt() returns this one (see there).
| checker | Pointer to a checker object |
| token | A string containing the (detached) token to verify |
| payload | The out-of-band payload bytes |
| len | The payload length in bytes. For a C string pass its strlen(): an unencoded payload that counts the terminating NUL is rejected. |