Skip to content

Commit 2b28d6c

Browse files
davidbenrvagg
authored andcommitted
crypto: make Hash 1.1.0-compatible
OpenSSL 1.1.0 requires EVP_MD_CTX be heap-allocated. PR-URL: nodejs#16130 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Rod Vagg <rod@vagg.org>
1 parent 146e8f8 commit 2b28d6c

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
@@ -197,6 +197,9 @@ static int X509_up_ref(X509* cert) {
197197
CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
198198
return 1;
199199
}
200+
201+
#define EVP_MD_CTX_new EVP_MD_CTX_create
202+
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
200203
#endif // OPENSSL_VERSION_NUMBER < 0x10100000L
201204

202205
// Subject DER of CNNIC ROOT CA and CNNIC EV ROOT CA are taken from
@@ -3906,6 +3909,11 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
39063909
}
39073910

39083911

3912+
Hash::~Hash() {
3913+
EVP_MD_CTX_free(mdctx_);
3914+
}
3915+
3916+
39093917
void Hash::Initialize(Environment* env, v8::Local<v8::Object> target) {
39103918
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
39113919

@@ -3935,20 +3943,22 @@ bool Hash::HashInit(const char* hash_type) {
39353943
const EVP_MD* md = EVP_get_digestbyname(hash_type);
39363944
if (md == nullptr)
39373945
return false;
3938-
EVP_MD_CTX_init(&mdctx_);
3939-
if (EVP_DigestInit_ex(&mdctx_, md, nullptr) <= 0) {
3946+
mdctx_ = EVP_MD_CTX_new();
3947+
if (mdctx_ == nullptr ||
3948+
EVP_DigestInit_ex(mdctx_, md, nullptr) <= 0) {
3949+
EVP_MD_CTX_free(mdctx_);
3950+
mdctx_ = nullptr;
39403951
return false;
39413952
}
3942-
initialised_ = true;
39433953
finalized_ = false;
39443954
return true;
39453955
}
39463956

39473957

39483958
bool Hash::HashUpdate(const char* data, int len) {
3949-
if (!initialised_)
3959+
if (mdctx_ == nullptr)
39503960
return false;
3951-
EVP_DigestUpdate(&mdctx_, data, len);
3961+
EVP_DigestUpdate(mdctx_, data, len);
39523962
return true;
39533963
}
39543964

@@ -3992,8 +4002,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
39924002
unsigned char md_value[EVP_MAX_MD_SIZE];
39934003
unsigned int md_len;
39944004

3995-
EVP_DigestFinal_ex(&hash->mdctx_, md_value, &md_len);
3996-
EVP_MD_CTX_cleanup(&hash->mdctx_);
4005+
EVP_DigestFinal_ex(hash->mdctx_, md_value, &md_len);
39974006
hash->finalized_ = true;
39984007

39994008
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)