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

📄 clientstore.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
📖 第 1 页 / 共 4 页
字号:
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  VtFileNameList *nameList = (VtFileNameList *)0;
  time_t updateTime;

  updateLen = sizeof (updateTime);
  distNameLen = sizeof (distName);
  nameToUse = distName;

  do
  {
    /* If the entry obj is already set, error.
     */
    status = VT_ERROR_INVALID_STORAGE_ENTRY;
    if ( (distObj->paramsText != (unsigned char *)0) ||
         (distObj->paramObj != (VtParameterObject)0) )
      break;

    /* Open the parameters registry key
     */
    status = VT_ERROR_OPEN_REGISTRY_KEY;
    ret = RegOpenKey (
      HKEY_CURRENT_USER, 
      _T("Software\\Voltage\\VSCOM\\parameters"), &paramKey);

    /* If there is no registry entry for this key it means no district.
     * Hence no params were found.
     */
    if (ret != ERROR_SUCCESS)
    {
      if (ret == ERROR_FILE_NOT_FOUND)
        status = VT_ERROR_UNKNOWN_DISTRICT;
      break;
    }

    /* Now open the update key to find the last update time
     * for public params.
     */
    ret = RegOpenKey (
      HKEY_CURRENT_USER, 
      _T("Software\\Voltage\\VSCOM\\paramUpdates"), &updateKey);
    if (ret != ERROR_SUCCESS)
      break;

    status = VT_ERROR_INVALID_STORAGE_REF;
    if (distObj->qualDistrictName.data == (unsigned char *)0)
    {
      /* If there's no qualified name, there can be no domain name.
       */
      if (distObj->unqualDistrictName.data != (unsigned char *)0)
        break;

      /* We have to get the value based on the index now.
      */
      valueCount = 0;
      status = VT_ERROR_READ_REGISTRY_VALUE;
      ret = RegQueryInfoKey (
        paramKey, NULL, NULL, NULL, NULL, NULL, 
        NULL, &valueCount, NULL, NULL, NULL, NULL);
      if (ret != ERROR_SUCCESS)
        break;
      status = VT_ERROR_ENTRY_NOT_FOUND;
      if (valueCount == 0)
        break;
      if (index >= valueCount)
        break;

      status = VT_ERROR_READ_REGISTRY_VALUE;
      ret = RegEnumValue (
        paramKey, index, distName, &distNameLen, 
        NULL, NULL, NULL, &paramTextLen);
      if (ret != ERROR_SUCCESS)
        break;

      distName[distNameLen] = 0;
    }
    else
    {
      /* We get the entry based on district
      */      
      status = VT_ERROR_READ_REGISTRY_VALUE;
      ret = RegQueryValueEx (
           paramKey, distObj->qualDistrictName.data, NULL,
           NULL, (unsigned char *)0, &paramTextLen);
      if (ret == ERROR_FILE_NOT_FOUND)
        status = VT_ERROR_UNKNOWN_DISTRICT;
      if (ret != ERROR_SUCCESS)
        break;

      nameToUse = distObj->qualDistrictName.data;
    }

    /* Allocate the memory to read the public parameters
    */
    status = VT_ERROR_MEMORY;
    contents = (unsigned char *)Z2Malloc (paramTextLen, 0);
    if (contents == (unsigned char *)0)
      break;

    status = VT_ERROR_READ_REGISTRY_VALUE;
    ret = RegQueryValueEx (
      paramKey, nameToUse, NULL, NULL, contents, &paramTextLen);
    if (ret != ERROR_SUCCESS)
      break;

    ret = RegQueryValueEx (
      updateKey, nameToUse, NULL, NULL, (unsigned char *)&updateTime,
      &updateLen);
    if (ret != ERROR_SUCCESS)
      break;

    /* Convert the time_t struct to Voltage time
    */
    VoltConvertTimeToVoltage ((VoltTime *)&updateTime , timeOfStore);

    /* Now set the district object with the param text
    */
    status = VoltSetDistFromParamsText (
      contents, paramTextLen, distObj, distObj->mpCtx, libCtx);

  } while (0);

  if (paramKey != (HKEY)0)
    RegCloseKey(paramKey);
  if (updateKey != (HKEY)0)
    RegCloseKey(updateKey);
  if (contents != (unsigned char *)0)
    Z2Free (contents);
  
  return (status);
}

int VoltClientDeleteCurrentDistrict (
   VtStorageCtx storageCtx,
   unsigned char *reference
   )
{
  HKEY cdKey = (HKEY)0;
  HKEY updateKey = (HKEY)0;
  int status, ret;
  int cdExists, updateExists;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDefaultStorageCtx *defStorageCtx =
    (VoltDefaultStorageCtx *)(ctx->localStorageCtx);
  VoltFileCtx *fileCtx = (VoltFileCtx *)(defStorageCtx->fCtx);
  unsigned char *fileName = (unsigned char *)0;

  cdExists = 1;
  updateExists = 1;
  do
  {
    status = VT_ERROR_OPEN_REGISTRY_KEY;
    ret = RegOpenKey (
      HKEY_CURRENT_USER, 
      _T("Software\\Voltage\\VSCOM\\currentDistricts"), &cdKey);
    if (ret != ERROR_SUCCESS)
    {
      if (ret == ERROR_FILE_NOT_FOUND)
        cdExists = 0;
      else
        break;
    }
   ret = RegOpenKey (
      HKEY_CURRENT_USER, 
      _T("Software\\Voltage\\VSCOM\\cdUpdates"), &updateKey);
   if (ret != ERROR_SUCCESS)
   {
     if (ret == ERROR_FILE_NOT_FOUND)
       updateExists = 0;
     else
       break;
   }
   
   /* Delete the district entries from the registry
   */
   status = VT_ERROR_DELETE_REGISTRY_VALUE;
   if (cdExists == 1)
   {
     ret = RegDeleteValue(cdKey, reference);
     if (ret != ERROR_SUCCESS && ret != ERROR_FILE_NOT_FOUND)
       break;
   }
   if (updateExists == 1)
   {
     ret = RegDeleteValue(updateKey, reference);
     if (ret != ERROR_SUCCESS && ret != ERROR_FILE_NOT_FOUND)
       break;
   }

   status = 0;

  } while (0);

  if (cdKey != (HKEY)0)
    RegCloseKey (cdKey);
  if (updateKey != (HKEY)0)
    RegCloseKey (updateKey);
  
  return (status);
}

int VoltClientRetrievePublicSigningCert (
   VtStorageCtx storageCtx,
   VtIdentityObject reference,
   unsigned int index,
   VtCertObject certObj
   )
{
  int status;
  VoltFileInt fileSize;
  unsigned int fileNameLen, contentsLen;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltIdentityObject *idObj = (VoltIdentityObject *)reference;
  VoltFileCtx *fileCtx = (VoltFileCtx *)0;
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  VoltDefaultStorageCtx *defStorageCtx;
  unsigned char *fileName = (unsigned char *)0;
  unsigned char *contents = (unsigned char *)0;
  unsigned char *nameToUse;
  VtFileNameList *nameList = (VtFileNameList *)0;
  VtCertInfo certInfo;
  VtDerCoder *derCoders[1] = { VtDerCoderDSAPublicKey };

  /* This implementation works only on X.509 certs and only with DSA as
   * the public key and the signing key.
   */

  /* Get the File Ctx
  */
  defStorageCtx = (VoltDefaultStorageCtx *)ctx->localStorageCtx;
  fileCtx = (VoltFileCtx *)defStorageCtx->fCtx;

  Z2Memset (&certInfo, 0, sizeof (VtCertInfo));

  do
  {
    /* If there's no reference, get whatever value is at the index.
     */
    if (reference == (VtIdentityObject)0)
    {
      status = VoltGetStorageFileListAlloc (
        ctx, VOLT_STORE_DIR_SIGN_CERTS_CLIENT, VOLT_STORE_DIR_SIGN_CERTS_CLIENT_LEN,
        0, &nameList);
      if (status != 0)
        break;

      status = VT_ERROR_ENTRY_NOT_FOUND;
      if (nameList == (VtFileNameList *)0)
        break;

      if (index >= nameList->nameCount)
        break;

      nameToUse = nameList->nameList[index];
    }
    else
    {
      /* We need the identity encoded.
       */
      status = VT_ERROR_INVALID_STORAGE_REF;
      if (idObj->encoding.data == (unsigned char *)0)
        break;

      /* Get the file name.
       */
      status = VoltGetFileNameFromValueAlloc (
        ctx, (unsigned char *)0, 0, idObj->encoding.data, idObj->encoding.len,
        VOLT_FILE_NAME_VALUE_TYPE_CERT_CLIENT, &fileName, &fileNameLen);
      if (status != 0)
        break;

      nameToUse = fileName;
    }

    /* Try to open this file.
     */
    status = fileCtx->CtxOpenFile (
      fileCtx, &fileHandle, nameToUse, VOLT_FILE_MODE_READ_ONLY, 0);

    /* If the return to the open call is ERROR, there was no file to
     * open, this function indicates no key found.
     */
    if (status != 0)
    {
      if (status == VT_ERROR_FILE_DOES_NOT_EXIST)
        status = VT_ERROR_ENTRY_NOT_FOUND;
      
      break;
    }    

    /* Get the contents of the file. Firts see how big is the file
    */
    status = fileCtx->CtxGetFileSize (
      fileCtx, fileHandle, (char *)0, &fileSize);
    if (status != 0)
      break;

    /* Allocate the buffer to hold the cert data
    */
    status = VT_ERROR_MEMORY;
    contents = (unsigned char *)Z2Malloc (fileSize, 0);
    if (contents == (unsigned char *)0)
      break;

    /* Now read the data from the file into the buffer
    */
    status = fileCtx->CtxReadFile (
      fileCtx, fileHandle, (unsigned int)fileSize, contents, &contentsLen);
    if (status != 0)
      break;

    /* The contents should be the DER of the private key.
     */
    certInfo.derCoders = derCoders;
    certInfo.derCoderCount = 1;
    certInfo.cert = contents;
    certInfo.certLen = contentsLen;
    status = VtSetCertParam (certObj, VtCertParamX509Der, (Pointer)&certInfo);

  } while (0);

  if (fileHandle != (VoltFileHandle)0)
    fileCtx->CtxCloseFile (fileCtx, &fileHandle);
  if (nameList != (VtFileNameList *)0)
    VoltFileListFree (libCtx, &nameList);

  if (fileName != (unsigned char *)0)
    Z2Free (fileName);
  if (contents != (unsigned char *)0)
    Z2Free (contents);

  return (status);
}

int VoltClientDeleteDistrictParameters (
   VtStorageCtx storageCtx,
   VtDistrictObject reference,
   unsigned int index
   )
{
  HKEY paramKey = (HKEY)0;
  HKEY updateKey = (HKEY)0;
  int status, ret;
  int paramExists, updateExists;
  char district[256];
  unsigned int districtLen = 256;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDefaultStorageCtx *defStorageCtx =
    (VoltDefaultStorageCtx *)(ctx->localStorageCtx);
  VoltFileCtx *fileCtx = (VoltFileCtx *)(defStorageCtx->fCtx);
  VoltDistrictObject *distObj = (VoltDistrictObject *)reference;
  VtDistrictObject tempObject = (VtDistrictObject)0;
  VtFileNameList *nameList = (VtFileNameList *)0;
  unsigned char *distPtr = (unsigned char *)0;

  paramExists = 1;
  updateExists = 1;

  do
  {
    status = VT_ERROR_OPEN_REGISTRY_KEY;
    ret = RegOpenKey (
      HKEY_CURRENT_USER, 
      _T("Software\\Voltage\\VSCOM\\parameters"), &paramKey);
    if (ret != ERROR_SUCCESS)
    {
      if (ret == ERROR_FILE_NOT_FOUND)
        paramExists = 0;
      else
        break;
    }
   ret = RegOpenKey (
      HKEY_CURRENT_USER, 
      _T("Software\\Voltage\\VSCOM\\paramUpdates"), &updateKey);
   if (ret != ERROR_SUCCESS)
   {
     if (ret == ERROR_FILE_NOT_FOUND)
       updateExists = 0;
     else
       break;
   }
   
   /* If there's no reference,delete based on index.
   */
   if (reference == (VtDistrictObject)0)
   {
     status = VT_ERROR_READ_REGISTRY_VALUE;
     ret = RegEnumValue (
       paramKey, index, district, &districtLen, NULL, NULL, NULL, NULL) ;

     /* If there is no value at specfied index we don't need to do anything
     */
     if (ret == ERROR_NO_MORE_ITEMS)
     {
       status = 0;
       break;
     }

     distPtr = (unsigned char *)district;
   }    
   else
   {
     status = VT_ERROR_INVALID_STORAGE_REF;

     /* We need the qualified name.
     */
     if (distObj->qualDistrictName.data == (unsigned char *)0)
       break;
     
     distPtr = distObj->qualDistrictName.data ;
    }

    status = VT_ERROR_DELETE_REGISTRY_VALUE;
    if (paramExists)
    {
      ret = RegDeleteValue(paramKey, distPtr);
      if (ret != ERROR_SUCCESS && ret != ERROR_FILE_NOT_FOUND)
       break;
    }
    if (updateExists)
    {
      ret = RegDeleteValue (updateKey, distPtr);
      if (ret != ERROR_SUCCESS && ret != ERROR_FILE_NOT_FOUND)
       break;
    }
   
    status = 0;

  } while (0);

  if (paramKey != (HKEY)0)
    RegCloseKey (paramKey);
  if (updateKey != (HKEY)0)
    RegCloseKey (updateKey);

  return (status);
}

int VoltClientDeleteIBEPrivateKey (
   VtStorageCtx storageCtx,
   VtIdentityObject reference,
   unsigned int index
   )
{  
  return (VoltDeleteEntryFromIdentity (
    storageCtx, reference, index, VOLT_STORE_DIR_IBE_KEYS_CLIENT,
    VOLT_STORE_DIR_IBE_KEYS_CLIENT_LEN,
    VOLT_FILE_NAME_VALUE_TYPE_IBE_PRI_CLIENT));
}

int VoltClientDeletePrivateSigningKey (
   VtStorageCtx storageCtx,
   VtIdentityObject reference,
   unsigned int index
   )
{
  return (VoltDeleteEntryFromIdentity (
    storageCtx, reference, index, VOLT_STORE_DIR_SIGN_KEYS_CLIENT,
    VOLT_STORE_DIR_SIGN_KEYS_CLIENT_LEN, VOLT_FILE_NAME_VALUE_TYPE_SIGN_PRI_CLIENT));
}

int VoltClientDeletePublicSigningCert (
  VtStorageCtx storageCtx,
  VtIdentityObject reference,
  unsigned int index
  )
{
  return (VoltDeleteEntryFromIdentity (
    storageCtx, reference, index, VOLT_STORE_DIR_SIGN_CERTS_CLIENT,
    VOLT_STORE_DIR_SIGN_CERTS_CLIENT_LEN, VOLT_FILE_NAME_VALUE_TYPE_CERT_CLIENT));
}

int VoltClientDeleteAuthToken (
   VtStorageCtx storageCtx,
   VtDistrictObject reference,
   unsigned int index
   )
{
  return VoltDeleteAuthTokenForDistrict (
    storageCtx, reference, index,
    VOLT_FILE_NAME_VALUE_TYPE_IBE_TOKEN_CLIENT);
}

#endif /* VOLT_COMPILER != VOLT_MS_EVC_4_0 */
#endif /* VOLT_OS == VOLT_WINDOWS_32 */

⌨️ 快捷键说明

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