1*12720SWyllys.Ingersoll@Sun.COM /*
2*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER START
3*12720SWyllys.Ingersoll@Sun.COM *
4*12720SWyllys.Ingersoll@Sun.COM * The contents of this file are subject to the terms of the
5*12720SWyllys.Ingersoll@Sun.COM * Common Development and Distribution License (the "License").
6*12720SWyllys.Ingersoll@Sun.COM * You may not use this file except in compliance with the License.
7*12720SWyllys.Ingersoll@Sun.COM *
8*12720SWyllys.Ingersoll@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12720SWyllys.Ingersoll@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*12720SWyllys.Ingersoll@Sun.COM * See the License for the specific language governing permissions
11*12720SWyllys.Ingersoll@Sun.COM * and limitations under the License.
12*12720SWyllys.Ingersoll@Sun.COM *
13*12720SWyllys.Ingersoll@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*12720SWyllys.Ingersoll@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12720SWyllys.Ingersoll@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*12720SWyllys.Ingersoll@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*12720SWyllys.Ingersoll@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*12720SWyllys.Ingersoll@Sun.COM *
19*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER END
20*12720SWyllys.Ingersoll@Sun.COM *
21*12720SWyllys.Ingersoll@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
22*12720SWyllys.Ingersoll@Sun.COM */
23*12720SWyllys.Ingersoll@Sun.COM
24*12720SWyllys.Ingersoll@Sun.COM #include <pthread.h>
25*12720SWyllys.Ingersoll@Sun.COM #include <errno.h>
26*12720SWyllys.Ingersoll@Sun.COM #include <security/cryptoki.h>
27*12720SWyllys.Ingersoll@Sun.COM #include "kmsGlobal.h"
28*12720SWyllys.Ingersoll@Sun.COM #include "kmsSession.h"
29*12720SWyllys.Ingersoll@Sun.COM #include "kmsSlot.h"
30*12720SWyllys.Ingersoll@Sun.COM #include "kmsKeystoreUtil.h"
31*12720SWyllys.Ingersoll@Sun.COM
32*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_OpenSession(CK_SLOT_ID slotID,CK_FLAGS flags,CK_VOID_PTR pApplication,CK_NOTIFY Notify,CK_SESSION_HANDLE_PTR phSession)33*12720SWyllys.Ingersoll@Sun.COM C_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
34*12720SWyllys.Ingersoll@Sun.COM CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
35*12720SWyllys.Ingersoll@Sun.COM {
36*12720SWyllys.Ingersoll@Sun.COM CK_RV rv = CKR_OK;
37*12720SWyllys.Ingersoll@Sun.COM kms_slot_t *pslot;
38*12720SWyllys.Ingersoll@Sun.COM
39*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
40*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
41*12720SWyllys.Ingersoll@Sun.COM
42*12720SWyllys.Ingersoll@Sun.COM if (!(flags & CKF_SERIAL_SESSION))
43*12720SWyllys.Ingersoll@Sun.COM return (CKR_SESSION_PARALLEL_NOT_SUPPORTED);
44*12720SWyllys.Ingersoll@Sun.COM
45*12720SWyllys.Ingersoll@Sun.COM if (phSession == NULL)
46*12720SWyllys.Ingersoll@Sun.COM return (CKR_ARGUMENTS_BAD);
47*12720SWyllys.Ingersoll@Sun.COM
48*12720SWyllys.Ingersoll@Sun.COM if (slotID != KMS_TOKEN_SLOTID) {
49*12720SWyllys.Ingersoll@Sun.COM return (CKR_SLOT_ID_INVALID);
50*12720SWyllys.Ingersoll@Sun.COM }
51*12720SWyllys.Ingersoll@Sun.COM
52*12720SWyllys.Ingersoll@Sun.COM /*
53*12720SWyllys.Ingersoll@Sun.COM * Acquire the slot lock to protect sl_state and sl_sess_list.
54*12720SWyllys.Ingersoll@Sun.COM * These two fields need to be protected atomically, even though
55*12720SWyllys.Ingersoll@Sun.COM * "sl_sess_list" is updated in kms_add_session().
56*12720SWyllys.Ingersoll@Sun.COM */
57*12720SWyllys.Ingersoll@Sun.COM pslot = get_slotinfo();
58*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_lock(&pslot->sl_mutex);
59*12720SWyllys.Ingersoll@Sun.COM
60*12720SWyllys.Ingersoll@Sun.COM /* If SO is logged in the slot, only the RW session is allowed. */
61*12720SWyllys.Ingersoll@Sun.COM if ((pslot->sl_state == CKU_SO) && !(flags & CKF_RW_SESSION)) {
62*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_unlock(&pslot->sl_mutex);
63*12720SWyllys.Ingersoll@Sun.COM return (CKR_SESSION_READ_WRITE_SO_EXISTS);
64*12720SWyllys.Ingersoll@Sun.COM }
65*12720SWyllys.Ingersoll@Sun.COM
66*12720SWyllys.Ingersoll@Sun.COM /* Create a new session */
67*12720SWyllys.Ingersoll@Sun.COM rv = kms_add_session(slotID, flags, pApplication, Notify,
68*12720SWyllys.Ingersoll@Sun.COM phSession);
69*12720SWyllys.Ingersoll@Sun.COM
70*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_unlock(&pslot->sl_mutex);
71*12720SWyllys.Ingersoll@Sun.COM return (rv);
72*12720SWyllys.Ingersoll@Sun.COM }
73*12720SWyllys.Ingersoll@Sun.COM
74*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_CloseSession(CK_SESSION_HANDLE hSession)75*12720SWyllys.Ingersoll@Sun.COM C_CloseSession(CK_SESSION_HANDLE hSession)
76*12720SWyllys.Ingersoll@Sun.COM {
77*12720SWyllys.Ingersoll@Sun.COM CK_RV rv;
78*12720SWyllys.Ingersoll@Sun.COM
79*12720SWyllys.Ingersoll@Sun.COM kms_session_t *session_p;
80*12720SWyllys.Ingersoll@Sun.COM boolean_t ses_lock_held = B_FALSE;
81*12720SWyllys.Ingersoll@Sun.COM
82*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
83*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
84*12720SWyllys.Ingersoll@Sun.COM
85*12720SWyllys.Ingersoll@Sun.COM /*
86*12720SWyllys.Ingersoll@Sun.COM * Obtain the session pointer. Also, increment the session
87*12720SWyllys.Ingersoll@Sun.COM * reference count.
88*12720SWyllys.Ingersoll@Sun.COM */
89*12720SWyllys.Ingersoll@Sun.COM rv = handle2session(hSession, &session_p);
90*12720SWyllys.Ingersoll@Sun.COM if (rv != CKR_OK)
91*12720SWyllys.Ingersoll@Sun.COM return (rv);
92*12720SWyllys.Ingersoll@Sun.COM
93*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_lock(&session_p->session_mutex);
94*12720SWyllys.Ingersoll@Sun.COM ses_lock_held = B_TRUE;
95*12720SWyllys.Ingersoll@Sun.COM
96*12720SWyllys.Ingersoll@Sun.COM /*
97*12720SWyllys.Ingersoll@Sun.COM * Set SESSION_IS_CLOSING flag so any access to this
98*12720SWyllys.Ingersoll@Sun.COM * session will be rejected.
99*12720SWyllys.Ingersoll@Sun.COM */
100*12720SWyllys.Ingersoll@Sun.COM if (session_p->ses_close_sync & SESSION_IS_CLOSING) {
101*12720SWyllys.Ingersoll@Sun.COM REFRELE(session_p, ses_lock_held);
102*12720SWyllys.Ingersoll@Sun.COM return (CKR_SESSION_CLOSED);
103*12720SWyllys.Ingersoll@Sun.COM }
104*12720SWyllys.Ingersoll@Sun.COM session_p->ses_close_sync |= SESSION_IS_CLOSING;
105*12720SWyllys.Ingersoll@Sun.COM
106*12720SWyllys.Ingersoll@Sun.COM /*
107*12720SWyllys.Ingersoll@Sun.COM * Decrement the session reference count.
108*12720SWyllys.Ingersoll@Sun.COM * We hold the session lock, and REFRELE()
109*12720SWyllys.Ingersoll@Sun.COM * will release the session lock for us.
110*12720SWyllys.Ingersoll@Sun.COM */
111*12720SWyllys.Ingersoll@Sun.COM REFRELE(session_p, ses_lock_held);
112*12720SWyllys.Ingersoll@Sun.COM
113*12720SWyllys.Ingersoll@Sun.COM /*
114*12720SWyllys.Ingersoll@Sun.COM * Delete a session by calling kms_delete_session() with
115*12720SWyllys.Ingersoll@Sun.COM * a session pointer and two boolean arguments. The 3rd argument
116*12720SWyllys.Ingersoll@Sun.COM * boolean value FALSE indicates that the caller does not
117*12720SWyllys.Ingersoll@Sun.COM * hold the slot lock. The 4th argument boolean value B_FALSE
118*12720SWyllys.Ingersoll@Sun.COM * indicates that we want to delete all the objects completely.
119*12720SWyllys.Ingersoll@Sun.COM *
120*12720SWyllys.Ingersoll@Sun.COM * kms_delete_session() will reset SESSION_IS_CLOSING
121*12720SWyllys.Ingersoll@Sun.COM * flag after it is done.
122*12720SWyllys.Ingersoll@Sun.COM */
123*12720SWyllys.Ingersoll@Sun.COM kms_delete_session(session_p, B_FALSE, B_FALSE);
124*12720SWyllys.Ingersoll@Sun.COM return (rv);
125*12720SWyllys.Ingersoll@Sun.COM }
126*12720SWyllys.Ingersoll@Sun.COM
127*12720SWyllys.Ingersoll@Sun.COM /*ARGSUSED*/
128*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_CloseAllSessions(CK_SLOT_ID slotID)129*12720SWyllys.Ingersoll@Sun.COM C_CloseAllSessions(CK_SLOT_ID slotID)
130*12720SWyllys.Ingersoll@Sun.COM {
131*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
132*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
133*12720SWyllys.Ingersoll@Sun.COM
134*12720SWyllys.Ingersoll@Sun.COM /* Delete all the sessions and release the allocated resources */
135*12720SWyllys.Ingersoll@Sun.COM kms_delete_all_sessions(B_FALSE);
136*12720SWyllys.Ingersoll@Sun.COM
137*12720SWyllys.Ingersoll@Sun.COM return (CKR_OK);
138*12720SWyllys.Ingersoll@Sun.COM }
139*12720SWyllys.Ingersoll@Sun.COM
140*12720SWyllys.Ingersoll@Sun.COM /*
141*12720SWyllys.Ingersoll@Sun.COM * Utility routine to get CK_STATE value for a session.
142*12720SWyllys.Ingersoll@Sun.COM * The caller should not be holding the session lock.
143*12720SWyllys.Ingersoll@Sun.COM */
144*12720SWyllys.Ingersoll@Sun.COM static CK_STATE
get_ses_state(kms_session_t * session_p)145*12720SWyllys.Ingersoll@Sun.COM get_ses_state(kms_session_t *session_p)
146*12720SWyllys.Ingersoll@Sun.COM {
147*12720SWyllys.Ingersoll@Sun.COM CK_STATE state;
148*12720SWyllys.Ingersoll@Sun.COM kms_slot_t *pslot;
149*12720SWyllys.Ingersoll@Sun.COM
150*12720SWyllys.Ingersoll@Sun.COM pslot = get_slotinfo();
151*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_lock(&pslot->sl_mutex);
152*12720SWyllys.Ingersoll@Sun.COM
153*12720SWyllys.Ingersoll@Sun.COM if (pslot->sl_state == CKU_PUBLIC) {
154*12720SWyllys.Ingersoll@Sun.COM state = (session_p->ses_RO) ?
155*12720SWyllys.Ingersoll@Sun.COM CKS_RO_PUBLIC_SESSION : CKS_RW_PUBLIC_SESSION;
156*12720SWyllys.Ingersoll@Sun.COM } else if (pslot->sl_state == CKU_USER) {
157*12720SWyllys.Ingersoll@Sun.COM state = (session_p->ses_RO) ?
158*12720SWyllys.Ingersoll@Sun.COM CKS_RO_USER_FUNCTIONS : CKS_RW_USER_FUNCTIONS;
159*12720SWyllys.Ingersoll@Sun.COM } else if (pslot->sl_state == CKU_SO) {
160*12720SWyllys.Ingersoll@Sun.COM state = CKS_RW_SO_FUNCTIONS;
161*12720SWyllys.Ingersoll@Sun.COM }
162*12720SWyllys.Ingersoll@Sun.COM
163*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_unlock(&pslot->sl_mutex);
164*12720SWyllys.Ingersoll@Sun.COM
165*12720SWyllys.Ingersoll@Sun.COM return (state);
166*12720SWyllys.Ingersoll@Sun.COM }
167*12720SWyllys.Ingersoll@Sun.COM
168*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_GetSessionInfo(CK_SESSION_HANDLE hSession,CK_SESSION_INFO_PTR pInfo)169*12720SWyllys.Ingersoll@Sun.COM C_GetSessionInfo(CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo)
170*12720SWyllys.Ingersoll@Sun.COM {
171*12720SWyllys.Ingersoll@Sun.COM kms_session_t *session_p;
172*12720SWyllys.Ingersoll@Sun.COM CK_RV rv;
173*12720SWyllys.Ingersoll@Sun.COM boolean_t ses_lock_held = B_FALSE;
174*12720SWyllys.Ingersoll@Sun.COM
175*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
176*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
177*12720SWyllys.Ingersoll@Sun.COM
178*12720SWyllys.Ingersoll@Sun.COM if (pInfo == NULL)
179*12720SWyllys.Ingersoll@Sun.COM return (CKR_ARGUMENTS_BAD);
180*12720SWyllys.Ingersoll@Sun.COM
181*12720SWyllys.Ingersoll@Sun.COM /*
182*12720SWyllys.Ingersoll@Sun.COM * Obtain the session pointer. Also, increment the session
183*12720SWyllys.Ingersoll@Sun.COM * reference count.
184*12720SWyllys.Ingersoll@Sun.COM */
185*12720SWyllys.Ingersoll@Sun.COM rv = handle2session(hSession, &session_p);
186*12720SWyllys.Ingersoll@Sun.COM if (rv != CKR_OK)
187*12720SWyllys.Ingersoll@Sun.COM return (rv);
188*12720SWyllys.Ingersoll@Sun.COM
189*12720SWyllys.Ingersoll@Sun.COM /* Provide information for the specified session */
190*12720SWyllys.Ingersoll@Sun.COM pInfo->slotID = session_p->ses_slotid;
191*12720SWyllys.Ingersoll@Sun.COM pInfo->flags = session_p->flags;
192*12720SWyllys.Ingersoll@Sun.COM pInfo->ulDeviceError = 0;
193*12720SWyllys.Ingersoll@Sun.COM pInfo->state = get_ses_state(session_p);
194*12720SWyllys.Ingersoll@Sun.COM
195*12720SWyllys.Ingersoll@Sun.COM /*
196*12720SWyllys.Ingersoll@Sun.COM * Decrement the session reference count.
197*12720SWyllys.Ingersoll@Sun.COM */
198*12720SWyllys.Ingersoll@Sun.COM REFRELE(session_p, ses_lock_held);
199*12720SWyllys.Ingersoll@Sun.COM
200*12720SWyllys.Ingersoll@Sun.COM return (rv);
201*12720SWyllys.Ingersoll@Sun.COM }
202*12720SWyllys.Ingersoll@Sun.COM
203*12720SWyllys.Ingersoll@Sun.COM /*ARGSUSED*/
204*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_GetOperationState(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pOperationState,CK_ULONG_PTR pulOperationStateLen)205*12720SWyllys.Ingersoll@Sun.COM C_GetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
206*12720SWyllys.Ingersoll@Sun.COM CK_ULONG_PTR pulOperationStateLen)
207*12720SWyllys.Ingersoll@Sun.COM {
208*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
209*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
210*12720SWyllys.Ingersoll@Sun.COM
211*12720SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_NOT_SUPPORTED);
212*12720SWyllys.Ingersoll@Sun.COM }
213*12720SWyllys.Ingersoll@Sun.COM
214*12720SWyllys.Ingersoll@Sun.COM /*ARGSUSED*/
215*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_SetOperationState(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pOperationState,CK_ULONG ulOperationStateLen,CK_OBJECT_HANDLE hEncryptionKey,CK_OBJECT_HANDLE hAuthenticationKey)216*12720SWyllys.Ingersoll@Sun.COM C_SetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
217*12720SWyllys.Ingersoll@Sun.COM CK_ULONG ulOperationStateLen, CK_OBJECT_HANDLE hEncryptionKey,
218*12720SWyllys.Ingersoll@Sun.COM CK_OBJECT_HANDLE hAuthenticationKey)
219*12720SWyllys.Ingersoll@Sun.COM {
220*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
221*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
222*12720SWyllys.Ingersoll@Sun.COM
223*12720SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_NOT_SUPPORTED);
224*12720SWyllys.Ingersoll@Sun.COM }
225*12720SWyllys.Ingersoll@Sun.COM
226*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_Login(CK_SESSION_HANDLE hSession,CK_USER_TYPE userType,CK_UTF8CHAR_PTR pPin,CK_ULONG ulPinLen)227*12720SWyllys.Ingersoll@Sun.COM C_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
228*12720SWyllys.Ingersoll@Sun.COM CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
229*12720SWyllys.Ingersoll@Sun.COM {
230*12720SWyllys.Ingersoll@Sun.COM CK_RV rv = CKR_OK;
231*12720SWyllys.Ingersoll@Sun.COM kms_session_t *session_p;
232*12720SWyllys.Ingersoll@Sun.COM kms_slot_t *pslot;
233*12720SWyllys.Ingersoll@Sun.COM boolean_t ses_lock_held = B_FALSE;
234*12720SWyllys.Ingersoll@Sun.COM
235*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
236*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
237*12720SWyllys.Ingersoll@Sun.COM
238*12720SWyllys.Ingersoll@Sun.COM if ((userType != CKU_SO) && (userType != CKU_USER)) {
239*12720SWyllys.Ingersoll@Sun.COM return (CKR_USER_TYPE_INVALID);
240*12720SWyllys.Ingersoll@Sun.COM }
241*12720SWyllys.Ingersoll@Sun.COM
242*12720SWyllys.Ingersoll@Sun.COM /*
243*12720SWyllys.Ingersoll@Sun.COM * Obtain the session pointer. Also, increment the session
244*12720SWyllys.Ingersoll@Sun.COM * reference count.
245*12720SWyllys.Ingersoll@Sun.COM */
246*12720SWyllys.Ingersoll@Sun.COM rv = handle2session(hSession, &session_p);
247*12720SWyllys.Ingersoll@Sun.COM if (rv != CKR_OK)
248*12720SWyllys.Ingersoll@Sun.COM return (rv);
249*12720SWyllys.Ingersoll@Sun.COM
250*12720SWyllys.Ingersoll@Sun.COM /* Acquire the slot lock */
251*12720SWyllys.Ingersoll@Sun.COM pslot = get_slotinfo();
252*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_lock(&pslot->sl_mutex);
253*12720SWyllys.Ingersoll@Sun.COM
254*12720SWyllys.Ingersoll@Sun.COM /* Check if the slot is logged in already */
255*12720SWyllys.Ingersoll@Sun.COM if ((pslot->sl_state == CKU_USER) || (pslot->sl_state == CKU_SO)) {
256*12720SWyllys.Ingersoll@Sun.COM rv = CKR_USER_ALREADY_LOGGED_IN;
257*12720SWyllys.Ingersoll@Sun.COM goto clean_exit;
258*12720SWyllys.Ingersoll@Sun.COM }
259*12720SWyllys.Ingersoll@Sun.COM
260*12720SWyllys.Ingersoll@Sun.COM /* To login as SO, every session in this slot needs to be R/W */
261*12720SWyllys.Ingersoll@Sun.COM if (userType == CKU_SO) {
262*12720SWyllys.Ingersoll@Sun.COM kms_session_t *sp;
263*12720SWyllys.Ingersoll@Sun.COM boolean_t found;
264*12720SWyllys.Ingersoll@Sun.COM
265*12720SWyllys.Ingersoll@Sun.COM found = B_FALSE;
266*12720SWyllys.Ingersoll@Sun.COM sp = pslot->sl_sess_list;
267*12720SWyllys.Ingersoll@Sun.COM while (sp) {
268*12720SWyllys.Ingersoll@Sun.COM /*
269*12720SWyllys.Ingersoll@Sun.COM * Need not to lock individual sessions before
270*12720SWyllys.Ingersoll@Sun.COM * accessing their "ses_RO" and "next" fields,
271*12720SWyllys.Ingersoll@Sun.COM * because they are always accessed under the
272*12720SWyllys.Ingersoll@Sun.COM * slot's mutex protection.
273*12720SWyllys.Ingersoll@Sun.COM */
274*12720SWyllys.Ingersoll@Sun.COM if (sp->ses_RO) {
275*12720SWyllys.Ingersoll@Sun.COM found = B_TRUE;
276*12720SWyllys.Ingersoll@Sun.COM break;
277*12720SWyllys.Ingersoll@Sun.COM }
278*12720SWyllys.Ingersoll@Sun.COM sp = sp->next;
279*12720SWyllys.Ingersoll@Sun.COM }
280*12720SWyllys.Ingersoll@Sun.COM
281*12720SWyllys.Ingersoll@Sun.COM if (found) {
282*12720SWyllys.Ingersoll@Sun.COM rv = CKR_SESSION_READ_ONLY_EXISTS;
283*12720SWyllys.Ingersoll@Sun.COM goto clean_exit;
284*12720SWyllys.Ingersoll@Sun.COM }
285*12720SWyllys.Ingersoll@Sun.COM }
286*12720SWyllys.Ingersoll@Sun.COM
287*12720SWyllys.Ingersoll@Sun.COM /*
288*12720SWyllys.Ingersoll@Sun.COM * Login to KMS by attempting to load the profile using
289*12720SWyllys.Ingersoll@Sun.COM * the given password.
290*12720SWyllys.Ingersoll@Sun.COM */
291*12720SWyllys.Ingersoll@Sun.COM rv = KMS_LoadProfile(
292*12720SWyllys.Ingersoll@Sun.COM &session_p->kmsProfile,
293*12720SWyllys.Ingersoll@Sun.COM &session_p->configInfo,
294*12720SWyllys.Ingersoll@Sun.COM (const char *)pPin,
295*12720SWyllys.Ingersoll@Sun.COM (size_t)ulPinLen);
296*12720SWyllys.Ingersoll@Sun.COM
297*12720SWyllys.Ingersoll@Sun.COM if (rv == CKR_OK) {
298*12720SWyllys.Ingersoll@Sun.COM /* Set the slot's session state. */
299*12720SWyllys.Ingersoll@Sun.COM pslot->sl_state = userType;
300*12720SWyllys.Ingersoll@Sun.COM }
301*12720SWyllys.Ingersoll@Sun.COM
302*12720SWyllys.Ingersoll@Sun.COM clean_exit:
303*12720SWyllys.Ingersoll@Sun.COM
304*12720SWyllys.Ingersoll@Sun.COM REFRELE(session_p, ses_lock_held);
305*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_unlock(&pslot->sl_mutex);
306*12720SWyllys.Ingersoll@Sun.COM return (rv);
307*12720SWyllys.Ingersoll@Sun.COM }
308*12720SWyllys.Ingersoll@Sun.COM
309*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_Logout(CK_SESSION_HANDLE hSession)310*12720SWyllys.Ingersoll@Sun.COM C_Logout(CK_SESSION_HANDLE hSession)
311*12720SWyllys.Ingersoll@Sun.COM {
312*12720SWyllys.Ingersoll@Sun.COM CK_RV rv = CKR_OK;
313*12720SWyllys.Ingersoll@Sun.COM kms_session_t *session_p;
314*12720SWyllys.Ingersoll@Sun.COM kms_slot_t *pslot;
315*12720SWyllys.Ingersoll@Sun.COM boolean_t ses_lock_held = B_FALSE;
316*12720SWyllys.Ingersoll@Sun.COM
317*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
318*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
319*12720SWyllys.Ingersoll@Sun.COM
320*12720SWyllys.Ingersoll@Sun.COM /*
321*12720SWyllys.Ingersoll@Sun.COM * Obtain the session pointer. Also, increment the session
322*12720SWyllys.Ingersoll@Sun.COM * reference count.
323*12720SWyllys.Ingersoll@Sun.COM */
324*12720SWyllys.Ingersoll@Sun.COM rv = handle2session(hSession, &session_p);
325*12720SWyllys.Ingersoll@Sun.COM if (rv != CKR_OK)
326*12720SWyllys.Ingersoll@Sun.COM return (rv);
327*12720SWyllys.Ingersoll@Sun.COM
328*12720SWyllys.Ingersoll@Sun.COM /* Acquire the slot lock. */
329*12720SWyllys.Ingersoll@Sun.COM pslot = get_slotinfo();
330*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_lock(&pslot->sl_mutex);
331*12720SWyllys.Ingersoll@Sun.COM
332*12720SWyllys.Ingersoll@Sun.COM /* Check if the user or SO was logged in */
333*12720SWyllys.Ingersoll@Sun.COM if (pslot->sl_state == CKU_PUBLIC) {
334*12720SWyllys.Ingersoll@Sun.COM rv = CKR_USER_NOT_LOGGED_IN;
335*12720SWyllys.Ingersoll@Sun.COM goto clean_exit;
336*12720SWyllys.Ingersoll@Sun.COM }
337*12720SWyllys.Ingersoll@Sun.COM
338*12720SWyllys.Ingersoll@Sun.COM KMS_UnloadProfile(&session_p->kmsProfile);
339*12720SWyllys.Ingersoll@Sun.COM
340*12720SWyllys.Ingersoll@Sun.COM /*
341*12720SWyllys.Ingersoll@Sun.COM * If this slot was logged in as USER previously, we need to clean up
342*12720SWyllys.Ingersoll@Sun.COM * all private object wrappers in library for this slot.
343*12720SWyllys.Ingersoll@Sun.COM */
344*12720SWyllys.Ingersoll@Sun.COM kms_cleanup_pri_objects_in_slot(pslot, session_p);
345*12720SWyllys.Ingersoll@Sun.COM
346*12720SWyllys.Ingersoll@Sun.COM if (rv == CKR_OK) {
347*12720SWyllys.Ingersoll@Sun.COM /* Reset the slot's session state. */
348*12720SWyllys.Ingersoll@Sun.COM pslot->sl_state = CKU_PUBLIC;
349*12720SWyllys.Ingersoll@Sun.COM }
350*12720SWyllys.Ingersoll@Sun.COM
351*12720SWyllys.Ingersoll@Sun.COM clean_exit:
352*12720SWyllys.Ingersoll@Sun.COM REFRELE(session_p, ses_lock_held);
353*12720SWyllys.Ingersoll@Sun.COM (void) pthread_mutex_unlock(&pslot->sl_mutex);
354*12720SWyllys.Ingersoll@Sun.COM return (rv);
355*12720SWyllys.Ingersoll@Sun.COM }
356