20 #define _TDECRYPTOGRAPHICCARDDEVICE_INTERNAL 1
23 #include "tdecryptographiccarddevice_private.h"
24 #include "tdecryptographiccarddevice.h"
29 #include <tqeventloop.h>
30 #include <tqapplication.h>
32 #include "tdeglobal.h"
34 #include "tdeapplication.h"
36 #include "tdehardwaredevices.h"
41 #define PCSC_POLL_TIMEOUT_S 1000
43 #define CARD_MAX_LOGIN_RETRY_COUNT 3
49 static TQString pcsc_error_code_to_string(
long errcode) {
50 if (errcode == SCARD_W_UNPOWERED_CARD) {
51 return i18n(
"card not powered on");
53 else if (errcode == SCARD_E_PROTO_MISMATCH) {
54 return i18n(
"protocol mismatch");
57 return TQString::null;
62 CryptoCardDeviceWatcher::CryptoCardDeviceWatcher() {
64 m_readerStates = NULL;
66 m_cardPINPromptDone =
true;
67 m_pinCallbacksEnabled =
false;
68 m_cardReusePIN =
false;
71 CryptoCardDeviceWatcher::~CryptoCardDeviceWatcher() {
77 void CryptoCardDeviceWatcher::run() {
84 LPSTR lpstring_readers = NULL;
89 m_terminationRequested =
false;
91 TQEventLoop* eventLoop = TQApplication::eventLoop();
92 if (!eventLoop)
return;
94 ret = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &m_cardContext);
95 if (ret != SCARD_S_SUCCESS) {
96 printf(
"TDECryptographicCardDevice: PCSC SCardEstablishContext cannot connect to resource manager (%lX)", ret);
101 ret = SCardListReaders(m_cardContext, NULL, NULL, &dword_readers);
102 if (ret == SCARD_S_SUCCESS) {
103 lpstring_readers = (LPSTR)malloc(
sizeof(
char)*dword_readers);
104 if (lpstring_readers == NULL) {
105 printf(
"TDECryptographicCardDevice: insufficient memory, aborting");
110 ret = SCardListReaders(m_cardContext, NULL, lpstring_readers, &dword_readers);
111 if (ret == SCARD_S_SUCCESS) {
113 char *ptr = lpstring_readers;
114 while (*ptr !=
'\0') {
116 ptr += strlen(ptr)+1;
119 free(lpstring_readers);
121 m_readerStates = (SCARD_READERSTATE*)calloc(readers.count(),
sizeof(*m_readerStates));
122 if (m_readerStates == NULL) {
123 printf(
"TDECryptographicCardDevice: insufficient memory, aborting");
124 free(lpstring_readers);
129 for (i=0; i<readers.count(); i++) {
130 m_readerStates[i].szReader = strdup(readers[i].ascii());
131 m_readerStates[i].dwCurrentState = SCARD_STATE_UNAWARE;
134 ret = SCardGetStatusChange(m_cardContext, PCSC_POLL_TIMEOUT_S, m_readerStates, readers.count());
135 while ((ret == SCARD_S_SUCCESS) || (ret == SCARD_E_TIMEOUT)) {
136 if (m_terminationRequested) {
137 for (i=0; i<readers.count(); i++) {
138 free((
char*)m_readerStates[i].szReader);
139 m_readerStates[i].szReader = NULL;
145 for (i=0; i<readers.count(); i++) {
149 SCARDHANDLE hCard = 0;
150 DWORD dwActiveProtocol = 0;
152 TQString reader_vendor_name;
153 TQString reader_interface_type;
155 ret = SCardConnect(m_cardContext, readers[i].ascii(), SCARD_SHARE_DIRECT, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
156 if (ret == SCARD_S_SUCCESS) {
157 ret = SCardGetAttrib(hCard, SCARD_ATTR_VENDOR_NAME, NULL, &cByte);
158 if (ret == SCARD_S_SUCCESS) {
159 char* data =
new char[cByte];
160 ret = SCardGetAttrib(hCard, SCARD_ATTR_VENDOR_NAME, (LPBYTE)data, &cByte);
161 reader_vendor_name = data;
164 ret = SCardGetAttrib(hCard, SCARD_ATTR_VENDOR_IFD_TYPE, NULL, &cByte);
165 if (ret == SCARD_S_SUCCESS) {
166 char* data =
new char[cByte];
167 ret = SCardGetAttrib(hCard, SCARD_ATTR_VENDOR_IFD_TYPE, (LPBYTE)data, &cByte);
168 reader_interface_type = data;
171 SCardDisconnect(hCard, SCARD_LEAVE_CARD);
179 if (readers.count() > 1) {
180 if (!readers[i].contains(cardDevice->friendlyName())) {
181 if (!cardDevice->friendlyName().contains(reader_vendor_name) ||
182 ((reader_interface_type !=
"") && !cardDevice->friendlyName().contains(reader_vendor_name))) {
189 if (m_readerStates[i].dwEventState & SCARD_STATE_PRESENT) {
191 TQString atr = getCardATR(readers[i]);
192 retrieveCardCertificates(readers[i]);
193 statusChanged(
"PRESENT", atr);
196 deleteAllCertificatesFromCache();
200 if (m_readerStates[i].dwEventState & SCARD_STATE_CHANGED) {
201 if ((m_readerStates[i].dwCurrentState & SCARD_STATE_PRESENT)
202 && (m_readerStates[i].dwEventState & SCARD_STATE_EMPTY)) {
203 deleteAllCertificatesFromCache();
204 statusChanged(
"REMOVED", TQString::null);
206 else if ((m_readerStates[i].dwCurrentState & SCARD_STATE_EMPTY)
207 && (m_readerStates[i].dwEventState & SCARD_STATE_PRESENT)) {
209 TQString atr = getCardATR(readers[i]);
210 retrieveCardCertificates(readers[i]);
211 statusChanged(
"INSERTED", atr);
213 m_readerStates[i].dwCurrentState = m_readerStates[i].dwEventState;
219 ret = SCardGetStatusChange(m_cardContext, PCSC_POLL_TIMEOUT_S, m_readerStates, readers.count());
228 void CryptoCardDeviceWatcher::requestTermination() {
229 m_terminationRequested =
true;
232 void CryptoCardDeviceWatcher::setProvidedPin(TQString pin) {
234 m_cardPINPromptDone =
true;
237 void CryptoCardDeviceWatcher::retrySamePin(
bool enable) {
238 m_cardReusePIN = enable;
240 m_cardPIN =
"SHREDDINGTHEPINISMOSTSECURE";
241 m_cardPIN = TQString::null;
245 TQString CryptoCardDeviceWatcher::getCardATR(TQString readerName) {
249 TQString atr_formatted;
250 SCARDHANDLE hCard = 0;
251 DWORD dwActiveProtocol = 0;
254 ret = SCardConnect(m_cardContext, readerName.ascii(), SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
255 if (ret == SCARD_S_SUCCESS) {
256 ret = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, NULL, &cByte);
257 if (ret == SCARD_S_SUCCESS) {
258 char* data =
new char[cByte];
259 ret = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, (LPBYTE)data, &cByte);
260 atr_formatted = TQString::null;
261 for (i=0; i<cByte; i++) {
263 formatted.sprintf(
"%02x ", ((uint8_t)(*(data+i))));
264 atr_formatted.append(formatted.upper());
266 atr_formatted = atr_formatted.stripWhiteSpace();
268 SCardDisconnect(hCard, SCARD_LEAVE_CARD);
272 TQString errstring = pcsc_error_code_to_string(ret);
273 if (errstring !=
"") {
274 atr_formatted =
i18n(
"Unknown (%1)").arg(errstring);
277 atr_formatted = TQString(
"CARD_CONNECT_FAIL (%1)").arg(ret, 0, 16);
281 return atr_formatted;
283 return TQString::null;
287 void CryptoCardDeviceWatcher::enablePINEntryCallbacks(
bool enable) {
288 m_pinCallbacksEnabled = enable;
291 TQString CryptoCardDeviceWatcher::doPinRequest(TQString prompt) {
292 if (!m_pinCallbacksEnabled) {
293 return TQString::null;
296 if (m_cardReusePIN) {
300 m_cardPINPromptDone =
false;
301 emit(pinRequested(prompt));
302 while (!m_cardPINPromptDone) {
306 if (m_cardPIN.length() > 0) {
310 return TQString::null;
315 static void pkcs_log_hook(IN
void *
const global_data, IN
unsigned flags, IN
const char *
const format, IN va_list args) {
316 vprintf(format, args);
320 static PKCS11H_BOOL pkcs_pin_hook(IN
void *
const global_data, IN
void *
const user_data, IN
const pkcs11h_token_id_t token, IN
const unsigned retry, OUT
char *
const pin, IN
const size_t pin_max) {
321 CryptoCardDeviceWatcher* watcher = (CryptoCardDeviceWatcher*)global_data;
323 TQString providedPin = watcher->doPinRequest(
i18n(
"Please enter the PIN for '%1'").arg(token->display));
324 if (providedPin.length() > 0) {
325 snprintf(pin, pin_max,
"%s", providedPin.ascii());
337 int CryptoCardDeviceWatcher::initializePkcs() {
338 #if defined(WITH_PKCS)
340 printf(
"Initializing pkcs11-helper\n");
341 if ((rv = pkcs11h_initialize()) != CKR_OK) {
342 printf(
"pkcs11h_initialize failed: %s\n", pkcs11h_getMessage(rv));
346 printf(
"Registering pkcs11-helper hooks\n");
347 if ((rv = pkcs11h_setLogHook(pkcs_log_hook,
this)) != CKR_OK) {
348 printf(
"pkcs11h_setLogHook failed: %s\n", pkcs11h_getMessage(rv));
351 pkcs11h_setLogLevel(PKCS11H_LOG_WARN);
355 if ((rv = pkcs11h_setTokenPromptHook(_pkcs11h_hooks_token_prompt, NULL)) != CKR_OK) {
356 printf(
"pkcs11h_setTokenPromptHook failed: %s\n", pkcs11h_getMessage(rv));
361 if ((rv = pkcs11h_setMaxLoginRetries(CARD_MAX_LOGIN_RETRY_COUNT)) != CKR_OK) {
362 printf(
"pkcs11h_setMaxLoginRetries failed: %s\n", pkcs11h_getMessage(rv));
366 if ((rv = pkcs11h_setPINPromptHook(pkcs_pin_hook,
this)) != CKR_OK) {
367 printf(
"pkcs11h_setPINPromptHook failed: %s\n", pkcs11h_getMessage(rv));
371 printf(
"Adding provider '%s'\n", OPENSC_PKCS11_PROVIDER_LIBRARY);
372 if ((rv = pkcs11h_addProvider(OPENSC_PKCS11_PROVIDER_LIBRARY, OPENSC_PKCS11_PROVIDER_LIBRARY,
false, PKCS11H_PRIVATEMODE_MASK_AUTO, PKCS11H_SLOTEVENT_METHOD_AUTO, 0,
false)) != CKR_OK) {
373 printf(
"pkcs11h_addProvider failed: %s\n", pkcs11h_getMessage(rv));
383 int CryptoCardDeviceWatcher::retrieveCardCertificates(TQString readerName) {
384 #if defined(WITH_PKCS)
388 pkcs11h_certificate_id_list_t issuers;
389 pkcs11h_certificate_id_list_t certs;
391 if (initializePkcs() < 0) {
392 printf(
"Unable to initialize PKCS\n");
396 rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE, NULL, PKCS11H_PROMPT_MASK_ALLOW_PIN_PROMPT, &issuers, &certs);
397 if ((rv != CKR_OK) || (certs == NULL)) {
398 printf(
"Cannot enumerate certificates: %s\n", pkcs11h_getMessage(rv));
401 printf(
"Successfully enumerated certificates\n");
404 for (pkcs11h_certificate_id_list_t cert = certs; cert != NULL; cert = cert->next) {
405 TQString
label = cert->certificate_id->displayName;
406 printf(
"Certificate %d name: '%s'\n", i,
label.ascii());
408 pkcs11h_certificate_t certificate;
409 rv = pkcs11h_certificate_create(certs->certificate_id, NULL, PKCS11H_PROMPT_MASK_ALLOW_PIN_PROMPT, PKCS11H_PIN_CACHE_INFINITE, &certificate);
411 printf(
"Cannot read certificate: %s\n", pkcs11h_getMessage(rv));
412 pkcs11h_certificate_freeCertificateId(certs->certificate_id);
417 pkcs11h_certificate_freeCertificateId(certs->certificate_id);
419 pkcs11h_openssl_session_t openssl_session = NULL;
420 if ((openssl_session = pkcs11h_openssl_createSession(certificate)) == NULL) {
421 printf(
"Cannot initialize openssl session to retrieve cryptographic objects\n");
422 pkcs11h_certificate_freeCertificate(certificate);
429 x509_local = pkcs11h_openssl_session_getX509(openssl_session);
431 printf(
"Successfully retrieved X509 certificate\n");
434 printf(
"Cannot get X509 object\n");
439 rsa_local = pkcs11h_openssl_session_getRSA(openssl_session);
441 printf(
"Successfully retrieved RSA public key\n");
444 printf(
"Cannot get RSA object\n");
449 X509* x509_copy = X509_dup(x509_local);
451 cardDevice->m_cardCertificates.append(x509_copy);
454 printf(
"Unable to copy X509 certificate\n");
457 pkcs11h_openssl_freeSession(openssl_session);
461 pkcs11h_certificate_freeCertificateIdList(issuers);
469 void CryptoCardDeviceWatcher::deleteAllCertificatesFromCache() {
473 X509CertificatePtrListIterator it;
474 for (it = cardDevice->m_cardCertificates.begin(); it != cardDevice->m_cardCertificates.end(); ++it) {
476 X509_free(x509_cert);
479 cardDevice->m_cardCertificates.clear();
483 TDECryptographicCardDevice::TDECryptographicCardDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn),
484 m_watcherThread(NULL),
485 m_watcherObject(NULL),
486 m_cardPresent(false) {
489 TDECryptographicCardDevice::~TDECryptographicCardDevice() {
490 enableCardMonitoring(
false);
493 void TDECryptographicCardDevice::enableCardMonitoring(
bool enable) {
496 if (m_watcherObject && m_watcherThread) {
498 if ((cardPresent() == 1) && (cardX509Certificates().count() > 0)) {
500 emit(certificateListAvailable(
this));
507 m_watcherThread =
new TQEventLoopThread();
508 m_watcherObject =
new CryptoCardDeviceWatcher();
510 m_watcherObject->cardDevice =
this;
511 m_watcherObject->moveToThread(m_watcherThread);
512 TQObject::connect(m_watcherObject, TQ_SIGNAL(statusChanged(TQString,TQString)),
this, TQ_SLOT(cardStatusChanged(TQString,TQString)));
513 TQObject::connect(m_watcherObject, TQ_SIGNAL(pinRequested(TQString)),
this, TQ_SLOT(workerRequestedPin(TQString)));
514 TQTimer::singleShot(0, m_watcherObject, TQ_SLOT(run()));
516 m_watcherThread->start();
519 if (m_watcherObject) {
520 m_watcherObject->requestTermination();
522 if (m_watcherThread) {
523 m_watcherThread->wait();
524 delete m_watcherThread;
525 m_watcherThread = NULL;
527 if (m_watcherObject) {
528 delete m_watcherObject;
529 m_watcherObject = NULL;
535 void TDECryptographicCardDevice::enablePINEntryCallbacks(
bool enable) {
536 if (m_watcherObject) {
537 m_watcherObject->enablePINEntryCallbacks(enable);
541 int TDECryptographicCardDevice::cardPresent() {
542 if (m_watcherObject && m_watcherThread) {
553 TQString TDECryptographicCardDevice::cardATR() {
554 if (m_watcherObject && m_watcherThread) {
558 return TQString::null;
561 return TQString::null;
565 X509CertificatePtrList TDECryptographicCardDevice::cardX509Certificates() {
566 if (m_watcherObject && m_watcherThread) {
568 return m_cardCertificates;
571 return X509CertificatePtrList();
575 return X509CertificatePtrList();
579 void TDECryptographicCardDevice::cardStatusChanged(TQString status, TQString atr) {
580 if (status ==
"INSERTED") {
581 m_cardPresent =
true;
583 emit(cardInserted(
this));
584 if (m_cardCertificates.count() > 0) {
585 emit(certificateListAvailable(
this));
588 else if (status ==
"REMOVED") {
589 m_cardPresent =
false;
591 emit(cardRemoved(
this));
593 else if (status ==
"PRESENT") {
595 m_cardPresent =
true;
596 if (m_cardCertificates.count() > 0) {
597 emit(certificateListAvailable(
this));
602 void TDECryptographicCardDevice::setProvidedPin(TQString pin) {
603 if (m_watcherObject) {
604 m_watcherObject->setProvidedPin(pin);
608 TQString TDECryptographicCardDevice::autoPIN() {
609 #if defined(WITH_PKCS)
610 TQString retString = TQString::null;
622 ASN1_OBJECT* tde_autopin_data_object = OBJ_txt2obj(
"1.3.6.1.4.1.40364.1.2.1", 0);
625 X509CertificatePtrListIterator it;
626 for (it = m_cardCertificates.begin(); it != m_cardCertificates.end(); ++it) {
627 X509* x509_cert = *it;
628 GENERAL_NAMES* subjectAltNames = (GENERAL_NAMES*)X509_get_ext_d2i(x509_cert, NID_subject_alt_name, NULL, NULL);
629 int altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
630 for (i=0; i < altNameCount; i++) {
631 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
632 if (generalName->type == GEN_OTHERNAME) {
633 OTHERNAME* otherName = generalName->d.otherName;
634 if (!OBJ_cmp(otherName->type_id, tde_autopin_data_object)) {
635 ASN1_TYPE* asnValue = otherName->value;
638 ASN1_TYPE* asnSeqValue = NULL;
639 ASN1_GENERALSTRING* asnGeneralString = NULL;
640 STACK_OF(ASN1_TYPE) *asnSeqValueStack = NULL;
641 long asn1SeqValueObjectLength;
642 int asn1SeqValueObjectTag;
643 int asn1SeqValueObjectClass;
647 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
648 const uint8_t* asnSeqValueString = ASN1_STRING_get0_data(asnValue->value.sequence);
649 asnSeqValueStack = d2i_ASN1_SEQUENCE_ANY(NULL, &asnSeqValueString, ASN1_STRING_length(asnValue->value.sequence));
651 uint8_t* asnSeqValueString = ASN1_STRING_data(asnValue->value.sequence);
652 asnSeqValueStack = ASN1_seq_unpack_ASN1_TYPE(asnSeqValueString, ASN1_STRING_length(asnValue->value.sequence), d2i_ASN1_TYPE, ASN1_TYPE_free);
654 asnSeqValue = sk_ASN1_TYPE_value(asnSeqValueStack, index);
656 if (asnSeqValue->value.octet_string->data[0] == ((V_ASN1_CONSTRUCTED | V_ASN1_CONTEXT_SPECIFIC) + index)) {
657 const unsigned char* asn1SeqValueObjectData = asnSeqValue->value.sequence->data;
658 returnCode = ASN1_get_object(&asn1SeqValueObjectData, &asn1SeqValueObjectLength, &asn1SeqValueObjectTag, &asn1SeqValueObjectClass, asnSeqValue->value.sequence->length);
659 if (!(returnCode & 0x80)) {
660 if (returnCode == (V_ASN1_CONSTRUCTED + index)) {
661 if (d2i_ASN1_GENERALSTRING(&asnGeneralString, &asn1SeqValueObjectData, asn1SeqValueObjectLength) != NULL) {
662 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
663 retString = TQString((
const char *)ASN1_STRING_get0_data(asnGeneralString));
665 retString = TQString((
const char *)ASN1_STRING_data(asnGeneralString));
672 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
673 sk_ASN1_TYPE_pop_free(asnSeqValueStack, ASN1_TYPE_free);
686 return TQString::null;
690 void TDECryptographicCardDevice::workerRequestedPin(TQString prompt) {
691 emit(pinRequested(prompt,
this));
694 int TDECryptographicCardDevice::decryptDataEncryptedWithCertPublicKey(TQByteArray &ciphertext, TQByteArray &plaintext, TQString *errstr) {
695 TQValueList<TQByteArray> cipherTextList;
696 TQValueList<TQByteArray> plainTextList;
697 TQValueList<int> retCodeList;
699 cipherTextList.append(ciphertext);
701 this->decryptDataEncryptedWithCertPublicKey(cipherTextList, plainTextList, retCodeList, errstr);
703 plaintext = plainTextList[0];
704 return retCodeList[0];
707 int TDECryptographicCardDevice::decryptDataEncryptedWithCertPublicKey(TQValueList<TQByteArray> &cipherTextList, TQValueList<TQByteArray> &plainTextList, TQValueList<int> &retcodes, TQString *errstr) {
708 #if defined(WITH_PKCS)
711 if (!m_watcherObject) {
712 if (errstr) *errstr =
i18n(
"Card watcher object not available");
717 pkcs11h_certificate_id_list_t issuers;
718 pkcs11h_certificate_id_list_t certs;
720 if (m_watcherObject->initializePkcs() < 0) {
721 if (errstr) *errstr =
i18n(
"Unable to initialize PKCS");
725 rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE, NULL, PKCS11H_PROMPT_MASK_ALLOW_PIN_PROMPT, &issuers, &certs);
726 if ((rv != CKR_OK) || (certs == NULL)) {
727 if (errstr) *errstr =
i18n(
"Cannot enumerate certificates: %1").arg(pkcs11h_getMessage(rv));
732 for (pkcs11h_certificate_id_list_t cert = certs; cert != NULL; cert = cert->next) {
733 TQString
label = cert->certificate_id->displayName;
735 pkcs11h_certificate_t certificate;
736 rv = pkcs11h_certificate_create(certs->certificate_id, NULL, PKCS11H_PROMPT_MASK_ALLOW_PIN_PROMPT, PKCS11H_PIN_CACHE_INFINITE, &certificate);
738 if (errstr) *errstr =
i18n(
"Cannot read certificate: %1").arg(pkcs11h_getMessage(rv));
739 pkcs11h_certificate_freeCertificateId(certs->certificate_id);
744 pkcs11h_certificate_freeCertificateId(certs->certificate_id);
746 pkcs11h_openssl_session_t openssl_session = NULL;
747 if ((openssl_session = pkcs11h_openssl_createSession(certificate)) == NULL) {
748 if (errstr) *errstr =
i18n(
"Cannot initialize openssl session to retrieve cryptographic objects");
749 pkcs11h_certificate_freeCertificate(certificate);
756 x509_local = pkcs11h_openssl_session_getX509(openssl_session);
758 if (errstr) *errstr =
i18n(
"Cannot get X509 object");
763 EVP_PKEY* x509_pubkey = NULL;
764 RSA* rsa_pubkey = NULL;
765 x509_pubkey = X509_get_pubkey(x509_local);
767 rsa_pubkey = EVP_PKEY_get1_RSA(x509_pubkey);
771 rv = pkcs11h_certificate_ensureKeyAccess(certificate);
773 if (rv == CKR_CANCEL) {
777 else if ((rv == CKR_PIN_INCORRECT) || (rv == CKR_USER_NOT_LOGGED_IN)) {
788 m_watcherObject->retrySamePin(
true);
790 TQValueList<TQByteArray>::iterator it;
791 TQValueList<TQByteArray>::iterator it2;
792 TQValueList<int>::iterator it3;
793 plainTextList.clear();
795 for (it = cipherTextList.begin(); it != cipherTextList.end(); ++it) {
796 plainTextList.append(TQByteArray());
799 for (it = cipherTextList.begin(), it2 = plainTextList.begin(), it3 = retcodes.begin(); it != cipherTextList.end(); ++it, ++it2, ++it3) {
800 TQByteArray& ciphertext = *it;
801 TQByteArray& plaintext = *it2;
805 if (ciphertext.size() < 16) {
806 if (errstr) *errstr =
i18n(
"Cannot decrypt: %1").arg(
i18n(
"Ciphertext too small"));
814 unsigned int rsa_length = RSA_size(rsa_pubkey);
815 if (ciphertext.size() > rsa_length) {
816 if (errstr) *errstr =
i18n(
"Cannot decrypt: %1").arg(
i18n(
"Ciphertext too large"));
825 rv = pkcs11h_certificate_decryptAny(certificate, CKM_RSA_PKCS, (
unsigned char*)ciphertext.data(), ciphertext.size(), NULL, &size);
827 if (errstr) *errstr =
i18n(
"Cannot determine decrypted message length: %1 (%2)").arg(pkcs11h_getMessage(rv)).arg(rv);
828 if (rv == CKR_CANCEL) {
833 else if ((rv == CKR_PIN_INCORRECT) || (rv == CKR_USER_NOT_LOGGED_IN)) {
845 plaintext.resize(size);
846 rv = pkcs11h_certificate_decryptAny(certificate, CKM_RSA_PKCS, (
unsigned char*)ciphertext.data(), ciphertext.size(), (
unsigned char*)plaintext.data(), &size);
848 if (errstr) *errstr =
i18n(
"Cannot decrypt: %1 (%2)").arg(pkcs11h_getMessage(rv)).arg(rv);
849 if (rv == CKR_CANCEL) {
854 else if ((rv == CKR_PIN_INCORRECT) || (rv == CKR_USER_NOT_LOGGED_IN)) {
865 if (errstr) *errstr = TQString::null;
872 pkcs11h_openssl_freeSession(openssl_session);
882 pkcs11h_certificate_freeCertificateIdList(issuers);
885 m_watcherObject->retrySamePin(
false);
893 int TDECryptographicCardDevice::createNewSecretRSAKeyFromCertificate(TQByteArray &plaintext, TQByteArray &ciphertext, X509* certificate) {
894 #if defined(WITH_PKCS)
899 EVP_PKEY* x509_pubkey = NULL;
900 RSA* rsa_pubkey = NULL;
901 x509_pubkey = X509_get_pubkey(certificate);
903 rsa_pubkey = EVP_PKEY_get1_RSA(x509_pubkey);
911 int rsa_padding_style = RSA_PKCS1_PADDING;
912 unsigned int rsa_length = RSA_size(rsa_pubkey);
913 unsigned int max_key_length = rsa_length - 41;
916 plaintext.resize(max_key_length);
917 for (i=0; i < max_key_length; i++) {
922 ciphertext.resize(rsa_length);
923 if (RSA_public_encrypt(plaintext.size(), (
unsigned char *)plaintext.data(), (
unsigned char *)ciphertext.data(), rsa_pubkey, rsa_padding_style) < 0) {
933 RSA_free(rsa_pubkey);
936 EVP_PKEY_free(x509_pubkey);
945 TQString TDECryptographicCardDevice::pkcsProviderLibrary() {
946 #if defined(WITH_PKCS)
947 return OPENSC_PKCS11_PROVIDER_LIBRARY;
949 return TQString::null;
953 #include "tdecryptographiccarddevice.moc"
954 #include "tdecryptographiccarddevice_private.moc"
static int random()
Generates a uniform random number.
TQString i18n(const char *text)
i18n is the function that does everything you need to translate a string.
TQString label(StdAccel id)
Returns a localized label for user-visible display.