2 Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
4 Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 Check to see if any of the keys in PcdFmpDevicePkcs7CertBufferXdr matches
14 the test key. PcdFmpDeviceTestKeySha256Digest contains the SHA256 hash of
15 the test key. For each key in PcdFmpDevicePkcs7CertBufferXdr, compute the
16 SHA256 hash and compare it to PcdFmpDeviceTestKeySha256Digest. If the
17 SHA256 hash matches or there is then error computing the SHA256 hash, then
18 set PcdTestKeyUsed to TRUE. Skip this check if PcdTestKeyUsed is already
19 TRUE or PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE
28 UINTN PublicKeyDataLength
;
29 UINT8
*PublicKeyDataXdr
;
30 UINT8
*PublicKeyDataXdrEnd
;
32 UINT8 Digest
[SHA256_DIGEST_SIZE
];
33 UINTN TestKeyDigestSize
;
36 // If PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE bytes,
37 // then skip the test key detection.
39 TestKeyDigestSize
= PcdGetSize (PcdFmpDeviceTestKeySha256Digest
);
40 if (TestKeyDigestSize
!= SHA256_DIGEST_SIZE
) {
45 // If PcdTestKeyUsed is already TRUE, then skip test key detection
47 TestKeyUsed
= PcdGetBool (PcdTestKeyUsed
);
53 // If PcdFmpDevicePkcs7CertBufferXdr is invalid, then skip test key detection
55 PublicKeyDataXdr
= PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr
);
56 PublicKeyDataXdrEnd
= PublicKeyDataXdr
+ PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr
);
57 if (PublicKeyDataXdr
== NULL
|| PublicKeyDataXdr
== PublicKeyDataXdrEnd
) {
62 // Allocate hash context buffer required for SHA 256
64 HashContext
= AllocatePool (Sha256GetContextSize ());
65 if (HashContext
== NULL
) {
70 // Loop through all keys in PcdFmpDevicePkcs7CertBufferXdr
72 while (!TestKeyUsed
&& PublicKeyDataXdr
< PublicKeyDataXdrEnd
) {
73 if (PublicKeyDataXdr
+ sizeof (UINT32
) > PublicKeyDataXdrEnd
) {
75 // Key data extends beyond end of PCD
80 // Read key length stored in big endian format
82 PublicKeyDataLength
= SwapBytes32 (*(UINT32
*)(PublicKeyDataXdr
));
84 // Point to the start of the key data
86 PublicKeyDataXdr
+= sizeof (UINT32
);
87 if (PublicKeyDataXdr
+ PublicKeyDataLength
> PublicKeyDataXdrEnd
) {
89 // Key data extends beyond end of PCD
95 // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
96 // If error occurs computing SHA256, then assume test key is in use.
98 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
99 if (!Sha256Init (HashContext
)) {
103 if (!Sha256Update (HashContext
, PublicKeyDataXdr
, PublicKeyDataLength
)) {
107 if (!Sha256Final (HashContext
, Digest
)) {
113 // Check if SHA256 hash of public key matches SHA256 hash of test key
115 if (CompareMem (Digest
, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest
), SHA256_DIGEST_SIZE
) == 0) {
121 // Point to start of next key
123 PublicKeyDataXdr
+= PublicKeyDataLength
;
124 PublicKeyDataXdr
= (UINT8
*)ALIGN_POINTER (PublicKeyDataXdr
, sizeof (UINT32
));
128 // Free hash context buffer required for SHA 256
130 if (HashContext
!= NULL
) {
131 FreePool (HashContext
);
136 // If test key detected or an error occurred checking for the test key, then
137 // set PcdTestKeyUsed to TRUE.
140 DEBUG ((DEBUG_INFO
, "FmpDxe(%s): Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n", mImageIdName
));
141 PcdSetBoolS (PcdTestKeyUsed
, TRUE
);
143 DEBUG ((DEBUG_INFO
, "FmpDxe(%s): No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n", mImageIdName
));