Skip to content

Commit 23fb644

Browse files
panvamarco-ippolito
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 1a193bf commit 23fb644

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

lib/internal/crypto/keys.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {
4-
ArrayFrom,
54
ArrayPrototypeSlice,
65
ObjectDefineProperty,
76
ObjectDefineProperties,
@@ -687,7 +686,7 @@ class CryptoKey {
687686
get usages() {
688687
if (!(this instanceof CryptoKey))
689688
throw new ERR_INVALID_THIS('CryptoKey');
690-
return ArrayFrom(this[kKeyUsages]);
689+
return this[kKeyUsages];
691690
}
692691
}
693692

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
@@ -81,6 +81,10 @@ const { subtle } = globalThis.crypto;
8181
hash: 'SHA-256'
8282
}, true, ['sign', 'verify']);
8383

84+
85+
assert.strictEqual(key.algorithm, key.algorithm);
86+
assert.strictEqual(key.usages, key.usages);
87+
8488
const raw = await subtle.exportKey('raw', key);
8589

8690
assert.deepStrictEqual(
@@ -122,6 +126,8 @@ const { subtle } = globalThis.crypto;
122126
name: 'AES-CTR',
123127
length: 256,
124128
}, true, ['encrypt', 'decrypt']);
129+
assert.strictEqual(key.algorithm, key.algorithm);
130+
assert.strictEqual(key.usages, key.usages);
125131

126132
const raw = await subtle.exportKey('raw', key);
127133

test/parallel/test-webcrypto-keygen.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ const vectors = {
298298
KeyObject.from(privateKey).asymmetricKeyDetails.publicExponent,
299299
bigIntArrayToUnsignedBigInt(publicExponent));
300300
assert.strictEqual(privateKey.algorithm.hash.name, hash);
301+
assert.strictEqual(privateKey.algorithm, privateKey.algorithm);
302+
assert.strictEqual(privateKey.usages, privateKey.usages);
303+
assert.strictEqual(publicKey.algorithm, publicKey.algorithm);
304+
assert.strictEqual(publicKey.usages, publicKey.usages);
301305

302306
// Missing parameters
303307
await assert.rejects(
@@ -442,6 +446,10 @@ const vectors = {
442446
assert.strictEqual(privateKey.algorithm.name, name);
443447
assert.strictEqual(publicKey.algorithm.namedCurve, namedCurve);
444448
assert.strictEqual(privateKey.algorithm.namedCurve, namedCurve);
449+
assert.strictEqual(privateKey.algorithm, privateKey.algorithm);
450+
assert.strictEqual(privateKey.usages, privateKey.usages);
451+
assert.strictEqual(publicKey.algorithm, publicKey.algorithm);
452+
assert.strictEqual(publicKey.usages, publicKey.usages);
445453

446454
// Invalid parameters
447455
[1, true, {}, [], null].forEach(async (namedCurve) => {
@@ -508,6 +516,8 @@ const vectors = {
508516
assert.deepStrictEqual(key.usages, usages);
509517
assert.strictEqual(key.algorithm.name, name);
510518
assert.strictEqual(key.algorithm.length, length);
519+
assert.strictEqual(key.algorithm, key.algorithm);
520+
assert.strictEqual(key.usages, key.usages);
511521

512522
// Invalid parameters
513523
[1, 100, 257, '', false, null].forEach(async (length) => {
@@ -568,6 +578,8 @@ const vectors = {
568578
assert.strictEqual(key.algorithm.name, 'HMAC');
569579
assert.strictEqual(key.algorithm.length, length);
570580
assert.strictEqual(key.algorithm.hash.name, hash);
581+
assert.strictEqual(key.algorithm, key.algorithm);
582+
assert.strictEqual(key.usages, key.usages);
571583

572584
[1, false, null].forEach(async (hash) => {
573585
await assert.rejects(
@@ -632,6 +644,10 @@ assert.throws(() => new CryptoKey(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' });
632644
assert.deepStrictEqual(privateKey.usages, privateUsages);
633645
assert.strictEqual(publicKey.algorithm.name, name);
634646
assert.strictEqual(privateKey.algorithm.name, name);
647+
assert.strictEqual(privateKey.algorithm, privateKey.algorithm);
648+
assert.strictEqual(privateKey.usages, privateKey.usages);
649+
assert.strictEqual(publicKey.algorithm, publicKey.algorithm);
650+
assert.strictEqual(publicKey.usages, publicKey.usages);
635651
}
636652

637653
const kTests = [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy