TlsImpl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /** @file
  2. Header file of Miscellaneous Routines for TlsDxe driver.
  3. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef __EFI_TLS_IMPL_H__
  7. #define __EFI_TLS_IMPL_H__
  8. //
  9. // Libraries
  10. //
  11. #include <Library/UefiBootServicesTableLib.h>
  12. #include <Library/MemoryAllocationLib.h>
  13. #include <Library/BaseMemoryLib.h>
  14. #include <Library/BaseLib.h>
  15. #include <Library/UefiLib.h>
  16. #include <Library/DebugLib.h>
  17. #include <Library/NetLib.h>
  18. #include <Library/BaseCryptLib.h>
  19. #include <Library/TlsLib.h>
  20. //
  21. // Consumed Protocols
  22. //
  23. #include <Protocol/Tls.h>
  24. #include <Protocol/TlsConfig.h>
  25. #include <IndustryStandard/Tls1.h>
  26. #include "TlsDriver.h"
  27. //
  28. // Protocol instances
  29. //
  30. extern EFI_SERVICE_BINDING_PROTOCOL mTlsServiceBinding;
  31. extern EFI_TLS_PROTOCOL mTlsProtocol;
  32. extern EFI_TLS_CONFIGURATION_PROTOCOL mTlsConfigurationProtocol;
  33. /**
  34. Encrypt the message listed in fragment.
  35. @param[in] TlsInstance The pointer to the TLS instance.
  36. @param[in, out] FragmentTable Pointer to a list of fragment.
  37. On input these fragments contain the TLS header and
  38. plain text TLS payload;
  39. On output these fragments contain the TLS header and
  40. cipher text TLS payload.
  41. @param[in] FragmentCount Number of fragment.
  42. @retval EFI_SUCCESS The operation completed successfully.
  43. @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
  44. @retval EFI_ABORTED TLS session state is incorrect.
  45. @retval Others Other errors as indicated.
  46. **/
  47. EFI_STATUS
  48. TlsEncryptPacket (
  49. IN TLS_INSTANCE *TlsInstance,
  50. IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable,
  51. IN UINT32 *FragmentCount
  52. );
  53. /**
  54. Decrypt the message listed in fragment.
  55. @param[in] TlsInstance The pointer to the TLS instance.
  56. @param[in, out] FragmentTable Pointer to a list of fragment.
  57. On input these fragments contain the TLS header and
  58. cipher text TLS payload;
  59. On output these fragments contain the TLS header and
  60. plain text TLS payload.
  61. @param[in] FragmentCount Number of fragment.
  62. @retval EFI_SUCCESS The operation completed successfully.
  63. @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
  64. @retval EFI_ABORTED TLS session state is incorrect.
  65. @retval Others Other errors as indicated.
  66. **/
  67. EFI_STATUS
  68. TlsDecryptPacket (
  69. IN TLS_INSTANCE *TlsInstance,
  70. IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable,
  71. IN UINT32 *FragmentCount
  72. );
  73. /**
  74. Set TLS session data.
  75. The SetSessionData() function set data for a new TLS session. All session data should
  76. be set before BuildResponsePacket() invoked.
  77. @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
  78. @param[in] DataType TLS session data type.
  79. @param[in] Data Pointer to session data.
  80. @param[in] DataSize Total size of session data.
  81. @retval EFI_SUCCESS The TLS session data is set successfully.
  82. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  83. This is NULL.
  84. Data is NULL.
  85. DataSize is 0.
  86. @retval EFI_UNSUPPORTED The DataType is unsupported.
  87. @retval EFI_ACCESS_DENIED If the DataType is one of below:
  88. EfiTlsClientRandom
  89. EfiTlsServerRandom
  90. EfiTlsKeyMaterial
  91. @retval EFI_NOT_READY Current TLS session state is NOT
  92. EfiTlsSessionStateNotStarted.
  93. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  94. **/
  95. EFI_STATUS
  96. EFIAPI
  97. TlsSetSessionData (
  98. IN EFI_TLS_PROTOCOL *This,
  99. IN EFI_TLS_SESSION_DATA_TYPE DataType,
  100. IN VOID *Data,
  101. IN UINTN DataSize
  102. );
  103. /**
  104. Get TLS session data.
  105. The GetSessionData() function return the TLS session information.
  106. @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
  107. @param[in] DataType TLS session data type.
  108. @param[in, out] Data Pointer to session data.
  109. @param[in, out] DataSize Total size of session data. On input, it means
  110. the size of Data buffer. On output, it means the size
  111. of copied Data buffer if EFI_SUCCESS, and means the
  112. size of desired Data buffer if EFI_BUFFER_TOO_SMALL.
  113. @retval EFI_SUCCESS The TLS session data is got successfully.
  114. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  115. This is NULL.
  116. DataSize is NULL.
  117. Data is NULL if *DataSize is not zero.
  118. @retval EFI_UNSUPPORTED The DataType is unsupported.
  119. @retval EFI_NOT_FOUND The TLS session data is not found.
  120. @retval EFI_NOT_READY The DataType is not ready in current session state.
  121. @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the data.
  122. **/
  123. EFI_STATUS
  124. EFIAPI
  125. TlsGetSessionData (
  126. IN EFI_TLS_PROTOCOL *This,
  127. IN EFI_TLS_SESSION_DATA_TYPE DataType,
  128. IN OUT VOID *Data OPTIONAL,
  129. IN OUT UINTN *DataSize
  130. );
  131. /**
  132. Build response packet according to TLS state machine. This function is only valid for
  133. alert, handshake and change_cipher_spec content type.
  134. The BuildResponsePacket() function builds TLS response packet in response to the TLS
  135. request packet specified by RequestBuffer and RequestSize. If RequestBuffer is NULL and
  136. RequestSize is 0, and TLS session status is EfiTlsSessionNotStarted, the TLS session
  137. will be initiated and the response packet needs to be ClientHello. If RequestBuffer is
  138. NULL and RequestSize is 0, and TLS session status is EfiTlsSessionClosing, the TLS
  139. session will be closed and response packet needs to be CloseNotify. If RequestBuffer is
  140. NULL and RequestSize is 0, and TLS session status is EfiTlsSessionError, the TLS
  141. session has errors and the response packet needs to be Alert message based on error
  142. type.
  143. @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
  144. @param[in] RequestBuffer Pointer to the most recently received TLS packet. NULL
  145. means TLS need initiate the TLS session and response
  146. packet need to be ClientHello.
  147. @param[in] RequestSize Packet size in bytes for the most recently received TLS
  148. packet. 0 is only valid when RequestBuffer is NULL.
  149. @param[out] Buffer Pointer to the buffer to hold the built packet.
  150. @param[in, out] BufferSize Pointer to the buffer size in bytes. On input, it is
  151. the buffer size provided by the caller. On output, it
  152. is the buffer size in fact needed to contain the
  153. packet.
  154. @retval EFI_SUCCESS The required TLS packet is built successfully.
  155. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  156. This is NULL.
  157. RequestBuffer is NULL but RequestSize is NOT 0.
  158. RequestSize is 0 but RequestBuffer is NOT NULL.
  159. BufferSize is NULL.
  160. Buffer is NULL if *BufferSize is not zero.
  161. @retval EFI_BUFFER_TOO_SMALL BufferSize is too small to hold the response packet.
  162. @retval EFI_NOT_READY Current TLS session state is NOT ready to build
  163. ResponsePacket.
  164. @retval EFI_ABORTED Something wrong build response packet.
  165. **/
  166. EFI_STATUS
  167. EFIAPI
  168. TlsBuildResponsePacket (
  169. IN EFI_TLS_PROTOCOL *This,
  170. IN UINT8 *RequestBuffer OPTIONAL,
  171. IN UINTN RequestSize OPTIONAL,
  172. OUT UINT8 *Buffer OPTIONAL,
  173. IN OUT UINTN *BufferSize
  174. );
  175. /**
  176. Decrypt or encrypt TLS packet during session. This function is only valid after
  177. session connected and for application_data content type.
  178. The ProcessPacket () function process each inbound or outbound TLS APP packet.
  179. @param[in] This Pointer to the EFI_TLS_PROTOCOL instance.
  180. @param[in, out] FragmentTable Pointer to a list of fragment. The caller will take
  181. responsible to handle the original FragmentTable while
  182. it may be reallocated in TLS driver. If CryptMode is
  183. EfiTlsEncrypt, on input these fragments contain the TLS
  184. header and plain text TLS APP payload; on output these
  185. fragments contain the TLS header and cipher text TLS
  186. APP payload. If CryptMode is EfiTlsDecrypt, on input
  187. these fragments contain the TLS header and cipher text
  188. TLS APP payload; on output these fragments contain the
  189. TLS header and plain text TLS APP payload.
  190. @param[in] FragmentCount Number of fragment.
  191. @param[in] CryptMode Crypt mode.
  192. @retval EFI_SUCCESS The operation completed successfully.
  193. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  194. This is NULL.
  195. FragmentTable is NULL.
  196. FragmentCount is NULL.
  197. CryptoMode is invalid.
  198. @retval EFI_NOT_READY Current TLS session state is NOT
  199. EfiTlsSessionDataTransferring.
  200. @retval EFI_ABORTED Something wrong decryption the message. TLS session
  201. status will become EfiTlsSessionError. The caller need
  202. call BuildResponsePacket() to generate Error Alert
  203. message and send it out.
  204. @retval EFI_OUT_OF_RESOURCES No enough resource to finish the operation.
  205. **/
  206. EFI_STATUS
  207. EFIAPI
  208. TlsProcessPacket (
  209. IN EFI_TLS_PROTOCOL *This,
  210. IN OUT EFI_TLS_FRAGMENT_DATA **FragmentTable,
  211. IN UINT32 *FragmentCount,
  212. IN EFI_TLS_CRYPT_MODE CryptMode
  213. );
  214. /**
  215. Set TLS configuration data.
  216. The SetData() function sets TLS configuration to non-volatile storage or volatile
  217. storage.
  218. @param[in] This Pointer to the EFI_TLS_CONFIGURATION_PROTOCOL instance.
  219. @param[in] DataType Configuration data type.
  220. @param[in] Data Pointer to configuration data.
  221. @param[in] DataSize Total size of configuration data.
  222. @retval EFI_SUCCESS The TLS configuration data is set successfully.
  223. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  224. This is NULL.
  225. Data is NULL.
  226. DataSize is 0.
  227. @retval EFI_UNSUPPORTED The DataType is unsupported.
  228. @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
  229. **/
  230. EFI_STATUS
  231. EFIAPI
  232. TlsConfigurationSetData (
  233. IN EFI_TLS_CONFIGURATION_PROTOCOL *This,
  234. IN EFI_TLS_CONFIG_DATA_TYPE DataType,
  235. IN VOID *Data,
  236. IN UINTN DataSize
  237. );
  238. /**
  239. Get TLS configuration data.
  240. The GetData() function gets TLS configuration.
  241. @param[in] This Pointer to the EFI_TLS_CONFIGURATION_PROTOCOL instance.
  242. @param[in] DataType Configuration data type.
  243. @param[in, out] Data Pointer to configuration data.
  244. @param[in, out] DataSize Total size of configuration data. On input, it means
  245. the size of Data buffer. On output, it means the size
  246. of copied Data buffer if EFI_SUCCESS, and means the
  247. size of desired Data buffer if EFI_BUFFER_TOO_SMALL.
  248. @retval EFI_SUCCESS The TLS configuration data is got successfully.
  249. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  250. This is NULL.
  251. DataSize is NULL.
  252. Data is NULL if *DataSize is not zero.
  253. @retval EFI_UNSUPPORTED The DataType is unsupported.
  254. @retval EFI_NOT_FOUND The TLS configuration data is not found.
  255. @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the data.
  256. **/
  257. EFI_STATUS
  258. EFIAPI
  259. TlsConfigurationGetData (
  260. IN EFI_TLS_CONFIGURATION_PROTOCOL *This,
  261. IN EFI_TLS_CONFIG_DATA_TYPE DataType,
  262. IN OUT VOID *Data OPTIONAL,
  263. IN OUT UINTN *DataSize
  264. );
  265. #endif