GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — crypto: report actual RSA modulus lengths · nodejs/node@53773e5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/crypto
    • rsa.js
  • test/parallel
    • test-webcrypto-rsa-modulus-length.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/crypto/rsa.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/rsa.js
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const {
3232
bigIntArrayToUnsignedInt,
3333
getUsagesMask,
3434
jobPromise,
35+
jobPromiseThen,
3536
normalizeHashName,
3637
validateMaxBufferLength,
3738
toUsagesSet,
@@ -130,15 +131,21 @@ function rsaKeyGenerate(
130131
const keyUsages = getKeyPairUsages(usagesSet, allowedUsages);
131132
validateUsagesNotEmpty(keyUsages.private);
132133

133-
return jobPromise(() => new RsaKeyPairGenJob(
134+
return jobPromiseThen(jobPromise(() => new RsaKeyPairGenJob(
134135
kCryptoJobWebCrypto,
135136
kKeyVariantRSA_SSA_PKCS1_v1_5,
136137
modulusLength,
137138
publicExponentConverted,
138139
keyAlgorithm,
139140
getUsagesMask(keyUsages.public),
140141
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+
});
142149
}
143150

144151
function rsaExportKey(key, format) {
Collapse file

‎test/parallel/test-webcrypto-rsa-modulus-length.js‎

Copy file name to clipboard
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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());

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.