]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
CryptoPkg: Remove deprecated function usage in X509GetCommonName()
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptX509.c
index 56e66308ae43337f05acf170a3089bcbaa2b5f84..c137df357f4b01ffc16ee91ef95fa5f3e9e342b4 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   X.509 Certificate Handler Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\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
@@ -303,7 +303,7 @@ _Exit:
   @param[in]      Cert             Pointer to the DER-encoded X509 certificate.\r
   @param[in]      CertSize         Size of the X509 certificate in bytes.\r
   @param[out]     CommonName       Buffer to contain the retrieved certificate common\r
-                                   name string. At most CommonNameSize bytes will be\r
+                                   name string (UTF8). At most CommonNameSize bytes will be\r
                                    written and the string will be null terminated. May be\r
                                    NULL in order to determine the size buffer needed.\r
   @param[in,out]  CommonNameSize   The size in bytes of the CommonName buffer on input,\r
@@ -332,13 +332,18 @@ X509GetCommonName (
   IN OUT  UINTN        *CommonNameSize\r
   )\r
 {\r
-  RETURN_STATUS  ReturnStatus;\r
-  BOOLEAN        Status;\r
-  X509           *X509Cert;\r
-  X509_NAME      *X509Name;\r
-  INTN           Length;\r
+  RETURN_STATUS    ReturnStatus;\r
+  BOOLEAN          Status;\r
+  X509             *X509Cert;\r
+  X509_NAME        *X509Name;\r
+  INT32            Index;\r
+  INTN             Length;\r
+  X509_NAME_ENTRY  *Entry;\r
+  ASN1_STRING      *EntryData;\r
+  UINT8            *UTF8Name;\r
 \r
   ReturnStatus = RETURN_INVALID_PARAMETER;\r
+  UTF8Name     = NULL;\r
 \r
   //\r
   // Check input parameters.\r
@@ -378,8 +383,8 @@ X509GetCommonName (
   //\r
   // Retrieve the CommonName information from X.509 Subject\r
   //\r
-  Length = (INTN) X509_NAME_get_text_by_NID (X509Name, NID_commonName, CommonName, (int)(*CommonNameSize));\r
-  if (Length < 0) {\r
+  Index = X509_NAME_get_index_by_NID (X509Name, NID_commonName, -1);\r
+  if (Index < 0) {\r
     //\r
     // No CommonName entry exists in X509_NAME object\r
     //\r
@@ -388,10 +393,35 @@ X509GetCommonName (
     goto _Exit;\r
   }\r
 \r
-  *CommonNameSize = (UINTN)(Length + 1);\r
+  Entry = X509_NAME_get_entry (X509Name, Index);\r
+  if (Entry == NULL) {\r
+    //\r
+    // Fail to retrieve name entry data\r
+    //\r
+    *CommonNameSize = 0;\r
+    ReturnStatus    = RETURN_NOT_FOUND;\r
+    goto _Exit;\r
+  }\r
+\r
+  EntryData = X509_NAME_ENTRY_get_data (Entry);\r
+\r
+  Length = ASN1_STRING_to_UTF8 (&UTF8Name, EntryData);\r
+  if (Length < 0) {\r
+    //\r
+    // Fail to convert the commonName string\r
+    //\r
+    *CommonNameSize = 0;\r
+    ReturnStatus    = RETURN_INVALID_PARAMETER;\r
+    goto _Exit;\r
+  }\r
+\r
   if (CommonName == NULL) {\r
+    *CommonNameSize = Length + 1;\r
     ReturnStatus = RETURN_BUFFER_TOO_SMALL;\r
   } else {\r
+    *CommonNameSize = MIN ((UINTN)Length, *CommonNameSize - 1) + 1;\r
+    CopyMem (CommonName, UTF8Name, *CommonNameSize - 1);\r
+    CommonName[*CommonNameSize - 1] = '\0';\r
     ReturnStatus = RETURN_SUCCESS;\r
   }\r
 \r
@@ -402,6 +432,9 @@ _Exit:
   if (X509Cert != NULL) {\r
     X509_free (X509Cert);\r
   }\r
+  if (UTF8Name != NULL) {\r
+    OPENSSL_free (UTF8Name);\r
+  }\r
 \r
   return ReturnStatus;\r
 }\r