aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Hall <r.sean.hall@gmail.com>2020-07-17 21:48:30 +1000
committerSean Hall <r.sean.hall@gmail.com>2020-07-17 21:52:36 +1000
commit6d763d9c86405644cc72530ad64978efd6ba5828 (patch)
treec22e036f02330d7bd3d7bf542a9eae79252cd8a6
parente845e7c68ae792ab2ccd4ca2f054700dde624375 (diff)
downloadwix-6d763d9c86405644cc72530ad64978efd6ba5828.tar.gz
wix-6d763d9c86405644cc72530ad64978efd6ba5828.tar.bz2
wix-6d763d9c86405644cc72530ad64978efd6ba5828.zip
Only support encrypting string variants.
-rw-r--r--src/engine/variant.cpp140
-rw-r--r--src/engine/variant.h3
-rw-r--r--src/test/BurnUnitTest/VariantTest.cpp8
3 files changed, 32 insertions, 119 deletions
diff --git a/src/engine/variant.cpp b/src/engine/variant.cpp
index 2a9f08ed..5e9bd29a 100644
--- a/src/engine/variant.cpp
+++ b/src/engine/variant.cpp
@@ -6,22 +6,12 @@
6 6
7// internal function declarations 7// internal function declarations
8 8
9static HRESULT BVariantEncryptNumeric(
10 __in BURN_VARIANT* pVariant,
11 __in BOOL fEncrypt
12 );
13
14static HRESULT BVariantEncryptString( 9static HRESULT BVariantEncryptString(
15 __in BURN_VARIANT* pVariant, 10 __in BURN_VARIANT* pVariant,
16 __in BOOL fEncrypt 11 __in BOOL fEncrypt
17 ); 12 );
18 13
19static HRESULT BVariantEncryptVersion( 14static void BVariantRetrieveNumeric(
20 __in BURN_VARIANT* pVariant,
21 __in BOOL fEncrypt
22 );
23
24static HRESULT BVariantRetrieveDecryptedNumeric(
25 __in BURN_VARIANT* pVariant, 15 __in BURN_VARIANT* pVariant,
26 __out LONGLONG* pllValue 16 __out LONGLONG* pllValue
27 ); 17 );
@@ -31,7 +21,7 @@ static HRESULT BVariantRetrieveDecryptedString(
31 __out LPWSTR* psczValue 21 __out LPWSTR* psczValue
32 ); 22 );
33 23
34static HRESULT BVariantRetrieveDecryptedVersion( 24static void BVariantRetrieveVersion(
35 __in BURN_VARIANT* pVariant, 25 __in BURN_VARIANT* pVariant,
36 __out DWORD64* pqwValue 26 __out DWORD64* pqwValue
37 ); 27 );
@@ -61,7 +51,7 @@ extern "C" HRESULT BVariantGetNumeric(
61 switch (pVariant->Type) 51 switch (pVariant->Type)
62 { 52 {
63 case BURN_VARIANT_TYPE_NUMERIC: 53 case BURN_VARIANT_TYPE_NUMERIC:
64 BVariantRetrieveDecryptedNumeric(pVariant, pllValue); 54 BVariantRetrieveNumeric(pVariant, pllValue);
65 break; 55 break;
66 case BURN_VARIANT_TYPE_STRING: 56 case BURN_VARIANT_TYPE_STRING:
67 hr = BVariantRetrieveDecryptedString(pVariant, &sczValue); 57 hr = BVariantRetrieveDecryptedString(pVariant, &sczValue);
@@ -76,7 +66,7 @@ extern "C" HRESULT BVariantGetNumeric(
76 StrSecureZeroFreeString(sczValue); 66 StrSecureZeroFreeString(sczValue);
77 break; 67 break;
78 case BURN_VARIANT_TYPE_VERSION: 68 case BURN_VARIANT_TYPE_VERSION:
79 BVariantRetrieveDecryptedVersion(pVariant, (DWORD64*)pllValue); 69 BVariantRetrieveVersion(pVariant, (DWORD64*)pllValue);
80 break; 70 break;
81 default: 71 default:
82 hr = E_INVALIDARG; 72 hr = E_INVALIDARG;
@@ -99,7 +89,7 @@ extern "C" HRESULT BVariantGetString(
99 switch (pVariant->Type) 89 switch (pVariant->Type)
100 { 90 {
101 case BURN_VARIANT_TYPE_NUMERIC: 91 case BURN_VARIANT_TYPE_NUMERIC:
102 hr = BVariantRetrieveDecryptedNumeric(pVariant, &llValue); 92 BVariantRetrieveNumeric(pVariant, &llValue);
103 if (SUCCEEDED(hr)) 93 if (SUCCEEDED(hr))
104 { 94 {
105 hr = StrAllocFormattedSecure(psczValue, L"%I64d", llValue); 95 hr = StrAllocFormattedSecure(psczValue, L"%I64d", llValue);
@@ -111,7 +101,7 @@ extern "C" HRESULT BVariantGetString(
111 hr = BVariantRetrieveDecryptedString(pVariant, psczValue); 101 hr = BVariantRetrieveDecryptedString(pVariant, psczValue);
112 break; 102 break;
113 case BURN_VARIANT_TYPE_VERSION: 103 case BURN_VARIANT_TYPE_VERSION:
114 hr = BVariantRetrieveDecryptedVersion(pVariant, &qwValue); 104 BVariantRetrieveVersion(pVariant, &qwValue);
115 if (SUCCEEDED(hr)) 105 if (SUCCEEDED(hr))
116 { 106 {
117 hr = StrAllocFormattedSecure(psczValue, L"%hu.%hu.%hu.%hu", 107 hr = StrAllocFormattedSecure(psczValue, L"%hu.%hu.%hu.%hu",
@@ -144,7 +134,7 @@ extern "C" HRESULT BVariantGetVersion(
144 switch (pVariant->Type) 134 switch (pVariant->Type)
145 { 135 {
146 case BURN_VARIANT_TYPE_NUMERIC: 136 case BURN_VARIANT_TYPE_NUMERIC:
147 BVariantRetrieveDecryptedNumeric(pVariant, (LONGLONG*)pqwValue); 137 BVariantRetrieveNumeric(pVariant, (LONGLONG*)pqwValue);
148 break; 138 break;
149 case BURN_VARIANT_TYPE_STRING: 139 case BURN_VARIANT_TYPE_STRING:
150 hr = BVariantRetrieveDecryptedString(pVariant, &sczValue); 140 hr = BVariantRetrieveDecryptedString(pVariant, &sczValue);
@@ -159,7 +149,7 @@ extern "C" HRESULT BVariantGetVersion(
159 StrSecureZeroFreeString(sczValue); 149 StrSecureZeroFreeString(sczValue);
160 break; 150 break;
161 case BURN_VARIANT_TYPE_VERSION: 151 case BURN_VARIANT_TYPE_VERSION:
162 BVariantRetrieveDecryptedVersion(pVariant, pqwValue); 152 BVariantRetrieveVersion(pVariant, pqwValue);
163 break; 153 break;
164 default: 154 default:
165 hr = E_INVALIDARG; 155 hr = E_INVALIDARG;
@@ -175,7 +165,7 @@ extern "C" HRESULT BVariantSetNumeric(
175 ) 165 )
176{ 166{
177 HRESULT hr = S_OK; 167 HRESULT hr = S_OK;
178 BOOL fEncryptValue = pVariant->fEncryptValue; 168 BOOL fEncrypt = pVariant->fEncryptString;
179 169
180 if (BURN_VARIANT_TYPE_STRING == pVariant->Type) 170 if (BURN_VARIANT_TYPE_STRING == pVariant->Type)
181 { 171 {
@@ -184,7 +174,7 @@ extern "C" HRESULT BVariantSetNumeric(
184 memset(pVariant, 0, sizeof(BURN_VARIANT)); 174 memset(pVariant, 0, sizeof(BURN_VARIANT));
185 pVariant->llValue = llValue; 175 pVariant->llValue = llValue;
186 pVariant->Type = BURN_VARIANT_TYPE_NUMERIC; 176 pVariant->Type = BURN_VARIANT_TYPE_NUMERIC;
187 BVariantSetEncryption(pVariant, fEncryptValue); 177 BVariantSetEncryption(pVariant, fEncrypt);
188 178
189 return hr; 179 return hr;
190} 180}
@@ -196,7 +186,7 @@ extern "C" HRESULT BVariantSetString(
196 ) 186 )
197{ 187{
198 HRESULT hr = S_OK; 188 HRESULT hr = S_OK;
199 BOOL fEncryptValue = pVariant->fEncryptValue; 189 BOOL fEncrypt = pVariant->fEncryptString;
200 190
201 if (!wzValue) // if we're nulling out the string, make the variable NONE. 191 if (!wzValue) // if we're nulling out the string, make the variable NONE.
202 { 192 {
@@ -211,7 +201,7 @@ extern "C" HRESULT BVariantSetString(
211 else 201 else
212 { 202 {
213 // We're about to copy an unencrypted value. 203 // We're about to copy an unencrypted value.
214 pVariant->fEncryptValue = FALSE; 204 pVariant->fEncryptString = FALSE;
215 } 205 }
216 206
217 hr = StrAllocStringSecure(&pVariant->sczValue, wzValue, cchValue); 207 hr = StrAllocStringSecure(&pVariant->sczValue, wzValue, cchValue);
@@ -221,7 +211,7 @@ extern "C" HRESULT BVariantSetString(
221 } 211 }
222 212
223LExit: 213LExit:
224 BVariantSetEncryption(pVariant, fEncryptValue); 214 BVariantSetEncryption(pVariant, fEncrypt);
225 return hr; 215 return hr;
226} 216}
227 217
@@ -231,7 +221,7 @@ extern "C" HRESULT BVariantSetVersion(
231 ) 221 )
232{ 222{
233 HRESULT hr = S_OK; 223 HRESULT hr = S_OK;
234 BOOL fEncryptValue = pVariant->fEncryptValue; 224 BOOL fEncryptValue = pVariant->fEncryptString;
235 225
236 if (BURN_VARIANT_TYPE_STRING == pVariant->Type) 226 if (BURN_VARIANT_TYPE_STRING == pVariant->Type)
237 { 227 {
@@ -254,7 +244,7 @@ extern "C" HRESULT BVariantSetValue(
254 LONGLONG llValue = 0; 244 LONGLONG llValue = 0;
255 LPWSTR sczValue = NULL; 245 LPWSTR sczValue = NULL;
256 DWORD64 qwValue = 0; 246 DWORD64 qwValue = 0;
257 BOOL fEncrypt = pVariant->fEncryptValue; 247 BOOL fEncrypt = pVariant->fEncryptString;
258 248
259 switch (pValue->Type) 249 switch (pValue->Type)
260 { 250 {
@@ -341,7 +331,7 @@ extern "C" HRESULT BVariantCopy(
341 } 331 }
342 ExitOnFailure(hr, "Failed to copy variant."); 332 ExitOnFailure(hr, "Failed to copy variant.");
343 333
344 hr = BVariantSetEncryption(pTarget, pSource->fEncryptValue); 334 hr = BVariantSetEncryption(pTarget, pSource->fEncryptString);
345 335
346LExit: 336LExit:
347 return hr; 337 return hr;
@@ -354,7 +344,7 @@ extern "C" HRESULT BVariantChangeType(
354{ 344{
355 HRESULT hr = S_OK; 345 HRESULT hr = S_OK;
356 BURN_VARIANT variant = { }; 346 BURN_VARIANT variant = { };
357 BOOL fEncryptValue = pVariant->fEncryptValue; 347 BOOL fEncrypt = pVariant->fEncryptString;
358 348
359 if (pVariant->Type == type) 349 if (pVariant->Type == type)
360 { 350 {
@@ -384,7 +374,7 @@ extern "C" HRESULT BVariantChangeType(
384 BVariantUninitialize(pVariant); 374 BVariantUninitialize(pVariant);
385 memcpy_s(pVariant, sizeof(BURN_VARIANT), &variant, sizeof(BURN_VARIANT)); 375 memcpy_s(pVariant, sizeof(BURN_VARIANT), &variant, sizeof(BURN_VARIANT));
386 SecureZeroMemory(&variant, sizeof(BURN_VARIANT)); 376 SecureZeroMemory(&variant, sizeof(BURN_VARIANT));
387 BVariantSetEncryption(pVariant, fEncryptValue); 377 BVariantSetEncryption(pVariant, fEncrypt);
388 378
389LExit: 379LExit:
390 return hr; 380 return hr;
@@ -397,7 +387,7 @@ extern "C" HRESULT BVariantSetEncryption(
397{ 387{
398 HRESULT hr = S_OK; 388 HRESULT hr = S_OK;
399 389
400 if (pVariant->fEncryptValue == fEncrypt) 390 if (pVariant->fEncryptString == fEncrypt)
401 { 391 {
402 // The requested encryption state is already applied. 392 // The requested encryption state is already applied.
403 ExitFunction(); 393 ExitFunction();
@@ -406,47 +396,23 @@ extern "C" HRESULT BVariantSetEncryption(
406 switch (pVariant->Type) 396 switch (pVariant->Type)
407 { 397 {
408 case BURN_VARIANT_TYPE_NONE: 398 case BURN_VARIANT_TYPE_NONE:
409 hr = S_OK;
410 break;
411 case BURN_VARIANT_TYPE_NUMERIC: 399 case BURN_VARIANT_TYPE_NUMERIC:
412 hr = BVariantEncryptNumeric(pVariant, fEncrypt); 400 case BURN_VARIANT_TYPE_VERSION:
401 hr = S_OK;
413 break; 402 break;
414 case BURN_VARIANT_TYPE_STRING: 403 case BURN_VARIANT_TYPE_STRING:
415 hr = BVariantEncryptString(pVariant, fEncrypt); 404 hr = BVariantEncryptString(pVariant, fEncrypt);
416 break; 405 break;
417 case BURN_VARIANT_TYPE_VERSION:
418 hr = BVariantEncryptVersion(pVariant, fEncrypt);
419 break;
420 default: 406 default:
421 hr = E_INVALIDARG; 407 hr = E_INVALIDARG;
422 } 408 }
423 ExitOnFailure(hr, "Failed to set the variant's encryption state"); 409 ExitOnFailure(hr, "Failed to set the variant's encryption state");
424 pVariant->fEncryptValue = fEncrypt; 410 pVariant->fEncryptString = fEncrypt;
425 411
426LExit: 412LExit:
427 return hr; 413 return hr;
428} 414}
429 415
430static HRESULT BVariantEncryptNumeric(
431 __in BURN_VARIANT* pVariant,
432 __in BOOL fEncrypt
433 )
434{
435 HRESULT hr = S_OK;
436
437 if (fEncrypt)
438 {
439 hr = CrypEncryptMemory(&pVariant->llValue, sizeof(pVariant->encryptionPadding), VARIANT_ENCRYPTION_SCOPE);
440 }
441 else
442 {
443 hr = CrypDecryptMemory(&pVariant->llValue, sizeof(pVariant->encryptionPadding), VARIANT_ENCRYPTION_SCOPE);
444 }
445
446//LExit:
447 return hr;
448}
449
450static HRESULT BVariantEncryptString( 416static HRESULT BVariantEncryptString(
451 __in BURN_VARIANT* pVariant, 417 __in BURN_VARIANT* pVariant,
452 __in BOOL fEncrypt 418 __in BOOL fEncrypt
@@ -496,50 +462,14 @@ LExit:
496 return hr; 462 return hr;
497} 463}
498 464
499static HRESULT BVariantEncryptVersion( 465static void BVariantRetrieveNumeric(
500 __in BURN_VARIANT* pVariant,
501 __in BOOL fEncrypt
502 )
503{
504 HRESULT hr = S_OK;
505
506 if (fEncrypt)
507 {
508 hr = CrypEncryptMemory(&pVariant->qwValue, sizeof(pVariant->encryptionPadding), VARIANT_ENCRYPTION_SCOPE);
509 }
510 else
511 {
512 hr = CrypDecryptMemory(&pVariant->qwValue, sizeof(pVariant->encryptionPadding), VARIANT_ENCRYPTION_SCOPE);
513 }
514
515//LExit:
516 return hr;
517}
518
519// The contents of pllValue may be sensitive, should keep encrypted and SecureZeroMemory.
520static HRESULT BVariantRetrieveDecryptedNumeric(
521 __in BURN_VARIANT* pVariant, 466 __in BURN_VARIANT* pVariant,
522 __out LONGLONG* pllValue 467 __out LONGLONG* pllValue
523 ) 468 )
524{ 469{
525 HRESULT hr = S_OK;
526
527 Assert(NULL != pllValue); 470 Assert(NULL != pllValue);
528 if (pVariant->fEncryptValue)
529 {
530 hr = BVariantEncryptNumeric(pVariant, FALSE);
531 ExitOnFailure(hr, "Failed to decrypt numeric");
532 }
533 471
534 *pllValue = pVariant->llValue; 472 *pllValue = pVariant->llValue;
535
536 if (pVariant->fEncryptValue)
537 {
538 hr = BVariantEncryptNumeric(pVariant, TRUE);
539 }
540
541LExit:
542 return hr;
543} 473}
544 474
545// The contents of psczValue may be sensitive, should keep encrypted and SecureZeroFree. 475// The contents of psczValue may be sensitive, should keep encrypted and SecureZeroFree.
@@ -550,13 +480,13 @@ static HRESULT BVariantRetrieveDecryptedString(
550{ 480{
551 HRESULT hr = S_OK; 481 HRESULT hr = S_OK;
552 482
553 if (NULL == pVariant->sczValue) 483 if (!pVariant->sczValue)
554 { 484 {
555 *psczValue = NULL; 485 *psczValue = NULL;
556 ExitFunction(); 486 ExitFunction();
557 } 487 }
558 488
559 if (pVariant->fEncryptValue) 489 if (pVariant->fEncryptString)
560 { 490 {
561 hr = BVariantEncryptString(pVariant, FALSE); 491 hr = BVariantEncryptString(pVariant, FALSE);
562 ExitOnFailure(hr, "Failed to decrypt string"); 492 ExitOnFailure(hr, "Failed to decrypt string");
@@ -565,7 +495,7 @@ static HRESULT BVariantRetrieveDecryptedString(
565 hr = StrAllocStringSecure(psczValue, pVariant->sczValue, 0); 495 hr = StrAllocStringSecure(psczValue, pVariant->sczValue, 0);
566 ExitOnFailure(hr, "Failed to copy value."); 496 ExitOnFailure(hr, "Failed to copy value.");
567 497
568 if (pVariant->fEncryptValue) 498 if (pVariant->fEncryptString)
569 { 499 {
570 hr = BVariantEncryptString(pVariant, TRUE); 500 hr = BVariantEncryptString(pVariant, TRUE);
571 } 501 }
@@ -574,28 +504,12 @@ LExit:
574 return hr; 504 return hr;
575} 505}
576 506
577// The contents of pqwValue may be sensitive, should keep encrypted and SecureZeroMemory. 507static void BVariantRetrieveVersion(
578static HRESULT BVariantRetrieveDecryptedVersion(
579 __in BURN_VARIANT* pVariant, 508 __in BURN_VARIANT* pVariant,
580 __out DWORD64* pqwValue 509 __out DWORD64* pqwValue
581 ) 510 )
582{ 511{
583 HRESULT hr = S_OK;
584
585 Assert(NULL != pqwValue); 512 Assert(NULL != pqwValue);
586 if (pVariant->fEncryptValue)
587 {
588 hr = BVariantEncryptVersion(pVariant, FALSE);
589 ExitOnFailure(hr, "Failed to decrypt version");
590 }
591 513
592 *pqwValue = pVariant->qwValue; 514 *pqwValue = pVariant->qwValue;
593
594 if (pVariant->fEncryptValue)
595 {
596 hr = BVariantEncryptVersion(pVariant, TRUE);
597 }
598
599LExit:
600 return hr;
601} 515}
diff --git a/src/engine/variant.h b/src/engine/variant.h
index 9259f05a..73fbe076 100644
--- a/src/engine/variant.h
+++ b/src/engine/variant.h
@@ -27,10 +27,9 @@ typedef struct _BURN_VARIANT
27 LONGLONG llValue; 27 LONGLONG llValue;
28 DWORD64 qwValue; 28 DWORD64 qwValue;
29 LPWSTR sczValue; 29 LPWSTR sczValue;
30 BYTE encryptionPadding[CRYP_ENCRYPT_MEMORY_SIZE];
31 }; 30 };
32 BURN_VARIANT_TYPE Type; 31 BURN_VARIANT_TYPE Type;
33 BOOL fEncryptValue; 32 BOOL fEncryptString;
34} BURN_VARIANT; 33} BURN_VARIANT;
35 34
36 35
diff --git a/src/test/BurnUnitTest/VariantTest.cpp b/src/test/BurnUnitTest/VariantTest.cpp
index dd679f08..d16ac699 100644
--- a/src/test/BurnUnitTest/VariantTest.cpp
+++ b/src/test/BurnUnitTest/VariantTest.cpp
@@ -77,7 +77,7 @@ namespace Bootstrapper
77 hr = BVariantSetEncryption(pActualValue, TRUE); 77 hr = BVariantSetEncryption(pActualValue, TRUE);
78 NativeAssert::Succeeded(hr, "Failed to encrypt variant {0}", wz); 78 NativeAssert::Succeeded(hr, "Failed to encrypt variant {0}", wz);
79 79
80 NativeAssert::True(pActualValue->fEncryptValue); 80 NativeAssert::True(pActualValue->fEncryptString);
81 } 81 }
82 } 82 }
83 83
@@ -95,7 +95,7 @@ namespace Bootstrapper
95 hr = BVariantSetEncryption(pActualValue, TRUE); 95 hr = BVariantSetEncryption(pActualValue, TRUE);
96 NativeAssert::Succeeded(hr, "Failed to encrypt variant {0}", wz); 96 NativeAssert::Succeeded(hr, "Failed to encrypt variant {0}", wz);
97 97
98 NativeAssert::True(pActualValue->fEncryptValue); 98 NativeAssert::True(pActualValue->fEncryptString);
99 } 99 }
100 } 100 }
101 101
@@ -115,7 +115,7 @@ namespace Bootstrapper
115 hr = BVariantSetEncryption(pActualValue, TRUE); 115 hr = BVariantSetEncryption(pActualValue, TRUE);
116 NativeAssert::Succeeded(hr, "Failed to encrypt variant {0}", wz); 116 NativeAssert::Succeeded(hr, "Failed to encrypt variant {0}", wz);
117 117
118 NativeAssert::True(pActualValue->fEncryptValue); 118 NativeAssert::True(pActualValue->fEncryptString);
119 } 119 }
120 } 120 }
121 121
@@ -133,7 +133,7 @@ namespace Bootstrapper
133 hr = BVariantSetEncryption(pActualValue, TRUE); 133 hr = BVariantSetEncryption(pActualValue, TRUE);
134 NativeAssert::Succeeded(hr, "Failed to encrypt variant {0}", wz); 134 NativeAssert::Succeeded(hr, "Failed to encrypt variant {0}", wz);
135 135
136 NativeAssert::True(pActualValue->fEncryptValue); 136 NativeAssert::True(pActualValue->fEncryptString);
137 } 137 }
138 } 138 }
139 139