GeezerVison is showing github.com — crypto: report actual RSA modulus lengths · nodejs/node@53773e5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/rsa.js +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,7 @@ const { | 32 | 32 | bigIntArrayToUnsignedInt, | 33 | 33 | getUsagesMask, | 34 | 34 | jobPromise, | | 35 | + jobPromiseThen, | 35 | 36 | normalizeHashName, | 36 | 37 | validateMaxBufferLength, | 37 | 38 | toUsagesSet, | @@ -130,15 +131,21 @@ function rsaKeyGenerate( | 130 | 131 | const keyUsages = getKeyPairUsages(usagesSet, allowedUsages); | 131 | 132 | validateUsagesNotEmpty(keyUsages.private); | 132 | 133 |
| 133 | | - return jobPromise(() => new RsaKeyPairGenJob( | | 134 | + return jobPromiseThen(jobPromise(() => new RsaKeyPairGenJob( | 134 | 135 | kCryptoJobWebCrypto, | 135 | 136 | kKeyVariantRSA_SSA_PKCS1_v1_5, | 136 | 137 | modulusLength, | 137 | 138 | publicExponentConverted, | 138 | 139 | keyAlgorithm, | 139 | 140 | getUsagesMask(keyUsages.public), | 140 | 141 | getUsagesMask(keyUsages.private), | 141 | | - extractable)); | | 142 | + extractable)), (result) => { | | 143 | + const { modulusLength: actualModulusLength } = | | 144 | + getCryptoKeyHandle(result.publicKey).keyDetail({ __proto__: null }); | | 145 | + getCryptoKeyAlgorithm(result.publicKey).modulusLength = actualModulusLength; | | 146 | + getCryptoKeyAlgorithm(result.privateKey).modulusLength = actualModulusLength; | | 147 | + return result; | | 148 | + }); | 142 | 149 | } | 143 | 150 |
| 144 | 151 | function rsaExportKey(key, format) { | |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const { generateKeyPairSync, KeyObject } = require('crypto'); | | 9 | +const { subtle } = globalThis.crypto; | | 10 | + | | 11 | +(async () => { | | 12 | + for (const name of ['RSA-PSS', 'RSASSA-PKCS1-v1_5', 'RSA-OAEP']) { | | 13 | + const usages = name === 'RSA-OAEP' ? ['encrypt', 'decrypt'] : ['sign', 'verify']; | | 14 | + for (const modulusLength of [1025, 2048, 2049]) { | | 15 | + let pair; | | 16 | + try { | | 17 | + pair = await subtle.generateKey({ | | 18 | + name, modulusLength, hash: 'SHA-256', publicExponent: new Uint8Array([1, 0, 1]), | | 19 | + }, true, usages); | | 20 | + } catch (err) { | | 21 | + if (modulusLength === 2048 || err.name !== 'OperationError') | | 22 | + throw err; | | 23 | + // Only reject sizes the backend also rejects. Some backends round | | 24 | + // the requested size, which must still produce a usable CryptoKey. | | 25 | + assert.throws(() => generateKeyPairSync('rsa', { modulusLength }), | | 26 | + { name: 'Error' }); | | 27 | + continue; | | 28 | + } | | 29 | + const actualModulusLength = | | 30 | + KeyObject.from(pair.publicKey).asymmetricKeyDetails.modulusLength; | | 31 | + for (const key of [pair.publicKey, pair.privateKey]) { | | 32 | + assert.strictEqual(key.algorithm.modulusLength, actualModulusLength); | | 33 | + assert.strictEqual( | | 34 | + KeyObject.from(key).asymmetricKeyDetails.modulusLength, actualModulusLength); | | 35 | + assert.deepStrictEqual(structuredClone(key).algorithm, key.algorithm); | | 36 | + | | 37 | + const format = key.type === 'public' ? 'spki' : 'pkcs8'; | | 38 | + const imported = await subtle.importKey( | | 39 | + format, await subtle.exportKey(format, key), | | 40 | + { name, hash: 'SHA-256' }, true, key.usages); | | 41 | + assert.deepStrictEqual(imported.algorithm, key.algorithm); | | 42 | + } | | 43 | + const publicKey = await subtle.getPublicKey(pair.privateKey, pair.publicKey.usages); | | 44 | + assert.deepStrictEqual(publicKey.algorithm, pair.publicKey.algorithm); | | 45 | + } | | 46 | + } | | 47 | +})().then(common.mustCall()); |
|