blob: 71b2980230226219e1bf03114d148e0851a703a3 [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
Linus Torvalds49eba532022-06-08 13:18:39 -070026/*
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
David Woodhouse1329e8c2015-07-20 21:16:30 +010033#define PKEY_ID_PKCS7 2
34
35static __attribute__((noreturn))
36void format(void)
37{
38 fprintf(stderr,
39 "Usage: scripts/extract-cert <source> <dest>\n");
40 exit(2);
41}
42
43static 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
Matthias Maennich2841a432020-11-26 10:08:38 +000059#ifndef OPENSSL_IS_BORINGSSL
David Woodhouse1329e8c2015-07-20 21:16:30 +010060static 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}
Matthias Maennich2841a432020-11-26 10:08:38 +000069#endif
David Woodhouse1329e8c2015-07-20 21:16:30 +010070
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
80static const char *key_pass;
David Woodhouse84706ca2015-07-20 21:16:33 +010081static BIO *wb;
82static char *cert_dst;
Masahiro Yamada1dbcf462020-07-29 12:18:45 +090083static int kbuild_verbose;
David Woodhouse84706ca2015-07-20 21:16:33 +010084
85static 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));
David Howells7c0d35a2015-09-11 13:07:36 -070094 ERR(!i2d_X509_bio(wb, x509), "%s", cert_dst);
David Woodhouse84706ca2015-07-20 21:16:33 +010095 if (kbuild_verbose)
96 fprintf(stderr, "Extracted cert: %s\n", buf);
97}
David Woodhouse1329e8c2015-07-20 21:16:30 +010098
99int main(int argc, char **argv)
100{
David Woodhouse84706ca2015-07-20 21:16:33 +0100101 char *cert_src;
David Woodhouse1329e8c2015-07-20 21:16:30 +0100102
103 OpenSSL_add_all_algorithms();
104 ERR_load_crypto_strings();
105 ERR_clear_error();
106
David Woodhouse84706ca2015-07-20 21:16:33 +0100107 kbuild_verbose = atoi(getenv("KBUILD_VERBOSE")?:"0");
108
David Woodhouse1329e8c2015-07-20 21:16:30 +0100109 key_pass = getenv("KBUILD_SIGN_PIN");
110
111 if (argc != 3)
112 format();
113
114 cert_src = argv[1];
115 cert_dst = argv[2];
116
David Woodhouse84706ca2015-07-20 21:16:33 +0100117 if (!cert_src[0]) {
118 /* Invoked with no input; create empty file */
119 FILE *f = fopen(cert_dst, "wb");
120 ERR(!f, "%s", cert_dst);
121 fclose(f);
122 exit(0);
123 } else if (!strncmp(cert_src, "pkcs11:", 7)) {
Matthias Maennich2841a432020-11-26 10:08:38 +0000124#ifdef OPENSSL_IS_BORINGSSL
125 ERR(1, "BoringSSL does not support extracting from PKCS#11");
126 exit(1);
127#else
David Woodhouse1329e8c2015-07-20 21:16:30 +0100128 ENGINE *e;
129 struct {
130 const char *cert_id;
131 X509 *cert;
132 } parms;
133
134 parms.cert_id = cert_src;
135 parms.cert = NULL;
136
137 ENGINE_load_builtin_engines();
138 drain_openssl_errors();
139 e = ENGINE_by_id("pkcs11");
140 ERR(!e, "Load PKCS#11 ENGINE");
141 if (ENGINE_init(e))
142 drain_openssl_errors();
143 else
144 ERR(1, "ENGINE_init");
145 if (key_pass)
146 ERR(!ENGINE_ctrl_cmd_string(e, "PIN", key_pass, 0), "Set PKCS#11 PIN");
147 ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, &parms, NULL, 1);
148 ERR(!parms.cert, "Get X.509 from PKCS#11");
David Woodhouse84706ca2015-07-20 21:16:33 +0100149 write_cert(parms.cert);
Matthias Maennich2841a432020-11-26 10:08:38 +0000150#endif
David Woodhouse1329e8c2015-07-20 21:16:30 +0100151 } else {
David Woodhouse84706ca2015-07-20 21:16:33 +0100152 BIO *b;
153 X509 *x509;
154
David Woodhouse1329e8c2015-07-20 21:16:30 +0100155 b = BIO_new_file(cert_src, "rb");
156 ERR(!b, "%s", cert_src);
David Woodhouse84706ca2015-07-20 21:16:33 +0100157
158 while (1) {
159 x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
160 if (wb && !x509) {
161 unsigned long err = ERR_peek_last_error();
162 if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
163 ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
164 ERR_clear_error();
165 break;
166 }
167 }
168 ERR(!x509, "%s", cert_src);
169 write_cert(x509);
170 }
David Woodhouse1329e8c2015-07-20 21:16:30 +0100171 }
172
David Woodhouse84706ca2015-07-20 21:16:33 +0100173 BIO_free(wb);
David Woodhouse1329e8c2015-07-20 21:16:30 +0100174
175 return 0;
176}