Skip to content

Commit 1329e8c

Browse files
David Woodhousedhowells
authored andcommitted
modsign: Extract signing cert from CONFIG_MODULE_SIG_KEY if needed
Where an external PEM file or PKCS#11 URI is given, we can get the cert from it for ourselves instead of making the user drop signing_key.x509 in place for us. Signed-off-by: David Woodhouse <David.Woodhouse@intel.com> Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 19e91b6 commit 1329e8c

5 files changed

Lines changed: 181 additions & 11 deletions

File tree

Documentation/module-signing.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,16 @@ This has a number of options available:
9393
Setting this option to something other than its default of
9494
"signing_key.priv" will disable the autogeneration of signing keys and
9595
allow the kernel modules to be signed with a key of your choosing.
96-
The string provided should identify a file containing a private key
97-
in PEM form, or — on systems where the OpenSSL ENGINE_pkcs11 is
98-
appropriately installed — a PKCS#11 URI as defined by RFC7512.
96+
The string provided should identify a file containing both a private
97+
key and its corresponding X.509 certificate in PEM form, or — on
98+
systems where the OpenSSL ENGINE_pkcs11 is functional — a PKCS#11 URI
99+
as defined by RFC7512. In the latter case, the PKCS#11 URI should
100+
reference both a certificate and a private key.
99101

100102
If the PEM file containing the private key is encrypted, or if the
101103
PKCS#11 token requries a PIN, this can be provided at build time by
102104
means of the KBUILD_SIGN_PIN variable.
103105

104-
The corresponding X.509 certificate in DER form should still be placed
105-
in a file named signing_key.x509 in the top-level build directory.
106-
107106

108107
=======================
109108
GENERATING SIGNING KEYS

init/Kconfig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,10 +1953,10 @@ config MODULE_SIG_KEY
19531953
default "signing_key.priv"
19541954
depends on MODULE_SIG
19551955
help
1956-
Provide the file name of a private key in PKCS#8 PEM format, or
1957-
a PKCS#11 URI according to RFC7512. The corresponding X.509
1958-
certificate in DER form should be present in signing_key.x509
1959-
in the top-level build directory.
1956+
Provide the file name of a private key/certificate in PEM format,
1957+
or a PKCS#11 URI according to RFC7512. The file should contain, or
1958+
the URI should identify, both the certificate and its corresponding
1959+
private key.
19601960

19611961
If this option is unchanged from its default "signing_key.priv",
19621962
then the kernel will automatically generate the private key and

kernel/Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,43 @@ x509.genkey:
210210
@echo >>x509.genkey "keyUsage=digitalSignature"
211211
@echo >>x509.genkey "subjectKeyIdentifier=hash"
212212
@echo >>x509.genkey "authorityKeyIdentifier=keyid"
213+
else
214+
# For external (PKCS#11 or PEM) key, we need to obtain the certificate from
215+
# CONFIG_MODULE_SIG_KEY automatically.
216+
quiet_cmd_extract_der = CERT_DER $(2)
217+
cmd_extract_der = scripts/extract-cert "$(2)" signing_key.x509
218+
219+
# CONFIG_MODULE_SIG_KEY is either a PKCS#11 URI or a filename. It is
220+
# surrounded by quotes, and may contain spaces. To strip the quotes
221+
# with $(patsubst) we need to turn the spaces into something else.
222+
# And if it's a filename, those spaces need to be escaped as '\ ' in
223+
# order to use it in dependencies or $(wildcard).
224+
space :=
225+
space +=
226+
space_escape := %%%SPACE%%%
227+
X509_SOURCE_temp := $(subst $(space),$(space_escape),$(CONFIG_MODULE_SIG_KEY))
228+
# We need this to check for absolute paths or PKCS#11 URIs.
229+
X509_SOURCE_ONEWORD := $(patsubst "%",%,$(X509_SOURCE_temp))
230+
# This is the actual source filename/URI without the quotes
231+
X509_SOURCE := $(subst $(space_escape),$(space),$(X509_SOURCE_ONEWORD))
232+
# This\ version\ with\ spaces\ escaped\ for\ $(wildcard)\ and\ dependencies
233+
X509_SOURCE_ESCAPED := $(subst $(space_escape),\$(space),$(X509_SOURCE_ONEWORD))
234+
235+
ifeq ($(patsubst pkcs11:%,%,$(X509_SOURCE_ONEWORD)),$(X509_SOURCE_ONEWORD))
236+
# If it's a filename, depend on it.
237+
X509_DEP := $(X509_SOURCE_ESCAPED)
238+
ifeq ($(patsubst /%,%,$(X509_SOURCE_ONEWORD)),$(X509_SOURCE_ONEWORD))
239+
ifeq ($(wildcard $(X509_SOURCE_ESCAPED)),)
240+
ifneq ($(wildcard $(srctree)/$(X509_SOURCE_ESCAPED)),)
241+
# Non-absolute filename, found in source tree and not build tree
242+
X509_SOURCE := $(srctree)/$(X509_SOURCE)
243+
X509_DEP := $(srctree)/$(X509_SOURCE_ESCAPED)
244+
endif
245+
endif
246+
endif
247+
endif
248+
249+
signing_key.x509: scripts/extract-cert include/config/module/sig/key.h $(X509_DEP)
250+
$(call cmd,extract_der,$(X509_SOURCE))
213251
endif
214252
endif

scripts/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ hostprogs-$(CONFIG_VT) += conmakehash
1616
hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount
1717
hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable
1818
hostprogs-$(CONFIG_ASN1) += asn1_compiler
19-
hostprogs-$(CONFIG_MODULE_SIG) += sign-file
19+
hostprogs-$(CONFIG_MODULE_SIG) += sign-file extract-cert
2020

2121
HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include
2222
HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include
2323
HOSTLOADLIBES_sign-file = -lcrypto
24+
HOSTLOADLIBES_extract-cert = -lcrypto
2425

2526
always := $(hostprogs-y) $(hostprogs-m)
2627

scripts/extract-cert.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/* Extract X.509 certificate in DER form from PKCS#11 or PEM.
2+
*
3+
* Copyright © 2014 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 General Public Licence
11+
* as published by the Free Software Foundation; either version
12+
* 2 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 <getopt.h>
21+
#include <err.h>
22+
#include <arpa/inet.h>
23+
#include <openssl/bio.h>
24+
#include <openssl/evp.h>
25+
#include <openssl/pem.h>
26+
#include <openssl/pkcs7.h>
27+
#include <openssl/err.h>
28+
#include <openssl/engine.h>
29+
30+
#define PKEY_ID_PKCS7 2
31+
32+
static __attribute__((noreturn))
33+
void format(void)
34+
{
35+
fprintf(stderr,
36+
"Usage: scripts/extract-cert <source> <dest>\n");
37+
exit(2);
38+
}
39+
40+
static void display_openssl_errors(int l)
41+
{
42+
const char *file;
43+
char buf[120];
44+
int e, line;
45+
46+
if (ERR_peek_error() == 0)
47+
return;
48+
fprintf(stderr, "At main.c:%d:\n", l);
49+
50+
while ((e = ERR_get_error_line(&file, &line))) {
51+
ERR_error_string(e, buf);
52+
fprintf(stderr, "- SSL %s: %s:%d\n", buf, file, line);
53+
}
54+
}
55+
56+
static void drain_openssl_errors(void)
57+
{
58+
const char *file;
59+
int line;
60+
61+
if (ERR_peek_error() == 0)
62+
return;
63+
while (ERR_get_error_line(&file, &line)) {}
64+
}
65+
66+
#define ERR(cond, fmt, ...) \
67+
do { \
68+
bool __cond = (cond); \
69+
display_openssl_errors(__LINE__); \
70+
if (__cond) { \
71+
err(1, fmt, ## __VA_ARGS__); \
72+
} \
73+
} while(0)
74+
75+
static const char *key_pass;
76+
77+
int main(int argc, char **argv)
78+
{
79+
char *cert_src, *cert_dst;
80+
X509 *x509;
81+
BIO *b;
82+
83+
OpenSSL_add_all_algorithms();
84+
ERR_load_crypto_strings();
85+
ERR_clear_error();
86+
87+
key_pass = getenv("KBUILD_SIGN_PIN");
88+
89+
if (argc != 3)
90+
format();
91+
92+
cert_src = argv[1];
93+
cert_dst = argv[2];
94+
95+
if (!strncmp(cert_src, "pkcs11:", 7)) {
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();
107+
e = ENGINE_by_id("pkcs11");
108+
ERR(!e, "Load PKCS#11 ENGINE");
109+
if (ENGINE_init(e))
110+
drain_openssl_errors();
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+
x509 = parms.cert;
118+
} else {
119+
b = BIO_new_file(cert_src, "rb");
120+
ERR(!b, "%s", cert_src);
121+
x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
122+
ERR(!x509, "%s", cert_src);
123+
BIO_free(b);
124+
}
125+
126+
b = BIO_new_file(cert_dst, "wb");
127+
ERR(!b, "%s", cert_dst);
128+
ERR(!i2d_X509_bio(b, x509), cert_dst);
129+
BIO_free(b);
130+
131+
return 0;
132+
}

0 commit comments

Comments
 (0)