<commit_before>#include "ec_jwk.h"

Handle<JwkEc> JwkEc::From(Handle<ScopedEVP_PKEY> pkey, int &key_type) {
	LOG_FUNC();

	LOG_INFO("Check key_type");
	if (!(key_type == NODESSL_KT_PRIVATE || key_type == NODESSL_KT_PUBLIC)) {
		THROW_ERROR("Wrong value of key_type");
	}

	LOG_INFO("Check pkey");
	if (pkey == nullptr) {
		THROW_ERROR("Key value is nullptr");
	}
    if (EVP_PKEY_base_id(pkey->Get()) != EVP_PKEY_EC) {
		THROW_ERROR("Key is not EC type");
	}

	LOG_INFO("Create JWK Object");
	Handle<JwkEc> jwk(new JwkEc());

	EC_KEY *ec = nullptr;
	const EC_POINT *point = nullptr;

	ScopedBN_CTX ctx(nullptr);
	const EC_GROUP *group = nullptr;

	LOG_INFO("Convert EC to JWK");
    ec = EVP_PKEY_get1_EC_KEY(pkey->Get());

	point = EC_KEY_get0_public_key(const_cast<const EC_KEY*>(ec));
	group = EC_KEY_get0_group(ec);

	LOG_INFO("Get curve name");
	jwk->crv = EC_GROUP_get_curve_name(group);

	ScopedBIGNUM x(BN_new());
	ScopedBIGNUM y(BN_new());

	LOG_INFO("Get public key");
	if (1 != EC_POINT_get_affine_coordinates(group, point, x.Get(), y.Get(), NULL)) {
		THROW_OPENSSL("EC_POINT_get_affine_coordinates");
	}
	jwk->x = BN_dup(x.Get());
	jwk->y = BN_dup(y.Get());

	if (key_type == NODESSL_KT_PRIVATE) {
		const BIGNUM *d = EC_KEY_get0_private_key(const_cast<const EC_KEY*>(ec));
		jwk->d = BN_dup(d);
		if (jwk->d.isEmpty()) {
			THROW_OPENSSL("EC_KEY_get0_private_key");
		}
	}<commit_msg>Add condition for EC_POINT_get_affine_coordinates<commit_after>#include "ec_jwk.h"

Handle<JwkEc> JwkEc::From(Handle<ScopedEVP_PKEY> pkey, int &key_type) {
	LOG_FUNC();

	LOG_INFO("Check key_type");
	if (!(key_type == NODESSL_KT_PRIVATE || key_type == NODESSL_KT_PUBLIC)) {
		THROW_ERROR("Wrong value of key_type");
	}

	LOG_INFO("Check pkey");
	if (pkey == nullptr) {
		THROW_ERROR("Key value is nullptr");
	}
    if (EVP_PKEY_base_id(pkey->Get()) != EVP_PKEY_EC) {
		THROW_ERROR("Key is not EC type");
	}

	LOG_INFO("Create JWK Object");
	Handle<JwkEc> jwk(new JwkEc());

	EC_KEY *ec = nullptr;
	const EC_POINT *point = nullptr;

	ScopedBN_CTX ctx(nullptr);
	const EC_GROUP *group = nullptr;

	LOG_INFO("Convert EC to JWK");
    ec = EVP_PKEY_get1_EC_KEY(pkey->Get());

	point = EC_KEY_get0_public_key(const_cast<const EC_KEY*>(ec));
	group = EC_KEY_get0_group(ec);

	LOG_INFO("Get curve name");
	jwk->crv = EC_GROUP_get_curve_name(group);

	ScopedBIGNUM x(BN_new());
	ScopedBIGNUM y(BN_new());

	LOG_INFO("Get public key");

#if NODE_MODULE_VERSION < 60 || OPENSSL_1_0_2
#define EC_POINT_GET_AFFINE_COORDINATES EC_POINT_get_affine_coordinates_GFp
#else
#define EC_POINT_GET_AFFINE_COORDINATES EC_POINT_get_affine_coordinates
#endif

	if (1 != EC_POINT_GET_AFFINE_COORDINATES(group, point, x.Get(), y.Get(), NULL)) {
		THROW_OPENSSL("EC_POINT_get_affine_coordinates_GFp");
	}
	jwk->x = BN_dup(x.Get());
	jwk->y = BN_dup(y.Get());

	if (key_type == NODESSL_KT_PRIVATE) {
		const BIGNUM *d = EC_KEY_get0_private_key(const_cast<const EC_KEY*>(ec));
		jwk->d = BN_dup(d);
		if (jwk->d.isEmpty()) {
			THROW_OPENSSL("EC_KEY_get0_private_key");
		}
	}
{'fraction_non_alphanumeric': 0.15369458128078817, 'fraction_numerical': 0.005582922824302135, 'mean_word_length': 5.829596412556054, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4073383', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1294, 'n_words': 221}