2b28d6cfacfd0ddd7ff97536152f4ad895bf4120.patch 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
From 2b28d6cfacfd0ddd7ff97536152f4ad895bf4120 Mon Sep 17 00:00:00 2001
From: David Benjamin <davidben@google.com>
Date: Fri, 22 Sep 2017 18:51:21 -0400
Subject: [PATCH] crypto: make Hash 1.1.0-compatible

OpenSSL 1.1.0 requires EVP_MD_CTX be heap-allocated.

PR-URL: https://github.com/nodejs/node/pull/16130
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Rod Vagg <rod@vagg.org>
---
 src/node_crypto.cc | 23 ++++++++++++++++-------
 src/node_crypto.h  | 12 ++++--------
 2 files changed, 20 insertions(+), 15 deletions(-)

--- a/src/node_crypto.cc
+++ b/src/node_crypto.cc
Jérémy Lal's avatar
Jérémy Lal committed
18
@@ -205,6 +205,9 @@
19 20 21 22 23 24 25 26 27
   CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
   return 1;
 }
+
+#define EVP_MD_CTX_new EVP_MD_CTX_create
+#define EVP_MD_CTX_free EVP_MD_CTX_destroy
 #endif  // OPENSSL_VERSION_NUMBER < 0x10100000L
 
 // Subject DER of CNNIC ROOT CA and CNNIC EV ROOT CA are taken from
Jérémy Lal's avatar
Jérémy Lal committed
28
@@ -3945,6 +3948,11 @@
29 30 31 32 33 34 35 36 37 38 39
 }
 
 
+Hash::~Hash() {
+  EVP_MD_CTX_free(mdctx_);
+}
+
+
 void Hash::Initialize(Environment* env, v8::Local<v8::Object> target) {
   Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
 
Jérémy Lal's avatar
Jérémy Lal committed
40
@@ -3979,20 +3987,22 @@
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
   const EVP_MD* md = EVP_get_digestbyname(hash_type);
   if (md == nullptr)
     return false;
-  EVP_MD_CTX_init(&mdctx_);
-  if (EVP_DigestInit_ex(&mdctx_, md, nullptr) <= 0) {
+  mdctx_ = EVP_MD_CTX_new();
+  if (mdctx_ == nullptr ||
+      EVP_DigestInit_ex(mdctx_, md, nullptr) <= 0) {
+    EVP_MD_CTX_free(mdctx_);
+    mdctx_ = nullptr;
     return false;
   }
-  initialised_ = true;
   finalized_ = false;
   return true;
 }
 
 
 bool Hash::HashUpdate(const char* data, int len) {
-  if (!initialised_)
+  if (mdctx_ == nullptr)
     return false;
-  EVP_DigestUpdate(&mdctx_, data, len);
+  EVP_DigestUpdate(mdctx_, data, len);
   return true;
 }
 
Jérémy Lal's avatar
Jérémy Lal committed
68
@@ -4057,8 +4067,7 @@
69 70 71 72 73 74 75 76 77 78 79
   unsigned char md_value[EVP_MAX_MD_SIZE];
   unsigned int md_len;
 
-  EVP_DigestFinal_ex(&hash->mdctx_, md_value, &md_len);
-  EVP_MD_CTX_cleanup(&hash->mdctx_);
+  EVP_DigestFinal_ex(hash->mdctx_, md_value, &md_len);
   hash->finalized_ = true;
 
   Local<Value> error;
--- a/src/node_crypto.h
+++ b/src/node_crypto.h
Jérémy Lal's avatar
Jérémy Lal committed
80
@@ -524,11 +524,7 @@
81 82 83 84 85 86 87 88 89 90 91 92
 
 class Hash : public BaseObject {
  public:
-  ~Hash() override {
-    if (!initialised_)
-      return;
-    EVP_MD_CTX_cleanup(&mdctx_);
-  }
+  ~Hash() override;
 
   static void Initialize(Environment* env, v8::Local<v8::Object> target);
 
Jérémy Lal's avatar
Jérémy Lal committed
93
@@ -542,13 +538,13 @@
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
 
   Hash(Environment* env, v8::Local<v8::Object> wrap)
       : BaseObject(env, wrap),
-        initialised_(false) {
+        mdctx_(nullptr),
+        finalized_(false) {
     MakeWeak<Hash>(this);
   }
 
  private:
-  EVP_MD_CTX mdctx_; /* coverity[member_decl] */
-  bool initialised_;
+  EVP_MD_CTX* mdctx_;
   bool finalized_;
 };