@@ -40,17 +40,67 @@ ifneq ($(TARGET),library)
4040 OBJS+=./hal/$(TARGET).o
4141endif
4242
43+ # User-provided key configuration
44+ # - USER_PRIVATE_KEY: Path to user's private key (DER format)
45+ # - USER_PUBLIC_KEY: Path to user's public key (DER format)
46+ # - USER_CERT_CHAIN: Path to user's certificate chain (DER format)
47+ # All must be provided together, or none at all
48+
49+ # Validate USER_PRIVATE_KEY and USER_PUBLIC_KEY are used together
50+ ifneq ($(USER_PRIVATE_KEY ) ,)
51+ ifeq ($(USER_PUBLIC_KEY),)
52+ $(error USER_PRIVATE_KEY requires USER_PUBLIC_KEY to also be set)
53+ endif
54+ ifeq ($(wildcard $(USER_PRIVATE_KEY)),)
55+ $(error USER_PRIVATE_KEY file not found : $(USER_PRIVATE_KEY ) )
56+ endif
57+ endif
58+
59+ ifneq ($(USER_PUBLIC_KEY ) ,)
60+ ifeq ($(USER_PRIVATE_KEY),)
61+ $(error USER_PUBLIC_KEY requires USER_PRIVATE_KEY to also be set)
62+ endif
63+ ifeq ($(wildcard $(USER_PUBLIC_KEY)),)
64+ $(error USER_PUBLIC_KEY file not found : $(USER_PUBLIC_KEY ) )
65+ endif
66+ endif
67+
68+ # Validate USER_CERT_CHAIN requires USER_PRIVATE_KEY and USER_PUBLIC_KEY
69+ ifneq ($(USER_CERT_CHAIN ) ,)
70+ ifeq ($(USER_PRIVATE_KEY),)
71+ $(error USER_CERT_CHAIN requires USER_PRIVATE_KEY to also be set)
72+ endif
73+ ifeq ($(USER_PUBLIC_KEY),)
74+ $(error USER_CERT_CHAIN requires USER_PUBLIC_KEY to also be set)
75+ endif
76+ ifeq ($(wildcard $(USER_CERT_CHAIN)),)
77+ $(error USER_CERT_CHAIN file not found : $(USER_CERT_CHAIN ) )
78+ endif
79+ endif
80+
4381ifeq ($(SIGN ) ,NONE)
4482 PRIVATE_KEY =
4583else
4684 # Key selection logic:
47- # - Without CERT_CHAIN_GEN: Single key (wolfboot_signing_private_key.der) signs everything
48- # - With CERT_CHAIN_GEN: Generate cert chain, use leaf key (test-dummy-ca/leaf-prvkey.der) for signing
49- # - With PRIVATE_KEY override: Use user-provided key (for offline cert chain workflow)
50- ifneq ($(CERT_CHAIN_GEN),)
51- PRIVATE_KEY? =test-dummy-ca/leaf-prvkey.der
85+ # 1. User-provided keys take precedence (USER_PRIVATE_KEY)
86+ # 2. If CERT_CHAIN_VERIFY enabled and USER_CERT_CHAIN not provided, auto-generate cert chain
87+ # 3. Otherwise use standard single key mode
88+ # PRIVATE_KEY can still be overridden on CLI
89+ ifneq ($(USER_PRIVATE_KEY),)
90+ PRIVATE_KEY =$(USER_PRIVATE_KEY )
5291 else
53- PRIVATE_KEY? =wolfboot_signing_private_key.der
92+ ifneq ($(CERT_CHAIN_VERIFY),)
93+ ifeq ($(USER_CERT_CHAIN),)
94+ # Auto-generate cert chain mode - use leaf key
95+ PRIVATE_KEY? =test-dummy-ca/leaf-prvkey.der
96+ else
97+ # User provided cert chain but no USER_PRIVATE_KEY - should have been caught by validation
98+ PRIVATE_KEY? =wolfboot_signing_private_key.der
99+ endif
100+ else
101+ # No cert chain verification - standard single key mode
102+ PRIVATE_KEY? =wolfboot_signing_private_key.der
103+ endif
54104 endif
55105 ifeq ($(FLASH_OTP_KEYSTORE),1)
56106 OBJS+ =./src/flash_otp_keystore.o
@@ -269,21 +319,31 @@ hal/$(TARGET).o:
269319
270320keytools_check : keytools
271321
272- # Generate the initial signing key
273- # - Always creates wolfboot_signing_private_key.der
274- # - If CERT_CHAIN_GEN is set , also generates cert chain with leaf key
322+ # Generate the initial signing key (only if not using user-provided keys)
323+ # - Creates wolfboot_signing_private_key.der when USER_PRIVATE_KEY is not set
324+ # - If CERT_CHAIN_VERIFY is enabled and USER_CERT_CHAIN not provided , also generates cert chain with leaf key
275325wolfboot_signing_private_key.der :
326+ ifeq ($(USER_PRIVATE_KEY ) ,)
276327 $(Q)$(MAKE) keytools_check
277328 $(Q)(test $(SIGN) = NONE) || ($(SIGN_ENV) "$(KEYGEN_TOOL)" $(KEYGEN_OPTIONS) -g wolfboot_signing_private_key.der) || true
278329 $(Q)(test $(SIGN) = NONE) && (echo "// SIGN=NONE" > src/keystore.c) || true
279330 $(Q)(test "$(FLASH_OTP_KEYSTORE)" = "1") && (make -C tools/keytools/otp) || true
280- $(Q ) (test $( SIGN) = NONE) || (test " $( CERT_CHAIN_VERIFY) " = " " ) || (test " $( CERT_CHAIN_GEN) " = " " ) || (tools/scripts/sim-gen-dummy-chain.sh --algo $( CERT_CHAIN_GEN_ALGO) --leaf wolfboot_signing_private_key.der)
331+ $(Q)(test $(SIGN) = NONE) || (test "$(CERT_CHAIN_VERIFY)" = "") || (test "$(USER_CERT_CHAIN)" != "") || (tools/scripts/sim-gen-dummy-chain.sh --algo $(CERT_CHAIN_GEN_ALGO) --leaf wolfboot_signing_private_key.der)
332+ else
333+ @echo "Using user-provided private key: $(USER_PRIVATE_KEY)"
334+ endif
281335
282- # CERT_CHAIN_GEN only: Ensure leaf key exists after cert chain generation
283- ifneq ($(CERT_CHAIN_GEN ) ,)
336+ # Auto-generate cert chain mode: Ensure leaf key exists after cert chain generation
337+ # Only applies when CERT_CHAIN_VERIFY is enabled and USER_CERT_CHAIN not provided
338+ # Skip this when using user-provided keys
339+ ifeq ($(USER_PRIVATE_KEY ) ,)
340+ ifneq ($(CERT_CHAIN_VERIFY ) ,)
341+ ifeq ($(USER_CERT_CHAIN ) ,)
284342$(PRIVATE_KEY ) : wolfboot_signing_private_key.der
285343 @test -f $(PRIVATE_KEY ) || (echo " Error: $( PRIVATE_KEY) not found" && exit 1)
286344endif
345+ endif
346+ endif
287347
288348$(SECONDARY_PRIVATE_KEY ) : $(PRIVATE_KEY ) keystore.der
289349 $(Q )$(MAKE ) keytools_check
@@ -436,13 +496,12 @@ srec: wolfboot.srec
436496 @echo " \t[ELF2SREC] $@ "
437497 @$(OBJCOPY ) -O srec $^ $@
438498
439- # When IMPORT_PUBLIC_KEY is set, generate keystore.c from the imported public key
440- # instead of relying on key generation. This supports offline cert chain workflow.
441- ifneq ($(IMPORT_PUBLIC_KEY ) ,)
442- src/keystore.c : $(IMPORT_PUBLIC_KEY )
443- @echo " Generating keystore from imported public key: $( IMPORT_PUBLIC_KEY) "
499+ # Keystore generation: use user-provided public key if available
500+ ifneq ($(USER_PUBLIC_KEY ) ,)
501+ src/keystore.c : $(USER_PUBLIC_KEY )
502+ @echo " Generating keystore from user-provided public key: $( USER_PUBLIC_KEY) "
444503 $(Q )$(MAKE ) keytools_check
445- $(Q )$(SIGN_ENV ) " $( KEYGEN_TOOL) " $(KEYGEN_OPTIONS ) --force -i $(IMPORT_PUBLIC_KEY )
504+ $(Q )$(SIGN_ENV ) " $( KEYGEN_TOOL) " $(KEYGEN_OPTIONS ) --force -i $(USER_PUBLIC_KEY )
446505else
447506src/keystore.c : $(PRIVATE_KEY )
448507endif
@@ -489,7 +548,7 @@ utilsclean: clean
489548
490549keysclean : clean
491550 $(Q ) rm -f * .pem * .der tags ./src/* _pub_key.c ./src/keystore.c include/target.h
492- $(Q ) (test " $( CERT_CHAIN_GEN ) " = " " ) || rm -rf test-dummy-ca || true
551+ $(Q ) (test " $( CERT_CHAIN_VERIFY ) " = " " || test " $( USER_CERT_CHAIN ) " ! = " " ) || rm -rf test-dummy-ca || true
493552
494553distclean : clean keysclean utilsclean
495554 $(Q ) rm -f * .bin * .elf
0 commit comments