extract-cert.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <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 <err.h>
  21. #include <openssl/bio.h>
  22. #include <openssl/pem.h>
  23. #include <openssl/err.h>
  24. #if OPENSSL_VERSION_MAJOR >= 3
  25. # define USE_PKCS11_PROVIDER
  26. # include <openssl/provider.h>
  27. # include <openssl/store.h>
  28. #else
  29. # if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  30. # define USE_PKCS11_ENGINE
  31. # include <openssl/engine.h>
  32. # endif
  33. #endif
  34. #include "ssl-common.h"
  35. #define PKEY_ID_PKCS7 2
  36. static __attribute__((noreturn))
  37. void format(void)
  38. {
  39. fprintf(stderr,
  40. "Usage: extract-cert <source> <dest>\n");
  41. exit(2);
  42. }
  43. static const char *key_pass;
  44. static BIO *wb;
  45. static char *cert_dst;
  46. static bool verbose;
  47. static void write_cert(X509 *x509)
  48. {
  49. char buf[200];
  50. if (!wb) {
  51. wb = BIO_new_file(cert_dst, "wb");
  52. ERR(!wb, "%s", cert_dst);
  53. }
  54. X509_NAME_oneline(X509_get_subject_name(x509), buf, sizeof(buf));
  55. ERR(!i2d_X509_bio(wb, x509), "%s", cert_dst);
  56. if (verbose)
  57. fprintf(stderr, "Extracted cert: %s\n", buf);
  58. }
  59. static X509 *load_cert_pkcs11(const char *cert_src)
  60. {
  61. X509 *cert = NULL;
  62. #ifdef USE_PKCS11_PROVIDER
  63. OSSL_STORE_CTX *store;
  64. if (!OSSL_PROVIDER_try_load(NULL, "pkcs11", true))
  65. ERR(1, "OSSL_PROVIDER_try_load(pkcs11)");
  66. if (!OSSL_PROVIDER_try_load(NULL, "default", true))
  67. ERR(1, "OSSL_PROVIDER_try_load(default)");
  68. store = OSSL_STORE_open(cert_src, NULL, NULL, NULL, NULL);
  69. ERR(!store, "OSSL_STORE_open");
  70. while (!OSSL_STORE_eof(store)) {
  71. OSSL_STORE_INFO *info = OSSL_STORE_load(store);
  72. if (!info) {
  73. drain_openssl_errors(__LINE__, 0);
  74. continue;
  75. }
  76. if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_CERT) {
  77. cert = OSSL_STORE_INFO_get1_CERT(info);
  78. ERR(!cert, "OSSL_STORE_INFO_get1_CERT");
  79. }
  80. OSSL_STORE_INFO_free(info);
  81. if (cert)
  82. break;
  83. }
  84. OSSL_STORE_close(store);
  85. #elif defined(USE_PKCS11_ENGINE)
  86. ENGINE *e;
  87. struct {
  88. const char *cert_id;
  89. X509 *cert;
  90. } parms;
  91. parms.cert_id = cert_src;
  92. parms.cert = NULL;
  93. ENGINE_load_builtin_engines();
  94. drain_openssl_errors(__LINE__, 1);
  95. e = ENGINE_by_id("pkcs11");
  96. ERR(!e, "Load PKCS#11 ENGINE");
  97. if (ENGINE_init(e))
  98. drain_openssl_errors(__LINE__, 1);
  99. else
  100. ERR(1, "ENGINE_init");
  101. if (key_pass)
  102. ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
  103. ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1);
  104. ERR(!parms.cert, "Get X.509 from PKCS#11");
  105. cert = parms.cert;
  106. #else
  107. fprintf(stderr, "no pkcs11 engine/provider available\n");
  108. exit(1);
  109. #endif
  110. return cert;
  111. }
  112. int main(int argc, char **argv)
  113. {
  114. char *cert_src;
  115. char *verbose_env;
  116. OpenSSL_add_all_algorithms();
  117. ERR_load_crypto_strings();
  118. ERR_clear_error();
  119. verbose_env = getenv("KBUILD_VERBOSE");
  120. if (verbose_env && strchr(verbose_env, '1'))
  121. verbose = true;
  122. key_pass = getenv("KBUILD_SIGN_PIN");
  123. if (argc != 3)
  124. format();
  125. cert_src = argv[1];
  126. cert_dst = argv[2];
  127. if (!cert_src[0]) {
  128. /* Invoked with no input; create empty file */
  129. FILE *f = fopen(cert_dst, "wb");
  130. ERR(!f, "%s", cert_dst);
  131. fclose(f);
  132. exit(0);
  133. } else if (!strncmp(cert_src, "pkcs11:", 7)) {
  134. X509 *cert = load_cert_pkcs11(cert_src);
  135. ERR(!cert, "load_cert_pkcs11 failed");
  136. write_cert(cert);
  137. } else {
  138. BIO *b;
  139. X509 *x509;
  140. b = BIO_new_file(cert_src, "rb");
  141. ERR(!b, "%s", cert_src);
  142. while (1) {
  143. x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
  144. if (wb && !x509) {
  145. unsigned long err = ERR_peek_last_error();
  146. if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
  147. ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
  148. ERR_clear_error();
  149. break;
  150. }
  151. }
  152. ERR(!x509, "%s", cert_src);
  153. write_cert(x509);
  154. }
  155. }
  156. BIO_free(wb);
  157. return 0;
  158. }