From c7dc9590849a8e836b0ea1fd359e0bab6a72300a Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Wed, 11 Nov 2009 09:25:59 +0100 Subject: [PATCH] add a load button - when changing the PKCS#11 library path in the options dialog a load button tries to load the lib and informs about failure or success --- lib/pkcs11.cpp | 3 +++ lib/pki_scard.cpp | 7 +++++-- ui/Options.ui | 13 ++++++++++--- widgets/MW_menu.cpp | 15 +++++---------- widgets/Options.cpp | 27 ++++++++++++++++++++++++++- widgets/Options.h | 4 +++- 6 files changed, 52 insertions(+), 17 deletions(-) diff --git a/lib/pkcs11.cpp b/lib/pkcs11.cpp index 05f7d137..0dec1cca 100644 --- a/lib/pkcs11.cpp +++ b/lib/pkcs11.cpp @@ -43,6 +43,9 @@ CK_SLOT_ID *pkcs11::getSlotList(unsigned long *num_slots) CK_RV rv; CK_SLOT_ID *p11_slots = NULL; + *num_slots = 0; + /* This one helps to avoid errors. + * Fist time it fails, 2nd time it works */ p11->C_GetSlotList(CK_TRUE, p11_slots, num_slots); do { rv = p11->C_GetSlotList(CK_TRUE, p11_slots, num_slots); diff --git a/lib/pki_scard.cpp b/lib/pki_scard.cpp index 0fad0f97..74d2c1f8 100644 --- a/lib/pki_scard.cpp +++ b/lib/pki_scard.cpp @@ -24,7 +24,6 @@ #include "db_base.h" #include "pkcs11.h" - #if defined(_WIN32) || defined(USE_CYGWIN) #define PKCS11_DEFAULT_MODULE_NAME "opensc-pkcs11.dll" #define ENGINE_LIB "engine_pkcs11.dll" @@ -75,8 +74,10 @@ bool pki_scard::init_p11engine(QString file, bool silent) XCA_ENGINE_cmd(e, "MODULE_PATH", CCHAR(file)); ENGINE_init(e); + if (ERR_peek_error() != 0) + return false; p11_engine = e; - return 1; + return true; } void pki_scard::init(void) @@ -173,6 +174,8 @@ int pki_scard::prepare_card() const return -1; while (1) { p11_slots = p11.getSlotList(&num_slots); + if (p11_slots) + free(p11_slots); for (i=0; i 0 0 - 437 - 455 + 494 + 398 @@ -151,7 +151,7 @@ p, li { white-space: pre-wrap; } - PKCS11 Library path: + PKCS#11 path: @@ -178,6 +178,13 @@ p, li { white-space: pre-wrap; } + + + + Load + + + diff --git a/widgets/MW_menu.cpp b/widgets/MW_menu.cpp index e07169bd..e007f54c 100644 --- a/widgets/MW_menu.cpp +++ b/widgets/MW_menu.cpp @@ -169,20 +169,15 @@ void MainWindow::setOptions() string_opt.length()+1, 1, setting,"string_opt"); } QString newpath = opt->pkcs11path->text(); - QString old_pkcs11path = pkcs11path; - if (newpath != pkcs11path || !pkcs11::loaded()) { + if (newpath != pkcs11path) { + pkcs11path = newpath; + mydb.set((const unsigned char *) CCHAR(pkcs11path), + pkcs11path.length()+1, 1,setting, "pkcs11path"); try { - if (newpath.isEmpty()) - pkcs11path = newpath; - if (pki_scard::init_p11engine(newpath, false)) - pkcs11path = newpath; + pki_scard::init_p11engine(pkcs11path, true); } catch (errorEx &err) { Error(err); } - if (pkcs11path != old_pkcs11path) { - mydb.set((const unsigned char *) CCHAR(pkcs11path), - pkcs11path.length()+1, 1,setting, "pkcs11path"); - } } scardMenuAction->setEnabled(pkcs11::loaded()); } diff --git a/widgets/Options.cpp b/widgets/Options.cpp index 3569193a..8df7e066 100644 --- a/widgets/Options.cpp +++ b/widgets/Options.cpp @@ -6,11 +6,14 @@ */ #include "Options.h" +#include "lib/pki_scard.h" #include +#include -Options::Options(QWidget *parent) +Options::Options(MainWindow *parent) :QDialog(parent) { + mw = parent; QStringList dnl; if (!MainWindow::mandatory_dn.isEmpty()) dnl = MainWindow::mandatory_dn.split(","); @@ -82,3 +85,25 @@ void Options::on_fileButton_clicked(void) pkcs11path->setText(fname); } + +void Options::on_tryLoadButton_clicked(void) +{ + unsigned long num_slots; + CK_SLOT_ID *p11_slots = NULL; + + try { + QString lib = pkcs11path->text(); + pki_scard::init_p11engine(lib, false); + pkcs11 p11; + p11_slots = p11.getSlotList(&num_slots); + if (!lib.isEmpty()) { + QMessageBox::information(this, XCA_TITLE, + tr("Successfully loaded PKCS#11 library: ") + lib, + tr("Ok")); + } + } catch (errorEx &err) { + mw->Error(err); + } + if (p11_slots) + free(p11_slots); +} diff --git a/widgets/Options.h b/widgets/Options.h index 89f40d90..cd6259c9 100644 --- a/widgets/Options.h +++ b/widgets/Options.h @@ -18,8 +18,9 @@ class Options: public QDialog, public Ui::Options Q_OBJECT private: QStringList string_opts; + MainWindow *mw; public: - Options(QWidget *parent); + Options(MainWindow *parent); public slots: void on_extDNadd_clicked(); void on_extDNdel_clicked(); @@ -27,6 +28,7 @@ class Options: public QDialog, public Ui::Options void setStringOpt(const QString string_opt); QString getStringOpt(); void on_fileButton_clicked(void); + void on_tryLoadButton_clicked(void); }; #endif