From dedb22e9653a004190f7246fb561fb09b6b32fcf Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 14 Jun 2022 14:17:21 +0200 Subject: [PATCH] crypto: fix webcrypto RSA generateKey() use of publicExponent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/43431 Reviewed-By: Tobias Nießen Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- lib/internal/crypto/rsa.js | 2 +- test/parallel/test-webcrypto-keygen.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/internal/crypto/rsa.js b/lib/internal/crypto/rsa.js index 27b5803db4f77d..728e4bd951cdc5 100644 --- a/lib/internal/crypto/rsa.js +++ b/lib/internal/crypto/rsa.js @@ -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( diff --git a/test/parallel/test-webcrypto-keygen.js b/test/parallel/test-webcrypto-keygen.js index c3429af99e66a7..32e8fc39ee6d15 100644 --- a/test/parallel/test-webcrypto-keygen.js +++ b/test/parallel/test-webcrypto-keygen.js @@ -1,3 +1,4 @@ +// Flags: --expose-internals 'use strict'; const common = require('../common'); @@ -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', @@ -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 @@ -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 = [