sign-file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* Sign a module file using the given key.
  2. *
  3. * Copyright © 2014-2016 Red Hat, Inc. All Rights Reserved.
  4. * Copyright © 2015 Intel Corporation.
  5. * Copyright © 2016 Hewlett Packard Enterprise Development LP
  6. *
  7. * Authors: David Howells <dhowells@redhat.com>
  8. * David Woodhouse <dwmw2@infradead.org>
  9. * Juerg Haefliger <juerg.haefliger@hpe.com>
  10. *
  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.
  15. */
  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>
  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>
  34. #else
  35. # if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  36. # define USE_PKCS11_ENGINE
  37. # include <openssl/engine.h>
  38. # endif
  39. #endif
  40. #include "ssl-common.h"
  41. /*
  42. * Use CMS if we have openssl-1.0.0 or newer available - otherwise we have to
  43. * assume that it's not available and its header file is missing and that we
  44. * should use PKCS#7 instead. Switching to the older PKCS#7 format restricts
  45. * the options we have on specifying the X.509 certificate we want.
  46. *
  47. * Further, older versions of OpenSSL don't support manually adding signers to
  48. * the PKCS#7 message so have to accept that we get a certificate included in
  49. * the signature message. Nor do such older versions of OpenSSL support
  50. * signing with anything other than SHA1 - so we're stuck with that if such is
  51. * the case.
  52. */
  53. #if defined(LIBRESSL_VERSION_NUMBER) || \
  54. OPENSSL_VERSION_NUMBER < 0x10000000L || \
  55. defined(OPENSSL_NO_CMS)
  56. #define USE_PKCS7
  57. #endif
  58. #ifndef USE_PKCS7
  59. #include <openssl/cms.h>
  60. #else
  61. #include <openssl/pkcs7.h>
  62. #endif
  63. struct module_signature {
  64. uint8_t algo; /* Public-key crypto algorithm [0] */
  65. uint8_t hash; /* Digest algorithm [0] */
  66. uint8_t id_type; /* Key identifier type [PKEY_ID_PKCS7] */
  67. uint8_t signer_len; /* Length of signer's name [0] */
  68. uint8_t key_id_len; /* Length of key identifier [0] */
  69. uint8_t __pad[3];
  70. uint32_t sig_len; /* Length of signature data */
  71. };
  72. #define PKEY_ID_PKCS7 2
  73. static char magic_number[] = "~Module signature appended~\n";
  74. static __attribute__((noreturn))
  75. void format(void)
  76. {
  77. fprintf(stderr,
  78. "Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n");
  79. fprintf(stderr,
  80. " scripts/sign-file -s <raw sig> <hash algo> <x509> <module> [<dest>]\n");
  81. exit(2);
  82. }
  83. static const char *key_pass;
  84. static int pem_pw_cb(char *buf, int len, int w, void *v)
  85. {
  86. int pwlen;
  87. if (!key_pass)
  88. return -1;
  89. pwlen = strlen(key_pass);
  90. if (pwlen >= len)
  91. return -1;
  92. strcpy(buf, key_pass);
  93. /* If it's wrong, don't keep trying it. */
  94. key_pass = NULL;
  95. return pwlen;
  96. }
  97. static EVP_PKEY *read_private_key_pkcs11(const char *private_key_name)
  98. {
  99. EVP_PKEY *private_key = NULL;
  100. #ifdef USE_PKCS11_PROVIDER
  101. OSSL_STORE_CTX *store;
  102. if (!OSSL_PROVIDER_try_load(NULL, "pkcs11", true))
  103. ERR(1, "OSSL_PROVIDER_try_load(pkcs11)");
  104. if (!OSSL_PROVIDER_try_load(NULL, "default", true))
  105. ERR(1, "OSSL_PROVIDER_try_load(default)");
  106. store = OSSL_STORE_open(private_key_name, NULL, NULL, NULL, NULL);
  107. ERR(!store, "OSSL_STORE_open");
  108. while (!OSSL_STORE_eof(store)) {
  109. OSSL_STORE_INFO *info = OSSL_STORE_load(store);
  110. if (!info) {
  111. drain_openssl_errors(__LINE__, 0);
  112. continue;
  113. }
  114. if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) {
  115. private_key = OSSL_STORE_INFO_get1_PKEY(info);
  116. ERR(!private_key, "OSSL_STORE_INFO_get1_PKEY");
  117. }
  118. OSSL_STORE_INFO_free(info);
  119. if (private_key)
  120. break;
  121. }
  122. OSSL_STORE_close(store);
  123. #elif defined(USE_PKCS11_ENGINE)
  124. ENGINE *e;
  125. ENGINE_load_builtin_engines();
  126. drain_openssl_errors(__LINE__, 1);
  127. e = ENGINE_by_id("pkcs11");
  128. ERR(!e, "Load PKCS#11 ENGINE");
  129. if (ENGINE_init(e))
  130. drain_openssl_errors(__LINE__, 1);
  131. else
  132. ERR(1, "ENGINE_init");
  133. if (key_pass)
  134. ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
  135. private_key = ENGINE_load_private_key(e, private_key_name, NULL, NULL);
  136. ERR(!private_key, "%s", private_key_name);
  137. #else
  138. fprintf(stderr, "no pkcs11 engine/provider available\n");
  139. exit(1);
  140. #endif
  141. return private_key;
  142. }
  143. static EVP_PKEY *read_private_key(const char *private_key_name)
  144. {
  145. if (!strncmp(private_key_name, "pkcs11:", 7)) {
  146. return read_private_key_pkcs11(private_key_name);
  147. } else {
  148. EVP_PKEY *private_key;
  149. BIO *b;
  150. b = BIO_new_file(private_key_name, "rb");
  151. ERR(!b, "%s", private_key_name);
  152. private_key = PEM_read_bio_PrivateKey(b, NULL, pem_pw_cb,
  153. NULL);
  154. ERR(!private_key, "%s", private_key_name);
  155. BIO_free(b);
  156. return private_key;
  157. }
  158. }
  159. static X509 *read_x509(const char *x509_name)
  160. {
  161. unsigned char buf[2];
  162. X509 *x509;
  163. BIO *b;
  164. int n;
  165. b = BIO_new_file(x509_name, "rb");
  166. ERR(!b, "%s", x509_name);
  167. /* Look at the first two bytes of the file to determine the encoding */
  168. n = BIO_read(b, buf, 2);
  169. if (n != 2) {
  170. if (BIO_should_retry(b)) {
  171. fprintf(stderr, "%s: Read wanted retry\n", x509_name);
  172. exit(1);
  173. }
  174. if (n >= 0) {
  175. fprintf(stderr, "%s: Short read\n", x509_name);
  176. exit(1);
  177. }
  178. ERR(1, "%s", x509_name);
  179. }
  180. ERR(BIO_reset(b) != 0, "%s", x509_name);
  181. if (buf[0] == 0x30 && buf[1] >= 0x81 && buf[1] <= 0x84)
  182. /* Assume raw DER encoded X.509 */
  183. x509 = d2i_X509_bio(b, NULL);
  184. else
  185. /* Assume PEM encoded X.509 */
  186. x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
  187. BIO_free(b);
  188. ERR(!x509, "%s", x509_name);
  189. return x509;
  190. }
  191. int main(int argc, char **argv)
  192. {
  193. struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 };
  194. char *hash_algo = NULL;
  195. char *private_key_name = NULL, *raw_sig_name = NULL;
  196. char *x509_name, *module_name, *dest_name;
  197. bool save_sig = false, replace_orig;
  198. bool sign_only = false;
  199. bool raw_sig = false;
  200. unsigned char buf[4096];
  201. unsigned long module_size, sig_size;
  202. unsigned int use_signed_attrs;
  203. const EVP_MD *digest_algo;
  204. EVP_PKEY *private_key;
  205. #ifndef USE_PKCS7
  206. CMS_ContentInfo *cms = NULL;
  207. unsigned int use_keyid = 0;
  208. #else
  209. PKCS7 *pkcs7 = NULL;
  210. #endif
  211. X509 *x509;
  212. BIO *bd, *bm;
  213. int opt, n;
  214. OpenSSL_add_all_algorithms();
  215. ERR_load_crypto_strings();
  216. ERR_clear_error();
  217. key_pass = getenv("KBUILD_SIGN_PIN");
  218. #ifndef USE_PKCS7
  219. use_signed_attrs = CMS_NOATTR;
  220. #else
  221. use_signed_attrs = PKCS7_NOATTR;
  222. #endif
  223. do {
  224. opt = getopt(argc, argv, "sdpk");
  225. switch (opt) {
  226. case 's': raw_sig = true; break;
  227. case 'p': save_sig = true; break;
  228. case 'd': sign_only = true; save_sig = true; break;
  229. #ifndef USE_PKCS7
  230. case 'k': use_keyid = CMS_USE_KEYID; break;
  231. #endif
  232. case -1: break;
  233. default: format();
  234. }
  235. } while (opt != -1);
  236. argc -= optind;
  237. argv += optind;
  238. if (argc < 4 || argc > 5)
  239. format();
  240. if (raw_sig) {
  241. raw_sig_name = argv[0];
  242. hash_algo = argv[1];
  243. } else {
  244. hash_algo = argv[0];
  245. private_key_name = argv[1];
  246. }
  247. x509_name = argv[2];
  248. module_name = argv[3];
  249. if (argc == 5 && strcmp(argv[3], argv[4]) != 0) {
  250. dest_name = argv[4];
  251. replace_orig = false;
  252. } else {
  253. ERR(asprintf(&dest_name, "%s.~signed~", module_name) < 0,
  254. "asprintf");
  255. replace_orig = true;
  256. }
  257. #ifdef USE_PKCS7
  258. if (strcmp(hash_algo, "sha1") != 0) {
  259. fprintf(stderr, "sign-file: %s only supports SHA1 signing\n",
  260. OPENSSL_VERSION_TEXT);
  261. exit(3);
  262. }
  263. #endif
  264. /* Open the module file */
  265. bm = BIO_new_file(module_name, "rb");
  266. ERR(!bm, "%s", module_name);
  267. if (!raw_sig) {
  268. /* Read the private key and the X.509 cert the PKCS#7 message
  269. * will point to.
  270. */
  271. private_key = read_private_key(private_key_name);
  272. x509 = read_x509(x509_name);
  273. /* Digest the module data. */
  274. OpenSSL_add_all_digests();
  275. drain_openssl_errors(__LINE__, 0);
  276. digest_algo = EVP_get_digestbyname(hash_algo);
  277. ERR(!digest_algo, "EVP_get_digestbyname");
  278. #ifndef USE_PKCS7
  279. /* Load the signature message from the digest buffer. */
  280. cms = CMS_sign(NULL, NULL, NULL, NULL,
  281. CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY |
  282. CMS_DETACHED | CMS_STREAM);
  283. ERR(!cms, "CMS_sign");
  284. ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo,
  285. CMS_NOCERTS | CMS_BINARY |
  286. CMS_NOSMIMECAP | use_keyid |
  287. use_signed_attrs),
  288. "CMS_add1_signer");
  289. ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) != 1,
  290. "CMS_final");
  291. #else
  292. pkcs7 = PKCS7_sign(x509, private_key, NULL, bm,
  293. PKCS7_NOCERTS | PKCS7_BINARY |
  294. PKCS7_DETACHED | use_signed_attrs);
  295. ERR(!pkcs7, "PKCS7_sign");
  296. #endif
  297. if (save_sig) {
  298. char *sig_file_name;
  299. BIO *b;
  300. ERR(asprintf(&sig_file_name, "%s.p7s", module_name) < 0,
  301. "asprintf");
  302. b = BIO_new_file(sig_file_name, "wb");
  303. ERR(!b, "%s", sig_file_name);
  304. #ifndef USE_PKCS7
  305. ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) != 1,
  306. "%s", sig_file_name);
  307. #else
  308. ERR(i2d_PKCS7_bio(b, pkcs7) != 1,
  309. "%s", sig_file_name);
  310. #endif
  311. BIO_free(b);
  312. }
  313. if (sign_only) {
  314. BIO_free(bm);
  315. return 0;
  316. }
  317. }
  318. /* Open the destination file now so that we can shovel the module data
  319. * across as we read it.
  320. */
  321. bd = BIO_new_file(dest_name, "wb");
  322. ERR(!bd, "%s", dest_name);
  323. /* Append the marker and the PKCS#7 message to the destination file */
  324. ERR(BIO_reset(bm) < 0, "%s", module_name);
  325. while ((n = BIO_read(bm, buf, sizeof(buf))),
  326. n > 0) {
  327. ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name);
  328. }
  329. BIO_free(bm);
  330. ERR(n < 0, "%s", module_name);
  331. module_size = BIO_number_written(bd);
  332. if (!raw_sig) {
  333. #ifndef USE_PKCS7
  334. ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) != 1, "%s", dest_name);
  335. #else
  336. ERR(i2d_PKCS7_bio(bd, pkcs7) != 1, "%s", dest_name);
  337. #endif
  338. } else {
  339. BIO *b;
  340. /* Read the raw signature file and write the data to the
  341. * destination file
  342. */
  343. b = BIO_new_file(raw_sig_name, "rb");
  344. ERR(!b, "%s", raw_sig_name);
  345. while ((n = BIO_read(b, buf, sizeof(buf))), n > 0)
  346. ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name);
  347. BIO_free(b);
  348. }
  349. sig_size = BIO_number_written(bd) - module_size;
  350. sig_info.sig_len = htonl(sig_size);
  351. ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name);
  352. ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name);
  353. ERR(BIO_free(bd) != 1, "%s", dest_name);
  354. /* Finally, if we're signing in place, replace the original. */
  355. if (replace_orig)
  356. ERR(rename(dest_name, module_name) < 0, "%s", dest_name);
  357. return 0;
  358. }