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

📄 passwordmanager.cpp

📁 voltage 公司提供的一个开发Ibe的工具包
💻 CPP
字号:
/* Copyright 2003-2005, Voltage Security, all rights reserved.
 */
#include "passwordmanager.h"
#include "PassDialog.h"

/* This is the password manager function. It will be called only with
 * collect, collect_retry or release options.
 */
int PasswordManager (
  VtLibCtx libCtx , 
  Pointer localInfo, 
  unsigned int purpose, 
  unsigned char **oldPassword, 
  unsigned int *oldPasswordLen, 
  unsigned char **password, 
  unsigned int *passwordLen)
{   
  static PasswordStatus *passwordList = (PasswordStatus *)0;
  static int maxTries = 0 ;
  int status;
  HWND hwnd = GetDesktopWindow ();
  CPassDialog dlg;
	dlg.m_check = true;	
	
  if (oldPassword != (unsigned char **)0)
    *oldPassword = (unsigned char *)0;
  if (oldPasswordLen != (unsigned int *)0)
    *oldPasswordLen = 0; 

  status = 0;
  switch (purpose)
  {
    //COLLECT means give the password that password manager currently has
    //without asking the user interactively.
  case VT_PASSWORD_MGR_PURPOSE_COLLECT : 
    maxTries = 1;
    status = GetPasswordForLocation (
      passwordList, localInfo, 
      password, passwordLen, 
      oldPassword, oldPasswordLen);
    if (status == 1)
    {
      status = 0;
      break;
    }

    /* This is the First time it has been called ..Ask the user for password
    */
    if (dlg.DoModal(hwnd) != IDOK)
    {
      status = VT_ERROR_GET_PASSWORD;
      break;
    }    
    status = AddLocationPasswordToPasswordList (
      &passwordList, localInfo, dlg.m_pass,
      password, passwordLen,
      oldPassword, oldPasswordLen);
    break;
   
  case VT_PASSWORD_MGR_PURPOSE_COLLECT_RETRY :   
    if (maxTries == NUM_MAX_PASSWORD_TRIES)
      return ERROR_MAX_PASSWORD_TRIES_REACHED;

    MessageBox (NULL, WRONG_PASSWORD_MSG, "Toolkit Token Handler", MB_OK);

    /* Ask the user for password again.
    */
    if (dlg.DoModal(hwnd) != IDOK)
    {
      status = VT_ERROR_GET_PASSWORD;
      break;
    }    
    status = ResetPasswordFordLocation (
      passwordList, localInfo, dlg.m_pass, 
      password, passwordLen,
      oldPassword, oldPasswordLen);
    maxTries ++;
    break;
    
  case VT_PASSWORD_MGR_PURPOSE_RELEASE :  
    break;

  case PASSWORD_MGR_PURPOSE_DESTROY :
    PasswordStatus *node, *temp;

    if (passwordList != (PasswordStatus *)0)
    {
      node = passwordList;
      while (node != (PasswordStatus *)0)
      {
        temp = node->next;        
        free (node);
        node = temp;
      }
    }
    break;

  default :
    return VT_ERROR_UNSUPPORTED;
  } //switch

  return status;
}

int AddLocationPasswordToPasswordList (
  PasswordStatus **storeList, 
  unsigned char *location,
  char *password,
  unsigned char **currPassword,
  unsigned int *currPasswordLen,
  unsigned char **oldPassword,
  unsigned int *oldPasswordLen
  )
{
  PasswordStatus *ptr = (PasswordStatus *)0;
  PasswordStatus *temp;
  unsigned char *tempPtr;
  int len, bufferSize, offset, passLen;

  passLen = 0;
  len = 0;
  if (location  != (unsigned char *)0)
    len = (int)strlen ( (const char *)location);
  if (password != (char *)0)
    passLen = (int)strlen ( (const char *)password );

  bufferSize = sizeof (PasswordStatus) + len + 1;
  ptr = (PasswordStatus *)malloc (bufferSize);
  if (ptr == (PasswordStatus *)0 )
    return VT_ERROR_MEMORY;
  
  ptr->oldPassword[0] = 0;
  ptr->oldPaswordLen = 0;
  memcpy (ptr->password, password, passLen);
  ptr->password[passLen] = 0;  
  ptr->passwordLen = passLen;
  ptr->next = (PasswordStatus *)0;
  ptr->location = (unsigned char *)0;
  tempPtr = (unsigned char *)ptr;  
  offset = sizeof (PasswordStatus);  

  /* Copy the location
   */
  if (location != (unsigned char *)0)
  {
    memcpy (tempPtr + offset, location, len + 1);
    ptr->location = tempPtr + offset;
  }

  /* return the pointers to current and old password
   */
  *currPassword = (unsigned char *) ptr->password;
  *currPasswordLen =  ptr->passwordLen;
  *oldPassword = (unsigned char *) ptr->oldPassword;
  *oldPasswordLen = ptr->oldPaswordLen;  

  /* If this is the first time we are adding a location the first
   * node is the beginning of the list.
   */
  if (*storeList == (PasswordStatus *)0)
  {
    *storeList = ptr;
    return 0;
  }

  /* Add this node to the end of the password list
   */
  temp = *storeList;
  while (temp->next != (PasswordStatus *)0)
    temp = temp->next;

  temp->next = ptr;
  return 0;
}

int ResetPasswordFordLocation (
  PasswordStatus *storeList, 
  unsigned char *location,
  char *newPassword,
  unsigned char **password,
  unsigned int *passwordLen,
  unsigned char **oldPassword,
  unsigned int *oldPasswordLen
  )
{ 
  unsigned int passLen;
  PasswordStatus *node;

  if (storeList == (PasswordStatus *)0)
    return VT_ERROR_NULL_ARG;

  passLen = 0;
  if (newPassword != (char *)0)
    passLen = (int)strlen ( (const char *)newPassword);

  node = storeList;
  do
  {
    /* if any of them NULL and the other is not just continue. If both NULL return 
    */
    if (location == (unsigned char *)0 ||
       node->location == (unsigned char *)0)
    {
      /* This test will pass only if both location and node->location
       * are NULL.
       */
      if (node->location == location)
        break;

      continue;
    }

    /* Both of them are non-NULL. So do a comparision.
     */
    if (strcmp ((const char *)node->location , (const char *)location) == 0)
      break;    

    node = node->next;

  } while (node != (PasswordStatus *)0);

  if (node == (PasswordStatus *)0)
    return VT_ERROR_ENTRY_NOT_FOUND ;
  
  memcpy (node->oldPassword, node->password, node->passwordLen);
  node->oldPassword[node->passwordLen] = 0;
  node->oldPaswordLen = node->passwordLen;
  memcpy (node->password, newPassword, passLen);
  node->password[passLen] = 0;
  node->passwordLen = passLen;

  /* return the pointers to current and old password
   */
  *password = (unsigned char *)node->password;
  *passwordLen = node->passwordLen;
  *oldPassword = (unsigned char *)node->oldPassword;
  *oldPasswordLen = node->oldPaswordLen;

  return 0;
}

int GetPasswordForLocation (
  PasswordStatus *storeList, 
  unsigned char *location,
  unsigned char **password,
  unsigned int *passwordLen,
  unsigned char **oldPassword,
  unsigned int *oldPasswordLen
  )
{  
  PasswordStatus *node;

  if (storeList == (PasswordStatus *)0 )
    return 0;

  node = storeList;  
  do
  {
    /* if any of them NULL and the other is not just continue. If both NULL return 
    */
    if (location == (unsigned char *)0 ||
       node->location == (unsigned char *)0)
    {
      if (node->location == location)
        break;

      continue;
    }

    /* Both of them are non-NULL. So do a comparision.
     */
    if (strcmp ((const char *)node->location , (const char *)location) == 0)
      break;    

    node = node->next;

  } while (node != (PasswordStatus *)0);

  /* if location was not found return 0
   */
  if (node == (PasswordStatus *)0)
    return 0;

  *password = (unsigned char *)node->password;
  *passwordLen = node->passwordLen ;
  *oldPassword = (unsigned char *)node->oldPassword ;
  *oldPasswordLen = node->oldPaswordLen ;

  return 1;
}

⌨️ 快捷键说明

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