]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigImpl.h
SecurityPkg: Fix potential bug in Security Boot dxe.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / SecureBootConfigDxe / SecureBootConfigImpl.h
... / ...
CommitLineData
1/** @file\r
2 The header file of HII Config Access protocol implementation of SecureBoot\r
3 configuration module.\r
4\r
5Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#ifndef __SECUREBOOT_CONFIG_IMPL_H__\r
17#define __SECUREBOOT_CONFIG_IMPL_H__\r
18\r
19#include <Uefi.h>\r
20\r
21#include <Protocol/HiiConfigAccess.h>\r
22#include <Protocol/HiiConfigRouting.h>\r
23#include <Protocol/SimpleFileSystem.h>\r
24#include <Protocol/BlockIo.h>\r
25#include <Protocol/DevicePath.h>\r
26#include <Protocol/DebugPort.h>\r
27#include <Protocol/LoadFile.h>\r
28\r
29#include <Library/BaseLib.h>\r
30#include <Library/BaseMemoryLib.h>\r
31#include <Library/DebugLib.h>\r
32#include <Library/MemoryAllocationLib.h>\r
33#include <Library/UefiBootServicesTableLib.h>\r
34#include <Library/UefiRuntimeServicesTableLib.h>\r
35#include <Library/UefiHiiServicesLib.h>\r
36#include <Library/UefiLib.h>\r
37#include <Library/HiiLib.h>\r
38#include <Library/DevicePathLib.h>\r
39#include <Library/PrintLib.h>\r
40#include <Library/PlatformSecureLib.h>\r
41#include <Library/BaseCryptLib.h>\r
42#include <Library/FileExplorerLib.h>\r
43#include <Library/PeCoffLib.h>\r
44\r
45#include <Guid/MdeModuleHii.h>\r
46#include <Guid/AuthenticatedVariableFormat.h>\r
47#include <Guid/FileSystemVolumeLabelInfo.h>\r
48#include <Guid/ImageAuthentication.h>\r
49#include <Guid/FileInfo.h>\r
50\r
51#include "SecureBootConfigNvData.h"\r
52\r
53//\r
54// Tool generated IFR binary data and String package data\r
55//\r
56extern UINT8 SecureBootConfigBin[];\r
57extern UINT8 SecureBootConfigDxeStrings[];\r
58\r
59//\r
60// Shared IFR form update data\r
61//\r
62extern VOID *mStartOpCodeHandle;\r
63extern VOID *mEndOpCodeHandle;\r
64extern EFI_IFR_GUID_LABEL *mStartLabel;\r
65extern EFI_IFR_GUID_LABEL *mEndLabel;\r
66\r
67#define MAX_CHAR 480\r
68#define TWO_BYTE_ENCODE 0x82\r
69\r
70\r
71//\r
72// SHA-256 digest size in bytes\r
73//\r
74#define SHA256_DIGEST_SIZE 32\r
75//\r
76// SHA-384 digest size in bytes\r
77//\r
78#define SHA384_DIGEST_SIZE 48\r
79//\r
80// SHA-512 digest size in bytes\r
81//\r
82#define SHA512_DIGEST_SIZE 64\r
83\r
84//\r
85// Set max digest size as SHA512 Output (64 bytes) by far\r
86//\r
87#define MAX_DIGEST_SIZE SHA512_DIGEST_SIZE\r
88\r
89#define WIN_CERT_UEFI_RSA2048_SIZE 256\r
90\r
91//\r
92// Support hash types\r
93//\r
94#define HASHALG_SHA224 0x00000000\r
95#define HASHALG_SHA256 0x00000001\r
96#define HASHALG_SHA384 0x00000002\r
97#define HASHALG_SHA512 0x00000003\r
98#define HASHALG_RAW 0x00000004\r
99#define HASHALG_MAX 0x00000004\r
100\r
101\r
102typedef struct {\r
103 UINTN Signature;\r
104 LIST_ENTRY Head;\r
105 UINTN MenuNumber;\r
106} SECUREBOOT_MENU_OPTION;\r
107\r
108typedef struct {\r
109 EFI_FILE_HANDLE FHandle;\r
110 UINT16 *FileName;\r
111} SECUREBOOT_FILE_CONTEXT;\r
112\r
113\r
114//\r
115// We define another format of 5th directory entry: security directory\r
116//\r
117typedef struct {\r
118 UINT32 Offset; // Offset of certificate\r
119 UINT32 SizeOfCert; // size of certificate appended\r
120} EFI_IMAGE_SECURITY_DATA_DIRECTORY;\r
121\r
122typedef enum{\r
123 ImageType_IA32,\r
124 ImageType_X64\r
125} IMAGE_TYPE;\r
126\r
127///\r
128/// HII specific Vendor Device Path definition.\r
129///\r
130typedef struct {\r
131 VENDOR_DEVICE_PATH VendorDevicePath;\r
132 EFI_DEVICE_PATH_PROTOCOL End;\r
133} HII_VENDOR_DEVICE_PATH;\r
134\r
135typedef struct {\r
136 UINTN Signature;\r
137\r
138 EFI_HII_CONFIG_ACCESS_PROTOCOL ConfigAccess;\r
139 EFI_HII_HANDLE HiiHandle;\r
140 EFI_HANDLE DriverHandle;\r
141\r
142 SECUREBOOT_FILE_CONTEXT *FileContext;\r
143\r
144 EFI_GUID *SignatureGUID;\r
145} SECUREBOOT_CONFIG_PRIVATE_DATA;\r
146\r
147extern SECUREBOOT_CONFIG_PRIVATE_DATA mSecureBootConfigPrivateDateTemplate;\r
148extern SECUREBOOT_CONFIG_PRIVATE_DATA *gSecureBootPrivateData;\r
149\r
150#define SECUREBOOT_CONFIG_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('S', 'E', 'C', 'B')\r
151#define SECUREBOOT_CONFIG_PRIVATE_FROM_THIS(a) CR (a, SECUREBOOT_CONFIG_PRIVATE_DATA, ConfigAccess, SECUREBOOT_CONFIG_PRIVATE_DATA_SIGNATURE)\r
152\r
153//\r
154// Cryptograhpic Key Information\r
155//\r
156#pragma pack(1)\r
157typedef struct _CPL_KEY_INFO {\r
158 UINT32 KeyLengthInBits; // Key Length In Bits\r
159 UINT32 BlockSize; // Operation Block Size in Bytes\r
160 UINT32 CipherBlockSize; // Output Cipher Block Size in Bytes\r
161 UINT32 KeyType; // Key Type\r
162 UINT32 CipherMode; // Cipher Mode for Symmetric Algorithm\r
163 UINT32 Flags; // Additional Key Property Flags\r
164} CPL_KEY_INFO;\r
165#pragma pack()\r
166\r
167\r
168/**\r
169 Retrieves the size, in bytes, of the context buffer required for hash operations.\r
170\r
171 @return The size, in bytes, of the context buffer required for hash operations.\r
172\r
173**/\r
174typedef\r
175EFI_STATUS\r
176(EFIAPI *HASH_GET_CONTEXT_SIZE)(\r
177 VOID\r
178 );\r
179\r
180/**\r
181 Initializes user-supplied memory pointed by HashContext as hash context for\r
182 subsequent use.\r
183\r
184 If HashContext is NULL, then ASSERT().\r
185\r
186 @param[in, out] HashContext Pointer to Context being initialized.\r
187\r
188 @retval TRUE HASH context initialization succeeded.\r
189 @retval FALSE HASH context initialization failed.\r
190\r
191**/\r
192typedef\r
193BOOLEAN\r
194(EFIAPI *HASH_INIT)(\r
195 IN OUT VOID *HashContext\r
196 );\r
197\r
198\r
199/**\r
200 Performs digest on a data buffer of the specified length. This function can\r
201 be called multiple times to compute the digest of long or discontinuous data streams.\r
202\r
203 If HashContext is NULL, then ASSERT().\r
204\r
205 @param[in, out] HashContext Pointer to the MD5 context.\r
206 @param[in] Data Pointer to the buffer containing the data to be hashed.\r
207 @param[in] DataLength Length of Data buffer in bytes.\r
208\r
209 @retval TRUE HASH data digest succeeded.\r
210 @retval FALSE Invalid HASH context. After HashFinal function has been called, the\r
211 HASH context cannot be reused.\r
212\r
213**/\r
214typedef\r
215BOOLEAN\r
216(EFIAPI *HASH_UPDATE)(\r
217 IN OUT VOID *HashContext,\r
218 IN CONST VOID *Data,\r
219 IN UINTN DataLength\r
220 );\r
221\r
222/**\r
223 Completes hash computation and retrieves the digest value into the specified\r
224 memory. After this function has been called, the context cannot be used again.\r
225\r
226 If HashContext is NULL, then ASSERT().\r
227 If HashValue is NULL, then ASSERT().\r
228\r
229 @param[in, out] HashContext Pointer to the MD5 context\r
230 @param[out] HashValue Pointer to a buffer that receives the HASH digest\r
231 value (16 bytes).\r
232\r
233 @retval TRUE HASH digest computation succeeded.\r
234 @retval FALSE HASH digest computation failed.\r
235\r
236**/\r
237typedef\r
238BOOLEAN\r
239(EFIAPI *HASH_FINAL)(\r
240 IN OUT VOID *HashContext,\r
241 OUT UINT8 *HashValue\r
242 );\r
243\r
244//\r
245// Hash Algorithm Table\r
246//\r
247typedef struct {\r
248 CHAR16 *Name; ///< Name for Hash Algorithm\r
249 UINTN DigestLength; ///< Digest Length\r
250 UINT8 *OidValue; ///< Hash Algorithm OID ASN.1 Value\r
251 UINTN OidLength; ///< Length of Hash OID Value\r
252 HASH_GET_CONTEXT_SIZE GetContextSize; ///< Pointer to Hash GetContentSize function\r
253 HASH_INIT HashInit; ///< Pointer to Hash Init function\r
254 HASH_UPDATE HashUpdate; ///< Pointer to Hash Update function\r
255 HASH_FINAL HashFinal; ///< Pointer to Hash Final function\r
256} HASH_TABLE;\r
257\r
258typedef struct {\r
259 WIN_CERTIFICATE Hdr;\r
260 UINT8 CertData[1];\r
261} WIN_CERTIFICATE_EFI_PKCS;\r
262\r
263\r
264/**\r
265 This function publish the SecureBoot configuration Form.\r
266\r
267 @param[in, out] PrivateData Points to SecureBoot configuration private data.\r
268\r
269 @retval EFI_SUCCESS HII Form is installed successfully.\r
270 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.\r
271 @retval Others Other errors as indicated.\r
272\r
273**/\r
274EFI_STATUS\r
275InstallSecureBootConfigForm (\r
276 IN OUT SECUREBOOT_CONFIG_PRIVATE_DATA *PrivateData\r
277 );\r
278\r
279\r
280/**\r
281 This function removes SecureBoot configuration Form.\r
282\r
283 @param[in, out] PrivateData Points to SecureBoot configuration private data.\r
284\r
285**/\r
286VOID\r
287UninstallSecureBootConfigForm (\r
288 IN OUT SECUREBOOT_CONFIG_PRIVATE_DATA *PrivateData\r
289 );\r
290\r
291\r
292/**\r
293 This function allows a caller to extract the current configuration for one\r
294 or more named elements from the target driver.\r
295\r
296 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
297 @param[in] Request A null-terminated Unicode string in\r
298 <ConfigRequest> format.\r
299 @param[out] Progress On return, points to a character in the Request\r
300 string. Points to the string's null terminator if\r
301 request was successful. Points to the most recent\r
302 '&' before the first failing name/value pair (or\r
303 the beginning of the string if the failure is in\r
304 the first name/value pair) if the request was not\r
305 successful.\r
306 @param[out] Results A null-terminated Unicode string in\r
307 <ConfigAltResp> format which has all values filled\r
308 in for the names in the Request string. String to\r
309 be allocated by the called function.\r
310\r
311 @retval EFI_SUCCESS The Results is filled with the requested values.\r
312 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.\r
313 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.\r
314 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this\r
315 driver.\r
316\r
317**/\r
318EFI_STATUS\r
319EFIAPI\r
320SecureBootExtractConfig (\r
321 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
322 IN CONST EFI_STRING Request,\r
323 OUT EFI_STRING *Progress,\r
324 OUT EFI_STRING *Results\r
325 );\r
326\r
327\r
328/**\r
329 This function processes the results of changes in configuration.\r
330\r
331 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
332 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>\r
333 format.\r
334 @param[out] Progress A pointer to a string filled in with the offset of\r
335 the most recent '&' before the first failing\r
336 name/value pair (or the beginning of the string if\r
337 the failure is in the first name/value pair) or\r
338 the terminating NULL if all was successful.\r
339\r
340 @retval EFI_SUCCESS The Results is processed successfully.\r
341 @retval EFI_INVALID_PARAMETER Configuration is NULL.\r
342 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this\r
343 driver.\r
344\r
345**/\r
346EFI_STATUS\r
347EFIAPI\r
348SecureBootRouteConfig (\r
349 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
350 IN CONST EFI_STRING Configuration,\r
351 OUT EFI_STRING *Progress\r
352 );\r
353\r
354\r
355/**\r
356 This function processes the results of changes in configuration.\r
357\r
358 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
359 @param[in] Action Specifies the type of action taken by the browser.\r
360 @param[in] QuestionId A unique value which is sent to the original\r
361 exporting driver so that it can identify the type\r
362 of data to expect.\r
363 @param[in] Type The type of value for the question.\r
364 @param[in] Value A pointer to the data being sent to the original\r
365 exporting driver.\r
366 @param[out] ActionRequest On return, points to the action requested by the\r
367 callback function.\r
368\r
369 @retval EFI_SUCCESS The callback successfully handled the action.\r
370 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the\r
371 variable and its data.\r
372 @retval EFI_DEVICE_ERROR The variable could not be saved.\r
373 @retval EFI_UNSUPPORTED The specified Action is not supported by the\r
374 callback.\r
375\r
376**/\r
377EFI_STATUS\r
378EFIAPI\r
379SecureBootCallback (\r
380 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
381 IN EFI_BROWSER_ACTION Action,\r
382 IN EFI_QUESTION_ID QuestionId,\r
383 IN UINT8 Type,\r
384 IN EFI_IFR_TYPE_VALUE *Value,\r
385 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest\r
386 );\r
387\r
388\r
389/**\r
390 This function converts an input device structure to a Unicode string.\r
391\r
392 @param[in] DevPath A pointer to the device path structure.\r
393\r
394 @return A new allocated Unicode string that represents the device path.\r
395\r
396**/\r
397CHAR16 *\r
398EFIAPI\r
399DevicePathToStr (\r
400 IN EFI_DEVICE_PATH_PROTOCOL *DevPath\r
401 );\r
402\r
403\r
404/**\r
405 Clean up the dynamic opcode at label and form specified by both LabelId.\r
406\r
407 @param[in] LabelId It is both the Form ID and Label ID for opcode deletion.\r
408 @param[in] PrivateData Module private data.\r
409\r
410**/\r
411VOID\r
412CleanUpPage (\r
413 IN UINT16 LabelId,\r
414 IN SECUREBOOT_CONFIG_PRIVATE_DATA *PrivateData\r
415 );\r
416\r
417\r
418/**\r
419 Read file content into BufferPtr, the size of the allocate buffer\r
420 is *FileSize plus AddtionAllocateSize.\r
421\r
422 @param[in] FileHandle The file to be read.\r
423 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.\r
424 @param[out] FileSize Size of input file\r
425 @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.\r
426 In case the buffer need to contain others besides the file content.\r
427\r
428 @retval EFI_SUCCESS The file was read into the buffer.\r
429 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
430 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
431 @retval others Unexpected error.\r
432\r
433**/\r
434EFI_STATUS\r
435ReadFileContent (\r
436 IN EFI_FILE_HANDLE FileHandle,\r
437 IN OUT VOID **BufferPtr,\r
438 OUT UINTN *FileSize,\r
439 IN UINTN AddtionAllocateSize\r
440 );\r
441\r
442\r
443/**\r
444 Close an open file handle.\r
445\r
446 @param[in] FileHandle The file handle to close.\r
447\r
448**/\r
449VOID\r
450CloseFile (\r
451 IN EFI_FILE_HANDLE FileHandle\r
452 );\r
453\r
454\r
455/**\r
456 Converts a nonnegative integer to an octet string of a specified length.\r
457\r
458 @param[in] Integer Pointer to the nonnegative integer to be converted\r
459 @param[in] IntSizeInWords Length of integer buffer in words\r
460 @param[out] OctetString Converted octet string of the specified length\r
461 @param[in] OSSizeInBytes Intended length of resulting octet string in bytes\r
462\r
463Returns:\r
464\r
465 @retval EFI_SUCCESS Data conversion successfully\r
466 @retval EFI_BUFFER_TOOL_SMALL Buffer is too small for output string\r
467\r
468**/\r
469EFI_STATUS\r
470EFIAPI\r
471Int2OctStr (\r
472 IN CONST UINTN *Integer,\r
473 IN UINTN IntSizeInWords,\r
474 OUT UINT8 *OctetString,\r
475 IN UINTN OSSizeInBytes\r
476 );\r
477\r
478/**\r
479 Worker function that prints an EFI_GUID into specified Buffer.\r
480\r
481 @param[in] Guid Pointer to GUID to print.\r
482 @param[in] Buffer Buffer to print Guid into.\r
483 @param[in] BufferSize Size of Buffer.\r
484\r
485 @retval Number of characters printed.\r
486\r
487**/\r
488UINTN\r
489GuidToString (\r
490 IN EFI_GUID *Guid,\r
491 IN CHAR16 *Buffer,\r
492 IN UINTN BufferSize\r
493 );\r
494\r
495/**\r
496 Update the PK form base on the input file path info.\r
497\r
498 @param FilePath Point to the file path.\r
499\r
500 @retval TRUE Exit caller function.\r
501 @retval FALSE Not exit caller function.\r
502**/\r
503BOOLEAN\r
504EFIAPI\r
505UpdatePKFromFile (\r
506 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
507 );\r
508\r
509/**\r
510 Update the KEK form base on the input file path info.\r
511\r
512 @param FilePath Point to the file path.\r
513\r
514 @retval TRUE Exit caller function.\r
515 @retval FALSE Not exit caller function.\r
516**/\r
517BOOLEAN\r
518EFIAPI\r
519UpdateKEKFromFile (\r
520 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
521 );\r
522\r
523/**\r
524 Update the DB form base on the input file path info.\r
525\r
526 @param FilePath Point to the file path.\r
527\r
528 @retval TRUE Exit caller function.\r
529 @retval FALSE Not exit caller function.\r
530**/\r
531BOOLEAN\r
532EFIAPI\r
533UpdateDBFromFile (\r
534 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
535 );\r
536\r
537/**\r
538 Update the DBX form base on the input file path info.\r
539\r
540 @param FilePath Point to the file path.\r
541\r
542 @retval TRUE Exit caller function.\r
543 @retval FALSE Not exit caller function.\r
544**/\r
545BOOLEAN\r
546EFIAPI\r
547UpdateDBXFromFile (\r
548 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
549 );\r
550\r
551/**\r
552 Update the DBT form base on the input file path info.\r
553\r
554 @param FilePath Point to the file path.\r
555\r
556 @retval TRUE Exit caller function.\r
557 @retval FALSE Not exit caller function.\r
558**/\r
559BOOLEAN\r
560EFIAPI\r
561UpdateDBTFromFile (\r
562 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
563 );\r
564\r
565#endif\r