diff --git a/lib/BioByteArray.cpp b/lib/BioByteArray.cpp new file mode 100644 index 00000000..d0ce3aff --- /dev/null +++ b/lib/BioByteArray.cpp @@ -0,0 +1,119 @@ +/* vi: set sw=4 ts=4: + * + * Copyright (C) 2020 Christian Hohnstaedt. + * + * All rights reserved. + */ + +#include "BioByteArray.h" +#include + +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; +} diff --git a/lib/BioByteArray.h b/lib/BioByteArray.h new file mode 100644 index 00000000..bd3d058b --- /dev/null +++ b/lib/BioByteArray.h @@ -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 +#include + +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 diff --git a/lib/Makefile b/lib/Makefile index b290e90d..de784e9a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -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)) diff --git a/lib/db_base.cpp b/lib/db_base.cpp index da255cfb..afdb6ea2 100644 --- a/lib/db_base.cpp +++ b/lib/db_base.cpp @@ -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 (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 diff --git a/lib/func.h b/lib/func.h index b9b9485d..81e55f0f 100644 --- a/lib/func.h +++ b/lib/func.h @@ -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 */ diff --git a/lib/main.cpp b/lib/main.cpp index 5eaee0c9..b1ca1693 100644 --- a/lib/main.cpp +++ b/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); diff --git a/lib/pki_base.cpp b/lib/pki_base.cpp index 9e015eeb..269a20eb 100644 --- a/lib/pki_base.cpp +++ b/lib/pki_base.cpp @@ -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 &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 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)); } } diff --git a/lib/pki_base.h b/lib/pki_base.h index 1e70bcfa..2a51eafe 100644 --- a/lib/pki_base.h +++ b/lib/pki_base.h @@ -12,6 +12,7 @@ #include #include #include +#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 &); diff --git a/lib/pki_crl.cpp b/lib/pki_crl.cpp index d0d27bad..09aad66b 100644 --- a/lib/pki_crl.cpp +++ b/lib/pki_crl.cpp @@ -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 &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; diff --git a/lib/pki_crl.h b/lib/pki_crl.h index 0cbbe11f..08c753d2 100644 --- a/lib/pki_crl.h +++ b/lib/pki_crl.h @@ -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 diff --git a/lib/pki_evp.cpp b/lib/pki_evp.cpp index 61697345..85db2d17 100644 --- a/lib/pki_evp.cpp +++ b/lib/pki_evp.cpp @@ -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()"< &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; diff --git a/lib/pki_key.h b/lib/pki_key.h index 80083dc8..9b48ea0b 100644 --- a/lib/pki_key.h +++ b/lib/pki_key.h @@ -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; diff --git a/lib/pki_multi.cpp b/lib/pki_multi.cpp index ef92a05e..cba001a8 100644 --- a/lib/pki_multi.cpp +++ b/lib/pki_multi.cpp @@ -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_multi::pull() diff --git a/lib/pki_multi.h b/lib/pki_multi.h index e1e8ca93..8bcc86d2 100644 --- a/lib/pki_multi.h +++ b/lib/pki_multi.h @@ -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 pull(); QList get() const; }; diff --git a/lib/pki_pkcs7.cpp b/lib/pki_pkcs7.cpp index 0331b330..1066544e 100644 --- a/lib/pki_pkcs7.cpp +++ b/lib/pki_pkcs7.cpp @@ -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) diff --git a/lib/pki_temp.cpp b/lib/pki_temp.cpp index df314129..0ae282f1 100644 --- a/lib/pki_temp.cpp +++ b/lib/pki_temp.cpp @@ -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 diff --git a/lib/pki_temp.h b/lib/pki_temp.h index 15b24f30..a46ecc49 100644 --- a/lib/pki_temp.h +++ b/lib/pki_temp.h @@ -28,7 +28,7 @@ class pki_temp: public pki_x509name protected: static const QList tmpl_keys; int dataSize(); - void try_fload(XFile &file, const char *mode); + void try_fload(XFile &file); bool pre_defined; x509name xname; QMap 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); diff --git a/lib/pki_x509.cpp b/lib/pki_x509.cpp index a6e0e367..ae9b47f6 100644 --- a/lib/pki_x509.cpp +++ b/lib/pki_x509.cpp @@ -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 &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; diff --git a/lib/pki_x509.h b/lib/pki_x509.h index 3ea97396..4eb11031 100644 --- a/lib/pki_x509.h +++ b/lib/pki_x509.h @@ -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); diff --git a/lib/pki_x509req.cpp b/lib/pki_x509req.cpp index 2d5af007..d0859fb3 100644 --- a/lib/pki_x509req.cpp +++ b/lib/pki_x509req.cpp @@ -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 &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; diff --git a/lib/pki_x509req.h b/lib/pki_x509req.h index d7a86264..cc434f06 100644 --- a/lib/pki_x509req.h +++ b/lib/pki_x509req.h @@ -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(); diff --git a/lib/x509name.cpp b/lib/x509name.cpp index 86c66a8f..d22fd29a 100644 --- a/lib/x509name.cpp +++ b/lib/x509name.cpp @@ -9,6 +9,7 @@ #include "x509name.h" #include "base.h" #include "func.h" +#include "BioByteArray.h" #include #include #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 diff --git a/lib/x509v3ext.cpp b/lib/x509v3ext.cpp index 41676351..cb4535a5 100644 --- a/lib/x509v3ext.cpp +++ b/lib/x509v3ext.cpp @@ -15,6 +15,7 @@ #include #include #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 diff --git a/lib/xfile.h b/lib/xfile.h index 5ee28d0f..1f150b02 100644 --- a/lib/xfile.h +++ b/lib/xfile.h @@ -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) diff --git a/widgets/NewX509_ext.cpp b/widgets/NewX509_ext.cpp index d5b735de..24e8f2b7 100644 --- a/widgets/NewX509_ext.cpp +++ b/widgets/NewX509_ext.cpp @@ -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; }