17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 590e0e8c4Sizick * Common Development and Distribution License (the "License"). 690e0e8c4Sizick * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 221f49a79aSZdenek Kotala * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #ifndef _SOFTSESSION_H 277c478bd9Sstevel@tonic-gate #define _SOFTSESSION_H 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #ifdef __cplusplus 307c478bd9Sstevel@tonic-gate extern "C" { 317c478bd9Sstevel@tonic-gate #endif 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <pthread.h> 347c478bd9Sstevel@tonic-gate #include <security/pkcs11t.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #define SOFTTOKEN_SESSION_MAGIC 0xECF00002 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * This is only used by the C_G(S)etOperationState. 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate #define DIGEST_OP 1 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * This is only used by the C_G(S)etOperationState. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate typedef struct internal_op_state { 487c478bd9Sstevel@tonic-gate /* Holds the length of the saved state */ 497c478bd9Sstevel@tonic-gate CK_ULONG op_len; 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* crypto operation to be saved or restored */ 527c478bd9Sstevel@tonic-gate CK_ULONG op_active; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* Holds the saved session state */ 557c478bd9Sstevel@tonic-gate CK_STATE op_session_state; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate } internal_op_state_t; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate typedef struct crypto_active_op { 607c478bd9Sstevel@tonic-gate CK_MECHANISM mech; 617c478bd9Sstevel@tonic-gate void *context; 627c478bd9Sstevel@tonic-gate uint32_t flags; 637c478bd9Sstevel@tonic-gate } crypto_active_op_t; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate * Definition for flags in crypto_active_op_t 687c478bd9Sstevel@tonic-gate */ 697c478bd9Sstevel@tonic-gate #define CRYPTO_OPERATION_ACTIVE 1 /* Cryptoki operation is active */ 707c478bd9Sstevel@tonic-gate #define CRYPTO_OPERATION_UPDATE 2 /* Cryptoki multi-part op active */ 717c478bd9Sstevel@tonic-gate #define CRYPTO_KEY_DIGESTED 3 /* A C_DigestKey() was called */ 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate typedef struct session { 747c478bd9Sstevel@tonic-gate ulong_t magic_marker; /* magic # be validated for integrity */ 757c478bd9Sstevel@tonic-gate pthread_mutex_t session_mutex; /* session's mutex lock */ 767c478bd9Sstevel@tonic-gate pthread_cond_t ses_free_cond; /* cond variable for signal and wait */ 777c478bd9Sstevel@tonic-gate uint32_t ses_refcnt; /* session reference count */ 787c478bd9Sstevel@tonic-gate uint32_t ses_close_sync; /* session closing flags */ 797c478bd9Sstevel@tonic-gate CK_STATE state; /* session state */ 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* Place holder for parameters passed in the C_OpenSession */ 827c478bd9Sstevel@tonic-gate CK_FLAGS flags; 837c478bd9Sstevel@tonic-gate CK_NOTIFY Notify; 847c478bd9Sstevel@tonic-gate CK_VOID_PTR pApplication; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* Pointers to form the global session list */ 877c478bd9Sstevel@tonic-gate struct session *next; /* points to next session on the list */ 887c478bd9Sstevel@tonic-gate struct session *prev; /* points to prev session on the list */ 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate struct object *object_list; /* points to list of objects */ 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate crypto_active_op_t digest; /* context of active digest operation */ 937c478bd9Sstevel@tonic-gate crypto_active_op_t encrypt; /* context of active encrypt op */ 947c478bd9Sstevel@tonic-gate crypto_active_op_t decrypt; /* context of active decrypt op */ 957c478bd9Sstevel@tonic-gate crypto_active_op_t sign; /* context of active sign op */ 967c478bd9Sstevel@tonic-gate crypto_active_op_t verify; /* context of active verify op */ 977c478bd9Sstevel@tonic-gate /* context of active FindObjects op */ 987c478bd9Sstevel@tonic-gate crypto_active_op_t find_objects; 997c478bd9Sstevel@tonic-gate } soft_session_t; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * slot_t is a global structure to be used only by the 1037c478bd9Sstevel@tonic-gate * token objects to hold the token object related 1047c478bd9Sstevel@tonic-gate * in-core information. 1057c478bd9Sstevel@tonic-gate */ 1067c478bd9Sstevel@tonic-gate typedef struct slot { 1077c478bd9Sstevel@tonic-gate uint_t ks_version; /* in-core keystore version number */ 1087c478bd9Sstevel@tonic-gate boolean_t authenticated; /* Has C_Login called */ 1097c478bd9Sstevel@tonic-gate boolean_t userpin_change_needed; /* set if PIN expired */ 1107c478bd9Sstevel@tonic-gate pthread_mutex_t slot_mutex; 11190e0e8c4Sizick pthread_mutex_t keystore_mutex; /* Protects keystore_load_status */ 11290e0e8c4Sizick uint_t keystore_load_status; /* Keystore load status */ 1137c478bd9Sstevel@tonic-gate /* points to in-core token object list */ 1147c478bd9Sstevel@tonic-gate struct object *token_object_list; 1157c478bd9Sstevel@tonic-gate } slot_t; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * The following structure is used to link the to-be-freed sessions 1197c478bd9Sstevel@tonic-gate * into a linked list. The sessions on this linked list have 1207c478bd9Sstevel@tonic-gate * not yet been freed via free() after C_CloseSession() call; instead 1217c478bd9Sstevel@tonic-gate * they are added to this list. The actual free will take place when 1227c478bd9Sstevel@tonic-gate * the number of sessions queued reaches MAX_SES_TO_BE_FREED, at which 1237c478bd9Sstevel@tonic-gate * time the first session in the list will be freed. 1247c478bd9Sstevel@tonic-gate */ 1257c478bd9Sstevel@tonic-gate #define MAX_SES_TO_BE_FREED 300 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate typedef struct ses_to_be_freed_list { 1287c478bd9Sstevel@tonic-gate struct session *first; /* points to the first session in the list */ 1297c478bd9Sstevel@tonic-gate struct session *last; /* points to the last session in the list */ 1307c478bd9Sstevel@tonic-gate uint32_t count; /* current total sessions in the list */ 1317c478bd9Sstevel@tonic-gate pthread_mutex_t ses_to_be_free_mutex; 1327c478bd9Sstevel@tonic-gate } ses_to_be_freed_list_t; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate /* 1357c478bd9Sstevel@tonic-gate * Flag definitions for ses_close_sync 1367c478bd9Sstevel@tonic-gate */ 1377c478bd9Sstevel@tonic-gate #define SESSION_IS_CLOSING 1 /* Session is in a closing state */ 1387c478bd9Sstevel@tonic-gate #define SESSION_REFCNT_WAITING 2 /* Waiting for session reference */ 1397c478bd9Sstevel@tonic-gate /* count to become zero */ 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * This macro is used to decrement the session reference count by one. 1437c478bd9Sstevel@tonic-gate * 1447c478bd9Sstevel@tonic-gate * The caller of this macro uses the argument lock_held to indicate that 1457c478bd9Sstevel@tonic-gate * whether the caller holds the lock on the session or not. 1467c478bd9Sstevel@tonic-gate * 1477c478bd9Sstevel@tonic-gate * SES_REFRELE macro does the following: 1487c478bd9Sstevel@tonic-gate * 1) Get the session lock if the caller does not hold it. 1497c478bd9Sstevel@tonic-gate * 2) Decrement the session reference count by one. 1507c478bd9Sstevel@tonic-gate * 3) If the session reference count becomes zero after being decremented, 1517c478bd9Sstevel@tonic-gate * and there is a closing session thread in the wait state, then 1527c478bd9Sstevel@tonic-gate * call pthread_cond_signal() to wake up that thread who is blocked 1537c478bd9Sstevel@tonic-gate * in the session deletion routine due to non-zero reference ount. 1547c478bd9Sstevel@tonic-gate * 4) Always release the session lock. 1557c478bd9Sstevel@tonic-gate */ 1567c478bd9Sstevel@tonic-gate #define SES_REFRELE(s, lock_held) { \ 1577c478bd9Sstevel@tonic-gate if (!lock_held) \ 1587c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&s->session_mutex); \ 1597c478bd9Sstevel@tonic-gate if ((--((s)->ses_refcnt) == 0) && \ 1607c478bd9Sstevel@tonic-gate (s->ses_close_sync & SESSION_REFCNT_WAITING)) { \ 1617c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&s->session_mutex); \ 1627c478bd9Sstevel@tonic-gate (void) pthread_cond_signal(&s->ses_free_cond); \ 1637c478bd9Sstevel@tonic-gate } else { \ 1647c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&s->session_mutex); \ 1657c478bd9Sstevel@tonic-gate } \ 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate extern pthread_mutex_t soft_sessionlist_mutex; 1707c478bd9Sstevel@tonic-gate extern soft_session_t *soft_session_list; 1717c478bd9Sstevel@tonic-gate extern int all_sessions_closing; 1727c478bd9Sstevel@tonic-gate extern CK_ULONG soft_session_cnt; /* the number of opened sessions */ 1737c478bd9Sstevel@tonic-gate extern CK_ULONG soft_session_rw_cnt; /* the number of opened R/W sessions */ 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * Function Prototypes. 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate CK_RV handle2session(CK_SESSION_HANDLE hSession, soft_session_t **session_p); 1807c478bd9Sstevel@tonic-gate 181a62b4373Sdarrenm CK_RV soft_delete_all_sessions(boolean_t force); 1827c478bd9Sstevel@tonic-gate 1831f49a79aSZdenek Kotala void soft_delete_all_objects_in_session(soft_session_t *sp, boolean_t force); 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate CK_RV soft_add_session(CK_FLAGS flags, CK_VOID_PTR pApplication, 1867c478bd9Sstevel@tonic-gate CK_NOTIFY notify, CK_ULONG *phSession); 1877c478bd9Sstevel@tonic-gate 188a62b4373Sdarrenm CK_RV soft_delete_session(soft_session_t *sp, 189a62b4373Sdarrenm boolean_t force, boolean_t lock_held); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate CK_RV soft_get_operationstate(soft_session_t *, CK_BYTE_PTR, CK_ULONG_PTR); 1927c478bd9Sstevel@tonic-gate CK_RV soft_set_operationstate(soft_session_t *, CK_BYTE_PTR, CK_ULONG, 1937c478bd9Sstevel@tonic-gate CK_OBJECT_HANDLE, CK_OBJECT_HANDLE); 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* Token object related function prototypes. */ 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate CK_RV soft_login(CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate void soft_logout(void); 2017c478bd9Sstevel@tonic-gate 202*83140133SZdenek Kotala void soft_acquire_all_session_mutexes(soft_session_t *session_p); 203*83140133SZdenek Kotala void soft_release_all_session_mutexes(soft_session_t *session_p); 2044daf2311Srupertk 2057c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate #endif 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate #endif /* _SOFTSESSION_H */ 210