PageRenderTime 37ms CodeModel.GetById 14ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/security/nss/lib/ckfw/dbm/session.c

http://github.com/zpao/v8monkey
C | 301 lines | 204 code | 40 blank | 57 comment | 41 complexity | d83ec20cdef400d445af60cbe36290e5 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is the Netscape security libraries.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Netscape Communications Corporation.
 18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *
 23 * Alternatively, the contents of this file may be used under the terms of
 24 * either the GNU General Public License Version 2 or later (the "GPL"), or
 25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26 * in which case the provisions of the GPL or the LGPL are applicable instead
 27 * of those above. If you wish to allow use of your version of this file only
 28 * under the terms of either the GPL or the LGPL, and not to allow others to
 29 * use your version of this file under the terms of the MPL, indicate your
 30 * decision by deleting the provisions above and replace them with the notice
 31 * and other provisions required by the GPL or the LGPL. If you do not delete
 32 * the provisions above, a recipient may use your version of this file under
 33 * the terms of any one of the MPL, the GPL or the LGPL.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 37#ifdef DEBUG
 38static const char CVS_ID[] = "@(#) $RCSfile: session.c,v $ $Revision: 1.3 $ $Date: 2006/03/02 22:48:54 $";
 39#endif /* DEBUG */
 40
 41#include "ckdbm.h"
 42
 43static void
 44nss_dbm_mdSession_Close
 45(
 46  NSSCKMDSession *mdSession,
 47  NSSCKFWSession *fwSession,
 48  NSSCKMDToken *mdToken,
 49  NSSCKFWToken *fwToken,
 50  NSSCKMDInstance *mdInstance,
 51  NSSCKFWInstance *fwInstance
 52)
 53{
 54  nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
 55
 56  struct nss_dbm_dbt_node *w;
 57
 58  /* Lock */
 59  {
 60    if( CKR_OK != NSSCKFWMutex_Lock(session->list_lock) ) {
 61      return;
 62    }
 63
 64    w = session->session_objects;
 65    session->session_objects = (struct nss_dbm_dbt_node *)NULL; /* sanity */
 66    
 67    (void)NSSCKFWMutex_Unlock(session->list_lock);
 68  }
 69
 70  for( ; (struct nss_dbm_dbt_node *)NULL != w; w = w->next ) {
 71    (void)nss_dbm_db_delete_object(w->dbt);
 72  }
 73}
 74
 75static CK_ULONG
 76nss_dbm_mdSession_GetDeviceError
 77(
 78  NSSCKMDSession *mdSession,
 79  NSSCKFWSession *fwSession,
 80  NSSCKMDToken *mdToken,
 81  NSSCKFWToken *fwToken,
 82  NSSCKMDInstance *mdInstance,
 83  NSSCKFWInstance *fwInstance
 84)
 85{
 86  nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
 87  return session->deviceError;
 88}
 89
 90/* Login isn't needed */
 91/* Logout isn't needed */
 92/* InitPIN is irrelevant */
 93/* SetPIN is irrelevant */
 94/* GetOperationStateLen is irrelevant */
 95/* GetOperationState is irrelevant */
 96/* SetOperationState is irrelevant */
 97
 98static NSSCKMDObject *
 99nss_dbm_mdSession_CreateObject
100(
101  NSSCKMDSession *mdSession,
102  NSSCKFWSession *fwSession,
103  NSSCKMDToken *mdToken,
104  NSSCKFWToken *fwToken,
105  NSSCKMDInstance *mdInstance,
106  NSSCKFWInstance *fwInstance,
107  NSSArena *handyArenaPointer,
108  CK_ATTRIBUTE_PTR pTemplate,
109  CK_ULONG ulAttributeCount,
110  CK_RV *pError
111)
112{
113  nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
114  nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
115  CK_ULONG i;
116  CK_BBOOL isToken = CK_FALSE; /* defaults to false */
117  NSSCKMDObject *rv;
118  struct nss_dbm_dbt_node *node = (struct nss_dbm_dbt_node *)NULL;
119  nss_dbm_object_t *object;
120  nss_dbm_db_t *which_db;
121
122  /* This framework should really pass this to me */
123  for( i = 0; i < ulAttributeCount; i++ ) {
124    if( CKA_TOKEN == pTemplate[i].type ) {
125      isToken = *(CK_BBOOL *)pTemplate[i].pValue;
126      break;
127    }
128  }
129
130  object = nss_ZNEW(handyArenaPointer, nss_dbm_object_t);
131  if( (nss_dbm_object_t *)NULL == object ) {
132    *pError = CKR_HOST_MEMORY;
133    return (NSSCKMDObject *)NULL;
134  }
135
136  object->arena = handyArenaPointer;
137  which_db = isToken ? token->slot->token_db : token->session_db;
138
139  /* Do this before the actual database call; it's easier to recover from */
140  rv = nss_dbm_mdObject_factory(object, pError);
141  if( (NSSCKMDObject *)NULL == rv ) {
142    return (NSSCKMDObject *)NULL;
143  }
144
145  if( CK_FALSE == isToken ) {
146    node = nss_ZNEW(session->arena, struct nss_dbm_dbt_node);
147    if( (struct nss_dbm_dbt_node *)NULL == node ) {
148      *pError = CKR_HOST_MEMORY;
149      return (NSSCKMDObject *)NULL;
150    }
151  }
152
153  object->handle = nss_dbm_db_create_object(handyArenaPointer, which_db, 
154                                            pTemplate, ulAttributeCount,
155                                            pError, &session->deviceError);
156  if( (nss_dbm_dbt_t *)NULL == object->handle ) {
157    return (NSSCKMDObject *)NULL;
158  }
159
160  if( CK_FALSE == isToken ) {
161    node->dbt = object->handle;
162    /* Lock */
163    {
164      *pError = NSSCKFWMutex_Lock(session->list_lock);
165      if( CKR_OK != *pError ) {
166        (void)nss_dbm_db_delete_object(object->handle);
167        return (NSSCKMDObject *)NULL;
168      }
169      
170      node->next = session->session_objects;
171      session->session_objects = node;
172      
173      *pError = NSSCKFWMutex_Unlock(session->list_lock);
174    }
175  }
176
177  return rv;
178}
179
180/* CopyObject isn't needed; the framework will use CreateObject */
181
182static NSSCKMDFindObjects *
183nss_dbm_mdSession_FindObjectsInit
184(
185  NSSCKMDSession *mdSession,
186  NSSCKFWSession *fwSession,
187  NSSCKMDToken *mdToken,
188  NSSCKFWToken *fwToken,
189  NSSCKMDInstance *mdInstance,
190  NSSCKFWInstance *fwInstance,
191  CK_ATTRIBUTE_PTR pTemplate,
192  CK_ULONG ulAttributeCount,
193  CK_RV *pError
194)
195{
196  nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
197  nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
198  NSSArena *arena;
199  nss_dbm_find_t *find;
200  NSSCKMDFindObjects *rv;
201
202  arena = NSSArena_Create();
203  if( (NSSArena *)NULL == arena ) {
204    *pError = CKR_HOST_MEMORY;
205    goto loser;
206  }
207
208  find = nss_ZNEW(arena, nss_dbm_find_t);
209  if( (nss_dbm_find_t *)NULL == find ) {
210    *pError = CKR_HOST_MEMORY;
211    goto loser;
212  }
213
214  find->arena = arena;
215  find->list_lock = NSSCKFWInstance_CreateMutex(fwInstance, arena, pError);
216  if( (NSSCKFWMutex *)NULL == find->list_lock ) {
217    goto loser;
218  }
219
220  *pError = nss_dbm_db_find_objects(find, token->slot->token_db, pTemplate, 
221                                    ulAttributeCount, &session->deviceError);
222  if( CKR_OK != *pError ) {
223    goto loser;
224  }
225
226  *pError = nss_dbm_db_find_objects(find, token->session_db, pTemplate, 
227                                    ulAttributeCount, &session->deviceError);
228  if( CKR_OK != *pError ) {
229    goto loser;
230  }
231
232  rv = nss_dbm_mdFindObjects_factory(find, pError);
233  if( (NSSCKMDFindObjects *)NULL == rv ) {
234    goto loser;
235  }
236
237  return rv;
238
239 loser:
240  if( (NSSArena *)NULL != arena ) {
241    (void)NSSArena_Destroy(arena);
242  }
243
244  return (NSSCKMDFindObjects *)NULL;
245}
246
247/* SeedRandom is irrelevant */
248/* GetRandom is irrelevant */
249
250NSS_IMPLEMENT NSSCKMDSession *
251nss_dbm_mdSession_factory
252(
253  nss_dbm_token_t *token,
254  NSSCKFWSession *fwSession,
255  NSSCKFWInstance *fwInstance,
256  CK_BBOOL rw,
257  CK_RV *pError
258)
259{
260  NSSArena *arena;
261  nss_dbm_session_t *session;
262  NSSCKMDSession *rv;
263
264  arena = NSSCKFWSession_GetArena(fwSession, pError);
265
266  session = nss_ZNEW(arena, nss_dbm_session_t);
267  if( (nss_dbm_session_t *)NULL == session ) {
268    *pError = CKR_HOST_MEMORY;
269    return (NSSCKMDSession *)NULL;
270  }
271
272  rv = nss_ZNEW(arena, NSSCKMDSession);
273  if( (NSSCKMDSession *)NULL == rv ) {
274    *pError = CKR_HOST_MEMORY;
275    return (NSSCKMDSession *)NULL;
276  }
277
278  session->arena = arena;
279  session->token = token;
280  session->list_lock = NSSCKFWInstance_CreateMutex(fwInstance, arena, pError);
281  if( (NSSCKFWMutex *)NULL == session->list_lock ) {
282    return (NSSCKMDSession *)NULL;
283  }
284
285  rv->etc = (void *)session;
286  rv->Close = nss_dbm_mdSession_Close;
287  rv->GetDeviceError = nss_dbm_mdSession_GetDeviceError;
288  /*  Login isn't needed */
289  /*  Logout isn't needed */
290  /*  InitPIN is irrelevant */
291  /*  SetPIN is irrelevant */
292  /*  GetOperationStateLen is irrelevant */
293  /*  GetOperationState is irrelevant */
294  /*  SetOperationState is irrelevant */
295  rv->CreateObject = nss_dbm_mdSession_CreateObject;
296  /*  CopyObject isn't needed; the framework will use CreateObject */
297  rv->FindObjectsInit = nss_dbm_mdSession_FindObjectsInit;
298  rv->null = NULL;
299
300  return rv;
301}