Commit | Line | Data |
---|---|---|
938f77f8 JY |
1 | /** @file\r |
2 | FMP Authentication RSA2048SHA256 handler.\r | |
3 | Provide generic FMP authentication functions for DXE/PEI post memory phase.\r | |
4 | \r | |
5 | Caution: This module requires additional review when modified.\r | |
6 | This module will have external input - capsule image.\r | |
7 | This external input must be validated carefully to avoid security issue like\r | |
8 | buffer overflow, integer overflow.\r | |
9 | \r | |
10 | FmpAuthenticatedHandlerRsa2048Sha256(), AuthenticateFmpImage() will receive\r | |
11 | untrusted input and do basic validation.\r | |
12 | \r | |
d4ccf264 | 13 | Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r |
289b714b | 14 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
938f77f8 JY |
15 | \r |
16 | **/\r | |
17 | \r | |
18 | #include <Uefi.h>\r | |
19 | \r | |
20 | #include <Guid/SystemResourceTable.h>\r | |
21 | #include <Guid/FirmwareContentsSigned.h>\r | |
22 | #include <Guid/WinCertificate.h>\r | |
23 | \r | |
24 | #include <Library/BaseLib.h>\r | |
25 | #include <Library/BaseMemoryLib.h>\r | |
26 | #include <Library/DebugLib.h>\r | |
27 | #include <Library/MemoryAllocationLib.h>\r | |
28 | #include <Library/BaseCryptLib.h>\r | |
29 | #include <Library/FmpAuthenticationLib.h>\r | |
30 | #include <Library/PcdLib.h>\r | |
31 | #include <Protocol/FirmwareManagement.h>\r | |
32 | #include <Guid/SystemResourceTable.h>\r | |
33 | \r | |
34 | ///\r | |
35 | /// Public Exponent of RSA Key.\r | |
36 | ///\r | |
37 | STATIC CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };\r | |
38 | \r | |
39 | /**\r | |
40 | The handler is used to do the authentication for FMP capsule based upon\r | |
41 | EFI_FIRMWARE_IMAGE_AUTHENTICATION.\r | |
42 | \r | |
43 | Caution: This function may receive untrusted input.\r | |
44 | \r | |
45 | This function assumes the caller AuthenticateFmpImage()\r | |
46 | already did basic validation for EFI_FIRMWARE_IMAGE_AUTHENTICATION.\r | |
47 | \r | |
48 | @param[in] Image Points to an FMP authentication image, started from EFI_FIRMWARE_IMAGE_AUTHENTICATION.\r | |
49 | @param[in] ImageSize Size of the authentication image in bytes.\r | |
50 | @param[in] PublicKeyData The public key data used to validate the signature.\r | |
51 | @param[in] PublicKeyDataLength The length of the public key data.\r | |
52 | \r | |
53 | @retval RETURN_SUCCESS Authentication pass.\r | |
54 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_SUCCESS.\r | |
55 | @retval RETURN_SECURITY_VIOLATION Authentication fail.\r | |
56 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR.\r | |
57 | @retval RETURN_INVALID_PARAMETER The image is in an invalid format.\r | |
58 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.\r | |
59 | @retval RETURN_OUT_OF_RESOURCES No Authentication handler associated with CertType.\r | |
60 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES.\r | |
61 | **/\r | |
62 | RETURN_STATUS\r | |
63 | FmpAuthenticatedHandlerRsa2048Sha256 (\r | |
64 | IN EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,\r | |
65 | IN UINTN ImageSize,\r | |
66 | IN CONST UINT8 *PublicKeyData,\r | |
67 | IN UINTN PublicKeyDataLength\r | |
68 | )\r | |
69 | {\r | |
70 | RETURN_STATUS Status;\r | |
71 | EFI_CERT_BLOCK_RSA_2048_SHA256 *CertBlockRsa2048Sha256;\r | |
72 | BOOLEAN CryptoStatus;\r | |
73 | UINT8 Digest[SHA256_DIGEST_SIZE];\r | |
74 | UINT8 *PublicKey;\r | |
75 | UINTN PublicKeyBufferSize;\r | |
76 | VOID *HashContext;\r | |
77 | VOID *Rsa;\r | |
78 | \r | |
79 | DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerRsa2048Sha256 - Image: 0x%08x - 0x%08x\n", (UINTN)Image, (UINTN)ImageSize));\r | |
80 | \r | |
81 | if (Image->AuthInfo.Hdr.dwLength != OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData) + sizeof(EFI_CERT_BLOCK_RSA_2048_SHA256)) {\r | |
82 | DEBUG((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256 - dwLength: 0x%04x, dwLength - 0x%04x\n", (UINTN)Image->AuthInfo.Hdr.dwLength, (UINTN)OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData) + sizeof(EFI_CERT_BLOCK_RSA_2048_SHA256)));\r | |
83 | return RETURN_INVALID_PARAMETER;\r | |
84 | }\r | |
85 | \r | |
86 | CertBlockRsa2048Sha256 = (EFI_CERT_BLOCK_RSA_2048_SHA256 *)Image->AuthInfo.CertData;\r | |
87 | if (!CompareGuid(&CertBlockRsa2048Sha256->HashType, &gEfiHashAlgorithmSha256Guid)) {\r | |
88 | DEBUG((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256 - HashType: %g, expect - %g\n", &CertBlockRsa2048Sha256->HashType, &gEfiHashAlgorithmSha256Guid));\r | |
89 | return RETURN_INVALID_PARAMETER;\r | |
90 | }\r | |
91 | \r | |
92 | HashContext = NULL;\r | |
93 | Rsa = NULL;\r | |
94 | \r | |
95 | //\r | |
96 | // Allocate hash context buffer required for SHA 256\r | |
97 | //\r | |
98 | HashContext = AllocatePool (Sha256GetContextSize ());\r | |
99 | if (HashContext == NULL) {\r | |
100 | CryptoStatus = FALSE;\r | |
101 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Can not allocate hash context\n"));\r | |
102 | Status = RETURN_OUT_OF_RESOURCES;\r | |
103 | goto Done;\r | |
104 | }\r | |
105 | \r | |
106 | //\r | |
107 | // Hash public key from data payload with SHA256.\r | |
108 | //\r | |
109 | ZeroMem (Digest, SHA256_DIGEST_SIZE);\r | |
110 | CryptoStatus = Sha256Init (HashContext);\r | |
111 | if (!CryptoStatus) {\r | |
112 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Init() failed\n"));\r | |
113 | Status = RETURN_OUT_OF_RESOURCES;\r | |
114 | goto Done;\r | |
115 | }\r | |
116 | CryptoStatus = Sha256Update (HashContext, &CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey));\r | |
117 | if (!CryptoStatus) {\r | |
118 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Update() failed\n"));\r | |
119 | Status = RETURN_OUT_OF_RESOURCES;\r | |
120 | goto Done;\r | |
121 | }\r | |
122 | CryptoStatus = Sha256Final (HashContext, Digest);\r | |
123 | if (!CryptoStatus) {\r | |
124 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Final() failed\n"));\r | |
125 | Status = RETURN_OUT_OF_RESOURCES;\r | |
126 | goto Done;\r | |
127 | }\r | |
128 | \r | |
129 | //\r | |
d4ccf264 | 130 | // Fail if the PublicKey is not one of the public keys in the input PublicKeyData.\r |
938f77f8 JY |
131 | //\r |
132 | PublicKey = (VOID *)PublicKeyData;\r | |
133 | PublicKeyBufferSize = PublicKeyDataLength;\r | |
134 | CryptoStatus = FALSE;\r | |
135 | while (PublicKeyBufferSize != 0) {\r | |
136 | if (CompareMem (Digest, PublicKey, SHA256_DIGEST_SIZE) == 0) {\r | |
137 | CryptoStatus = TRUE;\r | |
138 | break;\r | |
139 | }\r | |
140 | PublicKey = PublicKey + SHA256_DIGEST_SIZE;\r | |
141 | PublicKeyBufferSize = PublicKeyBufferSize - SHA256_DIGEST_SIZE;\r | |
142 | }\r | |
143 | if (!CryptoStatus) {\r | |
144 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Public key in section is not supported\n"));\r | |
145 | Status = RETURN_SECURITY_VIOLATION;\r | |
146 | goto Done;\r | |
147 | }\r | |
148 | \r | |
149 | //\r | |
150 | // Generate & Initialize RSA Context.\r | |
151 | //\r | |
152 | Rsa = RsaNew ();\r | |
153 | if (Rsa == NULL) {\r | |
154 | CryptoStatus = FALSE;\r | |
155 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaNew() failed\n"));\r | |
156 | Status = RETURN_OUT_OF_RESOURCES;\r | |
157 | goto Done;\r | |
158 | }\r | |
159 | \r | |
160 | //\r | |
161 | // Set RSA Key Components.\r | |
162 | // NOTE: Only N and E are needed to be set as RSA public key for signature verification.\r | |
163 | //\r | |
164 | CryptoStatus = RsaSetKey (Rsa, RsaKeyN, CertBlockRsa2048Sha256->PublicKey, sizeof(CertBlockRsa2048Sha256->PublicKey));\r | |
165 | if (!CryptoStatus) {\r | |
166 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaSetKey(RsaKeyN) failed\n"));\r | |
167 | Status = RETURN_OUT_OF_RESOURCES;\r | |
168 | goto Done;\r | |
169 | }\r | |
170 | CryptoStatus = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE));\r | |
171 | if (!CryptoStatus) {\r | |
172 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaSetKey(RsaKeyE) failed\n"));\r | |
173 | Status = RETURN_OUT_OF_RESOURCES;\r | |
174 | goto Done;\r | |
175 | }\r | |
176 | \r | |
177 | //\r | |
178 | // Hash data payload with SHA256.\r | |
179 | //\r | |
180 | ZeroMem (Digest, SHA256_DIGEST_SIZE);\r | |
181 | CryptoStatus = Sha256Init (HashContext);\r | |
182 | if (!CryptoStatus) {\r | |
183 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Init() failed\n"));\r | |
184 | Status = RETURN_OUT_OF_RESOURCES;\r | |
185 | goto Done;\r | |
186 | }\r | |
187 | \r | |
188 | // It is a signature across the variable data and the Monotonic Count value.\r | |
189 | CryptoStatus = Sha256Update (\r | |
190 | HashContext,\r | |
191 | (UINT8 *)Image + sizeof(Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength,\r | |
192 | ImageSize - sizeof(Image->MonotonicCount) - Image->AuthInfo.Hdr.dwLength\r | |
193 | );\r | |
194 | if (!CryptoStatus) {\r | |
195 | DEBUG((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Update() failed\n"));\r | |
196 | Status = RETURN_OUT_OF_RESOURCES;\r | |
197 | goto Done;\r | |
198 | }\r | |
199 | CryptoStatus = Sha256Update (\r | |
200 | HashContext,\r | |
201 | (UINT8 *)&Image->MonotonicCount,\r | |
202 | sizeof(Image->MonotonicCount)\r | |
203 | );\r | |
204 | if (!CryptoStatus) {\r | |
205 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Update() failed\n"));\r | |
206 | Status = RETURN_OUT_OF_RESOURCES;\r | |
207 | goto Done;\r | |
208 | }\r | |
209 | CryptoStatus = Sha256Final (HashContext, Digest);\r | |
210 | if (!CryptoStatus) {\r | |
211 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Final() failed\n"));\r | |
212 | Status = RETURN_OUT_OF_RESOURCES;\r | |
213 | goto Done;\r | |
214 | }\r | |
215 | \r | |
216 | //\r | |
217 | // Verify the RSA 2048 SHA 256 signature.\r | |
218 | //\r | |
219 | CryptoStatus = RsaPkcs1Verify (\r | |
220 | Rsa,\r | |
221 | Digest,\r | |
222 | SHA256_DIGEST_SIZE,\r | |
223 | CertBlockRsa2048Sha256->Signature,\r | |
224 | sizeof (CertBlockRsa2048Sha256->Signature)\r | |
225 | );\r | |
226 | if (!CryptoStatus) {\r | |
227 | //\r | |
228 | // If RSA 2048 SHA 256 signature verification fails, AUTH tested failed bit is set.\r | |
229 | //\r | |
230 | DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaPkcs1Verify() failed\n"));\r | |
231 | Status = RETURN_SECURITY_VIOLATION;\r | |
232 | goto Done;\r | |
233 | }\r | |
234 | DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerRsa2048Sha256: PASS verification\n"));\r | |
235 | \r | |
236 | Status = RETURN_SUCCESS;\r | |
237 | \r | |
238 | Done:\r | |
239 | //\r | |
240 | // Free allocated resources used to perform RSA 2048 SHA 256 signature verification\r | |
241 | //\r | |
242 | if (Rsa != NULL) {\r | |
243 | RsaFree (Rsa);\r | |
244 | }\r | |
245 | if (HashContext != NULL) {\r | |
246 | FreePool (HashContext);\r | |
247 | }\r | |
248 | \r | |
249 | return Status;\r | |
250 | }\r | |
251 | \r | |
252 | /**\r | |
253 | The function is used to do the authentication for FMP capsule based upon\r | |
254 | EFI_FIRMWARE_IMAGE_AUTHENTICATION.\r | |
255 | \r | |
256 | The FMP capsule image should start with EFI_FIRMWARE_IMAGE_AUTHENTICATION,\r | |
257 | followed by the payload.\r | |
258 | \r | |
259 | If the return status is RETURN_SUCCESS, the caller may continue the rest\r | |
260 | FMP update process.\r | |
261 | If the return status is NOT RETURN_SUCCESS, the caller should stop the FMP\r | |
262 | update process and convert the return status to LastAttemptStatus\r | |
263 | to indicate that FMP update fails.\r | |
264 | The LastAttemptStatus can be got from ESRT table or via\r | |
265 | EFI_FIRMWARE_MANAGEMENT_PROTOCOL.GetImageInfo().\r | |
266 | \r | |
267 | Caution: This function may receive untrusted input.\r | |
268 | \r | |
269 | @param[in] Image Points to an FMP authentication image, started from EFI_FIRMWARE_IMAGE_AUTHENTICATION.\r | |
270 | @param[in] ImageSize Size of the authentication image in bytes.\r | |
271 | @param[in] PublicKeyData The public key data used to validate the signature.\r | |
272 | @param[in] PublicKeyDataLength The length of the public key data.\r | |
273 | \r | |
274 | @retval RETURN_SUCCESS Authentication pass.\r | |
275 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_SUCCESS.\r | |
276 | @retval RETURN_SECURITY_VIOLATION Authentication fail.\r | |
277 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR.\r | |
278 | @retval RETURN_INVALID_PARAMETER The image is in an invalid format.\r | |
279 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.\r | |
280 | @retval RETURN_UNSUPPORTED No Authentication handler associated with CertType.\r | |
281 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.\r | |
282 | @retval RETURN_UNSUPPORTED Image or ImageSize is invalid.\r | |
283 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.\r | |
284 | @retval RETURN_OUT_OF_RESOURCES No Authentication handler associated with CertType.\r | |
285 | The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES.\r | |
286 | **/\r | |
287 | RETURN_STATUS\r | |
288 | EFIAPI\r | |
289 | AuthenticateFmpImage (\r | |
290 | IN EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,\r | |
291 | IN UINTN ImageSize,\r | |
292 | IN CONST UINT8 *PublicKeyData,\r | |
293 | IN UINTN PublicKeyDataLength\r | |
294 | )\r | |
295 | {\r | |
296 | GUID *CertType;\r | |
297 | EFI_STATUS Status;\r | |
298 | \r | |
299 | if ((Image == NULL) || (ImageSize == 0)) {\r | |
300 | return RETURN_UNSUPPORTED;\r | |
301 | }\r | |
302 | \r | |
492c05f5 JY |
303 | if ((PublicKeyDataLength % SHA256_DIGEST_SIZE) != 0) {\r |
304 | DEBUG ((DEBUG_ERROR, "PublicKeyDataLength is not multiple SHA256 size\n"));\r | |
305 | return RETURN_UNSUPPORTED;\r | |
306 | }\r | |
307 | \r | |
938f77f8 JY |
308 | if (ImageSize < sizeof(EFI_FIRMWARE_IMAGE_AUTHENTICATION)) {\r |
309 | DEBUG((DEBUG_ERROR, "AuthenticateFmpImage - ImageSize too small\n"));\r | |
310 | return RETURN_INVALID_PARAMETER;\r | |
311 | }\r | |
312 | if (Image->AuthInfo.Hdr.dwLength <= OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r | |
313 | DEBUG((DEBUG_ERROR, "AuthenticateFmpImage - dwLength too small\n"));\r | |
314 | return RETURN_INVALID_PARAMETER;\r | |
315 | }\r | |
ba47ae93 | 316 | if ((UINTN) Image->AuthInfo.Hdr.dwLength > MAX_UINTN - sizeof(UINT64)) {\r |
938f77f8 JY |
317 | DEBUG((DEBUG_ERROR, "AuthenticateFmpImage - dwLength too big\n"));\r |
318 | return RETURN_INVALID_PARAMETER;\r | |
319 | }\r | |
320 | if (ImageSize <= sizeof(Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) {\r | |
321 | DEBUG((DEBUG_ERROR, "AuthenticateFmpImage - ImageSize too small\n"));\r | |
322 | return RETURN_INVALID_PARAMETER;\r | |
323 | }\r | |
324 | if (Image->AuthInfo.Hdr.wRevision != 0x0200) {\r | |
325 | DEBUG((DEBUG_ERROR, "AuthenticateFmpImage - wRevision: 0x%02x, expect - 0x%02x\n", (UINTN)Image->AuthInfo.Hdr.wRevision, (UINTN)0x0200));\r | |
326 | return RETURN_INVALID_PARAMETER;\r | |
327 | }\r | |
328 | if (Image->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) {\r | |
329 | DEBUG((DEBUG_ERROR, "AuthenticateFmpImage - wCertificateType: 0x%02x, expect - 0x%02x\n", (UINTN)Image->AuthInfo.Hdr.wCertificateType, (UINTN)WIN_CERT_TYPE_EFI_GUID));\r | |
330 | return RETURN_INVALID_PARAMETER;\r | |
331 | }\r | |
332 | \r | |
333 | CertType = &Image->AuthInfo.CertType;\r | |
334 | DEBUG((DEBUG_INFO, "AuthenticateFmpImage - CertType: %g\n", CertType));\r | |
335 | \r | |
336 | if (CompareGuid (&gEfiCertTypeRsa2048Sha256Guid, CertType)) {\r | |
337 | //\r | |
338 | // Call the match handler to extract raw data for the input section data.\r | |
339 | //\r | |
340 | Status = FmpAuthenticatedHandlerRsa2048Sha256 (\r | |
341 | Image,\r | |
342 | ImageSize,\r | |
343 | PublicKeyData,\r | |
344 | PublicKeyDataLength\r | |
345 | );\r | |
346 | return Status;\r | |
347 | }\r | |
348 | \r | |
349 | //\r | |
350 | // Not found, the input guided section is not supported.\r | |
351 | //\r | |
352 | return RETURN_UNSUPPORTED;\r | |
353 | }\r | |
354 | \r |