📄 sdapass.c
字号:
/*__________________________________________________________________________
Copyright (C) 2000 Networks Associates Technology, Inc.
All rights reserved.
$Id: sdapass.c,v 1.12 2002/08/28 02:06:10 wjb Exp $
__________________________________________________________________________*/
#include "DecodeStub.h"
#include "shlobj.h"
#include "SpaceEdit.h"
#include "InfoBar.h"
#define NOANIMNUM 8
#define NOANIMTIMER 444
#define NOANIMINTERVAL 40
static int NoAnim[NOANIMNUM]={-3,3,3,-3,-3,3,3,-3};
// global variable structure for re-entrancy
typedef struct _GPP
{
LPSTR pszPassPhrase;
BOOL bHideText;
HICON hInfoIcon;
int tries;
SDAHEADER *SDAHeader;
PGPUInt32 *ExpandedKey;
PGPUInt32 NoAnimIndex;
char *pszDir;
} GPP;
void FreePassphrases(GPP *gpp)
{
if(gpp->pszPassPhrase)
{
free(gpp->pszPassPhrase);
gpp->pszPassPhrase=NULL;
}
}
// SetCapsLockMessageState shows or hides the caps lock message as needed.
void SetCapsLockMessageState(HWND hdlg)
{
if (GetKeyState(VK_CAPITAL) & 1)
{
ShowWindow(GetDlgItem(hdlg,IDC_CAPSWARNING),SW_SHOW);
}
else
{
ShowWindow(GetDlgItem(hdlg,IDC_CAPSWARNING),SW_HIDE);
}
}
void ClearPassphrases(HWND hDlg,GPP *gpp)
{
HWND hwndPhrase1;
hwndPhrase1=GetDlgItem(hDlg,IDC_PHRASE1);
SEWipeEditBox (hwndPhrase1);
SetFocus (hwndPhrase1);
}
BOOL BrowseForOutputDirectory(HWND hwnd,GPP *gpp)
{
BROWSEINFO bi;
LPITEMIDLIST pidl;
LPMALLOC pMalloc;
char sz[256];
// get task allocator
if (SHGetMalloc(&pMalloc) != NOERROR)
return FALSE;
// get prompt string
LoadString (g_hinst, IDS_DESTDIR, sz, sizeof(sz));
// initialize structure
bi.hwndOwner = hwnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = gpp->pszDir;
bi.lpszTitle = sz;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
// allow user to browse
pidl = SHBrowseForFolder (&bi);
if (pidl == NULL)
return FALSE;
SHGetPathFromIDList (pidl, gpp->pszDir);
pMalloc->lpVtbl->Free(pMalloc, pidl);
return TRUE;
}
BOOL ValidatePassphrase(GPP *gpp)
{
PGPSHAContext sha;
byte const *HashedPassphrase;
char PassCheckBytes[8];
PGPUInt8 j;
PGPUInt16 i, k;
// Hash passphrase w/ SHA
memset(&sha,0x00,sizeof(PGPSHAContext));
pgpSHAInit(&sha);
pgpSHAUpdate(&sha,
gpp->SDAHeader->Salt.saltBytes,
sizeof(gpp->SDAHeader->Salt.saltBytes));
// Hash the passphrase and a rotating byte counter the specified
// number of times into the hash field with the 8 bytes of salt
// from above.
k = (gpp->SDAHeader->hashReps);
for (i=0, j=0; i<k; i++, j++)
{
pgpSHAUpdate(&sha,gpp->pszPassPhrase,strlen(gpp->pszPassPhrase));
pgpSHAUpdate(&sha,&j, 1);
}
HashedPassphrase=pgpSHAFinalize(&sha);
gpp->ExpandedKey=(PGPUInt32 *)malloc(sizeof(PGPUInt32)*32);
memset(gpp->ExpandedKey,0x00,sizeof(PGPUInt32)*32);
// Expand hashed passphrase
(cipherCAST5.initKey)(gpp->ExpandedKey,
HashedPassphrase);
memcpy(PassCheckBytes,HashedPassphrase,8);
// Check if correct by decrypting checkbytes
(cipherCAST5.encrypt)(gpp->ExpandedKey,
(BYTE *)&PassCheckBytes,(BYTE *)PassCheckBytes);
// Compare the check bytes against the key
if (memcmp(PassCheckBytes,&(gpp->SDAHeader->CheckBytes),8)==0)
{
return TRUE;
}
memset(gpp->ExpandedKey,0x00,sizeof(PGPUInt32)*32);
free(gpp->ExpandedKey);
gpp->ExpandedKey=NULL;
return FALSE;
}
void ShiftDialog(HWND hdlg,int delta)
{
RECT rc;
GetWindowRect (hdlg, &rc);
rc.left=rc.left+delta;
SetWindowPos (hdlg, NULL,
rc.left,rc.top,
0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
void DoBadPassphraseShake(HWND hdlg,GPP *gpp)
{
if(gpp->NoAnimIndex==0)
{
ShiftDialog(hdlg,NoAnim[gpp->NoAnimIndex]);
gpp->NoAnimIndex++;
SetTimer (hdlg, NOANIMTIMER, NOANIMINTERVAL, NULL);
}
}
BOOL CALLBACK
DoCommonCalls (
HWND hDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
GPP *gpp;
switch (uMsg)
{
case WM_INITDIALOG:
{
SetWindowLong (hDlg, GWL_USERDATA, lParam);
gpp=(GPP *)lParam;
gpp->bHideText = TRUE;
CheckDlgButton (hDlg, IDC_HIDETYPING, BST_CHECKED);
SetCapsLockMessageState(hDlg);
SetForegroundWindow (hDlg);
gpp->hInfoIcon=LoadIcon(g_hinst,MAKEINTRESOURCE(IDI_INFOICON));
IBInit(GetDlgItem(hDlg,IDC_CAPSWARNING),gpp->hInfoIcon,FALSE);
IBInit(GetDlgItem(hDlg,IDC_PROMPTSTRING),gpp->hInfoIcon,TRUE);
// Force focus to passphrase box
SetFocus(GetDlgItem(hDlg, IDC_PHRASE1));
break;
}
case WM_KEYUP:
{
SetCapsLockMessageState(hDlg);
break;
}
case WM_TIMER:
{
gpp=(GPP *)GetWindowLong (hDlg, GWL_USERDATA);
if(gpp->NoAnimIndex!=NOANIMNUM)
{
ShiftDialog(hDlg,NoAnim[gpp->NoAnimIndex]);
gpp->NoAnimIndex++;
}
else
{
KillTimer(hDlg,NOANIMTIMER);
gpp->NoAnimIndex=0;
}
break;
}
case WM_QUIT:
case WM_CLOSE:
case WM_DESTROY:
{
HWND hwndPhrase1;
gpp=(GPP *)GetWindowLong (hDlg, GWL_USERDATA);
ClearPassphrases(hDlg,gpp);
hwndPhrase1=GetDlgItem(hDlg, IDC_PHRASE1);
SEDestroy(hwndPhrase1);
IBDestroy(GetDlgItem(hDlg, IDC_CAPSWARNING));
IBDestroy(GetDlgItem(hDlg, IDC_PROMPTSTRING));
DeleteObject(gpp->hInfoIcon);
EndDialog(hDlg,kPGPError_UserAbort);
break;
}
case WM_COMMAND:
{
gpp=(GPP *)GetWindowLong (hDlg, GWL_USERDATA);
switch(LOWORD (wParam))
{
case IDCANCEL:
EndDialog (hDlg, kPGPError_UserAbort);
break;
case IDC_HIDETYPING :
{
HWND hwndPhrase1;
hwndPhrase1=GetDlgItem(hDlg, IDC_PHRASE1);
if (IsDlgButtonChecked (hDlg, IDC_HIDETYPING)
== BST_CHECKED)
gpp->bHideText = TRUE;
else
gpp->bHideText = FALSE;
SEChangeHideTyping (hwndPhrase1, gpp->bHideText);
break;
}
}
break;
}
}
return FALSE;
}
VOID
SpaceEditCallback(HWND hwndEdit,void *pUserValue)
{
GPP *gpp;
BOOL bShowCapsWarning;
HWND hdlg;
HWND hwndPhrase1;
hdlg=GetParent(hwndEdit);
hwndPhrase1=GetDlgItem(hdlg,IDC_PHRASE1);
gpp=(GPP *)pUserValue;
bShowCapsWarning=SEGetShowWarning(hwndEdit);
if(bShowCapsWarning)
{
ShowWindow(GetDlgItem(hdlg,IDC_CAPSWARNING),SW_SHOW);
}
else
{
ShowWindow(GetDlgItem(hdlg,IDC_CAPSWARNING),SW_HIDE);
}
}
BOOL CALLBACK
pgpPassphraseDlgProc (
HWND hDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
GPP *gpp;
INT i;
BOOL Common;
Common=DoCommonCalls (hDlg,uMsg,wParam,lParam);
if(Common)
return Common;
switch (uMsg)
{
case WM_INITDIALOG:
{
char szPrompt[256];
gpp=(GPP *)GetWindowLong (hDlg, GWL_USERDATA);
SEInit(GetDlgItem (hDlg, IDC_PHRASE1),gpp->bHideText,
SpaceEditCallback,gpp);
LoadString (g_hinst, IDS_PASSPROMPT, szPrompt, sizeof(szPrompt));
SetWindowText(GetDlgItem(hDlg,IDC_PROMPTSTRING),szPrompt);
SetWindowText(GetDlgItem(hDlg,IDC_DIRECTORY),gpp->pszDir);
return FALSE;
}
case WM_COMMAND:
{
gpp=(GPP *)GetWindowLong (hDlg, GWL_USERDATA);
switch(LOWORD (wParam))
{
/*
case IDC_NAI:
{
char szURL[100];
LoadString (g_hinst, IDS_NAIURL, szURL, sizeof(szURL));
PGPsdaWebBrowse (szURL);
break;
}
*/
case IDC_DIRBROWSE:
{
BrowseForOutputDirectory(hDlg,gpp);
SetWindowText(GetDlgItem(hDlg,IDC_DIRECTORY),gpp->pszDir);
break;
}
case IDOK:
{
DWORD dwAttrib;
GetWindowText(GetDlgItem(hDlg,IDC_DIRECTORY),gpp->pszDir,MAX_PATH);
dwAttrib = GetFileAttributes (gpp->pszDir);
if ((dwAttrib==0xFFFFFFFF)||
(!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)))
{
ClearPassphrases(hDlg,gpp);
// Path isn't a directory, error out
MessageBox(hDlg,
"Invalid output directory specified","PGPsda",
MB_OK|MB_SETFOREGROUND|MB_ICONSTOP);
break;
}
// Add a final slash
if(strlen(gpp->pszDir)!=0)
{
if(gpp->pszDir[strlen(gpp->pszDir)-1]!='\\')
{
strcat(gpp->pszDir,"\\");
}
}
FreePassphrases(gpp);
i = SEGetTextLength(GetDlgItem(hDlg,IDC_PHRASE1))+1;
gpp->pszPassPhrase = (char *)malloc (i);
if (gpp->pszPassPhrase)
{
SEGetText(GetDlgItem(hDlg,IDC_PHRASE1),gpp->pszPassPhrase, i);
ClearPassphrases(hDlg,gpp);
if(ValidatePassphrase(gpp))
{
FreePassphrases(gpp);
EndDialog (hDlg, kPGPError_NoErr);
}
else
{
char szPrompt[256];
FreePassphrases(gpp);
LoadString (g_hinst, IDS_WRONGPASS, szPrompt, sizeof(szPrompt));
SetWindowText(GetDlgItem(hDlg,IDC_PROMPTSTRING),szPrompt);
IBSetHighlightMode(GetDlgItem(hDlg, IDC_PROMPTSTRING),FALSE);
DoBadPassphraseShake(hDlg,gpp);
gpp->tries++;
}
break;
}
// Couldn't allocate passphrases
ClearPassphrases(hDlg,gpp);
FreePassphrases(gpp);
EndDialog (hDlg, kPGPError_OutOfMemory);
break;
}
}
break;
}
}
return FALSE;
}
// Just a simple decryption
PGPError
SDAPassphraseDialog(HWND hwnd,
SDAHEADER *SDAHeader,
PGPUInt32 **ExpandedKey,
char *pszDir)
{
PGPError err;
GPP gpp;
int idd;
OSVERSIONINFO osvi;
memset(&gpp,0x00,sizeof(GPP));
gpp.SDAHeader=SDAHeader;
gpp.pszDir=pszDir;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx (&osvi);
if ((osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
(osvi.dwMinorVersion == 0))
{
idd = IDD_PASSPHRASEWIN95;
}
else
{
idd = IDD_PASSPHRASE;
}
err = DialogBoxParam (g_hinst,
MAKEINTRESOURCE (idd),
hwnd,
(DLGPROC)pgpPassphraseDlgProc, (LPARAM)&gpp);
*ExpandedKey=gpp.ExpandedKey;
return(err);
}
/*__Editor_settings____
Local Variables:
tab-width: 4
End:
vi: ts=4 sw=4
vim: si
_____________________*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -