TlsProcess.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /** @file
  2. SSL/TLS Process Library Wrapper Implementation over OpenSSL.
  3. The process includes the TLS handshake and packet I/O.
  4. Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
  5. (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include "InternalTlsLib.h"
  9. #define MAX_BUFFER_SIZE 32768
  10. /**
  11. Checks if the TLS handshake was done.
  12. This function will check if the specified TLS handshake was done.
  13. @param[in] Tls Pointer to the TLS object for handshake state checking.
  14. @retval TRUE The TLS handshake was done.
  15. @retval FALSE The TLS handshake was not done.
  16. **/
  17. BOOLEAN
  18. EFIAPI
  19. TlsInHandshake (
  20. IN VOID *Tls
  21. )
  22. {
  23. TLS_CONNECTION *TlsConn;
  24. TlsConn = (TLS_CONNECTION *) Tls;
  25. if (TlsConn == NULL || TlsConn->Ssl == NULL) {
  26. return FALSE;
  27. }
  28. //
  29. // Return the status which indicates if the TLS handshake was done.
  30. //
  31. return !SSL_is_init_finished (TlsConn->Ssl);
  32. }
  33. /**
  34. Perform a TLS/SSL handshake.
  35. This function will perform a TLS/SSL handshake.
  36. @param[in] Tls Pointer to the TLS object for handshake operation.
  37. @param[in] BufferIn Pointer to the most recently received TLS Handshake packet.
  38. @param[in] BufferInSize Packet size in bytes for the most recently received TLS
  39. Handshake packet.
  40. @param[out] BufferOut Pointer to the buffer to hold the built packet.
  41. @param[in, out] BufferOutSize Pointer to the buffer size in bytes. On input, it is
  42. the buffer size provided by the caller. On output, it
  43. is the buffer size in fact needed to contain the
  44. packet.
  45. @retval EFI_SUCCESS The required TLS packet is built successfully.
  46. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  47. Tls is NULL.
  48. BufferIn is NULL but BufferInSize is NOT 0.
  49. BufferInSize is 0 but BufferIn is NOT NULL.
  50. BufferOutSize is NULL.
  51. BufferOut is NULL if *BufferOutSize is not zero.
  52. @retval EFI_BUFFER_TOO_SMALL BufferOutSize is too small to hold the response packet.
  53. @retval EFI_ABORTED Something wrong during handshake.
  54. **/
  55. EFI_STATUS
  56. EFIAPI
  57. TlsDoHandshake (
  58. IN VOID *Tls,
  59. IN UINT8 *BufferIn, OPTIONAL
  60. IN UINTN BufferInSize, OPTIONAL
  61. OUT UINT8 *BufferOut, OPTIONAL
  62. IN OUT UINTN *BufferOutSize
  63. )
  64. {
  65. TLS_CONNECTION *TlsConn;
  66. UINTN PendingBufferSize;
  67. INTN Ret;
  68. UINTN ErrorCode;
  69. TlsConn = (TLS_CONNECTION *) Tls;
  70. PendingBufferSize = 0;
  71. Ret = 1;
  72. if (TlsConn == NULL || \
  73. TlsConn->Ssl == NULL || TlsConn->InBio == NULL || TlsConn->OutBio == NULL || \
  74. BufferOutSize == NULL || \
  75. (BufferIn == NULL && BufferInSize != 0) || \
  76. (BufferIn != NULL && BufferInSize == 0) || \
  77. (BufferOut == NULL && *BufferOutSize != 0)) {
  78. return EFI_INVALID_PARAMETER;
  79. }
  80. if(BufferIn == NULL && BufferInSize == 0) {
  81. //
  82. // If RequestBuffer is NULL and RequestSize is 0, and TLS session
  83. // status is EfiTlsSessionNotStarted, the TLS session will be initiated
  84. // and the response packet needs to be ClientHello.
  85. //
  86. PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio);
  87. if (PendingBufferSize == 0) {
  88. SSL_set_connect_state (TlsConn->Ssl);
  89. Ret = SSL_do_handshake (TlsConn->Ssl);
  90. PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio);
  91. }
  92. } else {
  93. PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio);
  94. if (PendingBufferSize == 0) {
  95. BIO_write (TlsConn->InBio, BufferIn, (UINT32) BufferInSize);
  96. Ret = SSL_do_handshake (TlsConn->Ssl);
  97. PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio);
  98. }
  99. }
  100. if (Ret < 1) {
  101. Ret = SSL_get_error (TlsConn->Ssl, (int) Ret);
  102. if (Ret == SSL_ERROR_SSL ||
  103. Ret == SSL_ERROR_SYSCALL ||
  104. Ret == SSL_ERROR_ZERO_RETURN) {
  105. DEBUG ((
  106. DEBUG_ERROR,
  107. "%a SSL_HANDSHAKE_ERROR State=0x%x SSL_ERROR_%a\n",
  108. __FUNCTION__,
  109. SSL_get_state (TlsConn->Ssl),
  110. Ret == SSL_ERROR_SSL ? "SSL" : Ret == SSL_ERROR_SYSCALL ? "SYSCALL" : "ZERO_RETURN"
  111. ));
  112. DEBUG_CODE_BEGIN ();
  113. while (TRUE) {
  114. ErrorCode = ERR_get_error ();
  115. if (ErrorCode == 0) {
  116. break;
  117. }
  118. DEBUG ((
  119. DEBUG_ERROR,
  120. "%a ERROR 0x%x=L%x:F%x:R%x\n",
  121. __FUNCTION__,
  122. ErrorCode,
  123. ERR_GET_LIB (ErrorCode),
  124. ERR_GET_FUNC (ErrorCode),
  125. ERR_GET_REASON (ErrorCode)
  126. ));
  127. }
  128. DEBUG_CODE_END ();
  129. return EFI_ABORTED;
  130. }
  131. }
  132. if (PendingBufferSize > *BufferOutSize) {
  133. *BufferOutSize = PendingBufferSize;
  134. return EFI_BUFFER_TOO_SMALL;
  135. }
  136. if (PendingBufferSize > 0) {
  137. *BufferOutSize = BIO_read (TlsConn->OutBio, BufferOut, (UINT32) PendingBufferSize);
  138. } else {
  139. *BufferOutSize = 0;
  140. }
  141. return EFI_SUCCESS;
  142. }
  143. /**
  144. Handle Alert message recorded in BufferIn. If BufferIn is NULL and BufferInSize is zero,
  145. TLS session has errors and the response packet needs to be Alert message based on error type.
  146. @param[in] Tls Pointer to the TLS object for state checking.
  147. @param[in] BufferIn Pointer to the most recently received TLS Alert packet.
  148. @param[in] BufferInSize Packet size in bytes for the most recently received TLS
  149. Alert packet.
  150. @param[out] BufferOut Pointer to the buffer to hold the built packet.
  151. @param[in, out] BufferOutSize Pointer to the buffer size in bytes. On input, it is
  152. the buffer size provided by the caller. On output, it
  153. is the buffer size in fact needed to contain the
  154. packet.
  155. @retval EFI_SUCCESS The required TLS packet is built successfully.
  156. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  157. Tls is NULL.
  158. BufferIn is NULL but BufferInSize is NOT 0.
  159. BufferInSize is 0 but BufferIn is NOT NULL.
  160. BufferOutSize is NULL.
  161. BufferOut is NULL if *BufferOutSize is not zero.
  162. @retval EFI_ABORTED An error occurred.
  163. @retval EFI_BUFFER_TOO_SMALL BufferOutSize is too small to hold the response packet.
  164. **/
  165. EFI_STATUS
  166. EFIAPI
  167. TlsHandleAlert (
  168. IN VOID *Tls,
  169. IN UINT8 *BufferIn, OPTIONAL
  170. IN UINTN BufferInSize, OPTIONAL
  171. OUT UINT8 *BufferOut, OPTIONAL
  172. IN OUT UINTN *BufferOutSize
  173. )
  174. {
  175. TLS_CONNECTION *TlsConn;
  176. UINTN PendingBufferSize;
  177. UINT8 *TempBuffer;
  178. INTN Ret;
  179. TlsConn = (TLS_CONNECTION *) Tls;
  180. PendingBufferSize = 0;
  181. TempBuffer = NULL;
  182. Ret = 0;
  183. if (TlsConn == NULL || \
  184. TlsConn->Ssl == NULL || TlsConn->InBio == NULL || TlsConn->OutBio == NULL || \
  185. BufferOutSize == NULL || \
  186. (BufferIn == NULL && BufferInSize != 0) || \
  187. (BufferIn != NULL && BufferInSize == 0) || \
  188. (BufferOut == NULL && *BufferOutSize != 0)) {
  189. return EFI_INVALID_PARAMETER;
  190. }
  191. PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio);
  192. if (PendingBufferSize == 0 && BufferIn != NULL && BufferInSize != 0) {
  193. Ret = BIO_write (TlsConn->InBio, BufferIn, (UINT32) BufferInSize);
  194. if (Ret != (INTN) BufferInSize) {
  195. return EFI_ABORTED;
  196. }
  197. TempBuffer = (UINT8 *) OPENSSL_malloc (MAX_BUFFER_SIZE);
  198. //
  199. // ssl3_send_alert() will be called in ssl3_read_bytes() function.
  200. // TempBuffer is invalid since it's a Alert message, so just ignore it.
  201. //
  202. SSL_read (TlsConn->Ssl, TempBuffer, MAX_BUFFER_SIZE);
  203. OPENSSL_free (TempBuffer);
  204. PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio);
  205. }
  206. if (PendingBufferSize > *BufferOutSize) {
  207. *BufferOutSize = PendingBufferSize;
  208. return EFI_BUFFER_TOO_SMALL;
  209. }
  210. if (PendingBufferSize > 0) {
  211. *BufferOutSize = BIO_read (TlsConn->OutBio, BufferOut, (UINT32) PendingBufferSize);
  212. } else {
  213. *BufferOutSize = 0;
  214. }
  215. return EFI_SUCCESS;
  216. }
  217. /**
  218. Build the CloseNotify packet.
  219. @param[in] Tls Pointer to the TLS object for state checking.
  220. @param[in, out] Buffer Pointer to the buffer to hold the built packet.
  221. @param[in, out] BufferSize Pointer to the buffer size in bytes. On input, it is
  222. the buffer size provided by the caller. On output, it
  223. is the buffer size in fact needed to contain the
  224. packet.
  225. @retval EFI_SUCCESS The required TLS packet is built successfully.
  226. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
  227. Tls is NULL.
  228. BufferSize is NULL.
  229. Buffer is NULL if *BufferSize is not zero.
  230. @retval EFI_BUFFER_TOO_SMALL BufferSize is too small to hold the response packet.
  231. **/
  232. EFI_STATUS
  233. EFIAPI
  234. TlsCloseNotify (
  235. IN VOID *Tls,
  236. IN OUT UINT8 *Buffer,
  237. IN OUT UINTN *BufferSize
  238. )
  239. {
  240. TLS_CONNECTION *TlsConn;
  241. UINTN PendingBufferSize;
  242. TlsConn = (TLS_CONNECTION *) Tls;
  243. PendingBufferSize = 0;
  244. if (TlsConn == NULL || \
  245. TlsConn->Ssl == NULL || TlsConn->InBio == NULL || TlsConn->OutBio == NULL || \
  246. BufferSize == NULL || \
  247. (Buffer == NULL && *BufferSize != 0)) {
  248. return EFI_INVALID_PARAMETER;
  249. }
  250. PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio);
  251. if (PendingBufferSize == 0) {
  252. //
  253. // ssl3_send_alert() and ssl3_dispatch_alert() function will be called.
  254. //
  255. SSL_shutdown (TlsConn->Ssl);
  256. PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio);
  257. }
  258. if (PendingBufferSize > *BufferSize) {
  259. *BufferSize = PendingBufferSize;
  260. return EFI_BUFFER_TOO_SMALL;
  261. }
  262. if (PendingBufferSize > 0) {
  263. *BufferSize = BIO_read (TlsConn->OutBio, Buffer, (UINT32) PendingBufferSize);
  264. } else {
  265. *BufferSize = 0;
  266. }
  267. return EFI_SUCCESS;
  268. }
  269. /**
  270. Attempts to read bytes from one TLS object and places the data in Buffer.
  271. This function will attempt to read BufferSize bytes from the TLS object
  272. and places the data in Buffer.
  273. @param[in] Tls Pointer to the TLS object.
  274. @param[in,out] Buffer Pointer to the buffer to store the data.
  275. @param[in] BufferSize The size of Buffer in bytes.
  276. @retval >0 The amount of data successfully read from the TLS object.
  277. @retval <=0 No data was successfully read.
  278. **/
  279. INTN
  280. EFIAPI
  281. TlsCtrlTrafficOut (
  282. IN VOID *Tls,
  283. IN OUT VOID *Buffer,
  284. IN UINTN BufferSize
  285. )
  286. {
  287. TLS_CONNECTION *TlsConn;
  288. TlsConn = (TLS_CONNECTION *) Tls;
  289. if (TlsConn == NULL || TlsConn->OutBio == 0) {
  290. return -1;
  291. }
  292. //
  293. // Read and return the amount of data from the BIO.
  294. //
  295. return BIO_read (TlsConn->OutBio, Buffer, (UINT32) BufferSize);
  296. }
  297. /**
  298. Attempts to write data from the buffer to TLS object.
  299. This function will attempt to write BufferSize bytes data from the Buffer
  300. to the TLS object.
  301. @param[in] Tls Pointer to the TLS object.
  302. @param[in] Buffer Pointer to the data buffer.
  303. @param[in] BufferSize The size of Buffer in bytes.
  304. @retval >0 The amount of data successfully written to the TLS object.
  305. @retval <=0 No data was successfully written.
  306. **/
  307. INTN
  308. EFIAPI
  309. TlsCtrlTrafficIn (
  310. IN VOID *Tls,
  311. IN VOID *Buffer,
  312. IN UINTN BufferSize
  313. )
  314. {
  315. TLS_CONNECTION *TlsConn;
  316. TlsConn = (TLS_CONNECTION *) Tls;
  317. if (TlsConn == NULL || TlsConn->InBio == 0) {
  318. return -1;
  319. }
  320. //
  321. // Write and return the amount of data to the BIO.
  322. //
  323. return BIO_write (TlsConn->InBio, Buffer, (UINT32) BufferSize);
  324. }
  325. /**
  326. Attempts to read bytes from the specified TLS connection into the buffer.
  327. This function tries to read BufferSize bytes data from the specified TLS
  328. connection into the Buffer.
  329. @param[in] Tls Pointer to the TLS connection for data reading.
  330. @param[in,out] Buffer Pointer to the data buffer.
  331. @param[in] BufferSize The size of Buffer in bytes.
  332. @retval >0 The read operation was successful, and return value is the
  333. number of bytes actually read from the TLS connection.
  334. @retval <=0 The read operation was not successful.
  335. **/
  336. INTN
  337. EFIAPI
  338. TlsRead (
  339. IN VOID *Tls,
  340. IN OUT VOID *Buffer,
  341. IN UINTN BufferSize
  342. )
  343. {
  344. TLS_CONNECTION *TlsConn;
  345. TlsConn = (TLS_CONNECTION *) Tls;
  346. if (TlsConn == NULL || TlsConn->Ssl == NULL) {
  347. return -1;
  348. }
  349. //
  350. // Read bytes from the specified TLS connection.
  351. //
  352. return SSL_read (TlsConn->Ssl, Buffer, (UINT32) BufferSize);
  353. }
  354. /**
  355. Attempts to write data to a TLS connection.
  356. This function tries to write BufferSize bytes data from the Buffer into the
  357. specified TLS connection.
  358. @param[in] Tls Pointer to the TLS connection for data writing.
  359. @param[in] Buffer Pointer to the data buffer.
  360. @param[in] BufferSize The size of Buffer in bytes.
  361. @retval >0 The write operation was successful, and return value is the
  362. number of bytes actually written to the TLS connection.
  363. @retval <=0 The write operation was not successful.
  364. **/
  365. INTN
  366. EFIAPI
  367. TlsWrite (
  368. IN VOID *Tls,
  369. IN VOID *Buffer,
  370. IN UINTN BufferSize
  371. )
  372. {
  373. TLS_CONNECTION *TlsConn;
  374. TlsConn = (TLS_CONNECTION *) Tls;
  375. if (TlsConn == NULL || TlsConn->Ssl == NULL) {
  376. return -1;
  377. }
  378. //
  379. // Write bytes to the specified TLS connection.
  380. //
  381. return SSL_write (TlsConn->Ssl, Buffer, (UINT32) BufferSize);
  382. }