]> git.proxmox.com Git - mirror_edk2.git/commitdiff
CryptoPkg/TlsLib: rewrite TlsSetCipherList()
authorLaszlo Ersek <lersek@redhat.com>
Sat, 31 Mar 2018 15:33:14 +0000 (17:33 +0200)
committerLaszlo Ersek <lersek@redhat.com>
Fri, 13 Apr 2018 12:06:24 +0000 (14:06 +0200)
Rewrite the TlsSetCipherList() function in order to fix the following
issues:

- Any cipher identifier in CipherId that is not recognized by
  TlsGetCipherMapping() will cause the function to return EFI_UNSUPPORTED.

  This is a problem because CipherId is an ordered preference list, and a
  caller should not get EFI_UNSUPPORTED just because it has an elaborate
  CipherId preference list. Instead, we can filter out cipher identifiers
  that we don't recognize, as long as we keep the relative order intact.

- CipherString is allocated on the stack, with 500 bytes.

  While processing a large CipherId preference list, this room may not be
  enough. Although no buffer overflow is possible, CipherString exhaustion
  can lead to a failed TLS connection, because any cipher names that don't
  fit on CipherString cannot be negotiated.

  Compute CipherStringSize first, and allocate CipherString dynamically.

- Finally, the "@STRENGTH" pseudo cipher name is appended to CipherString.
  (Assuming there is enough room left in CipherString.) This causes
  OpenSSL to sort the cipher list "in order of encryption algorithm key
  length".

  This is a bad idea. The caller specifically passes an ordered preference
  list in CipherId. Therefore TlsSetCipherList() must not ask OpenSSL to
  reorder the list, for any reason. Drop "@STRENGTH".

While at it, fix and unify the documentation of the CipherId parameter.

Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: Qin Long <qin.long@intel.com>
Cc: Siyuan Fu <siyuan.fu@intel.com>
Cc: Ting Ye <ting.ye@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=915
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Long Qin <qin.long@intel.com>
Reviewed-by: Jiaxin Wu <jiaxin.wu@intel.com>
CryptoPkg/Include/Library/TlsLib.h
CryptoPkg/Library/TlsLib/InternalTlsLib.h
CryptoPkg/Library/TlsLib/TlsConfig.c
CryptoPkg/Library/TlsLib/TlsLib.inf

index e19a38a214ce687d216828cd23227570df3ddd9d..e71291eaea453bcb914878d8b77d474f13f3178d 100644 (file)
@@ -348,13 +348,16 @@ TlsSetConnectionEnd (
   This function sets the ciphers for use by a specified TLS object.\r
 \r
   @param[in]  Tls          Pointer to a TLS object.\r
   This function sets the ciphers for use by a specified TLS object.\r
 \r
   @param[in]  Tls          Pointer to a TLS object.\r
-  @param[in]  CipherId     Pointer to a string that contains one or more\r
-                           ciphers separated by a colon.\r
+  @param[in]  CipherId     Array of UINT16 cipher identifiers. Each UINT16\r
+                           cipher identifier comes from the TLS Cipher Suite\r
+                           Registry of the IANA, interpreting Byte1 and Byte2\r
+                           in network (big endian) byte order.\r
   @param[in]  CipherNum    The number of cipher in the list.\r
 \r
   @retval  EFI_SUCCESS           The ciphers list was set successfully.\r
   @retval  EFI_INVALID_PARAMETER The parameter is invalid.\r
   @param[in]  CipherNum    The number of cipher in the list.\r
 \r
   @retval  EFI_SUCCESS           The ciphers list was set successfully.\r
   @retval  EFI_INVALID_PARAMETER The parameter is invalid.\r
-  @retval  EFI_UNSUPPORTED       Unsupported TLS cipher in the list.\r
+  @retval  EFI_UNSUPPORTED       No supported TLS cipher was found in CipherId.\r
+  @retval  EFI_OUT_OF_RESOURCES  Memory allocation failed.\r
 \r
 **/\r
 EFI_STATUS\r
 \r
 **/\r
 EFI_STATUS\r
index 3f18a461a8d17dcf58beab320e2a393f72c1f852..b6cf9816aa38ab7777333bde3ebc1934cf5d54b1 100644 (file)
@@ -19,9 +19,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #undef _WIN64\r
 \r
 #include <Library/BaseCryptLib.h>\r
 #undef _WIN64\r
 \r
 #include <Library/BaseCryptLib.h>\r
-#include <Library/BaseLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/SafeIntLib.h>\r
 #include <openssl/ssl.h>\r
 #include <openssl/bio.h>\r
 #include <openssl/err.h>\r
 #include <openssl/ssl.h>\r
 #include <openssl/bio.h>\r
 #include <openssl/err.h>\r
index e2f819b9035f0883c6cd18d97c4963047b0902ed..9154686610e00c859619f49103e1cf61b216d191 100644 (file)
@@ -235,12 +235,16 @@ TlsSetConnectionEnd (
   This function sets the ciphers for use by a specified TLS object.\r
 \r
   @param[in]  Tls          Pointer to a TLS object.\r
   This function sets the ciphers for use by a specified TLS object.\r
 \r
   @param[in]  Tls          Pointer to a TLS object.\r
-  @param[in]  CipherId     Pointer to a UINT16 cipher Id.\r
+  @param[in]  CipherId     Array of UINT16 cipher identifiers. Each UINT16\r
+                           cipher identifier comes from the TLS Cipher Suite\r
+                           Registry of the IANA, interpreting Byte1 and Byte2\r
+                           in network (big endian) byte order.\r
   @param[in]  CipherNum    The number of cipher in the list.\r
 \r
   @retval  EFI_SUCCESS           The ciphers list was set successfully.\r
   @retval  EFI_INVALID_PARAMETER The parameter is invalid.\r
   @param[in]  CipherNum    The number of cipher in the list.\r
 \r
   @retval  EFI_SUCCESS           The ciphers list was set successfully.\r
   @retval  EFI_INVALID_PARAMETER The parameter is invalid.\r
-  @retval  EFI_UNSUPPORTED       Unsupported TLS cipher in the list.\r
+  @retval  EFI_UNSUPPORTED       No supported TLS cipher was found in CipherId.\r
+  @retval  EFI_OUT_OF_RESOURCES  Memory allocation failed.\r
 \r
 **/\r
 EFI_STATUS\r
 \r
 **/\r
 EFI_STATUS\r
@@ -252,51 +256,173 @@ TlsSetCipherList (
   )\r
 {\r
   TLS_CONNECTION           *TlsConn;\r
   )\r
 {\r
   TLS_CONNECTION           *TlsConn;\r
+  EFI_STATUS               Status;\r
+  CONST TLS_CIPHER_MAPPING **MappedCipher;\r
+  UINTN                    MappedCipherBytes;\r
+  UINTN                    MappedCipherCount;\r
+  UINTN                    CipherStringSize;\r
   UINTN                    Index;\r
   CONST TLS_CIPHER_MAPPING *Mapping;\r
   UINTN                    Index;\r
   CONST TLS_CIPHER_MAPPING *Mapping;\r
-  CONST CHAR8              *MappingName;\r
-  CHAR8                    CipherString[500];\r
+  CHAR8                    *CipherString;\r
+  CHAR8                    *CipherStringPosition;\r
 \r
   TlsConn = (TLS_CONNECTION *) Tls;\r
   if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
 \r
   TlsConn = (TLS_CONNECTION *) Tls;\r
   if (TlsConn == NULL || TlsConn->Ssl == NULL || CipherId == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  Mapping     = NULL;\r
-  MappingName = NULL;\r
-\r
-  memset (CipherString, 0, sizeof (CipherString));\r
+  //\r
+  // Allocate the MappedCipher array for recording the mappings that we find\r
+  // for the input IANA identifiers in CipherId.\r
+  //\r
+  Status = SafeUintnMult (CipherNum, sizeof (*MappedCipher),\r
+             &MappedCipherBytes);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  MappedCipher = AllocatePool (MappedCipherBytes);\r
+  if (MappedCipher == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
 \r
 \r
+  //\r
+  // Map the cipher IDs, and count the number of bytes for the full\r
+  // CipherString.\r
+  //\r
+  MappedCipherCount = 0;\r
+  CipherStringSize = 0;\r
   for (Index = 0; Index < CipherNum; Index++) {\r
     //\r
   for (Index = 0; Index < CipherNum; Index++) {\r
     //\r
-    // Handling OpenSSL / RFC Cipher name mapping.\r
+    // Look up the IANA-to-OpenSSL mapping.\r
     //\r
     //\r
-    Mapping = TlsGetCipherMapping (*(CipherId + Index));\r
+    Mapping = TlsGetCipherMapping (CipherId[Index]);\r
     if (Mapping == NULL) {\r
     if (Mapping == NULL) {\r
-      return EFI_UNSUPPORTED;\r
-    }\r
-    MappingName = Mapping->OpensslCipher;\r
-\r
-    if (Index != 0) {\r
+      DEBUG ((DEBUG_VERBOSE, "%a:%a: skipping CipherId=0x%04x\n",\r
+        gEfiCallerBaseName, __FUNCTION__, CipherId[Index]));\r
       //\r
       //\r
-      // The ciphers were separated by a colon.\r
+      // Skipping the cipher is valid because CipherId is an ordered\r
+      // preference list of ciphers, thus we can filter it as long as we\r
+      // don't change the relative order of elements on it.\r
       //\r
       //\r
-      AsciiStrCatS (CipherString, sizeof (CipherString), ":");\r
+      continue;\r
     }\r
     }\r
+    //\r
+    // Accumulate Mapping->OpensslCipherLength into CipherStringSize. If this\r
+    // is not the first successful mapping, account for a colon (":") prefix\r
+    // too.\r
+    //\r
+    if (MappedCipherCount > 0) {\r
+      Status = SafeUintnAdd (CipherStringSize, 1, &CipherStringSize);\r
+      if (EFI_ERROR (Status)) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        goto FreeMappedCipher;\r
+      }\r
+    }\r
+    Status = SafeUintnAdd (CipherStringSize, Mapping->OpensslCipherLength,\r
+               &CipherStringSize);\r
+    if (EFI_ERROR (Status)) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto FreeMappedCipher;\r
+    }\r
+    //\r
+    // Record the mapping.\r
+    //\r
+    MappedCipher[MappedCipherCount++] = Mapping;\r
+  }\r
+\r
+  //\r
+  // Verify that at least one IANA cipher ID could be mapped; account for the\r
+  // terminating NUL character in CipherStringSize; allocate CipherString.\r
+  //\r
+  if (MappedCipherCount == 0) {\r
+    DEBUG ((DEBUG_ERROR, "%a:%a: no CipherId could be mapped\n",\r
+      gEfiCallerBaseName, __FUNCTION__));\r
+    Status = EFI_UNSUPPORTED;\r
+    goto FreeMappedCipher;\r
+  }\r
+  Status = SafeUintnAdd (CipherStringSize, 1, &CipherStringSize);\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto FreeMappedCipher;\r
+  }\r
+  CipherString = AllocatePool (CipherStringSize);\r
+  if (CipherString == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto FreeMappedCipher;\r
+  }\r
 \r
 \r
-    AsciiStrCatS (CipherString, sizeof (CipherString), MappingName);\r
+  //\r
+  // Go over the collected mappings and populate CipherString.\r
+  //\r
+  CipherStringPosition = CipherString;\r
+  for (Index = 0; Index < MappedCipherCount; Index++) {\r
+    Mapping = MappedCipher[Index];\r
+    //\r
+    // Append the colon (":") prefix except for the first mapping, then append\r
+    // Mapping->OpensslCipher.\r
+    //\r
+    if (Index > 0) {\r
+      *(CipherStringPosition++) = ':';\r
+    }\r
+    CopyMem (CipherStringPosition, Mapping->OpensslCipher,\r
+      Mapping->OpensslCipherLength);\r
+    CipherStringPosition += Mapping->OpensslCipherLength;\r
   }\r
 \r
   }\r
 \r
-  AsciiStrCatS (CipherString, sizeof (CipherString), ":@STRENGTH");\r
+  //\r
+  // NUL-terminate CipherString.\r
+  //\r
+  *(CipherStringPosition++) = '\0';\r
+  ASSERT (CipherStringPosition == CipherString + CipherStringSize);\r
+\r
+  //\r
+  // Log CipherString for debugging. CipherString can be very long if the\r
+  // caller provided a large CipherId array, so log CipherString in segments of\r
+  // 79 non-newline characters. (MAX_DEBUG_MESSAGE_LENGTH is usually 0x100 in\r
+  // DebugLib instances.)\r
+  //\r
+  DEBUG_CODE (\r
+    UINTN FullLength;\r
+    UINTN SegmentLength;\r
+\r
+    FullLength = CipherStringSize - 1;\r
+    DEBUG ((DEBUG_VERBOSE, "%a:%a: CipherString={\n", gEfiCallerBaseName,\r
+      __FUNCTION__));\r
+    for (CipherStringPosition = CipherString;\r
+         CipherStringPosition < CipherString + FullLength;\r
+         CipherStringPosition += SegmentLength) {\r
+      SegmentLength = FullLength - (CipherStringPosition - CipherString);\r
+      if (SegmentLength > 79) {\r
+        SegmentLength = 79;\r
+      }\r
+      DEBUG ((DEBUG_VERBOSE, "%.*a\n", SegmentLength, CipherStringPosition));\r
+    }\r
+    DEBUG ((DEBUG_VERBOSE, "}\n"));\r
+    //\r
+    // Restore the pre-debug value of CipherStringPosition by skipping over the\r
+    // trailing NUL.\r
+    //\r
+    CipherStringPosition++;\r
+    ASSERT (CipherStringPosition == CipherString + CipherStringSize);\r
+  );\r
 \r
   //\r
   // Sets the ciphers for use by the Tls object.\r
   //\r
   if (SSL_set_cipher_list (TlsConn->Ssl, CipherString) <= 0) {\r
 \r
   //\r
   // Sets the ciphers for use by the Tls object.\r
   //\r
   if (SSL_set_cipher_list (TlsConn->Ssl, CipherString) <= 0) {\r
-    return EFI_UNSUPPORTED;\r
+    Status = EFI_UNSUPPORTED;\r
+    goto FreeCipherString;\r
   }\r
 \r
   }\r
 \r
-  return EFI_SUCCESS;\r
+  Status = EFI_SUCCESS;\r
+\r
+FreeCipherString:\r
+  FreePool (CipherString);\r
+\r
+FreeMappedCipher:\r
+  FreePool (MappedCipher);\r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
index ae17a7d87444916cd522d858d4d0ead00acc61e4..4dacb2fab01463d1f606ebe8c7b6f875580e4b56 100644 (file)
 \r
 [LibraryClasses]\r
   BaseCryptLib\r
 \r
 [LibraryClasses]\r
   BaseCryptLib\r
-  BaseLib\r
   BaseMemoryLib\r
   DebugLib\r
   IntrinsicLib\r
   BaseMemoryLib\r
   DebugLib\r
   IntrinsicLib\r
+  MemoryAllocationLib\r
   OpensslLib\r
   OpensslLib\r
+  SafeIntLib\r
 \r
 [BuildOptions]\r
   #\r
 \r
 [BuildOptions]\r
   #\r