diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index fb17ba36ced0e3..250147d7529439 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -270,7 +270,10 @@ function prepareAsymmetricKey(key, ctx) { ...(ctx !== kCreatePrivate ? ['KeyObject'] : [])], key); } - return { data, ...parseKeyEncoding(key, undefined) }; + + const isPublic = + (ctx === kConsumePrivate || ctx === kCreatePrivate) ? false : undefined; + return { data, ...parseKeyEncoding(key, undefined, isPublic) }; } else { throw new ERR_INVALID_ARG_TYPE( 'key', diff --git a/test/parallel/test-crypto-key-objects.js b/test/parallel/test-crypto-key-objects.js index 558f7dc1f4b673..6fc41677abe9b1 100644 --- a/test/parallel/test-crypto-key-objects.js +++ b/test/parallel/test-crypto-key-objects.js @@ -200,6 +200,14 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem', library: 'BIO routines', function: 'BIO_new_mem_buf', }); + + // This should not abort either: https://github.com/nodejs/node/issues/29904 + assert.throws(() => { + createPrivateKey({ key: Buffer.alloc(0), format: 'der', type: 'spki' }); + }, { + code: 'ERR_INVALID_OPT_VALUE', + message: 'The value "spki" is invalid for option "type"' + }); } [