[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 5/7] 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@xxxxxxxxxxx>
---
 src/pki_gcrypt.c | 31 ++++++++-----------------------
 1 file changed, 8 insertions(+), 23 deletions(-)

diff --git a/src/pki_gcrypt.c b/src/pki_gcrypt.c
index 1c94378..24d7d9b 100644
--- a/src/pki_gcrypt.c
+++ b/src/pki_gcrypt.c
@@ -1154,34 +1154,17 @@ int pki_export_pubkey_rsa1(const ssh_key key,
                            char *rsa1,
                            size_t rsa1_len)
 {
-    gcry_sexp_t sexp;
+    gpg_error_t err;
     int rsa_size;
-    bignum b;
+    bignum E, N;
     char *e, *n;
 
-    sexp = gcry_sexp_find_token(key->rsa, "e", 0);
-    if (sexp == NULL) {
-        return SSH_ERROR;
-    }
-    b = gcry_sexp_nth_mpi(sexp, 1, GCRYMPI_FMT_USG);
-    gcry_sexp_release(sexp);
-    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);
+    err = gcry_sexp_extract_param(key->rsa, NULL, "en", &E, &N, NULL);
+    if (err) {
         return SSH_ERROR;
     }
-    n = bignum_bn2dec(b);
+    e = bignum_bn2dec(E);
+    n = bignum_bn2dec(N);
 
     rsa_size = (gcry_pk_get_nbits(key->rsa) + 7) / 8;
 
@@ -1190,6 +1173,8 @@ int pki_export_pubkey_rsa1(const ssh_key key,
              host, rsa_size << 3, e, n);
     SAFE_FREE(e);
     SAFE_FREE(n);
+    bignum_free(E);
+    bignum_free(N);
 
     return SSH_OK;
 }
-- 
2.1.4


References:
[PATCH 1/5] options: Check if a port has been givenJustus Winter <justus@xxxxxxxxxxx>
[PATCH 1/7] options: Check if a port has been givenJustus Winter <justus@xxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org