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

📄 zdm2write.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 5 页
字号:
  unsigned int addressLen;
  unsigned char* domain;
  unsigned int domainLen;
  int cmp;
#if 0
  UInt32 lenLo, lenHi;
  VtX509ExtensionList* extensionList;
  unsigned int index;
  VtItem* zdmLocationItem = (VtItem*)0;
  unsigned int tag, lengthLen, valueLen;
  unsigned char oldZdrLocationOid[VoltParamExtOldZdrOidBytesLen] =
    { VoltParamExtOldZdrOidBytes };
  unsigned char newZdrLocationOid[VoltParamExtZdrSrvOidBytesLen] =
    { VoltParamExtZdrSrvOidBytes };
  unsigned char zdrVersionOid[VoltParamExtZdrVersionOidBytesLen] =
    { VoltParamExtZdrVersionOidBytes };
  int foundNewZdrLocation = 0;
#endif
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)

  VT_ASSERT(zdmObj != (VtZDMObject)0);
  VT_ASSERT(zdmLocation != (Pointer*)0);
  VT_ASSERT(supportedVersion != (unsigned int*)0);
  VT_ASSERT(external != (int*)0);
  
  libCtx = zdmObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  localCtx = (VoltZDM2WriteLocalCtx*) zdmObj->localCtx;
  VT_ASSERT(localCtx != (VoltZDM2WriteLocalCtx*)0);
  
  *zdmLocation = (unsigned char*)0;
  *supportedVersion = 1;
  *external = 0;
  
  VOLT_SET_ERROR_TYPE(errorType, 0)

  do
  {
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtEncodeIdentity(identity, VT_ENCODE_IBCS_2_V_DISTRICT,
      localCtx->mPolicyCtx, localCtx->mStorageCtx,
      localCtx->mTransportCtx, (unsigned char *)0, 0, &encodingLen);
    if (status == 0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      status = VT_ERROR_GENERAL;
    }
    if (status != VT_ERROR_BUFFER_TOO_SMALL)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtGetIdentityParam(identity, VtIdentityParamDistrict,
      (Pointer*)&district);
    if (status != 0)
      break;
    
    VT_ASSERT(district != (VtDistrictObject)0);
    
    /* For this function, we must have the district params. We might
     * not have them if the identity cane in encoded.
     */
    status = VtObtainIBEParams (
      district, localCtx->mPolicyCtx, localCtx->mStorageCtx,
      localCtx->mTransportCtx);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtGetDistrictParam(district, VtDistrictParamDomainName,
      (Pointer*)&districtName);
    if (status != 0)
      break;
    
    VT_ASSERT(districtName != (unsigned char*)0);

    districtNameLen = Z2Strlen(districtName);
    
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltGetEmailAndDomain(identity, &address, &addressLen,
      &domain, &domainLen);
    if (status != 0)
      break;
    
    cmp = VoltCaseInsensitiveCompareBuffers(domain, domainLen,
      districtName, districtNameLen, libCtx);
      
    *external = (cmp != 0);

    VOLT_SET_FNCT_LINE(fnctLine)
    status = VoltZDM2GetZDMLocationAndVersion(district,
      zdmLocation, supportedVersion);
    if (status != 0)
      break;
    
#if 0
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtGetDistrictParam(district, VtDistrictParamExtensions,
      (Pointer *)&extensionList);
    if (status == VT_ERROR_GET_INFO_UNAVAILABLE)
    {
      status = 0;
      break;
    }
    if (status != 0)
      break;

    for (index = 0; index < extensionList->count; ++index)
    {
      /* Check for the new ZDR location extension */
      if ((extensionList->extensions[index].oid.len ==
        VoltParamExtZdrSrvOidBytesLen) &&
        (Z2Memcmp(extensionList->extensions[index].oid.data,
        newZdrLocationOid, VoltParamExtZdrSrvOidBytesLen) == 0))
      {
        zdmLocationItem = &(extensionList->extensions[index].value);
        foundNewZdrLocation = 1;
      }

      /* If we haven't found the new location, then check for
       * the old ZDR location extension
       */
      else if (!foundNewZdrLocation &&
        (extensionList->extensions[index].oid.len ==
        VoltParamExtOldZdrOidBytesLen) &&
        (Z2Memcmp(extensionList->extensions[index].oid.data,
        oldZdrLocationOid, VoltParamExtOldZdrOidBytesLen) == 0))
      {
        zdmLocationItem = &(extensionList->extensions[index].value);
      }
      
      /* Check for the version extension */
      else if ((extensionList->extensions[index].oid.len ==
        VoltParamExtZdrVersionOidBytesLen) &&
        (Z2Memcmp(extensionList->extensions[index].oid.data,
        zdrVersionOid, VoltParamExtZdrVersionOidBytesLen) == 0))
      {
        /* TODO: Real decoding logic */
        if ((extensionList->extensions[index].value.len != 3) ||
          (extensionList->extensions[index].value.data[0] != 2) ||
          (extensionList->extensions[index].value.data[1] != 1))
        {
          VOLT_SET_FNCT_LINE(fnctLine)
          VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
          status = VT_ERROR_INVALID_ZDM_VERSION;
          break;
        }
        *supportedVersion = extensionList->extensions[index].value.data[2];
      }
    }
    
    if (status != 0)
      break;
      
    if (zdmLocationItem != (VtItem*)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltDecodeTagAndLen(zdmLocationItem->data,
        libCtx, zdmLocationItem->len, &tag, &lengthLen, &lenLo, &lenHi,
        sizeof (unsigned int));
      if (status != 0)
        break;

      valueLen = (unsigned int)lenLo;
      
      *zdmLocation = (unsigned char*) Z3Malloc(valueLen + 1);
      if (*zdmLocation == (unsigned char*)0)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
        status = VT_ERROR_MEMORY;
        break;
      }
      
      Z2Memcpy(*zdmLocation, zdmLocationItem->data + lengthLen + 1, valueLen);
      (*zdmLocation)[valueLen] = 0;
    }
#endif
  }
  while (0);
  
  VOLT_LOG_ERROR_COMPARE(status, libCtx, status, errorType,
    fnctLine, "VoltZDM2GetDistrictParamInfo", (unsigned char*)0)
    
  return status;
}

static int VoltZDM2DetermineZDMLocation(
  VtZDMObject zdmObj
)
{
  int status = 0;
  int status2;
  VoltZDM2WriteLocalCtx* localCtx;
  VtLibCtx libCtx = (VtLibCtx)0;
  struct DistrictInfo {
    struct DistrictInfo*  next;
    unsigned char*        zdmLocation;
    int                   count;
  } *districtInfoList = (struct DistrictInfo*)0;
  struct DistrictInfo* districtInfo;
  struct DistrictInfo* nextDistrictInfo;
  int districtCount = 0;
  VtItem* defaultZDMLocationItem = (VtItem *)0;
  unsigned char* defaultZDMDomain = NULL;
  unsigned char* defaultZDMLocation = NULL;
  VtDistrictObject defaultZDMDistrict = (VtDistrictObject)0;
  int defaultZDMVersion;
  VoltZDM2RecipientInfo* recipientInfo;
  unsigned char* majorityZDMLocation = (unsigned char*)0;
  int majorityCount = 0;
  unsigned char* zdmLocation = (unsigned char*)0;
  unsigned int i;
  VtIdentityObject senderID = (VtIdentityObject)0;
  unsigned char* senderZDMLocation = (unsigned char*)0;
  int senderVersion;
  int senderExternal;
  int isSenderDistrict;
  unsigned char* defaultZDMLocationData = (unsigned char*)0;
  unsigned int defaultZDMLocationLength = 0;
  VtMpIntCtx mpIntCtx = (VtMpIntCtx)0;
  VOLT_DECLARE_FNCT_LINE(fnctLine)
  VOLT_DECLARE_ERROR_TYPE(errorType)

  VT_ASSERT(zdmObj != (VtZDMObject)0);
  
  libCtx = zdmObj->voltObject.libraryCtx;
  VT_ASSERT(libCtx != (VtLibCtx)0);
  localCtx = (VoltZDM2WriteLocalCtx*) zdmObj->localCtx;
  VT_ASSERT(localCtx != (VoltZDM2WriteLocalCtx*)0);
  
  if (localCtx->mZDMLocation != (unsigned char*)0)
    return 0;
    
  VOLT_SET_ERROR_TYPE(errorType, 0)

  do
  {
    if (localCtx->mPolicyCtx == (VtPolicyCtx)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      status = VT_ERROR_INVALID_POLICY_CTX;
      break;
    }

    /* Get the ZDM location for the default ZDM domain. This value
     * from the client policy is actually just the domain name
     * (i.e. not the ZDM location URL), so we still need to retrieve
     * the district parameters for the domain and extract the URL
     * to use for the ZDM location.
     */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = localCtx->mPolicyCtx->PolicyGetInfoAlloc(
      localCtx->mPolicyCtx, VOLT_POLICY_GET_DEFAULT_ZDR_LOCATION,
      (Pointer)0, (Pointer *)&defaultZDMLocationItem);
    if (status != 0)
      break;

    if (defaultZDMLocationItem != (VtItem*)0)
    {
      defaultZDMLocationData = defaultZDMLocationItem->data;
      defaultZDMLocationLength = defaultZDMLocationItem->len;
    }
    
    /* Copy the VtItem over to a null-terminated string */
    defaultZDMDomain = (unsigned char*)
      Z3Malloc(defaultZDMLocationLength + 1);
    if (defaultZDMDomain == (unsigned char*)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
      status = VT_ERROR_MEMORY;
      break;
    }
    Z2Memcpy(defaultZDMDomain, defaultZDMLocationData,
      defaultZDMLocationLength);
    defaultZDMDomain[defaultZDMLocationLength] = 0;
    
    /* Get the sender's identity */
    VOLT_SET_FNCT_LINE(fnctLine)
    status = VtGetSecureArchiveParam(localCtx->mSecureArchive,
      VtSecureArchiveParamSignerId, (Pointer*)&senderID);
    if (status != 0)  
      break;
    
    VT_ASSERT(defaultZDMDomain != (unsigned char*)0);
    
    if (Z2Strlen(defaultZDMDomain) > 0)
    {
      /* If there's any problem getting the default ZDM location,
       * we convert the error to the INVALID_ZDM_LOCATION error.
       */
      status = VT_ERROR_INVALID_ZDM_LOCATION;
      
      if (senderID != (VtIdentityObject)0)
        mpIntCtx = senderID->mpCtx;
      
      VOLT_SET_FNCT_LINE(fnctLine)
      status2 = VtCreateDistrictObject(libCtx,
        (mpIntCtx != 0) ? VtDistrictImplMpCtx : VtDistrictImplBasic,
        (Pointer)mpIntCtx, &defaultZDMDistrict);
      if (status2 != 0)
        break;
      
      VOLT_SET_FNCT_LINE(fnctLine)
      status2 = VtSetDistrictParam(defaultZDMDistrict,
        VtDistrictParamDomainName, (Pointer) defaultZDMDomain);
      if (status2 != 0)
        break;

      VOLT_SET_FNCT_LINE(fnctLine)
      status2 = VtObtainIBEParams(defaultZDMDistrict, localCtx->mPolicyCtx,
        localCtx->mStorageCtx, localCtx->mTransportCtx);
      if (status2 != 0)
        break;
      
      VOLT_SET_FNCT_LINE(fnctLine)
      status2 = VoltZDM2GetZDMLocationAndVersion(defaultZDMDistrict,
        &defaultZDMLocation, &defaultZDMVersion);
      if (status2 != 0)
        break;
      
      if (defaultZDMLocation == (unsigned char*)0)
      {
        VOLT_SET_FNCT_LINE(fnctLine)
        VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
        break;
      }
    }
    
    /* Get the sender's ZDM location */
    if (senderID != (VtIdentityObject)0)
    {
      VOLT_SET_FNCT_LINE(fnctLine)
      status = VoltZDM2GetDistrictParamInfo(zdmObj, senderID,
        &senderZDMLocation, &senderVersion, &senderExternal);
      if (status != 0)
        break;
    }
    
    /* Loop over recipients and keep track of which district/ZDM location
     * is used by the majority of the recipients.
     */
    for (i = 0; i < localCtx->mRecipientInfoList.mCount; i++)
    {
      recipientInfo = &localCtx->mRecipientInfoList.mInfoArray[i];
      
      if (recipientInfo->mZDMLocation != (unsigned char*)0)
      {
        /* See if it matches the default ZDM location. If it does, then
         * we know we will use the default ZDM location, so we don't need
         * to check the rest of the recipients.
         */
        if ((defaultZDMLocation != NULL) &&
            (Stricmp(recipientInfo->mZDMLocation, defaultZDMLocation, libCtx) == 0))
        {
          localCtx->mZDMLocation = defaultZDMLocation;
          defaultZDMLocation = NULL;
          break;
        }
        
        /* See if there's an existing district info entry for this recipient.
         * If there is, just bump the count for that entry.
         */
        districtInfo = districtInfoList;
        while (districtInfo != NULL)
        {
          if (Stricmp(districtInfo->zdmLocation,
            recipientInfo->mZDMLocation, libCtx) == 0)
          {
            districtInfo->count++;
            break;
          }
          districtInfo = districtInfo->next;
        }
        
        // If we didn't find an existing entry, create a new one.
        if (districtInfo == (struct DistrictInfo*)0)
        {
          districtInfo = (struct DistrictInfo*) Z3Malloc(sizeof(struct DistrictInfo));
          if (districtInfo == (struct DistrictInfo*)0)
          {
            VOLT_SET_FNCT_LINE(fnctLine)
            VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
            status = VT_ERROR_MEMORY;
            break;
          }
          districtInfo->zdmLocation = recipientInfo->mZDMLocation;
          districtInfo->count = 1;
          districtInfo->next = districtInfoList;
          districtInfoList = districtInfo;
          districtCount++;
        }
        
        VT_ASSERT(districtInfo != (struct DistrictInfo*)0);
        
        isSenderDistrict = (senderZDMLocation != (unsigned char*)0) &&
          (Z2Stricmp(senderZDMLocation, districtInfo->zdmLocation) == 0);
          
        // Check to see if the current ZDM location now has the majority
        if (!isSenderDistrict && (districtInfo->count > majorityCount))
        {
          majorityZDMLocation = districtInfo->zdmLocation;
          majorityCount = districtInfo->count;
        }
      }
    }
    
    if ((status != 0) || (localCtx->mZDMLocation != (unsigned char*)0))
      break;
    
    //printf("Majority ZDM location is: \"%s\"\n", (majorityZDMLocation != 0) ? majorityZDMLocation : "null");
    //printf("Default ZDM location is: \"%s\"\n", (defaultZDMLocation != 0) ? defaultZDMLocation : "null");
    
    /* If there are no districts that have ZDM enabled, then we can just sk

⌨️ 快捷键说明

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