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