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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ckdbm.h"
static void
nss_dbm_mdSession_Close(
NSSCKMDSession *mdSession,
NSSCKFWSession *fwSession,
NSSCKMDToken *mdToken,
NSSCKFWToken *fwToken,
NSSCKMDInstance *mdInstance,
NSSCKFWInstance *fwInstance)
{
nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
struct nss_dbm_dbt_node *w;
/* Lock */
{
if (CKR_OK != NSSCKFWMutex_Lock(session->list_lock)) {
return;
}
w = session->session_objects;
session->session_objects = (struct nss_dbm_dbt_node *)NULL; /* sanity */
(void)NSSCKFWMutex_Unlock(session->list_lock);
}
for (; (struct nss_dbm_dbt_node *)NULL != w; w = w->next) {
(void)nss_dbm_db_delete_object(w->dbt);
}
}
static CK_ULONG
nss_dbm_mdSession_GetDeviceError(
NSSCKMDSession *mdSession,
NSSCKFWSession *fwSession,
NSSCKMDToken *mdToken,
NSSCKFWToken *fwToken,
NSSCKMDInstance *mdInstance,
NSSCKFWInstance *fwInstance)
{
nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
return session->deviceError;
}
/* Login isn't needed */
/* Logout isn't needed */
/* InitPIN is irrelevant */
/* SetPIN is irrelevant */
/* GetOperationStateLen is irrelevant */
/* GetOperationState is irrelevant */
/* SetOperationState is irrelevant */
static NSSCKMDObject *
nss_dbm_mdSession_CreateObject(
NSSCKMDSession *mdSession,
NSSCKFWSession *fwSession,
NSSCKMDToken *mdToken,
NSSCKFWToken *fwToken,
NSSCKMDInstance *mdInstance,
NSSCKFWInstance *fwInstance,
NSSArena *handyArenaPointer,
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount,
CK_RV *pError)
{
nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
CK_ULONG i;
CK_BBOOL isToken = CK_FALSE; /* defaults to false */
NSSCKMDObject *rv;
struct nss_dbm_dbt_node *node = (struct nss_dbm_dbt_node *)NULL;
nss_dbm_object_t *object;
nss_dbm_db_t *which_db;
/* This framework should really pass this to me */
for (i = 0; i < ulAttributeCount; i++) {
if (CKA_TOKEN == pTemplate[i].type) {
isToken = *(CK_BBOOL *)pTemplate[i].pValue;
break;
}
}
object = nss_ZNEW(handyArenaPointer, nss_dbm_object_t);
if ((nss_dbm_object_t *)NULL == object) {
*pError = CKR_HOST_MEMORY;
return (NSSCKMDObject *)NULL;
}
object->arena = handyArenaPointer;
which_db = isToken ? token->slot->token_db : token->session_db;
/* Do this before the actual database call; it's easier to recover from */
rv = nss_dbm_mdObject_factory(object, pError);
if ((NSSCKMDObject *)NULL == rv) {
return (NSSCKMDObject *)NULL;
}
if (CK_FALSE == isToken) {
node = nss_ZNEW(session->arena, struct nss_dbm_dbt_node);
if ((struct nss_dbm_dbt_node *)NULL == node) {
*pError = CKR_HOST_MEMORY;
return (NSSCKMDObject *)NULL;
}
}
object->handle = nss_dbm_db_create_object(handyArenaPointer, which_db,
pTemplate, ulAttributeCount,
pError, &session->deviceError);
if ((nss_dbm_dbt_t *)NULL == object->handle) {
return (NSSCKMDObject *)NULL;
}
if (CK_FALSE == isToken) {
node->dbt = object->handle;
/* Lock */
{
*pError = NSSCKFWMutex_Lock(session->list_lock);
if (CKR_OK != *pError) {
(void)nss_dbm_db_delete_object(object->handle);
return (NSSCKMDObject *)NULL;
}
node->next = session->session_objects;
session->session_objects = node;
*pError = NSSCKFWMutex_Unlock(session->list_lock);
}
}
return rv;
}
/* CopyObject isn't needed; the framework will use CreateObject */
static NSSCKMDFindObjects *
nss_dbm_mdSession_FindObjectsInit(
NSSCKMDSession *mdSession,
NSSCKFWSession *fwSession,
NSSCKMDToken *mdToken,
NSSCKFWToken *fwToken,
NSSCKMDInstance *mdInstance,
NSSCKFWInstance *fwInstance,
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount,
CK_RV *pError)
{
nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
NSSArena *arena;
nss_dbm_find_t *find;
NSSCKMDFindObjects *rv;
arena = NSSArena_Create();
if ((NSSArena *)NULL == arena) {
*pError = CKR_HOST_MEMORY;
goto loser;
}
find = nss_ZNEW(arena, nss_dbm_find_t);
if ((nss_dbm_find_t *)NULL == find) {
*pError = CKR_HOST_MEMORY;
goto loser;
}
find->arena = arena;
find->list_lock = NSSCKFWInstance_CreateMutex(fwInstance, arena, pError);
if ((NSSCKFWMutex *)NULL == find->list_lock) {
goto loser;
}
*pError = nss_dbm_db_find_objects(find, token->slot->token_db, pTemplate,
ulAttributeCount, &session->deviceError);
if (CKR_OK != *pError) {
goto loser;
}
*pError = nss_dbm_db_find_objects(find, token->session_db, pTemplate,
ulAttributeCount, &session->deviceError);
if (CKR_OK != *pError) {
goto loser;
}
rv = nss_dbm_mdFindObjects_factory(find, pError);
if ((NSSCKMDFindObjects *)NULL == rv) {
goto loser;
}
return rv;
loser:
if ((NSSArena *)NULL != arena) {
(void)NSSArena_Destroy(arena);
}
return (NSSCKMDFindObjects *)NULL;
}
/* SeedRandom is irrelevant */
/* GetRandom is irrelevant */
NSS_IMPLEMENT NSSCKMDSession *
nss_dbm_mdSession_factory(
nss_dbm_token_t *token,
NSSCKFWSession *fwSession,
NSSCKFWInstance *fwInstance,
CK_BBOOL rw,
CK_RV *pError)
{
NSSArena *arena;
nss_dbm_session_t *session;
NSSCKMDSession *rv;
arena = NSSCKFWSession_GetArena(fwSession, pError);
session = nss_ZNEW(arena, nss_dbm_session_t);
if ((nss_dbm_session_t *)NULL == session) {
*pError = CKR_HOST_MEMORY;
return (NSSCKMDSession *)NULL;
}
rv = nss_ZNEW(arena, NSSCKMDSession);
if ((NSSCKMDSession *)NULL == rv) {
*pError = CKR_HOST_MEMORY;
return (NSSCKMDSession *)NULL;
}
session->arena = arena;
session->token = token;
session->list_lock = NSSCKFWInstance_CreateMutex(fwInstance, arena, pError);
if ((NSSCKFWMutex *)NULL == session->list_lock) {
return (NSSCKMDSession *)NULL;
}
rv->etc = (void *)session;
rv->Close = nss_dbm_mdSession_Close;
rv->GetDeviceError = nss_dbm_mdSession_GetDeviceError;
/* Login isn't needed */
/* Logout isn't needed */
/* InitPIN is irrelevant */
/* SetPIN is irrelevant */
/* GetOperationStateLen is irrelevant */
/* GetOperationState is irrelevant */
/* SetOperationState is irrelevant */
rv->CreateObject = nss_dbm_mdSession_CreateObject;
/* CopyObject isn't needed; the framework will use CreateObject */
rv->FindObjectsInit = nss_dbm_mdSession_FindObjectsInit;
rv->null = NULL;
return rv;
}
|