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

📄 pgppassphrasedialog.cpp

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*____________________________________________________________________________
	Copyright (C) 2002 PGP Corporation
	All rights reserved.
	
	$Id: PGPPassphraseDialog.cpp,v 1.32 2002/11/12 20:52:43 pbj Exp $
____________________________________________________________________________*/

#include "stdio.h"
#include "stdlib.h"
#include "PGPui32.h"
#include "pgpDialogs.h"
#include "pgpPassphraseUtils.h"
#include "PGPCL.h"
#include "pgpSDKUILibPriv.h"
#include "SpaceEdit.h"
#include "UTF8Edit.h"
#include "InfoBar.h"
#include "pgpUnicode.h"
#include "pgpUnicodeWin32.h"

#define MAXDECRYPTIONNAMECHAR		36
#define MAXUSERIDSTRING				kPGPMaxUserIDSize + 32

// 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;
	WNDPROC				wpOrigPhrase1Proc;  
	WNDPROC				wpOrigPhrase2Proc;  
	INT					iNextTabControl;
	BOOL				bHideText;
	HWND				hwndQuality;
	HWND				hwndMinQuality;
	PGPContextRef		context;
	HWND				hwndOptions;
	const CPGPPassphraseDialogOptions *options;
	HWND				hwndOptionsControl;
	int					NoAnimIndex;
	HICON				hInfoIcon;
	BOOL				bNoOK;
} 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
	807,				IDH_INPUTISTEXT, // Hardcoded in ClientLib
    0,0 
}; 

#define NOANIMNUM 8
#define NOANIMTIMER 444
#define NOANIMINTERVAL 40
static int NoAnim[NOANIMNUM]={-3,3,3,-3,-3,3,3,-3};

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.chm");
}

//	________________________________
//
//  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) 
	{
		// only use key presses, not releases
		if (!(lParam & 0x80000000))
			PGPGlobalRandomPoolAddKeystroke (wParam);
	}

	return 0;
} 
  
//	___________________________________
//
//  Hook procedure for WH_JOURNALRECORD hook 

 
LRESULT CALLBACK 
GenericHookProc (
		INT		iCode, 
		WPARAM	wParam, 
		LPARAM	lParam) 
{ 
	return 0;
} 

LRESULT CALLBACK 
GetMessageHookProc (
		INT		iCode, 
		WPARAM	wParam, 
		LPARAM	lParam) 
{ 
	MSG*	pMsg	= (MSG*)lParam;

	if (pMsg->message >= 0xc000)
		return CallNextHookEx (hhookGetMessage, iCode, wParam, lParam);
	else
		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, 
		GetMessageHookProc, 
		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 =  PGPPeekContextMemoryMgr (context);
	return (PGPNewSecureData (memmgr, uBytes, 0));
}


VOID 
secFree (VOID* p) 
{
	if (p) {
		FillMemory ((char *)p,lstrlen((char *)p), '\0');
		PGPFreeData ((char *)p);
	}
}

void FreePassphrases(GPP *gpp)
{
	if(gpp->pszPassPhrase)
	{
		secFree(gpp->pszPassPhrase);
		gpp->pszPassPhrase=NULL;
	}

	if(gpp->pszPassPhraseConf)
	{
		secFree(gpp->pszPassPhraseConf);
		gpp->pszPassPhraseConf=NULL;
	}
}

void ClearPassphrases(HWND hDlg,GPP *gpp)
{
	HWND hwndPhrase1,hwndPhrase2;

	if(gpp->pszPassPhraseConf)
	{
		secFree(gpp->pszPassPhraseConf);
		gpp->pszPassPhraseConf=NULL;
	}

	hwndPhrase1=GetDlgItem(hDlg,IDC_PHRASE1);
	hwndPhrase2=GetDlgItem(hDlg,IDC_PHRASE2);

	SEWipeEditBox (hwndPhrase1);
	SEWipeEditBox (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 [512];

	LoadString (gPGPsdkUILibInst, iCaption, szCaption, sizeof(szCaption));
	LoadString (gPGPsdkUILibInst, iMessage, szMessage, sizeof(szMessage));

	ulFlags |= MB_SETFOREGROUND;
	return (MessageBox (hWnd, szMessage, szCaption, ulFlags));
}

//	____________________________
//
//  Truncate text string

VOID
TruncateKeyText (HDC	hdc, 
				 LPSTR	pszOrig, 
				 LPSTR	szInfo, 
				 INT	iXmax, 
				 LPSTR	pszTrunc) 
{
	SIZE	s;
	INT		l, iW;
	UINT	u, ulen;
	WCHAR	wsz[MAXUSERIDSTRING];
	CHAR	sz[kPGPMaxUserIDSize];

	u = lstrlen (pszOrig);
	if (pgpIsntUTF8String (pszOrig, u))
	{
		PGPUInt32	u2;

		u2 = u;
		pgpLocalStringToUTF8 (0, 
				GetACP(), pszOrig, u2, sz, 
				sizeof(sz), &u);
	}
	else
		lstrcpyn (sz, pszOrig, u+1);

	pgpUTF8StringToUCS2 (sz, kPGPUnicodeNullTerminated,
			wsz, sizeof(wsz), &ulen);

	GetTextExtentPoint32 (hdc, szInfo, lstrlen (szInfo), &s);
	iXmax -= s.cx;

	if (iXmax <= 0) 
	{
		pszTrunc[0] = '\0';
		return;
	}

	lstrcpy (pszTrunc, pszOrig);
	GetTextExtentPoint32W (hdc, wsz, lstrlenW (wsz), &s);
	iW = s.cx + 4;

	l = lstrlenW (wsz);
	if (l < 3) 
	{
		if (iW > iXmax) 
			pszTrunc[0] = '\0';
	}
	else 
	{
		l = lstrlenW (wsz) - 3;
		while ((iW > iXmax) && (l >= 0)) 
		{
			lstrcpyW (&wsz[l], L"...");
			GetTextExtentPoint32W (hdc, wsz, lstrlenW (wsz), &s);
			iW = s.cx + 4;
			l--;
		}

		if (l < 0) 
			pszTrunc[0] = '\0';
		else
		{
			pgpUCS2StringToUTF8 (wsz, kPGPUnicodeNullTerminated, 
					pszTrunc, MAXUSERIDSTRING, NULL);
		}
	}

	lstrcat (pszTrunc, szInfo);
}

void GetKeyString(HDC hdc,INT iComboWidth,PGPKeyDBObjRef Key,char *szNameFinal)
{
	CHAR			sz1[32],sz2[32];
	CHAR			szName[MAXUSERIDSTRING];
	PGPUInt32		uAlgorithm,uKeyBits;
	UINT			u;

	PGPGetKeyDBObjNumericProperty (Key, kPGPKeyProperty_AlgorithmID, (int *)&uAlgorithm);

	// get key type / size info to append to name
	lstrcpy (sz2, "   (");
	switch (uAlgorithm) 
	{
		case kPGPPublicKeyAlgorithm_RSA :
			LoadString (gPGPsdkUILibInst, IDS_RSA, sz1, sizeof(sz1));
			lstrcat (sz2, sz1);
			lstrcat (sz2, "/");
			PGPGetKeyDBObjNumericProperty (Key, kPGPKeyProperty_Bits, (int *)&uKeyBits);
			wsprintf (sz1, "%i", uKeyBits);
			lstrcat (sz2, sz1);
			break;

		case kPGPPublicKeyAlgorithm_DSA :
			LoadString (gPGPsdkUILibInst, IDS_DSS, sz1, sizeof(sz1));
			lstrcat (sz2, sz1);
			lstrcat (sz2, "/");
			PGPGetKeyDBObjNumericProperty (Key, kPGPKeyProperty_Bits, (int *)&uKeyBits);
			wsprintf (sz1, "%i", uKeyBits);
			lstrcat (sz2, sz1);
			break;

		default :
			LoadString (gPGPsdkUILibInst, IDS_UNKNOWN, sz1, sizeof(sz1));
			lstrcat (sz2, sz1);
			lstrcat (sz2, "/");
			lstrcat (sz2, sz1);
			break;
	}
		
	lstrcat (sz2, ")");

	// get name on key
	PGPGetPrimaryUserIDName(Key, szName, sizeof(szName), &u);
	TruncateKeyText (hdc, szName, sz2, iComboWidth, szNameFinal);
}


//	____________________________
//
//  setup key display list box

BOOL 
AddKeySetToRecipientsTable (HWND hDlg, 
							PGPKeySetRef KeySet,
							CPGPDecryptionPassphraseDialogOptions *options) 
{
	BOOL			bAtLeastOneValidSecretKey	= FALSE;
	UINT			uUnknownKeys				= 0;
	PGPKeyListRef	KeyList;
	PGPKeyIterRef	KeyIter;
	PGPKeyDBObjRef	Key, SubKey;
	UINT			u, uIndex, uKeyBits, uAlgorithm;
	INT				iKeyDefault, iKeySelected;
	PGPBoolean		bSecret, bCanDecrypt;
	CHAR			szName[MAXUSERIDSTRING];
	CHAR			szNameFinal[MAXUSERIDSTRING];
	CHAR			sz[32], sz2[32];
	HDC				hdc;
	RECT			rc;
	HFONT			hfontOld;

	PGPOrderKeySet (KeySet, kPGPKeyOrdering_Validity, FALSE, &KeyList);
	PGPNewKeyIter (KeyList, &KeyIter);

	iKeySelected = -1;
	iKeyDefault = -1;

	GetClientRect (GetDlgItem (hDlg, IDC_KEYLISTBOX), &rc);
	hdc = GetDC (GetDlgItem (hDlg, IDC_KEYLISTBOX));
	hfontOld = (HFONT)SelectObject (hdc, (HFONT)GetStockObject (DEFAULT_GUI_FONT));

	PGPKeyIterNextKeyDBObj(KeyIter, kPGPKeyDBObjType_Key, &Key);
	while (Key) {
		PGPGetKeyDBObjBooleanProperty(Key, kPGPKeyProperty_IsSecret, &bSecret);
		PGPGetKeyDBObjBooleanProperty(Key, kPGPKeyProperty_CanDecrypt, &bCanDecrypt);
		PGPGetKeyDBObjNumericProperty(Key, kPGPKeyProperty_AlgorithmID, (PGPInt32 *)&uAlgorithm);

		if (bSecret && bCanDecrypt) bAtLeastOneValidSecretKey = TRUE;

		// get name on key
		PGPGetPrimaryUserIDName(Key, szName, sizeof(szName), &u);

		// append key type / size info to name
		lstrcpy (sz2, " (");

		switch (uAlgorithm) 
		{
			case kPGPPublicKeyAlgorithm_RSA :
				LoadString (gPGPsdkUILibInst, IDS_RSA, sz, sizeof(sz));
				lstrcat (sz2, sz);
				lstrcat (sz2, "/");
				PGPGetKeyDBObjNumericProperty (Key, kPGPKeyProperty_Bits, (int *)&uKeyBits);
				wsprintf (sz, "%i", uKeyBits);
				lstrcat (sz2, sz);
				break;

			case kPGPPublicKeyAlgorithm_DSA :
				LoadString (gPGPsdkUILibInst, IDS_DH, sz, sizeof(sz));
				lstrcat (sz2, sz);
				lstrcat (sz2, "/");
				PGPKeyIterNextKeyDBObj(KeyIter, kPGPKeyDBObjType_SubKey, &SubKey);
				if (SubKey) {
					PGPGetKeyDBObjNumericProperty(SubKey,
						kPGPSubKeyProperty_Bits, (PGPInt32 *)&uKeyBits);

					wsprintf (sz, "%i", uKeyBits);
					lstrcat (sz2, sz);
				}
				else lstrcat (sz2, "???");
				break;

			default :
				LoadString (gPGPsdkUILibInst, IDS_UNKNOWN, sz, sizeof(sz));
				lstrcat (sz2, sz);
				lstrcat (sz2, "/");
				lstrcat (sz2, sz);
				break;
		}
		lstrcat (sz2, ")");

		TruncateKeyText (hdc, szName, sz2, rc.right-rc.left, szNameFinal);

		uIndex = SendDlgItemMessage (hDlg, IDC_KEYLISTBOX, 
							LB_ADDSTRING, 0, (LPARAM)szNameFinal);
		PGPKeyIterNextKeyDBObj(KeyIter, kPGPKeyDBObjType_Key, &Key);
	}
	PGPFreeKeyIter (KeyIter);
	PGPFreeKeyList (KeyList);

	SelectObject (hdc, hfontOld);
	ReleaseDC (GetDlgItem (hDlg, IDC_KEYLISTBOX), hdc);

	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(PGPKeyDBRefIsValid( *(options->mNewKeys) ))
		{
			PGPKeySetRef keyset;
			PGPError err;
			err = PGPNewKeySet(*(options->mNewKeys), &keyset);
			if (err != kPGPError_NoErr) {
				AddKeySetToRecipientsTable( hDlg, keyset, options );
				PGPFreeKeySet(keyset);
			}
		}

	if( IsntNull( options->mMissingKeyIDList ) )

⌨️ 快捷键说明

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