security: deduplicate RAII wrapper class for EVP_MD_CTX

Change-Id: I256588a8bc601b06548f7ec45aee020ddabb9597
diff --git a/src/security/detail/openssl-helper.cpp b/src/security/detail/openssl-helper.cpp
index d854776..865bc36 100644
--- a/src/security/detail/openssl-helper.cpp
+++ b/src/security/detail/openssl-helper.cpp
@@ -47,6 +47,26 @@
 #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
 }
 
+EvpMdCtx::EvpMdCtx()
+#if OPENSSL_VERSION_NUMBER < 0x1010000fL
+  : m_ctx(EVP_MD_CTX_create())
+#else
+  : m_ctx(EVP_MD_CTX_new())
+#endif
+{
+  if (m_ctx == nullptr)
+    BOOST_THROW_EXCEPTION(std::runtime_error("EVP_MD_CTX creation failed"));
+}
+
+EvpMdCtx::~EvpMdCtx()
+{
+#if OPENSSL_VERSION_NUMBER < 0x1010000fL
+  EVP_MD_CTX_destroy(m_ctx);
+#else
+  EVP_MD_CTX_free(m_ctx);
+#endif
+}
+
 EvpPkeyCtx::EvpPkeyCtx(EVP_PKEY* key)
   : m_ctx(EVP_PKEY_CTX_new(key, nullptr))
 {
diff --git a/src/security/detail/openssl-helper.hpp b/src/security/detail/openssl-helper.hpp
index 51a4a36..58690ca 100644
--- a/src/security/detail/openssl-helper.hpp
+++ b/src/security/detail/openssl-helper.hpp
@@ -35,6 +35,22 @@
 int
 getEvpPkeyType(EVP_PKEY* key);
 
+class EvpMdCtx : noncopyable
+{
+public:
+  EvpMdCtx();
+
+  ~EvpMdCtx();
+
+  operator EVP_MD_CTX*() const
+  {
+    return m_ctx;
+  }
+
+private:
+  EVP_MD_CTX* m_ctx;
+};
+
 class EvpPkeyCtx : noncopyable
 {
 public: