pki_gcrypt: Rework 'pki_export_pubkey_rsa1'

* src/pki_gcrypt.c (pki_export_pubkey_rsa1): Rework to be more
idiomatic.  Fix leaking MPIs.

Signed-off-by: Justus Winter <justus@g10code.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Justus Winter
2016-03-29 13:07:02 +02:00
committed by Andreas Schneider
parent ed34425306
commit e518ec1cb7

View File

@@ -1189,34 +1189,17 @@ int pki_export_pubkey_rsa1(const ssh_key key,
char *rsa1, char *rsa1,
size_t rsa1_len) size_t rsa1_len)
{ {
gcry_sexp_t sexp; gpg_error_t err;
int rsa_size; int rsa_size;
bignum b; bignum E, N;
char *e, *n; char *e, *n;
sexp = gcry_sexp_find_token(key->rsa, "e", 0); err = gcry_sexp_extract_param(key->rsa, NULL, "en", &E, &N, NULL);
if (sexp == NULL) { if (err != 0) {
return SSH_ERROR; return SSH_ERROR;
} }
b = gcry_sexp_nth_mpi(sexp, 1, GCRYMPI_FMT_USG); e = bignum_bn2dec(E);
gcry_sexp_release(sexp); n = bignum_bn2dec(N);
if (b == NULL) {
return SSH_ERROR;
}
e = bignum_bn2dec(b);
sexp = gcry_sexp_find_token(key->rsa, "n", 0);
if (sexp == NULL) {
SAFE_FREE(e);
return SSH_ERROR;
}
b = gcry_sexp_nth_mpi(sexp, 1, GCRYMPI_FMT_USG);
gcry_sexp_release(sexp);
if (b == NULL) {
SAFE_FREE(e);
return SSH_ERROR;
}
n = bignum_bn2dec(b);
rsa_size = (gcry_pk_get_nbits(key->rsa) + 7) / 8; rsa_size = (gcry_pk_get_nbits(key->rsa) + 7) / 8;
@@ -1225,6 +1208,8 @@ int pki_export_pubkey_rsa1(const ssh_key key,
host, rsa_size << 3, e, n); host, rsa_size << 3, e, n);
SAFE_FREE(e); SAFE_FREE(e);
SAFE_FREE(n); SAFE_FREE(n);
bignum_free(E);
bignum_free(N);
return SSH_OK; return SSH_OK;
} }