mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Bastian Krause <bst@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Bastian Krause <bst@pengutronix.de>
Subject: [PATCH 1/3] keytoc: move __ENV__ resolving to dedicated function
Date: Mon,  6 Jan 2025 16:36:31 +0100	[thread overview]
Message-ID: <20250106153633.1470205-1-bst@pengutronix.de> (raw)

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




             reply	other threads:[~2025-01-06 15:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-06 15:36 Bastian Krause [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250106153633.1470205-1-bst@pengutronix.de \
    --to=bst@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox