8000 Add explicit EC point-on-curve validation in ElytronEcdhEsAlgorithmProvider closes #53639 by atiqur-rahman-pro · Pull Request #53644 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Add explicit EC point-on-curve validation in ElytronEcdhEsAlgorithmProvider closes #53639 - #53644

Open
atiqur-rahman-pro wants to merge 3 commits into
keycloak:mainfrom
atiqur-rahman-pro:fix/elytron-ecdh-invalid-curve-53639
Open

atiqur-rahman-pro wants to merge 3 commits into
keycloak:mainfrom
atiqur-rahman-pro:fix/elytron-ecdh-invalid-curve-53639

Conversation

@atiqur-rahman-pro
Copy link
Copy Markdown
Contributor

Closes #53639

Description

In ElytronEcdhEsAlgorithmProvider.toPublicKey(), ephemeral public keys (EPK) from incoming JWE headers were decoded using jose4j's EcKeyUtil.publicKey() and forwarded directly to KeyAgreement.getInstance("ECDH") without explicitly validating that the point lies on the specified elliptic curve.

While #48960 added explicit point-on-curve checks to BCEcdhEsAlgorithmProvider and BCFIPSEcdhEsAlgorithmProvider, ElytronEcdhEsAlgorithmProvider remained dependent on downstream JCA provider resolution order during doPhase().

This PR:

  1. Adds explicit fail-fast point-on-curve validation (validatePointOnCurve) in ElytronEcdhEsAlgorithmProvider.toPublicKey(), checking both prime field membership (0 <= x < p, 0 <= y < p) and the curve equation (y² ≡ x³ + ax + b mod p).
  2. Uses only standard JCA/JDK primitives (java.security.spec.ECFieldFp, BigInteger) to respect the Elytron module's independence from BouncyCastle.
  3. Adds a regression test toPublicKeyRejectsOffCurvePoint in ElytronEcdhEsAlgorithmProviderTest verifying rejection of off-curve points.

Copilot AI balanced review requested due to automatic review settings October 7, 2026 07:51
@atiqur-rahman-pro
atiqur-rahman-pro requested a review from a team as a code owner October 7, 2026 07:51
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The regression test uses a noncanonical coordinate encoding, and the new field-boundary checks lack coverage.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Adds explicit EC point validation to the Elytron ECDH-ES provider.

Changes:

  • Validates coordinate bounds and curve equation before key construction.
  • Adds an off-curve EPK regression test.
File Description
ElytronEcdhEsAlgorithmProvider.java Implements fail-fast point validation.
ElytronEcdhEsAlgorithmProviderTest.java Tests rejection of an off-curve point.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +199 to +203
if (x.signum() < 0 || x.compareTo(p) >= 0) {
throw new IllegalArgumentException("EPK x coordinate is not in the field");
}
if (y.signum() < 0 || y.compareTo(p) >= 0) {
throw new IllegalArgumentException("EPK y coordinate is not in the field");
Copilot AI balanced review requested due to automatic review settings October 7, 2026 08:06
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The validation is correct, bounded to supported prime curves, and adequately tested.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)

@atiqur-rahman-pro
Copy link
Copy Markdown
Contributor Author

The Windows/JDK 17 job fails before compiling project sources, while buildnumber-maven-plugin resolves maven-scm-provider-hg:1.9.4. This does not appear related to the Elytron changes. Could someone with write access please rerun the failed job? I’m unable to rerun the upstream workflow due to permissions.

* and cofactor one, so this check is sufficient to reject invalid-curve attacks.
*/
private static void validatePointOnCurve(BigInteger x, BigInteger y, ECParameterSpec spec) {
BigInteger p = ((java.security.spec.ECFieldFp) spec.getCurve().getField()).getP();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add import at the top. Check before perform a cast.

// Add to the imports at the top of ElytronEcdhEsAlgorithmProvider.java
import java.security.spec.ECFieldFp;

// Inside validatePointOnCurve — guard the cast
if (!(spec.getCurve().getField() instanceof ECFieldFp)) {
throw new IllegalArgumentException("Only prime-field (Fp) curves are supported for EPK validation");
}
BigInteger p = ((ECFieldFp) spec.getCurve().getField()).getP();

.ephemeralPublicKey(jwk)
.build();
PrivateKey decryptionKey = getPrivateKey("P-256", "VEmDZpDXXK8p8N0Cndsxs924q6nS1RXFASRl6BfUqdw");
try {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use assertThrows instead if try/catch

@jimmychakkalakal jimmychakkalakal left a comment •
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @atiqur-rahman-pro for the PR and the contribution. The PR solves the issue #53639.
Please check my comments

@mposolda mposolda self-assigned this Oct 12, 2026
Copilot AI balanced review requested due to automatic review settings October 14, 2026 07:35
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The validation is mathematically correct, provider-independent, and adequately covered by focused regression tests.

1 open finding

🧠 Review effort: Balanced

@atiqur-rahman-pro
Copy link
Copy Markdown
Contributor Author

Hi @jimmychakkalakal

Thank you for the review! I have addressed your comments in the latest commit:

  • Added import java.security.spec.ECFieldFp; and guarded the field cast with if (!(spec.getCurve().getField() instanceof ECFieldFp)).

  • Updated the test helper to use Assert.assertThrows(IllegalArgumentException.class, ...) instead of try/catch.

All 195 crypto Elytron tests pass cleanly.

…ovider closes keycloak#53639

Signed-off-by: Atiqur Rahman <rahman.atiqur.pro@gmail.com>
Signed-off-by: Atiqur Rahman <rahman.atiqur.pro@gmail.com>
… test

Signed-off-by: Atiqur Rahman <rahman.atiqur.pro@gmail.com>
@atiqur-rahman-pro
atiqur-rahman-pro force-pushed the fix/elytron-ecdh-invalid-curve-53639 branch from 9cc2015 to fd1716d Compare November 12, 2026 05:46
Copilot AI balanced review requested due to automatic review settings November 12, 2026 05:46
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the ECDH-ES point-validation changes.

1 open issue

Review effort: Balanced

@atiqur-rahman-pro
Copy link
Copy Markdown
Contributor Author

Hi @vramik ,

Could you please take a look at this PR when you get a chance? It adds explicit EC point-on-curve validation in ElytronEcdhEsAlgorithmProvider to prevent invalid curve attacks (closes #53639).

The branch has been rebased onto the latest main and all 69 CI checks are passing. Thanks!

@vramik
vramik commented Nov 12, 2026
Copy link
Copy Markdown
Contributor

Hi @atiqur-rahman-pro, I believe you meant to tag @jimmychakkalakal for this review rather than me.

@atiqur-rahman-pro
Copy link
Copy Markdown
Contributor Author

Thanks to @vramik for bringing this to our attention!

@jimmychakkalakal, could you please take a look at this PR when you have a chance? It adds EC point-on-curve validation to ElytronEcdhEsAlgorithmProvider, which will help prevent invalid curve attacks (this addresses issue #53639).

The branch has been rebased against the latest main, and all 69 CI checks have passed successfully. Thanks!

@jimmychakkalakal jimmychakkalakal left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @atiqur-rahman-pro for the PR and changes.

I aprove the changes from my end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add explicit EC point-on-curve validation in ElytronEcdhEsAlgorithmProvider

5 participants

0