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: KeyObject.asymmetricKeySize API #26387

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
@@ -1121,6 +1121,15 @@ exposes different functions.
Most applications should consider using the new `KeyObject` API instead of
passing keys as strings or `Buffer`s due to improved security features.

### keyObject.asymmetricKeySize
<!-- YAML
added: REPLACEME
-->
* {number}

For asymmetric keys, this property represents the size of the embedded key in
bytes. This property is `undefined` for symmetric keys.

### keyObject.asymmetricKeyType
<!-- YAML
added: v11.6.0
6 changes: 6 additions & 0 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
@@ -67,9 +67,15 @@ class SecretKeyObject extends KeyObject {
}
}

const kAsymmetricKeySize = Symbol('kAsymmetricKeySize');
const kAsymmetricKeyType = Symbol('kAsymmetricKeyType');

class AsymmetricKeyObject extends KeyObject {
get asymmetricKeySize() {
return this[kAsymmetricKeySize] ||
(this[kAsymmetricKeySize] = this[kHandle].getAsymmetricKeySize());
}

get asymmetricKeyType() {
return this[kAsymmetricKeyType] ||
(this[kAsymmetricKeyType] = this[kHandle].getAsymmetricKeyType());
13 changes: 13 additions & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
@@ -3330,6 +3330,8 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);

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

size_t KeyObject::GetAsymmetricKeySize() const {
CHECK_NE(key_type_, kKeyTypeSecret);
return EVP_PKEY_size(this->asymmetric_key_.get());
}

size_t KeyObject::GetSymmetricKeySize() const {
CHECK_EQ(key_type_, kKeyTypeSecret);
return this->symmetric_key_len_;
@@ -3474,6 +3481,12 @@ void KeyObject::GetAsymmetricKeyType(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(key->GetAsymmetricKeyType());
}

void KeyObject::GetAsymmetricKeySize(const FunctionCallbackInfo<Value>& args) {
KeyObject* key;
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());
args.GetReturnValue().Set(static_cast<uint32_t>(key->GetAsymmetricKeySize()));
}

void KeyObject::GetSymmetricKeySize(const FunctionCallbackInfo<Value>& args) {
KeyObject* key;
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());
4 changes: 4 additions & 0 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
@@ -456,6 +456,7 @@ class KeyObject : public BaseObject {
// only be used to implement cryptograohic operations requiring the key.
ManagedEVPPKey GetAsymmetricKey() const;
const char* GetSymmetricKey() const;
size_t GetAsymmetricKeySize() const;
size_t GetSymmetricKeySize() const;

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

static void GetAsymmetricKeySize(
const v8::FunctionCallbackInfo<v8::Value>& args);

static void GetSymmetricKeySize(
const v8::FunctionCallbackInfo<v8::Value>& args);

3 changes: 3 additions & 0 deletions test/parallel/test-crypto-key-objects.js
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
const key = createSecretKey(keybuf);
assert.strictEqual(key.type, 'secret');
assert.strictEqual(key.symmetricKeySize, 32);
assert.strictEqual(key.asymmetricKeySize, undefined);
assert.strictEqual(key.asymmetricKeyType, undefined);

const exportedKey = key.export();
@@ -73,11 +74,13 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
const publicKey = createPublicKey(publicPem);
assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(publicKey.asymmetricKeyType, 'rsa');
assert.strictEqual(publicKey.asymmetricKeySize, 128);
assert.strictEqual(publicKey.symmetricKeySize, undefined);

const privateKey = createPrivateKey(privatePem);
assert.strictEqual(privateKey.type, 'private');
assert.strictEqual(privateKey.asymmetricKeyType, 'rsa');
assert.strictEqual(privateKey.asymmetricKeySize, 128);
assert.strictEqual(privateKey.symmetricKeySize, undefined);

const publicDER = publicKey.export({
4 changes: 4 additions & 0 deletions test/parallel/test-crypto-keygen.js
Original file line number Diff line number Diff line change
@@ -108,10 +108,12 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
assert.strictEqual(typeof publicKey, 'object');
assert.strictEqual(publicKey.type, 'public');
assert.strictEqual(publicKey.asymmetricKeyType, 'rsa');
assert.strictEqual(publicKey.asymmetricKeySize, 64);

assert.strictEqual(typeof privateKey, 'object');
assert.strictEqual(privateKey.type, 'private');
assert.strictEqual(privateKey.asymmetricKeyType, 'rsa');
assert.strictEqual(publicKey.asymmetricKeySize, 64);
}

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

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

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