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

📄 defaultstorageprov.c

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

      /* Get the file name.
       */
      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_IBE_PRI_KEY, &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 return to the open call is ERROR, there was no file to
     * open, this function indicates no key found.
     */
    if (status == VT_ERROR_FILE_DOES_NOT_EXIST)
    {
      status = VT_ERROR_ENTRY_NOT_FOUND;
      break;
    }

    /* Any other error pass on.
     */
    if (status != 0)
      break;

    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;

    /* If there is a password, we don't need it anymore.
     */
    if (password != (unsigned char *)0)
      defStorageCtx->ReleaseExtraPassword (ctx, &password, &passwordLen);
    password = (unsigned char *)0;

    /* The contents should be the DER of the private key.
     */
    berInfo.derCoders = derCoders;
    berInfo.derCoderCount = 1;
    berInfo.berEncoding = contents;
    berInfo.maxEncodingLen = contentsLen;
    berInfo.storageCtx = storageCtx;
    berInfo.policyCtx = policyCtx;
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtSetKeyParam (keyObj, VtKeyParamBer, (Pointer)&berInfo);

  } while (0);

  if (password != (unsigned char *)0)
    defStorageCtx->ReleaseExtraPassword (ctx, &password, &passwordLen);
  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);

  if (status == 0)
    return 0;

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, errorType, fnctLine,
    "VoltDefaultRetrieveIBEPrivateKey", (char *)0)
  
  return (status);
}

int VoltDefaultRetrievePrivateSigningKey (
   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 *)0;
  VoltIdentityObject *idObj = (VoltIdentityObject *)reference;
  VoltKeyObject *key = (VoltKeyObject *)keyObj;
  VoltFileHandle fileHandle = (VoltFileHandle)0;
  unsigned char *fileName = (unsigned char *)0;
  unsigned char *password = (unsigned char *)0;
  VtFileNameList *nameList = (VtFileNameList *)0;
  unsigned char *contents = (unsigned char *)0;
  unsigned char *nameToUse;
  VtSetKeyBerInfo berInfo;
  VtDerCoder *derCoders[1] = { VtDerCoderDSAPrivateKey };
  VOLT_DECLARE_FNCT_LINE (fnctLine)
  VOLT_DECLARE_ERROR_TYPE (errorType)

  /* If this function was called by the VtStorageImplNull 
   * simply return Vt_ERROR_NO_STORAGE_PROVIDER_LOADED
   */
  if (ctx->providerNum == -2)
    return VT_ERROR_NO_STORAGE_PROVIDER_LOADED;

  fileCtx = (VoltFileCtx *)defStorageCtx->fCtx;
  Z2Memset (&berInfo, 0, sizeof (VtSetKeyBerInfo));
  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, VOLT_STORE_DIR_SIGN_KEYS_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_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_STORAGE_REF;
      if (idObj->encoding.data == (unsigned char *)0)
        break;

      /* Get the file name.
       */
      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, &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 return to the open call is ERROR, there was no file to
     * open, this function indicates no key found.
     */
    if (status == VT_ERROR_FILE_DOES_NOT_EXIST)
    {
      status = VT_ERROR_ENTRY_NOT_FOUND;
      break;
    }

    /* Any other error pass on.
     */
    if (status != 0)
      break;

    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;

    /* If there is a password, we don't need it anymore.
     */
    if (password != (unsigned char *)0)
      defStorageCtx->ReleaseExtraPassword (ctx, &password, &passwordLen);
    password = (unsigned char *)0;

    /* The contents should be the DER of the private key.
     */
    berInfo.derCoders = derCoders;
    berInfo.derCoderCount = 1;
    berInfo.berEncoding = contents;
    berInfo.maxEncodingLen = contentsLen;
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtSetKeyParam (keyObj, VtKeyParamBer, (Pointer)&berInfo);

  } while (0);

  if (password != (unsigned char *)0)
    defStorageCtx->ReleaseExtraPassword (ctx, &password, &passwordLen);
  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);

  if (status == 0)
    return 0;

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, errorType, fnctLine,
    "VoltDefaultRetrievePrivateSigningKey", (char *)0)

  return (status);
}

int VoltDefaultRetrievePublicSigningCert (
   VtStorageCtx storageCtx,
   VtIdentityObject reference,
   unsigned int index,
   VtCertObject certObj
   )
{
  int status;
  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 };
  VOLT_DECLARE_FNCT_LINE (fnctLine)
  VOLT_DECLARE_ERROR_TYPE (errorType)

  /* If this function was called by the VtStorageImplNull 
   * simply return Vt_ERROR_NO_STORAGE_PROVIDER_LOADED
   */
  if (ctx->providerNum == -2)
    return VT_ERROR_NO_STORAGE_PROVIDER_LOADED;

  /* 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)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetStorageFileListAlloc (
        ctx, VOLT_STORE_DIR_SIGN_CERTS, VOLT_STORE_DIR_SIGN_CERTS_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_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_STORAGE_REF;
      if (idObj->encoding.data == (unsigned char *)0)
        break;

      /* Get the file name.
       */
      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_CERT, &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 return to the open call is ERROR, there was no file to
     * open, this function indicates no key found.
     */
    if (status == VT_ERROR_FILE_DOES_NOT_EXIST)
    {
      status = VT_ERROR_ENTRY_NOT_FOUND;
      break;
    }

    /* Any other error pass on.
     */
    if (status != 0)
      break;

    /* Get the cert data
    */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = mIcLoadDataAlloc (
      ctx, fileCtx, fileHandle, (unsigned char *)0, 0, &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;
    VOLT_SET_FNCT_LINE (fnctLine)
    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);

  if (status == 0)
    return 0;

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, errorType, fnctLine,
    "VoltDefaultRetrievePublicSigningCert", (char *)0)

  return (status);
}

int VoltDefaultDeleteCurrentDistrict (
   VtStorageCtx storageCtx,
   unsigned char *reference
   )
{
  int status;
  unsigned int fileNameLen;
  VoltStorageCtx *ctx = (VoltStorageCtx *)storageCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltDefaultStorageCtx *defStorageCtx =
    (VoltDefaultStorageCtx *)(ctx->localStorageCtx);
  VoltFileCtx *fileCtx = (VoltFileCtx *)0;
  unsigned char *fileName = (unsigned char *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine) 

  /* If this function was called by the VtStorageImplNull 
   * simply return Vt_ERROR_NO_STORAGE_PROVIDER_LOADED
   */
  if (ctx->providerNum == -2)
    return VT_ERROR_NO_STORAGE_PROVIDER_LOADED;

  fileCtx = (VoltFileCtx *)defStorageCtx->fCtx;
  do
  {
    /* Get the file name.
     */    
    V

⌨️ 快捷键说明

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