From 500f11c9b3a8b7bf0d98f4585dca28d05c94fe5c Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Sat, 4 Apr 2020 22:47:44 +0200 Subject: [PATCH] Get rid of filename2bytearray and QString2filename We now use QFile or its derivate XFile, who smoothly handle unicode filenames also on windows. The lt_dlopen() only handles "char *" not wchar_t. Try to convert the filename with all known codecs until we can open it. filename2QString() remains to differently encode filenames provided on the commandline on Windows. --- lib/func.cpp | 9 --------- lib/func.h | 3 --- lib/pkcs11_lib.cpp | 44 ++++++++++++++++++++++++++++++++--------- lib/pki_pkcs7.cpp | 19 +++++------------- lib/pki_pkcs7.h | 6 +++--- widgets/MW_database.cpp | 4 ++-- 6 files changed, 45 insertions(+), 40 deletions(-) diff --git a/lib/func.cpp b/lib/func.cpp index 5a41625e..6ce5ff1b 100644 --- a/lib/func.cpp +++ b/lib/func.cpp @@ -301,15 +301,6 @@ QString getFullFilename(const QString & filename, const QString & selectedFilter return rv; } -QByteArray filename2bytearray(const QString &fname) -{ -#if defined(Q_OS_WIN32) - return fname.toLocal8Bit(); -#else - return fname.toUtf8(); -#endif -} - QString filename2QString(const char *fname) { #if defined(Q_OS_WIN32) diff --git a/lib/func.h b/lib/func.h index 74fe3cbf..55df646e 100644 --- a/lib/func.h +++ b/lib/func.h @@ -37,7 +37,6 @@ const QStringList getLibExtensions(); QString hostId(); QString formatHash(const QByteArray &data, QString sep = ":", int width = 2); -QByteArray filename2bytearray(const QString &fname); QString filename2QString(const char *fname); QString compressFilename(QString filename, int maxlen = 50); @@ -69,8 +68,6 @@ void *d2i_bytearray(void *(*d2i)(void*, unsigned char**, long), #define I2D_VOID(a) ((int (*)(const void *, unsigned char **))(a)) #define D2I_VOID(a) ((void *(*)(void *, unsigned char **, long))(a)) -#define QString2filename(str) filename2bytearray(str).constData() - static inline QString htmlEscape(const QString &html) { #if QT_VERSION < 0x050000 diff --git a/lib/pkcs11_lib.cpp b/lib/pkcs11_lib.cpp index 6e034427..c0bbd981 100644 --- a/lib/pkcs11_lib.cpp +++ b/lib/pkcs11_lib.cpp @@ -14,9 +14,38 @@ #include #include +#include #include #include "ui_SelectToken.h" +QByteArray find_filecodec(const QString &file) +{ +#if defined(Q_OS_WIN32) + QList codecs = QTextCodec::availableCodecs(); + QString fil = nativeSeparator(file); + + foreach(QByteArray codec, QTextCodec::availableCodecs()) + { + auto tc = QTextCodec::codecForName(codec); + bool can = tc->canEncode(file); + int fd = -1; + QByteArray fn; + if (can) { + fn = tc->fromUnicode(fil); + fd = open(fn, O_RDONLY); + if (fd != -1) + close(fd); + } + qDebug() << "TestCodec" << codec << can << fn << fd; + if (fd != -1) + return fn; + } + return fil.toLocal8Bit(); +#else + return file.toUtf8(); +#endif +} + pkcs11_lib::pkcs11_lib(const QString &f) { CK_RV (*c_get_function_list)(CK_FUNCTION_LIST_PTR_PTR); @@ -30,7 +59,7 @@ pkcs11_lib::pkcs11_lib(const QString &f) lt_dlinit(); try { - dl_handle = lt_dlopen(QString2filename(file)); + dl_handle = lt_dlopen(find_filecodec(file)); if (dl_handle == NULL) throw errorEx(QObject::tr("Failed to open PKCS11 library: %1: %2").arg(file).arg(lt_dlerror())); @@ -40,8 +69,7 @@ pkcs11_lib::pkcs11_lib(const QString &f) if (!c_get_function_list) throw errorEx(QObject::tr("This does not look like a PKCS#11 library. Symbol 'C_GetFunctionList' not found.")); - qDebug("Trying to load PKCS#11 provider %s", - QString2filename(file)); + qDebug() << "Trying to load PKCS#11 provider" << file; rv = c_get_function_list(&p11); if (rv != CKR_OK) pk11error("C_GetFunctionList", rv); @@ -50,8 +78,7 @@ pkcs11_lib::pkcs11_lib(const QString &f) if (rv != CKR_OK && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) pk11error("C_Initialize", rv); - qDebug("Successfully loaded PKCS#11 provider %s", - QString2filename(file)); + qDebug() << "Successfully loaded PKCS#11 provider" << file; } catch (errorEx &err) { load_error = err.getString(); WAITCURSOR_END; @@ -60,8 +87,7 @@ pkcs11_lib::pkcs11_lib(const QString &f) if (dl_handle) lt_dlclose(dl_handle); lt_dlexit(); - qDebug("Failed to load PKCS#11 provider %s", - QString2filename(file)); + qDebug() << "Failed to load PKCS#11 provider" << file; } } @@ -71,11 +97,11 @@ pkcs11_lib::~pkcs11_lib() (void)rv; if (!isLoaded()) return; - qDebug("Unloading PKCS#11 provider %s", QString2filename(file)); + qDebug() << "Unloading PKCS#11 provider" << file; CALL_P11_C(this, C_Finalize, NULL); lt_dlclose(dl_handle); lt_dlexit(); - qDebug("Unloaded PKCS#11 provider %s", QString2filename(file)); + qDebug() << "Unloaded PKCS#11 provider" << file; } QList pkcs11_lib::getSlotList() diff --git a/lib/pki_pkcs7.cpp b/lib/pki_pkcs7.cpp index e70e5af6..0c2aa0fc 100644 --- a/lib/pki_pkcs7.cpp +++ b/lib/pki_pkcs7.cpp @@ -25,13 +25,9 @@ pki_pkcs7::~pki_pkcs7() PKCS7_free(p7); } -void pki_pkcs7::encryptFile(pki_x509 *crt, QString filename) +void pki_pkcs7::encryptFile(pki_x509 *crt, const QString &filename) { - BIO *bio = NULL; - bio = BIO_new_file(QString2filename(filename), "r"); - openssl_error(); - encryptBio(crt, bio); - BIO_free(bio); + encryptBio(crt, XFile(filename).bio()); } void pki_pkcs7::encryptBio(pki_x509 *crt, BIO *bio) @@ -81,15 +77,10 @@ void pki_pkcs7::signBio(pki_x509 *crt, BIO *bio) sk_X509_free(certstack); } -void pki_pkcs7::signFile(pki_x509 *crt, QString filename) +void pki_pkcs7::signFile(pki_x509 *crt, const QString &filename) { - BIO *bio; - if (!crt) - return; - bio = BIO_new_file(QString2filename(filename), "r"); - openssl_error(); - signBio(crt, bio); - BIO_free(bio); + if (crt) + signBio(crt, XFile(filename).bio()); } void pki_pkcs7::signCert(pki_x509 *crt, pki_x509 *contCert) diff --git a/lib/pki_pkcs7.h b/lib/pki_pkcs7.h index 113a0617..c8e6610f 100644 --- a/lib/pki_pkcs7.h +++ b/lib/pki_pkcs7.h @@ -23,15 +23,15 @@ class pki_pkcs7: public pki_base pki_pkcs7(const QString name = ""); virtual ~pki_pkcs7(); - void signFile(pki_x509 *crt, QString filename); + void signFile(pki_x509 *crt, const QString &filename); void signCert(pki_x509 *crt, pki_x509 *contCert); - void encryptFile(pki_x509 *crt, QString filename); + void encryptFile(pki_x509 *crt, const QString &filename); void writeP7(XFile &file, bool PEM) const; void fromPEM_BIO(BIO *bio, const QString &name); void fload(const QString &fname); pki_x509 *getCert(int x); void addCert(pki_x509 *crt); - int numCert(); // number of certs; + int numCert(); }; diff --git a/widgets/MW_database.cpp b/widgets/MW_database.cpp index bb01a40b..142e46c3 100644 --- a/widgets/MW_database.cpp +++ b/widgets/MW_database.cpp @@ -246,7 +246,7 @@ int MainWindow::init_database(QString dbName) QSqlError err; QString oldDbFile; - qDebug("Opening database: %s", QString2filename(dbName)); + qDebug() << "Opening database:" << dbName; if (checkForOldDbFormat(dbName)) { QString newname = dbName; @@ -510,7 +510,7 @@ void MainWindow::close_database() return; } killTimer(dbTimer); - qDebug("Closing database: %s", QString2filename(currentDB)); + qDebug() << "Closing database:" << currentDB; Settings["mw_geometry"] = QString("%1,%2,%3") .arg(size().width()) .arg(size().height())