From eaabb2a28dc809149588e2eb34af4995d8355722 Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Sat, 6 Jan 2018 21:18:31 +0100 Subject: [PATCH] SF Bug #110 Exported private key from 4096 bit SSH key is wrong Actually, it just differs. It is PKCS#8 instead of PKCS#1 --- lib/db_key.cpp | 4 +++- lib/pki_key.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/db_key.cpp b/lib/db_key.cpp index 1b607036..4ddce537 100644 --- a/lib/db_key.cpp +++ b/lib/db_key.cpp @@ -219,7 +219,9 @@ exportType::etype db_key::clipboardFormat(QModelIndexList indexes) "pub", tr("SSH2 public")); if (allPriv) types << exportType(exportType::PEM_private, "pem", - tr("PEM private")); + tr("PEM private")) + << exportType(exportType::PKCS8, "pk8", + "PKCS#8"); ExportDialog *dlg = new ExportDialog(mainwin, tr("Export keys to Clipboard"), QString(), NULL, diff --git a/lib/pki_key.cpp b/lib/pki_key.cpp index ca1ec027..b3229cdd 100644 --- a/lib/pki_key.cpp +++ b/lib/pki_key.cpp @@ -81,6 +81,8 @@ BIO *pki_key::pem(BIO *b, int format) { EVP_PKEY *pkey; QByteArray ba; + int keytype; + if (!b) b = BIO_new(BIO_s_mem()); @@ -90,6 +92,30 @@ BIO *pki_key::pem(BIO *b, int format) BIO_write(b, ba.data(), ba.size()); break; case exportType::PEM_private: + pkey = decryptKey(); + keytype = EVP_PKEY_id(pkey); + switch (keytype) { + case EVP_PKEY_RSA: + PEM_write_bio_RSAPrivateKey(b, + EVP_PKEY_get0_RSA(pkey), + NULL, NULL, 0, NULL, NULL); + break; + case EVP_PKEY_DSA: + PEM_write_bio_DSAPrivateKey(b, + EVP_PKEY_get0_DSA(pkey), + NULL, NULL, 0, NULL, NULL); + break; +#ifndef OPENSSL_NO_EC + case EVP_PKEY_EC: + PEM_write_bio_ECPrivateKey(b, + EVP_PKEY_get0_EC_KEY(pkey), + NULL, NULL, 0, NULL, NULL); + break; +#endif + } + EVP_PKEY_free(pkey); + break; + case exportType::PKCS8: pkey = decryptKey(); PEM_write_bio_PrivateKey(b, pkey, NULL, NULL, 0, NULL, NULL); EVP_PKEY_free(pkey);