1 /* Extract X.509 certificate in DER form from PKCS#11 or PEM.
3 * Copyright © 2014-2015 Red Hat, Inc. All Rights Reserved.
4 * Copyright © 2015 Intel Corporation.
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.
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>
29 # if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
30 # define USE_PKCS11_ENGINE
31 # include <openssl/engine.h>
34 #include "ssl-common.h"
36 #define PKEY_ID_PKCS7 2
38 static __attribute__((noreturn))
42 "Usage: extract-cert <source> <dest>\n");
46 static const char *key_pass;
48 static char *cert_dst;
51 static void write_cert(X509 *x509)
56 wb = BIO_new_file(cert_dst, "wb");
57 ERR(!wb, "%s", cert_dst);
59 X509_NAME_oneline(X509_get_subject_name(x509), buf, sizeof(buf));
60 ERR(!i2d_X509_bio(wb, x509), "%s", cert_dst);
62 fprintf(stderr, "Extracted cert: %s\n", buf);
65 static X509 *load_cert_pkcs11(const char *cert_src)
68 #ifdef USE_PKCS11_PROVIDER
69 OSSL_STORE_CTX *store;
71 if (!OSSL_PROVIDER_try_load(NULL, "pkcs11", true))
72 ERR(1, "OSSL_PROVIDER_try_load(pkcs11)");
73 if (!OSSL_PROVIDER_try_load(NULL, "default", true))
74 ERR(1, "OSSL_PROVIDER_try_load(default)");
76 store = OSSL_STORE_open(cert_src, NULL, NULL, NULL, NULL);
77 ERR(!store, "OSSL_STORE_open");
79 while (!OSSL_STORE_eof(store)) {
80 OSSL_STORE_INFO *info = OSSL_STORE_load(store);
83 drain_openssl_errors(__LINE__, 0);
86 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_CERT) {
87 cert = OSSL_STORE_INFO_get1_CERT(info);
88 ERR(!cert, "OSSL_STORE_INFO_get1_CERT");
90 OSSL_STORE_INFO_free(info);
94 OSSL_STORE_close(store);
95 #elif defined(USE_PKCS11_ENGINE)
102 parms.cert_id = cert_src;
105 ENGINE_load_builtin_engines();
106 drain_openssl_errors(__LINE__, 1);
107 e = ENGINE_by_id("pkcs11");
108 ERR(!e, "Load PKCS#11 ENGINE");
110 drain_openssl_errors(__LINE__, 1);
112 ERR(1, "ENGINE_init");
114 ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
115 ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1);
116 ERR(!parms.cert, "Get X.509 from PKCS#11");
119 fprintf(stderr, "no pkcs11 engine/provider available\n");
125 int main(int argc, char **argv)
130 OpenSSL_add_all_algorithms();
131 ERR_load_crypto_strings();
134 verbose_env = getenv("KBUILD_VERBOSE");
135 if (verbose_env && strchr(verbose_env, '1'))
138 key_pass = getenv("KBUILD_SIGN_PIN");
147 /* Invoked with no input; create empty file */
148 FILE *f = fopen(cert_dst, "wb");
149 ERR(!f, "%s", cert_dst);
152 } else if (!strncmp(cert_src, "pkcs11:", 7)) {
153 X509 *cert = load_cert_pkcs11(cert_src);
155 ERR(!cert, "load_cert_pkcs11 failed");
161 b = BIO_new_file(cert_src, "rb");
162 ERR(!b, "%s", cert_src);
165 x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
167 unsigned long err = ERR_peek_last_error();
168 if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
169 ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
174 ERR(!x509, "%s", cert_src);