xref: /titanic_53/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelSession.h (revision 6d2259e1baf8d4ac11c96570f45ecdcd9771a68d)
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
5ba5f469cSkrishna  * Common Development and Distribution License (the "License").
6ba5f469cSkrishna  * 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 /*
22*6d2259e1SDan OpenSolaris Anderson  * 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 _KERNELSESSION_H
277c478bd9Sstevel@tonic-gate #define	_KERNELSESSION_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 <sys/crypto/common.h>
357c478bd9Sstevel@tonic-gate #include <security/pkcs11t.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	KERNELTOKEN_SESSION_MAGIC	0xECF00003
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate typedef struct crypto_active_op {
417c478bd9Sstevel@tonic-gate 	CK_MECHANISM	mech;
427c478bd9Sstevel@tonic-gate 	void		*context;
437c478bd9Sstevel@tonic-gate 	uint32_t	flags;
447c478bd9Sstevel@tonic-gate } crypto_active_op_t;
457c478bd9Sstevel@tonic-gate 
46ba5f469cSkrishna #define	EDIGEST_LENGTH  1024
47ba5f469cSkrishna 
48ba5f469cSkrishna /* Used for emulating digest and HMAC mechs */
49ba5f469cSkrishna typedef struct digest_buf {
50ba5f469cSkrishna 	uint8_t		*buf;
51ba5f469cSkrishna 	int		buf_len;
52ba5f469cSkrishna 	int		indata_len;
53ba5f469cSkrishna 	void		*soft_sp;
54ba5f469cSkrishna } digest_buf_t;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * Definition for flags in crypto_active_op_t
58ba5f469cSkrishna  *
59ba5f469cSkrishna  * CRYPTO_EMULATE flag is set for a digest or sign/verify with a HMAC
60ba5f469cSkrishna  * mechanism, if the session slot has a CRYPTO_LIMITED_HASH_SUPPORT flag set.
61ba5f469cSkrishna  * CRYPTO_EMULATE_USING_SW flag is meaningful only when CRYPTO_EMULATE flag
62ba5f469cSkrishna  * is set. And CRYPTO_EMULATE_UPDATE_DONE flag is meaningful only when
63ba5f469cSkrishna  * CRYPTO_EMULATE_USING_SW flag is set.
647c478bd9Sstevel@tonic-gate  */
65ba5f469cSkrishna #define	CRYPTO_OPERATION_ACTIVE	0x00000001 /* Cryptoki operation is active */
66ba5f469cSkrishna #define	CRYPTO_OPERATION_UPDATE	0x00000002 /* Cryptoki multi-part op active */
67ba5f469cSkrishna #define	CRYPTO_EMULATE		0x00000004 /* op needs emulation */
68ba5f469cSkrishna #define	CRYPTO_EMULATE_USING_SW	0x00000008 /* ... use software */
69ba5f469cSkrishna #define	CRYPTO_EMULATE_UPDATE_DONE 0x00000010 /* did at least one update */
70b2a96221Skrishna #define	CRYPTO_EMULATE_INIT_DONE 0x00000020 /* did init */
71*6d2259e1SDan OpenSolaris Anderson #define	CRYPTO_OPERATION_INPLACE_OK 0x00000040 /* INPLACE_MECHANISM is true */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate typedef struct session {
747c478bd9Sstevel@tonic-gate 	CK_ULONG	magic_marker;	/* magic # be validated for integrity */
757c478bd9Sstevel@tonic-gate 	pthread_mutex_t	session_mutex;	/* session's mutex lock */
767c478bd9Sstevel@tonic-gate 	pthread_mutex_t ses_free_mutex;	/* mutex used during closing session */
777c478bd9Sstevel@tonic-gate 	pthread_cond_t	ses_free_cond;	/* cond variable for signal and wait */
787c478bd9Sstevel@tonic-gate 	uint32_t	ses_refcnt;	/* session reference count */
797c478bd9Sstevel@tonic-gate 	uint32_t	ses_close_sync;	/* session closing flags */
807c478bd9Sstevel@tonic-gate 	crypto_session_id_t k_session;	/* kernel session ID */
817c478bd9Sstevel@tonic-gate 	boolean_t	ses_RO;		/* RO or RW session flag */
827c478bd9Sstevel@tonic-gate 	CK_SLOT_ID	ses_slotid;	/* slotID saved from C_OpenSession() */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/* Place holder for parameters passed in the C_OpenSession */
857c478bd9Sstevel@tonic-gate 	CK_FLAGS	flags;
867c478bd9Sstevel@tonic-gate 	CK_NOTIFY	Notify;
877c478bd9Sstevel@tonic-gate 	CK_VOID_PTR	pApplication;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/* Pointers to form the global session list */
907c478bd9Sstevel@tonic-gate 	struct session	*next;		/* points to next session on the list */
917c478bd9Sstevel@tonic-gate 	struct session	*prev;		/* points to prev session on the list */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	struct object	*object_list;	/* points to list of objects */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	crypto_active_op_t	digest;	/* context of active digest operation */
967c478bd9Sstevel@tonic-gate 	crypto_active_op_t	encrypt; /* context of active encrypt op */
977c478bd9Sstevel@tonic-gate 	crypto_active_op_t	decrypt; /* context of active decrypt op */
987c478bd9Sstevel@tonic-gate 	crypto_active_op_t	sign;	/* context of active sign op */
997c478bd9Sstevel@tonic-gate 	crypto_active_op_t	verify;	/* context of active verify op */
1007c478bd9Sstevel@tonic-gate 	crypto_active_op_t	find_objects;
1017c478bd9Sstevel@tonic-gate } kernel_session_t;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
10401223cbaSmcpowers  * The following structure is used to link the to-be-freed sessions
10501223cbaSmcpowers  * into a linked list. The sessions on this linked list have
10601223cbaSmcpowers  * not yet been freed via free() after C_CloseSession() call; instead
10701223cbaSmcpowers  * they are added to this list. The actual free will take place when
10801223cbaSmcpowers  * the number of sessions queued reaches MAX_SES_TO_BE_FREED, at which
10901223cbaSmcpowers  * time the first session in the list will be freed.
11001223cbaSmcpowers  */
11101223cbaSmcpowers #define	MAX_SES_TO_BE_FREED		300
11201223cbaSmcpowers 
11301223cbaSmcpowers typedef struct ses_to_be_freed_list {
11401223cbaSmcpowers 	kernel_session_t *first; /* points to the first session in the list */
11501223cbaSmcpowers 	kernel_session_t *last;  /* points to the last session in the list */
11601223cbaSmcpowers 	uint32_t	count;   /* current total sessions in the list */
11701223cbaSmcpowers 	pthread_mutex_t ses_to_be_free_mutex;
11801223cbaSmcpowers } ses_to_be_freed_list_t;
11901223cbaSmcpowers 
12001223cbaSmcpowers extern ses_to_be_freed_list_t ses_delay_freed;
12101223cbaSmcpowers 
12201223cbaSmcpowers /*
1237c478bd9Sstevel@tonic-gate  * Flag definitions for ses_close_sync
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate #define	SESSION_IS_CLOSING	1	/* Session is in a closing state */
1267c478bd9Sstevel@tonic-gate #define	SESSION_REFCNT_WAITING	2	/* Waiting for session reference */
1277c478bd9Sstevel@tonic-gate 					/* count to become zero */
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * This macro is used to decrement the session reference count by one.
1307c478bd9Sstevel@tonic-gate  *
1317c478bd9Sstevel@tonic-gate  * The caller of this macro uses the argument lock_held to indicate that
1327c478bd9Sstevel@tonic-gate  * whether the caller holds the lock on the session or not.
1337c478bd9Sstevel@tonic-gate  *
1347c478bd9Sstevel@tonic-gate  * REFRELE macro does the following:
1357c478bd9Sstevel@tonic-gate  * 1) Get the session lock if the caller does not hold it.
1367c478bd9Sstevel@tonic-gate  * 2) Decrement the session reference count by one.
1377c478bd9Sstevel@tonic-gate  * 3) If the session reference count becomes zero after being decremented,
1387c478bd9Sstevel@tonic-gate  *    and there is a closing session thread in the wait state, then
1397c478bd9Sstevel@tonic-gate  *    call pthread_cond_signal() to wake up that thread who is blocked
1407c478bd9Sstevel@tonic-gate  *    in the session deletion routine due to non-zero reference ount.
1417c478bd9Sstevel@tonic-gate  * 4) Always release the session lock.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate #define	REFRELE(s, ses_lock_held) { \
1447c478bd9Sstevel@tonic-gate 	if (!ses_lock_held) \
1457c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&s->session_mutex);   \
1467c478bd9Sstevel@tonic-gate 	if ((--((s)->ses_refcnt) == 0) &&    \
1477c478bd9Sstevel@tonic-gate 	    (s->ses_close_sync & SESSION_REFCNT_WAITING)) {     \
1487c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&s->session_mutex);   \
1497c478bd9Sstevel@tonic-gate 		(void) pthread_cond_signal(&s->ses_free_cond); \
1507c478bd9Sstevel@tonic-gate 	} else {        \
1517c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&s->session_mutex);   \
1527c478bd9Sstevel@tonic-gate 	}       \
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * Function Prototypes.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate CK_RV handle2session(CK_SESSION_HANDLE hSession, kernel_session_t **session_p);
1607c478bd9Sstevel@tonic-gate 
16101223cbaSmcpowers void kernel_delete_all_sessions(CK_SLOT_ID slotID, boolean_t wrapper_only);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate void kernel_delete_all_objects_in_session(kernel_session_t *sp,
1647c478bd9Sstevel@tonic-gate     boolean_t wrapper_only);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate CK_RV kernel_add_session(CK_SLOT_ID slotID, CK_FLAGS flags,
1677c478bd9Sstevel@tonic-gate     CK_VOID_PTR pApplication, CK_NOTIFY notify, CK_ULONG *phSession);
1687c478bd9Sstevel@tonic-gate 
16901223cbaSmcpowers void kernel_delete_session(CK_SLOT_ID slotID, kernel_session_t *sp,
1707c478bd9Sstevel@tonic-gate     boolean_t lock_held, boolean_t wrapper_only);
1717c478bd9Sstevel@tonic-gate 
17201223cbaSmcpowers void kernel_session_delay_free(kernel_session_t *sp);
17301223cbaSmcpowers 
1744daf2311Srupertk void kernel_acquire_all_slots_mutexes();
1754daf2311Srupertk void kernel_release_all_slots_mutexes();
1764daf2311Srupertk 
1777c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate #endif
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #endif /* _KERNELSESSION_H */
182