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
,
208 if (EFI_ERROR (Status
)) {
212 TlsCleanService (TlsService
);
215 if (HandleBuffer
!= NULL
) {
216 FreePool (HandleBuffer
);
223 This is the declaration of an EFI image entry point. This entry point is
224 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
225 both device drivers and bus drivers.
227 @param ImageHandle The firmware allocated handle for the UEFI image.
228 @param SystemTable A pointer to the EFI System Table.
230 @retval EFI_SUCCESS The operation completed successfully.
231 @retval Others An unexpected error occurred.
235 TlsDriverEntryPoint (
236 IN EFI_HANDLE ImageHandle
,
237 IN EFI_SYSTEM_TABLE
*SystemTable
242 TLS_SERVICE
*TlsService
;
245 // Create TLS Service
247 Status
= TlsCreateService (ImageHandle
, &TlsService
);
248 if (EFI_ERROR (Status
)) {
252 ASSERT (TlsService
!= NULL
);
255 // Initializes the OpenSSL library.
260 // Create a new SSL_CTX object as framework to establish TLS/SSL enabled
261 // connections. TLS 1.0 is used as the default version.
263 TlsService
->TlsCtx
= TlsCtxNew (TLS10_PROTOCOL_VERSION_MAJOR
, TLS10_PROTOCOL_VERSION_MINOR
);
264 if (TlsService
->TlsCtx
== NULL
) {
265 FreePool (TlsService
);
270 // Install the TlsServiceBinding Protocol onto Handle
272 Status
= gBS
->InstallMultipleProtocolInterfaces (
274 &gEfiTlsServiceBindingProtocolGuid
,
275 &TlsService
->ServiceBinding
,
278 if (EFI_ERROR (Status
)) {
279 goto ON_CLEAN_SERVICE
;
285 TlsCleanService (TlsService
);
291 Creates a child handle and installs a protocol.
293 The CreateChild() function installs a protocol on ChildHandle.
294 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
295 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
297 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
298 @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
299 then a new handle is created. If it is a pointer to an existing UEFI handle,
300 then the protocol is added to the existing UEFI handle.
302 @retval EFI_SUCCESS The protocol was added to ChildHandle.
303 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
304 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
306 @retval other The child handle was not created.
311 TlsServiceBindingCreateChild (
312 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
313 IN EFI_HANDLE
*ChildHandle
316 TLS_SERVICE
*TlsService
;
317 TLS_INSTANCE
*TlsInstance
;
321 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
322 return EFI_INVALID_PARAMETER
;
325 TlsService
= TLS_SERVICE_FROM_THIS (This
);
327 Status
= TlsCreateInstance (TlsService
, &TlsInstance
);
328 if (EFI_ERROR (Status
)) {
332 ASSERT (TlsInstance
!= NULL
);
335 // Create a new TLS connection object.
337 TlsInstance
->TlsConn
= TlsNew (TlsService
->TlsCtx
);
338 if (TlsInstance
->TlsConn
== NULL
) {
339 Status
= EFI_ABORTED
;
344 // Set default ConnectionEnd to EfiTlsClient
346 Status
= TlsSetConnectionEnd (TlsInstance
->TlsConn
, EfiTlsClient
);
347 if (EFI_ERROR (Status
)) {
352 // Install TLS protocol and configuration protocol onto ChildHandle
354 Status
= gBS
->InstallMultipleProtocolInterfaces (
356 &gEfiTlsProtocolGuid
,
358 &gEfiTlsConfigurationProtocolGuid
,
359 &TlsInstance
->TlsConfig
,
362 if (EFI_ERROR (Status
)) {
366 TlsInstance
->ChildHandle
= *ChildHandle
;
369 // Add it to the TLS service's child list.
371 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
373 InsertTailList (&TlsService
->TlsChildrenList
, &TlsInstance
->Link
);
374 TlsService
->TlsChildrenNum
++;
376 gBS
->RestoreTPL (OldTpl
);
381 TlsCleanInstance (TlsInstance
);
386 Destroys a child handle with a protocol installed on it.
388 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
389 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
390 last protocol on ChildHandle, then ChildHandle is destroyed.
392 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
393 @param ChildHandle Handle of the child to destroy.
395 @retval EFI_SUCCESS The protocol was removed from ChildHandle.
396 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
397 @retval EFI_INVALID_PARAMETER Child handle is NULL.
398 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
399 because its services are being used.
400 @retval other The child handle was not destroyed.
405 TlsServiceBindingDestroyChild (
406 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
407 IN EFI_HANDLE ChildHandle
410 TLS_SERVICE
*TlsService
;
411 TLS_INSTANCE
*TlsInstance
;
413 EFI_TLS_PROTOCOL
*Tls
;
414 EFI_TLS_CONFIGURATION_PROTOCOL
*TlsConfig
;
418 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
419 return EFI_INVALID_PARAMETER
;
422 TlsService
= TLS_SERVICE_FROM_THIS (This
);
425 // Find TLS protocol interface installed in ChildHandle
427 Status
= gBS
->OpenProtocol (
429 &gEfiTlsProtocolGuid
,
431 TlsService
->ImageHandle
,
433 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
435 if (EFI_ERROR (Status
)) {
440 // Find TLS configuration protocol interface installed in ChildHandle
442 Status
= gBS
->OpenProtocol (
444 &gEfiTlsConfigurationProtocolGuid
,
446 TlsService
->ImageHandle
,
448 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
450 if (EFI_ERROR (Status
)) {
454 TlsInstance
= TLS_INSTANCE_FROM_PROTOCOL (Tls
);
456 if (TlsInstance
->Service
!= TlsService
) {
457 return EFI_INVALID_PARAMETER
;
460 if (TlsInstance
->InDestroy
) {
464 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
466 TlsInstance
->InDestroy
= TRUE
;
469 // Uninstall the TLS protocol and TLS Configuration Protocol interface installed in ChildHandle.
471 Status
= gBS
->UninstallMultipleProtocolInterfaces (
473 &gEfiTlsProtocolGuid
,
475 &gEfiTlsConfigurationProtocolGuid
,
479 if (EFI_ERROR (Status
)) {
483 RemoveEntryList (&TlsInstance
->Link
);
484 TlsService
->TlsChildrenNum
--;
486 gBS
->RestoreTPL (OldTpl
);
488 TlsCleanInstance (TlsInstance
);