xref: /openbmc/linux/certs/Makefile (revision ea35e0d5)
1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
2cfc411e7SDavid Howells#
3cfc411e7SDavid Howells# Makefile for the linux kernel signature checking certificates.
4cfc411e7SDavid Howells#
5cfc411e7SDavid Howells
62565ca7fSEric Snowbergobj-$(CONFIG_SYSTEM_TRUSTED_KEYRING) += system_keyring.o system_certificates.o common.o
7d1f04410SEric Snowbergobj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist.o common.o
8d1f04410SEric Snowbergobj-$(CONFIG_SYSTEM_REVOCATION_LIST) += revocation_certificates.o
9734114f8SDavid Howellsifneq ($(CONFIG_SYSTEM_BLACKLIST_HASH_LIST),"")
10734114f8SDavid Howellsobj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist_hashes.o
11734114f8SDavid Howellselse
12734114f8SDavid Howellsobj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist_nohashes.o
13734114f8SDavid Howellsendif
14cfc411e7SDavid Howells
15cfc411e7SDavid Howellsifeq ($(CONFIG_SYSTEM_TRUSTED_KEYRING),y)
16cfc411e7SDavid Howells
17cfc411e7SDavid Howells$(eval $(call config_filename,SYSTEM_TRUSTED_KEYS))
18cfc411e7SDavid Howells
19cfc411e7SDavid Howells# GCC doesn't include .incbin files in -MD generated dependencies (PR#66871)
20cfc411e7SDavid Howells$(obj)/system_certificates.o: $(obj)/x509_certificate_list
21cfc411e7SDavid Howells
22cfc411e7SDavid Howells# Cope with signing_key.x509 existing in $(srctree) not $(objtree)
23cfc411e7SDavid HowellsAFLAGS_system_certificates.o := -I$(srctree)
24cfc411e7SDavid Howells
25cfc411e7SDavid Howellsquiet_cmd_extract_certs  = EXTRACT_CERTS   $(patsubst "%",%,$(2))
26172caf19SMasahiro Yamada      cmd_extract_certs  = scripts/extract-cert $(2) $@
27cfc411e7SDavid Howells
28cfc411e7SDavid Howellstargets += x509_certificate_list
29cfc411e7SDavid Howells$(obj)/x509_certificate_list: scripts/extract-cert $(SYSTEM_TRUSTED_KEYS_SRCPREFIX)$(SYSTEM_TRUSTED_KEYS_FILENAME) FORCE
30cfc411e7SDavid Howells	$(call if_changed,extract_certs,$(SYSTEM_TRUSTED_KEYS_SRCPREFIX)$(CONFIG_SYSTEM_TRUSTED_KEYS))
315ccbdbf9SJarkko Sakkinenendif # CONFIG_SYSTEM_TRUSTED_KEYRING
32cfc411e7SDavid Howells
33d1f04410SEric Snowbergclean-files := x509_certificate_list .x509.list x509_revocation_list
34cfc411e7SDavid Howells
35cfc411e7SDavid Howellsifeq ($(CONFIG_MODULE_SIG),y)
360165f4caSNayna Jain	SIGN_KEY = y
370165f4caSNayna Jainendif
380165f4caSNayna Jain
390165f4caSNayna Jainifeq ($(CONFIG_IMA_APPRAISE_MODSIG),y)
40781a5739SNayna Jainifeq ($(CONFIG_MODULES),y)
410165f4caSNayna Jain	SIGN_KEY = y
420165f4caSNayna Jainendif
43781a5739SNayna Jainendif
440165f4caSNayna Jain
450165f4caSNayna Jainifdef SIGN_KEY
46cfc411e7SDavid Howells###############################################################################
47cfc411e7SDavid Howells#
48cfc411e7SDavid Howells# If module signing is requested, say by allyesconfig, but a key has not been
49cfc411e7SDavid Howells# supplied, then one will need to be generated to make sure the build does not
50cfc411e7SDavid Howells# fail and that the kernel may be used afterwards.
51cfc411e7SDavid Howells#
52cfc411e7SDavid Howells###############################################################################
53cfc411e7SDavid Howellsifndef CONFIG_MODULE_SIG_HASH
54cfc411e7SDavid Howells$(error Could not determine digest type to use from kernel config)
55cfc411e7SDavid Howellsendif
56cfc411e7SDavid Howells
575d06ee20SArnd Bergmannredirect_openssl	= 2>&1
585d06ee20SArnd Bergmannquiet_redirect_openssl	= 2>&1
595d06ee20SArnd Bergmannsilent_redirect_openssl = 2>/dev/null
60*ea35e0d5SStefan Bergeropenssl_available       = $(shell openssl help 2>/dev/null && echo yes)
615d06ee20SArnd Bergmann
62cfc411e7SDavid Howells# We do it this way rather than having a boolean option for enabling an
63cfc411e7SDavid Howells# external private key, because 'make randconfig' might enable such a
64cfc411e7SDavid Howells# boolean option and we unfortunately can't make it depend on !RANDCONFIG.
65cfc411e7SDavid Howellsifeq ($(CONFIG_MODULE_SIG_KEY),"certs/signing_key.pem")
66*ea35e0d5SStefan Berger
67*ea35e0d5SStefan Bergerifeq ($(openssl_available),yes)
68*ea35e0d5SStefan BergerX509TEXT=$(shell openssl x509 -in "certs/signing_key.pem" -text 2>/dev/null)
69*ea35e0d5SStefan Berger
70*ea35e0d5SStefan Berger$(if $(findstring rsaEncryption,$(X509TEXT)),,$(shell rm -f "certs/signing_key.pem"))
71*ea35e0d5SStefan Bergerendif
72*ea35e0d5SStefan Berger
73cfc411e7SDavid Howells$(obj)/signing_key.pem: $(obj)/x509.genkey
745d06ee20SArnd Bergmann	@$(kecho) "###"
755d06ee20SArnd Bergmann	@$(kecho) "### Now generating an X.509 key pair to be used for signing modules."
765d06ee20SArnd Bergmann	@$(kecho) "###"
775d06ee20SArnd Bergmann	@$(kecho) "### If this takes a long time, you might wish to run rngd in the"
785d06ee20SArnd Bergmann	@$(kecho) "### background to keep the supply of entropy topped up.  It"
795d06ee20SArnd Bergmann	@$(kecho) "### needs to be run as root, and uses a hardware random"
805d06ee20SArnd Bergmann	@$(kecho) "### number generator if one is available."
815d06ee20SArnd Bergmann	@$(kecho) "###"
825d06ee20SArnd Bergmann	$(Q)openssl req -new -nodes -utf8 -$(CONFIG_MODULE_SIG_HASH) -days 36500 \
83cfc411e7SDavid Howells		-batch -x509 -config $(obj)/x509.genkey \
84cfc411e7SDavid Howells		-outform PEM -out $(obj)/signing_key.pem \
855d06ee20SArnd Bergmann		-keyout $(obj)/signing_key.pem \
865d06ee20SArnd Bergmann		$($(quiet)redirect_openssl)
875d06ee20SArnd Bergmann	@$(kecho) "###"
885d06ee20SArnd Bergmann	@$(kecho) "### Key pair generated."
895d06ee20SArnd Bergmann	@$(kecho) "###"
90cfc411e7SDavid Howells
91cfc411e7SDavid Howells$(obj)/x509.genkey:
925d06ee20SArnd Bergmann	@$(kecho) Generating X.509 key generation config
93cfc411e7SDavid Howells	@echo  >$@ "[ req ]"
94cfc411e7SDavid Howells	@echo >>$@ "default_bits = 4096"
95cfc411e7SDavid Howells	@echo >>$@ "distinguished_name = req_distinguished_name"
96cfc411e7SDavid Howells	@echo >>$@ "prompt = no"
97cfc411e7SDavid Howells	@echo >>$@ "string_mask = utf8only"
98cfc411e7SDavid Howells	@echo >>$@ "x509_extensions = myexts"
99cfc411e7SDavid Howells	@echo >>$@
100cfc411e7SDavid Howells	@echo >>$@ "[ req_distinguished_name ]"
101cfc411e7SDavid Howells	@echo >>$@ "#O = Unspecified company"
102cfc411e7SDavid Howells	@echo >>$@ "CN = Build time autogenerated kernel key"
103cfc411e7SDavid Howells	@echo >>$@ "#emailAddress = unspecified.user@unspecified.company"
104cfc411e7SDavid Howells	@echo >>$@
105cfc411e7SDavid Howells	@echo >>$@ "[ myexts ]"
106cfc411e7SDavid Howells	@echo >>$@ "basicConstraints=critical,CA:FALSE"
107cfc411e7SDavid Howells	@echo >>$@ "keyUsage=digitalSignature"
108cfc411e7SDavid Howells	@echo >>$@ "subjectKeyIdentifier=hash"
109cfc411e7SDavid Howells	@echo >>$@ "authorityKeyIdentifier=keyid"
1105ccbdbf9SJarkko Sakkinenendif # CONFIG_MODULE_SIG_KEY
111cfc411e7SDavid Howells
112cfc411e7SDavid Howells$(eval $(call config_filename,MODULE_SIG_KEY))
113cfc411e7SDavid Howells
114cfc411e7SDavid Howells# If CONFIG_MODULE_SIG_KEY isn't a PKCS#11 URI, depend on it
115cfc411e7SDavid Howellsifeq ($(patsubst pkcs11:%,%,$(firstword $(MODULE_SIG_KEY_FILENAME))),$(firstword $(MODULE_SIG_KEY_FILENAME)))
116cfc411e7SDavid HowellsX509_DEP := $(MODULE_SIG_KEY_SRCPREFIX)$(MODULE_SIG_KEY_FILENAME)
117cfc411e7SDavid Howellsendif
118cfc411e7SDavid Howells
119cfc411e7SDavid Howells# GCC PR#66871 again.
120cfc411e7SDavid Howells$(obj)/system_certificates.o: $(obj)/signing_key.x509
121cfc411e7SDavid Howells
12262172c81SDavid Woodhousetargets += signing_key.x509
12362172c81SDavid Woodhouse$(obj)/signing_key.x509: scripts/extract-cert $(X509_DEP) FORCE
12462172c81SDavid Woodhouse	$(call if_changed,extract_certs,$(MODULE_SIG_KEY_SRCPREFIX)$(CONFIG_MODULE_SIG_KEY))
1255ccbdbf9SJarkko Sakkinenendif # CONFIG_MODULE_SIG
126d1f04410SEric Snowberg
127d1f04410SEric Snowbergifeq ($(CONFIG_SYSTEM_REVOCATION_LIST),y)
128d1f04410SEric Snowberg
129d1f04410SEric Snowberg$(eval $(call config_filename,SYSTEM_REVOCATION_KEYS))
130d1f04410SEric Snowberg
131d1f04410SEric Snowberg$(obj)/revocation_certificates.o: $(obj)/x509_revocation_list
132d1f04410SEric Snowberg
133d1f04410SEric Snowbergquiet_cmd_extract_certs  = EXTRACT_CERTS   $(patsubst "%",%,$(2))
134d1f04410SEric Snowberg      cmd_extract_certs  = scripts/extract-cert $(2) $@
135d1f04410SEric Snowberg
136d1f04410SEric Snowbergtargets += x509_revocation_list
137d1f04410SEric Snowberg$(obj)/x509_revocation_list: scripts/extract-cert $(SYSTEM_REVOCATION_KEYS_SRCPREFIX)$(SYSTEM_REVOCATION_KEYS_FILENAME) FORCE
138d1f04410SEric Snowberg	$(call if_changed,extract_certs,$(SYSTEM_REVOCATION_KEYS_SRCPREFIX)$(CONFIG_SYSTEM_REVOCATION_KEYS))
139d1f04410SEric Snowbergendif
140