diff options
author | David Woodhouse <David.Woodhouse@intel.com> | 2015-07-20 16:16:33 -0400 |
---|---|---|
committer | David Howells <dhowells@redhat.com> | 2015-08-12 12:01:01 -0400 |
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) | |||
73 | } while(0) | 73 | } while(0) |
74 | 74 | ||
75 | static const char *key_pass; | 75 | static const char *key_pass; |
76 | static BIO *wb; | ||
77 | static char *cert_dst; | ||
78 | int kbuild_verbose; | ||
79 | |||
80 | static void write_cert(X509 *x509) | ||
81 | { | ||
82 | char buf[200]; | ||
83 | |||
84 | if (!wb) { | ||
85 | wb = BIO_new_file(cert_dst, "wb"); | ||
86 | ERR(!wb, "%s", cert_dst); | ||
87 | } | ||
88 | X509_NAME_oneline(X509_get_subject_name(x509), buf, sizeof(buf)); | ||
89 | ERR(!i2d_X509_bio(wb, x509), cert_dst); | ||
90 | if (kbuild_verbose) | ||
91 | fprintf(stderr, "Extracted cert: %s\n", buf); | ||
92 | } | ||
76 | 93 | ||
77 | int main(int argc, char **argv) | 94 | int main(int argc, char **argv) |
78 | { | 95 | { |
79 | char *cert_src, *cert_dst; | 96 | char *cert_src; |
80 | X509 *x509; | ||
81 | BIO *b; | ||
82 | 97 | ||
83 | OpenSSL_add_all_algorithms(); | 98 | OpenSSL_add_all_algorithms(); |
84 | ERR_load_crypto_strings(); | 99 | ERR_load_crypto_strings(); |
85 | ERR_clear_error(); | 100 | ERR_clear_error(); |
86 | 101 | ||
102 | kbuild_verbose = atoi(getenv("KBUILD_VERBOSE")?:"0"); | ||
103 | |||
87 | key_pass = getenv("KBUILD_SIGN_PIN"); | 104 | key_pass = getenv("KBUILD_SIGN_PIN"); |
88 | 105 | ||
89 | if (argc != 3) | 106 | if (argc != 3) |
@@ -92,7 +109,13 @@ int main(int argc, char **argv) | |||
92 | cert_src = argv[1]; | 109 | cert_src = argv[1]; |
93 | cert_dst = argv[2]; | 110 | cert_dst = argv[2]; |
94 | 111 | ||
95 | if (!strncmp(cert_src, "pkcs11:", 7)) { | 112 | if (!cert_src[0]) { |
113 | /* Invoked with no input; create empty file */ | ||
114 | FILE *f = fopen(cert_dst, "wb"); | ||
115 | ERR(!f, "%s", cert_dst); | ||
116 | fclose(f); | ||
117 | exit(0); | ||
118 | } else if (!strncmp(cert_src, "pkcs11:", 7)) { | ||
96 | ENGINE *e; | 119 | ENGINE *e; |
97 | struct { | 120 | struct { |
98 | const char *cert_id; | 121 | const char *cert_id; |
@@ -114,19 +137,30 @@ int main(int argc, char **argv) | |||
114 | ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN"); | 137 | ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN"); |
115 | ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1); | 138 | ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1); |
116 | ERR(!parms.cert, "Get X.509 from PKCS#11"); | 139 | ERR(!parms.cert, "Get X.509 from PKCS#11"); |
117 | x509 = parms.cert; | 140 | write_cert(parms.cert); |
118 | } else { | 141 | } else { |
142 | BIO *b; | ||
143 | X509 *x509; | ||
144 | |||
119 | b = BIO_new_file(cert_src, "rb"); | 145 | b = BIO_new_file(cert_src, "rb"); |
120 | ERR(!b, "%s", cert_src); | 146 | ERR(!b, "%s", cert_src); |
121 | x509 = PEM_read_bio_X509(b, NULL, NULL, NULL); | 147 | |
122 | ERR(!x509, "%s", cert_src); | 148 | while (1) { |
123 | BIO_free(b); | 149 | x509 = PEM_read_bio_X509(b, NULL, NULL, NULL); |
150 | if (wb && !x509) { | ||
151 | unsigned long err = ERR_peek_last_error(); | ||
152 | if (ERR_GET_LIB(err) == ERR_LIB_PEM && | ||
153 | ERR_GET_REASON(err) == PEM_R_NO_START_LINE) { | ||
154 | ERR_clear_error(); | ||
155 | break; | ||
156 | } | ||
157 | } | ||
158 | ERR(!x509, "%s", cert_src); | ||
159 | write_cert(x509); | ||
160 | } | ||
124 | } | 161 | } |
125 | 162 | ||
126 | b = BIO_new_file(cert_dst, "wb"); | 163 | BIO_free(wb); |
127 | ERR(!b, "%s", cert_dst); | ||
128 | ERR(!i2d_X509_bio(b, x509), cert_dst); | ||
129 | BIO_free(b); | ||
130 | 164 | ||
131 | return 0; | 165 | return 0; |
132 | } | 166 | } |