TlsProtocol.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /** @file
  2. Implementation of EFI TLS Protocol Interfaces.
  3. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "TlsImpl.h"
  7. EFI_TLS_PROTOCOL mTlsProtocol = {
  8. TlsSetSessionData,
  9. TlsGetSessionData,
  10. TlsBuildResponsePacket,
  11. TlsProcessPacket
  12. };
  13. /**
  14. Set TLS session data.
  15. The SetSessionData() function set data for a new TLS session. All session data should
  16. be set before BuildResponsePacket() invoked.
  17. @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
  18. @param[in] DataType TLS session data type.
  19. @param[in] Data Pointer to session data.
  20. @param[in] DataSize Total size of session data.
  21. @retval EFI_SUCCESS The TLS session data is set successfully.
  22. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  23. This is NULL.
  24. Data is NULL.
  25. DataSize is 0.
  26. DataSize is invalid for DataType.
  27. @retval EFI_UNSUPPORTED The DataType is unsupported.
  28. @retval EFI_ACCESS_DENIED If the DataType is one of below:
  29. EfiTlsClientRandom
  30. EfiTlsServerRandom
  31. EfiTlsKeyMaterial
  32. @retval EFI_NOT_READY Current TLS session state is NOT
  33. EfiTlsSessionStateNotStarted.
  34. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  35. **/
  36. EFI_STATUS
  37. EFIAPI
  38. TlsSetSessionData (
  39. IN EFI_TLS_PROTOCOL *This,
  40. IN EFI_TLS_SESSION_DATA_TYPE DataType,
  41. IN VOID *Data,
  42. IN UINTN DataSize
  43. )
  44. {
  45. EFI_STATUS Status;
  46. TLS_INSTANCE *Instance;
  47. UINT16 *CipherId;
  48. CONST EFI_TLS_CIPHER *TlsCipherList;
  49. UINTN CipherCount;
  50. CONST EFI_TLS_VERIFY_HOST *TlsVerifyHost;
  51. EFI_TLS_VERIFY VerifyMethod;
  52. UINTN VerifyMethodSize;
  53. UINTN Index;
  54. EFI_TPL OldTpl;
  55. Status = EFI_SUCCESS;
  56. CipherId = NULL;
  57. VerifyMethodSize = sizeof (EFI_TLS_VERIFY);
  58. if ((This == NULL) || (Data == NULL) || (DataSize == 0)) {
  59. return EFI_INVALID_PARAMETER;
  60. }
  61. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  62. Instance = TLS_INSTANCE_FROM_PROTOCOL (This);
  63. if ((DataType != EfiTlsSessionState) && (Instance->TlsSessionState != EfiTlsSessionNotStarted)) {
  64. Status = EFI_NOT_READY;
  65. goto ON_EXIT;
  66. }
  67. switch (DataType) {
  68. //
  69. // Session Configuration
  70. //
  71. case EfiTlsVersion:
  72. if (DataSize != sizeof (EFI_TLS_VERSION)) {
  73. Status = EFI_INVALID_PARAMETER;
  74. goto ON_EXIT;
  75. }
  76. Status = TlsSetVersion (Instance->TlsConn, ((EFI_TLS_VERSION *)Data)->Major, ((EFI_TLS_VERSION *)Data)->Minor);
  77. break;
  78. case EfiTlsConnectionEnd:
  79. if (DataSize != sizeof (EFI_TLS_CONNECTION_END)) {
  80. Status = EFI_INVALID_PARAMETER;
  81. goto ON_EXIT;
  82. }
  83. Status = TlsSetConnectionEnd (Instance->TlsConn, *((EFI_TLS_CONNECTION_END *)Data));
  84. break;
  85. case EfiTlsCipherList:
  86. if (DataSize % sizeof (EFI_TLS_CIPHER) != 0) {
  87. Status = EFI_INVALID_PARAMETER;
  88. goto ON_EXIT;
  89. }
  90. CipherId = AllocatePool (DataSize);
  91. if (CipherId == NULL) {
  92. Status = EFI_OUT_OF_RESOURCES;
  93. goto ON_EXIT;
  94. }
  95. TlsCipherList = (CONST EFI_TLS_CIPHER *)Data;
  96. CipherCount = DataSize / sizeof (EFI_TLS_CIPHER);
  97. for (Index = 0; Index < CipherCount; Index++) {
  98. CipherId[Index] = ((TlsCipherList[Index].Data1 << 8) |
  99. TlsCipherList[Index].Data2);
  100. }
  101. Status = TlsSetCipherList (Instance->TlsConn, CipherId, CipherCount);
  102. FreePool (CipherId);
  103. break;
  104. case EfiTlsCompressionMethod:
  105. //
  106. // TLS seems only define one CompressionMethod.null, which specifies that data exchanged via the
  107. // record protocol will not be compressed.
  108. // More information from OpenSSL: http://www.openssl.org/docs/manmaster/ssl/SSL_COMP_add_compression_method.html
  109. // The TLS RFC does however not specify compression methods or their corresponding identifiers,
  110. // so there is currently no compatible way to integrate compression with unknown peers.
  111. // It is therefore currently not recommended to integrate compression into applications.
  112. // Applications for non-public use may agree on certain compression methods.
  113. // Using different compression methods with the same identifier will lead to connection failure.
  114. //
  115. for (Index = 0; Index < DataSize / sizeof (EFI_TLS_COMPRESSION); Index++) {
  116. Status = TlsSetCompressionMethod (*((UINT8 *)Data + Index));
  117. if (EFI_ERROR (Status)) {
  118. break;
  119. }
  120. }
  121. break;
  122. case EfiTlsExtensionData:
  123. Status = EFI_UNSUPPORTED;
  124. goto ON_EXIT;
  125. case EfiTlsVerifyMethod:
  126. if (DataSize != sizeof (EFI_TLS_VERIFY)) {
  127. Status = EFI_INVALID_PARAMETER;
  128. goto ON_EXIT;
  129. }
  130. TlsSetVerify (Instance->TlsConn, *((UINT32 *)Data));
  131. break;
  132. case EfiTlsVerifyHost:
  133. if (DataSize != sizeof (EFI_TLS_VERIFY_HOST)) {
  134. Status = EFI_INVALID_PARAMETER;
  135. goto ON_EXIT;
  136. }
  137. TlsVerifyHost = (CONST EFI_TLS_VERIFY_HOST *)Data;
  138. if (((TlsVerifyHost->Flags & EFI_TLS_VERIFY_FLAG_ALWAYS_CHECK_SUBJECT) != 0) &&
  139. ((TlsVerifyHost->Flags & EFI_TLS_VERIFY_FLAG_NEVER_CHECK_SUBJECT) != 0))
  140. {
  141. Status = EFI_INVALID_PARAMETER;
  142. goto ON_EXIT;
  143. }
  144. if (((TlsVerifyHost->Flags & EFI_TLS_VERIFY_FLAG_NO_WILDCARDS) != 0) &&
  145. (((TlsVerifyHost->Flags & EFI_TLS_VERIFY_FLAG_NO_PARTIAL_WILDCARDS) != 0) ||
  146. ((TlsVerifyHost->Flags & EFI_TLS_VERIFY_FLAG_MULTI_LABEL_WILDCARDS) != 0)))
  147. {
  148. Status = EFI_INVALID_PARAMETER;
  149. goto ON_EXIT;
  150. }
  151. Status = This->GetSessionData (This, EfiTlsVerifyMethod, &VerifyMethod, &VerifyMethodSize);
  152. if (EFI_ERROR (Status)) {
  153. goto ON_EXIT;
  154. }
  155. if ((VerifyMethod & EFI_TLS_VERIFY_PEER) == 0) {
  156. Status = EFI_INVALID_PARAMETER;
  157. goto ON_EXIT;
  158. }
  159. Status = TlsSetVerifyHost (Instance->TlsConn, TlsVerifyHost->Flags, TlsVerifyHost->HostName);
  160. break;
  161. case EfiTlsSessionID:
  162. if (DataSize != sizeof (EFI_TLS_SESSION_ID)) {
  163. Status = EFI_INVALID_PARAMETER;
  164. goto ON_EXIT;
  165. }
  166. Status = TlsSetSessionId (
  167. Instance->TlsConn,
  168. ((EFI_TLS_SESSION_ID *)Data)->Data,
  169. ((EFI_TLS_SESSION_ID *)Data)->Length
  170. );
  171. break;
  172. case EfiTlsSessionState:
  173. if (DataSize != sizeof (EFI_TLS_SESSION_STATE)) {
  174. Status = EFI_INVALID_PARAMETER;
  175. goto ON_EXIT;
  176. }
  177. Instance->TlsSessionState = *(EFI_TLS_SESSION_STATE *)Data;
  178. break;
  179. //
  180. // Session information
  181. //
  182. case EfiTlsClientRandom:
  183. Status = EFI_ACCESS_DENIED;
  184. break;
  185. case EfiTlsServerRandom:
  186. Status = EFI_ACCESS_DENIED;
  187. break;
  188. case EfiTlsKeyMaterial:
  189. Status = EFI_ACCESS_DENIED;
  190. break;
  191. //
  192. // Unsupported type.
  193. //
  194. default:
  195. Status = EFI_UNSUPPORTED;
  196. }
  197. ON_EXIT:
  198. gBS->RestoreTPL (OldTpl);
  199. return Status;
  200. }
  201. /**
  202. Get TLS session data.
  203. The GetSessionData() function return the TLS session information.
  204. @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
  205. @param[in] DataType TLS session data type.
  206. @param[in, out] Data Pointer to session data.
  207. @param[in, out] DataSize Total size of session data. On input, it means
  208. the size of Data buffer. On output, it means the size
  209. of copied Data buffer if EFI_SUCCESS, and means the
  210. size of desired Data buffer if EFI_BUFFER_TOO_SMALL.
  211. @retval EFI_SUCCESS The TLS session data is got successfully.
  212. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  213. This is NULL.
  214. DataSize is NULL.
  215. Data is NULL if *DataSize is not zero.
  216. @retval EFI_UNSUPPORTED The DataType is unsupported.
  217. @retval EFI_NOT_FOUND The TLS session data is not found.
  218. @retval EFI_NOT_READY The DataType is not ready in current session state.
  219. @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the data.
  220. **/
  221. EFI_STATUS
  222. EFIAPI
  223. TlsGetSessionData (
  224. IN EFI_TLS_PROTOCOL *This,
  225. IN EFI_TLS_SESSION_DATA_TYPE DataType,
  226. IN OUT VOID *Data OPTIONAL,
  227. IN OUT UINTN *DataSize
  228. )
  229. {
  230. EFI_STATUS Status;
  231. TLS_INSTANCE *Instance;
  232. EFI_TPL OldTpl;
  233. Status = EFI_SUCCESS;
  234. if ((This == NULL) || (DataSize == NULL) || ((Data == NULL) && (*DataSize != 0))) {
  235. return EFI_INVALID_PARAMETER;
  236. }
  237. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  238. Instance = TLS_INSTANCE_FROM_PROTOCOL (This);
  239. if ((Instance->TlsSessionState == EfiTlsSessionNotStarted) &&
  240. ((DataType == EfiTlsSessionID) || (DataType == EfiTlsClientRandom) ||
  241. (DataType == EfiTlsServerRandom) || (DataType == EfiTlsKeyMaterial)))
  242. {
  243. Status = EFI_NOT_READY;
  244. goto ON_EXIT;
  245. }
  246. switch (DataType) {
  247. case EfiTlsVersion:
  248. if (*DataSize < sizeof (EFI_TLS_VERSION)) {
  249. *DataSize = sizeof (EFI_TLS_VERSION);
  250. Status = EFI_BUFFER_TOO_SMALL;
  251. goto ON_EXIT;
  252. }
  253. *DataSize = sizeof (EFI_TLS_VERSION);
  254. *((UINT16 *)Data) = HTONS (TlsGetVersion (Instance->TlsConn));
  255. break;
  256. case EfiTlsConnectionEnd:
  257. if (*DataSize < sizeof (EFI_TLS_CONNECTION_END)) {
  258. *DataSize = sizeof (EFI_TLS_CONNECTION_END);
  259. Status = EFI_BUFFER_TOO_SMALL;
  260. goto ON_EXIT;
  261. }
  262. *DataSize = sizeof (EFI_TLS_CONNECTION_END);
  263. *((UINT8 *)Data) = TlsGetConnectionEnd (Instance->TlsConn);
  264. break;
  265. case EfiTlsCipherList:
  266. //
  267. // Get the current session cipher suite.
  268. //
  269. if (*DataSize < sizeof (EFI_TLS_CIPHER)) {
  270. *DataSize = sizeof (EFI_TLS_CIPHER);
  271. Status = EFI_BUFFER_TOO_SMALL;
  272. goto ON_EXIT;
  273. }
  274. *DataSize = sizeof (EFI_TLS_CIPHER);
  275. Status = TlsGetCurrentCipher (Instance->TlsConn, (UINT16 *)Data);
  276. *((UINT16 *)Data) = HTONS (*((UINT16 *)Data));
  277. break;
  278. case EfiTlsCompressionMethod:
  279. //
  280. // Get the current session compression method.
  281. //
  282. if (*DataSize < sizeof (EFI_TLS_COMPRESSION)) {
  283. *DataSize = sizeof (EFI_TLS_COMPRESSION);
  284. Status = EFI_BUFFER_TOO_SMALL;
  285. goto ON_EXIT;
  286. }
  287. *DataSize = sizeof (EFI_TLS_COMPRESSION);
  288. Status = TlsGetCurrentCompressionId (Instance->TlsConn, (UINT8 *)Data);
  289. break;
  290. case EfiTlsExtensionData:
  291. Status = EFI_UNSUPPORTED;
  292. goto ON_EXIT;
  293. case EfiTlsVerifyMethod:
  294. if (*DataSize < sizeof (EFI_TLS_VERIFY)) {
  295. *DataSize = sizeof (EFI_TLS_VERIFY);
  296. Status = EFI_BUFFER_TOO_SMALL;
  297. goto ON_EXIT;
  298. }
  299. *DataSize = sizeof (EFI_TLS_VERIFY);
  300. *((UINT32 *)Data) = TlsGetVerify (Instance->TlsConn);
  301. break;
  302. case EfiTlsSessionID:
  303. if (*DataSize < sizeof (EFI_TLS_SESSION_ID)) {
  304. *DataSize = sizeof (EFI_TLS_SESSION_ID);
  305. Status = EFI_BUFFER_TOO_SMALL;
  306. goto ON_EXIT;
  307. }
  308. *DataSize = sizeof (EFI_TLS_SESSION_ID);
  309. Status = TlsGetSessionId (
  310. Instance->TlsConn,
  311. ((EFI_TLS_SESSION_ID *)Data)->Data,
  312. &(((EFI_TLS_SESSION_ID *)Data)->Length)
  313. );
  314. break;
  315. case EfiTlsSessionState:
  316. if (*DataSize < sizeof (EFI_TLS_SESSION_STATE)) {
  317. *DataSize = sizeof (EFI_TLS_SESSION_STATE);
  318. Status = EFI_BUFFER_TOO_SMALL;
  319. goto ON_EXIT;
  320. }
  321. *DataSize = sizeof (EFI_TLS_SESSION_STATE);
  322. CopyMem (Data, &Instance->TlsSessionState, *DataSize);
  323. break;
  324. case EfiTlsClientRandom:
  325. if (*DataSize < sizeof (EFI_TLS_RANDOM)) {
  326. *DataSize = sizeof (EFI_TLS_RANDOM);
  327. Status = EFI_BUFFER_TOO_SMALL;
  328. goto ON_EXIT;
  329. }
  330. *DataSize = sizeof (EFI_TLS_RANDOM);
  331. TlsGetClientRandom (Instance->TlsConn, (UINT8 *)Data);
  332. break;
  333. case EfiTlsServerRandom:
  334. if (*DataSize < sizeof (EFI_TLS_RANDOM)) {
  335. *DataSize = sizeof (EFI_TLS_RANDOM);
  336. Status = EFI_BUFFER_TOO_SMALL;
  337. goto ON_EXIT;
  338. }
  339. *DataSize = sizeof (EFI_TLS_RANDOM);
  340. TlsGetServerRandom (Instance->TlsConn, (UINT8 *)Data);
  341. break;
  342. case EfiTlsKeyMaterial:
  343. if (*DataSize < sizeof (EFI_TLS_MASTER_SECRET)) {
  344. *DataSize = sizeof (EFI_TLS_MASTER_SECRET);
  345. Status = EFI_BUFFER_TOO_SMALL;
  346. goto ON_EXIT;
  347. }
  348. *DataSize = sizeof (EFI_TLS_MASTER_SECRET);
  349. Status = TlsGetKeyMaterial (Instance->TlsConn, (UINT8 *)Data);
  350. break;
  351. //
  352. // Unsupported type.
  353. //
  354. default:
  355. Status = EFI_UNSUPPORTED;
  356. }
  357. ON_EXIT:
  358. gBS->RestoreTPL (OldTpl);
  359. return Status;
  360. }
  361. /**
  362. Build response packet according to TLS state machine. This function is only valid for
  363. alert, handshake and change_cipher_spec content type.
  364. The BuildResponsePacket() function builds TLS response packet in response to the TLS
  365. request packet specified by RequestBuffer and RequestSize. If RequestBuffer is NULL and
  366. RequestSize is 0, and TLS session status is EfiTlsSessionNotStarted, the TLS session
  367. will be initiated and the response packet needs to be ClientHello. If RequestBuffer is
  368. NULL and RequestSize is 0, and TLS session status is EfiTlsSessionClosing, the TLS
  369. session will be closed and response packet needs to be CloseNotify. If RequestBuffer is
  370. NULL and RequestSize is 0, and TLS session status is EfiTlsSessionError, the TLS
  371. session has errors and the response packet needs to be Alert message based on error
  372. type.
  373. @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
  374. @param[in] RequestBuffer Pointer to the most recently received TLS packet. NULL
  375. means TLS need initiate the TLS session and response
  376. packet need to be ClientHello.
  377. @param[in] RequestSize Packet size in bytes for the most recently received TLS
  378. packet. 0 is only valid when RequestBuffer is NULL.
  379. @param[out] Buffer Pointer to the buffer to hold the built packet.
  380. @param[in, out] BufferSize Pointer to the buffer size in bytes. On input, it is
  381. the buffer size provided by the caller. On output, it
  382. is the buffer size in fact needed to contain the
  383. packet.
  384. @retval EFI_SUCCESS The required TLS packet is built successfully.
  385. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  386. This is NULL.
  387. RequestBuffer is NULL but RequestSize is NOT 0.
  388. RequestSize is 0 but RequestBuffer is NOT NULL.
  389. BufferSize is NULL.
  390. Buffer is NULL if *BufferSize is not zero.
  391. @retval EFI_BUFFER_TOO_SMALL BufferSize is too small to hold the response packet.
  392. @retval EFI_NOT_READY Current TLS session state is NOT ready to build
  393. ResponsePacket.
  394. @retval EFI_ABORTED Something wrong build response packet.
  395. **/
  396. EFI_STATUS
  397. EFIAPI
  398. TlsBuildResponsePacket (
  399. IN EFI_TLS_PROTOCOL *This,
  400. IN UINT8 *RequestBuffer OPTIONAL,
  401. IN UINTN RequestSize OPTIONAL,
  402. OUT UINT8 *Buffer OPTIONAL,
  403. IN OUT UINTN *BufferSize
  404. )
  405. {
  406. EFI_STATUS Status;
  407. TLS_INSTANCE *Instance;
  408. EFI_TPL OldTpl;
  409. Status = EFI_SUCCESS;
  410. if ((This == NULL) || (BufferSize == NULL) ||
  411. ((RequestBuffer == NULL) && (RequestSize != 0)) ||
  412. ((RequestBuffer != NULL) && (RequestSize == 0)) ||
  413. ((Buffer == NULL) && (*BufferSize != 0)))
  414. {
  415. return EFI_INVALID_PARAMETER;
  416. }
  417. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  418. Instance = TLS_INSTANCE_FROM_PROTOCOL (This);
  419. if ((RequestBuffer == NULL) && (RequestSize == 0)) {
  420. switch (Instance->TlsSessionState) {
  421. case EfiTlsSessionNotStarted:
  422. //
  423. // ClientHello.
  424. //
  425. Status = TlsDoHandshake (
  426. Instance->TlsConn,
  427. NULL,
  428. 0,
  429. Buffer,
  430. BufferSize
  431. );
  432. if (EFI_ERROR (Status)) {
  433. goto ON_EXIT;
  434. }
  435. //
  436. // *BufferSize should not be zero when ClientHello.
  437. //
  438. if (*BufferSize == 0) {
  439. Status = EFI_ABORTED;
  440. goto ON_EXIT;
  441. }
  442. Instance->TlsSessionState = EfiTlsSessionHandShaking;
  443. break;
  444. case EfiTlsSessionClosing:
  445. //
  446. // TLS session will be closed and response packet needs to be CloseNotify.
  447. //
  448. Status = TlsCloseNotify (
  449. Instance->TlsConn,
  450. Buffer,
  451. BufferSize
  452. );
  453. if (EFI_ERROR (Status)) {
  454. goto ON_EXIT;
  455. }
  456. //
  457. // *BufferSize should not be zero when build CloseNotify message.
  458. //
  459. if (*BufferSize == 0) {
  460. Status = EFI_ABORTED;
  461. goto ON_EXIT;
  462. }
  463. break;
  464. case EfiTlsSessionError:
  465. //
  466. // TLS session has errors and the response packet needs to be Alert
  467. // message based on error type.
  468. //
  469. Status = TlsHandleAlert (
  470. Instance->TlsConn,
  471. NULL,
  472. 0,
  473. Buffer,
  474. BufferSize
  475. );
  476. if (EFI_ERROR (Status)) {
  477. goto ON_EXIT;
  478. }
  479. break;
  480. default:
  481. //
  482. // Current TLS session state is NOT ready to build ResponsePacket.
  483. //
  484. Status = EFI_NOT_READY;
  485. }
  486. } else {
  487. //
  488. // 1. Received packet may have multiple TLS record messages.
  489. // 2. One TLS record message may have multiple handshake protocol.
  490. // 3. Some errors may be happened in handshake.
  491. // TlsDoHandshake() can handle all of those cases.
  492. //
  493. if (TlsInHandshake (Instance->TlsConn)) {
  494. Status = TlsDoHandshake (
  495. Instance->TlsConn,
  496. RequestBuffer,
  497. RequestSize,
  498. Buffer,
  499. BufferSize
  500. );
  501. if (EFI_ERROR (Status)) {
  502. goto ON_EXIT;
  503. }
  504. if (!TlsInHandshake (Instance->TlsConn)) {
  505. Instance->TlsSessionState = EfiTlsSessionDataTransferring;
  506. }
  507. } else {
  508. //
  509. // Must be alert message, Decrypt it and build the ResponsePacket.
  510. //
  511. ASSERT (((TLS_RECORD_HEADER *)RequestBuffer)->ContentType == TlsContentTypeAlert);
  512. Status = TlsHandleAlert (
  513. Instance->TlsConn,
  514. RequestBuffer,
  515. RequestSize,
  516. Buffer,
  517. BufferSize
  518. );
  519. if (EFI_ERROR (Status)) {
  520. if (Status != EFI_BUFFER_TOO_SMALL) {
  521. Instance->TlsSessionState = EfiTlsSessionError;
  522. }
  523. goto ON_EXIT;
  524. }
  525. }
  526. }
  527. ON_EXIT:
  528. gBS->RestoreTPL (OldTpl);
  529. return Status;
  530. }
  531. /**
  532. Decrypt or encrypt TLS packet during session. This function is only valid after
  533. session connected and for application_data content type.
  534. The ProcessPacket () function process each inbound or outbound TLS APP packet.
  535. @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
  536. @param[in, out] FragmentTable Pointer to a list of fragment. The caller will take
  537. responsible to handle the original FragmentTable while
  538. it may be reallocated in TLS driver. If CryptMode is
  539. EfiTlsEncrypt, on input these fragments contain the TLS
  540. header and plain text TLS APP payload; on output these
  541. fragments contain the TLS header and cipher text TLS
  542. APP payload. If CryptMode is EfiTlsDecrypt, on input
  543. these fragments contain the TLS header and cipher text
  544. TLS APP payload; on output these fragments contain the
  545. TLS header and plain text TLS APP payload.
  546. @param[in] FragmentCount Number of fragment.
  547. @param[in] CryptMode Crypt mode.
  548. @retval EFI_SUCCESS The operation completed successfully.
  549. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  550. This is NULL.
  551. FragmentTable is NULL.
  552. FragmentCount is NULL.
  553. CryptoMode is invalid.
  554. @retval EFI_NOT_READY Current TLS session state is NOT
  555. EfiTlsSessionDataTransferring.
  556. @retval EFI_ABORTED Something wrong decryption the message. TLS session
  557. status will become EfiTlsSessionError. The caller need
  558. call BuildResponsePacket() to generate Error Alert
  559. message and send it out.
  560. @retval EFI_OUT_OF_RESOURCES No enough resource to finish the operation.
  561. **/
  562. EFI_STATUS
  563. EFIAPI
  564. TlsProcessPacket (
  565. IN EFI_TLS_PROTOCOL *This,
  566. IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable,
  567. IN UINT32 *FragmentCount,
  568. IN EFI_TLS_CRYPT_MODE CryptMode
  569. )
  570. {
  571. EFI_STATUS Status;
  572. TLS_INSTANCE *Instance;
  573. EFI_TPL OldTpl;
  574. Status = EFI_SUCCESS;
  575. if ((This == NULL) || (FragmentTable == NULL) || (FragmentCount == NULL)) {
  576. return EFI_INVALID_PARAMETER;
  577. }
  578. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  579. Instance = TLS_INSTANCE_FROM_PROTOCOL (This);
  580. if (Instance->TlsSessionState != EfiTlsSessionDataTransferring) {
  581. Status = EFI_NOT_READY;
  582. goto ON_EXIT;
  583. }
  584. //
  585. // Packet sent or received may have multiple TLS record messages (Application data type).
  586. // So,on input these fragments contain the TLS header and TLS APP payload;
  587. // on output these fragments also contain the TLS header and TLS APP payload.
  588. //
  589. switch (CryptMode) {
  590. case EfiTlsEncrypt:
  591. Status = TlsEncryptPacket (Instance, FragmentTable, FragmentCount);
  592. break;
  593. case EfiTlsDecrypt:
  594. Status = TlsDecryptPacket (Instance, FragmentTable, FragmentCount);
  595. break;
  596. default:
  597. return EFI_INVALID_PARAMETER;
  598. }
  599. ON_EXIT:
  600. gBS->RestoreTPL (OldTpl);
  601. return Status;
  602. }