Skip to content

Commit

Permalink
crypto: expose KeyObject.isKeyObject(obj) and CryptoKey.isCryptoKey(obj)
Browse files Browse the repository at this point in the history
closes #38611
  • Loading branch information
panva committed May 10, 2021
1 parent 4243ce0 commit 856c82a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
10 changes: 10 additions & 0 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,16 @@ passing keys as strings or `Buffer`s due to improved security features.
The receiver obtains a cloned `KeyObject`, and the `KeyObject` does not need to
be listed in the `transferList` argument.

### Static method: `KeyObject.isKeyObject(obj)`
<!-- YAML
added: REPLACEME
-->

* `obj` {Object}
* Returns: {boolean}

Returns `true` if `obj` is a `KeyObject`, `false` otherwise.

### Static method: `KeyObject.from(key)`
<!-- YAML
added: v15.0.0
Expand Down
12 changes: 12 additions & 0 deletions doc/api/webcrypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,18 @@ An error will be thrown if the given `typedArray` is larger than 65,536 bytes.
added: v15.0.0
-->

### Static method: `CryptoKey.isCryptoKey(obj)`
<!-- YAML
added: REPLACEME
-->

* `obj` {Object}
* Returns: {boolean}

Returns `true` if `obj` is a `CryptoKey`, `false` otherwise. This method
is specific to Node.js, it is not portable to other implementations of
[Web Crypto API][].

### `cryptoKey.algorithm`
<!-- YAML
added: v15.0.0
Expand Down
12 changes: 10 additions & 2 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ const {
});
}

static isKeyObject(obj) {
return isKeyObject(obj);
}

get type() {
return this[kKeyType];
}
Expand Down Expand Up @@ -639,8 +643,8 @@ function createPrivateKey(key) {
return new PrivateKeyObject(handle);
}

function isKeyObject(key) {
return key instanceof KeyObject;
function isKeyObject(obj) {
return obj != null && obj[kHandle] !== undefined;
}

// Our implementation of CryptoKey is a simple wrapper around a KeyObject
Expand All @@ -656,6 +660,10 @@ class CryptoKey extends JSTransferable {
throw new ERR_OPERATION_FAILED('Illegal constructor');
}

static isCryptoKey(obj) {
return isCryptoKey(obj);
}

[kInspect](depth, options) {
if (depth < 0)
return this;
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-crypto-key-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,14 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
message: `Unsupported JWK EC curve: ${namedCurve}.`
});
}

{
const buffer = Buffer.from('Hello World');
const keyObject = createSecretKey(buffer);
const keyPair = generateKeyPairSync('ed25519');
assert(KeyObject.isKeyObject(keyPair.publicKey));
assert(KeyObject.isKeyObject(keyPair.privateKey));
assert(KeyObject.isKeyObject(keyObject));

assert(!KeyObject.isKeyObject(buffer));
}
15 changes: 15 additions & 0 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ const vectors = {

assert(publicKey);
assert(privateKey);
assert(CryptoKey.isCryptoKey(publicKey));
assert(CryptoKey.isCryptoKey(privateKey));

assert(publicKey instanceof CryptoKey);
assert(privateKey instanceof CryptoKey);
Expand Down Expand Up @@ -366,6 +368,8 @@ const vectors = {

assert(publicKey);
assert(privateKey);
assert(CryptoKey.isCryptoKey(publicKey));
assert(CryptoKey.isCryptoKey(privateKey));

assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(privateKey.type, 'private');
Expand Down Expand Up @@ -430,6 +434,7 @@ const vectors = {
}, true, usages);

assert(key);
assert(CryptoKey.isCryptoKey(key));

assert.strictEqual(key.type, 'secret');
assert.strictEqual(key.extractable, true);
Expand Down Expand Up @@ -488,6 +493,7 @@ const vectors = {
}

assert(key);
assert(CryptoKey.isCryptoKey(key));

assert.strictEqual(key.type, 'secret');
assert.strictEqual(key.extractable, true);
Expand Down Expand Up @@ -544,6 +550,8 @@ const vectors = {

assert(publicKey);
assert(privateKey);
assert(CryptoKey.isCryptoKey(publicKey));
assert(CryptoKey.isCryptoKey(privateKey));

assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(privateKey.type, 'private');
Expand Down Expand Up @@ -634,6 +642,8 @@ const vectors = {
}, true, ['deriveKey']);
assert(publicKey);
assert(privateKey);
assert(CryptoKey.isCryptoKey(publicKey));
assert(CryptoKey.isCryptoKey(privateKey));
assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(privateKey.type, 'private');
assert.strictEqual(publicKey.algorithm.name, 'NODE-DH');
Expand All @@ -646,3 +656,8 @@ const vectors = {
assert.throws(() => new CryptoKey(), {
code: 'ERR_OPERATION_FAILED'
});

{
const buffer = Buffer.from('Hello World');
assert(!CryptoKey.isCryptoKey(buffer));
}

0 comments on commit 856c82a

Please sign in to comment.