]> git.proxmox.com Git - mirror_edk2.git/commitdiff
CryptoPkg: Remove deprecated function usage in X509GetCommonName()
authorLong Qin <qin.long@intel.com>
Thu, 24 May 2018 08:08:51 +0000 (16:08 +0800)
committerLong Qin <qin.long@intel.com>
Tue, 5 Jun 2018 02:16:03 +0000 (10:16 +0800)
BZ#: https://bugzilla.tianocore.org/show_bug.cgi?id=923

X509_NAME_get_text_by_NID() used in X509GetCommonName() implementation
is one legacy function which have various limitations. The returned
data may be not usable  when the target cert contains multicharacter
string type like a BMPString or a UTF8String.
This patch replaced the legacy function usage with more general
X509_NAME_get_index_by_NID() / X509_NAME_get_entry() APIs for X509
CommonName retrieving.

Tests: Validated the commonName retrieving with test certificates
       containing PrintableString or BMPString data.

Cc: Ye Ting <ting.ye@intel.com>
Cc: Michael Turner <Michael.Turner@microsoft.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Long Qin <qin.long@intel.com>
Reviewed-by: Ye Ting <ting.ye@intel.com>
CryptoPkg/Include/Library/BaseCryptLib.h
CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
CryptoPkg/Library/BaseCryptLib/Pk/CryptX509Null.c

index 027ea09febd57f9443510ec93597ca97e2991967..dc6aaf0635c2e90d84b118344475241d4caf950e 100644 (file)
@@ -4,7 +4,7 @@
   primitives (Hash Serials, HMAC, RSA, Diffie-Hellman, etc) for UEFI security\r
   functionality enabling.\r
 \r
-Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 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
@@ -2177,7 +2177,7 @@ X509GetSubjectName (
   @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
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
index d00f38daa83f8dc0fd2b31eb5acfaf62748af5fa..d86c784a7f284c9a9c43ec67faa31a3bf7240ded 100644 (file)
@@ -2,7 +2,7 @@
   X.509 Certificate Handler Wrapper Implementation which does not provide\r
   real capabilities.\r
 \r
-Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2012 - 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
@@ -135,7 +135,7 @@ X509GetSubjectName (
   @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