13#define MAX_BUFFER_SIZE 32768
35 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL)) {
42 return !SSL_is_init_finished (TlsConn->Ssl);
75 IN UINT8 *BufferIn OPTIONAL,
77 OUT UINT8 *BufferOut OPTIONAL,
82 UINTN PendingBufferSize;
86 PendingBufferSize = 0;
89 if ((TlsConn ==
NULL) || \
90 (TlsConn->Ssl ==
NULL) || (TlsConn->InBio ==
NULL) || (TlsConn->OutBio ==
NULL) || \
91 (BufferOutSize ==
NULL) || \
92 ((BufferIn ==
NULL) && (BufferInSize != 0)) || \
93 ((BufferIn !=
NULL) && (BufferInSize == 0)) || \
94 ((BufferOut ==
NULL) && (*BufferOutSize != 0)))
96 return EFI_INVALID_PARAMETER;
99 if ((BufferIn ==
NULL) && (BufferInSize == 0)) {
105 PendingBufferSize = (
UINTN)BIO_ctrl_pending (TlsConn->OutBio);
106 if (PendingBufferSize == 0) {
107 SSL_set_connect_state (TlsConn->Ssl);
108 Ret = SSL_do_handshake (TlsConn->Ssl);
109 PendingBufferSize = (
UINTN)BIO_ctrl_pending (TlsConn->OutBio);
112 PendingBufferSize = (
UINTN)BIO_ctrl_pending (TlsConn->OutBio);
113 if (PendingBufferSize == 0) {
114 BIO_write (TlsConn->InBio, BufferIn, (UINT32)BufferInSize);
115 Ret = SSL_do_handshake (TlsConn->Ssl);
116 PendingBufferSize = (
UINTN)BIO_ctrl_pending (TlsConn->OutBio);
121 Ret = SSL_get_error (TlsConn->Ssl, (
int)Ret);
122 if ((Ret == SSL_ERROR_SSL) ||
123 (Ret == SSL_ERROR_SYSCALL) ||
124 (Ret == SSL_ERROR_ZERO_RETURN))
128 "%a SSL_HANDSHAKE_ERROR State=0x%x SSL_ERROR_%a\n",
130 SSL_get_state (TlsConn->Ssl),
131 Ret == SSL_ERROR_SSL ?
"SSL" : Ret == SSL_ERROR_SYSCALL ?
"SYSCALL" :
"ZERO_RETURN"
135 unsigned long ErrorCode;
139 ErrorCode = ERR_get_error_all (
NULL,
NULL, &Func, &Data,
NULL);
140 if (ErrorCode == 0) {
146 "%a ERROR 0x%x=L%x:R%x %a(): %a\n",
149 ERR_GET_LIB (ErrorCode),
150 ERR_GET_REASON (ErrorCode),
161 if (PendingBufferSize > *BufferOutSize) {
162 *BufferOutSize = PendingBufferSize;
163 return EFI_BUFFER_TOO_SMALL;
166 if (PendingBufferSize > 0) {
167 *BufferOutSize = BIO_read (TlsConn->OutBio, BufferOut, (UINT32)PendingBufferSize);
204 IN UINT8 *BufferIn OPTIONAL,
205 IN UINTN BufferInSize OPTIONAL,
206 OUT UINT8 *BufferOut OPTIONAL,
211 UINTN PendingBufferSize;
216 PendingBufferSize = 0;
220 if ((TlsConn ==
NULL) || \
221 (TlsConn->Ssl ==
NULL) || (TlsConn->InBio ==
NULL) || (TlsConn->OutBio ==
NULL) || \
222 (BufferOutSize ==
NULL) || \
223 ((BufferIn ==
NULL) && (BufferInSize != 0)) || \
224 ((BufferIn !=
NULL) && (BufferInSize == 0)) || \
225 ((BufferOut ==
NULL) && (*BufferOutSize != 0)))
227 return EFI_INVALID_PARAMETER;
230 PendingBufferSize = (
UINTN)BIO_ctrl_pending (TlsConn->OutBio);
231 if ((PendingBufferSize == 0) && (BufferIn !=
NULL) && (BufferInSize != 0)) {
232 Ret = BIO_write (TlsConn->InBio, BufferIn, (UINT32)BufferInSize);
233 if (Ret != (
INTN)BufferInSize) {
237 TempBuffer = (UINT8 *)OPENSSL_malloc (MAX_BUFFER_SIZE);
243 SSL_read (TlsConn->Ssl, TempBuffer, MAX_BUFFER_SIZE);
245 OPENSSL_free (TempBuffer);
247 PendingBufferSize = (
UINTN)BIO_ctrl_pending (TlsConn->OutBio);
250 if (PendingBufferSize > *BufferOutSize) {
251 *BufferOutSize = PendingBufferSize;
252 return EFI_BUFFER_TOO_SMALL;
255 if (PendingBufferSize > 0) {
256 *BufferOutSize = BIO_read (TlsConn->OutBio, BufferOut, (UINT32)PendingBufferSize);
286 IN OUT UINT8 *Buffer,
291 UINTN PendingBufferSize;
294 PendingBufferSize = 0;
296 if ((TlsConn ==
NULL) || \
297 (TlsConn->Ssl ==
NULL) || (TlsConn->InBio ==
NULL) || (TlsConn->OutBio ==
NULL) || \
298 (BufferSize ==
NULL) || \
299 ((Buffer ==
NULL) && (*BufferSize != 0)))
301 return EFI_INVALID_PARAMETER;
304 PendingBufferSize = (
UINTN)BIO_ctrl_pending (TlsConn->OutBio);
305 if (PendingBufferSize == 0) {
309 SSL_shutdown (TlsConn->Ssl);
310 PendingBufferSize = (
UINTN)BIO_ctrl_pending (TlsConn->OutBio);
313 if (PendingBufferSize > *BufferSize) {
314 *BufferSize = PendingBufferSize;
315 return EFI_BUFFER_TOO_SMALL;
318 if (PendingBufferSize > 0) {
319 *BufferSize = BIO_read (TlsConn->OutBio, Buffer, (UINT32)PendingBufferSize);
352 if ((TlsConn ==
NULL) || (TlsConn->OutBio == 0)) {
359 return BIO_read (TlsConn->OutBio, Buffer, (UINT32)BufferSize);
387 if ((TlsConn ==
NULL) || (TlsConn->InBio == 0)) {
394 return BIO_write (TlsConn->InBio, Buffer, (UINT32)BufferSize);
423 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL)) {
430 return SSL_read (TlsConn->Ssl, Buffer, (UINT32)BufferSize);
459 if ((TlsConn ==
NULL) || (TlsConn->Ssl ==
NULL)) {
466 return SSL_write (TlsConn->Ssl, Buffer, (UINT32)BufferSize);
492 if ((TlsConn ==
NULL) || ((TlsConn->Ssl) ==
NULL)) {
493 return EFI_INVALID_PARAMETER;
496 SSL_set_quiet_shutdown (TlsConn->Ssl, 1);
497 SSL_shutdown (TlsConn->Ssl);
498 return SSL_clear (TlsConn->Ssl) == 1 ?
EFI_SUCCESS : EFI_PROTOCOL_ERROR;
#define DEBUG_CODE_BEGIN()
#define DEBUG(Expression)
EFI_STATUS EFIAPI TlsDoHandshake(IN VOID *Tls, IN UINT8 *BufferIn OPTIONAL, IN UINTN BufferInSize OPTIONAL, OUT UINT8 *BufferOut OPTIONAL, IN OUT UINTN *BufferOutSize)
INTN EFIAPI TlsCtrlTrafficIn(IN VOID *Tls, IN VOID *Buffer, IN UINTN BufferSize)
INTN EFIAPI TlsCtrlTrafficOut(IN VOID *Tls, IN OUT VOID *Buffer, IN UINTN BufferSize)
INTN EFIAPI TlsRead(IN VOID *Tls, IN OUT VOID *Buffer, IN UINTN BufferSize)
EFI_STATUS EFIAPI TlsShutdown(IN VOID *Tls)
BOOLEAN EFIAPI TlsInHandshake(IN VOID *Tls)
EFI_STATUS EFIAPI TlsHandleAlert(IN VOID *Tls, IN UINT8 *BufferIn OPTIONAL, IN UINTN BufferInSize OPTIONAL, OUT UINT8 *BufferOut OPTIONAL, IN OUT UINTN *BufferOutSize)
EFI_STATUS EFIAPI TlsCloseNotify(IN VOID *Tls, IN OUT UINT8 *Buffer, IN OUT UINTN *BufferSize)
INTN EFIAPI TlsWrite(IN VOID *Tls, IN VOID *Buffer, IN UINTN BufferSize)