@@ -1613,6 +1613,24 @@ static void AddFingerprintDigest(const unsigned char* md,
1613
1613
}
1614
1614
}
1615
1615
1616
+ static MaybeLocal<Object> ECPointToBuffer (Environment* env,
1617
+ const EC_GROUP* group,
1618
+ const EC_POINT* point,
1619
+ point_conversion_form_t form) {
1620
+ size_t len = EC_POINT_point2oct (group, point, form, nullptr , 0 , nullptr );
1621
+ if (len == 0 ) {
1622
+ env->ThrowError (" Failed to get public key length" );
1623
+ return MaybeLocal<Object>();
1624
+ }
1625
+ MallocedBuffer<unsigned char > buf (len);
1626
+ len = EC_POINT_point2oct (group, point, form, buf.data , buf.size , nullptr );
1627
+ if (len == 0 ) {
1628
+ env->ThrowError (" Failed to get public key" );
1629
+ return MaybeLocal<Object>();
1630
+ }
1631
+ return Buffer::New (env, buf.release (), len);
1632
+ }
1633
+
1616
1634
static Local<Object> X509ToObject (Environment* env, X509* cert) {
1617
1635
EscapableHandleScope scope (env->isolate ());
1618
1636
Local<Context> context = env->context ();
@@ -1729,16 +1747,12 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
1729
1747
}
1730
1748
}
1731
1749
1732
- unsigned char * pub = nullptr ;
1733
- size_t publen = EC_KEY_key2buf (ec.get (), EC_KEY_get_conv_form (ec.get ()),
1734
- &pub, nullptr );
1735
- if (publen > 0 ) {
1736
- Local<Object> buf = Buffer::New (env, pub, publen).ToLocalChecked ();
1737
- // Ownership of pub pointer accepted by Buffer.
1738
- pub = nullptr ;
1750
+ const EC_POINT* pubkey = EC_KEY_get0_public_key (ec.get ());
1751
+ if (pubkey != nullptr ) {
1752
+ Local<Object> buf =
1753
+ ECPointToBuffer (env, group, pubkey, EC_KEY_get_conv_form (ec.get ()))
1754
+ .ToLocalChecked ();
1739
1755
info->Set (context, env->pubkey_string (), buf).FromJust ();
1740
- } else {
1741
- CHECK_NULL (pub);
1742
1756
}
1743
1757
1744
1758
const int nid = EC_GROUP_get_curve_name (group);
@@ -5238,26 +5252,14 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
5238
5252
if (pub == nullptr )
5239
5253
return env->ThrowError (" Failed to get ECDH public key" );
5240
5254
5241
- int size;
5242
5255
CHECK (args[0 ]->IsUint32 ());
5243
5256
uint32_t val = args[0 ].As <Uint32>()->Value ();
5244
5257
point_conversion_form_t form = static_cast <point_conversion_form_t >(val);
5245
5258
5246
- size = EC_POINT_point2oct (ecdh->group_ , pub, form, nullptr , 0 , nullptr );
5247
- if (size == 0 )
5248
- return env->ThrowError (" Failed to get public key length" );
5249
-
5250
- unsigned char * out = node::Malloc<unsigned char >(size);
5251
-
5252
- int r = EC_POINT_point2oct (ecdh->group_ , pub, form, out, size, nullptr );
5253
- if (r != size) {
5254
- free (out);
5255
- return env->ThrowError (" Failed to get public key" );
5256
- }
5257
-
5258
- Local<Object> buf =
5259
- Buffer::New (env, reinterpret_cast <char *>(out), size).ToLocalChecked ();
5260
- args.GetReturnValue ().Set (buf);
5259
+ MaybeLocal<Object> buf =
5260
+ ECPointToBuffer (env, EC_KEY_get0_group (ecdh->key_ .get ()), pub, form);
5261
+ if (buf.IsEmpty ()) return ;
5262
+ args.GetReturnValue ().Set (buf.ToLocalChecked ());
5261
5263
}
5262
5264
5263
5265
@@ -6145,23 +6147,9 @@ void ConvertKey(const FunctionCallbackInfo<Value>& args) {
6145
6147
uint32_t val = args[2 ].As <Uint32>()->Value ();
6146
6148
point_conversion_form_t form = static_cast <point_conversion_form_t >(val);
6147
6149
6148
- int size = EC_POINT_point2oct (
6149
- group.get (), pub.get (), form, nullptr , 0 , nullptr );
6150
-
6151
- if (size == 0 )
6152
- return env->ThrowError (" Failed to get public key length" );
6153
-
6154
- unsigned char * out = node::Malloc<unsigned char >(size);
6155
-
6156
- int r = EC_POINT_point2oct (group.get (), pub.get (), form, out, size, nullptr );
6157
- if (r != size) {
6158
- free (out);
6159
- return env->ThrowError (" Failed to get public key" );
6160
- }
6161
-
6162
- Local<Object> buf =
6163
- Buffer::New (env, reinterpret_cast <char *>(out), size).ToLocalChecked ();
6164
- args.GetReturnValue ().Set (buf);
6150
+ MaybeLocal<Object> buf = ECPointToBuffer (env, group.get (), pub.get (), form);
6151
+ if (buf.IsEmpty ()) return ;
6152
+ args.GetReturnValue ().Set (buf.ToLocalChecked ());
6165
6153
}
6166
6154
6167
6155
0 commit comments