1 | #ifndef CRYPTOTOKEN_H |
---|
2 | #define CRYPTOTOKEN_H |
---|
3 | |
---|
4 | #include <QObject> |
---|
5 | #include <QStringList> |
---|
6 | |
---|
7 | #include "include/pkcs11.h" |
---|
8 | |
---|
9 | class CryptoToken |
---|
10 | { |
---|
11 | |
---|
12 | public: |
---|
13 | |
---|
14 | /** |
---|
15 | * @brief Constructor de la clase CryptoToken |
---|
16 | */ |
---|
17 | explicit CryptoToken(); |
---|
18 | |
---|
19 | /** |
---|
20 | * @brief Destructor de la clase |
---|
21 | */ |
---|
22 | virtual ~CryptoToken(); |
---|
23 | |
---|
24 | /** |
---|
25 | * @brief Inicializa el Criptoki |
---|
26 | * @return retorna si inicializa el Criptoki |
---|
27 | */ |
---|
28 | bool initializeCriptoki(); |
---|
29 | |
---|
30 | /** |
---|
31 | * @brief Finaliza el criptoki |
---|
32 | */ |
---|
33 | void finalize(); |
---|
34 | |
---|
35 | /** |
---|
36 | * @brief Abre una sesion en el dispositivo criptografico |
---|
37 | * @param Pin PIN del dispositivo |
---|
38 | * @param slotID slot asociado a la sesión en el dispositivo |
---|
39 | * @param requiredlogin si se requiere el login, por defecto si |
---|
40 | * @return handle de la sesión |
---|
41 | */ |
---|
42 | CK_SESSION_HANDLE openSession(char * Pin, CK_SLOT_ID & slotID, bool requiredlogin = true); |
---|
43 | |
---|
44 | void closeSession(CK_SESSION_HANDLE hSession); |
---|
45 | |
---|
46 | /** |
---|
47 | * @brief Retorna un handle a la clave privada encontrada dentro del dispositivo |
---|
48 | * @param hSession handle de la sesión abierta en el dispositivo |
---|
49 | * @param slotID identificador de slot |
---|
50 | * @param id etiqueta de la clave dentro del dispositivo |
---|
51 | * @return |
---|
52 | */ |
---|
53 | CK_OBJECT_HANDLE getPrivateKey(CK_SESSION_HANDLE hSession, CK_SLOT_ID slotID, QString id); |
---|
54 | |
---|
55 | /** |
---|
56 | * @brief Retorna un handle a la clave privada encontrada dentro del dispositivo |
---|
57 | * @param hSession handle de la sesión abierta en el dispositivo |
---|
58 | * @param slotID identificador de slot |
---|
59 | * @param id indice del certificado asociado a la clave privada |
---|
60 | * @return handle de la clave privada encontrada |
---|
61 | */ |
---|
62 | CK_OBJECT_HANDLE getPrivateKey(CK_SESSION_HANDLE hSession, CK_SLOT_ID slotID, int certificateIndex); |
---|
63 | |
---|
64 | /** |
---|
65 | * @brief firma digitalmente unos datos con la tarjeta inteligente |
---|
66 | * @param hSession handle a la sesion abierta en la tarjeta |
---|
67 | * @param privateKey handle a la clave privada de la tarjeta |
---|
68 | * @param someData datos a firmar (CK_BYTE_PTR: unsigned char *) |
---|
69 | * @param someDataLen longitud de los datos a firmar |
---|
70 | * @param sign firma de los datos |
---|
71 | * @param signLen longitud de la firma de los datos |
---|
72 | * @return firma electronica de los datos pasados en someData |
---|
73 | */ |
---|
74 | bool signSomeData(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE privateKey, CK_BYTE_PTR someData, |
---|
75 | CK_ULONG someDataLen, CK_BYTE_PTR sign, CK_ULONG_PTR signLen ); |
---|
76 | |
---|
77 | |
---|
78 | /** |
---|
79 | * @brief Sign a hash. |
---|
80 | * |
---|
81 | * @param hashToSign hash to be signed |
---|
82 | * @param pin pin of the cryptographic device |
---|
83 | * @param label label of the private key stored in the cryptographic device |
---|
84 | * @return signed hash |
---|
85 | */ |
---|
86 | std::vector<unsigned char> signHash(QString hashToSign, QString pin, QString label); |
---|
87 | |
---|
88 | /** |
---|
89 | * @brief Sign a hash. |
---|
90 | * |
---|
91 | * @param hashToSign hash a ser firmado |
---|
92 | * @param pin PIN del dispositivo |
---|
93 | * @param certificateIndex indice del certificado seleccionado para firmar |
---|
94 | * @return signed hash |
---|
95 | */ |
---|
96 | std::vector<unsigned char> signHash(QString hashToSign, QString pin, int certificateIndex); |
---|
97 | |
---|
98 | |
---|
99 | //std::vector<CK_OBJECT_HANDLE> findObject(CK_OBJECT_CLASS objectClass, CK_ULONG max) const; |
---|
100 | |
---|
101 | /** |
---|
102 | * @brief Returns a list of private key labels |
---|
103 | * |
---|
104 | * @param pin pin of the cryptographic device |
---|
105 | * @return QStringList of labels |
---|
106 | */ |
---|
107 | QStringList getPrivateKeyLabels(QString pin); |
---|
108 | |
---|
109 | |
---|
110 | QStringList getLabel(); |
---|
111 | |
---|
112 | /** |
---|
113 | * @brief get label of the key |
---|
114 | * @param hSession session handle |
---|
115 | * @param key key handle |
---|
116 | * @return key label as QString |
---|
117 | */ |
---|
118 | QString getKeyLabel(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE key); |
---|
119 | |
---|
120 | /** |
---|
121 | * @brief get certificate information bound to the key |
---|
122 | * @param hSession session handle |
---|
123 | * @param key key handle |
---|
124 | * @return QStringList with commonName and expiration date |
---|
125 | */ |
---|
126 | QStringList getCertificateInformation(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE certificate); |
---|
127 | |
---|
128 | /** |
---|
129 | * @brief Get information of commonName and expiration date of certificates inside the device |
---|
130 | * @return list of QStringList with <commnonName,expirationDate> |
---|
131 | */ |
---|
132 | QList<QStringList> getCertificateCNandExpirationDate(); |
---|
133 | |
---|
134 | /** |
---|
135 | * @brief Returns information about device's certificates. |
---|
136 | * |
---|
137 | * @return QList<QStringList> of information about certificate: <commonName, organizationName, expiration date> |
---|
138 | */ |
---|
139 | QList<QStringList> getDeviceCertificates(); |
---|
140 | |
---|
141 | |
---|
142 | void getCertificateAttributes(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hCert); |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | /** |
---|
148 | * @brief Retorna información del modulo PKCS11 |
---|
149 | * @return cadena con informacion del modulo PKCS11 |
---|
150 | */ |
---|
151 | QString getInfoCryptoki(); |
---|
152 | |
---|
153 | /** |
---|
154 | * @brief Retorna error de pkcs11 en formato de cadena QString |
---|
155 | * @param rv |
---|
156 | * @return cadena con el error |
---|
157 | */ |
---|
158 | QString returnErrorToQString(CK_RV rv); |
---|
159 | |
---|
160 | |
---|
161 | QByteArray toHex(const std::vector<unsigned char> &data); |
---|
162 | |
---|
163 | std::vector<unsigned char> fromHex(const QString &data); |
---|
164 | }; |
---|
165 | |
---|
166 | #endif // CRYPTOTOKEN_H |
---|