DetectTestKey.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /** @file
  2. Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
  3. Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <Library/DebugLib.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/PcdLib.h>
  11. #include <Library/MemoryAllocationLib.h>
  12. #include <Library/BaseCryptLib.h>
  13. /**
  14. Check to see if any of the keys in PcdFmpDevicePkcs7CertBufferXdr matches
  15. the test key. PcdFmpDeviceTestKeySha256Digest contains the SHA256 hash of
  16. the test key. For each key in PcdFmpDevicePkcs7CertBufferXdr, compute the
  17. SHA256 hash and compare it to PcdFmpDeviceTestKeySha256Digest. If the
  18. SHA256 hash matches or there is then error computing the SHA256 hash, then
  19. set PcdTestKeyUsed to TRUE. Skip this check if PcdTestKeyUsed is already
  20. TRUE or PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE
  21. bytes.
  22. **/
  23. VOID
  24. DetectTestKey (
  25. VOID
  26. )
  27. {
  28. BOOLEAN TestKeyUsed;
  29. UINTN PublicKeyDataLength;
  30. UINT8 *PublicKeyDataXdr;
  31. UINT8 *PublicKeyDataXdrEnd;
  32. VOID *HashContext;
  33. UINT8 Digest[SHA256_DIGEST_SIZE];
  34. UINTN TestKeyDigestSize;
  35. //
  36. // If PcdFmpDeviceTestKeySha256Digest is not exacty SHA256_DIGEST_SIZE bytes,
  37. // then skip the test key detection.
  38. //
  39. TestKeyDigestSize = PcdGetSize (PcdFmpDeviceTestKeySha256Digest);
  40. if (TestKeyDigestSize != SHA256_DIGEST_SIZE) {
  41. return;
  42. }
  43. //
  44. // If PcdTestKeyUsed is already TRUE, then skip test key detection
  45. //
  46. TestKeyUsed = PcdGetBool (PcdTestKeyUsed);
  47. if (TestKeyUsed) {
  48. return;
  49. }
  50. //
  51. // If PcdFmpDevicePkcs7CertBufferXdr is invalid, then skip test key detection
  52. //
  53. PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
  54. PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
  55. if (PublicKeyDataXdr == NULL || PublicKeyDataXdr == PublicKeyDataXdrEnd) {
  56. return;
  57. }
  58. //
  59. // Allocate hash context buffer required for SHA 256
  60. //
  61. HashContext = AllocatePool (Sha256GetContextSize ());
  62. if (HashContext == NULL) {
  63. TestKeyUsed = TRUE;
  64. }
  65. //
  66. // Loop through all keys in PcdFmpDevicePkcs7CertBufferXdr
  67. //
  68. while (!TestKeyUsed && PublicKeyDataXdr < PublicKeyDataXdrEnd) {
  69. if (PublicKeyDataXdr + sizeof (UINT32) > PublicKeyDataXdrEnd) {
  70. //
  71. // Key data extends beyond end of PCD
  72. //
  73. break;
  74. }
  75. //
  76. // Read key length stored in big endian format
  77. //
  78. PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
  79. //
  80. // Point to the start of the key data
  81. //
  82. PublicKeyDataXdr += sizeof (UINT32);
  83. if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
  84. //
  85. // Key data extends beyond end of PCD
  86. //
  87. break;
  88. }
  89. //
  90. // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
  91. // If error occurs computing SHA256, then assume test key is in use.
  92. //
  93. ZeroMem (Digest, SHA256_DIGEST_SIZE);
  94. if (!Sha256Init (HashContext)) {
  95. TestKeyUsed = TRUE;
  96. break;
  97. }
  98. if (!Sha256Update (HashContext, PublicKeyDataXdr, PublicKeyDataLength)) {
  99. TestKeyUsed = TRUE;
  100. break;
  101. }
  102. if (!Sha256Final (HashContext, Digest)) {
  103. TestKeyUsed = TRUE;
  104. break;
  105. }
  106. //
  107. // Check if SHA256 hash of public key matches SHA256 hash of test key
  108. //
  109. if (CompareMem (Digest, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest), SHA256_DIGEST_SIZE) == 0) {
  110. TestKeyUsed = TRUE;
  111. break;
  112. }
  113. //
  114. // Point to start of next key
  115. //
  116. PublicKeyDataXdr += PublicKeyDataLength;
  117. PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
  118. }
  119. //
  120. // Free hash context buffer required for SHA 256
  121. //
  122. if (HashContext != NULL) {
  123. FreePool (HashContext);
  124. HashContext = NULL;
  125. }
  126. //
  127. // If test key detected or an error occured checking for the test key, then
  128. // set PcdTestKeyUsed to TRUE.
  129. //
  130. if (TestKeyUsed) {
  131. DEBUG ((DEBUG_INFO, "FmpDxe: Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
  132. PcdSetBoolS (PcdTestKeyUsed, TRUE);
  133. } else {
  134. DEBUG ((DEBUG_INFO, "FmpDxe: No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
  135. }
  136. }