@@ -207,6 +207,20 @@ static int X509_up_ref(X509* cert) {
207
207
208
208
#define EVP_MD_CTX_new EVP_MD_CTX_create
209
209
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
210
+
211
+ HMAC_CTX* HMAC_CTX_new () {
212
+ HMAC_CTX* ctx = Malloc<HMAC_CTX>(1 );
213
+ HMAC_CTX_init (ctx);
214
+ return ctx;
215
+ }
216
+
217
+ void HMAC_CTX_free (HMAC_CTX* ctx) {
218
+ if (ctx == nullptr ) {
219
+ return ;
220
+ }
221
+ HMAC_CTX_cleanup (ctx);
222
+ free (ctx);
223
+ }
210
224
#endif // OPENSSL_VERSION_NUMBER < 0x10100000L
211
225
212
226
// Subject DER of CNNIC ROOT CA and CNNIC EV ROOT CA are taken from
@@ -3821,6 +3835,11 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
3821
3835
}
3822
3836
3823
3837
3838
+ Hmac::~Hmac () {
3839
+ HMAC_CTX_free (ctx_);
3840
+ }
3841
+
3842
+
3824
3843
void Hmac::Initialize (Environment* env, v8::Local<v8::Object> target) {
3825
3844
Local<FunctionTemplate> t = env->NewFunctionTemplate (New);
3826
3845
@@ -3847,14 +3866,16 @@ void Hmac::HmacInit(const char* hash_type, const char* key, int key_len) {
3847
3866
if (md == nullptr ) {
3848
3867
return env ()->ThrowError (" Unknown message digest" );
3849
3868
}
3850
- HMAC_CTX_init (&ctx_);
3851
3869
if (key_len == 0 ) {
3852
3870
key = " " ;
3853
3871
}
3854
- if (!HMAC_Init_ex (&ctx_, key, key_len, md, nullptr )) {
3872
+ ctx_ = HMAC_CTX_new ();
3873
+ if (ctx_ == nullptr ||
3874
+ !HMAC_Init_ex (ctx_, key, key_len, md, nullptr )) {
3875
+ HMAC_CTX_free (ctx_);
3876
+ ctx_ = nullptr ;
3855
3877
return ThrowCryptoError (env (), ERR_get_error ());
3856
3878
}
3857
- initialised_ = true ;
3858
3879
}
3859
3880
3860
3881
@@ -3871,9 +3892,9 @@ void Hmac::HmacInit(const FunctionCallbackInfo<Value>& args) {
3871
3892
3872
3893
3873
3894
bool Hmac::HmacUpdate (const char * data, int len) {
3874
- if (!initialised_ )
3895
+ if (ctx_ == nullptr )
3875
3896
return false ;
3876
- int r = HMAC_Update (& ctx_, reinterpret_cast <const unsigned char *>(data), len);
3897
+ int r = HMAC_Update (ctx_, reinterpret_cast <const unsigned char *>(data), len);
3877
3898
return r == 1 ;
3878
3899
}
3879
3900
@@ -3918,10 +3939,10 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
3918
3939
unsigned char md_value[EVP_MAX_MD_SIZE];
3919
3940
unsigned int md_len = 0 ;
3920
3941
3921
- if (hmac->initialised_ ) {
3922
- HMAC_Final (& hmac->ctx_ , md_value, &md_len);
3923
- HMAC_CTX_cleanup (& hmac->ctx_ );
3924
- hmac->initialised_ = false ;
3942
+ if (hmac->ctx_ != nullptr ) {
3943
+ HMAC_Final (hmac->ctx_ , md_value, &md_len);
3944
+ HMAC_CTX_free ( hmac->ctx_ );
3945
+ hmac->ctx_ = nullptr ;
3925
3946
}
3926
3947
3927
3948
Local<Value> error;
0 commit comments