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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Session Management Functions
* (as defined in PKCS#11 spec spection 11.6)
*/
#include <string.h>
#include "metaGlobal.h"
extern meta_session_t *meta_sessionlist_head;
extern pthread_rwlock_t meta_sessionlist_lock;
extern CK_ULONG num_meta_sessions;
extern CK_ULONG num_rw_meta_sessions;
/*
* meta_OpenSession
*
* NOTES:
* 1) The pApplication and Notify args are not used, as the metaslot does not
* support application callbacks.
* 2) the slotID argument is not checked or used because this function
* is only called from the framework.
*/
/* ARGSUSED */
CK_RV
meta_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
{
meta_session_t *new_session;
CK_RV rv;
if (!metaslot_enabled) {
return (CKR_SLOT_ID_INVALID);
}
if (phSession == NULL) {
return (CKR_ARGUMENTS_BAD);
}
/* Check for any unknown flags. */
if (flags & ~(CKF_SERIAL_SESSION | CKF_RW_SESSION)) {
return (CKR_ARGUMENTS_BAD);
}
if (!(flags & CKF_SERIAL_SESSION)) {
return (CKR_SESSION_PARALLEL_NOT_SUPPORTED);
}
if (meta_slotManager_token_write_protected() &&
(flags & CKF_RW_SESSION)) {
return (CKR_TOKEN_WRITE_PROTECTED);
}
rv = meta_session_alloc(&new_session);
if (rv != CKR_OK)
return (rv);
new_session->session_flags = flags;
rv = meta_session_activate(new_session);
if (rv != CKR_OK) {
meta_session_dealloc(new_session);
return (rv);
}
*phSession = (CK_SESSION_HANDLE) new_session;
num_meta_sessions++;
if (flags & CKF_RW_SESSION) {
num_rw_meta_sessions++;
}
return (CKR_OK);
}
/*
* meta_CloseSession
*
*/
CK_RV
meta_CloseSession(CK_SESSION_HANDLE hSession)
{
CK_RV rv;
meta_session_t *session;
CK_FLAGS flags;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
/* save info about session flags before they are destroyed */
flags = session->session_flags;
rv = meta_session_deactivate(session, B_FALSE);
if (rv == CKR_OK)
meta_session_dealloc(session);
num_meta_sessions--;
if (flags & CKF_RW_SESSION) {
num_rw_meta_sessions--;
}
return (rv);
}
/*
* meta_CloseAllSessions
*
* This is a simple loop that closes the sessionlist head (resulting in a
* new list head) until the list is empty.
*
*/
CK_RV
meta_CloseAllSessions(CK_SLOT_ID slotID)
{
CK_RV rv;
meta_session_t *session;
if (!metaslot_enabled) {
return (CKR_SLOT_ID_INVALID);
}
if (slotID != METASLOT_SLOTID)
return (CKR_SLOT_ID_INVALID);
(void) pthread_rwlock_wrlock(&meta_sessionlist_lock);
while ((session = meta_sessionlist_head) != NULL) {
rv = meta_handle2session((CK_SESSION_HANDLE)session, &session);
if (rv != CKR_OK) {
/*NOTREACHED*/
(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
return (CKR_FUNCTION_FAILED);
}
(void) meta_session_deactivate(session, B_TRUE);
meta_session_dealloc(session);
}
(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
/* All open sessions should be closed, just reset the variables */
num_meta_sessions = 0;
num_rw_meta_sessions = 0;
return (CKR_OK);
}
/*
* meta_GetSessionInfo
*
*/
CK_RV
meta_GetSessionInfo(CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo)
{
CK_RV rv;
meta_session_t *session;
if (pInfo == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
pInfo->slotID = METASLOT_SLOTID;
pInfo->flags = session->session_flags;
if (metaslot_logged_in()) {
if (IS_READ_ONLY_SESSION(session->session_flags)) {
pInfo->state = CKS_RO_USER_FUNCTIONS;
} else {
pInfo->state = CKS_RW_USER_FUNCTIONS;
}
} else {
if (IS_READ_ONLY_SESSION(session->session_flags)) {
pInfo->state = CKS_RO_PUBLIC_SESSION;
} else {
pInfo->state = CKS_RW_PUBLIC_SESSION;
}
}
pInfo->ulDeviceError = 0;
REFRELEASE(session);
return (CKR_OK);
}
CK_RV
meta_getopstatelen(meta_session_t *session, CK_ULONG *out_length)
{
CK_RV rv = CKR_OK;
slot_session_t *slot_session;
CK_ULONG length;
*out_length = sizeof (meta_opstate_t);
if (session->op1.type != 0) {
slot_session = session->op1.session;
rv = FUNCLIST(slot_session->fw_st_id)->C_GetOperationState(
slot_session->hSession, NULL, &length);
if (rv == CKR_OK)
*out_length += length;
}
return (rv);
}
/*
* meta_GetOperationState
*
*/
CK_RV
meta_GetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
CK_ULONG_PTR pulOperationStateLen)
{
CK_RV rv;
meta_session_t *session;
slot_session_t *slot_session = NULL;
meta_opstate_t opstate;
if (pulOperationStateLen == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
/*
* If no operation is active, then bail out.
*/
if (session->op1.type == 0) {
rv = CKR_OPERATION_NOT_INITIALIZED;
goto endgetopstate;
}
/*
* If the caller did not give an OpState buffer,
* shortcut and just return the size needed to hold
* a metaslot OpState record later.
* The actual size of the returned state will be the
* sizeof(meta_opstate_t) + SIZE (op1 state),
* so we have to get the size of
* the operation states now.
*/
if (pOperationState == NULL) {
rv = meta_getopstatelen(session, pulOperationStateLen);
REFRELEASE(session);
return (rv);
}
/*
* To be here, the caller must have supplied an
* already initialized meta_opstate_t pointer.
* Use it to get the real state info from the operation(s).
*
* The format of the Metaslot Opstate record:
* {
* struct metaopstate
* [ op1 state data ]
* }
*/
/*
* If the buffer is not even big enough for the metaslot
* opstate data, return error and set the returned
* state length to indicate the minimum needed.
*/
if (*pulOperationStateLen < sizeof (meta_opstate_t)) {
rv = meta_getopstatelen(session, pulOperationStateLen);
/*
* Remap the error so the caller knows that they
* used an invalid buffer size in the first place.
*/
if (rv == CKR_OK)
rv = CKR_BUFFER_TOO_SMALL;
goto endgetopstate;
}
(void) memset(&opstate, 0, sizeof (meta_opstate_t));
opstate.magic_marker = METASLOT_OPSTATE_MAGIC;
if (session->op1.type != 0) {
slot_session = session->op1.session;
opstate.state[0].op_type = session->op1.type;
opstate.state[0].op_slotnum = slot_session->slotnum;
opstate.state[0].op_state_len = *pulOperationStateLen -
sizeof (meta_opstate_t);
opstate.state[0].op_init_app = session->init.app;
opstate.state[0].op_init_done = session->init.done;
rv = FUNCLIST(slot_session->fw_st_id)->C_GetOperationState(
slot_session->hSession,
pOperationState + sizeof (meta_opstate_t),
&(opstate.state[0].op_state_len));
if (rv == CKR_BUFFER_TOO_SMALL) {
/*
* This should not happen, but if it does,
* recalculate the entire size needed
* and return the error.
*/
rv = meta_getopstatelen(session, pulOperationStateLen);
if (rv == CKR_OK)
rv = CKR_BUFFER_TOO_SMALL;
}
if (rv != CKR_OK)
goto endgetopstate;
}
endgetopstate:
if (rv == CKR_OK && pOperationState != NULL) {
(void) memcpy(pOperationState, (void *)&opstate,
sizeof (meta_opstate_t));
*pulOperationStateLen = sizeof (meta_opstate_t) +
opstate.state[0].op_state_len;
}
REFRELEASE(session);
return (rv);
}
static CK_RV
meta_set_opstate(slot_session_t *slot_session,
meta_object_t *meta_enc_key,
meta_object_t *meta_auth_key,
struct opstate_data *state,
CK_BYTE *databuf)
{
CK_RV rv;
static CK_ULONG encrypt_optypes = (CKF_ENCRYPT | CKF_DECRYPT);
static CK_ULONG sign_optypes = (CKF_SIGN | CKF_VERIFY |
CKF_SIGN_RECOVER | CKF_VERIFY_RECOVER);
slot_object_t *enc_key_obj = NULL, *auth_key_obj = NULL;
if (state->op_type & encrypt_optypes) {
rv = meta_object_get_clone(meta_enc_key, slot_session->slotnum,
slot_session, &enc_key_obj);
if (rv != CKR_OK) {
return (rv);
}
}
if (state->op_type & sign_optypes) {
rv = meta_object_get_clone(meta_auth_key, slot_session->slotnum,
slot_session, &auth_key_obj);
if (rv != CKR_OK) {
return (rv);
}
}
/*
* Check to see if the keys are needed to restore the
* state on the first operation.
*/
rv = FUNCLIST(slot_session->fw_st_id)->C_SetOperationState(
slot_session->hSession, databuf, state->op_state_len,
enc_key_obj ? enc_key_obj->hObject : CK_INVALID_HANDLE,
auth_key_obj ? auth_key_obj->hObject : CK_INVALID_HANDLE);
/*
* If the operation did not need a key, try again.
*/
if (rv == CKR_KEY_NOT_NEEDED) {
rv = FUNCLIST(slot_session->fw_st_id)->C_SetOperationState(
slot_session->hSession, databuf, state->op_state_len,
CK_INVALID_HANDLE, CK_INVALID_HANDLE);
/*
* Strange case... If the first try returned
* KEY_NOT_NEEDED, and this one returns KEY_NEEDED,
* we want to remap the return so the caller sees
* the original "CKR_KEY_NOT_NEEDED" return value.
* This ensures that a correct caller will retry
* without the unnecessary key argument and this
* 2nd attempt will not happen again.
*/
if (rv == CKR_KEY_NEEDED) {
rv = CKR_KEY_NOT_NEEDED;
}
}
return (rv);
}
/*
* meta_SetOperationState
*
*/
CK_RV
meta_SetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
CK_ULONG ulOperationStateLen, CK_OBJECT_HANDLE hEncryptionKey,
CK_OBJECT_HANDLE hAuthenticationKey)
{
CK_RV rv = CKR_OK;
meta_session_t *session;
slot_session_t *slot_session = NULL;
meta_opstate_t opstate;
meta_object_t *meta_enc_key = NULL, *meta_auth_key = NULL;
/*
* Make sure the opstate info buffer is big enough to be valid.
*/
if (ulOperationStateLen < sizeof (meta_opstate_t) ||
pOperationState == NULL)
return (CKR_ARGUMENTS_BAD);
/* Copy the opstate info into the structure */
(void) memcpy(&opstate, pOperationState, sizeof (meta_opstate_t));
/* verify that a metaslot operation state was supplied */
if (opstate.magic_marker != METASLOT_OPSTATE_MAGIC)
return (CKR_SAVED_STATE_INVALID);
/*
* Now, check the size again to make sure the "real" state
* data is present. Length of state provided must be exact.
*/
if (ulOperationStateLen != (sizeof (meta_opstate_t) +
opstate.state[0].op_state_len))
return (CKR_SAVED_STATE_INVALID);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (hEncryptionKey != CK_INVALID_HANDLE) {
rv = meta_handle2object(hEncryptionKey, &meta_enc_key);
if (rv != CKR_OK)
goto cleanup;
}
if (hAuthenticationKey != CK_INVALID_HANDLE) {
rv = meta_handle2object(hAuthenticationKey, &meta_auth_key);
if (rv != CKR_OK)
goto cleanup;
}
if (opstate.state[0].op_type != 0) {
if (session->op1.type != 0)
meta_operation_cleanup(session, session->op1.type,
B_FALSE);
if (session->op1.session != NULL) {
slot_session = session->op1.session;
} else {
rv = meta_get_slot_session(opstate.state[0].op_slotnum,
&slot_session, session->session_flags);
if (rv != CKR_OK)
goto cleanup;
}
session->op1.type = opstate.state[0].op_type;
session->op1.session = slot_session;
session->init.app = opstate.state[0].op_init_app;
session->init.done = opstate.state[0].op_init_done;
rv = meta_set_opstate(slot_session, meta_enc_key,
meta_auth_key, &(opstate.state[0]),
pOperationState + sizeof (meta_opstate_t));
if (rv != CKR_OK) {
meta_operation_cleanup(session, session->op1.type,
FALSE);
goto cleanup;
}
}
cleanup:
if (meta_enc_key != NULL)
OBJRELEASE(meta_enc_key);
if (meta_auth_key != NULL)
OBJRELEASE(meta_auth_key);
REFRELEASE(session);
return (rv);
}
/*
* meta_Login
*
* This allows the user to login to the object token. The metaslot itself
* does not have any kind of PIN.
*
*/
CK_RV
meta_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
{
CK_RV rv;
meta_session_t *session;
slot_session_t *login_session = NULL;
CK_TOKEN_INFO token_info;
CK_SLOT_ID true_id, fw_st_id;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (metaslot_logged_in()) {
rv = CKR_USER_ALREADY_LOGGED_IN;
goto finish;
}
/* Note: CKU_SO is not supported. */
if (userType != CKU_USER) {
rv = CKR_USER_TYPE_INVALID;
goto finish;
}
rv = meta_get_slot_session(get_keystore_slotnum(), &login_session,
session->session_flags);
if (rv != CKR_OK)
goto finish;
fw_st_id = login_session->fw_st_id;
rv = FUNCLIST(fw_st_id)->C_Login(login_session->hSession, userType,
pPin, ulPinLen);
if (rv != CKR_OK) {
goto finish;
}
/*
* Note:
*
* For some slots (eg: the pkcs11_softtoken.so), C_Login()
* returning OK don't mean that the login is truely
* successful. For pkcs11_softtoken.so, the CKF_USER_PIN_TO_BE_CHANGED
* is set to indicate that the pin needs to be changed, and
* the login is not really successful. We will check
* that flag for this special condition. Checking for
* this flag shouldn't be harmful for other slots that doesn't
* behave like pkcs11_softtoken.so.
*/
true_id = TRUEID(fw_st_id);
rv = FUNCLIST(fw_st_id)->C_GetTokenInfo(true_id, &token_info);
if (rv != CKR_OK) {
goto finish;
}
metaslot_set_logged_in_flag(B_TRUE);
if (token_info.flags & CKF_USER_PIN_TO_BE_CHANGED) {
metaslot_set_logged_in_flag(B_FALSE);
}
finish:
if (login_session)
meta_release_slot_session(login_session);
REFRELEASE(session);
return (rv);
}
/*
* meta_Logout
*
*/
CK_RV
meta_Logout(CK_SESSION_HANDLE hSession)
{
CK_RV rv = CKR_OK;
meta_session_t *session;
slot_session_t *logout_session = NULL;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (!metaslot_logged_in()) {
rv = CKR_USER_NOT_LOGGED_IN;
goto finish;
}
rv = meta_get_slot_session(get_keystore_slotnum(), &logout_session,
session->session_flags);
if (rv != CKR_OK)
goto finish;
rv = FUNCLIST(logout_session->fw_st_id)->C_Logout(
logout_session->hSession);
/* If the C_Logout fails, just ignore the error. */
metaslot_set_logged_in_flag(B_FALSE);
(void) meta_token_object_deactivate(PRIVATE_TOKEN);
finish:
if (logout_session)
meta_release_slot_session(logout_session);
REFRELEASE(session);
return (rv);
}
|