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