aboutsummaryrefslogtreecommitdiff
path: root/src/pki.c
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:13 +0100
commit136efd6ed571bcc8c8ad18ac2bed203eb8a9d8bc (patch)
treeb43e7bc2b974a4c68feb19935e8d7a3423136556 /src/pki.c
parent94969cf26304aa1c002705574baabfa9e70d8b28 (diff)
downloadlibssh-136efd6ed571bcc8c8ad18ac2bed203eb8a9d8bc.tar.gz
libssh-136efd6ed571bcc8c8ad18ac2bed203eb8a9d8bc.tar.xz
libssh-136efd6ed571bcc8c8ad18ac2bed203eb8a9d8bc.zip
pki: Add ssh_pki_import_privkey_file().
Diffstat (limited to 'src/pki.c')
-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;