Skip to content

Commit 32e45b2

Browse 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

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

doc/api/crypto.md

+9-14
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');

lib/internal/crypto/keygen.js

+5-1
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) {

test/parallel/test-crypto-keygen.js

+15
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)