Skip to content

Commit c4193ba

Browse files
tniessentargos
authored andcommitted
crypto: fix encrypted private -> public import
PR-URL: #37056 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent cb3b0ec commit c4193ba

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lib/internal/crypto/keys.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,10 @@ function createSecretKey(key, encoding) {
428428
}
429429

430430
function createPublicKey(key) {
431-
const { format, type, data } = prepareAsymmetricKey(key, kCreatePublic);
431+
const { format, type, data, passphrase } =
432+
prepareAsymmetricKey(key, kCreatePublic);
432433
const handle = new KeyObjectHandle();
433-
handle.init(kKeyTypePublic, data, format, type);
434+
handle.init(kKeyTypePublic, data, format, type, passphrase);
434435
return new PublicKeyObject(handle);
435436
}
436437

src/crypto/crypto_keys.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ void KeyObjectHandle::Init(const FunctionCallbackInfo<Value>& args) {
939939
break;
940940
}
941941
case kKeyTypePublic: {
942-
CHECK_EQ(args.Length(), 4);
942+
CHECK_EQ(args.Length(), 5);
943943

944944
offset = 1;
945945
pkey = ManagedEVPPKey::GetPublicOrPrivateKeyFromJs(args, &offset);

test/parallel/test-crypto-key-objects.js

+15
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,21 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
132132
assert.strictEqual(derivedPublicKey.asymmetricKeyType, 'rsa');
133133
assert.strictEqual(derivedPublicKey.symmetricKeySize, undefined);
134134

135+
// It should also be possible to import an encrypted private key as a public
136+
// key.
137+
const decryptedKey = createPublicKey({
138+
key: privateKey.export({
139+
type: 'pkcs8',
140+
format: 'pem',
141+
passphrase: '123',
142+
cipher: 'aes-128-cbc'
143+
}),
144+
format: 'pem',
145+
passphrase: '123'
146+
});
147+
assert.strictEqual(decryptedKey.type, 'public');
148+
assert.strictEqual(decryptedKey.asymmetricKeyType, 'rsa');
149+
135150
// Test exporting with an invalid options object, this should throw.
136151
for (const opt of [undefined, null, 'foo', 0, NaN]) {
137152
assert.throws(() => publicKey.export(opt), {

0 commit comments

Comments
 (0)