diff options
author | David Woodhouse <David.Woodhouse@intel.com> | 2015-07-20 21:16:33 +0100 |
---|---|---|
committer | David Howells <dhowells@redhat.com> | 2015-08-12 17:01:01 +0100 |
commit | 84706caae9e06363db4f956cde4f9715ce5c0ef3 (patch) | |
tree | 282821472d398da61e41035646ab9a20085db816 | |
parent | ed8c20762a314124cbdd62e9d3e8aa7aa2a16020 (diff) |
extract-cert: Cope with multiple X.509 certificates in a single file
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>
-rw-r--r-- | scripts/extract-cert.c | 58 |
1 files 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; } |