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 2003 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 <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
30*7c478bd9Sstevel@tonic-gate #include "pkcs11Global.h"
31*7c478bd9Sstevel@tonic-gate #include "pkcs11Slot.h"
32*7c478bd9Sstevel@tonic-gate #include "pkcs11Session.h"
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gate
35*7c478bd9Sstevel@tonic-gate /*
36*7c478bd9Sstevel@tonic-gate * pkcs11_session_add:
37*7c478bd9Sstevel@tonic-gate * Create a session and add it to the list of sessions associated
38*7c478bd9Sstevel@tonic-gate * with the slot it is being opened on. The session handle, fwsessionp,
39*7c478bd9Sstevel@tonic-gate * will be the memory address of the session, typecast to a CK_SESSION_HANDLE.
40*7c478bd9Sstevel@tonic-gate *
41*7c478bd9Sstevel@tonic-gate * Assumptions: slotp is a valid slot, mutexes are not held, and
42*7c478bd9Sstevel@tonic-gate * the provider already successfully opened related session.
43*7c478bd9Sstevel@tonic-gate */
44*7c478bd9Sstevel@tonic-gate CK_RV
pkcs11_session_add(pkcs11_slot_t * slotp,CK_SLOT_ID slot_id,CK_SESSION_HANDLE_PTR fwsessionp,CK_SESSION_HANDLE prov_sess)45*7c478bd9Sstevel@tonic-gate pkcs11_session_add(pkcs11_slot_t *slotp, CK_SLOT_ID slot_id,
46*7c478bd9Sstevel@tonic-gate CK_SESSION_HANDLE_PTR fwsessionp, CK_SESSION_HANDLE prov_sess)
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate pkcs11_session_t *newhandle = malloc(sizeof (pkcs11_session_t));
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate if (newhandle == NULL) {
52*7c478bd9Sstevel@tonic-gate return (CKR_HOST_MEMORY);
53*7c478bd9Sstevel@tonic-gate }
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate newhandle->se_magic = PKCS11_SESSION_MAGIC;
56*7c478bd9Sstevel@tonic-gate newhandle->se_handle = prov_sess;
57*7c478bd9Sstevel@tonic-gate newhandle->se_slotid = slot_id;
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&slotp->sl_mutex);
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate /* Insert the new session in the front of the slot's session list */
62*7c478bd9Sstevel@tonic-gate if (slotp->sl_sess_list == NULL) {
63*7c478bd9Sstevel@tonic-gate slotp->sl_sess_list = newhandle;
64*7c478bd9Sstevel@tonic-gate newhandle->se_prev = NULL;
65*7c478bd9Sstevel@tonic-gate newhandle->se_next = NULL;
66*7c478bd9Sstevel@tonic-gate } else {
67*7c478bd9Sstevel@tonic-gate slotp->sl_sess_list->se_prev = newhandle;
68*7c478bd9Sstevel@tonic-gate newhandle->se_next = slotp->sl_sess_list;
69*7c478bd9Sstevel@tonic-gate newhandle->se_prev = NULL;
70*7c478bd9Sstevel@tonic-gate slotp->sl_sess_list = newhandle;
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate /* Typecast the address of session structure to a session handle */
74*7c478bd9Sstevel@tonic-gate *fwsessionp = (CK_SESSION_HANDLE)newhandle;
75*7c478bd9Sstevel@tonic-gate
76*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&slotp->sl_mutex);
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate return (CKR_OK);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate /*
82*7c478bd9Sstevel@tonic-gate * pkcs11_session_delete:
83*7c478bd9Sstevel@tonic-gate * Delete a session from a particular slot's session list.
84*7c478bd9Sstevel@tonic-gate *
85*7c478bd9Sstevel@tonic-gate * Assumptions: slotp is a valid slot, sessp is a valid session,
86*7c478bd9Sstevel@tonic-gate * provider has already successfully closed this session, and
87*7c478bd9Sstevel@tonic-gate * mutexes are not held.
88*7c478bd9Sstevel@tonic-gate */
89*7c478bd9Sstevel@tonic-gate void
pkcs11_session_delete(pkcs11_slot_t * slotp,pkcs11_session_t * sessp)90*7c478bd9Sstevel@tonic-gate pkcs11_session_delete(pkcs11_slot_t *slotp, pkcs11_session_t *sessp)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate /* Acquire the slot's lock */
94*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&slotp->sl_mutex);
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate if (slotp->sl_sess_list == sessp) {
97*7c478bd9Sstevel@tonic-gate /* This is the first session in the list */
98*7c478bd9Sstevel@tonic-gate if (sessp->se_next != NULL) {
99*7c478bd9Sstevel@tonic-gate slotp->sl_sess_list = sessp->se_next;
100*7c478bd9Sstevel@tonic-gate sessp->se_next->se_prev = NULL;
101*7c478bd9Sstevel@tonic-gate } else {
102*7c478bd9Sstevel@tonic-gate /* Session is the only one in the list */
103*7c478bd9Sstevel@tonic-gate slotp->sl_sess_list = NULL;
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate } else {
106*7c478bd9Sstevel@tonic-gate /* Session is not the first one in the list */
107*7c478bd9Sstevel@tonic-gate if (sessp->se_next != NULL) {
108*7c478bd9Sstevel@tonic-gate /* Session is in the middle of the list */
109*7c478bd9Sstevel@tonic-gate sessp->se_prev->se_next = sessp->se_next;
110*7c478bd9Sstevel@tonic-gate sessp->se_next->se_prev = sessp->se_prev;
111*7c478bd9Sstevel@tonic-gate } else {
112*7c478bd9Sstevel@tonic-gate /* Session is the last one in the list */
113*7c478bd9Sstevel@tonic-gate sessp->se_prev->se_next = NULL;
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate
117*7c478bd9Sstevel@tonic-gate /* Mark session as no longer valid */
118*7c478bd9Sstevel@tonic-gate sessp->se_magic = 0;
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate free(sessp);
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&slotp->sl_mutex);
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate /*
127*7c478bd9Sstevel@tonic-gate * pkcs11_sessionlist_delete:
128*7c478bd9Sstevel@tonic-gate * Delete all sessions associated with a particular slot's session list.
129*7c478bd9Sstevel@tonic-gate *
130*7c478bd9Sstevel@tonic-gate * Assumptions: slotp is a valid slot, no mutexes are held, and the
131*7c478bd9Sstevel@tonic-gate * sessions were successfully closed with the provider already.
132*7c478bd9Sstevel@tonic-gate */
133*7c478bd9Sstevel@tonic-gate void
pkcs11_sessionlist_delete(pkcs11_slot_t * slotp)134*7c478bd9Sstevel@tonic-gate pkcs11_sessionlist_delete(pkcs11_slot_t *slotp)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate pkcs11_session_t *sessp, *sess_nextp;
138*7c478bd9Sstevel@tonic-gate
139*7c478bd9Sstevel@tonic-gate sessp = slotp->sl_sess_list;
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate /* Delete all the sessions in this slot's session list */
142*7c478bd9Sstevel@tonic-gate while (sessp) {
143*7c478bd9Sstevel@tonic-gate sess_nextp = sessp->se_next;
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate pkcs11_session_delete(slotp, sessp);
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate sessp = sess_nextp;
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate slotp->sl_sess_list = NULL;
151*7c478bd9Sstevel@tonic-gate
152*7c478bd9Sstevel@tonic-gate }
153