Skip to content

Commit

Permalink
sdk/core: Reimplement Md5OpenSSL using EVP API (#3609)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jepio authored May 3, 2022
1 parent 2c3d73e commit f1de8d2
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions sdk/core/azure-core/src/cryptography/md5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <bcrypt.h>
#elif defined(AZ_PLATFORM_POSIX)
#include <openssl/md5.h>
#include <openssl/evp.h>
#endif

#include <stdexcept>
Expand Down Expand Up @@ -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<MD5_CTX> 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<uint8_t> OnFinal(const uint8_t* data, size_t length)
std::vector<uint8_t> 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<uint8_t>(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<uint8_t>(std::begin(hash), std::begin(hash) + size);
}

public:
Md5OpenSSL()
{
m_context = std::make_unique<MD5_CTX>();
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
Expand Down

0 comments on commit f1de8d2

Please sign in to comment.