Skip to content
Permalink
Browse files

crypto: improve error handling in parseKeyEncoding

This change only affects KeyObject.export().

Backport-PR-URL: #26688
PR-URL: #26455
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information...
tniessen authored and targos committed Mar 5, 2019
1 parent ed7599b commit f2a07df27f86a4c7947e5041bc1779be0a78d43f
Showing with 13 additions and 0 deletions.
  1. +3 −0 lib/internal/crypto/keys.js
  2. +10 −0 test/parallel/test-crypto-key-objects.js
@@ -172,6 +172,9 @@ function isStringOrBuffer(val) {
}

function parseKeyEncoding(enc, keyType, isPublic, objName) {
if (enc === null || typeof enc !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', enc);

const isInput = keyType === undefined;

const {
@@ -104,6 +104,16 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
assert.strictEqual(derivedPublicKey.asymmetricKeyType, 'rsa');
assert.strictEqual(derivedPublicKey.symmetricKeySize, undefined);

// Test exporting with an invalid options object, this should throw.
for (const opt of [undefined, null, 'foo', 0, NaN]) {
common.expectsError(() => publicKey.export(opt), {
type: TypeError,
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type object. Received type ' +
typeof opt
});
}

const publicDER = publicKey.export({
format: 'der',
type: 'pkcs1'

0 comments on commit f2a07df

Please sign in to comment.
You can’t perform that action at this time.