blob: 8f06328d1cd1116e0045f587beb7d6ccf72d162b [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/*
33 * Use CMS if we have openssl-1.0.0 or newer available - otherwise we have to
34 * assume that it's not available and its header file is missing and that we
35 * should use PKCS#7 instead. Switching to the older PKCS#7 format restricts
36 * the options we have on specifying the X.509 certificate we want.
37 *
38 * Further, older versions of OpenSSL don't support manually adding signers to
39 * the PKCS#7 message so have to accept that we get a certificate included in
40 * the signature message. Nor do such older versions of OpenSSL support
41 * signing with anything other than SHA1 - so we're stuck with that if such is
42 * the case.
43 */
Felix Fietkauf8688012017-02-09 17:17:45 +000044#if defined(LIBRESSL_VERSION_NUMBER) || \
45 OPENSSL_VERSION_NUMBER < 0x10000000L || \
46 defined(OPENSSL_NO_CMS)
David Howells283e8ba2015-09-25 16:31:46 +010047#define USE_PKCS7
48#endif
49#ifndef USE_PKCS7
50#include <openssl/cms.h>
51#else
52#include <openssl/pkcs7.h>
53#endif
54
David Howellsbc1c3732015-07-20 21:16:27 +010055struct module_signature {
56 uint8_t algo; /* Public-key crypto algorithm [0] */
57 uint8_t hash; /* Digest algorithm [0] */
58 uint8_t id_type; /* Key identifier type [PKEY_ID_PKCS7] */
59 uint8_t signer_len; /* Length of signer's name [0] */
60 uint8_t key_id_len; /* Length of key identifier [0] */
61 uint8_t __pad[3];
62 uint32_t sig_len; /* Length of signature data */
63};
64
65#define PKEY_ID_PKCS7 2
66
67static char magic_number[] = "~Module signature appended~\n";
68
69static __attribute__((noreturn))
70void format(void)
71{
72 fprintf(stderr,
73 "Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n");
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +010074 fprintf(stderr,
75 " scripts/sign-file -s <raw sig> <hash algo> <x509> <module> [<dest>]\n");
David Howellsbc1c3732015-07-20 21:16:27 +010076 exit(2);
77}
78
79static void display_openssl_errors(int l)
80{
81 const char *file;
82 char buf[120];
83 int e, line;
84
85 if (ERR_peek_error() == 0)
86 return;
87 fprintf(stderr, "At main.c:%d:\n", l);
88
89 while ((e = ERR_get_error_line(&file, &line))) {
90 ERR_error_string(e, buf);
91 fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
92 }
93}
94
Lee Jonesfad17702021-10-07 11:32:12 +010095#ifndef OPENSSL_NO_ENGINE
David Howellsbc1c3732015-07-20 21:16:27 +010096static void drain_openssl_errors(void)
97{
98 const char *file;
99 int line;
100
101 if (ERR_peek_error() == 0)
102 return;
103 while (ERR_get_error_line(&file, &line)) {}
104}
Lee Jonesfad17702021-10-07 11:32:12 +0100105#endif
David Howellsbc1c3732015-07-20 21:16:27 +0100106
107#define ERR(cond, fmt, ...) \
108 do { \
109 bool __cond = (cond); \
110 display_openssl_errors(__LINE__); \
111 if (__cond) { \
112 err(1, fmt, ## __VA_ARGS__); \
113 } \
114 } while(0)
115
David Woodhouseaf1eb292015-07-20 21:16:28 +0100116static const char *key_pass;
117
118static int pem_pw_cb(char *buf, int len, int w, void *v)
119{
120 int pwlen;
121
122 if (!key_pass)
123 return -1;
124
125 pwlen = strlen(key_pass);
126 if (pwlen >= len)
127 return -1;
128
129 strcpy(buf, key_pass);
130
131 /* If it's wrong, don't keep trying it. */
132 key_pass = NULL;
133
134 return pwlen;
135}
136
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100137static EVP_PKEY *read_private_key(const char *private_key_name)
138{
139 EVP_PKEY *private_key;
Lee Jonese9d39632021-10-05 14:33:06 +0100140 BIO *b;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100141
Lee Jonese9d39632021-10-05 14:33:06 +0100142#ifndef OPENSSL_NO_ENGINE
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100143 if (!strncmp(private_key_name, "pkcs11:", 7)) {
144 ENGINE *e;
145
146 ENGINE_load_builtin_engines();
147 drain_openssl_errors();
148 e = ENGINE_by_id("pkcs11");
149 ERR(!e, "Load PKCS#11 ENGINE");
150 if (ENGINE_init(e))
151 drain_openssl_errors();
152 else
153 ERR(1, "ENGINE_init");
154 if (key_pass)
155 ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0),
156 "Set PKCS#11 PIN");
157 private_key = ENGINE_load_private_key(e, private_key_name,
158 NULL, NULL);
159 ERR(!private_key, "%s", private_key_name);
Lee Jonese9d39632021-10-05 14:33:06 +0100160 return private_key;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100161 }
Lee Jonese9d39632021-10-05 14:33:06 +0100162#endif
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100163
Lee Jonese9d39632021-10-05 14:33:06 +0100164 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);
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100170 return private_key;
171}
172
173static X509 *read_x509(const char *x509_name)
174{
David Howells9552c7a2016-06-14 13:18:33 +0100175 unsigned char buf[2];
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100176 X509 *x509;
177 BIO *b;
David Howells9552c7a2016-06-14 13:18:33 +0100178 int n;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100179
180 b = BIO_new_file(x509_name, "rb");
181 ERR(!b, "%s", x509_name);
David Howells9552c7a2016-06-14 13:18:33 +0100182
183 /* Look at the first two bytes of the file to determine the encoding */
184 n = BIO_read(b, buf, 2);
185 if (n != 2) {
186 if (BIO_should_retry(b)) {
187 fprintf(stderr, "%s: Read wanted retry\n", x509_name);
188 exit(1);
189 }
190 if (n >= 0) {
191 fprintf(stderr, "%s: Short read\n", x509_name);
192 exit(1);
193 }
194 ERR(1, "%s", x509_name);
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100195 }
David Howells9552c7a2016-06-14 13:18:33 +0100196
197 ERR(BIO_reset(b) != 0, "%s", x509_name);
198
199 if (buf[0] == 0x30 && buf[1] >= 0x81 && buf[1] <= 0x84)
200 /* Assume raw DER encoded X.509 */
201 x509 = d2i_X509_bio(b, NULL);
202 else
203 /* Assume PEM encoded X.509 */
204 x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
205
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100206 BIO_free(b);
207 ERR(!x509, "%s", x509_name);
208
209 return x509;
210}
211
David Howellsbc1c3732015-07-20 21:16:27 +0100212int main(int argc, char **argv)
213{
214 struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 };
215 char *hash_algo = NULL;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100216 char *private_key_name = NULL, *raw_sig_name = NULL;
217 char *x509_name, *module_name, *dest_name;
David Howells283e8ba2015-09-25 16:31:46 +0100218 bool save_sig = false, replace_orig;
Luis R. Rodriguez23dfbba2015-07-20 21:16:27 +0100219 bool sign_only = false;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100220 bool raw_sig = false;
David Howellsbc1c3732015-07-20 21:16:27 +0100221 unsigned char buf[4096];
David Howells283e8ba2015-09-25 16:31:46 +0100222 unsigned long module_size, sig_size;
223 unsigned int use_signed_attrs;
David Howellsbc1c3732015-07-20 21:16:27 +0100224 const EVP_MD *digest_algo;
225 EVP_PKEY *private_key;
David Howells283e8ba2015-09-25 16:31:46 +0100226#ifndef USE_PKCS7
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100227 CMS_ContentInfo *cms = NULL;
David Howells283e8ba2015-09-25 16:31:46 +0100228 unsigned int use_keyid = 0;
229#else
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100230 PKCS7 *pkcs7 = NULL;
David Howells283e8ba2015-09-25 16:31:46 +0100231#endif
David Howellsbc1c3732015-07-20 21:16:27 +0100232 X509 *x509;
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100233 BIO *bd, *bm;
David Howellsbc1c3732015-07-20 21:16:27 +0100234 int opt, n;
David Woodhouseaf1eb292015-07-20 21:16:28 +0100235 OpenSSL_add_all_algorithms();
David Howellsbc1c3732015-07-20 21:16:27 +0100236 ERR_load_crypto_strings();
237 ERR_clear_error();
238
David Woodhouseaf1eb292015-07-20 21:16:28 +0100239 key_pass = getenv("KBUILD_SIGN_PIN");
240
David Howells283e8ba2015-09-25 16:31:46 +0100241#ifndef USE_PKCS7
242 use_signed_attrs = CMS_NOATTR;
243#else
244 use_signed_attrs = PKCS7_NOATTR;
245#endif
246
David Howellsbc1c3732015-07-20 21:16:27 +0100247 do {
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100248 opt = getopt(argc, argv, "sdpk");
David Howellsbc1c3732015-07-20 21:16:27 +0100249 switch (opt) {
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100250 case 's': raw_sig = true; break;
David Howells283e8ba2015-09-25 16:31:46 +0100251 case 'p': save_sig = true; break;
252 case 'd': sign_only = true; save_sig = true; break;
253#ifndef USE_PKCS7
David Howellsed8c2072015-07-20 21:16:33 +0100254 case 'k': use_keyid = CMS_USE_KEYID; break;
David Howells283e8ba2015-09-25 16:31:46 +0100255#endif
David Howellsbc1c3732015-07-20 21:16:27 +0100256 case -1: break;
257 default: format();
258 }
259 } while (opt != -1);
260
261 argc -= optind;
262 argv += optind;
263 if (argc < 4 || argc > 5)
264 format();
265
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100266 if (raw_sig) {
267 raw_sig_name = argv[0];
268 hash_algo = argv[1];
269 } else {
270 hash_algo = argv[0];
271 private_key_name = argv[1];
272 }
David Howellsbc1c3732015-07-20 21:16:27 +0100273 x509_name = argv[2];
274 module_name = argv[3];
Alex Yashchenkoefcae7c2016-12-13 09:26:25 +0000275 if (argc == 5 && strcmp(argv[3], argv[4]) != 0) {
David Howellsbc1c3732015-07-20 21:16:27 +0100276 dest_name = argv[4];
277 replace_orig = false;
278 } else {
279 ERR(asprintf(&dest_name, "%s.~signed~", module_name) < 0,
280 "asprintf");
281 replace_orig = true;
282 }
283
David Howells283e8ba2015-09-25 16:31:46 +0100284#ifdef USE_PKCS7
285 if (strcmp(hash_algo, "sha1") != 0) {
286 fprintf(stderr, "sign-file: %s only supports SHA1 signing\n",
287 OPENSSL_VERSION_TEXT);
288 exit(3);
289 }
290#endif
291
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100292 /* Open the module file */
293 bm = BIO_new_file(module_name, "rb");
294 ERR(!bm, "%s", module_name);
David Woodhouse6e3e2812015-07-20 21:16:29 +0100295
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100296 if (!raw_sig) {
297 /* Read the private key and the X.509 cert the PKCS#7 message
298 * will point to.
299 */
300 private_key = read_private_key(private_key_name);
301 x509 = read_x509(x509_name);
David Howellsbc1c3732015-07-20 21:16:27 +0100302
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100303 /* Digest the module data. */
304 OpenSSL_add_all_digests();
305 display_openssl_errors(__LINE__);
306 digest_algo = EVP_get_digestbyname(hash_algo);
307 ERR(!digest_algo, "EVP_get_digestbyname");
308
309#ifndef USE_PKCS7
310 /* Load the signature message from the digest buffer. */
311 cms = CMS_sign(NULL, NULL, NULL, NULL,
312 CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY |
313 CMS_DETACHED | CMS_STREAM);
314 ERR(!cms, "CMS_sign");
315
316 ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo,
317 CMS_NOCERTS | CMS_BINARY |
318 CMS_NOSMIMECAP | use_keyid |
319 use_signed_attrs),
320 "CMS_add1_signer");
321 ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0,
322 "CMS_final");
323
324#else
325 pkcs7 = PKCS7_sign(x509, private_key, NULL, bm,
326 PKCS7_NOCERTS | PKCS7_BINARY |
327 PKCS7_DETACHED | use_signed_attrs);
328 ERR(!pkcs7, "PKCS7_sign");
329#endif
330
331 if (save_sig) {
332 char *sig_file_name;
333 BIO *b;
334
335 ERR(asprintf(&sig_file_name, "%s.p7s", module_name) < 0,
336 "asprintf");
337 b = BIO_new_file(sig_file_name, "wb");
338 ERR(!b, "%s", sig_file_name);
339#ifndef USE_PKCS7
340 ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0,
341 "%s", sig_file_name);
342#else
343 ERR(i2d_PKCS7_bio(b, pkcs7) < 0,
344 "%s", sig_file_name);
345#endif
346 BIO_free(b);
347 }
348
349 if (sign_only) {
350 BIO_free(bm);
351 return 0;
352 }
David Howellsbc1c3732015-07-20 21:16:27 +0100353 }
David Howellsbc1c3732015-07-20 21:16:27 +0100354
355 /* Open the destination file now so that we can shovel the module data
356 * across as we read it.
357 */
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100358 bd = BIO_new_file(dest_name, "wb");
359 ERR(!bd, "%s", dest_name);
Luis R. Rodriguez23dfbba2015-07-20 21:16:27 +0100360
David Howellsbc1c3732015-07-20 21:16:27 +0100361 /* Append the marker and the PKCS#7 message to the destination file */
362 ERR(BIO_reset(bm) < 0, "%s", module_name);
363 while ((n = BIO_read(bm, buf, sizeof(buf))),
364 n > 0) {
365 ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name);
366 }
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100367 BIO_free(bm);
David Howellsbc1c3732015-07-20 21:16:27 +0100368 ERR(n < 0, "%s", module_name);
369 module_size = BIO_number_written(bd);
370
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100371 if (!raw_sig) {
David Howells283e8ba2015-09-25 16:31:46 +0100372#ifndef USE_PKCS7
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100373 ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name);
David Howells283e8ba2015-09-25 16:31:46 +0100374#else
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100375 ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name);
David Howells283e8ba2015-09-25 16:31:46 +0100376#endif
Juerg Haefligere5a2e3c2016-02-04 12:09:25 +0100377 } else {
378 BIO *b;
379
380 /* Read the raw signature file and write the data to the
381 * destination file
382 */
383 b = BIO_new_file(raw_sig_name, "rb");
384 ERR(!b, "%s", raw_sig_name);
385 while ((n = BIO_read(b, buf, sizeof(buf))), n > 0)
386 ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name);
387 BIO_free(b);
388 }
389
David Howells283e8ba2015-09-25 16:31:46 +0100390 sig_size = BIO_number_written(bd) - module_size;
391 sig_info.sig_len = htonl(sig_size);
David Howellsbc1c3732015-07-20 21:16:27 +0100392 ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name);
393 ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name);
394
395 ERR(BIO_free(bd) < 0, "%s", dest_name);
396
397 /* Finally, if we're signing in place, replace the original. */
398 if (replace_orig)
399 ERR(rename(dest_name, module_name) < 0, "%s", dest_name);
400
401 return 0;
402}