2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <pthread.h>
2N/A#include <stdlib.h>
2N/A#include <security/cryptoki.h>
2N/A#include "pkcs11Global.h"
2N/A#include "pkcs11Slot.h"
2N/A#include "pkcs11Session.h"
2N/A
2N/A
2N/A/*
2N/A * pkcs11_session_add:
2N/A * Create a session and add it to the list of sessions associated
2N/A * with the slot it is being opened on. The session handle, fwsessionp,
2N/A * will be the memory address of the session, typecast to a CK_SESSION_HANDLE.
2N/A *
2N/A * Assumptions: slotp is a valid slot, mutexes are not held, and
2N/A * the provider already successfully opened related session.
2N/A */
2N/ACK_RV
2N/Apkcs11_session_add(pkcs11_slot_t *slotp, CK_SLOT_ID slot_id,
2N/A CK_SESSION_HANDLE_PTR fwsessionp, CK_SESSION_HANDLE prov_sess)
2N/A{
2N/A
2N/A pkcs11_session_t *newhandle = malloc(sizeof (pkcs11_session_t));
2N/A
2N/A if (newhandle == NULL) {
2N/A return (CKR_HOST_MEMORY);
2N/A }
2N/A
2N/A newhandle->se_magic = PKCS11_SESSION_MAGIC;
2N/A newhandle->se_handle = prov_sess;
2N/A newhandle->se_slotid = slot_id;
2N/A
2N/A (void) pthread_mutex_lock(&slotp->sl_mutex);
2N/A
2N/A /* Insert the new session in the front of the slot's session list */
2N/A if (slotp->sl_sess_list == NULL) {
2N/A slotp->sl_sess_list = newhandle;
2N/A newhandle->se_prev = NULL;
2N/A newhandle->se_next = NULL;
2N/A } else {
2N/A slotp->sl_sess_list->se_prev = newhandle;
2N/A newhandle->se_next = slotp->sl_sess_list;
2N/A newhandle->se_prev = NULL;
2N/A slotp->sl_sess_list = newhandle;
2N/A }
2N/A
2N/A /* Typecast the address of session structure to a session handle */
2N/A *fwsessionp = (CK_SESSION_HANDLE)newhandle;
2N/A
2N/A (void) pthread_mutex_unlock(&slotp->sl_mutex);
2N/A
2N/A return (CKR_OK);
2N/A}
2N/A
2N/A/*
2N/A * pkcs11_session_delete:
2N/A * Delete a session from a particular slot's session list.
2N/A *
2N/A * Assumptions: slotp is a valid slot, sessp is a valid session,
2N/A * provider has already successfully closed this session, and
2N/A * mutexes are not held.
2N/A */
2N/Avoid
2N/Apkcs11_session_delete(pkcs11_slot_t *slotp, pkcs11_session_t *sessp)
2N/A{
2N/A
2N/A /* Acquire the slot's lock */
2N/A (void) pthread_mutex_lock(&slotp->sl_mutex);
2N/A
2N/A if (slotp->sl_sess_list == sessp) {
2N/A /* This is the first session in the list */
2N/A if (sessp->se_next != NULL) {
2N/A slotp->sl_sess_list = sessp->se_next;
2N/A sessp->se_next->se_prev = NULL;
2N/A } else {
2N/A /* Session is the only one in the list */
2N/A slotp->sl_sess_list = NULL;
2N/A }
2N/A } else {
2N/A /* Session is not the first one in the list */
2N/A if (sessp->se_next != NULL) {
2N/A /* Session is in the middle of the list */
2N/A sessp->se_prev->se_next = sessp->se_next;
2N/A sessp->se_next->se_prev = sessp->se_prev;
2N/A } else {
2N/A /* Session is the last one in the list */
2N/A sessp->se_prev->se_next = NULL;
2N/A }
2N/A }
2N/A
2N/A /* Mark session as no longer valid */
2N/A sessp->se_magic = 0;
2N/A
2N/A free(sessp);
2N/A
2N/A (void) pthread_mutex_unlock(&slotp->sl_mutex);
2N/A
2N/A}
2N/A
2N/A/*
2N/A * pkcs11_sessionlist_delete:
2N/A * Delete all sessions associated with a particular slot's session list.
2N/A *
2N/A * Assumptions: slotp is a valid slot, no mutexes are held, and the
2N/A * sessions were successfully closed with the provider already.
2N/A */
2N/Avoid
2N/Apkcs11_sessionlist_delete(pkcs11_slot_t *slotp)
2N/A{
2N/A
2N/A pkcs11_session_t *sessp, *sess_nextp;
2N/A
2N/A sessp = slotp->sl_sess_list;
2N/A
2N/A /* Delete all the sessions in this slot's session list */
2N/A while (sessp) {
2N/A sess_nextp = sessp->se_next;
2N/A
2N/A pkcs11_session_delete(slotp, sessp);
2N/A
2N/A sessp = sess_nextp;
2N/A }
2N/A
2N/A slotp->sl_sess_list = NULL;
2N/A
2N/A}