sign-file.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/bio.h>
  24. #include <openssl/evp.h>
  25. #include <openssl/pem.h>
  26. #include <openssl/cms.h>
  27. #include <openssl/err.h>
  28. #include <openssl/engine.h>
  29. struct module_signature {
  30. uint8_t algo; /* Public-key crypto algorithm [0] */
  31. uint8_t hash; /* Digest algorithm [0] */
  32. uint8_t id_type; /* Key identifier type [PKEY_ID_PKCS7] */
  33. uint8_t signer_len; /* Length of signer's name [0] */
  34. uint8_t key_id_len; /* Length of key identifier [0] */
  35. uint8_t __pad[3];
  36. uint32_t sig_len; /* Length of signature data */
  37. };
  38. #define PKEY_ID_PKCS7 2
  39. static char magic_number[] = "~Module signature appended~\n";
  40. static __attribute__((noreturn))
  41. void format(void)
  42. {
  43. fprintf(stderr,
  44. "Usage: scripts/sign-file [-dp] <hash algo> <key> <x509> <module> [<dest>]\n");
  45. exit(2);
  46. }
  47. static void display_openssl_errors(int l)
  48. {
  49. const char *file;
  50. char buf[120];
  51. int e, line;
  52. if (ERR_peek_error() == 0)
  53. return;
  54. fprintf(stderr, "At main.c:%d:\n", l);
  55. while ((e = ERR_get_error_line(&file, &line))) {
  56. ERR_error_string(e, buf);
  57. fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
  58. }
  59. }
  60. static void drain_openssl_errors(void)
  61. {
  62. const char *file;
  63. int line;
  64. if (ERR_peek_error() == 0)
  65. return;
  66. while (ERR_get_error_line(&file, &line)) {}
  67. }
  68. #define ERR(cond, fmt, ...) \
  69. do { \
  70. bool __cond = (cond); \
  71. display_openssl_errors(__LINE__); \
  72. if (__cond) { \
  73. err(1, fmt, ## __VA_ARGS__); \
  74. } \
  75. } while(0)
  76. static const char *key_pass;
  77. static int pem_pw_cb(char *buf, int len, int w, void *v)
  78. {
  79. int pwlen;
  80. if (!key_pass)
  81. return -1;
  82. pwlen = strlen(key_pass);
  83. if (pwlen >= len)
  84. return -1;
  85. strcpy(buf, key_pass);
  86. /* If it's wrong, don't keep trying it. */
  87. key_pass = NULL;
  88. return pwlen;
  89. }
  90. int main(int argc, char **argv)
  91. {
  92. struct module_signature sig_info = { .id_type = PKEY_ID_PKCS7 };
  93. char *hash_algo = NULL;
  94. char *private_key_name, *x509_name, *module_name, *dest_name;
  95. bool save_cms = false, replace_orig;
  96. bool sign_only = false;
  97. unsigned char buf[4096];
  98. unsigned long module_size, cms_size;
  99. unsigned int use_keyid = 0, use_signed_attrs = CMS_NOATTR;
  100. const EVP_MD *digest_algo;
  101. EVP_PKEY *private_key;
  102. CMS_ContentInfo *cms;
  103. X509 *x509;
  104. BIO *b, *bd = NULL, *bm;
  105. int opt, n;
  106. OpenSSL_add_all_algorithms();
  107. ERR_load_crypto_strings();
  108. ERR_clear_error();
  109. key_pass = getenv("KBUILD_SIGN_PIN");
  110. do {
  111. opt = getopt(argc, argv, "dpk");
  112. switch (opt) {
  113. case 'p': save_cms = true; break;
  114. case 'd': sign_only = true; save_cms = true; break;
  115. case 'k': use_keyid = CMS_USE_KEYID; break;
  116. case -1: break;
  117. default: format();
  118. }
  119. } while (opt != -1);
  120. argc -= optind;
  121. argv += optind;
  122. if (argc < 4 || argc > 5)
  123. format();
  124. hash_algo = argv[0];
  125. private_key_name = argv[1];
  126. x509_name = argv[2];
  127. module_name = argv[3];
  128. if (argc == 5) {
  129. dest_name = argv[4];
  130. replace_orig = false;
  131. } else {
  132. ERR(asprintf(&dest_name, "%s.~signed~", module_name) < 0,
  133. "asprintf");
  134. replace_orig = true;
  135. }
  136. /* Read the private key and the X.509 cert the PKCS#7 message
  137. * will point to.
  138. */
  139. if (!strncmp(private_key_name, "pkcs11:", 7)) {
  140. ENGINE *e;
  141. ENGINE_load_builtin_engines();
  142. drain_openssl_errors();
  143. e = ENGINE_by_id("pkcs11");
  144. ERR(!e, "Load PKCS#11 ENGINE");
  145. if (ENGINE_init(e))
  146. drain_openssl_errors();
  147. else
  148. ERR(1, "ENGINE_init");
  149. if (key_pass)
  150. ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
  151. private_key = ENGINE_load_private_key(e, private_key_name, NULL,
  152. NULL);
  153. ERR(!private_key, "%s", private_key_name);
  154. } else {
  155. b = BIO_new_file(private_key_name, "rb");
  156. ERR(!b, "%s", private_key_name);
  157. private_key = PEM_read_bio_PrivateKey(b, NULL, pem_pw_cb, NULL);
  158. ERR(!private_key, "%s", private_key_name);
  159. BIO_free(b);
  160. }
  161. b = BIO_new_file(x509_name, "rb");
  162. ERR(!b, "%s", x509_name);
  163. x509 = d2i_X509_bio(b, NULL); /* Binary encoded X.509 */
  164. if (!x509) {
  165. ERR(BIO_reset(b) != 1, "%s", x509_name);
  166. x509 = PEM_read_bio_X509(b, NULL, NULL, NULL); /* PEM encoded X.509 */
  167. if (x509)
  168. drain_openssl_errors();
  169. }
  170. BIO_free(b);
  171. ERR(!x509, "%s", x509_name);
  172. /* Open the destination file now so that we can shovel the module data
  173. * across as we read it.
  174. */
  175. if (!sign_only) {
  176. bd = BIO_new_file(dest_name, "wb");
  177. ERR(!bd, "%s", dest_name);
  178. }
  179. /* Digest the module data. */
  180. OpenSSL_add_all_digests();
  181. display_openssl_errors(__LINE__);
  182. digest_algo = EVP_get_digestbyname(hash_algo);
  183. ERR(!digest_algo, "EVP_get_digestbyname");
  184. bm = BIO_new_file(module_name, "rb");
  185. ERR(!bm, "%s", module_name);
  186. /* Load the CMS message from the digest buffer. */
  187. cms = CMS_sign(NULL, NULL, NULL, NULL,
  188. CMS_NOCERTS | CMS_PARTIAL | CMS_BINARY | CMS_DETACHED | CMS_STREAM);
  189. ERR(!cms, "CMS_sign");
  190. ERR(!CMS_add1_signer(cms, x509, private_key, digest_algo,
  191. CMS_NOCERTS | CMS_BINARY | CMS_NOSMIMECAP |
  192. use_keyid | use_signed_attrs),
  193. "CMS_sign_add_signer");
  194. ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0,
  195. "CMS_final");
  196. if (save_cms) {
  197. char *cms_name;
  198. ERR(asprintf(&cms_name, "%s.p7s", module_name) < 0, "asprintf");
  199. b = BIO_new_file(cms_name, "wb");
  200. ERR(!b, "%s", cms_name);
  201. ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, "%s", cms_name);
  202. BIO_free(b);
  203. }
  204. if (sign_only)
  205. return 0;
  206. /* Append the marker and the PKCS#7 message to the destination file */
  207. ERR(BIO_reset(bm) < 0, "%s", module_name);
  208. while ((n = BIO_read(bm, buf, sizeof(buf))),
  209. n > 0) {
  210. ERR(BIO_write(bd, buf, n) < 0, "%s", dest_name);
  211. }
  212. ERR(n < 0, "%s", module_name);
  213. module_size = BIO_number_written(bd);
  214. ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name);
  215. cms_size = BIO_number_written(bd) - module_size;
  216. sig_info.sig_len = htonl(cms_size);
  217. ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name);
  218. ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name);
  219. ERR(BIO_free(bd) < 0, "%s", dest_name);
  220. /* Finally, if we're signing in place, replace the original. */
  221. if (replace_orig)
  222. ERR(rename(dest_name, module_name) < 0, "%s", dest_name);
  223. return 0;
  224. }