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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/**
* Copyright(c) 2011 Trusted Logic. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name Trusted Logic nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Implementation Notes:
*
* The PKCS11 session handle is directly mapped on the
* Trusted Foundations Software session handle (S_HANDLE).
*/
#include "pkcs11_internal.h"
/* ------------------------------------------------------------------------
Public Functions
------------------------------------------------------------------------- */
CK_RV PKCS11_EXPORT C_OpenSession(CK_SLOT_ID slotID, /* the slot's ID */
CK_FLAGS flags, /* defined in CK_SESSION_INFO */
CK_VOID_PTR pApplication, /* pointer passed to callback */
CK_NOTIFY Notify, /* notification callback function */
CK_SESSION_HANDLE* phSession) /* receives new session handle */
{
CK_RV nErrorCode = CKR_OK;
uint32_t nErrorOrigin = TEEC_ORIGIN_API;
TEEC_Result nTeeError;
TEEC_Operation sOperation;
PPKCS11_PRIMARY_SESSION_CONTEXT pSession = NULL;
PPKCS11_SECONDARY_SESSION_CONTEXT pSecondarySession = NULL;
uint32_t nLoginType;
uint32_t nLoginData = 0;
void* pLoginData = NULL;
bool bIsPrimarySession;
char* pSignatureFile = NULL;
uint32_t nSignatureFileLen = 0;
uint8_t nParamType3 = TEEC_NONE;
/* Prevent the compiler from complaining about unused parameters */
do{(void)pApplication;}while(0);
do{(void)Notify;}while(0);
if (phSession == NULL)
{
return CKR_ARGUMENTS_BAD;
}
/* Check Cryptoki is initialized */
if (!g_bCryptokiInitialized)
{
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if ((flags & CKVF_OPEN_SUB_SESSION) == 0)
{
*phSession = CK_INVALID_HANDLE;
/*
* Allocate the session context
*/
pSession = (PPKCS11_PRIMARY_SESSION_CONTEXT)malloc(sizeof(PKCS11_PRIMARY_SESSION_CONTEXT));
if (pSession == NULL)
{
return CKR_DEVICE_MEMORY;
}
pSession->sHeader.nMagicWord = PKCS11_SESSION_MAGIC;
pSession->sHeader.nSessionTag = PKCS11_PRIMARY_SESSION_TAG;
memset(&pSession->sSession, 0, sizeof(TEEC_Session));
pSession->sSecondarySessionTable.pRoot = NULL_PTR;
/* The structure must be initialized first (in a portable manner)
to make it work on Win32 */
memset(&pSession->sSecondarySessionTableMutex, 0,
sizeof(pSession->sSecondarySessionTableMutex));
libMutexInit(&pSession->sSecondarySessionTableMutex);
switch (slotID)
{
case CKV_TOKEN_SYSTEM_SHARED:
case CKV_TOKEN_USER_SHARED:
nLoginType = TEEC_LOGIN_PUBLIC;
break;
case CKV_TOKEN_SYSTEM:
case CKV_TOKEN_USER:
default:
nLoginType = TEEC_LOGIN_AUTHENTICATION;
break;
}
/* Group tokens */
if ((slotID >= 0x00010000) && (slotID <= 0x0002FFFF))
{
nLoginType = TEEC_LOGIN_GROUP;
/* The 16 lower-order bits encode the group identifier */
nLoginData = (uint32_t)slotID & 0x0000FFFF;
pLoginData = (void*)&nLoginData;
/* Update the slotID for the system / PKCS11 service */
if ((slotID >= 0x00010000) && (slotID <= 0x0001FFFF))
{
/* System group token */
slotID = 3; /* CKV_TOKEN_SYSTEM_GROUP */
}
else /* ((slotID >= 0x00020000) && (slotID <= 0x0002FFFF)) */
{
/* User group token */
slotID = 0x4014; /* CKV_TOKEN_USER_GROUP */
}
}
retry:
memset(&sOperation, 0, sizeof(TEEC_Operation));
if (nLoginType == TEEC_LOGIN_AUTHENTICATION)
{
nTeeError = TEEC_ReadSignatureFile((void **)&pSignatureFile, &nSignatureFileLen);
if (nTeeError != TEEC_ERROR_ITEM_NOT_FOUND)
{
if (nTeeError != TEEC_SUCCESS)
{
goto error;
}
sOperation.params[3].tmpref.buffer = pSignatureFile;
sOperation.params[3].tmpref.size = nSignatureFileLen;
nParamType3 = TEEC_MEMREF_TEMP_INPUT;
}
else
{
/* No signature file found.
* Should use LOGIN_APPLICATION for now
* Can not use TEEC_LOGIN_AUTHENTICATION as this means that all .exe wil need a signature file
* - a bit annoying for when passing the tests
*/
nLoginType = TEEC_LOGIN_USER_APPLICATION;
}
}
sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, nParamType3);
nTeeError = TEEC_OpenSession(&g_sContext,
&pSession->sSession, /* OUT session */
&SERVICE_UUID, /* destination UUID */
nLoginType, /* connectionMethod */
pLoginData, /* connectionData */
&sOperation, /* IN OUT operation */
NULL /* OUT returnOrigin, optional */
);
if (nTeeError != TEEC_SUCCESS)
{
/* No need of the returnOrigin as this is not specific to P11 */
if ( (nTeeError == TEEC_ERROR_NOT_SUPPORTED) &&
(nLoginType == TEEC_LOGIN_AUTHENTICATION))
{
/* We could not open a session with the login TEEC_LOGIN_AUTHENTICATION */
/* If it is not supported by the product, */
/* retry with fallback to TEEC_LOGIN_USER_APPLICATION */
nLoginType = TEEC_LOGIN_USER_APPLICATION;
goto retry;
}
/* The ERROR_ACCESS_DENIED, if returned, will be converted into CKR_TOKEN_NOT_PRESENT
* For the External Cryptographic API, this means that the authentication
* of the calling application fails.
*/
goto error;
}
memset(&sOperation, 0, sizeof(TEEC_Operation));
sOperation.params[0].value.a = slotID;
sOperation.params[0].value.b = flags; /* access flags */
sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
nTeeError = TEEC_InvokeCommand(&pSession->sSession,
SERVICE_SYSTEM_PKCS11_C_OPEN_SESSION_COMMAND_ID & 0x00007FFF,
&sOperation, /* IN OUT operation */
&nErrorOrigin /* OUT returnOrigin, optional */
);
if (nTeeError != TEEC_SUCCESS)
{
goto error;
}
*phSession = (CK_SESSION_HANDLE)pSession;
pSession->hCryptoSession = sOperation.params[0].value.a;
return CKR_OK;
}
else
{
bool bResult;
/* Check that {*phSession} is a valid primary session handle */
if ((!ckInternalSessionIsOpenedEx(*phSession, &bIsPrimarySession)) ||
(!bIsPrimarySession))
{
return CKR_SESSION_HANDLE_INVALID;
}
pSession = (PPKCS11_PRIMARY_SESSION_CONTEXT)(*phSession);
/* allocate the secondary session context */
pSecondarySession = (PKCS11_SECONDARY_SESSION_CONTEXT*)malloc(sizeof(PKCS11_SECONDARY_SESSION_CONTEXT));
if (pSecondarySession == NULL)
{
return CKR_DEVICE_MEMORY;
}
pSecondarySession->sHeader.nMagicWord = PKCS11_SESSION_MAGIC;
pSecondarySession->sHeader.nSessionTag = PKCS11_SECONDARY_SESSION_TAG;
pSecondarySession->pPrimarySession = pSession;
libMutexLock(&pSession->sSecondarySessionTableMutex);
bResult = libObjectHandle16Add(&pSession->sSecondarySessionTable,
&pSecondarySession->sSecondarySessionNode);
libMutexUnlock(&pSession->sSecondarySessionTableMutex);
if (bResult == false)
{
free(pSecondarySession);
return CKR_DEVICE_MEMORY;
}
memset(&sOperation, 0, sizeof(TEEC_Operation));
sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
nTeeError = TEEC_InvokeCommand(&pSession->sSession,
(pSession->hCryptoSession << 16) |
(1 << 15) |
(SERVICE_SYSTEM_PKCS11_C_OPEN_SESSION_COMMAND_ID & 0x00007FFF),
&sOperation, /* IN OUT operation */
&nErrorOrigin /* OUT returnOrigin, optional */
);
if (nTeeError != TEEC_SUCCESS)
{
goto error;
}
*phSession = (CK_SESSION_HANDLE)pSecondarySession;
pSecondarySession->hSecondaryCryptoSession = sOperation.params[0].value.a;
return CKR_OK;
}
error:
nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
nTeeError :
ckInternalTeeErrorToCKError(nTeeError));
if ((flags & CKVF_OPEN_SUB_SESSION) == 0)
{
libMutexDestroy(&pSession->sSecondarySessionTableMutex);
free(pSession);
}
else
{
libMutexLock(&pSession->sSecondarySessionTableMutex);
libObjectHandle16Remove(&pSession->sSecondarySessionTable,&pSecondarySession->sSecondarySessionNode);
libMutexUnlock(&pSession->sSecondarySessionTableMutex);
free(pSecondarySession);
}
return nErrorCode;
}
CK_RV PKCS11_EXPORT C_CloseSession(CK_SESSION_HANDLE hSession) /* the session's handle */
{
CK_RV nErrorCode = CKR_OK;
uint32_t nErrorOrigin = TEEC_ORIGIN_API;
TEEC_Result nTeeError;
TEEC_Operation sOperation;
bool bIsPrimarySession;
/* Check Cryptoki is initialized */
if (!g_bCryptokiInitialized)
{
return CKR_CRYPTOKI_NOT_INITIALIZED;
}
if (!ckInternalSessionIsOpenedEx(hSession, &bIsPrimarySession))
{
return CKR_SESSION_HANDLE_INVALID;
}
if (bIsPrimarySession)
{
LIB_OBJECT_NODE_HANDLE16* pObject;
PPKCS11_SECONDARY_SESSION_CONTEXT pSecSession;
PPKCS11_PRIMARY_SESSION_CONTEXT pSession = (PPKCS11_PRIMARY_SESSION_CONTEXT)hSession;
hSession = pSession->hCryptoSession;
memset(&sOperation, 0, sizeof(TEEC_Operation));
sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);
nTeeError = TEEC_InvokeCommand(&pSession->sSession,
(pSession->hCryptoSession << 16 ) |
(SERVICE_SYSTEM_PKCS11_C_CLOSE_SESSION_COMMAND_ID & 0x00007FFF),
&sOperation, /* IN OUT operation */
&nErrorOrigin /* OUT returnOrigin, optional */
);
if (nTeeError != TEEC_SUCCESS)
{
goto end;
}
TEEC_CloseSession(&pSession->sSession);
memset(&pSession->sSession, 0, sizeof(TEEC_Session));
/* Free all secondary session contexts */
libMutexLock(&pSession->sSecondarySessionTableMutex);
pObject = libObjectHandle16RemoveOne(&pSession->sSecondarySessionTable);
while (pObject != NULL)
{
/* find all secondary session contexts,
and release associated resources */
pSecSession = LIB_OBJECT_CONTAINER_OF(pObject, //ptr
PKCS11_SECONDARY_SESSION_CONTEXT,//type
sSecondarySessionNode);//member
/* free secondary session context */
free(pSecSession);
pObject = libObjectHandle16RemoveOne(&pSession->sSecondarySessionTable);
}
libMutexUnlock(&pSession->sSecondarySessionTableMutex);
libMutexDestroy(&pSession->sSecondarySessionTableMutex);
/* free primary session context */
free(pSession);
}
else
{
PPKCS11_SECONDARY_SESSION_CONTEXT pSecSession = (PPKCS11_SECONDARY_SESSION_CONTEXT)hSession;
PPKCS11_PRIMARY_SESSION_CONTEXT pSession;
uint32_t nCommandID = ( (pSecSession->hSecondaryCryptoSession & 0xFFFF) << 16 ) |
(1 << 15) |
(SERVICE_SYSTEM_PKCS11_C_CLOSE_SESSION_COMMAND_ID & 0x00007FFF);
/* every pre-check are fine, then, update the local handles */
hSession = pSecSession->pPrimarySession->hCryptoSession;
pSession = (PPKCS11_PRIMARY_SESSION_CONTEXT)(pSecSession->pPrimarySession);
memset(&sOperation, 0, sizeof(TEEC_Operation));
sOperation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);
nTeeError = TEEC_InvokeCommand(&pSession->sSession,
nCommandID,
&sOperation, /* IN OUT operation */
&nErrorOrigin /* OUT returnOrigin, optional */
);
if (nTeeError != TEEC_SUCCESS)
{
goto end;
}
/* remove the object from the table */
libMutexLock(&pSession->sSecondarySessionTableMutex);
libObjectHandle16Remove(&pSecSession->pPrimarySession->sSecondarySessionTable, &pSecSession->sSecondarySessionNode);
libMutexUnlock(&pSession->sSecondarySessionTableMutex);
/* free secondary session context */
free(pSecSession);
}
end:
nErrorCode = (nErrorOrigin == TEEC_ORIGIN_TRUSTED_APP ?
nTeeError :
ckInternalTeeErrorToCKError(nTeeError));
return nErrorCode;
}
CK_RV PKCS11_EXPORT C_Login(CK_SESSION_HANDLE hSession, /* the session's handle */
CK_USER_TYPE userType, /* the user type */
const CK_UTF8CHAR* pPin, /* the user's PIN */
CK_ULONG ulPinLen) /* the length of the PIN */
{
/* Prevent the compiler from complaining about unused variables */
do{(void)hSession;}while(0);
do{(void)userType;}while(0);
do{(void)pPin;}while(0);
do{(void)ulPinLen;}while(0);
return CKR_OK;
}
CK_RV PKCS11_EXPORT C_Logout(CK_SESSION_HANDLE hSession) /* the session's handle */
{
do{(void)hSession;}while(0);
return CKR_OK;
}
|