mirror of
git://git.gnupg.org/gnupg.git
synced 2025-01-03 12:11:33 +01:00
gpg: Fix memory leak in ECC encryption.
* g10/pkglue.c (pk_encrypt): Fix memory leak and streamline error handling.
This commit is contained in:
parent
a94674c54e
commit
98f65291d7
31
g10/pkglue.c
31
g10/pkglue.c
@ -190,7 +190,9 @@ int
|
|||||||
pk_encrypt (pubkey_algo_t algo, gcry_mpi_t *resarr, gcry_mpi_t data,
|
pk_encrypt (pubkey_algo_t algo, gcry_mpi_t *resarr, gcry_mpi_t data,
|
||||||
PKT_public_key *pk, gcry_mpi_t *pkey)
|
PKT_public_key *pk, gcry_mpi_t *pkey)
|
||||||
{
|
{
|
||||||
gcry_sexp_t s_ciph, s_data, s_pkey;
|
gcry_sexp_t s_ciph = NULL;
|
||||||
|
gcry_sexp_t s_data = NULL;
|
||||||
|
gcry_sexp_t s_pkey = NULL;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
/* Make a sexp from pkey. */
|
/* Make a sexp from pkey. */
|
||||||
@ -200,9 +202,8 @@ pk_encrypt (pubkey_algo_t algo, gcry_mpi_t *resarr, gcry_mpi_t data,
|
|||||||
"(public-key(elg(p%m)(g%m)(y%m)))",
|
"(public-key(elg(p%m)(g%m)(y%m)))",
|
||||||
pkey[0], pkey[1], pkey[2]);
|
pkey[0], pkey[1], pkey[2]);
|
||||||
/* Put DATA into a simplified S-expression. */
|
/* Put DATA into a simplified S-expression. */
|
||||||
if (rc || gcry_sexp_build (&s_data, NULL, "%m", data))
|
if (!rc)
|
||||||
BUG ();
|
rc = gcry_sexp_build (&s_data, NULL, "%m", data);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (algo == PUBKEY_ALGO_RSA || algo == PUBKEY_ALGO_RSA_E)
|
else if (algo == PUBKEY_ALGO_RSA || algo == PUBKEY_ALGO_RSA_E)
|
||||||
{
|
{
|
||||||
@ -210,17 +211,17 @@ pk_encrypt (pubkey_algo_t algo, gcry_mpi_t *resarr, gcry_mpi_t data,
|
|||||||
"(public-key(rsa(n%m)(e%m)))",
|
"(public-key(rsa(n%m)(e%m)))",
|
||||||
pkey[0], pkey[1]);
|
pkey[0], pkey[1]);
|
||||||
/* Put DATA into a simplified S-expression. */
|
/* Put DATA into a simplified S-expression. */
|
||||||
if (rc || gcry_sexp_build (&s_data, NULL, "%m", data))
|
if (!rc)
|
||||||
BUG ();
|
rc = gcry_sexp_build (&s_data, NULL, "%m", data);
|
||||||
}
|
}
|
||||||
else if (algo == PUBKEY_ALGO_ECDH)
|
else if (algo == PUBKEY_ALGO_ECDH)
|
||||||
{
|
{
|
||||||
gcry_mpi_t k;
|
gcry_mpi_t k;
|
||||||
char *curve;
|
|
||||||
|
|
||||||
rc = pk_ecdh_generate_ephemeral_key (pkey, &k);
|
rc = pk_ecdh_generate_ephemeral_key (pkey, &k);
|
||||||
if (rc)
|
if (!rc)
|
||||||
return rc;
|
{
|
||||||
|
char *curve;
|
||||||
|
|
||||||
curve = openpgp_oid_to_str (pkey[0]);
|
curve = openpgp_oid_to_str (pkey[0]);
|
||||||
if (!curve)
|
if (!curve)
|
||||||
@ -232,18 +233,20 @@ pk_encrypt (pubkey_algo_t algo, gcry_mpi_t *resarr, gcry_mpi_t data,
|
|||||||
"(public-key(ecdh(curve%s)(q%m)))",
|
"(public-key(ecdh(curve%s)(q%m)))",
|
||||||
curve, pkey[1]);
|
curve, pkey[1]);
|
||||||
xfree (curve);
|
xfree (curve);
|
||||||
/* FIXME: Take care of RC. */
|
|
||||||
/* Put K into a simplified S-expression. */
|
/* Put K into a simplified S-expression. */
|
||||||
if (rc || gcry_sexp_build (&s_data, NULL, "%m", k))
|
if (!rc)
|
||||||
BUG ();
|
rc = gcry_sexp_build (&s_data, NULL, "%m", k);
|
||||||
|
}
|
||||||
|
gcry_mpi_release (k);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return gpg_error (GPG_ERR_PUBKEY_ALGO);
|
rc = gpg_error (GPG_ERR_PUBKEY_ALGO);
|
||||||
|
|
||||||
|
|
||||||
/* Pass it to libgcrypt. */
|
/* Pass it to libgcrypt. */
|
||||||
|
if (!rc)
|
||||||
rc = gcry_pk_encrypt (&s_ciph, s_data, s_pkey);
|
rc = gcry_pk_encrypt (&s_ciph, s_data, s_pkey);
|
||||||
|
|
||||||
gcry_sexp_release (s_data);
|
gcry_sexp_release (s_data);
|
||||||
gcry_sexp_release (s_pkey);
|
gcry_sexp_release (s_pkey);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user