]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/TlsDxe/TlsDriver.c
NetworkPkg: Convert files to CRLF line ending
[mirror_edk2.git] / NetworkPkg / TlsDxe / TlsDriver.c
index 38bf5993ce9a779544d9d482ef3eb9f72fab4779..29bc966c3eae55e6f1fc436f89b95fa61ae2681a 100644 (file)
-/** @file
-  The Driver Binding and Service Binding Protocol for TlsDxe driver.
-
-  Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
-
-  This program and the accompanying materials
-  are licensed and made available under the terms and conditions of the BSD License
-  which accompanies this distribution.  The full text of the license may be found at
-  http://opensource.org/licenses/bsd-license.php.
-
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-
-**/
-
-#include "TlsImpl.h"
-
-EFI_SERVICE_BINDING_PROTOCOL mTlsServiceBinding = {
-  TlsServiceBindingCreateChild,
-  TlsServiceBindingDestroyChild
-};
-
-/**
-  Release all the resources used by the TLS instance.
-
-  @param[in]  Instance        The TLS instance data.
-
-**/
-VOID
-TlsCleanInstance (
-  IN TLS_INSTANCE           *Instance
-  )
-{
-  if (Instance != NULL) {
-    if (Instance->TlsConn != NULL) {
-      TlsFree (Instance->TlsConn);
-    }
-
-    FreePool (Instance);
-  }
-}
-
-/**
-  Create the TLS instance and initialize it.
-
-  @param[in]  Service              The pointer to the TLS service.
-  @param[out] Instance             The pointer to the TLS instance.
-
-  @retval EFI_OUT_OF_RESOURCES   Failed to allocate resources.
-  @retval EFI_SUCCESS            The TLS instance is created.
-
-**/
-EFI_STATUS
-TlsCreateInstance (
-  IN  TLS_SERVICE         *Service,
-  OUT TLS_INSTANCE        **Instance
-  )
-{
-  TLS_INSTANCE            *TlsInstance;
-
-  *Instance = NULL;
-
-  TlsInstance = AllocateZeroPool (sizeof (TLS_INSTANCE));
-  if (TlsInstance == NULL) {
-    return EFI_OUT_OF_RESOURCES;
-  }
-
-  TlsInstance->Signature = TLS_INSTANCE_SIGNATURE;
-  InitializeListHead (&TlsInstance->Link);
-  TlsInstance->InDestroy = FALSE;
-  TlsInstance->Service   = Service;
-
-  CopyMem (&TlsInstance->Tls, &mTlsProtocol, sizeof (TlsInstance->Tls));
-  CopyMem (&TlsInstance->TlsConfig, &mTlsConfigurationProtocol, sizeof (TlsInstance->TlsConfig));
-
-  TlsInstance->TlsSessionState = EfiTlsSessionNotStarted;
-
-  *Instance = TlsInstance;
-
-  return EFI_SUCCESS;
-}
-
-/**
-  Release all the resources used by the TLS service binding instance.
-
-  @param[in]  Service        The TLS service data.
-
-**/
-VOID
-TlsCleanService (
-  IN TLS_SERVICE     *Service
-  )
-{
-  if (Service != NULL) {
-    if (Service->TlsCtx != NULL) {
-      TlsCtxFree (Service->TlsCtx);
-    }
-
-    FreePool (Service);
-  }
-}
-
-/**
-  Create then initialize a TLS service.
-
-  @param[in]  Image                  ImageHandle of the TLS driver
-  @param[out] Service                The service for TLS driver
-
-  @retval EFI_OUT_OF_RESOURCES   Failed to allocate resource to create the service.
-  @retval EFI_SUCCESS            The service is created for the driver.
-
-**/
-EFI_STATUS
-TlsCreateService (
-  IN  EFI_HANDLE            Image,
-  OUT TLS_SERVICE           **Service
-  )
-{
-  TLS_SERVICE            *TlsService;
-
-  ASSERT (Service != NULL);
-
-  *Service = NULL;
-
-  //
-  // Allocate a TLS Service Data
-  //
-  TlsService = AllocateZeroPool (sizeof (TLS_SERVICE));
-  if (TlsService == NULL) {
-    return EFI_OUT_OF_RESOURCES;
-  }
-
-  //
-  // Initialize TLS Service Data
-  //
-  TlsService->Signature        = TLS_SERVICE_SIGNATURE;
-  CopyMem (&TlsService->ServiceBinding, &mTlsServiceBinding, sizeof (TlsService->ServiceBinding));
-  TlsService->TlsChildrenNum   = 0;
-  InitializeListHead (&TlsService->TlsChildrenList);
-  TlsService->ImageHandle      = Image;
-
-  *Service = TlsService;
-
-  return EFI_SUCCESS;
-}
-
-/**
-  Unloads an image.
-
-  @param[in]  ImageHandle           Handle that identifies the image to be unloaded.
-
-  @retval EFI_SUCCESS           The image has been unloaded.
-  @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
-
-**/
-EFI_STATUS
-EFIAPI
-TlsUnload (
-  IN EFI_HANDLE  ImageHandle
-  )
-{
-  EFI_STATUS                      Status;
-  UINTN                           HandleNum;
-  EFI_HANDLE                      *HandleBuffer;
-  UINT32                          Index;
-  EFI_SERVICE_BINDING_PROTOCOL    *ServiceBinding;
-  TLS_SERVICE                     *TlsService;
-
-  HandleBuffer   = NULL;
-  ServiceBinding = NULL;
-  TlsService     = NULL;
-
-  //
-  // Locate all the handles with Tls service binding protocol.
-  //
-  Status = gBS->LocateHandleBuffer (
-                  ByProtocol,
-                  &gEfiTlsServiceBindingProtocolGuid,
-                  NULL,
-                  &HandleNum,
-                  &HandleBuffer
-                  );
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  for (Index = 0; Index < HandleNum; Index++) {
-    //
-    // Firstly, find ServiceBinding interface
-    //
-    Status = gBS->OpenProtocol (
-                    HandleBuffer[Index],
-                    &gEfiTlsServiceBindingProtocolGuid,
-                    (VOID **) &ServiceBinding,
-                    ImageHandle,
-                    NULL,
-                    EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
-                    );
-    if (EFI_ERROR (Status)) {
-      return Status;
-    }
-
-    TlsService = TLS_SERVICE_FROM_THIS (ServiceBinding);
-
-    //
-    // Then, uninstall ServiceBinding interface
-    //
-    Status = gBS->UninstallMultipleProtocolInterfaces (
-                    HandleBuffer[Index],
-                    &gEfiTlsServiceBindingProtocolGuid, ServiceBinding,
-                    NULL
-                    );
-    if (EFI_ERROR (Status)) {
-      return Status;
-    }
-
-    TlsCleanService (TlsService);
-  }
-
-  if (HandleBuffer != NULL) {
-    FreePool (HandleBuffer);
-  }
-
-  return EFI_SUCCESS;
-}
-
-/**
-  This is the declaration of an EFI image entry point. This entry point is
-  the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
-  both device drivers and bus drivers.
-
-  @param  ImageHandle           The firmware allocated handle for the UEFI image.
-  @param  SystemTable           A pointer to the EFI System Table.
-
-  @retval EFI_SUCCESS           The operation completed successfully.
-  @retval Others                An unexpected error occurred.
-**/
-EFI_STATUS
-EFIAPI
-TlsDriverEntryPoint (
-  IN EFI_HANDLE        ImageHandle,
-  IN EFI_SYSTEM_TABLE  *SystemTable
-  )
-{
-  EFI_STATUS             Status;
-
-  TLS_SERVICE            *TlsService;
-
-  //
-  // Create TLS Service
-  //
-  Status = TlsCreateService (ImageHandle, &TlsService);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  ASSERT (TlsService != NULL);
-
-  //
-  // Initializes the OpenSSL library.
-  //
-  TlsInitialize ();
-
-  //
-  // Create a new SSL_CTX object as framework to establish TLS/SSL enabled
-  // connections. TLS 1.0 is used as the default version.
-  //
-  TlsService->TlsCtx = TlsCtxNew (TLS10_PROTOCOL_VERSION_MAJOR, TLS10_PROTOCOL_VERSION_MINOR);
-  if (TlsService->TlsCtx == NULL) {
-    FreePool (TlsService);
-    return EFI_ABORTED;
-  }
-
-  //
-  // Install the TlsServiceBinding Protocol onto Handle
-  //
-  Status = gBS->InstallMultipleProtocolInterfaces (
-                  &TlsService->Handle,
-                  &gEfiTlsServiceBindingProtocolGuid,
-                  &TlsService->ServiceBinding,
-                  NULL
-                  );
-  if (EFI_ERROR (Status)) {
-    goto ON_CLEAN_SERVICE;
-  }
-
-  return Status;
-
-ON_CLEAN_SERVICE:
-  TlsCleanService (TlsService);
-
-  return Status;
-}
-
-/**
-  Creates a child handle and installs a protocol.
-
-  The CreateChild() function installs a protocol on ChildHandle.
-  If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
-  If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
-
-  @param[in] This        Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
-  @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
-                         then a new handle is created. If it is a pointer to an existing UEFI handle,
-                         then the protocol is added to the existing UEFI handle.
-
-  @retval EFI_SUCCES            The protocol was added to ChildHandle.
-  @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
-  @retval EFI_OUT_OF_RESOURCES  There are not enough resources available to create
-                                the child.
-  @retval other                 The child handle was not created.
-
-**/
-EFI_STATUS
-EFIAPI
-TlsServiceBindingCreateChild (
-  IN EFI_SERVICE_BINDING_PROTOCOL  *This,
-  IN EFI_HANDLE                    *ChildHandle
-  )
-{
-  TLS_SERVICE         *TlsService;
-  TLS_INSTANCE        *TlsInstance;
-  EFI_STATUS           Status;
-  EFI_TPL              OldTpl;
-
-  if ((This == NULL) || (ChildHandle == NULL)) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  TlsService = TLS_SERVICE_FROM_THIS (This);
-
-  Status = TlsCreateInstance (TlsService, &TlsInstance);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  ASSERT (TlsInstance != NULL);
-
-  //
-  // Create a new TLS connection object.
-  //
-  TlsInstance->TlsConn = TlsNew (TlsService->TlsCtx);
-  if (TlsInstance->TlsConn == NULL) {
-    Status = EFI_ABORTED;
-    goto ON_ERROR;
-  }
-
-  //
-  // Set default ConnectionEnd to EfiTlsClient
-  //
-  Status = TlsSetConnectionEnd (TlsInstance->TlsConn, EfiTlsClient);
-  if (EFI_ERROR (Status)) {
-    goto ON_ERROR;
-  }
-
-  //
-  // Install TLS protocol and configuration protocol onto ChildHandle
-  //
-  Status = gBS->InstallMultipleProtocolInterfaces (
-                  ChildHandle,
-                  &gEfiTlsProtocolGuid,
-                  &TlsInstance->Tls,
-                  &gEfiTlsConfigurationProtocolGuid,
-                  &TlsInstance->TlsConfig,
-                  NULL
-                  );
-  if (EFI_ERROR (Status)) {
-    goto ON_ERROR;
-  }
-
-  TlsInstance->ChildHandle = *ChildHandle;
-
-  //
-  // Add it to the TLS service's child list.
-  //
-  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
-
-  InsertTailList (&TlsService->TlsChildrenList, &TlsInstance->Link);
-  TlsService->TlsChildrenNum++;
-
-  gBS->RestoreTPL (OldTpl);
-
-  return EFI_SUCCESS;
-
-ON_ERROR:
-  TlsCleanInstance (TlsInstance);
-  return Status;
-}
-
-/**
-  Destroys a child handle with a protocol installed on it.
-
-  The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
-  that was installed by CreateChild() from ChildHandle. If the removed protocol is the
-  last protocol on ChildHandle, then ChildHandle is destroyed.
-
-  @param  This        Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
-  @param  ChildHandle Handle of the child to destroy.
-
-  @retval EFI_SUCCES            The protocol was removed from ChildHandle.
-  @retval EFI_UNSUPPORTED       ChildHandle does not support the protocol that is being removed.
-  @retval EFI_INVALID_PARAMETER Child handle is NULL.
-  @retval EFI_ACCESS_DENIED     The protocol could not be removed from the ChildHandle
-                                because its services are being used.
-  @retval other                 The child handle was not destroyed.
-
-**/
-EFI_STATUS
-EFIAPI
-TlsServiceBindingDestroyChild (
-  IN EFI_SERVICE_BINDING_PROTOCOL  *This,
-  IN EFI_HANDLE                    ChildHandle
-  )
-{
-  TLS_SERVICE                    *TlsService;
-  TLS_INSTANCE                   *TlsInstance;
-
-  EFI_TLS_PROTOCOL               *Tls;
-  EFI_TLS_CONFIGURATION_PROTOCOL *TlsConfig;
-  EFI_STATUS                     Status;
-  EFI_TPL                        OldTpl;
-
-  if ((This == NULL) || (ChildHandle == NULL)) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  TlsService = TLS_SERVICE_FROM_THIS (This);
-
-  //
-  // Find TLS protocol interface installed in ChildHandle
-  //
-  Status = gBS->OpenProtocol (
-                  ChildHandle,
-                  &gEfiTlsProtocolGuid,
-                  (VOID **) &Tls,
-                  TlsService->ImageHandle,
-                  NULL,
-                  EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
-                  );
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  //
-  // Find TLS configuration protocol interface installed in ChildHandle
-  //
-  Status = gBS->OpenProtocol (
-                  ChildHandle,
-                  &gEfiTlsConfigurationProtocolGuid,
-                  (VOID **) &TlsConfig,
-                  TlsService->ImageHandle,
-                  NULL,
-                  EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
-                  );
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  TlsInstance  = TLS_INSTANCE_FROM_PROTOCOL (Tls);
-
-  if (TlsInstance->Service != TlsService) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  if (TlsInstance->InDestroy) {
-    return EFI_SUCCESS;
-  }
-
-  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
-
-  TlsInstance->InDestroy = TRUE;
-
-  //
-  // Uninstall the TLS protocol and TLS Configuration Protocol interface installed in ChildHandle.
-  //
-  Status = gBS->UninstallMultipleProtocolInterfaces (
-                  ChildHandle,
-                  &gEfiTlsProtocolGuid,
-                  Tls,
-                  &gEfiTlsConfigurationProtocolGuid,
-                  TlsConfig,
-                  NULL
-                  );
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  RemoveEntryList (&TlsInstance->Link);
-  TlsService->TlsChildrenNum--;
-
-  gBS->RestoreTPL (OldTpl);
-
-  TlsCleanInstance (TlsInstance);
-
-  return EFI_SUCCESS;
-}
+/** @file\r
+  The Driver Binding and Service Binding Protocol for TlsDxe driver.\r
+\r
+  Copyright (c) 2016, 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
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php.\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "TlsImpl.h"\r
+\r
+EFI_SERVICE_BINDING_PROTOCOL mTlsServiceBinding = {\r
+  TlsServiceBindingCreateChild,\r
+  TlsServiceBindingDestroyChild\r
+};\r
+\r
+/**\r
+  Release all the resources used by the TLS instance.\r
+\r
+  @param[in]  Instance        The TLS instance data.\r
+\r
+**/\r
+VOID\r
+TlsCleanInstance (\r
+  IN TLS_INSTANCE           *Instance\r
+  )\r
+{\r
+  if (Instance != NULL) {\r
+    if (Instance->TlsConn != NULL) {\r
+      TlsFree (Instance->TlsConn);\r
+    }\r
+\r
+    FreePool (Instance);\r
+  }\r
+}\r
+\r
+/**\r
+  Create the TLS instance and initialize it.\r
+\r
+  @param[in]  Service              The pointer to the TLS service.\r
+  @param[out] Instance             The pointer to the TLS instance.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   Failed to allocate resources.\r
+  @retval EFI_SUCCESS            The TLS instance is created.\r
+\r
+**/\r
+EFI_STATUS\r
+TlsCreateInstance (\r
+  IN  TLS_SERVICE         *Service,\r
+  OUT TLS_INSTANCE        **Instance\r
+  )\r
+{\r
+  TLS_INSTANCE            *TlsInstance;\r
+\r
+  *Instance = NULL;\r
+\r
+  TlsInstance = AllocateZeroPool (sizeof (TLS_INSTANCE));\r
+  if (TlsInstance == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  TlsInstance->Signature = TLS_INSTANCE_SIGNATURE;\r
+  InitializeListHead (&TlsInstance->Link);\r
+  TlsInstance->InDestroy = FALSE;\r
+  TlsInstance->Service   = Service;\r
+\r
+  CopyMem (&TlsInstance->Tls, &mTlsProtocol, sizeof (TlsInstance->Tls));\r
+  CopyMem (&TlsInstance->TlsConfig, &mTlsConfigurationProtocol, sizeof (TlsInstance->TlsConfig));\r
+\r
+  TlsInstance->TlsSessionState = EfiTlsSessionNotStarted;\r
+\r
+  *Instance = TlsInstance;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Release all the resources used by the TLS service binding instance.\r
+\r
+  @param[in]  Service        The TLS service data.\r
+\r
+**/\r
+VOID\r
+TlsCleanService (\r
+  IN TLS_SERVICE     *Service\r
+  )\r
+{\r
+  if (Service != NULL) {\r
+    if (Service->TlsCtx != NULL) {\r
+      TlsCtxFree (Service->TlsCtx);\r
+    }\r
+\r
+    FreePool (Service);\r
+  }\r
+}\r
+\r
+/**\r
+  Create then initialize a TLS service.\r
+\r
+  @param[in]  Image                  ImageHandle of the TLS driver\r
+  @param[out] Service                The service for TLS driver\r
+\r
+  @retval EFI_OUT_OF_RESOURCES   Failed to allocate resource to create the service.\r
+  @retval EFI_SUCCESS            The service is created for the driver.\r
+\r
+**/\r
+EFI_STATUS\r
+TlsCreateService (\r
+  IN  EFI_HANDLE            Image,\r
+  OUT TLS_SERVICE           **Service\r
+  )\r
+{\r
+  TLS_SERVICE            *TlsService;\r
+\r
+  ASSERT (Service != NULL);\r
+\r
+  *Service = NULL;\r
+\r
+  //\r
+  // Allocate a TLS Service Data\r
+  //\r
+  TlsService = AllocateZeroPool (sizeof (TLS_SERVICE));\r
+  if (TlsService == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Initialize TLS Service Data\r
+  //\r
+  TlsService->Signature        = TLS_SERVICE_SIGNATURE;\r
+  CopyMem (&TlsService->ServiceBinding, &mTlsServiceBinding, sizeof (TlsService->ServiceBinding));\r
+  TlsService->TlsChildrenNum   = 0;\r
+  InitializeListHead (&TlsService->TlsChildrenList);\r
+  TlsService->ImageHandle      = Image;\r
+\r
+  *Service = TlsService;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Unloads an image.\r
+\r
+  @param[in]  ImageHandle           Handle that identifies the image to be unloaded.\r
+\r
+  @retval EFI_SUCCESS           The image has been unloaded.\r
+  @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+TlsUnload (\r
+  IN EFI_HANDLE  ImageHandle\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  UINTN                           HandleNum;\r
+  EFI_HANDLE                      *HandleBuffer;\r
+  UINT32                          Index;\r
+  EFI_SERVICE_BINDING_PROTOCOL    *ServiceBinding;\r
+  TLS_SERVICE                     *TlsService;\r
+\r
+  HandleBuffer   = NULL;\r
+  ServiceBinding = NULL;\r
+  TlsService     = NULL;\r
+\r
+  //\r
+  // Locate all the handles with Tls service binding protocol.\r
+  //\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiTlsServiceBindingProtocolGuid,\r
+                  NULL,\r
+                  &HandleNum,\r
+                  &HandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  for (Index = 0; Index < HandleNum; Index++) {\r
+    //\r
+    // Firstly, find ServiceBinding interface\r
+    //\r
+    Status = gBS->OpenProtocol (\r
+                    HandleBuffer[Index],\r
+                    &gEfiTlsServiceBindingProtocolGuid,\r
+                    (VOID **) &ServiceBinding,\r
+                    ImageHandle,\r
+                    NULL,\r
+                    EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    TlsService = TLS_SERVICE_FROM_THIS (ServiceBinding);\r
+\r
+    //\r
+    // Then, uninstall ServiceBinding interface\r
+    //\r
+    Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                    HandleBuffer[Index],\r
+                    &gEfiTlsServiceBindingProtocolGuid, ServiceBinding,\r
+                    NULL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    TlsCleanService (TlsService);\r
+  }\r
+\r
+  if (HandleBuffer != NULL) {\r
+    FreePool (HandleBuffer);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  This is the declaration of an EFI image entry point. This entry point is\r
+  the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
+  both device drivers and bus drivers.\r
+\r
+  @param  ImageHandle           The firmware allocated handle for the UEFI image.\r
+  @param  SystemTable           A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS           The operation completed successfully.\r
+  @retval Others                An unexpected error occurred.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+TlsDriverEntryPoint (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS             Status;\r
+\r
+  TLS_SERVICE            *TlsService;\r
+\r
+  //\r
+  // Create TLS Service\r
+  //\r
+  Status = TlsCreateService (ImageHandle, &TlsService);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  ASSERT (TlsService != NULL);\r
+\r
+  //\r
+  // Initializes the OpenSSL library.\r
+  //\r
+  TlsInitialize ();\r
+\r
+  //\r
+  // Create a new SSL_CTX object as framework to establish TLS/SSL enabled\r
+  // connections. TLS 1.0 is used as the default version.\r
+  //\r
+  TlsService->TlsCtx = TlsCtxNew (TLS10_PROTOCOL_VERSION_MAJOR, TLS10_PROTOCOL_VERSION_MINOR);\r
+  if (TlsService->TlsCtx == NULL) {\r
+    FreePool (TlsService);\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  //\r
+  // Install the TlsServiceBinding Protocol onto Handle\r
+  //\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &TlsService->Handle,\r
+                  &gEfiTlsServiceBindingProtocolGuid,\r
+                  &TlsService->ServiceBinding,\r
+                  NULL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_CLEAN_SERVICE;\r
+  }\r
+\r
+  return Status;\r
+\r
+ON_CLEAN_SERVICE:\r
+  TlsCleanService (TlsService);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Creates a child handle and installs a protocol.\r
+\r
+  The CreateChild() function installs a protocol on ChildHandle.\r
+  If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.\r
+  If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.\r
+\r
+  @param[in] This        Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
+  @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,\r
+                         then a new handle is created. If it is a pointer to an existing UEFI handle,\r
+                         then the protocol is added to the existing UEFI handle.\r
+\r
+  @retval EFI_SUCCES            The protocol was added to ChildHandle.\r
+  @retval EFI_INVALID_PARAMETER ChildHandle is NULL.\r
+  @retval EFI_OUT_OF_RESOURCES  There are not enough resources available to create\r
+                                the child.\r
+  @retval other                 The child handle was not created.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+TlsServiceBindingCreateChild (\r
+  IN EFI_SERVICE_BINDING_PROTOCOL  *This,\r
+  IN EFI_HANDLE                    *ChildHandle\r
+  )\r
+{\r
+  TLS_SERVICE         *TlsService;\r
+  TLS_INSTANCE        *TlsInstance;\r
+  EFI_STATUS           Status;\r
+  EFI_TPL              OldTpl;\r
+\r
+  if ((This == NULL) || (ChildHandle == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  TlsService = TLS_SERVICE_FROM_THIS (This);\r
+\r
+  Status = TlsCreateInstance (TlsService, &TlsInstance);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  ASSERT (TlsInstance != NULL);\r
+\r
+  //\r
+  // Create a new TLS connection object.\r
+  //\r
+  TlsInstance->TlsConn = TlsNew (TlsService->TlsCtx);\r
+  if (TlsInstance->TlsConn == NULL) {\r
+    Status = EFI_ABORTED;\r
+    goto ON_ERROR;\r
+  }\r
+\r
+  //\r
+  // Set default ConnectionEnd to EfiTlsClient\r
+  //\r
+  Status = TlsSetConnectionEnd (TlsInstance->TlsConn, EfiTlsClient);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_ERROR;\r
+  }\r
+\r
+  //\r
+  // Install TLS protocol and configuration protocol onto ChildHandle\r
+  //\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  ChildHandle,\r
+                  &gEfiTlsProtocolGuid,\r
+                  &TlsInstance->Tls,\r
+                  &gEfiTlsConfigurationProtocolGuid,\r
+                  &TlsInstance->TlsConfig,\r
+                  NULL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_ERROR;\r
+  }\r
+\r
+  TlsInstance->ChildHandle = *ChildHandle;\r
+\r
+  //\r
+  // Add it to the TLS service's child list.\r
+  //\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+\r
+  InsertTailList (&TlsService->TlsChildrenList, &TlsInstance->Link);\r
+  TlsService->TlsChildrenNum++;\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  return EFI_SUCCESS;\r
+\r
+ON_ERROR:\r
+  TlsCleanInstance (TlsInstance);\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Destroys a child handle with a protocol installed on it.\r
+\r
+  The DestroyChild() function does the opposite of CreateChild(). It removes a protocol\r
+  that was installed by CreateChild() from ChildHandle. If the removed protocol is the\r
+  last protocol on ChildHandle, then ChildHandle is destroyed.\r
+\r
+  @param  This        Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.\r
+  @param  ChildHandle Handle of the child to destroy.\r
+\r
+  @retval EFI_SUCCES            The protocol was removed from ChildHandle.\r
+  @retval EFI_UNSUPPORTED       ChildHandle does not support the protocol that is being removed.\r
+  @retval EFI_INVALID_PARAMETER Child handle is NULL.\r
+  @retval EFI_ACCESS_DENIED     The protocol could not be removed from the ChildHandle\r
+                                because its services are being used.\r
+  @retval other                 The child handle was not destroyed.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+TlsServiceBindingDestroyChild (\r
+  IN EFI_SERVICE_BINDING_PROTOCOL  *This,\r
+  IN EFI_HANDLE                    ChildHandle\r
+  )\r
+{\r
+  TLS_SERVICE                    *TlsService;\r
+  TLS_INSTANCE                   *TlsInstance;\r
+\r
+  EFI_TLS_PROTOCOL               *Tls;\r
+  EFI_TLS_CONFIGURATION_PROTOCOL *TlsConfig;\r
+  EFI_STATUS                     Status;\r
+  EFI_TPL                        OldTpl;\r
+\r
+  if ((This == NULL) || (ChildHandle == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  TlsService = TLS_SERVICE_FROM_THIS (This);\r
+\r
+  //\r
+  // Find TLS protocol interface installed in ChildHandle\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ChildHandle,\r
+                  &gEfiTlsProtocolGuid,\r
+                  (VOID **) &Tls,\r
+                  TlsService->ImageHandle,\r
+                  NULL,\r
+                  EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Find TLS configuration protocol interface installed in ChildHandle\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ChildHandle,\r
+                  &gEfiTlsConfigurationProtocolGuid,\r
+                  (VOID **) &TlsConfig,\r
+                  TlsService->ImageHandle,\r
+                  NULL,\r
+                  EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  TlsInstance  = TLS_INSTANCE_FROM_PROTOCOL (Tls);\r
+\r
+  if (TlsInstance->Service != TlsService) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (TlsInstance->InDestroy) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+\r
+  TlsInstance->InDestroy = TRUE;\r
+\r
+  //\r
+  // Uninstall the TLS protocol and TLS Configuration Protocol interface installed in ChildHandle.\r
+  //\r
+  Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                  ChildHandle,\r
+                  &gEfiTlsProtocolGuid,\r
+                  Tls,\r
+                  &gEfiTlsConfigurationProtocolGuid,\r
+                  TlsConfig,\r
+                  NULL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  RemoveEntryList (&TlsInstance->Link);\r
+  TlsService->TlsChildrenNum--;\r
+\r
+  gBS->RestoreTPL (OldTpl);\r
+\r
+  TlsCleanInstance (TlsInstance);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r