sign-file.c 6.5 KB

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