aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2015-09-25 11:31:46 -0400
committerDavid Howells <dhowells@redhat.com>2015-09-25 11:31:46 -0400
commit283e8ba2dfde54f8f27d7d0f459a07de79a39d55 (patch)
tree0e4057e7b3e082a9b93d04ba7b1e9ca2f23ef034
parente7c87bef7de2417b219d4dbfe8d33a0098a8df54 (diff)
MODSIGN: Change from CMS to PKCS#7 signing if the openssl is too old
The sign-file.c program actually uses CMS rather than PKCS#7 to sign a file since that allows the target X.509 certificate to be specified by subjectKeyId rather than by issuer + serialNumber. However, older versions of the OpenSSL crypto library (such as may be found in CentOS 5.11) don't support CMS. Assume everything prior to OpenSSL-1.0.0 doesn't support CMS and switch to using PKCS#7 in that case. Further, the pre-1.0.0 OpenSSL only supports PKCS#7 signing with SHA1, so give an error from the sign-file script if the caller requests anything other than SHA1. The compiler gives the following error with an OpenSSL crypto library that's too old: HOSTCC scripts/sign-file scripts/sign-file.c:23:25: fatal error: openssl/cms.h: No such file or directory #include <openssl/cms.h> Reported-by: Vinson Lee <vlee@twopensource.com> Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: David Woodhouse <David.Woodhouse@intel.com>
-rw-r--r--Documentation/Changes2
-rwxr-xr-xscripts/sign-file.c94
2 files changed, 78 insertions, 18 deletions
diff --git a/Documentation/Changes b/Documentation/Changes
index 6d8863004858..f447f0516f07 100644
--- a/Documentation/Changes
+++ b/Documentation/Changes
@@ -43,7 +43,7 @@ o udev 081 # udevd --version
43o grub 0.93 # grub --version || grub-install --version 43o grub 0.93 # grub --version || grub-install --version
44o mcelog 0.6 # mcelog --version 44o mcelog 0.6 # mcelog --version
45o iptables 1.4.2 # iptables -V 45o iptables 1.4.2 # iptables -V
46o openssl & libcrypto 1.0.1k # openssl version 46o openssl & libcrypto 1.0.0 # openssl version
47 47
48 48
49Kernel compilation 49Kernel compilation
diff --git a/scripts/sign-file.c b/scripts/sign-file.c
index c3899ca4811c..250a7a645033 100755
--- a/scripts/sign-file.c
+++ b/scripts/sign-file.c
@@ -20,13 +20,34 @@
20#include <getopt.h> 20#include <getopt.h>
21#include <err.h> 21#include <err.h>
22#include <arpa/inet.h> 22#include <arpa/inet.h>
23#include <openssl/opensslv.h>
23#include <openssl/bio.h> 24#include <openssl/bio.h>
24#include <openssl/evp.h> 25#include <openssl/evp.h>
25#include <openssl/pem.h> 26#include <openssl/pem.h>
26#include <openssl/cms.h>
27#include <openssl/err.h> 27#include <openssl/err.h>
28#include <openssl/engine.h> 28#include <openssl/engine.h>
29 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
30struct module_signature { 51struct module_signature {
31 uint8_t algo; /* Public-key crypto algorithm [0] */ 52 uint8_t algo; /* Public-key crypto algorithm [0] */
32 uint8_t hash; /* Digest algorithm [0] */ 53 uint8_t hash; /* Digest algorithm [0] */
@@ -110,30 +131,42 @@ int main(int argc, char **argv)
110 struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 }; 131 struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 };
111 char *hash_algo = NULL; 132 char *hash_algo = NULL;
112 char *private_key_name, *x509_name, *module_name, *dest_name; 133 char *private_key_name, *x509_name, *module_name, *dest_name;
113 bool save_cms = false, replace_orig; 134 bool save_sig = false, replace_orig;
114 bool sign_only = false; 135 bool sign_only = false;
115 unsigned char buf[4096]; 136 unsigned char buf[4096];
116 unsigned long module_size, cms_size; 137 unsigned long module_size, sig_size;
117 unsigned int use_keyid = 0, use_signed_attrs = CMS_NOATTR; 138 unsigned int use_signed_attrs;
118 const EVP_MD *digest_algo; 139 const EVP_MD *digest_algo;
119 EVP_PKEY *private_key; 140 EVP_PKEY *private_key;
141#ifndef USE_PKCS7
120 CMS_ContentInfo *cms; 142 CMS_ContentInfo *cms;
143 unsigned int use_keyid = 0;
144#else
145 PKCS7 *pkcs7;
146#endif
121 X509 *x509; 147 X509 *x509;
122 BIO *b, *bd = NULL, *bm; 148 BIO *b, *bd = NULL, *bm;
123 int opt, n; 149 int opt, n;
124
125 OpenSSL_add_all_algorithms(); 150 OpenSSL_add_all_algorithms();
126 ERR_load_crypto_strings(); 151 ERR_load_crypto_strings();
127 ERR_clear_error(); 152 ERR_clear_error();
128 153
129 key_pass = getenv("KBUILD_SIGN_PIN"); 154 key_pass = getenv("KBUILD_SIGN_PIN");
130 155
156#ifndef USE_PKCS7
157 use_signed_attrs = CMS_NOATTR;
158#else
159 use_signed_attrs = PKCS7_NOATTR;
160#endif
161
131 do { 162 do {
132 opt = getopt(argc, argv, "dpk"); 163 opt = getopt(argc, argv, "dpk");
133 switch (opt) { 164 switch (opt) {
134 case 'p': save_cms = true; break; 165 case 'p': save_sig = true; break;
135 case 'd': sign_only = true; save_cms = true; break; 166 case 'd': sign_only = true; save_sig = true; break;
167#ifndef USE_PKCS7
136 case 'k': use_keyid = CMS_USE_KEYID; break; 168 case 'k': use_keyid = CMS_USE_KEYID; break;
169#endif
137 case -1: break; 170 case -1: break;
138 default: format(); 171 default: format();
139 } 172 }
@@ -157,6 +190,14 @@ int main(int argc, char **argv)
157 replace_orig = true; 190 replace_orig = true;
158 } 191 }
159 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
160 /* 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
161 * will point to. 202 * will point to.
162 */ 203 */
@@ -213,7 +254,8 @@ int main(int argc, char **argv)
213 bm = BIO_new_file(module_name, "rb"); 254 bm = BIO_new_file(module_name, "rb");
214 ERR(!bm, "%s", module_name); 255 ERR(!bm, "%s", module_name);
215 256
216 /* Load the CMS message from the digest buffer. */ 257#ifndef USE_PKCS7
258 /* Load the signature message from the digest buffer. */
217 cms = CMS_sign(NULL, NULL, NULL, NULL, 259 cms = CMS_sign(NULL, NULL, NULL, NULL,
218 CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY | CMS_DETACHED | CMS_STREAM); 260 CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY | CMS_DETACHED | CMS_STREAM);
219 ERR(!cms, "CMS_sign"); 261 ERR(!cms, "CMS_sign");
@@ -221,17 +263,31 @@ int main(int argc, char **argv)
221 ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo, 263 ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo,
222 CMS_NOCERTS | CMS_BINARY | CMS_NOSMIMECAP | 264 CMS_NOCERTS | CMS_BINARY | CMS_NOSMIMECAP |
223 use_keyid | use_signed_attrs), 265 use_keyid | use_signed_attrs),
224 "CMS_sign_add_signer"); 266 "CMS_add1_signer");
225 ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0, 267 ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0,
226 "CMS_final"); 268 "CMS_final");
227 269
228 if (save_cms) { 270#else
229 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
230 276
231 ERR(asprintf(&cms_name, "%s.p7s", module_name) < 0, "asprintf"); 277 if (save_sig) {
232 b = BIO_new_file(cms_name, "wb"); 278 char *sig_file_name;
233 ERR(!b, "%s", cms_name); 279
234 ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, "%s", cms_name); 280 ERR(asprintf(&sig_file_name, "%s.p7s", module_name) < 0,
281 "asprintf");
282 b = BIO_new_file(sig_file_name, "wb");
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
235 BIO_free(b); 291 BIO_free(b);
236 } 292 }
237 293
@@ -247,9 +303,13 @@ int main(int argc, char **argv)
247 ERR(n < 0, "%s", module_name); 303 ERR(n < 0, "%s", module_name);
248 module_size = BIO_number_written(bd); 304 module_size = BIO_number_written(bd);
249 305
306#ifndef USE_PKCS7
250 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);
251 cms_size = BIO_number_written(bd) - module_size; 308#else
252 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);
253 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);
254 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);
255 315