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