From 808c7a9be4f110605e76ea5678bf0b1986cc9b38 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 5 Sep 2011 11:02:23 +0200 Subject: pki: Add ssh_pki_export_pubkey_rsa1(). --- include/libssh/pki.h | 4 ++++ include/libssh/pki_priv.h | 4 ++++ src/pki.c | 8 ++++++++ src/pki_crypto.c | 29 +++++++++++++++++++++++++++++ src/pki_gcrypt.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+) diff --git a/include/libssh/pki.h b/include/libssh/pki.h index 04aa3b1f..d4cc8503 100644 --- a/include/libssh/pki.h +++ b/include/libssh/pki.h @@ -85,6 +85,10 @@ int ssh_pki_export_pubkey_blob(const ssh_key key, ssh_string *pblob); int ssh_pki_import_pubkey_blob(const ssh_string key_blob, ssh_key *pkey); +int ssh_pki_export_pubkey_rsa1(const ssh_key key, + const char *host, + char *rsa1, + size_t rsa1_len); /* SSH Signing Functions */ ssh_string ssh_pki_do_sign(ssh_session session, ssh_buffer sigbuf, diff --git a/include/libssh/pki_priv.h b/include/libssh/pki_priv.h index f90d7c86..d1ffa8a0 100644 --- a/include/libssh/pki_priv.h +++ b/include/libssh/pki_priv.h @@ -49,6 +49,10 @@ int pki_pubkey_build_rsa(ssh_key key, ssh_string e, ssh_string n); ssh_string pki_publickey_to_blob(const ssh_key key); +int pki_export_pubkey_rsa1(const ssh_key key, + const char *host, + char *rsa1, + size_t rsa1_len); /* SSH Signature Functions */ ssh_string pki_signature_to_blob(const ssh_signature sign); diff --git a/src/pki.c b/src/pki.c index 935f3680..3dd27ed3 100644 --- a/src/pki.c +++ b/src/pki.c @@ -958,6 +958,14 @@ int ssh_pki_export_pubkey_file(const ssh_key key, return SSH_OK; } +int ssh_pki_export_pubkey_rsa1(const ssh_key key, + const char *host, + char *rsa1, + size_t rsa1_len) +{ + return pki_export_pubkey_rsa1(key, host, rsa1, rsa1_len); +} + int ssh_pki_export_signature_blob(const ssh_signature sig, ssh_string *sig_blob) { diff --git a/src/pki_crypto.c b/src/pki_crypto.c index 29e589f1..32ee13d1 100644 --- a/src/pki_crypto.c +++ b/src/pki_crypto.c @@ -519,6 +519,35 @@ fail: return NULL; } +int pki_export_pubkey_rsa1(const ssh_key key, + const char *host, + char *rsa1, + size_t rsa1_len) +{ + char *e; + char *n; + int rsa_size = RSA_size(key->rsa); + + e = bignum_bn2dec(key->rsa->e); + if (e == NULL) { + return SSH_ERROR; + } + + n = bignum_bn2dec(key->rsa->n); + if (n == NULL) { + OPENSSL_free(e); + return SSH_ERROR; + } + + snprintf(rsa1, rsa1_len, + "%s %d %s %s\n", + host, rsa_size << 3, e, n); + OPENSSL_free(e); + OPENSSL_free(n); + + return SSH_OK; +} + /** * @internal * diff --git a/src/pki_gcrypt.c b/src/pki_gcrypt.c index cf770611..6f3cd957 100644 --- a/src/pki_gcrypt.c +++ b/src/pki_gcrypt.c @@ -1145,6 +1145,51 @@ fail: return NULL; } +int pki_export_pubkey_rsa1(const ssh_key key, + const char *host, + char *rsa1, + size_t rsa1_len) +{ + gcry_sexp_t sexp; + int rsa_size; + bignum b; + 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); + return SSH_ERROR; + } + n = bignum_bn2dec(b); + + rsa_size = (gcry_pk_get_nbits(key->rsa) + 7) / 8; + + snprintf(rsa1, rsa1_len, + "%s %d %s %s\n", + host, rsa_size << 3, e, n); + SAFE_FREE(e); + SAFE_FREE(n); + + return SSH_OK; +} + ssh_string pki_signature_to_blob(const ssh_signature sig) { char buffer[40] = {0}; -- cgit v1.2.3