LibJWT Dev
The C JSON Web Token Library +JWK +JWKS
Algorithm Management

Set and check algorithms and algorithm specific values. More...

Collaboration diagram for Algorithm Management:

Functions

int jwt_set_alg (jwt_t *jwt, jwt_alg_t alg, const unsigned char *key, int len)
 Set an algorithm for a jwt_t object.
 
jwt_alg_t jwt_get_alg (const jwt_t *jwt)
 Get the jwt_alg_t set for this JWT object.
 
const char * jwt_alg_str (jwt_alg_t alg)
 Convert alg type to it's string representation.
 
jwt_alg_t jwt_str_alg (const char *alg)
 Convert alg string to type.
 

Detailed Description

Set and check algorithms and algorithm specific values.

When working with functions that require a key, the underlying library takes care to scrub memory when the key is no longer used (e.g. when calling jwt_free() or when changing the algorithm, the old key, if it exists, is scrubbed).

Function Documentation

◆ jwt_alg_str()

const char * jwt_alg_str ( jwt_alg_t alg)

Convert alg type to it's string representation.

Returns a string that matches the alg type provided.

Parameters
algA valid jwt_alg_t specifier.
Returns
Returns a string (e.g. "RS256") matching the alg or NULL for invalid alg.

◆ jwt_get_alg()

jwt_alg_t jwt_get_alg ( const jwt_t * jwt)

Get the jwt_alg_t set for this JWT object.

Returns the jwt_alg_t type for this JWT object.

Warning
This is the alg for the jwt_t object and NOT the one that may be set in the header. This is a programatic check to see what LibJWT will use to encode the object into a JWT. To see what is embedded in the header, use jwt_get_header(jwt, "alg") instead.
Parameters
jwtPointer to a JWT object.
Returns
Returns a jwt_alg_t type for this object.

◆ jwt_set_alg()

int jwt_set_alg ( jwt_t * jwt,
jwt_alg_t alg,
const unsigned char * key,
int len )

Set an algorithm for a jwt_t object.

Specifies an algorithm for a jwt_t object. If JWT_ALG_NONE is used, then key must be NULL and len must be 0. All other algorithms must have a valid pointer to key data, which may be specific to the algorithm (e.g RS256 expects a PEM formatted RSA key).

Parameters
jwtPointer to a jwt_t object.
algA valid jwt_alg_t specifier.
keyThe key data to use for the algorithm.
lenThe length of the key data.
Returns
Returns 0 on success, valid errno otherwise.

◆ jwt_str_alg()

jwt_alg_t jwt_str_alg ( const char * alg)

Convert alg string to type.

Returns an alg type based on the string representation.

RFC-7518 Sec 3.1

Parameters
algA valid string for algorithm type (e.g. "RS256").
Returns
Returns a jwt_alg_t matching the string or JWT_ALG_INVAL if no matches were found.

Note, this only works for algorithms that LibJWT supports or knows about.