mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-08-08 19:02:06 +03:00
pki: Make publickey_to_string a legacy function.
This commit is contained in:
233
src/keys.c
233
src/keys.c
@@ -264,239 +264,6 @@ error:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
|
||||||
static int dsa_public_to_string(gcry_sexp_t key, ssh_buffer buffer) {
|
|
||||||
#elif defined HAVE_LIBCRYPTO
|
|
||||||
static int dsa_public_to_string(DSA *key, ssh_buffer buffer) {
|
|
||||||
#endif
|
|
||||||
ssh_string p = NULL;
|
|
||||||
ssh_string q = NULL;
|
|
||||||
ssh_string g = NULL;
|
|
||||||
ssh_string n = NULL;
|
|
||||||
|
|
||||||
int rc = -1;
|
|
||||||
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
|
||||||
const char *tmp = NULL;
|
|
||||||
size_t size;
|
|
||||||
gcry_sexp_t sexp;
|
|
||||||
|
|
||||||
sexp = gcry_sexp_find_token(key, "p", 0);
|
|
||||||
if (sexp == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
tmp = gcry_sexp_nth_data(sexp, 1, &size);
|
|
||||||
p = ssh_string_new(size);
|
|
||||||
if (p == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
ssh_string_fill(p, (char *) tmp, size);
|
|
||||||
gcry_sexp_release(sexp);
|
|
||||||
|
|
||||||
sexp = gcry_sexp_find_token(key, "q", 0);
|
|
||||||
if (sexp == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
tmp = gcry_sexp_nth_data(sexp, 1, &size);
|
|
||||||
q = ssh_string_new(size);
|
|
||||||
if (q == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
ssh_string_fill(q, (char *) tmp, size);
|
|
||||||
gcry_sexp_release(sexp);
|
|
||||||
|
|
||||||
sexp = gcry_sexp_find_token(key, "g", 0);
|
|
||||||
if (sexp == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
tmp = gcry_sexp_nth_data(sexp, 1, &size);
|
|
||||||
g = ssh_string_new(size);
|
|
||||||
if (g == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
ssh_string_fill(g, (char *) tmp, size);
|
|
||||||
gcry_sexp_release(sexp);
|
|
||||||
|
|
||||||
sexp = gcry_sexp_find_token(key, "y", 0);
|
|
||||||
if (sexp == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
tmp = gcry_sexp_nth_data(sexp, 1, &size);
|
|
||||||
n = ssh_string_new(size);
|
|
||||||
if (n == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
ssh_string_fill(n, (char *) tmp, size);
|
|
||||||
|
|
||||||
#elif defined HAVE_LIBCRYPTO
|
|
||||||
p = make_bignum_string(key->p);
|
|
||||||
q = make_bignum_string(key->q);
|
|
||||||
g = make_bignum_string(key->g);
|
|
||||||
n = make_bignum_string(key->pub_key);
|
|
||||||
if (p == NULL || q == NULL || g == NULL || n == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
#endif /* HAVE_LIBCRYPTO */
|
|
||||||
if (buffer_add_ssh_string(buffer, p) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (buffer_add_ssh_string(buffer, q) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (buffer_add_ssh_string(buffer, g) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (buffer_add_ssh_string(buffer, n) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = 0;
|
|
||||||
error:
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
|
||||||
gcry_sexp_release(sexp);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ssh_string_burn(p);
|
|
||||||
ssh_string_free(p);
|
|
||||||
ssh_string_burn(q);
|
|
||||||
ssh_string_free(q);
|
|
||||||
ssh_string_burn(g);
|
|
||||||
ssh_string_free(g);
|
|
||||||
ssh_string_burn(n);
|
|
||||||
ssh_string_free(n);
|
|
||||||
|
|
||||||
return rc;
|
|
||||||
#if defined(HAVE_LIBGCRYPT) || defined(HAVE_LIBCRYPTO)
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
|
||||||
static int rsa_public_to_string(gcry_sexp_t key, ssh_buffer buffer) {
|
|
||||||
#elif defined HAVE_LIBCRYPTO
|
|
||||||
static int rsa_public_to_string(RSA *key, ssh_buffer buffer) {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ssh_string e = NULL;
|
|
||||||
ssh_string n = NULL;
|
|
||||||
|
|
||||||
int rc = -1;
|
|
||||||
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
|
||||||
const char *tmp;
|
|
||||||
size_t size;
|
|
||||||
gcry_sexp_t sexp;
|
|
||||||
|
|
||||||
sexp = gcry_sexp_find_token(key, "n", 0);
|
|
||||||
if (sexp == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
tmp = gcry_sexp_nth_data(sexp, 1, &size);
|
|
||||||
n = ssh_string_new(size);
|
|
||||||
if (n == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
ssh_string_fill(n, (char *) tmp, size);
|
|
||||||
gcry_sexp_release(sexp);
|
|
||||||
|
|
||||||
sexp = gcry_sexp_find_token(key, "e", 0);
|
|
||||||
if (sexp == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
tmp = gcry_sexp_nth_data(sexp, 1, &size);
|
|
||||||
e = ssh_string_new(size);
|
|
||||||
if (e == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
ssh_string_fill(e, (char *) tmp, size);
|
|
||||||
|
|
||||||
#elif defined HAVE_LIBCRYPTO
|
|
||||||
e = make_bignum_string(key->e);
|
|
||||||
n = make_bignum_string(key->n);
|
|
||||||
if (e == NULL || n == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (buffer_add_ssh_string(buffer, e) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
if (buffer_add_ssh_string(buffer, n) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = 0;
|
|
||||||
error:
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
|
||||||
gcry_sexp_release(sexp);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ssh_string_burn(e);
|
|
||||||
ssh_string_free(e);
|
|
||||||
ssh_string_burn(n);
|
|
||||||
ssh_string_free(n);
|
|
||||||
|
|
||||||
return rc;
|
|
||||||
#if defined(HAVE_LIBGCRYPT) || defined(HAVE_LIBCRYPTO)
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Convert a public_key object into a a SSH string.
|
|
||||||
*
|
|
||||||
* @param[in] key The public key to convert.
|
|
||||||
*
|
|
||||||
* @returns An allocated SSH String containing the public key, NULL
|
|
||||||
* on error.
|
|
||||||
*
|
|
||||||
* @see string_free()
|
|
||||||
*/
|
|
||||||
ssh_string publickey_to_string(ssh_public_key key) {
|
|
||||||
ssh_string type = NULL;
|
|
||||||
ssh_string ret = NULL;
|
|
||||||
ssh_buffer buf = NULL;
|
|
||||||
|
|
||||||
buf = ssh_buffer_new();
|
|
||||||
if (buf == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
type = ssh_string_from_char(key->type_c);
|
|
||||||
if (type == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buffer_add_ssh_string(buf, type) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (key->type) {
|
|
||||||
case SSH_KEYTYPE_DSS:
|
|
||||||
if (dsa_public_to_string(key->dsa_pub, buf) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SSH_KEYTYPE_RSA:
|
|
||||||
case SSH_KEYTYPE_RSA1:
|
|
||||||
if (rsa_public_to_string(key->rsa_pub, buf) < 0) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ssh_string_new(buffer_get_rest_len(buf));
|
|
||||||
if (ret == NULL) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
ssh_string_fill(ret, buffer_get_rest(buf), buffer_get_rest_len(buf));
|
|
||||||
error:
|
|
||||||
ssh_buffer_free(buf);
|
|
||||||
if(type != NULL)
|
|
||||||
ssh_string_free(type);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Signature decoding functions */
|
/* Signature decoding functions */
|
||||||
ssh_string signature_to_string(SIGNATURE *sign) {
|
ssh_string signature_to_string(SIGNATURE *sign) {
|
||||||
unsigned char buffer[40] = {0};
|
unsigned char buffer[40] = {0};
|
||||||
|
23
src/legacy.c
23
src/legacy.c
@@ -381,6 +381,29 @@ ssh_public_key publickey_from_string(ssh_session session, ssh_string pubkey_s) {
|
|||||||
return pubkey;
|
return pubkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ssh_string publickey_to_string(ssh_public_key pubkey) {
|
||||||
|
ssh_key key;
|
||||||
|
ssh_string key_blob;
|
||||||
|
|
||||||
|
key = ssh_key_new();
|
||||||
|
if (key == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
key->type = pubkey->type;
|
||||||
|
key->type_c = pubkey->type_c;
|
||||||
|
|
||||||
|
key->dsa = pubkey->dsa_pub;
|
||||||
|
key->rsa = pubkey->rsa_pub;
|
||||||
|
|
||||||
|
key_blob = ssh_pki_publickey_to_blob(key);
|
||||||
|
|
||||||
|
key->dsa = NULL;
|
||||||
|
key->rsa = NULL;
|
||||||
|
ssh_key_free(key);
|
||||||
|
|
||||||
|
return key_blob;
|
||||||
|
}
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* SERVER SUPPORT
|
* SERVER SUPPORT
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
Reference in New Issue
Block a user