crypto: ensure CryptoKey usages and algorithm are cached objects · nodejs/node@fe2b344 · GitHub
Skip to content

Commit fe2b344

Browse files
panvaruyadorno
authored andcommitted
crypto: ensure CryptoKey usages and algorithm are cached objects
PR-URL: #56108 Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 843943d commit fe2b344

6 files changed

Lines changed: 51 additions & 2 deletions

File tree

lib/internal/crypto/keys.js

Lines changed: 1 addition & 2 deletions

test/parallel/test-webcrypto-export-import-cfrg.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ async function testImportSpki({ name, publicUsages }, extractable) {
115115
assert.strictEqual(key.extractable, extractable);
116116
assert.deepStrictEqual(key.usages, publicUsages);
117117
assert.deepStrictEqual(key.algorithm.name, name);
118+
assert.strictEqual(key.algorithm, key.algorithm);
119+
assert.strictEqual(key.usages, key.usages);
118120

119121
if (extractable) {
120122
// Test the roundtrip
@@ -151,6 +153,8 @@ async function testImportPkcs8({ name, privateUsages }, extractable) {
151153
assert.strictEqual(key.extractable, extractable);
152154
assert.deepStrictEqual(key.usages, privateUsages);
153155
assert.deepStrictEqual(key.algorithm.name, name);
156+
assert.strictEqual(key.algorithm, key.algorithm);
157+
assert.strictEqual(key.usages, key.usages);
154158

155159
if (extractable) {
156160
// Test the roundtrip
@@ -227,6 +231,10 @@ async function testImportJwk({ name, publicUsages, privateUsages }, extractable)
227231
assert.deepStrictEqual(privateKey.usages, privateUsages);
228232
assert.strictEqual(publicKey.algorithm.name, name);
229233
assert.strictEqual(privateKey.algorithm.name, name);
234+
assert.strictEqual(privateKey.algorithm, privateKey.algorithm);
235+
assert.strictEqual(privateKey.usages, privateKey.usages);
236+
assert.strictEqual(publicKey.algorithm, publicKey.algorithm);
237+
assert.strictEqual(publicKey.usages, publicKey.usages);
230238

231239
if (extractable) {
232240
// Test the round trip
@@ -345,6 +353,8 @@ async function testImportRaw({ name, publicUsages }) {
345353
assert.strictEqual(publicKey.type, 'public');
346354
assert.deepStrictEqual(publicKey.usages, publicUsages);
347355
assert.strictEqual(publicKey.algorithm.name, name);
356+
assert.strictEqual(publicKey.algorithm, publicKey.algorithm);
357+
assert.strictEqual(publicKey.usages, publicKey.usages);
348358
}
349359

350360
(async function() {

test/parallel/test-webcrypto-export-import-ec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ async function testImportSpki({ name, publicUsages }, namedCurve, extractable) {
111111
assert.deepStrictEqual(key.usages, publicUsages);
112112
assert.deepStrictEqual(key.algorithm.name, name);
113113
assert.deepStrictEqual(key.algorithm.namedCurve, namedCurve);
114+
assert.strictEqual(key.algorithm, key.algorithm);
115+
assert.strictEqual(key.usages, key.usages);
114116

115117
if (extractable) {
116118
// Test the roundtrip
@@ -151,6 +153,8 @@ async function testImportPkcs8(
151153
assert.deepStrictEqual(key.usages, privateUsages);
152154
assert.deepStrictEqual(key.algorithm.name, name);
153155
assert.deepStrictEqual(key.algorithm.namedCurve, namedCurve);
156+
assert.strictEqual(key.algorithm, key.algorithm);
157+
assert.strictEqual(key.usages, key.usages);
154158

155159
if (extractable) {
156160
// Test the roundtrip
@@ -234,6 +238,10 @@ async function testImportJwk(
234238
assert.strictEqual(privateKey.algorithm.name, name);
235239
assert.strictEqual(publicKey.algorithm.namedCurve, namedCurve);
236240
assert.strictEqual(privateKey.algorithm.namedCurve, namedCurve);
241+
assert.strictEqual(privateKey.algorithm, privateKey.algorithm);
242+
assert.strictEqual(privateKey.usages, privateKey.usages);
243+
assert.strictEqual(publicKey.algorithm, publicKey.algorithm);
244+
assert.strictEqual(publicKey.usages, publicKey.usages);
237245

238246
if (extractable) {
239247
// Test the round trip
@@ -368,6 +376,8 @@ async function testImportRaw({ name, publicUsages }, namedCurve) {
368376
assert.deepStrictEqual(publicKey.usages, publicUsages);
369377
assert.strictEqual(publicKey.algorithm.name, name);
370378
assert.strictEqual(publicKey.algorithm.namedCurve, namedCurve);
379+
assert.strictEqual(publicKey.algorithm, publicKey.algorithm);
380+
assert.strictEqual(publicKey.usages, publicKey.usages);
371381
}
372382

373383
(async function() {

test/parallel/test-webcrypto-export-import-rsa.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ async function testImportSpki({ name, publicUsages }, size, hash, extractable) {
315315
assert.deepStrictEqual(key.algorithm.publicExponent,
316316
new Uint8Array([1, 0, 1]));
317317
assert.strictEqual(key.algorithm.hash.name, hash);
318+
assert.strictEqual(key.algorithm, key.algorithm);
319+
assert.strictEqual(key.usages, key.usages);
318320

319321
if (extractable) {
320322
const spki = await subtle.exportKey('spki', key);
@@ -349,6 +351,8 @@ async function testImportPkcs8(
349351
assert.deepStrictEqual(key.algorithm.publicExponent,
350352
new Uint8Array([1, 0, 1]));
351353
assert.strictEqual(key.algorithm.hash.name, hash);
354+
assert.strictEqual(key.algorithm, key.algorithm);
355+
assert.strictEqual(key.usages, key.usages);
352356

353357
if (extractable) {
354358
const pkcs8 = await subtle.exportKey('pkcs8', key);
@@ -415,6 +419,10 @@ async function testImportJwk(
415419
new Uint8Array([1, 0, 1]));
416420
assert.deepStrictEqual(publicKey.algorithm.publicExponent,
417421
privateKey.algorithm.publicExponent);
422+
assert.strictEqual(privateKey.algorithm, privateKey.algorithm);
423+
assert.strictEqual(privateKey.usages, privateKey.usages);
424+
assert.strictEqual(publicKey.algorithm, publicKey.algorithm);
425+
assert.strictEqual(publicKey.usages, publicKey.usages);
418426

419427
if (extractable) {
420428
const [

test/parallel/test-webcrypto-export-import.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ const { subtle } = globalThis.crypto;
8686
hash: 'SHA-256'
8787
}, true, ['sign', 'verify']);
8888

89+
90+
assert.strictEqual(key.algorithm, key.algorithm);
91+
assert.strictEqual(key.usages, key.usages);
92+
8993
const raw = await subtle.exportKey('raw', key);
9094

9195
assert.deepStrictEqual(
@@ -127,6 +131,8 @@ const { subtle } = globalThis.crypto;
127131
name: 'AES-CTR',
128132
length: 256,
129133
}, true, ['encrypt', 'decrypt']);
134+
assert.strictEqual(key.algorithm, key.algorithm);
135+
assert.strictEqual(key.usages, key.usages);
130136

131137
const raw = await subtle.exportKey('raw', key);
132138

test/parallel/test-webcrypto-keygen.js

Lines changed: 16 additions & 0 deletions

0 commit comments

Comments
 (0)