1 /* Sign a module file using the given key.
3 * Copyright © 2014-2016 Red Hat, Inc. All Rights Reserved.
4 * Copyright © 2015 Intel Corporation.
5 * Copyright © 2016 Hewlett Packard Enterprise Development LP
7 * Authors: David Howells <dhowells@redhat.com>
8 * David Woodhouse <dwmw2@infradead.org>
9 * Juerg Haefliger <juerg.haefliger@hpe.com>
11 * This program is free software; you can redistribute it and/or
12 * 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.
24 #include <arpa/inet.h>
25 #include <openssl/opensslv.h>
26 #include <openssl/bio.h>
27 #include <openssl/evp.h>
28 #include <openssl/pem.h>
29 #include <openssl/err.h>
30 #if OPENSSL_VERSION_MAJOR >= 3
31 # define USE_PKCS11_PROVIDER
32 # include <openssl/provider.h>
33 # include <openssl/store.h>
35 # if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
36 # define USE_PKCS11_ENGINE
37 # include <openssl/engine.h>
40 #include "ssl-common.h"
43 * Use CMS if we have openssl-1.0.0 or newer available - otherwise we have to
44 * assume that it's not available and its header file is missing and that we
45 * should use PKCS#7 instead. Switching to the older PKCS#7 format restricts
46 * the options we have on specifying the X.509 certificate we want.
48 * Further, older versions of OpenSSL don't support manually adding signers to
49 * the PKCS#7 message so have to accept that we get a certificate included in
50 * the signature message. Nor do such older versions of OpenSSL support
51 * signing with anything other than SHA1 - so we're stuck with that if such is
54 #if defined(LIBRESSL_VERSION_NUMBER) || \
55 OPENSSL_VERSION_NUMBER < 0x10000000L || \
56 defined(OPENSSL_NO_CMS)
60 #include <openssl/cms.h>
62 #include <openssl/pkcs7.h>
65 struct module_signature
{
66 uint8_t algo
; /* Public-key crypto algorithm [0] */
67 uint8_t hash
; /* Digest algorithm [0] */
68 uint8_t id_type
; /* Key identifier type [PKEY_ID_PKCS7] */
69 uint8_t signer_len
; /* Length of signer's name [0] */
70 uint8_t key_id_len
; /* Length of key identifier [0] */
72 uint32_t sig_len
; /* Length of signature data */
75 #define PKEY_ID_PKCS7 2
77 static char magic_number
[] = "~Module signature appended~\n";
79 static __attribute__((noreturn
))
83 "Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n");
85 " scripts/sign-file -s <raw sig> <hash algo> <x509> <module> [<dest>]\n");
89 static const char *key_pass
;
91 static int pem_pw_cb(char *buf
, int len
, int w
, void *v
)
98 pwlen
= strlen(key_pass
);
102 strcpy(buf
, key_pass
);
104 /* If it's wrong, don't keep trying it. */
110 static EVP_PKEY
*read_private_key_pkcs11(const char *private_key_name
)
112 EVP_PKEY
*private_key
= NULL
;
113 #ifdef USE_PKCS11_PROVIDER
114 OSSL_STORE_CTX
*store
;
116 if (!OSSL_PROVIDER_try_load(NULL
, "pkcs11", true))
117 ERR(1, "OSSL_PROVIDER_try_load(pkcs11)");
118 if (!OSSL_PROVIDER_try_load(NULL
, "default", true))
119 ERR(1, "OSSL_PROVIDER_try_load(default)");
121 store
= OSSL_STORE_open(private_key_name
, NULL
, NULL
, NULL
, NULL
);
122 ERR(!store
, "OSSL_STORE_open");
124 while (!OSSL_STORE_eof(store
)) {
125 OSSL_STORE_INFO
*info
= OSSL_STORE_load(store
);
128 drain_openssl_errors(__LINE__
, 0);
131 if (OSSL_STORE_INFO_get_type(info
) == OSSL_STORE_INFO_PKEY
) {
132 private_key
= OSSL_STORE_INFO_get1_PKEY(info
);
133 ERR(!private_key
, "OSSL_STORE_INFO_get1_PKEY");
135 OSSL_STORE_INFO_free(info
);
139 OSSL_STORE_close(store
);
140 #elif defined(USE_PKCS11_ENGINE)
143 ENGINE_load_builtin_engines();
144 drain_openssl_errors(__LINE__
, 1);
145 e
= ENGINE_by_id("pkcs11");
146 ERR(!e
, "Load PKCS#11 ENGINE");
148 drain_openssl_errors(__LINE__
, 1);
150 ERR(1, "ENGINE_init");
152 ERR(!ENGINE_ctrl_cmd_string(e
, "PIN", key_pass
, 0), "Set PKCS#11 PIN");
153 private_key
= ENGINE_load_private_key(e
, private_key_name
, NULL
, NULL
);
154 ERR(!private_key
, "%s", private_key_name
);
156 fprintf(stderr
, "no pkcs11 engine/provider available\n");
162 static EVP_PKEY
*read_private_key(const char *private_key_name
)
164 if (!strncmp(private_key_name
, "pkcs11:", 7)) {
165 return read_private_key_pkcs11(private_key_name
);
167 EVP_PKEY
*private_key
;
170 b
= BIO_new_file(private_key_name
, "rb");
171 ERR(!b
, "%s", private_key_name
);
172 private_key
= PEM_read_bio_PrivateKey(b
, NULL
, pem_pw_cb
,
174 ERR(!private_key
, "%s", private_key_name
);
181 static X509
*read_x509(const char *x509_name
)
183 unsigned char buf
[2];
188 b
= BIO_new_file(x509_name
, "rb");
189 ERR(!b
, "%s", x509_name
);
191 /* Look at the first two bytes of the file to determine the encoding */
192 n
= BIO_read(b
, buf
, 2);
194 if (BIO_should_retry(b
)) {
195 fprintf(stderr
, "%s: Read wanted retry\n", x509_name
);
199 fprintf(stderr
, "%s: Short read\n", x509_name
);
202 ERR(1, "%s", x509_name
);
205 ERR(BIO_reset(b
) != 0, "%s", x509_name
);
207 if (buf
[0] == 0x30 && buf
[1] >= 0x81 && buf
[1] <= 0x84)
208 /* Assume raw DER encoded X.509 */
209 x509
= d2i_X509_bio(b
, NULL
);
211 /* Assume PEM encoded X.509 */
212 x509
= PEM_read_bio_X509(b
, NULL
, NULL
, NULL
);
215 ERR(!x509
, "%s", x509_name
);
220 int main(int argc
, char **argv
)
222 struct module_signature sig_info
= { .id_type
= PKEY_ID_PKCS7
};
223 char *hash_algo
= NULL
;
224 char *private_key_name
= NULL
, *raw_sig_name
= NULL
;
225 char *x509_name
, *module_name
, *dest_name
;
226 bool save_sig
= false, replace_orig
;
227 bool sign_only
= false;
228 bool raw_sig
= false;
229 unsigned char buf
[4096];
230 unsigned long module_size
, sig_size
;
231 unsigned int use_signed_attrs
;
232 const EVP_MD
*digest_algo
;
233 EVP_PKEY
*private_key
;
235 CMS_ContentInfo
*cms
= NULL
;
236 unsigned int use_keyid
= 0;
243 OpenSSL_add_all_algorithms();
244 ERR_load_crypto_strings();
247 key_pass
= getenv("KBUILD_SIGN_PIN");
250 use_signed_attrs
= CMS_NOATTR
;
252 use_signed_attrs
= PKCS7_NOATTR
;
256 opt
= getopt(argc
, argv
, "sdpk");
258 case 's': raw_sig
= true; break;
259 case 'p': save_sig
= true; break;
260 case 'd': sign_only
= true; save_sig
= true; break;
262 case 'k': use_keyid
= CMS_USE_KEYID
; break;
271 if (argc
< 4 || argc
> 5)
275 raw_sig_name
= argv
[0];
279 private_key_name
= argv
[1];
282 module_name
= argv
[3];
283 if (argc
== 5 && strcmp(argv
[3], argv
[4]) != 0) {
285 replace_orig
= false;
287 ERR(asprintf(&dest_name
, "%s.~signed~", module_name
) < 0,
293 if (strcmp(hash_algo
, "sha1") != 0) {
294 fprintf(stderr
, "sign-file: %s only supports SHA1 signing\n",
295 OPENSSL_VERSION_TEXT
);
300 /* Open the module file */
301 bm
= BIO_new_file(module_name
, "rb");
302 ERR(!bm
, "%s", module_name
);
305 /* Read the private key and the X.509 cert the PKCS#7 message
308 private_key
= read_private_key(private_key_name
);
309 x509
= read_x509(x509_name
);
311 /* Digest the module data. */
312 OpenSSL_add_all_digests();
313 drain_openssl_errors(__LINE__
, 0);
314 digest_algo
= EVP_get_digestbyname(hash_algo
);
315 ERR(!digest_algo
, "EVP_get_digestbyname");
318 /* Load the signature message from the digest buffer. */
319 cms
= CMS_sign(NULL
, NULL
, NULL
, NULL
,
320 CMS_NOCERTS
| CMS_PARTIAL
| CMS_BINARY
|
321 CMS_DETACHED
| CMS_STREAM
);
322 ERR(!cms
, "CMS_sign");
324 ERR(!CMS_add1_signer(cms
, x509
, private_key
, digest_algo
,
325 CMS_NOCERTS
| CMS_BINARY
|
326 CMS_NOSMIMECAP
| use_keyid
|
329 ERR(CMS_final(cms
, bm
, NULL
, CMS_NOCERTS
| CMS_BINARY
) != 1,
333 pkcs7
= PKCS7_sign(x509
, private_key
, NULL
, bm
,
334 PKCS7_NOCERTS
| PKCS7_BINARY
|
335 PKCS7_DETACHED
| use_signed_attrs
);
336 ERR(!pkcs7
, "PKCS7_sign");
343 ERR(asprintf(&sig_file_name
, "%s.p7s", module_name
) < 0,
345 b
= BIO_new_file(sig_file_name
, "wb");
346 ERR(!b
, "%s", sig_file_name
);
348 ERR(i2d_CMS_bio_stream(b
, cms
, NULL
, 0) != 1,
349 "%s", sig_file_name
);
351 ERR(i2d_PKCS7_bio(b
, pkcs7
) != 1,
352 "%s", sig_file_name
);
363 /* Open the destination file now so that we can shovel the module data
364 * across as we read it.
366 bd
= BIO_new_file(dest_name
, "wb");
367 ERR(!bd
, "%s", dest_name
);
369 /* Append the marker and the PKCS#7 message to the destination file */
370 ERR(BIO_reset(bm
) < 0, "%s", module_name
);
371 while ((n
= BIO_read(bm
, buf
, sizeof(buf
))),
373 ERR(BIO_write(bd
, buf
, n
) < 0, "%s", dest_name
);
376 ERR(n
< 0, "%s", module_name
);
377 module_size
= BIO_number_written(bd
);
381 ERR(i2d_CMS_bio_stream(bd
, cms
, NULL
, 0) != 1, "%s", dest_name
);
383 ERR(i2d_PKCS7_bio(bd
, pkcs7
) != 1, "%s", dest_name
);
388 /* Read the raw signature file and write the data to the
391 b
= BIO_new_file(raw_sig_name
, "rb");
392 ERR(!b
, "%s", raw_sig_name
);
393 while ((n
= BIO_read(b
, buf
, sizeof(buf
))), n
> 0)
394 ERR(BIO_write(bd
, buf
, n
) < 0, "%s", dest_name
);
398 sig_size
= BIO_number_written(bd
) - module_size
;
399 sig_info
.sig_len
= htonl(sig_size
);
400 ERR(BIO_write(bd
, &sig_info
, sizeof(sig_info
)) < 0, "%s", dest_name
);
401 ERR(BIO_write(bd
, magic_number
, sizeof(magic_number
) - 1) < 0, "%s", dest_name
);
403 ERR(BIO_free(bd
) != 1, "%s", dest_name
);
405 /* Finally, if we're signing in place, replace the original. */
407 ERR(rename(dest_name
, module_name
) < 0, "%s", dest_name
);