Skip to content

Commit 4895927

Browse files
parogaBridgeAR
authored andcommitted
crypto: add KeyObject.asymmetricKeySize
Expose the size of asymetric keys of crypto key object from the crypto module added in v11.6.0. PR-URL: #26387 Refs: #24234 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 53d4e04 commit 4895927

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed

doc/api/crypto.md

+9
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,15 @@ exposes different functions.
11211121
Most applications should consider using the new `KeyObject` API instead of
11221122
passing keys as strings or `Buffer`s due to improved security features.
11231123

1124+
### keyObject.asymmetricKeySize
1125+
<!-- YAML
1126+
added: REPLACEME
1127+
-->
1128+
* {number}
1129+
1130+
For asymmetric keys, this property represents the size of the embedded key in
1131+
bytes. This property is `undefined` for symmetric keys.
1132+
11241133
### keyObject.asymmetricKeyType
11251134
<!-- YAML
11261135
added: v11.6.0

lib/internal/crypto/keys.js

+6
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,15 @@ class SecretKeyObject extends KeyObject {
7373
}
7474
}
7575

76+
const kAsymmetricKeySize = Symbol('kAsymmetricKeySize');
7677
const kAsymmetricKeyType = Symbol('kAsymmetricKeyType');
7778

7879
class AsymmetricKeyObject extends KeyObject {
80+
get asymmetricKeySize() {
81+
return this[kAsymmetricKeySize] ||
82+
(this[kAsymmetricKeySize] = this[kHandle].getAsymmetricKeySize());
83+
}
84+
7985
get asymmetricKeyType() {
8086
return this[kAsymmetricKeyType] ||
8187
(this[kAsymmetricKeyType] = this[kHandle].getAsymmetricKeyType());

src/node_crypto.cc

+13
Original file line numberDiff line numberDiff line change
@@ -3330,6 +3330,8 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
33303330
t->InstanceTemplate()->SetInternalFieldCount(1);
33313331

33323332
env->SetProtoMethod(t, "init", Init);
3333+
env->SetProtoMethodNoSideEffect(t, "getAsymmetricKeySize",
3334+
GetAsymmetricKeySize);
33333335
env->SetProtoMethodNoSideEffect(t, "getSymmetricKeySize",
33343336
GetSymmetricKeySize);
33353337
env->SetProtoMethodNoSideEffect(t, "getAsymmetricKeyType",
@@ -3375,6 +3377,11 @@ const char* KeyObject::GetSymmetricKey() const {
33753377
return this->symmetric_key_.get();
33763378
}
33773379

3380+
size_t KeyObject::GetAsymmetricKeySize() const {
3381+
CHECK_NE(key_type_, kKeyTypeSecret);
3382+
return EVP_PKEY_size(this->asymmetric_key_.get());
3383+
}
3384+
33783385
size_t KeyObject::GetSymmetricKeySize() const {
33793386
CHECK_EQ(key_type_, kKeyTypeSecret);
33803387
return this->symmetric_key_len_;
@@ -3474,6 +3481,12 @@ void KeyObject::GetAsymmetricKeyType(const FunctionCallbackInfo<Value>& args) {
34743481
args.GetReturnValue().Set(key->GetAsymmetricKeyType());
34753482
}
34763483

3484+
void KeyObject::GetAsymmetricKeySize(const FunctionCallbackInfo<Value>& args) {
3485+
KeyObject* key;
3486+
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());
3487+
args.GetReturnValue().Set(static_cast<uint32_t>(key->GetAsymmetricKeySize()));
3488+
}
3489+
34773490
void KeyObject::GetSymmetricKeySize(const FunctionCallbackInfo<Value>& args) {
34783491
KeyObject* key;
34793492
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());

src/node_crypto.h

+4
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ class KeyObject : public BaseObject {
456456
// only be used to implement cryptograohic operations requiring the key.
457457
ManagedEVPPKey GetAsymmetricKey() const;
458458
const char* GetSymmetricKey() const;
459+
size_t GetAsymmetricKeySize() const;
459460
size_t GetSymmetricKeySize() const;
460461

461462
protected:
@@ -470,6 +471,9 @@ class KeyObject : public BaseObject {
470471
const v8::FunctionCallbackInfo<v8::Value>& args);
471472
v8::Local<v8::String> GetAsymmetricKeyType() const;
472473

474+
static void GetAsymmetricKeySize(
475+
const v8::FunctionCallbackInfo<v8::Value>& args);
476+
473477
static void GetSymmetricKeySize(
474478
const v8::FunctionCallbackInfo<v8::Value>& args);
475479

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

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
3838
const key = createSecretKey(keybuf);
3939
assert.strictEqual(key.type, 'secret');
4040
assert.strictEqual(key.symmetricKeySize, 32);
41+
assert.strictEqual(key.asymmetricKeySize, undefined);
4142
assert.strictEqual(key.asymmetricKeyType, undefined);
4243

4344
const exportedKey = key.export();
@@ -91,11 +92,13 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
9192
const publicKey = createPublicKey(publicPem);
9293
assert.strictEqual(publicKey.type, 'public');
9394
assert.strictEqual(publicKey.asymmetricKeyType, 'rsa');
95+
assert.strictEqual(publicKey.asymmetricKeySize, 128);
9496
assert.strictEqual(publicKey.symmetricKeySize, undefined);
9597

9698
const privateKey = createPrivateKey(privatePem);
9799
assert.strictEqual(privateKey.type, 'private');
98100
assert.strictEqual(privateKey.asymmetricKeyType, 'rsa');
101+
assert.strictEqual(privateKey.asymmetricKeySize, 128);
99102
assert.strictEqual(privateKey.symmetricKeySize, undefined);
100103

101104
// It should be possible to derive a public key from a private key.

test/parallel/test-crypto-keygen.js

+4
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
108108
assert.strictEqual(typeof publicKey, 'object');
109109
assert.strictEqual(publicKey.type, 'public');
110110
assert.strictEqual(publicKey.asymmetricKeyType, 'rsa');
111+
assert.strictEqual(publicKey.asymmetricKeySize, 64);
111112

112113
assert.strictEqual(typeof privateKey, 'object');
113114
assert.strictEqual(privateKey.type, 'private');
114115
assert.strictEqual(privateKey.asymmetricKeyType, 'rsa');
116+
assert.strictEqual(publicKey.asymmetricKeySize, 64);
115117
}
116118

117119
{
@@ -453,6 +455,7 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
453455
assert.strictEqual(typeof publicKey, 'object');
454456
assert.strictEqual(publicKey.type, 'public');
455457
assert.strictEqual(publicKey.asymmetricKeyType, 'rsa');
458+
assert.strictEqual(publicKey.asymmetricKeySize, 128);
456459

457460
// The private key should still be a string.
458461
assert.strictEqual(typeof privateKey, 'string');
@@ -477,6 +480,7 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
477480
assert.strictEqual(typeof privateKey, 'object');
478481
assert.strictEqual(privateKey.type, 'private');
479482
assert.strictEqual(privateKey.asymmetricKeyType, 'rsa');
483+
assert.strictEqual(privateKey.asymmetricKeySize, 128);
480484

481485
testEncryptDecrypt(publicKey, privateKey);
482486
testSignVerify(publicKey, privateKey);

0 commit comments

Comments
 (0)