2 Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
4 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/DebugLib.h>
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/PcdLib.h>
15 #include <Library/MemoryAllocationLib.h>
16 #include <Library/BaseCryptLib.h>
19 Check to see if any of the keys in PcdFmpDevicePkcs7CertBufferXdr matches
20 the test key. PcdFmpDeviceTestKeySha256Digest contains the SHA256 hash of
21 the test key. For each key in PcdFmpDevicePkcs7CertBufferXdr, compute the
22 SHA256 hash and compare it to PcdFmpDeviceTestKeySha256Digest. If the
23 SHA256 hash matches or there is then error computing the SHA256 hash, then
24 set PcdTestKeyUsed to TRUE. Skip this check if PcdTestKeyUsed is already
25 TRUE or PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE
34 UINTN PublicKeyDataLength
;
35 UINT8
*PublicKeyDataXdr
;
36 UINT8
*PublicKeyDataXdrEnd
;
38 UINT8 Digest
[SHA256_DIGEST_SIZE
];
39 UINTN TestKeyDigestSize
;
42 // If PcdFmpDeviceTestKeySha256Digest is not exacty SHA256_DIGEST_SIZE bytes,
43 // then skip the test key detection.
45 TestKeyDigestSize
= PcdGetSize (PcdFmpDeviceTestKeySha256Digest
);
46 if (TestKeyDigestSize
!= SHA256_DIGEST_SIZE
) {
51 // If PcdTestKeyUsed is already TRUE, then skip test key detection
53 TestKeyUsed
= PcdGetBool (PcdTestKeyUsed
);
59 // If PcdFmpDevicePkcs7CertBufferXdr is invalid, then skip test key detection
61 PublicKeyDataXdr
= PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr
);
62 PublicKeyDataXdrEnd
= PublicKeyDataXdr
+ PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr
);
63 if (PublicKeyDataXdr
== NULL
|| PublicKeyDataXdr
== PublicKeyDataXdrEnd
) {
68 // Allocate hash context buffer required for SHA 256
70 HashContext
= AllocatePool (Sha256GetContextSize ());
71 if (HashContext
== NULL
) {
76 // Loop through all keys in PcdFmpDevicePkcs7CertBufferXdr
78 while (!TestKeyUsed
&& PublicKeyDataXdr
< PublicKeyDataXdrEnd
) {
79 if (PublicKeyDataXdr
+ sizeof (UINT32
) > PublicKeyDataXdrEnd
) {
81 // Key data extends beyond end of PCD
86 // Read key length stored in big endian format
88 PublicKeyDataLength
= SwapBytes32 (*(UINT32
*)(PublicKeyDataXdr
));
90 // Point to the start of the key data
92 PublicKeyDataXdr
+= sizeof (UINT32
);
93 if (PublicKeyDataXdr
+ PublicKeyDataLength
> PublicKeyDataXdrEnd
) {
95 // Key data extends beyond end of PCD
101 // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
102 // If error occurs computing SHA256, then assume test key is in use.
104 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
105 if (!Sha256Init (HashContext
)) {
109 if (!Sha256Update (HashContext
, PublicKeyDataXdr
, PublicKeyDataLength
)) {
113 if (!Sha256Final (HashContext
, Digest
)) {
119 // Check if SHA256 hash of public key matches SHA256 hash of test key
121 if (CompareMem (Digest
, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest
), SHA256_DIGEST_SIZE
) == 0) {
127 // Point to start of next key
129 PublicKeyDataXdr
+= PublicKeyDataLength
;
130 PublicKeyDataXdr
= (UINT8
*)ALIGN_POINTER (PublicKeyDataXdr
, sizeof (UINT32
));
134 // Free hash context buffer required for SHA 256
136 if (HashContext
!= NULL
) {
137 FreePool (HashContext
);
142 // If test key detected or an error occured checking for the test key, then
143 // set PcdTestKeyUsed to TRUE.
146 DEBUG ((DEBUG_INFO
, "FmpDxe: Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
147 PcdSetBoolS (PcdTestKeyUsed
, TRUE
);
149 DEBUG ((DEBUG_INFO
, "FmpDxe: No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));