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