Skip to content

Commit 09c83d8

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: nodejs#26387 Refs: nodejs#24234 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent ff1958d commit 09c83d8

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed

doc/api/crypto.md

Lines changed: 9 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ class SecretKeyObject extends KeyObject {
6767
}
6868
}
6969

70+
const kAsymmetricKeySize = Symbol('kAsymmetricKeySize');
7071
const kAsymmetricKeyType = Symbol('kAsymmetricKeyType');
7172

7273
class AsymmetricKeyObject extends KeyObject {
74+
get asymmetricKeySize() {
75+
return this[kAsymmetricKeySize] ||
76+
(this[kAsymmetricKeySize] = this[kHandle].getAsymmetricKeySize());
77+
}
78+
7379
get asymmetricKeyType() {
7480
return this[kAsymmetricKeyType] ||
7581
(this[kAsymmetricKeyType] = this[kHandle].getAsymmetricKeyType());

src/node_crypto.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,8 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
33033303
t->InstanceTemplate()->SetInternalFieldCount(1);
33043304

33053305
env->SetProtoMethod(t, "init", Init);
3306+
env->SetProtoMethodNoSideEffect(t, "getAsymmetricKeySize",
3307+
GetAsymmetricKeySize);
33063308
env->SetProtoMethodNoSideEffect(t, "getSymmetricKeySize",
33073309
GetSymmetricKeySize);
33083310
env->SetProtoMethodNoSideEffect(t, "getAsymmetricKeyType",
@@ -3348,6 +3350,11 @@ const char* KeyObject::GetSymmetricKey() const {
33483350
return this->symmetric_key_.get();
33493351
}
33503352

3353+
size_t KeyObject::GetAsymmetricKeySize() const {
3354+
CHECK_NE(key_type_, kKeyTypeSecret);
3355+
return EVP_PKEY_size(this->asymmetric_key_.get());
3356+
}
3357+
33513358
size_t KeyObject::GetSymmetricKeySize() const {
33523359
CHECK_EQ(key_type_, kKeyTypeSecret);
33533360
return this->symmetric_key_len_;
@@ -3447,6 +3454,12 @@ void KeyObject::GetAsymmetricKeyType(const FunctionCallbackInfo<Value>& args) {
34473454
args.GetReturnValue().Set(key->GetAsymmetricKeyType());
34483455
}
34493456

3457+
void KeyObject::GetAsymmetricKeySize(const FunctionCallbackInfo<Value>& args) {
3458+
KeyObject* key;
3459+
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());
3460+
args.GetReturnValue().Set(static_cast<uint32_t>(key->GetAsymmetricKeySize()));
3461+
}
3462+
34503463
void KeyObject::GetSymmetricKeySize(const FunctionCallbackInfo<Value>& args) {
34513464
KeyObject* key;
34523465
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());

src/node_crypto.h

Lines changed: 4 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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();
@@ -73,11 +74,13 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
7374
const publicKey = createPublicKey(publicPem);
7475
assert.strictEqual(publicKey.type, 'public');
7576
assert.strictEqual(publicKey.asymmetricKeyType, 'rsa');
77+
assert.strictEqual(publicKey.asymmetricKeySize, 128);
7678
assert.strictEqual(publicKey.symmetricKeySize, undefined);
7779

7880
const privateKey = createPrivateKey(privatePem);
7981
assert.strictEqual(privateKey.type, 'private');
8082
assert.strictEqual(privateKey.asymmetricKeyType, 'rsa');
83+
assert.strictEqual(privateKey.asymmetricKeySize, 128);
8184
assert.strictEqual(privateKey.symmetricKeySize, undefined);
8285

8386
const publicDER = publicKey.export({

test/parallel/test-crypto-keygen.js

Lines changed: 4 additions & 0 deletions
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)