crypto: fix EdDSA support for KeyObject

PR-URL: https://github.com/nodejs/node/pull/26319
Fixes: https://github.com/nodejs/node/issues/26316
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
This commit is contained in:
Brian White 2019-02-26 05:24:38 -05:00
parent 4562697feb
commit 247c14c040
No known key found for this signature in database
GPG Key ID: 606D7358F94DA209
8 changed files with 57 additions and 1 deletions

View File

@ -1133,11 +1133,16 @@ bytes. This property is `undefined` for symmetric keys.
### keyObject.asymmetricKeyType
<!-- YAML
added: v11.6.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/26319
description: Added support for `'ed25519'` and `'ed448'`
-->
* {string}
For asymmetric keys, this property represents the type of the embedded key
(`'rsa'`, `'dsa'` or `'ec'`). This property is `undefined` for symmetric keys.
(`'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, or `'ed448'`).
This property is `undefined` for symmetric keys.
### keyObject.export([options])
<!-- YAML

View File

@ -144,6 +144,8 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
V(constants_string, "constants") \
V(crypto_dsa_string, "dsa") \
V(crypto_ec_string, "ec") \
V(crypto_ed25519_string, "ed25519") \
V(crypto_ed448_string, "ed448") \
V(crypto_rsa_string, "rsa") \
V(cwd_string, "cwd") \
V(data_string, "data") \

View File

@ -3468,6 +3468,10 @@ Local<String> KeyObject::GetAsymmetricKeyType() const {
return env()->crypto_dsa_string();
case EVP_PKEY_EC:
return env()->crypto_ec_string();
case EVP_PKEY_ED25519:
return env()->crypto_ed25519_string();
case EVP_PKEY_ED448:
return env()->crypto_ed448_string();
default:
CHECK(false);
}

View File

@ -0,0 +1,3 @@
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIHXLsXm1lsq5HtyqJwQyFmpfEluuf0KOqP6DqMgGxxDL
-----END PRIVATE KEY-----

3
test/fixtures/test_ed25519_pubkey.pem vendored Normal file
View File

@ -0,0 +1,3 @@
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAEXRYV3v5ucrHVR3mKqyPXxXqU34lASwc7Y7MoOvaqcs=
-----END PUBLIC KEY-----

4
test/fixtures/test_ed448_privkey.pem vendored Normal file
View File

@ -0,0 +1,4 @@
-----BEGIN PRIVATE KEY-----
MEcCAQAwBQYDK2VxBDsEObxytD95dGN3Hxk7kVk+Lig1rGYTRr3YdaHjRog++Sgk
QD7KwKmxroBURtkE2N0JbQ3ctdrpGRB5DQ==
-----END PRIVATE KEY-----

4
test/fixtures/test_ed448_pubkey.pem vendored Normal file
View File

@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MEMwBQYDK2VxAzoAIESY3jnpGdB5UVJDCznrv0vmBFIzgSMu+gafsbCX1rFtsJwR
M6XUDQiEY7dk6rmm/Fktyawna5EA
-----END PUBLIC KEY-----

View File

@ -170,3 +170,34 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
createPrivateKey({ key: '' });
}, /null/);
}
[
{ private: fixtures.readSync('test_ed25519_privkey.pem', 'ascii'),
public: fixtures.readSync('test_ed25519_pubkey.pem', 'ascii'),
keyType: 'ed25519' },
{ private: fixtures.readSync('test_ed448_privkey.pem', 'ascii'),
public: fixtures.readSync('test_ed448_pubkey.pem', 'ascii'),
keyType: 'ed448' }
].forEach((info) => {
const keyType = info.keyType;
{
const exportOptions = { type: 'pkcs8', format: 'pem' };
const key = createPrivateKey(info.private);
assert.strictEqual(key.type, 'private');
assert.strictEqual(key.asymmetricKeyType, keyType);
assert.strictEqual(key.symmetricKeySize, undefined);
assert.strictEqual(key.export(exportOptions), info.private);
}
{
const exportOptions = { type: 'spki', format: 'pem' };
[info.private, info.public].forEach((pem) => {
const key = createPublicKey(pem);
assert.strictEqual(key.type, 'public');
assert.strictEqual(key.asymmetricKeyType, keyType);
assert.strictEqual(key.symmetricKeySize, undefined);
assert.strictEqual(key.export(exportOptions), info.public);
});
}
});