Use BIO_f_md instead of EVP_MD_CTX to compute a message digest value of the MSI file

This commit is contained in:
olszomal 2023-02-07 10:17:45 +01:00 committed by Michał Trojnara
parent bde67ec1e2
commit ff8034af2e

47
msi.c
View File

@ -735,37 +735,30 @@ out:
/* Compute a simple sha1/sha256 message digest of the MSI file */ /* Compute a simple sha1/sha256 message digest of the MSI file */
int msi_calc_digest(char *indata, int mdtype, u_char *mdbuf, uint32_t fileend) int msi_calc_digest(char *indata, int mdtype, u_char *mdbuf, uint32_t fileend)
{ {
uint32_t n; uint32_t idx = 0, offset;
int ret = 0; size_t written;
const EVP_MD *md = EVP_get_digestbynid(mdtype); const EVP_MD *md = EVP_get_digestbynid(mdtype);
BIO *bio = BIO_new_mem_buf(indata, (int)fileend); BIO *bhash = BIO_new(BIO_f_md());
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
if (!EVP_DigestInit(mdctx, md)) { if (!BIO_set_md(bhash, md)) {
printf("Unable to set up the digest context\n"); printf("Unable to set the message digest of BIO\n");
goto out; BIO_free_all(bhash);
return 0; /* FAILED */
} }
(void)BIO_seek(bio, 0); BIO_push(bhash, BIO_new(BIO_s_null()));
offset = fileend;
n = 0; while (idx < offset) {
while (n < fileend) { uint32_t want = offset - idx;
int l; if (want > SIZE_64K)
static u_char bfb[16*1024*1024]; want = SIZE_64K;
uint32_t want = fileend - n; if (!BIO_write_ex(bhash, indata + idx, want, &written)) {
if (want > sizeof bfb) BIO_free_all(bhash);
want = sizeof bfb; return 0; /* FAILED */
l = BIO_read(bio, bfb, (int)want); }
if (l <= 0) idx += (uint32_t)written;
break;
EVP_DigestUpdate(mdctx, bfb, (size_t)l);
n += (uint32_t)l;
} }
EVP_DigestFinal(mdctx, mdbuf, NULL); BIO_gets(bhash, mdbuf, EVP_MD_size(md));
ret = 1; /* OK */ return 1; /* OK */
out:
EVP_MD_CTX_free(mdctx);
BIO_free(bio);
return ret;
} }
static void ministream_append(MSI_OUT *out, char *buf, uint32_t len) static void ministream_append(MSI_OUT *out, char *buf, uint32_t len)