extract-cert.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Extract X.509 certificate in DER form from PKCS#11 or PEM.
  2. *
  3. * Copyright © 2014-2015 Red Hat, Inc. All Rights Reserved.
  4. * Copyright © 2015 Intel Corporation.
  5. *
  6. * Authors: David Howells <[email protected]>
  7. * David Woodhouse <[email protected]>
  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 <err.h>
  21. #include <openssl/bio.h>
  22. #include <openssl/pem.h>
  23. #include <openssl/err.h>
  24. #include <openssl/engine.h>
  25. /*
  26. * OpenSSL 3.0 deprecates the OpenSSL's ENGINE API.
  27. *
  28. * Remove this if/when that API is no longer used
  29. */
  30. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  31. #define PKEY_ID_PKCS7 2
  32. static __attribute__((noreturn))
  33. void format(void)
  34. {
  35. fprintf(stderr,
  36. "Usage: extract-cert <source> <dest>\n");
  37. exit(2);
  38. }
  39. static void display_openssl_errors(int l)
  40. {
  41. const char *file;
  42. char buf[120];
  43. int e, line;
  44. if (ERR_peek_error() == 0)
  45. return;
  46. fprintf(stderr, "At main.c:%d:\n", l);
  47. while ((e = ERR_get_error_line(&file, &line))) {
  48. ERR_error_string(e, buf);
  49. fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
  50. }
  51. }
  52. #ifndef OPENSSL_IS_BORINGSSL
  53. static void drain_openssl_errors(void)
  54. {
  55. const char *file;
  56. int line;
  57. if (ERR_peek_error() == 0)
  58. return;
  59. while (ERR_get_error_line(&file, &line)) {}
  60. }
  61. #endif
  62. #define ERR(cond, fmt, ...) \
  63. do { \
  64. bool __cond = (cond); \
  65. display_openssl_errors(__LINE__); \
  66. if (__cond) { \
  67. err(1, fmt, ## __VA_ARGS__); \
  68. } \
  69. } while(0)
  70. static const char *key_pass;
  71. static BIO *wb;
  72. static char *cert_dst;
  73. static int kbuild_verbose;
  74. static void write_cert(X509 *x509)
  75. {
  76. char buf[200];
  77. if (!wb) {
  78. wb = BIO_new_file(cert_dst, "wb");
  79. ERR(!wb, "%s", cert_dst);
  80. }
  81. X509_NAME_oneline(X509_get_subject_name(x509), buf, sizeof(buf));
  82. ERR(!i2d_X509_bio(wb, x509), "%s", cert_dst);
  83. if (kbuild_verbose)
  84. fprintf(stderr, "Extracted cert: %s\n", buf);
  85. }
  86. int main(int argc, char **argv)
  87. {
  88. char *cert_src;
  89. OpenSSL_add_all_algorithms();
  90. ERR_load_crypto_strings();
  91. ERR_clear_error();
  92. kbuild_verbose = atoi(getenv("KBUILD_VERBOSE")?:"0");
  93. key_pass = getenv("KBUILD_SIGN_PIN");
  94. if (argc != 3)
  95. format();
  96. cert_src = argv[1];
  97. cert_dst = argv[2];
  98. if (!cert_src[0]) {
  99. /* Invoked with no input; create empty file */
  100. FILE *f = fopen(cert_dst, "wb");
  101. ERR(!f, "%s", cert_dst);
  102. fclose(f);
  103. exit(0);
  104. } else if (!strncmp(cert_src, "pkcs11:", 7)) {
  105. #ifdef OPENSSL_IS_BORINGSSL
  106. ERR(1, "BoringSSL does not support extracting from PKCS#11");
  107. exit(1);
  108. #else
  109. ENGINE *e;
  110. struct {
  111. const char *cert_id;
  112. X509 *cert;
  113. } parms;
  114. parms.cert_id = cert_src;
  115. parms.cert = NULL;
  116. ENGINE_load_builtin_engines();
  117. drain_openssl_errors();
  118. e = ENGINE_by_id("pkcs11");
  119. ERR(!e, "Load PKCS#11 ENGINE");
  120. if (ENGINE_init(e))
  121. drain_openssl_errors();
  122. else
  123. ERR(1, "ENGINE_init");
  124. if (key_pass)
  125. ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
  126. ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1);
  127. ERR(!parms.cert, "Get X.509 from PKCS#11");
  128. write_cert(parms.cert);
  129. #endif
  130. } else {
  131. BIO *b;
  132. X509 *x509;
  133. b = BIO_new_file(cert_src, "rb");
  134. ERR(!b, "%s", cert_src);
  135. while (1) {
  136. x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
  137. if (wb && !x509) {
  138. unsigned long err = ERR_peek_last_error();
  139. if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
  140. ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
  141. ERR_clear_error();
  142. break;
  143. }
  144. }
  145. ERR(!x509, "%s", cert_src);
  146. write_cert(x509);
  147. }
  148. }
  149. BIO_free(wb);
  150. return 0;
  151. }