diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index db7be824ac3e16..ecd747d947516f 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -215,7 +215,7 @@ function parsePrivateKeyEncoding(enc, keyType, objName) { function getKeyObjectHandle(key, isPublic, allowKeyObject) { if (!allowKeyObject) { - return new ERR_INVALID_ARG_TYPE( + throw new ERR_INVALID_ARG_TYPE( 'key', ['string', 'Buffer', 'TypedArray', 'DataView'], key diff --git a/test/parallel/test-crypto-key-objects.js b/test/parallel/test-crypto-key-objects.js index 8a28ac996084a5..d4ec93fbbfdd66 100644 --- a/test/parallel/test-crypto-key-objects.js +++ b/test/parallel/test-crypto-key-objects.js @@ -58,6 +58,17 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii'); assert(plaintext.equals(deciphered)); } +{ + // Passing an existing key object should throw. + const publicKey = createPublicKey(publicPem); + common.expectsError(() => createPublicKey(publicKey), { + type: TypeError, + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "key" argument must be one of type string, Buffer, ' + + 'TypedArray, or DataView. Received type object' + }); +} + { const publicKey = createPublicKey(publicPem); assert.strictEqual(publicKey.type, 'public');