summaryrefslogtreecommitdiff
path: root/p11-capi-session.h
blob: 8f8402634a99b663cc1697dab2a4507b9c2b0e36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* 
 * Copyright (C) 2007 Stef Walter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef P11C_SESSION_H
#define P11C_SESSION_H

#include "p11-capi.h"

/* --------------------------------------------------------------------
 * 
 * Session = P11cSession
 * - A PKCS#11 Session 
 * 
 * Objects = P11cObject
 * - There's a global list of objects in p11c-object.c indexed by 
 *   object handle. 
 * - The object itself has no attributes or cached data, but knows how 
 *   to load data when needed. 
 * - Each object has a unique key which guarantees we don't load the 
 *   same object twice with two different object handles.
 * 
 * Object Data = P11cObjectData
 * - Object Data is owned by the Session
 * - Loaded data and/or attributes for an object. 
 */

/* Callback to cleanup a current operation */
typedef void (*P11cSessionCancel) (struct _P11cSession* sess);

/* Represents an open session */
typedef struct _P11cSession 
{
	CK_SESSION_HANDLE id;                  /* Unique ID for this session */
	CK_SLOT_ID slot;
	int in_call;                           /* Whether this session is use in PKCS#11 function */

	HCERTSTORE store;                      /* Handle to an open certificate store */

	BOOL read_write;                       /* A read-write session? */

	int operation_type;                    /* Whether an operation is happening or not */
	void* operation_data;                  /* Data for this operation */
	P11cSessionCancel operation_cancel;    /* Callback to cancel operation when necessary */

	P11cHash* object_data;

	CK_NOTIFY notify_callback;             /* Application specified callback */
	CK_VOID_PTR user_data;                 /* Argument for above */

	int refs;                              /* Reference count */
	HANDLE mutex;                          /* Mutex for protecting this structure */
} 
P11cSession;

/* Debug print something related to a session */
#define DBGS(sess, msg) \
	p11c_debug("S%d: %s", (sess) ? (sess)->id : 0, (msg))

/* Create a session */
CK_RV           p11c_session_create              (CK_SLOT_ID slot, P11cSession** ret);

/* Destroy a session */
void            p11c_session_destroy             (P11cSession* sess);

/* Register a new session */
CK_RV           p11c_session_register            (P11cSession* sess);

/* Get information about a session */
void            p11c_session_get_info            (P11cSession* sess, 
                                                  CK_SESSION_INFO_PTR info);

/* Get a session from a handle, and lock it */
CK_RV           p11c_session_get_lock_ref        (CK_ULONG id, BOOL writable, 
                                                  P11cSession **sess);

/* Get a session from a handle, remove it from list, and lock it */
CK_RV           p11c_session_remove_lock_ref     (CK_ULONG id, P11cSession **sess);

/* Unlock and unreference a session */
void            p11c_session_unref_unlock        (P11cSession* sess);

/* Close all sessions on a certain slot/token */
CK_RV           p11c_session_close_all           (CK_SLOT_ID slot);



/* Start a find operation on a session */
CK_RV           p11c_session_find_init           (P11cSession* sess, 
                                                  CK_ATTRIBUTE_PTR templ, 
                                                  CK_ULONG count);

/* Return results from a find operation */
CK_RV           p11c_session_find                (P11cSession* sess, 
                                                  CK_OBJECT_HANDLE_PTR objects, 
                                                  CK_ULONG max_object_count, 
                                                  CK_ULONG_PTR object_count);

/* End a find operation */
CK_RV           p11c_session_find_final          (P11cSession* sess);


/* Start a sign operation on a session */
CK_RV           p11c_session_sign_init           (P11cSession* sess, 
                                                  CK_MECHANISM_PTR mech, 
                                                  P11cObjectData *objdata);

/* Perform sign operation */
CK_RV           p11c_session_sign                (P11cSession* sess, 
                                                  CK_BYTE_PTR data, CK_ULONG n_data,
                                                  CK_BYTE_PTR sig, CK_ULONG_PTR n_sig);

/* Start a decrypt operation on a session */
CK_RV           p11c_session_decrypt_init        (P11cSession* sess, 
                                                  CK_MECHANISM_PTR mech, 
                                                  P11cObjectData *objdata);

/* Perform decrypt operation */
CK_RV           p11c_session_decrypt             (P11cSession* sess, 
                                                  CK_BYTE_PTR encdata, CK_ULONG n_encdata,
                                                  CK_BYTE_PTR result, CK_ULONG_PTR n_result);

/* Get object data for an object */
CK_RV           p11c_session_get_object_data     (P11cSession* sess, 
                                                  P11cObject* obj, 
                                                  P11cObjectData** objdata);

/* Get object data for an object handle */
CK_RV           p11c_session_get_object_data_for (P11cSession* sess, 
                                                  CK_OBJECT_HANDLE hand, 
                                                  P11cObjectData** objdata);

/* Set object data for an object */
void            p11c_session_take_object_data    (P11cSession* sess, 
                                                  P11cObject* obj, 
                                                  P11cObjectData* objdata);

/* Clear object data for an object */
void            p11c_session_clear_object_data   (P11cSession* sess, 
                                                  P11cObject* obj);

/* Enumerate object data for all objects */
typedef void    (*P11cEnumObjectData)            (P11cSession* sess, 
                                                  P11cObject* obj, 
                                                  P11cObjectData* data, 
                                                  void* arg);

void            p11c_session_enum_object_data    (P11cSession* sess, 
                                                  P11cEnumObjectData enum_func, 
                                                  void* arg);

void            p11c_session_cleanup_all         (void);

#endif /* P11C_SESSION_H */