mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 1/2] keytoc: make keyspec a const char pointer
@ 2025-07-24 13:50 Marco Felsch
  2025-07-24 13:50 ` [PATCH v2 2/2] keytoc: fix env provided keyspec handling Marco Felsch
  0 siblings, 1 reply; 2+ messages in thread
From: Marco Felsch @ 2025-07-24 13:50 UTC (permalink / raw)
  To: barebox

Currently the code is working on the keypsec directly, this has the
drawback of the already existing freep variable to handle optional
key-names accordingly.

Delcare the keyspec as const and never operate on in directly to keep it
as it is and instead always alloc path and keyname, so the freep
variable can be dropped. For the split operation a simple helper was
added.

This prepares the code to fix the keyspec env handling which is done by
the next commit.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/keytoc.c | 54 +++++++++++++++++++++++++++++++++---------------
 1 file changed, 37 insertions(+), 17 deletions(-)

diff --git a/scripts/keytoc.c b/scripts/keytoc.c
index c92465707f65..617317d6607e 100644
--- a/scripts/keytoc.c
+++ b/scripts/keytoc.c
@@ -653,6 +653,34 @@ static int gen_key(const char *keyname, const char *path)
 	return ret;
 }
 
+static void get_name_path(const char *keyspec, char **keyname, char **path)
+{
+	char *sep, *spec;
+
+	spec = strdup(keyspec);
+	if (!spec)
+		enomem_exit(__func__);
+
+	/* Split <key-hint>:<key-path> pair, <key-hint> is optional */
+	sep = strchr(spec, ':');
+	if (!sep) {
+		*path = spec;
+		return;
+	}
+
+	*sep = 0;
+	*keyname = strdup(spec);
+	if (!*keyname)
+		enomem_exit(__func__);
+
+	sep++;
+	*path = strdup(sep);
+	if (!*path)
+		enomem_exit(__func__);
+
+	free(spec);
+}
+
 int main(int argc, char *argv[])
 {
 	int i, opt, ret;
@@ -705,35 +733,27 @@ int main(int argc, char *argv[])
 	}
 
 	for (i = optind; i < argc; i++) {
-		char *keyspec = argv[i];
+		const char *keyspec = argv[i];
 		char *keyname = NULL;
-		char *path, *freep = NULL;
+		char *path = NULL;
 
-		if (!strncmp(keyspec, "pkcs11:", 7)) {
-			path = keyspec;
-		} else {
-			path = strchr(keyspec, ':');
-			if (path) {
-				*path = 0;
-				path++;
-				keyname = keyspec;
-			} else {
-				path = keyspec;
-			}
-		}
+		if (!strncmp(keyspec, "pkcs11:", 7))
+			path = strdup(keyspec);
+		else
+			get_name_path(keyspec, &keyname, &path);
 
 		if (!keyname) {
-			ret = asprintf(&freep, "key_%d", keynum++);
+			ret = asprintf(&keyname, "key_%d", keynum++);
 			if (ret < 0)
 				enomem_exit("asprintf");
-			keyname = freep;
 		}
 
 		ret = gen_key(keyname, path);
 		if (ret)
 			exit(1);
 
-		free(freep);
+		free(keyname);
+		free(path);
 	}
 
 	if (dts) {
-- 
2.39.5




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

* [PATCH v2 2/2] keytoc: fix env provided keyspec handling
  2025-07-24 13:50 [PATCH v2 1/2] keytoc: make keyspec a const char pointer Marco Felsch
@ 2025-07-24 13:50 ` Marco Felsch
  0 siblings, 0 replies; 2+ messages in thread
From: Marco Felsch @ 2025-07-24 13:50 UTC (permalink / raw)
  To: barebox

Currently the env provided keyspec is resolved during the gen_key() step
by making use of the try_resolve_env(). This is wrong because it will
set the complete <hint>:<key> keyspec string for the 'keyname' and 'path'.

To fix this the resolve step must happen during the main-loop as first step
because the main-loop is doing the 'keyname' and 'path' split already.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 scripts/keytoc.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/scripts/keytoc.c b/scripts/keytoc.c
index 617317d6607e..105fa4b5413d 100644
--- a/scripts/keytoc.c
+++ b/scripts/keytoc.c
@@ -615,10 +615,6 @@ 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) {
@@ -628,10 +624,6 @@ static int gen_key(const char *keyname, const char *path)
 	}
 
 	/* path/URI handling */
-	path = try_resolve_env(path);
-	if (!path)
-		exit(1);
-
 	if (!strncmp(path, "pkcs11:", 7)) {
 		ret = engine_get_pub_key(path, &key);
 		if (ret)
@@ -737,6 +729,11 @@ int main(int argc, char *argv[])
 		char *keyname = NULL;
 		char *path = NULL;
 
+		/* Check if the keyspec is provided by the ENV first */
+		keyspec = try_resolve_env(keyspec);
+		if (!keyspec)
+			exit(1);
+
 		if (!strncmp(keyspec, "pkcs11:", 7))
 			path = strdup(keyspec);
 		else
-- 
2.39.5




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

end of thread, other threads:[~2025-07-24 13:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-07-24 13:50 [PATCH v2 1/2] keytoc: make keyspec a const char pointer Marco Felsch
2025-07-24 13:50 ` [PATCH v2 2/2] keytoc: fix env provided keyspec handling Marco Felsch

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