diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index 4051f73d..9b6b2fca 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -414,6 +414,8 @@ LIBSSH_API enum ssh_keytypes_e ssh_privatekey_type(ssh_private_key privatekey); LIBSSH_API ssh_key ssh_key_new(void); LIBSSH_API void ssh_key_free (ssh_key key); +LIBSSH_API int ssh_pki_import_privkey_base64(ssh_key key, ssh_session session, + const char *b64_key, const char *passphrase); LIBSSH_API void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len); LIBSSH_API int ssh_scp_accept_request(ssh_scp scp); LIBSSH_API int ssh_scp_close(ssh_scp scp); diff --git a/include/libssh/pki.h b/include/libssh/pki.h index 73f02ec3..1f593459 100644 --- a/include/libssh/pki.h +++ b/include/libssh/pki.h @@ -39,6 +39,9 @@ struct ssh_key_struct { #endif }; +#define ssh_key_is_public(k) (k && k->flags & SSH_KEY_FLAG_PUBLIC) +#define ssh_key_is_private(k) (k && k->flags & SSH_KEY_FLAG_PRIVATE) + void ssh_key_clean (ssh_key key); enum ssh_keytypes_e ssh_key_type(ssh_key key); int ssh_key_import_private(ssh_key key, ssh_session session, diff --git a/src/pki.c b/src/pki.c index aec7690c..6e1e4512 100644 --- a/src/pki.c +++ b/src/pki.c @@ -114,6 +114,31 @@ int ssh_key_import_private(ssh_key key, ssh_session session, const char *filenam return SSH_OK; } +int ssh_pki_import_privkey_base64(ssh_key key, ssh_session session, + const char *b64_key, const char *passphrase) { + ssh_private_key priv; + + if(b64_key == NULL || !*b64_key) { + return SSH_ERROR; + } + + priv = privatekey_from_base64(session, b64_key, 0, passphrase); + if(priv == NULL) { + return SSH_ERROR; + } + + ssh_key_clean(key); + + key->dsa = priv->dsa_priv; + key->rsa = priv->rsa_priv; + key->type = priv->type; + key->flags = SSH_KEY_FLAG_PRIVATE | SSH_KEY_FLAG_PUBLIC; + key->type_c = ssh_type_to_char(key->type); + + SAFE_FREE(priv); + return SSH_OK; +} + /** * @} */