📄 pgppassphrasedialog.cpp
字号:
/*____________________________________________________________________________
Copyright (C) 1997 Network Associates Inc. and affiliated companies.
All rights reserved.
$Id: PGPPassphraseDialog.cpp,v 1.68 1999/04/08 13:29:37 pbj Exp $
____________________________________________________________________________*/
#include "stdio.h"
#include "stdlib.h"
#include "PGPui32.h"
#include "pgpDialogs.h"
#include "pgpPassphraseUtils.h"
#include "PGPCL.h"
#include "pgpSDKUILibPriv.h"
#define MAXDECRYPTIONNAMECHAR 36
// local globals
HHOOK hhookKeyboard;
HHOOK hhookMouse;
HHOOK hhookCBT;
HHOOK hhookGetMessage;
HHOOK hhookMsgFilter;
// global variable structure for re-entrancy
typedef struct _GPP
{
LPSTR pszPassPhrase;
LPSTR pszPassPhraseConf;
LPSTR szDummy;
WNDPROC wpOrigPhrase1Proc;
WNDPROC wpOrigPhrase2Proc;
INT iNextTabControl;
BOOL bHideText;
HWND hwndQuality;
HWND hwndMinQuality;
PGPContextRef context;
HWND hwndOptions;
const CPGPPassphraseDialogOptions *options;
HWND hwndOptionsControl;
} GPP;
// Help IDs for passphrase dialogs
static DWORD aIds[] =
{
IDC_HIDETYPING, IDH_PGPCLPHRASE_HIDETYPING,
IDC_PHRASE1, IDH_PGPCLPHRASE_PHRASE,
IDC_PHRASE2, IDH_PGPCLPHRASE_CONFIRMATION,
IDC_SIGNKEYCOMBO, IDH_PGPCLPHRASE_SIGNINGKEY,
IDC_KEYLISTBOX, IDH_PGPCLPHRASE_KEYLIST,
IDC_PHRASEQUALITY, IDH_PGPCLPHRASE_QUALITY,
IDC_KEYNAME, IDH_PGPCLPHRASE_KEYNAME,
801, IDH_TEXTOUTPUT, // Hardcoded in ClientLib
804, IDH_DETACHEDSIG, // Hardcoded in ClientLib
0,0
};
PGPError
PGPsdkUIErrorBox (
HWND hWnd,
PGPError error)
{
PGPError err = kPGPError_NoErr;
CHAR szMessage[512];
char StrRes[500];
if (IsPGPError (error) && (error!=kPGPError_UserAbort))
{
PGPGetErrorString (error, sizeof(szMessage), szMessage);
LoadString (gPGPsdkUILibInst, IDS_PGPERROR, StrRes, sizeof(StrRes));
MessageBox (hWnd, szMessage, StrRes,
MB_OK|MB_ICONSTOP|MB_SETFOREGROUND);
}
return err;
}
// 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);
}
}
// Help file is located in C:\Windows\System
void GetHelpDir(char *szHelp)
{
GetSystemDirectory(szHelp,MAX_PATH);
strcat(szHelp,"\\Pgp.hlp");
}
// ________________________________
//
// Hook procedure for WH_MOUSE hook
LRESULT CALLBACK
MouseHookProc (
INT iCode,
WPARAM wParam,
LPARAM lParam)
{
MOUSEHOOKSTRUCT* pmhs;
if (iCode >= 0)
{
pmhs = (MOUSEHOOKSTRUCT*) lParam;
PGPGlobalRandomPoolMouseMoved ();
}
return 0;
}
// ___________________________________
//
// Hook procedure for WH_KEYBOARD hook
LRESULT CALLBACK
GetKBHookProc (
INT iCode,
WPARAM wParam,
LPARAM lParam)
{
if (iCode >= 0)
{
PGPGlobalRandomPoolAddKeystroke (wParam);
}
return 0;
}
// ___________________________________
//
// Hook procedure for WH_JOURNALRECORD hook
LRESULT CALLBACK
GenericHookProc (
INT iCode,
WPARAM wParam,
LPARAM lParam)
{
return 0;
}
LRESULT CALLBACK
CBTHookProc (
INT iCode,
WPARAM wParam,
LPARAM lParam)
{
if (iCode >= 0)
{
switch (iCode)
{
case HCBT_SETFOCUS:
return CallNextHookEx (hhookCBT, iCode, wParam, lParam);
break;
}
}
return 0;
}
void InitRandomKeyHook(HHOOK* phhookKeyboard, HHOOK* phhookMouse)
{
DWORD dwPhraseThreadID;
dwPhraseThreadID = GetCurrentThreadId ();
// the mouse and keyboard hooks trap entropy
*phhookMouse = SetWindowsHookEx (WH_MOUSE,
MouseHookProc,
NULL, dwPhraseThreadID);
*phhookKeyboard = SetWindowsHookEx (WH_KEYBOARD,
GetKBHookProc,
NULL, dwPhraseThreadID);
}
void UninitRandomKeyHook(HHOOK hhookKeyboard, HHOOK hhookMouse)
{
UnhookWindowsHookEx (hhookKeyboard);
UnhookWindowsHookEx (hhookMouse);
}
void InstallSecurityHooks(void)
{
DWORD dwPhraseThreadID;
dwPhraseThreadID = GetCurrentThreadId ();
// these are just to prevent sniffers from seeing these messages
hhookCBT = SetWindowsHookEx (WH_CBT,
CBTHookProc,
NULL, dwPhraseThreadID);
hhookGetMessage = SetWindowsHookEx (WH_GETMESSAGE,
GenericHookProc,
NULL, dwPhraseThreadID);
hhookMsgFilter = SetWindowsHookEx (WH_MSGFILTER,
GenericHookProc,
NULL, dwPhraseThreadID);
}
void UninstallSecurityHooks(void)
{
UnhookWindowsHookEx (hhookMsgFilter);
UnhookWindowsHookEx (hhookGetMessage);
UnhookWindowsHookEx (hhookCBT);
}
PGPUInt32
PGPEstimatePassphraseQuality(const char *passphrase)
{
return( pgpEstimatePassphraseQuality( passphrase ) );
}
//___________________________
//
// Secure memory allocation routines
//
VOID*
secAlloc (PGPContextRef context, UINT uBytes)
{
PGPMemoryMgrRef memmgr;
memmgr = PGPGetContextMemoryMgr (context);
return (PGPNewSecureData (memmgr, uBytes, 0));
}
VOID
secFree (VOID* p)
{
if (p) {
FillMemory ((char *)p,lstrlen((char *)p), '\0');
PGPFreeData ((char *)p);
}
}
// __________________
//
// Wipe edit box clean
VOID
WipeEditBox (
GPP *gpp,
HWND hDlg,
UINT uID)
{
CHAR* p;
INT i;
i = SendDlgItemMessage (hDlg, uID, WM_GETTEXTLENGTH, 0, 0);
if (i > 0) {
p = (char *)secAlloc (gpp->context,i+1);
if (p) {
FillMemory (p, i, ' ');
SendDlgItemMessage (hDlg, uID, WM_SETTEXT, 0, (LPARAM)p);
secFree (p);
}
}
}
void FreePassphrases(GPP *gpp)
{
if(gpp->pszPassPhrase)
{
secFree(gpp->pszPassPhrase);
gpp->pszPassPhrase=NULL;
}
if(gpp->pszPassPhraseConf)
{
secFree(gpp->pszPassPhraseConf);
gpp->pszPassPhraseConf=NULL;
}
if(gpp->szDummy)
{
secFree(gpp->szDummy);
gpp->szDummy=NULL;
}
}
void ClearPassphrases(HWND hDlg,GPP *gpp)
{
HWND hwndPhrase1,hwndPhrase2;
if(gpp->pszPassPhraseConf)
{
secFree(gpp->pszPassPhraseConf);
gpp->pszPassPhraseConf=NULL;
}
if(gpp->szDummy)
{
secFree(gpp->szDummy);
gpp->szDummy=NULL;
}
hwndPhrase1=GetDlgItem(hDlg,IDC_PHRASE1);
hwndPhrase2=GetDlgItem(hDlg,IDC_PHRASE2);
if(hwndPhrase1)
{
WipeEditBox (gpp,hDlg, IDC_PHRASE1);
SetWindowText (hwndPhrase1, "");
}
if(hwndPhrase2)
{
WipeEditBox (gpp,hDlg, IDC_PHRASE2);
SetWindowText (hwndPhrase2, "");
}
SetFocus (hwndPhrase1);
}
// ___________________________________________________
//
// Message box routine using string table resource IDs
LRESULT
PGPsdkUIMessageBox (
HWND hWnd,
INT iCaption,
INT iMessage,
ULONG ulFlags)
{
CHAR szCaption [128];
CHAR szMessage [256];
LoadString (gPGPsdkUILibInst, iCaption, szCaption, sizeof(szCaption));
LoadString (gPGPsdkUILibInst, iMessage, szMessage, sizeof(szMessage));
ulFlags |= MB_SETFOREGROUND;
return (MessageBox (hWnd, szMessage, szCaption, ulFlags));
}
// ____________________________
//
// setup key display list box
BOOL
AddKeySetToRecipientsTable (HWND hDlg,
PGPKeySetRef KeySet,
CPGPDecryptionPassphraseDialogOptions *options)
{
BOOL bAtLeastOneValidSecretKey = FALSE;
UINT uUnknownKeys = 0;
PGPKeyListRef KeyList;
PGPKeyIterRef KeyIter;
PGPKeyRef Key;
PGPSubKeyRef SubKey;
UINT u, uIndex, uKeyBits, uAlgorithm;
INT iKeyDefault, iKeySelected;
PGPBoolean bSecret, bCanDecrypt;
CHAR szName[kPGPMaxUserIDSize];
CHAR sz[128];
PGPOrderKeySet (KeySet, kPGPValidityOrdering, &KeyList);
PGPNewKeyIter (KeyList, &KeyIter);
iKeySelected = -1;
iKeyDefault = -1;
PGPKeyIterNext (KeyIter, &Key);
while (Key)
{
PGPGetKeyBoolean (Key, kPGPKeyPropIsSecret, &bSecret);
PGPGetKeyBoolean (Key, kPGPKeyPropCanDecrypt, &bCanDecrypt);
PGPGetKeyNumber (Key, kPGPKeyPropAlgID, (int *)&uAlgorithm);
if (bSecret && bCanDecrypt) bAtLeastOneValidSecretKey = TRUE;
// get name on key
PGPGetPrimaryUserIDNameBuffer (Key, sizeof(szName),
szName, &u);
if (u > MAXDECRYPTIONNAMECHAR)
{
u = MAXDECRYPTIONNAMECHAR;
lstrcat (szName, "...");
}
else
szName[u] = '\0';
// append key type / size info to name
lstrcat (szName, " (");
switch (uAlgorithm)
{
case kPGPPublicKeyAlgorithm_RSA :
LoadString (gPGPsdkUILibInst, IDS_RSA, sz, sizeof(sz));
lstrcat (szName, sz);
lstrcat (szName, "/");
PGPGetKeyNumber (Key, kPGPKeyPropBits, (int *)&uKeyBits);
wsprintf (sz, "%i", uKeyBits);
lstrcat (szName, sz);
break;
case kPGPPublicKeyAlgorithm_DSA :
LoadString (gPGPsdkUILibInst, IDS_DH, sz, sizeof(sz));
lstrcat (szName, sz);
lstrcat (szName, "/");
PGPKeyIterNextSubKey (KeyIter, &SubKey);
if (SubKey) {
PGPGetSubKeyNumber (SubKey, kPGPKeyPropBits,
(int *)&uKeyBits);
wsprintf (sz, "%i", uKeyBits);
lstrcat (szName, sz);
}
else lstrcat (szName, "???");
break;
default :
LoadString (gPGPsdkUILibInst, IDS_UNKNOWN, sz, sizeof(sz));
lstrcat (szName, sz);
lstrcat (szName, "/");
lstrcat (szName, sz);
break;
}
lstrcat (szName, ")");
uIndex = SendDlgItemMessage (hDlg, IDC_KEYLISTBOX,
LB_ADDSTRING, 0, (LPARAM)szName);
PGPKeyIterNext (KeyIter, &Key);
}
PGPFreeKeyIter (KeyIter);
PGPFreeKeyList (KeyList);
return bAtLeastOneValidSecretKey;
}
BOOL
InitEncryptedToKeyListBox (HWND hDlg, CPGPDecryptionPassphraseDialogOptions *options)
{
BOOL bAtLeastOneValidSecretKey;
SendMessage(GetDlgItem(hDlg, IDC_KEYLISTBOX),LB_RESETCONTENT,0,0);
if(PGPKeySetRefIsValid( options->mKeySet ))
bAtLeastOneValidSecretKey = AddKeySetToRecipientsTable( hDlg,options->mKeySet,options );
if(options->mNewKeys!=NULL)
if(PGPKeySetRefIsValid( *(options->mNewKeys) ))
AddKeySetToRecipientsTable( hDlg,*(options->mNewKeys),options );
if( IsntNull( options->mMissingKeyIDList ) )
{
char MsgTxt[255];
char StrRes[500];
LoadString (gPGPsdkUILibInst, IDS_NUMUNKNOWNKEYS, StrRes, sizeof(StrRes));
sprintf(MsgTxt,StrRes,options->mMissingKeyIDCount);
SendDlgItemMessage (hDlg, IDC_KEYLISTBOX, LB_INSERTSTRING, -1,
(LPARAM)MsgTxt);
}
if(!bAtLeastOneValidSecretKey)
{
EnableWindow (GetDlgItem (hDlg, IDC_PHRASE1), FALSE);
ShowWindow (GetDlgItem (hDlg, IDC_PHRASE1), SW_HIDE);
EnableWindow (GetDlgItem (hDlg, IDC_HIDETYPING), FALSE);
ShowWindow (GetDlgItem (hDlg, IDC_HIDETYPING), SW_HIDE);
EnableWindow (GetDlgItem (hDlg, IDOK), FALSE);
ShowWindow (GetDlgItem (hDlg, IDC_PROMPTSTRING), SW_HIDE);
ShowWindow (GetDlgItem (hDlg, IDC_CANNOTDECRYPTTEXT),SW_SHOW);
}
else
{
EnableWindow (GetDlgItem (hDlg, IDC_PHRASE1), TRUE);
ShowWindow (GetDlgItem (hDlg, IDC_PHRASE1), SW_SHOW);
EnableWindow (GetDlgItem (hDlg, IDC_HIDETYPING), TRUE);
ShowWindow (GetDlgItem (hDlg, IDC_HIDETYPING), SW_SHOW);
EnableWindow (GetDlgItem (hDlg, IDOK), TRUE);
ShowWindow (GetDlgItem (hDlg, IDC_PROMPTSTRING), SW_SHOW);
ShowWindow (GetDlgItem (hDlg, IDC_CANNOTDECRYPTTEXT),SW_HIDE);
}
return bAtLeastOneValidSecretKey;
}
// ____________________________
//
// Truncate text string
VOID
TruncateKeyText (HDC hdc,
LPSTR pszOrig,
LPSTR szInfo,
INT iXmax,
LPSTR pszTrunc)
{
SIZE s;
INT l, iW;
GetTextExtentPoint32 (hdc, szInfo, lstrlen (szInfo), &s);
iXmax -= s.cx;
if (iXmax <= 0) {
lstrcpy (pszTrunc, "");
return;
}
lstrcpy (pszTrunc, pszOrig);
GetTextExtentPoint32 (hdc, pszOrig, lstrlen (pszOrig), &s);
iW = s.cx + 4;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -