-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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: ECDH convertKey to convert public keys between different formats #19080
Changes from all commits
41a17db
9ded80c
e876f36
810cb37
17f1703
d5f07d9
c201c26
541a651
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 |
---|---|---|
|
@@ -4684,31 +4684,31 @@ void ECDH::GenerateKeys(const FunctionCallbackInfo<Value>& args) { | |
} | ||
|
||
|
||
EC_POINT* ECDH::BufferToPoint(char* data, size_t len) { | ||
EC_POINT* ECDH::BufferToPoint(Environment* env, | ||
const EC_GROUP* group, | ||
char* data, | ||
size_t len) { | ||
EC_POINT* pub; | ||
int r; | ||
|
||
pub = EC_POINT_new(group_); | ||
pub = EC_POINT_new(group); | ||
if (pub == nullptr) { | ||
env()->ThrowError("Failed to allocate EC_POINT for a public key"); | ||
env->ThrowError("Failed to allocate EC_POINT for a public key"); | ||
return nullptr; | ||
} | ||
|
||
r = EC_POINT_oct2point( | ||
group_, | ||
group, | ||
pub, | ||
reinterpret_cast<unsigned char*>(data), | ||
len, | ||
nullptr); | ||
if (!r) { | ||
goto fatal; | ||
EC_POINT_free(pub); | ||
return nullptr; | ||
} | ||
|
||
return pub; | ||
|
||
fatal: | ||
EC_POINT_free(pub); | ||
return nullptr; | ||
} | ||
|
||
|
||
|
@@ -4725,7 +4725,9 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) { | |
if (!ecdh->IsKeyPairValid()) | ||
return env->ThrowError("Invalid key pair"); | ||
|
||
EC_POINT* pub = ecdh->BufferToPoint(Buffer::Data(args[0]), | ||
EC_POINT* pub = ECDH::BufferToPoint(env, | ||
ecdh->group_, | ||
Buffer::Data(args[0]), | ||
Buffer::Length(args[0])); | ||
if (pub == nullptr) { | ||
args.GetReturnValue().Set( | ||
|
@@ -4874,7 +4876,9 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) { | |
|
||
MarkPopErrorOnReturn mark_pop_error_on_return; | ||
|
||
EC_POINT* pub = ecdh->BufferToPoint(Buffer::Data(args[0].As<Object>()), | ||
EC_POINT* pub = ECDH::BufferToPoint(env, | ||
ecdh->group_, | ||
Buffer::Data(args[0].As<Object>()), | ||
Buffer::Length(args[0].As<Object>())); | ||
if (pub == nullptr) | ||
return env->ThrowError("Failed to convert Buffer to EC_POINT"); | ||
|
@@ -5550,6 +5554,61 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) { | |
args.GetReturnValue().Set(outString); | ||
} | ||
|
||
|
||
// Convert the input public key to compressed, uncompressed, or hybrid formats. | ||
void ConvertKey(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
CHECK_EQ(args.Length(), 3); | ||
|
||
size_t len = Buffer::Length(args[0]); | ||
if (len == 0) | ||
return args.GetReturnValue().SetEmptyString(); | ||
|
||
node::Utf8Value curve(env->isolate(), args[1]); | ||
|
||
int nid = OBJ_sn2nid(*curve); | ||
if (nid == NID_undef) | ||
return env->ThrowTypeError("Invalid ECDH curve name"); | ||
|
||
EC_GROUP* group = EC_GROUP_new_by_curve_name(nid); | ||
if (group == nullptr) | ||
return env->ThrowError("Failed to get EC_GROUP"); | ||
|
||
EC_POINT* pub = ECDH::BufferToPoint(env, | ||
group, | ||
Buffer::Data(args[0]), | ||
len); | ||
|
||
std::shared_ptr<void> cleanup(nullptr, [group, pub] (...) { | ||
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. @bnoordhuis I am not sure if I am using 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. @wuweiweiwu we also have 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. @addaleax I will look at that! Is there a preference using 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. @wuweiweiwu The former is pretty new as an utility -- but basically, use whichever makes sense to you, or lets the code seem more readable. :) (Also, probably doesn't matter much now that this has landed, unless you want to send another PR right behind this. ¯\_(ツ)_/¯) 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. Sounds good! I'll go over both so I am more familiar |
||
EC_GROUP_free(group); | ||
EC_POINT_free(pub); | ||
}); | ||
|
||
if (pub == nullptr) | ||
return env->ThrowError("Failed to convert Buffer to EC_POINT"); | ||
|
||
point_conversion_form_t form = | ||
static_cast<point_conversion_form_t>(args[2]->Uint32Value()); | ||
|
||
int size = EC_POINT_point2oct(group, pub, form, nullptr, 0, nullptr); | ||
if (size == 0) | ||
return env->ThrowError("Failed to get public key length"); | ||
|
||
unsigned char* out = node::Malloc<unsigned char>(size); | ||
|
||
int r = EC_POINT_point2oct(group, pub, form, out, size, nullptr); | ||
if (r != size) { | ||
free(out); | ||
return env->ThrowError("Failed to get public key"); | ||
} | ||
|
||
Local<Object> buf = | ||
Buffer::New(env, reinterpret_cast<char*>(out), size).ToLocalChecked(); | ||
args.GetReturnValue().Set(buf); | ||
} | ||
|
||
|
||
void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) { | ||
CHECK(Buffer::HasInstance(args[0])); | ||
CHECK(Buffer::HasInstance(args[1])); | ||
|
@@ -5692,6 +5751,8 @@ void InitCrypto(Local<Object> target, | |
env->SetMethod(target, "certVerifySpkac", VerifySpkac); | ||
env->SetMethod(target, "certExportPublicKey", ExportPublicKey); | ||
env->SetMethod(target, "certExportChallenge", ExportChallenge); | ||
|
||
env->SetMethod(target, "ECDHConvertKey", ConvertKey); | ||
#ifndef OPENSSL_NO_ENGINE | ||
env->SetMethod(target, "setEngine", SetEngine); | ||
#endif // !OPENSSL_NO_ENGINE | ||
|
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.
Since you're making changes here, you might want to get rid of the
goto
by moving thefatal:
block inside the if statement.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.
Done!