From f1de8d20df20f3edbfab5f3848037fa615178765 Mon Sep 17 00:00:00 2001 From: Jeremi Piotrowski Date: Tue, 3 May 2022 19:32:29 +0200 Subject: [PATCH] sdk/core: Reimplement Md5OpenSSL using EVP API (#3609) The MD5_Init/Update/Final functions are deprecated in OpenSSL 3.0 and result in a compile-time warning. Due to the default usage of -Werror during compilation, these warnings are treated as errors and prevent the SDK from being built on Ubuntu 22.04, which ships with OpenSSL by default. The deprecated APIs should be replaced by the EVP APIs, which are already in use for the SHA family of functions, and supported on all versions of OpenSSL. --- sdk/core/azure-core/src/cryptography/md5.cpp | 39 +++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/sdk/core/azure-core/src/cryptography/md5.cpp b/sdk/core/azure-core/src/cryptography/md5.cpp index c3da3dcf6..a4bbaa217 100644 --- a/sdk/core/azure-core/src/cryptography/md5.cpp +++ b/sdk/core/azure-core/src/cryptography/md5.cpp @@ -10,7 +10,7 @@ #include #elif defined(AZ_PLATFORM_POSIX) -#include +#include #endif #include @@ -161,24 +161,45 @@ Azure::Core::Cryptography::Md5Hash::Md5Hash() : m_implementation(std::make_uniqu class Md5OpenSSL final : public Azure::Core::Cryptography::Hash { private: - std::unique_ptr m_context; + EVP_MD_CTX* m_context; - void OnAppend(const uint8_t* data, size_t length) { MD5_Update(m_context.get(), data, length); } + void OnAppend(const uint8_t* data, size_t length) override + { + if (1 != EVP_DigestUpdate(m_context, data, length)) + { + throw std::runtime_error("Crypto error while updating Md5Hash."); + } + } - std::vector OnFinal(const uint8_t* data, size_t length) + std::vector OnFinal(const uint8_t* data, size_t length) override { OnAppend(data, length); - unsigned char hash[MD5_DIGEST_LENGTH]; - MD5_Final(hash, m_context.get()); - return std::vector(std::begin(hash), std::end(hash)); + unsigned int size; + unsigned char hash[EVP_MAX_MD_SIZE]; + if (1 != EVP_DigestFinal(m_context, hash, &size)) + { + throw std::runtime_error("Crypto error while computing Md5Hash."); + } + + return std::vector(std::begin(hash), std::begin(hash) + size); } public: Md5OpenSSL() { - m_context = std::make_unique(); - MD5_Init(m_context.get()); + m_context = EVP_MD_CTX_new(); + if (m_context == NULL) + { + throw std::runtime_error("Crypto error while creating EVP context."); + } + if (1 != EVP_DigestInit_ex(m_context, EVP_md5(), NULL)) + { + EVP_MD_CTX_free(m_context); + throw std::runtime_error("Crypto error while init Md5Hash."); + } } + + ~Md5OpenSSL() { EVP_MD_CTX_free(m_context); } }; } // namespace