Skip to content

Commit

Permalink
crypto: fix crash of encrypted private key export without cipher
Browse files Browse the repository at this point in the history
PR-URL: nodejs#27041
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
panva authored and BridgeAR committed Apr 4, 2019
1 parent 6fb32ac commit b2bb6c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-crypto-key-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,17 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
assert.strictEqual(privateKey.asymmetricKeyType, 'dsa');
assert.strictEqual(privateKey.symmetricKeySize, undefined);
}

{
// 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"'
});
}

0 comments on commit b2bb6c2

Please sign in to comment.