public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "wenyi,xie" <xiewenyi2@huawei.com>
To: <devel@edk2.groups.io>, <jiewen.yao@intel.com>,
	<jian.j.wang@intel.com>, <xiaoyux.lu@intel.com>,
	<guomin.jiang@intel.com>
Cc: <songdongkuang@huawei.com>, <xiewenyi2@huawei.com>,
	<xujiaxia@huawei.com>
Subject: [PATCH EDK2 v1 1/1] CryptoPkg/BaseCryptLib: Support PEM certification
Date: Tue, 9 Nov 2021 20:58:19 +0800	[thread overview]
Message-ID: <20211109125819.21740-2-xiewenyi2@huawei.com> (raw)
In-Reply-To: <20211109125819.21740-1-xiewenyi2@huawei.com>

As PEM-encoded certificate is also necessary, add
support for PEM-encoded certificate in
X509ConstructCertificate.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Xiaoyu Lu <xiaoyux.lu@intel.com>
Cc: Guomin Jiang <guomin.jiang@intel.com>
Signed-off-by: Jiaxia Xu <xujiaxia@huawei.com>
Signed-off-by: Wenyi Xie <xiewenyi2@huawei.com>
---
 CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c | 33 ++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c b/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
index b1393a89c5ab..db122cd574fa 100644
--- a/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
+++ b/CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
@@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include "InternalCryptLib.h"
 #include <openssl/x509.h>
 #include <openssl/rsa.h>
+#include <openssl/pem.h>
 
 /**
   Construct a X509 object from DER-encoded certificate data.
@@ -33,7 +34,12 @@ X509ConstructCertificate (
   )
 {
   X509         *X509Cert;
+  BIO          *BioCert;
   CONST UINT8  *Temp;
+  BOOLEAN      CertFlag;
+
+  BioCert   = NULL;
+  CertFlag  = TRUE;
 
   //
   // Check input parameters.
@@ -48,12 +54,35 @@ X509ConstructCertificate (
   Temp     = Cert;
   X509Cert = d2i_X509 (NULL, &Temp, (long) CertSize);
   if (X509Cert == NULL) {
-    return FALSE;
+    BioCert = BIO_new (BIO_s_mem ());
+    if (BioCert == NULL) {
+      CertFlag = FALSE;
+      goto ON_EXIT;
+    }
+
+    if (BIO_write (BioCert, Temp, (UINT32) CertSize) <= 0) {
+      CertFlag = FALSE;
+      goto ON_EXIT;
+    }
+
+    //
+    // Read PEM-encoded X509 Certificate and Construct X509 object.
+    //
+    X509Cert = PEM_read_bio_X509 (BioCert, NULL, NULL, NULL);
+    if (X509Cert == NULL) {
+      CertFlag = FALSE;
+      goto ON_EXIT;
+    }
   }
 
   *SingleX509Cert = (UINT8 *) X509Cert;
 
-  return TRUE;
+ON_EXIT:
+  if (BioCert != NULL) {
+    BIO_free (BioCert);
+  }
+
+  return CertFlag;
 }
 
 /**
-- 
2.20.1.windows.1


      reply	other threads:[~2021-11-09 12:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-09 12:58 [PATCH EDK2 v1 0/1] CryptoPkg/BaseCryptLib: Support PEM certification wenyi,xie
2021-11-09 12:58 ` wenyi,xie [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211109125819.21740-2-xiewenyi2@huawei.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox