Skip to content

Commit

Permalink
crypto: reject public keys properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tniessen committed Oct 10, 2019
1 parent a04b04f commit 11b3dde
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-crypto-key-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
});
}

[
Expand Down

0 comments on commit 11b3dde

Please sign in to comment.