mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Compare commits
4 Commits
claude/wat
...
don/refact
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c145687538 | ||
|
|
c0ee1cca93 | ||
|
|
ff5890dd60 | ||
|
|
553973b4b2 |
@@ -88,6 +88,55 @@ JSC_DECLARE_HOST_FUNCTION(KeyObject__privateDecrypt);
|
||||
|
||||
namespace WebCore {
|
||||
|
||||
/// Encodes `undefined` if `key` is `nullptr`.
|
||||
JSC::EncodedJSValue encodeKey(const std::optional<JSCryptoKey*> key)
|
||||
{
|
||||
return key.has_value() ? JSC::JSValue::encode(key.value()) : EncodedJSValue {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Numeric Identifier (NID) of an `EVP_PKEY` storing an Eclyptic
|
||||
* Curve (EC) key. On failure, throws a JS exception and returns `NID_undef`.
|
||||
*
|
||||
* * -1: not an EC key, or `key` is `nullptr`
|
||||
* * -2: unable to identify EC curve
|
||||
* * -3: key has an unknown curve
|
||||
*
|
||||
* @param globalObject global JS object. Used to throw exceptions.
|
||||
* @param key Eclicptic Curve key. May be public/private/whatever.
|
||||
* @return std::optional<int>
|
||||
*/
|
||||
int curveName(JSC::JSGlobalObject* globalObject, const EVP_PKEY* key)
|
||||
{
|
||||
ThrowScope scope = DECLARE_THROW_SCOPE(globalObject->vm());
|
||||
// NOTE: EVP_PKEY_get0_EC_KEY handles null pointers, returning null itself.
|
||||
// NOTE: needs to live longer than ec_group, which stores a reference to
|
||||
// data owned by this key.
|
||||
EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(key);
|
||||
if (UNLIKELY(ec_key == nullptr)) {
|
||||
|
||||
throwTypeError(globalObject, scope, "Invalid EC key"_s);
|
||||
return NID_undef;
|
||||
}
|
||||
|
||||
// note: borrowed data. do not free.
|
||||
const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key);
|
||||
if (UNLIKELY(ec_group == nullptr)) {
|
||||
EC_KEY_free(ec_key);
|
||||
throwTypeError(globalObject, scope, "Invalid EC key"_s);
|
||||
return NID_undef;
|
||||
}
|
||||
|
||||
int curve_name = EC_GROUP_get_curve_name(ec_group);
|
||||
EC_KEY_free(ec_key);
|
||||
if (curve_name == NID_undef) {
|
||||
throwTypeError(globalObject, scope, "Unable to identify EC curve"_s);
|
||||
return NID_undef;
|
||||
}
|
||||
|
||||
return curve_name;
|
||||
}
|
||||
|
||||
static bool KeyObject__IsASN1Sequence(const unsigned char* data, size_t size,
|
||||
size_t* data_offset, size_t* data_size)
|
||||
{
|
||||
@@ -521,19 +570,9 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPrivateKey, (JSC::JSGlobalObject * glo
|
||||
auto impl = result.releaseNonNull();
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else if (pKeyID == EVP_PKEY_EC) {
|
||||
EC_KEY* ec_key = EVP_PKEY_get1_EC_KEY(pkey.get());
|
||||
if (UNLIKELY(ec_key == nullptr)) {
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Invalid EC private key"_s));
|
||||
return {};
|
||||
}
|
||||
const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key);
|
||||
// Get the curve name
|
||||
int curve_name = EC_GROUP_get_curve_name(ec_group);
|
||||
if (curve_name == NID_undef) {
|
||||
EC_KEY_free(ec_key);
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unable to identify EC curve"_s));
|
||||
return {};
|
||||
}
|
||||
int curve_name = curveName(globalObject, pkey.get());
|
||||
if (curve_name == NID_undef) return {}; // exception already thrown
|
||||
|
||||
CryptoKeyEC::NamedCurve curve;
|
||||
if (curve_name == NID_X9_62_prime256v1)
|
||||
curve = CryptoKeyEC::NamedCurve::P256;
|
||||
@@ -542,11 +581,9 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPrivateKey, (JSC::JSGlobalObject * glo
|
||||
else if (curve_name == NID_secp521r1)
|
||||
curve = CryptoKeyEC::NamedCurve::P521;
|
||||
else {
|
||||
EC_KEY_free(ec_key);
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unsupported EC curve"_s));
|
||||
return {};
|
||||
}
|
||||
EC_KEY_free(ec_key);
|
||||
auto impl = CryptoKeyEC::create(CryptoAlgorithmIdentifier::ECDH, curve, CryptoKeyType::Private, WTFMove(pkey), true, CryptoKeyUsageSign);
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else {
|
||||
@@ -620,19 +657,9 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPrivateKey, (JSC::JSGlobalObject * glo
|
||||
auto impl = result.releaseNonNull();
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else if (pKeyID == EVP_PKEY_EC) {
|
||||
EC_KEY* ec_key = EVP_PKEY_get1_EC_KEY(pkey.get());
|
||||
if (UNLIKELY(ec_key == nullptr)) {
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Invalid EC private key"_s));
|
||||
return {};
|
||||
}
|
||||
const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key);
|
||||
// Get the curve name
|
||||
int curve_name = EC_GROUP_get_curve_name(ec_group);
|
||||
if (curve_name == NID_undef) {
|
||||
EC_KEY_free(ec_key);
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unable to identify EC curve"_s));
|
||||
return {};
|
||||
}
|
||||
int curve_name = curveName(globalObject, pkey.get());
|
||||
if (curve_name == NID_undef) return {}; // exception already thrown
|
||||
|
||||
CryptoKeyEC::NamedCurve curve;
|
||||
if (curve_name == NID_X9_62_prime256v1)
|
||||
curve = CryptoKeyEC::NamedCurve::P256;
|
||||
@@ -641,7 +668,6 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPrivateKey, (JSC::JSGlobalObject * glo
|
||||
else if (curve_name == NID_secp521r1)
|
||||
curve = CryptoKeyEC::NamedCurve::P521;
|
||||
else {
|
||||
EC_KEY_free(ec_key);
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unsupported EC curve"_s));
|
||||
return {};
|
||||
}
|
||||
@@ -649,7 +675,6 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPrivateKey, (JSC::JSGlobalObject * glo
|
||||
if (UNLIKELY(result == nullptr)) {
|
||||
result = CryptoKeyEC::platformImportPkcs8(CryptoAlgorithmIdentifier::ECDSA, curve, Vector<uint8_t>(std::span { (uint8_t*)data, byteLength }), true, CryptoKeyUsageSign);
|
||||
}
|
||||
EC_KEY_free(ec_key);
|
||||
if (UNLIKELY(result == nullptr)) {
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Invalid EC private key"_s));
|
||||
return {};
|
||||
@@ -666,19 +691,9 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPrivateKey, (JSC::JSGlobalObject * glo
|
||||
auto pKeyID = EVP_PKEY_id(pkey.get());
|
||||
|
||||
if (pKeyID == EVP_PKEY_EC) {
|
||||
EC_KEY* ec_key = EVP_PKEY_get1_EC_KEY(pkey.get());
|
||||
if (UNLIKELY(ec_key == nullptr)) {
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Invalid EC private key"_s));
|
||||
return {};
|
||||
}
|
||||
const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key);
|
||||
// Get the curve name
|
||||
int curve_name = EC_GROUP_get_curve_name(ec_group);
|
||||
if (curve_name == NID_undef) {
|
||||
EC_KEY_free(ec_key);
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unable to identify EC curve"_s));
|
||||
return {};
|
||||
}
|
||||
int curve_name = curveName(globalObject, pkey.get());
|
||||
if (curve_name == NID_undef) return {}; // exception already thrown
|
||||
|
||||
CryptoKeyEC::NamedCurve curve;
|
||||
if (curve_name == NID_X9_62_prime256v1)
|
||||
curve = CryptoKeyEC::NamedCurve::P256;
|
||||
@@ -687,11 +702,9 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPrivateKey, (JSC::JSGlobalObject * glo
|
||||
else if (curve_name == NID_secp521r1)
|
||||
curve = CryptoKeyEC::NamedCurve::P521;
|
||||
else {
|
||||
EC_KEY_free(ec_key);
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unsupported EC curve"_s));
|
||||
return {};
|
||||
}
|
||||
EC_KEY_free(ec_key);
|
||||
auto impl = CryptoKeyEC::create(CryptoAlgorithmIdentifier::ECDH, curve, CryptoKeyType::Private, WTFMove(pkey), true, CryptoKeyUsageSign);
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else {
|
||||
@@ -708,7 +721,7 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPrivateKey, (JSC::JSGlobalObject * glo
|
||||
return {};
|
||||
}
|
||||
|
||||
static JSC::EncodedJSValue KeyObject__createRSAFromPrivate(JSC::JSGlobalObject* globalObject, EVP_PKEY* pkey, WebCore::CryptoAlgorithmIdentifier alg)
|
||||
static std::optional<JSCryptoKey*> KeyObject__createRSAFromPrivate(JSC::JSGlobalObject* globalObject, const EVP_PKEY* pkey, WebCore::CryptoAlgorithmIdentifier alg)
|
||||
{
|
||||
auto& vm = globalObject->vm();
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
@@ -717,21 +730,21 @@ static JSC::EncodedJSValue KeyObject__createRSAFromPrivate(JSC::JSGlobalObject*
|
||||
auto publicRSA = RSAPtr(RSAPublicKey_dup(rsa_key));
|
||||
if (!publicRSA) {
|
||||
JSC::throwTypeError(globalObject, scope, "ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE: Failed to create a public key from private"_s);
|
||||
return {};
|
||||
return std::nullopt;
|
||||
}
|
||||
auto publicPKey = EvpPKeyPtr(EVP_PKEY_new());
|
||||
if (EVP_PKEY_set1_RSA(publicPKey.get(), publicRSA.get()) <= 0) {
|
||||
JSC::throwTypeError(globalObject, scope, "ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE: Failed to create a public key from private"_s);
|
||||
return {};
|
||||
return std::nullopt;
|
||||
}
|
||||
auto impl = CryptoKeyRSA::create(alg, CryptoAlgorithmIdentifier::SHA_1, false, CryptoKeyType::Public, WTFMove(publicPKey), true, CryptoKeyUsageVerify);
|
||||
Zig::GlobalObject* zigGlobalObject = reinterpret_cast<Zig::GlobalObject*>(globalObject);
|
||||
auto* structure = zigGlobalObject->JSCryptoKeyStructure();
|
||||
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
return JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl));
|
||||
}
|
||||
|
||||
static JSC::EncodedJSValue KeyObject__createECFromPrivate(JSC::JSGlobalObject* globalObject, EVP_PKEY* pkey, CryptoKeyEC::NamedCurve namedCurve, WebCore::CryptoAlgorithmIdentifier alg)
|
||||
static std::optional<JSCryptoKey*> KeyObject__createECFromPrivate(JSC::JSGlobalObject* globalObject, const EVP_PKEY* pkey, CryptoKeyEC::NamedCurve namedCurve, WebCore::CryptoAlgorithmIdentifier alg)
|
||||
{
|
||||
auto& vm = globalObject->vm();
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
@@ -740,7 +753,7 @@ static JSC::EncodedJSValue KeyObject__createECFromPrivate(JSC::JSGlobalObject* g
|
||||
auto point = ECPointPtr(EC_POINT_dup(EC_KEY_get0_public_key(ec_key), EC_KEY_get0_group(ec_key)));
|
||||
if (!point) {
|
||||
JSC::throwTypeError(globalObject, scope, "ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE: Failed to create a public key from private 1"_s);
|
||||
return {};
|
||||
return std::nullopt;
|
||||
}
|
||||
auto curve = NID_undef;
|
||||
|
||||
@@ -758,28 +771,28 @@ static JSC::EncodedJSValue KeyObject__createECFromPrivate(JSC::JSGlobalObject* g
|
||||
auto publicECKey = ECKeyPtr(EC_KEY_new_by_curve_name(curve));
|
||||
if (!publicECKey) {
|
||||
JSC::throwTypeError(globalObject, scope, "ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE: Failed to create a public key from private 2"_s);
|
||||
return {};
|
||||
return std::nullopt;
|
||||
}
|
||||
// OPENSSL_EC_NAMED_CURVE needs to be set to export the key with the curve name, not with the curve parameters.
|
||||
EC_KEY_set_asn1_flag(publicECKey.get(), OPENSSL_EC_NAMED_CURVE);
|
||||
if (EC_KEY_set_public_key(publicECKey.get(), point.get()) <= 0) {
|
||||
JSC::throwTypeError(globalObject, scope, "ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE: Failed to create a public key from private 3"_s);
|
||||
return {};
|
||||
return std::nullopt;
|
||||
}
|
||||
auto publicPKey = EvpPKeyPtr(EVP_PKEY_new());
|
||||
if (EVP_PKEY_set1_EC_KEY(publicPKey.get(), publicECKey.get()) <= 0) {
|
||||
JSC::throwTypeError(globalObject, scope, "ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE: Failed to create a public key from private 4"_s);
|
||||
return {};
|
||||
return std::nullopt;
|
||||
}
|
||||
auto impl = CryptoKeyEC::create(alg, namedCurve, CryptoKeyType::Public, WTFMove(publicPKey), true, CryptoKeyUsageVerify);
|
||||
|
||||
Zig::GlobalObject* zigGlobalObject = reinterpret_cast<Zig::GlobalObject*>(globalObject);
|
||||
auto* structure = zigGlobalObject->JSCryptoKeyStructure();
|
||||
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
return JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl));
|
||||
}
|
||||
|
||||
static JSC::EncodedJSValue KeyObject__createOKPFromPrivate(JSC::JSGlobalObject* globalObject, const WebCore::CryptoKeyOKP::KeyMaterial keyData, CryptoKeyOKP::NamedCurve namedCurve, WebCore::CryptoAlgorithmIdentifier alg)
|
||||
static std::optional<JSCryptoKey*> KeyObject__createOKPFromPrivate(JSC::JSGlobalObject* globalObject, const WebCore::CryptoKeyOKP::KeyMaterial keyData, CryptoKeyOKP::NamedCurve namedCurve, WebCore::CryptoAlgorithmIdentifier alg)
|
||||
{
|
||||
auto& vm = globalObject->vm();
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
@@ -794,53 +807,46 @@ static JSC::EncodedJSValue KeyObject__createOKPFromPrivate(JSC::JSGlobalObject*
|
||||
auto result = CryptoKeyOKP::create(alg, namedCurve, CryptoKeyType::Public, WTFMove(public_key), true, CryptoKeyUsageVerify);
|
||||
if (UNLIKELY(result == nullptr)) {
|
||||
JSC::throwTypeError(globalObject, scope, "ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE: Failed to create a public key from private"_s);
|
||||
return {};
|
||||
return std::nullopt;
|
||||
}
|
||||
auto impl = result.releaseNonNull();
|
||||
|
||||
Zig::GlobalObject* zigGlobalObject = reinterpret_cast<Zig::GlobalObject*>(globalObject);
|
||||
auto* structure = zigGlobalObject->JSCryptoKeyStructure();
|
||||
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
return JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl));
|
||||
}
|
||||
|
||||
static JSC::EncodedJSValue KeyObject__createPublicFromPrivate(JSC::JSGlobalObject* globalObject, EVP_PKEY* pkey)
|
||||
static JSC::EncodedJSValue KeyObject__createPublicFromPrivate(JSC::JSGlobalObject* globalObject, const EVP_PKEY* pkey)
|
||||
{
|
||||
auto& vm = globalObject->vm();
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
|
||||
auto pKeyID = EVP_PKEY_id(pkey);
|
||||
int pKeyID = EVP_PKEY_id(pkey);
|
||||
if (pKeyID == EVP_PKEY_RSA || pKeyID == EVP_PKEY_RSA_PSS) {
|
||||
return KeyObject__createRSAFromPrivate(globalObject, pkey, pKeyID == EVP_PKEY_RSA_PSS ? CryptoAlgorithmIdentifier::RSA_PSS : CryptoAlgorithmIdentifier::RSA_OAEP);
|
||||
auto alg = pKeyID == EVP_PKEY_RSA_PSS ? CryptoAlgorithmIdentifier::RSA_PSS : CryptoAlgorithmIdentifier::RSA_OAEP;
|
||||
return encodeKey(KeyObject__createRSAFromPrivate(globalObject, pkey, alg));
|
||||
} else if (pKeyID == EVP_PKEY_EC) {
|
||||
int curve_name = curveName(globalObject, pkey);
|
||||
if (curve_name == NID_undef) return {}; // exception already thrown
|
||||
|
||||
EC_KEY* ec_key = EVP_PKEY_get1_EC_KEY(pkey);
|
||||
if (UNLIKELY(ec_key == nullptr)) {
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Invalid EC key"_s));
|
||||
return {};
|
||||
}
|
||||
const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key);
|
||||
// Get the curve name
|
||||
int curve_name = EC_GROUP_get_curve_name(ec_group);
|
||||
if (curve_name == NID_undef) {
|
||||
EC_KEY_free(ec_key);
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unable to identify EC curve"_s));
|
||||
return {};
|
||||
}
|
||||
CryptoKeyEC::NamedCurve curve;
|
||||
if (curve_name == NID_X9_62_prime256v1)
|
||||
switch (curve_name) {
|
||||
case NID_X9_62_prime256v1:
|
||||
curve = CryptoKeyEC::NamedCurve::P256;
|
||||
else if (curve_name == NID_secp384r1)
|
||||
break;
|
||||
case NID_secp384r1:
|
||||
curve = CryptoKeyEC::NamedCurve::P384;
|
||||
else if (curve_name == NID_secp521r1)
|
||||
break;
|
||||
case NID_secp521r1:
|
||||
curve = CryptoKeyEC::NamedCurve::P521;
|
||||
else {
|
||||
EC_KEY_free(ec_key);
|
||||
break;
|
||||
default:
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unsupported EC curve"_s));
|
||||
return {};
|
||||
}
|
||||
EC_KEY_free(ec_key);
|
||||
return KeyObject__createECFromPrivate(globalObject, pkey, curve, CryptoAlgorithmIdentifier::ECDSA);
|
||||
|
||||
return encodeKey(KeyObject__createECFromPrivate(globalObject, pkey, curve, CryptoAlgorithmIdentifier::ECDSA));
|
||||
} else if (pKeyID == EVP_PKEY_ED25519 || pKeyID == EVP_PKEY_X25519) {
|
||||
size_t out_len = 0;
|
||||
if (!EVP_PKEY_get_raw_private_key(pkey, nullptr, &out_len)) {
|
||||
@@ -852,7 +858,8 @@ static JSC::EncodedJSValue KeyObject__createPublicFromPrivate(JSC::JSGlobalObjec
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Invalid private key"_s));
|
||||
return {};
|
||||
}
|
||||
return KeyObject__createOKPFromPrivate(globalObject, out, pKeyID == EVP_PKEY_ED25519 ? CryptoKeyOKP::NamedCurve::Ed25519 : CryptoKeyOKP::NamedCurve::X25519, CryptoAlgorithmIdentifier::Ed25519);
|
||||
auto curve = pKeyID == EVP_PKEY_ED25519 ? CryptoKeyOKP::NamedCurve::Ed25519 : CryptoKeyOKP::NamedCurve::X25519;
|
||||
return encodeKey(KeyObject__createOKPFromPrivate(globalObject, out, curve, CryptoAlgorithmIdentifier::Ed25519));
|
||||
} else {
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Invalid private key type"_s));
|
||||
return {};
|
||||
@@ -868,7 +875,6 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPublicKey, (JSC::JSGlobalObject * glob
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
|
||||
if (count < 1) {
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
JSC::throwTypeError(globalObject, scope, "createPublicKey requires 1 arguments"_s);
|
||||
return {};
|
||||
}
|
||||
@@ -898,15 +904,15 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPublicKey, (JSC::JSGlobalObject * glob
|
||||
|
||||
switch (id) {
|
||||
case CryptoKeyClass::RSA: {
|
||||
return KeyObject__createRSAFromPrivate(globalObject, downcast<WebCore::CryptoKeyRSA>(wrapped).platformKey(), wrapped.algorithmIdentifier());
|
||||
return encodeKey(KeyObject__createRSAFromPrivate(globalObject, downcast<WebCore::CryptoKeyRSA>(wrapped).platformKey(), wrapped.algorithmIdentifier()));
|
||||
}
|
||||
case CryptoKeyClass::EC: {
|
||||
auto& impl = downcast<WebCore::CryptoKeyEC>(wrapped);
|
||||
return KeyObject__createECFromPrivate(globalObject, impl.platformKey(), impl.namedCurve(), wrapped.algorithmIdentifier());
|
||||
return encodeKey(KeyObject__createECFromPrivate(globalObject, impl.platformKey(), impl.namedCurve(), wrapped.algorithmIdentifier()));
|
||||
}
|
||||
case CryptoKeyClass::OKP: {
|
||||
auto& impl = downcast<WebCore::CryptoKeyOKP>(wrapped);
|
||||
return KeyObject__createOKPFromPrivate(globalObject, impl.exportKey(), impl.namedCurve(), wrapped.algorithmIdentifier());
|
||||
encodeKey(KeyObject__createOKPFromPrivate(globalObject, impl.exportKey(), impl.namedCurve(), wrapped.algorithmIdentifier()));
|
||||
}
|
||||
default: {
|
||||
JSC::throwTypeError(globalObject, scope, "ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE: Invalid key object type, expected private"_s);
|
||||
@@ -984,7 +990,7 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPublicKey, (JSC::JSGlobalObject * glob
|
||||
}
|
||||
auto impl = result.releaseNonNull();
|
||||
if (impl->type() == CryptoKeyType::Private) {
|
||||
return KeyObject__createOKPFromPrivate(globalObject, impl.get().exportKey(), CryptoKeyOKP::NamedCurve::Ed25519, CryptoAlgorithmIdentifier::Ed25519);
|
||||
return encodeKey(KeyObject__createOKPFromPrivate(globalObject, impl.get().exportKey(), CryptoKeyOKP::NamedCurve::Ed25519, CryptoAlgorithmIdentifier::Ed25519));
|
||||
}
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else if (jwk.crv == "X25519"_s) {
|
||||
@@ -995,7 +1001,7 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPublicKey, (JSC::JSGlobalObject * glob
|
||||
}
|
||||
auto impl = result.releaseNonNull();
|
||||
if (impl->type() == CryptoKeyType::Private) {
|
||||
return KeyObject__createOKPFromPrivate(globalObject, impl.get().exportKey(), CryptoKeyOKP::NamedCurve::X25519, CryptoAlgorithmIdentifier::Ed25519);
|
||||
return encodeKey(KeyObject__createOKPFromPrivate(globalObject, impl.get().exportKey(), CryptoKeyOKP::NamedCurve::X25519, CryptoAlgorithmIdentifier::Ed25519));
|
||||
}
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else {
|
||||
@@ -1010,7 +1016,7 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPublicKey, (JSC::JSGlobalObject * glob
|
||||
}
|
||||
auto impl = result.releaseNonNull();
|
||||
if (impl->type() == CryptoKeyType::Private) {
|
||||
return KeyObject__createECFromPrivate(globalObject, impl.get().platformKey(), impl.get().namedCurve(), CryptoAlgorithmIdentifier::ECDSA);
|
||||
return encodeKey(KeyObject__createECFromPrivate(globalObject, impl.get().platformKey(), impl.get().namedCurve(), CryptoAlgorithmIdentifier::ECDSA));
|
||||
}
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else if (jwk.kty == "RSA"_s) {
|
||||
@@ -1021,7 +1027,7 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPublicKey, (JSC::JSGlobalObject * glob
|
||||
}
|
||||
auto impl = result.releaseNonNull();
|
||||
if (impl->type() == CryptoKeyType::Private) {
|
||||
return KeyObject__createRSAFromPrivate(globalObject, impl.get().platformKey(), CryptoAlgorithmIdentifier::RSA_OAEP);
|
||||
return encodeKey(KeyObject__createRSAFromPrivate(globalObject, impl.get().platformKey(), CryptoAlgorithmIdentifier::RSA_OAEP));
|
||||
}
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else {
|
||||
@@ -1088,24 +1094,10 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPublicKey, (JSC::JSGlobalObject * glob
|
||||
auto impl = result.releaseNonNull();
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else if (pKeyID == EVP_PKEY_EC) {
|
||||
EC_KEY* ec_key = EVP_PKEY_get1_EC_KEY(pkey.get());
|
||||
if (UNLIKELY(ec_key == nullptr)) {
|
||||
if (pem.der_data) {
|
||||
OPENSSL_clear_free(pem.der_data, pem.der_len);
|
||||
}
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Invalid EC public key"_s));
|
||||
return {};
|
||||
}
|
||||
const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key);
|
||||
// Get the curve name
|
||||
int curve_name = EC_GROUP_get_curve_name(ec_group);
|
||||
int curve_name = curveName(globalObject, pkey.get());
|
||||
if (curve_name == NID_undef) {
|
||||
if (pem.der_data) {
|
||||
OPENSSL_clear_free(pem.der_data, pem.der_len);
|
||||
}
|
||||
EC_KEY_free(ec_key);
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unable to identify EC curve"_s));
|
||||
return {};
|
||||
if (pem.der_data) OPENSSL_clear_free(pem.der_data, pem.der_len);
|
||||
return {}; // exception already thrown
|
||||
}
|
||||
CryptoKeyEC::NamedCurve curve;
|
||||
if (curve_name == NID_X9_62_prime256v1)
|
||||
@@ -1169,7 +1161,7 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPublicKey, (JSC::JSGlobalObject * glob
|
||||
}
|
||||
|
||||
auto pKeyID = EVP_PKEY_id(pkey.get());
|
||||
return KeyObject__createRSAFromPrivate(globalObject, pkey.get(), pKeyID == EVP_PKEY_RSA_PSS ? CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5 : CryptoAlgorithmIdentifier::RSA_OAEP);
|
||||
return encodeKey(KeyObject__createRSAFromPrivate(globalObject, pkey.get(), pKeyID == EVP_PKEY_RSA_PSS ? CryptoAlgorithmIdentifier::RSASSA_PKCS1_v1_5 : CryptoAlgorithmIdentifier::RSA_OAEP));
|
||||
}
|
||||
|
||||
auto pKeyID = EVP_PKEY_id(pkey.get());
|
||||
@@ -1205,19 +1197,8 @@ JSC_DEFINE_HOST_FUNCTION(KeyObject__createPublicKey, (JSC::JSGlobalObject * glob
|
||||
auto impl = result.releaseNonNull();
|
||||
return JSC::JSValue::encode(JSCryptoKey::create(structure, zigGlobalObject, WTFMove(impl)));
|
||||
} else if (pKeyID == EVP_PKEY_EC) {
|
||||
EC_KEY* ec_key = EVP_PKEY_get1_EC_KEY(pkey.get());
|
||||
if (UNLIKELY(ec_key == nullptr)) {
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Invalid EC public key"_s));
|
||||
return {};
|
||||
}
|
||||
const EC_GROUP* ec_group = EC_KEY_get0_group(ec_key);
|
||||
// Get the curve name
|
||||
int curve_name = EC_GROUP_get_curve_name(ec_group);
|
||||
if (curve_name == NID_undef) {
|
||||
EC_KEY_free(ec_key);
|
||||
throwException(globalObject, scope, createTypeError(globalObject, "Unable to identify EC curve"_s));
|
||||
return {};
|
||||
}
|
||||
int curve_name = curveName(globalObject, pkey.get());
|
||||
if (curve_name == NID_undef) return {}; // exception already thrown.
|
||||
CryptoKeyEC::NamedCurve curve;
|
||||
if (curve_name == NID_X9_62_prime256v1)
|
||||
curve = CryptoKeyEC::NamedCurve::P256;
|
||||
|
||||
Reference in New Issue
Block a user