diff options
Diffstat (limited to 'scripts/sign-file.c')
-rwxr-xr-x | scripts/sign-file.c | 107 |
1 files changed, 85 insertions, 22 deletions
diff --git a/scripts/sign-file.c b/scripts/sign-file.c index 058bba3103e2..250a7a645033 100755 --- a/scripts/sign-file.c +++ b/scripts/sign-file.c | |||
@@ -1,12 +1,15 @@ | |||
1 | /* Sign a module file using the given key. | 1 | /* Sign a module file using the given key. |
2 | * | 2 | * |
3 | * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved. | 3 | * Copyright © 2014-2015 Red Hat, Inc. All Rights Reserved. |
4 | * Written by David Howells (dhowells@redhat.com) | 4 | * Copyright © 2015 Intel Corporation. |
5 | * | ||
6 | * Authors: David Howells <dhowells@redhat.com> | ||
7 | * David Woodhouse <dwmw2@infradead.org> | ||
5 | * | 8 | * |
6 | * This program is free software; you can redistribute it and/or | 9 | * This program is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU General Public Licence | 10 | * modify it under the terms of the GNU Lesser General Public License |
8 | * as published by the Free Software Foundation; either version | 11 | * as published by the Free Software Foundation; either version 2.1 |
9 | * 2 of the Licence, or (at your option) any later version. | 12 | * of the licence, or (at your option) any later version. |
10 | */ | 13 | */ |
11 | #define _GNU_SOURCE | 14 | #define _GNU_SOURCE |
12 | #include <stdio.h> | 15 | #include <stdio.h> |
@@ -17,13 +20,34 @@ | |||
17 | #include <getopt.h> | 20 | #include <getopt.h> |
18 | #include <err.h> | 21 | #include <err.h> |
19 | #include <arpa/inet.h> | 22 | #include <arpa/inet.h> |
23 | #include <openssl/opensslv.h> | ||
20 | #include <openssl/bio.h> | 24 | #include <openssl/bio.h> |
21 | #include <openssl/evp.h> | 25 | #include <openssl/evp.h> |
22 | #include <openssl/pem.h> | 26 | #include <openssl/pem.h> |
23 | #include <openssl/cms.h> | ||
24 | #include <openssl/err.h> | 27 | #include <openssl/err.h> |
25 | #include <openssl/engine.h> | 28 | #include <openssl/engine.h> |
26 | 29 | ||
30 | /* | ||
31 | * Use CMS if we have openssl-1.0.0 or newer available - otherwise we have to | ||
32 | * assume that it's not available and its header file is missing and that we | ||
33 | * should use PKCS#7 instead. Switching to the older PKCS#7 format restricts | ||
34 | * the options we have on specifying the X.509 certificate we want. | ||
35 | * | ||
36 | * Further, older versions of OpenSSL don't support manually adding signers to | ||
37 | * the PKCS#7 message so have to accept that we get a certificate included in | ||
38 | * the signature message. Nor do such older versions of OpenSSL support | ||
39 | * signing with anything other than SHA1 - so we're stuck with that if such is | ||
40 | * the case. | ||
41 | */ | ||
42 | #if OPENSSL_VERSION_NUMBER < 0x10000000L | ||
43 | #define USE_PKCS7 | ||
44 | #endif | ||
45 | #ifndef USE_PKCS7 | ||
46 | #include <openssl/cms.h> | ||
47 | #else | ||
48 | #include <openssl/pkcs7.h> | ||
49 | #endif | ||
50 | |||
27 | struct module_signature { | 51 | struct module_signature { |
28 | uint8_t algo; /* Public-key crypto algorithm [0] */ | 52 | uint8_t algo; /* Public-key crypto algorithm [0] */ |
29 | uint8_t hash; /* Digest algorithm [0] */ | 53 | uint8_t hash; /* Digest algorithm [0] */ |
@@ -107,30 +131,42 @@ int main(int argc, char **argv) | |||
107 | struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 }; | 131 | struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 }; |
108 | char *hash_algo = NULL; | 132 | char *hash_algo = NULL; |
109 | char *private_key_name, *x509_name, *module_name, *dest_name; | 133 | char *private_key_name, *x509_name, *module_name, *dest_name; |
110 | bool save_cms = false, replace_orig; | 134 | bool save_sig = false, replace_orig; |
111 | bool sign_only = false; | 135 | bool sign_only = false; |
112 | unsigned char buf[4096]; | 136 | unsigned char buf[4096]; |
113 | unsigned long module_size, cms_size; | 137 | unsigned long module_size, sig_size; |
114 | unsigned int use_keyid = 0, use_signed_attrs = CMS_NOATTR; | 138 | unsigned int use_signed_attrs; |
115 | const EVP_MD *digest_algo; | 139 | const EVP_MD *digest_algo; |
116 | EVP_PKEY *private_key; | 140 | EVP_PKEY *private_key; |
141 | #ifndef USE_PKCS7 | ||
117 | CMS_ContentInfo *cms; | 142 | CMS_ContentInfo *cms; |
143 | unsigned int use_keyid = 0; | ||
144 | #else | ||
145 | PKCS7 *pkcs7; | ||
146 | #endif | ||
118 | X509 *x509; | 147 | X509 *x509; |
119 | BIO *b, *bd = NULL, *bm; | 148 | BIO *b, *bd = NULL, *bm; |
120 | int opt, n; | 149 | int opt, n; |
121 | |||
122 | OpenSSL_add_all_algorithms(); | 150 | OpenSSL_add_all_algorithms(); |
123 | ERR_load_crypto_strings(); | 151 | ERR_load_crypto_strings(); |
124 | ERR_clear_error(); | 152 | ERR_clear_error(); |
125 | 153 | ||
126 | key_pass = getenv("KBUILD_SIGN_PIN"); | 154 | key_pass = getenv("KBUILD_SIGN_PIN"); |
127 | 155 | ||
156 | #ifndef USE_PKCS7 | ||
157 | use_signed_attrs = CMS_NOATTR; | ||
158 | #else | ||
159 | use_signed_attrs = PKCS7_NOATTR; | ||
160 | #endif | ||
161 | |||
128 | do { | 162 | do { |
129 | opt = getopt(argc, argv, "dpk"); | 163 | opt = getopt(argc, argv, "dpk"); |
130 | switch (opt) { | 164 | switch (opt) { |
131 | case 'p': save_cms = true; break; | 165 | case 'p': save_sig = true; break; |
132 | case 'd': sign_only = true; save_cms = true; break; | 166 | case 'd': sign_only = true; save_sig = true; break; |
167 | #ifndef USE_PKCS7 | ||
133 | case 'k': use_keyid = CMS_USE_KEYID; break; | 168 | case 'k': use_keyid = CMS_USE_KEYID; break; |
169 | #endif | ||
134 | case -1: break; | 170 | case -1: break; |
135 | default: format(); | 171 | default: format(); |
136 | } | 172 | } |
@@ -154,6 +190,14 @@ int main(int argc, char **argv) | |||
154 | replace_orig = true; | 190 | replace_orig = true; |
155 | } | 191 | } |
156 | 192 | ||
193 | #ifdef USE_PKCS7 | ||
194 | if (strcmp(hash_algo, "sha1") != 0) { | ||
195 | fprintf(stderr, "sign-file: %s only supports SHA1 signing\n", | ||
196 | OPENSSL_VERSION_TEXT); | ||
197 | exit(3); | ||
198 | } | ||
199 | #endif | ||
200 | |||
157 | /* Read the private key and the X.509 cert the PKCS#7 message | 201 | /* Read the private key and the X.509 cert the PKCS#7 message |
158 | * will point to. | 202 | * will point to. |
159 | */ | 203 | */ |
@@ -210,7 +254,8 @@ int main(int argc, char **argv) | |||
210 | bm = BIO_new_file(module_name, "rb"); | 254 | bm = BIO_new_file(module_name, "rb"); |
211 | ERR(!bm, "%s", module_name); | 255 | ERR(!bm, "%s", module_name); |
212 | 256 | ||
213 | /* Load the CMS message from the digest buffer. */ | 257 | #ifndef USE_PKCS7 |
258 | /* Load the signature message from the digest buffer. */ | ||
214 | cms = CMS_sign(NULL, NULL, NULL, NULL, | 259 | cms = CMS_sign(NULL, NULL, NULL, NULL, |
215 | CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY | CMS_DETACHED | CMS_STREAM); | 260 | CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY | CMS_DETACHED | CMS_STREAM); |
216 | ERR(!cms, "CMS_sign"); | 261 | ERR(!cms, "CMS_sign"); |
@@ -218,17 +263,31 @@ int main(int argc, char **argv) | |||
218 | ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo, | 263 | ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo, |
219 | CMS_NOCERTS | CMS_BINARY | CMS_NOSMIMECAP | | 264 | CMS_NOCERTS | CMS_BINARY | CMS_NOSMIMECAP | |
220 | use_keyid | use_signed_attrs), | 265 | use_keyid | use_signed_attrs), |
221 | "CMS_sign_add_signer"); | 266 | "CMS_add1_signer"); |
222 | ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0, | 267 | ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0, |
223 | "CMS_final"); | 268 | "CMS_final"); |
224 | 269 | ||
225 | if (save_cms) { | 270 | #else |
226 | char *cms_name; | 271 | pkcs7 = PKCS7_sign(x509, private_key, NULL, bm, |
272 | PKCS7_NOCERTS | PKCS7_BINARY | | ||
273 | PKCS7_DETACHED | use_signed_attrs); | ||
274 | ERR(!pkcs7, "PKCS7_sign"); | ||
275 | #endif | ||
276 | |||
277 | if (save_sig) { | ||
278 | char *sig_file_name; | ||
227 | 279 | ||
228 | ERR(asprintf(&cms_name, "%s.p7s", module_name) < 0, "asprintf"); | 280 | ERR(asprintf(&sig_file_name, "%s.p7s", module_name) < 0, |
229 | b = BIO_new_file(cms_name, "wb"); | 281 | "asprintf"); |
230 | ERR(!b, "%s", cms_name); | 282 | b = BIO_new_file(sig_file_name, "wb"); |
231 | ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, "%s", cms_name); | 283 | ERR(!b, "%s", sig_file_name); |
284 | #ifndef USE_PKCS7 | ||
285 | ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, | ||
286 | "%s", sig_file_name); | ||
287 | #else | ||
288 | ERR(i2d_PKCS7_bio(b, pkcs7) < 0, | ||
289 | "%s", sig_file_name); | ||
290 | #endif | ||
232 | BIO_free(b); | 291 | BIO_free(b); |
233 | } | 292 | } |
234 | 293 | ||
@@ -244,9 +303,13 @@ int main(int argc, char **argv) | |||
244 | ERR(n < 0, "%s", module_name); | 303 | ERR(n < 0, "%s", module_name); |
245 | module_size = BIO_number_written(bd); | 304 | module_size = BIO_number_written(bd); |
246 | 305 | ||
306 | #ifndef USE_PKCS7 | ||
247 | ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name); | 307 | ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name); |
248 | cms_size = BIO_number_written(bd) - module_size; | 308 | #else |
249 | sig_info.sig_len = htonl(cms_size); | 309 | ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name); |
310 | #endif | ||
311 | sig_size = BIO_number_written(bd) - module_size; | ||
312 | sig_info.sig_len = htonl(sig_size); | ||
250 | ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name); | 313 | ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name); |
251 | ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name); | 314 | ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name); |
252 | 315 | ||