1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-10 21:38:50 +01:00

gpg: Avoid a double free on error in the key generation.

* g10/keygen.c (card_store_key_with_backup): Avoid double free and
simplify error handling.
--

This is part of
GnuPG-bug-id: 7129
Co-authored-by: Jakub Jelen <jjelen@redhat.com>

(cherry picked from commit bcc002cd45)
This commit is contained in:
Werner Koch 2024-05-28 13:46:36 +02:00
parent f46d75f0b2
commit 234e9db3c3
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B

View File

@ -5478,11 +5478,10 @@ static gpg_error_t
card_store_key_with_backup (ctrl_t ctrl, PKT_public_key *sub_psk,
const char *backup_dir)
{
gpg_error_t err;
PKT_public_key *sk;
gnupg_isotime_t timestamp;
gpg_error_t err;
char *hexgrip;
int rc;
char *hexgrip = NULL;
struct agent_card_info_s info;
gcry_cipher_hd_t cipherhd = NULL;
char *cache_nonce = NULL;
@ -5490,9 +5489,14 @@ card_store_key_with_backup (ctrl_t ctrl, PKT_public_key *sub_psk,
size_t keklen;
char *ecdh_param_str = NULL;
memset (&info, 0, sizeof (info));
sk = copy_public_key (NULL, sub_psk);
if (!sk)
return gpg_error_from_syserror ();
{
err = gpg_error_from_syserror ();
goto leave;
}
epoch2isotime (timestamp, (time_t)sk->timestamp);
if (sk->pubkey_algo == PUBKEY_ALGO_ECDH)
@ -5500,37 +5504,23 @@ card_store_key_with_backup (ctrl_t ctrl, PKT_public_key *sub_psk,
ecdh_param_str = ecdh_param_str_from_pk (sk);
if (!ecdh_param_str)
{
free_public_key (sk);
return gpg_error_from_syserror ();
err = gpg_error_from_syserror ();
goto leave;
}
}
err = hexkeygrip_from_pk (sk, &hexgrip);
if (err)
{
xfree (ecdh_param_str);
free_public_key (sk);
goto leave;
}
goto leave;
memset(&info, 0, sizeof (info));
rc = agent_scd_getattr ("SERIALNO", &info);
if (rc)
{
xfree (ecdh_param_str);
free_public_key (sk);
err = (gpg_error_t)rc;
goto leave;
}
err = agent_scd_getattr ("SERIALNO", &info);
if (err)
goto leave;
rc = agent_keytocard (hexgrip, 2, 1, info.serialno,
timestamp, ecdh_param_str);
xfree (info.serialno);
if (rc)
{
err = (gpg_error_t)rc;
goto leave;
}
err = agent_keytocard (hexgrip, 2, 1, info.serialno,
timestamp, ecdh_param_str);
if (err)
goto leave;
err = agent_keywrap_key (ctrl, 1, &kek, &keklen);
if (err)
@ -5563,10 +5553,13 @@ card_store_key_with_backup (ctrl_t ctrl, PKT_public_key *sub_psk,
if (err)
log_error ("writing card key to backup file: %s\n", gpg_strerror (err));
else
/* Remove secret key data in agent side. */
agent_scd_learn (NULL, 1);
{
/* Remove secret key data in agent side. */
agent_scd_learn (NULL, 1);
}
leave:
xfree (info.serialno);
xfree (ecdh_param_str);
xfree (cache_nonce);
gcry_cipher_close (cipherhd);