| 1 | |
| 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 | #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 |
|
| 36 | static __attribute__((noreturn))
|
| 37 | void format(void)
|
| 38 | {
|
| 39 | fprintf(stderr,
|
| 40 | "Usage: extract-cert <source> <dest>\n");
|
| 41 | exit(2);
|
| 42 | }
|
| 43 |
|
| 44 | #ifdef USE_PKCS11_ENGINE
|
| 45 | static const char *key_pass;
|
| 46 | #endif
|
| 47 | static BIO *wb;
|
| 48 | static char *cert_dst;
|
| 49 | static bool verbose;
|
| 50 |
|
| 51 | static void write_cert(X509 *x509)
|
| 52 | {
|
| 53 | char buf[200];
|
| 54 |
|
| 55 | if (!wb) {
|
| 56 | wb = BIO_new_file(cert_dst, "wb");
|
| 57 | ERR(!wb, "%s", cert_dst);
|
| 58 | }
|
| 59 | X509_NAME_oneline(X509_get_subject_name(x509), buf, sizeof(buf));
|
| 60 | ERR(!i2d_X509_bio(wb, x509), "%s", cert_dst);
|
| 61 | if (verbose)
|
| 62 | fprintf(stderr, "Extracted cert: %s\n", buf);
|
| 63 | }
|
| 64 |
|
| 65 | static X509 *load_cert_pkcs11(const char *cert_src)
|
| 66 | {
|
| 67 | X509 *cert = NULL;
|
| 68 | #ifdef USE_PKCS11_PROVIDER
|
| 69 | OSSL_STORE_CTX *store;
|
| 70 |
|
| 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)");
|
| 75 |
|
| 76 | store = OSSL_STORE_open(cert_src, NULL, NULL, NULL, NULL);
|
| 77 | ERR(!store, "OSSL_STORE_open");
|
| 78 |
|
| 79 | while (!OSSL_STORE_eof(store)) {
|
| 80 | OSSL_STORE_INFO *info = OSSL_STORE_load(store);
|
| 81 |
|
| 82 | if (!info) {
|
| 83 | drain_openssl_errors(__LINE__, 0);
|
| 84 | continue;
|
| 85 | }
|
| 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");
|
| 89 | }
|
| 90 | OSSL_STORE_INFO_free(info);
|
| 91 | if (cert)
|
| 92 | break;
|
| 93 | }
|
| 94 | OSSL_STORE_close(store);
|
| 95 | #elif defined(USE_PKCS11_ENGINE)
|
| 96 | ENGINE *e;
|
| 97 | struct {
|
| 98 | const char *cert_id;
|
| 99 | X509 *cert;
|
| 100 | } parms;
|
| 101 |
|
| 102 | parms.cert_id = cert_src;
|
| 103 | parms.cert = NULL;
|
| 104 |
|
| 105 | ENGINE_load_builtin_engines();
|
| 106 | drain_openssl_errors(__LINE__, 1);
|
| 107 | e = ENGINE_by_id("pkcs11");
|
| 108 | ERR(!e, "Load PKCS#11 ENGINE");
|
| 109 | if (ENGINE_init(e))
|
| 110 | drain_openssl_errors(__LINE__, 1);
|
| 111 | else
|
| 112 | ERR(1, "ENGINE_init");
|
| 113 | if (key_pass)
|
| 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");
|
| 117 | cert = parms.cert;
|
| 118 | #else
|
| 119 | fprintf(stderr, "no pkcs11 engine/provider available\n");
|
| 120 | exit(1);
|
| 121 | #endif
|
| 122 | return cert;
|
| 123 | }
|
| 124 |
|
| 125 | int main(int argc, char **argv)
|
| 126 | {
|
| 127 | char *cert_src;
|
| 128 | char *verbose_env;
|
| 129 |
|
| 130 | OpenSSL_add_all_algorithms();
|
| 131 | ERR_load_crypto_strings();
|
| 132 | ERR_clear_error();
|
| 133 |
|
| 134 | verbose_env = getenv("KBUILD_VERBOSE");
|
| 135 | if (verbose_env && strchr(verbose_env, '1'))
|
| 136 | verbose = true;
|
| 137 |
|
| 138 | #ifdef USE_PKCS11_ENGINE
|
| 139 | key_pass = getenv("KBUILD_SIGN_PIN");
|
| 140 | #endif
|
| 141 |
|
| 142 | if (argc != 3)
|
| 143 | format();
|
| 144 |
|
| 145 | cert_src = argv[1];
|
| 146 | cert_dst = argv[2];
|
| 147 |
|
| 148 | if (!cert_src[0]) {
|
| 149 |
|
| 150 | FILE *f = fopen(cert_dst, "wb");
|
| 151 | ERR(!f, "%s", cert_dst);
|
| 152 | fclose(f);
|
| 153 | exit(0);
|
| 154 | } else if (!strncmp(cert_src, "pkcs11:", 7)) {
|
| 155 | X509 *cert = load_cert_pkcs11(cert_src);
|
| 156 |
|
| 157 | ERR(!cert, "load_cert_pkcs11 failed");
|
| 158 | write_cert(cert);
|
| 159 | } else {
|
| 160 | BIO *b;
|
| 161 | X509 *x509;
|
| 162 |
|
| 163 | b = BIO_new_file(cert_src, "rb");
|
| 164 | ERR(!b, "%s", cert_src);
|
| 165 |
|
| 166 | while (1) {
|
| 167 | x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
|
| 168 | if (wb && !x509) {
|
| 169 | unsigned long err = ERR_peek_last_error();
|
| 170 | if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
|
| 171 | ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
|
| 172 | ERR_clear_error();
|
| 173 | break;
|
| 174 | }
|
| 175 | }
|
| 176 | ERR(!x509, "%s", cert_src);
|
| 177 | write_cert(x509);
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | BIO_free(wb);
|
| 182 |
|
| 183 | return 0;
|
| 184 | }
|