Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: fix encrypted private -> public import #37056

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,10 @@ function createSecretKey(key, encoding) {
}

function createPublicKey(key) {
const { format, type, data } = prepareAsymmetricKey(key, kCreatePublic);
const { format, type, data, passphrase } =
prepareAsymmetricKey(key, kCreatePublic);
const handle = new KeyObjectHandle();
handle.init(kKeyTypePublic, data, format, type);
handle.init(kKeyTypePublic, data, format, type, passphrase);
return new PublicKeyObject(handle);
}

Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ void KeyObjectHandle::Init(const FunctionCallbackInfo<Value>& args) {
break;
}
case kKeyTypePublic: {
CHECK_EQ(args.Length(), 4);
CHECK_EQ(args.Length(), 5);

offset = 1;
pkey = ManagedEVPPKey::GetPublicOrPrivateKeyFromJs(args, &offset);
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-crypto-key-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
assert.strictEqual(derivedPublicKey.asymmetricKeyType, 'rsa');
assert.strictEqual(derivedPublicKey.symmetricKeySize, undefined);

// It should also be possible to import an encrypted private key as a public
// key.
const decryptedKey = createPublicKey({
key: privateKey.export({
type: 'pkcs8',
format: 'pem',
passphrase: '123',
cipher: 'aes-128-cbc'
}),
format: 'pem',
passphrase: '123'
});
assert.strictEqual(decryptedKey.type, 'public');
assert.strictEqual(decryptedKey.asymmetricKeyType, 'rsa');

// Test exporting with an invalid options object, this should throw.
for (const opt of [undefined, null, 'foo', 0, NaN]) {
assert.throws(() => publicKey.export(opt), {
Expand Down