20 #define _TDECRYPTOGRAPHICCARDDEVICE_INTERNAL 1
23 #include "tdecryptographiccarddevice_private.h"
24 #include "tdecryptographiccarddevice.h"
28 #include <ntqthread.h>
29 #include <ntqeventloop.h>
30 #include <ntqapplication.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, SIGNAL(statusChanged(TQString,TQString)),
this, SLOT(cardStatusChanged(TQString,TQString)));
513 TQObject::connect(m_watcherObject, SIGNAL(pinRequested(TQString)),
this, SLOT(workerRequestedPin(TQString)));
514 TQTimer::singleShot(0, m_watcherObject, 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;
639 ASN1_TYPE* asnSeqValue = NULL;
640 ASN1_GENERALSTRING* asnGeneralString = NULL;
641 STACK_OF(ASN1_TYPE) *asnSeqValueStack = NULL;
642 long asn1SeqValueObjectLength;
643 int asn1SeqValueObjectTag;
644 int asn1SeqValueObjectClass;
648 asnSeqValueStack = ASN1_seq_unpack_ASN1_TYPE(ASN1_STRING_data(asnValue->value.sequence), ASN1_STRING_length(asnValue->value.sequence), d2i_ASN1_TYPE, ASN1_TYPE_free);
649 asnSeqValue = sk_ASN1_TYPE_value(asnSeqValueStack, index);
651 if (asnSeqValue->value.octet_string->data[0] == ((V_ASN1_CONSTRUCTED | V_ASN1_CONTEXT_SPECIFIC) + index)) {
652 const unsigned char* asn1SeqValueObjectData = asnSeqValue->value.sequence->data;
653 returnCode = ASN1_get_object(&asn1SeqValueObjectData, &asn1SeqValueObjectLength, &asn1SeqValueObjectTag, &asn1SeqValueObjectClass, asnSeqValue->value.sequence->length);
654 if (!(returnCode & 0x80)) {
655 if (returnCode == (V_ASN1_CONSTRUCTED + index)) {
656 if (d2i_ASN1_GENERALSTRING(&asnGeneralString, &asn1SeqValueObjectData, asn1SeqValueObjectLength) != NULL) {
657 retString = TQString((
const char *)ASN1_STRING_data(asnGeneralString));
674 return TQString::null;
678 void TDECryptographicCardDevice::workerRequestedPin(TQString prompt) {
679 emit(pinRequested(prompt,
this));
682 int TDECryptographicCardDevice::decryptDataEncryptedWithCertPublicKey(TQByteArray &ciphertext, TQByteArray &plaintext, TQString *errstr) {
683 TQValueList<TQByteArray> cipherTextList;
684 TQValueList<TQByteArray> plainTextList;
685 TQValueList<int> retCodeList;
687 cipherTextList.append(ciphertext);
689 this->decryptDataEncryptedWithCertPublicKey(cipherTextList, plainTextList, retCodeList, errstr);
691 plaintext = plainTextList[0];
692 return retCodeList[0];
695 int TDECryptographicCardDevice::decryptDataEncryptedWithCertPublicKey(TQValueList<TQByteArray> &cipherTextList, TQValueList<TQByteArray> &plainTextList, TQValueList<int> &retcodes, TQString *errstr) {
696 #if defined(WITH_PKCS)
699 if (!m_watcherObject) {
700 if (errstr) *errstr =
i18n(
"Card watcher object not available");
705 pkcs11h_certificate_id_list_t issuers;
706 pkcs11h_certificate_id_list_t certs;
708 if (m_watcherObject->initializePkcs() < 0) {
709 if (errstr) *errstr =
i18n(
"Unable to initialize PKCS");
713 rv = pkcs11h_certificate_enumCertificateIds(PKCS11H_ENUM_METHOD_CACHE, NULL, PKCS11H_PROMPT_MASK_ALLOW_PIN_PROMPT, &issuers, &certs);
714 if ((rv != CKR_OK) || (certs == NULL)) {
715 if (errstr) *errstr =
i18n(
"Cannot enumerate certificates: %1").arg(pkcs11h_getMessage(rv));
720 for (pkcs11h_certificate_id_list_t cert = certs; cert != NULL; cert = cert->next) {
721 TQString label = cert->certificate_id->displayName;
723 pkcs11h_certificate_t certificate;
724 rv = pkcs11h_certificate_create(certs->certificate_id, NULL, PKCS11H_PROMPT_MASK_ALLOW_PIN_PROMPT, PKCS11H_PIN_CACHE_INFINITE, &certificate);
726 if (errstr) *errstr =
i18n(
"Cannot read certificate: %1").arg(pkcs11h_getMessage(rv));
727 pkcs11h_certificate_freeCertificateId(certs->certificate_id);
732 pkcs11h_certificate_freeCertificateId(certs->certificate_id);
734 pkcs11h_openssl_session_t openssl_session = NULL;
735 if ((openssl_session = pkcs11h_openssl_createSession(certificate)) == NULL) {
736 if (errstr) *errstr =
i18n(
"Cannot initialize openssl session to retrieve cryptographic objects");
737 pkcs11h_certificate_freeCertificate(certificate);
744 x509_local = pkcs11h_openssl_session_getX509(openssl_session);
746 if (errstr) *errstr =
i18n(
"Cannot get X509 object");
751 EVP_PKEY* x509_pubkey = NULL;
752 RSA* rsa_pubkey = NULL;
753 x509_pubkey = X509_get_pubkey(x509_local);
755 rsa_pubkey = EVP_PKEY_get1_RSA(x509_pubkey);
759 rv = pkcs11h_certificate_ensureKeyAccess(certificate);
761 if (rv == CKR_CANCEL) {
765 else if ((rv == CKR_PIN_INCORRECT) || (rv == CKR_USER_NOT_LOGGED_IN)) {
776 m_watcherObject->retrySamePin(
true);
778 TQValueList<TQByteArray>::iterator it;
779 TQValueList<TQByteArray>::iterator it2;
780 TQValueList<int>::iterator it3;
781 plainTextList.clear();
783 for (it = cipherTextList.begin(); it != cipherTextList.end(); ++it) {
784 plainTextList.append(TQByteArray());
787 for (it = cipherTextList.begin(), it2 = plainTextList.begin(), it3 = retcodes.begin(); it != cipherTextList.end(); ++it, ++it2, ++it3) {
788 TQByteArray& ciphertext = *it;
789 TQByteArray& plaintext = *it2;
793 if (ciphertext.size() < 16) {
794 if (errstr) *errstr =
i18n(
"Cannot decrypt: %1").arg(
i18n(
"Ciphertext too small"));
802 unsigned int rsa_length = RSA_size(rsa_pubkey);
803 if (ciphertext.size() > rsa_length) {
804 if (errstr) *errstr =
i18n(
"Cannot decrypt: %1").arg(
i18n(
"Ciphertext too large"));
813 rv = pkcs11h_certificate_decryptAny(certificate, CKM_RSA_PKCS, (
unsigned char*)ciphertext.data(), ciphertext.size(), NULL, &size);
815 if (errstr) *errstr =
i18n(
"Cannot determine decrypted message length: %1 (%2)").arg(pkcs11h_getMessage(rv)).arg(rv);
816 if (rv == CKR_CANCEL) {
821 else if ((rv == CKR_PIN_INCORRECT) || (rv == CKR_USER_NOT_LOGGED_IN)) {
833 plaintext.resize(size);
834 rv = pkcs11h_certificate_decryptAny(certificate, CKM_RSA_PKCS, (
unsigned char*)ciphertext.data(), ciphertext.size(), (
unsigned char*)plaintext.data(), &size);
836 if (errstr) *errstr =
i18n(
"Cannot decrypt: %1 (%2)").arg(pkcs11h_getMessage(rv)).arg(rv);
837 if (rv == CKR_CANCEL) {
842 else if ((rv == CKR_PIN_INCORRECT) || (rv == CKR_USER_NOT_LOGGED_IN)) {
853 if (errstr) *errstr = TQString::null;
860 pkcs11h_openssl_freeSession(openssl_session);
870 pkcs11h_certificate_freeCertificateIdList(issuers);
873 m_watcherObject->retrySamePin(
false);
881 int TDECryptographicCardDevice::createNewSecretRSAKeyFromCertificate(TQByteArray &plaintext, TQByteArray &ciphertext, X509* certificate) {
882 #if defined(WITH_PKCS)
887 EVP_PKEY* x509_pubkey = NULL;
888 RSA* rsa_pubkey = NULL;
889 x509_pubkey = X509_get_pubkey(certificate);
891 rsa_pubkey = EVP_PKEY_get1_RSA(x509_pubkey);
899 int rsa_padding_style = RSA_PKCS1_PADDING;
900 unsigned int rsa_length = RSA_size(rsa_pubkey);
901 unsigned int max_key_length = rsa_length - 41;
904 plaintext.resize(max_key_length);
905 for (i=0; i < max_key_length; i++) {
910 ciphertext.resize(rsa_length);
911 if (RSA_public_encrypt(plaintext.size(), (
unsigned char *)plaintext.data(), (
unsigned char *)ciphertext.data(), rsa_pubkey, rsa_padding_style) < 0) {
921 RSA_free(rsa_pubkey);
924 EVP_PKEY_free(x509_pubkey);
933 TQString TDECryptographicCardDevice::pkcsProviderLibrary() {
934 #if defined(WITH_PKCS)
935 return OPENSC_PKCS11_PROVIDER_LIBRARY;
937 return TQString::null;
941 #include "tdecryptographiccarddevice.moc"
942 #include "tdecryptographiccarddevice_private.moc"
TQString i18n(const char *text)
static int random()
Generates a uniform random number.
TQString label(StdAccel id)
Returns a localized label for user-visible display.