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