2 Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
4 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <Library/DebugLib.h>
29 #include <Library/BaseLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/PcdLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/BaseCryptLib.h>
36 Check to see if any of the keys in PcdFmpDevicePkcs7CertBufferXdr matches
37 the test key. PcdFmpDeviceTestKeySha256Digest contains the SHA256 hash of
38 the test key. For each key in PcdFmpDevicePkcs7CertBufferXdr, compute the
39 SHA256 hash and compare it to PcdFmpDeviceTestKeySha256Digest. If the
40 SHA256 hash matches or there is then error computing the SHA256 hash, then
41 set PcdTestKeyUsed to TRUE. Skip this check if PcdTestKeyUsed is already
42 TRUE or PcdFmpDeviceTestKeySha256Digest is not exactly SHA256_DIGEST_SIZE
51 UINTN PublicKeyDataLength
;
52 UINT8
*PublicKeyDataXdr
;
53 UINT8
*PublicKeyDataXdrEnd
;
55 UINT8 Digest
[SHA256_DIGEST_SIZE
];
58 // If PcdFmpDeviceTestKeySha256Digest is not exacty SHA256_DIGEST_SIZE bytes,
59 // then skip the test key detection.
61 if (PcdGetSize (PcdFmpDeviceTestKeySha256Digest
) != SHA256_DIGEST_SIZE
) {
66 // If PcdTestKeyUsed is already TRUE, then skip test key detection
68 TestKeyUsed
= PcdGetBool (PcdTestKeyUsed
);
74 // If PcdFmpDevicePkcs7CertBufferXdr is invalid, then skip test key detection
76 PublicKeyDataXdr
= PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr
);
77 PublicKeyDataXdrEnd
= PublicKeyDataXdr
+ PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr
);
78 if (PublicKeyDataXdr
== NULL
|| PublicKeyDataXdr
== PublicKeyDataXdrEnd
) {
83 // Allocate hash context buffer required for SHA 256
85 HashContext
= AllocatePool (Sha256GetContextSize ());
86 if (HashContext
== NULL
) {
91 // Loop through all keys in PcdFmpDevicePkcs7CertBufferXdr
93 while (!TestKeyUsed
&& PublicKeyDataXdr
< PublicKeyDataXdrEnd
) {
94 if (PublicKeyDataXdr
+ sizeof (UINT32
) > PublicKeyDataXdrEnd
) {
96 // Key data extends beyond end of PCD
101 // Read key length stored in big endian format
103 PublicKeyDataLength
= SwapBytes32 (*(UINT32
*)(PublicKeyDataXdr
));
105 // Point to the start of the key data
107 PublicKeyDataXdr
+= sizeof (UINT32
);
108 if (PublicKeyDataXdr
+ PublicKeyDataLength
> PublicKeyDataXdrEnd
) {
110 // Key data extends beyond end of PCD
116 // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
117 // If error occurs computing SHA256, then assume test key is in use.
119 ZeroMem (Digest
, SHA256_DIGEST_SIZE
);
120 if (!Sha256Init (HashContext
)) {
124 if (!Sha256Update (HashContext
, PublicKeyDataXdr
, PublicKeyDataLength
)) {
128 if (!Sha256Final (HashContext
, Digest
)) {
134 // Check if SHA256 hash of public key matches SHA256 hash of test key
136 if (CompareMem (Digest
, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest
), SHA256_DIGEST_SIZE
) == 0) {
142 // Point to start of next key
144 PublicKeyDataXdr
+= PublicKeyDataLength
;
145 PublicKeyDataXdr
= (UINT8
*)ALIGN_POINTER (PublicKeyDataXdr
, sizeof (UINT32
));
149 // Free hash context buffer required for SHA 256
151 if (HashContext
!= NULL
) {
152 FreePool (HashContext
);
157 // If test key detected or an error occured checking for the test key, then
158 // set PcdTestKeyUsed to TRUE.
161 DEBUG ((DEBUG_INFO
, "FmpDxe: Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
162 PcdSetBoolS (PcdTestKeyUsed
, TRUE
);
164 DEBUG ((DEBUG_INFO
, "FmpDxe: No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));