Skip to content

Commit 2d05c04

Browse files
panvatargos
authored andcommitted
crypto: return cached copies from CryptoKey algorithm and usages getters
Fixes: #59534 PR-URL: #59538 Fixes: #59535 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 207ffbe commit 2d05c04

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/internal/crypto/keys.js

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

33
const {
4+
ArrayFrom,
45
ArrayPrototypeSlice,
56
ObjectDefineProperties,
67
ObjectDefineProperty,
@@ -81,6 +82,8 @@ const kAlgorithm = Symbol('kAlgorithm');
8182
const kExtractable = Symbol('kExtractable');
8283
const kKeyType = Symbol('kKeyType');
8384
const kKeyUsages = Symbol('kKeyUsages');
85+
const kCachedAlgorithm = Symbol('kCachedAlgorithm');
86+
const kCachedKeyUsages = Symbol('kCachedKeyUsages');
8487

8588
// Key input contexts.
8689
const kConsumePublic = 0;
@@ -835,13 +838,19 @@ class CryptoKey {
835838
get algorithm() {
836839
if (!(this instanceof CryptoKey))
837840
throw new ERR_INVALID_THIS('CryptoKey');
838-
return this[kAlgorithm];
841+
if (!this[kCachedAlgorithm]) {
842+
this[kCachedAlgorithm] ??= { ...this[kAlgorithm] };
843+
this[kCachedAlgorithm].hash &&= { ...this[kCachedAlgorithm].hash };
844+
this[kCachedAlgorithm].publicExponent &&= new Uint8Array(this[kCachedAlgorithm].publicExponent);
845+
}
846+
return this[kCachedAlgorithm];
839847
}
840848

841849
get usages() {
842850
if (!(this instanceof CryptoKey))
843851
throw new ERR_INVALID_THIS('CryptoKey');
844-
return this[kKeyUsages];
852+
this[kCachedKeyUsages] ??= ArrayFrom(this[kKeyUsages]);
853+
return this[kCachedKeyUsages];
845854
}
846855
}
847856

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as common from '../common/index.mjs';
2+
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
import * as assert from 'node:assert';
7+
import * as util from 'node:util';
8+
9+
const { subtle } = globalThis.crypto;
10+
11+
const kp = await subtle.generateKey('Ed25519', true, ['sign', 'verify']);
12+
assert.notStrictEqual(kp.publicKey.algorithm, kp.privateKey.algorithm);
13+
assert.notStrictEqual(kp.publicKey.usages, kp.privateKey.usages);
14+
kp.publicKey.algorithm.name = 'ed25519';
15+
assert.strictEqual(kp.publicKey.algorithm.name, 'ed25519');
16+
kp.publicKey.usages.push('foo');
17+
assert.ok(kp.publicKey.usages.includes('foo'));
18+
assert.ok(util.inspect(kp.publicKey).includes("algorithm: { name: 'Ed25519' }"));
19+
assert.ok(util.inspect(kp.publicKey).includes("usages: [ 'verify' ]"));
20+
21+
await subtle.sign('Ed25519', kp.privateKey, Buffer.alloc(32));

0 commit comments

Comments
 (0)