@@ -1628,6 +1628,24 @@ static void AddFingerprintDigest(const unsigned char* md,
1628
1628
}
1629
1629
}
1630
1630
1631
+ static MaybeLocal<Object> ECPointToBuffer (Environment* env,
1632
+ const EC_GROUP* group,
1633
+ const EC_POINT* point,
1634
+ point_conversion_form_t form) {
1635
+ size_t len = EC_POINT_point2oct (group, point, form, nullptr , 0 , nullptr );
1636
+ if (len == 0 ) {
1637
+ env->ThrowError (" Failed to get public key length" );
1638
+ return MaybeLocal<Object>();
1639
+ }
1640
+ MallocedBuffer<unsigned char > buf (len);
1641
+ len = EC_POINT_point2oct (group, point, form, buf.data , buf.size , nullptr );
1642
+ if (len == 0 ) {
1643
+ env->ThrowError (" Failed to get public key" );
1644
+ return MaybeLocal<Object>();
1645
+ }
1646
+ return Buffer::New (env, buf.release (), len);
1647
+ }
1648
+
1631
1649
static Local<Object> X509ToObject (Environment* env, X509* cert) {
1632
1650
EscapableHandleScope scope (env->isolate ());
1633
1651
Local<Context> context = env->context ();
@@ -1744,16 +1762,12 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
1744
1762
}
1745
1763
}
1746
1764
1747
- unsigned char * pub = nullptr ;
1748
- size_t publen = EC_KEY_key2buf (ec.get (), EC_KEY_get_conv_form (ec.get ()),
1749
- &pub, nullptr );
1750
- if (publen > 0 ) {
1751
- Local<Object> buf = Buffer::New (env, pub, publen).ToLocalChecked ();
1752
- // Ownership of pub pointer accepted by Buffer.
1753
- pub = nullptr ;
1765
+ const EC_POINT* pubkey = EC_KEY_get0_public_key (ec.get ());
1766
+ if (pubkey != nullptr ) {
1767
+ Local<Object> buf =
1768
+ ECPointToBuffer (env, group, pubkey, EC_KEY_get_conv_form (ec.get ()))
1769
+ .ToLocalChecked ();
1754
1770
info->Set (context, env->pubkey_string (), buf).FromJust ();
1755
- } else {
1756
- CHECK_NULL (pub);
1757
1771
}
1758
1772
1759
1773
const int nid = EC_GROUP_get_curve_name (group);
@@ -5256,26 +5270,14 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
5256
5270
if (pub == nullptr )
5257
5271
return env->ThrowError (" Failed to get ECDH public key" );
5258
5272
5259
- int size;
5260
5273
CHECK (args[0 ]->IsUint32 ());
5261
5274
uint32_t val = args[0 ].As <Uint32>()->Value ();
5262
5275
point_conversion_form_t form = static_cast <point_conversion_form_t >(val);
5263
5276
5264
- size = EC_POINT_point2oct (ecdh->group_ , pub, form, nullptr , 0 , nullptr );
5265
- if (size == 0 )
5266
- return env->ThrowError (" Failed to get public key length" );
5267
-
5268
- unsigned char * out = node::Malloc<unsigned char >(size);
5269
-
5270
- int r = EC_POINT_point2oct (ecdh->group_ , pub, form, out, size, nullptr );
5271
- if (r != size) {
5272
- free (out);
5273
- return env->ThrowError (" Failed to get public key" );
5274
- }
5275
-
5276
- Local<Object> buf =
5277
- Buffer::New (env, reinterpret_cast <char *>(out), size).ToLocalChecked ();
5278
- args.GetReturnValue ().Set (buf);
5277
+ MaybeLocal<Object> buf =
5278
+ ECPointToBuffer (env, EC_KEY_get0_group (ecdh->key_ .get ()), pub, form);
5279
+ if (buf.IsEmpty ()) return ;
5280
+ args.GetReturnValue ().Set (buf.ToLocalChecked ());
5279
5281
}
5280
5282
5281
5283
@@ -6165,23 +6167,9 @@ void ConvertKey(const FunctionCallbackInfo<Value>& args) {
6165
6167
uint32_t val = args[2 ].As <Uint32>()->Value ();
6166
6168
point_conversion_form_t form = static_cast <point_conversion_form_t >(val);
6167
6169
6168
- int size = EC_POINT_point2oct (
6169
- group.get (), pub.get (), form, nullptr , 0 , nullptr );
6170
-
6171
- if (size == 0 )
6172
- return env->ThrowError (" Failed to get public key length" );
6173
-
6174
- unsigned char * out = node::Malloc<unsigned char >(size);
6175
-
6176
- int r = EC_POINT_point2oct (group.get (), pub.get (), form, out, size, nullptr );
6177
- if (r != size) {
6178
- free (out);
6179
- return env->ThrowError (" Failed to get public key" );
6180
- }
6181
-
6182
- Local<Object> buf =
6183
- Buffer::New (env, reinterpret_cast <char *>(out), size).ToLocalChecked ();
6184
- args.GetReturnValue ().Set (buf);
6170
+ MaybeLocal<Object> buf = ECPointToBuffer (env, group.get (), pub.get (), form);
6171
+ if (buf.IsEmpty ()) return ;
6172
+ args.GetReturnValue ().Set (buf.ToLocalChecked ());
6185
6173
}
6186
6174
6187
6175
0 commit comments