mirror of
https://github.com/chris2511/xca.git
synced 2026-09-12 19:51:05 +05:00
Encapsulate all BIOs in the BioByteArray class
If we have a QByteArray (ba) and must provide it to a BIO* expecting OpenSSL function, the following construct provides it: BioByteArray(ba).ro() directly providing the QByteArray buffer as BIO It also supports mixed writes: BIO_write(bba, buf, size) bba += QByteArray
This commit is contained in:
parent
4d95912d51
commit
ea453d4336
119
lib/BioByteArray.cpp
Normal file
119
lib/BioByteArray.cpp
Normal file
@ -0,0 +1,119 @@
|
||||
/* vi: set sw=4 ts=4:
|
||||
*
|
||||
* Copyright (C) 2020 Christian Hohnstaedt.
|
||||
*
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
#include "BioByteArray.h"
|
||||
#include <QDebug>
|
||||
|
||||
void BioByteArray::set(const QByteArray &qba)
|
||||
{
|
||||
if (read_write) {
|
||||
char buf[1024];
|
||||
qWarning() << "BioByteArray already in use";
|
||||
while (BIO_read(read_write, buf, sizeof buf) > 0)
|
||||
;
|
||||
memset(buf, 0, sizeof buf);
|
||||
}
|
||||
add(qba);
|
||||
}
|
||||
|
||||
void BioByteArray::add(const QByteArray &qba)
|
||||
{
|
||||
if (read_only) {
|
||||
qWarning() << "BioByteArray is read-only";
|
||||
return;
|
||||
}
|
||||
if (read_write)
|
||||
biowrite(qba);
|
||||
else
|
||||
store += qba;
|
||||
}
|
||||
|
||||
void BioByteArray::biowrite(const QByteArray &qba)
|
||||
{
|
||||
BIO_write(read_write, qba.data(), qba.size());
|
||||
}
|
||||
|
||||
void BioByteArray::cleanse_and_free(BIO *bio)
|
||||
{
|
||||
if (!bio)
|
||||
return;
|
||||
char *p;
|
||||
long l = BIO_get_mem_data(bio, &p);
|
||||
OPENSSL_cleanse(p, l);
|
||||
BIO_free(bio);
|
||||
}
|
||||
|
||||
BioByteArray::~BioByteArray()
|
||||
{
|
||||
store.fill(0);
|
||||
store.clear();
|
||||
cleanse_and_free(read_write);
|
||||
cleanse_and_free(read_only);
|
||||
}
|
||||
|
||||
BIO *BioByteArray::bio()
|
||||
{
|
||||
if (!read_write) {
|
||||
read_write = BIO_new(BIO_s_mem());
|
||||
biowrite(store);
|
||||
store.fill(0);
|
||||
store.clear();
|
||||
}
|
||||
return read_write;
|
||||
}
|
||||
|
||||
BIO *BioByteArray::ro()
|
||||
{
|
||||
if (!read_only)
|
||||
read_only = BIO_new_mem_buf(
|
||||
(void*)store.constData(), store.length());
|
||||
return read_only;
|
||||
}
|
||||
|
||||
QByteArray BioByteArray::byteArray() const
|
||||
{
|
||||
if (read_only || !read_write)
|
||||
return store;
|
||||
/* "read_write" Bio may differ from "store" */
|
||||
const char *p;
|
||||
int l = BIO_get_mem_data(read_write, &p);
|
||||
return QByteArray(p, l);
|
||||
}
|
||||
|
||||
BioByteArray::operator BIO*()
|
||||
{
|
||||
return bio();
|
||||
}
|
||||
|
||||
BioByteArray::operator QByteArray()
|
||||
{
|
||||
return byteArray();
|
||||
}
|
||||
|
||||
BioByteArray &BioByteArray::operator = (const BioByteArray &other)
|
||||
{
|
||||
set(other.byteArray());
|
||||
return *this;
|
||||
}
|
||||
|
||||
BioByteArray &BioByteArray::operator = (const QByteArray &qba)
|
||||
{
|
||||
set(qba);
|
||||
return *this;
|
||||
}
|
||||
|
||||
BioByteArray &BioByteArray::operator += (const BioByteArray &other)
|
||||
{
|
||||
add(other.byteArray());
|
||||
return *this;
|
||||
}
|
||||
|
||||
BioByteArray &BioByteArray::operator += (const QByteArray &qba)
|
||||
{
|
||||
add(qba);
|
||||
return *this;
|
||||
}
|
||||
44
lib/BioByteArray.h
Normal file
44
lib/BioByteArray.h
Normal file
@ -0,0 +1,44 @@
|
||||
/* vi: set sw=4 ts=4:
|
||||
*
|
||||
* Copyright (C) 2020 Christian Hohnstaedt.
|
||||
*
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
#ifndef __BIOBYTEARRAY_H
|
||||
#define __BIOBYTEARRAY_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <openssl/bio.h>
|
||||
|
||||
class BioByteArray
|
||||
{
|
||||
protected:
|
||||
BIO *read_write;
|
||||
BIO *read_only;
|
||||
QByteArray store;
|
||||
|
||||
void set(const QByteArray &qba);
|
||||
void add(const QByteArray &qba);
|
||||
void biowrite(const QByteArray &qba);
|
||||
void cleanse_and_free(BIO *bio);
|
||||
|
||||
public:
|
||||
BioByteArray(const QByteArray &qba) :
|
||||
read_write(NULL), read_only(NULL), store(qba) { }
|
||||
BioByteArray(const BioByteArray &bba) :
|
||||
read_write(NULL), read_only(NULL), store(bba.byteArray()) { }
|
||||
BioByteArray() :
|
||||
read_write(NULL), read_only(NULL), store() { }
|
||||
~BioByteArray();
|
||||
BIO *bio();
|
||||
BIO *ro();
|
||||
QByteArray byteArray() const;
|
||||
operator BIO*();
|
||||
operator QByteArray();
|
||||
BioByteArray &operator = (const BioByteArray &other);
|
||||
BioByteArray &operator = (const QByteArray &qba);
|
||||
BioByteArray &operator += (const BioByteArray &other);
|
||||
BioByteArray &operator += (const QByteArray &qba);
|
||||
};
|
||||
#endif
|
||||
@ -11,7 +11,7 @@ MOCNAMES=db_crl db_key db_temp db_x509 db_x509req db_x509super db_base db_token\
|
||||
NAMES=$(MOCNAMES) asn1int oid x509rev asn1time version \
|
||||
x509v3ext func load_obj x509name db settings \
|
||||
pk11_attribute pkcs11 pkcs11_lib Passwd builtin_curves entropy sql \
|
||||
dbhistory main arguments
|
||||
dbhistory main arguments BioByteArray
|
||||
|
||||
OBJS=$(patsubst %, %.o, $(NAMES)) $(patsubst %, moc_%.o, $(MOCNAMES))
|
||||
|
||||
|
||||
@ -273,24 +273,18 @@ void db_base::insertPKI(pki_base *pki)
|
||||
QString db_base::pem2QString(QModelIndexList indexes) const
|
||||
{
|
||||
exportType::etype format;
|
||||
QString msg;
|
||||
BioByteArray bba;
|
||||
|
||||
format = clipboardFormat(indexes);
|
||||
foreach(QModelIndex idx, indexes) {
|
||||
long l;
|
||||
const char *p;
|
||||
if (idx.column() != 0)
|
||||
continue;
|
||||
BIO *bio = BIO_new(BIO_s_mem());
|
||||
pki_base *pki = static_cast<pki_base*>
|
||||
(idx.internalPointer());
|
||||
pki->pem(bio, format);
|
||||
pki->pem(bba, format);
|
||||
openssl_error();
|
||||
l = BIO_get_mem_data(bio, &p);
|
||||
msg += QString::fromUtf8(p, l);
|
||||
BIO_free(bio);
|
||||
}
|
||||
return msg;
|
||||
return QString::fromUtf8(bba);
|
||||
}
|
||||
|
||||
void db_base::pem2clipboard(QModelIndexList indexes) const
|
||||
|
||||
@ -107,11 +107,6 @@ static inline QString htmlEscape(const QString &html)
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline BIO *BIO_from_QByteArray(const QByteArray &ba)
|
||||
{
|
||||
return BIO_new_mem_buf((void*)ba.constData(), ba.length());
|
||||
}
|
||||
|
||||
QString appendXcaComment(QString current, QString msg);
|
||||
|
||||
/* from version.cpp */
|
||||
|
||||
11
lib/main.cpp
11
lib/main.cpp
@ -255,20 +255,21 @@ static database_model* read_cmdline(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
FILE *fp = stdout;
|
||||
BioByteArray bba;
|
||||
foreach(pki_base *pki, cmdline_items->get()) {
|
||||
QString filename = pki->getFilename();
|
||||
if ((cmd_opts.has("text") || cmd_opts.has("print")) &&
|
||||
filename.size() > 0)
|
||||
{
|
||||
console_write(fp, "\n" COL_GREEN COL_UNDER "File: %s"
|
||||
COL_RESET "\n", CCHAR(filename));
|
||||
bba += QString("\n" COL_GREEN COL_UNDER "File: %1"
|
||||
COL_RESET "\n").arg(filename).toUtf8();
|
||||
}
|
||||
if (cmd_opts.has("print"))
|
||||
pki->print(fp, pki_base::print_coloured);
|
||||
pki->print(bba, pki_base::print_coloured);
|
||||
if (cmd_opts.has("text"))
|
||||
pki->print(fp, pki_base::print_openssl_txt);
|
||||
pki->print(bba, pki_base::print_openssl_txt);
|
||||
if (cmd_opts.has("pem"))
|
||||
pki->print(fp, pki_base::print_pem);
|
||||
pki->print(bba, pki_base::print_pem);
|
||||
}
|
||||
if (cmd_opts.has("import")) {
|
||||
models->insert(cmdline_items);
|
||||
|
||||
@ -124,10 +124,9 @@ QByteArray pki_base::i2d() const
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
BIO *pki_base::pem(BIO *, int format)
|
||||
bool pki_base::pem(BioByteArray &, int)
|
||||
{
|
||||
(void)format;
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *pki_base::getClassName() const
|
||||
@ -146,9 +145,7 @@ void pki_base::my_error(const QString &error) const
|
||||
|
||||
void pki_base::fromPEMbyteArray(const QByteArray &ba, const QString &name)
|
||||
{
|
||||
BIO *bio = BIO_from_QByteArray(ba);
|
||||
fromPEM_BIO(bio, name);
|
||||
BIO_free(bio);
|
||||
fromPEM_BIO(BioByteArray(ba).ro(), name);
|
||||
autoIntName(name);
|
||||
setFilename(name);
|
||||
}
|
||||
@ -413,7 +410,7 @@ void pki_base::collect_properties(QMap<QString, QString> &prp) const
|
||||
prp["Type"] = t;
|
||||
}
|
||||
|
||||
void pki_base::print(FILE *fp, enum print_opt opt) const
|
||||
void pki_base::print(BioByteArray &bba, enum print_opt opt) const
|
||||
{
|
||||
static const QStringList order = {
|
||||
"Type", "Descriptor", "Subject", "Issuer", "Serial",
|
||||
@ -425,7 +422,6 @@ void pki_base::print(FILE *fp, enum print_opt opt) const
|
||||
if (opt == print_coloured) {
|
||||
QMap<QString, QString> prp;
|
||||
QStringList keys;
|
||||
QString s;
|
||||
int w = 0;
|
||||
|
||||
collect_properties(prp);
|
||||
@ -441,17 +437,10 @@ void pki_base::print(FILE *fp, enum print_opt opt) const
|
||||
foreach (const QString &key, order) {
|
||||
if (!prp.contains(key))
|
||||
continue;
|
||||
QString val = prp[key];
|
||||
|
||||
if (val == "Yes")
|
||||
val = COL_GREEN "✓" COL_RESET " Yes";
|
||||
else if (val == "No")
|
||||
val = COL_RED "✗" COL_RESET " No";
|
||||
|
||||
s += QString(COL_YELL "%1" COL_RESET " %2\n")
|
||||
.arg(key + ":", w).arg(val);
|
||||
bba += QString(COL_YELL "%1" COL_RESET " %2\n")
|
||||
.arg(key + ":", w).arg(prp[key]).toUtf8();
|
||||
}
|
||||
console_write(fp, "%s", CCHAR(s));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
#include <QRegExp>
|
||||
#include <QVariant>
|
||||
#include <QByteArray>
|
||||
#include "BioByteArray.h"
|
||||
#include "asn1time.h"
|
||||
#include "pkcs11_lib.h"
|
||||
#include "base.h"
|
||||
@ -104,7 +105,7 @@ class pki_base : public QObject
|
||||
pki->setFilename(getFilename());
|
||||
}
|
||||
virtual QString comboText() const;
|
||||
virtual void print(FILE *fp, enum print_opt opt) const;
|
||||
virtual void print(BioByteArray &b, enum print_opt opt) const;
|
||||
QString getUnderlinedName() const;
|
||||
void setIntName(const QString &d)
|
||||
{
|
||||
@ -156,7 +157,7 @@ class pki_base : public QObject
|
||||
virtual int renameOnToken(const slotid &, const QString &);
|
||||
|
||||
/* Import / Export management */
|
||||
virtual BIO *pem(BIO *, int format=0);
|
||||
virtual bool pem(BioByteArray &b, int format=0);
|
||||
virtual void fromPEM_BIO(BIO *, const QString &);
|
||||
virtual void fromPEMbyteArray(const QByteArray &, const QString &);
|
||||
virtual void fload(const QString &);
|
||||
|
||||
@ -281,13 +281,9 @@ void pki_crl::writeCrl(XFile &file, bool pem) const
|
||||
pki_openssl_error();
|
||||
}
|
||||
|
||||
BIO *pki_crl::pem(BIO *b, int format)
|
||||
bool pki_crl::pem(BioByteArray &b, int)
|
||||
{
|
||||
(void)format;
|
||||
if (!b)
|
||||
b = BIO_new(BIO_s_mem());
|
||||
PEM_write_bio_X509_CRL(b, crl);
|
||||
return b;
|
||||
return PEM_write_bio_X509_CRL(b, crl);
|
||||
}
|
||||
|
||||
a1time pki_crl::getLastUpdate() const
|
||||
@ -461,15 +457,15 @@ void pki_crl::collect_properties(QMap<QString, QString> &prp) const
|
||||
pki_x509name::collect_properties(prp);
|
||||
}
|
||||
|
||||
void pki_crl::print(FILE *fp, enum print_opt opt) const
|
||||
void pki_crl::print(BioByteArray &bba, enum print_opt opt) const
|
||||
{
|
||||
pki_x509name::print(fp, opt);
|
||||
pki_x509name::print(bba, opt);
|
||||
switch (opt) {
|
||||
case print_openssl_txt:
|
||||
X509_CRL_print_fp(fp, crl);
|
||||
X509_CRL_print(bba, crl);
|
||||
break;
|
||||
case print_pem:
|
||||
PEM_write_X509_CRL(fp, crl);
|
||||
PEM_write_bio_X509_CRL(bba, crl);
|
||||
break;
|
||||
case print_coloured:
|
||||
break;
|
||||
|
||||
@ -92,14 +92,14 @@ class pki_crl: public pki_x509name
|
||||
void setCrlNumber(a1int num);
|
||||
bool getCrlNumber(a1int *num) const;
|
||||
a1int getCrlNumber() const;
|
||||
BIO *pem(BIO *, int);
|
||||
bool pem(BioByteArray &, int);
|
||||
bool visible() const;
|
||||
QSqlError lookupIssuer();
|
||||
QSqlError insertSqlData();
|
||||
QSqlError deleteSqlData();
|
||||
void restoreSql(const QSqlRecord &rec);
|
||||
QStringList icsVEVENT() const;
|
||||
void print(FILE *fp, enum print_opt opt) const;
|
||||
void print(BioByteArray &b, enum print_opt opt) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
#include "func.h"
|
||||
#include "db.h"
|
||||
#include "entropy.h"
|
||||
#include "BioByteArray.h"
|
||||
#include "widgets/PwDialog.h"
|
||||
#include "widgets/XcaWarning.h"
|
||||
#include "widgets/XcaProgress.h"
|
||||
@ -233,13 +234,12 @@ bool pki_evp::openssl_pw_error() const
|
||||
|
||||
void pki_evp::fromPEMbyteArray(const QByteArray &ba, const QString &name)
|
||||
{
|
||||
BIO *bio = BIO_from_QByteArray(ba);
|
||||
EVP_PKEY *pkey;
|
||||
pass_info p(XCA_TITLE,
|
||||
tr("Please enter the password to decrypt the private key %1.")
|
||||
.arg(name));
|
||||
do {
|
||||
pkey = PEM_read_bio_PrivateKey(bio, NULL,
|
||||
pkey = PEM_read_bio_PrivateKey(BioByteArray(ba).ro(), NULL,
|
||||
PwDialog::pwCallback, &p);
|
||||
if (openssl_pw_error())
|
||||
XCA_PASSWD_ERROR();
|
||||
@ -251,11 +251,8 @@ void pki_evp::fromPEMbyteArray(const QByteArray &ba, const QString &name)
|
||||
|
||||
if (!pkey) {
|
||||
pki_ign_openssl_error();
|
||||
BIO_free(bio);
|
||||
bio = BIO_from_QByteArray(ba);
|
||||
pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, 0);
|
||||
pkey = PEM_read_bio_PUBKEY(BioByteArray(ba).ro(), NULL, NULL, 0);
|
||||
}
|
||||
BIO_free(bio);
|
||||
pki_openssl_error();
|
||||
set_EVP_PKEY(pkey, name);
|
||||
}
|
||||
@ -463,10 +460,8 @@ EVP_PKEY *pki_evp::decryptKey() const
|
||||
qDebug() << "myencKey.count()"<<myencKey.count();
|
||||
if (myencKey.count() == 0)
|
||||
return NULL;
|
||||
BIO *b = BIO_from_QByteArray(myencKey);
|
||||
check_oom(b);
|
||||
EVP_PKEY *priv = NULL;
|
||||
X509_SIG *p8 = d2i_PKCS8_bio(b, NULL);
|
||||
X509_SIG *p8 = d2i_PKCS8_bio(BioByteArray(myencKey).ro(), NULL);
|
||||
if (p8) {
|
||||
PKCS8_PRIV_KEY_INFO *p8inf = PKCS8_decrypt(p8,
|
||||
ownPassBuf.constData(), ownPassBuf.size());
|
||||
@ -476,7 +471,6 @@ EVP_PKEY *pki_evp::decryptKey() const
|
||||
}
|
||||
X509_SIG_free(p8);
|
||||
}
|
||||
BIO_free(b);
|
||||
if (priv)
|
||||
return priv;
|
||||
pki_ign_openssl_error();
|
||||
@ -601,14 +595,11 @@ void pki_evp::encryptKey(const char *password)
|
||||
}
|
||||
|
||||
/* Convert private key to DER(PKCS8-aes) */
|
||||
const char *p;
|
||||
BIO *bio = BIO_new(BIO_s_mem());
|
||||
i2d_PKCS8PrivateKey_bio(bio, key, EVP_aes_256_cbc(),
|
||||
BioByteArray bba;
|
||||
i2d_PKCS8PrivateKey_bio(bba, key, EVP_aes_256_cbc(),
|
||||
ownPassBuf.data(), ownPassBuf.size(), NULL, 0);
|
||||
pki_openssl_error();
|
||||
int l = BIO_get_mem_data(bio, &p);
|
||||
encKey = QByteArray(p, l);
|
||||
BIO_free(bio);
|
||||
encKey = bba;
|
||||
|
||||
/* Replace private key by public key and
|
||||
have the encrypted private in "encKey"
|
||||
|
||||
@ -92,19 +92,15 @@ QByteArray pki_key::i2d() const
|
||||
return i2d_bytearray(I2D_VOID(i2d_PUBKEY), key);
|
||||
}
|
||||
|
||||
BIO *pki_key::pem(BIO *b, int format)
|
||||
bool pki_key::pem(BioByteArray &b, int format)
|
||||
{
|
||||
EVP_PKEY *pkey;
|
||||
QByteArray ba;
|
||||
int keytype;
|
||||
|
||||
if (!b)
|
||||
b = BIO_new(BIO_s_mem());
|
||||
|
||||
switch (format) {
|
||||
case exportType::SSH2_public:
|
||||
ba = SSH2publicQByteArray();
|
||||
BIO_write(b, ba.data(), ba.size());
|
||||
b += SSH2publicQByteArray();
|
||||
break;
|
||||
case exportType::PEM_private:
|
||||
pkey = decryptKey();
|
||||
@ -138,8 +134,10 @@ BIO *pki_key::pem(BIO *b, int format)
|
||||
case exportType::PEM_key:
|
||||
PEM_write_bio_PUBKEY(b, key);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return b;
|
||||
return true;
|
||||
}
|
||||
|
||||
QString pki_key::length() const
|
||||
@ -828,24 +826,19 @@ void pki_key::collect_properties(QMap<QString, QString> &prp) const
|
||||
pki_base::collect_properties(prp);
|
||||
}
|
||||
|
||||
void pki_key::print(FILE *fp, enum print_opt opt) const
|
||||
void pki_key::print(BioByteArray &bba, enum print_opt opt) const
|
||||
{
|
||||
pki_base::print(fp, opt);
|
||||
BIO *b;
|
||||
pki_base::print(bba, opt);
|
||||
switch (opt) {
|
||||
case print_openssl_txt:
|
||||
b = BIO_new(BIO_s_file());
|
||||
check_oom(b);
|
||||
#if OPENSSL_VERSION_NUMBER < 0x10000000L
|
||||
BIO_write(b, "Not supported\n", sizeof "Not supported\n" -1);
|
||||
bba += "Not supported\n";
|
||||
#else
|
||||
EVP_PKEY_print_public(b, key, 0, NULL);
|
||||
EVP_PKEY_print_public(bba, key, 0, NULL);
|
||||
#endif
|
||||
BIO_set_fp(b, fp, BIO_NOCLOSE);
|
||||
BIO_free(b);
|
||||
break;
|
||||
case print_pem:
|
||||
PEM_write_PUBKEY(fp, key);
|
||||
PEM_write_bio_PUBKEY(bba, key);
|
||||
break;
|
||||
case print_coloured:
|
||||
break;
|
||||
|
||||
@ -208,7 +208,7 @@ class pki_key: public pki_base
|
||||
{
|
||||
qFatal("generate in pki_key");
|
||||
}
|
||||
BIO *pem(BIO *, int);
|
||||
bool pem(BioByteArray &, int);
|
||||
QVariant column_data(const dbheader *hd) const;
|
||||
QString modulus() const;
|
||||
QString pubEx() const;
|
||||
@ -224,7 +224,7 @@ class pki_key: public pki_base
|
||||
void writeSSH2public(XFile &file) const;
|
||||
QString fingerprint(const QString &format) const;
|
||||
bool SSH2_compatible() const;
|
||||
void print(FILE *fp, enum print_opt opt) const;
|
||||
void print(BioByteArray &b, enum print_opt opt) const;
|
||||
void resetUcount()
|
||||
{
|
||||
useCount = -1;
|
||||
|
||||
@ -160,10 +160,10 @@ void pki_multi::probeAnything(const QString &fname)
|
||||
qDeleteAll(lbs);
|
||||
}
|
||||
|
||||
void pki_multi::print(FILE *fp, enum print_opt opt) const
|
||||
void pki_multi::print(BioByteArray &bba, enum print_opt opt) const
|
||||
{
|
||||
foreach(pki_base *pki, multi)
|
||||
pki->print(fp, opt);
|
||||
pki->print(bba, opt);
|
||||
}
|
||||
|
||||
QList<pki_base *> pki_multi::pull()
|
||||
|
||||
@ -25,7 +25,7 @@ class pki_multi: public pki_base
|
||||
void fload(const QString &fname);
|
||||
void probeAnything(const QString &fname);
|
||||
void append_item(pki_base *pki);
|
||||
void print(FILE *fp, enum print_opt opt) const;
|
||||
void print(BioByteArray &b, enum print_opt opt) const;
|
||||
QList<pki_base *> pull();
|
||||
QList<pki_base *> get() const;
|
||||
};
|
||||
|
||||
@ -86,14 +86,10 @@ void pki_pkcs7::signFile(pki_x509 *crt, const QString &filename)
|
||||
|
||||
void pki_pkcs7::signCert(pki_x509 *crt, pki_x509 *contCert)
|
||||
{
|
||||
BIO *bio;
|
||||
if (!crt)
|
||||
return;
|
||||
bio = BIO_new(BIO_s_mem());
|
||||
BioByteArray bba;
|
||||
i2d_X509_bio(bba, contCert->getCert());
|
||||
signBio(crt, bba);
|
||||
openssl_error();
|
||||
i2d_X509_bio(bio, contCert->getCert());
|
||||
signBio(crt, bio);
|
||||
BIO_free(bio);
|
||||
}
|
||||
|
||||
void pki_pkcs7::writeP7(XFile &file, bool PEM)
|
||||
|
||||
@ -405,16 +405,11 @@ void pki_temp::writeDefault(const QString &dirname) const
|
||||
writeTemp(file);
|
||||
}
|
||||
|
||||
BIO *pki_temp::pem(BIO *b, int format)
|
||||
bool pki_temp::pem(BioByteArray &b, int)
|
||||
{
|
||||
(void)format;
|
||||
QByteArray ba = toExportData();
|
||||
if (!b)
|
||||
b = BIO_new(BIO_s_mem());
|
||||
PEM_write_bio(b, PEM_STRING_XCA_TEMPLATE, (char*)"",
|
||||
return PEM_write_bio(b, PEM_STRING_XCA_TEMPLATE, (char*)"",
|
||||
(unsigned char*)(ba.data()), ba.size());
|
||||
pki_openssl_error();
|
||||
return b;
|
||||
}
|
||||
|
||||
void pki_temp::fromExportData(QByteArray data)
|
||||
@ -430,20 +425,14 @@ void pki_temp::fromExportData(QByteArray data)
|
||||
data.size(), version);
|
||||
}
|
||||
|
||||
void pki_temp::try_fload(XFile &file, const char *mode)
|
||||
void pki_temp::try_fload(XFile &file)
|
||||
{
|
||||
BIO *b = BIO_new(BIO_s_file());
|
||||
check_oom(b);
|
||||
pki_openssl_error();
|
||||
BIO_set_fp(b, file.fp(mode), BIO_NOCLOSE);
|
||||
QByteArray ba = file.read(4096*1024);
|
||||
try {
|
||||
fromPEM_BIO(b, file.fileName());
|
||||
fromPEM_BIO(BioByteArray(ba).ro(), file.fileName());
|
||||
} catch (errorEx &err) {
|
||||
file.retry_read();
|
||||
QByteArray ba = file.read(4096*1024);
|
||||
fromExportData(ba);
|
||||
}
|
||||
BIO_free(b);
|
||||
pki_openssl_error();
|
||||
}
|
||||
|
||||
@ -452,14 +441,14 @@ void pki_temp::fload(const QString &fname)
|
||||
try {
|
||||
XFile file(fname);
|
||||
file.open_read();
|
||||
try_fload(file, "rb");
|
||||
try_fload(file);
|
||||
} catch (errorEx &err) {
|
||||
#if defined(Q_OS_WIN32)
|
||||
/* Try again in ascii mode on Windows
|
||||
* to support pre 1.1.0 template exports */
|
||||
XFile file(fname);
|
||||
file.open(QIODevice::ReadOnly | QIODevice::QIODevice::Text);
|
||||
try_fload(file, "r");
|
||||
try_fload(file);
|
||||
#else
|
||||
throw err;
|
||||
#endif
|
||||
|
||||
@ -28,7 +28,7 @@ class pki_temp: public pki_x509name
|
||||
protected:
|
||||
static const QList<QString> tmpl_keys;
|
||||
int dataSize();
|
||||
void try_fload(XFile &file, const char *mode);
|
||||
void try_fload(XFile &file);
|
||||
bool pre_defined;
|
||||
x509name xname;
|
||||
QMap<QString, QString> settings;
|
||||
@ -85,7 +85,7 @@ class pki_temp: public pki_x509name
|
||||
{
|
||||
xname = n;
|
||||
}
|
||||
BIO *pem(BIO *b, int format);
|
||||
bool pem(BioByteArray &, int);
|
||||
QByteArray toExportData() const;
|
||||
void fromPEM_BIO(BIO *, const QString &);
|
||||
void fromExportData(QByteArray data);
|
||||
|
||||
@ -680,13 +680,9 @@ QString pki_x509::getIndexEntry()
|
||||
QString(X509_NAME_oneline(getSubject().get(), NULL, 0)));
|
||||
}
|
||||
|
||||
BIO *pki_x509::pem(BIO *b, int format)
|
||||
bool pki_x509::pem(BioByteArray &b, int)
|
||||
{
|
||||
(void)format;
|
||||
if (!b)
|
||||
b = BIO_new(BIO_s_mem());
|
||||
PEM_write_bio_X509(b, cert);
|
||||
return b;
|
||||
return PEM_write_bio_X509(b, cert);
|
||||
}
|
||||
|
||||
bool pki_x509::cmpIssuerAndSerial(pki_x509 *refcert)
|
||||
@ -973,15 +969,15 @@ void pki_x509::collect_properties(QMap<QString, QString> &prp) const
|
||||
pki_x509super::collect_properties(prp);
|
||||
}
|
||||
|
||||
void pki_x509::print(FILE *fp, enum print_opt opt) const
|
||||
void pki_x509::print(BioByteArray &bba, enum print_opt opt) const
|
||||
{
|
||||
pki_x509super::print(fp, opt);
|
||||
pki_x509super::print(bba, opt);
|
||||
switch (opt) {
|
||||
case print_openssl_txt:
|
||||
X509_print_fp(fp, cert);
|
||||
X509_print(bba, cert);
|
||||
break;
|
||||
case print_pem:
|
||||
PEM_write_X509(fp, cert);
|
||||
PEM_write_bio_X509(bba, cert);
|
||||
break;
|
||||
case print_coloured:
|
||||
break;
|
||||
|
||||
@ -137,7 +137,7 @@ class pki_x509 : public pki_x509super
|
||||
bool cmpIssuerAndSerial(pki_x509 *refcert);
|
||||
bool visible() const;
|
||||
void updateView();
|
||||
void print(FILE *fp, enum print_opt opt) const;
|
||||
void print(BioByteArray &b, enum print_opt opt) const;
|
||||
x509v3ext getExtByNid(int nid) const;
|
||||
QVariant column_data(const dbheader *hd) const;
|
||||
QVariant getIcon(const dbheader *hd) const;
|
||||
@ -148,7 +148,7 @@ class pki_x509 : public pki_x509super
|
||||
void deleteFromToken(const slotid &slot);
|
||||
QString getMsg(msg_type msg) const;
|
||||
int renameOnToken(const slotid &slot, const QString &name);
|
||||
BIO *pem(BIO *, int);
|
||||
bool pem(BioByteArray &, int);
|
||||
QVariant bg_color(const dbheader *hd) const;
|
||||
void mergeRevList(x509revList &l);
|
||||
void setRevocations(const x509revList &rl);
|
||||
|
||||
@ -265,13 +265,9 @@ void pki_x509req::writeReq(XFile &file, bool pem) const
|
||||
pki_openssl_error();
|
||||
}
|
||||
|
||||
BIO *pki_x509req::pem(BIO *b, int format)
|
||||
bool pki_x509req::pem(BioByteArray &b, int)
|
||||
{
|
||||
(void)format;
|
||||
if (!b)
|
||||
b = BIO_new(BIO_s_mem());
|
||||
PEM_write_bio_X509_REQ(b, request);
|
||||
return b;
|
||||
return PEM_write_bio_X509_REQ(b, request);
|
||||
}
|
||||
|
||||
bool pki_x509req::verify() const
|
||||
@ -371,15 +367,15 @@ void pki_x509req::collect_properties(QMap<QString, QString> &prp) const
|
||||
prp["Verify Ok"] = verify() ? "Yes" : "No";
|
||||
}
|
||||
|
||||
void pki_x509req::print(FILE *fp, enum print_opt opt) const
|
||||
void pki_x509req::print(BioByteArray &bba, enum print_opt opt) const
|
||||
{
|
||||
pki_x509super::print(fp, opt);
|
||||
pki_x509super::print(bba, opt);
|
||||
switch (opt) {
|
||||
case print_openssl_txt:
|
||||
X509_REQ_print_fp(fp, request);
|
||||
X509_REQ_print(bba, request);
|
||||
break;
|
||||
case print_pem:
|
||||
PEM_write_X509_REQ(fp, request);
|
||||
PEM_write_bio_X509_REQ(bba, request);
|
||||
break;
|
||||
case print_coloured:
|
||||
break;
|
||||
|
||||
@ -44,7 +44,7 @@ class pki_x509req : public pki_x509super
|
||||
x509name getSubject() const;
|
||||
void writeReq(XFile &file, bool pem) const;
|
||||
void markSigned(bool signe);
|
||||
void print(FILE *fp, enum print_opt opt) const;
|
||||
void print(BioByteArray &b, enum print_opt opt) const;
|
||||
X509_REQ *getReq()
|
||||
{
|
||||
return request;
|
||||
@ -75,7 +75,7 @@ class pki_x509req : public pki_x509super
|
||||
virtual QString getMsg(msg_type msg) const;
|
||||
void d2i(QByteArray &ba);
|
||||
QByteArray i2d() const;
|
||||
BIO *pem(BIO *, int);
|
||||
bool pem(BioByteArray &, int);
|
||||
bool visible() const;
|
||||
QSqlError insertSqlData();
|
||||
QSqlError deleteSqlData();
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
#include "x509name.h"
|
||||
#include "base.h"
|
||||
#include "func.h"
|
||||
#include "BioByteArray.h"
|
||||
#include <openssl/asn1.h>
|
||||
#include <openssl/err.h>
|
||||
#include "exception.h"
|
||||
@ -65,15 +66,9 @@ x509name &x509name::set(const STACK_OF(X509_NAME_ENTRY) *entries)
|
||||
|
||||
QString x509name::oneLine(unsigned long flags) const
|
||||
{
|
||||
QString ret;
|
||||
long l;
|
||||
const char *p;
|
||||
BIO *mem = BIO_new(BIO_s_mem());
|
||||
X509_NAME_print_ex(mem, xn, 0, flags);
|
||||
l = BIO_get_mem_data(mem, &p);
|
||||
ret = ret.fromUtf8(p,l);
|
||||
BIO_free(mem);
|
||||
return ret;
|
||||
BioByteArray bba;
|
||||
X509_NAME_print_ex(bba, xn, 0, flags);
|
||||
return QString::fromUtf8(bba);
|
||||
}
|
||||
|
||||
QString x509name::getEntryByNid(int nid) const
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
#include <QStringList>
|
||||
#include <QDebug>
|
||||
#include "base.h"
|
||||
#include "BioByteArray.h"
|
||||
|
||||
x509v3ext::x509v3ext()
|
||||
{
|
||||
@ -143,21 +144,13 @@ ASN1_OCTET_STRING *x509v3ext::getData() const
|
||||
|
||||
QString x509v3ext::getValue() const
|
||||
{
|
||||
QString text = "";
|
||||
int ret;
|
||||
char *p = NULL;
|
||||
BIO *bio = BIO_new(BIO_s_mem());
|
||||
|
||||
ret = X509V3_EXT_print(bio, ext, X509V3_EXT_DEFAULT, 0);
|
||||
if (ign_openssl_error() || !ret) {
|
||||
ret = ASN1_STRING_print(bio, (ASN1_STRING *) getData());
|
||||
}
|
||||
if (!ign_openssl_error() && ret) {
|
||||
long len = BIO_get_mem_data(bio, &p);
|
||||
text = QString::fromLocal8Bit(p, len);
|
||||
}
|
||||
BIO_free(bio);
|
||||
return text.trimmed();
|
||||
BioByteArray bba;
|
||||
int ret = X509V3_EXT_print(bba, ext, X509V3_EXT_DEFAULT, 0);
|
||||
if (ign_openssl_error() || !ret)
|
||||
ret = ASN1_STRING_print(bba, (ASN1_STRING *)getData());
|
||||
if (ign_openssl_error() || !ret)
|
||||
return QString();
|
||||
return QString::fromLocal8Bit(bba).trimmed();
|
||||
}
|
||||
|
||||
QString x509v3ext::getHtmlValue() const
|
||||
|
||||
@ -41,9 +41,8 @@ class XFile : public QFile
|
||||
}
|
||||
BIO *bio()
|
||||
{
|
||||
if (b)
|
||||
BIO_free(b);
|
||||
b = BIO_new_fp(fp(), BIO_NOCLOSE);
|
||||
if (!b)
|
||||
b = BIO_new_fp(fp(), BIO_NOCLOSE);
|
||||
return b;
|
||||
}
|
||||
FILE *fp(const char *mode = NULL)
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
|
||||
#include "MainWindow.h"
|
||||
#include "lib/x509v3ext.h"
|
||||
#include "lib/BioByteArray.h"
|
||||
#include "lib/func.h"
|
||||
|
||||
#include "lib/openssl_compat.h"
|
||||
@ -168,7 +169,6 @@ extList NewX509::getAdvanced()
|
||||
{
|
||||
QString conf_str;
|
||||
CONF *conf;
|
||||
BIO *bio;
|
||||
extList elist;
|
||||
long err_line=0;
|
||||
STACK_OF(X509_EXTENSION) **sk, *sk_tmp = NULL;
|
||||
@ -184,14 +184,10 @@ extList NewX509::getAdvanced()
|
||||
if (conf_str.isEmpty())
|
||||
return elist;
|
||||
|
||||
QByteArray cs = conf_str.toLatin1();
|
||||
bio = BIO_from_QByteArray(cs);
|
||||
if (!bio)
|
||||
return elist;
|
||||
conf = NCONF_new(NULL);
|
||||
ret = NCONF_load_bio(conf, bio, &err_line);
|
||||
ret = NCONF_load_bio(conf, BioByteArray(conf_str.toLatin1()).ro(),
|
||||
&err_line);
|
||||
if (ret != 1) {
|
||||
BIO_free(bio);
|
||||
openssl_error(tr("Configfile error on line %1\n").
|
||||
arg(err_line));
|
||||
return elist;
|
||||
@ -224,7 +220,6 @@ extList NewX509::getAdvanced()
|
||||
|
||||
X509V3_set_nconf(&ext_ctx, NULL);
|
||||
NCONF_free(conf);
|
||||
BIO_free(bio);
|
||||
openssl_error();
|
||||
return elist;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user