Skip to content

Commit

Permalink
crypto: fix webcrypto RSA generateKey() use of publicExponent
Browse files Browse the repository at this point in the history
PR-URL: #43431
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
panva authored and targos committed Jul 12, 2022
1 parent 018f61c commit dedb22e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/crypto/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async function rsaKeyGenerate(
return new Promise((resolve, reject) => {
generateKeyPair('rsa', {
modulusLength,
publicExponentConverted,
publicExponent: publicExponentConverted,
}, (err, pubKey, privKey) => {
if (err) {
return reject(lazyDOMException(
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
Expand All @@ -10,8 +11,11 @@ const { types: { isCryptoKey } } = require('util');
const {
webcrypto: { subtle, CryptoKey },
createSecretKey,
KeyObject,
} = require('crypto');

const { bigIntArrayToUnsignedBigInt } = require('internal/crypto/util');

const allUsages = [
'encrypt',
'decrypt',
Expand Down Expand Up @@ -264,10 +268,16 @@ const vectors = {
assert.strictEqual(publicKey.algorithm.name, name);
assert.strictEqual(publicKey.algorithm.modulusLength, modulusLength);
assert.deepStrictEqual(publicKey.algorithm.publicExponent, publicExponent);
assert.strictEqual(
KeyObject.from(publicKey).asymmetricKeyDetails.publicExponent,
bigIntArrayToUnsignedBigInt(publicExponent));
assert.strictEqual(publicKey.algorithm.hash.name, hash);
assert.strictEqual(privateKey.algorithm.name, name);
assert.strictEqual(privateKey.algorithm.modulusLength, modulusLength);
assert.deepStrictEqual(privateKey.algorithm.publicExponent, publicExponent);
assert.strictEqual(
KeyObject.from(privateKey).asymmetricKeyDetails.publicExponent,
bigIntArrayToUnsignedBigInt(publicExponent));
assert.strictEqual(privateKey.algorithm.hash.name, hash);

// Missing parameters
Expand Down Expand Up @@ -344,6 +354,17 @@ const vectors = {
code: 'ERR_INVALID_ARG_TYPE'
});
}));

await Promise.all([[1], [1, 0, 0]].map((publicExponent) => {
return assert.rejects(subtle.generateKey({
name,
modulusLength,
publicExponent: new Uint8Array(publicExponent),
hash
}, true, usages), {
name: 'OperationError',
});
}));
}

const kTests = [
Expand Down

0 comments on commit dedb22e

Please sign in to comment.