Browse code

Fix some race conditions. Fix some memory leaks.

Shawn Webb authored on 2014/02/14 03:05:50
Showing 18 changed files
... ...
@@ -58,7 +58,11 @@ void XzCheck_Init(CXzCheck *p, int mode)
58 58
   {
59 59
     case XZ_CHECK_CRC32: p->crc = CRC_INIT_VAL; break;
60 60
     case XZ_CHECK_CRC64: p->crc64 = CRC64_INIT_VAL; break;
61
-    case XZ_CHECK_SHA256: EVP_DigestInit(&p->sha, EVP_sha256()); break;
61
+    case XZ_CHECK_SHA256:
62
+        p->sha = EVP_MD_CTX_create();
63
+        if ((p->sha))
64
+            EVP_DigestInit_ex(p->sha, EVP_sha256(), NULL);
65
+        break;
62 66
   }
63 67
 }
64 68
 
... ...
@@ -68,7 +72,10 @@ void XzCheck_Update(CXzCheck *p, const void *data, size_t size)
68 68
   {
69 69
     case XZ_CHECK_CRC32: p->crc = CrcUpdate(p->crc, data, size); break;
70 70
     case XZ_CHECK_CRC64: p->crc64 = Crc64Update(p->crc64, data, size); break;
71
-    case XZ_CHECK_SHA256: EVP_DigestUpdate(&p->sha, (const Byte *)data, size); break;
71
+    case XZ_CHECK_SHA256:
72
+        if ((p->sha))
73
+            EVP_DigestUpdate(p->sha, (const Byte *)data, size);
74
+        break;
72 75
   }
73 76
 }
74 77
 
... ...
@@ -88,8 +95,11 @@ int XzCheck_Final(CXzCheck *p, Byte *digest)
88 88
       break;
89 89
     }
90 90
     case XZ_CHECK_SHA256:
91
-      EVP_DigestFinal(&p->sha, digest, NULL);
92
-      EVP_MD_CTX_cleanup(&(p->sha));
91
+      if (!(p->sha))
92
+          return 0;
93
+
94
+      EVP_DigestFinal_ex(p->sha, digest, NULL);
95
+      EVP_MD_CTX_destroy(p->sha);
93 96
       break;
94 97
     default:
95 98
       return 0;
... ...
@@ -81,7 +81,7 @@ typedef struct
81 81
   int mode;
82 82
   UInt32 crc;
83 83
   UInt64 crc64;
84
-  EVP_MD_CTX sha;
84
+  EVP_MD_CTX *sha;
85 85
 } CXzCheck;
86 86
 
87 87
 void XzCheck_Init(CXzCheck *p, int mode);
... ...
@@ -217,7 +217,7 @@ typedef struct
217 217
   CMixCoder decoder;
218 218
   CXzBlock block;
219 219
   CXzCheck check;
220
-  EVP_MD_CTX sha;
220
+  EVP_MD_CTX *sha;
221 221
   Byte shaDigest[SHA256_DIGEST_SIZE];
222 222
   Byte buf[XZ_BLOCK_HEADER_SIZE_MAX];
223 223
 } CXzUnpacker;
... ...
@@ -651,7 +651,8 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
651 651
         Byte temp[32];
652 652
         unsigned num = Xz_WriteVarInt(temp, p->packSize + p->blockHeaderSize + XzFlags_GetCheckSize(p->streamFlags));
653 653
         num += Xz_WriteVarInt(temp + num, p->unpackSize);
654
-        EVP_DigestUpdate(&p->sha, temp, num);
654
+        if ((p->sha))
655
+            EVP_DigestUpdate(p->sha, temp, num);
655 656
         p->indexSize += num;
656 657
         p->numBlocks++;
657 658
         
... ...
@@ -686,7 +687,9 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
686 686
         {
687 687
           RINOK(Xz_ParseHeader(&p->streamFlags, p->buf));
688 688
           p->state = XZ_STATE_BLOCK_HEADER;
689
-          EVP_DigestInit(&p->sha, EVP_sha256());
689
+          p->sha = EVP_MD_CTX_create();
690
+          if ((p->sha))
691
+              EVP_DigestInit_ex(p->sha, EVP_sha256(), NULL);
690 692
           p->indexSize = 0;
691 693
           p->numBlocks = 0;
692 694
           p->pos = 0;
... ...
@@ -705,9 +708,13 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
705 705
             p->indexPreSize = 1 + Xz_WriteVarInt(p->buf + 1, p->numBlocks);
706 706
             p->indexPos = p->indexPreSize;
707 707
             p->indexSize += p->indexPreSize;
708
-            EVP_DigestFinal(&p->sha, p->shaDigest, NULL);
709
-            EVP_MD_CTX_cleanup(&p->sha);
710
-            EVP_DigestInit(&p->sha, EVP_sha256());
708
+            if ((p->sha)) {
709
+                EVP_DigestFinal_ex(p->sha, p->shaDigest, NULL);
710
+                EVP_MD_CTX_destroy(p->sha);
711
+                p->sha = EVP_MD_CTX_create();
712
+                if ((p->sha))
713
+                    EVP_DigestInit_ex(p->sha, EVP_sha256(), NULL);
714
+            }
711 715
             p->crc = CrcUpdate(CRC_INIT_VAL, p->buf, p->indexPreSize);
712 716
             p->state = XZ_STATE_STREAM_INDEX;
713 717
           }
... ...
@@ -785,7 +792,8 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
785 785
             if (srcRem > cur)
786 786
               srcRem = (SizeT)cur;
787 787
             p->crc = CrcUpdate(p->crc, src, srcRem);
788
-            EVP_DigestUpdate(&p->sha, src, srcRem);
788
+            if ((p->sha))
789
+                EVP_DigestUpdate(p->sha, src, srcRem);
789 790
             (*srcLen) += srcRem;
790 791
             src += srcRem;
791 792
             p->indexPos += srcRem;
... ...
@@ -806,8 +814,11 @@ SRes XzUnpacker_Code(CXzUnpacker *p, Byte *dest, SizeT *destLen,
806 806
             p->state = XZ_STATE_STREAM_INDEX_CRC;
807 807
             p->indexSize += 4;
808 808
             p->pos = 0;
809
-            EVP_DigestFinal(&p->sha, digest, NULL);
810
-            EVP_MD_CTX_cleanup(&p->sha);
809
+            if ((p->sha)) {
810
+                EVP_DigestFinal_ex(p->sha, digest, NULL);
811
+                EVP_MD_CTX_destroy(p->sha);
812
+            }
813
+
811 814
             if (memcmp(digest, p->shaDigest, SHA256_DIGEST_SIZE) != 0)
812 815
               return SZ_ERROR_CRC;
813 816
           }
... ...
@@ -731,7 +731,7 @@ static int asn1_parse_mscat(fmap_t *map, size_t offset, unsigned int size, crtmg
731 731
     unsigned int dsize, message_size, attrs_size;
732 732
     cli_crt_hashtype hashtype;
733 733
     cli_crt *x509;
734
-    EVP_MD_CTX ctx;
734
+    EVP_MD_CTX *ctx;
735 735
     int result;
736 736
     int isBlacklisted = 0;
737 737
 
... ...
@@ -1021,11 +1021,15 @@ static int asn1_parse_mscat(fmap_t *map, size_t offset, unsigned int size, crtmg
1021 1021
 	    break;
1022 1022
 	}
1023 1023
 
1024
-    EVP_DigestInit(&ctx, EVP_sha1());
1025
-	EVP_DigestUpdate(&ctx, "\x31", 1);
1026
-	EVP_DigestUpdate(&ctx, attrs + 1, attrs_size - 1);
1027
-	EVP_DigestFinal(&ctx, sha1, NULL);
1028
-    EVP_MD_CTX_cleanup(&ctx);
1024
+    ctx = EVP_MD_CTX_create();
1025
+    if (!(ctx))
1026
+        break;
1027
+
1028
+    EVP_DigestInit_ex(ctx, EVP_sha1(), NULL);
1029
+	EVP_DigestUpdate(ctx, "\x31", 1);
1030
+	EVP_DigestUpdate(ctx, attrs + 1, attrs_size - 1);
1031
+	EVP_DigestFinal_ex(ctx, sha1, NULL);
1032
+    EVP_MD_CTX_destroy(ctx);
1029 1033
 
1030 1034
 	if(!fmap_need_ptr_once(map, asn1.content, asn1.size)) {
1031 1035
 	    cli_dbgmsg("asn1_parse_mscat: failed to read encryptedDigest\n");
... ...
@@ -1263,17 +1267,25 @@ static int asn1_parse_mscat(fmap_t *map, size_t offset, unsigned int size, crtmg
1263 1263
 	}
1264 1264
 
1265 1265
 	if(hashtype == CLI_SHA1RSA) {
1266
-        EVP_DigestInit(&ctx, EVP_sha1());
1267
-        EVP_DigestUpdate(&ctx, "\x31", 1);
1268
-        EVP_DigestUpdate(&ctx, attrs + 1, attrs_size - 1);
1269
-        EVP_DigestFinal(&ctx, sha1, NULL);
1270
-        EVP_MD_CTX_cleanup(&ctx);
1266
+        ctx = EVP_MD_CTX_create();
1267
+        if (!(ctx))
1268
+            break;
1269
+
1270
+        EVP_DigestInit_ex(ctx, EVP_sha1(), NULL);
1271
+        EVP_DigestUpdate(ctx, "\x31", 1);
1272
+        EVP_DigestUpdate(ctx, attrs + 1, attrs_size - 1);
1273
+        EVP_DigestFinal_ex(ctx, sha1, NULL);
1274
+        EVP_MD_CTX_destroy(ctx);
1271 1275
 	} else {
1272
-        EVP_DigestInit(&ctx, EVP_md5());
1273
-        EVP_DigestUpdate(&ctx, "\x31", 1);
1274
-        EVP_DigestUpdate(&ctx, attrs + 1, attrs_size - 1);
1275
-        EVP_DigestFinal(&ctx, sha1, NULL);
1276
-        EVP_MD_CTX_cleanup(&ctx);
1276
+        ctx = EVP_MD_CTX_create();
1277
+        if (!(ctx))
1278
+            break;
1279
+
1280
+        EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
1281
+        EVP_DigestUpdate(ctx, "\x31", 1);
1282
+        EVP_DigestUpdate(ctx, attrs + 1, attrs_size - 1);
1283
+        EVP_DigestFinal_ex(ctx, sha1, NULL);
1284
+        EVP_MD_CTX_destroy(ctx);
1277 1285
 	}
1278 1286
 
1279 1287
 	if(!fmap_need_ptr_once(map, asn1.content, asn1.size)) {
... ...
@@ -905,7 +905,7 @@ void cache_remove(unsigned char *md5, size_t size, const struct cl_engine *engin
905 905
 int cache_check(unsigned char *hash, cli_ctx *ctx) {
906 906
     fmap_t *map;
907 907
     size_t todo, at = 0;
908
-    EVP_MD_CTX hashctx;
908
+    EVP_MD_CTX *hashctx;
909 909
     int ret;
910 910
 
911 911
     if(!ctx || !ctx->engine || !ctx->engine->cache)
... ...
@@ -919,7 +919,11 @@ int cache_check(unsigned char *hash, cli_ctx *ctx) {
919 919
     map = *ctx->fmap;
920 920
     todo = map->len;
921 921
 
922
-    EVP_DigestInit(&hashctx, EVP_md5());
922
+    hashctx = EVP_MD_CTX_create();
923
+    if (!(hashctx))
924
+        return CL_VIRUS;
925
+
926
+    EVP_DigestInit_ex(hashctx, EVP_md5(), NULL);
923 927
 
924 928
     while(todo) {
925 929
         const void *buf;
... ...
@@ -931,14 +935,14 @@ int cache_check(unsigned char *hash, cli_ctx *ctx) {
931 931
         todo -= readme;
932 932
         at += readme;
933 933
 
934
-        if (!EVP_DigestUpdate(&hashctx, buf, readme)) {
934
+        if (!EVP_DigestUpdate(hashctx, buf, readme)) {
935 935
             cli_errmsg("cache_check: error reading while generating hash!\n");
936 936
             return CL_EREAD;
937 937
         }
938 938
     }
939 939
 
940
-    EVP_DigestFinal(&hashctx, hash, NULL);
941
-    EVP_MD_CTX_cleanup(&hashctx);
940
+    EVP_DigestFinal_ex(hashctx, hash, NULL);
941
+    EVP_MD_CTX_destroy(hashctx);
942 942
 
943 943
     ret = cache_lookup_hash(hash, map->len, ctx->engine->cache, ctx->recursion);
944 944
     cli_dbgmsg("cache_check: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x is %s\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15], (ret == CL_VIRUS) ? "negative" : "positive");
... ...
@@ -78,7 +78,7 @@ void cl_cleanup_crypto(void)
78 78
 
79 79
 unsigned char *cl_hash_data(char *alg, const void *buf, size_t len, unsigned char *obuf, unsigned int *olen)
80 80
 {
81
-    EVP_MD_CTX ctx;
81
+    EVP_MD_CTX *ctx;
82 82
     unsigned char *ret;
83 83
     size_t mdsz;
84 84
     const EVP_MD *md;
... ...
@@ -95,43 +95,54 @@ unsigned char *cl_hash_data(char *alg, const void *buf, size_t len, unsigned cha
95 95
     if (!(ret))
96 96
         return NULL;
97 97
 
98
-    if (!EVP_DigestInit(&ctx, md)) {
98
+    ctx = EVP_MD_CTX_create();
99
+    if (!(ctx)) {
100
+        if (!(obuf))
101
+            free(ret);
102
+
103
+        return NULL;
104
+    }
105
+
106
+    if (!EVP_DigestInit_ex(ctx, md, NULL)) {
99 107
         if (!(obuf))
100 108
             free(ret);
101 109
 
102 110
         if ((olen))
103 111
             *olen = 0;
104 112
 
113
+        EVP_MD_CTX_destroy(ctx);
105 114
         return NULL;
106 115
     }
107 116
 
108 117
     cur=0;
109 118
     while (cur < len) {
110 119
         size_t todo = MIN(EVP_MD_block_size(md), len-cur);
111
-        if (!EVP_DigestUpdate(&ctx, (void *)(((unsigned char *)buf)+cur), todo)) {
120
+        if (!EVP_DigestUpdate(ctx, (void *)(((unsigned char *)buf)+cur), todo)) {
112 121
             if (!(obuf))
113 122
                 free(ret);
114 123
 
115 124
             if ((olen))
116 125
                 *olen = 0;
117 126
 
127
+            EVP_MD_CTX_destroy(ctx);
118 128
             return NULL;
119 129
         }
120 130
 
121 131
         cur += todo;
122 132
     }
123 133
 
124
-    if (!EVP_DigestFinal(&ctx, ret, &i)) {
134
+    if (!EVP_DigestFinal_ex(ctx, ret, &i)) {
125 135
         if (!(obuf))
126 136
             free(ret);
127 137
 
128 138
         if ((olen))
129 139
             *olen = 0;
130 140
 
141
+        EVP_MD_CTX_destroy(ctx);
131 142
         return NULL;
132 143
     }
133 144
 
134
-    EVP_MD_CTX_cleanup(&ctx);
145
+    EVP_MD_CTX_destroy(ctx);
135 146
 
136 147
     if ((olen))
137 148
         *olen = i;
... ...
@@ -141,7 +152,7 @@ unsigned char *cl_hash_data(char *alg, const void *buf, size_t len, unsigned cha
141 141
 
142 142
 unsigned char *cl_hash_file_fd(int fd, char *alg, unsigned int *olen)
143 143
 {
144
-    EVP_MD_CTX ctx;
144
+    EVP_MD_CTX *ctx;
145 145
     const EVP_MD *md;
146 146
     unsigned char *res;
147 147
 
... ...
@@ -149,11 +160,17 @@ unsigned char *cl_hash_file_fd(int fd, char *alg, unsigned int *olen)
149 149
     if (!(md))
150 150
         return NULL;
151 151
 
152
-    if (!EVP_DigestInit(&ctx, md)) {
152
+    ctx = EVP_MD_CTX_create();
153
+    if (!(ctx))
154
+        return NULL;
155
+
156
+    if (!EVP_DigestInit_ex(ctx, md, NULL)) {
157
+        EVP_MD_CTX_destroy(ctx);
153 158
         return NULL;
154 159
     }
155 160
 
156
-    res = cl_hash_file_fd_ctx(&ctx, fd, olen);
161
+    res = cl_hash_file_fd_ctx(ctx, fd, olen);
162
+    EVP_MD_CTX_destroy(ctx);
157 163
 
158 164
     return res;
159 165
 }
... ...
@@ -210,15 +227,13 @@ unsigned char *cl_hash_file_fd_ctx(EVP_MD_CTX *ctx, int fd, unsigned int *olen)
210 210
         }
211 211
     }
212 212
 
213
-    if (!EVP_DigestFinal(ctx, hash, &hashlen)) {
213
+    if (!EVP_DigestFinal_ex(ctx, hash, &hashlen)) {
214 214
         free(hash);
215 215
         free(buf);
216 216
 
217 217
         return NULL;
218 218
     }
219 219
 
220
-    EVP_MD_CTX_cleanup(ctx);
221
-
222 220
     if ((olen))
223 221
         *olen = hashlen;
224 222
 
... ...
@@ -244,7 +259,7 @@ unsigned char *cl_sha1(const void *buf, size_t len, unsigned char *obuf, unsigne
244 244
 
245 245
 int cl_verify_signature_hash(EVP_PKEY *pkey, char *alg, unsigned char *sig, unsigned int siglen, unsigned char *digest)
246 246
 {
247
-    EVP_MD_CTX ctx;
247
+    EVP_MD_CTX *ctx;
248 248
     const EVP_MD *md;
249 249
     size_t mdsz;
250 250
 
... ...
@@ -252,26 +267,34 @@ int cl_verify_signature_hash(EVP_PKEY *pkey, char *alg, unsigned char *sig, unsi
252 252
     if (!(md))
253 253
         return -1;
254 254
 
255
+    ctx = EVP_MD_CTX_create();
256
+    if (!(ctx))
257
+        return -1;
258
+
255 259
     mdsz = EVP_MD_size(md);
256 260
 
257
-    if (!EVP_VerifyInit(&ctx, md)) {
261
+    if (!EVP_VerifyInit_ex(ctx, md, NULL)) {
262
+        EVP_MD_CTX_destroy(ctx);
258 263
         return -1;
259 264
     }
260 265
 
261
-    if (!EVP_VerifyUpdate(&ctx, digest, mdsz)) {
266
+    if (!EVP_VerifyUpdate(ctx, digest, mdsz)) {
267
+        EVP_MD_CTX_destroy(ctx);
262 268
         return -1;
263 269
     }
264 270
 
265
-    if (EVP_VerifyFinal(&ctx, sig, siglen, pkey) != 0) {
271
+    if (EVP_VerifyFinal(ctx, sig, siglen, pkey) != 0) {
272
+        EVP_MD_CTX_destroy(ctx);
266 273
         return -1;
267 274
     }
268 275
 
276
+    EVP_MD_CTX_destroy(ctx);
269 277
     return 0;
270 278
 }
271 279
 
272 280
 int cl_verify_signature_fd(EVP_PKEY *pkey, char *alg, unsigned char *sig, unsigned int siglen, int fd)
273 281
 {
274
-    EVP_MD_CTX ctx;
282
+    EVP_MD_CTX *ctx;
275 283
     const EVP_MD *md;
276 284
     size_t mdsz;
277 285
     unsigned char *digest;
... ...
@@ -281,33 +304,45 @@ int cl_verify_signature_fd(EVP_PKEY *pkey, char *alg, unsigned char *sig, unsign
281 281
         return -1;
282 282
 
283 283
     md = EVP_get_digestbyname(alg);
284
-    if (!(md))
284
+    if (!(md)) {
285
+        free(digest);
285 286
         return -1;
287
+    }
286 288
 
287 289
     mdsz = EVP_MD_size(md);
288 290
 
289
-    if (!EVP_VerifyInit(&ctx, md)) {
291
+    ctx = EVP_MD_CTX_create();
292
+    if (!(ctx)) {
290 293
         free(digest);
291 294
         return -1;
292 295
     }
293 296
 
294
-    if (!EVP_VerifyUpdate(&ctx, digest, mdsz)) {
297
+    if (!EVP_VerifyInit_ex(ctx, md, NULL)) {
295 298
         free(digest);
299
+        EVP_MD_CTX_destroy(ctx);
296 300
         return -1;
297 301
     }
298 302
 
299
-    if (EVP_VerifyFinal(&ctx, sig, siglen, pkey) != 0) {
303
+    if (!EVP_VerifyUpdate(ctx, digest, mdsz)) {
300 304
         free(digest);
305
+        EVP_MD_CTX_destroy(ctx);
306
+        return -1;
307
+    }
308
+
309
+    if (EVP_VerifyFinal(ctx, sig, siglen, pkey) != 0) {
310
+        free(digest);
311
+        EVP_MD_CTX_destroy(ctx);
301 312
         return -1;
302 313
     }
303 314
 
304 315
     free(digest);
316
+    EVP_MD_CTX_destroy(ctx);
305 317
     return 0;
306 318
 }
307 319
 
308 320
 int cl_verify_signature(EVP_PKEY *pkey, char *alg, unsigned char *sig, unsigned int siglen, unsigned char *data, size_t datalen, int decode)
309 321
 {
310
-    EVP_MD_CTX ctx;
322
+    EVP_MD_CTX *ctx;
311 323
     const EVP_MD *md;
312 324
     size_t mdsz;
313 325
     unsigned char *digest;
... ...
@@ -343,27 +378,39 @@ int cl_verify_signature(EVP_PKEY *pkey, char *alg, unsigned char *sig, unsigned
343 343
 
344 344
     mdsz = EVP_MD_size(md);
345 345
 
346
-    if (!EVP_VerifyInit(&ctx, md)) {
346
+    ctx = EVP_MD_CTX_create();
347
+    if (!(ctx)) {
348
+        free(digest);
349
+        if (decode)
350
+            free(sig);
351
+
352
+        return -1;
353
+    }
354
+
355
+    if (!EVP_VerifyInit_ex(ctx, md, NULL)) {
347 356
         free(digest);
348 357
         if (decode)
349 358
             free(sig);
350 359
 
360
+        EVP_MD_CTX_destroy(ctx);
351 361
         return -1;
352 362
     }
353 363
 
354
-    if (!EVP_VerifyUpdate(&ctx, digest, mdsz)) {
364
+    if (!EVP_VerifyUpdate(ctx, digest, mdsz)) {
355 365
         free(digest);
356 366
         if (decode)
357 367
             free(sig);
358 368
 
369
+        EVP_MD_CTX_destroy(ctx);
359 370
         return -1;
360 371
     }
361 372
 
362
-    if (EVP_VerifyFinal(&ctx, sig, siglen, pkey) != 0) {
373
+    if (EVP_VerifyFinal(ctx, sig, siglen, pkey) != 0) {
363 374
         free(digest);
364 375
         if (decode)
365 376
             free(sig);
366 377
 
378
+        EVP_MD_CTX_destroy(ctx);
367 379
         return -1;
368 380
     }
369 381
 
... ...
@@ -371,6 +418,7 @@ int cl_verify_signature(EVP_PKEY *pkey, char *alg, unsigned char *sig, unsigned
371 371
         free(sig);
372 372
 
373 373
     free(digest);
374
+    EVP_MD_CTX_destroy(ctx);
374 375
     return 0;
375 376
 }
376 377
 
... ...
@@ -528,7 +576,7 @@ unsigned char *cl_sign_data_keyfile(char *keypath, char *alg, unsigned char *has
528 528
 
529 529
 unsigned char *cl_sign_data(EVP_PKEY *pkey, char *alg, unsigned char *hash, unsigned int *olen, int encode)
530 530
 {
531
-    EVP_MD_CTX ctx;
531
+    EVP_MD_CTX *ctx;
532 532
     const EVP_MD *md;
533 533
     unsigned int siglen;
534 534
     unsigned char *sig;
... ...
@@ -537,27 +585,35 @@ unsigned char *cl_sign_data(EVP_PKEY *pkey, char *alg, unsigned char *hash, unsi
537 537
     if (!(md))
538 538
         return NULL;
539 539
 
540
+    ctx = EVP_MD_CTX_create();
541
+    if (!(ctx))
542
+        return NULL;
543
+
540 544
     sig = (unsigned char *)calloc(1, EVP_PKEY_size(pkey));
541 545
     if (!(sig)) {
542 546
         free(hash);
547
+        EVP_MD_CTX_destroy(ctx);
543 548
         return NULL;
544 549
     }
545 550
 
546
-    if (!EVP_SignInit(&ctx, md)) {
551
+    if (!EVP_SignInit_ex(ctx, md, NULL)) {
547 552
         free(sig);
548 553
         free(hash);
554
+        EVP_MD_CTX_destroy(ctx);
549 555
         return NULL;
550 556
     }
551 557
 
552
-    if (!EVP_SignUpdate(&ctx, hash, EVP_MD_size(md))) {
558
+    if (!EVP_SignUpdate(ctx, hash, EVP_MD_size(md))) {
553 559
         free(sig);
554 560
         free(hash);
561
+        EVP_MD_CTX_destroy(ctx);
555 562
         return NULL;
556 563
     }
557 564
 
558
-    if (!EVP_SignFinal(&ctx, sig, &siglen, pkey)) {
565
+    if (!EVP_SignFinal(ctx, sig, &siglen, pkey)) {
559 566
         free(sig);
560 567
         free(hash);
568
+        EVP_MD_CTX_destroy(ctx);
561 569
         return NULL;
562 570
     }
563 571
 
... ...
@@ -566,6 +622,7 @@ unsigned char *cl_sign_data(EVP_PKEY *pkey, char *alg, unsigned char *hash, unsi
566 566
         if (!(newsig)) {
567 567
             free(sig);
568 568
             free(hash);
569
+            EVP_MD_CTX_destroy(ctx);
569 570
             return NULL;
570 571
         }
571 572
 
... ...
@@ -577,6 +634,7 @@ unsigned char *cl_sign_data(EVP_PKEY *pkey, char *alg, unsigned char *hash, unsi
577 577
     free(hash);
578 578
 
579 579
     *olen = siglen;
580
+    EVP_MD_CTX_destroy(ctx);
580 581
     return sig;
581 582
 }
582 583
 
... ...
@@ -326,7 +326,7 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
326 326
             cli_tgzload_cleanup(compr, dbio, fdd);
327 327
             return CL_EMALFDB;
328 328
         }
329
-        EVP_DigestInit(dbio->hashctx, EVP_sha256());
329
+        EVP_DigestInit_ex(dbio->hashctx, EVP_sha256(), NULL);
330 330
     }
331 331
 	dbio->bread = 0;
332 332
 
... ...
@@ -361,8 +361,8 @@ static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, un
361 361
 			cli_tgzload_cleanup(compr, dbio, fdd);
362 362
 			return CL_EMALFDB;
363 363
 		    }
364
-            EVP_DigestFinal(dbio->hashctx, hash, NULL);
365
-            EVP_DigestInit(dbio->hashctx, EVP_sha256());
364
+            EVP_DigestFinal_ex(dbio->hashctx, hash, NULL);
365
+            EVP_DigestInit_ex(dbio->hashctx, EVP_sha256(), NULL);
366 366
 		    if(memcmp(db->hash, hash, 32)) {
367 367
 			cli_errmsg("cli_tgzload: Invalid checksum for file %s\n", name);
368 368
 			cli_tgzload_cleanup(compr, dbio, fdd);
... ...
@@ -158,7 +158,7 @@ int cli_versig2(const unsigned char *sha256, const char *dsig_str, const char *n
158 158
 	unsigned char *decoded, digest1[HASH_LEN], digest2[HASH_LEN], digest3[HASH_LEN], *salt;
159 159
 	unsigned char mask[BLK_LEN], data[BLK_LEN], final[8 + 2 * HASH_LEN], c[4];
160 160
 	unsigned int i, rounds;
161
-    EVP_MD_CTX ctx;
161
+    EVP_MD_CTX *ctx;
162 162
 	mp_int n, e;
163 163
 
164 164
     mp_init(&e);
... ...
@@ -187,11 +187,15 @@ int cli_versig2(const unsigned char *sha256, const char *dsig_str, const char *n
187 187
 	c[2] = (unsigned char) (i / 256);
188 188
 	c[3] = (unsigned char) i;
189 189
 
190
-    EVP_DigestInit(&ctx, EVP_sha256());
191
-	EVP_DigestUpdate(&ctx, digest2, HASH_LEN);
192
-	EVP_DigestUpdate(&ctx, c, 4);
193
-	EVP_DigestFinal(&ctx, digest3, NULL);
194
-    EVP_MD_CTX_cleanup(&ctx);
190
+    ctx = EVP_MD_CTX_create();
191
+    if (!(ctx))
192
+        return CL_EMEM;
193
+
194
+    EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
195
+	EVP_DigestUpdate(ctx, digest2, HASH_LEN);
196
+	EVP_DigestUpdate(ctx, c, 4);
197
+	EVP_DigestFinal_ex(ctx, digest3, NULL);
198
+    EVP_MD_CTX_destroy(ctx);
195 199
 	if(i + 1 == rounds)
196 200
             memcpy(&data[i * 32], digest3, BLK_LEN - i * HASH_LEN);
197 201
 	else
... ...
@@ -213,10 +217,14 @@ int cli_versig2(const unsigned char *sha256, const char *dsig_str, const char *n
213 213
     memcpy(&final[8], sha256, HASH_LEN);
214 214
     memcpy(&final[8 + HASH_LEN], salt, SALT_LEN);
215 215
 
216
-    EVP_DigestInit(&ctx, EVP_sha256());
217
-	EVP_DigestUpdate(&ctx, final, sizeof(final));
218
-	EVP_DigestFinal(&ctx, digest1, NULL);
219
-    EVP_MD_CTX_cleanup(&ctx);
216
+    ctx = EVP_MD_CTX_create();
217
+    if (!(ctx))
218
+        return CL_EMEM;
219
+
220
+    EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
221
+	EVP_DigestUpdate(ctx, final, sizeof(final));
222
+	EVP_DigestFinal_ex(ctx, digest1, NULL);
223
+    EVP_MD_CTX_destroy(ctx);
220 224
 
221 225
     return memcmp(digest1, digest2, HASH_LEN) ? CL_EVERIFY : CL_SUCCESS;
222 226
 }
... ...
@@ -244,7 +244,7 @@ char *internal_get_host_id(void)
244 244
     unsigned char raw_md5[16];
245 245
     char *printable_md5;
246 246
     struct device *devices;
247
-    EVP_MD_CTX ctx;
247
+    EVP_MD_CTX *ctx;
248 248
 
249 249
     devices = get_devices();
250 250
     if (!(devices))
... ...
@@ -256,12 +256,23 @@ char *internal_get_host_id(void)
256 256
         return NULL;
257 257
     }
258 258
 
259
-    EVP_DigestInit(&ctx, EVP_md5());
259
+    ctx = EVP_MD_CTX_create();
260
+    if (!(ctx)) {
261
+        for (i=0; devices[i].name != NULL; i++)
262
+            free(devices[i].name);
263
+
264
+        free(devices);
265
+        free(printable_md5);
266
+
267
+        return NULL;
268
+    }
269
+
270
+    EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
260 271
     for (i=0; devices[i].name != NULL; i++)
261
-        EVP_DigestUpdate(&ctx, devices[i].mac, sizeof(devices[i].mac));
272
+        EVP_DigestUpdate(ctx, devices[i].mac, sizeof(devices[i].mac));
262 273
 
263
-    EVP_DigestFinal(&ctx, raw_md5, NULL);
264
-    EVP_MD_CTX_cleanup(&ctx);
274
+    EVP_DigestFinal_ex(ctx, raw_md5, NULL);
275
+    EVP_MD_CTX_destroy(ctx);
265 276
 
266 277
     for (i=0; devices[i].name != NULL; i++)
267 278
         free(devices[i].name);
... ...
@@ -718,22 +718,39 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
718 718
     const char *virname = NULL;
719 719
     uint32_t viroffset = 0;
720 720
     uint32_t viruses_found = 0;
721
-    EVP_MD_CTX md5ctx, sha1ctx, sha256ctx;
721
+    EVP_MD_CTX *md5ctx, *sha1ctx, *sha256ctx;
722 722
 
723 723
     if(!ctx->engine) {
724 724
         cli_errmsg("cli_scandesc: engine == NULL\n");
725 725
         return CL_ENULLARG;
726 726
     }
727 727
 
728
-    if (!EVP_DigestInit(&md5ctx, EVP_md5())) {
728
+    md5ctx = EVP_MD_CTX_create();
729
+    if (!(md5ctx))
730
+        return CL_EMEM;
731
+
732
+    sha1ctx = EVP_MD_CTX_create();
733
+    if (!(sha1ctx)) {
734
+        EVP_MD_CTX_destroy(md5ctx);
735
+        return CL_EMEM;
736
+    }
737
+
738
+    sha256ctx = EVP_MD_CTX_create();
739
+    if (!(sha256ctx)) {
740
+        EVP_MD_CTX_destroy(md5ctx);
741
+        EVP_MD_CTX_destroy(sha1ctx);
742
+        return CL_EMEM;
743
+    }
744
+
745
+    if (!EVP_DigestInit_ex(md5ctx, EVP_md5(), NULL)) {
729 746
         return CL_CLEAN;
730 747
     }
731 748
 
732
-    if (!EVP_DigestInit(&sha1ctx, EVP_sha1())) {
749
+    if (!EVP_DigestInit_ex(sha1ctx, EVP_sha1(), NULL)) {
733 750
         return CL_CLEAN;
734 751
     }
735 752
 
736
-    if (!EVP_DigestInit(&sha256ctx, EVP_sha256())) {
753
+    if (!EVP_DigestInit_ex(sha256ctx, EVP_sha256(), NULL)) {
737 754
         return CL_CLEAN;
738 755
     }
739 756
 
... ...
@@ -751,9 +768,9 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
751 751
 
752 752
     if(ftonly) {
753 753
         if(!troot) {
754
-            EVP_MD_CTX_cleanup(&md5ctx);
755
-            EVP_MD_CTX_cleanup(&sha1ctx);
756
-            EVP_MD_CTX_cleanup(&sha256ctx);
754
+            EVP_MD_CTX_destroy(md5ctx);
755
+            EVP_MD_CTX_destroy(sha1ctx);
756
+            EVP_MD_CTX_destroy(sha256ctx);
757 757
             return CL_CLEAN;
758 758
         }
759 759
 
... ...
@@ -773,9 +790,9 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
773 773
                 free(info.exeinfo.section);
774 774
 
775 775
             cli_hashset_destroy(&info.exeinfo.vinfo);
776
-            EVP_MD_CTX_cleanup(&md5ctx);
777
-            EVP_MD_CTX_cleanup(&sha1ctx);
778
-            EVP_MD_CTX_cleanup(&sha256ctx);
776
+            EVP_MD_CTX_destroy(md5ctx);
777
+            EVP_MD_CTX_destroy(sha1ctx);
778
+            EVP_MD_CTX_destroy(sha256ctx);
779 779
             return ret;
780 780
         }
781 781
     }
... ...
@@ -788,9 +805,9 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
788 788
                 free(info.exeinfo.section);
789 789
 
790 790
             cli_hashset_destroy(&info.exeinfo.vinfo);
791
-            EVP_MD_CTX_cleanup(&md5ctx);
792
-            EVP_MD_CTX_cleanup(&sha1ctx);
793
-            EVP_MD_CTX_cleanup(&sha256ctx);
791
+            EVP_MD_CTX_destroy(md5ctx);
792
+            EVP_MD_CTX_destroy(sha1ctx);
793
+            EVP_MD_CTX_destroy(sha256ctx);
794 794
             return ret;
795 795
         }
796 796
         if(troot->bm_offmode) {
... ...
@@ -804,9 +821,9 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
804 804
                         free(info.exeinfo.section);
805 805
 
806 806
                     cli_hashset_destroy(&info.exeinfo.vinfo);
807
-                    EVP_MD_CTX_cleanup(&md5ctx);
808
-                    EVP_MD_CTX_cleanup(&sha1ctx);
809
-                    EVP_MD_CTX_cleanup(&sha256ctx);
807
+                    EVP_MD_CTX_destroy(md5ctx);
808
+                    EVP_MD_CTX_destroy(sha1ctx);
809
+                    EVP_MD_CTX_destroy(sha256ctx);
810 810
                     return ret;
811 811
                 }
812 812
 
... ...
@@ -873,9 +890,9 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
873 873
                     free(info.exeinfo.section);
874 874
 
875 875
                 cli_hashset_destroy(&info.exeinfo.vinfo);
876
-                EVP_MD_CTX_cleanup(&md5ctx);
877
-                EVP_MD_CTX_cleanup(&sha1ctx);
878
-                EVP_MD_CTX_cleanup(&sha256ctx);
876
+                EVP_MD_CTX_destroy(md5ctx);
877
+                EVP_MD_CTX_destroy(sha1ctx);
878
+                EVP_MD_CTX_destroy(sha256ctx);
879 879
                 return ret;
880 880
             }
881 881
         }
... ...
@@ -912,11 +929,11 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
912 912
                 uint32_t data_len = bytes - maxpatlen * (offset!=0);
913 913
 
914 914
                 if(compute_hash[CLI_HASH_MD5])
915
-                    EVP_DigestUpdate(&md5ctx, data, data_len);
915
+                    EVP_DigestUpdate(md5ctx, data, data_len);
916 916
                 if(compute_hash[CLI_HASH_SHA1])
917
-                    EVP_DigestUpdate(&sha1ctx, data, data_len);
917
+                    EVP_DigestUpdate(sha1ctx, data, data_len);
918 918
                 if(compute_hash[CLI_HASH_SHA256])
919
-                    EVP_DigestUpdate(&sha256ctx, data, data_len);
919
+                    EVP_DigestUpdate(sha256ctx, data, data_len);
920 920
             }
921 921
         }
922 922
 
... ...
@@ -935,15 +952,15 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
935 935
         enum CLI_HASH_TYPE hashtype, hashtype2;
936 936
 
937 937
         if(compute_hash[CLI_HASH_MD5]) {
938
-            EVP_DigestFinal(&md5ctx, digest[CLI_HASH_MD5], NULL);
938
+            EVP_DigestFinal_ex(md5ctx, digest[CLI_HASH_MD5], NULL);
939 939
         }
940 940
         if(refhash)
941 941
             compute_hash[CLI_HASH_MD5] = 1;
942 942
         if(compute_hash[CLI_HASH_SHA1]) {
943
-            EVP_DigestFinal(&sha1ctx, digest[CLI_HASH_SHA1], NULL);
943
+            EVP_DigestFinal_ex(sha1ctx, digest[CLI_HASH_SHA1], NULL);
944 944
         }
945 945
         if(compute_hash[CLI_HASH_SHA256]) {
946
-            EVP_DigestFinal(&sha256ctx, digest[CLI_HASH_SHA256], NULL);
946
+            EVP_DigestFinal_ex(sha256ctx, digest[CLI_HASH_SHA256], NULL);
947 947
         }
948 948
 
949 949
         virname = NULL;
... ...
@@ -1001,9 +1018,9 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli
1001 1001
         }
1002 1002
     }
1003 1003
 
1004
-    EVP_MD_CTX_cleanup(&md5ctx);
1005
-    EVP_MD_CTX_cleanup(&sha1ctx);
1006
-    EVP_MD_CTX_cleanup(&sha256ctx);
1004
+    EVP_MD_CTX_destroy(md5ctx);
1005
+    EVP_MD_CTX_destroy(sha1ctx);
1006
+    EVP_MD_CTX_destroy(sha256ctx);
1007 1007
 
1008 1008
     if(troot) {
1009 1009
         if(ret != CL_VIRUS || SCAN_ALL)
... ...
@@ -881,20 +881,24 @@ char *cli_hashstream(FILE *fs, unsigned char *digcpy, int type)
881 881
     char buff[FILEBUFF];
882 882
     char *hashstr, *pt;
883 883
     int i, bytes, size;
884
-    EVP_MD_CTX ctx;
884
+    EVP_MD_CTX *ctx;
885
+
886
+    ctx = EVP_MD_CTX_create();
887
+    if (!(ctx))
888
+        return NULL;
885 889
 
886 890
     if(type == 1)
887
-        EVP_DigestInit(&ctx, EVP_md5());
891
+        EVP_DigestInit_ex(ctx, EVP_md5(), NULL);
888 892
     else if(type == 2)
889
-        EVP_DigestInit(&ctx, EVP_sha1());
893
+        EVP_DigestInit_ex(ctx, EVP_sha1(), NULL);
890 894
     else
891
-        EVP_DigestInit(&ctx, EVP_sha256());
895
+        EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
892 896
 
893 897
     while((bytes = fread(buff, 1, FILEBUFF, fs)))
894
-        EVP_DigestUpdate(&ctx, buff, bytes);
898
+        EVP_DigestUpdate(ctx, buff, bytes);
895 899
 
896
-    EVP_DigestFinal(&ctx, digest, &size);
897
-    EVP_MD_CTX_cleanup(&ctx);
900
+    EVP_DigestFinal_ex(ctx, digest, &size);
901
+    EVP_MD_CTX_destroy(ctx);
898 902
 
899 903
     if(!(hashstr = (char *) cli_calloc(size*2 + 1, sizeof(char))))
900 904
         return NULL;
... ...
@@ -2800,7 +2800,7 @@ int cli_checkfp_pe(cli_ctx *ctx, uint8_t *authsha1, stats_section_t *hashes, uin
2800 2800
     struct cli_exe_section *exe_sections;
2801 2801
     struct pe_image_data_dir *dirs;
2802 2802
     fmap_t *map = *ctx->fmap;
2803
-    EVP_MD_CTX hashctx;
2803
+    EVP_MD_CTX *hashctx=NULL;
2804 2804
 
2805 2805
     if (flags & CL_CHECKFP_PE_FLAG_STATS)
2806 2806
         if (!(hashes))
... ...
@@ -2927,7 +2927,13 @@ int cli_checkfp_pe(cli_ctx *ctx, uint8_t *authsha1, stats_section_t *hashes, uin
2927 2927
     }
2928 2928
 
2929 2929
     cli_qsort(exe_sections, nsections, sizeof(*exe_sections), sort_sects);
2930
-    EVP_DigestInit(&hashctx, EVP_sha1());
2930
+    hashctx = EVP_MD_CTX_create();
2931
+    if (!(hashctx)) {
2932
+        if (flags & CL_CHECKFP_PE_FLAG_AUTHENTICODE)
2933
+            flags ^= CL_CHECKFP_PE_FLAG_AUTHENTICODE;
2934
+    }
2935
+    if (hashctx)
2936
+        EVP_DigestInit_ex(hashctx, EVP_sha1(), NULL);
2931 2937
 
2932 2938
     if (flags & CL_CHECKFP_PE_FLAG_AUTHENTICODE) {
2933 2939
         /* Check to see if we have a security section. */
... ...
@@ -2936,7 +2942,8 @@ int cli_checkfp_pe(cli_ctx *ctx, uint8_t *authsha1, stats_section_t *hashes, uin
2936 2936
                 /* If stats is enabled, continue parsing the sample */
2937 2937
                 flags ^= CL_CHECKFP_PE_FLAG_AUTHENTICODE;
2938 2938
             } else {
2939
-                EVP_MD_CTX_cleanup(&hashctx);
2939
+                if (hashctx)
2940
+                    EVP_MD_CTX_destroy(hashctx);
2940 2941
                 return CL_BREAK;
2941 2942
             }
2942 2943
         }
... ...
@@ -2948,16 +2955,21 @@ int cli_checkfp_pe(cli_ctx *ctx, uint8_t *authsha1, stats_section_t *hashes, uin
2948 2948
         if(!(size)) break; \
2949 2949
         if(!(hptr = fmap_need_off_once(map, where, size))){ \
2950 2950
             free(exe_sections); \
2951
+            if (hashctx) \
2952
+                EVP_MD_CTX_destroy(hashctx); \
2951 2953
             return CL_EFORMAT; \
2952 2954
         } \
2953
-        if (flags & CL_CHECKFP_PE_FLAG_AUTHENTICODE) \
2954
-            EVP_DigestUpdate(&hashctx, hptr, size); \
2955
+        if (flags & CL_CHECKFP_PE_FLAG_AUTHENTICODE && hashctx) \
2956
+            EVP_DigestUpdate(hashctx, hptr, size); \
2955 2957
         if (isStatAble && flags & CL_CHECKFP_PE_FLAG_STATS) { \
2956
-            EVP_MD_CTX md5ctx; \
2957
-            EVP_DigestInit(&md5ctx, EVP_md5()); \
2958
-            EVP_DigestUpdate(&md5ctx, hptr, size); \
2959
-            EVP_DigestFinal(&md5ctx, hashes->sections[section].md5, NULL); \
2960
-            EVP_MD_CTX_cleanup(&md5ctx); \
2958
+            EVP_MD_CTX *md5ctx; \
2959
+            md5ctx = EVP_MD_CTX_create(); \
2960
+            if (md5ctx) { \
2961
+                EVP_DigestInit_ex(md5ctx, EVP_md5(), NULL); \
2962
+                EVP_DigestUpdate(md5ctx, hptr, size); \
2963
+                EVP_DigestFinal_ex(md5ctx, hashes->sections[section].md5, NULL); \
2964
+                EVP_MD_CTX_destroy(md5ctx); \
2965
+            } \
2961 2966
         } \
2962 2967
     } while(0)
2963 2968
 
... ...
@@ -2982,7 +2994,8 @@ int cli_checkfp_pe(cli_ctx *ctx, uint8_t *authsha1, stats_section_t *hashes, uin
2982 2982
                 break;
2983 2983
             } else {
2984 2984
                 free(exe_sections);
2985
-                EVP_MD_CTX_cleanup(&hashctx);
2985
+                if (hashctx)
2986
+                    EVP_MD_CTX_destroy(hashctx);
2986 2987
                 return CL_EFORMAT;
2987 2988
             }
2988 2989
         }
... ...
@@ -3014,7 +3027,8 @@ int cli_checkfp_pe(cli_ctx *ctx, uint8_t *authsha1, stats_section_t *hashes, uin
3014 3014
                     break;
3015 3015
                 } else {
3016 3016
                     free(exe_sections);
3017
-                    EVP_MD_CTX_cleanup(&hashctx);
3017
+                    if (hashctx)
3018
+                        EVP_MD_CTX_destroy(hashctx);
3018 3019
                     return CL_EFORMAT;
3019 3020
                 }
3020 3021
             }
... ...
@@ -3029,8 +3043,8 @@ int cli_checkfp_pe(cli_ctx *ctx, uint8_t *authsha1, stats_section_t *hashes, uin
3029 3029
 
3030 3030
     free(exe_sections);
3031 3031
 
3032
-    if (flags & CL_CHECKFP_PE_FLAG_AUTHENTICODE) {
3033
-        EVP_DigestFinal(&hashctx, authsha1, NULL);
3032
+    if (flags & CL_CHECKFP_PE_FLAG_AUTHENTICODE && hashctx) {
3033
+        EVP_DigestFinal_ex(hashctx, authsha1, NULL);
3034 3034
 
3035 3035
         if(cli_debug_flag) {
3036 3036
             char shatxt[SHA1_HASH_SIZE*2+1];
... ...
@@ -3045,10 +3059,11 @@ int cli_checkfp_pe(cli_ctx *ctx, uint8_t *authsha1, stats_section_t *hashes, uin
3045 3045
 
3046 3046
         hlen -= 8;
3047 3047
 
3048
-        EVP_MD_CTX_cleanup(&hashctx);
3048
+        EVP_MD_CTX_destroy(hashctx);
3049 3049
         return asn1_check_mscat((struct cl_engine *)(ctx->engine), map, at + 8, hlen, authsha1);
3050 3050
     } else {
3051
-        EVP_MD_CTX_cleanup(&hashctx);
3051
+        if (hashctx)
3052
+            EVP_MD_CTX_destroy(hashctx);
3052 3053
         return CL_VIRUS;
3053 3054
     }
3054 3055
 }
... ...
@@ -1202,13 +1202,17 @@ static int hash_match(const struct regex_matcher *rlist, const char *host, size_
1202 1202
 	    unsigned char h[65];
1203 1203
 	    unsigned char sha256_dig[32];
1204 1204
 	    unsigned i;
1205
-        EVP_MD_CTX sha256;
1205
+        EVP_MD_CTX *sha256;
1206 1206
 
1207
-        EVP_DigestInit(&sha256, EVP_sha256());
1208
-        EVP_DigestUpdate(&sha256, host, hlen);
1209
-        EVP_DigestUpdate(&sha256, path, plen);
1210
-        EVP_DigestFinal(&sha256, sha256_dig, NULL);
1211
-        EVP_MD_CTX_cleanup(&sha256);
1207
+        sha256 = EVP_MD_CTX_create();
1208
+        if (!(sha256))
1209
+            return CL_EMEM;
1210
+
1211
+        EVP_DigestInit_ex(sha256, EVP_sha256(), NULL);
1212
+        EVP_DigestUpdate(sha256, host, hlen);
1213
+        EVP_DigestUpdate(sha256, path, plen);
1214
+        EVP_DigestFinal_ex(sha256, sha256_dig, NULL);
1215
+        EVP_MD_CTX_destroy(sha256);
1212 1216
 
1213 1217
 	    for(i=0;i<32;i++) {
1214 1218
 		h[2*i] = hexchars[sha256_dig[i]>>4];
... ...
@@ -1745,13 +1745,13 @@ static int cli_loadinfo(FILE *fs, struct cl_engine *engine, unsigned int options
1745 1745
     if (!(ctx))
1746 1746
         return CL_EMALFDB;
1747 1747
 
1748
-    EVP_DigestInit(ctx, EVP_sha256());
1748
+    EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
1749 1749
 
1750 1750
     while(cli_dbgets(buffer, FILEBUFF, fs, dbio)) {
1751 1751
 	line++;
1752 1752
 	if(!(options & CL_DB_UNSIGNED) && !strncmp(buffer, "DSIG:", 5)) {
1753 1753
 	    dsig = 1;
1754
-	    EVP_DigestFinal(ctx, hash, NULL);
1754
+	    EVP_DigestFinal_ex(ctx, hash, NULL);
1755 1755
         EVP_MD_CTX_destroy(ctx);
1756 1756
 	    if(cli_versig2(hash, buffer + 5, INFO_NSTR, INFO_ESTR) != CL_SUCCESS) {
1757 1757
 		cli_errmsg("cli_loadinfo: Incorrect digital signature\n");
... ...
@@ -325,17 +325,27 @@ static int xar_scan_subdocuments(xmlTextReaderPtr reader, cli_ctx *ctx)
325 325
     return rc;
326 326
 }
327 327
 
328
-static EVP_MD_CTX * xar_hash_init(int hash, EVP_MD_CTX *sc, EVP_MD_CTX *mc)
328
+static EVP_MD_CTX * xar_hash_init(int hash, EVP_MD_CTX **sc, EVP_MD_CTX **mc)
329 329
 {
330 330
     if (!sc && !mc)
331 331
         return NULL;
332 332
     switch (hash) {
333 333
     case XAR_CKSUM_SHA1:
334
-        EVP_DigestInit(sc, EVP_sha1());
335
-        return sc;
334
+        *sc = EVP_MD_CTX_create();
335
+        if (!(*sc)) {
336
+            return NULL;
337
+        }
338
+
339
+        EVP_DigestInit_ex(*sc, EVP_sha1(), NULL);
340
+        return *sc;
336 341
     case XAR_CKSUM_MD5:
337
-        EVP_DigestInit(mc, EVP_md5());
338
-        return mc;
342
+        *mc = EVP_MD_CTX_create();
343
+        if (!(*mc)) {
344
+            return NULL;
345
+        }
346
+
347
+        EVP_DigestInit_ex(*mc, EVP_md5(), NULL);
348
+        return *mc;
339 349
     case XAR_CKSUM_OTHER:
340 350
     case XAR_CKSUM_NONE:
341 351
     default:
... ...
@@ -369,8 +379,8 @@ static void xar_hash_final(EVP_MD_CTX * hash_ctx, void * result, int hash)
369 369
         return;
370 370
     }
371 371
 
372
-    EVP_DigestFinal(hash_ctx, result, NULL);
373
-    EVP_MD_CTX_cleanup(hash_ctx);
372
+    EVP_DigestFinal_ex(hash_ctx, result, NULL);
373
+    EVP_MD_CTX_destroy(hash_ctx);
374 374
 }
375 375
 
376 376
 static int xar_hash_check(int hash, const void * result, const void * expected)
... ...
@@ -530,8 +540,8 @@ int cli_scanxar(cli_ctx *ctx)
530 530
                                                        &a_cksum, &a_hash, &e_cksum, &e_hash))) {
531 531
         int do_extract_cksum = 1;
532 532
         unsigned char * blockp;
533
-        EVP_MD_CTX a_sc, e_sc;
534
-        EVP_MD_CTX a_mc, e_mc;
533
+        EVP_MD_CTX *a_sc, *e_sc;
534
+        EVP_MD_CTX *a_mc, *e_mc;
535 535
         EVP_MD_CTX *a_hash_ctx, *e_hash_ctx;
536 536
         char result[SHA1_HASH_SIZE];
537 537
         char * expected;
... ...
@@ -767,7 +767,7 @@ int cdiff_apply(int fd, unsigned short mode)
767 767
 	int end, i, n;
768 768
 	struct stat sb;
769 769
 	int desc;
770
-	EVP_MD_CTX sha256ctx;
770
+	EVP_MD_CTX *sha256ctx;
771 771
 	unsigned char digest[32];
772 772
 	int sum, bread;
773 773
 #define DSIGBUFF 350
... ...
@@ -851,18 +851,27 @@ int cdiff_apply(int fd, unsigned short mode)
851 851
 	    return -1;
852 852
 	}
853 853
 
854
-	EVP_DigestInit(&sha256ctx, EVP_sha256());
854
+    sha256ctx = EVP_MD_CTX_create();
855
+    if (!(sha256ctx)) {
856
+        close(desc);
857
+        free(line);
858
+        free(lbuf);
859
+        return -1;
860
+    }
861
+
862
+	EVP_DigestInit_ex(sha256ctx, EVP_sha256(), NULL);
855 863
 	sum = 0;
856 864
 	while((bread = read(desc, buff, FILEBUFF)) > 0) {
857 865
 	    if(sum + bread >= end) {
858
-		EVP_DigestUpdate(&sha256ctx, (unsigned char *) buff, end - sum);
866
+		EVP_DigestUpdate(sha256ctx, (unsigned char *) buff, end - sum);
859 867
 		break;
860 868
 	    } else {
861
-		EVP_DigestUpdate(&sha256ctx, (unsigned char *) buff, bread);
869
+		EVP_DigestUpdate(sha256ctx, (unsigned char *) buff, bread);
862 870
 	    }
863 871
 	    sum += bread;
864 872
 	}
865
-	EVP_DigestFinal(&sha256ctx, digest, NULL);
873
+	EVP_DigestFinal_ex(sha256ctx, digest, NULL);
874
+    EVP_MD_CTX_destroy(sha256ctx);
866 875
 
867 876
 	if(cli_versig2(digest, dsig, PSS_NSTR, PSS_ESTR) != CL_SUCCESS) {
868 877
 	    logg("!cdiff_apply: Incorrect digital signature\n");
... ...
@@ -400,22 +400,27 @@ static char *sha256file(const char *file, unsigned int *size)
400 400
 	unsigned int i, bytes;
401 401
 	unsigned char digest[32], buffer[FILEBUFF];
402 402
 	char *sha;
403
-	EVP_MD_CTX ctx;
403
+	EVP_MD_CTX *ctx;
404 404
 
405
+    ctx = EVP_MD_CTX_create();
406
+    if (!(ctx))
407
+        return NULL;
405 408
 
406
-    EVP_DigestInit(&ctx, EVP_sha256());
409
+    EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
407 410
     if(!(fh = fopen(file, "rb"))) {
408 411
 	mprintf("!sha256file: Can't open file %s\n", file);
412
+    EVP_MD_CTX_destroy(ctx);
409 413
 	return NULL;
410 414
     }
411 415
     if(size)
412 416
 	*size = 0;
413 417
     while((bytes = fread(buffer, 1, sizeof(buffer), fh))) {
414
-	EVP_DigestUpdate(&ctx, buffer, bytes);
418
+	EVP_DigestUpdate(ctx, buffer, bytes);
415 419
 	if(size)
416 420
 	    *size += bytes;
417 421
     }
418
-    EVP_DigestFinal(&ctx, digest, NULL);
422
+    EVP_DigestFinal_ex(ctx, digest, NULL);
423
+    EVP_MD_CTX_destroy(ctx);
419 424
     sha = (char *) malloc(65);
420 425
     if(!sha)
421 426
     {
... ...
@@ -435,7 +440,7 @@ static int writeinfo(const char *dbname, const char *builder, const char *header
435 435
 	unsigned int i, bytes;
436 436
 	char file[32], *pt, dbfile[32];
437 437
 	unsigned char digest[32], buffer[FILEBUFF];
438
-	EVP_MD_CTX ctx;
438
+	EVP_MD_CTX *ctx;
439 439
 
440 440
     snprintf(file, sizeof(file), "%s.info", dbname);
441 441
     if(!access(file, R_OK)) {
... ...
@@ -492,10 +497,17 @@ static int writeinfo(const char *dbname, const char *builder, const char *header
492 492
     }
493 493
     if(!optget(opts, "unsigned")->enabled) {
494 494
 	rewind(fh);
495
-	EVP_DigestInit(&ctx, EVP_sha256());
495
+    ctx = EVP_MD_CTX_create();
496
+    if (!(ctx)) {
497
+        fclose(fh);
498
+        return -1;
499
+    }
500
+
501
+	EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
496 502
 	while((bytes = fread(buffer, 1, sizeof(buffer), fh)))
497
-	    EVP_DigestUpdate(&ctx, buffer, bytes);
498
-	EVP_DigestFinal(&ctx, digest, NULL);
503
+	    EVP_DigestUpdate(ctx, buffer, bytes);
504
+	EVP_DigestFinal_ex(ctx, digest, NULL);
505
+    EVP_MD_CTX_destroy(ctx);
499 506
 	if(!(pt = getdsig(optget(opts, "server")->strarg, builder, digest, 32, 3))) {
500 507
 	    mprintf("!writeinfo: Can't get digital signature from remote server\n");
501 508
 	    fclose(fh);
... ...
@@ -515,7 +527,7 @@ static int script2cdiff(const char *script, const char *builder, const struct op
515 515
 {
516 516
 	char *cdiff, *pt, buffer[FILEBUFF];
517 517
 	unsigned char digest[32];
518
-	EVP_MD_CTX ctx;
518
+	EVP_MD_CTX *ctx;
519 519
 	STATBUF sb;
520 520
 	FILE *scripth, *cdiffh;
521 521
 	gzFile gzh;
... ...
@@ -599,13 +611,20 @@ static int script2cdiff(const char *script, const char *builder, const struct op
599 599
 	return -1;
600 600
     }
601 601
 
602
-    EVP_DigestInit(&ctx, EVP_sha256());
602
+    ctx = EVP_MD_CTX_create();
603
+    if (!(ctx)) {
604
+        unlink(cdiff);
605
+        free(cdiff);
606
+        return -1;
607
+    }
608
+    EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
603 609
 
604 610
     while((bytes = fread(buffer, 1, sizeof(buffer), cdiffh)))
605
-	EVP_DigestUpdate(&ctx, (unsigned char *) buffer, bytes);
611
+	EVP_DigestUpdate(ctx, (unsigned char *) buffer, bytes);
606 612
 
607 613
     fclose(cdiffh);
608
-    EVP_DigestFinal(&ctx, digest, NULL);
614
+    EVP_DigestFinal_ex(ctx, digest, NULL);
615
+    EVP_MD_CTX_destroy(ctx);
609 616
 
610 617
     if(!(pt = getdsig(optget(opts, "server")->strarg, builder, digest, 32, 2))) {
611 618
 	mprintf("!script2cdiff: Can't get digital signature from remote server\n");
... ...
@@ -794,7 +794,7 @@ static uint8_t res256[3][SHA256_HASH_SIZE] = {
794 794
 
795 795
 START_TEST (test_sha256)
796 796
 {
797
-    EVP_MD_CTX sha256;
797
+    EVP_MD_CTX *sha256;
798 798
     uint8_t hsha256[SHA256_HASH_SIZE];
799 799
     uint8_t buf[1000];
800 800
     int i;
... ...
@@ -807,10 +807,14 @@ START_TEST (test_sha256)
807 807
     cl_sha256(tv2, sizeof(tv2), hsha256, NULL);
808 808
     fail_unless(!memcmp (hsha256, res256[1], sizeof (hsha256)), "sha256 test vector #2 failed");
809 809
 
810
-    EVP_DigestInit (&sha256, EVP_sha256());
810
+    sha256 = EVP_MD_CTX_create();
811
+    fail_unless(sha256 != NULL, "Could not create EVP_MD_CTX for sha256");
812
+
813
+    EVP_DigestInit_ex(sha256, EVP_sha256(), NULL);
811 814
     for (i = 0; i < 1000; i++)
812
-        EVP_DigestUpdate (&sha256, buf, sizeof (buf));
813
-    EVP_DigestFinal (&sha256, hsha256, NULL);
815
+        EVP_DigestUpdate (sha256, buf, sizeof (buf));
816
+    EVP_DigestFinal_ex(sha256, hsha256, NULL);
817
+    EVP_MD_CTX_destroy(sha256);
814 818
     fail_unless(!memcmp (hsha256, res256[2], sizeof (hsha256)), "sha256 test vector #3 failed");
815 819
 }
816 820
 END_TEST