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