TlsDriver.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /** @file
  2. The Driver Binding and Service Binding Protocol for TlsDxe driver.
  3. Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "TlsImpl.h"
  7. EFI_SERVICE_BINDING_PROTOCOL mTlsServiceBinding = {
  8. TlsServiceBindingCreateChild,
  9. TlsServiceBindingDestroyChild
  10. };
  11. /**
  12. Release all the resources used by the TLS instance.
  13. @param[in] Instance The TLS instance data.
  14. **/
  15. VOID
  16. TlsCleanInstance (
  17. IN TLS_INSTANCE *Instance
  18. )
  19. {
  20. if (Instance != NULL) {
  21. if (Instance->TlsConn != NULL) {
  22. TlsFree (Instance->TlsConn);
  23. }
  24. FreePool (Instance);
  25. }
  26. }
  27. /**
  28. Create the TLS instance and initialize it.
  29. @param[in] Service The pointer to the TLS service.
  30. @param[out] Instance The pointer to the TLS instance.
  31. @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
  32. @retval EFI_SUCCESS The TLS instance is created.
  33. **/
  34. EFI_STATUS
  35. TlsCreateInstance (
  36. IN TLS_SERVICE *Service,
  37. OUT TLS_INSTANCE **Instance
  38. )
  39. {
  40. TLS_INSTANCE *TlsInstance;
  41. *Instance = NULL;
  42. TlsInstance = AllocateZeroPool (sizeof (TLS_INSTANCE));
  43. if (TlsInstance == NULL) {
  44. return EFI_OUT_OF_RESOURCES;
  45. }
  46. TlsInstance->Signature = TLS_INSTANCE_SIGNATURE;
  47. InitializeListHead (&TlsInstance->Link);
  48. TlsInstance->InDestroy = FALSE;
  49. TlsInstance->Service = Service;
  50. CopyMem (&TlsInstance->Tls, &mTlsProtocol, sizeof (TlsInstance->Tls));
  51. CopyMem (&TlsInstance->TlsConfig, &mTlsConfigurationProtocol, sizeof (TlsInstance->TlsConfig));
  52. TlsInstance->TlsSessionState = EfiTlsSessionNotStarted;
  53. *Instance = TlsInstance;
  54. return EFI_SUCCESS;
  55. }
  56. /**
  57. Release all the resources used by the TLS service binding instance.
  58. @param[in] Service The TLS service data.
  59. **/
  60. VOID
  61. TlsCleanService (
  62. IN TLS_SERVICE *Service
  63. )
  64. {
  65. if (Service != NULL) {
  66. if (Service->TlsCtx != NULL) {
  67. TlsCtxFree (Service->TlsCtx);
  68. }
  69. FreePool (Service);
  70. }
  71. }
  72. /**
  73. Create then initialize a TLS service.
  74. @param[in] Image ImageHandle of the TLS driver
  75. @param[out] Service The service for TLS driver
  76. @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to create the service.
  77. @retval EFI_SUCCESS The service is created for the driver.
  78. **/
  79. EFI_STATUS
  80. TlsCreateService (
  81. IN EFI_HANDLE Image,
  82. OUT TLS_SERVICE **Service
  83. )
  84. {
  85. TLS_SERVICE *TlsService;
  86. ASSERT (Service != NULL);
  87. *Service = NULL;
  88. //
  89. // Allocate a TLS Service Data
  90. //
  91. TlsService = AllocateZeroPool (sizeof (TLS_SERVICE));
  92. if (TlsService == NULL) {
  93. return EFI_OUT_OF_RESOURCES;
  94. }
  95. //
  96. // Initialize TLS Service Data
  97. //
  98. TlsService->Signature = TLS_SERVICE_SIGNATURE;
  99. CopyMem (&TlsService->ServiceBinding, &mTlsServiceBinding, sizeof (TlsService->ServiceBinding));
  100. TlsService->TlsChildrenNum = 0;
  101. InitializeListHead (&TlsService->TlsChildrenList);
  102. TlsService->ImageHandle = Image;
  103. *Service = TlsService;
  104. return EFI_SUCCESS;
  105. }
  106. /**
  107. Unloads an image.
  108. @param[in] ImageHandle Handle that identifies the image to be unloaded.
  109. @retval EFI_SUCCESS The image has been unloaded.
  110. @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
  111. **/
  112. EFI_STATUS
  113. EFIAPI
  114. TlsUnload (
  115. IN EFI_HANDLE ImageHandle
  116. )
  117. {
  118. EFI_STATUS Status;
  119. UINTN HandleNum;
  120. EFI_HANDLE *HandleBuffer;
  121. UINT32 Index;
  122. EFI_SERVICE_BINDING_PROTOCOL *ServiceBinding;
  123. TLS_SERVICE *TlsService;
  124. HandleBuffer = NULL;
  125. ServiceBinding = NULL;
  126. TlsService = NULL;
  127. //
  128. // Locate all the handles with Tls service binding protocol.
  129. //
  130. Status = gBS->LocateHandleBuffer (
  131. ByProtocol,
  132. &gEfiTlsServiceBindingProtocolGuid,
  133. NULL,
  134. &HandleNum,
  135. &HandleBuffer
  136. );
  137. if (EFI_ERROR (Status)) {
  138. return Status;
  139. }
  140. for (Index = 0; Index < HandleNum; Index++) {
  141. //
  142. // Firstly, find ServiceBinding interface
  143. //
  144. Status = gBS->OpenProtocol (
  145. HandleBuffer[Index],
  146. &gEfiTlsServiceBindingProtocolGuid,
  147. (VOID **)&ServiceBinding,
  148. ImageHandle,
  149. NULL,
  150. EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
  151. );
  152. if (EFI_ERROR (Status)) {
  153. return Status;
  154. }
  155. TlsService = TLS_SERVICE_FROM_THIS (ServiceBinding);
  156. //
  157. // Then, uninstall ServiceBinding interface
  158. //
  159. Status = gBS->UninstallMultipleProtocolInterfaces (
  160. HandleBuffer[Index],
  161. &gEfiTlsServiceBindingProtocolGuid,
  162. ServiceBinding,
  163. NULL
  164. );
  165. if (EFI_ERROR (Status)) {
  166. return Status;
  167. }
  168. TlsCleanService (TlsService);
  169. }
  170. if (HandleBuffer != NULL) {
  171. FreePool (HandleBuffer);
  172. }
  173. return EFI_SUCCESS;
  174. }
  175. /**
  176. This is the declaration of an EFI image entry point. This entry point is
  177. the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
  178. both device drivers and bus drivers.
  179. @param ImageHandle The firmware allocated handle for the UEFI image.
  180. @param SystemTable A pointer to the EFI System Table.
  181. @retval EFI_SUCCESS The operation completed successfully.
  182. @retval Others An unexpected error occurred.
  183. **/
  184. EFI_STATUS
  185. EFIAPI
  186. TlsDriverEntryPoint (
  187. IN EFI_HANDLE ImageHandle,
  188. IN EFI_SYSTEM_TABLE *SystemTable
  189. )
  190. {
  191. EFI_STATUS Status;
  192. TLS_SERVICE *TlsService;
  193. //
  194. // Create TLS Service
  195. //
  196. Status = TlsCreateService (ImageHandle, &TlsService);
  197. if (EFI_ERROR (Status)) {
  198. return Status;
  199. }
  200. ASSERT (TlsService != NULL);
  201. //
  202. // Initializes the OpenSSL library.
  203. //
  204. TlsInitialize ();
  205. //
  206. // Create a new SSL_CTX object as framework to establish TLS/SSL enabled
  207. // connections. TLS 1.0 is used as the default version.
  208. //
  209. TlsService->TlsCtx = TlsCtxNew (TLS10_PROTOCOL_VERSION_MAJOR, TLS10_PROTOCOL_VERSION_MINOR);
  210. if (TlsService->TlsCtx == NULL) {
  211. FreePool (TlsService);
  212. return EFI_ABORTED;
  213. }
  214. //
  215. // Install the TlsServiceBinding Protocol onto Handle
  216. //
  217. Status = gBS->InstallMultipleProtocolInterfaces (
  218. &TlsService->Handle,
  219. &gEfiTlsServiceBindingProtocolGuid,
  220. &TlsService->ServiceBinding,
  221. NULL
  222. );
  223. if (EFI_ERROR (Status)) {
  224. goto ON_CLEAN_SERVICE;
  225. }
  226. return Status;
  227. ON_CLEAN_SERVICE:
  228. TlsCleanService (TlsService);
  229. return Status;
  230. }
  231. /**
  232. Creates a child handle and installs a protocol.
  233. The CreateChild() function installs a protocol on ChildHandle.
  234. If ChildHandle is a pointer to NULL, then a new handle is created and returned in ChildHandle.
  235. If ChildHandle is not a pointer to NULL, then the protocol installs on the existing ChildHandle.
  236. @param[in] This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  237. @param[in] ChildHandle Pointer to the handle of the child to create. If it is NULL,
  238. then a new handle is created. If it is a pointer to an existing UEFI handle,
  239. then the protocol is added to the existing UEFI handle.
  240. @retval EFI_SUCCESS The protocol was added to ChildHandle.
  241. @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
  242. @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
  243. the child.
  244. @retval other The child handle was not created.
  245. **/
  246. EFI_STATUS
  247. EFIAPI
  248. TlsServiceBindingCreateChild (
  249. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  250. IN EFI_HANDLE *ChildHandle
  251. )
  252. {
  253. TLS_SERVICE *TlsService;
  254. TLS_INSTANCE *TlsInstance;
  255. EFI_STATUS Status;
  256. EFI_TPL OldTpl;
  257. if ((This == NULL) || (ChildHandle == NULL)) {
  258. return EFI_INVALID_PARAMETER;
  259. }
  260. TlsService = TLS_SERVICE_FROM_THIS (This);
  261. Status = TlsCreateInstance (TlsService, &TlsInstance);
  262. if (EFI_ERROR (Status)) {
  263. return Status;
  264. }
  265. ASSERT (TlsInstance != NULL);
  266. //
  267. // Create a new TLS connection object.
  268. //
  269. TlsInstance->TlsConn = TlsNew (TlsService->TlsCtx);
  270. if (TlsInstance->TlsConn == NULL) {
  271. Status = EFI_ABORTED;
  272. goto ON_ERROR;
  273. }
  274. //
  275. // Set default ConnectionEnd to EfiTlsClient
  276. //
  277. Status = TlsSetConnectionEnd (TlsInstance->TlsConn, EfiTlsClient);
  278. if (EFI_ERROR (Status)) {
  279. goto ON_ERROR;
  280. }
  281. //
  282. // Install TLS protocol and configuration protocol onto ChildHandle
  283. //
  284. Status = gBS->InstallMultipleProtocolInterfaces (
  285. ChildHandle,
  286. &gEfiTlsProtocolGuid,
  287. &TlsInstance->Tls,
  288. &gEfiTlsConfigurationProtocolGuid,
  289. &TlsInstance->TlsConfig,
  290. NULL
  291. );
  292. if (EFI_ERROR (Status)) {
  293. goto ON_ERROR;
  294. }
  295. TlsInstance->ChildHandle = *ChildHandle;
  296. //
  297. // Add it to the TLS service's child list.
  298. //
  299. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  300. InsertTailList (&TlsService->TlsChildrenList, &TlsInstance->Link);
  301. TlsService->TlsChildrenNum++;
  302. gBS->RestoreTPL (OldTpl);
  303. return EFI_SUCCESS;
  304. ON_ERROR:
  305. TlsCleanInstance (TlsInstance);
  306. return Status;
  307. }
  308. /**
  309. Destroys a child handle with a protocol installed on it.
  310. The DestroyChild() function does the opposite of CreateChild(). It removes a protocol
  311. that was installed by CreateChild() from ChildHandle. If the removed protocol is the
  312. last protocol on ChildHandle, then ChildHandle is destroyed.
  313. @param This Pointer to the EFI_SERVICE_BINDING_PROTOCOL instance.
  314. @param ChildHandle Handle of the child to destroy.
  315. @retval EFI_SUCCESS The protocol was removed from ChildHandle.
  316. @retval EFI_UNSUPPORTED ChildHandle does not support the protocol that is being removed.
  317. @retval EFI_INVALID_PARAMETER Child handle is NULL.
  318. @retval EFI_ACCESS_DENIED The protocol could not be removed from the ChildHandle
  319. because its services are being used.
  320. @retval other The child handle was not destroyed.
  321. **/
  322. EFI_STATUS
  323. EFIAPI
  324. TlsServiceBindingDestroyChild (
  325. IN EFI_SERVICE_BINDING_PROTOCOL *This,
  326. IN EFI_HANDLE ChildHandle
  327. )
  328. {
  329. TLS_SERVICE *TlsService;
  330. TLS_INSTANCE *TlsInstance;
  331. EFI_TLS_PROTOCOL *Tls;
  332. EFI_TLS_CONFIGURATION_PROTOCOL *TlsConfig;
  333. EFI_STATUS Status;
  334. EFI_TPL OldTpl;
  335. if ((This == NULL) || (ChildHandle == NULL)) {
  336. return EFI_INVALID_PARAMETER;
  337. }
  338. TlsService = TLS_SERVICE_FROM_THIS (This);
  339. //
  340. // Find TLS protocol interface installed in ChildHandle
  341. //
  342. Status = gBS->OpenProtocol (
  343. ChildHandle,
  344. &gEfiTlsProtocolGuid,
  345. (VOID **)&Tls,
  346. TlsService->ImageHandle,
  347. NULL,
  348. EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
  349. );
  350. if (EFI_ERROR (Status)) {
  351. return Status;
  352. }
  353. //
  354. // Find TLS configuration protocol interface installed in ChildHandle
  355. //
  356. Status = gBS->OpenProtocol (
  357. ChildHandle,
  358. &gEfiTlsConfigurationProtocolGuid,
  359. (VOID **)&TlsConfig,
  360. TlsService->ImageHandle,
  361. NULL,
  362. EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
  363. );
  364. if (EFI_ERROR (Status)) {
  365. return Status;
  366. }
  367. TlsInstance = TLS_INSTANCE_FROM_PROTOCOL (Tls);
  368. if (TlsInstance->Service != TlsService) {
  369. return EFI_INVALID_PARAMETER;
  370. }
  371. if (TlsInstance->InDestroy) {
  372. return EFI_SUCCESS;
  373. }
  374. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  375. TlsInstance->InDestroy = TRUE;
  376. //
  377. // Uninstall the TLS protocol and TLS Configuration Protocol interface installed in ChildHandle.
  378. //
  379. Status = gBS->UninstallMultipleProtocolInterfaces (
  380. ChildHandle,
  381. &gEfiTlsProtocolGuid,
  382. Tls,
  383. &gEfiTlsConfigurationProtocolGuid,
  384. TlsConfig,
  385. NULL
  386. );
  387. if (EFI_ERROR (Status)) {
  388. return Status;
  389. }
  390. RemoveEntryList (&TlsInstance->Link);
  391. TlsService->TlsChildrenNum--;
  392. gBS->RestoreTPL (OldTpl);
  393. TlsCleanInstance (TlsInstance);
  394. return EFI_SUCCESS;
  395. }