From f2ca2d50b5ef5559e66ed6cced71eaadec8ef866 Mon Sep 17 00:00:00 2001 From: Vic Lee Date: Wed, 30 Dec 2009 10:40:34 +0800 Subject: [PATCH] In ssh_userauth_pubkey, if publickey argument is NULL, generate it from privatekey. Signed-off-by: Vic Lee Signed-off-by: Andreas Schneider --- libssh/auth.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/libssh/auth.c b/libssh/auth.c index 48bd6ea2..1d94135a 100644 --- a/libssh/auth.c +++ b/libssh/auth.c @@ -467,7 +467,8 @@ error: * ssh_option_set_username() has been used. You cannot try * two different logins in a row. * - * @param publickey A public key returned by publickey_from_file(). + * @param publickey A public key returned by publickey_from_file(), or NULL + * to generate automatically from privatekey. * * @param privatekey A private key returned by privatekey_from_file(). * @@ -489,6 +490,8 @@ int ssh_userauth_pubkey(ssh_session session, const char *username, ssh_string method = NULL; ssh_string algo = NULL; ssh_string sign = NULL; + ssh_public_key pk = NULL; + ssh_string pkstr = NULL; int rc = SSH_AUTH_ERROR; enter_function(); @@ -534,6 +537,17 @@ int ssh_userauth_pubkey(ssh_session session, const char *username, if (algo == NULL) { goto error; } + if (publickey == NULL) { + pk = publickey_from_privatekey(privatekey); + if (pk == NULL) { + goto error; + } + pkstr = publickey_to_string(pk); + publickey_free(pk); + if (pkstr == NULL) { + goto error; + } + } /* we said previously the public key was accepted */ if (buffer_add_u8(session->out_buffer, SSH2_MSG_USERAUTH_REQUEST) < 0 || @@ -542,7 +556,7 @@ int ssh_userauth_pubkey(ssh_session session, const char *username, buffer_add_ssh_string(session->out_buffer, method) < 0 || buffer_add_u8(session->out_buffer, 1) < 0 || buffer_add_ssh_string(session->out_buffer, algo) < 0 || - buffer_add_ssh_string(session->out_buffer, publickey) < 0) { + buffer_add_ssh_string(session->out_buffer, (publickey == NULL ? pkstr : publickey)) < 0) { goto error; } @@ -550,6 +564,7 @@ int ssh_userauth_pubkey(ssh_session session, const char *username, string_free(service); string_free(method); string_free(algo); + string_free(pkstr); sign = ssh_do_sign(session,session->out_buffer, privatekey); if (sign) { @@ -573,6 +588,7 @@ error: string_free(service); string_free(method); string_free(algo); + string_free(pkstr); leave_function(); return rc;