-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
crypto: add keyObject.params for asymmetric keys #30045
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,12 +76,18 @@ class SecretKeyObject extends KeyObject { | |
| } | ||
|
|
||
| const kAsymmetricKeyType = Symbol('kAsymmetricKeyType'); | ||
| const kParams = Symbol('kParams'); | ||
|
|
||
| class AsymmetricKeyObject extends KeyObject { | ||
| get asymmetricKeyType() { | ||
| return this[kAsymmetricKeyType] || | ||
| (this[kAsymmetricKeyType] = this[kHandle].getAsymmetricKeyType()); | ||
| } | ||
|
|
||
| get params() { | ||
| return this[kParams] || | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, but could you rewrite this as: if (this[kParams] === undefined)
this[kParams] = this[kHandle].getAsymmetricParams();
return this[kParams];
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to suggest something similar at first but it's locally consistent, |
||
| (this[kParams] = this[kHandle].getAsymmetricParams()); | ||
| } | ||
| } | ||
|
|
||
| class PublicKeyObject extends AsymmetricKeyObject { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3761,6 +3761,8 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) { | |
| GetSymmetricKeySize); | ||
| env->SetProtoMethodNoSideEffect(t, "getAsymmetricKeyType", | ||
| GetAsymmetricKeyType); | ||
| env->SetProtoMethodNoSideEffect(t, "getAsymmetricParams", | ||
| GetAsymmetricParams); | ||
| env->SetProtoMethod(t, "export", Export); | ||
|
|
||
| auto function = t->GetFunction(env->context()).ToLocalChecked(); | ||
|
|
@@ -3912,6 +3914,72 @@ void KeyObject::GetAsymmetricKeyType(const FunctionCallbackInfo<Value>& args) { | |
| args.GetReturnValue().Set(key->GetAsymmetricKeyType()); | ||
| } | ||
|
|
||
| static inline void SetParam(Environment* env, Local<Object> params, | ||
| const char* key, Local<Value> value) { | ||
| bool ok = params->DefineOwnProperty( | ||
| env->context(), | ||
| String::NewFromUtf8(env->isolate(), key, NewStringType::kNormal) | ||
| .ToLocalChecked(), | ||
| value, | ||
| PropertyAttribute::ReadOnly).ToChecked(); | ||
| CHECK(ok); | ||
| } | ||
|
|
||
| Local<Value> KeyObject::GetAsymmetricParams() const { | ||
| CHECK_NE(key_type_, kKeyTypeSecret); | ||
|
|
||
| Local<Object> params = Object::New(env()->isolate()); | ||
| EVP_PKEY* pkey = asymmetric_key_.get(); | ||
| RSA* rsa; | ||
| DSA* dsa; | ||
| EC_KEY* ec; | ||
| const EC_GROUP* ec_group; | ||
| int ec_curve_id; | ||
| const char* ec_curve_name; | ||
|
|
||
| switch (EVP_PKEY_id(pkey)) { | ||
| case EVP_PKEY_RSA_PSS: | ||
| // TODO(tniessen): Also retrieve additional PSS params as soon as OpenSSL | ||
| // provides a way to do that. | ||
| case EVP_PKEY_RSA: | ||
| // TODO(tniessen): This should really be EVP_PKEY_get0_RSA, but OpenSSL does | ||
| // not support that for RSA-PSS. | ||
| rsa = reinterpret_cast<RSA*>(EVP_PKEY_get0(pkey)); | ||
| SetParam(env(), params, "modulusLength", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: add the string to |
||
| Integer::New(env()->isolate(), RSA_bits(rsa))); | ||
| // TODO(tniessen): Add publicExponent. | ||
| break; | ||
| case EVP_PKEY_DSA: | ||
| dsa = EVP_PKEY_get0_DSA(pkey); | ||
| SetParam(env(), params, "modulusLength", | ||
| Integer::New(env()->isolate(), DSA_bits(dsa))); | ||
| SetParam(env(), params, "divisorLength", | ||
| Integer::New(env()->isolate(), BN_num_bits(DSA_get0_q(dsa)))); | ||
| break; | ||
| case EVP_PKEY_EC: | ||
| ec = EVP_PKEY_get0_EC_KEY(pkey); | ||
| ec_group = EC_KEY_get0_group(ec); | ||
| ec_curve_id = EC_GROUP_get_curve_name(ec_group); | ||
| if (ec_curve_id != NID_undef) { | ||
| ec_curve_name = OBJ_nid2sn(ec_curve_id); | ||
| if (ec_curve_name != nullptr) { | ||
| SetParam(env(), params, "namedCurve", | ||
| String::NewFromUtf8(env()->isolate(), ec_curve_name, | ||
| NewStringType::kNormal).ToLocalChecked()); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| return params; | ||
| } | ||
tniessen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| void KeyObject::GetAsymmetricParams(const FunctionCallbackInfo<Value>& args) { | ||
| KeyObject* key; | ||
| ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder()); | ||
|
|
||
| args.GetReturnValue().Set(key->GetAsymmetricParams()); | ||
| } | ||
|
|
||
| void KeyObject::GetSymmetricKeySize(const FunctionCallbackInfo<Value>& args) { | ||
| KeyObject* key; | ||
| ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder()); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.