mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
crypto: fix crash of encrypted private key export without cipher
PR-URL: https://github.com/nodejs/node/pull/27041 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
This commit is contained in:
parent
70d9f32011
commit
066cd6090f
|
|
@ -186,14 +186,18 @@ function parseKeyEncoding(enc, keyType, isPublic, objName) {
|
|||
if (isPublic !== true) {
|
||||
({ cipher, passphrase } = enc);
|
||||
|
||||
if (!isInput && cipher != null) {
|
||||
if (typeof cipher !== 'string')
|
||||
if (!isInput) {
|
||||
if (cipher != null) {
|
||||
if (typeof cipher !== 'string')
|
||||
throw new ERR_INVALID_OPT_VALUE(option('cipher', objName), cipher);
|
||||
if (format === kKeyFormatDER &&
|
||||
(type === kKeyEncodingPKCS1 ||
|
||||
type === kKeyEncodingSEC1)) {
|
||||
throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(
|
||||
encodingNames[type], 'does not support encryption');
|
||||
}
|
||||
} else if (passphrase !== undefined) {
|
||||
throw new ERR_INVALID_OPT_VALUE(option('cipher', objName), cipher);
|
||||
if (format === kKeyFormatDER &&
|
||||
(type === kKeyEncodingPKCS1 ||
|
||||
type === kKeyEncodingSEC1)) {
|
||||
throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(
|
||||
encodingNames[type], 'does not support encryption');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -167,3 +167,17 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
|
|||
createPrivateKey({ key: '' });
|
||||
}, /null/);
|
||||
}
|
||||
|
||||
{
|
||||
// Exporting an encrypted private key requires a cipher
|
||||
const privateKey = createPrivateKey(privatePem);
|
||||
common.expectsError(() => {
|
||||
privateKey.export({
|
||||
format: 'pem', type: 'pkcs8', passphrase: 'super-secret'
|
||||
});
|
||||
}, {
|
||||
type: TypeError,
|
||||
code: 'ERR_INVALID_OPT_VALUE',
|
||||
message: 'The value "undefined" is invalid for option "cipher"'
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user