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
This commit is contained in:
Christian Hohnstaedt 2009-11-11 09:25:59 +01:00
parent 7e0a18dba2
commit c7dc959084
6 changed files with 52 additions and 17 deletions

View File

@ -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);

View File

@ -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<num_slots; i++) {
pkcs11 myp11;
QStringList sl = myp11.tokenInfo(i);

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>437</width>
<height>455</height>
<width>494</width>
<height>398</height>
</rect>
</property>
<property name="windowTitle">
@ -151,7 +151,7 @@ p, li { white-space: pre-wrap; }
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>PKCS11 Library path: </string>
<string>PKCS#11 path: </string>
</property>
</widget>
</item>
@ -178,6 +178,13 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="tryLoadButton">
<property name="text">
<string>Load</string>
</property>
</widget>
</item>
</layout>
</item>
<item>

View File

@ -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());
}

View File

@ -6,11 +6,14 @@
*/
#include "Options.h"
#include "lib/pki_scard.h"
#include <openssl/objects.h>
#include <qmessagebox.h>
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);
}

View File

@ -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