2 The Miscellaneous Routines for TlsAuthConfigDxe driver.
4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "TlsAuthConfigImpl.h"
12 VOID
*mStartOpCodeHandle
= NULL
;
13 VOID
*mEndOpCodeHandle
= NULL
;
14 EFI_IFR_GUID_LABEL
*mStartLabel
= NULL
;
15 EFI_IFR_GUID_LABEL
*mEndLabel
= NULL
;
17 CHAR16 mTlsAuthConfigStorageName
[] = L
"TLS_AUTH_CONFIG_IFR_NVDATA";
19 TLS_AUTH_CONFIG_PRIVATE_DATA
*mTlsAuthPrivateData
= NULL
;
21 HII_VENDOR_DEVICE_PATH mTlsAuthConfigHiiVendorDevicePath
= {
27 (UINT8
)(sizeof (VENDOR_DEVICE_PATH
)),
28 (UINT8
)((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
35 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
37 (UINT8
)(END_DEVICE_PATH_LENGTH
),
38 (UINT8
)((END_DEVICE_PATH_LENGTH
) >> 8)
44 // Possible DER-encoded certificate file suffixes, end with NULL pointer.
46 CHAR16
*mDerPemEncodedSuffix
[] = {
55 This code checks if the FileSuffix is one of the possible DER/PEM-encoded certificate suffix.
57 @param[in] FileSuffix The suffix of the input certificate file
59 @retval TRUE It's a DER/PEM-encoded certificate.
60 @retval FALSE It's NOT a DER/PEM-encoded certificate.
64 IsDerPemEncodeCertificate (
65 IN CONST CHAR16
*FileSuffix
70 for (Index
= 0; mDerPemEncodedSuffix
[Index
] != NULL
; Index
++) {
71 if (StrCmp (FileSuffix
, mDerPemEncodedSuffix
[Index
]) == 0) {
80 Worker function that prints an EFI_GUID into specified Buffer.
82 @param[in] Guid Pointer to GUID to print.
83 @param[in] Buffer Buffer to print Guid into.
84 @param[in] BufferSize Size of Buffer.
86 @retval Number of characters printed.
96 return UnicodeSPrint (
105 List all cert in specified database by GUID in the page
106 for user to select and delete as needed.
108 @param[in] PrivateData Module's private data.
109 @param[in] VariableName The variable name of the vendor's signature database.
110 @param[in] VendorGuid A unique identifier for the vendor.
111 @param[in] LabelNumber Label number to insert opcodes.
112 @param[in] FormId Form ID of current page.
113 @param[in] QuestionIdBase Base question id of the signature list.
115 @retval EFI_SUCCESS Success to update the signature list page
116 @retval EFI_OUT_OF_RESOURCES Unable to allocate required resources.
121 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
,
122 IN CHAR16
*VariableName
,
123 IN EFI_GUID
*VendorGuid
,
124 IN UINT16 LabelNumber
,
125 IN EFI_FORM_ID FormId
,
126 IN EFI_QUESTION_ID QuestionIdBase
133 VOID
*StartOpCodeHandle
;
134 VOID
*EndOpCodeHandle
;
135 EFI_IFR_GUID_LABEL
*StartLabel
;
136 EFI_IFR_GUID_LABEL
*EndLabel
;
139 EFI_SIGNATURE_LIST
*CertList
;
140 EFI_SIGNATURE_DATA
*Cert
;
143 EFI_STRING_ID GuidID
;
150 StartOpCodeHandle
= NULL
;
151 EndOpCodeHandle
= NULL
;
154 // Initialize the container for dynamic opcodes.
156 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
157 if (StartOpCodeHandle
== NULL
) {
158 Status
= EFI_OUT_OF_RESOURCES
;
162 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
163 if (EndOpCodeHandle
== NULL
) {
164 Status
= EFI_OUT_OF_RESOURCES
;
169 // Create Hii Extend Label OpCode.
171 StartLabel
= (EFI_IFR_GUID_LABEL
*)HiiCreateGuidOpCode (
175 sizeof (EFI_IFR_GUID_LABEL
)
177 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
178 StartLabel
->Number
= LabelNumber
;
180 EndLabel
= (EFI_IFR_GUID_LABEL
*)HiiCreateGuidOpCode (
184 sizeof (EFI_IFR_GUID_LABEL
)
186 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
187 EndLabel
->Number
= LABEL_END
;
193 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
194 if (EFI_ERROR (Status
) && (Status
!= EFI_BUFFER_TOO_SMALL
)) {
198 Data
= (UINT8
*)AllocateZeroPool (DataSize
);
200 Status
= EFI_OUT_OF_RESOURCES
;
204 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, Data
);
205 if (EFI_ERROR (Status
)) {
209 GuidStr
= AllocateZeroPool (100);
210 if (GuidStr
== NULL
) {
211 Status
= EFI_OUT_OF_RESOURCES
;
216 // Enumerate all data.
218 ItemDataSize
= (UINT32
)DataSize
;
219 CertList
= (EFI_SIGNATURE_LIST
*)Data
;
222 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
223 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
224 Help
= STRING_TOKEN (STR_CERT_TYPE_PCKS_GUID
);
227 // The signature type is not supported in current implementation.
229 ItemDataSize
-= CertList
->SignatureListSize
;
230 CertList
= (EFI_SIGNATURE_LIST
*)((UINT8
*)CertList
+ CertList
->SignatureListSize
);
234 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
235 for (Index
= 0; Index
< CertCount
; Index
++) {
236 Cert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)CertList
237 + sizeof (EFI_SIGNATURE_LIST
)
238 + CertList
->SignatureHeaderSize
239 + Index
* CertList
->SignatureSize
);
241 // Display GUID and help
243 GuidToString (&Cert
->SignatureOwner
, GuidStr
, 100);
244 GuidID
= HiiSetString (Private
->RegisteredHandle
, 0, GuidStr
, NULL
);
245 HiiCreateCheckBoxOpCode (
247 (EFI_QUESTION_ID
)(QuestionIdBase
+ GuidIndex
++),
252 EFI_IFR_FLAG_CALLBACK
,
258 ItemDataSize
-= CertList
->SignatureListSize
;
259 CertList
= (EFI_SIGNATURE_LIST
*)((UINT8
*)CertList
+ CertList
->SignatureListSize
);
264 Private
->RegisteredHandle
,
271 if (StartOpCodeHandle
!= NULL
) {
272 HiiFreeOpCodeHandle (StartOpCodeHandle
);
275 if (EndOpCodeHandle
!= NULL
) {
276 HiiFreeOpCodeHandle (EndOpCodeHandle
);
283 if (GuidStr
!= NULL
) {
291 Delete one entry from cert database.
293 @param[in] Private Module's private data.
294 @param[in] VariableName The variable name of the database.
295 @param[in] VendorGuid A unique identifier for the vendor.
296 @param[in] LabelNumber Label number to insert opcodes.
297 @param[in] FormId Form ID of current page.
298 @param[in] QuestionIdBase Base question id of the cert list.
299 @param[in] DeleteIndex Cert index to delete.
301 @retval EFI_SUCCESS Delete signature successfully.
302 @retval EFI_NOT_FOUND Can't find the signature item,
303 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
307 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
,
308 IN CHAR16
*VariableName
,
309 IN EFI_GUID
*VendorGuid
,
310 IN UINT16 LabelNumber
,
311 IN EFI_FORM_ID FormId
,
312 IN EFI_QUESTION_ID QuestionIdBase
,
322 EFI_SIGNATURE_LIST
*CertList
;
323 EFI_SIGNATURE_LIST
*NewCertList
;
324 EFI_SIGNATURE_DATA
*Cert
;
338 // Get original signature list data.
341 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, NULL
, &DataSize
, NULL
);
342 if (EFI_ERROR (Status
) && (Status
!= EFI_BUFFER_TOO_SMALL
)) {
346 OldData
= (UINT8
*)AllocateZeroPool (DataSize
);
347 if (OldData
== NULL
) {
348 Status
= EFI_OUT_OF_RESOURCES
;
352 Status
= gRT
->GetVariable (VariableName
, VendorGuid
, &Attr
, &DataSize
, OldData
);
353 if (EFI_ERROR (Status
)) {
358 // Allocate space for new variable.
360 Data
= (UINT8
*)AllocateZeroPool (DataSize
);
362 Status
= EFI_OUT_OF_RESOURCES
;
367 // Enumerate all data and erasing the target item.
370 ItemDataSize
= (UINT32
)DataSize
;
371 CertList
= (EFI_SIGNATURE_LIST
*)OldData
;
374 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
375 if (CompareGuid (&CertList
->SignatureType
, &gEfiCertX509Guid
)) {
377 // Copy EFI_SIGNATURE_LIST header then calculate the signature count in this list.
379 CopyMem (Data
+ Offset
, CertList
, (sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
));
380 NewCertList
= (EFI_SIGNATURE_LIST
*)(Data
+ Offset
);
381 Offset
+= (sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
382 Cert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)CertList
+ sizeof (EFI_SIGNATURE_LIST
) + CertList
->SignatureHeaderSize
);
383 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
384 for (Index
= 0; Index
< CertCount
; Index
++) {
385 if (GuidIndex
== DeleteIndex
) {
389 NewCertList
->SignatureListSize
-= CertList
->SignatureSize
;
393 // This item doesn't match. Copy it to the Data buffer.
395 CopyMem (Data
+ Offset
, (UINT8
*)(Cert
), CertList
->SignatureSize
);
396 Offset
+= CertList
->SignatureSize
;
400 Cert
= (EFI_SIGNATURE_DATA
*)((UINT8
*)Cert
+ CertList
->SignatureSize
);
404 // This List doesn't match. Just copy it to the Data buffer.
406 CopyMem (Data
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
407 Offset
+= CertList
->SignatureListSize
;
410 ItemDataSize
-= CertList
->SignatureListSize
;
411 CertList
= (EFI_SIGNATURE_LIST
*)((UINT8
*)CertList
+ CertList
->SignatureListSize
);
416 // Doesn't find the signature Item!
418 Status
= EFI_NOT_FOUND
;
423 // Delete the EFI_SIGNATURE_LIST header if there is no signature in the list.
425 ItemDataSize
= Offset
;
426 CertList
= (EFI_SIGNATURE_LIST
*)Data
;
428 ZeroMem (OldData
, ItemDataSize
);
429 while ((ItemDataSize
> 0) && (ItemDataSize
>= CertList
->SignatureListSize
)) {
430 CertCount
= (CertList
->SignatureListSize
- sizeof (EFI_SIGNATURE_LIST
) - CertList
->SignatureHeaderSize
) / CertList
->SignatureSize
;
431 DEBUG ((DEBUG_INFO
, " CertCount = %x\n", CertCount
));
432 if (CertCount
!= 0) {
433 CopyMem (OldData
+ Offset
, (UINT8
*)(CertList
), CertList
->SignatureListSize
);
434 Offset
+= CertList
->SignatureListSize
;
437 ItemDataSize
-= CertList
->SignatureListSize
;
438 CertList
= (EFI_SIGNATURE_LIST
*)((UINT8
*)CertList
+ CertList
->SignatureListSize
);
443 Status
= gRT
->SetVariable (
450 if (EFI_ERROR (Status
)) {
451 DEBUG ((DEBUG_ERROR
, "Failed to set variable, Status = %r\n", Status
));
460 if (OldData
!= NULL
) {
464 return UpdateDeletePage (
475 Clean the file related resource.
477 @param[in] Private Module's private data.
482 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
485 if (Private
->FileContext
->FHandle
!= NULL
) {
486 Private
->FileContext
->FHandle
->Close (Private
->FileContext
->FHandle
);
487 Private
->FileContext
->FHandle
= NULL
;
488 if (Private
->FileContext
->FileName
!= NULL
) {
489 FreePool (Private
->FileContext
->FileName
);
490 Private
->FileContext
->FileName
= NULL
;
496 Read file content into BufferPtr, the size of the allocate buffer
497 is *FileSize plus AddtionAllocateSize.
499 @param[in] FileHandle The file to be read.
500 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
501 @param[out] FileSize Size of input file
502 @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.
503 In case the buffer need to contain others besides the file content.
505 @retval EFI_SUCCESS The file was read into the buffer.
506 @retval EFI_INVALID_PARAMETER A parameter was invalid.
507 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
508 @retval others Unexpected error.
513 IN EFI_FILE_HANDLE FileHandle
,
514 IN OUT VOID
**BufferPtr
,
516 IN UINTN AddtionAllocateSize
521 UINT64 SourceFileSize
;
525 if ((FileHandle
== NULL
) || (FileSize
== NULL
)) {
526 return EFI_INVALID_PARAMETER
;
534 Status
= FileHandle
->SetPosition (FileHandle
, (UINT64
)-1);
535 if (EFI_ERROR (Status
)) {
539 Status
= FileHandle
->GetPosition (FileHandle
, &SourceFileSize
);
540 if (EFI_ERROR (Status
)) {
544 Status
= FileHandle
->SetPosition (FileHandle
, 0);
545 if (EFI_ERROR (Status
)) {
549 BufferSize
= (UINTN
)SourceFileSize
+ AddtionAllocateSize
;
550 Buffer
= AllocateZeroPool (BufferSize
);
551 if (Buffer
== NULL
) {
552 return EFI_OUT_OF_RESOURCES
;
555 BufferSize
= (UINTN
)SourceFileSize
;
556 *FileSize
= BufferSize
;
558 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, Buffer
);
559 if (EFI_ERROR (Status
) || (BufferSize
!= *FileSize
)) {
562 Status
= EFI_BAD_BUFFER_SIZE
;
573 This function converts an input device structure to a Unicode string.
575 @param[in] DevPath A pointer to the device path structure.
577 @return A new allocated Unicode string that represents the device path.
583 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
586 return ConvertDevicePathToText (
594 Extract filename from device path. The returned buffer is allocated using AllocateCopyPool.
595 The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL
596 means not enough memory resource.
598 @param DevicePath Device path.
600 @retval NULL Not enough memory resource for AllocateCopyPool.
601 @retval Other A new allocated string that represents the file name.
605 ExtractFileNameFromDevicePath (
606 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
615 ASSERT (DevicePath
!= NULL
);
617 String
= DevicePathToStr (DevicePath
);
618 MatchString
= String
;
622 while (MatchString
!= NULL
) {
623 LastMatch
= MatchString
+ 1;
624 MatchString
= StrStr (LastMatch
, L
"\\");
627 Length
= StrLen (LastMatch
);
628 FileName
= AllocateCopyPool ((Length
+ 1) * sizeof (CHAR16
), LastMatch
);
629 if (FileName
!= NULL
) {
630 *(FileName
+ Length
) = 0;
639 Enroll a new X509 certificate into Variable.
641 @param[in] PrivateData The module's private data.
642 @param[in] VariableName Variable name of CA database.
644 @retval EFI_SUCCESS New X509 is enrolled successfully.
645 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
649 EnrollX509toVariable (
650 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
,
651 IN CHAR16
*VariableName
657 EFI_SIGNATURE_LIST
*CACert
;
658 EFI_SIGNATURE_DATA
*CACertData
;
673 Status
= ReadFileContent (
674 Private
->FileContext
->FHandle
,
679 if (EFI_ERROR (Status
)) {
683 ASSERT (X509Data
!= NULL
);
685 SigDataSize
= sizeof (EFI_SIGNATURE_LIST
) + sizeof (EFI_SIGNATURE_DATA
) - 1 + X509DataSize
;
687 Data
= AllocateZeroPool (SigDataSize
);
689 Status
= EFI_OUT_OF_RESOURCES
;
694 // Fill Certificate Database parameters.
696 CACert
= (EFI_SIGNATURE_LIST
*)Data
;
697 CACert
->SignatureListSize
= (UINT32
)SigDataSize
;
698 CACert
->SignatureHeaderSize
= 0;
699 CACert
->SignatureSize
= (UINT32
)(sizeof (EFI_SIGNATURE_DATA
) - 1 + X509DataSize
);
700 CopyGuid (&CACert
->SignatureType
, &gEfiCertX509Guid
);
702 CACertData
= (EFI_SIGNATURE_DATA
*)((UINT8
*)CACert
+ sizeof (EFI_SIGNATURE_LIST
));
703 CopyGuid (&CACertData
->SignatureOwner
, Private
->CertGuid
);
704 CopyMem ((UINT8
*)(CACertData
->SignatureData
), X509Data
, X509DataSize
);
707 // Check if the signature database entry already exists. If it does, use the
708 // EFI_VARIABLE_APPEND_WRITE attribute to append the new signature data to
709 // the original variable, plus preserve the original variable attributes.
711 Status
= gRT
->GetVariable (
713 &gEfiTlsCaCertificateGuid
,
718 if (Status
== EFI_BUFFER_TOO_SMALL
) {
719 Attr
|= EFI_VARIABLE_APPEND_WRITE
;
720 } else if (Status
== EFI_NOT_FOUND
) {
721 Attr
= TLS_AUTH_CONFIG_VAR_BASE_ATTR
;
726 Status
= gRT
->SetVariable (
728 &gEfiTlsCaCertificateGuid
,
733 if (EFI_ERROR (Status
)) {
738 CleanFileContext (Private
);
740 if (Private
->CertGuid
!= NULL
) {
741 FreePool (Private
->CertGuid
);
742 Private
->CertGuid
= NULL
;
749 if (X509Data
!= NULL
) {
757 Enroll Cert into TlsCaCertificate. The GUID will be Private->CertGuid.
759 @param[in] PrivateData The module's private data.
760 @param[in] VariableName Variable name of signature database.
762 @retval EFI_SUCCESS New Cert enrolled successfully.
763 @retval EFI_INVALID_PARAMETER The parameter is invalid.
764 @retval EFI_UNSUPPORTED The Cert file is unsupported type.
765 @retval others Fail to enroll Cert data.
770 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
,
771 IN CHAR16
*VariableName
777 if ((Private
->FileContext
->FileName
== NULL
) || (Private
->FileContext
->FHandle
== NULL
) || (Private
->CertGuid
== NULL
)) {
778 return EFI_INVALID_PARAMETER
;
782 // Parse the file's postfix.
784 NameLength
= StrLen (Private
->FileContext
->FileName
);
785 if (NameLength
<= 4) {
786 return EFI_INVALID_PARAMETER
;
789 FilePostFix
= Private
->FileContext
->FileName
+ NameLength
- 4;
791 if (IsDerPemEncodeCertificate (FilePostFix
)) {
793 // Supports DER-encoded X509 certificate.
795 return EnrollX509toVariable (Private
, VariableName
);
798 return EFI_UNSUPPORTED
;
802 Refresh the global UpdateData structure.
811 // Free current updated date
813 if (mStartOpCodeHandle
!= NULL
) {
814 HiiFreeOpCodeHandle (mStartOpCodeHandle
);
818 // Create new OpCode Handle
820 mStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
823 // Create Hii Extend Label OpCode as the start opcode
825 mStartLabel
= (EFI_IFR_GUID_LABEL
*)HiiCreateGuidOpCode (
829 sizeof (EFI_IFR_GUID_LABEL
)
831 mStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
835 Clean up the dynamic opcode at label and form specified by both LabelId.
837 @param[in] LabelId It is both the Form ID and Label ID for opcode deletion.
838 @param[in] PrivateData Module private data.
844 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*PrivateData
847 RefreshUpdateData ();
850 // Remove all op-codes from dynamic page
852 mStartLabel
->Number
= LabelId
;
854 PrivateData
->RegisteredHandle
,
857 mStartOpCodeHandle
, // Label LabelId
858 mEndOpCodeHandle
// LABEL_END
863 Update the form base on the selected file.
865 @param FilePath Point to the file path.
866 @param FormId The form need to display.
868 @retval TRUE Exit caller function.
869 @retval FALSE Not exit caller function.
874 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
875 IN EFI_FORM_ID FormId
879 EFI_STRING_ID StringToken
;
883 if (FilePath
!= NULL
) {
884 FileName
= ExtractFileNameFromDevicePath (FilePath
);
887 if (FileName
== NULL
) {
889 // FileName = NULL has two case:
890 // 1. FilePath == NULL, not select file.
891 // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource.
892 // In these two case, no need to update the form, and exit the caller function.
897 StringToken
= HiiSetString (mTlsAuthPrivateData
->RegisteredHandle
, 0, FileName
, NULL
);
899 mTlsAuthPrivateData
->FileContext
->FileName
= FileName
;
901 EfiOpenFileByDevicePath (
903 &mTlsAuthPrivateData
->FileContext
->FHandle
,
908 // Create Subtitle op-code for the display string of the option.
910 RefreshUpdateData ();
911 mStartLabel
->Number
= FormId
;
913 HiiCreateSubTitleOpCode (
922 mTlsAuthPrivateData
->RegisteredHandle
,
925 mStartOpCodeHandle
, /// Label FormId
926 mEndOpCodeHandle
/// LABEL_END
933 Update the form base on the input file path info.
935 @param FilePath Point to the file path.
937 @retval TRUE Exit caller function.
938 @retval FALSE Not exit caller function.
943 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
946 return UpdatePage (FilePath
, TLS_AUTH_CONFIG_FORMID4_FORM
);
950 Unload the configuration form, this includes: delete all the configuration
951 entries, uninstall the form callback protocol, and free the resources used.
953 @param[in] Private Pointer to the driver private data.
955 @retval EFI_SUCCESS The configuration form is unloaded.
956 @retval Others Failed to unload the form.
960 TlsAuthConfigFormUnload (
961 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
964 if (Private
->DriverHandle
!= NULL
) {
966 // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
968 gBS
->UninstallMultipleProtocolInterfaces (
969 Private
->DriverHandle
,
970 &gEfiDevicePathProtocolGuid
,
971 &mTlsAuthConfigHiiVendorDevicePath
,
972 &gEfiHiiConfigAccessProtocolGuid
,
973 &Private
->ConfigAccess
,
976 Private
->DriverHandle
= NULL
;
979 if (Private
->RegisteredHandle
!= NULL
) {
981 // Remove HII package list
983 HiiRemovePackages (Private
->RegisteredHandle
);
984 Private
->RegisteredHandle
= NULL
;
987 if (Private
->CertGuid
!= NULL
) {
988 FreePool (Private
->CertGuid
);
991 if (Private
->FileContext
!= NULL
) {
992 FreePool (Private
->FileContext
);
997 if (mStartOpCodeHandle
!= NULL
) {
998 HiiFreeOpCodeHandle (mStartOpCodeHandle
);
1001 if (mEndOpCodeHandle
!= NULL
) {
1002 HiiFreeOpCodeHandle (mEndOpCodeHandle
);
1009 Initialize the configuration form.
1011 @param[in] Private Pointer to the driver private data.
1013 @retval EFI_SUCCESS The configuration form is initialized.
1014 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1018 TlsAuthConfigFormInit (
1019 IN TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
1024 Private
->Signature
= TLS_AUTH_CONFIG_PRIVATE_DATA_SIGNATURE
;
1026 Private
->ConfigAccess
.ExtractConfig
= TlsAuthConfigAccessExtractConfig
;
1027 Private
->ConfigAccess
.RouteConfig
= TlsAuthConfigAccessRouteConfig
;
1028 Private
->ConfigAccess
.Callback
= TlsAuthConfigAccessCallback
;
1031 // Install Device Path Protocol and Config Access protocol to driver handle.
1033 Status
= gBS
->InstallMultipleProtocolInterfaces (
1034 &Private
->DriverHandle
,
1035 &gEfiDevicePathProtocolGuid
,
1036 &mTlsAuthConfigHiiVendorDevicePath
,
1037 &gEfiHiiConfigAccessProtocolGuid
,
1038 &Private
->ConfigAccess
,
1041 if (EFI_ERROR (Status
)) {
1046 // Publish our HII data.
1048 Private
->RegisteredHandle
= HiiAddPackages (
1049 &gTlsAuthConfigGuid
,
1050 Private
->DriverHandle
,
1051 TlsAuthConfigDxeStrings
,
1052 TlsAuthConfigVfrBin
,
1055 if (Private
->RegisteredHandle
== NULL
) {
1056 Status
= EFI_OUT_OF_RESOURCES
;
1060 Private
->FileContext
= AllocateZeroPool (sizeof (TLS_AUTH_CONFIG_FILE_CONTEXT
));
1061 if (Private
->FileContext
== NULL
) {
1062 Status
= EFI_OUT_OF_RESOURCES
;
1067 // Init OpCode Handle and Allocate space for creation of Buffer
1069 mStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1070 if (mStartOpCodeHandle
== NULL
) {
1071 Status
= EFI_OUT_OF_RESOURCES
;
1075 mEndOpCodeHandle
= HiiAllocateOpCodeHandle ();
1076 if (mEndOpCodeHandle
== NULL
) {
1077 Status
= EFI_OUT_OF_RESOURCES
;
1082 // Create Hii Extend Label OpCode as the start opcode
1084 mStartLabel
= (EFI_IFR_GUID_LABEL
*)HiiCreateGuidOpCode (
1088 sizeof (EFI_IFR_GUID_LABEL
)
1090 mStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1093 // Create Hii Extend Label OpCode as the end opcode
1095 mEndLabel
= (EFI_IFR_GUID_LABEL
*)HiiCreateGuidOpCode (
1099 sizeof (EFI_IFR_GUID_LABEL
)
1101 mEndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1102 mEndLabel
->Number
= LABEL_END
;
1107 TlsAuthConfigFormUnload (Private
);
1113 This function allows the caller to request the current
1114 configuration for one or more named elements. The resulting
1115 string is in <ConfigAltResp> format. Any and all alternative
1116 configuration strings shall also be appended to the end of the
1117 current configuration string. If they are, they must appear
1118 after the current configuration. They must contain the same
1119 routing (GUID, NAME, PATH) as the current configuration string.
1120 They must have an additional description indicating the type of
1121 alternative configuration the string represents,
1122 "ALTCFG=<StringToken>". That <StringToken> (when
1123 converted from Hex UNICODE to binary) is a reference to a
1124 string in the associated string pack.
1126 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1128 @param Request A null-terminated Unicode string in
1129 <ConfigRequest> format. Note that this
1130 includes the routing information as well as
1131 the configurable name / value pairs. It is
1132 invalid for this string to be in
1133 <MultiConfigRequest> format.
1134 If a NULL is passed in for the Request field,
1135 all of the settings being abstracted by this function
1136 will be returned in the Results field. In addition,
1137 if a ConfigHdr is passed in with no request elements,
1138 all of the settings being abstracted for that particular
1139 ConfigHdr reference will be returned in the Results Field.
1141 @param Progress On return, points to a character in the
1142 Request string. Points to the string's null
1143 terminator if request was successful. Points
1144 to the most recent "&" before the first
1145 failing name / value pair (or the beginning
1146 of the string if the failure is in the first
1147 name / value pair) if the request was not
1150 @param Results A null-terminated Unicode string in
1151 <MultiConfigAltResp> format which has all values
1152 filled in for the names in the Request string.
1153 String to be allocated by the called function.
1155 @retval EFI_SUCCESS The Results string is filled with the
1156 values corresponding to all requested
1159 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
1160 parts of the results that must be
1161 stored awaiting possible future
1164 @retval EFI_NOT_FOUND Routing data doesn't match any
1165 known driver. Progress set to the
1166 first character in the routing header.
1167 Note: There is no requirement that the
1168 driver validate the routing data. It
1169 must skip the <ConfigHdr> in order to
1172 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
1173 to most recent "&" before the
1174 error or the beginning of the
1177 @retval EFI_INVALID_PARAMETER Unknown name. Progress points
1178 to the & before the name in
1184 TlsAuthConfigAccessExtractConfig (
1185 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
1186 IN CONST EFI_STRING Request
,
1187 OUT EFI_STRING
*Progress
,
1188 OUT EFI_STRING
*Results
1194 EFI_STRING ConfigRequest
;
1195 EFI_STRING ConfigRequestHdr
;
1196 TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
;
1197 BOOLEAN AllocatedRequest
;
1199 if ((Progress
== NULL
) || (Results
== NULL
)) {
1200 return EFI_INVALID_PARAMETER
;
1203 AllocatedRequest
= FALSE
;
1204 ConfigRequestHdr
= NULL
;
1205 ConfigRequest
= NULL
;
1208 Private
= TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This
);
1210 BufferSize
= sizeof (TLS_AUTH_CONFIG_IFR_NVDATA
);
1211 ZeroMem (&Private
->TlsAuthConfigNvData
, BufferSize
);
1213 *Progress
= Request
;
1215 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
)) {
1216 return EFI_NOT_FOUND
;
1219 ConfigRequest
= Request
;
1220 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
1222 // Request is set to NULL or OFFSET is NULL, construct full request string.
1224 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
1225 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
1227 ConfigRequestHdr
= HiiConstructConfigHdr (&gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
, Private
->DriverHandle
);
1228 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
1229 ConfigRequest
= AllocateZeroPool (Size
);
1230 ASSERT (ConfigRequest
!= NULL
);
1231 AllocatedRequest
= TRUE
;
1232 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
1233 FreePool (ConfigRequestHdr
);
1234 ConfigRequestHdr
= NULL
;
1237 Status
= gHiiConfigRouting
->BlockToConfig (
1240 (UINT8
*)&Private
->TlsAuthConfigNvData
,
1247 // Free the allocated config request string.
1249 if (AllocatedRequest
) {
1250 FreePool (ConfigRequest
);
1254 // Set Progress string to the original request string.
1256 if (Request
== NULL
) {
1258 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
1259 *Progress
= Request
+ StrLen (Request
);
1267 This function applies changes in a driver's configuration.
1268 Input is a Configuration, which has the routing data for this
1269 driver followed by name / value configuration pairs. The driver
1270 must apply those pairs to its configurable storage. If the
1271 driver's configuration is stored in a linear block of data
1272 and the driver's name / value pairs are in <BlockConfig>
1273 format, it may use the ConfigToBlock helper function (above) to
1276 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1278 @param Configuration A null-terminated Unicode string in
1279 <ConfigString> format.
1281 @param Progress A pointer to a string filled in with the
1282 offset of the most recent '&' before the
1283 first failing name / value pair (or the
1284 beginning of the string if the failure
1285 is in the first name / value pair) or
1286 the terminating NULL if all was
1289 @retval EFI_SUCCESS The results have been distributed or are
1290 awaiting distribution.
1292 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
1293 parts of the results that must be
1294 stored awaiting possible future
1297 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
1298 Results parameter would result
1299 in this type of error.
1301 @retval EFI_NOT_FOUND Target for the specified routing data
1307 TlsAuthConfigAccessRouteConfig (
1308 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
1309 IN CONST EFI_STRING Configuration
,
1310 OUT EFI_STRING
*Progress
1315 TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
;
1317 if (Progress
== NULL
) {
1318 return EFI_INVALID_PARAMETER
;
1321 *Progress
= Configuration
;
1323 if (Configuration
== NULL
) {
1324 return EFI_INVALID_PARAMETER
;
1328 // Check routing data in <ConfigHdr>.
1329 // Note: there is no name for Name/Value storage, only GUID will be checked
1331 if (!HiiIsConfigHdrMatch (Configuration
, &gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
)) {
1332 return EFI_NOT_FOUND
;
1335 Private
= TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This
);
1337 BufferSize
= sizeof (TLS_AUTH_CONFIG_IFR_NVDATA
);
1338 ZeroMem (&Private
->TlsAuthConfigNvData
, BufferSize
);
1340 Status
= gHiiConfigRouting
->ConfigToBlock (
1343 (UINT8
*)&Private
->TlsAuthConfigNvData
,
1347 if (EFI_ERROR (Status
)) {
1356 This function is called to provide results data to the driver.
1357 This data consists of a unique key that is used to identify
1358 which data is either being passed back or being asked for.
1360 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
1361 @param Action Specifies the type of action taken by the browser.
1362 @param QuestionId A unique value which is sent to the original
1363 exporting driver so that it can identify the type
1364 of data to expect. The format of the data tends to
1365 vary based on the opcode that generated the callback.
1366 @param Type The type of value for the question.
1367 @param Value A pointer to the data being sent to the original
1369 @param ActionRequest On return, points to the action requested by the
1372 @retval EFI_SUCCESS The callback successfully handled the action.
1373 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
1374 variable and its data.
1375 @retval EFI_DEVICE_ERROR The variable could not be saved.
1376 @retval EFI_UNSUPPORTED The specified Action is not supported by the
1381 TlsAuthConfigAccessCallback (
1382 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
1383 IN EFI_BROWSER_ACTION Action
,
1384 IN EFI_QUESTION_ID QuestionId
,
1386 IN OUT EFI_IFR_TYPE_VALUE
*Value
,
1387 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
1391 RETURN_STATUS RStatus
;
1392 TLS_AUTH_CONFIG_PRIVATE_DATA
*Private
;
1394 TLS_AUTH_CONFIG_IFR_NVDATA
*IfrNvData
;
1396 EFI_DEVICE_PATH_PROTOCOL
*File
;
1397 EFI_HII_POPUP_PROTOCOL
*HiiPopUp
;
1398 EFI_HII_POPUP_SELECTION PopUpSelect
;
1400 Status
= EFI_SUCCESS
;
1403 if ((This
== NULL
) || (Value
== NULL
) || (ActionRequest
== NULL
)) {
1404 return EFI_INVALID_PARAMETER
;
1407 Private
= TLS_AUTH_CONFIG_PRIVATE_FROM_THIS (This
);
1409 mTlsAuthPrivateData
= Private
;
1410 Status
= gBS
->LocateProtocol (&gEfiHiiPopupProtocolGuid
, NULL
, (VOID
**)&HiiPopUp
);
1411 if (EFI_ERROR (Status
)) {
1412 DEBUG ((DEBUG_ERROR
, "Can't find Form PopUp protocol. Exit (%r)\n", Status
));
1417 // Retrieve uncommitted data from Browser
1419 BufferSize
= sizeof (TLS_AUTH_CONFIG_IFR_NVDATA
);
1420 IfrNvData
= AllocateZeroPool (BufferSize
);
1421 if (IfrNvData
== NULL
) {
1422 return EFI_OUT_OF_RESOURCES
;
1425 HiiGetBrowserData (&gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
, BufferSize
, (UINT8
*)IfrNvData
);
1427 if ((Action
!= EFI_BROWSER_ACTION_CHANGED
) &&
1428 (Action
!= EFI_BROWSER_ACTION_CHANGING
) &&
1429 (Action
!= EFI_BROWSER_ACTION_FORM_CLOSE
))
1431 Status
= EFI_UNSUPPORTED
;
1435 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
1436 switch (QuestionId
) {
1437 case KEY_TLS_AUTH_CONFIG_CLIENT_CERT
:
1438 case KEY_TLS_AUTH_CONFIG_SERVER_CA
:
1442 ZeroMem (IfrNvData
->CertGuid
, sizeof (IfrNvData
->CertGuid
));
1443 if (Private
->CertGuid
== NULL
) {
1444 Private
->CertGuid
= (EFI_GUID
*)AllocateZeroPool (sizeof (EFI_GUID
));
1445 if (Private
->CertGuid
== NULL
) {
1446 return EFI_OUT_OF_RESOURCES
;
1450 if (QuestionId
== KEY_TLS_AUTH_CONFIG_CLIENT_CERT
) {
1451 LabelId
= TLS_AUTH_CONFIG_FORMID3_FORM
;
1453 LabelId
= TLS_AUTH_CONFIG_FORMID4_FORM
;
1457 // Refresh selected file.
1459 CleanUpPage (LabelId
, Private
);
1461 case KEY_TLS_AUTH_CONFIG_ENROLL_CERT_FROM_FILE
:
1463 // If the file is already opened, clean the file related resource first.
1465 CleanFileContext (Private
);
1467 ChooseFile (NULL
, NULL
, UpdateCAFromFile
, &File
);
1470 case KEY_TLS_AUTH_CONFIG_VALUE_SAVE_AND_EXIT
:
1471 Status
= EnrollCertDatabase (Private
, EFI_TLS_CA_CERTIFICATE_VARIABLE
);
1472 if (EFI_ERROR (Status
)) {
1473 CleanFileContext (Private
);
1475 HiiPopUp
->CreatePopup (
1477 EfiHiiPopupStyleError
,
1479 Private
->RegisteredHandle
,
1480 STRING_TOKEN (STR_TLS_AUTH_ENROLL_CERT_FAILURE
),
1487 case KEY_TLS_AUTH_CONFIG_VALUE_NO_SAVE_AND_EXIT
:
1488 CleanFileContext (Private
);
1490 if (Private
->CertGuid
!= NULL
) {
1491 FreePool (Private
->CertGuid
);
1492 Private
->CertGuid
= NULL
;
1497 case KEY_TLS_AUTH_CONFIG_DELETE_CERT
:
1500 EFI_TLS_CA_CERTIFICATE_VARIABLE
,
1501 &gEfiTlsCaCertificateGuid
,
1503 TLS_AUTH_CONFIG_FORMID5_FORM
,
1504 OPTION_DEL_CA_ESTION_ID
1509 if ((QuestionId
>= OPTION_DEL_CA_ESTION_ID
) &&
1510 (QuestionId
< (OPTION_DEL_CA_ESTION_ID
+ OPTION_CONFIG_RANGE
)))
1514 EFI_TLS_CA_CERTIFICATE_VARIABLE
,
1515 &gEfiTlsCaCertificateGuid
,
1517 TLS_AUTH_CONFIG_FORMID5_FORM
,
1518 OPTION_DEL_CA_ESTION_ID
,
1519 QuestionId
- OPTION_DEL_CA_ESTION_ID
1525 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
1526 switch (QuestionId
) {
1527 case KEY_TLS_AUTH_CONFIG_CERT_GUID
:
1528 ASSERT (Private
->CertGuid
!= NULL
);
1529 RStatus
= StrToGuid (
1530 IfrNvData
->CertGuid
,
1533 if (RETURN_ERROR (RStatus
) || (IfrNvData
->CertGuid
[GUID_STRING_LENGTH
] != L
'\0')) {
1534 Status
= EFI_INVALID_PARAMETER
;
1538 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
1543 } else if (Action
== EFI_BROWSER_ACTION_FORM_CLOSE
) {
1544 CleanFileContext (Private
);
1549 if (!EFI_ERROR (Status
)) {
1550 BufferSize
= sizeof (TLS_AUTH_CONFIG_IFR_NVDATA
);
1551 HiiSetBrowserData (&gTlsAuthConfigGuid
, mTlsAuthConfigStorageName
, BufferSize
, (UINT8
*)IfrNvData
, NULL
);
1554 FreePool (IfrNvData
);