-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: add keyObject.params for asymmetric keys #30045
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
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()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, but could you rewrite this as:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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,
get asymmetricKeyType()
uses the same style.