| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1483,6 +1483,16 @@ util.types.isBoxedPrimitive(Object(Symbol('foo'))); // Returns true | |||
| 1483 | 1483 | util.types.isBoxedPrimitive(Object(BigInt(5))); // Returns true | |
| 1484 | 1484 | ``` | |
| 1485 | 1485 | ||
| 1486 | + ### `util.types.isCryptoKey(value)` | ||
| 1487 | + <!-- YAML | ||
| 1488 | + added: REPLACEME | ||
| 1489 | + --> | ||
| 1490 | + | ||
| 1491 | + * `value` {Object} | ||
| 1492 | + * Returns: {boolean} | ||
| 1493 | + | ||
| 1494 | + Returns `true` if `value` is a {CryptoKey}, `false` otherwise. | ||
| 1495 | + | ||
| 1486 | 1496 | ### `util.types.isDataView(value)` | |
| 1487 | 1497 | <!-- YAML | |
| 1488 | 1498 | added: v10.0.0 | |
@@ -1678,6 +1688,16 @@ util.types.isInt32Array(new Int32Array()); // Returns true | |||
| 1678 | 1688 | util.types.isInt32Array(new Float64Array()); // Returns false | |
| 1679 | 1689 | ``` | |
| 1680 | 1690 | ||
| 1691 | + ### `util.types.isKeyObject(value)` | ||
| 1692 | + <!-- YAML | ||
| 1693 | + added: REPLACEME | ||
| 1694 | + --> | ||
| 1695 | + | ||
| 1696 | + * `value` {Object} | ||
| 1697 | + * Returns: {boolean} | ||
| 1698 | + | ||
| 1699 | + Returns `true` if `value` is a {KeyObject}, `false` otherwise. | ||
| 1700 | + | ||
| 1681 | 1701 | ### `util.types.isMap(value)` | |
| 1682 | 1702 | <!-- YAML | |
| 1683 | 1703 | added: v10.0.0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -639,8 +639,8 @@ function createPrivateKey(key) { | |||
| 639 | 639 | return new PrivateKeyObject(handle); | |
| 640 | 640 | } | |
| 641 | 641 | ||
| 642 | - function isKeyObject(key) { | ||
| 643 | - return key instanceof KeyObject; | ||
| 642 | + function isKeyObject(obj) { | ||
| 643 | + return obj != null && obj[kKeyType] !== undefined; | ||
| 644 | 644 | } | |
| 645 | 645 | ||
| 646 | 646 | // Our implementation of CryptoKey is a simple wrapper around a KeyObject | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | ArrayBufferIsView, | |
| 5 | + ObjectDefineProperties, | ||
| 5 | 6 | TypedArrayPrototypeGetSymbolToStringTag, | |
| 6 | 7 | } = primordials; | |
| 7 | 8 | ||
@@ -69,3 +70,39 @@ module.exports = { | |||
| 69 | 70 | isBigInt64Array, | |
| 70 | 71 | isBigUint64Array | |
| 71 | 72 | }; | |
| 73 | + | ||
| 74 | + let isCryptoKey; | ||
| 75 | + let isKeyObject; | ||
| 76 | + | ||
| 77 | + ObjectDefineProperties(module.exports, { | ||
| 78 | + isKeyObject: { | ||
| 79 | + configurable: false, | ||
| 80 | + enumerable: true, | ||
| 81 | + value(obj) { | ||
| 82 | + if (!process.versions.openssl) { | ||
| 83 | + return false; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + if (!isKeyObject) { | ||
| 87 | + ({ isKeyObject } = require('internal/crypto/keys')); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + return isKeyObject(obj); | ||
| 91 | + } | ||
| 92 | + }, | ||
| 93 | + isCryptoKey: { | ||
| 94 | + configurable: false, | ||
| 95 | + enumerable: true, | ||
| 96 | + value(obj) { | ||
| 97 | + if (!process.versions.openssl) { | ||
| 98 | + return false; | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + if (!isCryptoKey) { | ||
| 102 | + ({ isCryptoKey } = require('internal/crypto/keys')); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + return isCryptoKey(obj); | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ if (common.hasOpenSSL3) | |||
| 8 | 8 | common.skip('temporarily skipping for OpenSSL 3.0-alpha15'); | |
| 9 | 9 | ||
| 10 | 10 | const assert = require('assert'); | |
| 11 | + const { types: { isKeyObject } } = require('util'); | ||
| 11 | 12 | const { | |
| 12 | 13 | createCipheriv, | |
| 13 | 14 | createDecipheriv, | |
@@ -23,7 +24,8 @@ const { | |||
| 23 | 24 | privateDecrypt, | |
| 24 | 25 | privateEncrypt, | |
| 25 | 26 | getCurves, | |
| 26 | - generateKeyPairSync | ||
| 27 | + generateKeyPairSync, | ||
| 28 | + webcrypto, | ||
| 27 | 29 | } = require('crypto'); | |
| 28 | 30 | ||
| 29 | 31 | const fixtures = require('../common/fixtures'); | |
@@ -774,3 +776,24 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem', | |||
| 774 | 776 | message: `Unsupported JWK EC curve: ${namedCurve}.` | |
| 775 | 777 | }); | |
| 776 | 778 | } | |
| 779 | + | ||
| 780 | + { | ||
| 781 | + const buffer = Buffer.from('Hello World'); | ||
| 782 | + const keyObject = createSecretKey(buffer); | ||
| 783 | + const keyPair = generateKeyPairSync('ec', { namedCurve: 'P-256' }); | ||
| 784 | + assert(isKeyObject(keyPair.publicKey)); | ||
| 785 | + assert(isKeyObject(keyPair.privateKey)); | ||
| 786 | + assert(isKeyObject(keyObject)); | ||
| 787 | + | ||
| 788 | + assert(!isKeyObject(buffer)); | ||
| 789 | + | ||
| 790 | + webcrypto.subtle.importKey( | ||
| 791 | + 'node.keyObject', | ||
| 792 | + keyPair.publicKey, | ||
| 793 | + { name: 'ECDH', namedCurve: 'P-256' }, | ||
| 794 | + false, | ||
| 795 | + [], | ||
| 796 | + ).then((cryptoKey) => { | ||
| 797 | + assert(!isKeyObject(cryptoKey)); | ||
| 798 | + }); | ||
| 799 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -281,3 +281,13 @@ for (const [ value, _method ] of [ | |||
| 281 | 281 | await m.evaluate(); | |
| 282 | 282 | assert.ok(types.isModuleNamespaceObject(m.namespace)); | |
| 283 | 283 | })().then(common.mustCall()); | |
| 284 | + | ||
| 285 | + { | ||
| 286 | + // eslint-disable-next-line node-core/crypto-check | ||
| 287 | + if (common.hasCrypto) { | ||
| 288 | + const crypto = require('crypto'); | ||
| 289 | + assert.ok(!types.isKeyObject(crypto.createHash('sha1'))); | ||
| 290 | + } | ||
| 291 | + assert.ok(!types.isCryptoKey()); | ||
| 292 | + assert.ok(!types.isKeyObject()); | ||
| 293 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,11 @@ if (!common.hasCrypto) | |||
| 6 | 6 | common.skip('missing crypto'); | |
| 7 | 7 | ||
| 8 | 8 | const assert = require('assert'); | |
| 9 | - const { subtle, CryptoKey } = require('crypto').webcrypto; | ||
| 9 | + const { types: { isCryptoKey } } = require('util'); | ||
| 10 | + const { | ||
| 11 | + webcrypto: { subtle, CryptoKey }, | ||
| 12 | + createSecretKey, | ||
| 13 | + } = require('crypto'); | ||
| 10 | 14 | ||
| 11 | 15 | const allUsages = [ | |
| 12 | 16 | 'encrypt', | |
@@ -220,6 +224,8 @@ const vectors = { | |||
| 220 | 224 | ||
| 221 | 225 | assert(publicKey); | |
| 222 | 226 | assert(privateKey); | |
| 227 | + assert(isCryptoKey(publicKey)); | ||
| 228 | + assert(isCryptoKey(privateKey)); | ||
| 223 | 229 | ||
| 224 | 230 | assert(publicKey instanceof CryptoKey); | |
| 225 | 231 | assert(privateKey instanceof CryptoKey); | |
@@ -366,6 +372,8 @@ const vectors = { | |||
| 366 | 372 | ||
| 367 | 373 | assert(publicKey); | |
| 368 | 374 | assert(privateKey); | |
| 375 | + assert(isCryptoKey(publicKey)); | ||
| 376 | + assert(isCryptoKey(privateKey)); | ||
| 369 | 377 | ||
| 370 | 378 | assert.strictEqual(publicKey.type, 'public'); | |
| 371 | 379 | assert.strictEqual(privateKey.type, 'private'); | |
@@ -430,6 +438,7 @@ const vectors = { | |||
| 430 | 438 | }, true, usages); | |
| 431 | 439 | ||
| 432 | 440 | assert(key); | |
| 441 | + assert(isCryptoKey(key)); | ||
| 433 | 442 | ||
| 434 | 443 | assert.strictEqual(key.type, 'secret'); | |
| 435 | 444 | assert.strictEqual(key.extractable, true); | |
@@ -488,6 +497,7 @@ const vectors = { | |||
| 488 | 497 | } | |
| 489 | 498 | ||
| 490 | 499 | assert(key); | |
| 500 | + assert(isCryptoKey(key)); | ||
| 491 | 501 | ||
| 492 | 502 | assert.strictEqual(key.type, 'secret'); | |
| 493 | 503 | assert.strictEqual(key.extractable, true); | |
@@ -544,6 +554,8 @@ const vectors = { | |||
| 544 | 554 | ||
| 545 | 555 | assert(publicKey); | |
| 546 | 556 | assert(privateKey); | |
| 557 | + assert(isCryptoKey(publicKey)); | ||
| 558 | + assert(isCryptoKey(privateKey)); | ||
| 547 | 559 | ||
| 548 | 560 | assert.strictEqual(publicKey.type, 'public'); | |
| 549 | 561 | assert.strictEqual(privateKey.type, 'private'); | |
@@ -634,6 +646,8 @@ const vectors = { | |||
| 634 | 646 | }, true, ['deriveKey']); | |
| 635 | 647 | assert(publicKey); | |
| 636 | 648 | assert(privateKey); | |
| 649 | + assert(isCryptoKey(publicKey)); | ||
| 650 | + assert(isCryptoKey(privateKey)); | ||
| 637 | 651 | assert.strictEqual(publicKey.type, 'public'); | |
| 638 | 652 | assert.strictEqual(privateKey.type, 'private'); | |
| 639 | 653 | assert.strictEqual(publicKey.algorithm.name, 'NODE-DH'); | |
@@ -646,3 +660,10 @@ const vectors = { | |||
| 646 | 660 | assert.throws(() => new CryptoKey(), { | |
| 647 | 661 | code: 'ERR_OPERATION_FAILED' | |
| 648 | 662 | }); | |
| 663 | + | ||
| 664 | + { | ||
| 665 | + const buffer = Buffer.from('Hello World'); | ||
| 666 | + const keyObject = createSecretKey(buffer); | ||
| 667 | + assert(!isCryptoKey(buffer)); | ||
| 668 | + assert(!isCryptoKey(keyObject)); | ||
| 669 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments