jose-rs
Pure-Rust JOSE (JSON Object Signing and Encryption) library covering JWS, JWE, JWK, and JWT standards. Built on kryptering for cryptographic operations, supporting both in-memory software keys and PKCS#11 HSM-backed keys.
Standards
| Standard | RFC | Coverage |
|---|---|---|
| JWS | 7515 | Compact + Flattened JSON + General JSON serialization |
| JWE | 7516 | Compact serialization |
| JWK | 7517 | All key types, JWK Sets, Thumbprints (RFC 7638), key generation |
| JWT | 7519 | Claims, validation (exp/nbf/iss/aud/sub), nested JWT |
| JWA | 7518 | All standard signature, encryption, and key management algorithms |
Supported algorithms
JWS signatures: HS256/384/512, RS256/384/512, PS256/384/512, ES256/384/512, EdDSA
JWS post-quantum signatures (opt-in): ML-DSA-44 / ML-DSA-65 / ML-DSA-87 (FIPS 204), enabled with --features post-quantum. See Post-quantum signatures below.
JWE key management: dir, A128KW/A192KW/A256KW, RSA-OAEP, RSA-OAEP-256
JWE content encryption: A128GCM/A192GCM/A256GCM, A128CBC-HS256/A192CBC-HS384/A256CBC-HS512
Feature flags
| Feature | Default | Description |
|---|---|---|
pkcs11 |
Yes | PKCS#11 HSM key support via kryptering |
post-quantum |
No | ML-DSA, SLH-DSA algorithms |
deprecated |
No | Legacy algorithms (RSA1_5, none) |
Usage
JWT (sign and verify)
use ;
use ;
// Create a signer (HMAC-SHA256)
let key = Hmac;
let signer = new.unwrap;
// Encode
let header = jwt;
let mut claims = default;
claims.iss = Some;
claims.sub = Some;
let token = encode.unwrap;
// Decode and validate
let verifier = new.unwrap;
let validation = new.with_issuer;
let decoded = decode.unwrap;
assert_eq!;
JWE (encrypt and decrypt)
use ;
let cek = ; // 256-bit key for A256GCM
let plaintext = b"sensitive data";
let token = encrypt.unwrap;
let decrypted = decrypt.unwrap;
assert_eq!;
JWK (key management)
use jwk;
// Generate keys
let rsa_jwk = generate_rsa.unwrap;
let ec_jwk = generate_ec.unwrap;
let ed_jwk = generate_ed25519.unwrap;
// Convert to/from kryptering keys
let software_key = jwk_to_software_key.unwrap;
let roundtripped = software_key_to_jwk.unwrap;
// Thumbprint (RFC 7638)
let thumbprint = thumbprint_sha256.unwrap;
HSM-backed signing
use ;
use Claims;
// The same JWT API works with HSM keys -- just pass an HSM-backed signer
let provider = new.unwrap;
let session = provider.open_session.unwrap;
let signer = new.unwrap;
let header = jwt;
let claims = default;
let token = encode.unwrap;
Examples
See the examples directory for complete, runnable examples covering all major JOSE operations:
- generate_keys -- generate RSA, EC P-256, Ed25519, HMAC, and AES keys as JWK files
- jwt_hmac -- JWT sign/verify with HMAC-SHA256
- jwt_rsa -- JWT sign with RS256, verify with public key
- jwt_ecdsa -- JWT sign with ES256, verify with public key
- jwt_eddsa -- JWT sign with EdDSA (Ed25519), verify with public key
- jwe_aes_kw -- JWE encrypt/decrypt with AES Key Wrap + AES-GCM
- jwe_rsa_oaep -- JWE encrypt with RSA-OAEP, decrypt with private key
- jws_json -- JWS flattened and general JSON serialization (multi-signature)
- jwk_thumbprint -- RFC 7638 JWK Thumbprint for all key types
- nested_jwt -- sign a JWT then encrypt it inside a JWE
Run cargo run --example generate_keys first to create the key files, then run any other example.
Security notes
-
rsacrate advisory (RUSTSEC-2023-0071, a.k.a. "Marvin attack"). The upstreamrsacrate used by this library has a known timing side-channel in PKCS#1 v1.5 and OAEP decryption. The vulnerability affects JWERSA-OAEP-256(and, with thedeprecatedfeature,RSA-OAEP) on the decryption side. There is no patchedrsa0.9.x release yet. If you operate a service that decrypts JWEs from untrusted senders, rate-limit decryption failures and prefer non-RSA key management algorithms. -
Trust-sensitive header fields.
jku,x5u, andjwkheaders are parsed by the library but never fetched or trusted — callers must never resolvejku/x5uURLs without an explicit allow-list (SSRF / key-substitution risk) and must never treat an inlinejwkheader as a verification key without first verifying it against a trusted key store. -
jwt::decode_unverifiedis for inspection only. It returns the header and claims without any cryptographic check. Production code paths must calljwt::decodewith a real verifier andValidation. -
Algorithm binding.
jws/jwtverify functions reject tokens whosealgheader does not match the verifier's algorithm and always rejectalg: "none"— even with thedeprecatedfeature enabled. Non-emptycritheaders are rejected per RFC 7515 §4.1.11. -
RSA minimum key size. Keys below 2048 bits are rejected at parse and at generation time (RFC 7518 §3.3 / §4.2).
-
AES-GCM nonce collision bound. Content encryption with A128GCM / A192GCM / A256GCM uses a 96-bit random nonce per RFC 7518 §5.3. Under a single CEK the collision probability reaches 2⁻³² after ~2³² messages (NIST SP 800-38D §8.3). For high-throughput senders, rotate CEKs well before that bound — the
dirmode with a fresh CEK per message avoids the issue entirely. -
Token size cap. The JWS and JWE decoders reject any input larger than
jose_rs::MAX_TOKEN_BYTES(1 MiB) before allocating any base64url buffer, to bound DoS from oversized attacker-supplied tokens. -
Debug output redaction.
Jwk'sDebugimplementation redacts private components (d,p,q,dp,dq,qi,k) — logging a privateJwkwill not spill the private material. -
CEK zeroization. Content Encryption Keys generated or recovered inside JWE
encrypt/decrypt— including theMAC_KEY || ENC_KEYsplit used for AES-CBC-HS — are wrapped inzeroize::Zeroizingand wiped from the heap when they go out of scope. This does not extend to key material held inside the underlying cipher crates (which manage their own state), and the decrypted plaintext returned to the caller is not zeroized — if your plaintext is itself a secret, wrap it on the caller side. -
Signing-side algorithm binding. Symmetric to the verify path, the
jws/jwtsign functions reject a header whosealgdoes not match the signer's algorithm, refusealg: "none", and refuse a non-emptycrit. This prevents emitting malformed tokens whose header advertises a different algorithm than the one actually used. -
JWT Best Current Practices (RFC 8725).
Validationsupports pinning thetypheader (with_typ), capping token age viaiat(with_max_age), rejecting future-datediatby default, and restricting the accepted signing algorithms independently of the verifier (with_allowed_algorithms). These close the JWT-context confusion and replay-window gaps that fall outside JWS-layer verification. -
JWK authorization (RFC 7517 §4.2/§4.3). Call
Jwk::check_opbefore using a key. If the JWK'suseorkey_opsfield is set, the call enforces it — a verify-only key cannot be used to sign, anenc-marked key cannot be used for signatures, and so on.Jwkalso providesto_public_jwk()for safely exporting a key (it strips every private component —d,p,q,dp,dq,qi,k). -
JWK
alg/ktyconsistency. Importing a JWK whosealgcontradicts itskty(e.g.alg: "RS256"on akty: "oct"key, oralg: "ES256"on aP-384curve) is rejected at conversion time with a clear error, rather than failing opaquely downstream. -
HMAC key minimum length (RFC 7518 §3.2). When an
octJWK declaresalg: "HS256","HS384", or"HS512", thekmaterial must be at least as long as the hash output (32, 48, or 64 bytes respectively). Shorter keys are rejected at JWK import. -
Private JWK fields zeroize on drop.
Jwk'sDropimpl callszeroize::Zeroizeond,p,q,dp,dq,qi, andkwhen present, so private material is wiped from the heap before the allocation is returned to the allocator. -
jwt::decode_unverifiedis#[deprecated]. Every call site now emits a compiler warning pointing users tojwt::decode. The function remains available for legitimate pre-verification inspection (e.g. reading a token'skidto select the right verifier). -
JWK-first API (safer one-shot for every operation). Prefer the
*_with_jwkfunctions over the manual "build a signer/verifier yourself" path:- Sign/verify:
jws::compact::sign_with_jwk,jws::compact::verify_with_jwk,jwt::encode_with_jwk,jwt::decode_with_jwk,jwt::decode_with_jwkset. - Encrypt/decrypt:
jwe::encrypt_with_jwk,jwe::decrypt_with_jwk.
Each derives the algorithm from the JWK (or the token header, on the receive side), enforces
Jwk::check_opfor the intended operation (Sign/Verify/Encrypt/Decrypt/WrapKey/UnwrapKey), requires any pinnedjwk.algto agree with the token's header, and constructs the underlying signer/verifier or key-material form internally.decode_with_jwksetis the canonical OIDC flow (kid-lookup with fall-through to each key in the set). - Sign/verify:
Post-quantum signatures (experimental)
ML-DSA support is available behind the opt-in post-quantum feature:
[]
= { = "0.1", = ["post-quantum"] }
Enabling this pulls in the ml-dsa and pkcs8-pq crates plus kryptering's
post-quantum backend, and adds three JwsAlgorithm variants corresponding
to the IANA-registered identifiers ML-DSA-44, ML-DSA-65, ML-DSA-87
(FIPS 204).
JWK wire format (kty = "AKP")
Per draft-ietf-cose-dilithium, ML-DSA keys use the new "AKP"
("Algorithm Key Pair") key type with two base64url members:
pub— the raw FIPS 204 encoded public key (1312 / 1952 / 2592 bytes for the three security levels).priv— the 32-byte FIPS 204 seed (not an expanded private key). The expanded signing key is derived on demand viaML-DSA.KeyGen_internal(seed).
Jwk.priv is zeroized on Drop alongside the existing RSA/EC/oct
private-component wipe. Jwk::to_public_jwk() strips priv and keeps
pub, which is the correct shape for a JWK Set endpoint.
Status and caveats
-
Draft-spec, not yet RFC. The authoritative spec is
draft-ietf-cose-dilithium-11(active, submitted to IESG, expected to publish as an RFC in 2026). The wire format may shift before publication — do not use this feature for long-lived signed artifacts that you cannot re-issue later. -
ml-dsacrate history. The RustCryptoml-dsacrate shipped three moderate-severity advisories during its 0.1.0 release-candidate series. All three are fixed in versions earlier than our pin, andcargo auditis clean againstml-dsa 0.1.0-rc.8:Advisory Summary Patched from GHSA-hcp2-x6j4-29j7 Decomposetiming side-channel during signing>= 0.1.0-rc.3GHSA-5x2r-hc65-25f9 Repeated hint indices (signature malleability) >= 0.1.0-rc.4GHSA-h37v-hp6w-2pp8 UseHintoff-by-two>= 0.1.0-rc.5Because the pin is
=0.1.0-rc.8, any futureml-dsaadvisory will surface as an explicit lockfile event — consultcargo auditbefore bumping, and track RustCrypto/signatures for theml-dsa 0.1.0stable release. -
Default signing mode is randomized (hedged). ML-DSA sign uses real randomness per FIPS 204 Algorithm 2, so repeated signatures over the same message and key are expected to differ while still verifying. The optional deterministic signing variant (equivalent to a zero
rnd) is not currently exposed. -
ML-DSA-87 test note. The expanded signing key (~5 KiB) exceeds the default 2 MiB debug-build test-thread stack; the ML-DSA-87 round-trip test spawns itself on a larger-stack thread. Release builds have smaller stack frames and are unaffected.
Development tooling
Dependency audit
The repository is wired for cargo-audit.
Local run:
cargo install --locked cargo-audit
cargo audit
CI runs the same command on every push to main, every pull request
that touches Cargo.toml / Cargo.lock, and on a weekly schedule
(.github/workflows/audit.yml).
.cargo/audit.toml carries a single documented advisory ignore —
RUSTSEC-2023-0071 (the rsa crate Marvin timing advisory) — with
mitigations described above. Any advisory that is not in the
ignore list fails CI; the ignore file itself is the source of truth
for "what are we accepting, and why".
Interop test vectors (RFC 7520)
tests/rfc7520.rs verifies examples from
RFC 7520 — these are the
canonical worked vectors every JOSE implementation is expected to
reproduce, which gives the library a cross-implementation interop
baseline:
- JWS §4.1–4.4. RS256, PS384, ES512 verify; HS256 as a full byte-for-byte deterministic roundtrip (sign then byte-compare against the RFC's published compact serialization).
- JWE §5.6, §5.8. Direct encryption with A128GCM and A128KW + A128GCM — decrypt the RFC's published JWE and check the recovered plaintext.
- JWE §5.2. RSA-OAEP + A256GCM decrypt using the RFC's RSA-4096 key.
Runs only under
--features deprecatedbecause RSA-OAEP uses SHA-1.
All vectors are embedded verbatim from rfcs/rfc7520.txt with their
original indent-continuations intact; runtime whitespace-stripping
eliminates transcription risk in 1000+-char RSA key material.
Fuzzing
fuzz/ is a cargo-fuzz
project with eight targets covering the attacker-controlled parse and
verify paths (base64url, JWK JSON, thumbprint, JWS decode/verify, JWE
decrypt, JWT header/claims, and post-quantum JWS verify via
jws_verify_mldsa). See fuzz/README.md for the per-target breakdown.
rustup toolchain install nightly
cargo install --locked cargo-fuzz
cargo +nightly fuzz run jwk_from_json # run indefinitely
cargo +nightly fuzz run jws_verify -- -max_total_time=300
CI (.github/workflows/fuzz.yml) builds every target on every PR to
catch compilation regressions; the actual fuzzing runs are expected to
live in a longer-running job or OSS-Fuzz once the corpus stabilizes.
License
BSD-2-Clause