⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 host_api.c

📁 IBM的Linux上的PKCS#11实现
💻 C
📖 第 1 页 / 共 5 页
字号:
#endif   WaitForSingleObject( pkcs_mutex, INFINITE );      // Handle global initialization issues first if we have not   // been initialized.   if (Initialized() == FALSE){      memset( &leeds_id, 0, sizeof(leeds_id) );      leeds_id.DeveloperID[0] = PKCS_11_DEVELOPER_ID;      leeds_id.DeveloperID[1] = 0x00;      memcpy( leeds_id.ProgramID,              PKCS_11_PRG_ID,              sizeof(leeds_id.ProgramID) );      leeds_id.Version[0]  = PKCS_11_VERSION;      leeds_id.Instance[0] = PKCS_11_INSTANCE;      leeds_id.Queue[0]    = PKCS_11_QUEUE;#ifdef DEBUGON      loginit();#endif      // Zero out the adapter handle array      //  an adapter handle of 0 indicates that the particular adapter has      // NOT been initialized      bzero((char *)adapter_handle,sizeof(sccAdapterHandle_t)*MAX_SLOT_ID);      initialized = TRUE;      initedpid = getpid();      SC_SetFunctionList();       bzero((char *)correlator_init,PKW_MAX_DEVICES * sizeof(struct Cor_init));      {         int i;         for (i=0;i<PKW_MAX_DEVICES;i++){            pid_list[i] = 0x00;         }      }   }#ifndef SOCKET   {  // Take the correlator as the minor number of the device we want to use  // check the correlator_init array to determine if the slot has been  // initialized already.  //  // Correlators start with 0 - N being the number of adapters -1.  // Right now we do no additional validation of the usage.  It is possible  // that non-initialized correlators can be accessed.  It is unlikely.  //  int i;      i = (int)atoi((char *)Correlator);      if ( correlator_init[i].init != 0 ){         (*FunctionList) = NULL;         ReleaseMutex(pkcs_mutex);         return CKR_FUNCTION_FAILED;      } else {         // Open the file for the adapter here..         // The open will fail if the adapter does not exist           rc = sccOpenAdapter( i, &adapter_handle[i] );         if (rc == 0 ){            correlator_init[i].init = 1; // Mark in use            correlator_init[i].API_Slotid = SlotNumber;         } else {            (*FunctionList) = NULL;            ReleaseMutex(pkcs_mutex);            return CKR_FUNCTION_FAILED;         }      }      usage_count++;   }#else   {  // Take the correlator as the minor number of the device we want to use  // check the correlator_init array to determine if the slot has been  // initialized already.  //  // Correlators start with 0 - N being the number of adapters -1.  // Right now we do no additional validation of the usage.  It is possible  // that non-initialized correlators can be accessed.  It is unlikely.  //   int i;      i = atoi(Correlator);      if ( correlator_init[i].init != 0 ){         (*FunctionList) = NULL;         ReleaseMutex(pkcs_mutex);         return CKR_FUNCTION_FAILED;      } else {         // Open the file for the adapter here..         // The open will fail if the adapter does not exist  rc = 0;#ifndef ALLSOCK#ifdef DEBUGON   logit(LOG_DEBUG, "%-25s:  slot_id %d, \n","SC_Initialize",i);#endif   {      int j;         j = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);         if (j) {             adapter_handle[i] = j;             hs = gethostbyname(HOSTNAME);            if (! hs ) {               exit(2 );            }            hst=*hs;            saddr.sin_family = AF_INET;            bcopy(hs->h_addr,(char *)&saddr.sin_addr,hs->h_length);            saddr.sin_port = htons(PORT);            if ( connect (adapter_handle[i],(struct sockaddr *)&saddr,sizeof(struct sockaddr_in)) < 0 ){               // XXX FIXME   just bail for now.#ifdef DEBUGON   logit(LOG_DEBUG, "%-25s: connect errno %d \n","SC_Initialize",errno);#endif               ReleaseMutex(pkcs_mutex);               return CKR_FUNCTION_FAILED;            } else {                  rc = 0;            }         } else {#ifdef DEBUGON   logit(LOG_DEBUG, "%-25s: open errno %d \n","SC_Initialize",errno);#endif            ReleaseMutex(pkcs_mutex);            return CKR_FUNCTION_FAILED;         }   }         if (rc == 0 ){            correlator_init[i].init = 1; // Mark in use            correlator_init[i].API_Slotid = SlotNumber;         } else {            (*FunctionList) = NULL;            ReleaseMutex(pkcs_mutex);            return CKR_FUNCTION_FAILED;         }      }      usage_count++;   }#endif // ifdef ALLSOCK#endif    (*FunctionList) = &function_list;   ReleaseMutex( pkcs_mutex );   return CKR_OK;}// FIXME : This needs to be more fully debugged.//CK_RV SC_Finalize( CK_SLOT_ID sid ){   CK_ULONG       req_len, repl_len;   CK_ULONG       i;   SLT_CHECK#ifdef DEBUGON   logit(LOG_DEBUG, "%-25s:  sid %d slot_id %d, \n","SC_Finalize",sid,slot_id);#endif   WaitForSingleObject( pkcs_mutex, INFINITE );   if (Initialized() == FALSE){      ReleaseMutex(pkcs_mutex);      return CKR_CRYPTOKI_NOT_INITIALIZED;   }#if 0   if (pReserved != NULL){      ReleaseMutex(pkcs_mutex);      return CKR_ARGUMENTS_BAD;   }#endif   {#ifdef DEBUGON   logit(LOG_DEBUG, "\t Slot %x  pid list %x %x\n",slot_id,pid_list[slot_id],pid_list[slot_id-1]);#endif      if (pid_list[slot_id -1 ] != 0){         req_len  = 0;         repl_len = 0;         communicate(C_FINALIZE,slot_id,NULL,req_len,                     NULL,&repl_len,                     NULL,0,                     NULL,0);         // need to close the correct adapter handle...      }      i = slot_id -1; // slot id's are 1 based for leeds, array is zero based      if ( adapter_handle[i] != 0 ){         sccCloseAdapter( adapter_handle[i] );	 i++;      }   }   // Decrement module usage count   // if module usage account is 0 then whack it.   // since all instances of the module are cleaned up   usage_count --;   if (usage_count == 0 ){      initialized = FALSE;   }   ReleaseMutex( pkcs_mutex );   return CKR_OK;}#ifdef PKCS64////CK_RV SC_GetTokenInfo( CK_SLOT_ID         sid,                      CK_TOKEN_INFO_PTR  pInfo ){   CK_TOKEN_INFO_32     reply;   CK_ULONG          req_len;   CK_ULONG          repl_len;   CK_RV             rc;   SLT_CHECK   if (Initialized() == FALSE)      return CKR_CRYPTOKI_NOT_INITIALIZED;   if (!pInfo)      return CKR_FUNCTION_FAILED;   if (slot_id > MAX_SLOT_ID)      return CKR_SLOT_ID_INVALID;   // no arguments   //   req_len = 0;   repl_len = sizeof(reply);   rc = communicate( C_GETTOKENINFO, slot_id,                   NULL,   req_len,                  &reply, &repl_len,                   NULL,   0,                   NULL,   0 );#ifdef __64BIT__   if (rc == CKR_OK) {     // The first 96 bytes are ok since the fields contain no longs:      // label, manufacture ID, etc. See  CK_TOKEN_INFO for details.      memcpy( pInfo, &reply, 96 );      pInfo->flags                = reply.flags;      pInfo->ulMaxSessionCount    = reply.ulMaxSessionCount;      pInfo->ulSessionCount       = reply.ulSessionCount;      pInfo->ulMaxRwSessionCount  = reply.ulMaxRwSessionCount;       pInfo->ulRwSessionCount     = reply.ulRwSessionCount;      pInfo->ulMaxPinLen          = reply.ulMaxPinLen;      pInfo->ulMinPinLen          = reply.ulMinPinLen;      pInfo->ulTotalPublicMemory  = reply.ulTotalPublicMemory;      pInfo->ulFreePublicMemory   = reply.ulFreePublicMemory;      pInfo->ulTotalPrivateMemory = reply.ulTotalPrivateMemory;      pInfo->ulFreePrivateMemory  = reply.ulFreePrivateMemory;   }#else   if (rc == CKR_OK)      memcpy( pInfo, &reply, sizeof(CK_TOKEN_INFO) );#endif   pInfo->flags                = CTOHL( pInfo->flags                );   pInfo->ulMaxSessionCount    = CTOHL( pInfo->ulMaxSessionCount    );   pInfo->ulSessionCount       = CTOHL( pInfo->ulSessionCount       );   pInfo->ulMaxRwSessionCount  = CTOHL( pInfo->ulMaxRwSessionCount  );   pInfo->ulRwSessionCount     = CTOHL( pInfo->ulRwSessionCount     );   pInfo->ulMaxPinLen          = CTOHL( pInfo->ulMaxPinLen          );   pInfo->ulMinPinLen          = CTOHL( pInfo->ulMinPinLen          );   pInfo->ulTotalPublicMemory  = CTOHL( pInfo->ulTotalPublicMemory  );   pInfo->ulFreePublicMemory   = CTOHL( pInfo->ulFreePublicMemory   );   pInfo->ulTotalPrivateMemory = CTOHL( pInfo->ulTotalPrivateMemory );   pInfo->ulFreePrivateMemory  = CTOHL( pInfo->ulFreePrivateMemory  );   // These don't need to be swapped at this time.   //pInfo->hardwareVersion      = long_reverse( pInfo->hardwareVersion      );   //pInfo->firmwareVersion      = long_reverse( pInfo->firmwareVersion      );   //#ifdef DEBUGON   logit(LOG_DEBUG, "%-25s:  rc = %08x\n", "C_GetTokenInfo", rc );#endif   return rc;}#else////CK_RV SC_GetTokenInfo( CK_SLOT_ID         sid,                      CK_TOKEN_INFO_PTR  pInfo ){   CK_TOKEN_INFO     reply;   CK_ULONG          req_len;   CK_ULONG          repl_len;   CK_RV             rc;   SLT_CHECK   if (Initialized() == FALSE)      return CKR_CRYPTOKI_NOT_INITIALIZED;   if (!pInfo)      return CKR_FUNCTION_FAILED;   if (slot_id > MAX_SLOT_ID)      return CKR_SLOT_ID_INVALID;   // no arguments   //   req_len = 0;   repl_len = sizeof(reply);   rc = communicate( C_GETTOKENINFO, slot_id,                   NULL,   req_len,                  &reply, &repl_len,                   NULL,   0,                   NULL,   0 );   if (rc == CKR_OK)      memcpy( pInfo, &reply, sizeof(CK_TOKEN_INFO) );   pInfo->flags                = CTOHL( pInfo->flags                );   pInfo->ulMaxSessionCount    = CTOHL( pInfo->ulMaxSessionCount    );   pInfo->ulSessionCount       = CTOHL( pInfo->ulSessionCount       );   pInfo->ulMaxRwSessionCount  = CTOHL( pInfo->ulMaxRwSessionCount  );   pInfo->ulRwSessionCount     = CTOHL( pInfo->ulRwSessionCount     );   pInfo->ulMaxPinLen          = CTOHL( pInfo->ulMaxPinLen          );   pInfo->ulMinPinLen          = CTOHL( pInfo->ulMinPinLen          );   pInfo->ulTotalPublicMemory  = CTOHL( pInfo->ulTotalPublicMemory  );   pInfo->ulFreePublicMemory   = CTOHL( pInfo->ulFreePublicMemory   );   pInfo->ulTotalPrivateMemory = CTOHL( pInfo->ulTotalPrivateMemory );   pInfo->ulFreePrivateMemory  = CTOHL( pInfo->ulFreePrivateMemory  );   // These don't need to be swapped at this time.   //pInfo->hardwareVersion      = long_reverse( pInfo->hardwareVersion      );   //pInfo->firmwareVersion      = long_reverse( pInfo->firmwareVersion      );   //#ifdef DEBUGON   logit(LOG_DEBUG, "%-25s:  rc = %08x\n", "C_GetTokenInfo", rc );#endif   return rc;}#endif#ifdef PKCS64////CK_RV SC_GetMechanismList( CK_SLOT_ID             sid,                          CK_MECHANISM_TYPE_PTR  pMechList,                          CK_ULONG_PTR           count ){   GetMechList_Args   args;   CK_BY

⌨️ 快捷键说明

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