You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
343 lines
12 KiB
343 lines
12 KiB
14 years ago
|
#ifndef SMARTCARDAUTH_H
|
||
|
#define SMARTCARDAUTH_H
|
||
|
|
||
|
#include <QtCore/QMutex>
|
||
14 years ago
|
#include <QtCore/QWaitCondition>
|
||
14 years ago
|
#include <QtNetwork/QSslSocket>
|
||
14 years ago
|
#include <QtNetwork/QSslConfiguration>
|
||
|
#include <QtNetwork/QSslCertificate>
|
||
14 years ago
|
#include <QtNetwork/QSslKey>
|
||
14 years ago
|
#include <QtGui/QMessageBox>
|
||
14 years ago
|
|
||
14 years ago
|
#include <pinentry.hxx>
|
||
|
|
||
|
#include <cryptoki.hxx>
|
||
14 years ago
|
#include <pcsc.hxx>
|
||
14 years ago
|
#include <openssl-engine.hxx>
|
||
|
#include <openssl.hxx>
|
||
|
|
||
|
#include <memory>
|
||
|
|
||
13 years ago
|
#ifndef LOG
|
||
|
#define LOG qDebug()<<__PRETTY_FUNCTION__
|
||
|
#endif
|
||
|
|
||
14 years ago
|
class CryptokiEngine: public QObject, public openssl::Engine {
|
||
|
|
||
|
Q_OBJECT;
|
||
|
|
||
|
Q_SIGNALS:
|
||
|
|
||
|
void certRequired();
|
||
|
|
||
14 years ago
|
public:
|
||
14 years ago
|
|
||
14 years ago
|
CryptokiEngine(std::string lib):
|
||
|
_cryptoki(lib) {
|
||
13 years ago
|
LOG;
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
operator bool() {
|
||
13 years ago
|
LOG<<"Status of CryptokiEngine: "
|
||
|
<<(_privateKey.get()
|
||
|
?"privateKey defined, ":"privateKey undefined");
|
||
14 years ago
|
return _privateKey.get();
|
||
14 years ago
|
}
|
||
|
|
||
|
cryptoki::Init& cryptoki() {
|
||
|
return _cryptoki;
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
void cert(cryptoki::Object& privateKey, const std::string& certVal) {
|
||
13 years ago
|
LOG;
|
||
14 years ago
|
_privateKey = std::auto_ptr<cryptoki::Object>
|
||
|
(new cryptoki::Object(privateKey));
|
||
14 years ago
|
try { // new
|
||
|
QSslConfiguration sslConfig(QSslConfiguration::defaultConfiguration());
|
||
|
QSslCertificate localcert(QByteArray(certVal.data(),
|
||
|
certVal.size()),
|
||
|
QSsl::Der);
|
||
|
sslConfig.setLocalCertificate(localcert);
|
||
|
assert(localcert.isValid());
|
||
|
|
||
14 years ago
|
//RSA_set_default_method(ENGINE_get_RSA(_e));
|
||
14 years ago
|
QByteArray pem // empty dummy key for qt object instantiation
|
||
|
("-----BEGIN RSA PRIVATE KEY-----\n"
|
||
14 years ago
|
"MIIBOwIBAAJBAMH2yqAGeVNPdgeZ2GoHo31m9aUxZ7QfK2Go2qLTahLpQ3UL1C8G\n"
|
||
|
"LkuMS8SNK0ZGfRMalIpIhv6bW5l3kjogOncCAwEAAQJABVGECtFCoGMsZFb2lSmy\n"
|
||
|
"dOzOzYHGSy0TnnDn1dEgNnZ8sIljElPtUzm9dyXs2P3ICL1sOd7qjpzfJeyxknDL\n"
|
||
|
"AQIhAO5iKdLmhyuW+EDEH19vDs1Pmqs3/ZnT5UgUiJnTJqz3AiEA0ExIfUOCnxq2\n"
|
||
|
"a3Z46KEivcr8JB2P9VqouBbVryiq/oECIQDj8bPCejMoiEzMSX0iWWTTB9qC/KAg\n"
|
||
|
"FtF4skHIrXKfEwIgPCs86Uo+Ch2aQjKHvJMHSRHAgeI0OmiEwiB+e0lhE4ECIQDd\n"
|
||
|
"IbUmHIXt6oHLJmoGFX46bCcfil5eE5FXfiaw7Q9iPw==\n"
|
||
14 years ago
|
"-----END RSA PRIVATE KEY-----\n");
|
||
|
QSslKey privkey(pem, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
|
||
14 years ago
|
RSA* rsa((RSA*)privkey.handle());
|
||
|
if (!ENGINE_init(_e)) return;
|
||
|
rsa->engine=_e;
|
||
|
rsa->meth=ENGINE_get_RSA(_e);
|
||
|
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, rsa, &rsa->ex_data)) {
|
||
|
ENGINE_finish(_e);
|
||
|
return;
|
||
|
}
|
||
14 years ago
|
set(rsa->n, privateKey, CKA_MODULUS, "CKA_MODULUS");
|
||
|
set(rsa->e, privateKey, CKA_PUBLIC_EXPONENT, "CKA_PUBLIC_EXPONENT");
|
||
|
set(rsa->d, privateKey, CKA_PRIVATE_EXPONENT, "CKA_PRIVATE_EXPONENT");
|
||
|
set(rsa->p, privateKey, CKA_PRIME_1, "CKA_PRIME_1");
|
||
|
set(rsa->q, privateKey, CKA_PRIME_2, "CKA_PRIME_2");
|
||
|
set(rsa->dmp1, privateKey, CKA_EXPONENT_1, "CKA_EXPONENT_1");
|
||
|
set(rsa->dmq1, privateKey, CKA_EXPONENT_2, "CKA_EXPONENT_2");
|
||
|
set(rsa->iqmp, privateKey, CKA_COEFFICIENT, "CKA_COEFFICIENT");
|
||
|
rsa->flags |= RSA_FLAG_SIGN_VER; // don't emulate with encrypt/decrypt
|
||
|
assert(!privkey.isNull());
|
||
13 years ago
|
LOG<<"Setup RSA finished";
|
||
14 years ago
|
sslConfig.setPrivateKey(privkey);
|
||
|
QSslConfiguration::setDefaultConfiguration(sslConfig);
|
||
|
} catch (const std::exception& e) {
|
||
13 years ago
|
LOG<<"SETUP ERROR: "<<e.what();
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
protected:
|
||
14 years ago
|
|
||
14 years ago
|
void set(BIGNUM*& num, cryptoki::Object& key, int type, std::string name) {
|
||
|
try {
|
||
|
std::string value(key.attribute(type).value);
|
||
|
num = BN_bin2bn((const unsigned char*)value.data(),
|
||
|
value.size(), num);
|
||
|
} catch (const std::exception& x) {
|
||
13 years ago
|
LOG<<"**** ERROR: key attribute missing:"<<name.c_str()<<x.what();
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
virtual const char* id() {
|
||
13 years ago
|
LOG;
|
||
14 years ago
|
return "CryptokiEngine_ID";
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
virtual const char* name() {
|
||
13 years ago
|
LOG;
|
||
14 years ago
|
return "CryptokiEngine_NAME";
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
virtual std::string rsaSign(const std::string& in, unsigned int type) {
|
||
13 years ago
|
LOG<<"log; type="<<type<<"; size="<<in.size();
|
||
|
LOG<<crypto::readable(in).c_str();
|
||
14 years ago
|
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");
|
||
13 years ago
|
LOG<<"ready to sign";
|
||
14 years ago
|
try {
|
||
|
return _privateKey->sign(in, CKM_RSA_PKCS);
|
||
|
} catch (const std::exception& x) {
|
||
|
certRequired(); // get new certificate
|
||
|
return _privateKey->sign(in, CKM_RSA_PKCS); // try again
|
||
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
private:
|
||
14 years ago
|
|
||
14 years ago
|
cryptoki::Init _cryptoki;
|
||
14 years ago
|
std::auto_ptr<cryptoki::Object> _privateKey;
|
||
14 years ago
|
|
||
|
};
|
||
|
|
||
|
class SmartCardAuth: public QObject {
|
||
|
Q_OBJECT;
|
||
|
|
||
|
public:
|
||
|
|
||
|
SmartCardAuth(const QString& lib, QWidget* p=0, bool loginAtStart=true):
|
||
14 years ago
|
_parent(p), _e(new CryptokiEngine(lib.toStdString())), _reg(_e) {
|
||
13 years ago
|
LOG;
|
||
14 years ago
|
if (loginAtStart) login();
|
||
14 years ago
|
assert(connect(_e, SIGNAL(certRequired()), SLOT(login())));
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
public Q_SLOTS:
|
||
14 years ago
|
|
||
14 years ago
|
void login(bool force=true) {
|
||
14 years ago
|
try {
|
||
13 years ago
|
LOG;
|
||
|
Lock lock;
|
||
|
LOG<<"got lock";
|
||
|
if (!_e || (!force && *_e)) return; // no smartcard or already logged in
|
||
|
LOG<<"start login to smartcard";
|
||
14 years ago
|
QList<CertInfo> authcerts;
|
||
|
QList<CertInfo> allcerts;
|
||
|
QSslConfiguration sslConfig(QSslConfiguration::defaultConfiguration());
|
||
14 years ago
|
_slots = _e->cryptoki().slotList();
|
||
13 years ago
|
LOG<<"number of slots"<<_slots.size();
|
||
14 years ago
|
// look for login certificates ----------------------------------------
|
||
14 years ago
|
for (cryptoki::SlotList::iterator slot(_slots.begin());
|
||
|
slot!=_slots.end(); ++slot) {
|
||
13 years ago
|
cryptoki::Session session(*slot);
|
||
|
cryptoki::ObjectList certs(session.find
|
||
14 years ago
|
(cryptoki::Attribute(CKA_CLASS)
|
||
|
.from<CK_OBJECT_CLASS>(CKO_CERTIFICATE)));
|
||
|
for (cryptoki::ObjectList::iterator cert(certs.begin());
|
||
|
cert!=certs.end(); ++cert) {
|
||
|
cryptoki::Attribute label(cert->attribute(CKA_LABEL));
|
||
|
cryptoki::Attribute id(cert->attribute(CKA_ID));
|
||
13 years ago
|
LOG<<"**** FOUND CERTIFICATE: "<<label.value.c_str();
|
||
14 years ago
|
cryptoki::ObjectList keys
|
||
13 years ago
|
(session.find(cryptoki::Attribute(CKA_CLASS)
|
||
|
.from<CK_OBJECT_CLASS>(CKO_PUBLIC_KEY),
|
||
|
id));
|
||
|
LOG<<"**** with keys: "<<keys.size();
|
||
14 years ago
|
std::string data(cert->attribute(CKA_VALUE).value);
|
||
|
if (!keys.size()) { // add CA-certificate
|
||
13 years ago
|
LOG<<"**** add to CA-certificates";
|
||
14 years ago
|
} else {
|
||
13 years ago
|
LOG<<"**** user cert, check for authentictaion";
|
||
14 years ago
|
if (label.value.find("auth")==0 ||
|
||
|
label.value.find("Authentication")!=std::string::npos) {
|
||
13 years ago
|
LOG<<"**** it's our authentication cert";
|
||
14 years ago
|
authcerts.push_back(CertInfo(data, slot, id));
|
||
|
} else {
|
||
13 years ago
|
LOG<<"**** it's an unknown cert";
|
||
14 years ago
|
allcerts.push_back(CertInfo(data, slot, id));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
14 years ago
|
// get pin and install client certificate ------------------------------
|
||
13 years ago
|
LOG<<"00000000000000000000000000000000000000000000000000";
|
||
14 years ago
|
if (!authcerts.isEmpty() || !allcerts.isEmpty()) {
|
||
13 years ago
|
LOG<<"11111111111111111111111111111111111111111111111111";
|
||
14 years ago
|
CertInfo c(authcerts.size()?authcerts[0]:allcerts[0]);
|
||
|
PinEntry pinEntry(QSslCertificate(QByteArray(c.data.data(),
|
||
|
c.data.size()),
|
||
|
QSsl::Der), _parent);
|
||
13 years ago
|
while (true) try {
|
||
|
LOG<<"******************************************1*******";
|
||
|
pinEntry.retries(retries(c.slot->slotinfo().slotDescription));
|
||
|
LOG<<"******************************************2*******";
|
||
|
int res(pinEntry.myexec());
|
||
|
LOG<<"******************************************3*******";
|
||
|
if (res!=PinEntry::Accepted) return;
|
||
|
LOG<<"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
|
||
14 years ago
|
_session = // session login with pin
|
||
14 years ago
|
std::auto_ptr<cryptoki::Session>
|
||
|
(new cryptoki::Session(*c.slot));
|
||
13 years ago
|
LOG<<"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
|
||
14 years ago
|
_session->login(pinEntry.pin().toStdString());
|
||
13 years ago
|
LOG<<"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";
|
||
14 years ago
|
cryptoki::ObjectList keys
|
||
|
(_session->find(cryptoki::Attribute(CKA_CLASS)
|
||
|
.from<CK_OBJECT_CLASS>(CKO_PRIVATE_KEY),
|
||
|
c.id));
|
||
13 years ago
|
LOG<<"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
|
||
14 years ago
|
if (keys.size()==1) {
|
||
13 years ago
|
LOG<<"**** found one private key";
|
||
14 years ago
|
_e->cert(keys[0], c.data); // install client cert
|
||
13 years ago
|
LOG<<"==================================================";
|
||
14 years ago
|
break;
|
||
|
}
|
||
13 years ago
|
LOG<<"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
|
||
14 years ago
|
} catch (std::exception& x) {
|
||
|
pinEntry.pin().clear();
|
||
13 years ago
|
LOG<<"**** ERROR"<<x.what();
|
||
14 years ago
|
QMessageBox::critical(0, QMessageBox::tr("Wrong PIN"),
|
||
|
QMessageBox::tr("Authentication failed,"
|
||
|
" please try again."));
|
||
|
}
|
||
|
}
|
||
13 years ago
|
LOG<<"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
|
||
|
} catch (std::exception& x) {
|
||
|
LOG<<"**** ERROR"<<x.what();
|
||
14 years ago
|
throw;
|
||
|
}
|
||
13 years ago
|
LOG<<"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
|
private:
|
||
|
|
||
|
int retries(const std::string& name) try {
|
||
13 years ago
|
LOG<<name.c_str();
|
||
14 years ago
|
pcsc::Connection pcsc;
|
||
|
pcsc::Connection::Reader& reader(pcsc.reader(name));
|
||
|
#ifndef Q_OS_MAC
|
||
|
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)
|
||
14 years ago
|
!= std::string("\x90\x00", 2) || !reader) {
|
||
13 years ago
|
LOG<<"Select File failed";
|
||
14 years ago
|
return -2;
|
||
|
}
|
||
14 years ago
|
std::string res(reader.transmit(0x00, 0xB0, 0x00, 0x00));
|
||
14 years ago
|
if (res.substr(res.size()-2)!=std::string("\x90\x00", 2)) {
|
||
13 years ago
|
LOG<<"read error";
|
||
14 years ago
|
return -2;
|
||
|
}
|
||
13 years ago
|
LOG<<"version text is: "<<res.substr(4, res[3]).c_str();
|
||
14 years ago
|
// if (res.substr(4, res[3]) != "RAPost 2009" &&
|
||
|
// res.substr(4, res[3]) != "RAPost 2010") {
|
||
13 years ago
|
// LOG<<"unsupported card";
|
||
14 years ago
|
// return -2;
|
||
|
// }
|
||
14 years ago
|
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 {
|
||
13 years ago
|
LOG<<"**** ERROR in select MF while reading pin status";
|
||
14 years ago
|
}
|
||
|
return -1; // locked
|
||
|
} catch (const std::exception& x) {
|
||
13 years ago
|
LOG<<"**** ERROR while reading pin status: "<<x.what();
|
||
14 years ago
|
return -2;
|
||
|
}
|
||
|
|
||
|
int retCode(const std::string& res) {
|
||
|
if (res.size()>=2)
|
||
|
return ((((unsigned int)(unsigned char)res[res.size()-2])*256)
|
||
|
+((unsigned int)(unsigned char)res[res.size()-1]));
|
||
|
else
|
||
|
return -1;
|
||
|
}
|
||
14 years ago
|
|
||
|
private:
|
||
|
|
||
|
struct CertInfo {
|
||
|
CertInfo(std::string d, cryptoki::SlotList::iterator s,
|
||
|
cryptoki::Attribute i):
|
||
|
data(d), slot(s), id(i) {
|
||
|
}
|
||
|
std::string data;
|
||
|
cryptoki::SlotList::iterator slot;
|
||
|
cryptoki::Attribute id;
|
||
|
};
|
||
13 years ago
|
|
||
13 years ago
|
class Lock {
|
||
13 years ago
|
public:
|
||
13 years ago
|
Lock() {
|
||
|
LOG<<loops().size();
|
||
|
loops().append(new QEventLoop); // add to queue
|
||
|
if (loops().size()>1) loops().back()->exec(); // wait
|
||
13 years ago
|
}
|
||
13 years ago
|
~Lock() {
|
||
|
LOG<<loops().size();
|
||
|
delete loops().front(); // mine is the first;
|
||
|
loops().erase(loops().begin()); // mine is the first;
|
||
|
if (loops().begin()!=loops().end())
|
||
|
loops().front()->exit(0); // wake up next
|
||
13 years ago
|
}
|
||
|
private:
|
||
13 years ago
|
QList<QEventLoop*>& loops() { // same as a static member variable
|
||
|
static QList<QEventLoop*> _loops;
|
||
|
return _loops;
|
||
|
}
|
||
13 years ago
|
};
|
||
14 years ago
|
|
||
|
private:
|
||
|
|
||
|
QWidget* _parent;
|
||
14 years ago
|
CryptokiEngine* _e;
|
||
|
openssl::RegisterEngine<CryptokiEngine> _reg;
|
||
14 years ago
|
cryptoki::SlotList _slots;
|
||
|
std::auto_ptr<cryptoki::Session> _session;
|
||
14 years ago
|
|
||
|
};
|
||
|
|
||
|
#endif // SMARTCARDAUTH_H
|