Skip to content

Commit dc41d49

Browse files
authored
Merge pull request #649 from Kr0emer/fix/bug-005-rsa-zero-modulus
fix(rsa): reject zero-modulus JWK keys and throw on BigInteger divide-by-zero
2 parents 0710e39 + d1e331f commit dc41d49

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

ext/jsbn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ function bnpSquareTo(r) {
354354
// r != q, this != m. q or r may be null.
355355
function bnpDivRemTo(m,q,r) {
356356
var pm = m.abs();
357-
if(pm.t <= 0) return;
357+
if(pm.t <= 0) throw "BigInteger divide by zero";
358358
var pt = this.abs();
359359
if(pt.t < pm.t) {
360360
if(q != null) q.fromInt(0);

ext/rsa.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ function RSASetPublic(N, E) {
167167
} else {
168168
throw "Invalid RSA public key";
169169
}
170+
171+
if (this.n == null ||
172+
typeof this.n.compareTo !== "function" ||
173+
this.n.compareTo(BigInteger.ONE) <= 0 ||
174+
this.e == null ||
175+
isNaN(this.e) ||
176+
this.e <= 0) {
177+
throw "Invalid RSA public key";
178+
}
170179
}
171180

172181
// Perform raw public operation on "x": return x^e (mod n)

test/qunit-do-crypto.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,31 @@
232232
equal(n, 100, "100 times success:" + n0 + ":" + n1 + ":" + n2 + ":" + n3);
233233
});
234234

235+
test("RSASetPublic rejects zero modulus", function() {
236+
throws(function() {
237+
var pub = new RSAKey();
238+
pub.setPublic("00", "10001");
239+
},
240+
"Invalid RSA public key",
241+
"reject zero modulus");
242+
});
243+
244+
test("KEYUTIL.getKey rejects JWK with zero modulus", function() {
245+
throws(function() {
246+
KEYUTIL.getKey({kty: "RSA", n: "AA", e: "AQAB"});
247+
},
248+
"Invalid RSA public key",
249+
"reject JWK n=0");
250+
});
251+
252+
test("BigInteger.modPowInt throws when modulus is zero", function() {
253+
throws(function() {
254+
new BigInteger("deadbeef", 16).modPowInt(65537, BigInteger.ZERO);
255+
},
256+
"BigInteger divide by zero",
257+
"reject mod(0)");
258+
});
259+
235260
test("MessageDigest test", function() {
236261
expect(10);
237262
var md1 = new KJUR.crypto.MessageDigest({"alg": "sha1", "prov": "cryptojs"});
@@ -419,4 +444,3 @@ <h2 id="qunit-userAgent"></h2>
419444
</p>
420445
</body>
421446
</html>
422-

0 commit comments

Comments
 (0)