sign-file.c 10 KB

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