blob: e8d0ebdad7a0fadd9fa29a57dbe7c61cf676a1a6 [file] [log] [blame]
David Howellsbc1c3732015-07-20 21:16:27 +01001/* Sign a module file using the given key.
2 *
David Howells9552c7a2016-06-14 13:18:33 +01003 * Copyright © 2014-2016 Red Hat, Inc. All Rights Reserved.
David Woodhouse09a77a82015-09-15 16:03:36 +01004 * Copyright © 2015 Intel Corporation.
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +01005 * Copyright © 2016 Hewlett Packard Enterprise Development LP
David Woodhouse09a77a82015-09-15 16:03:36 +01006 *
7 * Authors: David Howells <dhowells@redhat.com>
8 * David Woodhouse <dwmw2@infradead.org>
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +01009 * Juerg Haefliger <juerg.haefliger@hpe.com>
David Howellsbc1c3732015-07-20 21:16:27 +010010 *
11 * This program is free software; you can redistribute it and/or
David Woodhouse09a77a82015-09-15 16:03:36 +010012 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the licence, or (at your option) any later version.
David Howellsbc1c3732015-07-20 21:16:27 +010015 */
16#define _GNU_SOURCE
17#include <stdio.h>
18#include <stdlib.h>
19#include <stdint.h>
20#include <stdbool.h>
21#include <string.h>
22#include <getopt.h>
23#include <err.h>
24#include <arpa/inet.h>
David Howells283e8ba2015-09-25 16:31:46 +010025#include <openssl/opensslv.h>
David Howellsbc1c3732015-07-20 21:16:27 +010026#include <openssl/bio.h>
27#include <openssl/evp.h>
28#include <openssl/pem.h>
David Howellsbc1c3732015-07-20 21:16:27 +010029#include <openssl/err.h>
David Woodhouse6e3e2812015-07-20 21:16:29 +010030#include <openssl/engine.h>
David Howellsbc1c3732015-07-20 21:16:27 +010031
David Howells283e8ba2015-09-25 16:31:46 +010032/*
Linus Torvalds9997d492022-06-08 13:18:39 -070033 * OpenSSL 3.0 deprecates the OpenSSL's ENGINE API.
34 *
35 * Remove this if/when that API is no longer used
36 */
37#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38
39/*
David Howells283e8ba2015-09-25 16:31:46 +010040 * Use CMS if we have openssl-1.0.0 or newer available - otherwise we have to
41 * assume that it's not available and its header file is missing and that we
42 * should use PKCS#7 instead. Switching to the older PKCS#7 format restricts
43 * the options we have on specifying the X.509 certificate we want.
44 *
45 * Further, older versions of OpenSSL don't support manually adding signers to
46 * the PKCS#7 message so have to accept that we get a certificate included in
47 * the signature message. Nor do such older versions of OpenSSL support
48 * signing with anything other than SHA1 - so we're stuck with that if such is
49 * the case.
50 */
Marc-Antoine Perennou41693d12016-03-01 09:53:00 +010051#if OPENSSL_VERSION_NUMBER < 0x10000000L || defined(OPENSSL_NO_CMS)
David Howells283e8ba2015-09-25 16:31:46 +010052#define USE_PKCS7
53#endif
54#ifndef USE_PKCS7
55#include <openssl/cms.h>
56#else
57#include <openssl/pkcs7.h>
58#endif
59
David Howellsbc1c3732015-07-20 21:16:27 +010060struct module_signature {
61 uint8_t algo; /* Public-key crypto algorithm [0] */
62 uint8_t hash; /* Digest algorithm [0] */
63 uint8_t id_type; /* Key identifier type [PKEY_ID_PKCS7] */
64 uint8_t signer_len; /* Length of signer's name [0] */
65 uint8_t key_id_len; /* Length of key identifier [0] */
66 uint8_t __pad[3];
67 uint32_t sig_len; /* Length of signature data */
68};
69
70#define PKEY_ID_PKCS7 2
71
72static char magic_number[] = "~Module signature appended~\n";
73
74static __attribute__((noreturn))
75void format(void)
76{
77 fprintf(stderr,
78 "Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n");
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +010079 fprintf(stderr,
80 " scripts/sign-file -s <raw sig> <hash algo> <x509> <module> [<dest>]\n");
David Howellsbc1c3732015-07-20 21:16:27 +010081 exit(2);
82}
83
84static void display_openssl_errors(int l)
85{
86 const char *file;
87 char buf[120];
88 int e, line;
89
90 if (ERR_peek_error() == 0)
91 return;
92 fprintf(stderr, "At main.c:%d:\n", l);
93
94 while ((e = ERR_get_error_line(&file, &line))) {
95 ERR_error_string(e, buf);
96 fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
97 }
98}
99
100static void drain_openssl_errors(void)
101{
102 const char *file;
103 int line;
104
105 if (ERR_peek_error() == 0)
106 return;
107 while (ERR_get_error_line(&file, &line)) {}
108}
109
110#define ERR(cond, fmt, ...) \
111 do { \
112 bool __cond = (cond); \
113 display_openssl_errors(__LINE__); \
114 if (__cond) { \
115 err(1, fmt, ## __VA_ARGS__); \
116 } \
117 } while(0)
118
David Woodhouseaf1eb292015-07-20 21:16:28 +0100119static const char *key_pass;
120
121static int pem_pw_cb(char *buf, int len, int w, void *v)
122{
123 int pwlen;
124
125 if (!key_pass)
126 return -1;
127
128 pwlen = strlen(key_pass);
129 if (pwlen >= len)
130 return -1;
131
132 strcpy(buf, key_pass);
133
134 /* If it's wrong, don't keep trying it. */
135 key_pass = NULL;
136
137 return pwlen;
138}
139
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100140static EVP_PKEY *read_private_key(const char *private_key_name)
141{
142 EVP_PKEY *private_key;
143
144 if (!strncmp(private_key_name, "pkcs11:", 7)) {
145 ENGINE *e;
146
147 ENGINE_load_builtin_engines();
148 drain_openssl_errors();
149 e = ENGINE_by_id("pkcs11");
150 ERR(!e, "Load PKCS#11 ENGINE");
151 if (ENGINE_init(e))
152 drain_openssl_errors();
153 else
154 ERR(1, "ENGINE_init");
155 if (key_pass)
156 ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0),
157 "Set PKCS#11 PIN");
158 private_key = ENGINE_load_private_key(e, private_key_name,
159 NULL, NULL);
160 ERR(!private_key, "%s", private_key_name);
161 } else {
162 BIO *b;
163
164 b = BIO_new_file(private_key_name, "rb");
165 ERR(!b, "%s", private_key_name);
166 private_key = PEM_read_bio_PrivateKey(b, NULL, pem_pw_cb,
167 NULL);
168 ERR(!private_key, "%s", private_key_name);
169 BIO_free(b);
170 }
171
172 return private_key;
173}
174
175static X509 *read_x509(const char *x509_name)
176{
David Howells9552c7a2016-06-14 13:18:33 +0100177 unsigned char buf[2];
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100178 X509 *x509;
179 BIO *b;
David Howells9552c7a2016-06-14 13:18:33 +0100180 int n;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100181
182 b = BIO_new_file(x509_name, "rb");
183 ERR(!b, "%s", x509_name);
David Howells9552c7a2016-06-14 13:18:33 +0100184
185 /* Look at the first two bytes of the file to determine the encoding */
186 n = BIO_read(b, buf, 2);
187 if (n != 2) {
188 if (BIO_should_retry(b)) {
189 fprintf(stderr, "%s: Read wanted retry\n", x509_name);
190 exit(1);
191 }
192 if (n >= 0) {
193 fprintf(stderr, "%s: Short read\n", x509_name);
194 exit(1);
195 }
196 ERR(1, "%s", x509_name);
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100197 }
David Howells9552c7a2016-06-14 13:18:33 +0100198
199 ERR(BIO_reset(b) != 0, "%s", x509_name);
200
201 if (buf[0] == 0x30 && buf[1] >= 0x81 && buf[1] <= 0x84)
202 /* Assume raw DER encoded X.509 */
203 x509 = d2i_X509_bio(b, NULL);
204 else
205 /* Assume PEM encoded X.509 */
206 x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
207
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100208 BIO_free(b);
209 ERR(!x509, "%s", x509_name);
210
211 return x509;
212}
213
David Howellsbc1c3732015-07-20 21:16:27 +0100214int main(int argc, char **argv)
215{
216 struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 };
217 char *hash_algo = NULL;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100218 char *private_key_name = NULL, *raw_sig_name = NULL;
219 char *x509_name, *module_name, *dest_name;
David Howells283e8ba2015-09-25 16:31:46 +0100220 bool save_sig = false, replace_orig;
Luis R. Rodriguez23dfbba2015-07-20 21:16:27 +0100221 bool sign_only = false;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100222 bool raw_sig = false;
David Howellsbc1c3732015-07-20 21:16:27 +0100223 unsigned char buf[4096];
David Howells283e8ba2015-09-25 16:31:46 +0100224 unsigned long module_size, sig_size;
225 unsigned int use_signed_attrs;
David Howellsbc1c3732015-07-20 21:16:27 +0100226 const EVP_MD *digest_algo;
227 EVP_PKEY *private_key;
David Howells283e8ba2015-09-25 16:31:46 +0100228#ifndef USE_PKCS7
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100229 CMS_ContentInfo *cms = NULL;
David Howells283e8ba2015-09-25 16:31:46 +0100230 unsigned int use_keyid = 0;
231#else
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100232 PKCS7 *pkcs7 = NULL;
David Howells283e8ba2015-09-25 16:31:46 +0100233#endif
David Howellsbc1c3732015-07-20 21:16:27 +0100234 X509 *x509;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100235 BIO *bd, *bm;
David Howellsbc1c3732015-07-20 21:16:27 +0100236 int opt, n;
David Woodhouseaf1eb292015-07-20 21:16:28 +0100237 OpenSSL_add_all_algorithms();
David Howellsbc1c3732015-07-20 21:16:27 +0100238 ERR_load_crypto_strings();
239 ERR_clear_error();
240
David Woodhouseaf1eb292015-07-20 21:16:28 +0100241 key_pass = getenv("KBUILD_SIGN_PIN");
242
David Howells283e8ba2015-09-25 16:31:46 +0100243#ifndef USE_PKCS7
244 use_signed_attrs = CMS_NOATTR;
245#else
246 use_signed_attrs = PKCS7_NOATTR;
247#endif
248
David Howellsbc1c3732015-07-20 21:16:27 +0100249 do {
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100250 opt = getopt(argc, argv, "sdpk");
David Howellsbc1c3732015-07-20 21:16:27 +0100251 switch (opt) {
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100252 case 's': raw_sig = true; break;
David Howells283e8ba2015-09-25 16:31:46 +0100253 case 'p': save_sig = true; break;
254 case 'd': sign_only = true; save_sig = true; break;
255#ifndef USE_PKCS7
David Howellsed8c2072015-07-20 21:16:33 +0100256 case 'k': use_keyid = CMS_USE_KEYID; break;
David Howells283e8ba2015-09-25 16:31:46 +0100257#endif
David Howellsbc1c3732015-07-20 21:16:27 +0100258 case -1: break;
259 default: format();
260 }
261 } while (opt != -1);
262
263 argc -= optind;
264 argv += optind;
265 if (argc < 4 || argc > 5)
266 format();
267
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100268 if (raw_sig) {
269 raw_sig_name = argv[0];
270 hash_algo = argv[1];
271 } else {
272 hash_algo = argv[0];
273 private_key_name = argv[1];
274 }
David Howellsbc1c3732015-07-20 21:16:27 +0100275 x509_name = argv[2];
276 module_name = argv[3];
277 if (argc == 5) {
278 dest_name = argv[4];
279 replace_orig = false;
280 } else {
281 ERR(asprintf(&dest_name, "%s.~signed~", module_name) < 0,
282 "asprintf");
283 replace_orig = true;
284 }
285
David Howells283e8ba2015-09-25 16:31:46 +0100286#ifdef USE_PKCS7
287 if (strcmp(hash_algo, "sha1") != 0) {
288 fprintf(stderr, "sign-file: %s only supports SHA1 signing\n",
289 OPENSSL_VERSION_TEXT);
290 exit(3);
291 }
292#endif
293
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100294 /* Open the module file */
295 bm = BIO_new_file(module_name, "rb");
296 ERR(!bm, "%s", module_name);
David Woodhouse6e3e2812015-07-20 21:16:29 +0100297
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100298 if (!raw_sig) {
299 /* Read the private key and the X.509 cert the PKCS#7 message
300 * will point to.
301 */
302 private_key = read_private_key(private_key_name);
303 x509 = read_x509(x509_name);
David Howellsbc1c3732015-07-20 21:16:27 +0100304
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100305 /* Digest the module data. */
306 OpenSSL_add_all_digests();
307 display_openssl_errors(__LINE__);
308 digest_algo = EVP_get_digestbyname(hash_algo);
309 ERR(!digest_algo, "EVP_get_digestbyname");
310
311#ifndef USE_PKCS7
312 /* Load the signature message from the digest buffer. */
313 cms = CMS_sign(NULL, NULL, NULL, NULL,
314 CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY |
315 CMS_DETACHED | CMS_STREAM);
316 ERR(!cms, "CMS_sign");
317
318 ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo,
319 CMS_NOCERTS | CMS_BINARY |
320 CMS_NOSMIMECAP | use_keyid |
321 use_signed_attrs),
322 "CMS_add1_signer");
323 ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0,
324 "CMS_final");
325
326#else
327 pkcs7 = PKCS7_sign(x509, private_key, NULL, bm,
328 PKCS7_NOCERTS | PKCS7_BINARY |
329 PKCS7_DETACHED | use_signed_attrs);
330 ERR(!pkcs7, "PKCS7_sign");
331#endif
332
333 if (save_sig) {
334 char *sig_file_name;
335 BIO *b;
336
337 ERR(asprintf(&sig_file_name, "%s.p7s", module_name) < 0,
338 "asprintf");
339 b = BIO_new_file(sig_file_name, "wb");
340 ERR(!b, "%s", sig_file_name);
341#ifndef USE_PKCS7
342 ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0,
343 "%s", sig_file_name);
344#else
345 ERR(i2d_PKCS7_bio(b, pkcs7) < 0,
346 "%s", sig_file_name);
347#endif
348 BIO_free(b);
349 }
350
351 if (sign_only) {
352 BIO_free(bm);
353 return 0;
354 }
David Howellsbc1c3732015-07-20 21:16:27 +0100355 }
David Howellsbc1c3732015-07-20 21:16:27 +0100356
357 /* Open the destination file now so that we can shovel the module data
358 * across as we read it.
359 */
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100360 bd = BIO_new_file(dest_name, "wb");
361 ERR(!bd, "%s", dest_name);
Luis R. Rodriguez23dfbba2015-07-20 21:16:27 +0100362
David Howellsbc1c3732015-07-20 21:16:27 +0100363 /* Append the marker and the PKCS#7 message to the destination file */
364 ERR(BIO_reset(bm) < 0, "%s", module_name);
365 while ((n = BIO_read(bm, buf, sizeof(buf))),
366 n > 0) {
367 ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name);
368 }
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100369 BIO_free(bm);
David Howellsbc1c3732015-07-20 21:16:27 +0100370 ERR(n < 0, "%s", module_name);
371 module_size = BIO_number_written(bd);
372
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100373 if (!raw_sig) {
David Howells283e8ba2015-09-25 16:31:46 +0100374#ifndef USE_PKCS7
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100375 ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name);
David Howells283e8ba2015-09-25 16:31:46 +0100376#else
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100377 ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name);
David Howells283e8ba2015-09-25 16:31:46 +0100378#endif
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100379 } else {
380 BIO *b;
381
382 /* Read the raw signature file and write the data to the
383 * destination file
384 */
385 b = BIO_new_file(raw_sig_name, "rb");
386 ERR(!b, "%s", raw_sig_name);
387 while ((n = BIO_read(b, buf, sizeof(buf))), n > 0)
388 ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name);
389 BIO_free(b);
390 }
391
David Howells283e8ba2015-09-25 16:31:46 +0100392 sig_size = BIO_number_written(bd) - module_size;
393 sig_info.sig_len = htonl(sig_size);
David Howellsbc1c3732015-07-20 21:16:27 +0100394 ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name);
395 ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name);
396
397 ERR(BIO_free(bd) < 0, "%s", dest_name);
398
399 /* Finally, if we're signing in place, replace the original. */
400 if (replace_orig)
401 ERR(rename(dest_name, module_name) < 0, "%s", dest_name);
402
403 return 0;
404}