TianoCore EDK2 master
Loading...
Searching...
No Matches
DetectTestKey.c
Go to the documentation of this file.
1
10#include "FmpDxe.h"
11
22VOID
24 VOID
25 )
26{
27 BOOLEAN TestKeyUsed;
28 UINTN PublicKeyDataLength;
29 UINT8 *PublicKeyDataXdr;
30 UINT8 *PublicKeyDataXdrEnd;
31 VOID *HashContext;
32 UINT8 Digest[SHA256_DIGEST_SIZE];
33 UINTN TestKeyDigestSize;
34
35 //
36 // If PcdFmpDeviceTestKeySha256Digest is not exactly 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 //
45 // If PcdTestKeyUsed is already TRUE, then skip test key detection
46 //
47 TestKeyUsed = PcdGetBool (PcdTestKeyUsed);
48 if (TestKeyUsed) {
49 return;
50 }
51
52 //
53 // If PcdFmpDevicePkcs7CertBufferXdr is invalid, then skip test key detection
54 //
55 PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
56 PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
57 if ((PublicKeyDataXdr == NULL) || (PublicKeyDataXdr == PublicKeyDataXdrEnd)) {
58 return;
59 }
60
61 //
62 // Allocate hash context buffer required for SHA 256
63 //
64 HashContext = AllocatePool (Sha256GetContextSize ());
65 if (HashContext == NULL) {
66 TestKeyUsed = TRUE;
67 }
68
69 //
70 // Loop through all keys in PcdFmpDevicePkcs7CertBufferXdr
71 //
72 while (!TestKeyUsed && PublicKeyDataXdr < PublicKeyDataXdrEnd) {
73 if (PublicKeyDataXdr + sizeof (UINT32) > PublicKeyDataXdrEnd) {
74 //
75 // Key data extends beyond end of PCD
76 //
77 break;
78 }
79
80 //
81 // Read key length stored in big endian format
82 //
83 PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
84 //
85 // Point to the start of the key data
86 //
87 PublicKeyDataXdr += sizeof (UINT32);
88 if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
89 //
90 // Key data extends beyond end of PCD
91 //
92 break;
93 }
94
95 //
96 // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
97 // If error occurs computing SHA256, then assume test key is in use.
98 //
100 if (!Sha256Init (HashContext)) {
101 TestKeyUsed = TRUE;
102 break;
103 }
104
105 if (!Sha256Update (HashContext, PublicKeyDataXdr, PublicKeyDataLength)) {
106 TestKeyUsed = TRUE;
107 break;
108 }
109
110 if (!Sha256Final (HashContext, Digest)) {
111 TestKeyUsed = TRUE;
112 break;
113 }
114
115 //
116 // Check if SHA256 hash of public key matches SHA256 hash of test key
117 //
118 if (CompareMem (Digest, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest), SHA256_DIGEST_SIZE) == 0) {
119 TestKeyUsed = TRUE;
120 break;
121 }
122
123 //
124 // Point to start of next key
125 //
126 PublicKeyDataXdr += PublicKeyDataLength;
127 PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
128 }
129
130 //
131 // Free hash context buffer required for SHA 256
132 //
133 if (HashContext != NULL) {
134 FreePool (HashContext);
135 HashContext = NULL;
136 }
137
138 //
139 // If test key detected or an error occurred checking for the test key, then
140 // set PcdTestKeyUsed to TRUE.
141 //
142 if (TestKeyUsed) {
143 DEBUG ((DEBUG_INFO, "FmpDxe(%s): Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n", mImageIdName));
144 PcdSetBoolS (PcdTestKeyUsed, TRUE);
145 } else {
146 DEBUG ((DEBUG_INFO, "FmpDxe(%s): No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n", mImageIdName));
147 }
148}
UINT64 UINTN
UINTN EFIAPI Sha256GetContextSize(VOID)
Definition: CryptSha256.c:20
BOOLEAN EFIAPI Sha256Init(OUT VOID *Sha256Context)
Definition: CryptSha256.c:44
BOOLEAN EFIAPI Sha256Final(IN OUT VOID *Sha256Context, OUT UINT8 *HashValue)
Definition: CryptSha256.c:161
#define SHA256_DIGEST_SIZE
Definition: BaseCryptLib.h:44
BOOLEAN EFIAPI Sha256Update(IN OUT VOID *Sha256Context, IN CONST VOID *Data, IN UINTN DataSize)
Definition: CryptSha256.c:113
UINT32 EFIAPI SwapBytes32(IN UINT32 Value)
Definition: SwapBytes32.c:25
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID DetectTestKey(VOID)
Definition: DetectTestKey.c:23
VOID EFIAPI FreePool(IN VOID *Buffer)
CHAR16 * mImageIdName
Definition: FmpDxe.c:101
#define NULL
Definition: Base.h:319
#define ALIGN_POINTER(Pointer, Alignment)
Definition: Base.h:963
#define TRUE
Definition: Base.h:301
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGetSize(TokenName)
Definition: PcdLib.h:440
#define PcdSetBoolS(TokenName, Value)
Definition: PcdLib.h:549
#define PcdGetBool(TokenName)
Definition: PcdLib.h:401
#define PcdGetPtr(TokenName)
Definition: PcdLib.h:388
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)