extract-cert.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Extract X.509 certificate in DER form from PKCS#11 or PEM.
  2. *
  3. * Copyright © 2014 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 General Public Licence
  11. * as published by the Free Software Foundation; either version
  12. * 2 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/pkcs7.h>
  27. #include <openssl/err.h>
  28. #include <openssl/engine.h>
  29. #define PKEY_ID_PKCS7 2
  30. static __attribute__((noreturn))
  31. void format(void)
  32. {
  33. fprintf(stderr,
  34. "Usage: scripts/extract-cert <source> <dest>\n");
  35. exit(2);
  36. }
  37. static void display_openssl_errors(int l)
  38. {
  39. const char *file;
  40. char buf[120];
  41. int e, line;
  42. if (ERR_peek_error() == 0)
  43. return;
  44. fprintf(stderr, "At main.c:%d:\n", l);
  45. while ((e = ERR_get_error_line(&file, &line))) {
  46. ERR_error_string(e, buf);
  47. fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
  48. }
  49. }
  50. static void drain_openssl_errors(void)
  51. {
  52. const char *file;
  53. int line;
  54. if (ERR_peek_error() == 0)
  55. return;
  56. while (ERR_get_error_line(&file, &line)) {}
  57. }
  58. #define ERR(cond, fmt, ...) \
  59. do { \
  60. bool __cond = (cond); \
  61. display_openssl_errors(__LINE__); \
  62. if (__cond) { \
  63. err(1, fmt, ## __VA_ARGS__); \
  64. } \
  65. } while(0)
  66. static const char *key_pass;
  67. int main(int argc, char **argv)
  68. {
  69. char *cert_src, *cert_dst;
  70. X509 *x509;
  71. BIO *b;
  72. OpenSSL_add_all_algorithms();
  73. ERR_load_crypto_strings();
  74. ERR_clear_error();
  75. key_pass = getenv("KBUILD_SIGN_PIN");
  76. if (argc != 3)
  77. format();
  78. cert_src = argv[1];
  79. cert_dst = argv[2];
  80. if (!strncmp(cert_src, "pkcs11:", 7)) {
  81. ENGINE *e;
  82. struct {
  83. const char *cert_id;
  84. X509 *cert;
  85. } parms;
  86. parms.cert_id = cert_src;
  87. parms.cert = NULL;
  88. ENGINE_load_builtin_engines();
  89. drain_openssl_errors();
  90. e = ENGINE_by_id("pkcs11");
  91. ERR(!e, "Load PKCS#11 ENGINE");
  92. if (ENGINE_init(e))
  93. drain_openssl_errors();
  94. else
  95. ERR(1, "ENGINE_init");
  96. if (key_pass)
  97. ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
  98. ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1);
  99. ERR(!parms.cert, "Get X.509 from PKCS#11");
  100. x509 = parms.cert;
  101. } else {
  102. b = BIO_new_file(cert_src, "rb");
  103. ERR(!b, "%s", cert_src);
  104. x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
  105. ERR(!x509, "%s", cert_src);
  106. BIO_free(b);
  107. }
  108. b = BIO_new_file(cert_dst, "wb");
  109. ERR(!b, "%s", cert_dst);
  110. ERR(!i2d_X509_bio(b, x509), cert_dst);
  111. BIO_free(b);
  112. return 0;
  113. }