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