jose_jwe(3)
π jose_jwe π
okay bestie, gather round, because today we're decoding jose_jwe(3) β the library functions manual page for JSON Web Encryption (RFC 7516). it's giving crypto, it's giving JosΓ©, it's giving "i have twelve recipients and only one ciphertext." slay.
β¨ NAME β¨
jose_jwe β JSON Web Encryption (RFC 7516)
π¦ SYNOPSIS π¦
Functions
json_t * jose_jwe_hdr (const json_t *jwe, const json_t *rcp)β Merges the JOSE headers of a JWE object and a JWE recpient object.bool jose_jwe_enc (jose_cfg_t *cfg, json_t *jwe, json_t *rcp, const json_t *jwk, const void *pt, size_t ptl)β Wraps and encrypts plaintext.jose_io_t * jose_jwe_enc_io (jose_cfg_t *cfg, json_t *jwe, json_t *rcp, const json_t *jwk, jose_io_t *next)β Wraps and encrypts plaintext using streaming.bool jose_jwe_enc_jwk (jose_cfg_t *cfg, json_t *jwe, json_t *rcp, const json_t *jwk, json_t *cek)β Wraps a CEK with a JWK.bool jose_jwe_enc_cek (jose_cfg_t *cfg, json_t *jwe, const json_t *cek, const void *pt, size_t ptl)β Encrypts plaintext with the CEK.jose_io_t * jose_jwe_enc_cek_io (jose_cfg_t *cfg, json_t *jwe, const json_t *cek, jose_io_t *next)β Encrypts plaintext with the CEK using streaming.void * jose_jwe_dec (jose_cfg_t *cfg, const json_t *jwe, const json_t *rcp, const json_t *jwk, size_t *ptl)β Unwraps and decrypts ciphertext.jose_io_t * jose_jwe_dec_io (jose_cfg_t *cfg, const json_t *jwe, const json_t *rcp, const json_t *jwk, jose_io_t *next)β Unwraps and decrypts ciphertext using streaming.json_t * jose_jwe_dec_jwk (jose_cfg_t *cfg, const json_t *jwe, const json_t *rcp, const json_t *jwk)β Unwraps a CEK with a JWK.void * jose_jwe_dec_cek (jose_cfg_t *cfg, const json_t *jwe, const json_t *cek, size_t *ptl)β Decrypts ciphertext with the CEK.jose_io_t * jose_jwe_dec_cek_io (jose_cfg_t *cfg, const json_t *jwe, const json_t *cek, jose_io_t *next)β Decrypts ciphertext with the CEK using streaming.
π Detailed Description π
JSON Web Encryption (RFC 7516) β the whole vibe in one line.
so a JSON Web Encryption (JWE) object is doing the most, in the best way. it contains (usually) two levels of encryption. first, the plaintext gets encrypted with a random symmetric key. in JosΓ© we call this key the Content Encryption Key (CEK). then, the CEK gets wrapped (aka encrypted) with one or more keys. those keys are standard JSON Web Keys (JWK) and they may be symmetric or asymmetric. periodt.
so the general rule is: one CEK, one or more JWKs. but bestie, there are exceptions, obviously. two such examples are the algorithms 'dir' and 'ECDH-ES'. in the first case, the JWK is a symmetric key and it's used directly as the CEK. in the second case, an ECDH key exchange happens and the result is used directly as the CEK. but in general, the maxim holds. π
this means you can encrypt your data with one CEK and then encrypt that CEK to multiple recipients. with this schema, multiple recipients can each decrypt the data using their own key, without you having to send separate ciphertext to each recipient. iconic, no notes. β¨
for maximum flexibility, JosΓ© structures its API to take advantage of this schema. for example, when encrypting to two recipients, the code could look like this (error handling omitted):
json_t *enc(void *plaintext, size_t len, json_t *jwk0, json_t *jwk1) {
json_auto_t *jwe = json_object();
json_auto_t *cek = json_object();
// Wrap to the first recipient (CEK generated implicitly)
jose_jwe_enc_jwk(NULL, jwe, NULL, jwk0, cek);
// Wrap to the second recipient
jose_jwe_enc_jwk(NULL, jwe, NULL, jwk1, cek);
// Encrypt plaintext using the generated CEK
jose_jwe_enc_cek(NULL, jwe, cek, plaintext, len);
return json_incref(jwe);
}
however β because JosΓ© intends to be easy to use β we also provide shortcuts. for example, you could use a JWKSet which contains multiple JWKs:
json_t *enc(void *plaintext, size_t len, json_t *jwkset) {
json_auto_t *jwe = json_object();
// Perform wrapping and encryption to all recipients
jose_jwe_enc(NULL, jwe, NULL, jwkset, plaintext, len);
return json_incref(jwe);
}
here are two tips to remember, bestie, write these on your mirror in lip gloss:
- Let JosΓ© generate your CEK implicitly.
- Always perform wrapping before encryption.
both of these tips are important because some wrapping algorithms may impose constraints on the generation of the CEK. π
to decrypt a JWE, we just reverse the process. first, we use a JWK to unwrap the CEK. then we use the CEK to decrypt the ciphertext. here's how that might look in code (again, error handling omitted):
void *dec(json_t *jwe, json_t *jwk, size_t *len) {
json_auto_t *cek = NULL;
// Unwrap the CEK using our JWK
cek = jose_jwe_dec_jwk(NULL, jwe, NULL, jwk);
// Decrypt ciphertext using the CEK
return jose_jwe_dec_cek(NULL, jwe, cek, len);
}
or, again, in simplified form:
void *dec(json_t *jwe, json_t *jwk, size_t *len) {
return jose_jwe_dec(NULL, jwe, NULL, jwk, len);
}
if you need to forward a JWE to a new recipient, you can do this without performing re-encryption. just unwrap the CEK and then wrap the CEK again using a new JWK. for example:
void fwd(json_t *jwe, json_t *oldjwk, json_t *newjwk) {
json_auto_t *cek = NULL;
// Unwrap the CEK using the old JWK
cek = jose_jwe_dec_jwk(NULL, jwe, NULL, oldjwk);
// Wrap the CEK to the new JWK
jose_jwe_enc_jwk(NULL, jwe, NULL, newjwk, cek);
}
in all the above examples, parameters like which encryption algorithms to use were inferred from our keys. where such an inference cannot be made, sensible and secure defaults were chosen automatically. if you would like more control over the process, simply set parameters in the appropriate objects (more on this in the function documentation). for example, to enable plaintext compression, you can specify the zip property in the JWE Protected Header:
json_t *enc(void *plaintext, size_t len, json_t *jwkset) {
json_auto_t *jwe = json_pack("{s:{s:s}}", "protected", "zip", "DEF");
// Perform compressed wrapping and encryption to all recipients
jose_jwe_enc(NULL, jwe, NULL, jwkset, plaintext, len);
return json_incref(jwe);
}
JosΓ© currently supports the 'alg', 'enc' and 'zip' header parameters, as well as any algorithm-specific header parameters used by the specific algorithms we implement. other header parameters may be specified, but they do not effect the behavior of JosΓ©'s JWE implementation. π«Ά
See also:
π§ Function Documentation π§
πΈ json_t* jose_jwe_hdr (const json_t * jwe, const json_t * rcp)
Merges the JOSE headers of a JWE object and a JWE recpient object. merging, bestie. it's giving teamwork.
Parameters:
jweβ A JWE object.rcpβ A JWE recipient object.
Returns:
- The newly allocated JOSE header.
πΈ bool jose_jwe_enc (jose_cfg_t * cfg, json_t * jwe, json_t * rcp, const json_t * jwk, const void * pt, size_t ptl)
Wraps and encrypts plaintext. this function is a thin wrapper around the jose_jwe_enc_io() function, allowing the user to specify the plaintext in a single call. the ciphertext output will be appended to the JWE as the 'ciphertext' property. one call, whole job done. we love efficiency. π₯
See also:
jose_jwe_enc_cek_io()
Parameters:
cfgβ The configuration context (optional).jweβ The JWE object.rcpβ The JWE recipient object(s) orNULL.jwkβ The JWK(s) or JWKSet used for wrapping.ptβ The plaintext.ptlβ The length of the plaintext.
Returns:
- On success,
true. Otherwise,false.
πΈ jose_io_t* jose_jwe_enc_io (jose_cfg_t * cfg, json_t * jwe, json_t * rcp, const json_t * jwk, jose_io_t * next)
Wraps and encrypts plaintext using streaming. this function is a thin wrapper around the jose_jwe_enc_jwk() and jose_jwe_enc_cek_io() functions, removing the complexity of managing the CEK. you just vibe, it handles the CEK. π
See also:
jose_jwe_enc_jwk()jose_jwe_enc_cek_io()
Parameters:
cfgβ The configuration context (optional).jweβ The JWE object.rcpβ The JWE recipient object(s) orNULL.jwkβ The JWK(s) or JWKSet used for wrapping.nextβ The next IO object in the chain.
Returns:
- The new IO object or
NULLon error.
πΈ bool jose_jwe_enc_jwk (jose_cfg_t * cfg, json_t * jwe, json_t * rcp, const json_t * jwk, json_t * cek)
Wraps a CEK with a JWK. the purpose of this function is to wrap (encrypt) or, in some cases, produce the CEK (cek) from the provided JWK(s) (jwk). this function can be used in two modes: single-JWK and multi-JWK.
in single-JWK mode, the jwk parameter contains a JWK object and the rcp parameter must contain either a JWE recipient object or NULL, in which case a default empty JWE recipient object is created internally.
multi-JWK mode works exactly the same as single-JWK mode except that it performs multiple wrappings in a single call. this mode is enabled by passing either an array of JWK objects or a JWKSet as the jwk parameter. in this mode, the rcp parameter contains one of the following values:
- A JWE recipient object that will be used for all wrappings. in this case, a copy will be made for each wrapping and
rcpwill not be modified in any way. - An array of JWE recipient objects. each object will be used with its corresponding JWK from
jwk. if the arrays insigandjwkare a different size, an error will occur. NULL. this has the same effect as passingNULLfor each separate JWK.
in either mode, if the resulting JWE (jwe) would contain only a single recipient, the JWE will be represented in Flattened JWE JSON Serialization Syntax. otherwise, it will be represented in General JWE JSON Serialization Syntax.
if the 'alg' parameter is not specified in the JOSE Header, it will be inferred from the JWK and the chosen algorithm will be added to the JWE Per-Recipient Unprotected Header. likewise, any missing, required, algorithm-specific parameters will be either inferred or sensible, secure defaults will be chosen, and the results will be added to the JWE Per-Recipient Unprotected Header. the algorithm really said "i gotchu." π
if the provided CEK (cek) does not contain key material, it will be implicitly generated during the first call to jose_jwe_enc_jwk(). this important feature enables the use of the 'dir' and 'ECDH-ES' algorithms. in the case of the 'dir' algorithm, the JWK is the CEK and thus the key material is copied from jwk to cek. a similar situation arises with the algorithm 'ECDH-ES' where the result of a key exchange is used as the CEK; thus, the CEK is produced during the wrapping process. this feature implies that if multiple wrappings are created, only one of them may have the algorithm 'ECDH-ES' and it must be the first wrapping.
careful bestie!! attempting to use 'ECDH-ES' with an existing CEK will result in an error. π don't do it.
It's also totally possible to yeet a password JSON string as key input to the PBES2 family of algorithms anywhere a JWK is allowed, bestie. And if the 'alg' JOSE Header parameter isn't specified and you're using a JSON string instead of a JWK, the PBES2 family of algorithms will be inferred. π
Parameters:
cfgβ the configuration context (optional).jweβ the JWE object.rcpβ the JWE recipient object(s) orNULL.jwkβ the JWK(s) or JWKSet used for wrapping.cekβ the CEK to wrap (if empty, it gets generated).
Returns: On success, true. Otherwise, false. β¨
bool jose_jwe_enc_cek (jose_cfg_t * cfg, json_t * jwe, const json_t * cek, const void * pt, size_t ptl)
bool jose_jwe_enc_cek (jose_cfg_t * cfg, json_t * jwe, const json_t * cek,
const void * pt, size_t ptl)
This one encrypts plaintext with the CEK. It's a thin little wrapper around jose_jwe_enc_cek_io() so you can just pass the plaintext in a single call. The ciphertext output gets appended to the JWE as the 'ciphertext' property. Obsessed. π«Ά
See also: jose_jwe_enc_cek_io()
Parameters:
cfgβ the configuration context (optional).jweβ the JWE object.cekβ the CEK object.ptβ the plaintext.ptlβ the length of the plaintext.
Returns: On success, true. Otherwise, false.
jose_io_t* jose_jwe_enc_cek_io (jose_cfg_t * cfg, json_t * jwe, const json_t * cek, jose_io_t * next)
jose_io_t* jose_jwe_enc_cek_io (jose_cfg_t * cfg, json_t * jwe, const json_t
* cek, jose_io_t * next)
Encrypts plaintext with the CEK using streaming. You feed the plaintext through the returned IO object. The plaintext gets encrypted and written to the next IO object. Heads up: this IO object works on binary data, so depending on your situation you might need a URL-safe Base64 decoder on input and a URL-safe Base64 encoder on output. π
Compressed plaintext can be implicitly enabled by specifying the 'zip' parameter in the JWE Protected Header.
If the JWE Protected Header is a JSON object rather than an encoded string, this function will encode the JWE Protected Header to its URL-safe Base64 encoding as defined in RFC 7516. But bestie, listen: this function will never modify a JWE Protected Header that is already encoded. Periodt. π
If the 'enc' parameter is not specified in the JWE Protected Header or the JWE Shared Unprotected Header, it will be inferred from the CEK and stored in either the JWE Protected Header or the JWE Shared Unprotected Header (preferring the JWE Protected header if it can be modified).
Please note that the'tag'property will only be added to the JWE whenjose_io_t::done()returns. π
Parameters:
cfgβ the configuration context (optional).jweβ the JWE object.cekβ the CEK object.nextβ the next IO object in the chain.
Returns: The new IO object or NULL on error.
void* jose_jwe_dec (jose_cfg_t * cfg, const json_t * jwe, const json_t * rcp, const json_t * jwk, size_t * ptl)
void* jose_jwe_dec (jose_cfg_t * cfg, const json_t * jwe, const json_t *
rcp, const json_t * jwk, size_t * ptl)
Unwraps and decrypts ciphertext. This is a thin wrapper around jose_jwe_dec_io() so you can get the plaintext in a single call. The ciphertext input will be obtained from the JWE 'ciphertext' property. Slay. β¨
See also: jose_jwe_dec_io()
Parameters:
cfgβ the configuration context (optional).jweβ the JWE object.rcpβ the JWE recipient object(s) orNULL.jwkβ the JWK(s) or JWKSet used for wrapping.ptlβ the length of the plaintext (output).
Returns: The newly-allocated plaintext.
jose_io_t* jose_jwe_dec_io (jose_cfg_t * cfg, const json_t * jwe, const json_t * rcp, const json_t * jwk, jose_io_t * next)
jose_io_t* jose_jwe_dec_io (jose_cfg_t * cfg, const json_t * jwe, const
json_t * rcp, const json_t * jwk, jose_io_t * next)
Unwraps and decrypts ciphertext using streaming. This function is a thin wrapper around the jose_jwe_dec_jwk() and jose_jwe_dec_cek_io() functions, removing the complexity of managing the CEK. So you can just vibe. π
See also:
jose_jwe_dec_jwk()jose_jwe_dec_cek_io()
Parameters:
cfgβ the configuration context (optional).jweβ the JWE object.rcpβ the JWE recipient object(s) orNULL.jwkβ the JWK(s) or JWKSet used for unwrapping.nextβ the next IO object in the chain.
Returns: The new IO object or NULL on error.
json_t* jose_jwe_dec_jwk (jose_cfg_t * cfg, const json_t * jwe, const json_t * rcp, const json_t * jwk)
json_t* jose_jwe_dec_jwk (jose_cfg_t * cfg, const json_t * jwe, const json_t
* rcp, const json_t * jwk)
Unwraps a CEK with a JWK. The whole point of this function is to unwrap (decrypt) or, in some cases, produce the CEK (cek) from the provided JWK(s) (jwk). It can be used in two modes: single-JWK and multi-JWK. π
In single-JWK mode, the jwk parameter contains a JWK object and the rcp parameter must contain either a JWE recipient object you wish to unwrap or NULL, in which case all JWE recipients will be tried.
Multi-JWK mode works exactly the same as single-JWK mode except that it attempts to unwrap with multiple JWKs in a single call. This mode is enabled by passing either an array of JWK objects or a JWKSet as the jwk parameter. In this mode, the rcp parameter contains one of the following values:
- A JWE recipient object that will be used for all attempted unwrappings.
- An array of JWE recipient objects. Each object will be used with its corresponding JWK from
jwk. If the arrays insigandjwkare a different size, an error will occur. NULL. This has the same effect as passingNULLfor each separate JWK.
In either mode, a CEK is returned for the first JWK that successfully unwraps a CEK. Two exceptions to this rule are if the 'dir' or 'ECDH-ES' algorithms are used. In this case, a CEK may be returned which will fail during decryption since there is no way to completely validate the JWK with these algorithms. Thus, we suggest placing the keys for these algorithms last when unwrapping with multiple JWKs. No cap, that's important. π₯
If the 'alg' parameter is not specified in the JOSE Header, it will be inferred from the JWK. This includes using a JSON string in place of a JWK for the PBES2 family of algorithms.
Parameters:
cfgβ the configuration context (optional).jweβ the JWE object.rcpβ the JWE recipient object(s) orNULL.jwkβ the JWK(s) or JWKSet used for wrapping.
Returns: On success, a newly-allocated CEK object. Otherwise, NULL.
void* jose_jwe_dec_cek (jose_cfg_t * cfg, const json_t * jwe, const json_t * cek, size_t * ptl)
void* jose_jwe_dec_cek (jose_cfg_t * cfg, const json_t * jwe, const json_t *
cek, size_t * ptl)
Decrypts ciphertext with the CEK. This is a thin wrapper around jose_jwe_dec_cek_io() so you can get the plaintext in a single call. The ciphertext input will be obtained from the JWE 'ciphertext' property. π
See also: jose_jwe_dec_cek_io()
Parameters:
cfgβ the configuration context (optional).jweβ the JWE object.cekβ the CEK object.ptlβ the length of the plaintext (output).
Returns: The newly-allocated plaintext.
jose_io_t* jose_jwe_dec_cek_io (jose_cfg_t * cfg, const json_t * jwe, const json_t * cek, jose_io_t * next)
jose_io_t* jose_jwe_dec_cek_io (jose_cfg_t * cfg, const json_t * jwe, const
json_t * cek, jose_io_t * next)
Decrypts ciphertext with the CEK using streaming. You provide the ciphertext through the returned IO object. The ciphertext gets decrypted and written to the next IO object. Again, this IO object works on binary data, so you may need a URL-safe Base64 decoder on input and a URL-safe Base64 encoder on output, depending on your situation. π
Please note that validation of the ciphertext integrity protection is delayed until jose_io_t::done() returns. This means it is incredibly important to check this return value, and it is also important to be careful with the plaintext emitted until this check is performed. Like, seriously, bestie, don't skip that check. π
Compressed plaintext will be internally decompressed if the 'zip' property is appropriately defined.
If the 'enc' parameter is not specified in the JWE Protected Header or the JWE Shared Unprotected Header, it will be inferred from the CEK.
Please note that the'tag'property on the JWE will only be accessed whenjose_io_t::done()is called. So you may define it at any time on the JWE object before callingjose_io_t::done(). π¦
Parameters:
cfgβ the configuration context (optional).jweβ the JWE object.cekβ the CEK object.nextβ the next IO object in the chain.
Returns: The new IO object or NULL on error.
π Author π
Generated automatically by Doxygen for JosΓ© from the source code.
That's the whole vibe, bestie! This page is from JosΓ©, dated Tue May 30 2017, and it's all about jose_jwe(3). Stay iconic. πβ¨
π¦ where this page comes from
- alpine 3.24: jose-doc 14-r0 Β· Apache-2.0
- arch rolling: jose 15-2 Β· Apache-2.0
- centos-stream 10: libjose-devel 14-102.el10 Β· Apache-2.0
- centos-stream 9: libjose-devel 14-1.el9 Β· ASL 2.0
- centos-stream 9: libjose-devel 14-1.el9 Β· ASL 2.0
- debian bookworm: libjose-dev 11-2+deb12u1 Β· Apache-2.0
- debian trixie: libjose-dev 14-2 Β· Apache-2.0
- fedora 43: libjose-devel 14-5.fc43 Β· Apache-2.0
- fedora 43: libjose-devel 14-5.fc43 Β· Apache-2.0
- fedora 44: libjose-devel 14-6.fc44 Β· Apache-2.0
- fedora 44: libjose-devel 14-6.fc44 Β· Apache-2.0
- nixpkgs unstable: jose 14
- opensuse-leap 16.0: libjose0 13-bp160.1.14 Β· Apache-2.0
- opensuse-leap 16.0: libjose0 13-bp160.1.14 Β· Apache-2.0
- opensuse-leap 16.0: libjose0 13-bp160.1.14 Β· Apache-2.0
- opensuse-leap 16.0: libjose0 13-bp160.1.14 Β· Apache-2.0
- ubuntu jammy: libjose-dev 11-2 Β· Apache-2.0
- ubuntu noble: libjose-dev 13-1 Β· Apache-2.0
- ubuntu resolute: libjose-dev 14-2build1 Β· Apache-2.0
- ubuntu stonking: libjose-dev 14-2build2 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms