source: terepaima/signHash/main.cpp @ 0f7c727

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

Agregado directorio signHash que corrresponde a prueba de concepto para firmar un hash con un dispositivo criptográfico en C++. El objetivo es que desde Terepaima se pueda firmar el hash que se recibe del servicio web Murachí.

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