fix memory handling

This commit is contained in:
NIIBE Yutaka
2013-07-19 12:16:14 +09:00
parent 596a307219
commit 4db6994fa0
5 changed files with 35 additions and 18 deletions

View File

@@ -742,7 +742,7 @@ gpg_do_write_prvkey (enum kind_of_key kk, const uint8_t *key_data, int key_len,
const uint8_t *ks_pw1;
const uint8_t *ks_rc;
struct key_data_internal kdi;
int pubkey_allocated_here = 0;
uint8_t *pubkey_allocated_here = NULL;
uint8_t ks_pw1_len = 0;
uint8_t ks_rc_len = 0;
int pubkey_len = KEY_CONTENT_LEN;
@@ -775,20 +775,18 @@ gpg_do_write_prvkey (enum kind_of_key kk, const uint8_t *key_data, int key_len,
if (pubkey == NULL)
{
#ifdef RSA_AUTH
pubkey = modulus_calc (key_data, key_len);
pubkey_allocated_here = modulus_calc (key_data, key_len);
#else /* ECDSA for authentication */
if (kk == GPG_KEY_FOR_AUTHENTICATION)
pubkey = ecdsa_compute_public (key_data);
pubkey_allocated_here = ecdsa_compute_public (key_data);
else
pubkey = modulus_calc (key_data, key_len);
pubkey_allocated_here = modulus_calc (key_data, key_len);
#endif
if (pubkey == NULL)
if (pubkey_allocated_here == NULL)
{
free (pd);
return -1;
}
pubkey_allocated_here = 1;
}
DEBUG_INFO ("Getting keystore address...\r\n");
@@ -796,7 +794,10 @@ gpg_do_write_prvkey (enum kind_of_key kk, const uint8_t *key_data, int key_len,
if (key_addr == NULL)
{
if (pubkey_allocated_here)
free ((void *)pubkey);
{
memset (pubkey_allocated_here, 0, pubkey_len);
free (pubkey_allocated_here);
}
free (pd);
return -1;
}
@@ -827,13 +828,19 @@ gpg_do_write_prvkey (enum kind_of_key kk, const uint8_t *key_data, int key_len,
encrypt (dek, iv, (uint8_t *)&kdi, sizeof (struct key_data_internal));
r = flash_key_write (key_addr, (const uint8_t *)kdi.data, pubkey, pubkey_len);
r = flash_key_write (key_addr, (const uint8_t *)kdi.data,
pubkey_allocated_here? pubkey_allocated_here: pubkey,
pubkey_len);
if (pubkey_allocated_here)
free ((void *)pubkey);
{
memset (pubkey_allocated_here, 0, pubkey_len);
free (pubkey_allocated_here);
}
if (r < 0)
{
random_bytes_free (dek);
memset (pd, 0, sizeof (struct prvkey_data));
free (pd);
return r;
}
@@ -873,6 +880,7 @@ gpg_do_write_prvkey (enum kind_of_key kk, const uint8_t *key_data, int key_len,
do_ptr[nr - NR_DO__FIRST__] = p;
random_bytes_free (dek);
memset (pd, 0, sizeof (struct prvkey_data));
free (pd);
if (p == NULL)
return -1;
@@ -924,6 +932,7 @@ gpg_do_chks_prvkey (enum kind_of_key kk,
p = flash_do_write (nr, (const uint8_t *)pd, sizeof (struct prvkey_data));
do_ptr[nr - NR_DO__FIRST__] = p;
memset (pd, 0, sizeof (struct prvkey_data));
free (pd);
if (p == NULL)
return -1;
@@ -1633,7 +1642,7 @@ gpg_do_keygen (uint8_t kk_byte)
{
enum kind_of_key kk;
const uint8_t *keystring_admin;
const uint8_t *p_q_modulus;
uint8_t *p_q_modulus;
const uint8_t *p_q;
const uint8_t *modulus;
int r;
@@ -1665,7 +1674,8 @@ gpg_do_keygen (uint8_t kk_byte)
r = gpg_do_write_prvkey (kk, p_q, KEY_CONTENT_LEN,
keystring_admin, modulus);
free ((uint8_t *)p_q_modulus);
memset (p_q_modulus, 0, KEY_CONTENT_LEN*2);
free (p_q_modulus);
if (r < 0)
{
GPG_ERROR ();