pem_format(): Move private key cases to pki_evp

Support "exportType::PKCS8_encrypt"
This commit is contained in:
Christian Hohnstädt 2021-09-24 14:33:12 +02:00
parent ac506c83c2
commit 810b5509b1
4 changed files with 57 additions and 41 deletions

View File

@ -665,6 +665,61 @@ QSqlError pki_evp::deleteSqlData()
return q.lastError();
}
bool pki_evp::pem_format(BioByteArray &b, exportType::etype format)
{
EVP_PKEY *pkey;
int keytype;
switch (format) {
case exportType::PEM_private:
case exportType::SSH2_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;
#ifdef EVP_PKEY_ED25519
case EVP_PKEY_ED25519:
if (format == exportType::PEM_private)
return false;
write_SSH2_ed25519_private(b, pkey, NULL);
break;
#endif
#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);
break;
case exportType::PKCS8_encrypt:
pkey = decryptKey();
PEM_write_bio_PrivateKey(b, pkey, EVP_aes_256_cbc(),
passwd.constUchar(), passwd.size(),
NULL, NULL);
EVP_PKEY_free(pkey);
break;
default:
return pki_key::pem_format(b, format);
}
return true;
}
void pki_evp::writePKCS8(XFile &file, const EVP_CIPHER *enc,
pem_password_cb *cb, bool pem) const
{

View File

@ -57,6 +57,7 @@ class pki_evp: public pki_key
static QString removeTypeFromIntName(QString n);
void fromPEMbyteArray(const QByteArray &ba, const QString &name);
void fload(const QString &fname);
bool pem_format(BioByteArray &, exportType::etype format);
EVP_PKEY *load_ssh_ed25519_privatekey(const QByteArray &ba,
const pass_info &p);
void writeDefault(const QString &dirname) const;

View File

@ -125,51 +125,10 @@ void pki_key::write_SSH2_ed25519_private(BIO *b,
bool pki_key::pem(BioByteArray &b, int format)
{
EVP_PKEY *pkey;
QByteArray ba;
int keytype;
switch (format) {
case exportType::SSH2_public:
b += SSH2publicQByteArray();
break;
case exportType::PEM_private:
case exportType::SSH2_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;
#ifdef EVP_PKEY_ED25519
case EVP_PKEY_ED25519:
if (format == exportType::PEM_private)
return false;
write_SSH2_ed25519_private(b, pkey, NULL);
break;
#endif
#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);
break;
case exportType::PEM_key:
PEM_write_bio_PUBKEY(b, key);
break;

View File

@ -13,6 +13,7 @@
#include <openssl/pem.h>
#include <openssl/evp.h>
#include "pki_base.h"
#include "exportType.h"
#include "builtin_curves.h"
#define PEM_STRING_OPENSSH_KEY "OPENSSH PRIVATE KEY"