]> git.proxmox.com Git - mirror_edk2.git/blob - FmpDevicePkg/FmpDxe/DetectTestKey.c
EmulatorPkg: don't display the cpu current speed
[mirror_edk2.git] / FmpDevicePkg / FmpDxe / DetectTestKey.c
1 /** @file
2 Detects if PcdFmpDevicePkcs7CertBufferXdr contains a test key.
3
4 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <PiDxe.h>
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>
17
18 /**
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
26 bytes.
27 **/
28 VOID
29 DetectTestKey (
30 VOID
31 )
32 {
33 BOOLEAN TestKeyUsed;
34 UINTN PublicKeyDataLength;
35 UINT8 *PublicKeyDataXdr;
36 UINT8 *PublicKeyDataXdrEnd;
37 VOID *HashContext;
38 UINT8 Digest[SHA256_DIGEST_SIZE];
39 UINTN TestKeyDigestSize;
40
41 //
42 // If PcdFmpDeviceTestKeySha256Digest is not exacty SHA256_DIGEST_SIZE bytes,
43 // then skip the test key detection.
44 //
45 TestKeyDigestSize = PcdGetSize (PcdFmpDeviceTestKeySha256Digest);
46 if (TestKeyDigestSize != SHA256_DIGEST_SIZE) {
47 return;
48 }
49
50 //
51 // If PcdTestKeyUsed is already TRUE, then skip test key detection
52 //
53 TestKeyUsed = PcdGetBool (PcdTestKeyUsed);
54 if (TestKeyUsed) {
55 return;
56 }
57
58 //
59 // If PcdFmpDevicePkcs7CertBufferXdr is invalid, then skip test key detection
60 //
61 PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
62 PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
63 if (PublicKeyDataXdr == NULL || PublicKeyDataXdr == PublicKeyDataXdrEnd) {
64 return;
65 }
66
67 //
68 // Allocate hash context buffer required for SHA 256
69 //
70 HashContext = AllocatePool (Sha256GetContextSize ());
71 if (HashContext == NULL) {
72 TestKeyUsed = TRUE;
73 }
74
75 //
76 // Loop through all keys in PcdFmpDevicePkcs7CertBufferXdr
77 //
78 while (!TestKeyUsed && PublicKeyDataXdr < PublicKeyDataXdrEnd) {
79 if (PublicKeyDataXdr + sizeof (UINT32) > PublicKeyDataXdrEnd) {
80 //
81 // Key data extends beyond end of PCD
82 //
83 break;
84 }
85 //
86 // Read key length stored in big endian format
87 //
88 PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
89 //
90 // Point to the start of the key data
91 //
92 PublicKeyDataXdr += sizeof (UINT32);
93 if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
94 //
95 // Key data extends beyond end of PCD
96 //
97 break;
98 }
99
100 //
101 // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
102 // If error occurs computing SHA256, then assume test key is in use.
103 //
104 ZeroMem (Digest, SHA256_DIGEST_SIZE);
105 if (!Sha256Init (HashContext)) {
106 TestKeyUsed = TRUE;
107 break;
108 }
109 if (!Sha256Update (HashContext, PublicKeyDataXdr, PublicKeyDataLength)) {
110 TestKeyUsed = TRUE;
111 break;
112 }
113 if (!Sha256Final (HashContext, Digest)) {
114 TestKeyUsed = TRUE;
115 break;
116 }
117
118 //
119 // Check if SHA256 hash of public key matches SHA256 hash of test key
120 //
121 if (CompareMem (Digest, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest), SHA256_DIGEST_SIZE) == 0) {
122 TestKeyUsed = TRUE;
123 break;
124 }
125
126 //
127 // Point to start of next key
128 //
129 PublicKeyDataXdr += PublicKeyDataLength;
130 PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
131 }
132
133 //
134 // Free hash context buffer required for SHA 256
135 //
136 if (HashContext != NULL) {
137 FreePool (HashContext);
138 HashContext = NULL;
139 }
140
141 //
142 // If test key detected or an error occured checking for the test key, then
143 // set PcdTestKeyUsed to TRUE.
144 //
145 if (TestKeyUsed) {
146 DEBUG ((DEBUG_INFO, "FmpDxe: Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
147 PcdSetBoolS (PcdTestKeyUsed, TRUE);
148 } else {
149 DEBUG ((DEBUG_INFO, "FmpDxe: No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
150 }
151 }