blob: 058bba3103e2707911cd493f70a1b39015f9d697 [file] [log] [blame]
David Howellsbc1c3732015-07-20 21:16:27 +01001/* Sign a module file using the given key.
2 *
3 * Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11#define _GNU_SOURCE
12#include <stdio.h>
13#include <stdlib.h>
14#include <stdint.h>
15#include <stdbool.h>
16#include <string.h>
17#include <getopt.h>
18#include <err.h>
19#include <arpa/inet.h>
20#include <openssl/bio.h>
21#include <openssl/evp.h>
22#include <openssl/pem.h>
David Howellsed8c2072015-07-20 21:16:33 +010023#include <openssl/cms.h>
David Howellsbc1c3732015-07-20 21:16:27 +010024#include <openssl/err.h>
David Woodhouse6e3e2812015-07-20 21:16:29 +010025#include <openssl/engine.h>
David Howellsbc1c3732015-07-20 21:16:27 +010026
27struct module_signature {
28 uint8_t algo; /* Public-key crypto algorithm [0] */
29 uint8_t hash; /* Digest algorithm [0] */
30 uint8_t id_type; /* Key identifier type [PKEY_ID_PKCS7] */
31 uint8_t signer_len; /* Length of signer's name [0] */
32 uint8_t key_id_len; /* Length of key identifier [0] */
33 uint8_t __pad[3];
34 uint32_t sig_len; /* Length of signature data */
35};
36
37#define PKEY_ID_PKCS7 2
38
39static char magic_number[] = "~Module signature appended~\n";
40
41static __attribute__((noreturn))
42void format(void)
43{
44 fprintf(stderr,
45 "Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n");
46 exit(2);
47}
48
49static void display_openssl_errors(int l)
50{
51 const char *file;
52 char buf[120];
53 int e, line;
54
55 if (ERR_peek_error() == 0)
56 return;
57 fprintf(stderr, "At main.c:%d:\n", l);
58
59 while ((e = ERR_get_error_line(&file, &line))) {
60 ERR_error_string(e, buf);
61 fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
62 }
63}
64
65static void drain_openssl_errors(void)
66{
67 const char *file;
68 int line;
69
70 if (ERR_peek_error() == 0)
71 return;
72 while (ERR_get_error_line(&file, &line)) {}
73}
74
75#define ERR(cond, fmt, ...) \
76 do { \
77 bool __cond = (cond); \
78 display_openssl_errors(__LINE__); \
79 if (__cond) { \
80 err(1, fmt, ## __VA_ARGS__); \
81 } \
82 } while(0)
83
David Woodhouseaf1eb292015-07-20 21:16:28 +010084static const char *key_pass;
85
86static int pem_pw_cb(char *buf, int len, int w, void *v)
87{
88 int pwlen;
89
90 if (!key_pass)
91 return -1;
92
93 pwlen = strlen(key_pass);
94 if (pwlen >= len)
95 return -1;
96
97 strcpy(buf, key_pass);
98
99 /* If it's wrong, don't keep trying it. */
100 key_pass = NULL;
101
102 return pwlen;
103}
104
David Howellsbc1c3732015-07-20 21:16:27 +0100105int main(int argc, char **argv)
106{
107 struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 };
108 char *hash_algo = NULL;
109 char *private_key_name, *x509_name, *module_name, *dest_name;
David Howellsed8c2072015-07-20 21:16:33 +0100110 bool save_cms = false, replace_orig;
Luis R. Rodriguez23dfbba2015-07-20 21:16:27 +0100111 bool sign_only = false;
David Howellsbc1c3732015-07-20 21:16:27 +0100112 unsigned char buf[4096];
David Howellsed8c2072015-07-20 21:16:33 +0100113 unsigned long module_size, cms_size;
David Howells99db4432015-08-05 15:22:27 +0100114 unsigned int use_keyid = 0, use_signed_attrs = CMS_NOATTR;
David Howellsbc1c3732015-07-20 21:16:27 +0100115 const EVP_MD *digest_algo;
116 EVP_PKEY *private_key;
David Howellsed8c2072015-07-20 21:16:33 +0100117 CMS_ContentInfo *cms;
David Howellsbc1c3732015-07-20 21:16:27 +0100118 X509 *x509;
Luis R. Rodriguez23dfbba2015-07-20 21:16:27 +0100119 BIO *b, *bd = NULL, *bm;
David Howellsbc1c3732015-07-20 21:16:27 +0100120 int opt, n;
121
David Woodhouseaf1eb292015-07-20 21:16:28 +0100122 OpenSSL_add_all_algorithms();
David Howellsbc1c3732015-07-20 21:16:27 +0100123 ERR_load_crypto_strings();
124 ERR_clear_error();
125
David Woodhouseaf1eb292015-07-20 21:16:28 +0100126 key_pass = getenv("KBUILD_SIGN_PIN");
127
David Howellsbc1c3732015-07-20 21:16:27 +0100128 do {
David Howellsed8c2072015-07-20 21:16:33 +0100129 opt = getopt(argc, argv, "dpk");
David Howellsbc1c3732015-07-20 21:16:27 +0100130 switch (opt) {
David Howellsed8c2072015-07-20 21:16:33 +0100131 case 'p': save_cms = true; break;
132 case 'd': sign_only = true; save_cms = true; break;
133 case 'k': use_keyid = CMS_USE_KEYID; break;
David Howellsbc1c3732015-07-20 21:16:27 +0100134 case -1: break;
135 default: format();
136 }
137 } while (opt != -1);
138
139 argc -= optind;
140 argv += optind;
141 if (argc < 4 || argc > 5)
142 format();
143
144 hash_algo = argv[0];
145 private_key_name = argv[1];
146 x509_name = argv[2];
147 module_name = argv[3];
148 if (argc == 5) {
149 dest_name = argv[4];
150 replace_orig = false;
151 } else {
152 ERR(asprintf(&dest_name, "%s.~signed~", module_name) < 0,
153 "asprintf");
154 replace_orig = true;
155 }
156
157 /* Read the private key and the X.509 cert the PKCS#7 message
158 * will point to.
159 */
David Woodhouse6e3e2812015-07-20 21:16:29 +0100160 if (!strncmp(private_key_name, "pkcs11:", 7)) {
161 ENGINE *e;
162
163 ENGINE_load_builtin_engines();
164 drain_openssl_errors();
165 e = ENGINE_by_id("pkcs11");
166 ERR(!e, "Load PKCS#11 ENGINE");
167 if (ENGINE_init(e))
168 drain_openssl_errors();
169 else
170 ERR(1, "ENGINE_init");
171 if (key_pass)
172 ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
173 private_key = ENGINE_load_private_key(e, private_key_name, NULL,
174 NULL);
175 ERR(!private_key, "%s", private_key_name);
176 } else {
177 b = BIO_new_file(private_key_name, "rb");
178 ERR(!b, "%s", private_key_name);
179 private_key = PEM_read_bio_PrivateKey(b, NULL, pem_pw_cb, NULL);
180 ERR(!private_key, "%s", private_key_name);
181 BIO_free(b);
182 }
David Howellsbc1c3732015-07-20 21:16:27 +0100183
184 b = BIO_new_file(x509_name, "rb");
185 ERR(!b, "%s", x509_name);
186 x509 = d2i_X509_bio(b, NULL); /* Binary encoded X.509 */
187 if (!x509) {
David Howellse9a5e8c2015-08-13 04:03:12 +0100188 ERR(BIO_reset(b) != 1, "%s", x509_name);
David Howellsbc1c3732015-07-20 21:16:27 +0100189 x509 = PEM_read_bio_X509(b, NULL, NULL, NULL); /* PEM encoded X.509 */
190 if (x509)
191 drain_openssl_errors();
192 }
193 BIO_free(b);
194 ERR(!x509, "%s", x509_name);
195
196 /* Open the destination file now so that we can shovel the module data
197 * across as we read it.
198 */
Luis R. Rodriguez23dfbba2015-07-20 21:16:27 +0100199 if (!sign_only) {
200 bd = BIO_new_file(dest_name, "wb");
201 ERR(!bd, "%s", dest_name);
202 }
David Howellsbc1c3732015-07-20 21:16:27 +0100203
204 /* Digest the module data. */
205 OpenSSL_add_all_digests();
206 display_openssl_errors(__LINE__);
207 digest_algo = EVP_get_digestbyname(hash_algo);
208 ERR(!digest_algo, "EVP_get_digestbyname");
209
210 bm = BIO_new_file(module_name, "rb");
211 ERR(!bm, "%s", module_name);
212
David Howellsed8c2072015-07-20 21:16:33 +0100213 /* Load the CMS message from the digest buffer. */
214 cms = CMS_sign(NULL, NULL, NULL, NULL,
215 CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY | CMS_DETACHED | CMS_STREAM);
216 ERR(!cms, "CMS_sign");
David Howellsbc1c3732015-07-20 21:16:27 +0100217
David Howellsed8c2072015-07-20 21:16:33 +0100218 ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo,
David Howells99db4432015-08-05 15:22:27 +0100219 CMS_NOCERTS | CMS_BINARY | CMS_NOSMIMECAP |
220 use_keyid | use_signed_attrs),
David Howellsed8c2072015-07-20 21:16:33 +0100221 "CMS_sign_add_signer");
222 ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0,
223 "CMS_final");
David Howellsbc1c3732015-07-20 21:16:27 +0100224
David Howellsed8c2072015-07-20 21:16:33 +0100225 if (save_cms) {
226 char *cms_name;
David Howellsbc1c3732015-07-20 21:16:27 +0100227
David Howellsed8c2072015-07-20 21:16:33 +0100228 ERR(asprintf(&cms_name, "%s.p7s", module_name) < 0, "asprintf");
229 b = BIO_new_file(cms_name, "wb");
230 ERR(!b, "%s", cms_name);
231 ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, "%s", cms_name);
David Howellsbc1c3732015-07-20 21:16:27 +0100232 BIO_free(b);
233 }
234
Luis R. Rodriguez23dfbba2015-07-20 21:16:27 +0100235 if (sign_only)
236 return 0;
237
David Howellsbc1c3732015-07-20 21:16:27 +0100238 /* Append the marker and the PKCS#7 message to the destination file */
239 ERR(BIO_reset(bm) < 0, "%s", module_name);
240 while ((n = BIO_read(bm, buf, sizeof(buf))),
241 n > 0) {
242 ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name);
243 }
244 ERR(n < 0, "%s", module_name);
245 module_size = BIO_number_written(bd);
246
David Howellsed8c2072015-07-20 21:16:33 +0100247 ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name);
248 cms_size = BIO_number_written(bd) - module_size;
249 sig_info.sig_len = htonl(cms_size);
David Howellsbc1c3732015-07-20 21:16:27 +0100250 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);
252
253 ERR(BIO_free(bd) < 0, "%s", dest_name);
254
255 /* Finally, if we're signing in place, replace the original. */
256 if (replace_orig)
257 ERR(rename(dest_name, module_name) < 0, "%s", dest_name);
258
259 return 0;
260}