sign-file.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Sign a module file using the given key.
  2. *
  3. * Copyright © 2014-2015 Red Hat, Inc. All Rights Reserved.
  4. * Copyright © 2015 Intel Corporation.
  5. *
  6. * Authors: David Howells <dhowells@redhat.com>
  7. * David Woodhouse <dwmw2@infradead.org>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public License
  11. * as published by the Free Software Foundation; either version 2.1
  12. * of the licence, or (at your option) any later version.
  13. */
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <getopt.h>
  21. #include <err.h>
  22. #include <arpa/inet.h>
  23. #include <openssl/opensslv.h>
  24. #include <openssl/bio.h>
  25. #include <openssl/evp.h>
  26. #include <openssl/pem.h>
  27. #include <openssl/err.h>
  28. #include <openssl/engine.h>
  29. /*
  30. * Use CMS if we have openssl-1.0.0 or newer available - otherwise we have to
  31. * assume that it's not available and its header file is missing and that we
  32. * should use PKCS#7 instead. Switching to the older PKCS#7 format restricts
  33. * the options we have on specifying the X.509 certificate we want.
  34. *
  35. * Further, older versions of OpenSSL don't support manually adding signers to
  36. * the PKCS#7 message so have to accept that we get a certificate included in
  37. * the signature message. Nor do such older versions of OpenSSL support
  38. * signing with anything other than SHA1 - so we're stuck with that if such is
  39. * the case.
  40. */
  41. #if OPENSSL_VERSION_NUMBER < 0x10000000L
  42. #define USE_PKCS7
  43. #endif
  44. #ifndef USE_PKCS7
  45. #include <openssl/cms.h>
  46. #else
  47. #include <openssl/pkcs7.h>
  48. #endif
  49. struct module_signature {
  50. uint8_t algo; /* Public-key crypto algorithm [0] */
  51. uint8_t hash; /* Digest algorithm [0] */
  52. uint8_t id_type; /* Key identifier type [PKEY_ID_PKCS7] */
  53. uint8_t signer_len; /* Length of signer's name [0] */
  54. uint8_t key_id_len; /* Length of key identifier [0] */
  55. uint8_t __pad[3];
  56. uint32_t sig_len; /* Length of signature data */
  57. };
  58. #define PKEY_ID_PKCS7 2
  59. static char magic_number[] = "~Module signature appended~\n";
  60. static __attribute__((noreturn))
  61. void format(void)
  62. {
  63. fprintf(stderr,
  64. "Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n");
  65. exit(2);
  66. }
  67. static void display_openssl_errors(int l)
  68. {
  69. const char *file;
  70. char buf[120];
  71. int e, line;
  72. if (ERR_peek_error() == 0)
  73. return;
  74. fprintf(stderr, "At main.c:%d:\n", l);
  75. while ((e = ERR_get_error_line(&file, &line))) {
  76. ERR_error_string(e, buf);
  77. fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
  78. }
  79. }
  80. static void drain_openssl_errors(void)
  81. {
  82. const char *file;
  83. int line;
  84. if (ERR_peek_error() == 0)
  85. return;
  86. while (ERR_get_error_line(&file, &line)) {}
  87. }
  88. #define ERR(cond, fmt, ...) \
  89. do { \
  90. bool __cond = (cond); \
  91. display_openssl_errors(__LINE__); \
  92. if (__cond) { \
  93. err(1, fmt, ## __VA_ARGS__); \
  94. } \
  95. } while(0)
  96. static const char *key_pass;
  97. static int pem_pw_cb(char *buf, int len, int w, void *v)
  98. {
  99. int pwlen;
  100. if (!key_pass)
  101. return -1;
  102. pwlen = strlen(key_pass);
  103. if (pwlen >= len)
  104. return -1;
  105. strcpy(buf, key_pass);
  106. /* If it's wrong, don't keep trying it. */
  107. key_pass = NULL;
  108. return pwlen;
  109. }
  110. int main(int argc, char **argv)
  111. {
  112. struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 };
  113. char *hash_algo = NULL;
  114. char *private_key_name, *x509_name, *module_name, *dest_name;
  115. bool save_sig = false, replace_orig;
  116. bool sign_only = false;
  117. unsigned char buf[4096];
  118. unsigned long module_size, sig_size;
  119. unsigned int use_signed_attrs;
  120. const EVP_MD *digest_algo;
  121. EVP_PKEY *private_key;
  122. #ifndef USE_PKCS7
  123. CMS_ContentInfo *cms;
  124. unsigned int use_keyid = 0;
  125. #else
  126. PKCS7 *pkcs7;
  127. #endif
  128. X509 *x509;
  129. BIO *b, *bd = NULL, *bm;
  130. int opt, n;
  131. OpenSSL_add_all_algorithms();
  132. ERR_load_crypto_strings();
  133. ERR_clear_error();
  134. key_pass = getenv("KBUILD_SIGN_PIN");
  135. #ifndef USE_PKCS7
  136. use_signed_attrs = CMS_NOATTR;
  137. #else
  138. use_signed_attrs = PKCS7_NOATTR;
  139. #endif
  140. do {
  141. opt = getopt(argc, argv, "dpk");
  142. switch (opt) {
  143. case 'p': save_sig = true; break;
  144. case 'd': sign_only = true; save_sig = true; break;
  145. #ifndef USE_PKCS7
  146. case 'k': use_keyid = CMS_USE_KEYID; break;
  147. #endif
  148. case -1: break;
  149. default: format();
  150. }
  151. } while (opt != -1);
  152. argc -= optind;
  153. argv += optind;
  154. if (argc < 4 || argc > 5)
  155. format();
  156. hash_algo = argv[0];
  157. private_key_name = argv[1];
  158. x509_name = argv[2];
  159. module_name = argv[3];
  160. if (argc == 5) {
  161. dest_name = argv[4];
  162. replace_orig = false;
  163. } else {
  164. ERR(asprintf(&dest_name, "%s.~signed~", module_name) < 0,
  165. "asprintf");
  166. replace_orig = true;
  167. }
  168. #ifdef USE_PKCS7
  169. if (strcmp(hash_algo, "sha1") != 0) {
  170. fprintf(stderr, "sign-file: %s only supports SHA1 signing\n",
  171. OPENSSL_VERSION_TEXT);
  172. exit(3);
  173. }
  174. #endif
  175. /* Read the private key and the X.509 cert the PKCS#7 message
  176. * will point to.
  177. */
  178. if (!strncmp(private_key_name, "pkcs11:", 7)) {
  179. ENGINE *e;
  180. ENGINE_load_builtin_engines();
  181. drain_openssl_errors();
  182. e = ENGINE_by_id("pkcs11");
  183. ERR(!e, "Load PKCS#11 ENGINE");
  184. if (ENGINE_init(e))
  185. drain_openssl_errors();
  186. else
  187. ERR(1, "ENGINE_init");
  188. if (key_pass)
  189. ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
  190. private_key = ENGINE_load_private_key(e, private_key_name, NULL,
  191. NULL);
  192. ERR(!private_key, "%s", private_key_name);
  193. } else {
  194. b = BIO_new_file(private_key_name, "rb");
  195. ERR(!b, "%s", private_key_name);
  196. private_key = PEM_read_bio_PrivateKey(b, NULL, pem_pw_cb, NULL);
  197. ERR(!private_key, "%s", private_key_name);
  198. BIO_free(b);
  199. }
  200. b = BIO_new_file(x509_name, "rb");
  201. ERR(!b, "%s", x509_name);
  202. x509 = d2i_X509_bio(b, NULL); /* Binary encoded X.509 */
  203. if (!x509) {
  204. ERR(BIO_reset(b) != 1, "%s", x509_name);
  205. x509 = PEM_read_bio_X509(b, NULL, NULL, NULL); /* PEM encoded X.509 */
  206. if (x509)
  207. drain_openssl_errors();
  208. }
  209. BIO_free(b);
  210. ERR(!x509, "%s", x509_name);
  211. /* Open the destination file now so that we can shovel the module data
  212. * across as we read it.
  213. */
  214. if (!sign_only) {
  215. bd = BIO_new_file(dest_name, "wb");
  216. ERR(!bd, "%s", dest_name);
  217. }
  218. /* Digest the module data. */
  219. OpenSSL_add_all_digests();
  220. display_openssl_errors(__LINE__);
  221. digest_algo = EVP_get_digestbyname(hash_algo);
  222. ERR(!digest_algo, "EVP_get_digestbyname");
  223. bm = BIO_new_file(module_name, "rb");
  224. ERR(!bm, "%s", module_name);
  225. #ifndef USE_PKCS7
  226. /* Load the signature message from the digest buffer. */
  227. cms = CMS_sign(NULL, NULL, NULL, NULL,
  228. CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY | CMS_DETACHED | CMS_STREAM);
  229. ERR(!cms, "CMS_sign");
  230. ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo,
  231. CMS_NOCERTS | CMS_BINARY | CMS_NOSMIMECAP |
  232. use_keyid | use_signed_attrs),
  233. "CMS_add1_signer");
  234. ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0,
  235. "CMS_final");
  236. #else
  237. pkcs7 = PKCS7_sign(x509, private_key, NULL, bm,
  238. PKCS7_NOCERTS | PKCS7_BINARY |
  239. PKCS7_DETACHED | use_signed_attrs);
  240. ERR(!pkcs7, "PKCS7_sign");
  241. #endif
  242. if (save_sig) {
  243. char *sig_file_name;
  244. ERR(asprintf(&sig_file_name, "%s.p7s", module_name) < 0,
  245. "asprintf");
  246. b = BIO_new_file(sig_file_name, "wb");
  247. ERR(!b, "%s", sig_file_name);
  248. #ifndef USE_PKCS7
  249. ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0,
  250. "%s", sig_file_name);
  251. #else
  252. ERR(i2d_PKCS7_bio(b, pkcs7) < 0,
  253. "%s", sig_file_name);
  254. #endif
  255. BIO_free(b);
  256. }
  257. if (sign_only)
  258. return 0;
  259. /* Append the marker and the PKCS#7 message to the destination file */
  260. ERR(BIO_reset(bm) < 0, "%s", module_name);
  261. while ((n = BIO_read(bm, buf, sizeof(buf))),
  262. n > 0) {
  263. ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name);
  264. }
  265. ERR(n < 0, "%s", module_name);
  266. module_size = BIO_number_written(bd);
  267. #ifndef USE_PKCS7
  268. ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name);
  269. #else
  270. ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name);
  271. #endif
  272. sig_size = BIO_number_written(bd) - module_size;
  273. sig_info.sig_len = htonl(sig_size);
  274. ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name);
  275. ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name);
  276. ERR(BIO_free(bd) < 0, "%s", dest_name);
  277. /* Finally, if we're signing in place, replace the original. */
  278. if (replace_orig)
  279. ERR(rename(dest_name, module_name) < 0, "%s", dest_name);
  280. return 0;
  281. }