@joebigelow / wix-1 / commits / 3bbf1347

Add SHA512 support to apuputil.

#3992

Sean Hall committed Mar 2, 2021 at 14:18 UTC 3bbf1347b900ec115a12faf8f46965c9b7649696
3 files changed +29 -10
src/dutil/apuputil.cpp
+25 -10
@@ -2,13 +2,11 @@
2
3 #include "precomp.h"
4
5 -#define SHA256_DIGEST_LEN 32
6 -
5 // prototypes
6 static HRESULT ProcessEntry(
7 __in ATOM_ENTRY* pAtomEntry,
8 __in LPCWSTR wzDefaultAppId,
11 - __out APPLICATION_UPDATE_ENTRY* pApupEntry
9 + __inout APPLICATION_UPDATE_ENTRY* pApupEntry
10 );
11 static HRESULT ParseEnclosure(
12 __in ATOM_LINK* pLink,
@@ -192,7 +190,7 @@ extern "C" void DAPI ApupFreeChain(
190 static HRESULT ProcessEntry(
191 __in ATOM_ENTRY* pAtomEntry,
192 __in LPCWSTR wzDefaultAppId,
195 - __out APPLICATION_UPDATE_ENTRY* pApupEntry
193 + __inout APPLICATION_UPDATE_ENTRY* pApupEntry
194 )
195 {
196 HRESULT hr = S_OK;
@@ -325,6 +323,9 @@ static HRESULT ParseEnclosure(
323 )
324 {
325 HRESULT hr = S_OK;
326 + DWORD dwDigestLength = 0;
327 + DWORD dwDigestStringLength = 0;
328 + size_t cchDigestString = 0;
329
330 // First search the ATOM link's custom elements to try and find the application update enclosure information.
331 for (ATOM_UNKNOWN_ELEMENT* pElement = pLink->pUnknownElements; pElement; pElement = pElement->pNext)
@@ -333,36 +334,50 @@ static HRESULT ParseEnclosure(
334 {
335 if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, L"digest", -1, pElement->wzElement, -1))
336 {
336 - // Find the digest[@algorithm='sha256'] which is required. Everything else is ignored.
337 + // Find the digest[@algorithm] which is required. Everything else is ignored.
338 for (ATOM_UNKNOWN_ATTRIBUTE* pAttribute = pElement->pAttributes; pAttribute; pAttribute = pAttribute->pNext)
339 {
340 + dwDigestLength = 0;
341 if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, L"algorithm", -1, pAttribute->wzAttribute, -1))
342 {
343 if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, L"md5", -1, pAttribute->wzValue, -1))
344 {
345 pEnclosure->digestAlgorithm = APUP_HASH_ALGORITHM_MD5;
346 + dwDigestLength = MD5_HASH_LEN;
347 }
348 else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, L"sha1", -1, pAttribute->wzValue, -1))
349 {
350 pEnclosure->digestAlgorithm = APUP_HASH_ALGORITHM_SHA1;
351 + dwDigestLength = SHA1_HASH_LEN;
352 }
349 - if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, L"sha256", -1, pAttribute->wzValue, -1))
353 + else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, L"sha256", -1, pAttribute->wzValue, -1))
354 {
355 pEnclosure->digestAlgorithm = APUP_HASH_ALGORITHM_SHA256;
356 + dwDigestLength = SHA256_HASH_LEN;
357 + }
358 + else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, L"sha512", -1, pAttribute->wzValue, -1))
359 + {
360 + pEnclosure->digestAlgorithm = APUP_HASH_ALGORITHM_SHA512;
361 + dwDigestLength = SHA512_HASH_LEN;
362 }
363 break;
364 }
365 }
366
357 - if (APUP_HASH_ALGORITHM_SHA256 == pEnclosure->digestAlgorithm)
367 + if (dwDigestLength)
368 {
359 - if (64 != lstrlenW(pElement->wzValue))
369 + dwDigestStringLength = 2 * dwDigestLength;
370 +
371 + hr = ::StringCchLengthW(pElement->wzValue, STRSAFE_MAX_CCH, &cchDigestString);
372 + ExitOnFailure(hr, "Failed to get string length of digest value.");
373 +
374 + if (dwDigestStringLength != cchDigestString)
375 {
376 hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
362 - ExitOnRootFailure(hr, "Invalid digest length for SHA256 algorithm.");
377 + ExitOnRootFailure(hr, "Invalid digest length (%zu) for digest algorithm (%u).", cchDigestString, dwDigestStringLength);
378 }
379
365 - pEnclosure->cbDigest = sizeof(BYTE) * SHA256_DIGEST_LEN;
380 + pEnclosure->cbDigest = sizeof(BYTE) * dwDigestLength;
381 pEnclosure->rgbDigest = static_cast<BYTE*>(MemAlloc(pEnclosure->cbDigest, TRUE));
382 ExitOnNull(pEnclosure->rgbDigest, hr, E_OUTOFMEMORY, "Failed to allocate memory for digest.");
383
src/dutil/inc/apuputil.h
+1
@@ -18,6 +18,7 @@ typedef enum APUP_HASH_ALGORITHM
18 APUP_HASH_ALGORITHM_MD5,
19 APUP_HASH_ALGORITHM_SHA1,
20 APUP_HASH_ALGORITHM_SHA256,
21 + APUP_HASH_ALGORITHM_SHA512,
22 } APUP_HASH_ALGORITHM;
23
24
src/dutil/inc/cryputil.h
+3
@@ -11,7 +11,10 @@ extern "C" {
11
12 // Use CRYPTPROTECTMEMORY_BLOCK_SIZE, because it's larger and thus more restrictive than RTL_ENCRYPT_MEMORY_SIZE.
13 #define CRYP_ENCRYPT_MEMORY_SIZE CRYPTPROTECTMEMORY_BLOCK_SIZE
14 +#define MD5_HASH_LEN 16
15 #define SHA1_HASH_LEN 20
16 +#define SHA256_HASH_LEN 32
17 +#define SHA512_HASH_LEN 64
18
19 typedef NTSTATUS (APIENTRY *PFN_RTLENCRYPTMEMORY)(
20 __inout PVOID Memory,