All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philippe Reynes <philippe.reynes@softathome.com>
To: sjg@chromium.org, mr.nuke.me@gmail.com, joel.peshkin@broadcom.com
Cc: u-boot@lists.denx.de, Philippe Reynes <philippe.reynes@softathome.com>
Subject: [RFC PATCH v3 7/8] mkimage: add public key for image pre-load stage
Date: Wed, 17 Nov 2021 18:52:14 +0100	[thread overview]
Message-ID: <20211117175215.24262-8-philippe.reynes@softathome.com> (raw)
In-Reply-To: <20211117175215.24262-1-philippe.reynes@softathome.com>

This commit enhances mkimage to update the node
/image/pre-load/sig with the public key.

Signed-off-by: Philippe Reynes <philippe.reynes@softathome.com>
---
 include/image.h    |  15 ++++++
 tools/fit_image.c  |   3 ++
 tools/image-host.c | 116 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+)

diff --git a/include/image.h b/include/image.h
index 42fb01ab07..ac27e7acb2 100644
--- a/include/image.h
+++ b/include/image.h
@@ -1019,6 +1019,21 @@ int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
 
 int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
 
+/**
+ * fit_pre_load_data() - add public key to fdt blob
+ *
+ * @keydir:	Directory containing keys
+ * @keydest:	FDT blob to write public key
+ * @fit:	Pointer to the FIT format image header
+ *
+ * Adds public key to the node pre load.
+ *
+ * returns:
+ *	0, on success
+ *	< 0, on failure
+ */
+int fit_pre_load_data(const char *keydir, void *keydest, void *fit);
+
 int fit_cipher_data(const char *keydir, void *keydest, void *fit,
 		    const char *comment, int require_keys,
 		    const char *engine_id, const char *cmdname);
diff --git a/tools/fit_image.c b/tools/fit_image.c
index f4f372ba62..43ce41efbe 100644
--- a/tools/fit_image.c
+++ b/tools/fit_image.c
@@ -59,6 +59,9 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
 		ret = fit_set_timestamp(ptr, 0, time);
 	}
 
+	if (!ret)
+		ret = fit_pre_load_data(params->keydir, dest_blob, ptr);
+
 	if (!ret) {
 		ret = fit_cipher_data(params->keydir, dest_blob, ptr,
 				      params->comment,
diff --git a/tools/image-host.c b/tools/image-host.c
index a6b0a94420..20e59c14a9 100644
--- a/tools/image-host.c
+++ b/tools/image-host.c
@@ -14,6 +14,11 @@
 #include <image.h>
 #include <version.h>
 
+#include <openssl/pem.h>
+#include <openssl/evp.h>
+
+#define IMAGE_PRE_LOAD_PATH                             "/image/pre-load/sig"
+
 /**
  * fit_set_hash_value - set hash value in requested has node
  * @fit: pointer to the FIT format image header
@@ -1020,6 +1025,117 @@ static int fit_config_add_verification_data(const char *keydir,
 	return 0;
 }
 
+/*
+ * 0) open file (open)
+ * 1) read certificate (PEM_read_X509)
+ * 2) get public key (X509_get_pubkey)
+ * 3) provide der format (d2i_RSAPublicKey)
+ */
+static int read_pub_key(const char *keydir, const void *name,
+			unsigned char **pubkey, int *pubkey_len)
+{
+	char path[1024];
+	EVP_PKEY *key = NULL;
+	X509 *cert;
+	FILE *f;
+	int ret;
+
+	memset(path, 0, 1024);
+	snprintf(path, sizeof(path), "%s/%s.crt", keydir, (char *)name);
+
+	/* Open certificate file */
+	f = fopen(path, "r");
+	if (!f) {
+		fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
+			path, strerror(errno));
+		return -EACCES;
+	}
+
+	/* Read the certificate */
+	cert = NULL;
+	if (!PEM_read_X509(f, &cert, NULL, NULL)) {
+		printf("Couldn't read certificate");
+		ret = -EINVAL;
+		goto err_cert;
+	}
+
+	/* Get the public key from the certificate. */
+	key = X509_get_pubkey(cert);
+	if (!key) {
+		printf("Couldn't read public key\n");
+		ret = -EINVAL;
+		goto err_pubkey;
+	}
+
+	/* Get DER form */
+	ret = i2d_PublicKey(key, pubkey);
+	if (ret < 0) {
+		printf("Couldn't get DER form\n");
+		ret = -EINVAL;
+		goto err_pubkey;
+	}
+
+	*pubkey_len = ret;
+	ret = 0;
+
+err_pubkey:
+	X509_free(cert);
+err_cert:
+	fclose(f);
+	return ret;
+}
+
+int fit_pre_load_data(const char *keydir, void *keydest, void *fit)
+{
+	int pre_load_noffset;
+	const void *header_size, *algo_name;
+	const void *key_name;
+	unsigned char *pubkey = NULL;
+	int ret, pubkey_len;
+
+	if (!keydir || !keydest || !fit)
+		return 0;
+
+	/* Search node pre-load sig */
+	pre_load_noffset = fdt_path_offset(keydest, IMAGE_PRE_LOAD_PATH);
+	if (pre_load_noffset < 0) {
+		ret = 0;
+		goto out;
+	}
+
+	header_size  = fdt_getprop(keydest, pre_load_noffset, "header-size", NULL);
+	algo_name    = fdt_getprop(keydest, pre_load_noffset, "algo-name", NULL);
+	key_name     = fdt_getprop(keydest, pre_load_noffset, "key-name", NULL);
+
+	/* Check that all mandatory properties are present */
+	if (!header_size || !algo_name || !key_name) {
+		if (!header_size)
+			printf("The property header-size is missing in the node %s\n",
+			       IMAGE_PRE_LOAD_PATH);
+		if (!algo_name)
+			printf("The property algo-name is missing in the node %s\n",
+			       IMAGE_PRE_LOAD_PATH);
+		if (!key_name)
+			printf("The property key-name is missing in the node %s\n",
+			       IMAGE_PRE_LOAD_PATH);
+		ret = -ENODATA;
+		goto out;
+	}
+
+	/* Read public key */
+	ret = read_pub_key(keydir, key_name, &pubkey, &pubkey_len);
+	if (ret < 0)
+		goto out;
+
+	/* Add the public key to the device tree */
+	ret = fdt_setprop(keydest, pre_load_noffset, "public-key", pubkey, pubkey_len);
+	if (ret)
+		printf("Can't set public-key in node %s\n", IMAGE_PRE_LOAD_PATH);
+
+ out:
+	return ret;
+}
+
 int fit_cipher_data(const char *keydir, void *keydest, void *fit,
 		    const char *comment, int require_keys,
 		    const char *engine_id, const char *cmdname)
-- 
2.17.1


  parent reply	other threads:[~2021-11-17 17:54 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-17 17:52 [RFC PATCH v3 0/8] image: add a stage pre-load Philippe Reynes
2021-11-17 17:52 ` [RFC PATCH v3 1/8] lib: allow to build asn1 decoder and oid registry in SPL Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 2/8] lib: crypto: allow to build crypyo " Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 3/8] lib: rsa: allow rsa verify with pkey " Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 4/8] boot: image: add a stage pre-load Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 5/8] cmd: bootm: " Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` [RFC PATCH v3 6/8] common: spl: fit_ram: allow to use image pre load Philippe Reynes
2021-11-25  0:12   ` Simon Glass
2021-11-17 17:52 ` Philippe Reynes [this message]
2021-11-25  0:13   ` [RFC PATCH v3 7/8] mkimage: add public key for image pre-load stage Simon Glass
2021-12-03 10:29     ` Philippe REYNES
2021-11-17 17:52 ` [RFC PATCH v3 8/8] tools: gen_pre_load_header.sh: initial import Philippe Reynes
2021-11-25  0:13   ` Simon Glass
2021-12-06  8:23   ` Rasmus Villemoes
2021-12-08 18:10     ` Philippe REYNES
2021-12-09  1:04       ` Rasmus Villemoes
2021-12-10  0:14       ` Simon Glass
2021-12-10  7:41         ` Rasmus Villemoes
2021-12-11 11:37           ` Simon Glass
2021-11-25  0:13 ` [RFC PATCH v3 0/8] image: add a stage pre-load Simon Glass

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=20211117175215.24262-8-philippe.reynes@softathome.com \
    --to=philippe.reynes@softathome.com \
    --cc=joel.peshkin@broadcom.com \
    --cc=mr.nuke.me@gmail.com \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.