lib: rsa: Extract public key from private key if keyfile argument is used
PATCH 1/1 lib: rsa: Extract public key from private key if keyfile argument is used
If the 'keyfile' (-G) argument is used, there is little value to require 'keydir' (-k) argument since the public key can also be extracted from the private key itself.
--- lib/rsa/rsa-sign.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)
lib/rsa/rsa-sign.c +3 -3
@@ -49,16 +49,16 @@ static int rsa_err(const char *msg) }/** -* rsa_pem_get_pub_key() - read a public key from a .crt file +* rsa_pem_get_pub_key() - read a public key from a private key file or .crt file * -* @keydir: Directory containins the key -* @name Name of key file (will have a .crt extension) +* @keydir: Directory containing the key, can be NULL +* @name Name of key file (will apply a .crt extension if keydir is not NULL) * @evpp Returns EVP_PKEY object, or NULL on failure * @return 0 if ok, -ve on error (in which case *evpp will be set to
NULL)
*/
static int rsa_pem_get_pub_key(const char *keydir, const char *name,EVP_PKEY **evpp)
{
- char path[1024];
+ char path[1024] = {0};
EVP_PKEY *key = NULL;
X509 *cert;
FILE *f;
@@ -68,7 +68,10 @@ static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **
return -EINVAL;
*evpp = NULL;
- snprintf(path, sizeof(path), "%s/%s.crt", keydir, name);
+ if (keydir && name)
+ snprintf(path, sizeof(path), "%s/%s.crt", keydir, name);
+ else if (name)
+ snprintf(path, sizeof(path), "%s", name);
f = fopen(path, "r");
if (!f) {
fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
@@ -76,7 +79,13 @@ static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **
return -EACCES;
}
- /* Read the certificate */
+ /* See if it contains a PEM private key? */
+ if (PEM_read_PrivateKey(f, evpp, NULL, path)) {
+ fclose(f);
+ return 0;
+ }
+
+ /* Not a PEM private key, read the certificate */
cert = NULL;
if (!PEM_read_X509(f, &cert, NULL, NULL)) {
rsa_err("Couldn't read certificate");
@@ -672,7 +681,12 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
if (ret)
return ret;
}
- ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey);
+ if (info->keydir && info->keyname)
+ ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey);
+ else if (info->keyfile)
+ ret = rsa_get_pub_key(NULL, info->keyfile, e, &pkey);
+ else
+ ret = -EINVAL;
if (ret)
goto err_get_pub_key;
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
Signature · 1 line
2.16.6
Quoted 80 lines · Donald
If the 'keyfile' (-G) argument is used, there is little value to require 'keydir' (-k) argument since the public key can also be extracted from the private key itself.
Signed-off-by Donald Chan <[email protected]>--- lib/rsa/rsa-sign.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)lib/rsa/rsa-sign.c +1
-1@@ -49,16 +49,16 @@ static int rsa_err(const char *msg) }/** -* rsa_pem_get_pub_key() - read a public key from a .crt file +* rsa_pem_get_pub_key() - read a public key from a private key file or .crt file
*
- * @keydir: Directory containins the key
- * @name Name of key file (will have a .crt extension)
+ * @keydir: Directory containing the key, can be NULL
+ * @name Name of key file (will apply a .crt extension if keydir is not
NULL)
* @evpp ReturnsEVP_PKEYobject, or NULL on failure
* @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) */
static int rsa_pem_get_pub_key(const char *keydir, const char *name,EVP_PKEY**evpp)
{lib/rsa/rsa-sign.c +1
-1-char path[1024 ]; +char path[1024 ] = {0 };EVP_PKEY *key =NULL ;X509 *cert;FILE *f;@@ -68,7 +68,10 @@ static int rsa_pem_get_pub_key(const char *keydir, constchar *name,
EVP_PKEY**lib/rsa/rsa-sign.c +4
-1return -EINVAL ; *evpp =NULL ; -snprintf (path,sizeof (path),"%s/%s.crt" , keydir, name); +if (keydir && name) +snprintf (path,sizeof (path),"%s/%s.crt" , keydir, name); +else if (name) +snprintf (path,sizeof (path),"%s" , name); f =fopen (path,"r" );if (!f) { fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",@@ -76,7 +79,13 @@ static int rsa_pem_get_pub_key(const char *keydir, constchar *name,
EVP_PKEY**lib/rsa/rsa-sign.c +13
-2return -EACCES ; } -/* Read the certificate */ +/* See if it contains a PEM private key? */ +if (PEM_read_PrivateKey (f, evpp,NULL , path)) { +fclose (f); +return 0 ; + } + +/* Not a PEM private key, read the certificate */ cert =NULL ;if (!PEM_read_X509 (f, &cert,NULL ,NULL )) {rsa_err ("Couldn't read certificate" );@@ -672,7 +681,12 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)if (ret)return ret; } - ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +if (info->keydir && info->keyname) + ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +else if (info->keyfile) + ret =rsa_get_pub_key (NULL , info->keyfile, e, &pkey); +else + ret = -EINVAL ;if (ret) goto err_get_pub_key;
Quoted 1 line
#if
OPENSSL_VERSION_NUMBER<0x10100000L|| \
This seems reasonable, but the formatting of the patch was destroyed somewhere along the way, can you please resend? Thanks.
Signature · 1 line
Tom
PATCH 1/1 lib: rsa: Extract public key from private key if keyfile argument is used
If the 'keyfile' (-G) argument is used, there is little value to require 'keydir' (-k) argument since the public key can also be extracted from the private key itself.
--- lib/rsa/rsa-sign.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)
lib/rsa/rsa-sign.c +21 -7
@@ -49,16 +49,16 @@ static int rsa_err(const char *msg) }/** -* rsa_pem_get_pub_key() - read a public key from a .crt file +* rsa_pem_get_pub_key() - read a public key from a private key file or .crt file * -* @keydir: Directory containins the key -* @name Name of key file (will have a .crt extension) +* @keydir: Directory containing the key, can be NULL +* @name Name of key file (will apply a .crt extension if keydir is not NULL) * @evpp Returns EVP_PKEY object, or NULL on failure * @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) */ static int rsa_pem_get_pub_key (const char *keydir,const char *name,EVP_PKEY **evpp) { -char path[1024 ]; +char path[1024 ] = {0 };EVP_PKEY *key =NULL ;X509 *cert;FILE *f;
@@ -68,7 +68,10 @@ static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **return -EINVAL ; *evpp =NULL ; -snprintf (path,sizeof (path),"%s/%s.crt" , keydir, name); +if (keydir && name) +snprintf (path,sizeof (path),"%s/%s.crt" , keydir, name); +else if (name) +snprintf (path,sizeof (path),"%s" , name); f =fopen (path,"r" );if (!f) { fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
@@ -76,7 +79,13 @@ static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **return -EACCES ; } -/* Read the certificate */ +/* See if it contains a PEM private key? */ +if (PEM_read_PrivateKey (f, evpp,NULL , path)) { +fclose (f); +return 0 ; + } + +/* Not a PEM private key, read the certificate */ cert =NULL ;if (!PEM_read_X509 (f, &cert,NULL ,NULL )) {rsa_err ("Couldn't read certificate" );
@@ -672,7 +681,12 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)if (ret)return ret; } - ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +if (info->keydir && info->keyname) + ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +else if (info->keyfile) + ret =rsa_get_pub_key (NULL , info->keyfile, e, &pkey); +else + ret = -EINVAL ;if (ret) goto err_get_pub_key; #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
Signature · 1 line
2.16.6
Quoted 8 lines · Donald
If the 'keyfile' (-G) argument is used, there is little value to require 'keydir' (-k) argument since the public key can also be extracted from the private key itself.
Signed-off-by Donald Chan <[email protected]>--- lib/rsa/rsa-sign.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)
This is again corrupted. I think you may need to get git send-email configured to work.
Signature · 1 line
Tom
PATCH 1/1 lib: rsa: Extract public key from private key if keyfile argument is used
If the 'keyfile' (-G) argument is used, there is little value to require 'keydir' (-k) argument since the public key can also be extracted from the private key itself.
--- lib/rsa/rsa-sign.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)
lib/rsa/rsa-sign.c +21 -7
@@ -49,16 +49,16 @@ static int rsa_err(const char *msg) }/** -* rsa_pem_get_pub_key() - read a public key from a .crt file +* rsa_pem_get_pub_key() - read a public key from a private key file or .crt file * -* @keydir: Directory containins the key -* @name Name of key file (will have a .crt extension) +* @keydir: Directory containing the key, can be NULL +* @name Name of key file (will apply a .crt extension if keydir is not NULL) * @evpp Returns EVP_PKEY object, or NULL on failure * @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) */ static int rsa_pem_get_pub_key (const char *keydir,const char *name,EVP_PKEY **evpp) { -char path[1024 ]; +char path[1024 ] = {0 };EVP_PKEY *key =NULL ;X509 *cert;FILE *f;
@@ -68,7 +68,10 @@ static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **return -EINVAL ; *evpp =NULL ; -snprintf (path,sizeof (path),"%s/%s.crt" , keydir, name); +if (keydir && name) +snprintf (path,sizeof (path),"%s/%s.crt" , keydir, name); +else if (name) +snprintf (path,sizeof (path),"%s" , name); f =fopen (path,"r" );if (!f) { fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",
@@ -76,7 +79,13 @@ static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **return -EACCES ; } -/* Read the certificate */ +/* See if it contains a PEM private key? */ +if (PEM_read_PrivateKey (f, evpp,NULL , path)) { +fclose (f); +return 0 ; + } + +/* Not a PEM private key, read the certificate */ cert =NULL ;if (!PEM_read_X509 (f, &cert,NULL ,NULL )) {rsa_err ("Couldn't read certificate" );
@@ -672,7 +681,12 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)if (ret)return ret; } - ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +if (info->keydir && info->keyname) + ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +else if (info->keyfile) + ret =rsa_get_pub_key (NULL , info->keyfile, e, &pkey); +else + ret = -EINVAL ;if (ret) goto err_get_pub_key; #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
Signature · 1 line
2.16.6
Hi Donald,
Quoted 77 lines · Donald Chan
If the 'keyfile' (-G) argument is used, there is little value to require 'keydir' (-k) argument since the public key can also be extracted from the private key itself.
Signed-off-by Donald Chan <[email protected]>--- lib/rsa/rsa-sign.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)lib/rsa/rsa-sign.c +21
-8@@ -49,16 +49,16 @@ static int rsa_err(const char *msg) }/** -* rsa_pem_get_pub_key() - read a public key from a .crt file +* rsa_pem_get_pub_key() - read a public key from a private key file or .crt file * -* @keydir: Directory containins the key -* @name Name of key file (will have a .crt extension) +* @keydir: Directory containing the key, can be NULL +* @name Name of key file (will apply a .crt extension if keydir is not NULL) * @evpp Returns EVP_PKEY object, or NULL on failure * @return 0 if ok, -ve on error (in which case *evpp will be set to NULL) */ static int rsa_pem_get_pub_key (const char *keydir,const char *name,EVP_PKEY **evpp) { -char path[1024 ]; +char path[1024 ] = {0 };EVP_PKEY *key =NULL ;X509 *cert;FILE *f;@@ -68,7 +68,10 @@ static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **return -EINVAL ; *evpp =NULL ; -snprintf (path,sizeof (path),"%s/%s.crt" , keydir, name); +if (keydir && name) +snprintf (path,sizeof (path),"%s/%s.crt" , keydir, name); +else if (name) +snprintf (path,sizeof (path),"%s" , name); f =fopen (path,"r" );if (!f) { fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n",@@ -76,7 +79,13 @@ static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **return -EACCES ; } -/* Read the certificate */ +/* See if it contains a PEM private key? */ +if (PEM_read_PrivateKey (f, evpp,NULL , path)) { +fclose (f); +return 0 ; + } + +/* Not a PEM private key, read the certificate */ cert =NULL ;if (!PEM_read_X509 (f, &cert,NULL ,NULL )) {rsa_err ("Couldn't read certificate" );@@ -672,7 +681,12 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)if (ret)return ret; } - ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +if (info->keydir && info->keyname) + ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +else if (info->keyfile) + ret =rsa_get_pub_key (NULL , info->keyfile, e, &pkey); +else + ret = -EINVAL ;if (ret) goto err_get_pub_key; #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ --
Quoted 2 lines
2.16.6
Can we work this into a test somehow? The normal test is test_vboot.py
- you could modify that or add a new test into that file perhaps?
Regards,
Simon
Hi Simon,
Quoted 91 lines · Simon Glass
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you can confirm the sender and know the content is safe.
Hi Donald,
Quoted 84 lines · Donald Chan
If the 'keyfile' (-G) argument is used, there is little value to require 'keydir' (-k) argument since the public key can also be extracted from the private key itself.
Signed-off-by Donald Chan <[email protected]>--- lib/rsa/rsa-sign.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-)lib/rsa/rsa-sign.c +1
-1@@ -49,16 +49,16 @@ static int rsa_err(const char *msg) }/** -* rsa_pem_get_pub_key() - read a public key from a .crt file +* rsa_pem_get_pub_key() - read a public key from a private key file or .crt file
*- * @keydir: Directory containins the key - * @name Name of key file (will have a .crt extension) + * @keydir: Directory containing the key, can be NULL + * @name Name of key file (will apply a .crt extension ifkeydir is not NULL)
* @evpp ReturnsEVP_PKEYobject, or NULL on failure
* @return 0 if ok, -ve on error (in which case *evpp will be set to
NULL)
*/
static int rsa_pem_get_pub_key(const char *keydir, const char *name,EVP_PKEY**evpp)
{lib/rsa/rsa-sign.c +1
-1-char path[1024 ]; +char path[1024 ] = {0 };EVP_PKEY *key =NULL ;X509 *cert;FILE *f;@@ -68,7 +68,10 @@ static int rsa_pem_get_pub_key(const char *keydir,const char *name,
EVP_PKEY**return -EINVAL; *evpp = NULL;lib/rsa/rsa-sign.c +2
-1-snprintf (path,sizeof (path),"%s/%s.crt" , keydir, name); +if (keydir && name) + snprintf(path, sizeof(path), "%s/%s.crt", keydir,name);
lib/rsa/rsa-sign.c +2
-0+else if (name) +snprintf (path,sizeof (path),"%s" , name); f =fopen (path,"r" );if (!f) { fprintf(stderr, "Couldn't open RSA certificate: '%s':%s\n",
lib/rsa/rsa-sign.c +0
-0@@ -76,7 +79,13 @@ static int rsa_pem_get_pub_key(const char *keydir,const char *name,
EVP_PKEY**return -EACCES; }lib/rsa/rsa-sign.c +13
-2-/* Read the certificate */ +/* See if it contains a PEM private key? */ +if (PEM_read_PrivateKey (f, evpp,NULL , path)) { +fclose (f); +return 0 ; + } + +/* Not a PEM private key, read the certificate */ cert =NULL ;if (!PEM_read_X509 (f, &cert,NULL ,NULL )) {rsa_err ("Couldn't read certificate" );@@ -672,7 +681,12 @@ int rsa_add_verify_data(struct image_sign_info *info, void *keydest)if (ret)return ret; } - ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +if (info->keydir && info->keyname) + ret =rsa_get_pub_key (info->keydir, info->keyname, e, &pkey); +else if (info->keyfile) + ret =rsa_get_pub_key (NULL , info->keyfile, e, &pkey); +else + ret = -EINVAL ;if (ret)
Quoted 9 lines
Quoted 5 lines
goto err_get_pub_key;#if
OPENSSL_VERSION_NUMBER<0x10100000L|| \
--
2.16.6Can we work this into a test somehow? The normal test is
test_vboot.py
- you could modify that or add a new test into that file perhaps?
Sure, let me work on this and submit a new patch.
Thanks
Donald
Quoted 5 lines · Donald Chan
If the 'keyfile' (-G) argument is used, there is little value to require 'keydir' (-k) argument since the public key can also be extracted from the private key itself.
Signed-off-by Donald Chan <[email protected]>
This breaks the "vboot" tests run under sandbox, please investigate and fix, thanks!
Signature · 1 line
Tom