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

[PATCH 4/7] pki_gcrypt: Rework 'pki_publickey_to_blob'


* src/pki_gcrypt.c (pki_publickey_to_blob): Rework using the new
helper 'ssh_sexp_extract_mpi'.

Signed-off-by: Justus Winter <justus@xxxxxxxxxxx>
---
 src/pki_gcrypt.c | 62 +++++++++++---------------------------------------------
 1 file changed, 12 insertions(+), 50 deletions(-)

diff --git a/src/pki_gcrypt.c b/src/pki_gcrypt.c
index ef4cf4a..1c94378 100644
--- a/src/pki_gcrypt.c
+++ b/src/pki_gcrypt.c
@@ -1001,9 +1001,6 @@ ssh_string pki_publickey_to_blob(const ssh_key key)
     ssh_string p = NULL;
     ssh_string g = NULL;
     ssh_string q = NULL;
-    const char *tmp = NULL;
-    size_t size;
-    gcry_sexp_t sexp;
     int rc;
 
     buffer = ssh_buffer_new();
@@ -1035,52 +1032,29 @@ ssh_string pki_publickey_to_blob(const ssh_key key)
 
     switch (key->type) {
         case SSH_KEYTYPE_DSS:
-            sexp = gcry_sexp_find_token(key->dsa, "p", 0);
-            if (sexp == NULL) {
-                goto fail;
-            }
-            tmp = gcry_sexp_nth_data(sexp, 1, &size);
-            p = ssh_string_new(size);
+            p = ssh_sexp_extract_mpi(key->dsa, "p", GCRYMPI_FMT_USG,
+                                     GCRYMPI_FMT_STD);
             if (p == NULL) {
                 goto fail;
             }
-            ssh_string_fill(p, (char *) tmp, size);
-            gcry_sexp_release(sexp);
 
-            sexp = gcry_sexp_find_token(key->dsa, "q", 0);
-            if (sexp == NULL) {
-                goto fail;
-            }
-            tmp = gcry_sexp_nth_data(sexp, 1, &size);
-            q = ssh_string_new(size);
+            q = ssh_sexp_extract_mpi(key->dsa, "q", GCRYMPI_FMT_USG,
+                                     GCRYMPI_FMT_STD);
             if (q == NULL) {
                 goto fail;
             }
-            ssh_string_fill(q, (char *) tmp, size);
-            gcry_sexp_release(sexp);
 
-            sexp = gcry_sexp_find_token(key->dsa, "g", 0);
-            if (sexp == NULL) {
-                goto fail;
-            }
-            tmp = gcry_sexp_nth_data(sexp, 1, &size);
-            g = ssh_string_new(size);
+            g = ssh_sexp_extract_mpi(key->dsa, "g", GCRYMPI_FMT_USG,
+                                     GCRYMPI_FMT_STD);
             if (g == NULL) {
                 goto fail;
             }
-            ssh_string_fill(g, (char *) tmp, size);
-            gcry_sexp_release(sexp);
 
-            sexp = gcry_sexp_find_token(key->dsa, "y", 0);
-            if (sexp == NULL) {
-                goto fail;
-            }
-            tmp = gcry_sexp_nth_data(sexp, 1, &size);
-            n = ssh_string_new(size);
+            n = ssh_sexp_extract_mpi(key->dsa, "y", GCRYMPI_FMT_USG,
+                                     GCRYMPI_FMT_STD);
             if (n == NULL) {
                 goto fail;
             }
-            ssh_string_fill(n, (char *) tmp, size);
 
             if (ssh_buffer_add_ssh_string(buffer, p) < 0) {
                 goto fail;
@@ -1107,29 +1081,17 @@ ssh_string pki_publickey_to_blob(const ssh_key key)
             break;
         case SSH_KEYTYPE_RSA:
         case SSH_KEYTYPE_RSA1:
-            sexp = gcry_sexp_find_token(key->rsa, "e", 0);
-            if (sexp == NULL) {
-                goto fail;
-            }
-            tmp = gcry_sexp_nth_data(sexp, 1, &size);
-            e = ssh_string_new(size);
+            e = ssh_sexp_extract_mpi(key->rsa, "e", GCRYMPI_FMT_USG,
+                                     GCRYMPI_FMT_STD);
             if (e == NULL) {
                 goto fail;
             }
-            ssh_string_fill(e, (char *) tmp, size);
-            gcry_sexp_release(sexp);
 
-            sexp = gcry_sexp_find_token(key->rsa, "n", 0);
-            if (sexp == NULL) {
-                goto fail;
-            }
-            tmp = gcry_sexp_nth_data(sexp, 1, &size);
-            n = ssh_string_new(size);
+            n = ssh_sexp_extract_mpi(key->rsa, "n", GCRYMPI_FMT_USG,
+                                     GCRYMPI_FMT_STD);
             if (n == NULL) {
                 goto fail;
             }
-            ssh_string_fill(n, (char *) tmp, size);
-            gcry_sexp_release(sexp);
 
             if (ssh_buffer_add_ssh_string(buffer, e) < 0) {
                 goto fail;
-- 
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