-
Notifications
You must be signed in to change notification settings - Fork 1
/
Token.hpp
309 lines (171 loc) · 9.91 KB
/
Token.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* PKCS#11 library for .Net smart cards
* Copyright (C) 2007-2009 Gemalto <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef __GEMALTO_TOKEN__
#define __GEMALTO_TOKEN__
#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/ptr_container/ptr_set.hpp>
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/random.hpp>
#include <string>
#include <vector>
#include "MiniDriver.hpp"
#include "Device.hpp"
#include "Session.hpp"
#include "Pkcs11ObjectStorage.hpp"
#include "Pkcs11ObjectKeyPrivateRSA.hpp"
#include "Pkcs11ObjectCertificateX509PublicKey.hpp"
#include "MiniDriverException.hpp"
#include "Pkcs11ObjectKeyPublicRSA.hpp"
class Slot;
/*
*/
class Token {
public:
typedef boost::ptr_map< CK_OBJECT_HANDLE, StorageObject > TOKEN_OBJECTS;
static const unsigned long FLAG_OBJECT_TOKEN = 0x00000000;
static const unsigned long MASK_OBJECT_TOKEN = 0x00FF0000;
Token( Slot*, Device* );
inline virtual ~Token( ) { clear( ); }
void login( const CK_ULONG&, Marshaller::u1Array* );
void logout( void );
void generateRandom( CK_BYTE_PTR, const CK_ULONG& );
void addObject( StorageObject*, CK_OBJECT_HANDLE_PTR, const bool& a_bRegisterObject = true );
void addObjectPrivateKey( RSAPrivateKeyObject*, CK_OBJECT_HANDLE_PTR );
void addObjectCertificate( X509PubKeyCertObject*, CK_OBJECT_HANDLE_PTR );
void addObjectPublicKey( Pkcs11ObjectKeyPublicRSA*, CK_OBJECT_HANDLE_PTR );
void deleteObject( const CK_OBJECT_HANDLE& );
// === TEST
//inline void findObjectsInit( void ) { m_TokenObjectsReturnedInSearch.clear( ); synchronizeIfSmartCardContentHasChanged( ); }
inline void findObjectsInit( void ) { m_TokenObjectsReturnedInSearch.clear( ); try{ synchronizeIfSmartCardContentHasChanged( ); } catch( ... ){} }
void findObjects( Session*, CK_OBJECT_HANDLE_PTR, const CK_ULONG&, CK_ULONG_PTR );
void getAttributeValue( const CK_OBJECT_HANDLE&, CK_ATTRIBUTE_PTR, const CK_ULONG& );
void setAttributeValue( const CK_OBJECT_HANDLE&, CK_ATTRIBUTE_PTR, const CK_ULONG& );
void generateKeyPair( Pkcs11ObjectKeyPublicRSA*, RSAPrivateKeyObject*, CK_OBJECT_HANDLE_PTR , CK_OBJECT_HANDLE_PTR );
StorageObject* getObject( const CK_OBJECT_HANDLE& );
void sign( const RSAPrivateKeyObject*, Marshaller::u1Array*, const CK_ULONG&, CK_BYTE_PTR );
void decrypt( const StorageObject*, Marshaller::u1Array*, const CK_ULONG&, CK_BYTE_PTR , CK_ULONG_PTR );
void verify( const StorageObject*, Marshaller::u1Array*, const CK_ULONG&, Marshaller::u1Array* );
void encrypt( const StorageObject*, Marshaller::u1Array*, const CK_ULONG&,CK_BYTE_PTR );
void initToken( Marshaller::u1Array*, Marshaller::u1Array* );
void initPIN( Marshaller::u1Array*, Marshaller::u1Array* );
void setPIN( Marshaller::u1Array*, Marshaller::u1Array* );
inline const CK_ULONG& getLoggedRole( void ) { return m_RoleLogged; }
inline void setLoggedRole( const CK_ULONG& r ) { m_RoleLogged = r; }
inline CK_TOKEN_INFO& getTokenInfo( void ) { return m_TokenInfo; }
inline bool isToken( const CK_OBJECT_HANDLE& a_hObject ) { return ( ( a_hObject & MASK_OBJECT_TOKEN ) == FLAG_OBJECT_TOKEN ); }
bool synchronizeIfSmartCardContentHasChanged( void );
static CK_RV checkException( MiniDriverException& );
private:
typedef std::vector< StorageObject* > OBJECTS;
std::string g_stPathPKCS11;
std::string g_stPathTokenInfo;
std::string g_stPrefixData;
std::string g_stPrefixKeyPublic;
std::string g_stPrefixKeyPrivate;
std::string g_stPrefixPublicObject;
std::string g_stPrefixPrivateObject;
std::string g_stPrefixRootCertificate;
bool checkSmartCardContent( void );
bool m_bCheckSmartCardContentDone;
void initializeObjectIndex( void );
void checkTokenInfo( void );
void setTokenInfo( void );
void writeTokenInfo( void );
void readTokenInfo( void );
void createTokenInfo( void );
//void initializeTokenInfo( void );
CK_OBJECT_HANDLE computeObjectHandle( const CK_OBJECT_CLASS&, const bool& );
inline void clear( void ) { m_Objects.clear( ); }
void authenticateUser( Marshaller::u1Array* );
void authenticateAdmin( Marshaller::u1Array* );
Marshaller::u1Array* PadRSAPKCS1v15( Marshaller::u1Array*, const CK_ULONG& );
Marshaller::u1Array* PadRSAX509( Marshaller::u1Array*, const CK_ULONG& );
Marshaller::u1Array* EncodeHashForSigning( Marshaller::u1Array*, const CK_ULONG&, const CK_ULONG& );
void verifyRSAPKCS1v15( Marshaller::u1Array*, Marshaller::u1Array*, const unsigned int& );
void verifyRSAX509( Marshaller::u1Array*, Marshaller::u1Array*, const unsigned int& );
void verifyHash( Marshaller::u1Array*, Marshaller::u1Array*, const unsigned int&, const CK_ULONG& );
void deleteObjectFromCard( StorageObject* );
void computeObjectFileName( StorageObject*, std::string& );
void writeObject( StorageObject* );
CK_OBJECT_HANDLE registerStorageObject( StorageObject* );
void unregisterStorageObject( const CK_OBJECT_HANDLE& );
CK_OBJECT_HANDLE computeObjectHandle( void );
void synchronizeObjects( void );
void synchronizePublicObjects( void );
void synchronizePrivateObjects( void );
void synchronizePIN( void );
void synchronizePublicCertificateAndKeyObjects( void );
void synchronizePrivateCertificateAndKeyObjects( void );
void synchronizePublicDataObjects( void );
void synchronizePrivateDataObjects( void );
void synchronizePrivateKeyObjects( void );
void synchronizeRootCertificateObjects( void );
void synchronizeEmptyContainers( void );
void createCertificateFromMiniDriverFile( const std::string&, const unsigned char&, const unsigned char& );
void createCertificateFromPKCS11ObjectFile( const std::string&, const std::string& );
void createPublicKeyFromPKCS11ObjectFile( const std::string& );
void createPublicKeyFromMiniDriverFile( const std::string&, const unsigned char& a_ucIndex, const unsigned int& a_ucKeySpec, Marshaller::u1Array*, Marshaller::u1Array* );
void createPrivateKeyFromPKCS11ObjectFile( const std::string& );
void createPrivateKeyFromMiniDriverFile( const std::string&, const unsigned char&, const unsigned int&, Marshaller::u1Array*, Marshaller::u1Array* );
bool isPrivate( const CK_OBJECT_HANDLE& a_ObjectHandle ) { return ( ( ( a_ObjectHandle >> 8 ) & 0x000000FF ) >= 0x00000010 ); }
void checkAuthenticationStatus( CK_ULONG, MiniDriverException& );
void printObject( StorageObject* );
Marshaller::u1Array* computeSHA1( const unsigned char* a_pData, const size_t& a_uiLength );
boost::mt19937 m_RandomNumberGenerator;
Device* m_Device;
TOKEN_OBJECTS m_Objects;
std::vector< StorageObject* > m_ObjectsToCreate;
//std::vector< std::string > m_ObjectsToDelete;
CK_TOKEN_INFO m_TokenInfo;
CK_ULONG m_RoleLogged;
unsigned char m_uiObjectIndex;
bool m_bCreateDirectoryP11;
bool m_bCreateTokenInfoFile;
bool m_bWriteTokenInfoFile;
bool m_bSynchronizeObjectsPublic;
bool m_bSynchronizeObjectsPrivate;
std::set< CK_OBJECT_HANDLE > m_TokenObjectsReturnedInSearch;
Slot* m_pSlot;
unsigned char computeIndex( const std::string& );
void generateDefaultAttributesCertificate( X509PubKeyCertObject* );
void generateDefaultAttributesKeyPublic( Pkcs11ObjectKeyPublicRSA* );
void generateDefaultAttributesKeyPrivate( RSAPrivateKeyObject* );
void generateLabel( boost::shared_ptr< Marshaller::u1Array>&, boost::shared_ptr< Marshaller::u1Array>& );
void generateID(boost::shared_ptr< Marshaller::u1Array>&, boost::shared_ptr< Marshaller::u1Array>& );
void generateSubject( boost::shared_ptr< Marshaller::u1Array>&, boost::shared_ptr< Marshaller::u1Array>& );
void generateSerialNumber( boost::shared_ptr< Marshaller::u1Array>&, boost::shared_ptr< Marshaller::u1Array>& );
void generateIssuer( boost::shared_ptr< Marshaller::u1Array>&, boost::shared_ptr< Marshaller::u1Array>& );
void generatePublicKeyModulus( boost::shared_ptr< Marshaller::u1Array>&, boost::shared_ptr< Marshaller::u1Array>&, u8& );
void generateRootAndSmartCardLogonFlags( boost::shared_ptr< Marshaller::u1Array>&, bool&, unsigned long&, bool& );
void searchContainerIndex( boost::shared_ptr< Marshaller::u1Array>&, unsigned char&, unsigned char& );
void setDefaultAttributesCertificate( X509PubKeyCertObject* );
void setDefaultAttributesKeyPublic( Pkcs11ObjectKeyPublicRSA* );
void setDefaultAttributesKeyPrivate( RSAPrivateKeyObject* );
void setContainerIndexToCertificate( boost::shared_ptr< Marshaller::u1Array>&, const unsigned char&, const unsigned char& );
void setContainerIndexToKeyPublic( boost::shared_ptr< Marshaller::u1Array>&, const unsigned char&, const unsigned char& );
void computeObjectNameData( std::string&, /*const*/ StorageObject* );
void computeObjectNamePublicKey( std::string&, /*const*/ StorageObject* );
void computeObjectNamePrivateKey( std::string&, /*const*/ StorageObject* );
void computeObjectNameCertificate( std::string&, /*const*/ StorageObject* );
void incrementObjectIndex( void );
bool isObjectNameValid( const std::string&, const MiniDriverFiles::FILES_NAME& );
};
#endif // __GEMALTO_TOKEN__