2 The Driver Binding and Service Binding Protocol for TlsDxe driver.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 EFI_SERVICE_BINDING_PROTOCOL mTlsServiceBinding
= {
19 TlsServiceBindingCreateChild
,
20 TlsServiceBindingDestroyChild
24 Release all the resources used by the TLS instance.
26 @param[in] Instance The TLS instance data.
31 IN TLS_INSTANCE
*Instance
34 if (Instance
!= NULL
) {
35 if (Instance
->TlsConn
!= NULL
) {
36 TlsFree (Instance
->TlsConn
);
44 Create the TLS instance and initialize it.
46 @param[in] Service The pointer to the TLS service.
47 @param[out] Instance The pointer to the TLS instance.
49 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
50 @retval EFI_SUCCESS The TLS instance is created.
55 IN TLS_SERVICE
*Service
,
56 OUT TLS_INSTANCE
**Instance
59 TLS_INSTANCE
*TlsInstance
;
63 TlsInstance
= AllocateZeroPool (sizeof (TLS_INSTANCE
));
64 if (TlsInstance
== NULL
) {
65 return EFI_OUT_OF_RESOURCES
;
68 TlsInstance
->Signature
= TLS_INSTANCE_SIGNATURE
;
69 InitializeListHead (&TlsInstance
->Link
);
70 TlsInstance
->InDestroy
= FALSE
;
71 TlsInstance
->Service
= Service
;
73 CopyMem (&TlsInstance
->Tls
, &mTlsProtocol
, sizeof (TlsInstance
->Tls
));
74 CopyMem (&TlsInstance
->TlsConfig
, &mTlsConfigurationProtocol
, sizeof (TlsInstance
->TlsConfig
));
76 TlsInstance
->TlsSessionState
= EfiTlsSessionNotStarted
;
78 *Instance
= TlsInstance
;
84 Release all the resources used by the TLS service binding instance.
86 @param[in] Service The TLS service data.
91 IN TLS_SERVICE
*Service
94 if (Service
!= NULL
) {
95 if (Service
->TlsCtx
!= NULL
) {
96 TlsCtxFree (Service
->TlsCtx
);
104 Create then initialize a TLS service.
106 @param[in] Image ImageHandle of the TLS driver
107 @param[out] Service The service for TLS driver
109 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the service.
110 @retval EFI_SUCCESS The service is created for the driver.
116 OUT TLS_SERVICE
**Service
119 TLS_SERVICE
*TlsService
;
121 ASSERT (Service
!= NULL
);
126 // Allocate a TLS Service Data
128 TlsService
= AllocateZeroPool (sizeof (TLS_SERVICE
));
129 if (TlsService
== NULL
) {
130 return EFI_OUT_OF_RESOURCES
;
134 // Initialize TLS Service Data
136 TlsService
->Signature
= TLS_SERVICE_SIGNATURE
;
137 CopyMem (&TlsService
->ServiceBinding
, &mTlsServiceBinding
, sizeof (TlsService
->ServiceBinding
));
138 TlsService
->TlsChildrenNum
= 0;
139 InitializeListHead (&TlsService
->TlsChildrenList
);
140 TlsService
->ImageHandle
= Image
;
142 *Service
= TlsService
;
150 @param[in] ImageHandle Handle that identifies the image to be unloaded.
152 @retval EFI_SUCCESS The image has been unloaded.
153 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
159 IN EFI_HANDLE ImageHandle
164 EFI_HANDLE
*HandleBuffer
;
166 EFI_SERVICE_BINDING_PROTOCOL
*ServiceBinding
;
167 TLS_SERVICE
*TlsService
;
170 ServiceBinding
= NULL
;
174 // Locate all the handles with Tls service binding protocol.
176 Status
= gBS
->LocateHandleBuffer (
178 &gEfiTlsServiceBindingProtocolGuid
,
183 if (EFI_ERROR (Status
)) {
187 for (Index
= 0; Index
< HandleNum
; Index
++) {
189 // Firstly, find ServiceBinding interface
191 Status
= gBS
->OpenProtocol (
193 &gEfiTlsServiceBindingProtocolGuid
,
194 (VOID
**) &ServiceBinding
,
197 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
199 if (EFI_ERROR (Status
)) {
203 TlsService
= TLS_SERVICE_FROM_THIS (ServiceBinding
);
206 // Then, uninstall ServiceBinding interface
208 Status
= gBS
->UninstallMultipleProtocolInterfaces (
210 &gEfiTlsServiceBindingProtocolGuid
, ServiceBinding
,
213 if (EFI_ERROR (Status
)) {
217 TlsCleanService (TlsService
);
220 if (HandleBuffer
!= NULL
) {
221 FreePool (HandleBuffer
);
228 This is the declaration of an EFI image entry point. This entry point is
229 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
230 both device drivers and bus drivers.
232 @param ImageHandle The firmware allocated handle for the UEFI image.
233 @param SystemTable A pointer to the EFI System Table.
235 @retval EFI_SUCCESS The operation completed successfully.
236 @retval Others An unexpected error occurred.
240 TlsDriverEntryPoint (
241 IN EFI_HANDLE ImageHandle
,
242 IN EFI_SYSTEM_TABLE
*SystemTable
247 TLS_SERVICE
*TlsService
;
250 // Create TLS Service
252 Status
= TlsCreateService (ImageHandle
, &TlsService
);
253 if (EFI_ERROR (Status
)) {
257 ASSERT (TlsService
!= NULL
);
260 // Initializes the OpenSSL library.
265 // Create a new SSL_CTX object as framework to establish TLS/SSL enabled
266 // connections. TLS 1.0 is used as the default version.
268 TlsService
->TlsCtx
= TlsCtxNew (TLS10_PROTOCOL_VERSION_MAJOR
, TLS10_PROTOCOL_VERSION_MINOR
);
269 if (TlsService
->TlsCtx
== NULL
) {
270 FreePool (TlsService
);
275 // Install the TlsServiceBinding Protocol onto Handle
277 Status
= gBS
->InstallMultipleProtocolInterfaces (
279 &gEfiTlsServiceBindingProtocolGuid
,
280 &TlsService
->ServiceBinding
,
283 if (EFI_ERROR (Status
)) {
284 goto ON_CLEAN_SERVICE
;
290 TlsCleanService (TlsService
);
296 Creates a child handle and installs a protocol.
298 The CreateChild() function installs a protocol on ChildHandle.
299 If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
300 If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
302 @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
303 @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
304 then a new handle is created. If it is a pointer to an existing UEFI handle,
305 then the protocol is added to the existing UEFI handle.
307 @retval EFI_SUCCES The protocol was added to ChildHandle.
308 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
309 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
311 @retval other The child handle was not created.
316 TlsServiceBindingCreateChild (
317 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
318 IN EFI_HANDLE
*ChildHandle
321 TLS_SERVICE
*TlsService
;
322 TLS_INSTANCE
*TlsInstance
;
326 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
327 return EFI_INVALID_PARAMETER
;
330 TlsService
= TLS_SERVICE_FROM_THIS (This
);
332 Status
= TlsCreateInstance (TlsService
, &TlsInstance
);
333 if (EFI_ERROR (Status
)) {
337 ASSERT (TlsInstance
!= NULL
);
340 // Create a new TLS connection object.
342 TlsInstance
->TlsConn
= TlsNew (TlsService
->TlsCtx
);
343 if (TlsInstance
->TlsConn
== NULL
) {
344 Status
= EFI_ABORTED
;
349 // Set default ConnectionEnd to EfiTlsClient
351 Status
= TlsSetConnectionEnd (TlsInstance
->TlsConn
, EfiTlsClient
);
352 if (EFI_ERROR (Status
)) {
357 // Install TLS protocol and configuration protocol onto ChildHandle
359 Status
= gBS
->InstallMultipleProtocolInterfaces (
361 &gEfiTlsProtocolGuid
,
363 &gEfiTlsConfigurationProtocolGuid
,
364 &TlsInstance
->TlsConfig
,
367 if (EFI_ERROR (Status
)) {
371 TlsInstance
->ChildHandle
= *ChildHandle
;
374 // Add it to the TLS service's child list.
376 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
378 InsertTailList (&TlsService
->TlsChildrenList
, &TlsInstance
->Link
);
379 TlsService
->TlsChildrenNum
++;
381 gBS
->RestoreTPL (OldTpl
);
386 TlsCleanInstance (TlsInstance
);
391 Destroys a child handle with a protocol installed on it.
393 The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
394 that was installed by CreateChild() from ChildHandle. If the removed protocol is the
395 last protocol on ChildHandle, then ChildHandle is destroyed.
397 @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
398 @param ChildHandle Handle of the child to destroy.
400 @retval EFI_SUCCES The protocol was removed from ChildHandle.
401 @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
402 @retval EFI_INVALID_PARAMETER Child handle is NULL.
403 @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
404 because its services are being used.
405 @retval other The child handle was not destroyed.
410 TlsServiceBindingDestroyChild (
411 IN EFI_SERVICE_BINDING_PROTOCOL
*This
,
412 IN EFI_HANDLE ChildHandle
415 TLS_SERVICE
*TlsService
;
416 TLS_INSTANCE
*TlsInstance
;
418 EFI_TLS_PROTOCOL
*Tls
;
419 EFI_TLS_CONFIGURATION_PROTOCOL
*TlsConfig
;
423 if ((This
== NULL
) || (ChildHandle
== NULL
)) {
424 return EFI_INVALID_PARAMETER
;
427 TlsService
= TLS_SERVICE_FROM_THIS (This
);
430 // Find TLS protocol interface installed in ChildHandle
432 Status
= gBS
->OpenProtocol (
434 &gEfiTlsProtocolGuid
,
436 TlsService
->ImageHandle
,
438 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
440 if (EFI_ERROR (Status
)) {
445 // Find TLS configuration protocol interface installed in ChildHandle
447 Status
= gBS
->OpenProtocol (
449 &gEfiTlsConfigurationProtocolGuid
,
450 (VOID
**) &TlsConfig
,
451 TlsService
->ImageHandle
,
453 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
455 if (EFI_ERROR (Status
)) {
459 TlsInstance
= TLS_INSTANCE_FROM_PROTOCOL (Tls
);
461 if (TlsInstance
->Service
!= TlsService
) {
462 return EFI_INVALID_PARAMETER
;
465 if (TlsInstance
->InDestroy
) {
469 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
471 TlsInstance
->InDestroy
= TRUE
;
474 // Uninstall the TLS protocol and TLS Configuration Protocol interface installed in ChildHandle.
476 Status
= gBS
->UninstallMultipleProtocolInterfaces (
478 &gEfiTlsProtocolGuid
,
480 &gEfiTlsConfigurationProtocolGuid
,
484 if (EFI_ERROR (Status
)) {
488 RemoveEntryList (&TlsInstance
->Link
);
489 TlsService
->TlsChildrenNum
--;
491 gBS
->RestoreTPL (OldTpl
);
493 TlsCleanInstance (TlsInstance
);