Skip to content

fix: validate JWK alg/crv/kty per algorithm in subtle.importKey#1021

Merged
boorad merged 2 commits into
mainfrom
feat/jwk-alg-crv-validation
May 4, 2026
Merged

fix: validate JWK alg/crv/kty per algorithm in subtle.importKey#1021
boorad merged 2 commits into
mainfrom
feat/jwk-alg-crv-validation

Conversation

@boorad
Copy link
Copy Markdown
Collaborator

@boorad boorad commented May 4, 2026

Summary

Tightens JWK validation in subtle.importKey for RSA and CFRG algorithms so that mismatched alg, crv, or kty parameters are rejected at the JS layer with a DataError DOMException, matching Node.js's WebCrypto behavior.

Previously, mismatched JWK parameters either passed through to the C++ layer (yielding opaque errors) or were silently accepted.

Changes

  • RSA (RSASSA-PKCS1-v1_5, RSA-PSS, RSA-OAEP): when jwk.alg is set, verify it matches the algorithm + hash combination (e.g. RSA-PSS + SHA-256 → PS256).
  • CFRG (Ed25519, Ed448, X25519, X448): verify jwk.kty === 'OKP' and jwk.crv === algorithm.name. For Ed curves, if jwk.alg is set it must equal the algorithm name or 'EdDSA'.
  • Error messages mirror Node verbatim ('JWK "alg" does not match the requested algorithm', 'JWK "crv" Parameter and algorithm name mismatch', 'Invalid JWK "kty" Parameter') for downstream parity.
  • Tests added covering RSA wrong-alg, Ed wrong-crv/wrong-alg/'EdDSA'-accepted/wrong-kty, and X25519/X448 wrong-crv.

Test plan

  • RSA importKey rejects mismatched alg for each of RSASSA-PKCS1-v1_5, RSA-PSS, RSA-OAEP
  • Ed25519/Ed448 importKey rejects mismatched crv, mismatched alg, and wrong kty
  • Ed25519/Ed448 importKey accepts alg: 'EdDSA'
  • X25519/X448 importKey rejects mismatched crv
  • Existing JWK roundtrip tests still pass

Fixes #1001

boorad added 2 commits May 3, 2026 22:29
Node validates JWK alg and crv fields against the algorithm name and
hash at import time. RNQC silently accepted mismatched values, which
allowed malformed keys to flow into crypto operations and broke
interop with Node-produced JWKs.

- RSA: validate jwk.alg against the per-context expected name
  derived from algorithm.hash (RS256, PS256, RSA-OAEP-256, ...)
- Ed25519/Ed448: require crv === algorithm.name; if alg is present,
  accept only algorithm.name or 'EdDSA'
- X25519/X448: require crv === algorithm.name (no alg check, per Node)

Closes #1001
Add `kty === 'OKP'` check to edImportKey to reject non-OKP keyData
inline, matching the pattern already used by rsaImportKey/ecImportKey
and Node's `Invalid JWK "kty" Parameter` message. Refactor the nested
ternary in rsaImportKey's alg check into a switch for readability.

Add X25519/X448 `crv` mismatch tests to cover the now-ungated crv
check, and replace the Ed `use="enc"` test (which only exercised
pre-existing validateJwkStructure behavior) with a `kty: 'EC'` test
that actually exercises the new check.
@boorad boorad self-assigned this May 4, 2026
@vercel
Copy link
Copy Markdown

vercel Bot commented May 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
react-native-quick-crypto Ready Ready Preview, Comment May 4, 2026 2:40am

Request Review

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

🤖 End-to-End Test Results - Android

Status: ✅ Passed
Platform: Android
Run: 25298365697

📸 Final Test Screenshot

Maestro Test Results - android

Screenshot automatically captured from End-to-End tests and will expire in 30 days


This comment is automatically updated on each test run.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

🤖 End-to-End Test Results - iOS

Status: ✅ Passed
Platform: iOS
Run: 25298365687

📸 Final Test Screenshot

Maestro Test Results - ios

Screenshot automatically captured from End-to-End tests and will expire in 30 days


This comment is automatically updated on each test run.

@boorad boorad merged commit 3076769 into main May 4, 2026
9 checks passed
@boorad boorad deleted the feat/jwk-alg-crv-validation branch May 4, 2026 02:51
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.

subtle.importKey: validate JWK alg/crv/use parameters per algorithm

1 participant