From 85d2a26aa92becd06cbc4991b7fbef8fd1af2815 Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Wed, 20 May 2015 11:15:36 +0200 Subject: [PATCH] Fix all fopen() calls to always use "wb" or "rb" fix PKI item autodetection Fix compiler warning because of a changed OpenSSL 0.9.8/1.0.1 "PEM_load_bio()" API change --- lib/db_base.cpp | 4 ++-- lib/func.h | 13 ++++++++++++- lib/oid.cpp | 4 ++-- lib/pki_crl.cpp | 4 ++-- lib/pki_evp.cpp | 6 +++--- lib/pki_key.cpp | 2 +- lib/pki_multi.cpp | 4 +++- lib/pki_pkcs12.cpp | 4 ++-- lib/pki_pkcs7.cpp | 4 ++-- lib/pki_temp.cpp | 9 +++++++-- lib/pki_x509.cpp | 6 +++--- lib/pki_x509req.cpp | 4 ++-- lib/pki_x509super.cpp | 2 +- widgets/MW_database.cpp | 6 +++--- widgets/MainWindow.cpp | 2 +- 15 files changed, 46 insertions(+), 28 deletions(-) diff --git a/lib/db_base.cpp b/lib/db_base.cpp index 2829bc34..a0ccc238 100644 --- a/lib/db_base.cpp +++ b/lib/db_base.cpp @@ -97,7 +97,7 @@ int db_base::handleBadEntry(unsigned char *p, db_header_t *head) size_t l; db_header_t h; - FILE *fp = fopen(QString2filename(s), "wb"); + FILE *fp = fopen_write(s); if (!fp) { throw errorEx(tr("Error opening file: '%1': %2"). arg(s).arg(strerror(errno)), class_name); @@ -637,7 +637,7 @@ void db_base::store(QModelIndexList indexes) mainwin->setPath(s.mid(0, s.lastIndexOf(QRegExp("[/\\\\]")) )); try { QString pem = pem2QString(indexes); - FILE *fp = fopen(QString2filename(s), "wb"); + FILE *fp = fopen_write(s); if (!fp) { throw errorEx(tr("Error opening file: '%1': %2"). arg(s).arg(strerror(errno)), class_name); diff --git a/lib/func.h b/lib/func.h index 9054f655..1465bef4 100644 --- a/lib/func.h +++ b/lib/func.h @@ -8,6 +8,7 @@ #ifndef __FUNC_H #define __FUNC_H +#include #include #include #include @@ -42,7 +43,6 @@ void inc_progress_bar(int, int, void *p); extern bool translate_dn; extern QMap dn_translations; void dn_translations_setup(); - #define openssl_error(x) _openssl_error(QString(x), C_FILE, __LINE__) #define ign_openssl_error(x) _ign_openssl_error(QString(x), C_FILE, __LINE__) void _openssl_error(const QString txt, const char *file, int line); @@ -57,4 +57,15 @@ BIO *BIO_QBA_mem_buf(QByteArray &a); #define D2I_VOID(a) ((void *(*)(void *, unsigned char **, long))(a)) #define QString2filename(str) filename2bytearray(str).constData() + +static inline FILE *fopen_read(QString s) +{ + return fopen(QString2filename(s), "rb"); +} + +static inline FILE *fopen_write(QString s) +{ + return fopen(QString2filename(s), "wb"); +} + #endif diff --git a/lib/oid.cpp b/lib/oid.cpp index f42de4bb..d52800b9 100644 --- a/lib/oid.cpp +++ b/lib/oid.cpp @@ -26,7 +26,7 @@ static void readOIDs(QString fname) int line = 0; QStringList sl; - fp = fopen(QString2filename(fname), "r"); + fp = fopen_read(fname); if (fp == NULL) return; @@ -92,7 +92,7 @@ NIDlist readNIDlist(QString fname) int line = 0, nid; NIDlist nl; nl.clear(); - fp = fopen(QString2filename(fname), "r"); + fp = fopen_read(fname); if (fp == NULL) return nl; while (fgets(buff, 127, fp)) { line++; diff --git a/lib/pki_crl.cpp b/lib/pki_crl.cpp index 1be5b392..cefb4716 100644 --- a/lib/pki_crl.cpp +++ b/lib/pki_crl.cpp @@ -54,7 +54,7 @@ QString pki_crl::getMsg(msg_type msg) void pki_crl::fload(const QString fname) { - FILE *fp = fopen(QString2filename(fname), "r"); + FILE *fp = fopen_read(fname); X509_CRL *_crl; if (fp != NULL) { _crl = PEM_read_X509_CRL(fp, NULL, NULL, NULL); @@ -198,7 +198,7 @@ void pki_crl::writeDefault(const QString fname) void pki_crl::writeCrl(const QString fname, bool pem) { - FILE *fp = fopen(QString2filename(fname), "w"); + FILE *fp = fopen_write(fname); if (fp != NULL) { if (crl){ if (pem) diff --git a/lib/pki_evp.cpp b/lib/pki_evp.cpp index 6244c933..16ffede9 100644 --- a/lib/pki_evp.cpp +++ b/lib/pki_evp.cpp @@ -240,7 +240,7 @@ void pki_evp::fload(const QString fname) pass_info p(XCA_TITLE, tr("Please enter the password to decrypt the private key from file:\n%1"). arg(compressFilename(fname))); pem_password_cb *cb = PwDialog::pwCallback; - FILE *fp = fopen(QString2filename(fname), "r"); + FILE *fp = fopen_read(fname); EVP_PKEY *pkey; pki_ign_openssl_error(); @@ -561,7 +561,7 @@ void pki_evp::writePKCS8(const QString fname, const EVP_CIPHER *enc, { EVP_PKEY *pkey; pass_info p(XCA_TITLE, tr("Please enter the password protecting the PKCS#8 key '%1'").arg(getIntName())); - FILE *fp = fopen(QString2filename(fname), "w"); + FILE *fp = fopen_write(fname); if (fp != NULL) { if (key) { pkey = decryptKey(); @@ -600,7 +600,7 @@ void pki_evp::writeKey(const QString fname, const EVP_CIPHER *enc, writePublic(fname, pem); return; } - FILE *fp = fopen(QString2filename(fname), "w"); + FILE *fp = fopen_write(fname); if (!fp) { fopen_error(fname); return; diff --git a/lib/pki_key.cpp b/lib/pki_key.cpp index c5815df1..ac6672a2 100644 --- a/lib/pki_key.cpp +++ b/lib/pki_key.cpp @@ -300,7 +300,7 @@ bool pki_key::compare(pki_base *ref) void pki_key::writePublic(const QString fname, bool pem) { - FILE *fp = fopen(QString2filename(fname), "w"); + FILE *fp = fopen_write(fname); if (fp == NULL) { fopen_error(fname); return; diff --git a/lib/pki_multi.cpp b/lib/pki_multi.cpp index 38ccbc72..f7099ae7 100644 --- a/lib/pki_multi.cpp +++ b/lib/pki_multi.cpp @@ -98,7 +98,7 @@ void pki_multi::fload(const QString fname) FILE * fp; BIO *bio = NULL; - fp = fopen(QString2filename(fname), "r"); + fp = fopen_read(fname); if (!fp) { fopen_error(fname); return; @@ -151,6 +151,8 @@ void pki_multi::fromPEM_BIO(BIO *bio, QString name) item = NULL; } } + if (multi.size() == 0) + throw errorEx(tr("No known PEM encoded items found")); } void pki_multi::probeAnything(const QString fname) diff --git a/lib/pki_pkcs12.cpp b/lib/pki_pkcs12.cpp index 743dd708..d06a9624 100644 --- a/lib/pki_pkcs12.cpp +++ b/lib/pki_pkcs12.cpp @@ -36,7 +36,7 @@ pki_pkcs12::pki_pkcs12(const QString fname) class_name="pki_pkcs12"; certstack = sk_X509_new_null(); pass_info p(XCA_TITLE, tr("Please enter the password to decrypt the PKCS#12 file:\n%1").arg(compressFilename(fname))); - fp = fopen(QString2filename(fname), "rb"); + fp = fopen_read(fname); if (fp) { PKCS12 *pkcs12 = d2i_PKCS12_fp(fp, NULL); fclose(fp); @@ -114,7 +114,7 @@ void pki_pkcs12::writePKCS12(const QString fname) my_error(tr("No key or no Cert and no pkcs12")); } - FILE *fp = fopen(QString2filename(fname), "wb"); + FILE *fp = fopen_write(fname); if (fp != NULL) { if (PwDialog::execute(&p, &pass, true) != 1) { fclose(fp); diff --git a/lib/pki_pkcs7.cpp b/lib/pki_pkcs7.cpp index 1fa09c06..5470a999 100644 --- a/lib/pki_pkcs7.cpp +++ b/lib/pki_pkcs7.cpp @@ -100,7 +100,7 @@ void pki_pkcs7::signCert(pki_x509 *crt, pki_x509 *contCert) void pki_pkcs7::writeP7(QString fname,bool PEM) { FILE *fp; - fp = fopen(QString2filename(fname),"w"); + fp = fopen_write(fname); if (fp != NULL) { if (p7){ if (PEM) @@ -145,7 +145,7 @@ void pki_pkcs7::fload(const QString fname) { FILE *fp; PKCS7 *_p7; - fp = fopen(QString2filename(fname), "rb"); + fp = fopen_read(fname); if (fp) { _p7 = PEM_read_PKCS7(fp, NULL, NULL, NULL); if (!_p7) { diff --git a/lib/pki_temp.cpp b/lib/pki_temp.cpp index 59d83046..b9157d7a 100644 --- a/lib/pki_temp.cpp +++ b/lib/pki_temp.cpp @@ -342,7 +342,7 @@ QByteArray pki_temp::toExportData() void pki_temp::writeTemp(QString fname) { - FILE *fp = fopen(QString2filename(fname),"wb"); + FILE *fp = fopen_write(fname); if (fp == NULL) { fopen_error(fname); @@ -362,8 +362,13 @@ BIO *pki_temp::pem(BIO *b, int format) QByteArray ba = toExportData(); if (!b) b = BIO_new(BIO_s_mem()); +#if OPENSSL_VERSION_NUMBER < 0x10000000L + PEM_write_bio(b, PEM_STRING_XCA_TEMPLATE, (char*)"", + (unsigned char*)(ba.data()), ba.size()); +#else PEM_write_bio(b, PEM_STRING_XCA_TEMPLATE, "", - (unsigned char*)ba.data(), ba.size()); + (const unsigned char*)(ba.constData()), ba.size()); +#endif pki_openssl_error(); return b; } diff --git a/lib/pki_x509.cpp b/lib/pki_x509.cpp index b8739c2e..eec8dbba 100644 --- a/lib/pki_x509.cpp +++ b/lib/pki_x509.cpp @@ -93,7 +93,7 @@ void pki_x509::fromPEM_BIO(BIO *bio, QString name) void pki_x509::fload(const QString fname) { - FILE *fp = fopen(QString2filename(fname), "r"); + FILE *fp = fopen_read(fname); X509 *_cert; if (!fp) { fopen_error(fname); @@ -566,9 +566,9 @@ void pki_x509::writeDefault(const QString fname) void pki_x509::writeCert(const QString fname, bool PEM, bool append) { FILE *fp; - const char *p = "w"; + const char *p = "wb"; if (append) - p = "a"; + p = "ab"; fp = fopen(QString2filename(fname), p); if (fp != NULL) { if (cert){ diff --git a/lib/pki_x509req.cpp b/lib/pki_x509req.cpp index 6f4be2e8..f588dacd 100644 --- a/lib/pki_x509req.cpp +++ b/lib/pki_x509req.cpp @@ -113,7 +113,7 @@ void pki_x509req::fromPEM_BIO(BIO *bio, QString name) void pki_x509req::fload(const QString fname) { - FILE *fp = fopen(QString2filename(fname), "r"); + FILE *fp = fopen_read(fname); X509_REQ *_req; int ret = 0; @@ -241,7 +241,7 @@ void pki_x509req::writeDefault(const QString fname) void pki_x509req::writeReq(const QString fname, bool pem) { - FILE *fp = fopen(QString2filename(fname), "w"); + FILE *fp = fopen_write(fname); if (fp) { if (request){ if (pem) diff --git a/lib/pki_x509super.cpp b/lib/pki_x509super.cpp index 7131c6a0..092583dc 100644 --- a/lib/pki_x509super.cpp +++ b/lib/pki_x509super.cpp @@ -127,7 +127,7 @@ void pki_x509super::opensslConf(QString fname) "%2").arg(name).arg(extensions). arg(ASN1_STRING_get_default_mask(), 0, 16); - FILE *fp = fopen(QString2filename(fname),"w"); + FILE *fp = fopen_write(fname); if (fp == NULL) { fopen_error(fname); return; diff --git a/widgets/MW_database.cpp b/widgets/MW_database.cpp index 879c3452..3763f629 100644 --- a/widgets/MW_database.cpp +++ b/widgets/MW_database.cpp @@ -243,8 +243,8 @@ int MainWindow::open_default_db() { if (!dbfile.isEmpty()) return 0; - FILE *fp = fopen(QString2filename(getUserSettingsDir() + - QDir::separator() + "defaultdb"), "r"); + FILE *fp = fopen_read(getUserSettingsDir() + + QDir::separator() + "defaultdb"); if (!fp) return 0; @@ -273,7 +273,7 @@ void MainWindow::default_database() } d.mkpath(dir); - fp = fopen(QString2filename(file), "w"); + fp = fopen_write(file); if (fp) { QByteArray ba; ba = filename2bytearray(fi.canonicalFilePath() + "\n"); diff --git a/widgets/MainWindow.cpp b/widgets/MainWindow.cpp index 616cef30..b027b3ee 100644 --- a/widgets/MainWindow.cpp +++ b/widgets/MainWindow.cpp @@ -879,7 +879,7 @@ void MainWindow::generateDHparam() fname, "All files ( * )", NULL); if (fname == "") throw errorEx(""); - fp = fopen(QString2filename(fname), "w"); + fp = fopen_write(fname); if (fp == NULL) { throw errorEx(tr("Error opening file: '%1': %2"). arg(fname).arg(strerror(errno)));