@@ -387,21 +387,24 @@ bool KeyObjectData::ToEncodedPublicKey(
387387 Mutex::ScopedLock lock (mutex ());
388388 const auto & pkey = GetAsymmetricKey ();
389389 if (pkey.id () == EVP_PKEY_EC ) {
390+ auto form = static_cast <point_conversion_form_t >(config.ec_point_form );
391+ auto bytes = ncrypto::Ec::TryExportPublic (pkey, form);
392+ if (bytes)
393+ return Buffer::Copy (env, bytes.get <const char >(), bytes.size ())
394+ .ToLocal (out);
390395 ECKeyPointer ec_key (pkey);
391396 if (!ec_key) {
392397 THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS (env);
393398 return false ;
394399 }
395- // A provider-backed key need not expose its public point.
396400 if (ec_key.getPublicKey () == nullptr ) {
397401 THROW_ERR_CRYPTO_OPERATION_FAILED (env,
398402 " Failed to export EC public key" );
399403 return false ;
400404 }
401- auto form = static_cast <point_conversion_form_t >(config.ec_point_form );
402- const auto group = ec_key.getGroup ();
403- const auto point = ec_key.getPublicKey ();
404- return ECPointToBuffer (env, group, point, form).ToLocal (out);
405+ return ECPointToBuffer (
406+ env, ec_key.getGroup (), ec_key.getPublicKey (), form)
407+ .ToLocal (out);
405408 }
406409 const int id = pkey.id ();
407410 bool is_raw_supported = id == EVP_PKEY_ED25519 || id == EVP_PKEY_ED448 ||
@@ -442,25 +445,7 @@ bool KeyObjectData::ToEncodedPrivateKey(
442445 Mutex::ScopedLock lock (mutex ());
443446 const auto & pkey = GetAsymmetricKey ();
444447 if (pkey.id () == EVP_PKEY_EC ) {
445- ECKeyPointer ec_key (pkey);
446- if (!ec_key) {
447- THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS (env);
448- return false ;
449- }
450- const BIGNUM * private_key = ec_key.getPrivateKey ();
451- if (private_key == nullptr ) {
452- THROW_ERR_CRYPTO_OPERATION_FAILED (env,
453- " Failed to export EC private key" );
454- return false ;
455- }
456- const auto group = ec_key.getGroup ();
457- auto order = BignumPointer::New ();
458- if (!order || !EC_GROUP_get_order (group, order.get (), nullptr )) {
459- THROW_ERR_CRYPTO_OPERATION_FAILED (env,
460- " Failed to export EC private key" );
461- return false ;
462- }
463- auto buf = BignumPointer::EncodePadded (private_key, order.byteLength ());
448+ auto buf = ncrypto::Ec::ExportPrivate (pkey);
464449 if (!buf) {
465450 THROW_ERR_CRYPTO_OPERATION_FAILED (env,
466451 " Failed to export EC private key" );
@@ -1582,24 +1567,27 @@ void KeyObjectHandle::ExportECPublicRaw(
15821567 return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS (env);
15831568 }
15841569
1570+ CHECK (args[0 ]->IsInt32 ());
1571+ auto form =
1572+ static_cast <point_conversion_form_t >(args[0 ].As <Int32>()->Value ());
1573+
1574+ auto bytes = ncrypto::Ec::TryExportPublic (m_pkey, form);
1575+ if (bytes) {
1576+ args.GetReturnValue ().Set (
1577+ Buffer::Copy (env, bytes.get <const char >(), bytes.size ())
1578+ .FromMaybe (Local<Value>()));
1579+ return ;
1580+ }
15851581 ECKeyPointer ec_key (m_pkey);
15861582 if (!ec_key) return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS (env);
1587- // A provider-backed key need not expose its public point.
15881583 if (ec_key.getPublicKey () == nullptr ) {
15891584 return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
15901585 " Failed to export EC public key" );
15911586 }
1592-
1593- CHECK (args[0 ]->IsInt32 ());
1594- auto form =
1595- static_cast <point_conversion_form_t >(args[0 ].As <Int32>()->Value ());
1596-
1597- const auto group = ec_key.getGroup ();
1598- const auto point = ec_key.getPublicKey ();
1599-
16001587 Local<Object> buf;
1601- if (!ECPointToBuffer (env, group, point, form).ToLocal (&buf)) return ;
1602-
1588+ if (!ECPointToBuffer (env, ec_key.getGroup (), ec_key.getPublicKey (), form)
1589+ .ToLocal (&buf))
1590+ return ;
16031591 args.GetReturnValue ().Set (buf);
16041592}
16051593
@@ -1618,23 +1606,7 @@ void KeyObjectHandle::ExportECPrivateRaw(
16181606 return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS (env);
16191607 }
16201608
1621- ECKeyPointer ec_key (m_pkey);
1622- if (!ec_key) return THROW_ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS (env);
1623-
1624- const BIGNUM * private_key = ec_key.getPrivateKey ();
1625- if (private_key == nullptr ) {
1626- return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
1627- " Failed to export EC private key" );
1628- }
1629-
1630- const auto group = ec_key.getGroup ();
1631- auto order = BignumPointer::New ();
1632- if (!order || !EC_GROUP_get_order (group, order.get (), nullptr )) {
1633- return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
1634- " Failed to export EC private key" );
1635- }
1636-
1637- auto buf = BignumPointer::EncodePadded (private_key, order.byteLength ());
1609+ auto buf = ncrypto::Ec::ExportPrivate (m_pkey);
16381610 if (!buf) {
16391611 return THROW_ERR_CRYPTO_OPERATION_FAILED (env,
16401612 " Failed to export EC private key" );
0 commit comments