Skip to content

Commit cfc411e

Browse files
dhowellsDavid Woodhouse
authored and
David Woodhouse
committed
Move certificate handling to its own directory
Move certificate handling out of the kernel/ directory and into a certs/ directory to get all the weird stuff in one place and move the generated signing keys into this directory. Signed-off-by: David Howells <[email protected]> Reviewed-by: David Woodhouse <[email protected]>
1 parent 0e38c35 commit cfc411e

File tree

10 files changed

+212
-195
lines changed

10 files changed

+212
-195
lines changed

Documentation/module-signing.txt

+9-9
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ This has a number of options available:
9292
(4) "File name or PKCS#11 URI of module signing key" (CONFIG_MODULE_SIG_KEY)
9393

9494
Setting this option to something other than its default of
95-
"signing_key.pem" will disable the autogeneration of signing keys and
96-
allow the kernel modules to be signed with a key of your choosing.
97-
The string provided should identify a file containing both a private
98-
key and its corresponding X.509 certificate in PEM form, or — on
99-
systems where the OpenSSL ENGINE_pkcs11 is functional — a PKCS#11 URI
100-
as defined by RFC7512. In the latter case, the PKCS#11 URI should
101-
reference both a certificate and a private key.
95+
"certs/signing_key.pem" will disable the autogeneration of signing keys
96+
and allow the kernel modules to be signed with a key of your choosing.
97+
The string provided should identify a file containing both a private key
98+
and its corresponding X.509 certificate in PEM form, or — on systems where
99+
the OpenSSL ENGINE_pkcs11 is functional — a PKCS#11 URI as defined by
100+
RFC7512. In the latter case, the PKCS#11 URI should reference both a
101+
certificate and a private key.
102102

103103
If the PEM file containing the private key is encrypted, or if the
104104
PKCS#11 token requries a PIN, this can be provided at build time by
@@ -130,12 +130,12 @@ Under normal conditions, when CONFIG_MODULE_SIG_KEY is unchanged from its
130130
default, the kernel build will automatically generate a new keypair using
131131
openssl if one does not exist in the file:
132132

133-
signing_key.pem
133+
certs/signing_key.pem
134134

135135
during the building of vmlinux (the public part of the key needs to be built
136136
into vmlinux) using parameters in the:
137137

138-
x509.genkey
138+
certs/x509.genkey
139139

140140
file (which is also generated if it does not already exist).
141141

MAINTAINERS

+9
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,15 @@ S: Supported
25892589
F: Documentation/filesystems/ceph.txt
25902590
F: fs/ceph/
25912591

2592+
CERTIFICATE HANDLING:
2593+
M: David Howells <[email protected]>
2594+
M: David Woodhouse <[email protected]>
2595+
2596+
S: Maintained
2597+
F: Documentation/module-signing.txt
2598+
F: certs/
2599+
F: scripts/extract-cert.c
2600+
25922601
CERTIFIED WIRELESS USB (WUSB) SUBSYSTEM:
25932602
25942603
S: Orphan

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ INITRD_COMPRESS-$(CONFIG_RD_LZ4) := lz4
871871

872872
ifdef CONFIG_MODULE_SIG_ALL
873873
MODSECKEY = $(CONFIG_MODULE_SIG_KEY)
874-
MODPUBKEY = ./signing_key.x509
874+
MODPUBKEY = certs/signing_key.x509
875875
export MODPUBKEY
876876
mod_sign_cmd = scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY)
877877
else
@@ -881,7 +881,7 @@ export mod_sign_cmd
881881

882882

883883
ifeq ($(KBUILD_EXTMOD),)
884-
core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/
884+
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/
885885

886886
vmlinux-dirs := $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \
887887
$(core-y) $(core-m) $(drivers-y) $(drivers-m) \

certs/Kconfig

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
menu "Certificates for signature checking"
2+
3+
config MODULE_SIG_KEY
4+
string "File name or PKCS#11 URI of module signing key"
5+
default "certs/signing_key.pem"
6+
depends on MODULE_SIG
7+
help
8+
Provide the file name of a private key/certificate in PEM format,
9+
or a PKCS#11 URI according to RFC7512. The file should contain, or
10+
the URI should identify, both the certificate and its corresponding
11+
private key.
12+
13+
If this option is unchanged from its default "certs/signing_key.pem",
14+
then the kernel will automatically generate the private key and
15+
certificate as described in Documentation/module-signing.txt
16+
17+
config SYSTEM_TRUSTED_KEYRING
18+
bool "Provide system-wide ring of trusted keys"
19+
depends on KEYS
20+
help
21+
Provide a system keyring to which trusted keys can be added. Keys in
22+
the keyring are considered to be trusted. Keys may be added at will
23+
by the kernel from compiled-in data and from hardware key stores, but
24+
userspace may only add extra keys if those keys can be verified by
25+
keys already in the keyring.
26+
27+
Keys in this keyring are used by module signature checking.
28+
29+
config SYSTEM_TRUSTED_KEYS
30+
string "Additional X.509 keys for default system keyring"
31+
depends on SYSTEM_TRUSTED_KEYRING
32+
help
33+
If set, this option should be the filename of a PEM-formatted file
34+
containing trusted X.509 certificates to be included in the default
35+
system keyring. Any certificate used for module signing is implicitly
36+
also trusted.
37+
38+
NOTE: If you previously provided keys for the system keyring in the
39+
form of DER-encoded *.x509 files in the top-level build directory,
40+
those are no longer used. You will need to set this option instead.
41+
42+
endmenu

certs/Makefile

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#
2+
# Makefile for the linux kernel signature checking certificates.
3+
#
4+
5+
obj-$(CONFIG_SYSTEM_TRUSTED_KEYRING) += system_keyring.o system_certificates.o
6+
7+
###############################################################################
8+
#
9+
# When a Kconfig string contains a filename, it is suitable for
10+
# passing to shell commands. It is surrounded by double-quotes, and
11+
# any double-quotes or backslashes within it are escaped by
12+
# backslashes.
13+
#
14+
# This is no use for dependencies or $(wildcard). We need to strip the
15+
# surrounding quotes and the escaping from quotes and backslashes, and
16+
# we *do* need to escape any spaces in the string. So, for example:
17+
#
18+
# Usage: $(eval $(call config_filename,FOO))
19+
#
20+
# Defines FOO_FILENAME based on the contents of the CONFIG_FOO option,
21+
# transformed as described above to be suitable for use within the
22+
# makefile.
23+
#
24+
# Also, if the filename is a relative filename and exists in the source
25+
# tree but not the build tree, define FOO_SRCPREFIX as $(srctree)/ to
26+
# be prefixed to *both* command invocation and dependencies.
27+
#
28+
# Note: We also print the filenames in the quiet_cmd_foo text, and
29+
# perhaps ought to have a version specially escaped for that purpose.
30+
# But it's only cosmetic, and $(patsubst "%",%,$(CONFIG_FOO)) is good
31+
# enough. It'll strip the quotes in the common case where there's no
32+
# space and it's a simple filename, and it'll retain the quotes when
33+
# there's a space. There are some esoteric cases in which it'll print
34+
# the wrong thing, but we don't really care. The actual dependencies
35+
# and commands *do* get it right, with various combinations of single
36+
# and double quotes, backslashes and spaces in the filenames.
37+
#
38+
###############################################################################
39+
#
40+
quote := $(firstword " ")
41+
space :=
42+
space +=
43+
space_escape := %%%SPACE%%%
44+
#
45+
define config_filename
46+
ifneq ($$(CONFIG_$(1)),"")
47+
$(1)_FILENAME := $$(subst \\,\,$$(subst \$$(quote),$$(quote),$$(subst $$(space_escape),\$$(space),$$(patsubst "%",%,$$(subst $$(space),$$(space_escape),$$(CONFIG_$(1)))))))
48+
ifneq ($$(patsubst /%,%,$$(firstword $$($(1)_FILENAME))),$$(firstword $$($(1)_FILENAME)))
49+
else
50+
ifeq ($$(wildcard $$($(1)_FILENAME)),)
51+
ifneq ($$(wildcard $$(srctree)/$$($(1)_FILENAME)),)
52+
$(1)_SRCPREFIX := $(srctree)/
53+
endif
54+
endif
55+
endif
56+
endif
57+
endef
58+
#
59+
###############################################################################
60+
61+
ifeq ($(CONFIG_SYSTEM_TRUSTED_KEYRING),y)
62+
63+
$(eval $(call config_filename,SYSTEM_TRUSTED_KEYS))
64+
65+
# GCC doesn't include .incbin files in -MD generated dependencies (PR#66871)
66+
$(obj)/system_certificates.o: $(obj)/x509_certificate_list
67+
68+
# Cope with signing_key.x509 existing in $(srctree) not $(objtree)
69+
AFLAGS_system_certificates.o := -I$(srctree)
70+
71+
quiet_cmd_extract_certs = EXTRACT_CERTS $(patsubst "%",%,$(2))
72+
cmd_extract_certs = scripts/extract-cert $(2) $@ || ( rm $@; exit 1)
73+
74+
targets += x509_certificate_list
75+
$(obj)/x509_certificate_list: scripts/extract-cert $(SYSTEM_TRUSTED_KEYS_SRCPREFIX)$(SYSTEM_TRUSTED_KEYS_FILENAME) FORCE
76+
$(call if_changed,extract_certs,$(SYSTEM_TRUSTED_KEYS_SRCPREFIX)$(CONFIG_SYSTEM_TRUSTED_KEYS))
77+
endif
78+
79+
clean-files := x509_certificate_list .x509.list
80+
81+
ifeq ($(CONFIG_MODULE_SIG),y)
82+
###############################################################################
83+
#
84+
# If module signing is requested, say by allyesconfig, but a key has not been
85+
# supplied, then one will need to be generated to make sure the build does not
86+
# fail and that the kernel may be used afterwards.
87+
#
88+
###############################################################################
89+
ifndef CONFIG_MODULE_SIG_HASH
90+
$(error Could not determine digest type to use from kernel config)
91+
endif
92+
93+
# We do it this way rather than having a boolean option for enabling an
94+
# external private key, because 'make randconfig' might enable such a
95+
# boolean option and we unfortunately can't make it depend on !RANDCONFIG.
96+
ifeq ($(CONFIG_MODULE_SIG_KEY),"certs/signing_key.pem")
97+
$(obj)/signing_key.pem: $(obj)/x509.genkey
98+
@echo "###"
99+
@echo "### Now generating an X.509 key pair to be used for signing modules."
100+
@echo "###"
101+
@echo "### If this takes a long time, you might wish to run rngd in the"
102+
@echo "### background to keep the supply of entropy topped up. It"
103+
@echo "### needs to be run as root, and uses a hardware random"
104+
@echo "### number generator if one is available."
105+
@echo "###"
106+
openssl req -new -nodes -utf8 -$(CONFIG_MODULE_SIG_HASH) -days 36500 \
107+
-batch -x509 -config $(obj)/x509.genkey \
108+
-outform PEM -out $(obj)/signing_key.pem \
109+
-keyout $(obj)/signing_key.pem 2>&1
110+
@echo "###"
111+
@echo "### Key pair generated."
112+
@echo "###"
113+
114+
$(obj)/x509.genkey:
115+
@echo Generating X.509 key generation config
116+
@echo >$@ "[ req ]"
117+
@echo >>$@ "default_bits = 4096"
118+
@echo >>$@ "distinguished_name = req_distinguished_name"
119+
@echo >>$@ "prompt = no"
120+
@echo >>$@ "string_mask = utf8only"
121+
@echo >>$@ "x509_extensions = myexts"
122+
@echo >>$@
123+
@echo >>$@ "[ req_distinguished_name ]"
124+
@echo >>$@ "#O = Unspecified company"
125+
@echo >>$@ "CN = Build time autogenerated kernel key"
126+
@echo >>$@ "#emailAddress = [email protected]"
127+
@echo >>$@
128+
@echo >>$@ "[ myexts ]"
129+
@echo >>$@ "basicConstraints=critical,CA:FALSE"
130+
@echo >>$@ "keyUsage=digitalSignature"
131+
@echo >>$@ "subjectKeyIdentifier=hash"
132+
@echo >>$@ "authorityKeyIdentifier=keyid"
133+
endif
134+
135+
$(eval $(call config_filename,MODULE_SIG_KEY))
136+
137+
# If CONFIG_MODULE_SIG_KEY isn't a PKCS#11 URI, depend on it
138+
ifeq ($(patsubst pkcs11:%,%,$(firstword $(MODULE_SIG_KEY_FILENAME))),$(firstword $(MODULE_SIG_KEY_FILENAME)))
139+
X509_DEP := $(MODULE_SIG_KEY_SRCPREFIX)$(MODULE_SIG_KEY_FILENAME)
140+
endif
141+
142+
# GCC PR#66871 again.
143+
$(obj)/system_certificates.o: $(obj)/signing_key.x509
144+
145+
$(obj)/signing_key.x509: scripts/extract-cert include/config/module/sig/key.h $(X509_DEP)
146+
$(call cmd,extract_certs,$(MODULE_SIG_KEY_SRCPREFIX)$(CONFIG_MODULE_SIG_KEY))
147+
endif

kernel/system_certificates.S certs/system_certificates.S

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
VMLINUX_SYMBOL(system_certificate_list):
99
__cert_list_start:
1010
#ifdef CONFIG_MODULE_SIG
11-
.incbin "signing_key.x509"
11+
.incbin "certs/signing_key.x509"
1212
#endif
13-
.incbin "kernel/x509_certificate_list"
13+
.incbin "certs/x509_certificate_list"
1414
__cert_list_end:
1515

1616
.align 8
File renamed without changes.

crypto/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -1601,5 +1601,6 @@ config CRYPTO_HASH_INFO
16011601

16021602
source "drivers/crypto/Kconfig"
16031603
source crypto/asymmetric_keys/Kconfig
1604+
source certs/Kconfig
16041605

16051606
endif # if CRYPTO

init/Kconfig

-39
Original file line numberDiff line numberDiff line change
@@ -1740,31 +1740,6 @@ config MMAP_ALLOW_UNINITIALIZED
17401740

17411741
See Documentation/nommu-mmap.txt for more information.
17421742

1743-
config SYSTEM_TRUSTED_KEYRING
1744-
bool "Provide system-wide ring of trusted keys"
1745-
depends on KEYS
1746-
help
1747-
Provide a system keyring to which trusted keys can be added. Keys in
1748-
the keyring are considered to be trusted. Keys may be added at will
1749-
by the kernel from compiled-in data and from hardware key stores, but
1750-
userspace may only add extra keys if those keys can be verified by
1751-
keys already in the keyring.
1752-
1753-
Keys in this keyring are used by module signature checking.
1754-
1755-
config SYSTEM_TRUSTED_KEYS
1756-
string "Additional X.509 keys for default system keyring"
1757-
depends on SYSTEM_TRUSTED_KEYRING
1758-
help
1759-
If set, this option should be the filename of a PEM-formatted file
1760-
containing trusted X.509 certificates to be included in the default
1761-
system keyring. Any certificate used for module signing is implicitly
1762-
also trusted.
1763-
1764-
NOTE: If you previously provided keys for the system keyring in the
1765-
form of DER-encoded *.x509 files in the top-level build directory,
1766-
those are no longer used. You will need to set this option instead.
1767-
17681743
config SYSTEM_DATA_VERIFICATION
17691744
def_bool n
17701745
select SYSTEM_TRUSTED_KEYRING
@@ -1965,20 +1940,6 @@ config MODULE_SIG_HASH
19651940
default "sha384" if MODULE_SIG_SHA384
19661941
default "sha512" if MODULE_SIG_SHA512
19671942

1968-
config MODULE_SIG_KEY
1969-
string "File name or PKCS#11 URI of module signing key"
1970-
default "signing_key.pem"
1971-
depends on MODULE_SIG
1972-
help
1973-
Provide the file name of a private key/certificate in PEM format,
1974-
or a PKCS#11 URI according to RFC7512. The file should contain, or
1975-
the URI should identify, both the certificate and its corresponding
1976-
private key.
1977-
1978-
If this option is unchanged from its default "signing_key.pem",
1979-
then the kernel will automatically generate the private key and
1980-
certificate as described in Documentation/module-signing.txt
1981-
19821943
config MODULE_COMPRESS
19831944
bool "Compress modules on installation"
19841945
depends on MODULES

0 commit comments

Comments
 (0)