From 9dfef44fd88292ab19f46bdfa428e5378f5ae41f Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 16 Aug 2011 00:25:29 +0200 Subject: pki: Add ssh_pki_publickey_to_base64(). --- include/libssh/libssh.h | 3 +++ src/pki.c | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/unittests/torture_pki.c | 5 +++++ 3 files changed, 50 insertions(+) diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index cbe9ff1..de3480e 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -444,6 +444,9 @@ LIBSSH_API int ssh_pki_import_pubkey_file(ssh_session session, const char *filename, ssh_key *pkey); LIBSSH_API ssh_string ssh_pki_publickey_to_blob(const ssh_key key); +LIBSSH_API int ssh_pki_publickey_to_base64(const ssh_key key, + unsigned char **b64_key, + enum ssh_keytypes_e *ptype); LIBSSH_API int ssh_userauth_pki_pubkey(ssh_session session, const char *username, ssh_string publickey, ssh_key privatekey); diff --git a/src/pki.c b/src/pki.c index bafe7a4..7dc55b7 100644 --- a/src/pki.c +++ b/src/pki.c @@ -714,6 +714,48 @@ ssh_string ssh_pki_publickey_to_blob(const ssh_key key) return pki_publickey_to_string(key); } +/** + * @brief Convert a public key to a base64 hased key. + * + * @param[in] key The key to hash + * + * @param[out] b64_key A pointer to store the base64 hased key. + * + * @param[out] ptype The type of the key. + * + * @return SSH_OK on success, SSH_ERROR on error. + * + * @see ssh_string_free_char() + */ +int ssh_pki_publickey_to_base64(const ssh_key key, unsigned char **b64_key, + enum ssh_keytypes_e *ptype) +{ + enum ssh_keytypes_e type; + ssh_string key_str; + ssh_buffer key_buf; + char *type_c; + unsigned char *b64; + int rc; + + if (key == NULL || b64_key == NULL || ptype == NULL) { + return SSH_ERROR; + } + + key_str = pki_publickey_to_string(key); + if (key_str == NULL) { + return SSH_ERROR; + } + + + b64 = bin_to_base64(ssh_string_data(key_str), ssh_string_len(key_str)); + ssh_string_free(key_str); + + *ptype = type; + *b64_key = b64; + + return SSH_OK; +} + /* * This function signs the session id (known as H) as a string then * the content of sigbuf */ diff --git a/tests/unittests/torture_pki.c b/tests/unittests/torture_pki.c index 6425d6d..42082b1 100644 --- a/tests/unittests/torture_pki.c +++ b/tests/unittests/torture_pki.c @@ -306,6 +306,11 @@ static void torture_pki_publickey_rsa_base64(void **state) rc = ssh_pki_import_pubkey_base64(session, q, type, &key); assert_true(rc == 0); + rc = ssh_pki_publickey_to_base64(key, &b64_key, &type); + assert_true(rc == 0); + + assert_string_equal(q, b64_key); + free(key_buf); ssh_key_free(key); } -- cgit