Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
crypto: add keyObject.asymmetricKeyDetails for asymmetric keys#36188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
2efd01595fc8833559e94ec80c32File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1284,6 +1284,25 @@ 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. | ||
| ### `keyObject.asymmetricKeyDetails` | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| * {Object} | ||
| * `modulusLength`: {number} Key size in bits (RSA, DSA). | ||
| * `publicExponent`: {bigint} Public exponent (RSA). | ||
| * `divisorLength`: {number} Size of `q` in bits (DSA). | ||
| * `namedCurve`: {string} Name of the curve (EC). | ||
| This property exists only on asymmetric keys. Depending on the type of the key, | ||
| this object contains information about the key. None of the information obtained | ||
| through this property can be used to uniquely identify a key or to compromise | ||
| the security of the key. | ||
panva marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| RSA-PSS parameters, DH, or any future key type details might be exposed via this | ||
| API using additional attributes. | ||
| ### `keyObject.asymmetricKeyType` | ||
| <!-- YAML | ||
| added: v11.6.0 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,6 +3,7 @@ | ||
| const { | ||
| ArrayPrototypeIncludes, | ||
| ArrayPrototypePush, | ||
| BigInt, | ||
| FunctionPrototypeBind, | ||
| Number, | ||
| Promise, | ||
| @@ -308,6 +309,17 @@ function bigIntArrayToUnsignedInt(input) { | ||
| return result; | ||
| } | ||
| function bigIntArrayToUnsignedBigInt(input) { | ||
| let result = 0n; | ||
| for (let n = 0; n < input.length; ++n) { | ||
| const n_reversed = input.length - n - 1; | ||
| result |= BigInt(input[n]) << 8n * BigInt(n_reversed); | ||
| } | ||
| return result; | ||
| } | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion, feel free to ignore: Out of curiosity, did you try benchmarking this versus BigInt(`0x${Buffer.from(input).toString('hex')}`)Or, if performance really is a concern in this code path, BigInt(`0x${Buffer.from(input.buffer,input.byteOffset,input.byteLength).toString('hex')}`)(I know that this is essentially the same as the existing function | ||
| function getStringOption(options, key) { | ||
| let value; | ||
| if (options && (value = options[key]) != null) | ||
| @@ -413,6 +425,7 @@ module.exports = { | ||
| jobPromise, | ||
| lazyRequire, | ||
| validateMaxBufferLength, | ||
| bigIntArrayToUnsignedBigInt, | ||
| bigIntArrayToUnsignedInt, | ||
| getStringOption, | ||
| getUsagesUnion, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -114,6 +114,31 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher); | ||
| testSignVerify(publicKey, privateKey); | ||
| } | ||
| { | ||
| // Test sync key generation with key objects with a non-standard | ||
| // publicExpononent | ||
| const { publicKey, privateKey } = generateKeyPairSync('rsa', { | ||
| publicExponent: 3, | ||
| modulusLength: 512 | ||
| }); | ||
| assert.strictEqual(typeof publicKey, 'object'); | ||
| assert.strictEqual(publicKey.type, 'public'); | ||
| assert.strictEqual(publicKey.asymmetricKeyType, 'rsa'); | ||
| assert.deepStrictEqual(publicKey.asymmetricKeyDetails, { | ||
| modulusLength: 512, | ||
| publicExponent: 3n | ||
| }); | ||
| assert.strictEqual(typeof privateKey, 'object'); | ||
| assert.strictEqual(privateKey.type, 'private'); | ||
| assert.strictEqual(privateKey.asymmetricKeyType, 'rsa'); | ||
| assert.deepStrictEqual(privateKey.asymmetricKeyDetails, { | ||
| modulusLength: 512, | ||
| publicExponent: 3n | ||
| }); | ||
| } | ||
| { | ||
| // Test sync key generation with key objects. | ||
| const { publicKey, privateKey } = generateKeyPairSync('rsa', { | ||
| @@ -123,10 +148,18 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher); | ||
| assert.strictEqual(typeof publicKey, 'object'); | ||
| assert.strictEqual(publicKey.type, 'public'); | ||
| assert.strictEqual(publicKey.asymmetricKeyType, 'rsa'); | ||
| assert.deepStrictEqual(publicKey.asymmetricKeyDetails, { | ||
| modulusLength: 512, | ||
| publicExponent: 65537n | ||
| }); | ||
panva marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| assert.strictEqual(typeof privateKey, 'object'); | ||
| assert.strictEqual(privateKey.type, 'private'); | ||
| assert.strictEqual(privateKey.asymmetricKeyType, 'rsa'); | ||
| assert.deepStrictEqual(privateKey.asymmetricKeyDetails, { | ||
| modulusLength: 512, | ||
| publicExponent: 65537n | ||
| }); | ||
| } | ||
| { | ||
| @@ -268,9 +301,17 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher); | ||
| }, common.mustSucceed((publicKey, privateKey) => { | ||
| assert.strictEqual(publicKey.type, 'public'); | ||
| assert.strictEqual(publicKey.asymmetricKeyType, 'rsa-pss'); | ||
| assert.deepStrictEqual(publicKey.asymmetricKeyDetails, { | ||
| modulusLength: 512, | ||
| publicExponent: 65537n | ||
| }); | ||
| assert.strictEqual(privateKey.type, 'private'); | ||
| assert.strictEqual(privateKey.asymmetricKeyType, 'rsa-pss'); | ||
| assert.deepStrictEqual(privateKey.asymmetricKeyDetails, { | ||
| modulusLength: 512, | ||
| publicExponent: 65537n | ||
| }); | ||
| // Unlike RSA, RSA-PSS does not allow encryption. | ||
| assert.throws(() => { | ||
| @@ -342,6 +383,28 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher); | ||
| })); | ||
| } | ||
| { | ||
| // Test async DSA key object generation. | ||
| generateKeyPair('dsa', { | ||
| modulusLength: 512, | ||
| divisorLength: 256 | ||
| }, common.mustSucceed((publicKey, privateKey) => { | ||
| assert.strictEqual(publicKey.type, 'public'); | ||
| assert.strictEqual(publicKey.asymmetricKeyType, 'dsa'); | ||
| assert.deepStrictEqual(publicKey.asymmetricKeyDetails, { | ||
| modulusLength: 512, | ||
| divisorLength: 256 | ||
| }); | ||
| assert.strictEqual(privateKey.type, 'private'); | ||
| assert.strictEqual(privateKey.asymmetricKeyType, 'dsa'); | ||
| assert.deepStrictEqual(privateKey.asymmetricKeyDetails, { | ||
| modulusLength: 512, | ||
| divisorLength: 256 | ||
| }); | ||
| })); | ||
| } | ||
| { | ||
| // Test async elliptic curve key generation, e.g. for ECDSA, with a SEC1 | ||
| // private key. | ||
| @@ -925,16 +988,24 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher); | ||
| // It should recognize both NIST and standard curve names. | ||
| generateKeyPair('ec', { | ||
| namedCurve: 'P-256', | ||
| publicKeyEncoding: { type: 'spki', format: 'pem' }, | ||
| privateKeyEncoding: { type: 'pkcs8', format: 'pem' } | ||
| }, common.mustSucceed((publicKey, privateKey) => { | ||
| assert.deepStrictEqual(publicKey.asymmetricKeyDetails, { | ||
| namedCurve: 'prime256v1' | ||
| }); | ||
| assert.deepStrictEqual(privateKey.asymmetricKeyDetails, { | ||
| namedCurve: 'prime256v1' | ||
| }); | ||
| })); | ||
| generateKeyPair('ec', { | ||
| namedCurve: 'secp256k1', | ||
| publicKeyEncoding: { type: 'spki', format: 'pem' }, | ||
| privateKeyEncoding: { type: 'pkcs8', format: 'pem' } | ||
| }, common.mustSucceed((publicKey, privateKey) => { | ||
| assert.deepStrictEqual(publicKey.asymmetricKeyDetails, { | ||
| namedCurve: 'secp256k1' | ||
| }); | ||
| assert.deepStrictEqual(privateKey.asymmetricKeyDetails, { | ||
| namedCurve: 'secp256k1' | ||
| }); | ||
| })); | ||
| } | ||
| @@ -945,9 +1016,11 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher); | ||
| generateKeyPair(keyType, common.mustSucceed((publicKey, privateKey) => { | ||
| assert.strictEqual(publicKey.type, 'public'); | ||
| assert.strictEqual(publicKey.asymmetricKeyType, keyType); | ||
| assert.deepStrictEqual(publicKey.asymmetricKeyDetails, {}); | ||
| assert.strictEqual(privateKey.type, 'private'); | ||
| assert.strictEqual(privateKey.asymmetricKeyType, keyType); | ||
| assert.deepStrictEqual(privateKey.asymmetricKeyDetails, {}); | ||
| })); | ||
| }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.