aboutsummaryrefslogtreecommitdiff
path: root/src/pki.c
diff options
context:
space:
mode:
authorDavidWed <davidwe@posteo.de>2016-11-23 13:33:19 +0100
committerAndreas Schneider <asn@cryptomilk.org>2018-09-20 16:35:13 +0200
commitd0ce2d1ecdfae15c4ec6c6a16e6fb171d25960d4 (patch)
treecd72817fef55146daca451c969f63d83be42a321 /src/pki.c
parent5a198732a5cefac63fef3dc9d6a4dafa2ca6b87a (diff)
downloadlibssh-d0ce2d1ecdfae15c4ec6c6a16e6fb171d25960d4.tar.gz
libssh-d0ce2d1ecdfae15c4ec6c6a16e6fb171d25960d4.tar.xz
libssh-d0ce2d1ecdfae15c4ec6c6a16e6fb171d25960d4.zip
pki: Add ssh_pki_export_privkey_base64()
Fixes T53 Signed-off-by: DavidWedderwille <davidwe@posteo.de> Reviewed-by: Andreas Schneider <asn@samba.org>
Diffstat (limited to 'src/pki.c')
-rw-r--r--src/pki.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/pki.c b/src/pki.c
index 22ff3cd5..f463288a 100644
--- a/src/pki.c
+++ b/src/pki.c
@@ -596,6 +596,67 @@ int ssh_pki_import_privkey_base64(const char *b64_key,
return SSH_OK;
}
+ /**
+ * @brief Convert a private key to a pem base64 encoded key, or OpenSSH format for
+ * keytype ssh-ed25519
+ *
+ * @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[out] b64_key A pointer to store the allocated base64 encoded key. You
+ * need to free the buffer.
+ *
+ * @return SSH_OK on success, SSH_ERROR on error.
+ */
+int ssh_pki_export_privkey_base64(const ssh_key privkey,
+ const char *passphrase,
+ ssh_auth_callback auth_fn,
+ void *auth_data,
+ char **b64_key)
+{
+ ssh_string blob;
+ unsigned char *b64;
+
+ if (privkey == NULL || !ssh_key_is_private(privkey)) {
+ return SSH_ERROR;
+ }
+
+ if (privkey->type == SSH_KEYTYPE_ED25519){
+ blob = ssh_pki_openssh_privkey_export(privkey,
+ passphrase,
+ auth_fn,
+ auth_data);
+ } else {
+ blob = pki_private_key_to_pem(privkey,
+ passphrase,
+ auth_fn,
+ auth_data);
+ }
+ if (blob == NULL) {
+ return SSH_ERROR;
+ }
+
+
+ b64 = malloc(ssh_string_len(blob));
+ if(b64 == NULL){
+ ssh_string_free(blob);
+ return SSH_ERROR;
+ }
+
+ memcpy(b64,ssh_string_data(blob),ssh_string_len(blob));
+
+ ssh_string_free(blob);
+
+ *b64_key = (char *)b64;
+
+ return SSH_OK;
+}
/**
* @brief Import a key from a file.