]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/TlsDxe/TlsImpl.c
NetworkPkg: Convert files to CRLF line ending
[mirror_edk2.git] / NetworkPkg / TlsDxe / TlsImpl.c
index efdec2d92dd56816b17eb0658f0b457f7f065998..8e1238216b4d31096e40fc6766adc10551a781c5 100644 (file)
-/** @file
-  The Miscellaneous Routines for TlsDxe driver.
-
-Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
-
-This program and the accompanying materials
-are licensed and made available under the terms and conditions of the BSD License
-which accompanies this distribution.  The full text of the license may be found at
-http://opensource.org/licenses/bsd-license.php
-
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-
-**/
-
-#include "TlsImpl.h"
-
-/**
-  Encrypt the message listed in fragment.
-
-  @param[in]       TlsInstance    The pointer to the TLS instance.
-  @param[in, out]  FragmentTable  Pointer to a list of fragment.
-                                  On input these fragments contain the TLS header and
-                                  plain text TLS payload;
-                                  On output these fragments contain the TLS header and
-                                  cipher text TLS payload.
-  @param[in]       FragmentCount  Number of fragment.
-
-  @retval EFI_SUCCESS             The operation completed successfully.
-  @retval EFI_OUT_OF_RESOURCES    Can't allocate memory resources.
-  @retval EFI_ABORTED             TLS session state is incorrect.
-  @retval Others                  Other errors as indicated.
-**/
-EFI_STATUS
-TlsEncryptPacket (
-  IN     TLS_INSTANCE                  *TlsInstance,
-  IN OUT EFI_TLS_FRAGMENT_DATA         **FragmentTable,
-  IN     UINT32                        *FragmentCount
-  )
-{
-  EFI_STATUS          Status;
-  UINTN               Index;
-  UINT32              BytesCopied;
-  UINT32              BufferInSize;
-  UINT8               *BufferIn;
-  UINT8               *BufferInPtr;
-  TLS_RECORD_HEADER   *RecordHeaderIn;
-  UINT16              ThisPlainMessageSize;
-  TLS_RECORD_HEADER   *TempRecordHeader;
-  UINT16              ThisMessageSize;
-  UINT32              BufferOutSize;
-  UINT8               *BufferOut;
-  INTN                Ret;
-  
-  Status           = EFI_SUCCESS;
-  BytesCopied      = 0;
-  BufferInSize     = 0;
-  BufferIn         = NULL;
-  BufferInPtr      = NULL;
-  RecordHeaderIn   = NULL;
-  TempRecordHeader = NULL;
-  BufferOutSize    = 0;
-  BufferOut        = NULL;
-  Ret              = 0;
-
-  //
-  // Calculate the size according to the fragment table.
-  //
-  for (Index = 0; Index < *FragmentCount; Index++) {
-    BufferInSize += (*FragmentTable)[Index].FragmentLength;
-  }
-
-  //
-  // Allocate buffer for processing data.
-  //
-  BufferIn = AllocateZeroPool (BufferInSize);
-  if (BufferIn == NULL) {
-    Status = EFI_OUT_OF_RESOURCES;
-    goto ERROR;
-  }
-
-  //
-  // Copy all TLS plain record header and payload into BufferIn.
-  //
-  for (Index = 0; Index < *FragmentCount; Index++) {
-    CopyMem (
-      (BufferIn + BytesCopied),
-      (*FragmentTable)[Index].FragmentBuffer,
-      (*FragmentTable)[Index].FragmentLength
-      );
-    BytesCopied += (*FragmentTable)[Index].FragmentLength;
-  }
-
-  BufferOut = AllocateZeroPool (MAX_BUFFER_SIZE);
-  if (BufferOut == NULL) {
-    Status = EFI_OUT_OF_RESOURCES;
-    goto ERROR;
-  }
-
-  //
-  // Parsing buffer.
-  //
-  BufferInPtr = BufferIn;
-  TempRecordHeader = (TLS_RECORD_HEADER *) BufferOut;
-  while ((UINTN) BufferInPtr < (UINTN) BufferIn + BufferInSize) {
-    RecordHeaderIn = (TLS_RECORD_HEADER *) BufferInPtr;
-    
-    if (RecordHeaderIn->ContentType != TlsContentTypeApplicationData) {
-      Status = EFI_INVALID_PARAMETER;
-      goto ERROR;
-    }
-    
-    ThisPlainMessageSize = RecordHeaderIn->Length;
-
-    TlsWrite (TlsInstance->TlsConn, (UINT8 *) (RecordHeaderIn + 1), ThisPlainMessageSize);
-    
-    Ret = TlsCtrlTrafficOut (TlsInstance->TlsConn, (UINT8 *)(TempRecordHeader), MAX_BUFFER_SIZE - BufferOutSize);
-
-    if (Ret > 0) {
-      ThisMessageSize = (UINT16) Ret;
-    } else {
-      //
-      // No data was successfully encrypted, continue to encrypt other messages.
-      //
-      DEBUG ((EFI_D_WARN, "TlsEncryptPacket: No data read from TLS object.\n"));
-    
-      ThisMessageSize = 0;
-    }
-
-    BufferOutSize += ThisMessageSize;
-
-    BufferInPtr += RECORD_HEADER_LEN + ThisPlainMessageSize;
-    TempRecordHeader += ThisMessageSize;
-  }
-
-  FreePool (BufferIn);
-  BufferIn = NULL;
-
-  //
-  // The caller will be responsible to handle the original fragment table.
-  //
-  *FragmentTable = AllocateZeroPool (sizeof (EFI_TLS_FRAGMENT_DATA));
-  if (*FragmentTable == NULL) {
-    Status = EFI_OUT_OF_RESOURCES;
-    goto ERROR;
-  }
-
-  (*FragmentTable)[0].FragmentBuffer  = BufferOut;
-  (*FragmentTable)[0].FragmentLength  = BufferOutSize;
-  *FragmentCount                      = 1;
-
-  return Status;
-
-ERROR:
-  
-  if (BufferIn != NULL) {
-    FreePool (BufferIn);
-    BufferIn = NULL;
-  }
-
-  if (BufferOut != NULL) {
-    FreePool (BufferOut);
-    BufferOut = NULL;
-  }
-  
-  return Status;
-}
-
-/**
-  Decrypt the message listed in fragment.
-
-  @param[in]       TlsInstance    The pointer to the TLS instance.
-  @param[in, out]  FragmentTable  Pointer to a list of fragment.
-                                  On input these fragments contain the TLS header and
-                                  cipher text TLS payload;
-                                  On output these fragments contain the TLS header and
-                                  plain text TLS payload.
-  @param[in]       FragmentCount  Number of fragment.
-
-  @retval EFI_SUCCESS             The operation completed successfully.
-  @retval EFI_OUT_OF_RESOURCES    Can't allocate memory resources.
-  @retval EFI_ABORTED             TLS session state is incorrect.
-  @retval Others                  Other errors as indicated.
-**/
-EFI_STATUS
-TlsDecryptPacket (
-  IN     TLS_INSTANCE                  *TlsInstance,
-  IN OUT EFI_TLS_FRAGMENT_DATA         **FragmentTable,
-  IN     UINT32                        *FragmentCount
-  )
-{
-  EFI_STATUS          Status;
-  UINTN               Index;
-  UINT32              BytesCopied;
-  UINT8               *BufferIn;
-  UINT32              BufferInSize;
-  UINT8               *BufferInPtr;
-  TLS_RECORD_HEADER   *RecordHeaderIn;
-  UINT16              ThisCipherMessageSize;
-  TLS_RECORD_HEADER   *TempRecordHeader;
-  UINT16              ThisPlainMessageSize;
-  UINT8               *BufferOut;
-  UINT32              BufferOutSize;
-  INTN                Ret;
-
-  Status           = EFI_SUCCESS;
-  BytesCopied      = 0;
-  BufferIn         = NULL;
-  BufferInSize     = 0;
-  BufferInPtr      = NULL;
-  RecordHeaderIn   = NULL;
-  TempRecordHeader = NULL;
-  BufferOut        = NULL;
-  BufferOutSize    = 0;
-  Ret              = 0;
-
-  //
-  // Calculate the size according to the fragment table.
-  //
-  for (Index = 0; Index < *FragmentCount; Index++) {
-    BufferInSize += (*FragmentTable)[Index].FragmentLength;
-  }
-
-  //
-  // Allocate buffer for processing data
-  //
-  BufferIn = AllocateZeroPool (BufferInSize);
-  if (BufferIn == NULL) {
-    Status = EFI_OUT_OF_RESOURCES;
-    goto ERROR;
-  }
-
-  //
-  // Copy all TLS plain record header and payload to BufferIn
-  //
-  for (Index = 0; Index < *FragmentCount; Index++) {
-    CopyMem (
-      (BufferIn + BytesCopied),
-      (*FragmentTable)[Index].FragmentBuffer,
-      (*FragmentTable)[Index].FragmentLength
-      );
-    BytesCopied += (*FragmentTable)[Index].FragmentLength;
-  }
-
-  BufferOut = AllocateZeroPool (MAX_BUFFER_SIZE);
-  if (BufferOut == NULL) {
-    Status = EFI_OUT_OF_RESOURCES;
-    goto ERROR;
-  }
-
-  //
-  // Parsing buffer. Received packet may have multiple TLS record messages.
-  //
-  BufferInPtr = BufferIn;
-  TempRecordHeader = (TLS_RECORD_HEADER *) BufferOut;
-  while ((UINTN) BufferInPtr < (UINTN) BufferIn + BufferInSize) {
-    RecordHeaderIn = (TLS_RECORD_HEADER *) BufferInPtr;
-
-    if (RecordHeaderIn->ContentType != TlsContentTypeApplicationData) {
-      Status = EFI_INVALID_PARAMETER;
-      goto ERROR;
-    }
-    
-    ThisCipherMessageSize = NTOHS (RecordHeaderIn->Length);
-
-    Ret = TlsCtrlTrafficIn (TlsInstance->TlsConn, (UINT8 *) (RecordHeaderIn), RECORD_HEADER_LEN + ThisCipherMessageSize);
-    if (Ret != RECORD_HEADER_LEN + ThisCipherMessageSize) {
-      TlsInstance->TlsSessionState = EfiTlsSessionError;
-      Status = EFI_ABORTED;
-      goto ERROR;
-    }
-
-    Ret = 0;
-    Ret = TlsRead (TlsInstance->TlsConn, (UINT8 *) (TempRecordHeader + 1), MAX_BUFFER_SIZE - BufferOutSize);
-
-    if (Ret > 0) {
-      ThisPlainMessageSize = (UINT16) Ret;
-    } else {
-      //
-      // No data was successfully decrypted, continue to decrypt other messages.
-      //
-      DEBUG ((EFI_D_WARN, "TlsDecryptPacket: No data read from TLS object.\n"));
-    
-      ThisPlainMessageSize = 0;
-    }
-
-    CopyMem (TempRecordHeader, RecordHeaderIn, RECORD_HEADER_LEN);
-    TempRecordHeader->Length = ThisPlainMessageSize;
-    BufferOutSize += RECORD_HEADER_LEN + ThisPlainMessageSize;
-
-    BufferInPtr += RECORD_HEADER_LEN + ThisCipherMessageSize;
-    TempRecordHeader += RECORD_HEADER_LEN + ThisPlainMessageSize;
-  }
-
-  FreePool (BufferIn);
-  BufferIn = NULL;
-
-  //
-  // The caller will be responsible to handle the original fragment table
-  //
-  *FragmentTable = AllocateZeroPool (sizeof (EFI_TLS_FRAGMENT_DATA));
-  if (*FragmentTable == NULL) {
-    Status = EFI_OUT_OF_RESOURCES;
-    goto ERROR;
-  }
-
-  (*FragmentTable)[0].FragmentBuffer  = BufferOut;
-  (*FragmentTable)[0].FragmentLength  = BufferOutSize;
-  *FragmentCount                      = 1;
-
-  return Status;
-
-ERROR:
-  
-  if (BufferIn != NULL) {
-    FreePool (BufferIn);
-    BufferIn = NULL;
-  }
-
-  if (BufferOut != NULL) {
-    FreePool (BufferOut);
-    BufferOut = NULL;
-  }
-  
-  return Status;  
-}
+/** @file\r
+  The Miscellaneous Routines for TlsDxe driver.\r
+\r
+Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
+\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "TlsImpl.h"\r
+\r
+/**\r
+  Encrypt the message listed in fragment.\r
+\r
+  @param[in]       TlsInstance    The pointer to the TLS instance.\r
+  @param[in, out]  FragmentTable  Pointer to a list of fragment.\r
+                                  On input these fragments contain the TLS header and\r
+                                  plain text TLS payload;\r
+                                  On output these fragments contain the TLS header and\r
+                                  cipher text TLS payload.\r
+  @param[in]       FragmentCount  Number of fragment.\r
+\r
+  @retval EFI_SUCCESS             The operation completed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES    Can't allocate memory resources.\r
+  @retval EFI_ABORTED             TLS session state is incorrect.\r
+  @retval Others                  Other errors as indicated.\r
+**/\r
+EFI_STATUS\r
+TlsEncryptPacket (\r
+  IN     TLS_INSTANCE                  *TlsInstance,\r
+  IN OUT EFI_TLS_FRAGMENT_DATA         **FragmentTable,\r
+  IN     UINT32                        *FragmentCount\r
+  )\r
+{\r
+  EFI_STATUS          Status;\r
+  UINTN               Index;\r
+  UINT32              BytesCopied;\r
+  UINT32              BufferInSize;\r
+  UINT8               *BufferIn;\r
+  UINT8               *BufferInPtr;\r
+  TLS_RECORD_HEADER   *RecordHeaderIn;\r
+  UINT16              ThisPlainMessageSize;\r
+  TLS_RECORD_HEADER   *TempRecordHeader;\r
+  UINT16              ThisMessageSize;\r
+  UINT32              BufferOutSize;\r
+  UINT8               *BufferOut;\r
+  INTN                Ret;\r
+\r
+  Status           = EFI_SUCCESS;\r
+  BytesCopied      = 0;\r
+  BufferInSize     = 0;\r
+  BufferIn         = NULL;\r
+  BufferInPtr      = NULL;\r
+  RecordHeaderIn   = NULL;\r
+  TempRecordHeader = NULL;\r
+  BufferOutSize    = 0;\r
+  BufferOut        = NULL;\r
+  Ret              = 0;\r
+\r
+  //\r
+  // Calculate the size according to the fragment table.\r
+  //\r
+  for (Index = 0; Index < *FragmentCount; Index++) {\r
+    BufferInSize += (*FragmentTable)[Index].FragmentLength;\r
+  }\r
+\r
+  //\r
+  // Allocate buffer for processing data.\r
+  //\r
+  BufferIn = AllocateZeroPool (BufferInSize);\r
+  if (BufferIn == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ERROR;\r
+  }\r
+\r
+  //\r
+  // Copy all TLS plain record header and payload into BufferIn.\r
+  //\r
+  for (Index = 0; Index < *FragmentCount; Index++) {\r
+    CopyMem (\r
+      (BufferIn + BytesCopied),\r
+      (*FragmentTable)[Index].FragmentBuffer,\r
+      (*FragmentTable)[Index].FragmentLength\r
+      );\r
+    BytesCopied += (*FragmentTable)[Index].FragmentLength;\r
+  }\r
+\r
+  BufferOut = AllocateZeroPool (MAX_BUFFER_SIZE);\r
+  if (BufferOut == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ERROR;\r
+  }\r
+\r
+  //\r
+  // Parsing buffer.\r
+  //\r
+  BufferInPtr = BufferIn;\r
+  TempRecordHeader = (TLS_RECORD_HEADER *) BufferOut;\r
+  while ((UINTN) BufferInPtr < (UINTN) BufferIn + BufferInSize) {\r
+    RecordHeaderIn = (TLS_RECORD_HEADER *) BufferInPtr;\r
+\r
+    if (RecordHeaderIn->ContentType != TlsContentTypeApplicationData) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto ERROR;\r
+    }\r
+\r
+    ThisPlainMessageSize = RecordHeaderIn->Length;\r
+\r
+    TlsWrite (TlsInstance->TlsConn, (UINT8 *) (RecordHeaderIn + 1), ThisPlainMessageSize);\r
+\r
+    Ret = TlsCtrlTrafficOut (TlsInstance->TlsConn, (UINT8 *)(TempRecordHeader), MAX_BUFFER_SIZE - BufferOutSize);\r
+\r
+    if (Ret > 0) {\r
+      ThisMessageSize = (UINT16) Ret;\r
+    } else {\r
+      //\r
+      // No data was successfully encrypted, continue to encrypt other messages.\r
+      //\r
+      DEBUG ((EFI_D_WARN, "TlsEncryptPacket: No data read from TLS object.\n"));\r
+\r
+      ThisMessageSize = 0;\r
+    }\r
+\r
+    BufferOutSize += ThisMessageSize;\r
+\r
+    BufferInPtr += RECORD_HEADER_LEN + ThisPlainMessageSize;\r
+    TempRecordHeader += ThisMessageSize;\r
+  }\r
+\r
+  FreePool (BufferIn);\r
+  BufferIn = NULL;\r
+\r
+  //\r
+  // The caller will be responsible to handle the original fragment table.\r
+  //\r
+  *FragmentTable = AllocateZeroPool (sizeof (EFI_TLS_FRAGMENT_DATA));\r
+  if (*FragmentTable == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ERROR;\r
+  }\r
+\r
+  (*FragmentTable)[0].FragmentBuffer  = BufferOut;\r
+  (*FragmentTable)[0].FragmentLength  = BufferOutSize;\r
+  *FragmentCount                      = 1;\r
+\r
+  return Status;\r
+\r
+ERROR:\r
+\r
+  if (BufferIn != NULL) {\r
+    FreePool (BufferIn);\r
+    BufferIn = NULL;\r
+  }\r
+\r
+  if (BufferOut != NULL) {\r
+    FreePool (BufferOut);\r
+    BufferOut = NULL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Decrypt the message listed in fragment.\r
+\r
+  @param[in]       TlsInstance    The pointer to the TLS instance.\r
+  @param[in, out]  FragmentTable  Pointer to a list of fragment.\r
+                                  On input these fragments contain the TLS header and\r
+                                  cipher text TLS payload;\r
+                                  On output these fragments contain the TLS header and\r
+                                  plain text TLS payload.\r
+  @param[in]       FragmentCount  Number of fragment.\r
+\r
+  @retval EFI_SUCCESS             The operation completed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES    Can't allocate memory resources.\r
+  @retval EFI_ABORTED             TLS session state is incorrect.\r
+  @retval Others                  Other errors as indicated.\r
+**/\r
+EFI_STATUS\r
+TlsDecryptPacket (\r
+  IN     TLS_INSTANCE                  *TlsInstance,\r
+  IN OUT EFI_TLS_FRAGMENT_DATA         **FragmentTable,\r
+  IN     UINT32                        *FragmentCount\r
+  )\r
+{\r
+  EFI_STATUS          Status;\r
+  UINTN               Index;\r
+  UINT32              BytesCopied;\r
+  UINT8               *BufferIn;\r
+  UINT32              BufferInSize;\r
+  UINT8               *BufferInPtr;\r
+  TLS_RECORD_HEADER   *RecordHeaderIn;\r
+  UINT16              ThisCipherMessageSize;\r
+  TLS_RECORD_HEADER   *TempRecordHeader;\r
+  UINT16              ThisPlainMessageSize;\r
+  UINT8               *BufferOut;\r
+  UINT32              BufferOutSize;\r
+  INTN                Ret;\r
+\r
+  Status           = EFI_SUCCESS;\r
+  BytesCopied      = 0;\r
+  BufferIn         = NULL;\r
+  BufferInSize     = 0;\r
+  BufferInPtr      = NULL;\r
+  RecordHeaderIn   = NULL;\r
+  TempRecordHeader = NULL;\r
+  BufferOut        = NULL;\r
+  BufferOutSize    = 0;\r
+  Ret              = 0;\r
+\r
+  //\r
+  // Calculate the size according to the fragment table.\r
+  //\r
+  for (Index = 0; Index < *FragmentCount; Index++) {\r
+    BufferInSize += (*FragmentTable)[Index].FragmentLength;\r
+  }\r
+\r
+  //\r
+  // Allocate buffer for processing data\r
+  //\r
+  BufferIn = AllocateZeroPool (BufferInSize);\r
+  if (BufferIn == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ERROR;\r
+  }\r
+\r
+  //\r
+  // Copy all TLS plain record header and payload to BufferIn\r
+  //\r
+  for (Index = 0; Index < *FragmentCount; Index++) {\r
+    CopyMem (\r
+      (BufferIn + BytesCopied),\r
+      (*FragmentTable)[Index].FragmentBuffer,\r
+      (*FragmentTable)[Index].FragmentLength\r
+      );\r
+    BytesCopied += (*FragmentTable)[Index].FragmentLength;\r
+  }\r
+\r
+  BufferOut = AllocateZeroPool (MAX_BUFFER_SIZE);\r
+  if (BufferOut == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ERROR;\r
+  }\r
+\r
+  //\r
+  // Parsing buffer. Received packet may have multiple TLS record messages.\r
+  //\r
+  BufferInPtr = BufferIn;\r
+  TempRecordHeader = (TLS_RECORD_HEADER *) BufferOut;\r
+  while ((UINTN) BufferInPtr < (UINTN) BufferIn + BufferInSize) {\r
+    RecordHeaderIn = (TLS_RECORD_HEADER *) BufferInPtr;\r
+\r
+    if (RecordHeaderIn->ContentType != TlsContentTypeApplicationData) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto ERROR;\r
+    }\r
+\r
+    ThisCipherMessageSize = NTOHS (RecordHeaderIn->Length);\r
+\r
+    Ret = TlsCtrlTrafficIn (TlsInstance->TlsConn, (UINT8 *) (RecordHeaderIn), RECORD_HEADER_LEN + ThisCipherMessageSize);\r
+    if (Ret != RECORD_HEADER_LEN + ThisCipherMessageSize) {\r
+      TlsInstance->TlsSessionState = EfiTlsSessionError;\r
+      Status = EFI_ABORTED;\r
+      goto ERROR;\r
+    }\r
+\r
+    Ret = 0;\r
+    Ret = TlsRead (TlsInstance->TlsConn, (UINT8 *) (TempRecordHeader + 1), MAX_BUFFER_SIZE - BufferOutSize);\r
+\r
+    if (Ret > 0) {\r
+      ThisPlainMessageSize = (UINT16) Ret;\r
+    } else {\r
+      //\r
+      // No data was successfully decrypted, continue to decrypt other messages.\r
+      //\r
+      DEBUG ((EFI_D_WARN, "TlsDecryptPacket: No data read from TLS object.\n"));\r
+\r
+      ThisPlainMessageSize = 0;\r
+    }\r
+\r
+    CopyMem (TempRecordHeader, RecordHeaderIn, RECORD_HEADER_LEN);\r
+    TempRecordHeader->Length = ThisPlainMessageSize;\r
+    BufferOutSize += RECORD_HEADER_LEN + ThisPlainMessageSize;\r
+\r
+    BufferInPtr += RECORD_HEADER_LEN + ThisCipherMessageSize;\r
+    TempRecordHeader += RECORD_HEADER_LEN + ThisPlainMessageSize;\r
+  }\r
+\r
+  FreePool (BufferIn);\r
+  BufferIn = NULL;\r
+\r
+  //\r
+  // The caller will be responsible to handle the original fragment table\r
+  //\r
+  *FragmentTable = AllocateZeroPool (sizeof (EFI_TLS_FRAGMENT_DATA));\r
+  if (*FragmentTable == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto ERROR;\r
+  }\r
+\r
+  (*FragmentTable)[0].FragmentBuffer  = BufferOut;\r
+  (*FragmentTable)[0].FragmentLength  = BufferOutSize;\r
+  *FragmentCount                      = 1;\r
+\r
+  return Status;\r
+\r
+ERROR:\r
+\r
+  if (BufferIn != NULL) {\r
+    FreePool (BufferIn);\r
+    BufferIn = NULL;\r
+  }\r
+\r
+  if (BufferOut != NULL) {\r
+    FreePool (BufferOut);\r
+    BufferOut = NULL;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r