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
81 // Read key length stored in big endian format
83 PublicKeyDataLength
= SwapBytes32 (*(UINT32
*)(PublicKeyDataXdr
));
85 // Point to the start of the key data
87 PublicKeyDataXdr
+= sizeof (UINT32
);
88 if (PublicKeyDataXdr
+ PublicKeyDataLength
> PublicKeyDataXdrEnd
) {
90 // Key data extends beyond end of PCD
96 // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
97 // If error occurs computing SHA256, then assume test key is in use.
99 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
100 if (!Sha256Init (HashContext
)) {
105 if (!Sha256Update (HashContext
, PublicKeyDataXdr
, PublicKeyDataLength
)) {
110 if (!Sha256Final (HashContext
, Digest
)) {
116 // Check if SHA256 hash of public key matches SHA256 hash of test key
118 if (CompareMem (Digest
, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest
), SHA256_DIGEST_SIZE
) == 0) {
124 // Point to start of next key
126 PublicKeyDataXdr
+= PublicKeyDataLength
;
127 PublicKeyDataXdr
= (UINT8
*)ALIGN_POINTER (PublicKeyDataXdr
, sizeof (UINT32
));
131 // Free hash context buffer required for SHA 256
133 if (HashContext
!= NULL
) {
134 FreePool (HashContext
);
139 // If test key detected or an error occurred checking for the test key, then
140 // set PcdTestKeyUsed to TRUE.
143 DEBUG ((DEBUG_INFO
, "FmpDxe(%s): Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n", mImageIdName
));
144 PcdSetBoolS (PcdTestKeyUsed
, TRUE
);
146 DEBUG ((DEBUG_INFO
, "FmpDxe(%s): No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n", mImageIdName
));