michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * session.c michael@0: * michael@0: * This file implements the NSSCKFWSession type and methods. michael@0: */ michael@0: michael@0: #ifndef CK_T michael@0: #include "ck.h" michael@0: #endif /* CK_T */ michael@0: michael@0: /* michael@0: * NSSCKFWSession michael@0: * michael@0: * -- create/destroy -- michael@0: * nssCKFWSession_Create michael@0: * nssCKFWSession_Destroy michael@0: * michael@0: * -- public accessors -- michael@0: * NSSCKFWSession_GetMDSession michael@0: * NSSCKFWSession_GetArena michael@0: * NSSCKFWSession_CallNotification michael@0: * NSSCKFWSession_IsRWSession michael@0: * NSSCKFWSession_IsSO michael@0: * michael@0: * -- implement public accessors -- michael@0: * nssCKFWSession_GetMDSession michael@0: * nssCKFWSession_GetArena michael@0: * nssCKFWSession_CallNotification michael@0: * nssCKFWSession_IsRWSession michael@0: * nssCKFWSession_IsSO michael@0: * michael@0: * -- private accessors -- michael@0: * nssCKFWSession_GetSlot michael@0: * nssCKFWSession_GetSessionState michael@0: * nssCKFWSession_SetFWFindObjects michael@0: * nssCKFWSession_GetFWFindObjects michael@0: * nssCKFWSession_SetMDSession michael@0: * nssCKFWSession_SetHandle michael@0: * nssCKFWSession_GetHandle michael@0: * nssCKFWSession_RegisterSessionObject michael@0: * nssCKFWSession_DeegisterSessionObject michael@0: * michael@0: * -- module fronts -- michael@0: * nssCKFWSession_GetDeviceError michael@0: * nssCKFWSession_Login michael@0: * nssCKFWSession_Logout michael@0: * nssCKFWSession_InitPIN michael@0: * nssCKFWSession_SetPIN michael@0: * nssCKFWSession_GetOperationStateLen michael@0: * nssCKFWSession_GetOperationState michael@0: * nssCKFWSession_SetOperationState michael@0: * nssCKFWSession_CreateObject michael@0: * nssCKFWSession_CopyObject michael@0: * nssCKFWSession_FindObjectsInit michael@0: * nssCKFWSession_SeedRandom michael@0: * nssCKFWSession_GetRandom michael@0: */ michael@0: michael@0: struct NSSCKFWSessionStr { michael@0: NSSArena *arena; michael@0: NSSCKMDSession *mdSession; michael@0: NSSCKFWToken *fwToken; michael@0: NSSCKMDToken *mdToken; michael@0: NSSCKFWInstance *fwInstance; michael@0: NSSCKMDInstance *mdInstance; michael@0: CK_VOID_PTR pApplication; michael@0: CK_NOTIFY Notify; michael@0: michael@0: /* michael@0: * Everything above is set at creation time, and then not modified. michael@0: * The items below are atomic. No locking required. If we fear michael@0: * about pointer-copies being nonatomic, we'll lock fwFindObjects. michael@0: */ michael@0: michael@0: CK_BBOOL rw; michael@0: NSSCKFWFindObjects *fwFindObjects; michael@0: NSSCKFWCryptoOperation *fwOperationArray[NSSCKFWCryptoOperationState_Max]; michael@0: nssCKFWHash *sessionObjectHash; michael@0: CK_SESSION_HANDLE hSession; michael@0: }; michael@0: michael@0: #ifdef DEBUG michael@0: /* michael@0: * But first, the pointer-tracking stuff. michael@0: * michael@0: * NOTE: the pointer-tracking support in NSS/base currently relies michael@0: * upon NSPR's CallOnce support. That, however, relies upon NSPR's michael@0: * locking, which is tied into the runtime. We need a pointer-tracker michael@0: * implementation that uses the locks supplied through C_Initialize. michael@0: * That support, however, can be filled in later. So for now, I'll michael@0: * just do this routines as no-ops. michael@0: */ michael@0: michael@0: static CK_RV michael@0: session_add_pointer michael@0: ( michael@0: const NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: static CK_RV michael@0: session_remove_pointer michael@0: ( michael@0: const NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_verifyPointer michael@0: ( michael@0: const NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: #endif /* DEBUG */ michael@0: michael@0: /* michael@0: * nssCKFWSession_Create michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT NSSCKFWSession * michael@0: nssCKFWSession_Create michael@0: ( michael@0: NSSCKFWToken *fwToken, michael@0: CK_BBOOL rw, michael@0: CK_VOID_PTR pApplication, michael@0: CK_NOTIFY Notify, michael@0: CK_RV *pError michael@0: ) michael@0: { michael@0: NSSArena *arena = (NSSArena *)NULL; michael@0: NSSCKFWSession *fwSession; michael@0: NSSCKFWSlot *fwSlot; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if (!pError) { michael@0: return (NSSCKFWSession *)NULL; michael@0: } michael@0: michael@0: *pError = nssCKFWToken_verifyPointer(fwToken); michael@0: if( CKR_OK != *pError ) { michael@0: return (NSSCKFWSession *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: arena = NSSArena_Create(); michael@0: if (!arena) { michael@0: *pError = CKR_HOST_MEMORY; michael@0: return (NSSCKFWSession *)NULL; michael@0: } michael@0: michael@0: fwSession = nss_ZNEW(arena, NSSCKFWSession); michael@0: if (!fwSession) { michael@0: *pError = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: michael@0: fwSession->arena = arena; michael@0: fwSession->mdSession = (NSSCKMDSession *)NULL; /* set later */ michael@0: fwSession->fwToken = fwToken; michael@0: fwSession->mdToken = nssCKFWToken_GetMDToken(fwToken); michael@0: michael@0: fwSlot = nssCKFWToken_GetFWSlot(fwToken); michael@0: fwSession->fwInstance = nssCKFWSlot_GetFWInstance(fwSlot); michael@0: fwSession->mdInstance = nssCKFWSlot_GetMDInstance(fwSlot); michael@0: michael@0: fwSession->rw = rw; michael@0: fwSession->pApplication = pApplication; michael@0: fwSession->Notify = Notify; michael@0: michael@0: fwSession->fwFindObjects = (NSSCKFWFindObjects *)NULL; michael@0: michael@0: fwSession->sessionObjectHash = nssCKFWHash_Create(fwSession->fwInstance, arena, pError); michael@0: if (!fwSession->sessionObjectHash) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: goto loser; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: *pError = session_add_pointer(fwSession); michael@0: if( CKR_OK != *pError ) { michael@0: goto loser; michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: return fwSession; michael@0: michael@0: loser: michael@0: if (arena) { michael@0: if (fwSession && fwSession->sessionObjectHash) { michael@0: (void)nssCKFWHash_Destroy(fwSession->sessionObjectHash); michael@0: } michael@0: NSSArena_Destroy(arena); michael@0: } michael@0: michael@0: return (NSSCKFWSession *)NULL; michael@0: } michael@0: michael@0: static void michael@0: nss_ckfw_session_object_destroy_iterator michael@0: ( michael@0: const void *key, michael@0: void *value, michael@0: void *closure michael@0: ) michael@0: { michael@0: NSSCKFWObject *fwObject = (NSSCKFWObject *)value; michael@0: nssCKFWObject_Finalize(fwObject, PR_TRUE); michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_Destroy michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_Destroy michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_BBOOL removeFromTokenHash michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: nssCKFWHash *sessionObjectHash; michael@0: NSSCKFWCryptoOperationState i; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if( removeFromTokenHash ) { michael@0: error = nssCKFWToken_RemoveSession(fwSession->fwToken, fwSession); michael@0: } michael@0: michael@0: /* michael@0: * Invalidate session objects michael@0: */ michael@0: michael@0: sessionObjectHash = fwSession->sessionObjectHash; michael@0: fwSession->sessionObjectHash = (nssCKFWHash *)NULL; michael@0: michael@0: nssCKFWHash_Iterate(sessionObjectHash, michael@0: nss_ckfw_session_object_destroy_iterator, michael@0: (void *)NULL); michael@0: michael@0: for (i=0; i < NSSCKFWCryptoOperationState_Max; i++) { michael@0: if (fwSession->fwOperationArray[i]) { michael@0: nssCKFWCryptoOperation_Destroy(fwSession->fwOperationArray[i]); michael@0: } michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: (void)session_remove_pointer(fwSession); michael@0: #endif /* DEBUG */ michael@0: (void)nssCKFWHash_Destroy(sessionObjectHash); michael@0: NSSArena_Destroy(fwSession->arena); michael@0: michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetMDSession michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT NSSCKMDSession * michael@0: nssCKFWSession_GetMDSession michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return (NSSCKMDSession *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: return fwSession->mdSession; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetArena michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT NSSArena * michael@0: nssCKFWSession_GetArena michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_RV *pError michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if (!pError) { michael@0: return (NSSArena *)NULL; michael@0: } michael@0: michael@0: *pError = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != *pError ) { michael@0: return (NSSArena *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: return fwSession->arena; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_CallNotification michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_CallNotification michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_NOTIFICATION event michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: CK_SESSION_HANDLE handle; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if( (CK_NOTIFY)NULL == fwSession->Notify ) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: handle = nssCKFWInstance_FindSessionHandle(fwSession->fwInstance, fwSession); michael@0: if( (CK_SESSION_HANDLE)0 == handle ) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: michael@0: error = fwSession->Notify(handle, event, fwSession->pApplication); michael@0: michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_IsRWSession michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_BBOOL michael@0: nssCKFWSession_IsRWSession michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return CK_FALSE; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: return fwSession->rw; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_IsSO michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_BBOOL michael@0: nssCKFWSession_IsSO michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: CK_STATE state; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return CK_FALSE; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: state = nssCKFWToken_GetSessionState(fwSession->fwToken); michael@0: switch( state ) { michael@0: case CKS_RO_PUBLIC_SESSION: michael@0: case CKS_RO_USER_FUNCTIONS: michael@0: case CKS_RW_PUBLIC_SESSION: michael@0: case CKS_RW_USER_FUNCTIONS: michael@0: return CK_FALSE; michael@0: case CKS_RW_SO_FUNCTIONS: michael@0: return CK_TRUE; michael@0: default: michael@0: return CK_FALSE; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetFWSlot michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT NSSCKFWSlot * michael@0: nssCKFWSession_GetFWSlot michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return (NSSCKFWSlot *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: return nssCKFWToken_GetFWSlot(fwSession->fwToken); michael@0: } michael@0: michael@0: /* michael@0: * nssCFKWSession_GetSessionState michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_STATE michael@0: nssCKFWSession_GetSessionState michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return CKS_RO_PUBLIC_SESSION; /* whatever */ michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: return nssCKFWToken_GetSessionState(fwSession->fwToken); michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_SetFWFindObjects michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_SetFWFindObjects michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWFindObjects *fwFindObjects michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: CK_RV error = CKR_OK; michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: /* fwFindObjects may be null */ michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if ((fwSession->fwFindObjects) && michael@0: (fwFindObjects)) { michael@0: return CKR_OPERATION_ACTIVE; michael@0: } michael@0: michael@0: fwSession->fwFindObjects = fwFindObjects; michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetFWFindObjects michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT NSSCKFWFindObjects * michael@0: nssCKFWSession_GetFWFindObjects michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_RV *pError michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if (!pError) { michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: michael@0: *pError = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != *pError ) { michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (!fwSession->fwFindObjects) { michael@0: *pError = CKR_OPERATION_NOT_INITIALIZED; michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: michael@0: return fwSession->fwFindObjects; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_SetMDSession michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_SetMDSession michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKMDSession *mdSession michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: CK_RV error = CKR_OK; michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!mdSession) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: michael@0: fwSession->mdSession = mdSession; michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_SetHandle michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_SetHandle michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_SESSION_HANDLE hSession michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: CK_RV error = CKR_OK; michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if( (CK_SESSION_HANDLE)0 != fwSession->hSession ) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: michael@0: fwSession->hSession = hSession; michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetHandle michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_SESSION_HANDLE michael@0: nssCKFWSession_GetHandle michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: return fwSession->hSession; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_RegisterSessionObject michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_RegisterSessionObject michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWObject *fwObject michael@0: ) michael@0: { michael@0: CK_RV rv = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (fwSession->sessionObjectHash) { michael@0: rv = nssCKFWHash_Add(fwSession->sessionObjectHash, fwObject, fwObject); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_DeregisterSessionObject michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_DeregisterSessionObject michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWObject *fwObject michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (fwSession->sessionObjectHash) { michael@0: nssCKFWHash_Remove(fwSession->sessionObjectHash, fwObject); michael@0: } michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetDeviceError michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_ULONG michael@0: nssCKFWSession_GetDeviceError michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return (CK_ULONG)0; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return (CK_ULONG)0; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (!fwSession->mdSession->GetDeviceError) { michael@0: return (CK_ULONG)0; michael@0: } michael@0: michael@0: return fwSession->mdSession->GetDeviceError(fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, michael@0: fwSession->mdInstance, fwSession->fwInstance); michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_Login michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_Login michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_USER_TYPE userType, michael@0: NSSItem *pin michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: CK_STATE oldState; michael@0: CK_STATE newState; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: switch( userType ) { michael@0: case CKU_SO: michael@0: case CKU_USER: michael@0: break; michael@0: default: michael@0: return CKR_USER_TYPE_INVALID; michael@0: } michael@0: michael@0: if (!pin) { michael@0: if( CK_TRUE != nssCKFWToken_GetHasProtectedAuthenticationPath(fwSession->fwToken) ) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: oldState = nssCKFWToken_GetSessionState(fwSession->fwToken); michael@0: michael@0: /* michael@0: * It's not clear what happens when you're already logged in. michael@0: * I'll just fail; but if we decide to change, the logic is michael@0: * all right here. michael@0: */ michael@0: michael@0: if( CKU_SO == userType ) { michael@0: switch( oldState ) { michael@0: case CKS_RO_PUBLIC_SESSION: michael@0: /* michael@0: * There's no such thing as a read-only security officer michael@0: * session, so fail. The error should be CKR_SESSION_READ_ONLY, michael@0: * except that C_Login isn't defined to return that. So we'll michael@0: * do CKR_SESSION_READ_ONLY_EXISTS, which is what is documented. michael@0: */ michael@0: return CKR_SESSION_READ_ONLY_EXISTS; michael@0: case CKS_RO_USER_FUNCTIONS: michael@0: return CKR_USER_ANOTHER_ALREADY_LOGGED_IN; michael@0: case CKS_RW_PUBLIC_SESSION: michael@0: newState = CKS_RW_SO_FUNCTIONS; michael@0: break; michael@0: case CKS_RW_USER_FUNCTIONS: michael@0: return CKR_USER_ANOTHER_ALREADY_LOGGED_IN; michael@0: case CKS_RW_SO_FUNCTIONS: michael@0: return CKR_USER_ALREADY_LOGGED_IN; michael@0: default: michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: } else /* CKU_USER == userType */ { michael@0: switch( oldState ) { michael@0: case CKS_RO_PUBLIC_SESSION: michael@0: newState = CKS_RO_USER_FUNCTIONS; michael@0: break; michael@0: case CKS_RO_USER_FUNCTIONS: michael@0: return CKR_USER_ALREADY_LOGGED_IN; michael@0: case CKS_RW_PUBLIC_SESSION: michael@0: newState = CKS_RW_USER_FUNCTIONS; michael@0: break; michael@0: case CKS_RW_USER_FUNCTIONS: michael@0: return CKR_USER_ALREADY_LOGGED_IN; michael@0: case CKS_RW_SO_FUNCTIONS: michael@0: return CKR_USER_ANOTHER_ALREADY_LOGGED_IN; michael@0: default: michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * So now we're in one of three cases: michael@0: * michael@0: * Old == CKS_RW_PUBLIC_SESSION, New == CKS_RW_SO_FUNCTIONS; michael@0: * Old == CKS_RW_PUBLIC_SESSION, New == CKS_RW_USER_FUNCTIONS; michael@0: * Old == CKS_RO_PUBLIC_SESSION, New == CKS_RO_USER_FUNCTIONS; michael@0: */ michael@0: michael@0: if (!fwSession->mdSession->Login) { michael@0: /* michael@0: * The Module doesn't want to be informed (or check the pin) michael@0: * it'll just rely on the Framework as needed. michael@0: */ michael@0: ; michael@0: } else { michael@0: error = fwSession->mdSession->Login(fwSession->mdSession, fwSession, michael@0: fwSession->mdToken, fwSession->fwToken, fwSession->mdInstance, michael@0: fwSession->fwInstance, userType, pin, oldState, newState); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: } michael@0: michael@0: (void)nssCKFWToken_SetSessionState(fwSession->fwToken, newState); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_Logout michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_Logout michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: CK_STATE oldState; michael@0: CK_STATE newState; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: oldState = nssCKFWToken_GetSessionState(fwSession->fwToken); michael@0: michael@0: switch( oldState ) { michael@0: case CKS_RO_PUBLIC_SESSION: michael@0: return CKR_USER_NOT_LOGGED_IN; michael@0: case CKS_RO_USER_FUNCTIONS: michael@0: newState = CKS_RO_PUBLIC_SESSION; michael@0: break; michael@0: case CKS_RW_PUBLIC_SESSION: michael@0: return CKR_USER_NOT_LOGGED_IN; michael@0: case CKS_RW_USER_FUNCTIONS: michael@0: newState = CKS_RW_PUBLIC_SESSION; michael@0: break; michael@0: case CKS_RW_SO_FUNCTIONS: michael@0: newState = CKS_RW_PUBLIC_SESSION; michael@0: break; michael@0: default: michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: michael@0: /* michael@0: * So now we're in one of three cases: michael@0: * michael@0: * Old == CKS_RW_SO_FUNCTIONS, New == CKS_RW_PUBLIC_SESSION; michael@0: * Old == CKS_RW_USER_FUNCTIONS, New == CKS_RW_PUBLIC_SESSION; michael@0: * Old == CKS_RO_USER_FUNCTIONS, New == CKS_RO_PUBLIC_SESSION; michael@0: */ michael@0: michael@0: if (!fwSession->mdSession->Logout) { michael@0: /* michael@0: * The Module doesn't want to be informed. Okay. michael@0: */ michael@0: ; michael@0: } else { michael@0: error = fwSession->mdSession->Logout(fwSession->mdSession, fwSession, michael@0: fwSession->mdToken, fwSession->fwToken, fwSession->mdInstance, michael@0: fwSession->fwInstance, oldState, newState); michael@0: if( CKR_OK != error ) { michael@0: /* michael@0: * Now what?! A failure really should end up with the Framework michael@0: * considering it logged out, right? michael@0: */ michael@0: ; michael@0: } michael@0: } michael@0: michael@0: (void)nssCKFWToken_SetSessionState(fwSession->fwToken, newState); michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_InitPIN michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_InitPIN michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSItem *pin michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: CK_STATE state; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: state = nssCKFWToken_GetSessionState(fwSession->fwToken); michael@0: if( CKS_RW_SO_FUNCTIONS != state ) { michael@0: return CKR_USER_NOT_LOGGED_IN; michael@0: } michael@0: michael@0: if (!pin) { michael@0: CK_BBOOL has = nssCKFWToken_GetHasProtectedAuthenticationPath(fwSession->fwToken); michael@0: if( CK_TRUE != has ) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: } michael@0: michael@0: if (!fwSession->mdSession->InitPIN) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: error = fwSession->mdSession->InitPIN(fwSession->mdSession, fwSession, michael@0: fwSession->mdToken, fwSession->fwToken, fwSession->mdInstance, michael@0: fwSession->fwInstance, pin); michael@0: michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_SetPIN michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_SetPIN michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSItem *newPin, michael@0: NSSItem *oldPin michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (!newPin) { michael@0: CK_BBOOL has = nssCKFWToken_GetHasProtectedAuthenticationPath(fwSession->fwToken); michael@0: if( CK_TRUE != has ) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: } michael@0: michael@0: if (!oldPin) { michael@0: CK_BBOOL has = nssCKFWToken_GetHasProtectedAuthenticationPath(fwSession->fwToken); michael@0: if( CK_TRUE != has ) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: } michael@0: michael@0: if (!fwSession->mdSession->SetPIN) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: error = fwSession->mdSession->SetPIN(fwSession->mdSession, fwSession, michael@0: fwSession->mdToken, fwSession->fwToken, fwSession->mdInstance, michael@0: fwSession->fwInstance, newPin, oldPin); michael@0: michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetOperationStateLen michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_ULONG michael@0: nssCKFWSession_GetOperationStateLen michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_RV *pError michael@0: ) michael@0: { michael@0: CK_ULONG mdAmt; michael@0: CK_ULONG fwAmt; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if (!pError) { michael@0: return (CK_ULONG)0; michael@0: } michael@0: michael@0: *pError = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != *pError ) { michael@0: return (CK_ULONG)0; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: return (CK_ULONG)0; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (!fwSession->mdSession->GetOperationStateLen) { michael@0: *pError = CKR_STATE_UNSAVEABLE; michael@0: return (CK_ULONG)0; michael@0: } michael@0: michael@0: /* michael@0: * We could check that the session is actually in some state.. michael@0: */ michael@0: michael@0: mdAmt = fwSession->mdSession->GetOperationStateLen(fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, fwSession->mdInstance, michael@0: fwSession->fwInstance, pError); michael@0: michael@0: if( ((CK_ULONG)0 == mdAmt) && (CKR_OK != *pError) ) { michael@0: return (CK_ULONG)0; michael@0: } michael@0: michael@0: /* michael@0: * Add a bit of sanity-checking michael@0: */ michael@0: fwAmt = mdAmt + 2*sizeof(CK_ULONG); michael@0: michael@0: return fwAmt; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetOperationState michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_GetOperationState michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSItem *buffer michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: CK_ULONG fwAmt; michael@0: CK_ULONG *ulBuffer; michael@0: NSSItem i2; michael@0: CK_ULONG n, i; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!buffer) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: if (!buffer->data) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (!fwSession->mdSession->GetOperationState) { michael@0: return CKR_STATE_UNSAVEABLE; michael@0: } michael@0: michael@0: /* michael@0: * Sanity-check the caller's buffer. michael@0: */ michael@0: michael@0: error = CKR_OK; michael@0: fwAmt = nssCKFWSession_GetOperationStateLen(fwSession, &error); michael@0: if( ((CK_ULONG)0 == fwAmt) && (CKR_OK != error) ) { michael@0: return error; michael@0: } michael@0: michael@0: if( buffer->size < fwAmt ) { michael@0: return CKR_BUFFER_TOO_SMALL; michael@0: } michael@0: michael@0: ulBuffer = (CK_ULONG *)buffer->data; michael@0: michael@0: i2.size = buffer->size - 2*sizeof(CK_ULONG); michael@0: i2.data = (void *)&ulBuffer[2]; michael@0: michael@0: error = fwSession->mdSession->GetOperationState(fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, michael@0: fwSession->mdInstance, fwSession->fwInstance, &i2); michael@0: michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * Add a little integrety/identity check. michael@0: * NOTE: right now, it's pretty stupid. michael@0: * A CRC or something would be better. michael@0: */ michael@0: michael@0: ulBuffer[0] = 0x434b4657; /* CKFW */ michael@0: ulBuffer[1] = 0; michael@0: n = i2.size/sizeof(CK_ULONG); michael@0: for( i = 0; i < n; i++ ) { michael@0: ulBuffer[1] ^= ulBuffer[2+i]; michael@0: } michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_SetOperationState michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_SetOperationState michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSItem *state, michael@0: NSSCKFWObject *encryptionKey, michael@0: NSSCKFWObject *authenticationKey michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: CK_ULONG *ulBuffer; michael@0: CK_ULONG n, i; michael@0: CK_ULONG x; michael@0: NSSItem s; michael@0: NSSCKMDObject *mdek; michael@0: NSSCKMDObject *mdak; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!state) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: if (!state->data) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: if (encryptionKey) { michael@0: error = nssCKFWObject_verifyPointer(encryptionKey); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: } michael@0: michael@0: if (authenticationKey) { michael@0: error = nssCKFWObject_verifyPointer(authenticationKey); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: ulBuffer = (CK_ULONG *)state->data; michael@0: if( 0x43b4657 != ulBuffer[0] ) { michael@0: return CKR_SAVED_STATE_INVALID; michael@0: } michael@0: n = (state->size / sizeof(CK_ULONG)) - 2; michael@0: x = (CK_ULONG)0; michael@0: for( i = 0; i < n; i++ ) { michael@0: x ^= ulBuffer[2+i]; michael@0: } michael@0: michael@0: if( x != ulBuffer[1] ) { michael@0: return CKR_SAVED_STATE_INVALID; michael@0: } michael@0: michael@0: if (!fwSession->mdSession->SetOperationState) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: michael@0: s.size = state->size - 2*sizeof(CK_ULONG); michael@0: s.data = (void *)&ulBuffer[2]; michael@0: michael@0: if (encryptionKey) { michael@0: mdek = nssCKFWObject_GetMDObject(encryptionKey); michael@0: } else { michael@0: mdek = (NSSCKMDObject *)NULL; michael@0: } michael@0: michael@0: if (authenticationKey) { michael@0: mdak = nssCKFWObject_GetMDObject(authenticationKey); michael@0: } else { michael@0: mdak = (NSSCKMDObject *)NULL; michael@0: } michael@0: michael@0: error = fwSession->mdSession->SetOperationState(fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, fwSession->mdInstance, michael@0: fwSession->fwInstance, &s, mdek, encryptionKey, mdak, authenticationKey); michael@0: michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * Here'd we restore any session data michael@0: */ michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: static CK_BBOOL michael@0: nss_attributes_form_token_object michael@0: ( michael@0: CK_ATTRIBUTE_PTR pTemplate, michael@0: CK_ULONG ulAttributeCount michael@0: ) michael@0: { michael@0: CK_ULONG i; michael@0: CK_BBOOL rv; michael@0: michael@0: for( i = 0; i < ulAttributeCount; i++ ) { michael@0: if( CKA_TOKEN == pTemplate[i].type ) { michael@0: /* If we sanity-check, we can remove this sizeof check */ michael@0: if( sizeof(CK_BBOOL) == pTemplate[i].ulValueLen ) { michael@0: (void)nsslibc_memcpy(&rv, pTemplate[i].pValue, sizeof(CK_BBOOL)); michael@0: return rv; michael@0: } else { michael@0: return CK_FALSE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return CK_FALSE; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_CreateObject michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT NSSCKFWObject * michael@0: nssCKFWSession_CreateObject michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_ATTRIBUTE_PTR pTemplate, michael@0: CK_ULONG ulAttributeCount, michael@0: CK_RV *pError michael@0: ) michael@0: { michael@0: NSSArena *arena; michael@0: NSSCKMDObject *mdObject; michael@0: NSSCKFWObject *fwObject; michael@0: CK_BBOOL isTokenObject; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if (!pError) { michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: *pError = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != pError ) { michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: if( (CK_ATTRIBUTE_PTR)NULL == pTemplate ) { michael@0: *pError = CKR_ARGUMENTS_BAD; michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: /* michael@0: * Here would be an excellent place to sanity-check the object. michael@0: */ michael@0: michael@0: isTokenObject = nss_attributes_form_token_object(pTemplate, ulAttributeCount); michael@0: if( CK_TRUE == isTokenObject ) { michael@0: /* === TOKEN OBJECT === */ michael@0: michael@0: if (!fwSession->mdSession->CreateObject) { michael@0: *pError = CKR_TOKEN_WRITE_PROTECTED; michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: arena = nssCKFWToken_GetArena(fwSession->fwToken, pError); michael@0: if (!arena) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: goto callmdcreateobject; michael@0: } else { michael@0: /* === SESSION OBJECT === */ michael@0: michael@0: arena = nssCKFWSession_GetArena(fwSession, pError); michael@0: if (!arena) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: if( CK_TRUE == nssCKFWInstance_GetModuleHandlesSessionObjects( michael@0: fwSession->fwInstance) ) { michael@0: /* --- module handles the session object -- */ michael@0: michael@0: if (!fwSession->mdSession->CreateObject) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: goto callmdcreateobject; michael@0: } else { michael@0: /* --- framework handles the session object -- */ michael@0: mdObject = nssCKMDSessionObject_Create(fwSession->fwToken, michael@0: arena, pTemplate, ulAttributeCount, pError); michael@0: goto gotmdobject; michael@0: } michael@0: } michael@0: michael@0: callmdcreateobject: michael@0: mdObject = fwSession->mdSession->CreateObject(fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, michael@0: fwSession->mdInstance, fwSession->fwInstance, arena, pTemplate, michael@0: ulAttributeCount, pError); michael@0: michael@0: gotmdobject: michael@0: if (!mdObject) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: fwObject = nssCKFWObject_Create(arena, mdObject, michael@0: isTokenObject ? NULL : fwSession, michael@0: fwSession->fwToken, fwSession->fwInstance, pError); michael@0: if (!fwObject) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: michael@0: if (mdObject->Destroy) { michael@0: (void)mdObject->Destroy(mdObject, (NSSCKFWObject *)NULL, michael@0: fwSession->mdSession, fwSession, fwSession->mdToken, michael@0: fwSession->fwToken, fwSession->mdInstance, fwSession->fwInstance); michael@0: } michael@0: michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: if( CK_FALSE == isTokenObject ) { michael@0: if( CK_FALSE == nssCKFWHash_Exists(fwSession->sessionObjectHash, fwObject) ) { michael@0: *pError = nssCKFWHash_Add(fwSession->sessionObjectHash, fwObject, fwObject); michael@0: if( CKR_OK != *pError ) { michael@0: nssCKFWObject_Finalize(fwObject, PR_TRUE); michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return fwObject; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_CopyObject michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT NSSCKFWObject * michael@0: nssCKFWSession_CopyObject michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWObject *fwObject, michael@0: CK_ATTRIBUTE_PTR pTemplate, michael@0: CK_ULONG ulAttributeCount, michael@0: CK_RV *pError michael@0: ) michael@0: { michael@0: CK_BBOOL oldIsToken; michael@0: CK_BBOOL newIsToken; michael@0: CK_ULONG i; michael@0: NSSCKFWObject *rv; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if (!pError) { michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: *pError = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != *pError ) { michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: *pError = nssCKFWObject_verifyPointer(fwObject); michael@0: if( CKR_OK != *pError ) { michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: /* michael@0: * Sanity-check object michael@0: */ michael@0: michael@0: if (!fwObject) { michael@0: *pError = CKR_ARGUMENTS_BAD; michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: oldIsToken = nssCKFWObject_IsTokenObject(fwObject); michael@0: michael@0: newIsToken = oldIsToken; michael@0: for( i = 0; i < ulAttributeCount; i++ ) { michael@0: if( CKA_TOKEN == pTemplate[i].type ) { michael@0: /* Since we sanity-checked the object, we know this is the right size. */ michael@0: (void)nsslibc_memcpy(&newIsToken, pTemplate[i].pValue, sizeof(CK_BBOOL)); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * If the Module handles its session objects, or if both the new michael@0: * and old object are token objects, use CopyObject if it exists. michael@0: */ michael@0: michael@0: if ((fwSession->mdSession->CopyObject) && michael@0: (((CK_TRUE == oldIsToken) && (CK_TRUE == newIsToken)) || michael@0: (CK_TRUE == nssCKFWInstance_GetModuleHandlesSessionObjects( michael@0: fwSession->fwInstance))) ) { michael@0: /* use copy object */ michael@0: NSSArena *arena; michael@0: NSSCKMDObject *mdOldObject; michael@0: NSSCKMDObject *mdObject; michael@0: michael@0: mdOldObject = nssCKFWObject_GetMDObject(fwObject); michael@0: michael@0: if( CK_TRUE == newIsToken ) { michael@0: arena = nssCKFWToken_GetArena(fwSession->fwToken, pError); michael@0: } else { michael@0: arena = nssCKFWSession_GetArena(fwSession, pError); michael@0: } michael@0: if (!arena) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: mdObject = fwSession->mdSession->CopyObject(fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, michael@0: fwSession->mdInstance, fwSession->fwInstance, mdOldObject, michael@0: fwObject, arena, pTemplate, ulAttributeCount, pError); michael@0: if (!mdObject) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: rv = nssCKFWObject_Create(arena, mdObject, michael@0: newIsToken ? NULL : fwSession, michael@0: fwSession->fwToken, fwSession->fwInstance, pError); michael@0: michael@0: if( CK_FALSE == newIsToken ) { michael@0: if( CK_FALSE == nssCKFWHash_Exists(fwSession->sessionObjectHash, rv) ) { michael@0: *pError = nssCKFWHash_Add(fwSession->sessionObjectHash, rv, rv); michael@0: if( CKR_OK != *pError ) { michael@0: nssCKFWObject_Finalize(rv, PR_TRUE); michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return rv; michael@0: } else { michael@0: /* use create object */ michael@0: NSSArena *tmpArena; michael@0: CK_ATTRIBUTE_PTR newTemplate; michael@0: CK_ULONG i, j, n, newLength, k; michael@0: CK_ATTRIBUTE_TYPE_PTR oldTypes; michael@0: NSSCKFWObject *rv; michael@0: michael@0: n = nssCKFWObject_GetAttributeCount(fwObject, pError); michael@0: if( (0 == n) && (CKR_OK != *pError) ) { michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: tmpArena = NSSArena_Create(); michael@0: if (!tmpArena) { michael@0: *pError = CKR_HOST_MEMORY; michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: oldTypes = nss_ZNEWARRAY(tmpArena, CK_ATTRIBUTE_TYPE, n); michael@0: if( (CK_ATTRIBUTE_TYPE_PTR)NULL == oldTypes ) { michael@0: NSSArena_Destroy(tmpArena); michael@0: *pError = CKR_HOST_MEMORY; michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: *pError = nssCKFWObject_GetAttributeTypes(fwObject, oldTypes, n); michael@0: if( CKR_OK != *pError ) { michael@0: NSSArena_Destroy(tmpArena); michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: newLength = n; michael@0: for( i = 0; i < ulAttributeCount; i++ ) { michael@0: for( j = 0; j < n; j++ ) { michael@0: if( oldTypes[j] == pTemplate[i].type ) { michael@0: if( (CK_VOID_PTR)NULL == pTemplate[i].pValue ) { michael@0: /* Removing the attribute */ michael@0: newLength--; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: if( j == n ) { michael@0: /* Not found */ michael@0: newLength++; michael@0: } michael@0: } michael@0: michael@0: newTemplate = nss_ZNEWARRAY(tmpArena, CK_ATTRIBUTE, newLength); michael@0: if( (CK_ATTRIBUTE_PTR)NULL == newTemplate ) { michael@0: NSSArena_Destroy(tmpArena); michael@0: *pError = CKR_HOST_MEMORY; michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: k = 0; michael@0: for( j = 0; j < n; j++ ) { michael@0: for( i = 0; i < ulAttributeCount; i++ ) { michael@0: if( oldTypes[j] == pTemplate[i].type ) { michael@0: if( (CK_VOID_PTR)NULL == pTemplate[i].pValue ) { michael@0: /* This attribute is being deleted */ michael@0: ; michael@0: } else { michael@0: /* This attribute is being replaced */ michael@0: newTemplate[k].type = pTemplate[i].type; michael@0: newTemplate[k].pValue = pTemplate[i].pValue; michael@0: newTemplate[k].ulValueLen = pTemplate[i].ulValueLen; michael@0: k++; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: if( i == ulAttributeCount ) { michael@0: /* This attribute is being copied over from the old object */ michael@0: NSSItem item, *it; michael@0: item.size = 0; michael@0: item.data = (void *)NULL; michael@0: it = nssCKFWObject_GetAttribute(fwObject, oldTypes[j], michael@0: &item, tmpArena, pError); michael@0: if (!it) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: NSSArena_Destroy(tmpArena); michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: newTemplate[k].type = oldTypes[j]; michael@0: newTemplate[k].pValue = it->data; michael@0: newTemplate[k].ulValueLen = it->size; michael@0: k++; michael@0: } michael@0: } michael@0: /* assert that k == newLength */ michael@0: michael@0: rv = nssCKFWSession_CreateObject(fwSession, newTemplate, newLength, pError); michael@0: if (!rv) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: NSSArena_Destroy(tmpArena); michael@0: return (NSSCKFWObject *)NULL; michael@0: } michael@0: michael@0: NSSArena_Destroy(tmpArena); michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_FindObjectsInit michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT NSSCKFWFindObjects * michael@0: nssCKFWSession_FindObjectsInit michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_ATTRIBUTE_PTR pTemplate, michael@0: CK_ULONG ulAttributeCount, michael@0: CK_RV *pError michael@0: ) michael@0: { michael@0: NSSCKMDFindObjects *mdfo1 = (NSSCKMDFindObjects *)NULL; michael@0: NSSCKMDFindObjects *mdfo2 = (NSSCKMDFindObjects *)NULL; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if (!pError) { michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: michael@0: *pError = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != *pError ) { michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: michael@0: if( ((CK_ATTRIBUTE_PTR)NULL == pTemplate) && (ulAttributeCount != 0) ) { michael@0: *pError = CKR_ARGUMENTS_BAD; michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if( CK_TRUE != nssCKFWInstance_GetModuleHandlesSessionObjects( michael@0: fwSession->fwInstance) ) { michael@0: CK_ULONG i; michael@0: michael@0: /* michael@0: * Does the search criteria restrict us to token or session michael@0: * objects? michael@0: */ michael@0: michael@0: for( i = 0; i < ulAttributeCount; i++ ) { michael@0: if( CKA_TOKEN == pTemplate[i].type ) { michael@0: /* Yes, it does. */ michael@0: CK_BBOOL isToken; michael@0: if( sizeof(CK_BBOOL) != pTemplate[i].ulValueLen ) { michael@0: *pError = CKR_ATTRIBUTE_VALUE_INVALID; michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: (void)nsslibc_memcpy(&isToken, pTemplate[i].pValue, sizeof(CK_BBOOL)); michael@0: michael@0: if( CK_TRUE == isToken ) { michael@0: /* Pass it on to the module's search routine */ michael@0: if (!fwSession->mdSession->FindObjectsInit) { michael@0: goto wrap; michael@0: } michael@0: michael@0: mdfo1 = fwSession->mdSession->FindObjectsInit(fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, michael@0: fwSession->mdInstance, fwSession->fwInstance, michael@0: pTemplate, ulAttributeCount, pError); michael@0: } else { michael@0: /* Do the search ourselves */ michael@0: mdfo1 = nssCKMDFindSessionObjects_Create(fwSession->fwToken, michael@0: pTemplate, ulAttributeCount, pError); michael@0: } michael@0: michael@0: if (!mdfo1) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: michael@0: goto wrap; michael@0: } michael@0: } michael@0: michael@0: if( i == ulAttributeCount ) { michael@0: /* No, it doesn't. Do a hybrid search. */ michael@0: mdfo1 = fwSession->mdSession->FindObjectsInit(fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, michael@0: fwSession->mdInstance, fwSession->fwInstance, michael@0: pTemplate, ulAttributeCount, pError); michael@0: michael@0: if (!mdfo1) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: michael@0: mdfo2 = nssCKMDFindSessionObjects_Create(fwSession->fwToken, michael@0: pTemplate, ulAttributeCount, pError); michael@0: if (!mdfo2) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: if (mdfo1->Final) { michael@0: mdfo1->Final(mdfo1, (NSSCKFWFindObjects *)NULL, fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, michael@0: fwSession->mdInstance, fwSession->fwInstance); michael@0: } michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: michael@0: goto wrap; michael@0: } michael@0: /*NOTREACHED*/ michael@0: } else { michael@0: /* Module handles all its own objects. Pass on to module's search */ michael@0: mdfo1 = fwSession->mdSession->FindObjectsInit(fwSession->mdSession, michael@0: fwSession, fwSession->mdToken, fwSession->fwToken, michael@0: fwSession->mdInstance, fwSession->fwInstance, michael@0: pTemplate, ulAttributeCount, pError); michael@0: michael@0: if (!mdfo1) { michael@0: if( CKR_OK == *pError ) { michael@0: *pError = CKR_GENERAL_ERROR; michael@0: } michael@0: return (NSSCKFWFindObjects *)NULL; michael@0: } michael@0: michael@0: goto wrap; michael@0: } michael@0: michael@0: wrap: michael@0: return nssCKFWFindObjects_Create(fwSession, fwSession->fwToken, michael@0: fwSession->fwInstance, mdfo1, mdfo2, pError); michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_SeedRandom michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_SeedRandom michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSItem *seed michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!seed) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: if (!seed->data) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: if( 0 == seed->size ) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (!fwSession->mdSession->SeedRandom) { michael@0: return CKR_RANDOM_SEED_NOT_SUPPORTED; michael@0: } michael@0: michael@0: error = fwSession->mdSession->SeedRandom(fwSession->mdSession, fwSession, michael@0: fwSession->mdToken, fwSession->fwToken, fwSession->mdInstance, michael@0: fwSession->fwInstance, seed); michael@0: michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetRandom michael@0: * michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_GetRandom michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSItem *buffer michael@0: ) michael@0: { michael@0: CK_RV error = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!buffer) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: if (!buffer->data) { michael@0: return CKR_ARGUMENTS_BAD; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if (!fwSession->mdSession->GetRandom) { michael@0: if( CK_TRUE == nssCKFWToken_GetHasRNG(fwSession->fwToken) ) { michael@0: return CKR_GENERAL_ERROR; michael@0: } else { michael@0: return CKR_RANDOM_NO_RNG; michael@0: } michael@0: } michael@0: michael@0: if( 0 == buffer->size ) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: error = fwSession->mdSession->GetRandom(fwSession->mdSession, fwSession, michael@0: fwSession->mdToken, fwSession->fwToken, fwSession->mdInstance, michael@0: fwSession->fwInstance, buffer); michael@0: michael@0: return error; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * nssCKFWSession_SetCurrentCryptoOperation michael@0: */ michael@0: NSS_IMPLEMENT void michael@0: nssCKFWSession_SetCurrentCryptoOperation michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWCryptoOperation * fwOperation, michael@0: NSSCKFWCryptoOperationState state michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: CK_RV error = CKR_OK; michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return; michael@0: } michael@0: michael@0: if ( state >= NSSCKFWCryptoOperationState_Max) { michael@0: return; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: fwSession->fwOperationArray[state] = fwOperation; michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_GetCurrentCryptoOperation michael@0: */ michael@0: NSS_IMPLEMENT NSSCKFWCryptoOperation * michael@0: nssCKFWSession_GetCurrentCryptoOperation michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWCryptoOperationState state michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: CK_RV error = CKR_OK; michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return (NSSCKFWCryptoOperation *)NULL; michael@0: } michael@0: michael@0: if ( state >= NSSCKFWCryptoOperationState_Max) { michael@0: return (NSSCKFWCryptoOperation *)NULL; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return (NSSCKFWCryptoOperation *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: return fwSession->fwOperationArray[state]; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_Final michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_Final michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWCryptoOperationType type, michael@0: NSSCKFWCryptoOperationState state, michael@0: CK_BYTE_PTR outBuf, michael@0: CK_ULONG_PTR outBufLen michael@0: ) michael@0: { michael@0: NSSCKFWCryptoOperation *fwOperation; michael@0: NSSItem outputBuffer; michael@0: CK_RV error = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: /* make sure we have a valid operation initialized */ michael@0: fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession, state); michael@0: if (!fwOperation) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: /* make sure it's the correct type */ michael@0: if (type != nssCKFWCryptoOperation_GetType(fwOperation)) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: /* handle buffer issues, note for Verify, the type is an input buffer. */ michael@0: if (NSSCKFWCryptoOperationType_Verify == type) { michael@0: if ((CK_BYTE_PTR)NULL == outBuf) { michael@0: error = CKR_ARGUMENTS_BAD; michael@0: goto done; michael@0: } michael@0: } else { michael@0: CK_ULONG len = nssCKFWCryptoOperation_GetFinalLength(fwOperation, &error); michael@0: CK_ULONG maxBufLen = *outBufLen; michael@0: michael@0: if (CKR_OK != error) { michael@0: goto done; michael@0: } michael@0: *outBufLen = len; michael@0: if ((CK_BYTE_PTR)NULL == outBuf) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: if (len > maxBufLen) { michael@0: return CKR_BUFFER_TOO_SMALL; michael@0: } michael@0: } michael@0: outputBuffer.data = outBuf; michael@0: outputBuffer.size = *outBufLen; michael@0: michael@0: error = nssCKFWCryptoOperation_Final(fwOperation, &outputBuffer); michael@0: done: michael@0: if (CKR_BUFFER_TOO_SMALL == error) { michael@0: return error; michael@0: } michael@0: /* clean up our state */ michael@0: nssCKFWCryptoOperation_Destroy(fwOperation); michael@0: nssCKFWSession_SetCurrentCryptoOperation(fwSession, NULL, state); michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_Update michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_Update michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWCryptoOperationType type, michael@0: NSSCKFWCryptoOperationState state, michael@0: CK_BYTE_PTR inBuf, michael@0: CK_ULONG inBufLen, michael@0: CK_BYTE_PTR outBuf, michael@0: CK_ULONG_PTR outBufLen michael@0: ) michael@0: { michael@0: NSSCKFWCryptoOperation *fwOperation; michael@0: NSSItem inputBuffer; michael@0: NSSItem outputBuffer; michael@0: CK_ULONG len; michael@0: CK_ULONG maxBufLen; michael@0: CK_RV error = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: /* make sure we have a valid operation initialized */ michael@0: fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession, state); michael@0: if (!fwOperation) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: /* make sure it's the correct type */ michael@0: if (type != nssCKFWCryptoOperation_GetType(fwOperation)) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: inputBuffer.data = inBuf; michael@0: inputBuffer.size = inBufLen; michael@0: michael@0: /* handle buffer issues, note for Verify, the type is an input buffer. */ michael@0: len = nssCKFWCryptoOperation_GetOperationLength(fwOperation, &inputBuffer, michael@0: &error); michael@0: if (CKR_OK != error) { michael@0: return error; michael@0: } michael@0: maxBufLen = *outBufLen; michael@0: michael@0: *outBufLen = len; michael@0: if ((CK_BYTE_PTR)NULL == outBuf) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: if (len > maxBufLen) { michael@0: return CKR_BUFFER_TOO_SMALL; michael@0: } michael@0: outputBuffer.data = outBuf; michael@0: outputBuffer.size = *outBufLen; michael@0: michael@0: return nssCKFWCryptoOperation_Update(fwOperation, michael@0: &inputBuffer, &outputBuffer); michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_DigestUpdate michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_DigestUpdate michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWCryptoOperationType type, michael@0: NSSCKFWCryptoOperationState state, michael@0: CK_BYTE_PTR inBuf, michael@0: CK_ULONG inBufLen michael@0: ) michael@0: { michael@0: NSSCKFWCryptoOperation *fwOperation; michael@0: NSSItem inputBuffer; michael@0: CK_RV error = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: /* make sure we have a valid operation initialized */ michael@0: fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession, state); michael@0: if (!fwOperation) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: /* make sure it's the correct type */ michael@0: if (type != nssCKFWCryptoOperation_GetType(fwOperation)) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: inputBuffer.data = inBuf; michael@0: inputBuffer.size = inBufLen; michael@0: michael@0: michael@0: error = nssCKFWCryptoOperation_DigestUpdate(fwOperation, &inputBuffer); michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_DigestUpdate michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_DigestKey michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWObject *fwKey michael@0: ) michael@0: { michael@0: NSSCKFWCryptoOperation *fwOperation; michael@0: NSSItem *inputBuffer; michael@0: CK_RV error = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: /* make sure we have a valid operation initialized */ michael@0: fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession, michael@0: NSSCKFWCryptoOperationState_Digest); michael@0: if (!fwOperation) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: /* make sure it's the correct type */ michael@0: if (NSSCKFWCryptoOperationType_Digest != michael@0: nssCKFWCryptoOperation_GetType(fwOperation)) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: error = nssCKFWCryptoOperation_DigestKey(fwOperation, fwKey); michael@0: if (CKR_FUNCTION_FAILED != error) { michael@0: return error; michael@0: } michael@0: michael@0: /* no machine depended way for this to happen, do it by hand */ michael@0: inputBuffer=nssCKFWObject_GetAttribute(fwKey, CKA_VALUE, NULL, NULL, &error); michael@0: if (!inputBuffer) { michael@0: /* couldn't get the value, just fail then */ michael@0: return error; michael@0: } michael@0: error = nssCKFWCryptoOperation_DigestUpdate(fwOperation, inputBuffer); michael@0: nssItem_Destroy(inputBuffer); michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * nssCKFWSession_UpdateFinal michael@0: */ michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_UpdateFinal michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWCryptoOperationType type, michael@0: NSSCKFWCryptoOperationState state, michael@0: CK_BYTE_PTR inBuf, michael@0: CK_ULONG inBufLen, michael@0: CK_BYTE_PTR outBuf, michael@0: CK_ULONG_PTR outBufLen michael@0: ) michael@0: { michael@0: NSSCKFWCryptoOperation *fwOperation; michael@0: NSSItem inputBuffer; michael@0: NSSItem outputBuffer; michael@0: PRBool isEncryptDecrypt; michael@0: CK_RV error = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: /* make sure we have a valid operation initialized */ michael@0: fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession, state); michael@0: if (!fwOperation) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: /* make sure it's the correct type */ michael@0: if (type != nssCKFWCryptoOperation_GetType(fwOperation)) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: inputBuffer.data = inBuf; michael@0: inputBuffer.size = inBufLen; michael@0: isEncryptDecrypt = (PRBool) ((NSSCKFWCryptoOperationType_Encrypt == type) || michael@0: (NSSCKFWCryptoOperationType_Decrypt == type)) ; michael@0: michael@0: /* handle buffer issues, note for Verify, the type is an input buffer. */ michael@0: if (NSSCKFWCryptoOperationType_Verify == type) { michael@0: if ((CK_BYTE_PTR)NULL == outBuf) { michael@0: error = CKR_ARGUMENTS_BAD; michael@0: goto done; michael@0: } michael@0: } else { michael@0: CK_ULONG maxBufLen = *outBufLen; michael@0: CK_ULONG len; michael@0: michael@0: len = (isEncryptDecrypt) ? michael@0: nssCKFWCryptoOperation_GetOperationLength(fwOperation, michael@0: &inputBuffer, &error) : michael@0: nssCKFWCryptoOperation_GetFinalLength(fwOperation, &error); michael@0: michael@0: if (CKR_OK != error) { michael@0: goto done; michael@0: } michael@0: michael@0: *outBufLen = len; michael@0: if ((CK_BYTE_PTR)NULL == outBuf) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: if (len > maxBufLen) { michael@0: return CKR_BUFFER_TOO_SMALL; michael@0: } michael@0: } michael@0: outputBuffer.data = outBuf; michael@0: outputBuffer.size = *outBufLen; michael@0: michael@0: error = nssCKFWCryptoOperation_UpdateFinal(fwOperation, michael@0: &inputBuffer, &outputBuffer); michael@0: michael@0: /* UpdateFinal isn't support, manually use Update and Final */ michael@0: if (CKR_FUNCTION_FAILED == error) { michael@0: error = isEncryptDecrypt ? michael@0: nssCKFWCryptoOperation_Update(fwOperation, &inputBuffer, &outputBuffer) : michael@0: nssCKFWCryptoOperation_DigestUpdate(fwOperation, &inputBuffer); michael@0: michael@0: if (CKR_OK == error) { michael@0: error = nssCKFWCryptoOperation_Final(fwOperation, &outputBuffer); michael@0: } michael@0: } michael@0: michael@0: michael@0: done: michael@0: if (CKR_BUFFER_TOO_SMALL == error) { michael@0: /* if we return CKR_BUFFER_TOO_SMALL, we the caller is not expecting. michael@0: * the crypto state to be freed */ michael@0: return error; michael@0: } michael@0: michael@0: /* clean up our state */ michael@0: nssCKFWCryptoOperation_Destroy(fwOperation); michael@0: nssCKFWSession_SetCurrentCryptoOperation(fwSession, NULL, state); michael@0: return error; michael@0: } michael@0: michael@0: NSS_IMPLEMENT CK_RV michael@0: nssCKFWSession_UpdateCombo michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWCryptoOperationType encryptType, michael@0: NSSCKFWCryptoOperationType digestType, michael@0: NSSCKFWCryptoOperationState digestState, michael@0: CK_BYTE_PTR inBuf, michael@0: CK_ULONG inBufLen, michael@0: CK_BYTE_PTR outBuf, michael@0: CK_ULONG_PTR outBufLen michael@0: ) michael@0: { michael@0: NSSCKFWCryptoOperation *fwOperation; michael@0: NSSCKFWCryptoOperation *fwPeerOperation; michael@0: NSSItem inputBuffer; michael@0: NSSItem outputBuffer; michael@0: CK_ULONG maxBufLen = *outBufLen; michael@0: CK_ULONG len; michael@0: CK_RV error = CKR_OK; michael@0: michael@0: #ifdef NSSDEBUG michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: michael@0: if (!fwSession->mdSession) { michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: /* make sure we have a valid operation initialized */ michael@0: fwOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession, michael@0: NSSCKFWCryptoOperationState_EncryptDecrypt); michael@0: if (!fwOperation) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: /* make sure it's the correct type */ michael@0: if (encryptType != nssCKFWCryptoOperation_GetType(fwOperation)) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: /* make sure we have a valid operation initialized */ michael@0: fwPeerOperation = nssCKFWSession_GetCurrentCryptoOperation(fwSession, michael@0: digestState); michael@0: if (!fwPeerOperation) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: /* make sure it's the correct type */ michael@0: if (digestType != nssCKFWCryptoOperation_GetType(fwOperation)) { michael@0: return CKR_OPERATION_NOT_INITIALIZED; michael@0: } michael@0: michael@0: inputBuffer.data = inBuf; michael@0: inputBuffer.size = inBufLen; michael@0: len = nssCKFWCryptoOperation_GetOperationLength(fwOperation, michael@0: &inputBuffer, &error); michael@0: if (CKR_OK != error) { michael@0: return error; michael@0: } michael@0: michael@0: *outBufLen = len; michael@0: if ((CK_BYTE_PTR)NULL == outBuf) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: if (len > maxBufLen) { michael@0: return CKR_BUFFER_TOO_SMALL; michael@0: } michael@0: michael@0: outputBuffer.data = outBuf; michael@0: outputBuffer.size = *outBufLen; michael@0: michael@0: error = nssCKFWCryptoOperation_UpdateCombo(fwOperation, fwPeerOperation, michael@0: &inputBuffer, &outputBuffer); michael@0: if (CKR_FUNCTION_FAILED == error) { michael@0: PRBool isEncrypt = michael@0: (PRBool) (NSSCKFWCryptoOperationType_Encrypt == encryptType); michael@0: michael@0: if (isEncrypt) { michael@0: error = nssCKFWCryptoOperation_DigestUpdate(fwPeerOperation, michael@0: &inputBuffer); michael@0: if (CKR_OK != error) { michael@0: return error; michael@0: } michael@0: } michael@0: error = nssCKFWCryptoOperation_Update(fwOperation, michael@0: &inputBuffer, &outputBuffer); michael@0: if (CKR_OK != error) { michael@0: return error; michael@0: } michael@0: if (!isEncrypt) { michael@0: error = nssCKFWCryptoOperation_DigestUpdate(fwPeerOperation, michael@0: &outputBuffer); michael@0: } michael@0: } michael@0: return error; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * NSSCKFWSession_GetMDSession michael@0: * michael@0: */ michael@0: michael@0: NSS_IMPLEMENT NSSCKMDSession * michael@0: NSSCKFWSession_GetMDSession michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: #ifdef DEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return (NSSCKMDSession *)NULL; michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: return nssCKFWSession_GetMDSession(fwSession); michael@0: } michael@0: michael@0: /* michael@0: * NSSCKFWSession_GetArena michael@0: * michael@0: */ michael@0: michael@0: NSS_IMPLEMENT NSSArena * michael@0: NSSCKFWSession_GetArena michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_RV *pError michael@0: ) michael@0: { michael@0: #ifdef DEBUG michael@0: if (!pError) { michael@0: return (NSSArena *)NULL; michael@0: } michael@0: michael@0: *pError = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != *pError ) { michael@0: return (NSSArena *)NULL; michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: return nssCKFWSession_GetArena(fwSession, pError); michael@0: } michael@0: michael@0: /* michael@0: * NSSCKFWSession_CallNotification michael@0: * michael@0: */ michael@0: michael@0: NSS_IMPLEMENT CK_RV michael@0: NSSCKFWSession_CallNotification michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: CK_NOTIFICATION event michael@0: ) michael@0: { michael@0: #ifdef DEBUG michael@0: CK_RV error = CKR_OK; michael@0: michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return error; michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: return nssCKFWSession_CallNotification(fwSession, event); michael@0: } michael@0: michael@0: /* michael@0: * NSSCKFWSession_IsRWSession michael@0: * michael@0: */ michael@0: michael@0: NSS_IMPLEMENT CK_BBOOL michael@0: NSSCKFWSession_IsRWSession michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: #ifdef DEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return CK_FALSE; michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: return nssCKFWSession_IsRWSession(fwSession); michael@0: } michael@0: michael@0: /* michael@0: * NSSCKFWSession_IsSO michael@0: * michael@0: */ michael@0: michael@0: NSS_IMPLEMENT CK_BBOOL michael@0: NSSCKFWSession_IsSO michael@0: ( michael@0: NSSCKFWSession *fwSession michael@0: ) michael@0: { michael@0: #ifdef DEBUG michael@0: if( CKR_OK != nssCKFWSession_verifyPointer(fwSession) ) { michael@0: return CK_FALSE; michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: return nssCKFWSession_IsSO(fwSession); michael@0: } michael@0: michael@0: NSS_IMPLEMENT NSSCKFWCryptoOperation * michael@0: NSSCKFWSession_GetCurrentCryptoOperation michael@0: ( michael@0: NSSCKFWSession *fwSession, michael@0: NSSCKFWCryptoOperationState state michael@0: ) michael@0: { michael@0: #ifdef DEBUG michael@0: CK_RV error = CKR_OK; michael@0: error = nssCKFWSession_verifyPointer(fwSession); michael@0: if( CKR_OK != error ) { michael@0: return (NSSCKFWCryptoOperation *)NULL; michael@0: } michael@0: michael@0: if ( state >= NSSCKFWCryptoOperationState_Max) { michael@0: return (NSSCKFWCryptoOperation *)NULL; michael@0: } michael@0: #endif /* DEBUG */ michael@0: return nssCKFWSession_GetCurrentCryptoOperation(fwSession, state); michael@0: }