2 The Miscellaneous Routines for TlsAuthConfigDxe driver.
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "TlsAuthConfigImpl.h"
18 VOID
*mStartOpCodeHandle
= NULL
;
19 VOID
*mEndOpCodeHandle
= NULL
;
20 EFI_IFR_GUID_LABEL
*mStartLabel
= NULL
;
21 EFI_IFR_GUID_LABEL
*mEndLabel
= NULL
;
24 CHAR16 mTlsAuthConfigStorageName
[] = L
"TLS_AUTH_CONFIG_IFR_NVDATA";
26 TLS_AUTH_CONFIG_PRIVATE_DATA
*mTlsAuthPrivateData
= NULL
;
28 HII_VENDOR_DEVICE_PATH mTlsAuthConfigHiiVendorDevicePath
= {
34 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
35 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
42 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
44 (UINT8
) (END_DEVICE_PATH_LENGTH
),
45 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
51 // Possible DER-encoded certificate file suffixes, end with NULL pointer.
53 CHAR16
* mDerPemEncodedSuffix
[] = {
62 This code checks if the FileSuffix is one of the possible DER/PEM-encoded certificate suffix.
64 @param[in] FileSuffix The suffix of the input certificate file
66 @retval TRUE It's a DER/PEM-encoded certificate.
67 @retval FALSE It's NOT a DER/PEM-encoded certificate.
71 IsDerPemEncodeCertificate (
72 IN CONST CHAR16
*FileSuffix
76 for (Index
= 0; mDerPemEncodedSuffix
[Index
] != NULL
; Index
++) {
77 if (StrCmp (FileSuffix
, mDerPemEncodedSuffix
[Index
]) == 0) {
85 Worker function that prints an EFI_GUID into specified Buffer.
87 @param[in] Guid Pointer to GUID to print.
88 @param[in] Buffer Buffer to print Guid into.
89 @param[in] BufferSize Size of Buffer.
91 @retval Number of characters printed.
101 return UnicodeSPrint (
110 List all cert in specified database by GUID in the page
111 for user to select and delete as needed.
113 @param[in] PrivateData Module's private data.
114 @param[in] VariableName The variable name of the vendor's signature database.
115 @param[in] VendorGuid A unique identifier for the vendor.
116 @param[in] LabelNumber Label number to insert opcodes.
117 @param[in] FormId Form ID of current page.
118 @param[in] QuestionIdBase Base question id of the signature list.
120 @retval EFI_SUCCESS Success to update the signature list page
121 @retval EFI_OUT_OF_RESOURCES Unable to allocate required resources.
126 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
,
127 IN CHAR16
*VariableName
,
128 IN EFI_GUID
*VendorGuid
,
129 IN UINT16 LabelNumber
,
130 IN EFI_FORM_ID FormId
,
131 IN EFI_QUESTION_ID QuestionIdBase
138 VOID
*StartOpCodeHandle
;
139 VOID
*EndOpCodeHandle
;
140 EFI_IFR_GUID_LABEL
*StartLabel
;
141 EFI_IFR_GUID_LABEL
*EndLabel
;
144 EFI_SIGNATURE_LIST
*CertList
;
145 EFI_SIGNATURE_DATA
*Cert
;
148 EFI_STRING_ID GuidID
;
155 StartOpCodeHandle
= NULL
;
156 EndOpCodeHandle
= NULL
;
159 // Initialize the container for dynamic opcodes.
161 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
162 if (StartOpCodeHandle
== NULL
) {
163 Status
= EFI_OUT_OF_RESOURCES
;
167 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
168 if (EndOpCodeHandle
== NULL
) {
169 Status
= EFI_OUT_OF_RESOURCES
;
174 // Create Hii Extend Label OpCode.
176 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
180 sizeof (EFI_IFR_GUID_LABEL
)
182 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
183 StartLabel
->Number
= LabelNumber
;
185 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
189 sizeof (EFI_IFR_GUID_LABEL
)
191 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
192 EndLabel
->Number
= LABEL_END
;
198 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
199 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
203 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
205 Status
= EFI_OUT_OF_RESOURCES
;
209 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
210 if (EFI_ERROR (Status
)) {
214 GuidStr
= AllocateZeroPool (100);
215 if (GuidStr
== NULL
) {
216 Status
= EFI_OUT_OF_RESOURCES
;
221 // Enumerate all data.
223 ItemDataSize
= (UINT32
) DataSize
;
224 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
227 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
229 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
230 Help
= STRING_TOKEN (STR_CERT_TYPE_PCKS_GUID
);
233 // The signature type is not supported in current implementation.
235 ItemDataSize
-= CertList
->SignatureListSize
;
236 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
240 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
241 for (Index
= 0; Index
< CertCount
; Index
++) {
242 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
243 + sizeof (EFI_SIGNATURE_LIST
)
244 + CertList
->SignatureHeaderSize
245 + Index
* CertList
->SignatureSize
);
247 // Display GUID and help
249 GuidToString (&Cert
->SignatureOwner
, GuidStr
, 100);
250 GuidID
= HiiSetString (Private
->RegisteredHandle
, 0, GuidStr
, NULL
);
251 HiiCreateCheckBoxOpCode (
253 (EFI_QUESTION_ID
) (QuestionIdBase
+ GuidIndex
++),
258 EFI_IFR_FLAG_CALLBACK
,
264 ItemDataSize
-= CertList
->SignatureListSize
;
265 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
270 Private
->RegisteredHandle
,
277 if (StartOpCodeHandle
!= NULL
) {
278 HiiFreeOpCodeHandle (StartOpCodeHandle
);
281 if (EndOpCodeHandle
!= NULL
) {
282 HiiFreeOpCodeHandle (EndOpCodeHandle
);
289 if (GuidStr
!= NULL
) {
297 Delete one entry from cert database.
299 @param[in] Private Module's private data.
300 @param[in] VariableName The variable name of the database.
301 @param[in] VendorGuid A unique identifier for the vendor.
302 @param[in] LabelNumber Label number to insert opcodes.
303 @param[in] FormId Form ID of current page.
304 @param[in] QuestionIdBase Base question id of the cert list.
305 @param[in] DeleteIndex Cert index to delete.
307 @retval EFI_SUCCESS Delete siganture successfully.
308 @retval EFI_NOT_FOUND Can't find the signature item,
309 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
313 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
,
314 IN CHAR16
*VariableName
,
315 IN EFI_GUID
*VendorGuid
,
316 IN UINT16 LabelNumber
,
317 IN EFI_FORM_ID FormId
,
318 IN EFI_QUESTION_ID QuestionIdBase
,
328 EFI_SIGNATURE_LIST
*CertList
;
329 EFI_SIGNATURE_LIST
*NewCertList
;
330 EFI_SIGNATURE_DATA
*Cert
;
344 // Get original signature list data.
347 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, NULL
);
348 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
352 OldData
= (UINT8
*) AllocateZeroPool (DataSize
);
353 if (OldData
== NULL
) {
354 Status
= EFI_OUT_OF_RESOURCES
;
358 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, &Attr
, &DataSize
, OldData
);
359 if (EFI_ERROR(Status
)) {
364 // Allocate space for new variable.
366 Data
= (UINT8
*) AllocateZeroPool (DataSize
);
368 Status
= EFI_OUT_OF_RESOURCES
;
373 // Enumerate all data and erasing the target item.
376 ItemDataSize
= (UINT32
) DataSize
;
377 CertList
= (EFI_SIGNATURE_LIST
*) OldData
;
380 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
381 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
383 // Copy EFI_SIGNATURE_LIST header then calculate the signature count in this list.
385 CopyMem (Data
+ Offset
, CertList
, (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
));
386 NewCertList
= (EFI_SIGNATURE_LIST
*) (Data
+ Offset
);
387 Offset
+= (sizeof(EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
388 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
389 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
390 for (Index
= 0; Index
< CertCount
; Index
++) {
391 if (GuidIndex
== DeleteIndex
) {
395 NewCertList
->SignatureListSize
-= CertList
->SignatureSize
;
399 // This item doesn't match. Copy it to the Data buffer.
401 CopyMem (Data
+ Offset
, (UINT8
*)(Cert
), CertList
->SignatureSize
);
402 Offset
+= CertList
->SignatureSize
;
405 Cert
= (EFI_SIGNATURE_DATA
*) ((UINT8
*) Cert
+ CertList
->SignatureSize
);
409 // This List doesn't match. Just copy it to the Data buffer.
411 CopyMem (Data
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
412 Offset
+= CertList
->SignatureListSize
;
415 ItemDataSize
-= CertList
->SignatureListSize
;
416 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
421 // Doesn't find the signature Item!
423 Status
= EFI_NOT_FOUND
;
428 // Delete the EFI_SIGNATURE_LIST header if there is no signature in the list.
430 ItemDataSize
= Offset
;
431 CertList
= (EFI_SIGNATURE_LIST
*) Data
;
433 ZeroMem (OldData
, ItemDataSize
);
434 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
435 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
436 DEBUG ((DEBUG_INFO
, " CertCount = %x\n", CertCount
));
437 if (CertCount
!= 0) {
438 CopyMem (OldData
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
439 Offset
+= CertList
->SignatureListSize
;
441 ItemDataSize
-= CertList
->SignatureListSize
;
442 CertList
= (EFI_SIGNATURE_LIST
*) ((UINT8
*) CertList
+ CertList
->SignatureListSize
);
447 Status
= gRT
->SetVariable(
454 if (EFI_ERROR (Status
)) {
455 DEBUG ((DEBUG_ERROR
, "Failed to set variable, Status = %r\n", Status
));
464 if (OldData
!= NULL
) {
468 return UpdateDeletePage (
480 Clean the file related resource.
482 @param[in] Private Module's private data.
487 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
490 if (Private
->FileContext
->FHandle
!= NULL
) {
491 Private
->FileContext
->FHandle
->Close (Private
->FileContext
->FHandle
);
492 Private
->FileContext
->FHandle
= NULL
;
493 if (Private
->FileContext
->FileName
!= NULL
){
494 FreePool(Private
->FileContext
->FileName
);
495 Private
->FileContext
->FileName
= NULL
;
501 Read file content into BufferPtr, the size of the allocate buffer
502 is *FileSize plus AddtionAllocateSize.
504 @param[in] FileHandle The file to be read.
505 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
506 @param[out] FileSize Size of input file
507 @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.
508 In case the buffer need to contain others besides the file content.
510 @retval EFI_SUCCESS The file was read into the buffer.
511 @retval EFI_INVALID_PARAMETER A parameter was invalid.
512 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
513 @retval others Unexpected error.
518 IN EFI_FILE_HANDLE FileHandle
,
519 IN OUT VOID
**BufferPtr
,
521 IN UINTN AddtionAllocateSize
526 UINT64 SourceFileSize
;
530 if ((FileHandle
== NULL
) || (FileSize
== NULL
)) {
531 return EFI_INVALID_PARAMETER
;
539 Status
= FileHandle
->SetPosition (FileHandle
, (UINT64
) -1);
540 if (EFI_ERROR (Status
)) {
544 Status
= FileHandle
->GetPosition (FileHandle
, &SourceFileSize
);
545 if (EFI_ERROR (Status
)) {
549 Status
= FileHandle
->SetPosition (FileHandle
, 0);
550 if (EFI_ERROR (Status
)) {
554 BufferSize
= (UINTN
) SourceFileSize
+ AddtionAllocateSize
;
555 Buffer
= AllocateZeroPool(BufferSize
);
556 if (Buffer
== NULL
) {
557 return EFI_OUT_OF_RESOURCES
;
560 BufferSize
= (UINTN
) SourceFileSize
;
561 *FileSize
= BufferSize
;
563 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, Buffer
);
564 if (EFI_ERROR (Status
) || BufferSize
!= *FileSize
) {
567 Status
= EFI_BAD_BUFFER_SIZE
;
578 This function will open a file or directory referenced by DevicePath.
580 This function opens a file with the open mode according to the file path. The
581 Attributes is valid only for EFI_FILE_MODE_CREATE.
583 @param[in, out] FilePath On input, the device path to the file.
584 On output, the remaining device path.
585 @param[out] FileHandle Pointer to the file handle.
586 @param[in] OpenMode The mode to open the file with.
587 @param[in] Attributes The file's file attributes.
589 @retval EFI_SUCCESS The information was set.
590 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
591 @retval EFI_UNSUPPORTED Could not open the file path.
592 @retval EFI_NOT_FOUND The specified file could not be found on the
593 device or the file system could not be found on
595 @retval EFI_NO_MEDIA The device has no medium.
596 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the
597 medium is no longer supported.
598 @retval EFI_DEVICE_ERROR The device reported an error.
599 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
600 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
601 @retval EFI_ACCESS_DENIED The file was opened read only.
602 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the
604 @retval EFI_VOLUME_FULL The volume is full.
608 OpenFileByDevicePath (
609 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
610 OUT EFI_FILE_HANDLE
*FileHandle
,
616 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*EfiSimpleFileSystemProtocol
;
617 EFI_FILE_PROTOCOL
*Handle1
;
618 EFI_FILE_PROTOCOL
*Handle2
;
619 EFI_HANDLE DeviceHandle
;
621 if ((FilePath
== NULL
|| FileHandle
== NULL
)) {
622 return EFI_INVALID_PARAMETER
;
625 Status
= gBS
->LocateDevicePath (
626 &gEfiSimpleFileSystemProtocolGuid
,
630 if (EFI_ERROR (Status
)) {
634 Status
= gBS
->OpenProtocol(
636 &gEfiSimpleFileSystemProtocolGuid
,
637 (VOID
**)&EfiSimpleFileSystemProtocol
,
640 EFI_OPEN_PROTOCOL_GET_PROTOCOL
642 if (EFI_ERROR (Status
)) {
646 Status
= EfiSimpleFileSystemProtocol
->OpenVolume(EfiSimpleFileSystemProtocol
, &Handle1
);
647 if (EFI_ERROR (Status
)) {
653 // go down directories one node at a time.
655 while (!IsDevicePathEnd (*FilePath
)) {
657 // For file system access each node should be a file path component
659 if (DevicePathType (*FilePath
) != MEDIA_DEVICE_PATH
||
660 DevicePathSubType (*FilePath
) != MEDIA_FILEPATH_DP
663 return (EFI_INVALID_PARAMETER
);
666 // Open this file path node
672 // Try to test opening an existing file
674 Status
= Handle2
->Open (
677 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
678 OpenMode
&~EFI_FILE_MODE_CREATE
,
683 // see if the error was that it needs to be created
685 if ((EFI_ERROR (Status
)) && (OpenMode
!= (OpenMode
&~EFI_FILE_MODE_CREATE
))) {
686 Status
= Handle2
->Open (
689 ((FILEPATH_DEVICE_PATH
*)*FilePath
)->PathName
,
695 // Close the last node
697 Handle2
->Close (Handle2
);
699 if (EFI_ERROR(Status
)) {
706 *FilePath
= NextDevicePathNode (*FilePath
);
710 // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!
712 *FileHandle
= (VOID
*)Handle1
;
717 This function converts an input device structure to a Unicode string.
719 @param[in] DevPath A pointer to the device path structure.
721 @return A new allocated Unicode string that represents the device path.
727 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
730 return ConvertDevicePathToText (
739 Extract filename from device path. The returned buffer is allocated using AllocateCopyPool.
740 The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL
741 means not enough memory resource.
743 @param DevicePath Device path.
745 @retval NULL Not enough memory resourece for AllocateCopyPool.
746 @retval Other A new allocated string that represents the file name.
750 ExtractFileNameFromDevicePath (
751 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
760 ASSERT(DevicePath
!= NULL
);
762 String
= DevicePathToStr(DevicePath
);
763 MatchString
= String
;
767 while(MatchString
!= NULL
){
768 LastMatch
= MatchString
+ 1;
769 MatchString
= StrStr(LastMatch
,L
"\\");
772 Length
= StrLen(LastMatch
);
773 FileName
= AllocateCopyPool ((Length
+ 1) * sizeof(CHAR16
), LastMatch
);
774 if (FileName
!= NULL
) {
775 *(FileName
+ Length
) = 0;
784 Enroll a new X509 certificate into Variable.
786 @param[in] PrivateData The module's private data.
787 @param[in] VariableName Variable name of CA database.
789 @retval EFI_SUCCESS New X509 is enrolled successfully.
790 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
794 EnrollX509toVariable (
795 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
,
796 IN CHAR16
*VariableName
802 EFI_SIGNATURE_LIST
*CACert
;
803 EFI_SIGNATURE_DATA
*CACertData
;
817 Status
= ReadFileContent (
818 Private
->FileContext
->FHandle
,
823 if (EFI_ERROR (Status
)) {
826 ASSERT (X509Data
!= NULL
);
828 SigDataSize
= sizeof(EFI_SIGNATURE_LIST
) + sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
;
830 Data
= AllocateZeroPool (SigDataSize
);
832 Status
= EFI_OUT_OF_RESOURCES
;
837 // Fill Certificate Database parameters.
839 CACert
= (EFI_SIGNATURE_LIST
*) Data
;
840 CACert
->SignatureListSize
= (UINT32
) SigDataSize
;
841 CACert
->SignatureHeaderSize
= 0;
842 CACert
->SignatureSize
= (UINT32
) (sizeof(EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
843 CopyGuid (&CACert
->SignatureType
, &gEfiCertX509Guid
);
845 CACertData
= (EFI_SIGNATURE_DATA
*) ((UINT8
* ) CACert
+ sizeof (EFI_SIGNATURE_LIST
));
846 CopyGuid (&CACertData
->SignatureOwner
, Private
->CertGuid
);
847 CopyMem ((UINT8
* ) (CACertData
->SignatureData
), X509Data
, X509DataSize
);
850 // Check if signature database entry has been already existed.
851 // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the
852 // new signature data to original variable
854 Attr
= TLS_AUTH_CONFIG_VAR_BASE_ATTR
;
856 Status
= gRT
->GetVariable(
858 &gEfiTlsCaCertificateGuid
,
863 if (Status
== EFI_BUFFER_TOO_SMALL
) {
864 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
865 } else if (Status
!= EFI_NOT_FOUND
) {
869 Status
= gRT
->SetVariable(
871 &gEfiTlsCaCertificateGuid
,
876 if (EFI_ERROR (Status
)) {
881 CleanFileContext (Private
);
883 if (Private
->CertGuid
!= NULL
) {
884 FreePool (Private
->CertGuid
);
885 Private
->CertGuid
= NULL
;
892 if (X509Data
!= NULL
) {
900 Enroll Cert into TlsCaCertificate. The GUID will be Private->CertGuid.
902 @param[in] PrivateData The module's private data.
903 @param[in] VariableName Variable name of signature database.
905 @retval EFI_SUCCESS New Cert enrolled successfully.
906 @retval EFI_INVALID_PARAMETER The parameter is invalid.
907 @retval EFI_UNSUPPORTED The Cert file is unsupported type.
908 @retval others Fail to enroll Cert data.
913 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
,
914 IN CHAR16
*VariableName
920 if ((Private
->FileContext
->FileName
== NULL
) || (Private
->FileContext
->FHandle
== NULL
) || (Private
->CertGuid
== NULL
)) {
921 return EFI_INVALID_PARAMETER
;
925 // Parse the file's postfix.
927 NameLength
= StrLen (Private
->FileContext
->FileName
);
928 if (NameLength
<= 4) {
929 return EFI_INVALID_PARAMETER
;
931 FilePostFix
= Private
->FileContext
->FileName
+ NameLength
- 4;
933 if (IsDerPemEncodeCertificate (FilePostFix
)) {
935 // Supports DER-encoded X509 certificate.
937 return EnrollX509toVariable (Private
, VariableName
);
940 return EFI_UNSUPPORTED
;
944 Refresh the global UpdateData structure.
953 // Free current updated date
955 if (mStartOpCodeHandle
!= NULL
) {
956 HiiFreeOpCodeHandle (mStartOpCodeHandle
);
960 // Create new OpCode Handle
962 mStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
965 // Create Hii Extend Label OpCode as the start opcode
967 mStartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
971 sizeof (EFI_IFR_GUID_LABEL
)
973 mStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
977 Clean up the dynamic opcode at label and form specified by both LabelId.
979 @param[in] LabelId It is both the Form ID and Label ID for opcode deletion.
980 @param[in] PrivateData Module private data.
986 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*PrivateData
989 RefreshUpdateData ();
992 // Remove all op-codes from dynamic page
994 mStartLabel
->Number
= LabelId
;
996 PrivateData
->RegisteredHandle
,
999 mStartOpCodeHandle
, // Label LabelId
1000 mEndOpCodeHandle
// LABEL_END
1005 Update the form base on the selected file.
1007 @param FilePath Point to the file path.
1008 @param FormId The form need to display.
1010 @retval TRUE Exit caller function.
1011 @retval FALSE Not exit caller function.
1016 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1017 IN EFI_FORM_ID FormId
1021 EFI_STRING_ID StringToken
;
1025 if (FilePath
!= NULL
) {
1026 FileName
= ExtractFileNameFromDevicePath(FilePath
);
1028 if (FileName
== NULL
) {
1030 // FileName = NULL has two case:
1031 // 1. FilePath == NULL, not select file.
1032 // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource.
1033 // In these two case, no need to update the form, and exit the caller function.
1037 StringToken
= HiiSetString (mTlsAuthPrivateData
->RegisteredHandle
, 0, FileName
, NULL
);
1039 mTlsAuthPrivateData
->FileContext
->FileName
= FileName
;
1041 OpenFileByDevicePath (
1043 &mTlsAuthPrivateData
->FileContext
->FHandle
,
1048 // Create Subtitle op-code for the display string of the option.
1050 RefreshUpdateData ();
1051 mStartLabel
->Number
= FormId
;
1053 HiiCreateSubTitleOpCode (
1062 mTlsAuthPrivateData
->RegisteredHandle
,
1063 &gTlsAuthConfigGuid
,
1065 mStartOpCodeHandle
, /// Label FormId
1066 mEndOpCodeHandle
/// LABEL_END
1073 Update the form base on the input file path info.
1075 @param FilePath Point to the file path.
1077 @retval TRUE Exit caller function.
1078 @retval FALSE Not exit caller function.
1083 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1086 return UpdatePage(FilePath
, TLS_AUTH_CONFIG_FORMID4_FORM
);
1090 Unload the configuration form, this includes: delete all the configuration
1091 entries, uninstall the form callback protocol, and free the resources used.
1093 @param[in] Private Pointer to the driver private data.
1095 @retval EFI_SUCCESS The configuration form is unloaded.
1096 @retval Others Failed to unload the form.
1100 TlsAuthConfigFormUnload (
1101 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
1104 if (Private
->DriverHandle
!= NULL
) {
1106 // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
1108 gBS
->UninstallMultipleProtocolInterfaces (
1109 Private
->DriverHandle
,
1110 &gEfiDevicePathProtocolGuid
,
1111 &mTlsAuthConfigHiiVendorDevicePath
,
1112 &gEfiHiiConfigAccessProtocolGuid
,
1113 &Private
->ConfigAccess
,
1116 Private
->DriverHandle
= NULL
;
1119 if (Private
->RegisteredHandle
!= NULL
) {
1121 // Remove HII package list
1123 HiiRemovePackages (Private
->RegisteredHandle
);
1124 Private
->RegisteredHandle
= NULL
;
1127 if (Private
->CertGuid
!= NULL
) {
1128 FreePool (Private
->CertGuid
);
1131 if (Private
->FileContext
!= NULL
) {
1132 FreePool (Private
->FileContext
);
1137 if (mStartOpCodeHandle
!= NULL
) {
1138 HiiFreeOpCodeHandle (mStartOpCodeHandle
);
1141 if (mEndOpCodeHandle
!= NULL
) {
1142 HiiFreeOpCodeHandle (mEndOpCodeHandle
);
1150 Initialize the configuration form.
1152 @param[in] Private Pointer to the driver private data.
1154 @retval EFI_SUCCESS The configuration form is initialized.
1155 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1159 TlsAuthConfigFormInit (
1160 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
1165 Private
->Signature
= TLS_AUTH_CONFIG_PRIVATE_DATA_SIGNATURE
;
1167 Private
->ConfigAccess
.ExtractConfig
= TlsAuthConfigAccessExtractConfig
;
1168 Private
->ConfigAccess
.RouteConfig
= TlsAuthConfigAccessRouteConfig
;
1169 Private
->ConfigAccess
.Callback
= TlsAuthConfigAccessCallback
;
1172 // Install Device Path Protocol and Config Access protocol to driver handle.
1174 Status
= gBS
->InstallMultipleProtocolInterfaces (
1175 &Private
->DriverHandle
,
1176 &gEfiDevicePathProtocolGuid
,
1177 &mTlsAuthConfigHiiVendorDevicePath
,
1178 &gEfiHiiConfigAccessProtocolGuid
,
1179 &Private
->ConfigAccess
,
1182 if (EFI_ERROR (Status
)) {
1187 // Publish our HII data.
1189 Private
->RegisteredHandle
= HiiAddPackages (
1190 &gTlsAuthConfigGuid
,
1191 Private
->DriverHandle
,
1192 TlsAuthConfigDxeStrings
,
1193 TlsAuthConfigVfrBin
,
1196 if (Private
->RegisteredHandle
== NULL
) {
1197 Status
= EFI_OUT_OF_RESOURCES
;
1201 Private
->FileContext
= AllocateZeroPool (sizeof (TLS_AUTH_CONFIG_FILE_CONTEXT
));
1202 if (Private
->FileContext
== NULL
) {
1203 Status
= EFI_OUT_OF_RESOURCES
;
1208 // Init OpCode Handle and Allocate space for creation of Buffer
1210 mStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1211 if (mStartOpCodeHandle
== NULL
) {
1212 Status
= EFI_OUT_OF_RESOURCES
;
1216 mEndOpCodeHandle
= HiiAllocateOpCodeHandle ();
1217 if (mEndOpCodeHandle
== NULL
) {
1218 Status
= EFI_OUT_OF_RESOURCES
;
1223 // Create Hii Extend Label OpCode as the start opcode
1225 mStartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1229 sizeof (EFI_IFR_GUID_LABEL
)
1231 mStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1234 // Create Hii Extend Label OpCode as the end opcode
1236 mEndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1240 sizeof (EFI_IFR_GUID_LABEL
)
1242 mEndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1243 mEndLabel
->Number
= LABEL_END
;
1248 TlsAuthConfigFormUnload (Private
);
1254 This function allows the caller to request the current
1255 configuration for one or more named elements. The resulting
1256 string is in <ConfigAltResp> format. Any and all alternative
1257 configuration strings shall also be appended to the end of the
1258 current configuration string. If they are, they must appear
1259 after the current configuration. They must contain the same
1260 routing (GUID, NAME, PATH) as the current configuration string.
1261 They must have an additional description indicating the type of
1262 alternative configuration the string represents,
1263 "ALTCFG=<StringToken>". That <StringToken> (when
1264 converted from Hex UNICODE to binary) is a reference to a
1265 string in the associated string pack.
1267 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1269 @param Request A null-terminated Unicode string in
1270 <ConfigRequest> format. Note that this
1271 includes the routing information as well as
1272 the configurable name / value pairs. It is
1273 invalid for this string to be in
1274 <MultiConfigRequest> format.
1275 If a NULL is passed in for the Request field,
1276 all of the settings being abstracted by this function
1277 will be returned in the Results field. In addition,
1278 if a ConfigHdr is passed in with no request elements,
1279 all of the settings being abstracted for that particular
1280 ConfigHdr reference will be returned in the Results Field.
1282 @param Progress On return, points to a character in the
1283 Request string. Points to the string's null
1284 terminator if request was successful. Points
1285 to the most recent "&" before the first
1286 failing name / value pair (or the beginning
1287 of the string if the failure is in the first
1288 name / value pair) if the request was not
1291 @param Results A null-terminated Unicode string in
1292 <MultiConfigAltResp> format which has all values
1293 filled in for the names in the Request string.
1294 String to be allocated by the called function.
1296 @retval EFI_SUCCESS The Results string is filled with the
1297 values corresponding to all requested
1300 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
1301 parts of the results that must be
1302 stored awaiting possible future
1305 @retval EFI_NOT_FOUND Routing data doesn't match any
1306 known driver. Progress set to the
1307 first character in the routing header.
1308 Note: There is no requirement that the
1309 driver validate the routing data. It
1310 must skip the <ConfigHdr> in order to
1313 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
1314 to most recent "&" before the
1315 error or the beginning of the
1318 @retval EFI_INVALID_PARAMETER Unknown name. Progress points
1319 to the & before the name in
1325 TlsAuthConfigAccessExtractConfig (
1326 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
1327 IN CONST EFI_STRING Request
,
1328 OUT EFI_STRING
*Progress
,
1329 OUT EFI_STRING
*Results
1335 EFI_STRING ConfigRequest
;
1336 EFI_STRING ConfigRequestHdr
;
1337 TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
;
1338 BOOLEAN AllocatedRequest
;
1340 if (Progress
== NULL
|| Results
== NULL
) {
1341 return EFI_INVALID_PARAMETER
;
1344 AllocatedRequest
= FALSE
;
1345 ConfigRequestHdr
= NULL
;
1346 ConfigRequest
= NULL
;
1349 Private
= TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This
);
1351 BufferSize
= sizeof (TLS_AUTH_CONFIG_IFR_NVDATA
);
1352 ZeroMem (&Private
->TlsAuthConfigNvData
, BufferSize
);
1354 *Progress
= Request
;
1356 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
)) {
1357 return EFI_NOT_FOUND
;
1360 ConfigRequest
= Request
;
1361 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
1363 // Request is set to NULL or OFFSET is NULL, construct full request string.
1365 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
1366 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
1368 ConfigRequestHdr
= HiiConstructConfigHdr (&gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
, Private
->DriverHandle
);
1369 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
1370 ConfigRequest
= AllocateZeroPool (Size
);
1371 ASSERT (ConfigRequest
!= NULL
);
1372 AllocatedRequest
= TRUE
;
1373 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
1374 FreePool (ConfigRequestHdr
);
1375 ConfigRequestHdr
= NULL
;
1378 Status
= gHiiConfigRouting
->BlockToConfig (
1381 (UINT8
*) &Private
->TlsAuthConfigNvData
,
1388 // Free the allocated config request string.
1390 if (AllocatedRequest
) {
1391 FreePool (ConfigRequest
);
1395 // Set Progress string to the original request string.
1397 if (Request
== NULL
) {
1399 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
1400 *Progress
= Request
+ StrLen (Request
);
1408 This function applies changes in a driver's configuration.
1409 Input is a Configuration, which has the routing data for this
1410 driver followed by name / value configuration pairs. The driver
1411 must apply those pairs to its configurable storage. If the
1412 driver's configuration is stored in a linear block of data
1413 and the driver's name / value pairs are in <BlockConfig>
1414 format, it may use the ConfigToBlock helper function (above) to
1417 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1419 @param Configuration A null-terminated Unicode string in
1420 <ConfigString> format.
1422 @param Progress A pointer to a string filled in with the
1423 offset of the most recent '&' before the
1424 first failing name / value pair (or the
1425 beginn ing of the string if the failure
1426 is in the first name / value pair) or
1427 the terminating NULL if all was
1430 @retval EFI_SUCCESS The results have been distributed or are
1431 awaiting distribution.
1433 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
1434 parts of the results that must be
1435 stored awaiting possible future
1438 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
1439 Results parameter would result
1440 in this type of error.
1442 @retval EFI_NOT_FOUND Target for the specified routing data
1448 TlsAuthConfigAccessRouteConfig (
1449 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
1450 IN CONST EFI_STRING Configuration
,
1451 OUT EFI_STRING
*Progress
1456 TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
;
1458 if (Progress
== NULL
) {
1459 return EFI_INVALID_PARAMETER
;
1461 *Progress
= Configuration
;
1463 if (Configuration
== NULL
) {
1464 return EFI_INVALID_PARAMETER
;
1468 // Check routing data in <ConfigHdr>.
1469 // Note: there is no name for Name/Value storage, only GUID will be checked
1471 if (!HiiIsConfigHdrMatch (Configuration
, &gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
)) {
1472 return EFI_NOT_FOUND
;
1475 Private
= TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This
);
1477 BufferSize
= sizeof (TLS_AUTH_CONFIG_IFR_NVDATA
);
1478 ZeroMem (&Private
->TlsAuthConfigNvData
, BufferSize
);
1480 Status
= gHiiConfigRouting
->ConfigToBlock (
1483 (UINT8
*) &Private
->TlsAuthConfigNvData
,
1487 if (EFI_ERROR (Status
)) {
1496 This function is called to provide results data to the driver.
1497 This data consists of a unique key that is used to identify
1498 which data is either being passed back or being asked for.
1500 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1501 @param Action Specifies the type of action taken by the browser.
1502 @param QuestionId A unique value which is sent to the original
1503 exporting driver so that it can identify the type
1504 of data to expect. The format of the data tends to
1505 vary based on the opcode that generated the callback.
1506 @param Type The type of value for the question.
1507 @param Value A pointer to the data being sent to the original
1509 @param ActionRequest On return, points to the action requested by the
1512 @retval EFI_SUCCESS The callback successfully handled the action.
1513 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
1514 variable and its data.
1515 @retval EFI_DEVICE_ERROR The variable could not be saved.
1516 @retval EFI_UNSUPPORTED The specified Action is not supported by the
1521 TlsAuthConfigAccessCallback (
1522 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
1523 IN EFI_BROWSER_ACTION Action
,
1524 IN EFI_QUESTION_ID QuestionId
,
1526 IN OUT EFI_IFR_TYPE_VALUE
*Value
,
1527 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
1532 RETURN_STATUS RStatus
;
1533 TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
;
1535 TLS_AUTH_CONFIG_IFR_NVDATA
*IfrNvData
;
1537 EFI_DEVICE_PATH_PROTOCOL
*File
;
1539 Status
= EFI_SUCCESS
;
1542 if ((This
== NULL
) || (Value
== NULL
) || (ActionRequest
== NULL
)) {
1543 return EFI_INVALID_PARAMETER
;
1546 Private
= TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This
);
1548 mTlsAuthPrivateData
= Private
;
1551 // Retrieve uncommitted data from Browser
1553 BufferSize
= sizeof (TLS_AUTH_CONFIG_IFR_NVDATA
);
1554 IfrNvData
= AllocateZeroPool (BufferSize
);
1555 if (IfrNvData
== NULL
) {
1556 return EFI_OUT_OF_RESOURCES
;
1559 HiiGetBrowserData (&gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
, BufferSize
, (UINT8
*) IfrNvData
);
1561 if ((Action
!= EFI_BROWSER_ACTION_CHANGED
) &&
1562 (Action
!= EFI_BROWSER_ACTION_CHANGING
) &&
1563 (Action
!= EFI_BROWSER_ACTION_FORM_CLOSE
)) {
1564 Status
= EFI_UNSUPPORTED
;
1568 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
1569 switch (QuestionId
) {
1570 case KEY_TLS_AUTH_CONFIG_CLIENT_CERT
:
1571 case KEY_TLS_AUTH_CONFIG_SERVER_CA
:
1575 ZeroMem (IfrNvData
->CertGuid
, sizeof (IfrNvData
->CertGuid
));
1576 if (Private
->CertGuid
== NULL
) {
1577 Private
->CertGuid
= (EFI_GUID
*) AllocateZeroPool (sizeof (EFI_GUID
));
1578 if (Private
->CertGuid
== NULL
) {
1579 return EFI_OUT_OF_RESOURCES
;
1582 if (QuestionId
== KEY_TLS_AUTH_CONFIG_CLIENT_CERT
) {
1583 LabelId
= TLS_AUTH_CONFIG_FORMID3_FORM
;
1585 LabelId
= TLS_AUTH_CONFIG_FORMID4_FORM
;
1589 // Refresh selected file.
1591 CleanUpPage (LabelId
, Private
);
1593 case KEY_TLS_AUTH_CONFIG_ENROLL_CERT_FROM_FILE
:
1595 // If the file is already opened, clean the file related resource first.
1597 CleanFileContext (Private
);
1599 ChooseFile( NULL
, NULL
, UpdateCAFromFile
, &File
);
1602 case KEY_TLS_AUTH_CONFIG_VALUE_SAVE_AND_EXIT
:
1603 Status
= EnrollCertDatabase (Private
, EFI_TLS_CA_CERTIFICATE_VARIABLE
);
1604 if (EFI_ERROR (Status
)) {
1605 CleanFileContext (Private
);
1608 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
1610 L
"ERROR: Enroll Cert Failure!",
1616 case KEY_TLS_AUTH_CONFIG_VALUE_NO_SAVE_AND_EXIT
:
1617 CleanFileContext (Private
);
1619 if (Private
->CertGuid
!= NULL
) {
1620 FreePool (Private
->CertGuid
);
1621 Private
->CertGuid
= NULL
;
1625 case KEY_TLS_AUTH_CONFIG_DELETE_CERT
:
1628 EFI_TLS_CA_CERTIFICATE_VARIABLE
,
1629 &gEfiTlsCaCertificateGuid
,
1631 TLS_AUTH_CONFIG_FORMID5_FORM
,
1632 OPTION_DEL_CA_ESTION_ID
1637 if ((QuestionId
>= OPTION_DEL_CA_ESTION_ID
) &&
1638 (QuestionId
< (OPTION_DEL_CA_ESTION_ID
+ OPTION_CONFIG_RANGE
))) {
1641 EFI_TLS_CA_CERTIFICATE_VARIABLE
,
1642 &gEfiTlsCaCertificateGuid
,
1644 TLS_AUTH_CONFIG_FORMID5_FORM
,
1645 OPTION_DEL_CA_ESTION_ID
,
1646 QuestionId
- OPTION_DEL_CA_ESTION_ID
1651 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
1652 switch (QuestionId
) {
1653 case KEY_TLS_AUTH_CONFIG_CERT_GUID
:
1654 ASSERT (Private
->CertGuid
!= NULL
);
1655 RStatus
= StrToGuid (
1656 IfrNvData
->CertGuid
,
1659 if (RETURN_ERROR (RStatus
) || (IfrNvData
->CertGuid
[GUID_STRING_LENGTH
] != L
'\0')) {
1660 Status
= EFI_INVALID_PARAMETER
;
1664 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
1669 } else if (Action
== EFI_BROWSER_ACTION_FORM_CLOSE
) {
1670 CleanFileContext (Private
);
1675 if (!EFI_ERROR (Status
)) {
1676 BufferSize
= sizeof (TLS_AUTH_CONFIG_IFR_NVDATA
);
1677 HiiSetBrowserData (&gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
, BufferSize
, (UINT8
*) IfrNvData
, NULL
);
1680 FreePool (IfrNvData
);