Back to index

lightning-sunbird  0.9+nobinonly
session.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifdef DEBUG
00038 static const char CVS_ID[] = "@(#) $RCSfile: session.c,v $ $Revision: 1.2.28.1 $ $Date: 2006/03/02 22:45:14 $";
00039 #endif /* DEBUG */
00040 
00041 #include "ckdbm.h"
00042 
00043 static void
00044 nss_dbm_mdSession_Close
00045 (
00046   NSSCKMDSession *mdSession,
00047   NSSCKFWSession *fwSession,
00048   NSSCKMDToken *mdToken,
00049   NSSCKFWToken *fwToken,
00050   NSSCKMDInstance *mdInstance,
00051   NSSCKFWInstance *fwInstance
00052 )
00053 {
00054   nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
00055 
00056   struct nss_dbm_dbt_node *w;
00057 
00058   /* Lock */
00059   {
00060     if( CKR_OK != NSSCKFWMutex_Lock(session->list_lock) ) {
00061       return;
00062     }
00063 
00064     w = session->session_objects;
00065     session->session_objects = (struct nss_dbm_dbt_node *)NULL; /* sanity */
00066     
00067     (void)NSSCKFWMutex_Unlock(session->list_lock);
00068   }
00069 
00070   for( ; (struct nss_dbm_dbt_node *)NULL != w; w = w->next ) {
00071     (void)nss_dbm_db_delete_object(w->dbt);
00072   }
00073 }
00074 
00075 static CK_ULONG
00076 nss_dbm_mdSession_GetDeviceError
00077 (
00078   NSSCKMDSession *mdSession,
00079   NSSCKFWSession *fwSession,
00080   NSSCKMDToken *mdToken,
00081   NSSCKFWToken *fwToken,
00082   NSSCKMDInstance *mdInstance,
00083   NSSCKFWInstance *fwInstance
00084 )
00085 {
00086   nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
00087   return session->deviceError;
00088 }
00089 
00090 /* Login isn't needed */
00091 /* Logout isn't needed */
00092 /* InitPIN is irrelevant */
00093 /* SetPIN is irrelevant */
00094 /* GetOperationStateLen is irrelevant */
00095 /* GetOperationState is irrelevant */
00096 /* SetOperationState is irrelevant */
00097 
00098 static NSSCKMDObject *
00099 nss_dbm_mdSession_CreateObject
00100 (
00101   NSSCKMDSession *mdSession,
00102   NSSCKFWSession *fwSession,
00103   NSSCKMDToken *mdToken,
00104   NSSCKFWToken *fwToken,
00105   NSSCKMDInstance *mdInstance,
00106   NSSCKFWInstance *fwInstance,
00107   NSSArena *handyArenaPointer,
00108   CK_ATTRIBUTE_PTR pTemplate,
00109   CK_ULONG ulAttributeCount,
00110   CK_RV *pError
00111 )
00112 {
00113   nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
00114   nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
00115   CK_ULONG i;
00116   CK_BBOOL isToken = CK_FALSE; /* defaults to false */
00117   NSSCKMDObject *rv;
00118   struct nss_dbm_dbt_node *node = (struct nss_dbm_dbt_node *)NULL;
00119   nss_dbm_object_t *object;
00120   nss_dbm_db_t *which_db;
00121 
00122   /* This framework should really pass this to me */
00123   for( i = 0; i < ulAttributeCount; i++ ) {
00124     if( CKA_TOKEN == pTemplate[i].type ) {
00125       isToken = *(CK_BBOOL *)pTemplate[i].pValue;
00126       break;
00127     }
00128   }
00129 
00130   object = nss_ZNEW(handyArenaPointer, nss_dbm_object_t);
00131   if( (nss_dbm_object_t *)NULL == object ) {
00132     *pError = CKR_HOST_MEMORY;
00133     return (NSSCKMDObject *)NULL;
00134   }
00135 
00136   object->arena = handyArenaPointer;
00137   which_db = isToken ? token->slot->token_db : token->session_db;
00138 
00139   /* Do this before the actual database call; it's easier to recover from */
00140   rv = nss_dbm_mdObject_factory(object, pError);
00141   if( (NSSCKMDObject *)NULL == rv ) {
00142     return (NSSCKMDObject *)NULL;
00143   }
00144 
00145   if( CK_FALSE == isToken ) {
00146     node = nss_ZNEW(session->arena, struct nss_dbm_dbt_node);
00147     if( (struct nss_dbm_dbt_node *)NULL == node ) {
00148       *pError = CKR_HOST_MEMORY;
00149       return (NSSCKMDObject *)NULL;
00150     }
00151   }
00152 
00153   object->handle = nss_dbm_db_create_object(handyArenaPointer, which_db, 
00154                                             pTemplate, ulAttributeCount,
00155                                             pError, &session->deviceError);
00156   if( (nss_dbm_dbt_t *)NULL == object->handle ) {
00157     return (NSSCKMDObject *)NULL;
00158   }
00159 
00160   if( CK_FALSE == isToken ) {
00161     node->dbt = object->handle;
00162     /* Lock */
00163     {
00164       *pError = NSSCKFWMutex_Lock(session->list_lock);
00165       if( CKR_OK != *pError ) {
00166         (void)nss_dbm_db_delete_object(object->handle);
00167         return (NSSCKMDObject *)NULL;
00168       }
00169       
00170       node->next = session->session_objects;
00171       session->session_objects = node;
00172       
00173       *pError = NSSCKFWMutex_Unlock(session->list_lock);
00174     }
00175   }
00176 
00177   return rv;
00178 }
00179 
00180 /* CopyObject isn't needed; the framework will use CreateObject */
00181 
00182 static NSSCKMDFindObjects *
00183 nss_dbm_mdSession_FindObjectsInit
00184 (
00185   NSSCKMDSession *mdSession,
00186   NSSCKFWSession *fwSession,
00187   NSSCKMDToken *mdToken,
00188   NSSCKFWToken *fwToken,
00189   NSSCKMDInstance *mdInstance,
00190   NSSCKFWInstance *fwInstance,
00191   CK_ATTRIBUTE_PTR pTemplate,
00192   CK_ULONG ulAttributeCount,
00193   CK_RV *pError
00194 )
00195 {
00196   nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
00197   nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
00198   NSSArena *arena;
00199   nss_dbm_find_t *find;
00200   NSSCKMDFindObjects *rv;
00201 
00202   arena = NSSArena_Create();
00203   if( (NSSArena *)NULL == arena ) {
00204     *pError = CKR_HOST_MEMORY;
00205     goto loser;
00206   }
00207 
00208   find = nss_ZNEW(arena, nss_dbm_find_t);
00209   if( (nss_dbm_find_t *)NULL == find ) {
00210     *pError = CKR_HOST_MEMORY;
00211     goto loser;
00212   }
00213 
00214   find->arena = arena;
00215   find->list_lock = NSSCKFWInstance_CreateMutex(fwInstance, arena, pError);
00216   if( (NSSCKFWMutex *)NULL == find->list_lock ) {
00217     goto loser;
00218   }
00219 
00220   *pError = nss_dbm_db_find_objects(find, token->slot->token_db, pTemplate, 
00221                                     ulAttributeCount, &session->deviceError);
00222   if( CKR_OK != *pError ) {
00223     goto loser;
00224   }
00225 
00226   *pError = nss_dbm_db_find_objects(find, token->session_db, pTemplate, 
00227                                     ulAttributeCount, &session->deviceError);
00228   if( CKR_OK != *pError ) {
00229     goto loser;
00230   }
00231 
00232   rv = nss_dbm_mdFindObjects_factory(find, pError);
00233   if( (NSSCKMDFindObjects *)NULL == rv ) {
00234     goto loser;
00235   }
00236 
00237   return rv;
00238 
00239  loser:
00240   if( (NSSArena *)NULL != arena ) {
00241     (void)NSSArena_Destroy(arena);
00242   }
00243 
00244   return (NSSCKMDFindObjects *)NULL;
00245 }
00246 
00247 /* SeedRandom is irrelevant */
00248 /* GetRandom is irrelevant */
00249 
00250 NSS_IMPLEMENT NSSCKMDSession *
00251 nss_dbm_mdSession_factory
00252 (
00253   nss_dbm_token_t *token,
00254   NSSCKFWSession *fwSession,
00255   NSSCKFWInstance *fwInstance,
00256   CK_BBOOL rw,
00257   CK_RV *pError
00258 )
00259 {
00260   NSSArena *arena;
00261   nss_dbm_session_t *session;
00262   NSSCKMDSession *rv;
00263 
00264   arena = NSSCKFWSession_GetArena(fwSession, pError);
00265 
00266   session = nss_ZNEW(arena, nss_dbm_session_t);
00267   if( (nss_dbm_session_t *)NULL == session ) {
00268     *pError = CKR_HOST_MEMORY;
00269     return (NSSCKMDSession *)NULL;
00270   }
00271 
00272   rv = nss_ZNEW(arena, NSSCKMDSession);
00273   if( (NSSCKMDSession *)NULL == rv ) {
00274     *pError = CKR_HOST_MEMORY;
00275     return (NSSCKMDSession *)NULL;
00276   }
00277 
00278   session->arena = arena;
00279   session->token = token;
00280   session->list_lock = NSSCKFWInstance_CreateMutex(fwInstance, arena, pError);
00281   if( (NSSCKFWMutex *)NULL == session->list_lock ) {
00282     return (NSSCKMDSession *)NULL;
00283   }
00284 
00285   rv->etc = (void *)session;
00286   rv->Close = nss_dbm_mdSession_Close;
00287   rv->GetDeviceError = nss_dbm_mdSession_GetDeviceError;
00288   /*  Login isn't needed */
00289   /*  Logout isn't needed */
00290   /*  InitPIN is irrelevant */
00291   /*  SetPIN is irrelevant */
00292   /*  GetOperationStateLen is irrelevant */
00293   /*  GetOperationState is irrelevant */
00294   /*  SetOperationState is irrelevant */
00295   rv->CreateObject = nss_dbm_mdSession_CreateObject;
00296   /*  CopyObject isn't needed; the framework will use CreateObject */
00297   rv->FindObjectsInit = nss_dbm_mdSession_FindObjectsInit;
00298   rv->null = NULL;
00299 
00300   return rv;
00301 }