blob: 3034d991ba55279df22fb9ec20ddf0f06f3d9dc5 [file] [log] [blame]
David Woodhouse1329e8c2015-07-20 21:16:30 +01001/* Extract X.509 certificate in DER form from PKCS#11 or PEM.
2 *
David Woodhouse09a77a82015-09-15 16:03:36 +01003 * Copyright © 2014-2015 Red Hat, Inc. All Rights Reserved.
4 * Copyright © 2015 Intel Corporation.
David Woodhouse1329e8c2015-07-20 21:16:30 +01005 *
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
David Woodhouse09a77a82015-09-15 16:03:36 +010010 * 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.
David Woodhouse1329e8c2015-07-20 21:16:30 +010013 */
14#define _GNU_SOURCE
15#include <stdio.h>
16#include <stdlib.h>
17#include <stdint.h>
18#include <stdbool.h>
19#include <string.h>
David Woodhouse1329e8c2015-07-20 21:16:30 +010020#include <err.h>
David Woodhouse1329e8c2015-07-20 21:16:30 +010021#include <openssl/bio.h>
David Woodhouse1329e8c2015-07-20 21:16:30 +010022#include <openssl/pem.h>
David Woodhouse1329e8c2015-07-20 21:16:30 +010023#include <openssl/err.h>
24#include <openssl/engine.h>
25
26#define PKEY_ID_PKCS7 2
27
28static __attribute__((noreturn))
29void format(void)
30{
31 fprintf(stderr,
32 "Usage: scripts/extract-cert <source> <dest>\n");
33 exit(2);
34}
35
36static void display_openssl_errors(int l)
37{
38 const char *file;
39 char buf[120];
40 int e, line;
41
42 if (ERR_peek_error() == 0)
43 return;
44 fprintf(stderr, "At main.c:%d:\n", l);
45
46 while ((e = ERR_get_error_line(&file, &line))) {
47 ERR_error_string(e, buf);
48 fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
49 }
50}
51
Matthias Maennich2841a432020-11-26 10:08:38 +000052#ifndef OPENSSL_IS_BORINGSSL
David Woodhouse1329e8c2015-07-20 21:16:30 +010053static void drain_openssl_errors(void)
54{
55 const char *file;
56 int line;
57
58 if (ERR_peek_error() == 0)
59 return;
60 while (ERR_get_error_line(&file, &line)) {}
61}
Matthias Maennich2841a432020-11-26 10:08:38 +000062#endif
David Woodhouse1329e8c2015-07-20 21:16:30 +010063
64#define ERR(cond, fmt, ...) \
65 do { \
66 bool __cond = (cond); \
67 display_openssl_errors(__LINE__); \
68 if (__cond) { \
69 err(1, fmt, ## __VA_ARGS__); \
70 } \
71 } while(0)
72
73static const char *key_pass;
David Woodhouse84706ca2015-07-20 21:16:33 +010074static BIO *wb;
75static char *cert_dst;
Masahiro Yamada1dbcf462020-07-29 12:18:45 +090076static int kbuild_verbose;
David Woodhouse84706ca2015-07-20 21:16:33 +010077
78static void write_cert(X509 *x509)
79{
80 char buf[200];
81
82 if (!wb) {
83 wb = BIO_new_file(cert_dst, "wb");
84 ERR(!wb, "%s", cert_dst);
85 }
86 X509_NAME_oneline(X509_get_subject_name(x509), buf, sizeof(buf));
David Howells7c0d35a2015-09-11 13:07:36 -070087 ERR(!i2d_X509_bio(wb, x509), "%s", cert_dst);
David Woodhouse84706ca2015-07-20 21:16:33 +010088 if (kbuild_verbose)
89 fprintf(stderr, "Extracted cert: %s\n", buf);
90}
David Woodhouse1329e8c2015-07-20 21:16:30 +010091
92int main(int argc, char **argv)
93{
David Woodhouse84706ca2015-07-20 21:16:33 +010094 char *cert_src;
David Woodhouse1329e8c2015-07-20 21:16:30 +010095
96 OpenSSL_add_all_algorithms();
97 ERR_load_crypto_strings();
98 ERR_clear_error();
99
David Woodhouse84706ca2015-07-20 21:16:33 +0100100 kbuild_verbose = atoi(getenv("KBUILD_VERBOSE")?:"0");
101
David Woodhouse1329e8c2015-07-20 21:16:30 +0100102 key_pass = getenv("KBUILD_SIGN_PIN");
103
104 if (argc != 3)
105 format();
106
107 cert_src = argv[1];
108 cert_dst = argv[2];
109
David Woodhouse84706ca2015-07-20 21:16:33 +0100110 if (!cert_src[0]) {
111 /* Invoked with no input; create empty file */
112 FILE *f = fopen(cert_dst, "wb");
113 ERR(!f, "%s", cert_dst);
114 fclose(f);
115 exit(0);
116 } else if (!strncmp(cert_src, "pkcs11:", 7)) {
Matthias Maennich2841a432020-11-26 10:08:38 +0000117#ifdef OPENSSL_IS_BORINGSSL
118 ERR(1, "BoringSSL does not support extracting from PKCS#11");
119 exit(1);
120#else
David Woodhouse1329e8c2015-07-20 21:16:30 +0100121 ENGINE *e;
122 struct {
123 const char *cert_id;
124 X509 *cert;
125 } parms;
126
127 parms.cert_id = cert_src;
128 parms.cert = NULL;
129
130 ENGINE_load_builtin_engines();
131 drain_openssl_errors();
132 e = ENGINE_by_id("pkcs11");
133 ERR(!e, "Load PKCS#11 ENGINE");
134 if (ENGINE_init(e))
135 drain_openssl_errors();
136 else
137 ERR(1, "ENGINE_init");
138 if (key_pass)
139 ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
140 ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1);
141 ERR(!parms.cert, "Get X.509 from PKCS#11");
David Woodhouse84706ca2015-07-20 21:16:33 +0100142 write_cert(parms.cert);
Matthias Maennich2841a432020-11-26 10:08:38 +0000143#endif
David Woodhouse1329e8c2015-07-20 21:16:30 +0100144 } else {
David Woodhouse84706ca2015-07-20 21:16:33 +0100145 BIO *b;
146 X509 *x509;
147
David Woodhouse1329e8c2015-07-20 21:16:30 +0100148 b = BIO_new_file(cert_src, "rb");
149 ERR(!b, "%s", cert_src);
David Woodhouse84706ca2015-07-20 21:16:33 +0100150
151 while (1) {
152 x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
153 if (wb && !x509) {
154 unsigned long err = ERR_peek_last_error();
155 if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
156 ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
157 ERR_clear_error();
158 break;
159 }
160 }
161 ERR(!x509, "%s", cert_src);
162 write_cert(x509);
163 }
David Woodhouse1329e8c2015-07-20 21:16:30 +0100164 }
165
David Woodhouse84706ca2015-07-20 21:16:33 +0100166 BIO_free(wb);
David Woodhouse1329e8c2015-07-20 21:16:30 +0100167
168 return 0;
169}