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