mkimage: Add a 'keyfile' argument for image signing
cover mkimage: Add a 'keyfile' argument for image signing
This series is a continuation of the following series
* [PATCH v5 0/6] Add support for ECDSA image signing (with test)
There were some requests in the previous series to be more consistent between RSA and ECDSA paths, particularly in the area of handling the 'keydir' argument.
This series adds a 'keyfile' argument to be used in lieu of the antiquated 'keydir'. Using 'keyfile' allows signing images that do not contain a "key-name-hint" property.
Other issues, such as automatically adding the 'signature' node were discussed. Such features are beyond the scope of this series. I am going to halt any further work on mkimage because of time constraints. I hope the corrections in this series are sufficient to finally allow users to sign their images with ECDSA.
Alexandru Gagniuc (4):
doc: signature.txt: Document the keydir and keyfile arguments
mkimage: Add a 'keyfile' argument for image signing lib/rsa: Use the 'keyfile' argument from mkimage lib/ecdsa: Use the 'keydir' argument from mkimage if appropriate
doc/uImage.FIT/signature.txt | 13 +++++++++ include/image.h | 8 ++++-- lib/ecdsa/ecdsa-libcrypto.c | 10 ++++++- lib/rsa/rsa-sign.c | 34 +++++++++++++++++------ tools/fit_image.c | 3 +- tools/image-host.c | 54 ++++++++++++++++++++---------------- tools/imagetool.h | 1 + tools/mkimage.c | 6 +++- 8 files changed, 91 insertions(+), 38 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/4 doc: signature.txt: Document the keydir and keyfile arguments
After lots of debating, this documents how we'd like mkimage to treat 'keydir' and 'keyfile' arguments. The rest is in the docs.
--- doc/uImage.FIT/signature.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+)
doc/uImage.FIT/signature.txt +13 -0
@@ -472,6 +472,19 @@ Test Verified Boot Run: signed config with bad hash: OK Test passed +Software signing: keydir vs keyfile +----------------------------------- + +In the simplest case, signing is done by giving mkimage the 'keyfile'. This is +the path to a file containing the signing key. + +The alternative is to pass the 'keydir' argument. In this case the filename of +the key is derived from the 'keydir' and the "key-name-hint" property in the +FIT. In this case the "key-name-hint" property is mandatory, and the key must +exist in "<keydir>/<key-name-hint>.<ext>" Here the extension "ext" is +specific to the signing algorithm. + + Hardware Signing with PKCS#11 or with HSM -----------------------------------------
Signature · 1 line
2.26.2
PATCH 1/4 doc: signature.txt: Document the keydir and keyfile arguments
Quoted 8 lines · Alexandru Gagniuc
After lots of debating, this documents how we'd like mkimage to treat 'keydir' and 'keyfile' arguments. The rest is in the docs.
Signed-off-by Alexandru Gagniuc <[email protected]>--- doc/uImage.FIT/signature.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+)
PATCH 2/4 mkimage: Add a 'keyfile' argument for image signing
In reply to Alexandru GagniucIt's not always desirable to use 'keydir' and some ad-hoc heuristics to get the filename of the signing key. More often, just passing the filename is the simpler, easier, and logical thing to do.
Since mkimage doesn't use long options, we're slowly running out of letters. I've chosen '-G' because it was available.
--- include/image.h | 8 ++++--- tools/fit_image.c | 3 ++- tools/image-host.c | 58 +++++++++++++++++++++++++--------------------- tools/imagetool.h | 1 + tools/mkimage.c | 6 ++++- 5 files changed, 45 insertions(+), 31 deletions(-)
include/image.h +5 -3
@@ -1128,9 +1128,10 @@ int fit_cipher_data(const char *keydir, void *keydest, void *fit,* 0, on success * libfdt error code, on failure */ -int fit_add_verification_data (const char *keydir,void *keydest,void *fit, -const char *comment,int require_keys, -const char *engine_id,const char *cmdname); +int fit_add_verification_data (const char *keydir,const char *keyfile, +void *keydest,void *fit,const char *comment, +int require_keys,const char *engine_id, +const char *cmdname);int fit_image_verify_with_data (const void *fit,int image_noffset,const void *data,size_t size);
@@ -1236,6 +1237,7 @@ void image_set_host_blob(void *host_blob);struct image_sign_info {const char *keydir;/* Directory conaining keys */ const char *keyname;/* Name of key to use */ +const char *keyfile;/* Filename of private or public key */ void *fit;/* Pointer to FIT blob */ int node_offset;/* Offset of signature node */ const char *name;/* Algorithm name */
tools/fit_image.c +2 -1
@@ -68,7 +68,8 @@ static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, }if (!ret) { - ret = fit_add_verification_data(params->keydir, dest_blob, ptr, + ret = fit_add_verification_data(params->keydir, + params->keyfile, dest_blob, ptr, params->comment, params->require_keys, params->engine_id,
tools/image-host.c +32 -26
144 lines not on this page · raw message
tools/imagetool.h +1 -0
@@ -67,6 +67,7 @@ struct image_tool_params {const char *outfile;/* Output filename */ const char *keydir;/* Directory holding private keys */ const char *keydest;/* Destination .dtb for public key */ +const char *keyfile;/* Filename of private or public key */ const char *comment;/* Comment to add to signature node */ int require_keys;/* 1 to mark signing keys as 'required' */ int file_size;/* Total size of output file */
tools/mkimage.c +5 -1
@@ -108,6 +108,7 @@ static void usage(const char *msg)"Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n" " -k => set directory containing private keys\n" " -K => write public keys to this .dtb file\n" +" -G => use this signing key (in lieu of -k)\n" " -c => add comment in signature node\n" " -F => re-sign existing FIT image\n" " -p => place external data at a static position\n"
@@ -151,7 +152,7 @@ static void process_args(int argc, char **argv)int opt;while ((opt =getopt (argc, argv, -"a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx" )) !=-1 ) { +"a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:O:rR:qstT:vVx" )) !=-1 ) {switch (opt) {case 'a' : params.addr =strtoull (optarg, &ptr,16 );
@@ -226,6 +227,9 @@ static void process_args(int argc, char **argv) params.type =IH_TYPE_FLATDT ; params.fflag =1 ;break ; +case 'G' : + params.keyfile = optarg; +break ;case 'i' : params.fit_ramdisk = optarg;break ;
Signature · 1 line
2.26.2
PATCH 2/4 mkimage: Add a 'keyfile' argument for image signing
Hi Alexandru,
Quoted 8 lines · Alexandru Gagniuc
It's not always desirable to use 'keydir' and some ad-hoc heuristics to get the filename of the signing key. More often, just passing the filename is the simpler, easier, and logical thing to do.
Since mkimage doesn't use long options, we're slowly running out of letters. I've chosen '-G' because it was available.
Quoted 9 lines
Signed-off-by Alexandru Gagniuc <[email protected]>--- include/image.h | 8 ++++--- tools/fit_image.c | 3 ++- tools/image-host.c | 58 +++++++++++++++++++++++++--------------------- tools/imagetool.h | 1 + tools/mkimage.c | 6 ++++- 5 files changed, 45 insertions(+), 31 deletions(-)
See below.
Quoted 19 lines
include/image.h +4
-3@@ -1128,9 +1128,10 @@ int fit_cipher_data(const char *keydir, void *keydest, void *fit,* 0, on success * libfdt error code, on failure */ -int fit_add_verification_data (const char *keydir,void *keydest,void *fit, -const char *comment,int require_keys, -const char *engine_id,const char *cmdname); +int fit_add_verification_data (const char *keydir,const char *keyfile, +void *keydest,void *fit,const char *comment, +int require_keys,const char *engine_id, +const char *cmdname);int fit_image_verify_with_data (const void *fit,int image_noffset,const void *data,size_t size);
include/image.h void image_set_host_blob(void *host_blob); · line 1240
Quoted 4 lines
include/image.h +1
-0@@ -1236,6 +1237,7 @@ void image_set_host_blob(void *host_blob);struct image_sign_info {const char *keydir;/* Directory conaining keys */ const char *keyname;/* Name of key to use */ +const char *keyfile;/* Filename of private or public key */
Please also document the semantics of this...can it be NULL? I think you intend that either keydir or keyfile is used but not both, right?
Regards,
Simon
PATCH 3/4 lib/rsa: Use the 'keyfile' argument from mkimage
In reply to Alexandru GagniucKeys can be derived from keydir, and the "key-name-hint" property of the FIT. They can also be specified ad-literam via 'keyfile'. Update the RSA signing path to use the appropriate one.
--- lib/rsa/rsa-sign.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-)
lib/rsa/rsa-sign.c +26 -8
@@ -210,14 +210,20 @@ static int rsa_get_pub_key(const char *keydir, const char *name,* @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) */ static int rsa_pem_get_priv_key (const char *keydir,const char *name, -RSA **rsap) +const char *keyfile,RSA **rsap) {char path[1024 ];RSA *rsa;FILE *f; *rsap =NULL ; -snprintf (path,sizeof (path),"%s/%s.key" , keydir, name); +if (keydir && name) +snprintf (path,sizeof (path),"%s/%s.key" , keydir, name); +else if (keyfile) +snprintf (path,sizeof (path),"%s" , keyfile); +else +return -EINVAL ; + f =fopen (path,"r" );if (!f) { fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n",
@@ -247,6 +253,7 @@ static int rsa_pem_get_priv_key(const char *keydir, const char *name,* @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) */ static int rsa_engine_get_priv_key (const char *keydir,const char *name, +const char *keyfile,ENGINE *engine,RSA **rsap) {const char *engine_id;
@@ -260,6 +267,10 @@ static int rsa_engine_get_priv_key(const char *keydir, const char *name, engine_id =ENGINE_get_id (engine);if (engine_id && !strcmp (engine_id,"pkcs11" )) { +if (!keydir && !name) { +fprintf (stderr,"Please use 'keydir' with PKCS11\n" ); +return -EINVAL ; + }if (keydir)if (strstr (keydir,"object=" )) snprintf(key_id, sizeof(key_id),
@@ -274,14 +285,19 @@ static int rsa_engine_get_priv_key(const char *keydir, const char *name,"pkcs11:object=%s;type=private" , name); }else if (engine_id) { -if (keydir) +if (keydir && name)snprintf (key_id,sizeof (key_id),"%s%s" , keydir, name); -else +else if (keydir)snprintf (key_id,sizeof (key_id),"%s" , name); +else if (keyfile) +snprintf (key_id,sizeof (key_id),"%s" , keyfile); +else +return -EINVAL ; + }else {fprintf (stderr,"Engine not supported\n" );return -ENOTSUP ;
@@ -319,11 +335,12 @@ err_rsa:* @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) */ static int rsa_get_priv_key (const char *keydir,const char *name, -ENGINE *engine,RSA **rsap) +const char *keyfile,ENGINE *engine,RSA **rsap) {if (engine) -return rsa_engine_get_priv_key (keydir, name, engine, rsap); -return rsa_pem_get_priv_key (keydir, name, rsap); +return rsa_engine_get_priv_key (keydir, name, keyfile, engine, + rsap); +return rsa_pem_get_priv_key (keydir, name, keyfile, rsap); }static int rsa_init (void )
@@ -534,7 +551,8 @@ int rsa_sign(struct image_sign_info *info, goto err_engine; } - ret =rsa_get_priv_key (info->keydir, info->keyname, e, &rsa); + ret =rsa_get_priv_key (info->keydir, info->keyname, info->keyfile, + e, &rsa);if (ret) goto err_priv; ret = rsa_sign_with_key(rsa, info->padding, info->checksum, region,
Signature · 1 line
2.26.2
PATCH 3/4 lib/rsa: Use the 'keyfile' argument from mkimage
Quoted 9 lines · Alexandru Gagniuc
Keys can be derived from keydir, and the "key-name-hint" property of the FIT. They can also be specified ad-literam via 'keyfile'. Update the RSA signing path to use the appropriate one.
Signed-off-by Alexandru Gagniuc <[email protected]>--- lib/rsa/rsa-sign.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-)
PATCH 4/4 lib/ecdsa: Use the 'keydir' argument from mkimage if appropriate
In reply to Alexandru GagniucKeys can be derived from keydir, and the "key-name-hint" property of the FIT. They can also be specified ad-literam via 'keyfile'. Update the ECDSA signing path to use the appropriate one.
--- lib/ecdsa/ecdsa-libcrypto.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
lib/ecdsa/ecdsa-libcrypto.c +13 -1
@@ -140,8 +140,20 @@ static int read_key(struct signer *ctx, const char *key_name)/* Prepare a 'signer' context that's ready to sign and verify. */ static int prepare_ctx (struct signer *ctx,const struct image_sign_info *info) { -const char *kname = info->keydir;int key_len_bytes, ret; +char kname[1024 ]; + +memset (ctx,0 ,sizeof (*ctx)); + +if (info->keyfile) { +snprintf (kname,sizeof (kname),"%s" , info->keyfile); + }else if (info->keydir && info->keyname) { +snprintf (kname,sizeof (kname),"%s/%s.pem" , info->keydir, + info->keyname); + }else { +fprintf (stderr,"keyfile, keyname, or key-name-hint missing\n" ); +return -EINVAL ; + } ret =alloc_ctx (ctx, info);if (ret)
Signature · 1 line
2.26.2
PATCH 4/4 lib/ecdsa: Use the 'keydir' argument from mkimage if appropriate
Quoted 9 lines · Alexandru Gagniuc
Keys can be derived from keydir, and the "key-name-hint" property of the FIT. They can also be specified ad-literam via 'keyfile'. Update the ECDSA signing path to use the appropriate one.
Signed-off-by Alexandru Gagniuc <[email protected]>--- lib/ecdsa/ecdsa-libcrypto.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)