Skip to content

Commit 0ef35a1

Browse files
davidbengibfahn
authored andcommitted
crypto: make Hash 1.1.0-compatible
OpenSSL 1.1.0 requires EVP_MD_CTX be heap-allocated. PR-URL: #16130 Backport-PR-URL: #18622 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Rod Vagg <rod@vagg.org>
1 parent e0cbc39 commit 0ef35a1

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/node_crypto.cc

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ static int X509_up_ref(X509* cert) {
205205
CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
206206
return 1;
207207
}
208+
209+
#define EVP_MD_CTX_new EVP_MD_CTX_create
210+
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
208211
#endif // OPENSSL_VERSION_NUMBER < 0x10100000L
209212

210213
// Subject DER of CNNIC ROOT CA and CNNIC EV ROOT CA are taken from
@@ -3955,6 +3958,11 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
39553958
}
39563959

39573960

3961+
Hash::~Hash() {
3962+
EVP_MD_CTX_free(mdctx_);
3963+
}
3964+
3965+
39583966
void Hash::Initialize(Environment* env, v8::Local<v8::Object> target) {
39593967
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
39603968

@@ -3989,20 +3997,22 @@ bool Hash::HashInit(const char* hash_type) {
39893997
const EVP_MD* md = EVP_get_digestbyname(hash_type);
39903998
if (md == nullptr)
39913999
return false;
3992-
EVP_MD_CTX_init(&mdctx_);
3993-
if (EVP_DigestInit_ex(&mdctx_, md, nullptr) <= 0) {
4000+
mdctx_ = EVP_MD_CTX_new();
4001+
if (mdctx_ == nullptr ||
4002+
EVP_DigestInit_ex(mdctx_, md, nullptr) <= 0) {
4003+
EVP_MD_CTX_free(mdctx_);
4004+
mdctx_ = nullptr;
39944005
return false;
39954006
}
3996-
initialised_ = true;
39974007
finalized_ = false;
39984008
return true;
39994009
}
40004010

40014011

40024012
bool Hash::HashUpdate(const char* data, int len) {
4003-
if (!initialised_)
4013+
if (mdctx_ == nullptr)
40044014
return false;
4005-
EVP_DigestUpdate(&mdctx_, data, len);
4015+
EVP_DigestUpdate(mdctx_, data, len);
40064016
return true;
40074017
}
40084018

@@ -4067,8 +4077,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
40674077
unsigned char md_value[EVP_MAX_MD_SIZE];
40684078
unsigned int md_len;
40694079

4070-
EVP_DigestFinal_ex(&hash->mdctx_, md_value, &md_len);
4071-
EVP_MD_CTX_cleanup(&hash->mdctx_);
4080+
EVP_DigestFinal_ex(hash->mdctx_, md_value, &md_len);
40724081
hash->finalized_ = true;
40734082

40744083
Local<Value> error;

src/node_crypto.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,7 @@ class Hmac : public BaseObject {
524524

525525
class Hash : public BaseObject {
526526
public:
527-
~Hash() override {
528-
if (!initialised_)
529-
return;
530-
EVP_MD_CTX_cleanup(&mdctx_);
531-
}
527+
~Hash() override;
532528

533529
static void Initialize(Environment* env, v8::Local<v8::Object> target);
534530

@@ -542,13 +538,13 @@ class Hash : public BaseObject {
542538

543539
Hash(Environment* env, v8::Local<v8::Object> wrap)
544540
: BaseObject(env, wrap),
545-
initialised_(false) {
541+
mdctx_(nullptr),
542+
finalized_(false) {
546543
MakeWeak<Hash>(this);
547544
}
548545

549546
private:
550-
EVP_MD_CTX mdctx_; /* coverity[member_decl] */
551-
bool initialised_;
547+
EVP_MD_CTX* mdctx_;
552548
bool finalized_;
553549
};
554550

0 commit comments

Comments
 (0)