1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
/** @file
The Driver Binding and Service Binding Protocol for TlsDxe driver.
Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#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_SUCCESS 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_SUCCESS 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;
}
|