source: terepaima/signHash/main.cpp @ 2506bdb

Last change on this file since 2506bdb was 2506bdb, checked in by Antonio Araujo <aaraujo@…>, 7 years ago

Agreadas funciones para obtener información de los certificados firmantes que se encuentran en un dispositivo criptográfico.

  • Property mode set to 100644
File size: 7.5 KB
Line 
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
14extern CK_FUNCTION_LIST_PTR fl;
15
16
17char *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
24char *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
43int main(int argc, char *argv[])
44{
45    QCoreApplication a(argc, argv);
46
47    qDebug("hola mundo");
48
49    CryptoToken* ct = new CryptoToken();
50
51    // ********************* prueba para token *******************************
52/*
53    // pin
54    QString pin("123456");
55
56    // label of the private key
57    QString label = "New Key aaraujo";
58    //QString label = "GemP15-1:Tibisay's CENDITEL ID";
59
60    // hash
61    QString hash("cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04");
62
63    // result
64    std::vector<unsigned char> result = ct->signHash(hash, pin, label);
65
66    qDebug(ct->toHex(result));
67
68    // check the signature
69    assert(ct->toHex(result) == "6cea780ecd21141bfe460d4fd2172f52366c8e357303e9914310f1553951876f2b3d6127571f645b52b8148dfc9f6016e851641ff2c6f785dd84186fe82d802982afd2f88951e22f03dc6982600277a1c18faeda0da89a60d2afb4a51a865bbd4fc3871b8516e8a02afe309b626f8aadb53b6543d99e9c4ab5b334634edcd0898171cb6753b2abe00f64303a1398795e25d64f960ea73041b7178ba539f6bc0cedd16b87f366b4e752fbb7ca4e33fddee8b5adf3bc70f5406a3c69ac8ff62d99ff77a7e340ad6e1d18a7b25e8652653dec5b653a07a8bb289dd6ad9fa876094008864bf475e8589a9cefd2240f2f1f537593e3a94ce01fbea90e9f18bbf3783d");
70
71    qDebug("exit");
72*/
73    // ***********************************************************************
74
75
76    // ********************* prueba para tarjeta *******************************
77
78    try{
79        // pin
80        QString pin("123456");
81
82        // hash
83        QString hash("cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04");
84
85        //QString label = "GemP15-1:Tibisay's CENDITEL ID";
86
87        //qDebug("lista de claves privadas:");
88        //QStringList list = ct->getPrivateKeyLabels(pin);
89        //for (int i = 0; i < list.size(); ++i)
90        //    std::cout << "private key label: " << list.at(i).toLocal8Bit().constData() << std::endl;
91
92
93        // obtener lista de información de certificados <privateKeyLabel, commonName, expirationDate>
94        QList<QStringList> certificateInformationList = ct->getDeviceCertificates("123456");
95
96        // con la funcion anterior se obtiene una lista:
97        // certificateInformationList:
98        //      <privateKeyLabel, commonName, expirationDate>
99        //      <privateKeyLabel, commonName, expirationDate>
100        //      <privateKeyLabel, commonName, expirationDate>
101        //
102        // en la cual cada entrada corresponde a la información de un certificado existente en la tarjeta
103        //
104        // Para obtener el label de la clave privada a utilizar para firmar se selecciona el elemento de la lista
105        // (primer indice) y luego el indice 0 de la lista interna
106        QString label = certificateInformationList[0][0];
107        qDebug("label: %s", qPrintable(label));
108
109        // ejemplo de firma
110        std::vector<unsigned char> result = ct->signHash(hash, pin, label);
111
112        qDebug("signature: ");
113        qDebug(ct->toHex(result));
114
115    }catch(std::runtime_error e){
116        qDebug("exception");
117        qDebug(e.what());
118    }
119
120    qDebug("after...");
121
122
123    // ***********************************************************************
124
125
126    //result = ct->signHash(hash, pin, list[0]);
127    //qDebug("**** ");
128    //qDebug(ct->toHex(result));
129
130    //return a.exec();
131    return 0;
132}
133
134
135
136/*
137int main(int argc, char *argv[])
138{
139    QCoreApplication a(argc, argv);
140
141    qDebug("hola mundo");
142
143    CryptoToken* ct = new CryptoToken();
144
145    //qDebug(qPrintable(ct->getInfoCryptoki()));
146
147    char strPin[16];
148    QString PIN("123456");
149    strcpy(strPin,qPrintable(PIN));
150
151    CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
152    CK_SLOT_ID slotID;
153    //CK_SLOT_ID slotID2;
154
155    // inicializacion del criptoki
156    CK_RV rv;
157    if (!ct->initializeCriptoki())
158    {
159        //qDebug("fallo la incializacion de criptoki");
160        return 1;
161    }
162
163    hSession = ct->openSession(strPin, slotID);
164    if (hSession == CK_INVALID_HANDLE)
165    {
166        qDebug("Fallo ptr_SC->openSession");
167        rv = C_Finalize(NULL_PTR);
168        qDebug("C_Finalize: rv = %x",rv);
169        assert(rv == CKR_OK);
170        return 1;
171    }
172
173    // obtencion de la clave privada para firmar los datos
174    CK_OBJECT_HANDLE privateKey = CK_INVALID_HANDLE;
175
176    QString label = "New Key aaraujo";
177    privateKey = ct->getPrivateKey(hSession, slotID, label);
178
179
180    if (privateKey == CK_INVALID_HANDLE)
181    {
182        qDebug("Fallo ptr_SC->getPrivateKey");
183        rv = C_CloseSession(hSession);
184        qDebug("C_CloseSession: rv = %x",rv);
185        assert(rv == CKR_OK);
186        rv = C_Finalize(NULL_PTR);
187        qDebug("C_Finalize: rv = %x",rv);
188        assert(rv == CKR_OK);
189        return 1;
190    }
191
192
193    CK_ULONG slen  = 512;
194    CK_BYTE_PTR sign = new CK_BYTE[slen];
195
196    CK_BYTE hash[64];
197    CK_ULONG hashLen = (CK_ULONG) sizeof(hash);
198
199    // un has recibido del servidor 64 bytes
200    // aaf363de5f571c7ae7976ca52891af440d2934a146860c82f0f5672ddc4ee078
201    // cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04
202
203    QString hashInHex("cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04");
204    qDebug("longitud de hashInHex: %d", hashInHex.size());
205
206
207    memcpy(hash,qPrintable(hashInHex), hashInHex.size());
208    //hash = (unsigned char) EstEID_hex2bin(qPrintable(hashInHex));
209
210
211    if(!ct->signSomeData(hSession, privateKey, hash, hashLen, sign, &slen))
212    {
213        //QMessageBox::warning(this,XCA_TITLE, tr("Process sign for random data failed!"));
214        qDebug("Fallo sc_ptr->signSomeData");
215        rv = C_CloseSession(hSession);
216        qDebug("C_CloseSession: rv = %x",rv);
217        assert(rv == CKR_OK);
218        rv = C_Finalize(NULL_PTR);
219        qDebug("C_Finalize: rv = %x",rv);
220        assert(rv == CKR_OK);
221        return 1;
222    }
223    // aqui debo colocar terminar el arreglo de firma con NULL
224    sign[slen] = '\0';
225
226    qDebug("Valor de la firma signature:");
227    qDebug((const char *) sign);
228    qDebug("Valor de signatureLength: ");
229    qDebug(qPrintable(QString::number(slen)));
230
231    char * signatureInHex = EstEID_bin2hex((const char *) sign, slen);
232
233
234    qDebug("valor de la firma en hexadecimal: %s", signatureInHex);
235
236    assert(signatureInHex == "6cea780ecd21141bfe460d4fd2172f52366c8e357303e9914310f1553951876f2b3d6127571f645b52b8148dfc9f6016e851641ff2c6f785dd84186fe82d802982afd2f88951e22f03dc6982600277a1c18faeda0da89a60d2afb4a51a865bbd4fc3871b8516e8a02afe309b626f8aadb53b6543d99e9c4ab5b334634edcd0898171cb6753b2abe00f64303a1398795e25d64f960ea73041b7178ba539f6bc0cedd16b87f366b4e752fbb7ca4e33fddee8b5adf3bc70f5406a3c69ac8ff62d99ff77a7e340ad6e1d18a7b25e8652653dec5b653a07a8bb289dd6ad9fa876094008864bf475e8589a9cefd2240f2f1f537593e3a94ce01fbea90e9f18bbf3783d");
237
238
239    qDebug("closeSession...");
240
241    ct->closeSession(hSession);
242
243
244    //return a.exec();
245    return 0;
246}
247
248*/
Note: See TracBrowser for help on using the repository browser.