Skip to content

Commit 53c0f2f

Browse files
panvatargos
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 9b272ae commit 53c0f2f

6 files changed

+51
-2
lines changed

lib/internal/crypto/keys.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {
4-
ArrayFrom,
54
ArrayPrototypeSlice,
65
ObjectDefineProperties,
76
ObjectDefineProperty,
@@ -781,7 +780,7 @@ class CryptoKey {
781780
get usages() {
782781
if (!(this instanceof CryptoKey))
783782
throw new ERR_INVALID_THIS('CryptoKey');
784-
return ArrayFrom(this[kKeyUsages]);
783+
return this[kKeyUsages];
785784
}
786785
}
787786

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

+10
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

+10
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

+8
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

+6
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

+16
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)