1 | #include <QCoreApplication> |
---|
2 | |
---|
3 | #include <assert.h> |
---|
4 | #include <iostream> |
---|
5 | #include <stdio.h> |
---|
6 | #include <QByteArray> |
---|
7 | |
---|
8 | #include "pkcs11.h" |
---|
9 | #include "cryptotoken.h" |
---|
10 | |
---|
11 | #define FAILURE 0 |
---|
12 | #define SUCCESS 1 |
---|
13 | |
---|
14 | extern CK_FUNCTION_LIST_PTR fl; |
---|
15 | |
---|
16 | |
---|
17 | char *EstEID_bin2hex(const char *bin, const int binLength) { |
---|
18 | char *hex = (char *)malloc(binLength * 2 + 1); |
---|
19 | for (unsigned int j = 0; j < binLength; j++) sprintf(hex + (j * 2), "%02X", (unsigned char)bin[j]); |
---|
20 | hex[binLength * 2] = '\0'; |
---|
21 | return hex; |
---|
22 | } |
---|
23 | |
---|
24 | char *EstEID_hex2bin(const char *hex) { |
---|
25 | //LOG_LOCATION; |
---|
26 | int binLength = strlen(hex) / 2;printf("binLength: %d\n", binLength); |
---|
27 | char *bin = (char *)malloc(binLength); |
---|
28 | char *c = bin; |
---|
29 | char *h = (char *)hex; |
---|
30 | int i = 0; |
---|
31 | while (*h) { |
---|
32 | int x; |
---|
33 | sscanf(h, "%2X", &x); |
---|
34 | *c = x; |
---|
35 | c++; |
---|
36 | h += 2; |
---|
37 | i++; |
---|
38 | } |
---|
39 | return bin; |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | int main(int argc, char *argv[]) |
---|
44 | { |
---|
45 | QCoreApplication a(argc, argv); |
---|
46 | |
---|
47 | qDebug("hola mundo"); |
---|
48 | |
---|
49 | CryptoToken* ct = new CryptoToken(); |
---|
50 | |
---|
51 | // pin |
---|
52 | QString pin("123456"); |
---|
53 | |
---|
54 | // label of the private key |
---|
55 | QString label = "New Key aaraujo"; |
---|
56 | |
---|
57 | // hash |
---|
58 | QString hash("cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04"); |
---|
59 | |
---|
60 | // result |
---|
61 | std::vector<unsigned char> result = ct->signHash(hash, pin, label); |
---|
62 | |
---|
63 | qDebug(ct->toHex(result)); |
---|
64 | |
---|
65 | // check the signature |
---|
66 | assert(ct->toHex(result) == "6cea780ecd21141bfe460d4fd2172f52366c8e357303e9914310f1553951876f2b3d6127571f645b52b8148dfc9f6016e851641ff2c6f785dd84186fe82d802982afd2f88951e22f03dc6982600277a1c18faeda0da89a60d2afb4a51a865bbd4fc3871b8516e8a02afe309b626f8aadb53b6543d99e9c4ab5b334634edcd0898171cb6753b2abe00f64303a1398795e25d64f960ea73041b7178ba539f6bc0cedd16b87f366b4e752fbb7ca4e33fddee8b5adf3bc70f5406a3c69ac8ff62d99ff77a7e340ad6e1d18a7b25e8652653dec5b653a07a8bb289dd6ad9fa876094008864bf475e8589a9cefd2240f2f1f537593e3a94ce01fbea90e9f18bbf3783d"); |
---|
67 | |
---|
68 | |
---|
69 | //return a.exec(); |
---|
70 | return 0; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | /* |
---|
76 | int main(int argc, char *argv[]) |
---|
77 | { |
---|
78 | QCoreApplication a(argc, argv); |
---|
79 | |
---|
80 | qDebug("hola mundo"); |
---|
81 | |
---|
82 | CryptoToken* ct = new CryptoToken(); |
---|
83 | |
---|
84 | //qDebug(qPrintable(ct->getInfoCryptoki())); |
---|
85 | |
---|
86 | char strPin[16]; |
---|
87 | QString PIN("123456"); |
---|
88 | strcpy(strPin,qPrintable(PIN)); |
---|
89 | |
---|
90 | CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; |
---|
91 | CK_SLOT_ID slotID; |
---|
92 | //CK_SLOT_ID slotID2; |
---|
93 | |
---|
94 | // inicializacion del criptoki |
---|
95 | CK_RV rv; |
---|
96 | if (!ct->initializeCriptoki()) |
---|
97 | { |
---|
98 | //qDebug("fallo la incializacion de criptoki"); |
---|
99 | return 1; |
---|
100 | } |
---|
101 | |
---|
102 | hSession = ct->openSession(strPin, slotID); |
---|
103 | if (hSession == CK_INVALID_HANDLE) |
---|
104 | { |
---|
105 | qDebug("Fallo ptr_SC->openSession"); |
---|
106 | rv = C_Finalize(NULL_PTR); |
---|
107 | qDebug("C_Finalize: rv = %x",rv); |
---|
108 | assert(rv == CKR_OK); |
---|
109 | return 1; |
---|
110 | } |
---|
111 | |
---|
112 | // obtencion de la clave privada para firmar los datos |
---|
113 | CK_OBJECT_HANDLE privateKey = CK_INVALID_HANDLE; |
---|
114 | |
---|
115 | QString label = "New Key aaraujo"; |
---|
116 | privateKey = ct->getPrivateKey(hSession, slotID, label); |
---|
117 | |
---|
118 | |
---|
119 | if (privateKey == CK_INVALID_HANDLE) |
---|
120 | { |
---|
121 | qDebug("Fallo ptr_SC->getPrivateKey"); |
---|
122 | rv = C_CloseSession(hSession); |
---|
123 | qDebug("C_CloseSession: rv = %x",rv); |
---|
124 | assert(rv == CKR_OK); |
---|
125 | rv = C_Finalize(NULL_PTR); |
---|
126 | qDebug("C_Finalize: rv = %x",rv); |
---|
127 | assert(rv == CKR_OK); |
---|
128 | return 1; |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | CK_ULONG slen = 512; |
---|
133 | CK_BYTE_PTR sign = new CK_BYTE[slen]; |
---|
134 | |
---|
135 | CK_BYTE hash[64]; |
---|
136 | CK_ULONG hashLen = (CK_ULONG) sizeof(hash); |
---|
137 | |
---|
138 | // un has recibido del servidor 64 bytes |
---|
139 | // aaf363de5f571c7ae7976ca52891af440d2934a146860c82f0f5672ddc4ee078 |
---|
140 | // cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04 |
---|
141 | |
---|
142 | QString hashInHex("cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04"); |
---|
143 | qDebug("longitud de hashInHex: %d", hashInHex.size()); |
---|
144 | |
---|
145 | |
---|
146 | memcpy(hash,qPrintable(hashInHex), hashInHex.size()); |
---|
147 | //hash = (unsigned char) EstEID_hex2bin(qPrintable(hashInHex)); |
---|
148 | |
---|
149 | |
---|
150 | if(!ct->signSomeData(hSession, privateKey, hash, hashLen, sign, &slen)) |
---|
151 | { |
---|
152 | //QMessageBox::warning(this,XCA_TITLE, tr("Process sign for random data failed!")); |
---|
153 | qDebug("Fallo sc_ptr->signSomeData"); |
---|
154 | rv = C_CloseSession(hSession); |
---|
155 | qDebug("C_CloseSession: rv = %x",rv); |
---|
156 | assert(rv == CKR_OK); |
---|
157 | rv = C_Finalize(NULL_PTR); |
---|
158 | qDebug("C_Finalize: rv = %x",rv); |
---|
159 | assert(rv == CKR_OK); |
---|
160 | return 1; |
---|
161 | } |
---|
162 | // aqui debo colocar terminar el arreglo de firma con NULL |
---|
163 | sign[slen] = '\0'; |
---|
164 | |
---|
165 | qDebug("Valor de la firma signature:"); |
---|
166 | qDebug((const char *) sign); |
---|
167 | qDebug("Valor de signatureLength: "); |
---|
168 | qDebug(qPrintable(QString::number(slen))); |
---|
169 | |
---|
170 | char * signatureInHex = EstEID_bin2hex((const char *) sign, slen); |
---|
171 | |
---|
172 | |
---|
173 | qDebug("valor de la firma en hexadecimal: %s", signatureInHex); |
---|
174 | |
---|
175 | assert(signatureInHex == "6cea780ecd21141bfe460d4fd2172f52366c8e357303e9914310f1553951876f2b3d6127571f645b52b8148dfc9f6016e851641ff2c6f785dd84186fe82d802982afd2f88951e22f03dc6982600277a1c18faeda0da89a60d2afb4a51a865bbd4fc3871b8516e8a02afe309b626f8aadb53b6543d99e9c4ab5b334634edcd0898171cb6753b2abe00f64303a1398795e25d64f960ea73041b7178ba539f6bc0cedd16b87f366b4e752fbb7ca4e33fddee8b5adf3bc70f5406a3c69ac8ff62d99ff77a7e340ad6e1d18a7b25e8652653dec5b653a07a8bb289dd6ad9fa876094008864bf475e8589a9cefd2240f2f1f537593e3a94ce01fbea90e9f18bbf3783d"); |
---|
176 | |
---|
177 | |
---|
178 | qDebug("closeSession..."); |
---|
179 | |
---|
180 | ct->closeSession(hSession); |
---|
181 | |
---|
182 | |
---|
183 | //return a.exec(); |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | */ |
---|