session.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 1,963 行 · 第 1/3 页

C
1,963
字号
/*  * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape security libraries. *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */#ifdef DEBUGstatic const char CVS_ID[] = "@(#) $RCSfile: session.c,v $ $Revision: 1.5 $ $Date: 2000/09/13 21:15:07 $ $Name: NSS_3_1_1_RTM $";#endif /* DEBUG *//* * session.c * * This file implements the NSSCKFWSession type and methods. */#ifndef CK_T#include "ck.h"#endif /* CK_T *//* * NSSCKFWSession * *  -- create/destroy -- *  nssCKFWSession_Create *  nssCKFWSession_Destroy * *  -- public accessors -- *  NSSCKFWSession_GetMDSession *  NSSCKFWSession_GetArena *  NSSCKFWSession_CallNotification *  NSSCKFWSession_IsRWSession *  NSSCKFWSession_IsSO * *  -- implement public accessors -- *  nssCKFWSession_GetMDSession *  nssCKFWSession_GetArena *  nssCKFWSession_CallNotification *  nssCKFWSession_IsRWSession *  nssCKFWSession_IsSO * *  -- private accessors -- *  nssCKFWSession_GetSlot *  nssCKFWSession_GetSessionState *  nssCKFWSession_SetFWFindObjects *  nssCKFWSession_GetFWFindObjects *  nssCKFWSession_SetMDSession *  nssCKFWSession_SetHandle *  nssCKFWSession_GetHandle *  nssCKFWSession_RegisterSessionObject *  nssCKFWSession_DeegisterSessionObject * *  -- module fronts -- *  nssCKFWSession_GetDeviceError *  nssCKFWSession_Login *  nssCKFWSession_Logout *  nssCKFWSession_InitPIN *  nssCKFWSession_SetPIN *  nssCKFWSession_GetOperationStateLen *  nssCKFWSession_GetOperationState *  nssCKFWSession_SetOperationState *  nssCKFWSession_CreateObject *  nssCKFWSession_CopyObject *  nssCKFWSession_FindObjectsInit *  nssCKFWSession_SeedRandom *  nssCKFWSession_GetRandom */struct NSSCKFWSessionStr {  NSSArena *arena;  NSSCKMDSession *mdSession;  NSSCKFWToken *fwToken;  NSSCKMDToken *mdToken;  NSSCKFWInstance *fwInstance;  NSSCKMDInstance *mdInstance;  CK_VOID_PTR pApplication;  CK_NOTIFY Notify;  /*   * Everything above is set at creation time, and then not modified.   * The items below are atomic.  No locking required.  If we fear   * about pointer-copies being nonatomic, we'll lock fwFindObjects.   */  CK_BBOOL rw;  NSSCKFWFindObjects *fwFindObjects;  nssCKFWHash *sessionObjectHash;  CK_SESSION_HANDLE hSession;};#ifdef DEBUG/* * But first, the pointer-tracking stuff. * * NOTE: the pointer-tracking support in NSS/base currently relies * upon NSPR's CallOnce support.  That, however, relies upon NSPR's * locking, which is tied into the runtime.  We need a pointer-tracker * implementation that uses the locks supplied through C_Initialize. * That support, however, can be filled in later.  So for now, I'll * just do this routines as no-ops. */static CK_RVsession_add_pointer(  const NSSCKFWSession *fwSession){  return CKR_OK;}static CK_RVsession_remove_pointer(  const NSSCKFWSession *fwSession){  return CKR_OK;}NSS_IMPLEMENT CK_RVnssCKFWSession_verifyPointer(  const NSSCKFWSession *fwSession){  return CKR_OK;}#endif /* DEBUG *//* * nssCKFWSession_Create * */NSS_IMPLEMENT NSSCKFWSession *nssCKFWSession_Create(  NSSCKFWToken *fwToken,  CK_BBOOL rw,  CK_VOID_PTR pApplication,  CK_NOTIFY Notify,  CK_RV *pError){  NSSArena *arena = (NSSArena *)NULL;  NSSCKFWSession *fwSession;  NSSCKFWSlot *fwSlot;#ifdef NSSDEBUG  if( (CK_RV *)NULL == pError ) {    return (NSSCKFWSession *)NULL;  }  *pError = nssCKFWToken_verifyPointer(fwToken);  if( CKR_OK != *pError ) {    return (NSSCKFWSession *)NULL;  }#endif /* NSSDEBUG */  arena = NSSArena_Create();  if( (NSSArena *)NULL == arena ) {    *pError = CKR_HOST_MEMORY;    return (NSSCKFWSession *)NULL;  }  fwSession = nss_ZNEW(arena, NSSCKFWSession);  if( (NSSCKFWSession *)NULL == fwSession ) {    *pError = CKR_HOST_MEMORY;    goto loser;  }  fwSession->arena = arena;  fwSession->mdSession = (NSSCKMDSession *)NULL; /* set later */  fwSession->fwToken = fwToken;  fwSession->mdToken = nssCKFWToken_GetMDToken(fwToken);  fwSlot = nssCKFWToken_GetFWSlot(fwToken);  fwSession->fwInstance = nssCKFWSlot_GetFWInstance(fwSlot);  fwSession->mdInstance = nssCKFWSlot_GetMDInstance(fwSlot);  fwSession->rw = rw;  fwSession->pApplication = pApplication;  fwSession->Notify = Notify;  fwSession->fwFindObjects = (NSSCKFWFindObjects *)NULL;  fwSession->sessionObjectHash = nssCKFWHash_Create(fwSession->fwInstance, arena, pError);  if( (nssCKFWHash *)NULL == fwSession->sessionObjectHash ) {    if( CKR_OK == *pError ) {      *pError = CKR_GENERAL_ERROR;    }    goto loser;  }#ifdef DEBUG  *pError = session_add_pointer(fwSession);  if( CKR_OK != *pError ) {    goto loser;  }#endif /* DEBUG */  return fwSession; loser:  if( (NSSArena *)NULL != arena ) {    if( (nssCKFWHash *)NULL != fwSession->sessionObjectHash ) {      (void)nssCKFWHash_Destroy(fwSession->sessionObjectHash);    }    NSSArena_Destroy(arena);  }  return (NSSCKFWSession *)NULL;}static voidnss_ckfw_session_object_destroy_iterator(  const void *key,  void *value,  void *closure){  NSSCKFWObject *fwObject = (NSSCKFWObject *)value;  nssCKFWObject_Finalize(fwObject);}/* * nssCKFWSession_Destroy * */NSS_IMPLEMENT CK_RVnssCKFWSession_Destroy(  NSSCKFWSession *fwSession,  CK_BBOOL removeFromTokenHash){  CK_RV error = CKR_OK;  nssCKFWHash *sessionObjectHash;#ifdef NSSDEBUG  error = nssCKFWSession_verifyPointer(fwSession);  if( CKR_OK != error ) {    return error;  }#endif /* NSSDEBUG */  if( removeFromTokenHash ) {    error = nssCKFWToken_RemoveSession(fwSession->fwToken, fwSession);  }  /*   * Invalidate session objects   */  sessionObjectHash = fwSession->sessionObjectHash;  fwSession->sessionObjectHash = (nssCKFWHash *)NULL;  nssCKFWHash_Iterate(sessionObjectHash,                       nss_ckfw_session_object_destroy_iterator,                       (void *)NULL);#ifdef DEBUG  (void)session_remove_pointer(fwSession);#endif /* DEBUG */  (void)nssCKFWHash_Destroy(sessionObjectHash);  NSSArena_Destroy(fwSession->arena);  return error;}/* * nssCKFWSession_GetMDSession * */NSS_IMPLEMENT NSSCKMDSession *nssCKFWSession_GetMDSession(  NSSCKFWSession *fwSession){#ifdef NSSDEBUG  if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {    return (NSSCKMDSession *)NULL;  }#endif /* NSSDEBUG */  return fwSession->mdSession;}/* * nssCKFWSession_GetArena * */NSS_IMPLEMENT NSSArena *nssCKFWSession_GetArena(  NSSCKFWSession *fwSession,  CK_RV *pError){#ifdef NSSDEBUG  if( (CK_RV *)NULL == pError ) {    return (NSSArena *)NULL;  }  *pError = nssCKFWSession_verifyPointer(fwSession);  if( CKR_OK != *pError ) {    return (NSSArena *)NULL;  }#endif /* NSSDEBUG */  return fwSession->arena;}/* * nssCKFWSession_CallNotification * */NSS_IMPLEMENT CK_RVnssCKFWSession_CallNotification(  NSSCKFWSession *fwSession,  CK_NOTIFICATION event){  CK_RV error = CKR_OK;  CK_SESSION_HANDLE handle;#ifdef NSSDEBUG  error = nssCKFWSession_verifyPointer(fwSession);  if( CKR_OK != error ) {    return error;  }#endif /* NSSDEBUG */  if( (CK_NOTIFY)NULL == fwSession->Notify ) {    return CKR_OK;  }  handle = nssCKFWInstance_FindSessionHandle(fwSession->fwInstance, fwSession);  if( (CK_SESSION_HANDLE)0 == handle ) {    return CKR_GENERAL_ERROR;  }  error = fwSession->Notify(handle, event, fwSession->pApplication);  return error;}/* * nssCKFWSession_IsRWSession * */NSS_IMPLEMENT CK_BBOOLnssCKFWSession_IsRWSession(  NSSCKFWSession *fwSession){#ifdef NSSDEBUG  if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {    return CK_FALSE;  }#endif /* NSSDEBUG */  return fwSession->rw;}/* * nssCKFWSession_IsSO * */NSS_IMPLEMENT CK_BBOOLnssCKFWSession_IsSO(  NSSCKFWSession *fwSession){  CK_STATE state;#ifdef NSSDEBUG  if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {    return CK_FALSE;  }#endif /* NSSDEBUG */  state = nssCKFWToken_GetSessionState(fwSession->fwToken);  switch( state ) {  case CKS_RO_PUBLIC_SESSION:  case CKS_RO_USER_FUNCTIONS:  case CKS_RW_PUBLIC_SESSION:  case CKS_RW_USER_FUNCTIONS:    return CK_FALSE;  case CKS_RW_SO_FUNCTIONS:    return CK_TRUE;  default:    return CK_FALSE;  }}/* * nssCKFWSession_GetFWSlot * */NSS_IMPLEMENT NSSCKFWSlot *nssCKFWSession_GetFWSlot(  NSSCKFWSession *fwSession){#ifdef NSSDEBUG  if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {    return (NSSCKFWSlot *)NULL;  }#endif /* NSSDEBUG */  return nssCKFWToken_GetFWSlot(fwSession->fwToken);}/* * nssCFKWSession_GetSessionState * */NSS_IMPLEMENT CK_STATEnssCKFWSession_GetSessionState(  NSSCKFWSession *fwSession){#ifdef NSSDEBUG  if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {    return CKS_RO_PUBLIC_SESSION; /* whatever */  }#endif /* NSSDEBUG */  return nssCKFWToken_GetSessionState(fwSession->fwToken);}/* * nssCKFWSession_SetFWFindObjects * */NSS_IMPLEMENT CK_RVnssCKFWSession_SetFWFindObjects(  NSSCKFWSession *fwSession,  NSSCKFWFindObjects *fwFindObjects){#ifdef NSSDEBUG  CK_RV error = CKR_OK;#endif /* NSSDEBUG */#ifdef NSSDEBUG  error = nssCKFWSession_verifyPointer(fwSession);  if( CKR_OK != error ) {    return error;  }  /* fwFindObjects may be null */#endif /* NSSDEBUG */  if( ((NSSCKFWFindObjects *)NULL != fwSession->fwFindObjects) &&      ((NSSCKFWFindObjects *)NULL != fwFindObjects) ) {    return CKR_OPERATION_ACTIVE;  }  fwSession->fwFindObjects = fwFindObjects;  return CKR_OK;}/* * nssCKFWSession_GetFWFindObjects * */NSS_IMPLEMENT NSSCKFWFindObjects *nssCKFWSession_GetFWFindObjects(  NSSCKFWSession *fwSession,  CK_RV *pError){#ifdef NSSDEBUG  if( (CK_RV *)NULL == pError ) {    return (NSSCKFWFindObjects *)NULL;  }  *pError = nssCKFWSession_verifyPointer(fwSession);  if( CKR_OK != *pError ) {    return (NSSCKFWFindObjects *)NULL;  }#endif /* NSSDEBUG */  if( (NSSCKFWFindObjects *)NULL == fwSession->fwFindObjects ) {    *pError = CKR_OPERATION_NOT_INITIALIZED;    return (NSSCKFWFindObjects *)NULL;  }  return fwSession->fwFindObjects;}/* * nssCKFWSession_SetMDSession * */NSS_IMPLEMENT CK_RVnssCKFWSession_SetMDSession(  NSSCKFWSession *fwSession,  NSSCKMDSession *mdSession){#ifdef NSSDEBUG  CK_RV error = CKR_OK;#endif /* NSSDEBUG */#ifdef NSSDEBUG  error = nssCKFWSession_verifyPointer(fwSession);  if( CKR_OK != error ) {    return error;  }  if( (NSSCKMDSession *)NULL == mdSession ) {    return CKR_ARGUMENTS_BAD;  }#endif /* NSSDEBUG */  if( (NSSCKMDSession *)NULL != fwSession->mdSession ) {    return CKR_GENERAL_ERROR;  }  fwSession->mdSession = mdSession;  return CKR_OK;}/* * nssCKFWSession_SetHandle * */NSS_IMPLEMENT CK_RVnssCKFWSession_SetHandle(  NSSCKFWSession *fwSession,  CK_SESSION_HANDLE hSession){#ifdef NSSDEBUG  CK_RV error = CKR_OK;#endif /* NSSDEBUG */#ifdef NSSDEBUG  error = nssCKFWSession_verifyPointer(fwSession);  if( CKR_OK != error ) {    return error;  }#endif /* NSSDEBUG */  if( (CK_SESSION_HANDLE)0 != fwSession->hSession ) {    return CKR_GENERAL_ERROR;  }  fwSession->hSession = hSession;  return CKR_OK;}/* * nssCKFWSession_GetHandle * */NSS_IMPLEMENT CK_SESSION_HANDLEnssCKFWSession_GetHandle(  NSSCKFWSession *fwSession){#ifdef NSSDEBUG  if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {    return NULL;  }#endif /* NSSDEBUG */  return fwSession->hSession;}/* * nssCKFWSession_RegisterSessionObject * */NSS_IMPLEMENT CK_RVnssCKFWSession_RegisterSessionObject(  NSSCKFWSession *fwSession,  NSSCKFWObject *fwObject){  CK_RV rv = CKR_OK;#ifdef NSSDEBUG  if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {    return CKR_GENERAL_ERROR;  }#endif /* NSSDEBUG */  if( (nssCKFWHash *)NULL != fwSession->sessionObjectHash ) {    rv = nssCKFWHash_Add(fwSession->sessionObjectHash, fwObject, fwObject);  }  return rv;}/* * nssCKFWSession_DeregisterSessionObject * */NSS_IMPLEMENT CK_RVnssCKFWSession_DeregisterSessionObject(  NSSCKFWSession *fwSession,  NSSCKFWObject *fwObject){#ifdef NSSDEBUG  if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?