xref: /titanic_41/usr/src/lib/pkcs11/libpkcs11/common/metaSession.c (revision 2449e17f82f6097fd2c665b64723e31ceecbeca6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Session Management Functions
30  * (as defined in PKCS#11 spec spection 11.6)
31  */
32 
33 #include <string.h>
34 #include "metaGlobal.h"
35 
36 extern meta_session_t *meta_sessionlist_head;
37 extern pthread_rwlock_t meta_sessionlist_lock;
38 extern CK_ULONG num_meta_sessions;
39 extern CK_ULONG num_rw_meta_sessions;
40 
41 /*
42  * meta_OpenSession
43  *
44  * NOTES:
45  * 1) The pApplication and Notify args are not used, as the metaslot does not
46  *    support application callbacks.
47  * 2) the slotID argument is not checked or used because this function
48  *    is only called from the framework.
49  */
50 /* ARGSUSED */
51 CK_RV
52 meta_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
53     CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
54 {
55 	meta_session_t *new_session;
56 	CK_RV rv;
57 
58 	if (!metaslot_enabled) {
59 		return (CKR_SLOT_ID_INVALID);
60 	}
61 
62 	if (phSession == NULL) {
63 		return (CKR_ARGUMENTS_BAD);
64 	}
65 
66 	/* Check for any unknown flags. */
67 	if (flags & ~(CKF_SERIAL_SESSION | CKF_RW_SESSION)) {
68 		return (CKR_ARGUMENTS_BAD);
69 	}
70 
71 	if (!(flags & CKF_SERIAL_SESSION)) {
72 		return (CKR_SESSION_PARALLEL_NOT_SUPPORTED);
73 	}
74 
75 	if (meta_slotManager_token_write_protected() &&
76 	    (flags & CKF_RW_SESSION)) {
77 		return (CKR_TOKEN_WRITE_PROTECTED);
78 	}
79 
80 	rv = meta_session_alloc(&new_session);
81 	if (rv != CKR_OK)
82 		return (rv);
83 
84 	new_session->session_flags = flags;
85 
86 	rv = meta_session_activate(new_session);
87 	if (rv != CKR_OK) {
88 		meta_session_dealloc(new_session);
89 		return (rv);
90 	}
91 
92 	*phSession = (CK_SESSION_HANDLE) new_session;
93 
94 	num_meta_sessions++;
95 	if (flags & CKF_RW_SESSION) {
96 		num_rw_meta_sessions++;
97 	}
98 
99 	return (CKR_OK);
100 }
101 
102 
103 /*
104  * meta_CloseSession
105  *
106  */
107 CK_RV
108 meta_CloseSession(CK_SESSION_HANDLE hSession)
109 {
110 	CK_RV rv;
111 	meta_session_t *session;
112 	CK_FLAGS flags;
113 
114 	rv = meta_handle2session(hSession, &session);
115 	if (rv != CKR_OK)
116 		return (rv);
117 
118 	/* save info about session flags before they are destroyed */
119 	flags = session->session_flags;
120 
121 	rv = meta_session_deactivate(session, B_FALSE);
122 
123 	if (rv == CKR_OK)
124 		meta_session_dealloc(session);
125 
126 	num_meta_sessions--;
127 	if (flags & CKF_RW_SESSION) {
128 		num_rw_meta_sessions--;
129 	}
130 
131 	return (rv);
132 }
133 
134 
135 /*
136  * meta_CloseAllSessions
137  *
138  * This is a simple loop that closes the sessionlist head (resulting in a
139  * new list head) until the list is empty.
140  *
141  */
142 CK_RV
143 meta_CloseAllSessions(CK_SLOT_ID slotID)
144 {
145 	CK_RV rv;
146 	meta_session_t *session;
147 
148 	if (!metaslot_enabled) {
149 		return (CKR_SLOT_ID_INVALID);
150 	}
151 
152 	if (slotID != METASLOT_SLOTID)
153 		return (CKR_SLOT_ID_INVALID);
154 
155 	(void) pthread_rwlock_wrlock(&meta_sessionlist_lock);
156 	while ((session = meta_sessionlist_head) != NULL) {
157 		rv = meta_handle2session((CK_SESSION_HANDLE)session, &session);
158 		if (rv != CKR_OK) {
159 			/*NOTREACHED*/
160 			(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
161 			return (CKR_FUNCTION_FAILED);
162 		}
163 
164 		(void) meta_session_deactivate(session, B_TRUE);
165 		meta_session_dealloc(session);
166 	}
167 	(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
168 
169 	/* All open sessions should be closed, just reset the variables */
170 	num_meta_sessions = 0;
171 	num_rw_meta_sessions = 0;
172 
173 	return (CKR_OK);
174 }
175 
176 
177 /*
178  * meta_GetSessionInfo
179  *
180  */
181 CK_RV
182 meta_GetSessionInfo(CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo)
183 {
184 	CK_RV rv;
185 	meta_session_t *session;
186 
187 	if (pInfo == NULL)
188 		return (CKR_ARGUMENTS_BAD);
189 
190 	rv = meta_handle2session(hSession, &session);
191 	if (rv != CKR_OK)
192 		return (rv);
193 
194 	pInfo->slotID = METASLOT_SLOTID;
195 	pInfo->flags = session->session_flags;
196 
197 	if (metaslot_logged_in()) {
198 		if (IS_READ_ONLY_SESSION(session->session_flags)) {
199 			pInfo->state = CKS_RO_USER_FUNCTIONS;
200 		} else {
201 			pInfo->state = CKS_RW_USER_FUNCTIONS;
202 		}
203 	} else {
204 		if (IS_READ_ONLY_SESSION(session->session_flags)) {
205 			pInfo->state = CKS_RO_PUBLIC_SESSION;
206 		} else {
207 			pInfo->state = CKS_RW_PUBLIC_SESSION;
208 		}
209 	}
210 
211 	pInfo->ulDeviceError = 0;
212 
213 	REFRELEASE(session);
214 
215 	return (CKR_OK);
216 }
217 
218 CK_RV
219 meta_getopstatelen(meta_session_t *session, CK_ULONG *out_length)
220 {
221 	CK_RV rv = CKR_OK;
222 	slot_session_t *slot_session;
223 	CK_ULONG length;
224 
225 	*out_length = sizeof (meta_opstate_t);
226 	if (session->op1.type != 0) {
227 		slot_session = session->op1.session;
228 		rv = FUNCLIST(slot_session->fw_st_id)->C_GetOperationState(
229 		    slot_session->hSession, NULL, &length);
230 		if (rv == CKR_OK)
231 			*out_length += length;
232 	}
233 	return (rv);
234 }
235 
236 /*
237  * meta_GetOperationState
238  *
239  */
240 CK_RV
241 meta_GetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
242     CK_ULONG_PTR pulOperationStateLen)
243 {
244 	CK_RV rv;
245 	meta_session_t *session;
246 	slot_session_t *slot_session = NULL;
247 	meta_opstate_t opstate;
248 
249 	if (pulOperationStateLen == NULL)
250 		return (CKR_ARGUMENTS_BAD);
251 
252 	rv = meta_handle2session(hSession, &session);
253 	if (rv != CKR_OK)
254 		return (rv);
255 
256 	/*
257 	 * If no operation is active, then bail out.
258 	 */
259 	if (session->op1.type == 0) {
260 		rv = CKR_OPERATION_NOT_INITIALIZED;
261 		goto endgetopstate;
262 	}
263 
264 	/*
265 	 * If the caller did not give an OpState buffer,
266 	 * shortcut and just return the size needed to hold
267 	 * a metaslot OpState record later.
268 	 * The actual size of the returned state will be the
269 	 * sizeof(meta_opstate_t) + SIZE (op1 state),
270 	 * so we have to get the size of
271 	 * the operation states now.
272 	 */
273 	if (pOperationState == NULL) {
274 		rv = meta_getopstatelen(session, pulOperationStateLen);
275 		REFRELEASE(session);
276 		return (rv);
277 	}
278 
279 	/*
280 	 * To be here, the caller must have supplied an
281 	 * already initialized meta_opstate_t pointer.
282 	 * Use it to get the real state info from the operation(s).
283 	 *
284 	 * The format of the Metaslot Opstate record:
285 	 * {
286 	 *    struct metaopstate
287 	 *    [ op1 state data ]
288 	 * }
289 	 */
290 
291 	/*
292 	 * If the buffer is not even big enough for the metaslot
293 	 * opstate data, return error and set the returned
294 	 * state length to indicate the minimum needed.
295 	 */
296 	if (*pulOperationStateLen < sizeof (meta_opstate_t)) {
297 		rv = meta_getopstatelen(session, pulOperationStateLen);
298 		/*
299 		 * Remap the error so the caller knows that they
300 		 * used an invalid buffer size in the first place.
301 		 */
302 		if (rv == CKR_OK)
303 			rv = CKR_BUFFER_TOO_SMALL;
304 		goto endgetopstate;
305 	}
306 
307 	(void) memset(&opstate, 0, sizeof (meta_opstate_t));
308 	opstate.magic_marker = METASLOT_OPSTATE_MAGIC;
309 
310 	if (session->op1.type != 0) {
311 		slot_session = session->op1.session;
312 		opstate.state[0].op_type = session->op1.type;
313 		opstate.state[0].op_slotnum = slot_session->slotnum;
314 		opstate.state[0].op_state_len = *pulOperationStateLen -
315 		    sizeof (meta_opstate_t);
316 		rv = FUNCLIST(slot_session->fw_st_id)->C_GetOperationState(
317 		    slot_session->hSession,
318 		    pOperationState + sizeof (meta_opstate_t),
319 		    &(opstate.state[0].op_state_len));
320 
321 		if (rv == CKR_BUFFER_TOO_SMALL) {
322 			/*
323 			 * This should not happen, but if it does,
324 			 * recalculate the entire size needed
325 			 * and return the error.
326 			 */
327 			rv = meta_getopstatelen(session, pulOperationStateLen);
328 			if (rv == CKR_OK)
329 				rv = CKR_BUFFER_TOO_SMALL;
330 		}
331 
332 		if (rv != CKR_OK)
333 			goto endgetopstate;
334 	}
335 
336 endgetopstate:
337 	if (rv == CKR_OK && pOperationState != NULL) {
338 		(void) memcpy(pOperationState, (void *)&opstate,
339 		    sizeof (meta_opstate_t));
340 
341 		*pulOperationStateLen = sizeof (meta_opstate_t) +
342 		    opstate.state[0].op_state_len;
343 	}
344 
345 	REFRELEASE(session);
346 	return (rv);
347 }
348 
349 static CK_RV
350 meta_set_opstate(slot_session_t *slot_session,
351 		meta_object_t *meta_enc_key,
352 		meta_object_t *meta_auth_key,
353 		struct opstate_data *state,
354 		CK_BYTE *databuf)
355 {
356 	CK_RV rv;
357 	static CK_ULONG encrypt_optypes = (CKF_ENCRYPT | CKF_DECRYPT);
358 	static CK_ULONG sign_optypes = (CKF_SIGN | CKF_VERIFY |
359 	    CKF_SIGN_RECOVER | CKF_VERIFY_RECOVER);
360 	slot_object_t *enc_key_obj = NULL, *auth_key_obj = NULL;
361 
362 	if (state->op_type & encrypt_optypes) {
363 		rv = meta_object_get_clone(meta_enc_key, slot_session->slotnum,
364 		    slot_session, &enc_key_obj);
365 		if (rv != CKR_OK) {
366 			return (rv);
367 		}
368 	}
369 	if (state->op_type & sign_optypes) {
370 		rv = meta_object_get_clone(meta_auth_key, slot_session->slotnum,
371 		    slot_session, &auth_key_obj);
372 		if (rv != CKR_OK) {
373 			return (rv);
374 		}
375 	}
376 
377 	/*
378 	 * Check to see if the keys are needed to restore the
379 	 * state on the first operation.
380 	 */
381 	rv = FUNCLIST(slot_session->fw_st_id)->C_SetOperationState(
382 	    slot_session->hSession, databuf, state->op_state_len,
383 	    enc_key_obj ? enc_key_obj->hObject : CK_INVALID_HANDLE,
384 	    auth_key_obj ? auth_key_obj->hObject : CK_INVALID_HANDLE);
385 	/*
386 	 * If the operation did not need a key, try again.
387 	 */
388 	if (rv == CKR_KEY_NOT_NEEDED) {
389 		rv = FUNCLIST(slot_session->fw_st_id)->C_SetOperationState(
390 		    slot_session->hSession, databuf, state->op_state_len,
391 		    CK_INVALID_HANDLE, CK_INVALID_HANDLE);
392 		/*
393 		 * Strange case... If the first try returned
394 		 * KEY_NOT_NEEDED, and this one returns KEY_NEEDED,
395 		 * we want to remap the return so the caller sees
396 		 * the original "CKR_KEY_NOT_NEEDED" return value.
397 		 * This ensures that a correct caller will retry
398 		 * without the unnecessary key argument and this
399 		 * 2nd attempt will not happen again.
400 		 */
401 		if (rv == CKR_KEY_NEEDED) {
402 			rv  = CKR_KEY_NOT_NEEDED;
403 		}
404 	}
405 
406 	return (rv);
407 }
408 
409 /*
410  * meta_SetOperationState
411  *
412  */
413 CK_RV
414 meta_SetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
415     CK_ULONG ulOperationStateLen, CK_OBJECT_HANDLE hEncryptionKey,
416     CK_OBJECT_HANDLE hAuthenticationKey)
417 {
418 	CK_RV rv = CKR_OK;
419 	meta_session_t *session;
420 	slot_session_t *slot_session = NULL;
421 	meta_opstate_t opstate;
422 	meta_object_t *meta_enc_key = NULL, *meta_auth_key = NULL;
423 
424 	/*
425 	 * Make sure the opstate info buffer is big enough to be valid.
426 	 */
427 	if (ulOperationStateLen < sizeof (meta_opstate_t) ||
428 	    pOperationState == NULL)
429 		return (CKR_ARGUMENTS_BAD);
430 
431 	/* Copy the opstate info into the structure */
432 	(void) memcpy(&opstate, pOperationState, sizeof (meta_opstate_t));
433 
434 	/* verify that a metaslot operation state was supplied */
435 	if (opstate.magic_marker != METASLOT_OPSTATE_MAGIC)
436 		return (CKR_SAVED_STATE_INVALID);
437 
438 	/*
439 	 * Now, check the size again to make sure the "real" state
440 	 * data is present.  Length of state provided must be exact.
441 	 */
442 	if (ulOperationStateLen != (sizeof (meta_opstate_t) +
443 	    opstate.state[0].op_state_len))
444 		return (CKR_SAVED_STATE_INVALID);
445 
446 	rv = meta_handle2session(hSession, &session);
447 	if (rv != CKR_OK)
448 		return (rv);
449 
450 	if (hEncryptionKey != CK_INVALID_HANDLE) {
451 		rv = meta_handle2object(hEncryptionKey, &meta_enc_key);
452 		if (rv != CKR_OK)
453 			goto cleanup;
454 	}
455 	if (hAuthenticationKey != CK_INVALID_HANDLE) {
456 		rv = meta_handle2object(hAuthenticationKey, &meta_auth_key);
457 		if (rv != CKR_OK)
458 			goto cleanup;
459 	}
460 
461 	if (opstate.state[0].op_type != 0) {
462 		if (session->op1.type != 0)
463 			meta_operation_cleanup(session, session->op1.type,
464 			    B_FALSE);
465 
466 		rv = meta_get_slot_session(opstate.state[0].op_slotnum,
467 		    &slot_session, session->session_flags);
468 		if (rv != CKR_OK)
469 			goto cleanup;
470 
471 		session->op1.type = opstate.state[0].op_type;
472 		session->op1.session = slot_session;
473 
474 		rv = meta_set_opstate(slot_session, meta_enc_key,
475 		    meta_auth_key, &(opstate.state[0]),
476 		    pOperationState + sizeof (meta_opstate_t));
477 
478 		if (rv != CKR_OK) {
479 			meta_operation_cleanup(session, session->op1.type,
480 			    FALSE);
481 			goto cleanup;
482 		}
483 	}
484 
485 cleanup:
486 	if (meta_enc_key != NULL)
487 		OBJRELEASE(meta_enc_key);
488 	if (meta_auth_key != NULL)
489 		OBJRELEASE(meta_auth_key);
490 	REFRELEASE(session);
491 	return (rv);
492 }
493 
494 /*
495  * meta_Login
496  *
497  * This allows the user to login to the object token. The metaslot itself
498  * does not have any kind of PIN.
499  *
500  */
501 CK_RV
502 meta_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
503     CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
504 {
505 	CK_RV rv;
506 	meta_session_t *session;
507 	slot_session_t *login_session = NULL;
508 	CK_TOKEN_INFO token_info;
509 	CK_SLOT_ID true_id, fw_st_id;
510 
511 	rv = meta_handle2session(hSession, &session);
512 	if (rv != CKR_OK)
513 		return (rv);
514 
515 	if (metaslot_logged_in()) {
516 		rv = CKR_USER_ALREADY_LOGGED_IN;
517 		goto finish;
518 	}
519 
520 	/* Note: CKU_SO is not supported. */
521 	if (userType != CKU_USER) {
522 		rv = CKR_USER_TYPE_INVALID;
523 		goto finish;
524 	}
525 
526 	rv = meta_get_slot_session(get_keystore_slotnum(), &login_session,
527 	    session->session_flags);
528 	if (rv != CKR_OK)
529 		goto finish;
530 
531 
532 	fw_st_id = login_session->fw_st_id;
533 	rv = FUNCLIST(fw_st_id)->C_Login(login_session->hSession, userType,
534 	    pPin, ulPinLen);
535 
536 	if (rv != CKR_OK) {
537 		goto finish;
538 	}
539 
540 	/*
541 	 * Note:
542 	 *
543 	 * For some slots (eg: the pkcs11_softtoken.so), C_Login()
544 	 * returning OK don't mean that the login is truely
545 	 * successful.  For pkcs11_softtoken.so, the CKF_USER_PIN_TO_BE_CHANGED
546 	 * is set to indicate that the pin needs to be changed, and
547 	 * the login is not really successful.  We will check
548 	 * that flag for this special condition.  Checking for
549 	 * this flag shouldn't be harmful for other slots that doesn't
550 	 * behave like pkcs11_softtoken.so.
551 	 */
552 
553 	true_id = TRUEID(fw_st_id);
554 	rv = FUNCLIST(fw_st_id)->C_GetTokenInfo(true_id, &token_info);
555 	if (rv != CKR_OK) {
556 		goto finish;
557 	}
558 
559 	metaslot_set_logged_in_flag(B_TRUE);
560 	if (token_info.flags & CKF_USER_PIN_TO_BE_CHANGED) {
561 		metaslot_set_logged_in_flag(B_FALSE);
562 	}
563 finish:
564 	if (login_session)
565 		meta_release_slot_session(login_session);
566 
567 	REFRELEASE(session);
568 
569 	return (rv);
570 }
571 
572 /*
573  * meta_Logout
574  *
575  */
576 CK_RV
577 meta_Logout(CK_SESSION_HANDLE hSession)
578 {
579 	CK_RV rv = CKR_OK;
580 	meta_session_t *session;
581 	slot_session_t *logout_session = NULL;
582 
583 	rv = meta_handle2session(hSession, &session);
584 	if (rv != CKR_OK)
585 		return (rv);
586 
587 	if (!metaslot_logged_in()) {
588 		rv = CKR_USER_NOT_LOGGED_IN;
589 		goto finish;
590 	}
591 
592 	rv = meta_get_slot_session(get_keystore_slotnum(), &logout_session,
593 	    session->session_flags);
594 	if (rv != CKR_OK)
595 		goto finish;
596 
597 	rv = FUNCLIST(logout_session->fw_st_id)->C_Logout(
598 	    logout_session->hSession);
599 
600 	/* If the C_Logout fails, just ignore the error. */
601 	metaslot_set_logged_in_flag(B_FALSE);
602 
603 finish:
604 	if (logout_session)
605 		meta_release_slot_session(logout_session);
606 
607 	REFRELEASE(session);
608 
609 	return (rv);
610 }
611