]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Library/FmpAuthenticationLibRsa2048Sha256/FmpAuthenticationLibRsa2048Sha256.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / SecurityPkg / Library / FmpAuthenticationLibRsa2048Sha256 / FmpAuthenticationLibRsa2048Sha256.c
CommitLineData
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
c411b485 37STATIC CONST UINT8 mRsaE[] = { 0x01, 0x00, 0x01 };\r
938f77f8
JY
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
62RETURN_STATUS\r
63FmpAuthenticatedHandlerRsa2048Sha256 (\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
c411b485
MK
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
938f77f8
JY
78\r
79 DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerRsa2048Sha256 - Image: 0x%08x - 0x%08x\n", (UINTN)Image, (UINTN)ImageSize));\r
80\r
c411b485
MK
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
938f77f8
JY
83 return RETURN_INVALID_PARAMETER;\r
84 }\r
85\r
86 CertBlockRsa2048Sha256 = (EFI_CERT_BLOCK_RSA_2048_SHA256 *)Image->AuthInfo.CertData;\r
c411b485
MK
87 if (!CompareGuid (&CertBlockRsa2048Sha256->HashType, &gEfiHashAlgorithmSha256Guid)) {\r
88 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256 - HashType: %g, expect - %g\n", &CertBlockRsa2048Sha256->HashType, &gEfiHashAlgorithmSha256Guid));\r
938f77f8
JY
89 return RETURN_INVALID_PARAMETER;\r
90 }\r
91\r
92 HashContext = NULL;\r
c411b485 93 Rsa = NULL;\r
938f77f8
JY
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
c411b485
MK
116\r
117 CryptoStatus = Sha256Update (HashContext, &CertBlockRsa2048Sha256->PublicKey, sizeof (CertBlockRsa2048Sha256->PublicKey));\r
938f77f8
JY
118 if (!CryptoStatus) {\r
119 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Update() failed\n"));\r
120 Status = RETURN_OUT_OF_RESOURCES;\r
121 goto Done;\r
122 }\r
c411b485
MK
123\r
124 CryptoStatus = Sha256Final (HashContext, Digest);\r
938f77f8
JY
125 if (!CryptoStatus) {\r
126 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Final() failed\n"));\r
127 Status = RETURN_OUT_OF_RESOURCES;\r
128 goto Done;\r
129 }\r
130\r
131 //\r
d4ccf264 132 // Fail if the PublicKey is not one of the public keys in the input PublicKeyData.\r
938f77f8 133 //\r
c411b485 134 PublicKey = (VOID *)PublicKeyData;\r
938f77f8 135 PublicKeyBufferSize = PublicKeyDataLength;\r
c411b485 136 CryptoStatus = FALSE;\r
938f77f8
JY
137 while (PublicKeyBufferSize != 0) {\r
138 if (CompareMem (Digest, PublicKey, SHA256_DIGEST_SIZE) == 0) {\r
139 CryptoStatus = TRUE;\r
140 break;\r
141 }\r
c411b485
MK
142\r
143 PublicKey = PublicKey + SHA256_DIGEST_SIZE;\r
938f77f8
JY
144 PublicKeyBufferSize = PublicKeyBufferSize - SHA256_DIGEST_SIZE;\r
145 }\r
c411b485 146\r
938f77f8
JY
147 if (!CryptoStatus) {\r
148 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Public key in section is not supported\n"));\r
149 Status = RETURN_SECURITY_VIOLATION;\r
150 goto Done;\r
151 }\r
152\r
153 //\r
154 // Generate & Initialize RSA Context.\r
155 //\r
156 Rsa = RsaNew ();\r
157 if (Rsa == NULL) {\r
158 CryptoStatus = FALSE;\r
159 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaNew() failed\n"));\r
160 Status = RETURN_OUT_OF_RESOURCES;\r
161 goto Done;\r
162 }\r
163\r
164 //\r
165 // Set RSA Key Components.\r
166 // NOTE: Only N and E are needed to be set as RSA public key for signature verification.\r
167 //\r
c411b485 168 CryptoStatus = RsaSetKey (Rsa, RsaKeyN, CertBlockRsa2048Sha256->PublicKey, sizeof (CertBlockRsa2048Sha256->PublicKey));\r
938f77f8
JY
169 if (!CryptoStatus) {\r
170 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaSetKey(RsaKeyN) failed\n"));\r
171 Status = RETURN_OUT_OF_RESOURCES;\r
172 goto Done;\r
173 }\r
c411b485 174\r
938f77f8
JY
175 CryptoStatus = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE));\r
176 if (!CryptoStatus) {\r
177 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaSetKey(RsaKeyE) failed\n"));\r
178 Status = RETURN_OUT_OF_RESOURCES;\r
179 goto Done;\r
180 }\r
181\r
182 //\r
183 // Hash data payload with SHA256.\r
184 //\r
185 ZeroMem (Digest, SHA256_DIGEST_SIZE);\r
186 CryptoStatus = Sha256Init (HashContext);\r
187 if (!CryptoStatus) {\r
188 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Init() failed\n"));\r
189 Status = RETURN_OUT_OF_RESOURCES;\r
190 goto Done;\r
191 }\r
192\r
193 // It is a signature across the variable data and the Monotonic Count value.\r
194 CryptoStatus = Sha256Update (\r
195 HashContext,\r
c411b485
MK
196 (UINT8 *)Image + sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength,\r
197 ImageSize - sizeof (Image->MonotonicCount) - Image->AuthInfo.Hdr.dwLength\r
938f77f8
JY
198 );\r
199 if (!CryptoStatus) {\r
c411b485 200 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Update() failed\n"));\r
938f77f8
JY
201 Status = RETURN_OUT_OF_RESOURCES;\r
202 goto Done;\r
203 }\r
c411b485 204\r
938f77f8
JY
205 CryptoStatus = Sha256Update (\r
206 HashContext,\r
207 (UINT8 *)&Image->MonotonicCount,\r
c411b485 208 sizeof (Image->MonotonicCount)\r
938f77f8
JY
209 );\r
210 if (!CryptoStatus) {\r
211 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Update() failed\n"));\r
212 Status = RETURN_OUT_OF_RESOURCES;\r
213 goto Done;\r
214 }\r
c411b485
MK
215\r
216 CryptoStatus = Sha256Final (HashContext, Digest);\r
938f77f8
JY
217 if (!CryptoStatus) {\r
218 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: Sha256Final() failed\n"));\r
219 Status = RETURN_OUT_OF_RESOURCES;\r
220 goto Done;\r
221 }\r
222\r
223 //\r
224 // Verify the RSA 2048 SHA 256 signature.\r
225 //\r
226 CryptoStatus = RsaPkcs1Verify (\r
227 Rsa,\r
228 Digest,\r
229 SHA256_DIGEST_SIZE,\r
230 CertBlockRsa2048Sha256->Signature,\r
231 sizeof (CertBlockRsa2048Sha256->Signature)\r
232 );\r
233 if (!CryptoStatus) {\r
234 //\r
235 // If RSA 2048 SHA 256 signature verification fails, AUTH tested failed bit is set.\r
236 //\r
237 DEBUG ((DEBUG_ERROR, "FmpAuthenticatedHandlerRsa2048Sha256: RsaPkcs1Verify() failed\n"));\r
238 Status = RETURN_SECURITY_VIOLATION;\r
239 goto Done;\r
240 }\r
c411b485 241\r
938f77f8
JY
242 DEBUG ((DEBUG_INFO, "FmpAuthenticatedHandlerRsa2048Sha256: PASS verification\n"));\r
243\r
244 Status = RETURN_SUCCESS;\r
245\r
246Done:\r
247 //\r
248 // Free allocated resources used to perform RSA 2048 SHA 256 signature verification\r
249 //\r
250 if (Rsa != NULL) {\r
251 RsaFree (Rsa);\r
252 }\r
c411b485 253\r
938f77f8
JY
254 if (HashContext != NULL) {\r
255 FreePool (HashContext);\r
256 }\r
257\r
258 return Status;\r
259}\r
260\r
261/**\r
262 The function is used to do the authentication for FMP capsule based upon\r
263 EFI_FIRMWARE_IMAGE_AUTHENTICATION.\r
264\r
265 The FMP capsule image should start with EFI_FIRMWARE_IMAGE_AUTHENTICATION,\r
266 followed by the payload.\r
267\r
268 If the return status is RETURN_SUCCESS, the caller may continue the rest\r
269 FMP update process.\r
270 If the return status is NOT RETURN_SUCCESS, the caller should stop the FMP\r
271 update process and convert the return status to LastAttemptStatus\r
272 to indicate that FMP update fails.\r
273 The LastAttemptStatus can be got from ESRT table or via\r
274 EFI_FIRMWARE_MANAGEMENT_PROTOCOL.GetImageInfo().\r
275\r
276 Caution: This function may receive untrusted input.\r
277\r
278 @param[in] Image Points to an FMP authentication image, started from EFI_FIRMWARE_IMAGE_AUTHENTICATION.\r
279 @param[in] ImageSize Size of the authentication image in bytes.\r
280 @param[in] PublicKeyData The public key data used to validate the signature.\r
281 @param[in] PublicKeyDataLength The length of the public key data.\r
282\r
283 @retval RETURN_SUCCESS Authentication pass.\r
284 The LastAttemptStatus should be LAST_ATTEMPT_STATUS_SUCCESS.\r
285 @retval RETURN_SECURITY_VIOLATION Authentication fail.\r
286 The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_AUTH_ERROR.\r
287 @retval RETURN_INVALID_PARAMETER The image is in an invalid format.\r
288 The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.\r
289 @retval RETURN_UNSUPPORTED No Authentication handler associated with CertType.\r
290 The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.\r
291 @retval RETURN_UNSUPPORTED Image or ImageSize is invalid.\r
292 The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT.\r
293 @retval RETURN_OUT_OF_RESOURCES No Authentication handler associated with CertType.\r
294 The LastAttemptStatus should be LAST_ATTEMPT_STATUS_ERROR_INSUFFICIENT_RESOURCES.\r
295**/\r
296RETURN_STATUS\r
297EFIAPI\r
298AuthenticateFmpImage (\r
299 IN EFI_FIRMWARE_IMAGE_AUTHENTICATION *Image,\r
300 IN UINTN ImageSize,\r
301 IN CONST UINT8 *PublicKeyData,\r
302 IN UINTN PublicKeyDataLength\r
303 )\r
304{\r
c411b485
MK
305 GUID *CertType;\r
306 EFI_STATUS Status;\r
938f77f8
JY
307\r
308 if ((Image == NULL) || (ImageSize == 0)) {\r
309 return RETURN_UNSUPPORTED;\r
310 }\r
311\r
492c05f5
JY
312 if ((PublicKeyDataLength % SHA256_DIGEST_SIZE) != 0) {\r
313 DEBUG ((DEBUG_ERROR, "PublicKeyDataLength is not multiple SHA256 size\n"));\r
314 return RETURN_UNSUPPORTED;\r
315 }\r
316\r
c411b485
MK
317 if (ImageSize < sizeof (EFI_FIRMWARE_IMAGE_AUTHENTICATION)) {\r
318 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - ImageSize too small\n"));\r
938f77f8
JY
319 return RETURN_INVALID_PARAMETER;\r
320 }\r
c411b485
MK
321\r
322 if (Image->AuthInfo.Hdr.dwLength <= OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r
323 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - dwLength too small\n"));\r
938f77f8
JY
324 return RETURN_INVALID_PARAMETER;\r
325 }\r
c411b485
MK
326\r
327 if ((UINTN)Image->AuthInfo.Hdr.dwLength > MAX_UINTN - sizeof (UINT64)) {\r
328 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - dwLength too big\n"));\r
938f77f8
JY
329 return RETURN_INVALID_PARAMETER;\r
330 }\r
c411b485
MK
331\r
332 if (ImageSize <= sizeof (Image->MonotonicCount) + Image->AuthInfo.Hdr.dwLength) {\r
333 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - ImageSize too small\n"));\r
938f77f8
JY
334 return RETURN_INVALID_PARAMETER;\r
335 }\r
c411b485 336\r
938f77f8 337 if (Image->AuthInfo.Hdr.wRevision != 0x0200) {\r
c411b485 338 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - wRevision: 0x%02x, expect - 0x%02x\n", (UINTN)Image->AuthInfo.Hdr.wRevision, (UINTN)0x0200));\r
938f77f8
JY
339 return RETURN_INVALID_PARAMETER;\r
340 }\r
c411b485 341\r
938f77f8 342 if (Image->AuthInfo.Hdr.wCertificateType != WIN_CERT_TYPE_EFI_GUID) {\r
c411b485 343 DEBUG ((DEBUG_ERROR, "AuthenticateFmpImage - wCertificateType: 0x%02x, expect - 0x%02x\n", (UINTN)Image->AuthInfo.Hdr.wCertificateType, (UINTN)WIN_CERT_TYPE_EFI_GUID));\r
938f77f8
JY
344 return RETURN_INVALID_PARAMETER;\r
345 }\r
346\r
347 CertType = &Image->AuthInfo.CertType;\r
c411b485 348 DEBUG ((DEBUG_INFO, "AuthenticateFmpImage - CertType: %g\n", CertType));\r
938f77f8
JY
349\r
350 if (CompareGuid (&gEfiCertTypeRsa2048Sha256Guid, CertType)) {\r
351 //\r
352 // Call the match handler to extract raw data for the input section data.\r
353 //\r
354 Status = FmpAuthenticatedHandlerRsa2048Sha256 (\r
355 Image,\r
356 ImageSize,\r
357 PublicKeyData,\r
358 PublicKeyDataLength\r
359 );\r
360 return Status;\r
361 }\r
362\r
363 //\r
364 // Not found, the input guided section is not supported.\r
365 //\r
366 return RETURN_UNSUPPORTED;\r
367}\r