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

📄 clientstore.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 5 页
字号:

    priKeyInfo.ibeParams = (VtParameterObject)paramObj;
    priKeyInfo.encodedId.data = asn1PriKey->pubKey->base.data;
    priKeyInfo.encodedId.len =
      (unsigned int)(asn1PriKey->pubKey->base.length);
    priKeyInfo.privatePoint.isInfinity = ibePoint->isInfinity;
    priKeyInfo.privatePoint.xCoord.data = ibePoint->xCoord.data;
    priKeyInfo.privatePoint.xCoord.len = ibePoint->xCoord.len;
    priKeyInfo.privatePoint.yCoord.data = ibePoint->yCoord.data;
    priKeyInfo.privatePoint.yCoord.len = ibePoint->yCoord.len;
    
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtSetKeyParam (
      keyObj, VtKeyParamBFType1IBEPrivate, (Pointer)&priKeyInfo);

  } while (0);

  if (password != (unsigned char *)0)
    defStorageCtx->ReleaseExtraPassword (ctx, &password, &passwordLen);

  VtDestroyDistrictObject (&district);

  if (ibePoint != (VtBFType1IBEPoint *)0)
    Z2Free (ibePoint);
  if (asn1PriKey != (Asn1IBEPrivateKey *)0)
    Asn1IBEPrivateKey_free (asn1PriKey);
  if (fileHandle != (VoltFileHandle)0)
    fileCtx->CtxCloseFile (fileCtx, &fileHandle);
  if (nameList != (VtFileNameList *)0)
    VoltFileListFree (libCtx, &nameList);

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

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, storageCtx, status, 0, errorType,
    (char *)0, "VoltClientRetrieveIBEPrivateKey", fnctLine, (char *)0)

  return (status);
}

int VoltClientRetrievePrivateSigningKey (
   VtStorageCtx storageCtx,
   VtIdentityObject reference,
   unsigned int index,
   VtKeyObject keyObj
   )
{
  int status;
  unsigned int fileNameLen, contentsLen, passwordLen;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDefaultStorageCtx *defStorageCtx =
    (VoltDefaultStorageCtx *)(ctx->localStorageCtx);
  VoltFileCtx *fileCtx = (VoltFileCtx *)(defStorageCtx->fCtx);
  VoltIdentityObject *idObj = (VoltIdentityObject *)reference;
  VoltKeyObject *key = (VoltKeyObject *)keyObj;
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  unsigned char *password = (unsigned char *)0;
  unsigned char *fileName = (unsigned char *)0;
  VtFileNameList *nameList = (VtFileNameList *)0;
  unsigned char *contents = (unsigned char *)0;
  unsigned char *backupName = (unsigned char *)0;
  unsigned char *nameToUse;
  unsigned char *temp;
  Asn1ClientDSAPriKey *asn1ClientKey = (Asn1ClientDSAPriKey *)0;
  VtSetKeyBerInfo berInfo;
  VtDSAPriKeyInfo priKeyInfo;
  VtDerCoder *derCoders[1] = { VtDerCoderDSAPrivateKey };
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  Z2Memset (&berInfo, 0, sizeof (VtSetKeyBerInfo));
  Z2Memset (&priKeyInfo, 0, sizeof (VtDSAPriKeyInfo));

  do
  {
    /* If the keyObj is already set, error.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_KEY_OBJ;
    if (key->keyType != 0)
      break;

    /* If there's no reference, get whatever value is at the index.
     */
    if (reference == (VtIdentityObject)0)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetStorageFileListAlloc (
        ctx, VOLT_STORE_DIR_SIGN_KEYS_CLIENT,
        VOLT_STORE_DIR_SIGN_KEYS_CLIENT_LEN, 0, &nameList);
      if (status != 0)
        break;
      
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      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.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_STORAGE_REF;
      if (idObj->encoding.data == (unsigned char *)0)
        break;

      /* Get the file name as if it's in the toolkit directory.
       */
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetFileNameFromValueAlloc (
        ctx, (unsigned char *)0, 0, idObj->encoding.data, idObj->encoding.len,
        VOLT_FILE_NAME_VALUE_TYPE_SIGN_PRI_CLIENT, &fileName, &fileNameLen);
      if (status != 0)
        break;

      nameToUse = fileName;
    }

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

    /* If the file doesn't exist, return VT_ERROR_ENTRY_NOT_FOUND
     * otherwise return the error received.
     */
    if (status != 0)
    {
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      if (status == VT_ERROR_FILE_DOES_NOT_EXIST)
        status = VT_ERROR_ENTRY_NOT_FOUND;

      break;
    }    

    /* Get the client storage password if any
     */
    if (defStorageCtx->GetExtraPassword != (VGetExtraPassword)0)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = defStorageCtx->GetExtraPassword (ctx, &password, &passwordLen);
      if (status != 0)
        break;
    }

    /* Get the contents of the file.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = mIcLoadDataAlloc (
      ctx, fileCtx, fileHandle, password, passwordLen,
      &contents, &contentsLen);
    if (status != 0)
      break;

    /* Decode the private signing key
    */    
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    asn1ClientKey = Asn1ClientDSAPriKey_new ();
    if (asn1ClientKey == (Asn1ClientDSAPriKey *)0)
      break;
    
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_ENTRY_NOT_FOUND;
    temp = contents;
    d2i_Asn1ClientDSAPriKey (&asn1ClientKey, &temp, (int)contentsLen);
    if (asn1ClientKey == (Asn1ClientDSAPriKey *)0)
      break;
    
    priKeyInfo.primeP.data = asn1ClientKey->prime->data;
    priKeyInfo.primeP.len = (unsigned int)(asn1ClientKey->prime->length);
    priKeyInfo.subprimeQ.data = asn1ClientKey->subprime->data;
    priKeyInfo.subprimeQ.len = (unsigned int)(asn1ClientKey->subprime->length);
    priKeyInfo.baseG.data = asn1ClientKey->base->data;
    priKeyInfo.baseG.len = (unsigned int)(asn1ClientKey->base->length);
    priKeyInfo.pubValY.data = asn1ClientKey->pubVal->data;
    priKeyInfo.pubValY.len = (unsigned int)(asn1ClientKey->pubVal->length);
    priKeyInfo.priValX.data = asn1ClientKey->priVal->data;
    priKeyInfo.priValX.len = (unsigned int)(asn1ClientKey->priVal->length);
    
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtSetKeyParam (
      keyObj, VtKeyParamDSAPrivate, (Pointer)&priKeyInfo);

  } while (0);

  if (password != (unsigned char *)0)
    defStorageCtx->ReleaseExtraPassword (ctx, &password, &passwordLen);
  if (asn1ClientKey != (Asn1ClientDSAPriKey *)0)
    Asn1ClientDSAPriKey_free (asn1ClientKey);
  if (fileHandle != (VoltFileHandle)0)
    fileCtx->CtxCloseFile (fileCtx, &fileHandle);
  if (nameList != (VtFileNameList *)0)
    VoltFileListFree (libCtx, &nameList);

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

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, storageCtx, status, 0, errorType,
    (char *)0, "VoltClientRetrievePrivateSigningKey", fnctLine, (char *)0)

  return (status);
}

int VoltClientRetrieveCurrentDistrictAlloc (
   VtStorageCtx storageCtx,
   unsigned char *reference,
   unsigned int *currentDistrictResult,
   unsigned char **currentDistrict,
   VtTime *validityStart,
   VtTime *validityEnd,
   VtTime *timeOfStore
   )
{
  HKEY cdKey = (HKEY)0;
  HKEY paramKey = (HKEY)0;
  HKEY updateKey = (HKEY)0;
  int status, ret;
  unsigned char district[256];
  unsigned char *params = (unsigned char *)0;
  unsigned int districtLen, paramsLen, updateLen;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VtMpIntCtx mpCtx = (VtMpIntCtx )0;
  VtMpIntCtx mpCtxToUse;
  VoltDefaultStorageCtx *defStorageCtx =
    (VoltDefaultStorageCtx *)(ctx->localStorageCtx);
  VoltFileCtx *fileCtx = (VoltFileCtx *)(defStorageCtx->fCtx);
  unsigned char *fileName = (unsigned char *)0;
  unsigned char *contents = (unsigned char *)0;
  unsigned char *nameCopy = (unsigned char *)0;
  unsigned char *password = (unsigned char *)0;
  VtDistrictObject distObj = (VtDistrictObject)0;
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  VoltTime seconds ;
  time_t updateTime;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  ret = ERROR_SUCCESS;
  districtLen = 256;
  paramsLen = 20000;
  updateLen = sizeof (updateTime);
  *currentDistrictResult = VOLT_CURRENT_DIST_UNKNOWN;

  do
  {
    /* Open the currentDistrict registry key. If the registry key doesn't
     * exist we can't say anything about the current distrct. The result
     * is already set to VOLT_CURRENT_DIST_UNKNOWN.
     */
    VOLT_SET_ERROR_TYPE (
      errorType, VT_ERROR_TYPE_PRIMARY | VT_ERROR_TYPE_SYSTEM)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_OPEN_REGISTRY_KEY;
    ret = RegOpenKey (
      HKEY_CURRENT_USER,
      _T("Software\\Voltage\\VSCOM\\currentDistricts"), &cdKey);
    if (ret != ERROR_SUCCESS)
    {
      /* Any other error is a real error so pass it on.
       */
      if (ret != ERROR_FILE_NOT_FOUND)
        break;

      status = 0;
      break;
    }

    /* Open the update and the param key for the current district.
     * this is required to get the last update time of the district
     * and the validity periods of the district.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    ret = RegOpenKey (
      HKEY_CURRENT_USER, 
      _T("Software\\Voltage\\VSCOM\\cdUpdates"), &updateKey);
    if (ret != ERROR_SUCCESS)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    ret = RegOpenKey (
      HKEY_CURRENT_USER, 
      _T("Software\\Voltage\\VSCOM\\parameters"), &paramKey);
    if (ret != ERROR_SUCCESS)
      break;

    /* Read registry values.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_READ_REGISTRY_VALUE;
    ret = RegQueryValueEx (
      cdKey, reference, NULL, NULL, district, &districtLen);
    if (ret != ERROR_SUCCESS)
    {
      /* Any other errors pass on
       */
      if (ret != ERROR_FILE_NOT_FOUND)
        break;

      status = 0;
      break;
    }

    /* Did the registry contain a current district? Or did it contain
     * the information that there is no current district?
     * if the districtLen is 4 we have no district.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    ret = RegQueryValueEx (
      updateKey, reference, NULL, NULL, (unsigned char *)&updateTime,
      &updateLen);
    if (ret != ERROR_SUCCESS)
      break;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    VoltConvertTimeToVoltage ((VoltTime *)&updateTime , timeOfStore);
    if (districtLen == 4)
    {
      *currentDistrictResult = VOLT_CURRENT_DIST_NO_DIST;
      status = 0;
      break;
    }

    /* Now look for the district parameters to find the validity dates.
     * If we can't get the params, we can't get the validity dates. If
     * we don't know the validity dates, we don't know if the current
     * district info we have is correct. That means
     * currentDistrictResult is VOLT_CURRENT_DIST_UNKNOWN.
     * If something goes wrong, by returning UNKNOWN, it allows an app
     * to possibly obtain info some other way.
     */
    status = 0;
    ret = RegQueryValueEx (
      paramKey, district, NULL, NULL, params, &paramsLen);
    if (ret != ERROR_SUCCESS)
      break;

    /* allocate the buffer to hold the public params and get
     * them from the registry.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    params = (unsigned char *)Z2Malloc (paramsLen, 0);
    if (params == (unsigned char *)0)
      break;

    status = 0;
    ret = RegQueryValueEx (
      paramKey, district, NULL, NULL, params, &paramsLen);
    if (ret != ERROR_SUCCESS)
      break;

    /* we found the current district and parameters. We need an 
     * MpIntCtx to decode the public parameters. If the user has not
     * provided one create a temporary MpCtx.
     */    
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    mpCtxToUse = (VtMpIntCtx) VoltGetLibCtxInfo (
      (VtLibCtx)libCtx, VOLT_LIB_CTX_INFO_TYPE_MP_CTX);
    if (mpCtxToUse == (VtMpIntCtx )0)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtCreateMpIntCtx (
        (VtLibCtx)libCtx, VtMpIntImplOpenSSL, (Pointer)0, &mpCtx);
      if (status != 0)
        break;
      mpCtxToUse = mpCtx;
    }

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreateDistrictObject (
      (VtLibCtx)libCtx, VtDistrictImplMpCtx, (Pointer)mpCtxToUse, &distObj);
    if (status != 0)
      break;

    /* Set the district object with params then get the validityStart
     * and validityEnd values for the district.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtSetDistrictParam (
      distObj, VtDistrictParamBase64Params, (Pointer)params);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltConvertTimeToSeconds (
      libCtx, &(distObj->validityStart), &seconds);
    if (status != 0)
      break;
    VoltConvertTimeToVoltage (&seconds, validityStart);

⌨️ 快捷键说明

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