]> 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 //
81 // Read key length stored in big endian format
82 //
83 PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
84 //
85 // Point to the start of the key data
86 //
87 PublicKeyDataXdr += sizeof (UINT32);
88 if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
89 //
90 // Key data extends beyond end of PCD
91 //
92 break;
93 }
94
95 //
96 // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
97 // If error occurs computing SHA256, then assume test key is in use.
98 //
99 ZeroMem (Digest, SHA256_DIGEST_SIZE);
100 if (!Sha256Init (HashContext)) {
101 TestKeyUsed = TRUE;
102 break;
103 }
104
105 if (!Sha256Update (HashContext, PublicKeyDataXdr, PublicKeyDataLength)) {
106 TestKeyUsed = TRUE;
107 break;
108 }
109
110 if (!Sha256Final (HashContext, Digest)) {
111 TestKeyUsed = TRUE;
112 break;
113 }
114
115 //
116 // Check if SHA256 hash of public key matches SHA256 hash of test key
117 //
118 if (CompareMem (Digest, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest), SHA256_DIGEST_SIZE) == 0) {
119 TestKeyUsed = TRUE;
120 break;
121 }
122
123 //
124 // Point to start of next key
125 //
126 PublicKeyDataXdr += PublicKeyDataLength;
127 PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
128 }
129
130 //
131 // Free hash context buffer required for SHA 256
132 //
133 if (HashContext != NULL) {
134 FreePool (HashContext);
135 HashContext = NULL;
136 }
137
138 //
139 // If test key detected or an error occurred checking for the test key, then
140 // set PcdTestKeyUsed to TRUE.
141 //
142 if (TestKeyUsed) {
143 DEBUG ((DEBUG_INFO, "FmpDxe(%s): Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n", mImageIdName));
144 PcdSetBoolS (PcdTestKeyUsed, TRUE);
145 } else {
146 DEBUG ((DEBUG_INFO, "FmpDxe(%s): No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n", mImageIdName));
147 }
148 }