1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <pthread.h>
28*7c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
29*7c478bd9Sstevel@tonic-gate #include "pkcs11Global.h"
30*7c478bd9Sstevel@tonic-gate #include "pkcs11Session.h"
31*7c478bd9Sstevel@tonic-gate #include "pkcs11Slot.h"
32*7c478bd9Sstevel@tonic-gate #include "metaGlobal.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * C_OpenSession will need to create a pseudo session associated
36*7c478bd9Sstevel@tonic-gate  * with the session created by the plugged in provider.  Only
37*7c478bd9Sstevel@tonic-gate  * minimal argument checking is done here, as we rely on the
38*7c478bd9Sstevel@tonic-gate  * underlying provider to catch most errors.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate CK_RV
C_OpenSession(CK_SLOT_ID slotID,CK_FLAGS flags,CK_VOID_PTR pApplication,CK_NOTIFY Notify,CK_SESSION_HANDLE_PTR phSession)41*7c478bd9Sstevel@tonic-gate C_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
42*7c478bd9Sstevel@tonic-gate     CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
46*7c478bd9Sstevel@tonic-gate 	CK_SLOT_ID true_id;
47*7c478bd9Sstevel@tonic-gate 	CK_SLOT_ID fw_st_id; /* id for accessing framework's slottable */
48*7c478bd9Sstevel@tonic-gate 	CK_SESSION_HANDLE prov_sess;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	if (!pkcs11_initialized) {
51*7c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
52*7c478bd9Sstevel@tonic-gate 	}
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	/* Check for a fastpath */
55*7c478bd9Sstevel@tonic-gate 	if (purefastpath || policyfastpath) {
56*7c478bd9Sstevel@tonic-gate 		if (metaslot_enabled) {
57*7c478bd9Sstevel@tonic-gate 			/*
58*7c478bd9Sstevel@tonic-gate 			 * if metaslot is enabled and we are in fastpath
59*7c478bd9Sstevel@tonic-gate 			 * mode, only one other slot is in the framework
60*7c478bd9Sstevel@tonic-gate 			 * so, need to go to that slot's entry
61*7c478bd9Sstevel@tonic-gate 			 * to look up the true slot ID for the slot
62*7c478bd9Sstevel@tonic-gate 			 */
63*7c478bd9Sstevel@tonic-gate 			return (fast_funcs->C_OpenSession(TRUEID(slotID+1),
64*7c478bd9Sstevel@tonic-gate 			    flags, pApplication, Notify, phSession));
65*7c478bd9Sstevel@tonic-gate 		} else {
66*7c478bd9Sstevel@tonic-gate 			return (fast_funcs->C_OpenSession(slotID, flags,
67*7c478bd9Sstevel@tonic-gate 			    pApplication, Notify, phSession));
68*7c478bd9Sstevel@tonic-gate 		}
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	if (slotID == METASLOT_FRAMEWORK_ID) {
73*7c478bd9Sstevel@tonic-gate 		rv = meta_OpenSession(METASLOT_SLOTID, flags,
74*7c478bd9Sstevel@tonic-gate 		    pApplication, Notify, &prov_sess);
75*7c478bd9Sstevel@tonic-gate 	} else {
76*7c478bd9Sstevel@tonic-gate 		/* Check that slotID is valid */
77*7c478bd9Sstevel@tonic-gate 		if (pkcs11_validate_and_convert_slotid(slotID, &fw_st_id)
78*7c478bd9Sstevel@tonic-gate 		    != CKR_OK) {
79*7c478bd9Sstevel@tonic-gate 			return (CKR_SLOT_ID_INVALID);
80*7c478bd9Sstevel@tonic-gate 		}
81*7c478bd9Sstevel@tonic-gate 		true_id = TRUEID(fw_st_id);
82*7c478bd9Sstevel@tonic-gate 		rv = FUNCLIST(fw_st_id)->C_OpenSession(true_id, flags,
83*7c478bd9Sstevel@tonic-gate 		    pApplication, Notify, &prov_sess);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/* Present consistent interface for framework */
87*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
88*7c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
89*7c478bd9Sstevel@tonic-gate 	} else if (rv != CKR_OK) {
90*7c478bd9Sstevel@tonic-gate 		/* could not create session with provider, return now */
91*7c478bd9Sstevel@tonic-gate 		return (rv);
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	/* Provider was successful, now create session in framework */
95*7c478bd9Sstevel@tonic-gate 	if (slotID == METASLOT_FRAMEWORK_ID) {
96*7c478bd9Sstevel@tonic-gate 		rv = pkcs11_session_add(
97*7c478bd9Sstevel@tonic-gate 		    slottable->st_slots[METASLOT_FRAMEWORK_ID],
98*7c478bd9Sstevel@tonic-gate 		    METASLOT_FRAMEWORK_ID, phSession, prov_sess);
99*7c478bd9Sstevel@tonic-gate 	} else {
100*7c478bd9Sstevel@tonic-gate 		rv = pkcs11_session_add(slottable->st_slots[fw_st_id],
101*7c478bd9Sstevel@tonic-gate 		    fw_st_id, phSession, prov_sess);
102*7c478bd9Sstevel@tonic-gate 	}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
105*7c478bd9Sstevel@tonic-gate 		/* Trouble in the framework, clean up provider session */
106*7c478bd9Sstevel@tonic-gate 		FUNCLIST(slotID)->C_CloseSession(prov_sess);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 	return (rv);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate  * C_CloseSession will close a session with the underlying provider,
113*7c478bd9Sstevel@tonic-gate  * and if that's successful will close it in the framework.
114*7c478bd9Sstevel@tonic-gate  */
115*7c478bd9Sstevel@tonic-gate CK_RV
C_CloseSession(CK_SESSION_HANDLE hSession)116*7c478bd9Sstevel@tonic-gate C_CloseSession(CK_SESSION_HANDLE hSession)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
119*7c478bd9Sstevel@tonic-gate 	pkcs11_session_t *sessp;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	/* Check for a fastpath */
122*7c478bd9Sstevel@tonic-gate 	if (purefastpath || policyfastpath) {
123*7c478bd9Sstevel@tonic-gate 		return (fast_funcs->C_CloseSession(hSession));
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (!pkcs11_initialized) {
127*7c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer */
131*7c478bd9Sstevel@tonic-gate 	HANDLE2SESSION(hSession, sessp, rv);
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
134*7c478bd9Sstevel@tonic-gate 		return (rv);
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	/* Delete the session with the provider */
138*7c478bd9Sstevel@tonic-gate 	rv = FUNCLIST(sessp->se_slotid)->C_CloseSession(sessp->se_handle);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	/* Present consistent interface for framework */
141*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
142*7c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
143*7c478bd9Sstevel@tonic-gate 	} else if (rv != CKR_OK) {
144*7c478bd9Sstevel@tonic-gate 		/* could not delete session with provider, return now */
145*7c478bd9Sstevel@tonic-gate 		return (rv);
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	/* Delete session from the framework */
149*7c478bd9Sstevel@tonic-gate 	pkcs11_session_delete(slottable->st_slots[sessp->se_slotid], sessp);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	return (rv);
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /*
155*7c478bd9Sstevel@tonic-gate  * C_CloseAllSessions will close all sessions associated with this
156*7c478bd9Sstevel@tonic-gate  * slot with the underlying provider.  If that is successful, will
157*7c478bd9Sstevel@tonic-gate  * close the associated sessions in the framework.  If the provider
158*7c478bd9Sstevel@tonic-gate  * has not implemented C_CloseAllSessions, then we will loop through
159*7c478bd9Sstevel@tonic-gate  * the list of sessions and individually call C_CloseSession.
160*7c478bd9Sstevel@tonic-gate  */
161*7c478bd9Sstevel@tonic-gate CK_RV
C_CloseAllSessions(CK_SLOT_ID slotID)162*7c478bd9Sstevel@tonic-gate C_CloseAllSessions(CK_SLOT_ID slotID)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	CK_RV rv, rv1;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	CK_SLOT_ID true_id;
168*7c478bd9Sstevel@tonic-gate 	CK_SLOT_ID fw_st_id; /* id for accessing framework's slottable */
169*7c478bd9Sstevel@tonic-gate 	pkcs11_session_t *sessp, *sess_nextp;
170*7c478bd9Sstevel@tonic-gate 	pkcs11_slot_t *slotp;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if (!pkcs11_initialized) {
173*7c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
174*7c478bd9Sstevel@tonic-gate 	}
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	/* Check for a fastpath */
177*7c478bd9Sstevel@tonic-gate 	if (purefastpath || policyfastpath) {
178*7c478bd9Sstevel@tonic-gate 		if (metaslot_enabled) {
179*7c478bd9Sstevel@tonic-gate 			/*
180*7c478bd9Sstevel@tonic-gate 			 * if metaslot is enabled and we are in fastpath
181*7c478bd9Sstevel@tonic-gate 			 * mode, only one other slot is in the framework
182*7c478bd9Sstevel@tonic-gate 			 * so, need to go to that slot's entry
183*7c478bd9Sstevel@tonic-gate 			 * to look up the true slot ID for the slot
184*7c478bd9Sstevel@tonic-gate 			 */
185*7c478bd9Sstevel@tonic-gate 			return (fast_funcs->C_CloseAllSessions(
186*7c478bd9Sstevel@tonic-gate 			    TRUEID(slotID+1)));
187*7c478bd9Sstevel@tonic-gate 		} else {
188*7c478bd9Sstevel@tonic-gate 			return (fast_funcs->C_CloseAllSessions(slotID));
189*7c478bd9Sstevel@tonic-gate 		}
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	/* Check that slotID is valid */
193*7c478bd9Sstevel@tonic-gate 	if (pkcs11_validate_and_convert_slotid(slotID, &fw_st_id) != CKR_OK) {
194*7c478bd9Sstevel@tonic-gate 		return (CKR_SLOT_ID_INVALID);
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	slotp = slottable->st_slots[fw_st_id];
198*7c478bd9Sstevel@tonic-gate 	true_id = TRUEID(fw_st_id);
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	rv = FUNCLIST(fw_st_id)->C_CloseAllSessions(true_id);
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	/* Present consistent interface for framework */
203*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
204*7c478bd9Sstevel@tonic-gate 		/* Need to attempt to individually delete sessions */
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 		/* reset rv */
207*7c478bd9Sstevel@tonic-gate 		rv = CKR_OK;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&slotp->sl_mutex);
210*7c478bd9Sstevel@tonic-gate 		sessp = slotp->sl_sess_list;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 		while (sessp) {
213*7c478bd9Sstevel@tonic-gate 			sess_nextp = sessp->se_next;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 			rv1 = FUNCLIST(fw_st_id)->
216*7c478bd9Sstevel@tonic-gate 			    C_CloseSession(sessp->se_handle);
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 			/* Record the first error encountered */
219*7c478bd9Sstevel@tonic-gate 			if ((rv == CKR_OK) && (rv1 != CKR_OK)) {
220*7c478bd9Sstevel@tonic-gate 				rv = rv1;
221*7c478bd9Sstevel@tonic-gate 			}
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 			sessp = sess_nextp;
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&slotp->sl_mutex);
227*7c478bd9Sstevel@tonic-gate 	}
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
230*7c478bd9Sstevel@tonic-gate 		/* could not delete sessionlist with provider, return now */
231*7c478bd9Sstevel@tonic-gate 		return (rv);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	/* Delete sessions from the framework */
235*7c478bd9Sstevel@tonic-gate 	pkcs11_sessionlist_delete(slotp);
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	return (rv);
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate /*
241*7c478bd9Sstevel@tonic-gate  * C_GetSessionInfo is a pure wrapper to the underlying provider.
242*7c478bd9Sstevel@tonic-gate  * The only argument checked is whether or not hSession is valid.
243*7c478bd9Sstevel@tonic-gate  */
244*7c478bd9Sstevel@tonic-gate CK_RV
C_GetSessionInfo(CK_SESSION_HANDLE hSession,CK_SESSION_INFO_PTR pInfo)245*7c478bd9Sstevel@tonic-gate C_GetSessionInfo(CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo)
246*7c478bd9Sstevel@tonic-gate {
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
249*7c478bd9Sstevel@tonic-gate 	CK_SLOT_ID slot_id;
250*7c478bd9Sstevel@tonic-gate 	pkcs11_session_t *sessp;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	/* Check for a fastpath */
253*7c478bd9Sstevel@tonic-gate 	if (purefastpath || policyfastpath) {
254*7c478bd9Sstevel@tonic-gate 		rv = fast_funcs->C_GetSessionInfo(hSession, pInfo);
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 		/*
257*7c478bd9Sstevel@tonic-gate 		 * If metaslot is enabled, and we are here, that
258*7c478bd9Sstevel@tonic-gate 		 * that means there's only 1 other slot in the
259*7c478bd9Sstevel@tonic-gate 		 * framework, and that slot should be hidden.
260*7c478bd9Sstevel@tonic-gate 		 * so, override value of slot id to be metaslot's
261*7c478bd9Sstevel@tonic-gate 		 * slot id.
262*7c478bd9Sstevel@tonic-gate 		 */
263*7c478bd9Sstevel@tonic-gate 		if (metaslot_enabled) {
264*7c478bd9Sstevel@tonic-gate 			pInfo->slotID = METASLOT_FRAMEWORK_ID;
265*7c478bd9Sstevel@tonic-gate 		}
266*7c478bd9Sstevel@tonic-gate 		return (rv);
267*7c478bd9Sstevel@tonic-gate 	}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	if (!pkcs11_initialized) {
270*7c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
271*7c478bd9Sstevel@tonic-gate 	}
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer */
274*7c478bd9Sstevel@tonic-gate 	HANDLE2SESSION(hSession, sessp, rv);
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
277*7c478bd9Sstevel@tonic-gate 		return (rv);
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	/* Find the slot id for the framework */
281*7c478bd9Sstevel@tonic-gate 	slot_id = sessp->se_slotid;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	/* Get session info from the provider */
284*7c478bd9Sstevel@tonic-gate 	rv = FUNCLIST(slot_id)->
285*7c478bd9Sstevel@tonic-gate 	    C_GetSessionInfo(sessp->se_handle, pInfo);
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	/* Present consistent interface to the application */
288*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
289*7c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	/* Override value of slot id to framework's */
293*7c478bd9Sstevel@tonic-gate 	pInfo->slotID = slot_id;
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	return (rv);
296*7c478bd9Sstevel@tonic-gate }
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate /*
299*7c478bd9Sstevel@tonic-gate  * C_GetOperationState is a pure wrapper to the underlying provider.
300*7c478bd9Sstevel@tonic-gate  * The only argument checked is whether or not hSession is valid.
301*7c478bd9Sstevel@tonic-gate  */
302*7c478bd9Sstevel@tonic-gate CK_RV
C_GetOperationState(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pOperationState,CK_ULONG_PTR pulOperationStateLen)303*7c478bd9Sstevel@tonic-gate C_GetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
304*7c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulOperationStateLen)
305*7c478bd9Sstevel@tonic-gate {
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
308*7c478bd9Sstevel@tonic-gate 	pkcs11_session_t *sessp;
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	/* Check for a fastpath */
311*7c478bd9Sstevel@tonic-gate 	if (purefastpath || policyfastpath) {
312*7c478bd9Sstevel@tonic-gate 		return (fast_funcs->C_GetOperationState(hSession,
313*7c478bd9Sstevel@tonic-gate 			    pOperationState, pulOperationStateLen));
314*7c478bd9Sstevel@tonic-gate 	}
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	if (!pkcs11_initialized) {
317*7c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
318*7c478bd9Sstevel@tonic-gate 	}
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer */
321*7c478bd9Sstevel@tonic-gate 	HANDLE2SESSION(hSession, sessp, rv);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
324*7c478bd9Sstevel@tonic-gate 		return (rv);
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	/* Get the operation state with the provider */
328*7c478bd9Sstevel@tonic-gate 	rv = FUNCLIST(sessp->se_slotid)->C_GetOperationState(sessp->se_handle,
329*7c478bd9Sstevel@tonic-gate 		pOperationState, pulOperationStateLen);
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	/* Present consistent interface to the application */
332*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
333*7c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
334*7c478bd9Sstevel@tonic-gate 	}
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 	return (rv);
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate /*
341*7c478bd9Sstevel@tonic-gate  * C_SetOperationState is a pure wrapper to the underlying provider.
342*7c478bd9Sstevel@tonic-gate  * The only argument checked is whether or not hSession is valid.
343*7c478bd9Sstevel@tonic-gate  */
344*7c478bd9Sstevel@tonic-gate CK_RV
C_SetOperationState(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pOperationState,CK_ULONG ulOperationStateLen,CK_OBJECT_HANDLE hEncryptionKey,CK_OBJECT_HANDLE hAuthenticationKey)345*7c478bd9Sstevel@tonic-gate C_SetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
346*7c478bd9Sstevel@tonic-gate     CK_ULONG ulOperationStateLen, CK_OBJECT_HANDLE hEncryptionKey,
347*7c478bd9Sstevel@tonic-gate     CK_OBJECT_HANDLE hAuthenticationKey)
348*7c478bd9Sstevel@tonic-gate {
349*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
350*7c478bd9Sstevel@tonic-gate 	pkcs11_session_t *sessp;
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	/* Check for a fastpath */
353*7c478bd9Sstevel@tonic-gate 	if (purefastpath || policyfastpath) {
354*7c478bd9Sstevel@tonic-gate 		return (fast_funcs->C_SetOperationState(hSession,
355*7c478bd9Sstevel@tonic-gate 			    pOperationState, ulOperationStateLen,
356*7c478bd9Sstevel@tonic-gate 			    hEncryptionKey, hAuthenticationKey));
357*7c478bd9Sstevel@tonic-gate 	}
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	if (!pkcs11_initialized) {
360*7c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer */
364*7c478bd9Sstevel@tonic-gate 	HANDLE2SESSION(hSession, sessp, rv);
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
367*7c478bd9Sstevel@tonic-gate 		return (rv);
368*7c478bd9Sstevel@tonic-gate 	}
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 	/* Set the operation state with the provider */
371*7c478bd9Sstevel@tonic-gate 	rv = FUNCLIST(sessp->se_slotid)->C_SetOperationState(sessp->se_handle,
372*7c478bd9Sstevel@tonic-gate 		pOperationState, ulOperationStateLen, hEncryptionKey,
373*7c478bd9Sstevel@tonic-gate 		hAuthenticationKey);
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	/* Present consistent interface to the application */
376*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
377*7c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
378*7c478bd9Sstevel@tonic-gate 	}
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	return (rv);
381*7c478bd9Sstevel@tonic-gate }
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate /*
385*7c478bd9Sstevel@tonic-gate  * C_Login is a pure wrapper to the underlying provider.
386*7c478bd9Sstevel@tonic-gate  * The only argument checked is whether or not hSession is valid.
387*7c478bd9Sstevel@tonic-gate  */
388*7c478bd9Sstevel@tonic-gate CK_RV
C_Login(CK_SESSION_HANDLE hSession,CK_USER_TYPE userType,CK_UTF8CHAR_PTR pPin,CK_ULONG ulPinLen)389*7c478bd9Sstevel@tonic-gate C_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
390*7c478bd9Sstevel@tonic-gate 	CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
391*7c478bd9Sstevel@tonic-gate {
392*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
393*7c478bd9Sstevel@tonic-gate 	pkcs11_session_t *sessp;
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	/* Check for a fastpath */
396*7c478bd9Sstevel@tonic-gate 	if (purefastpath || policyfastpath) {
397*7c478bd9Sstevel@tonic-gate 		return (fast_funcs->C_Login(hSession, userType, pPin,
398*7c478bd9Sstevel@tonic-gate 			    ulPinLen));
399*7c478bd9Sstevel@tonic-gate 	}
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 	if (!pkcs11_initialized) {
402*7c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer */
406*7c478bd9Sstevel@tonic-gate 	HANDLE2SESSION(hSession, sessp, rv);
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
409*7c478bd9Sstevel@tonic-gate 		return (rv);
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	/* Login with the provider */
413*7c478bd9Sstevel@tonic-gate 	rv = FUNCLIST(sessp->se_slotid)->C_Login(sessp->se_handle,
414*7c478bd9Sstevel@tonic-gate 	    userType, pPin, ulPinLen);
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	/* Present consistent interface to the application */
417*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
418*7c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
419*7c478bd9Sstevel@tonic-gate 	}
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	return (rv);
422*7c478bd9Sstevel@tonic-gate }
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate /*
425*7c478bd9Sstevel@tonic-gate  * C_Logout is a pure wrapper to the underlying provider.
426*7c478bd9Sstevel@tonic-gate  * The only argument checked is whether or not hSession is valid.
427*7c478bd9Sstevel@tonic-gate  */
428*7c478bd9Sstevel@tonic-gate CK_RV
C_Logout(CK_SESSION_HANDLE hSession)429*7c478bd9Sstevel@tonic-gate C_Logout(CK_SESSION_HANDLE hSession)
430*7c478bd9Sstevel@tonic-gate {
431*7c478bd9Sstevel@tonic-gate 	CK_RV rv;
432*7c478bd9Sstevel@tonic-gate 	pkcs11_session_t *sessp;
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	/* Check for a fastpath */
435*7c478bd9Sstevel@tonic-gate 	if (purefastpath || policyfastpath) {
436*7c478bd9Sstevel@tonic-gate 		return (fast_funcs->C_Logout(hSession));
437*7c478bd9Sstevel@tonic-gate 	}
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	if (!pkcs11_initialized) {
440*7c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
441*7c478bd9Sstevel@tonic-gate 	}
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer */
444*7c478bd9Sstevel@tonic-gate 	HANDLE2SESSION(hSession, sessp, rv);
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
447*7c478bd9Sstevel@tonic-gate 		return (rv);
448*7c478bd9Sstevel@tonic-gate 	}
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	rv = FUNCLIST(sessp->se_slotid)->C_Logout(sessp->se_handle);
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 	/* Present consistent interface to the application */
453*7c478bd9Sstevel@tonic-gate 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
454*7c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
455*7c478bd9Sstevel@tonic-gate 	}
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 	return (rv);
458*7c478bd9Sstevel@tonic-gate }
459