forked from arnaudmorin/libgtop11dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.cpp
339 lines (227 loc) · 8.74 KB
/
Session.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* 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
*
*/
#include "Template.hpp"
#include "Session.hpp"
#include "Slot.hpp"
#include "PKCS11Exception.hpp"
#include <boost/foreach.hpp>
unsigned char Session::s_ucSessionObjectIndex = 0;
/*
*/
Session::Session( Slot* a_pSlot, const CK_SESSION_HANDLE& a_hSession, const CK_BBOOL& a_bIsReadWrite ) {
m_Slot = a_pSlot;
m_ulId = a_hSession;
m_bIsReadWrite = a_bIsReadWrite;
m_bIsSearchActive = false;
m_bIsDigestActive = false;
m_bIsDigestActiveRSA = false;
m_bIsDigestVerificationActiveRSA = false;
// The User or the SO has may be performed a login before to open this session
// In this case the state of the session must be updated
getState( );
}
/*
*/
CK_STATE Session::getState( void ) {
if( !m_Slot ) {
throw PKCS11Exception( CKR_TOKEN_NOT_PRESENT );
}
CK_USER_TYPE ulRole = m_Slot->getUserType( );
updateState( ulRole );
return m_ulState;
}
/*
*/
void Session::updateState( const CK_ULONG& a_ulRoleLogged ) {
if( m_bIsReadWrite ) {
switch( a_ulRoleLogged ) {
case CK_UNAVAILABLE_INFORMATION:
m_ulState = CKS_RW_PUBLIC_SESSION;
break;
case CKU_USER:
m_ulState = CKS_RW_USER_FUNCTIONS;
break;
case CKU_SO:
m_ulState = CKS_RW_SO_FUNCTIONS;
break;
}
} else {
switch( a_ulRoleLogged ) {
case CK_UNAVAILABLE_INFORMATION:
m_ulState = CKS_RO_PUBLIC_SESSION;
break;
case CKU_USER:
m_ulState = CKS_RO_USER_FUNCTIONS;
break;
case CKU_SO:
throw PKCS11Exception( CKR_SESSION_READ_ONLY );
}
}
}
/*
*/
StorageObject* Session::getObject( const CK_OBJECT_HANDLE& a_hObject ) {
if( !a_hObject ) {
throw PKCS11Exception( CKR_OBJECT_HANDLE_INVALID );
}
// Find the targeted object
SESSION_OBJECTS::iterator i = m_Objects.find( a_hObject );
if( i == m_Objects.end( ) ) {
throw PKCS11Exception( CKR_OBJECT_HANDLE_INVALID );
}
return i->second;
}
/*
*/
void Session::findObjects( CK_OBJECT_HANDLE_PTR a_phObject, const CK_ULONG& a_ulMaxObjectCount, CK_ULONG_PTR a_pulObjectCount ) {
if( !m_Slot ) {
throw PKCS11Exception( CKR_TOKEN_NOT_PRESENT );
}
bool bIsNotAllowedToAccessPrivateObjects = !m_Slot->isAuthenticated( );
Session::EXPLORED_HANDLES::iterator end = m_SessionObjectsReturnedInSearch.end( );
// For each P11 object
BOOST_FOREACH( const SESSION_OBJECTS::value_type& o, m_Objects ) {
// Check if the search has reached the allowed maximum of objects to search
if( *a_pulObjectCount >= a_ulMaxObjectCount ) {
break;
}
// Check if this object has been already compared to the search template
if( end != m_SessionObjectsReturnedInSearch.find( o->first ) ) {
// This object has already been analysed by a previous call of findObjects for this template
continue;
}
// If the object is private and the user is not logged in
if( o->second->isPrivate( ) && bIsNotAllowedToAccessPrivateObjects )
{
// Then avoid this element.
// Do not add it the list of already explored objects (may be a C_Login can occur)
continue;
}
// Add the object to the list of the objects compared to the search template
m_SessionObjectsReturnedInSearch.insert( o->first );
// If the template is NULL then return all objects
if( !_searchTempl ) {
a_phObject[ *a_pulObjectCount ] = o->first;
++(*a_pulObjectCount);
} else {
// The template is not NULL.
bool match = true;
// In this case the template attributes have to be compared to the objects ones.
BOOST_FOREACH( CK_ATTRIBUTE& t, _searchTempl->getAttributes( ) ) {
if( ! o->second->compare( t ) ) {
match = false;
break;
}
}
// The attributes match
if( match ) {
// Add the object handle to the outgoing list
a_phObject[ *a_pulObjectCount ] = o->first;
// Increment the number of found objects
++(*a_pulObjectCount);
}
}
}
}
/*
*/
void Session::deleteObject( const CK_OBJECT_HANDLE& a_hObject ) {
if( !m_Slot ) {
throw PKCS11Exception( CKR_TOKEN_NOT_PRESENT );
}
// Find the targeted object
StorageObject* o = getObject( a_hObject );
// if this is a readonly session and user is not logged
// then only public session objects can be created
if( !m_bIsReadWrite && o->isToken( ) ) {
throw PKCS11Exception( CKR_SESSION_READ_ONLY );
}
if( o->isPrivate( ) && !m_Slot->isAuthenticated( ) ) {
throw PKCS11Exception( CKR_USER_NOT_LOGGED_IN );
}
try {
m_Objects.erase( a_hObject );
} catch( ... ) {
throw PKCS11Exception( CKR_OBJECT_HANDLE_INVALID );
}
}
/*
*/
void Session::getAttributeValue( const CK_OBJECT_HANDLE& a_hObject, CK_ATTRIBUTE_PTR a_pTemplate, const CK_ULONG& a_ulCount ) {
if( !m_Slot ) {
throw PKCS11Exception( CKR_TOKEN_NOT_PRESENT );
}
// Find the targeted object
StorageObject* o = getObject( a_hObject );
if( o->isPrivate( ) && !m_Slot->isAuthenticated( ) ) {
for( u4 i = 0 ; i < a_ulCount ; ++i ) {
a_pTemplate[ i ].ulValueLen = (CK_ULONG)CK_UNAVAILABLE_INFORMATION;
}
throw PKCS11Exception( CKR_USER_NOT_LOGGED_IN );
}
for( CK_ULONG i = 0 ; i < a_ulCount ; ++i ) {
o->getAttribute( &a_pTemplate[ i ] );
}
}
/*
*/
void Session::setAttributeValue( const CK_OBJECT_HANDLE& a_hObject, CK_ATTRIBUTE_PTR a_pTemplate, const CK_ULONG& a_ulCount ) {
if( !m_Slot ) {
throw PKCS11Exception( CKR_TOKEN_NOT_PRESENT );
}
// Find the targeted object
StorageObject* o = getObject( a_hObject );
if( o->isPrivate( ) && !m_Slot->isAuthenticated( ) ) {
throw PKCS11Exception( CKR_USER_NOT_LOGGED_IN );
}
for( CK_ULONG i = 0 ; i < a_ulCount ; ++i ) {
o->setAttribute( a_pTemplate[ i ], false );
}
}
/*
*/
CK_OBJECT_HANDLE Session::computeObjectHandle( const CK_OBJECT_CLASS& a_ulClass, const bool& a_bIsPrivate ) {
// Register the session object id (value from 0 to 255)
unsigned char ucByte1 = ++s_ucSessionObjectIndex;
// Register the object class and if the object is private:
// Private Data 1000 [08] = set class to CKO_DATA (0x00) and Private to TRUE (0x08)
// Public Data 0000 [00] = set class to CKO_DATA (0x00) and Private to FALSE (0x00)
// Private Certificate 1001 [09] = set class to CKO_CERTIFICATE (0x01) and Private to TRUE (0x08)
// Public Certificate 0001 [01] = set class to CKO_CERTIFICATE (0x01) and Private to FALSE (0x00)
// Private Public Key 1010 [10] = set class to CKO_PUBLIC_KEY (0x02) and Private to TRUE (0x08)
// Public Public Key 0010 [02] = set class to CKO_PUBLIC_KEY (0x02) and Private to FALSE (0x00)
// Private Private Key 1011 [11] = set class to CKO_PRIVATE_KEY (0x03) and Private to TRUE (0x08)
// Public Private Key 0011 [03] = set class to CKO_PRIVATE_KEY (0x03) and Private to FALSE (0x00)
unsigned char ucByte2 = (unsigned char)a_ulClass + ( a_bIsPrivate ? 0x08 : 0x00 );
// Register if the object is owned by the token (value 0) or the session (value corresponding to the session id from 1 to 255)
unsigned char ucByte3 = (unsigned char) ( 0x000000FF & m_ulId );
// Register the slot id
unsigned char ucByte4 = (unsigned char) ( 0x000000FF & m_Slot->getSlotId( ) );
// Compute the object handle: byte4 as Slot Id, byte3 as Token/Session, byte2 as attributes and byte1 as object Id
CK_OBJECT_HANDLE h = ( ucByte4 << 24 ) + ( ucByte3 << 16 ) + ( ucByte2 << 8 )+ ucByte1;
return h;
}
/*
*/
void Session::addObject( StorageObject* a_pObj, CK_OBJECT_HANDLE_PTR a_phObject ) {
*a_phObject = computeObjectHandle( a_pObj->getClass( ), a_pObj->isPrivate( ) );
CK_OBJECT_HANDLE h = *a_phObject;
m_Objects.insert( h, a_pObj );
}