2 The Driver Binding and Service Binding Protocol for TlsDxe driver.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 EFI_SERVICE_BINDING_PROTOCOL mTlsServiceBinding
= {
13 TlsServiceBindingCreateChild
,
14 TlsServiceBindingDestroyChild
18 Release all the resources used by the TLS instance.
20 @param[in] Instance The TLS instance data.
25 IN TLS_INSTANCE
*Instance
28 if (Instance
!= NULL
) {
29 if (Instance
->TlsConn
!= NULL
) {
30 TlsFree (Instance
->TlsConn
);
38 Create the TLS instance and initialize it.
40 @param[in] Service The pointer to the TLS service.
41 @param[out] Instance The pointer to the TLS instance.
43 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
44 @retval EFI_SUCCESS The TLS instance is created.
49 IN TLS_SERVICE
*Service
,
50 OUT TLS_INSTANCE
**Instance
53 TLS_INSTANCE
*TlsInstance
;
57 TlsInstance
= AllocateZeroPool (sizeof (TLS_INSTANCE
));
58 if (TlsInstance
== NULL
) {
59 return EFI_OUT_OF_RESOURCES
;
62 TlsInstance
->Signature
= TLS_INSTANCE_SIGNATURE
;
63 InitializeListHead (&TlsInstance
->Link
);
64 TlsInstance
->InDestroy
= FALSE
;
65 TlsInstance
->Service
= Service
;
67 CopyMem (&TlsInstance
->Tls
, &mTlsProtocol
, sizeof (TlsInstance
->Tls
));
68 CopyMem (&TlsInstance
->TlsConfig
, &mTlsConfigurationProtocol
, sizeof (TlsInstance
->TlsConfig
));
70 TlsInstance
->TlsSessionState
= EfiTlsSessionNotStarted
;
72 *Instance
= TlsInstance
;
78 Release all the resources used by the TLS service binding instance.
80 @param[in] Service The TLS service data.
85 IN TLS_SERVICE
*Service
88 if (Service
!= NULL
) {
89 if (Service
->TlsCtx
!= NULL
) {
90 TlsCtxFree (Service
->TlsCtx
);
98 Create then initialize a TLS service.
100 @param[in] Image ImageHandle of the TLS driver
101 @param[out] Service The service for TLS driver
103 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the service.
104 @retval EFI_SUCCESS The service is created for the driver.
110 OUT TLS_SERVICE
**Service
113 TLS_SERVICE
*TlsService
;
115 ASSERT (Service
!= NULL
);
120 // Allocate a TLS Service Data
122 TlsService
= AllocateZeroPool (sizeof (TLS_SERVICE
));
123 if (TlsService
== NULL
) {
124 return EFI_OUT_OF_RESOURCES
;
128 // Initialize TLS Service Data
130 TlsService
->Signature
= TLS_SERVICE_SIGNATURE
;
131 CopyMem (&TlsService
->ServiceBinding
, &mTlsServiceBinding
, sizeof (TlsService
->ServiceBinding
));
132 TlsService
->TlsChildrenNum
= 0;
133 InitializeListHead (&TlsService
->TlsChildrenList
);
134 TlsService
->ImageHandle
= Image
;
136 *Service
= TlsService
;
144 @param[in] ImageHandle Handle that identifies the image to be unloaded.
146 @retval EFI_SUCCESS The image has been unloaded.
147 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
153 IN EFI_HANDLE ImageHandle
158 EFI_HANDLE
*HandleBuffer
;
160 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
161 TLS_SERVICE
*TlsService
;
164 ServiceBinding
= NULL
;
168 // Locate all the handles with Tls service binding protocol.
170 Status
= gBS
->LocateHandleBuffer (
172 &gEfiTlsServiceBindingProtocolGuid
,
177 if (EFI_ERROR (Status
)) {
181 for (Index
= 0; Index
< HandleNum
; Index
++) {
183 // Firstly, find ServiceBinding interface
185 Status
= gBS
->OpenProtocol (
187 &gEfiTlsServiceBindingProtocolGuid
,
188 (VOID
**) &ServiceBinding
,
191 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
193 if (EFI_ERROR (Status
)) {
197 TlsService
= TLS_SERVICE_FROM_THIS (ServiceBinding
);
200 // Then, uninstall ServiceBinding interface
202 Status
= gBS
->UninstallMultipleProtocolInterfaces (
204 &gEfiTlsServiceBindingProtocolGuid
, ServiceBinding
,
207 if (EFI_ERROR (Status
)) {
211 TlsCleanService (TlsService
);
214 if (HandleBuffer
!= NULL
) {
215 FreePool (HandleBuffer
);
222 This is the declaration of an EFI image entry point. This entry point is
223 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
224 both device drivers and bus drivers.
226 @param ImageHandle The firmware allocated handle for the UEFI image.
227 @param SystemTable A pointer to the EFI System Table.
229 @retval EFI_SUCCESS The operation completed successfully.
230 @retval Others An unexpected error occurred.
234 TlsDriverEntryPoint (
235 IN EFI_HANDLE ImageHandle
,
236 IN EFI_SYSTEM_TABLE
*SystemTable
241 TLS_SERVICE
*TlsService
;
244 // Create TLS Service
246 Status
= TlsCreateService (ImageHandle
, &TlsService
);
247 if (EFI_ERROR (Status
)) {
251 ASSERT (TlsService
!= NULL
);
254 // Initializes the OpenSSL library.
259 // Create a new SSL_CTX object as framework to establish TLS/SSL enabled
260 // connections. TLS 1.0 is used as the default version.
262 TlsService
->TlsCtx
= TlsCtxNew (TLS10_PROTOCOL_VERSION_MAJOR
, TLS10_PROTOCOL_VERSION_MINOR
);
263 if (TlsService
->TlsCtx
== NULL
) {
264 FreePool (TlsService
);
269 // Install the TlsServiceBinding Protocol onto Handle
271 Status
= gBS
->InstallMultipleProtocolInterfaces (
273 &gEfiTlsServiceBindingProtocolGuid
,
274 &TlsService
->ServiceBinding
,
277 if (EFI_ERROR (Status
)) {
278 goto ON_CLEAN_SERVICE
;
284 TlsCleanService (TlsService
);
290 Creates a child handle and installs a protocol.
292 The CreateChild() function installs a protocol on ChildHandle.
293 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
294 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
296 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
297 @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
298 then a new handle is created. If it is a pointer to an existing UEFI handle,
299 then the protocol is added to the existing UEFI handle.
301 @retval EFI_SUCCES The protocol was added to ChildHandle.
302 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
303 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
305 @retval other The child handle was not created.
310 TlsServiceBindingCreateChild (
311 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
312 IN EFI_HANDLE
*ChildHandle
315 TLS_SERVICE
*TlsService
;
316 TLS_INSTANCE
*TlsInstance
;
320 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
321 return EFI_INVALID_PARAMETER
;
324 TlsService
= TLS_SERVICE_FROM_THIS (This
);
326 Status
= TlsCreateInstance (TlsService
, &TlsInstance
);
327 if (EFI_ERROR (Status
)) {
331 ASSERT (TlsInstance
!= NULL
);
334 // Create a new TLS connection object.
336 TlsInstance
->TlsConn
= TlsNew (TlsService
->TlsCtx
);
337 if (TlsInstance
->TlsConn
== NULL
) {
338 Status
= EFI_ABORTED
;
343 // Set default ConnectionEnd to EfiTlsClient
345 Status
= TlsSetConnectionEnd (TlsInstance
->TlsConn
, EfiTlsClient
);
346 if (EFI_ERROR (Status
)) {
351 // Install TLS protocol and configuration protocol onto ChildHandle
353 Status
= gBS
->InstallMultipleProtocolInterfaces (
355 &gEfiTlsProtocolGuid
,
357 &gEfiTlsConfigurationProtocolGuid
,
358 &TlsInstance
->TlsConfig
,
361 if (EFI_ERROR (Status
)) {
365 TlsInstance
->ChildHandle
= *ChildHandle
;
368 // Add it to the TLS service's child list.
370 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
372 InsertTailList (&TlsService
->TlsChildrenList
, &TlsInstance
->Link
);
373 TlsService
->TlsChildrenNum
++;
375 gBS
->RestoreTPL (OldTpl
);
380 TlsCleanInstance (TlsInstance
);
385 Destroys a child handle with a protocol installed on it.
387 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
388 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
389 last protocol on ChildHandle, then ChildHandle is destroyed.
391 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
392 @param ChildHandle Handle of the child to destroy.
394 @retval EFI_SUCCES The protocol was removed from ChildHandle.
395 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
396 @retval EFI_INVALID_PARAMETER Child handle is NULL.
397 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
398 because its services are being used.
399 @retval other The child handle was not destroyed.
404 TlsServiceBindingDestroyChild (
405 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
406 IN EFI_HANDLE ChildHandle
409 TLS_SERVICE
*TlsService
;
410 TLS_INSTANCE
*TlsInstance
;
412 EFI_TLS_PROTOCOL
*Tls
;
413 EFI_TLS_CONFIGURATION_PROTOCOL
*TlsConfig
;
417 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
418 return EFI_INVALID_PARAMETER
;
421 TlsService
= TLS_SERVICE_FROM_THIS (This
);
424 // Find TLS protocol interface installed in ChildHandle
426 Status
= gBS
->OpenProtocol (
428 &gEfiTlsProtocolGuid
,
430 TlsService
->ImageHandle
,
432 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
434 if (EFI_ERROR (Status
)) {
439 // Find TLS configuration protocol interface installed in ChildHandle
441 Status
= gBS
->OpenProtocol (
443 &gEfiTlsConfigurationProtocolGuid
,
444 (VOID
**) &TlsConfig
,
445 TlsService
->ImageHandle
,
447 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
449 if (EFI_ERROR (Status
)) {
453 TlsInstance
= TLS_INSTANCE_FROM_PROTOCOL (Tls
);
455 if (TlsInstance
->Service
!= TlsService
) {
456 return EFI_INVALID_PARAMETER
;
459 if (TlsInstance
->InDestroy
) {
463 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
465 TlsInstance
->InDestroy
= TRUE
;
468 // Uninstall the TLS protocol and TLS Configuration Protocol interface installed in ChildHandle.
470 Status
= gBS
->UninstallMultipleProtocolInterfaces (
472 &gEfiTlsProtocolGuid
,
474 &gEfiTlsConfigurationProtocolGuid
,
478 if (EFI_ERROR (Status
)) {
482 RemoveEntryList (&TlsInstance
->Link
);
483 TlsService
->TlsChildrenNum
--;
485 gBS
->RestoreTPL (OldTpl
);
487 TlsCleanInstance (TlsInstance
);