]> git.proxmox.com Git - mirror_edk2.git/blob - FmpDevicePkg/FmpDxe/DetectTestKey.c
FmpDevicePkg DSC: Add NOOPT target to verify NOOPT target
[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 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.
13
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.
24
25 **/
26
27 #include <PiDxe.h>
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>
34
35 /**
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
43 bytes.
44 **/
45 VOID
46 DetectTestKey (
47 VOID
48 )
49 {
50 BOOLEAN TestKeyUsed;
51 UINTN PublicKeyDataLength;
52 UINT8 *PublicKeyDataXdr;
53 UINT8 *PublicKeyDataXdrEnd;
54 VOID *HashContext;
55 UINT8 Digest[SHA256_DIGEST_SIZE];
56 UINTN TestKeyDigestSize;
57
58 //
59 // If PcdFmpDeviceTestKeySha256Digest is not exacty SHA256_DIGEST_SIZE bytes,
60 // then skip the test key detection.
61 //
62 TestKeyDigestSize = PcdGetSize (PcdFmpDeviceTestKeySha256Digest);
63 if (TestKeyDigestSize != SHA256_DIGEST_SIZE) {
64 return;
65 }
66
67 //
68 // If PcdTestKeyUsed is already TRUE, then skip test key detection
69 //
70 TestKeyUsed = PcdGetBool (PcdTestKeyUsed);
71 if (TestKeyUsed) {
72 return;
73 }
74
75 //
76 // If PcdFmpDevicePkcs7CertBufferXdr is invalid, then skip test key detection
77 //
78 PublicKeyDataXdr = PcdGetPtr (PcdFmpDevicePkcs7CertBufferXdr);
79 PublicKeyDataXdrEnd = PublicKeyDataXdr + PcdGetSize (PcdFmpDevicePkcs7CertBufferXdr);
80 if (PublicKeyDataXdr == NULL || PublicKeyDataXdr == PublicKeyDataXdrEnd) {
81 return;
82 }
83
84 //
85 // Allocate hash context buffer required for SHA 256
86 //
87 HashContext = AllocatePool (Sha256GetContextSize ());
88 if (HashContext == NULL) {
89 TestKeyUsed = TRUE;
90 }
91
92 //
93 // Loop through all keys in PcdFmpDevicePkcs7CertBufferXdr
94 //
95 while (!TestKeyUsed && PublicKeyDataXdr < PublicKeyDataXdrEnd) {
96 if (PublicKeyDataXdr + sizeof (UINT32) > PublicKeyDataXdrEnd) {
97 //
98 // Key data extends beyond end of PCD
99 //
100 break;
101 }
102 //
103 // Read key length stored in big endian format
104 //
105 PublicKeyDataLength = SwapBytes32 (*(UINT32 *)(PublicKeyDataXdr));
106 //
107 // Point to the start of the key data
108 //
109 PublicKeyDataXdr += sizeof (UINT32);
110 if (PublicKeyDataXdr + PublicKeyDataLength > PublicKeyDataXdrEnd) {
111 //
112 // Key data extends beyond end of PCD
113 //
114 break;
115 }
116
117 //
118 // Hash public key from PcdFmpDevicePkcs7CertBufferXdr using SHA256.
119 // If error occurs computing SHA256, then assume test key is in use.
120 //
121 ZeroMem (Digest, SHA256_DIGEST_SIZE);
122 if (!Sha256Init (HashContext)) {
123 TestKeyUsed = TRUE;
124 break;
125 }
126 if (!Sha256Update (HashContext, PublicKeyDataXdr, PublicKeyDataLength)) {
127 TestKeyUsed = TRUE;
128 break;
129 }
130 if (!Sha256Final (HashContext, Digest)) {
131 TestKeyUsed = TRUE;
132 break;
133 }
134
135 //
136 // Check if SHA256 hash of public key matches SHA256 hash of test key
137 //
138 if (CompareMem (Digest, PcdGetPtr (PcdFmpDeviceTestKeySha256Digest), SHA256_DIGEST_SIZE) == 0) {
139 TestKeyUsed = TRUE;
140 break;
141 }
142
143 //
144 // Point to start of next key
145 //
146 PublicKeyDataXdr += PublicKeyDataLength;
147 PublicKeyDataXdr = (UINT8 *)ALIGN_POINTER (PublicKeyDataXdr, sizeof (UINT32));
148 }
149
150 //
151 // Free hash context buffer required for SHA 256
152 //
153 if (HashContext != NULL) {
154 FreePool (HashContext);
155 HashContext = NULL;
156 }
157
158 //
159 // If test key detected or an error occured checking for the test key, then
160 // set PcdTestKeyUsed to TRUE.
161 //
162 if (TestKeyUsed) {
163 DEBUG ((DEBUG_INFO, "FmpDxe: Test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
164 PcdSetBoolS (PcdTestKeyUsed, TRUE);
165 } else {
166 DEBUG ((DEBUG_INFO, "FmpDxe: No test key detected in PcdFmpDevicePkcs7CertBufferXdr.\n"));
167 }
168 }