]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/TlsLib/TlsConfig.c
CryptoPkg/TlsLib: use binary search in the TlsGetCipherMapping() function
[mirror_edk2.git] / CryptoPkg / Library / TlsLib / TlsConfig.c
index 4c88229b8921492a55623393390cd4290d5c0544..c1d91a599482528228520f078eae79779e63a4d1 100644 (file)
@@ -24,13 +24,15 @@ typedef struct {
   // OpenSSL-used Cipher Suite String\r
   //\r
   CONST CHAR8                     *OpensslCipher;\r
-} TLS_CIPHER_PAIR;\r
+} TLS_CIPHER_MAPPING;\r
 \r
 //\r
 // The mapping table between IANA/IETF Cipher Suite definitions and\r
 // OpenSSL-used Cipher Suite name.\r
 //\r
-STATIC CONST TLS_CIPHER_PAIR TlsCipherMappingTable[] = {\r
+// Keep the table uniquely sorted by the IanaCipher field, in increasing order.\r
+//\r
+STATIC CONST TLS_CIPHER_MAPPING TlsCipherMappingTable[] = {\r
   { 0x0001, "NULL-MD5" },                 /// TLS_RSA_WITH_NULL_MD5\r
   { 0x0002, "NULL-SHA" },                 /// TLS_RSA_WITH_NULL_SHA\r
   { 0x0004, "RC4-MD5" },                  /// TLS_RSA_WITH_RC4_128_MD5\r
@@ -57,36 +59,44 @@ STATIC CONST TLS_CIPHER_PAIR TlsCipherMappingTable[] = {
 };\r
 \r
 /**\r
-  Gets the OpenSSL cipher suite string for the supplied IANA TLS cipher suite.\r
+  Gets the OpenSSL cipher suite mapping for the supplied IANA TLS cipher suite.\r
 \r
   @param[in]  CipherId    The supplied IANA TLS cipher suite ID.\r
 \r
-  @return  The corresponding OpenSSL cipher suite string if found,\r
+  @return  The corresponding OpenSSL cipher suite mapping if found,\r
            NULL otherwise.\r
 \r
 **/\r
 STATIC\r
-CONST CHAR8 *\r
-TlsGetCipherString (\r
+CONST TLS_CIPHER_MAPPING *\r
+TlsGetCipherMapping (\r
   IN     UINT16                   CipherId\r
   )\r
 {\r
-  CONST TLS_CIPHER_PAIR  *CipherEntry;\r
-  UINTN                  TableSize;\r
-  UINTN                  Index;\r
-\r
-  CipherEntry = TlsCipherMappingTable;\r
-  TableSize = sizeof (TlsCipherMappingTable) / sizeof (TLS_CIPHER_PAIR);\r
+  INTN                      Left;\r
+  INTN                      Right;\r
+  INTN                      Middle;\r
 \r
   //\r
-  // Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation\r
+  // Binary Search Cipher Mapping Table for IANA-OpenSSL Cipher Translation\r
   //\r
-  for (Index = 0; Index < TableSize; Index++, CipherEntry++) {\r
-    //\r
-    // Translate IANA cipher suite name to OpenSSL name.\r
-    //\r
-    if (CipherEntry->IanaCipher == CipherId) {\r
-      return CipherEntry->OpensslCipher;\r
+  Left  = 0;\r
+  Right = ARRAY_SIZE (TlsCipherMappingTable) - 1;\r
+\r
+  while (Right >= Left) {\r
+    Middle = (Left + Right) / 2;\r
+\r
+    if (CipherId == TlsCipherMappingTable[Middle].IanaCipher) {\r
+      //\r
+      // Translate IANA cipher suite ID to OpenSSL name.\r
+      //\r
+      return &TlsCipherMappingTable[Middle];\r
+    }\r
+\r
+    if (CipherId < TlsCipherMappingTable[Middle].IanaCipher) {\r
+      Right = Middle - 1;\r
+    } else {\r
+      Left  = Middle + 1;\r
     }\r
   }\r
 \r
@@ -229,16 +239,18 @@ TlsSetCipherList (
   IN     UINTN                    CipherNum\r
   )\r
 {\r
-  TLS_CONNECTION  *TlsConn;\r
-  UINTN           Index;\r
-  CONST CHAR8     *MappingName;\r
-  CHAR8           CipherString[500];\r
+  TLS_CONNECTION           *TlsConn;\r
+  UINTN                    Index;\r
+  CONST TLS_CIPHER_MAPPING *Mapping;\r
+  CONST CHAR8              *MappingName;\r
+  CHAR8                    CipherString[500];\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
@@ -247,10 +259,11 @@ TlsSetCipherList (
     //\r
     // Handling OpenSSL / RFC Cipher name mapping.\r
     //\r
-    MappingName = TlsGetCipherString (*(CipherId + Index));\r
-    if (MappingName == NULL) {\r
+    Mapping = TlsGetCipherMapping (*(CipherId + Index));\r
+    if (Mapping == NULL) {\r
       return EFI_UNSUPPORTED;\r
     }\r
+    MappingName = Mapping->OpensslCipher;\r
 \r
     if (Index != 0) {\r
       //\r
@@ -642,6 +655,8 @@ TlsSetCertRevocationList (
   This function returns the protocol version used by the specified TLS\r
   connection.\r
 \r
+  If Tls is NULL, then ASSERT().\r
+\r
   @param[in]  Tls    Pointer to the TLS object.\r
 \r
   @return  The protocol version of the specified TLS connection.\r
@@ -668,6 +683,8 @@ TlsGetVersion (
   This function returns the connection end (as client or as server) used by\r
   the specified TLS connection.\r
 \r
+  If Tls is NULL, then ASSERT().\r
+\r
   @param[in]  Tls    Pointer to the TLS object.\r
 \r
   @return  The connection end used by the specified TLS connection.\r
@@ -761,6 +778,8 @@ TlsGetCurrentCompressionId (
   This function returns the peer verification mode currently set in the\r
   specified TLS connection.\r
 \r
+  If Tls is NULL, then ASSERT().\r
+\r
   @param[in]  Tls    Pointer to the TLS object.\r
 \r
   @return  The verification mode set in the specified TLS connection.\r
@@ -984,7 +1003,7 @@ TlsGetHostPublicCert (
   Cert    = NULL;\r
   TlsConn = (TLS_CONNECTION *) Tls;\r
 \r
-  if (TlsConn == NULL || TlsConn->Ssl == NULL || DataSize == NULL) {\r
+  if (TlsConn == NULL || TlsConn->Ssl == NULL || DataSize == NULL || (*DataSize != 0 && Data == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r