diff options
author | David Howells <dhowells@redhat.com> | 2015-07-20 16:16:33 -0400 |
---|---|---|
committer | David Howells <dhowells@redhat.com> | 2015-08-12 12:01:01 -0400 |
commit | ed8c20762a314124cbdd62e9d3e8aa7aa2a16020 (patch) | |
tree | c92b41d3ff5f5971061631459029edadae904df6 /scripts | |
parent | 60d65cacd7c2d84a6dcad69bcb57bbf0220c8643 (diff) |
sign-file: Generate CMS message as signature instead of PKCS#7
Make sign-file use the OpenSSL CMS routines to generate a message to be
used as the signature blob instead of the PKCS#7 routines. This allows us
to change how the matching X.509 certificate is selected. With PKCS#7 the
only option is to match on the serial number and issuer fields of an X.509
certificate; with CMS, we also have the option of matching by subjectKeyId
extension. The new behaviour is selected with the "-k" flag.
Without the -k flag specified, the output is pretty much identical to the
PKCS#7 output.
Whilst we're at it, don't include the S/MIME capability list in the message
as it's irrelevant to us.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-By: David Woodhouse <David.Woodhouse@intel.com
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/sign-file.c | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/scripts/sign-file.c b/scripts/sign-file.c index ad0aa21bd3ac..de213e5c0cd3 100755 --- a/scripts/sign-file.c +++ b/scripts/sign-file.c | |||
@@ -20,7 +20,7 @@ | |||
20 | #include <openssl/bio.h> | 20 | #include <openssl/bio.h> |
21 | #include <openssl/evp.h> | 21 | #include <openssl/evp.h> |
22 | #include <openssl/pem.h> | 22 | #include <openssl/pem.h> |
23 | #include <openssl/pkcs7.h> | 23 | #include <openssl/cms.h> |
24 | #include <openssl/err.h> | 24 | #include <openssl/err.h> |
25 | #include <openssl/engine.h> | 25 | #include <openssl/engine.h> |
26 | 26 | ||
@@ -107,13 +107,14 @@ int main(int argc, char **argv) | |||
107 | struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 }; | 107 | struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 }; |
108 | char *hash_algo = NULL; | 108 | char *hash_algo = NULL; |
109 | char *private_key_name, *x509_name, *module_name, *dest_name; | 109 | char *private_key_name, *x509_name, *module_name, *dest_name; |
110 | bool save_pkcs7 = false, replace_orig; | 110 | bool save_cms = false, replace_orig; |
111 | bool sign_only = false; | 111 | bool sign_only = false; |
112 | unsigned char buf[4096]; | 112 | unsigned char buf[4096]; |
113 | unsigned long module_size, pkcs7_size; | 113 | unsigned long module_size, cms_size; |
114 | unsigned int use_keyid = 0; | ||
114 | const EVP_MD *digest_algo; | 115 | const EVP_MD *digest_algo; |
115 | EVP_PKEY *private_key; | 116 | EVP_PKEY *private_key; |
116 | PKCS7 *pkcs7; | 117 | CMS_ContentInfo *cms; |
117 | X509 *x509; | 118 | X509 *x509; |
118 | BIO *b, *bd = NULL, *bm; | 119 | BIO *b, *bd = NULL, *bm; |
119 | int opt, n; | 120 | int opt, n; |
@@ -125,10 +126,11 @@ int main(int argc, char **argv) | |||
125 | key_pass = getenv("KBUILD_SIGN_PIN"); | 126 | key_pass = getenv("KBUILD_SIGN_PIN"); |
126 | 127 | ||
127 | do { | 128 | do { |
128 | opt = getopt(argc, argv, "dp"); | 129 | opt = getopt(argc, argv, "dpk"); |
129 | switch (opt) { | 130 | switch (opt) { |
130 | case 'p': save_pkcs7 = true; break; | 131 | case 'p': save_cms = true; break; |
131 | case 'd': sign_only = true; save_pkcs7 = true; break; | 132 | case 'd': sign_only = true; save_cms = true; break; |
133 | case 'k': use_keyid = CMS_USE_KEYID; break; | ||
132 | case -1: break; | 134 | case -1: break; |
133 | default: format(); | 135 | default: format(); |
134 | } | 136 | } |
@@ -208,23 +210,24 @@ int main(int argc, char **argv) | |||
208 | bm = BIO_new_file(module_name, "rb"); | 210 | bm = BIO_new_file(module_name, "rb"); |
209 | ERR(!bm, "%s", module_name); | 211 | ERR(!bm, "%s", module_name); |
210 | 212 | ||
211 | /* Load the PKCS#7 message from the digest buffer. */ | 213 | /* Load the CMS message from the digest buffer. */ |
212 | pkcs7 = PKCS7_sign(NULL, NULL, NULL, NULL, | 214 | cms = CMS_sign(NULL, NULL, NULL, NULL, |
213 | PKCS7_NOCERTS | PKCS7_PARTIAL | PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM); | 215 | CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY | CMS_DETACHED | CMS_STREAM); |
214 | ERR(!pkcs7, "PKCS7_sign"); | 216 | ERR(!cms, "CMS_sign"); |
215 | 217 | ||
216 | ERR(!PKCS7_sign_add_signer(pkcs7, x509, private_key, digest_algo, PKCS7_NOCERTS | PKCS7_BINARY), | 218 | ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo, |
217 | "PKCS7_sign_add_signer"); | 219 | CMS_NOCERTS | CMS_BINARY | CMS_NOSMIMECAP | use_keyid), |
218 | ERR(PKCS7_final(pkcs7, bm, PKCS7_NOCERTS | PKCS7_BINARY) < 0, | 220 | "CMS_sign_add_signer"); |
219 | "PKCS7_final"); | 221 | ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0, |
222 | "CMS_final"); | ||
220 | 223 | ||
221 | if (save_pkcs7) { | 224 | if (save_cms) { |
222 | char *pkcs7_name; | 225 | char *cms_name; |
223 | 226 | ||
224 | ERR(asprintf(&pkcs7_name, "%s.pkcs7", module_name) < 0, "asprintf"); | 227 | ERR(asprintf(&cms_name, "%s.p7s", module_name) < 0, "asprintf"); |
225 | b = BIO_new_file(pkcs7_name, "wb"); | 228 | b = BIO_new_file(cms_name, "wb"); |
226 | ERR(!b, "%s", pkcs7_name); | 229 | ERR(!b, "%s", cms_name); |
227 | ERR(i2d_PKCS7_bio_stream(b, pkcs7, NULL, 0) < 0, "%s", pkcs7_name); | 230 | ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, "%s", cms_name); |
228 | BIO_free(b); | 231 | BIO_free(b); |
229 | } | 232 | } |
230 | 233 | ||
@@ -240,9 +243,9 @@ int main(int argc, char **argv) | |||
240 | ERR(n < 0, "%s", module_name); | 243 | ERR(n < 0, "%s", module_name); |
241 | module_size = BIO_number_written(bd); | 244 | module_size = BIO_number_written(bd); |
242 | 245 | ||
243 | ERR(i2d_PKCS7_bio_stream(bd, pkcs7, NULL, 0) < 0, "%s", dest_name); | 246 | ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name); |
244 | pkcs7_size = BIO_number_written(bd) - module_size; | 247 | cms_size = BIO_number_written(bd) - module_size; |
245 | sig_info.sig_len = htonl(pkcs7_size); | 248 | sig_info.sig_len = htonl(cms_size); |
246 | ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name); | 249 | ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name); |
247 | ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name); | 250 | ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name); |
248 | 251 | ||