new option --debug to enable debugging and debug window in info; refs #153

This commit is contained in:
Marc Wäckerlin
2012-05-09 14:26:55 +00:00
parent 40a926571c
commit ded89e5c43
29 changed files with 1089 additions and 560 deletions

View File

@@ -1,6 +1,7 @@
#ifndef SMARTCARDAUTH_H
#define SMARTCARDAUTH_H
#include <qbrowserlib/log.hxx>
#include <QtCore/QMutex>
#include <QtCore/QWaitCondition>
#include <QtNetwork/QSslSocket>
@@ -18,10 +19,6 @@
#include <memory>
#ifndef LOG
#define LOG qDebug()<<__PRETTY_FUNCTION__
#endif
class CryptokiEngine: public QObject, public openssl::Engine {
Q_OBJECT;
@@ -34,11 +31,11 @@ class CryptokiEngine: public QObject, public openssl::Engine {
CryptokiEngine(std::string lib):
_cryptoki(lib) {
LOG;
TRC;
}
operator bool() {
LOG<<"Status of CryptokiEngine: "
TRC; LOG<<"Status of CryptokiEngine: "
<<(_privateKey.get()
?"privateKey defined, ":"privateKey undefined");
return _privateKey.get();
@@ -49,7 +46,7 @@ class CryptokiEngine: public QObject, public openssl::Engine {
}
void cert(cryptoki::Object& privateKey, const std::string& certVal) {
LOG;
TRC;
_privateKey = std::auto_ptr<cryptoki::Object>
(new cryptoki::Object(privateKey));
try { // new
@@ -101,6 +98,7 @@ class CryptokiEngine: public QObject, public openssl::Engine {
protected:
void set(BIGNUM*& num, cryptoki::Object& key, int type, std::string name) {
TRC;
try {
std::string value(key.attribute(type).value);
num = BN_bin2bn((const unsigned char*)value.data(),
@@ -111,17 +109,17 @@ class CryptokiEngine: public QObject, public openssl::Engine {
}
virtual const char* id() {
LOG;
TRC;
return "CryptokiEngine_ID";
}
virtual const char* name() {
LOG;
TRC;
return "CryptokiEngine_NAME";
}
virtual std::string rsaSign(const std::string& in, unsigned int type) {
LOG<<"log; type="<<type<<"; size="<<in.size();
TRC; LOG<<"log; type="<<type<<"; size="<<in.size();
LOG<<crypto::readable(in).c_str();
if (type != NID_md5_sha1) throw std::runtime_error("wrong sign type");
if (in.size() != 36) throw std::runtime_error("wrong msg size to sign");
@@ -148,7 +146,7 @@ class SmartCardAuth: public QObject {
SmartCardAuth(const QString& lib, QWidget* p=0, bool loginAtStart=true):
_parent(p), _e(new CryptokiEngine(lib.toStdString())), _reg(_e) {
LOG;
TRC;
if (loginAtStart) login();
assert(connect(_e, SIGNAL(certRequired()), SLOT(login())));
}
@@ -156,8 +154,8 @@ class SmartCardAuth: public QObject {
public Q_SLOTS:
void login(bool force=true) {
TRC;
try {
LOG;
Lock lock;
LOG<<"got lock";
if (!_e || (!force && *_e)) return; // no smartcard or already logged in
@@ -252,42 +250,44 @@ class SmartCardAuth: public QObject {
private:
int retries(const std::string& name) try {
LOG<<name.c_str();
pcsc::Connection pcsc;
pcsc::Connection::Reader& reader(pcsc.reader(name));
int retries(const std::string& name) {
TRC; LOG<<name.c_str();
try {
pcsc::Connection pcsc;
pcsc::Connection::Reader& reader(pcsc.reader(name));
#ifndef Q_OS_MAC
pcsc::Connection::Reader::Transaction lock(reader);
pcsc::Connection::Reader::Transaction lock(reader);
#endif
// first try to read version info
if (reader.transmit(0x00, 0xA4, 0x08, 0x0C, "\x3f\x00\x56\x49", 4)
!= std::string("\x90\x00", 2) || !reader) {
LOG<<"Select File failed";
// first try to read version info
if (reader.transmit(0x00, 0xA4, 0x08, 0x0C, "\x3f\x00\x56\x49", 4)
!= std::string("\x90\x00", 2) || !reader) {
LOG<<"Select File failed";
return -2;
}
std::string res(reader.transmit(0x00, 0xB0, 0x00, 0x00));
if (res.substr(res.size()-2)!=std::string("\x90\x00", 2)) {
LOG<<"read error";
return -2;
}
LOG<<"version text is: "<<res.substr(4, res[3]).c_str();
// if (res.substr(4, res[3]) != "RAPost 2009" &&
// res.substr(4, res[3]) != "RAPost 2010") {
// LOG<<"unsupported card";
// return -2;
// }
if (retCode(reader.transmit(0x00, 0xA4, 0x00, 0x0C)) == 0x9000) {
int value(retCode(reader.transmit(0x00, 0x20, 0x00, 0x81)));
if ((value&0x63C0)==0x63C0) return value&0x0F;
} else {
LOG<<"**** ERROR in select MF while reading pin status";
}
return -1; // locked
} catch (const std::exception& x) {
LOG<<"**** ERROR while reading pin status: "<<x.what();
return -2;
}
std::string res(reader.transmit(0x00, 0xB0, 0x00, 0x00));
if (res.substr(res.size()-2)!=std::string("\x90\x00", 2)) {
LOG<<"read error";
return -2;
}
LOG<<"version text is: "<<res.substr(4, res[3]).c_str();
// if (res.substr(4, res[3]) != "RAPost 2009" &&
// res.substr(4, res[3]) != "RAPost 2010") {
// LOG<<"unsupported card";
// return -2;
// }
if (retCode(reader.transmit(0x00, 0xA4, 0x00, 0x0C)) == 0x9000) {
int value(retCode(reader.transmit(0x00, 0x20, 0x00, 0x81)));
if ((value&0x63C0)==0x63C0) return value&0x0F;
} else {
LOG<<"**** ERROR in select MF while reading pin status";
}
return -1; // locked
} catch (const std::exception& x) {
LOG<<"**** ERROR while reading pin status: "<<x.what();
return -2;
}
int retCode(const std::string& res) {
if (res.size()>=2)
return ((((unsigned int)(unsigned char)res[res.size()-2])*256)
@@ -311,12 +311,12 @@ class SmartCardAuth: public QObject {
class Lock {
public:
Lock() {
LOG<<loops().size();
TRC; LOG<<loops().size();
loops().append(new QEventLoop); // add to queue
if (loops().size()>1) loops().back()->exec(); // wait
}
~Lock() {
LOG<<loops().size();
TRC; LOG<<loops().size();
delete loops().front(); // mine is the first;
loops().erase(loops().begin()); // mine is the first;
if (loops().begin()!=loops().end())