improve PKCS#11 lib loading for Windows

This commit is contained in:
Christian Hohnstaedt 2010-03-12 08:17:25 +01:00
parent e316ced8c8
commit cceec6a700
4 changed files with 32 additions and 6 deletions

View File

@ -33,6 +33,20 @@ pkcs11::~pkcs11()
p11->C_CloseSession(session);
}
void pkcs11::initialize()
{
CK_RV rv = p11->C_Initialize(NULL);
if (rv != CKR_OK && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)
pk11error("C_Initialize", rv);
}
void pkcs11::finalize()
{
CK_RV rv = p11->C_Finalize(NULL);
if (rv != CKR_OK && rv != CKR_CRYPTOKI_NOT_INITIALIZED)
pk11error("C_Finalize", rv);
}
void pkcs11::startSession(unsigned long slot, bool rw)
{
CK_RV rv;
@ -526,7 +540,7 @@ bool pkcs11::load_lib(QString file, bool silent)
if (lt_dlclose(dl_handle) < 0) {
if (silent)
return false;
throw errorEx("Failed to close PKCS11 library: " + file);
throw errorEx(QObject::tr("Failed to close PKCS11 library: %1").arg(file));
}
}
p11 = NULL;
@ -534,14 +548,15 @@ bool pkcs11::load_lib(QString file, bool silent)
if (file.isEmpty()) {
if (silent)
return false;
throw errorEx("PKCS11 library filename empty");
throw errorEx(QObject::tr("PKCS11 library filename empty"));
}
dl_handle = lt_dlopen(QString2filename(file));
if (dl_handle == NULL) {
if (silent)
return false;
throw errorEx("Failed to open PKCS11 library: " + file);
throw errorEx(QObject::tr("Failed to open PKCS11 library: %1").
arg(file));
}
/* Get the list of function pointers */
@ -554,7 +569,8 @@ bool pkcs11::load_lib(QString file, bool silent)
/* This state is always worth an error ! */
if (lt_dlclose(dl_handle) == 0)
dl_handle = NULL;
throw errorEx("Failed to open PKCS11 library: " + file);
throw errorEx(QObject::tr("Failed to open PKCS11 library: %1").
arg(file));
return false;
}

View File

@ -103,6 +103,8 @@ class pkcs11
pkcs11();
~pkcs11();
static bool load_lib(QString file, bool silent);
static void initialize();
static void finalize();
tkInfo tokenInfo(CK_SLOT_ID slot);
tkInfo tokenInfo();

View File

@ -160,9 +160,11 @@ void MainWindow::setOptions()
opt->setStringOpt(string_opt);
opt->pkcs11path->setText(pkcs11path);
if (!opt->exec())
if (!opt->exec()) {
delete opt;
enableTokenMenu(pkcs11::loaded());
return;
}
QString alg = opt->hashAlgo->currentHashName();
db mydb(dbfile);
mydb.set((const unsigned char *)CCHAR(alg), alg.length()+1, 1,
@ -195,4 +197,5 @@ void MainWindow::setOptions()
#endif
}
enableTokenMenu(pkcs11::loaded());
delete opt;
}

View File

@ -92,6 +92,7 @@ void Options::on_tryLoadButton_clicked(void)
QString lib = pkcs11path->text();
#ifdef WIN32
pkcs11::load_lib(lib, false);
pkcs11::initialize();
#else
pki_scard::init_p11engine(lib, false);
#endif
@ -100,6 +101,10 @@ void Options::on_tryLoadButton_clicked(void)
pkcs11 p11;
info = p11.driverInfo();
}
#ifdef WIN32
pkcs11::finalize();
pkcs11::load_lib(QString(), true);
#endif
if (!lib.isEmpty()) {
QMessageBox::information(this, XCA_TITLE,
tr("Successfully loaded PKCS#11 library: %1\n").