Skip to content

Commit abe3dc4

Browse files
davidbenevanlucas
authored andcommitted
crypto: make Hash 1.1.0-compatible
OpenSSL 1.1.0 requires EVP_MD_CTX be heap-allocated. PR-URL: #16130 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
1 parent 59acd27 commit abe3dc4

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/node_crypto.cc

+16-7
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ static int X509_up_ref(X509* cert) {
204204
CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
205205
return 1;
206206
}
207+
208+
#define EVP_MD_CTX_new EVP_MD_CTX_create
209+
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
207210
#endif // OPENSSL_VERSION_NUMBER < 0x10100000L
208211

209212
// Subject DER of CNNIC ROOT CA and CNNIC EV ROOT CA are taken from
@@ -3937,6 +3940,11 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
39373940
}
39383941

39393942

3943+
Hash::~Hash() {
3944+
EVP_MD_CTX_free(mdctx_);
3945+
}
3946+
3947+
39403948
void Hash::Initialize(Environment* env, v8::Local<v8::Object> target) {
39413949
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
39423950

@@ -3966,20 +3974,22 @@ bool Hash::HashInit(const char* hash_type) {
39663974
const EVP_MD* md = EVP_get_digestbyname(hash_type);
39673975
if (md == nullptr)
39683976
return false;
3969-
EVP_MD_CTX_init(&mdctx_);
3970-
if (EVP_DigestInit_ex(&mdctx_, md, nullptr) <= 0) {
3977+
mdctx_ = EVP_MD_CTX_new();
3978+
if (mdctx_ == nullptr ||
3979+
EVP_DigestInit_ex(mdctx_, md, nullptr) <= 0) {
3980+
EVP_MD_CTX_free(mdctx_);
3981+
mdctx_ = nullptr;
39713982
return false;
39723983
}
3973-
initialised_ = true;
39743984
finalized_ = false;
39753985
return true;
39763986
}
39773987

39783988

39793989
bool Hash::HashUpdate(const char* data, int len) {
3980-
if (!initialised_)
3990+
if (mdctx_ == nullptr)
39813991
return false;
3982-
EVP_DigestUpdate(&mdctx_, data, len);
3992+
EVP_DigestUpdate(mdctx_, data, len);
39833993
return true;
39843994
}
39853995

@@ -4023,8 +4033,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
40234033
unsigned char md_value[EVP_MAX_MD_SIZE];
40244034
unsigned int md_len;
40254035

4026-
EVP_DigestFinal_ex(&hash->mdctx_, md_value, &md_len);
4027-
EVP_MD_CTX_cleanup(&hash->mdctx_);
4036+
EVP_DigestFinal_ex(hash->mdctx_, md_value, &md_len);
40284037
hash->finalized_ = true;
40294038

40304039
Local<Value> error;

src/node_crypto.h

+4-8
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)