#include #include #include #include #include #include "/include/pkcs11.h" #include "/include/cryptotoken.h" #define FAILURE 0 #define SUCCESS 1 extern CK_FUNCTION_LIST_PTR fl; char *EstEID_bin2hex(const char *bin, const int binLength) { char *hex = (char *)malloc(binLength * 2 + 1); for (unsigned int j = 0; j < binLength; j++) sprintf(hex + (j * 2), "%02X", (unsigned char)bin[j]); hex[binLength * 2] = '\0'; return hex; } char *EstEID_hex2bin(const char *hex) { //LOG_LOCATION; int binLength = strlen(hex) / 2;printf("binLength: %d\n", binLength); char *bin = (char *)malloc(binLength); char *c = bin; char *h = (char *)hex; int i = 0; while (*h) { int x; sscanf(h, "%2X", &x); *c = x; c++; h += 2; i++; } return bin; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug("hola mundo"); CryptoToken* ct = new CryptoToken(); // pin QString pin("1234567890"); // label of the private key QString label = "New Key aaraujo"; // hash QString hash("cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04"); // result std::vector result = ct->signHash(hash, pin, label); qDebug(ct->toHex(result)); // check the signature assert(ct->toHex(result) == "6cea780ecd21141bfe460d4fd2172f52366c8e357303e9914310f1553951876f2b3d6127571f645b52b8148dfc9f6016e851641ff2c6f785dd84186fe82d802982afd2f88951e22f03dc6982600277a1c18faeda0da89a60d2afb4a51a865bbd4fc3871b8516e8a02afe309b626f8aadb53b6543d99e9c4ab5b334634edcd0898171cb6753b2abe00f64303a1398795e25d64f960ea73041b7178ba539f6bc0cedd16b87f366b4e752fbb7ca4e33fddee8b5adf3bc70f5406a3c69ac8ff62d99ff77a7e340ad6e1d18a7b25e8652653dec5b653a07a8bb289dd6ad9fa876094008864bf475e8589a9cefd2240f2f1f537593e3a94ce01fbea90e9f18bbf3783d"); //return a.exec(); return 0; } /* int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug("hola mundo"); CryptoToken* ct = new CryptoToken(); //qDebug(qPrintable(ct->getInfoCryptoki())); char strPin[16]; QString PIN("123456"); strcpy(strPin,qPrintable(PIN)); CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; CK_SLOT_ID slotID; //CK_SLOT_ID slotID2; // inicializacion del criptoki CK_RV rv; if (!ct->initializeCriptoki()) { //qDebug("fallo la incializacion de criptoki"); return 1; } hSession = ct->openSession(strPin, slotID); if (hSession == CK_INVALID_HANDLE) { qDebug("Fallo ptr_SC->openSession"); rv = C_Finalize(NULL_PTR); qDebug("C_Finalize: rv = %x",rv); assert(rv == CKR_OK); return 1; } // obtencion de la clave privada para firmar los datos CK_OBJECT_HANDLE privateKey = CK_INVALID_HANDLE; QString label = "New Key aaraujo"; privateKey = ct->getPrivateKey(hSession, slotID, label); if (privateKey == CK_INVALID_HANDLE) { qDebug("Fallo ptr_SC->getPrivateKey"); rv = C_CloseSession(hSession); qDebug("C_CloseSession: rv = %x",rv); assert(rv == CKR_OK); rv = C_Finalize(NULL_PTR); qDebug("C_Finalize: rv = %x",rv); assert(rv == CKR_OK); return 1; } CK_ULONG slen = 512; CK_BYTE_PTR sign = new CK_BYTE[slen]; CK_BYTE hash[64]; CK_ULONG hashLen = (CK_ULONG) sizeof(hash); // un has recibido del servidor 64 bytes // aaf363de5f571c7ae7976ca52891af440d2934a146860c82f0f5672ddc4ee078 // cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04 QString hashInHex("cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04"); qDebug("longitud de hashInHex: %d", hashInHex.size()); memcpy(hash,qPrintable(hashInHex), hashInHex.size()); //hash = (unsigned char) EstEID_hex2bin(qPrintable(hashInHex)); if(!ct->signSomeData(hSession, privateKey, hash, hashLen, sign, &slen)) { //QMessageBox::warning(this,XCA_TITLE, tr("Process sign for random data failed!")); qDebug("Fallo sc_ptr->signSomeData"); rv = C_CloseSession(hSession); qDebug("C_CloseSession: rv = %x",rv); assert(rv == CKR_OK); rv = C_Finalize(NULL_PTR); qDebug("C_Finalize: rv = %x",rv); assert(rv == CKR_OK); return 1; } // aqui debo colocar terminar el arreglo de firma con NULL sign[slen] = '\0'; qDebug("Valor de la firma signature:"); qDebug((const char *) sign); qDebug("Valor de signatureLength: "); qDebug(qPrintable(QString::number(slen))); char * signatureInHex = EstEID_bin2hex((const char *) sign, slen); qDebug("valor de la firma en hexadecimal: %s", signatureInHex); assert(signatureInHex == "6cea780ecd21141bfe460d4fd2172f52366c8e357303e9914310f1553951876f2b3d6127571f645b52b8148dfc9f6016e851641ff2c6f785dd84186fe82d802982afd2f88951e22f03dc6982600277a1c18faeda0da89a60d2afb4a51a865bbd4fc3871b8516e8a02afe309b626f8aadb53b6543d99e9c4ab5b334634edcd0898171cb6753b2abe00f64303a1398795e25d64f960ea73041b7178ba539f6bc0cedd16b87f366b4e752fbb7ca4e33fddee8b5adf3bc70f5406a3c69ac8ff62d99ff77a7e340ad6e1d18a7b25e8652653dec5b653a07a8bb289dd6ad9fa876094008864bf475e8589a9cefd2240f2f1f537593e3a94ce01fbea90e9f18bbf3783d"); qDebug("closeSession..."); ct->closeSession(hSession); //return a.exec(); return 0; } */