source: terepaima/signHash/main.cpp @ ce57b45

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

Avance en función getPrivateKeyLabels

  • Property mode set to 100644
File size: 5.4 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    // 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    qDebug("lista de claves privadas:");
70    ct->getPrivateKeyLabels("123456");
71
72    //return a.exec();
73    return 0;
74}
75
76
77
78/*
79int main(int argc, char *argv[])
80{
81    QCoreApplication a(argc, argv);
82
83    qDebug("hola mundo");
84
85    CryptoToken* ct = new CryptoToken();
86
87    //qDebug(qPrintable(ct->getInfoCryptoki()));
88
89    char strPin[16];
90    QString PIN("123456");
91    strcpy(strPin,qPrintable(PIN));
92
93    CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
94    CK_SLOT_ID slotID;
95    //CK_SLOT_ID slotID2;
96
97    // inicializacion del criptoki
98    CK_RV rv;
99    if (!ct->initializeCriptoki())
100    {
101        //qDebug("fallo la incializacion de criptoki");
102        return 1;
103    }
104
105    hSession = ct->openSession(strPin, slotID);
106    if (hSession == CK_INVALID_HANDLE)
107    {
108        qDebug("Fallo ptr_SC->openSession");
109        rv = C_Finalize(NULL_PTR);
110        qDebug("C_Finalize: rv = %x",rv);
111        assert(rv == CKR_OK);
112        return 1;
113    }
114
115    // obtencion de la clave privada para firmar los datos
116    CK_OBJECT_HANDLE privateKey = CK_INVALID_HANDLE;
117
118    QString label = "New Key aaraujo";
119    privateKey = ct->getPrivateKey(hSession, slotID, label);
120
121
122    if (privateKey == CK_INVALID_HANDLE)
123    {
124        qDebug("Fallo ptr_SC->getPrivateKey");
125        rv = C_CloseSession(hSession);
126        qDebug("C_CloseSession: rv = %x",rv);
127        assert(rv == CKR_OK);
128        rv = C_Finalize(NULL_PTR);
129        qDebug("C_Finalize: rv = %x",rv);
130        assert(rv == CKR_OK);
131        return 1;
132    }
133
134
135    CK_ULONG slen  = 512;
136    CK_BYTE_PTR sign = new CK_BYTE[slen];
137
138    CK_BYTE hash[64];
139    CK_ULONG hashLen = (CK_ULONG) sizeof(hash);
140
141    // un has recibido del servidor 64 bytes
142    // aaf363de5f571c7ae7976ca52891af440d2934a146860c82f0f5672ddc4ee078
143    // cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04
144
145    QString hashInHex("cdbc23b0c23e164225acd0dbf8afecc420ca61ded483a0a43d88d4a76916cc04");
146    qDebug("longitud de hashInHex: %d", hashInHex.size());
147
148
149    memcpy(hash,qPrintable(hashInHex), hashInHex.size());
150    //hash = (unsigned char) EstEID_hex2bin(qPrintable(hashInHex));
151
152
153    if(!ct->signSomeData(hSession, privateKey, hash, hashLen, sign, &slen))
154    {
155        //QMessageBox::warning(this,XCA_TITLE, tr("Process sign for random data failed!"));
156        qDebug("Fallo sc_ptr->signSomeData");
157        rv = C_CloseSession(hSession);
158        qDebug("C_CloseSession: rv = %x",rv);
159        assert(rv == CKR_OK);
160        rv = C_Finalize(NULL_PTR);
161        qDebug("C_Finalize: rv = %x",rv);
162        assert(rv == CKR_OK);
163        return 1;
164    }
165    // aqui debo colocar terminar el arreglo de firma con NULL
166    sign[slen] = '\0';
167
168    qDebug("Valor de la firma signature:");
169    qDebug((const char *) sign);
170    qDebug("Valor de signatureLength: ");
171    qDebug(qPrintable(QString::number(slen)));
172
173    char * signatureInHex = EstEID_bin2hex((const char *) sign, slen);
174
175
176    qDebug("valor de la firma en hexadecimal: %s", signatureInHex);
177
178    assert(signatureInHex == "6cea780ecd21141bfe460d4fd2172f52366c8e357303e9914310f1553951876f2b3d6127571f645b52b8148dfc9f6016e851641ff2c6f785dd84186fe82d802982afd2f88951e22f03dc6982600277a1c18faeda0da89a60d2afb4a51a865bbd4fc3871b8516e8a02afe309b626f8aadb53b6543d99e9c4ab5b334634edcd0898171cb6753b2abe00f64303a1398795e25d64f960ea73041b7178ba539f6bc0cedd16b87f366b4e752fbb7ca4e33fddee8b5adf3bc70f5406a3c69ac8ff62d99ff77a7e340ad6e1d18a7b25e8652653dec5b653a07a8bb289dd6ad9fa876094008864bf475e8589a9cefd2240f2f1f537593e3a94ce01fbea90e9f18bbf3783d");
179
180
181    qDebug("closeSession...");
182
183    ct->closeSession(hSession);
184
185
186    //return a.exec();
187    return 0;
188}
189
190*/
Note: See TracBrowser for help on using the repository browser.