nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0366-appended-signatures-parse-PKCS-7-signedData-and-X.50.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Daniel Axtens <dja@axtens.net>
80913e
Date: Thu, 30 Jul 2020 01:33:46 +1000
80913e
Subject: [PATCH] appended signatures: parse PKCS#7 signedData and X.509
80913e
 certificates
80913e
80913e
This code allows us to parse:
80913e
80913e
 - PKCS#7 signedData messages. Only a single signerInfo is supported,
80913e
   which is all that the Linux sign-file utility supports creating
80913e
   out-of-the-box. Only RSA, SHA-256 and SHA-512 are supported.
80913e
   Any certificate embedded in the PKCS#7 message will be ignored.
80913e
80913e
 - X.509 certificates: at least enough to verify the signatures on the
80913e
   PKCS#7 messages. We expect that the certificates embedded in grub will
80913e
   be leaf certificates, not CA certificates. The parser enforces this.
80913e
80913e
Signed-off-by: Daniel Axtens <dja@axtens.net>
80913e
---
80913e
 grub-core/commands/appendedsig/asn1util.c    | 102 +++
80913e
 grub-core/commands/appendedsig/pkcs7.c       | 305 +++++++++
80913e
 grub-core/commands/appendedsig/x509.c        | 972 +++++++++++++++++++++++++++
80913e
 grub-core/commands/appendedsig/appendedsig.h | 110 +++
80913e
 4 files changed, 1489 insertions(+)
80913e
 create mode 100644 grub-core/commands/appendedsig/asn1util.c
80913e
 create mode 100644 grub-core/commands/appendedsig/pkcs7.c
80913e
 create mode 100644 grub-core/commands/appendedsig/x509.c
80913e
 create mode 100644 grub-core/commands/appendedsig/appendedsig.h
80913e
80913e
diff --git a/grub-core/commands/appendedsig/asn1util.c b/grub-core/commands/appendedsig/asn1util.c
80913e
new file mode 100644
80913e
index 00000000000..eff095a9df2
80913e
--- /dev/null
80913e
+++ b/grub-core/commands/appendedsig/asn1util.c
80913e
@@ -0,0 +1,102 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2020  IBM Corporation.
80913e
+ *
80913e
+ *  GRUB is free software: you can redistribute it and/or modify
80913e
+ *  it under the terms of the GNU General Public License as published by
80913e
+ *  the Free Software Foundation, either version 3 of the License, or
80913e
+ *  (at your option) any later version.
80913e
+ *
80913e
+ *  GRUB is distributed in the hope that it will be useful,
80913e
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
80913e
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80913e
+ *  GNU General Public License for more details.
80913e
+ *
80913e
+ *  You should have received a copy of the GNU General Public License
80913e
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
+ */
80913e
+
80913e
+#include <grub/libtasn1.h>
80913e
+#include <grub/types.h>
80913e
+#include <grub/err.h>
80913e
+#include <grub/mm.h>
80913e
+#include <grub/crypto.h>
80913e
+#include <grub/gcrypt/gcrypt.h>
80913e
+
80913e
+#include "appendedsig.h"
80913e
+
80913e
+asn1_node _gnutls_gnutls_asn = ASN1_TYPE_EMPTY;
80913e
+asn1_node _gnutls_pkix_asn = ASN1_TYPE_EMPTY;
80913e
+
80913e
+extern const ASN1_ARRAY_TYPE gnutls_asn1_tab[];
80913e
+extern const ASN1_ARRAY_TYPE pkix_asn1_tab[];
80913e
+
80913e
+/*
80913e
+ * Read a value from an ASN1 node, allocating memory to store it.
80913e
+ *
80913e
+ * It will work for anything where the size libtasn1 returns is right:
80913e
+ *  - Integers
80913e
+ *  - Octet strings
80913e
+ *  - DER encoding of other structures
80913e
+ * It will _not_ work for things where libtasn1 size requires adjustment:
80913e
+ *  - Strings that require an extra NULL byte at the end
80913e
+ *  - Bit strings because libtasn1 returns the length in bits, not bytes.
80913e
+ *
80913e
+ * If the function returns a non-NULL value, the caller must free it.
80913e
+ */
80913e
+void *
80913e
+grub_asn1_allocate_and_read (asn1_node node, const char *name,
80913e
+			     const char *friendly_name, int *content_size)
80913e
+{
80913e
+  int result;
80913e
+  grub_uint8_t *tmpstr = NULL;
80913e
+  int tmpstr_size = 0;
80913e
+
80913e
+  result = asn1_read_value (node, name, NULL, &tmpstr_size);
80913e
+  if (result != ASN1_MEM_ERROR)
80913e
+    {
80913e
+      grub_snprintf (grub_errmsg, sizeof (grub_errmsg),
80913e
+		     _
80913e
+		     ("Reading size of %s did not return expected status: %s"),
80913e
+		     friendly_name, asn1_strerror (result));
80913e
+      grub_errno = GRUB_ERR_BAD_FILE_TYPE;
80913e
+      return NULL;
80913e
+    }
80913e
+
80913e
+  tmpstr = grub_malloc (tmpstr_size);
80913e
+  if (tmpstr == NULL)
80913e
+    {
80913e
+      grub_snprintf (grub_errmsg, sizeof (grub_errmsg),
80913e
+		     "Could not allocate memory to store %s", friendly_name);
80913e
+      grub_errno = GRUB_ERR_OUT_OF_MEMORY;
80913e
+      return NULL;
80913e
+    }
80913e
+
80913e
+  result = asn1_read_value (node, name, tmpstr, &tmpstr_size);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      grub_free (tmpstr);
80913e
+      grub_snprintf (grub_errmsg, sizeof (grub_errmsg),
80913e
+		     "Error reading %s: %s",
80913e
+		     friendly_name, asn1_strerror (result));
80913e
+      grub_errno = GRUB_ERR_BAD_FILE_TYPE;
80913e
+      return NULL;
80913e
+    }
80913e
+
80913e
+  *content_size = tmpstr_size;
80913e
+
80913e
+  return tmpstr;
80913e
+}
80913e
+
80913e
+int
80913e
+asn1_init (void)
80913e
+{
80913e
+  int res;
80913e
+  res = asn1_array2tree (gnutls_asn1_tab, &_gnutls_gnutls_asn, NULL);
80913e
+  if (res != ASN1_SUCCESS)
80913e
+    {
80913e
+      return res;
80913e
+    }
80913e
+  res = asn1_array2tree (pkix_asn1_tab, &_gnutls_pkix_asn, NULL);
80913e
+  return res;
80913e
+}
80913e
diff --git a/grub-core/commands/appendedsig/pkcs7.c b/grub-core/commands/appendedsig/pkcs7.c
80913e
new file mode 100644
80913e
index 00000000000..dc6afe203f7
80913e
--- /dev/null
80913e
+++ b/grub-core/commands/appendedsig/pkcs7.c
80913e
@@ -0,0 +1,305 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2020  IBM Corporation.
80913e
+ *
80913e
+ *  GRUB is free software: you can redistribute it and/or modify
80913e
+ *  it under the terms of the GNU General Public License as published by
80913e
+ *  the Free Software Foundation, either version 3 of the License, or
80913e
+ *  (at your option) any later version.
80913e
+ *
80913e
+ *  GRUB is distributed in the hope that it will be useful,
80913e
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
80913e
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80913e
+ *  GNU General Public License for more details.
80913e
+ *
80913e
+ *  You should have received a copy of the GNU General Public License
80913e
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
+ */
80913e
+
80913e
+#include "appendedsig.h"
80913e
+#include <grub/misc.h>
80913e
+#include <grub/crypto.h>
80913e
+#include <grub/gcrypt/gcrypt.h>
80913e
+
80913e
+
80913e
+static char asn1_error[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
80913e
+
80913e
+/*
80913e
+ * RFC 5652 s 5.1
80913e
+ */
80913e
+const char *signedData_oid = "1.2.840.113549.1.7.2";
80913e
+
80913e
+/*
80913e
+ * RFC 4055 s 2.1
80913e
+ */
80913e
+const char *sha256_oid = "2.16.840.1.101.3.4.2.1";
80913e
+const char *sha512_oid = "2.16.840.1.101.3.4.2.3";
80913e
+
80913e
+static grub_err_t
80913e
+process_content (grub_uint8_t * content, int size,
80913e
+		 struct pkcs7_signedData *msg)
80913e
+{
80913e
+  int res;
80913e
+  asn1_node signed_part;
80913e
+  grub_err_t err = GRUB_ERR_NONE;
80913e
+  char algo_oid[MAX_OID_LEN];
80913e
+  int algo_oid_size = sizeof (algo_oid);
80913e
+  int algo_count;
80913e
+  char version;
80913e
+  int version_size = sizeof (version);
80913e
+  grub_uint8_t *result_buf;
80913e
+  int result_size = 0;
80913e
+  int crls_size = 0;
80913e
+  gcry_error_t gcry_err;
80913e
+
80913e
+  res = asn1_create_element (_gnutls_pkix_asn, "PKIX1.pkcs-7-SignedData",
80913e
+			     &signed_part);
80913e
+  if (res != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			 "Could not create ASN.1 structure for PKCS#7 signed part.");
80913e
+    }
80913e
+
80913e
+  res = asn1_der_decoding2 (&signed_part, content, &size,
80913e
+			    ASN1_DECODE_FLAG_STRICT_DER, asn1_error);
80913e
+  if (res != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Error reading PKCS#7 signed data: %s", asn1_error);
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  /* SignedData ::= SEQUENCE {
80913e
+   *     version CMSVersion,
80913e
+   *     digestAlgorithms DigestAlgorithmIdentifiers,
80913e
+   *     encapContentInfo EncapsulatedContentInfo,
80913e
+   *     certificates [0] IMPLICIT CertificateSet OPTIONAL,
80913e
+   *     crls [1] IMPLICIT RevocationInfoChoices OPTIONAL,
80913e
+   *     signerInfos SignerInfos }
80913e
+   */
80913e
+
80913e
+  /* version per the algo in 5.1, must be 1 */
80913e
+  res = asn1_read_value (signed_part, "version", &version, &version_size);
80913e
+  if (res != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Error reading signedData version: %s",
80913e
+		    asn1_strerror (res));
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  if (version != 1)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Unexpected signature version v%d, only v1 supported",
80913e
+		    version);
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  /*
80913e
+   * digestAlgorithms DigestAlgorithmIdentifiers
80913e
+   *
80913e
+   * DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
80913e
+   * DigestAlgorithmIdentifer is an X.509 AlgorithmIdentifier (10.1.1)
80913e
+   * 
80913e
+   * RFC 4055 s 2.1:
80913e
+   * sha256Identifier  AlgorithmIdentifier  ::=  { id-sha256, NULL }
80913e
+   * sha512Identifier  AlgorithmIdentifier  ::=  { id-sha512, NULL }
80913e
+   *
80913e
+   * We only support 1 element in the set, and we do not check parameters atm.
80913e
+   */
80913e
+  res =
80913e
+    asn1_number_of_elements (signed_part, "digestAlgorithms", &algo_count);
80913e
+  if (res != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Error counting number of digest algorithms: %s",
80913e
+		    asn1_strerror (res));
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  if (algo_count != 1)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
80913e
+		    "Only 1 digest algorithm is supported");
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  res =
80913e
+    asn1_read_value (signed_part, "digestAlgorithms.?1.algorithm", algo_oid,
80913e
+		     &algo_oid_size);
80913e
+  if (res != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Error reading digest algorithm: %s",
80913e
+		    asn1_strerror (res));
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  if (grub_strncmp (sha512_oid, algo_oid, algo_oid_size) == 0)
80913e
+    {
80913e
+      msg->hash = grub_crypto_lookup_md_by_name ("sha512");
80913e
+    }
80913e
+  else if (grub_strncmp (sha256_oid, algo_oid, algo_oid_size) == 0)
80913e
+    {
80913e
+      msg->hash = grub_crypto_lookup_md_by_name ("sha256");
80913e
+    }
80913e
+  else
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
80913e
+		    "Only SHA-256 and SHA-512 hashes are supported, found OID %s",
80913e
+		    algo_oid);
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  if (!msg->hash)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Hash algorithm for OID %s not loaded", algo_oid);
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  /*
80913e
+   * We ignore the certificates, but we don't permit CRLs.
80913e
+   * A CRL entry might be revoking the certificate we're using, and we have
80913e
+   * no way of dealing with that at the moment.
80913e
+   */
80913e
+  res = asn1_read_value (signed_part, "crls", NULL, &crls_size);
80913e
+  if (res != ASN1_ELEMENT_NOT_FOUND)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
80913e
+		    "PKCS#7 messages with embedded CRLs are not supported");
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  /* read the signature */
80913e
+  result_buf =
80913e
+    grub_asn1_allocate_and_read (signed_part, "signerInfos.?1.signature",
80913e
+				 "signature data", &result_size);
80913e
+  if (!result_buf)
80913e
+    {
80913e
+      err = grub_errno;
80913e
+      goto cleanup_signed_part;
80913e
+    }
80913e
+
80913e
+  gcry_err =
80913e
+    gcry_mpi_scan (&(msg->sig_mpi), GCRYMPI_FMT_USG, result_buf, result_size,
80913e
+		   NULL);
80913e
+  if (gcry_err != GPG_ERR_NO_ERROR)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Error loading signature into MPI structure: %d",
80913e
+		    gcry_err);
80913e
+      goto cleanup_result;
80913e
+    }
80913e
+
80913e
+cleanup_result:
80913e
+  grub_free (result_buf);
80913e
+cleanup_signed_part:
80913e
+  asn1_delete_structure (&signed_part);
80913e
+
80913e
+  return err;
80913e
+}
80913e
+
80913e
+grub_err_t
80913e
+parse_pkcs7_signedData (void *sigbuf, grub_size_t data_size,
80913e
+			struct pkcs7_signedData *msg)
80913e
+{
80913e
+  int res;
80913e
+  asn1_node content_info;
80913e
+  grub_err_t err = GRUB_ERR_NONE;
80913e
+  char content_oid[MAX_OID_LEN];
80913e
+  grub_uint8_t *content;
80913e
+  int content_size;
80913e
+  int content_oid_size = sizeof (content_oid);
80913e
+  int size;
80913e
+
80913e
+  if (data_size > GRUB_INT_MAX)
80913e
+    return grub_error (GRUB_ERR_OUT_OF_RANGE,
80913e
+		       "Cannot parse a PKCS#7 message where data size > INT_MAX");
80913e
+  size = (int) data_size;
80913e
+
80913e
+  res = asn1_create_element (_gnutls_pkix_asn,
80913e
+			     "PKIX1.pkcs-7-ContentInfo", &content_info);
80913e
+  if (res != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			 "Could not create ASN.1 structure for PKCS#7 data: %s",
80913e
+			 asn1_strerror (res));
80913e
+    }
80913e
+
80913e
+  res = asn1_der_decoding2 (&content_info, sigbuf, &size,
80913e
+			    ASN1_DECODE_FLAG_STRICT_DER, asn1_error);
80913e
+  if (res != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Error decoding PKCS#7 message DER: %s", asn1_error);
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  /*
80913e
+   * ContentInfo ::= SEQUENCE {
80913e
+   *     contentType ContentType,
80913e
+   *     content [0] EXPLICIT ANY DEFINED BY contentType }
80913e
+   *
80913e
+   * ContentType ::= OBJECT IDENTIFIER
80913e
+   */
80913e
+  res =
80913e
+    asn1_read_value (content_info, "contentType", content_oid,
80913e
+		     &content_oid_size);
80913e
+  if (res != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Error reading PKCS#7 content type: %s",
80913e
+		    asn1_strerror (res));
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  /* OID for SignedData defined in 5.1 */
80913e
+  if (grub_strncmp (signedData_oid, content_oid, content_oid_size) != 0)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_SIGNATURE,
80913e
+		    "Unexpected content type in PKCS#7 message: OID %s",
80913e
+		    content_oid);
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  content =
80913e
+    grub_asn1_allocate_and_read (content_info, "content",
80913e
+				 "PKCS#7 message content", &content_size);
80913e
+  if (!content)
80913e
+    {
80913e
+      err = grub_errno;
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  err = process_content (content, content_size, msg);
80913e
+  grub_free (content);
80913e
+
80913e
+cleanup:
80913e
+  asn1_delete_structure (&content_info);
80913e
+  return err;
80913e
+}
80913e
+
80913e
+/*
80913e
+ * Release all the storage associated with the PKCS#7 message.
80913e
+ * If the caller dynamically allocated the message, it must free it.
80913e
+ */
80913e
+void
80913e
+pkcs7_signedData_release (struct pkcs7_signedData *msg)
80913e
+{
80913e
+  gcry_mpi_release (msg->sig_mpi);
80913e
+}
80913e
diff --git a/grub-core/commands/appendedsig/x509.c b/grub-core/commands/appendedsig/x509.c
80913e
new file mode 100644
80913e
index 00000000000..652e4f16870
80913e
--- /dev/null
80913e
+++ b/grub-core/commands/appendedsig/x509.c
80913e
@@ -0,0 +1,972 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2020  IBM Corporation.
80913e
+ *
80913e
+ *  GRUB is free software: you can redistribute it and/or modify
80913e
+ *  it under the terms of the GNU General Public License as published by
80913e
+ *  the Free Software Foundation, either version 3 of the License, or
80913e
+ *  (at your option) any later version.
80913e
+ *
80913e
+ *  GRUB is distributed in the hope that it will be useful,
80913e
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
80913e
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80913e
+ *  GNU General Public License for more details.
80913e
+ *
80913e
+ *  You should have received a copy of the GNU General Public License
80913e
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
+ */
80913e
+
80913e
+#include <grub/libtasn1.h>
80913e
+#include <grub/types.h>
80913e
+#include <grub/err.h>
80913e
+#include <grub/mm.h>
80913e
+#include <grub/crypto.h>
80913e
+#include <grub/gcrypt/gcrypt.h>
80913e
+
80913e
+#include "appendedsig.h"
80913e
+
80913e
+static char asn1_error[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
80913e
+
80913e
+/*
80913e
+ * RFC 3279 2.3.1  RSA Keys
80913e
+ */
80913e
+const char *rsaEncryption_oid = "1.2.840.113549.1.1.1";
80913e
+
80913e
+/*
80913e
+ * RFC 5280 Appendix A
80913e
+ */
80913e
+const char *commonName_oid = "2.5.4.3";
80913e
+
80913e
+/*
80913e
+ * RFC 5280 4.2.1.3 Key Usage
80913e
+ */
80913e
+const char *keyUsage_oid = "2.5.29.15";
80913e
+
80913e
+/*
80913e
+ * RFC 5280 4.2.1.9 Basic Constraints
80913e
+ */
80913e
+const char *basicConstraints_oid = "2.5.29.19";
80913e
+
80913e
+/*
80913e
+ * RFC 3279 2.3.1
80913e
+ *
80913e
+ *  The RSA public key MUST be encoded using the ASN.1 type RSAPublicKey:
80913e
+ *
80913e
+ *     RSAPublicKey ::= SEQUENCE {
80913e
+ *        modulus            INTEGER,    -- n
80913e
+ *        publicExponent     INTEGER  }  -- e
80913e
+ *
80913e
+ *  where modulus is the modulus n, and publicExponent is the public
80913e
+ *  exponent e.
80913e
+ */
80913e
+static grub_err_t
80913e
+grub_parse_rsa_pubkey (grub_uint8_t * der, int dersize,
80913e
+		       struct x509_certificate *certificate)
80913e
+{
80913e
+  int result;
80913e
+  asn1_node spk = ASN1_TYPE_EMPTY;
80913e
+  grub_uint8_t *m_data, *e_data;
80913e
+  int m_size, e_size;
80913e
+  grub_err_t err = GRUB_ERR_NONE;
80913e
+  gcry_error_t gcry_err;
80913e
+
80913e
+  result =
80913e
+    asn1_create_element (_gnutls_gnutls_asn, "GNUTLS.RSAPublicKey", &spk;;
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			 "Cannot create storage for public key ASN.1 data");
80913e
+    }
80913e
+
80913e
+  result = asn1_der_decoding2 (&spk, der, &dersize,
80913e
+			       ASN1_DECODE_FLAG_STRICT_DER, asn1_error);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Cannot decode certificate public key DER: %s",
80913e
+		    asn1_error);
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  m_data =
80913e
+    grub_asn1_allocate_and_read (spk, "modulus", "RSA modulus", &m_size);
80913e
+  if (!m_data)
80913e
+    {
80913e
+      err = grub_errno;
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  e_data =
80913e
+    grub_asn1_allocate_and_read (spk, "publicExponent", "RSA public exponent",
80913e
+				 &e_size);
80913e
+  if (!e_data)
80913e
+    {
80913e
+      err = grub_errno;
80913e
+      goto cleanup_m_data;
80913e
+    }
80913e
+
80913e
+  /*
80913e
+   * convert m, e to mpi
80913e
+   *
80913e
+   * nscanned is not set for FMT_USG, it's only set for FMT_PGP, 
80913e
+   * so we can't verify it
80913e
+   */
80913e
+  gcry_err =
80913e
+    gcry_mpi_scan (&certificate->mpis[0], GCRYMPI_FMT_USG, m_data, m_size,
80913e
+		   NULL);
80913e
+  if (gcry_err != GPG_ERR_NO_ERROR)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Error loading RSA modulus into MPI structure: %d",
80913e
+		    gcry_err);
80913e
+      goto cleanup_e_data;
80913e
+    }
80913e
+
80913e
+  gcry_err =
80913e
+    gcry_mpi_scan (&certificate->mpis[1], GCRYMPI_FMT_USG, e_data, e_size,
80913e
+		   NULL);
80913e
+  if (gcry_err != GPG_ERR_NO_ERROR)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Error loading RSA exponent into MPI structure: %d",
80913e
+		    gcry_err);
80913e
+      goto cleanup_m_mpi;
80913e
+    }
80913e
+
80913e
+  grub_free (e_data);
80913e
+  grub_free (m_data);
80913e
+  asn1_delete_structure (&spk;;
80913e
+  return GRUB_ERR_NONE;
80913e
+
80913e
+cleanup_m_mpi:
80913e
+  gcry_mpi_release (certificate->mpis[0]);
80913e
+cleanup_e_data:
80913e
+  grub_free (e_data);
80913e
+cleanup_m_data:
80913e
+  grub_free (m_data);
80913e
+cleanup:
80913e
+  asn1_delete_structure (&spk;;
80913e
+  return err;
80913e
+}
80913e
+
80913e
+
80913e
+/*
80913e
+ * RFC 5280:
80913e
+ *   SubjectPublicKeyInfo  ::=  SEQUENCE  {
80913e
+ *       algorithm            AlgorithmIdentifier,
80913e
+ *       subjectPublicKey     BIT STRING  }
80913e
+ *
80913e
+ * AlgorithmIdentifiers come from RFC 3279, we are not strictly compilant as we
80913e
+ * only support RSA Encryption.
80913e
+ */
80913e
+
80913e
+static grub_err_t
80913e
+grub_x509_read_subject_public_key (asn1_node asn,
80913e
+				   struct x509_certificate *results)
80913e
+{
80913e
+  int result;
80913e
+  grub_err_t err;
80913e
+  const char *algo_name =
80913e
+    "tbsCertificate.subjectPublicKeyInfo.algorithm.algorithm";
80913e
+  const char *params_name =
80913e
+    "tbsCertificate.subjectPublicKeyInfo.algorithm.parameters";
80913e
+  const char *pk_name =
80913e
+    "tbsCertificate.subjectPublicKeyInfo.subjectPublicKey";
80913e
+  char algo_oid[MAX_OID_LEN];
80913e
+  int algo_size = sizeof (algo_oid);
80913e
+  char params_value[2];
80913e
+  int params_size = sizeof (params_value);
80913e
+  grub_uint8_t *key_data = NULL;
80913e
+  int key_size = 0;
80913e
+  unsigned int key_type;
80913e
+
80913e
+  /* algorithm: see notes for rsaEncryption_oid */
80913e
+  result = asn1_read_value (asn, algo_name, algo_oid, &algo_size);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Error reading x509 public key algorithm: %s",
80913e
+			 asn1_strerror (result));
80913e
+    }
80913e
+
80913e
+  if (grub_strncmp (algo_oid, rsaEncryption_oid, sizeof (rsaEncryption_oid))
80913e
+      != 0)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
80913e
+			 "Unsupported x509 public key algorithm: %s",
80913e
+			 algo_oid);
80913e
+    }
80913e
+
80913e
+  /* 
80913e
+   * RFC 3279 2.3.1
80913e
+   * The rsaEncryption OID is intended to be used in the algorithm field
80913e
+   * of a value of type AlgorithmIdentifier.  The parameters field MUST
80913e
+   * have ASN.1 type NULL for this algorithm identifier.
80913e
+   */
80913e
+  result = asn1_read_value (asn, params_name, params_value, &params_size);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Error reading x509 public key parameters: %s",
80913e
+			 asn1_strerror (result));
80913e
+    }
80913e
+
80913e
+  if (params_value[0] != ASN1_TAG_NULL)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Invalid x509 public key parameters: expected NULL");
80913e
+    }
80913e
+
80913e
+  /*
80913e
+   * RFC 3279 2.3.1:  The DER encoded RSAPublicKey is the value of the BIT
80913e
+   * STRING subjectPublicKey.
80913e
+   */
80913e
+  result = asn1_read_value_type (asn, pk_name, NULL, &key_size, &key_type);
80913e
+  if (result != ASN1_MEM_ERROR)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Error reading size of x509 public key: %s",
80913e
+			 asn1_strerror (result));
80913e
+    }
80913e
+  if (key_type != ASN1_ETYPE_BIT_STRING)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Unexpected ASN.1 type when reading x509 public key: %x",
80913e
+			 key_type);
80913e
+    }
80913e
+
80913e
+  /* length is in bits */
80913e
+  key_size = (key_size + 7) / 8;
80913e
+
80913e
+  key_data = grub_malloc (key_size);
80913e
+  if (!key_data)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			 "Out of memory for x509 public key");
80913e
+    }
80913e
+
80913e
+  result = asn1_read_value (asn, pk_name, key_data, &key_size);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      grub_free (key_data);
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Error reading public key data");
80913e
+    }
80913e
+  key_size = (key_size + 7) / 8;
80913e
+
80913e
+  err = grub_parse_rsa_pubkey (key_data, key_size, results);
80913e
+  grub_free (key_data);
80913e
+
80913e
+  return err;
80913e
+}
80913e
+
80913e
+/* Decode a string as defined in Appendix A */
80913e
+static grub_err_t
80913e
+decode_string (char *der, int der_size, char **string,
80913e
+	       grub_size_t * string_size)
80913e
+{
80913e
+  asn1_node strasn;
80913e
+  int result;
80913e
+  char *choice;
80913e
+  int choice_size = 0;
80913e
+  int tmp_size = 0;
80913e
+  grub_err_t err = GRUB_ERR_NONE;
80913e
+
80913e
+  result =
80913e
+    asn1_create_element (_gnutls_pkix_asn, "PKIX1.DirectoryString", &strasn);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			 "Could not create ASN.1 structure for certificate: %s",
80913e
+			 asn1_strerror (result));
80913e
+    }
80913e
+
80913e
+  result = asn1_der_decoding2 (&strasn, der, &der_size,
80913e
+			       ASN1_DECODE_FLAG_STRICT_DER, asn1_error);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Could not parse DER for DirectoryString: %s",
80913e
+		    asn1_error);
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  choice =
80913e
+    grub_asn1_allocate_and_read (strasn, "", "DirectoryString choice",
80913e
+				 &choice_size);
80913e
+  if (!choice)
80913e
+    {
80913e
+      err = grub_errno;
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  if (grub_strncmp ("utf8String", choice, choice_size) == 0)
80913e
+    {
80913e
+      result = asn1_read_value (strasn, "utf8String", NULL, &tmp_size);
80913e
+      if (result != ASN1_MEM_ERROR)
80913e
+	{
80913e
+	  err =
80913e
+	    grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			"Error reading size of UTF-8 string: %s",
80913e
+			asn1_strerror (result));
80913e
+	  goto cleanup_choice;
80913e
+	}
80913e
+    }
80913e
+  else if (grub_strncmp("printableString", choice, choice_size) == 0)
80913e
+    {
80913e
+      result = asn1_read_value (strasn, "printableString", NULL, &tmp_size);
80913e
+      if (result != ASN1_MEM_ERROR)
80913e
+	{
80913e
+	  err =
80913e
+	    grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			"Error reading size of UTF-8 string: %s",
80913e
+			asn1_strerror (result));
80913e
+	  goto cleanup_choice;
80913e
+	}
80913e
+    }
80913e
+  else
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
80913e
+		    "Only UTF-8 and printable DirectoryStrings are supported, got %s",
80913e
+		    choice);
80913e
+      goto cleanup_choice;
80913e
+    }
80913e
+
80913e
+  /* read size does not include trailing null */
80913e
+  tmp_size++;
80913e
+
80913e
+  *string = grub_malloc (tmp_size);
80913e
+  if (!*string)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+		    "Cannot allocate memory for DirectoryString contents");
80913e
+      goto cleanup_choice;
80913e
+    }
80913e
+
80913e
+  result = asn1_read_value (strasn, choice, *string, &tmp_size);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Error reading out %s in DirectoryString: %s",
80913e
+		    choice, asn1_strerror (result));
80913e
+      grub_free (*string);
80913e
+      goto cleanup_choice;
80913e
+    }
80913e
+  *string_size = tmp_size + 1;
80913e
+  (*string)[tmp_size] = '\0';
80913e
+
80913e
+cleanup_choice:
80913e
+  grub_free (choice);
80913e
+cleanup:
80913e
+  asn1_delete_structure (&strasn);
80913e
+  return err;
80913e
+}
80913e
+
80913e
+/*
80913e
+ * TBSCertificate  ::=  SEQUENCE  {
80913e
+ *       version         [0]  EXPLICIT Version DEFAULT v1,
80913e
+ * ...
80913e
+ * 
80913e
+ * Version  ::=  INTEGER  {  v1(0), v2(1), v3(2)  }
80913e
+ */
80913e
+static grub_err_t
80913e
+check_version (asn1_node certificate)
80913e
+{
80913e
+  int rc;
80913e
+  const char *name = "tbsCertificate.version";
80913e
+  grub_uint8_t version;
80913e
+  int len = 1;
80913e
+
80913e
+  rc = asn1_read_value (certificate, name, &version, &len;;
80913e
+
80913e
+  /* require version 3 */
80913e
+  if (rc != ASN1_SUCCESS || len != 1)
80913e
+    return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		       "Error reading certificate version");
80913e
+
80913e
+  if (version != 0x02)
80913e
+    return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		       "Invalid x509 certificate version, expected v3 (0x02), got 0x%02x",
80913e
+		       version);
80913e
+
80913e
+  return GRUB_ERR_NONE;
80913e
+}
80913e
+
80913e
+/*
80913e
+ * This is an X.501 Name, which is complex.
80913e
+ *
80913e
+ * For simplicity, we extract only the CN.
80913e
+ */
80913e
+static grub_err_t
80913e
+read_name (asn1_node asn, const char *name_path, char **name,
80913e
+	   grub_size_t * name_size)
80913e
+{
80913e
+  int seq_components, set_components;
80913e
+  int result;
80913e
+  int i, j;
80913e
+  char *top_path, *set_path, *type_path, *val_path;
80913e
+  char type[MAX_OID_LEN];
80913e
+  int type_len = sizeof (type);
80913e
+  int string_size = 0;
80913e
+  char *string_der;
80913e
+  grub_err_t err;
80913e
+
80913e
+  *name = NULL;
80913e
+
80913e
+  top_path = grub_xasprintf ("%s.rdnSequence", name_path);
80913e
+  if (!top_path)
80913e
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+		       "Could not allocate memory for %s name parsing path",
80913e
+		       name_path);
80913e
+
80913e
+  result = asn1_number_of_elements (asn, top_path, &seq_components);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Error counting name components: %s",
80913e
+		    asn1_strerror (result));
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  for (i = 1; i <= seq_components; i++)
80913e
+    {
80913e
+      set_path = grub_xasprintf ("%s.?%d", top_path, i);
80913e
+      if (!set_path)
80913e
+	{
80913e
+	  err =
80913e
+	    grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			"Could not allocate memory for %s name set parsing path",
80913e
+			name_path);
80913e
+	  goto cleanup_set;
80913e
+	}
80913e
+      /* this brings us, hopefully, to a set */
80913e
+      result = asn1_number_of_elements (asn, set_path, &set_components);
80913e
+      if (result != ASN1_SUCCESS)
80913e
+	{
80913e
+	  err =
80913e
+	    grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			"Error counting name sub-components components (element %d): %s",
80913e
+			i, asn1_strerror (result));
80913e
+	  goto cleanup_set;
80913e
+	}
80913e
+      for (j = 1; j <= set_components; j++)
80913e
+	{
80913e
+	  type_path = grub_xasprintf ("%s.?%d.?%d.type", top_path, i, j);
80913e
+	  if (!type_path)
80913e
+	    {
80913e
+	      err =
80913e
+		grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			    "Could not allocate memory for %s name component type path",
80913e
+			    name_path);
80913e
+	      goto cleanup_set;
80913e
+	    }
80913e
+	  type_len = sizeof (type);
80913e
+	  result = asn1_read_value (asn, type_path, type, &type_len);
80913e
+	  if (result != ASN1_SUCCESS)
80913e
+	    {
80913e
+	      err =
80913e
+		grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			    "Error reading %s name component type: %s",
80913e
+			    name_path, asn1_strerror (result));
80913e
+	      goto cleanup_type;
80913e
+	    }
80913e
+
80913e
+	  if (grub_strncmp (type, commonName_oid, type_len) != 0)
80913e
+	    {
80913e
+	      grub_free (type_path);
80913e
+	      continue;
80913e
+	    }
80913e
+
80913e
+	  val_path = grub_xasprintf ("%s.?%d.?%d.value", top_path, i, j);
80913e
+	  if (!val_path)
80913e
+	    {
80913e
+	      err =
80913e
+		grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			    "Could not allocate memory for %s name component value path",
80913e
+			    name_path);
80913e
+	      goto cleanup_set;
80913e
+	    }
80913e
+
80913e
+	  string_der =
80913e
+	    grub_asn1_allocate_and_read (asn, val_path, name_path,
80913e
+					 &string_size);
80913e
+	  if (!string_der)
80913e
+	    {
80913e
+	      err = grub_errno;
80913e
+	      goto cleanup_val_path;
80913e
+	    }
80913e
+
80913e
+	  err = decode_string (string_der, string_size, name, name_size);
80913e
+	  if (err)
80913e
+	    goto cleanup_string;
80913e
+
80913e
+	  grub_free (string_der);
80913e
+	  grub_free (type_path);
80913e
+	  grub_free (val_path);
80913e
+	  break;
80913e
+	}
80913e
+      grub_free (set_path);
80913e
+
80913e
+      if (*name)
80913e
+	break;
80913e
+    }
80913e
+
80913e
+  return GRUB_ERR_NONE;
80913e
+
80913e
+cleanup_string:
80913e
+  grub_free (string_der);
80913e
+cleanup_val_path:
80913e
+  grub_free (val_path);
80913e
+cleanup_type:
80913e
+  grub_free (type_path);
80913e
+cleanup_set:
80913e
+  grub_free (set_path);
80913e
+cleanup:
80913e
+  grub_free (top_path);
80913e
+  return err;
80913e
+}
80913e
+
80913e
+/*
80913e
+ * details here
80913e
+ */
80913e
+static grub_err_t
80913e
+verify_key_usage (grub_uint8_t * value, int value_size)
80913e
+{
80913e
+  asn1_node usageasn;
80913e
+  int result;
80913e
+  grub_err_t err = GRUB_ERR_NONE;
80913e
+  grub_uint8_t usage = 0xff;
80913e
+  int usage_size = 1;
80913e
+
80913e
+  result =
80913e
+    asn1_create_element (_gnutls_pkix_asn, "PKIX1.KeyUsage", &usageasn);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			 "Could not create ASN.1 structure for key usage");
80913e
+    }
80913e
+
80913e
+  result = asn1_der_decoding2 (&usageasn, value, &value_size,
80913e
+			       ASN1_DECODE_FLAG_STRICT_DER, asn1_error);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Error parsing DER for Key Usage: %s", asn1_error);
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  result = asn1_read_value (usageasn, "", &usage, &usage_size);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Error reading Key Usage value: %s",
80913e
+		    asn1_strerror (result));
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  /* Only the first bit is permitted to be set */
80913e
+  if (usage != 0x80)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE, "Unexpected Key Usage value: %x",
80913e
+		    usage);
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+cleanup:
80913e
+  asn1_delete_structure (&usageasn);
80913e
+  return err;
80913e
+}
80913e
+
80913e
+/*
80913e
+ * BasicConstraints ::= SEQUENCE {
80913e
+ *       cA                      BOOLEAN DEFAULT FALSE,
80913e
+ *       pathLenConstraint       INTEGER (0..MAX) OPTIONAL }
80913e
+ */
80913e
+static grub_err_t
80913e
+verify_basic_constraints (grub_uint8_t * value, int value_size)
80913e
+{
80913e
+  asn1_node basicasn;
80913e
+  int result;
80913e
+  grub_err_t err = GRUB_ERR_NONE;
80913e
+  char cA[6];			/* FALSE or TRUE */
80913e
+  int cA_size = sizeof (cA);
80913e
+
80913e
+  result =
80913e
+    asn1_create_element (_gnutls_pkix_asn, "PKIX1.BasicConstraints",
80913e
+			 &basicasn);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			 "Could not create ASN.1 structure for Basic Constraints");
80913e
+    }
80913e
+
80913e
+  result = asn1_der_decoding2 (&basicasn, value, &value_size,
80913e
+			       ASN1_DECODE_FLAG_STRICT_DER, asn1_error);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Error parsing DER for Basic Constraints: %s",
80913e
+		    asn1_error);
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  result = asn1_read_value (basicasn, "cA", cA, &cA_size);
80913e
+  if (result == ASN1_ELEMENT_NOT_FOUND)
80913e
+    {
80913e
+      /* Not present, default is False, so this is OK */
80913e
+      err = GRUB_ERR_NONE;
80913e
+      goto cleanup;
80913e
+    }
80913e
+  else if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Error reading Basic Constraints cA value: %s",
80913e
+		    asn1_strerror (result));
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  /* The certificate must not be a CA certificate */
80913e
+  if (grub_strncmp ("FALSE", cA, cA_size) != 0)
80913e
+    {
80913e
+      err = grub_error (GRUB_ERR_BAD_FILE_TYPE, "Unexpected CA value: %s",
80913e
+			cA);
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+cleanup:
80913e
+  asn1_delete_structure (&basicasn);
80913e
+  return err;
80913e
+}
80913e
+
80913e
+
80913e
+/*
80913e
+ * Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension
80913e
+ *
80913e
+ * Extension  ::=  SEQUENCE  {
80913e
+ *      extnID      OBJECT IDENTIFIER,
80913e
+ *      critical    BOOLEAN DEFAULT FALSE,
80913e
+ *      extnValue   OCTET STRING
80913e
+ *                  -- contains the DER encoding of an ASN.1 value
80913e
+ *                  -- corresponding to the extension type identified
80913e
+ *                  -- by extnID
80913e
+ * }
80913e
+ *
80913e
+ * We require that a certificate:
80913e
+ *  - contain the Digital Signature usage only
80913e
+ *  - not be a CA
80913e
+ *  - MUST not contain any other critical extensions (RFC 5280 s 4.2)
80913e
+ */
80913e
+static grub_err_t
80913e
+verify_extensions (asn1_node cert)
80913e
+{
80913e
+  int result;
80913e
+  int ext, num_extensions = 0;
80913e
+  int usage_present = 0, constraints_present = 0;
80913e
+  char *oid_path, *critical_path, *value_path;
80913e
+  char extnID[MAX_OID_LEN];
80913e
+  int extnID_size;
80913e
+  grub_err_t err;
80913e
+  char critical[6];		/* we get either "TRUE" or "FALSE" */
80913e
+  int critical_size;
80913e
+  grub_uint8_t *value;
80913e
+  int value_size;
80913e
+
80913e
+  result =
80913e
+    asn1_number_of_elements (cert, "tbsCertificate.extensions",
80913e
+			     &num_extensions);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Error counting number of extensions: %s",
80913e
+			 asn1_strerror (result));
80913e
+    }
80913e
+
80913e
+  if (num_extensions < 2)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Insufficient number of extensions for certificate, need at least 2, got %d",
80913e
+			 num_extensions);
80913e
+    }
80913e
+
80913e
+  for (ext = 1; ext <= num_extensions; ext++)
80913e
+    {
80913e
+      oid_path = grub_xasprintf ("tbsCertificate.extensions.?%d.extnID", ext);
80913e
+
80913e
+      extnID_size = sizeof (extnID);
80913e
+      result = asn1_read_value (cert, oid_path, extnID, &extnID_size);
80913e
+      if (result != GRUB_ERR_NONE)
80913e
+	{
80913e
+	  err =
80913e
+	    grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			"Error reading extension OID: %s",
80913e
+			asn1_strerror (result));
80913e
+	  goto cleanup_oid_path;
80913e
+	}
80913e
+
80913e
+      critical_path =
80913e
+	grub_xasprintf ("tbsCertificate.extensions.?%d.critical", ext);
80913e
+      critical_size = sizeof (critical);
80913e
+      result =
80913e
+	asn1_read_value (cert, critical_path, critical, &critical_size);
80913e
+      if (result == ASN1_ELEMENT_NOT_FOUND)
80913e
+	{
80913e
+	  critical[0] = '\0';
80913e
+	}
80913e
+      else if (result != ASN1_SUCCESS)
80913e
+	{
80913e
+	  err =
80913e
+	    grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			"Error reading extension criticality: %s",
80913e
+			asn1_strerror (result));
80913e
+	  goto cleanup_critical_path;
80913e
+	}
80913e
+
80913e
+      value_path =
80913e
+	grub_xasprintf ("tbsCertificate.extensions.?%d.extnValue", ext);
80913e
+      value =
80913e
+	grub_asn1_allocate_and_read (cert, value_path,
80913e
+				     "certificate extension value",
80913e
+				     &value_size);
80913e
+      if (!value)
80913e
+	{
80913e
+	  err = grub_errno;
80913e
+	  goto cleanup_value_path;
80913e
+	}
80913e
+
80913e
+      /*
80913e
+       * Now we must see if we recognise the OID.
80913e
+       * If we have an unrecognised critical extension we MUST bail.
80913e
+       */
80913e
+      if (grub_strncmp (keyUsage_oid, extnID, extnID_size) == 0)
80913e
+	{
80913e
+	  err = verify_key_usage (value, value_size);
80913e
+	  if (err != GRUB_ERR_NONE)
80913e
+	    {
80913e
+	      goto cleanup_value;
80913e
+	    }
80913e
+	  usage_present++;
80913e
+	}
80913e
+      else if (grub_strncmp (basicConstraints_oid, extnID, extnID_size) == 0)
80913e
+	{
80913e
+	  err = verify_basic_constraints (value, value_size);
80913e
+	  if (err != GRUB_ERR_NONE)
80913e
+	    {
80913e
+	      goto cleanup_value;
80913e
+	    }
80913e
+	  constraints_present++;
80913e
+	}
80913e
+      else if (grub_strncmp ("TRUE", critical, critical_size) == 0)
80913e
+	{
80913e
+	  /*
80913e
+	   * per the RFC, we must not process a certificate with
80913e
+	   * a critical extension we do not understand.
80913e
+	   */
80913e
+	  err =
80913e
+	    grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			"Unhandled critical x509 extension with OID %s",
80913e
+			extnID);
80913e
+	  goto cleanup_value;
80913e
+	}
80913e
+
80913e
+      grub_free (value);
80913e
+      grub_free (value_path);
80913e
+      grub_free (critical_path);
80913e
+      grub_free (oid_path);
80913e
+    }
80913e
+
80913e
+  if (usage_present != 1)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Unexpected number of Key Usage extensions - expected 1, got %d",
80913e
+			 usage_present);
80913e
+    }
80913e
+  if (constraints_present != 1)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+			 "Unexpected number of basic constraints extensions - expected 1, got %d",
80913e
+			 constraints_present);
80913e
+    }
80913e
+  return GRUB_ERR_NONE;
80913e
+
80913e
+cleanup_value:
80913e
+  grub_free (value);
80913e
+cleanup_value_path:
80913e
+  grub_free (value_path);
80913e
+cleanup_critical_path:
80913e
+  grub_free (critical_path);
80913e
+cleanup_oid_path:
80913e
+  grub_free (oid_path);
80913e
+  return err;
80913e
+}
80913e
+
80913e
+/*
80913e
+ * Parse a certificate whose DER-encoded form is in @data, of size @data_size.
80913e
+ * Return the results in @results, which must point to an allocated x509 certificate.
80913e
+ */
80913e
+grub_err_t
80913e
+certificate_import (void *data, grub_size_t data_size,
80913e
+		    struct x509_certificate *results)
80913e
+{
80913e
+  int result = 0;
80913e
+  asn1_node cert;
80913e
+  grub_err_t err;
80913e
+  int size;
80913e
+  int tmp_size;
80913e
+
80913e
+  if (data_size > GRUB_INT_MAX)
80913e
+    return grub_error (GRUB_ERR_OUT_OF_RANGE,
80913e
+		       "Cannot parse a certificate where data size > INT_MAX");
80913e
+  size = (int) data_size;
80913e
+
80913e
+  result = asn1_create_element (_gnutls_pkix_asn, "PKIX1.Certificate", &cert);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
80913e
+			 "Could not create ASN.1 structure for certificate: %s",
80913e
+			 asn1_strerror (result));
80913e
+    }
80913e
+
80913e
+  result = asn1_der_decoding2 (&cert, data, &size,
80913e
+			       ASN1_DECODE_FLAG_STRICT_DER, asn1_error);
80913e
+  if (result != ASN1_SUCCESS)
80913e
+    {
80913e
+      err =
80913e
+	grub_error (GRUB_ERR_BAD_FILE_TYPE,
80913e
+		    "Could not parse DER for certificate: %s", asn1_error);
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  /* 
80913e
+   * TBSCertificate  ::=  SEQUENCE {
80913e
+   *     version         [0]  EXPLICIT Version DEFAULT v1
80913e
+   */
80913e
+  err = check_version (cert);
80913e
+  if (err != GRUB_ERR_NONE)
80913e
+    {
80913e
+      goto cleanup;
80913e
+    }
80913e
+
80913e
+  /*
80913e
+   * serialNumber         CertificateSerialNumber,
80913e
+   *
80913e
+   * CertificateSerialNumber  ::=  INTEGER
80913e
+   */
80913e
+  results->serial =
80913e
+    grub_asn1_allocate_and_read (cert, "tbsCertificate.serialNumber",
80913e
+				 "certificate serial number", &tmp_size);
80913e
+  if (!results->serial)
80913e
+    {
80913e
+      err = grub_errno;
80913e
+      goto cleanup;
80913e
+    }
80913e
+  /*
80913e
+   * It's safe to cast the signed int to an unsigned here, we know
80913e
+   * length is non-negative
80913e
+   */
80913e
+  results->serial_len = tmp_size;
80913e
+
80913e
+  /* 
80913e
+   * signature            AlgorithmIdentifier,
80913e
+   *
80913e
+   * We don't load the signature or issuer at the moment,
80913e
+   * as we don't attempt x509 verification.
80913e
+   */
80913e
+
80913e
+  /*
80913e
+   * issuer               Name,
80913e
+   *
80913e
+   * The RFC only requires the serial number to be unique within
80913e
+   * issuers, so to avoid ambiguity we _technically_ ought to make
80913e
+   * this available.
80913e
+   */
80913e
+
80913e
+  /*
80913e
+   * validity             Validity,
80913e
+   *
80913e
+   * Validity ::= SEQUENCE {
80913e
+   *     notBefore      Time,
80913e
+   *     notAfter       Time }
80913e
+   *
80913e
+   * We can't validate this reasonably, we have no true time source on several
80913e
+   * platforms. For now we do not parse them.
80913e
+   */
80913e
+
80913e
+  /*
80913e
+   * subject              Name,
80913e
+   * 
80913e
+   * This is an X501 name, we parse out just the CN.
80913e
+   */
80913e
+  err =
80913e
+    read_name (cert, "tbsCertificate.subject", &results->subject,
80913e
+	       &results->subject_len);
80913e
+  if (err != GRUB_ERR_NONE)
80913e
+    goto cleanup_serial;
80913e
+
80913e
+  /*
80913e
+   * TBSCertificate  ::=  SEQUENCE  {
80913e
+   *    ...
80913e
+   *    subjectPublicKeyInfo SubjectPublicKeyInfo,
80913e
+   *    ...
80913e
+   */
80913e
+  err = grub_x509_read_subject_public_key (cert, results);
80913e
+  if (err != GRUB_ERR_NONE)
80913e
+    goto cleanup_name;
80913e
+
80913e
+  /*
80913e
+   * TBSCertificate  ::=  SEQUENCE  {
80913e
+   *    ...
80913e
+   *    extensions      [3]  EXPLICIT Extensions OPTIONAL
80913e
+   *                         -- If present, version MUST be v3
80913e
+   * }
80913e
+   */
80913e
+
80913e
+  err = verify_extensions (cert);
80913e
+  if (err != GRUB_ERR_NONE)
80913e
+    goto cleanup_name;
80913e
+
80913e
+
80913e
+  /*
80913e
+   * We do not read or check the signature on the certificate:
80913e
+   * as discussed we do not try to validate the certificate but trust
80913e
+   * it implictly.
80913e
+   */
80913e
+
80913e
+  asn1_delete_structure (&cert);
80913e
+  return GRUB_ERR_NONE;
80913e
+
80913e
+
80913e
+cleanup_name:
80913e
+  grub_free (results->subject);
80913e
+cleanup_serial:
80913e
+  grub_free (results->serial);
80913e
+cleanup:
80913e
+  asn1_delete_structure (&cert);
80913e
+  return err;
80913e
+}
80913e
+
80913e
+/*
80913e
+ * Release all the storage associated with the x509 certificate.
80913e
+ * If the caller dynamically allocated the certificate, it must free it.
80913e
+ * The caller is also responsible for maintenance of the linked list.
80913e
+ */
80913e
+void
80913e
+certificate_release (struct x509_certificate *cert)
80913e
+{
80913e
+  grub_free (cert->subject);
80913e
+  grub_free (cert->serial);
80913e
+  gcry_mpi_release (cert->mpis[0]);
80913e
+  gcry_mpi_release (cert->mpis[1]);
80913e
+}
80913e
diff --git a/grub-core/commands/appendedsig/appendedsig.h b/grub-core/commands/appendedsig/appendedsig.h
80913e
new file mode 100644
80913e
index 00000000000..9792ef3901e
80913e
--- /dev/null
80913e
+++ b/grub-core/commands/appendedsig/appendedsig.h
80913e
@@ -0,0 +1,110 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2020  IBM Corporation.
80913e
+ *
80913e
+ *  GRUB is free software: you can redistribute it and/or modify
80913e
+ *  it under the terms of the GNU General Public License as published by
80913e
+ *  the Free Software Foundation, either version 3 of the License, or
80913e
+ *  (at your option) any later version.
80913e
+ *
80913e
+ *  GRUB is distributed in the hope that it will be useful,
80913e
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
80913e
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80913e
+ *  GNU General Public License for more details.
80913e
+ *
80913e
+ *  You should have received a copy of the GNU General Public License
80913e
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
+ */
80913e
+
80913e
+#include <grub/crypto.h>
80913e
+#include <grub/libtasn1.h>
80913e
+
80913e
+extern asn1_node _gnutls_gnutls_asn;
80913e
+extern asn1_node _gnutls_pkix_asn;
80913e
+
80913e
+#define MAX_OID_LEN 32
80913e
+
80913e
+/*
80913e
+ * One or more x509 certificates.
80913e
+ *
80913e
+ * We do limited parsing: extracting only the serial, CN and RSA public key.
80913e
+ */
80913e
+struct x509_certificate
80913e
+{
80913e
+  struct x509_certificate *next;
80913e
+
80913e
+  grub_uint8_t *serial;
80913e
+  grub_size_t serial_len;
80913e
+
80913e
+  char *subject;
80913e
+  grub_size_t subject_len;
80913e
+
80913e
+  /* We only support RSA public keys. This encodes [modulus, publicExponent] */
80913e
+  gcry_mpi_t mpis[2];
80913e
+};
80913e
+
80913e
+/*
80913e
+ * A PKCS#7 signedData message.
80913e
+ *
80913e
+ * We make no attempt to match intelligently, so we don't save any info about
80913e
+ * the signer. We also support only 1 signerInfo, so we only store a single
80913e
+ * MPI for the signature.
80913e
+ */
80913e
+struct pkcs7_signedData
80913e
+{
80913e
+  const gcry_md_spec_t *hash;
80913e
+  gcry_mpi_t sig_mpi;
80913e
+};
80913e
+
80913e
+
80913e
+/* Do libtasn1 init */
80913e
+int asn1_init (void);
80913e
+
80913e
+/*
80913e
+ * Import a DER-encoded certificate at 'data', of size 'size'.
80913e
+ *
80913e
+ * Place the results into 'results', which must be already allocated.
80913e
+ */
80913e
+grub_err_t
80913e
+certificate_import (void *data, grub_size_t size,
80913e
+		    struct x509_certificate *results);
80913e
+
80913e
+/*
80913e
+ * Release all the storage associated with the x509 certificate.
80913e
+ * If the caller dynamically allocated the certificate, it must free it.
80913e
+ * The caller is also responsible for maintenance of the linked list.
80913e
+ */
80913e
+void certificate_release (struct x509_certificate *cert);
80913e
+
80913e
+/*
80913e
+ * Parse a PKCS#7 message, which must be a signedData message.
80913e
+ *
80913e
+ * The message must be in 'sigbuf' and of size 'data_size'. The result is
80913e
+ * placed in 'msg', which must already be allocated.
80913e
+ */
80913e
+grub_err_t
80913e
+parse_pkcs7_signedData (void *sigbuf, grub_size_t data_size,
80913e
+			struct pkcs7_signedData *msg);
80913e
+
80913e
+/*
80913e
+ * Release all the storage associated with the PKCS#7 message.
80913e
+ * If the caller dynamically allocated the message, it must free it.
80913e
+ */
80913e
+void pkcs7_signedData_release (struct pkcs7_signedData *msg);
80913e
+
80913e
+/*
80913e
+ * Read a value from an ASN1 node, allocating memory to store it.
80913e
+ *
80913e
+ * It will work for anything where the size libtasn1 returns is right:
80913e
+ *  - Integers
80913e
+ *  - Octet strings
80913e
+ *  - DER encoding of other structures
80913e
+ * It will _not_ work for things where libtasn1 size requires adjustment:
80913e
+ *  - Strings that require an extra NULL byte at the end
80913e
+ *  - Bit strings because libtasn1 returns the length in bits, not bytes.
80913e
+ *
80913e
+ * If the function returns a non-NULL value, the caller must free it.
80913e
+ */
80913e
+void *grub_asn1_allocate_and_read (asn1_node node, const char *name,
80913e
+				   const char *friendly_name,
80913e
+				   int *content_size);