aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2013-11-27 20:08:02 +0100
committerAndreas Schneider <asn@cryptomilk.org>2013-11-27 22:54:40 +0100
commitf1c56e4309fdaeef611322321205bfe801efd14e (patch)
tree8f6bc7a3c7aaa067e68222ff954d4ddeb4609211 /src
parent1fdc1025a87792c740fa8cdd8463801392fa918e (diff)
downloadlibssh-f1c56e4309fdaeef611322321205bfe801efd14e.tar.gz
libssh-f1c56e4309fdaeef611322321205bfe801efd14e.tar.xz
libssh-f1c56e4309fdaeef611322321205bfe801efd14e.zip
pki: Add ssh_pki_import_privkey_file().
Diffstat (limited to 'src')
-rw-r--r--src/pki.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/pki.c b/src/pki.c
index ec5a6883..28a27d83 100644
--- a/src/pki.c
+++ b/src/pki.c
@@ -476,6 +476,64 @@ int ssh_pki_import_privkey_file(const char *filename,
return SSH_OK;
}
+/**
+ * @brief Export a private key to a pam file on disk.
+ *
+ * @param[in] privkey The private key to export.
+ *
+ * @param[in] passphrase The passphrase to use to encrypt the key with or
+ * NULL. An empty string means no passphrase.
+ *
+ * @param[in] auth_fn An auth function you may want to use or NULL.
+ *
+ * @param[in] auth_data Private data passed to the auth function.
+ *
+ * @param[in] filename The path where to store the pem file.
+ *
+ * @return SSH_OK on success, SSH_ERROR on error.
+ */
+int ssh_pki_export_privkey_file(const ssh_key privkey,
+ const char *passphrase,
+ ssh_auth_callback auth_fn,
+ void *auth_data,
+ const char *filename)
+{
+ ssh_string blob;
+ FILE *fp;
+ int rc;
+
+ if (privkey == NULL || !ssh_key_is_private(privkey)) {
+ return SSH_ERROR;
+ }
+
+ fp = fopen(filename, "wb");
+ if (fp == NULL) {
+ SSH_LOG(SSH_LOG_FUNCTIONS, "Error opening %s: %s",
+ filename, strerror(errno));
+ return SSH_EOF;
+ }
+
+
+ blob = pki_private_key_to_pem(privkey,
+ passphrase,
+ auth_fn,
+ auth_data);
+ if (blob == NULL) {
+ fclose(fp);
+ return -1;
+ }
+
+ rc = fwrite(ssh_string_data(blob), ssh_string_len(blob), 1, fp);
+ if (rc != 1 || ferror(fp)) {
+ fclose(fp);
+ unlink(filename);
+ return SSH_ERROR;
+ }
+ fclose(fp);
+
+ return SSH_OK;
+}
+
/* temporary function to migrate seemlessly to ssh_key */
ssh_public_key ssh_pki_convert_key_to_publickey(const ssh_key key) {
ssh_public_key pub;