]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c
OptionRomPkg: Removing ipf which is no longer supported from edk2.
[mirror_edk2.git] / NetworkPkg / TlsAuthConfigDxe / TlsAuthConfigImpl.c
index 81f7e7d0f4f136c88f7d854b8ba79742c6bd549e..0780b03bbab43f78fba3624d7da90915ec102112 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   The Miscellaneous Routines for TlsAuthConfigDxe driver.\r
 \r
-Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
@@ -296,7 +296,7 @@ ON_EXIT:
 /**\r
   Delete one entry from cert database.\r
 \r
-  @param[in]    PrivateData         Module's private data.\r
+  @param[in]    Private             Module's private data.\r
   @param[in]    VariableName        The variable name of the database.\r
   @param[in]    VendorGuid          A unique identifier for the vendor.\r
   @param[in]    LabelNumber         Label number to insert opcodes.\r
@@ -477,18 +477,23 @@ ON_EXIT:
 \r
 \r
 /**\r
-  Close an open file handle.\r
+  Clean the file related resource.\r
 \r
-  @param[in] FileHandle           The file handle to close.\r
+  @param[in]    Private             Module's private data.\r
 \r
 **/\r
 VOID\r
-CloseFile (\r
-  IN EFI_FILE_HANDLE   FileHandle\r
+CleanFileContext (\r
+  IN TLS_AUTH_CONFIG_PRIVATE_DATA     *Private\r
   )\r
 {\r
-  if (FileHandle != NULL) {\r
-    FileHandle->Close (FileHandle);\r
+  if (Private->FileContext->FHandle != NULL) {\r
+    Private->FileContext->FHandle->Close (Private->FileContext->FHandle);\r
+    Private->FileContext->FHandle = NULL;\r
+    if (Private->FileContext->FileName!= NULL){\r
+      FreePool(Private->FileContext->FileName);\r
+      Private->FileContext->FileName = NULL;\r
+    }\r
   }\r
 }\r
 \r
@@ -569,145 +574,6 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-/**\r
-  This function will open a file or directory referenced by DevicePath.\r
-\r
-  This function opens a file with the open mode according to the file path. The\r
-  Attributes is valid only for EFI_FILE_MODE_CREATE.\r
-\r
-  @param[in, out]  FilePath        On input, the device path to the file.\r
-                                   On output, the remaining device path.\r
-  @param[out]      FileHandle      Pointer to the file handle.\r
-  @param[in]       OpenMode        The mode to open the file with.\r
-  @param[in]       Attributes      The file's file attributes.\r
-\r
-  @retval EFI_SUCCESS              The information was set.\r
-  @retval EFI_INVALID_PARAMETER    One of the parameters has an invalid value.\r
-  @retval EFI_UNSUPPORTED          Could not open the file path.\r
-  @retval EFI_NOT_FOUND            The specified file could not be found on the\r
-                                   device or the file system could not be found on\r
-                                   the device.\r
-  @retval EFI_NO_MEDIA             The device has no medium.\r
-  @retval EFI_MEDIA_CHANGED        The device has a different medium in it or the\r
-                                   medium is no longer supported.\r
-  @retval EFI_DEVICE_ERROR         The device reported an error.\r
-  @retval EFI_VOLUME_CORRUPTED     The file system structures are corrupted.\r
-  @retval EFI_WRITE_PROTECTED      The file or medium is write protected.\r
-  @retval EFI_ACCESS_DENIED        The file was opened read only.\r
-  @retval EFI_OUT_OF_RESOURCES     Not enough resources were available to open the\r
-                                   file.\r
-  @retval EFI_VOLUME_FULL          The volume is full.\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-OpenFileByDevicePath (\r
-  IN OUT EFI_DEVICE_PATH_PROTOCOL     **FilePath,\r
-  OUT EFI_FILE_HANDLE                 *FileHandle,\r
-  IN UINT64                           OpenMode,\r
-  IN UINT64                           Attributes\r
-  )\r
-{\r
-  EFI_STATUS                      Status;\r
-  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *EfiSimpleFileSystemProtocol;\r
-  EFI_FILE_PROTOCOL               *Handle1;\r
-  EFI_FILE_PROTOCOL               *Handle2;\r
-  EFI_HANDLE                      DeviceHandle;\r
-\r
-  if ((FilePath == NULL || FileHandle == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  Status = gBS->LocateDevicePath (\r
-                  &gEfiSimpleFileSystemProtocolGuid,\r
-                  FilePath,\r
-                  &DeviceHandle\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  Status = gBS->OpenProtocol(\r
-                  DeviceHandle,\r
-                  &gEfiSimpleFileSystemProtocolGuid,\r
-                  (VOID**)&EfiSimpleFileSystemProtocol,\r
-                  gImageHandle,\r
-                  NULL,\r
-                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
-                  );\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  Status = EfiSimpleFileSystemProtocol->OpenVolume(EfiSimpleFileSystemProtocol, &Handle1);\r
-  if (EFI_ERROR (Status)) {\r
-    FileHandle = NULL;\r
-    return Status;\r
-  }\r
-\r
-  //\r
-  // go down directories one node at a time.\r
-  //\r
-  while (!IsDevicePathEnd (*FilePath)) {\r
-    //\r
-    // For file system access each node should be a file path component\r
-    //\r
-    if (DevicePathType    (*FilePath) != MEDIA_DEVICE_PATH ||\r
-        DevicePathSubType (*FilePath) != MEDIA_FILEPATH_DP\r
-       ) {\r
-      FileHandle = NULL;\r
-      return (EFI_INVALID_PARAMETER);\r
-    }\r
-    //\r
-    // Open this file path node\r
-    //\r
-    Handle2  = Handle1;\r
-    Handle1 = NULL;\r
-\r
-    //\r
-    // Try to test opening an existing file\r
-    //\r
-    Status = Handle2->Open (\r
-                        Handle2,\r
-                        &Handle1,\r
-                        ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
-                        OpenMode &~EFI_FILE_MODE_CREATE,\r
-                        0\r
-                        );\r
-\r
-    //\r
-    // see if the error was that it needs to be created\r
-    //\r
-    if ((EFI_ERROR (Status)) && (OpenMode != (OpenMode &~EFI_FILE_MODE_CREATE))) {\r
-      Status = Handle2->Open (\r
-                          Handle2,\r
-                          &Handle1,\r
-                          ((FILEPATH_DEVICE_PATH*)*FilePath)->PathName,\r
-                          OpenMode,\r
-                          Attributes\r
-                          );\r
-    }\r
-    //\r
-    // Close the last node\r
-    //\r
-    Handle2->Close (Handle2);\r
-\r
-    if (EFI_ERROR(Status)) {\r
-      return (Status);\r
-    }\r
-\r
-    //\r
-    // Get the next node\r
-    //\r
-    *FilePath = NextDevicePathNode (*FilePath);\r
-  }\r
-\r
-  //\r
-  // This is a weak spot since if the undefined SHELL_FILE_HANDLE format changes this must change also!\r
-  //\r
-  *FileHandle = (VOID*)Handle1;\r
-  return EFI_SUCCESS;\r
-}\r
-\r
 /**\r
   This function converts an input device structure to a Unicode string.\r
 \r
@@ -797,6 +663,7 @@ EnrollX509toVariable (
   EFI_SIGNATURE_LIST                *CACert;\r
   EFI_SIGNATURE_DATA                *CACertData;\r
   VOID                              *Data;\r
+  VOID                              *CurrentData;\r
   UINTN                             DataSize;\r
   UINTN                             SigDataSize;\r
   UINT32                            Attr;\r
@@ -808,6 +675,8 @@ EnrollX509toVariable (
   CACert        = NULL;\r
   CACertData    = NULL;\r
   Data          = NULL;\r
+  CurrentData   = NULL;\r
+  Attr          = 0;\r
 \r
   Status = ReadFileContent (\r
              Private->FileContext->FHandle,\r
@@ -842,12 +711,10 @@ EnrollX509toVariable (
   CopyMem ((UINT8* ) (CACertData->SignatureData), X509Data, X509DataSize);\r
 \r
   //\r
-  // Check if signature database entry has been already existed.\r
-  // If true, use EFI_VARIABLE_APPEND_WRITE attribute to append the\r
-  // new signature data to original variable\r
+  // Check if the signature database entry already exists. If it does, use the\r
+  // EFI_VARIABLE_APPEND_WRITE attribute to append the new signature data to\r
+  // the original variable, plus preserve the original variable attributes.\r
   //\r
-  Attr = TLS_AUTH_CONFIG_VAR_BASE_ATTR;\r
-\r
   Status = gRT->GetVariable(\r
                   VariableName,\r
                   &gEfiTlsCaCertificateGuid,\r
@@ -856,8 +723,29 @@ EnrollX509toVariable (
                   NULL\r
                   );\r
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    //\r
+    // Per spec, we have to fetch the variable's contents, even though we're\r
+    // only interested in the variable's attributes.\r
+    //\r
+    CurrentData = AllocatePool (DataSize);\r
+    if (CurrentData == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto ON_EXIT;\r
+    }\r
+    Status = gRT->GetVariable(\r
+                    VariableName,\r
+                    &gEfiTlsCaCertificateGuid,\r
+                    &Attr,\r
+                    &DataSize,\r
+                    CurrentData\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      goto ON_EXIT;\r
+    }\r
     Attr |= EFI_VARIABLE_APPEND_WRITE;\r
-  } else if (Status != EFI_NOT_FOUND) {\r
+  } else if (Status == EFI_NOT_FOUND) {\r
+    Attr = TLS_AUTH_CONFIG_VAR_BASE_ATTR;\r
+  } else {\r
     goto ON_EXIT;\r
   }\r
 \r
@@ -873,14 +761,7 @@ EnrollX509toVariable (
   }\r
 \r
 ON_EXIT:\r
-\r
-  CloseFile (Private->FileContext->FHandle);\r
-  if (Private->FileContext->FileName != NULL) {\r
-    FreePool(Private->FileContext->FileName);\r
-    Private->FileContext->FileName = NULL;\r
-  }\r
-\r
-  Private->FileContext->FHandle = NULL;\r
+  CleanFileContext (Private);\r
 \r
   if (Private->CertGuid != NULL) {\r
     FreePool (Private->CertGuid);\r
@@ -891,6 +772,10 @@ ON_EXIT:
     FreePool (Data);\r
   }\r
 \r
+  if (CurrentData != NULL) {\r
+    FreePool (CurrentData);\r
+  }\r
+\r
   if (X509Data != NULL) {\r
     FreePool (X509Data);\r
   }\r
@@ -1040,7 +925,7 @@ UpdatePage(
 \r
   mTlsAuthPrivateData->FileContext->FileName = FileName;\r
 \r
-  OpenFileByDevicePath (\r
+  EfiOpenFileByDevicePath (\r
     &FilePath,\r
     &mTlsAuthPrivateData->FileContext->FHandle,\r
     EFI_FILE_MODE_READ,\r
@@ -1561,7 +1446,8 @@ TlsAuthConfigAccessCallback (
   HiiGetBrowserData (&gTlsAuthConfigGuid, mTlsAuthConfigStorageName, BufferSize, (UINT8 *) IfrNvData);\r
 \r
   if ((Action != EFI_BROWSER_ACTION_CHANGED) &&\r
-      (Action != EFI_BROWSER_ACTION_CHANGING)) {\r
+      (Action != EFI_BROWSER_ACTION_CHANGING) &&\r
+      (Action != EFI_BROWSER_ACTION_FORM_CLOSE)) {\r
     Status = EFI_UNSUPPORTED;\r
     goto EXIT;\r
   }\r
@@ -1592,12 +1478,19 @@ TlsAuthConfigAccessCallback (
       CleanUpPage (LabelId, Private);\r
       break;\r
     case KEY_TLS_AUTH_CONFIG_ENROLL_CERT_FROM_FILE:\r
+      //\r
+      // If the file is already opened, clean the file related resource first.\r
+      //\r
+      CleanFileContext (Private);\r
+\r
       ChooseFile( NULL, NULL, UpdateCAFromFile, &File);\r
       break;\r
 \r
     case KEY_TLS_AUTH_CONFIG_VALUE_SAVE_AND_EXIT:\r
       Status = EnrollCertDatabase (Private, EFI_TLS_CA_CERTIFICATE_VARIABLE);\r
       if (EFI_ERROR (Status)) {\r
+        CleanFileContext (Private);\r
+\r
         CreatePopUp (\r
           EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,\r
           &Key,\r
@@ -1608,14 +1501,7 @@ TlsAuthConfigAccessCallback (
       break;\r
 \r
     case KEY_TLS_AUTH_CONFIG_VALUE_NO_SAVE_AND_EXIT:\r
-      if (Private->FileContext->FHandle != NULL) {\r
-        CloseFile (Private->FileContext->FHandle);\r
-        Private->FileContext->FHandle = NULL;\r
-        if (Private->FileContext->FileName!= NULL){\r
-          FreePool(Private->FileContext->FileName);\r
-          Private->FileContext->FileName = NULL;\r
-        }\r
-      }\r
+      CleanFileContext (Private);\r
 \r
       if (Private->CertGuid!= NULL) {\r
         FreePool (Private->CertGuid);\r
@@ -1667,6 +1553,8 @@ TlsAuthConfigAccessCallback (
     default:\r
       break;\r
     }\r
+  } else if (Action == EFI_BROWSER_ACTION_FORM_CLOSE) {\r
+    CleanFileContext (Private);\r
   }\r
 \r
 EXIT:\r