summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/libssh/legacy.h3
-rw-r--r--include/libssh/libssh.h3
-rw-r--r--src/legacy.c70
-rw-r--r--src/pki_gcrypt.c78
4 files changed, 73 insertions, 81 deletions
diff --git a/include/libssh/legacy.h b/include/libssh/legacy.h
index db151f8..0cbcb1e 100644
--- a/include/libssh/legacy.h
+++ b/include/libssh/legacy.h
@@ -97,6 +97,9 @@ LIBSSH_API ssh_string publickey_from_file(ssh_session session, const char *filen
int *type);
LIBSSH_API ssh_public_key publickey_from_privatekey(ssh_private_key prv);
LIBSSH_API ssh_string publickey_to_string(ssh_public_key key);
+LIBSSH_API int ssh_try_publickey_from_file(ssh_session session, const char *keyfile,
+ ssh_string *publickey, int *type);
+
LIBSSH_API ssh_message ssh_message_retrieve(ssh_session session, uint32_t packettype);
LIBSSH_API void string_burn(ssh_string str);
LIBSSH_API ssh_string string_copy(ssh_string str);
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
index 1e5f835..868445f 100644
--- a/include/libssh/libssh.h
+++ b/include/libssh/libssh.h
@@ -364,9 +364,6 @@ LIBSSH_API void ssh_channel_set_blocking(ssh_channel channel, int blocking);
LIBSSH_API int ssh_channel_write(ssh_channel channel, const void *data, uint32_t len);
LIBSSH_API uint32_t ssh_channel_window_size(ssh_channel channel);
-LIBSSH_API int ssh_try_publickey_from_file(ssh_session session, const char *keyfile,
- ssh_string *publickey, int *type);
-
LIBSSH_API char *ssh_basename (const char *path);
LIBSSH_API void ssh_clean_pubkey_hash(unsigned char **hash);
LIBSSH_API int ssh_connect(ssh_session session);
diff --git a/src/legacy.c b/src/legacy.c
index 2789b1c..9550834 100644
--- a/src/legacy.c
+++ b/src/legacy.c
@@ -26,6 +26,7 @@
#include "config.h"
+#include <errno.h>
#include <stdio.h>
#include <libssh/priv.h>
@@ -34,6 +35,7 @@
#include <libssh/buffer.h>
#include <libssh/pki.h>
#include "libssh/pki_priv.h"
+#include <libssh/misc.h>
#include <libssh/keys.h>
#include <libssh/keyfiles.h>
@@ -502,6 +504,74 @@ ssh_string publickey_to_string(ssh_public_key pubkey) {
return key_blob;
}
+int ssh_publickey_to_file(ssh_session session,
+ const char *file,
+ ssh_string pubkey,
+ int type)
+{
+ FILE *fp;
+ char *user;
+ char buffer[1024];
+ char host[256];
+ unsigned char *pubkey_64;
+ size_t len;
+ int rc;
+ if(session==NULL)
+ return SSH_ERROR;
+ if(file==NULL || pubkey==NULL){
+ ssh_set_error(session, SSH_FATAL, "Invalid parameters");
+ return SSH_ERROR;
+ }
+ pubkey_64 = bin_to_base64(string_data(pubkey), ssh_string_len(pubkey));
+ if (pubkey_64 == NULL) {
+ return SSH_ERROR;
+ }
+
+ user = ssh_get_local_username();
+ if (user == NULL) {
+ SAFE_FREE(pubkey_64);
+ return SSH_ERROR;
+ }
+
+ rc = gethostname(host, sizeof(host));
+ if (rc < 0) {
+ SAFE_FREE(user);
+ SAFE_FREE(pubkey_64);
+ return SSH_ERROR;
+ }
+
+ snprintf(buffer, sizeof(buffer), "%s %s %s@%s\n",
+ ssh_type_to_char(type),
+ pubkey_64,
+ user,
+ host);
+
+ SAFE_FREE(pubkey_64);
+ SAFE_FREE(user);
+
+ ssh_log(session, SSH_LOG_RARE, "Trying to write public key file: %s", file);
+ ssh_log(session, SSH_LOG_PACKET, "public key file content: %s", buffer);
+
+ fp = fopen(file, "w+");
+ if (fp == NULL) {
+ ssh_set_error(session, SSH_REQUEST_DENIED,
+ "Error opening %s: %s", file, strerror(errno));
+ return SSH_ERROR;
+ }
+
+ len = strlen(buffer);
+ if (fwrite(buffer, len, 1, fp) != 1 || ferror(fp)) {
+ ssh_set_error(session, SSH_REQUEST_DENIED,
+ "Unable to write to %s", file);
+ fclose(fp);
+ unlink(file);
+ return SSH_ERROR;
+ }
+
+ fclose(fp);
+ return SSH_OK;
+}
+
/****************************************************************************
* SERVER SUPPORT
****************************************************************************/
diff --git a/src/pki_gcrypt.c b/src/pki_gcrypt.c
index 78d92cf..9e2bd44 100644
--- a/src/pki_gcrypt.c
+++ b/src/pki_gcrypt.c
@@ -1531,84 +1531,6 @@ ssh_signature pki_do_sign_sessionid(const ssh_key key,
*/
/**
- * @brief Write a public key to a file.
- *
- * @param[in] session The ssh session to use.
- *
- * @param[in] file The filename to write the key into.
- *
- * @param[in] pubkey The public key to write.
- *
- * @param[in] type The type of the public key.
- *
- * @return 0 on success, -1 on error.
- */
-int ssh_publickey_to_file(ssh_session session, const char *file,
- ssh_string pubkey, int type) {
- FILE *fp;
- char *user;
- char buffer[1024];
- char host[256];
- unsigned char *pubkey_64;
- size_t len;
- int rc;
- if(session==NULL)
- return SSH_ERROR;
- if(file==NULL || pubkey==NULL){
- ssh_set_error(session, SSH_FATAL, "Invalid parameters");
- return SSH_ERROR;
- }
- pubkey_64 = bin_to_base64(pubkey->string, ssh_string_len(pubkey));
- if (pubkey_64 == NULL) {
- return SSH_ERROR;
- }
-
- user = ssh_get_local_username();
- if (user == NULL) {
- SAFE_FREE(pubkey_64);
- return SSH_ERROR;
- }
-
- rc = gethostname(host, sizeof(host));
- if (rc < 0) {
- SAFE_FREE(user);
- SAFE_FREE(pubkey_64);
- return SSH_ERROR;
- }
-
- snprintf(buffer, sizeof(buffer), "%s %s %s@%s\n",
- ssh_type_to_char(type),
- pubkey_64,
- user,
- host);
-
- SAFE_FREE(pubkey_64);
- SAFE_FREE(user);
-
- ssh_log(session, SSH_LOG_RARE, "Trying to write public key file: %s", file);
- ssh_log(session, SSH_LOG_PACKET, "public key file content: %s", buffer);
-
- fp = fopen(file, "w+");
- if (fp == NULL) {
- ssh_set_error(session, SSH_REQUEST_DENIED,
- "Error opening %s: %s", file, strerror(errno));
- return SSH_ERROR;
- }
-
- len = strlen(buffer);
- if (fwrite(buffer, len, 1, fp) != 1 || ferror(fp)) {
- ssh_set_error(session, SSH_REQUEST_DENIED,
- "Unable to write to %s", file);
- fclose(fp);
- unlink(file);
- return SSH_ERROR;
- }
-
- fclose(fp);
- return SSH_OK;
-}
-
-/**
* @brief Try to read the public key from a given file.
*
* @param[in] session The ssh session to use.