DetectTestKey.c 4.2 KB

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