wrap.c

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

C
3,172
字号
/*  * 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: wrap.c,v $ $Revision: 1.3 $ $Date: 2000/09/06 22:23:57 $ $Name: NSS_3_1_1_RTM $";#endif /* DEBUG *//* * wrap.c * * This file contains the routines that actually implement the cryptoki * API, using the internal APIs of the NSS Cryptoki Framework.  There is * one routine here for every cryptoki routine.  For linking reasons * the actual entry points passed back with C_GetFunctionList have to * exist in one of the Module's source files; however, those are merely * simple wrappers that call these routines.  The intelligence of the * implementations is here. */#ifndef CK_T#include "ck.h"#endif /* CK_T *//* * NSSCKFWC_Initialize * NSSCKFWC_Finalize * NSSCKFWC_GetInfo * -- NSSCKFWC_GetFunctionList -- see the API insert file * NSSCKFWC_GetSlotList * NSSCKFWC_GetSlotInfo * NSSCKFWC_GetTokenInfo * NSSCKFWC_WaitForSlotEvent * NSSCKFWC_GetMechanismList * NSSCKFWC_GetMechanismInfo * NSSCKFWC_InitToken * NSSCKFWC_InitPIN * NSSCKFWC_SetPIN * NSSCKFWC_OpenSession * NSSCKFWC_CloseSession * NSSCKFWC_CloseAllSessions * NSSCKFWC_GetSessionInfo * NSSCKFWC_GetOperationState * NSSCKFWC_SetOperationState * NSSCKFWC_Login * NSSCKFWC_Logout * NSSCKFWC_CreateObject * NSSCKFWC_CopyObject * NSSCKFWC_DestroyObject * NSSCKFWC_GetObjectSize * NSSCKFWC_GetAttributeValue * NSSCKFWC_SetAttributeValue * NSSCKFWC_FindObjectsInit * NSSCKFWC_FindObjects * NSSCKFWC_FindObjectsFinal * NSSCKFWC_EncryptInit * NSSCKFWC_Encrypt * NSSCKFWC_EncryptUpdate * NSSCKFWC_EncryptFinal * NSSCKFWC_DecryptInit * NSSCKFWC_Decrypt * NSSCKFWC_DecryptUpdate * NSSCKFWC_DecryptFinal * NSSCKFWC_DigestInit * NSSCKFWC_Digest * NSSCKFWC_DigestUpdate * NSSCKFWC_DigestKey * NSSCKFWC_DigestFinal * NSSCKFWC_SignInit * NSSCKFWC_Sign * NSSCKFWC_SignUpdate * NSSCKFWC_SignFinal * NSSCKFWC_SignRecoverInit * NSSCKFWC_SignRecover * NSSCKFWC_VerifyInit * NSSCKFWC_Verify * NSSCKFWC_VerifyUpdate * NSSCKFWC_VerifyFinal * NSSCKFWC_VerifyRecoverInit * NSSCKFWC_VerifyRecover * NSSCKFWC_DigestEncryptUpdate * NSSCKFWC_DecryptDigestUpdate * NSSCKFWC_SignEncryptUpdate * NSSCKFWC_DecryptVerifyUpdate * NSSCKFWC_GenerateKey * NSSCKFWC_GenerateKeyPair * NSSCKFWC_WrapKey * NSSCKFWC_UnwrapKey * NSSCKFWC_DeriveKey * NSSCKFWC_SeedRandom * NSSCKFWC_GenerateRandom * NSSCKFWC_GetFunctionStatus * NSSCKFWC_CancelFunction *//* * NSSCKFWC_Initialize * */NSS_IMPLEMENT CK_RVNSSCKFWC_Initialize(  NSSCKFWInstance **pFwInstance,  NSSCKMDInstance *mdInstance,  CK_VOID_PTR pInitArgs){  CK_RV error = CKR_OK;  if( (NSSCKFWInstance **)NULL == pFwInstance ) {    error = CKR_GENERAL_ERROR;    goto loser;  }  if( (NSSCKFWInstance *)NULL != *pFwInstance ) {    error = CKR_CRYPTOKI_ALREADY_INITIALIZED;    goto loser;  }  if( (NSSCKMDInstance *)NULL == mdInstance ) {    error = CKR_GENERAL_ERROR;    goto loser;  }  /* remember the locking args for those times we need to get a lock in code   * outside the framework.   */  nssSetLockArgs(pInitArgs);  *pFwInstance = nssCKFWInstance_Create(pInitArgs, mdInstance, &error);  if( (NSSCKFWInstance *)NULL == *pFwInstance ) {    goto loser;  }  return CKR_OK; loser:  switch( error ) {  case CKR_ARGUMENTS_BAD:  case CKR_CANT_LOCK:  case CKR_CRYPTOKI_ALREADY_INITIALIZED:  case CKR_FUNCTION_FAILED:  case CKR_GENERAL_ERROR:  case CKR_HOST_MEMORY:  case CKR_NEED_TO_CREATE_THREADS:    break;  default:  case CKR_OK:    error = CKR_GENERAL_ERROR;    break;  }  return error;}/* * NSSCKFWC_Finalize * */NSS_IMPLEMENT CK_RVNSSCKFWC_Finalize(  NSSCKFWInstance **pFwInstance){  CK_RV error = CKR_OK;  if( (NSSCKFWInstance **)NULL == pFwInstance ) {    error = CKR_GENERAL_ERROR;    goto loser;  }  if( (NSSCKFWInstance *)NULL == *pFwInstance ) {    error = CKR_CRYPTOKI_NOT_INITIALIZED;    goto loser;  }  error = nssCKFWInstance_Destroy(*pFwInstance);  /* In any case */  *pFwInstance = (NSSCKFWInstance *)NULL; loser:  switch( error ) {  case CKR_CRYPTOKI_NOT_INITIALIZED:  case CKR_FUNCTION_FAILED:  case CKR_GENERAL_ERROR:  case CKR_HOST_MEMORY:  case CKR_OK:    break;  default:    error = CKR_GENERAL_ERROR;    break;  }  return error;}/* * NSSCKFWC_GetInfo * */NSS_IMPLEMENT CK_RVNSSCKFWC_GetInfo(  NSSCKFWInstance *fwInstance,  CK_INFO_PTR pInfo){  CK_RV error = CKR_OK;  if( (CK_INFO_PTR)CK_NULL_PTR == pInfo ) {    error = CKR_ARGUMENTS_BAD;    goto loser;  }  /*   * A purify error here means a caller error   */  (void)nsslibc_memset(pInfo, 0, sizeof(CK_INFO));  pInfo->cryptokiVersion = nssCKFWInstance_GetCryptokiVersion(fwInstance);  error = nssCKFWInstance_GetManufacturerID(fwInstance, pInfo->manufacturerID);  if( CKR_OK != error ) {    goto loser;  }  pInfo->flags = nssCKFWInstance_GetFlags(fwInstance);  error = nssCKFWInstance_GetLibraryDescription(fwInstance, pInfo->libraryDescription);  if( CKR_OK != error ) {    goto loser;  }  pInfo->libraryVersion = nssCKFWInstance_GetLibraryVersion(fwInstance);  return CKR_OK; loser:  switch( error ) {  case CKR_CRYPTOKI_NOT_INITIALIZED:  case CKR_FUNCTION_FAILED:  case CKR_GENERAL_ERROR:  case CKR_HOST_MEMORY:    break;  default:    error = CKR_GENERAL_ERROR;    break;  }  return error;}  /* * C_GetFunctionList is implemented entirely in the Module's file which * includes the Framework API insert file.  It requires no "actual" * NSSCKFW routine. *//* * NSSCKFWC_GetSlotList * */NSS_IMPLEMENT CK_RVNSSCKFWC_GetSlotList(  NSSCKFWInstance *fwInstance,  CK_BBOOL tokenPresent,  CK_SLOT_ID_PTR pSlotList,  CK_ULONG_PTR pulCount){  CK_RV error = CKR_OK;  CK_ULONG nSlots;  if( (NSSCKFWInstance *)NULL == fwInstance ) {    error = CKR_CRYPTOKI_NOT_INITIALIZED;    goto loser;  }  switch( tokenPresent ) {  case CK_TRUE:  case CK_FALSE:    break;  default:    error = CKR_ARGUMENTS_BAD;    goto loser;  }  if( (CK_ULONG_PTR)CK_NULL_PTR == pulCount ) {    error = CKR_ARGUMENTS_BAD;    goto loser;  }  nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error);  if( (CK_ULONG)0 == nSlots ) {    goto loser;  }  if( (CK_SLOT_ID_PTR)CK_NULL_PTR == pSlotList ) {    *pulCount = nSlots;    return CKR_OK;  }       /*   * A purify error here indicates caller error.   */  (void)nsslibc_memset(pSlotList, 0, *pulCount * sizeof(CK_SLOT_ID));  if( *pulCount < nSlots ) {    *pulCount = nSlots;    error = CKR_BUFFER_TOO_SMALL;    goto loser;  } else {    CK_ULONG i;    *pulCount = nSlots;        /*      * Our secret "mapping": CK_SLOT_IDs are integers [1,N], and we     * just index one when we need it.     */    for( i = 0; i < nSlots; i++ ) {      pSlotList[i] = i+1;    }    return CKR_OK;  } loser:  switch( error ) {  case CKR_BUFFER_TOO_SMALL:  case CKR_CRYPTOKI_NOT_INITIALIZED:  case CKR_FUNCTION_FAILED:  case CKR_GENERAL_ERROR:  case CKR_HOST_MEMORY:    break;  default:  case CKR_OK:    error = CKR_GENERAL_ERROR;    break;  }  return error;} /* * NSSCKFWC_GetSlotInfo * */NSS_IMPLEMENT CK_RVNSSCKFWC_GetSlotInfo(  NSSCKFWInstance *fwInstance,  CK_SLOT_ID slotID,  CK_SLOT_INFO_PTR pInfo){  CK_RV error = CKR_OK;  CK_ULONG nSlots;  NSSCKFWSlot **slots;  NSSCKFWSlot *fwSlot;  if( (NSSCKFWInstance *)NULL == fwInstance ) {    error = CKR_CRYPTOKI_NOT_INITIALIZED;    goto loser;  }  nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error);  if( (CK_ULONG)0 == nSlots ) {    goto loser;  }  if( (slotID < 1) || (slotID > nSlots) ) {    error = CKR_SLOT_ID_INVALID;    goto loser;  }  if( (CK_SLOT_INFO_PTR)CK_NULL_PTR == pInfo ) {    error = CKR_ARGUMENTS_BAD;    goto loser;  }  /*   * A purify error here indicates caller error.   */  (void)nsslibc_memset(pInfo, 0, sizeof(CK_SLOT_INFO));  slots = nssCKFWInstance_GetSlots(fwInstance, &error);  if( (NSSCKFWSlot **)NULL == slots ) {    goto loser;  }  fwSlot = slots[ slotID-1 ];  error = nssCKFWSlot_GetSlotDescription(fwSlot, pInfo->slotDescription);  if( CKR_OK != error ) {    goto loser;  }  error = nssCKFWSlot_GetManufacturerID(fwSlot, pInfo->manufacturerID);  if( CKR_OK != error ) {    goto loser;  }  if( nssCKFWSlot_GetTokenPresent(fwSlot) ) {    pInfo->flags |= CKF_TOKEN_PRESENT;  }  if( nssCKFWSlot_GetRemovableDevice(fwSlot) ) {    pInfo->flags |= CKF_REMOVABLE_DEVICE;  }  if( nssCKFWSlot_GetHardwareSlot(fwSlot) ) {    pInfo->flags |= CKF_HW_SLOT;  }  pInfo->hardwareVersion = nssCKFWSlot_GetHardwareVersion(fwSlot);  pInfo->firmwareVersion = nssCKFWSlot_GetFirmwareVersion(fwSlot);  return CKR_OK; loser:  switch( error ) {  case CKR_CRYPTOKI_NOT_INITIALIZED:  case CKR_DEVICE_ERROR:  case CKR_FUNCTION_FAILED:  case CKR_GENERAL_ERROR:  case CKR_HOST_MEMORY:  case CKR_SLOT_ID_INVALID:    break;  default:  case CKR_OK:    error = CKR_GENERAL_ERROR;  }  return error;}/* * NSSCKFWC_GetTokenInfo * */NSS_IMPLEMENT CK_RVNSSCKFWC_GetTokenInfo(  NSSCKFWInstance *fwInstance,  CK_SLOT_ID slotID,  CK_TOKEN_INFO_PTR pInfo){  CK_RV error = CKR_OK;  CK_ULONG nSlots;  NSSCKFWSlot **slots;  NSSCKFWSlot *fwSlot;  NSSCKFWToken *fwToken = (NSSCKFWToken *)NULL;  if( (NSSCKFWInstance *)NULL == fwInstance ) {    error = CKR_CRYPTOKI_NOT_INITIALIZED;    goto loser;  }  nSlots = nssCKFWInstance_GetNSlots(fwInstance, &error);  if( (CK_ULONG)0 == nSlots ) {    goto loser;  }  if( (slotID < 1) || (slotID > nSlots) ) {    error = CKR_SLOT_ID_INVALID;    goto loser;  }  if( (CK_TOKEN_INFO_PTR)CK_NULL_PTR == pInfo ) {    error = CKR_ARGUMENTS_BAD;    goto loser;  }  /*   * A purify error here indicates caller error.   */  (void)nsslibc_memset(pInfo, 0, sizeof(CK_TOKEN_INFO));  slots = nssCKFWInstance_GetSlots(fwInstance, &error);  if( (NSSCKFWSlot **)NULL == slots ) {    goto loser;  }  fwSlot = slots[ slotID-1 ];  if( CK_TRUE != nssCKFWSlot_GetTokenPresent(fwSlot) ) {    error = CKR_TOKEN_NOT_PRESENT;    goto loser;  }  fwToken = nssCKFWSlot_GetToken(fwSlot, &error);  if( (NSSCKFWToken *)NULL == fwToken ) {    goto loser;  }  error = nssCKFWToken_GetLabel(fwToken, pInfo->label);  if( CKR_OK != error ) {    goto loser;  }  error = nssCKFWToken_GetManufacturerID(fwToken, pInfo->manufacturerID);  if( CKR_OK != error ) {    goto loser;  }  error = nssCKFWToken_GetModel(fwToken, pInfo->model);  if( CKR_OK != error ) {    goto loser;  }  error = nssCKFWToken_GetSerialNumber(fwToken, pInfo->serialNumber);  if( CKR_OK != error ) {    goto loser;  }  if( nssCKFWToken_GetHasRNG(fwToken) ) {    pInfo->flags |= CKF_RNG;  }  if( nssCKFWToken_GetIsWriteProtected(fwToken) ) {    pInfo->flags |= CKF_WRITE_PROTECTED;  }  if( nssCKFWToken_GetLoginRequired(fwToken) ) {    pInfo->flags |= CKF_LOGIN_REQUIRED;  }  if( nssCKFWToken_GetUserPinInitialized(fwToken) ) {    pInfo->flags |= CKF_USER_PIN_INITIALIZED;  }  if( nssCKFWToken_GetRestoreKeyNotNeeded(fwToken) ) {    pInfo->flags |= CKF_RESTORE_KEY_NOT_NEEDED;  }  if( nssCKFWToken_GetHasClockOnToken(fwToken) ) {    pInfo->flags |= CKF_CLOCK_ON_TOKEN;  }  if( nssCKFWToken_GetHasProtectedAuthenticationPath(fwToken) ) {    pInfo->flags |= CKF_PROTECTED_AUTHENTICATION_PATH;  }  if( nssCKFWToken_GetSupportsDualCryptoOperations(fwToken) ) {    pInfo->flags |= CKF_DUAL_CRYPTO_OPERATIONS;  }  pInfo->ulMaxSessionCount = nssCKFWToken_GetMaxSessionCount(fwToken);  pInfo->ulSessionCount = nssCKFWToken_GetSessionCount(fwToken);  pInfo->ulMaxRwSessionCount = nssCKFWToken_GetMaxRwSessionCount(fwToken);  pInfo->ulRwSessionCount= nssCKFWToken_GetRwSessionCount(fwToken);  pInfo->ulMaxPinLen = nssCKFWToken_GetMaxPinLen(fwToken);  pInfo->ulMinPinLen = nssCKFWToken_GetMinPinLen(fwToken);  pInfo->ulTotalPublicMemory = nssCKFWToken_GetTotalPublicMemory(fwToken);  pInfo->ulFreePublicMemory = nssCKFWToken_GetFreePublicMemory(fwToken);  pInfo->ulTotalPrivateMemory = nssCKFWToken_GetTotalPrivateMemory(fwToken);  pInfo->ulFreePrivateMemory = nssCKFWToken_GetFreePrivateMemory(fwToken);  pInfo->hardwareVersion = nssCKFWToken_GetHardwareVersion(fwToken);  pInfo->firmwareVersion = nssCKFWToken_GetFirmwareVersion(fwToken);    error = nssCKFWToken_GetUTCTime(fwToken, pInfo->utcTime);  if( CKR_OK != error ) {    goto loser;  }  return CKR_OK; loser:  switch( error ) {  case CKR_DEVICE_REMOVED:  case CKR_TOKEN_NOT_PRESENT:    (void)nssCKFWToken_Destroy(fwToken);    break;  case CKR_CRYPTOKI_NOT_INITIALIZED:  case CKR_DEVICE_ERROR:  case CKR_DEVICE_MEMORY:  case CKR_FUNCTION_FAILED:  case CKR_GENERAL_ERROR:  case CKR_HOST_MEMORY:  case CKR_SLOT_ID_INVALID:  case CKR_TOKEN_NOT_RECOGNIZED:    break;  default:  case CKR_OK:    error = CKR_GENERAL_ERROR;    break;  }  return error;}/* * NSSCKFWC_WaitForSlotEvent * */NSS_IMPLEMENT CK_RVNSSCKFWC_WaitForSlotEvent(  NSSCKFWInstance *fwInstance,  CK_FLAGS flags,  CK_SLOT_ID_PTR pSlot,

⌨️ 快捷键说明

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