static ec_key ec_key_new_secp256k1_compressed() { EC_KEY* key = EC_KEY_new_by_curve_name (NID_secp256k1); if (key == nullptr) throw std::runtime_error ("EC_KEY_new_by_curve_name() failed"); EC_KEY_set_conv_form (key, POINT_CONVERSION_COMPRESSED); return ec_key((ec_key::pointer_t) key); }
Handle<ScopedEVP_PKEY> JwkEc::To(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("import EC from JWK"); ScopedEC_KEY ec_key(EC_KEY_new()); LOG_INFO("set public key"); ScopedEC_GROUP group(EC_GROUP_new_by_curve_name(this->crv)); if (group.isEmpty()) { THROW_OPENSSL("EC_GROUP_new_by_curve_name"); } EC_KEY_set_group(ec_key.Get(), group.Get()); ScopedBIGNUM x(BN_dup(this->x.Get())); ScopedBIGNUM y(BN_dup(this->y.Get())); if (EC_KEY_set_public_key_affine_coordinates(ec_key.Get(), x.Get(), y.Get()) != 1) { THROW_OPENSSL("EC_KEY_set_public_key_affine_coordinates"); } x.unref(); y.unref(); if (key_type == NODESSL_KT_PRIVATE) { LOG_INFO("set private key"); ScopedBIGNUM d(BN_dup(this->d.Get())); if (EC_KEY_set_private_key(ec_key.Get(), d.Get()) != 1) { THROW_OPENSSL("EC_KEY_set_private_key"); } d.unref(); } LOG_INFO("set internal key"); Handle<ScopedEVP_PKEY> new_key(new ScopedEVP_PKEY(EVP_PKEY_new())); EVP_PKEY_assign_EC_KEY(new_key->Get(), ec_key.Get()); ec_key.unref(); return new_key; }