diff --git a/source/extensions/common/crypto/utility_impl.cc b/source/extensions/common/crypto/utility_impl.cc index cfac2a38e5..3b2ab7d633 100644 --- a/source/extensions/common/crypto/utility_impl.cc +++ b/source/extensions/common/crypto/utility_impl.cc @@ -52,6 +52,7 @@ const VerificationOutput UtilityImpl::verifySignature(absl::string_view hash, Cr if (md == nullptr) { return {false, absl::StrCat(hash, " is not supported.")}; } + // Step 3: initialize EVP_DigestVerify auto pkey_wrapper = Common::Crypto::Access::getTyped(key); EVP_PKEY* pkey = pkey_wrapper->getEVP_PKEY(); @@ -77,10 +78,9 @@ const VerificationOutput UtilityImpl::verifySignature(absl::string_view hash, Cr return {false, absl::StrCat("Failed to verify digest. Error code: ", ok)}; } -// This is a dummy implementation of the interface, as EVP_parse_public_key isn't available under OpenSSL CryptoObjectPtr UtilityImpl::importPublicKey(const std::vector& key) { - CBS cbs({key.data(), key.size()}); - return std::make_unique(); //EVP_parse_public_key(&cbs)); + const unsigned char* tmp = key.data(); + return std::make_unique(d2i_PUBKEY(nullptr, &tmp, key.size())); } const EVP_MD* UtilityImpl::getHashFunction(absl::string_view name) { diff --git a/source/extensions/filters/http/lua/lua_filter.cc b/source/extensions/filters/http/lua/lua_filter.cc index 84c744f485..bf9c3cfc4b 100644 --- a/source/extensions/filters/http/lua/lua_filter.cc +++ b/source/extensions/filters/http/lua/lua_filter.cc @@ -468,6 +468,22 @@ int StreamHandleWrapper::luaVerifySignature(lua_State* state) { return 2; } +int StreamHandleWrapper::luaImportPublicKey(lua_State* state) { + // Get byte array and the length. + const char* str = luaL_checkstring(state, 2); + int n = luaL_checknumber(state, 3); + std::vector key(str, str + n); + if (public_key_wrapper_.get() != nullptr) { + public_key_wrapper_.pushStack(); + } else { + auto& crypto_util = Envoy::Common::Crypto::UtilitySingleton::get(); + Common::Crypto::CryptoObjectPtr crypto_ptr = crypto_util.importPublicKey(key); + public_key_wrapper_.reset(PublicKeyWrapper::create(state, std::move(crypto_ptr)), true); + } + + return 1; +} + FilterConfig::FilterConfig(const std::string& lua_code, ThreadLocal::SlotAllocator& tls, Upstream::ClusterManager& cluster_manager) : cluster_manager_(cluster_manager), lua_state_(lua_code, tls) { diff --git a/source/extensions/filters/http/lua/lua_filter.h b/source/extensions/filters/http/lua/lua_filter.h index 3ff31a27eb..2b99219124 100644 --- a/source/extensions/filters/http/lua/lua_filter.h +++ b/source/extensions/filters/http/lua/lua_filter.h @@ -144,6 +144,7 @@ class StreamHandleWrapper : public Filters::Common::Lua::BaseLuaObject