ima-evm-utils: convert sign v2 from RSA to EVP_PKEY API
RFC PATCH 1/1 ima-evm-utils: convert sign v2 from RSA to EVP_PKEY API
Convert sign_v2 and related to using EVP_PKEY API instead of RSA API. This enables more signatures to work out of the box.
Only in single instance GOST NIDs are checked to produce correct keyid. Other than that code is quite generic.
Remove RSA_ASN1_templates[] as it does not needed anymore. OpenSSL sign is doing proper PKCS1 padding automatically (tested to be compatible with previous version, except for MD4). This also fixes bug with MD4 which produced wrong signature because of absence of the appropriate RSA_ASN1_template.
--- src/evmctl.c | 25 +++--- src/imaevm.h | 4 +- src/libimaevm.c | 271 +++++++++++++++++++++++++++----------------------------- 3 files changed, 146 insertions(+), 154 deletions(-)
src/evmctl.c +15 -10
@@ -891,7 +891,6 @@ static int cmd_import(struct command *cmd)int id, len, err =0 ;char name[20 ];uint8_t keyid[8 ]; -RSA *key; inkey = g_argv[optind++];if (!inkey) {
@@ -925,18 +924,26 @@ static int cmd_import(struct command *cmd) } } - key =read_pub_key (inkey, params.x509); -if (!key) -return 1 ; -if (params.x509) { +EVP_PKEY *pkey =read_pub_pkey (inkey, params.x509); + +if (!pkey) +return 1 ; pub =file2bin (inkey,NULL , &len); -if (!pub) - goto out; -calc_keyid_v2 ((uint32_t *)keyid, name, key); +if (!pub) { +EVP_PKEY_free (pkey); +return 1 ; + } +calc_keyid_v2 ((uint32_t *)keyid, name, pkey); +EVP_PKEY_free (pkey); }else { +RSA *key =read_pub_key (inkey, params.x509); + +if (!key) +return 1 ; len =key2bin (key, pub);calc_keyid_v1 (keyid, name, pub, len); +RSA_free (key); }log_info ("Importing public key %s from file %s into keyring %d\n" , name, inkey, id);
@@ -951,8 +958,6 @@ static int cmd_import(struct command *cmd) }if (params.x509)free (pub); -out: -RSA_free (key);return err; }
src/imaevm.h +2 -2
@@ -208,7 +208,6 @@ struct RSA_ASN1_template {#define NUM_PCRS 20#define DEFAULT_PCR 10 -extern const struct RSA_ASN1_template RSA_ASN1_templates[PKEY_HASH__LAST ];extern struct libevm_params params;void do_dump (FILE *fp,const void *ptr,int len,bool cr);
@@ -217,9 +216,10 @@ int get_filesize(const char *filename);int ima_calc_hash (const char *file,uint8_t *hash);int get_hash_algo (const char *algo);RSA *read_pub_key (const char *keyfile,int x509); +EVP_PKEY *read_pub_pkey (const char *keyfile,int x509);void calc_keyid_v1 (uint8_t *keyid,char *str,const unsigned char *pkey,int len); -void calc_keyid_v2 (uint32_t *keyid,char *str,RSA *key); +void calc_keyid_v2 (uint32_t *keyid,char *str,EVP_PKEY *key);int key2bin (RSA *key,unsigned char *pub);int sign_hash (const char *algo,const unsigned char *hash,int size,const char *keyfile,const char *keypass,unsigned char *sig);
src/libimaevm.c +129 -142
401 lines not on this page · raw message
Signature · 1 line
2.11.0
Quoted 5 lines · Vitaly Chikunov
Convert
sign_v2and related to usingEVP_PKEYAPI instead of RSA API. This enables more signatures to work out of the box.Only in single instance GOST NIDs are checked to produce correct keyid. Other than that code is quite generic.
There is was to generalize it a bit more.
Quoted 32 lines
Remove RSA_ASN1_templates[] as it does not needed anymore. OpenSSL sign is doing proper PKCS1 padding automatically (tested to be compatible with previous version, except for MD4). This also fixes bug with MD4 which produced wrong signature because of absence of the appropriate
RSA_ASN1_template.Signed-off-by Vitaly Chikunov <[email protected]>--- src/evmctl.c | 25 +++--- src/imaevm.h | 4 +- src/libimaevm.c | 271 +++++++++++++++++++++++++++----------------------------- 3 files changed, 146 insertions(+), 154 deletions(-)src/libimaevm.c +4
-3@@ -776,16 +724,32 @@ void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int lenlog_info ("keyid-v1: %s\n" , str); } -void calc_keyid_v2 (uint32_t *keyid,char *str,RSA *key) +void calc_keyid_v2 (uint32_t *keyid,char *str,EVP_PKEY *key) { +X509_PUBKEY *pk =NULL ;uint8_t sha1[SHA_DIGEST_LENGTH ]; -unsigned char *pkey =NULL ; +const unsigned char *pkey =NULL ; +unsigned char *pp =NULL ;int len; - len =i2d_RSAPublicKey (key, &pkey);
src/libimaevm.c void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int len · line 742
Quoted 10 lines
src/libimaevm.c +8
-2- -SHA1 (pkey, len, sha1); +switch (EVP_PKEY_id (key)) { +case NID_id_GostR3410_2012_256: +case NID_id_GostR3410_2012_512: +X509_PUBKEY_set (&pk, key); +X509_PUBKEY_get0_param (NULL , &pkey, &len,NULL , pk); +break ; +default : + len =i2d_PublicKey (key, &pp);
Because two calls to X509_PUBKEY_set and X509_PUBKEY_get0_param can handle more keys (including RSA), call to i2d_PublicKey could be avoided, so switch with Gost NIDs could be removed too. Tested.
Quoted 13 lines
src/libimaevm.c +9
-0+ pkey = pp; + } +if (len <=0 ) { +ERR_print_errors_fp (stderr); +/* Produce invalid key in case of error. */ + len =SHA_DIGEST_LENGTH ; +memset (sha1,0 , len); + }else +SHA1 (pkey, len, sha1);/* sha1[12 - 19] is exactly keyid from gpg file */ memcpy (keyid, sha1 +16 ,4 );log_debug ("keyid: " );
Hi,
I had problems with home computer I did not want to fix :)
What is original message?
Dmiry
Quoted 70 lines · Vitaly Chikunov
Quoted 5 lines · Vitaly Chikunov
Convert
sign_v2and related to usingEVP_PKEYAPI instead of RSA API. This enables more signatures to work out of the box.Only in single instance GOST NIDs are checked to produce correct keyid. Other than that code is quite generic.
There is was to generalize it a bit more.
Quoted 32 lines
Remove RSA_ASN1_templates[] as it does not needed anymore. OpenSSL sign is doing proper PKCS1 padding automatically (tested to be compatible with previous version, except for MD4). This also fixes bug with MD4 which produced wrong signature because of absence of the appropriate
RSA_ASN1_template.Signed-off-by Vitaly Chikunov <[email protected]>--- src/evmctl.c | 25 +++--- src/imaevm.h | 4 +- src/libimaevm.c | 271 +++++++++++++++++++++++++++----------------------------- 3 files changed, 146 insertions(+), 154 deletions(-)src/libimaevm.c +4
-3@@ -776,16 +724,32 @@ void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int lenlog_info ("keyid-v1: %s\n" , str); } -void calc_keyid_v2 (uint32_t *keyid,char *str,RSA *key) +void calc_keyid_v2 (uint32_t *keyid,char *str,EVP_PKEY *key) { +X509_PUBKEY *pk =NULL ;uint8_t sha1[SHA_DIGEST_LENGTH ]; -unsigned char *pkey =NULL ; +const unsigned char *pkey =NULL ; +unsigned char *pp =NULL ;int len; - len =i2d_RSAPublicKey (key, &pkey);src/libimaevm.c void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int len · line 742
Quoted 10 lines
src/libimaevm.c +8
-2- -SHA1 (pkey, len, sha1); +switch (EVP_PKEY_id (key)) { +case NID_id_GostR3410_2012_256: +case NID_id_GostR3410_2012_512: +X509_PUBKEY_set (&pk, key); +X509_PUBKEY_get0_param (NULL , &pkey, &len,NULL , pk); +break ; +default : + len =i2d_PublicKey (key, &pp);Because two calls to
X509_PUBKEY_setandX509_PUBKEY_get0_paramcan handle more keys (including RSA), call toi2d_PublicKeycould be avoided, so switch with Gost NIDs could be removed too. Tested.Quoted 13 lines
src/libimaevm.c +9
-0+ pkey = pp; + } +if (len <=0 ) { +ERR_print_errors_fp (stderr); +/* Produce invalid key in case of error. */ + len =SHA_DIGEST_LENGTH ; +memset (sha1,0 , len); + }else +SHA1 (pkey, len, sha1);/* sha1[12 - 19] is exactly keyid from gpg file */ memcpy (keyid, sha1 +16 ,4 );log_debug ("keyid: " );
Signature · 2 lines
Thanks, Dmitry
Quoted 41 lines · Vitaly Chikunov
Quoted 5 lines · Vitaly Chikunov
Convert
sign_v2and related to usingEVP_PKEYAPI instead of RSA API. This enables more signatures to work out of the box.Only in single instance GOST NIDs are checked to produce correct keyid. Other than that code is quite generic.
There is was to generalize it a bit more.
Quoted 31 lines
Remove RSA_ASN1_templates[] as it does not needed anymore. OpenSSL sign is doing proper PKCS1 padding automatically (tested to be compatible with previous version, except for MD4). This also fixes bug with MD4 which produced wrong signature because of absence of the appropriate
RSA_ASN1_template.Signed-off-by Vitaly Chikunov <[email protected]>--- src/evmctl.c | 25 +++--- src/imaevm.h | 4 +- src/libimaevm.c | 271 +++++++++++++++++++++++++++----------------------------- 3 files changed, 146 insertions(+), 154 deletions(-)src/libimaevm.c +4
-2@@ -776,16 +724,32 @@ void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int lenlog_info ("keyid-v1: %s\n" , str); } -void calc_keyid_v2 (uint32_t *keyid,char *str,RSA *key) +void calc_keyid_v2 (uint32_t *keyid,char *str,EVP_PKEY *key) { +X509_PUBKEY *pk =NULL ;uint8_t sha1[SHA_DIGEST_LENGTH ]; -unsigned char *pkey =NULL ; +const unsigned char *pkey =NULL ; +unsigned char *pp =NULL ;int len;
Quoted 29 lines
src/libimaevm.c void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int len · line 742
Quoted 11 lines
src/libimaevm.c +8
-3- len =i2d_RSAPublicKey (key, &pkey); - -SHA1 (pkey, len, sha1); +switch (EVP_PKEY_id (key)) { +case NID_id_GostR3410_2012_256: +case NID_id_GostR3410_2012_512: +X509_PUBKEY_set (&pk, key); +X509_PUBKEY_get0_param (NULL , &pkey, &len,NULL , pk); +break ; +default : + len =i2d_PublicKey (key, &pp);Because two calls to
X509_PUBKEY_setandX509_PUBKEY_get0_paramcan handle more keys (including RSA), call toi2d_PublicKeycould be avoided, so switch with Gost NIDs could be removed too. Tested.Quoted 13 lines
src/libimaevm.c +9
-0+ pkey = pp; + } +if (len <=0 ) { +ERR_print_errors_fp (stderr); +/* Produce invalid key in case of error. */ + len =SHA_DIGEST_LENGTH ; +memset (sha1,0 , len); + }else +SHA1 (pkey, len, sha1);/* sha1[12 - 19] is exactly keyid from gpg file */ memcpy (keyid, sha1 +16 ,4 );log_debug ("keyid: " );
I have tested EVM and IMA signature verification with new patch and it seems to be OK.
But when I try to sign with new patch, then signing fails...
$ sudo ./src/evmctl -k keys/privkey_ima.pem ima_sign car 139794956297792:error:0608C09B:digital envelope routines:EVP_PKEY_sign:buffer too small:../crypto/evp/pmeth_fn.c:65: evmctl: evmctl.c:601: sign_ima: Assertion `len < sizeof(sig)' failed. Aborted
sign_hash() returns -1
any ideas why?
Dmitry
Signature · 2 lines
Thanks, Dmitry
Quoted 85 lines · Dmitry Kasatkin
Quoted 41 lines · Vitaly Chikunov
Quoted 5 lines · Vitaly Chikunov
Convert
sign_v2and related to usingEVP_PKEYAPI instead of RSA API. This enables more signatures to work out of the box.Only in single instance GOST NIDs are checked to produce correct keyid. Other than that code is quite generic.
There is was to generalize it a bit more.
Quoted 31 lines
Remove RSA_ASN1_templates[] as it does not needed anymore. OpenSSL sign is doing proper PKCS1 padding automatically (tested to be compatible with previous version, except for MD4). This also fixes bug with MD4 which produced wrong signature because of absence of the appropriate
RSA_ASN1_template.Signed-off-by Vitaly Chikunov <[email protected]>--- src/evmctl.c | 25 +++--- src/imaevm.h | 4 +- src/libimaevm.c | 271 +++++++++++++++++++++++++++----------------------------- 3 files changed, 146 insertions(+), 154 deletions(-)src/libimaevm.c +4
-2@@ -776,16 +724,32 @@ void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int lenlog_info ("keyid-v1: %s\n" , str); } -void calc_keyid_v2 (uint32_t *keyid,char *str,RSA *key) +void calc_keyid_v2 (uint32_t *keyid,char *str,EVP_PKEY *key) { +X509_PUBKEY *pk =NULL ;uint8_t sha1[SHA_DIGEST_LENGTH ]; -unsigned char *pkey =NULL ; +const unsigned char *pkey =NULL ; +unsigned char *pp =NULL ;int len;Quoted 29 lines
src/libimaevm.c void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int len · line 742
Quoted 11 lines
src/libimaevm.c +8
-3- len =i2d_RSAPublicKey (key, &pkey); - -SHA1 (pkey, len, sha1); +switch (EVP_PKEY_id (key)) { +case NID_id_GostR3410_2012_256: +case NID_id_GostR3410_2012_512: +X509_PUBKEY_set (&pk, key); +X509_PUBKEY_get0_param (NULL , &pkey, &len,NULL , pk); +break ; +default : + len =i2d_PublicKey (key, &pp);Because two calls to
X509_PUBKEY_setandX509_PUBKEY_get0_paramcan handle more keys (including RSA), call toi2d_PublicKeycould be avoided, so switch with Gost NIDs could be removed too. Tested.Quoted 13 lines
src/libimaevm.c +9
-0+ pkey = pp; + } +if (len <=0 ) { +ERR_print_errors_fp (stderr); +/* Produce invalid key in case of error. */ + len =SHA_DIGEST_LENGTH ; +memset (sha1,0 , len); + }else +SHA1 (pkey, len, sha1);/* sha1[12 - 19] is exactly keyid from gpg file */ memcpy (keyid, sha1 +16 ,4 );log_debug ("keyid: " );I have tested EVM and IMA signature verification with new patch and it seems to be OK.
But when I try to sign with new patch, then signing fails...
$ sudo ./src/evmctl -k keys/privkey_ima.pem ima_sign car 139794956297792:error:0608C09B:digital envelope routines:EVP_PKEY_sign:buffer too small:../crypto/evp/pmeth_fn.c:65: evmctl: evmctl.c:601: sign_ima: Assertion `len < sizeof(sig)' failed. Aborted
Quoted 6 lines
sign_hash()returns -1any ideas why?
Dmitry
I actually found out...
int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen);
siglen must contain maxim size of the buffer.
In the patch, the value is uninitialized...
So please fix it.
Thanks,
Dmitry