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@g10code.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Justus Winter
2016-03-29 13:07:01 +02:00
committed by Andreas Schneider
parent 1d7f87fc0e
commit ed34425306

View File

@@ -1018,9 +1018,6 @@ ssh_string pki_publickey_to_blob(const ssh_key key)
ssh_string p = NULL; ssh_string p = NULL;
ssh_string g = NULL; ssh_string g = NULL;
ssh_string q = NULL; ssh_string q = NULL;
const char *tmp = NULL;
size_t size;
gcry_sexp_t sexp;
int rc; int rc;
buffer = ssh_buffer_new(); buffer = ssh_buffer_new();
@@ -1052,63 +1049,52 @@ ssh_string pki_publickey_to_blob(const ssh_key key)
switch (key->type) { switch (key->type) {
case SSH_KEYTYPE_DSS: case SSH_KEYTYPE_DSS:
sexp = gcry_sexp_find_token(key->dsa, "p", 0); p = ssh_sexp_extract_mpi(key->dsa,
if (sexp == NULL) { "p",
goto fail; GCRYMPI_FMT_USG,
} GCRYMPI_FMT_STD);
tmp = gcry_sexp_nth_data(sexp, 1, &size);
p = ssh_string_new(size);
if (p == NULL) { if (p == NULL) {
goto fail; goto fail;
} }
ssh_string_fill(p, (char *) tmp, size);
gcry_sexp_release(sexp);
sexp = gcry_sexp_find_token(key->dsa, "q", 0); q = ssh_sexp_extract_mpi(key->dsa,
if (sexp == NULL) { "q",
goto fail; GCRYMPI_FMT_USG,
} GCRYMPI_FMT_STD);
tmp = gcry_sexp_nth_data(sexp, 1, &size);
q = ssh_string_new(size);
if (q == NULL) { if (q == NULL) {
goto fail; goto fail;
} }
ssh_string_fill(q, (char *) tmp, size);
gcry_sexp_release(sexp);
sexp = gcry_sexp_find_token(key->dsa, "g", 0); g = ssh_sexp_extract_mpi(key->dsa,
if (sexp == NULL) { "g",
goto fail; GCRYMPI_FMT_USG,
} GCRYMPI_FMT_STD);
tmp = gcry_sexp_nth_data(sexp, 1, &size);
g = ssh_string_new(size);
if (g == NULL) { if (g == NULL) {
goto fail; goto fail;
} }
ssh_string_fill(g, (char *) tmp, size);
gcry_sexp_release(sexp);
sexp = gcry_sexp_find_token(key->dsa, "y", 0); n = ssh_sexp_extract_mpi(key->dsa,
if (sexp == NULL) { "y",
goto fail; GCRYMPI_FMT_USG,
} GCRYMPI_FMT_STD);
tmp = gcry_sexp_nth_data(sexp, 1, &size);
n = ssh_string_new(size);
if (n == NULL) { if (n == NULL) {
goto fail; goto fail;
} }
ssh_string_fill(n, (char *) tmp, size);
if (ssh_buffer_add_ssh_string(buffer, p) < 0) { rc = ssh_buffer_add_ssh_string(buffer, p);
if (rc < 0) {
goto fail; goto fail;
} }
if (ssh_buffer_add_ssh_string(buffer, q) < 0) { rc = ssh_buffer_add_ssh_string(buffer, q);
if (rc < 0) {
goto fail; goto fail;
} }
if (ssh_buffer_add_ssh_string(buffer, g) < 0) { rc = ssh_buffer_add_ssh_string(buffer, g);
if (rc < 0) {
goto fail; goto fail;
} }
if (ssh_buffer_add_ssh_string(buffer, n) < 0) { rc = ssh_buffer_add_ssh_string(buffer, n);
if (rc < 0) {
goto fail; goto fail;
} }
@@ -1124,34 +1110,28 @@ ssh_string pki_publickey_to_blob(const ssh_key key)
break; break;
case SSH_KEYTYPE_RSA: case SSH_KEYTYPE_RSA:
case SSH_KEYTYPE_RSA1: case SSH_KEYTYPE_RSA1:
sexp = gcry_sexp_find_token(key->rsa, "e", 0); e = ssh_sexp_extract_mpi(key->rsa,
if (sexp == NULL) { "e",
goto fail; GCRYMPI_FMT_USG,
} GCRYMPI_FMT_STD);
tmp = gcry_sexp_nth_data(sexp, 1, &size);
e = ssh_string_new(size);
if (e == NULL) { if (e == NULL) {
goto fail; goto fail;
} }
ssh_string_fill(e, (char *) tmp, size);
gcry_sexp_release(sexp);
sexp = gcry_sexp_find_token(key->rsa, "n", 0); n = ssh_sexp_extract_mpi(key->rsa,
if (sexp == NULL) { "n",
goto fail; GCRYMPI_FMT_USG,
} GCRYMPI_FMT_STD);
tmp = gcry_sexp_nth_data(sexp, 1, &size);
n = ssh_string_new(size);
if (n == NULL) { if (n == NULL) {
goto fail; goto fail;
} }
ssh_string_fill(n, (char *) tmp, size);
gcry_sexp_release(sexp);
if (ssh_buffer_add_ssh_string(buffer, e) < 0) { rc = ssh_buffer_add_ssh_string(buffer, e);
if (rc < 0) {
goto fail; goto fail;
} }
if (ssh_buffer_add_ssh_string(buffer, n) < 0) { rc = ssh_buffer_add_ssh_string(buffer, n);
if (rc < 0) {
goto fail; goto fail;
} }