Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1200760

[PATCH 22/27] extract-cert: Cope with multiple X.509 certificates in a single file [ver #7]

From David Howells <dhowells@redhat.com>
Newsgroups linux.kernel
Subject [PATCH 22/27] extract-cert: Cope with multiple X.509 certificates in a single file [ver #7]
Date 2015-08-05 15:50 +0200
Message-ID <pU7eV-2WR-13@gated-at.bofh.it> (permalink)
References <pU7eV-2WR-7@gated-at.bofh.it>
Organization Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903

Show all headers | View raw


From: David Woodhouse <David.Woodhouse@intel.com>

This is not required for the module signing key, although it doesn't do any
harm — it just means that any additional certs in the PEM file are also
trusted by the kernel.

But it does allow us to use the extract-cert tool for processing the extra
certs from CONFIG_SYSTEM_TRUSTED_KEYS, instead of that horrid awk|base64
hack.

Also cope with being invoked with no input file, creating an empty output
file as a result.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
Signed-off-by: David Howells <dhowells@redhat.com>
---

 scripts/extract-cert.c |   58 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 46 insertions(+), 12 deletions(-)

diff --git a/scripts/extract-cert.c b/scripts/extract-cert.c
index 4fd5b2f07b45..fd0db015c65c 100644
--- a/scripts/extract-cert.c
+++ b/scripts/extract-cert.c
@@ -73,17 +73,34 @@ static void drain_openssl_errors(void)
 	} while(0)
 
 static const char *key_pass;
+static BIO *wb;
+static char *cert_dst;
+int kbuild_verbose;
+
+static void write_cert(X509 *x509)
+{
+	char buf[200];
+
+	if (!wb) {
+		wb = BIO_new_file(cert_dst, "wb");
+		ERR(!wb, "%s", cert_dst);
+	}
+	X509_NAME_oneline(X509_get_subject_name(x509), buf, sizeof(buf));
+	ERR(!i2d_X509_bio(wb, x509), cert_dst);
+	if (kbuild_verbose)
+		fprintf(stderr, "Extracted cert: %s\n", buf);
+}
 
 int main(int argc, char **argv)
 {
-	char *cert_src, *cert_dst;
-	X509 *x509;
-	BIO *b;
+	char *cert_src;
 
 	OpenSSL_add_all_algorithms();
 	ERR_load_crypto_strings();
 	ERR_clear_error();
 
+	kbuild_verbose = atoi(getenv("KBUILD_VERBOSE")?:"0");
+
         key_pass = getenv("KBUILD_SIGN_PIN");
 
 	if (argc != 3)
@@ -92,7 +109,13 @@ int main(int argc, char **argv)
 	cert_src = argv[1];
 	cert_dst = argv[2];
 
-	if (!strncmp(cert_src, "pkcs11:", 7)) {
+	if (!cert_src[0]) {
+		/* Invoked with no input; create empty file */
+		FILE *f = fopen(cert_dst, "wb");
+		ERR(!f, "%s", cert_dst);
+		fclose(f);
+		exit(0);
+	} else if (!strncmp(cert_src, "pkcs11:", 7)) {
 		ENGINE *e;
 		struct {
 			const char *cert_id;
@@ -114,19 +137,30 @@ int main(int argc, char **argv)
 			ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
 		ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1);
 		ERR(!parms.cert, "Get X.509 from PKCS#11");
-		x509 = parms.cert;
+		write_cert(parms.cert);
 	} else {
+		BIO *b;
+		X509 *x509;
+
 		b = BIO_new_file(cert_src, "rb");
 		ERR(!b, "%s", cert_src);
-		x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
-		ERR(!x509, "%s", cert_src);
-		BIO_free(b);
+
+		while (1) {
+			x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
+			if (wb && !x509) {
+				unsigned long err = ERR_peek_last_error();
+				if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
+				    ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
+					ERR_clear_error();
+					break;
+				}
+			}
+			ERR(!x509, "%s", cert_src);
+			write_cert(x509);
+		}
 	}
 
-	b = BIO_new_file(cert_dst, "wb");
-	ERR(!b, "%s", cert_dst);
-	ERR(!i2d_X509_bio(b, x509), cert_dst);
-	BIO_free(b);
+	BIO_free(wb);
 
 	return 0;
 }

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 00/27] MODSIGN: Use PKCS#7 for module signatures [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 10/27] MODSIGN: Extract the blob PKCS#7 signature verifier  from module signing [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 22/27] extract-cert: Cope with multiple X.509 certificates  in a single file [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 16/27] modsign: Use single PEM file for autogenerated key  [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 21/27] sign-file: Generate CMS message as signature instead  of PKCS#7 [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 01/27] ASN.1: Add an ASN.1 compiler option to dump the  element tree [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 18/27] PKCS#7: Check content type and versions [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 11/27] modsign: Abort modules_install when signing fails  [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 04/27] X.509: Support X.509 lookup by Issuer+Serial form  AuthorityKeyIdentifier [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 19/27] X.509: Change recorded SKID & AKID to not include  Subject or Issuer [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 26/27] KEYS: Add a name for PKEY_ID_PKCS7 [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 06/27] MODSIGN: Provide a utility to append a PKCS#7  signature to a module [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 24/27] PKCS#7: Improve and export the X.509 ASN.1 time  object decoder [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 08/27] sign-file: Add option to only create signature file  [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 14/27] modsign: Allow external signing key to be specified  [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 17/27] modsign: Add explicit CONFIG_SYSTEM_TRUSTED_KEYS  option [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 23/27] modsign: Use extract-cert to process  CONFIG_SYSTEM_TRUSTED_KEYS [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 05/27] PKCS#7: Allow detached data to be supplied for  signature checking purposes [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 15/27] modsign: Extract signing cert from  CONFIG_MODULE_SIG_KEY if needed [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 07/27] MODSIGN: Use PKCS#7 messages as module signatures  [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 27/27] PKCS#7: Restrict content type and authenticated  attributes by purpose [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 02/27] ASN.1: Copy string names to tokens in ASN.1 compiler  [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
    Re: [PATCH 02/27] ASN.1: Copy string names to tokens in ASN.1  compiler [ver #7] Mimi Zohar <zohar@linux.vnet.ibm.com> - 2015-08-05 20:20 +0200
      Re: [PATCH 02/27] ASN.1: Copy string names to tokens in ASN.1 compiler [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 20:30 +0200
        Re: [PATCH 02/27] ASN.1: Copy string names to tokens in ASN.1  compiler [ver #7] Mimi Zohar <zohar@linux.vnet.ibm.com> - 2015-08-05 21:00 +0200
  [PATCH 25/27] PKCS#7: Appropriately require or forbid authenticated  attributes [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
    Re: [PATCH 25/27] PKCS#7: Appropriately require or forbid authenticated attributes [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 16:30 +0200
  [PATCH 20/27] PKCS#7: Support CMS messages also [RFC5652] [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 15:50 +0200
  [PATCH 13/27] modsign: Allow signing key to be PKCS#11 [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 16:00 +0200
  [PATCH 09/27] system_keyring.c doesn't need to #include  module-internal.h [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 16:00 +0200
  [PATCH 03/27] X.509: Extract both parts of the  AuthorityKeyIdentifier [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 16:00 +0200
  [PATCH 12/27] modsign: Allow password to be specified for signing  key [ver #7] David Howells <dhowells@redhat.com> - 2015-08-05 16:00 +0200

csiph-web