Skip to content

Comments

fix(rsa): reject zero-modulus JWK keys and throw on BigInteger divide-by-zero#649

Merged
kjur merged 1 commit intokjur:masterfrom
Kr0emer:fix/bug-005-rsa-zero-modulus
Feb 20, 2026
Merged

fix(rsa): reject zero-modulus JWK keys and throw on BigInteger divide-by-zero#649
kjur merged 1 commit intokjur:masterfrom
Kr0emer:fix/bug-005-rsa-zero-modulus

Conversation

@Kr0emer
Copy link
Contributor

@Kr0emer Kr0emer commented Feb 20, 2026

Summary

This PR fixes an RSA robustness/security issue where a malformed JWK RSA public key with n = "AA" (decoded modulus = 0) could be accepted during import.
Combined with BigInteger mod(0) behavior, RSA public operations could silently degrade to deterministic wrong output.

Root Cause

  • RSASetPublic accepted non-empty modulus/exponent strings without validating parsed numeric values.
  • bnpDivRemTo returned silently when divisor was zero, so modPowInt(e, 0) did not fail loudly.

Changes

  • ext/rsa.js
    • Hardened RSASetPublic with numeric checks after parsing:
      • reject n <= 1
      • reject invalid/non-positive e
  • ext/jsbn.js
    • bnpDivRemTo now throws "BigInteger divide by zero" when divisor is zero.
  • test/qunit-do-crypto.html
    • Added regression tests:
      • RSASetPublic rejects zero modulus
      • KEYUTIL.getKey rejects JWK RSA with n=0 (n: "AA")
      • BigInteger.modPowInt(..., BigInteger.ZERO) throws

Verification

  • Reproduced before fix:
    • zero-modulus JWK was accepted
    • modPowInt(e, 0) returned 0
  • After fix:
    • zero-modulus JWK is rejected with Invalid RSA public key
    • modPowInt(e, 0) throws BigInteger divide by zero
  • Ran tests:
    • npm test (under npm/) passes (6 passing)

@kjur kjur merged commit dc41d49 into kjur:master Feb 20, 2026
2 checks passed
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.

2 participants