Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 32e45b2

Browse filesBrowse files
tniessenaddaleax
authored andcommitted
crypto: fix key object wrapping in sync keygen
PR-URL: #25326 Fixes: #25322 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 456f76a commit 32e45b2
Copy full SHA for 32e45b2

File tree

Expand file treeCollapse file tree

3 files changed

+29
-15
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+29
-15
lines changed
Open diff view settings
Collapse file

‎doc/api/crypto.md‎

Copy file name to clipboardExpand all lines: doc/api/crypto.md
+9-14Lines changed: 9 additions & 14 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1951,27 +1951,22 @@ changes:
19511951
- `publicExponent`: {number} Public exponent (RSA). **Default:** `0x10001`.
19521952
- `divisorLength`: {number} Size of `q` in bits (DSA).
19531953
- `namedCurve`: {string} Name of the curve to use (EC).
1954-
- `publicKeyEncoding`: {Object}
1955-
- `type`: {string} Must be one of `'pkcs1'` (RSA only) or `'spki'`.
1956-
- `format`: {string} Must be `'pem'` or `'der'`.
1957-
- `privateKeyEncoding`: {Object}
1958-
- `type`: {string} Must be one of `'pkcs1'` (RSA only), `'pkcs8'` or
1959-
`'sec1'` (EC only).
1960-
- `format`: {string} Must be `'pem'` or `'der'`.
1961-
- `cipher`: {string} If specified, the private key will be encrypted with
1962-
the given `cipher` and `passphrase` using PKCS#5 v2.0 password based
1963-
encryption.
1964-
- `passphrase`: {string | Buffer} The passphrase to use for encryption, see
1965-
`cipher`.
1954+
- `publicKeyEncoding`: {Object} See [`keyObject.export()`][].
1955+
- `privateKeyEncoding`: {Object} See [`keyObject.export()`][].
19661956
* Returns: {Object}
19671957
- `publicKey`: {string | Buffer | KeyObject}
19681958
- `privateKey`: {string | Buffer | KeyObject}
19691959

19701960
Generates a new asymmetric key pair of the given `type`. Only RSA, DSA and EC
19711961
are currently supported.
19721962

1973-
It is recommended to encode public keys as `'spki'` and private keys as
1974-
`'pkcs8'` with encryption:
1963+
If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function
1964+
behaves as if [`keyObject.export()`][] had been called on its result. Otherwise,
1965+
the respective part of the key is returned as a [`KeyObject`].
1966+
1967+
When encoding public keys, it is recommended to use `'spki'`. When encoding
1968+
private keys, it is recommended to use `'pks8'` with a strong passphrase, and to
1969+
keep the passphrase confidential.
19751970

19761971
```js
19771972
const { generateKeyPairSync } = require('crypto');
Collapse file

‎lib/internal/crypto/keygen.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/keygen.js
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ function handleError(impl, wrap) {
7474
if (err !== undefined)
7575
throw err;
7676

77-
return { publicKey, privateKey };
77+
// If no encoding was chosen, return key objects instead.
78+
return {
79+
publicKey: wrapKey(publicKey, PublicKeyObject),
80+
privateKey: wrapKey(privateKey, PrivateKeyObject)
81+
};
7882
}
7983

8084
function parseKeyEncoding(keyType, options) {
Collapse file

‎test/parallel/test-crypto-keygen.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-keygen.js
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
9595
testSignVerify(publicKey, privateKey);
9696
}
9797

98+
{
99+
// Test sync key generation with key objects.
100+
const { publicKey, privateKey } = generateKeyPairSync('rsa', {
101+
modulusLength: 512
102+
});
103+
104+
assert.strictEqual(typeof publicKey, 'object');
105+
assert.strictEqual(publicKey.type, 'public');
106+
assert.strictEqual(publicKey.asymmetricKeyType, 'rsa');
107+
108+
assert.strictEqual(typeof privateKey, 'object');
109+
assert.strictEqual(privateKey.type, 'private');
110+
assert.strictEqual(privateKey.asymmetricKeyType, 'rsa');
111+
}
112+
98113
{
99114
const publicKeyEncoding = {
100115
type: 'pkcs1',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.