mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] keytoc: move __ENV__ resolving to dedicated function
@ 2025-01-06 15:36 Bastian Krause
  2025-01-06 15:36 ` [PATCH 2/3] keytoc: allow __ENV__ lookup for keyname hint Bastian Krause
  2025-01-06 15:36 ` [PATCH 3/3] crypto: document __ENV__ lookup in CRYPTO_PUBLIC_KEYS Bastian Krause
  0 siblings, 2 replies; 3+ messages in thread
From: Bastian Krause @ 2025-01-06 15:36 UTC (permalink / raw)
  To: barebox; +Cc: Bastian Krause

A future commit will also allow the FIT keyname hint to be passed via
an environment variable. So move the resolving to a dedicated function
now and use it for the FIT key name hint in a future commit.

Signed-off-by: Bastian Krause <bst@pengutronix.de>
---
 scripts/keytoc.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/scripts/keytoc.c b/scripts/keytoc.c
index c60df8a5f01..9b3522a602f 100644
--- a/scripts/keytoc.c
+++ b/scripts/keytoc.c
@@ -509,6 +509,24 @@ static int gen_key_ecdsa(EVP_PKEY *key, const char *key_name, const char *key_na
 	return 0;
 }
 
+static const char *try_resolve_env(const char *input)
+{
+	const char *var;
+
+	if (strncmp(input, "__ENV__", 7))
+		return input;
+
+	var = getenv(input + 7);
+	if (!var || *var == '\0') {
+		fprintf(stderr,
+			"environment variable \"%s\" is not set or empty\n",
+			input + 7);
+		return NULL;
+	}
+
+	return var;
+}
+
 static int gen_key_rsa(EVP_PKEY *key, const char *key_name, const char *key_name_c)
 {
 	BIGNUM *modulus, *r_squared;
@@ -579,6 +597,7 @@ static int gen_key(const char *keyname, const char *path)
 	EVP_PKEY *key;
 	char *tmp, *key_name_c;
 
+	/* key name handling */
 	tmp = key_name_c = strdup(keyname);
 
 	while (*tmp) {
@@ -587,15 +606,10 @@ static int gen_key(const char *keyname, const char *path)
 		tmp++;
 	}
 
-	if (!strncmp(path, "__ENV__", 7)) {
-		const char *var = getenv(path + 7);
-		if (!var) {
-			fprintf(stderr,
-				"environment variable \"%s\" is empty\n", path + 7);
-			exit(1);
-		}
-		path = var;
-	}
+	/* path/URI handling */
+	path = try_resolve_env(path);
+	if (!path)
+		exit(1);
 
 	if (!strncmp(path, "pkcs11:", 7)) {
 		ret = engine_get_pub_key(path, &key);
@@ -607,6 +621,7 @@ static int gen_key(const char *keyname, const char *path)
 			exit(1);
 	}
 
+	/* generate built-in keys */
 	ret = gen_key_ecdsa(key, keyname, key_name_c);
 	if (ret == -EOPNOTSUPP)
 		return ret;
-- 
2.39.5




^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/3] keytoc: allow __ENV__ lookup for keyname hint
  2025-01-06 15:36 [PATCH 1/3] keytoc: move __ENV__ resolving to dedicated function Bastian Krause
@ 2025-01-06 15:36 ` Bastian Krause
  2025-01-06 15:36 ` [PATCH 3/3] crypto: document __ENV__ lookup in CRYPTO_PUBLIC_KEYS Bastian Krause
  1 sibling, 0 replies; 3+ messages in thread
From: Bastian Krause @ 2025-01-06 15:36 UTC (permalink / raw)
  To: barebox; +Cc: Bastian Krause

CONFIG_CRYPTO_PUBLIC_KEYS already supports __ENV__VARNAME making barebox
look up the value in the environment variable VARNAME instead. But this
only works for the public key (path or PKCS#11 URI), not for the
corresponding (optional) key name hint.

Change that by using the same logic as for the public key for the key
name hint.

For example, ..

  CONFIG_CRYPTO_PUBLIC_KEYS="__ENV__FIT_KEY_NAME_HINT:__ENV__FIT_KEY"

..would look up the environment variables KEY_NAME_HINT and FIT_KEY.
Multiple keys can still be passed by space separating multiple key name
hint and public key combinations.

Signed-off-by: Bastian Krause <bst@pengutronix.de>
---
 scripts/keytoc.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/scripts/keytoc.c b/scripts/keytoc.c
index 9b3522a602f..cf9c2f52482 100644
--- a/scripts/keytoc.c
+++ b/scripts/keytoc.c
@@ -598,6 +598,10 @@ static int gen_key(const char *keyname, const char *path)
 	char *tmp, *key_name_c;
 
 	/* key name handling */
+	keyname = try_resolve_env(keyname);
+	if (!keyname)
+		exit(1);
+
 	tmp = key_name_c = strdup(keyname);
 
 	while (*tmp) {
-- 
2.39.5




^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 3/3] crypto: document __ENV__ lookup in CRYPTO_PUBLIC_KEYS
  2025-01-06 15:36 [PATCH 1/3] keytoc: move __ENV__ resolving to dedicated function Bastian Krause
  2025-01-06 15:36 ` [PATCH 2/3] keytoc: allow __ENV__ lookup for keyname hint Bastian Krause
@ 2025-01-06 15:36 ` Bastian Krause
  1 sibling, 0 replies; 3+ messages in thread
From: Bastian Krause @ 2025-01-06 15:36 UTC (permalink / raw)
  To: barebox; +Cc: Bastian Krause

Signed-off-by: Bastian Krause <bst@pengutronix.de>
---
 crypto/Kconfig | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 191bd510262..14728be4aa9 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -144,6 +144,10 @@ config CRYPTO_PUBLIC_KEYS
 	  This avoids the mkimage dependency of CONFIG_BOOTM_FITIMAGE_PUBKEY
 	  at the cost of an openssl build-time dependency.
 
+	  Placeholders such as __ENV__VAR_NAME can be used to look up the
+	  corresponding value in the environment variable VAR_NAME for both
+	  public key paths/URIs as well as key name hints.
+
 config CRYPTO_KEYSTORE
 	bool "Keystore"
 	help
-- 
2.39.5




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-01-06 15:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-06 15:36 [PATCH 1/3] keytoc: move __ENV__ resolving to dedicated function Bastian Krause
2025-01-06 15:36 ` [PATCH 2/3] keytoc: allow __ENV__ lookup for keyname hint Bastian Krause
2025-01-06 15:36 ` [PATCH 3/3] crypto: document __ENV__ lookup in CRYPTO_PUBLIC_KEYS Bastian Krause

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox