clgenwiz.c

来自「PGP8.0源码 请认真阅读您的文件包然后写出其具体功能」· C语言 代码 · 共 2,833 行 · 第 1/5 页

C
2,833
字号
/*____________________________________________________________________________
	Copyright (C) 2002 PGP Corporation
	All rights reserved.

	CLgenwiz.c - handle wizard for creating new keys

	$Id: CLgenwiz.c,v 1.115 2002/11/22 03:12:09 pbj Exp $
____________________________________________________________________________*/
#include "pgpPFLConfig.h"

// project header files
#include "PGPclx.h"
#include "SpaceEdit.h"
#include "UTF8Edit.h"

// pgp header files
#include "pgpRandomPool.h"
#include "pgpUserInterface.h"
#include "pgpReconstruct.h"
#include "pgpUnicode.h"

// pgp disk files
#define PGPDISK_DYNLINK

#include "pgpDiskEngine.h"
#include "pgpDiskUI.h"

#include <stdio.h>

// system header files
#include <process.h>
#include <lmaccess.h>
#include <lmerr.h>

// constant definitions
#define TASK_KEYGEN				0
#define TASK_SUBKEYGEN			1
#define TASK_EXCHANGELOOKUP		2
#define TASK_SENDTOSERVER		3
#define TASK_REQUESTCERT		4
#define TASK_SENDRECONSTRUCT	5
#define MAX_TASKS				6

#define ICON_NONE				0
#define ICON_ARROW				1
#define ICON_CHECK				2
#define ICON_ERROR				3

#define MAX_FULL_NAME_LEN		126
#define MAX_EMAIL_LEN			126

#define BADPHRASE_LENGTH		0x01
#define BADPHRASE_QUALITY		0x02
#define BADPHRASE_CONFIRMATION	0x04
#define BADPHRASE_SMARTCARD		0x08

#define DEFAULT_MIN_KEY_SIZE	1024
#define MIN_RSA_KEY_SIZE		1024
#define MAX_RSA_KEY_SIZE		4096
#define MIN_RSA_LEGACY_KEY_SIZE	1024
#define MAX_RSA_LEGACY_KEY_SIZE	2048
#define MIN_DHDSS_KEY_SIZE		1024
#define MAX_DHDSS_KEY_SIZE		4096
#define MIN_EC_KEY_SIZE			283
#define MAX_EC_KEY_SIZE			283

#define DEFAULT_MINPHRASELEN	8
#define DEFAULT_MINPHRASEQUAL	60
#define DEFAULT_KEYSIZE			2048
#define DEFAULT_SUBKEYSIZE		2048
#define DEFAULT_KEYALGORITHM	kPGPPublicKeyAlgorithm_DSA
#define DEFAULT_EXPIREDAYS		0

#define PROGRESS_TIMER			2345L
#define LEDTIMERPERIOD			150L
#define NUMLEDS					10
#define LEDSPACING				2

#define CL_M_GENERATION_COMPLETE		WM_APP+97

#define KGWIZ_INTRO		0
#define KGWIZ_NAME		1
#define KGWIZ_EXPERT	2
#define KGWIZ_EXCHANGE	3
#define KGWIZ_CORPOPTS	4
#define KGWIZ_PHRASE	5
#define KGWIZ_BADPHRASE	6
#define KGWIZ_RECON		7
#define KGWIZ_ENTROPY   8
#define KGWIZ_PROGRESS	9
#define KGWIZ_DONE		10
#define NUM_WIZ_PAGES	11


// external globals
extern HINSTANCE		g_hInst;
extern PGPContextRef	g_context;
extern HIMAGELIST		g_hilClientLib;
extern BOOL				g_bUnicodeOS;

// local globals
static HHOOK	s_hhookKeyboard;
static HHOOK	s_hhookMouse;

static HHOOK	s_hhookCBT;
static HHOOK	s_hhookGetMessage;
static HHOOK	s_hhookMsgFilter;

static BOOL		s_bHooksInstalled		= FALSE;
static HWND		s_hwndCollectEntropy	= NULL;

// typedefs
typedef struct _KeyGenInfo
{
	PGPContextRef		context;
	PGPtlsContextRef	tlscontext;
	HWND				hwndWiz;
	HWND				hwndParent;
	PGPKeyDBRef			keydbMain;

	BOOL				bRecenter;
	HBITMAP				hbitmapIntro;
	HBITMAP				hbitmapDone;
	HPALETTE			hpaletteIntro;
	HPALETTE			hpaletteDone;
	HFONT				hfontIntro;
	HFONT				hfontInterior;
	HICON				hiconAlert;
	HICON				hiconDoing;
	HICON				hiconDone;
	HICON				hiconNotDone;

	BOOL				bExpertMode;
	BOOL				bIsExchangeUser;
	BOOL				bLookupExchangeUserID;
	BOOL				bAutoAddExchangeUserID;
	LPSTR				pszFullName;
	LPSTR				pszEmail;
	LPSTR				pszNotesFullName;
	LPSTR				pszNotesEmail;
	LPSTR				pszUserID;
	LPSTR				pszExchangeUserID;
	LPSTR				pszPassPhrase;

	PGPKeyDBObjRef		keyADK;
	PGPKeyDBObjRef		keyCorp;
	PGPKeyDBObjRef		keyRevoker;
	PGPKeyDBObjRef		keyRootCA;
	BOOL				bEnforceADK;
	BOOL				bMetaCorpKey;
	UINT				uMetaTrustLevel;

	BOOL				bPreset;
	BOOL				bDoSend;
	BOOL				bDoCertRequest;
	BOOL				bDoReconstruction;
	BOOL				bAllowRSAGen;
	BOOL				bAllowECGen;
	BOOL				bMinPhraseQuality;
	INT					iMinPhraseQuality;
	BOOL				bMinPhraseLength;
	INT					iMinPhraseLength;
	BOOL				bAddNotesUserID;

	UINT				uMinKeySize;
	UINT				uPhraseFlags;
	UINT				uKeyType;
	UINT				uKeyAlg;
	UINT				uKeyFlags;
	UINT				uKeySize;
	UINT				uSubKeyAlg;
	UINT				uSubKeyFlags;
	UINT				uSubKeySize;
	UINT				uExpireDays;
	BOOL				bOnToken;
	SYSTEMTIME			stPresetExpiration;

	HWND				hwndExpirationDate;
	LONG				lRandomBitsNeeded;
	LONG				lOriginalEntropy;
	PGPKeyDBObjRef		keyNew;

	PGPReconPrompts		reconPrompts;
	PGPReconPasses		reconPasses;

	UINT				uNumTasks;
	UINT				uCurrentTask;
	BOOL				bTaskError;
	UINT				uTask[MAX_TASKS];
	BOOL				bTaskComplete;

	BOOL				bFinishSelected;
	BOOL				bCancelPending;
	BOOL				bCancelled;
	BOOL				bInGeneration;

	INT					iStatusValue;
	INT					iStatusDirection;

	INT					iFinalResult;
} KEYGENINFO, *PKEYGENINFO;

//	____________________________________
//
// Secure memory allocation routines
//

static VOID*
secAlloc (UINT uBytes)
{
	return (PGPNewSecureData (
				PGPPeekContextMemoryMgr (g_context),
				uBytes, 0));
}


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


//	____________________________________
//
//  Hook procedure for WH_KEYBOARD hook

static LRESULT CALLBACK
sWizKeyboardHookProc (
		INT		iCode,
		WPARAM	wParam,
		LPARAM	lParam)
{
	if (iCode >= 0)
	{
		// only use key presses (not releases)
		if (!(lParam & 0x80000000))
		{
			PGPGlobalRandomPoolAddKeystroke (wParam);
			if (s_hwndCollectEntropy)
			{
				if (wParam != VK_ESCAPE)
				{
					PostMessage (s_hwndCollectEntropy, WM_MOUSEMOVE, 0, 0);
					return 1;
				}
			}
		}
	}

	return 0;
}


//	____________________________________
//
//  Hook procedure for WH_MOUSE hook

static LRESULT CALLBACK
sWizMouseHookProc (
		INT		iCode,
		WPARAM	wParam,
		LPARAM	lParam)
{
	if (iCode >= 0)
		PGPGlobalRandomPoolMouseMoved ();

	return 0;
}


//	____________________________________
//
//  Hook procedure for various hooks -- used to prevent
//  passing the hook info on to next hook procedure

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


//	____________________________________
//
//  set the message hooks for gathering entropy

static VOID
sInstallEntropyHooks (void)
{
	DWORD	dwThreadID;

	dwThreadID = GetCurrentThreadId ();

	// keyboard hook is to trap entropy
	s_hhookKeyboard = SetWindowsHookEx (
			WH_KEYBOARD, sWizKeyboardHookProc, NULL, dwThreadID);

	// mouse hook is to trap entropy
	s_hhookMouse = SetWindowsHookEx (
			WH_MOUSE, sWizMouseHookProc, NULL, dwThreadID);
}


//	____________________________________
//
//  set the message hooks for preventing snooping

static VOID
sInstallSecurityHooks (void)
{
	DWORD	dwThreadID;

	dwThreadID = GetCurrentThreadId ();

	s_hhookCBT = SetWindowsHookEx (
			WH_CBT, sWizGenericHookProc, NULL, dwThreadID);
	s_hhookGetMessage = SetWindowsHookEx (
			WH_GETMESSAGE, sWizGenericHookProc, NULL, dwThreadID);
	s_hhookMsgFilter = SetWindowsHookEx (
			WH_MSGFILTER, sWizGenericHookProc, NULL, dwThreadID);
}


//	____________________________________
//
//  remove the entropy message hooks

static VOID
sUninstallEntropyHooks (void)
{
	UnhookWindowsHookEx (s_hhookMouse);
	UnhookWindowsHookEx (s_hhookKeyboard);
}


//	____________________________________
//
//  remove the security message hooks

static VOID
sUninstallSecurityHooks (void)
{
	UnhookWindowsHookEx (s_hhookMsgFilter);
	UnhookWindowsHookEx (s_hhookGetMessage);
	UnhookWindowsHookEx (s_hhookCBT);
}


//	______________________________________________________
//
//	Convert time to string format based on system settings

static INT
sPGPTimeToDays (
		PGPTime			pgptime,
		SYSTEMTIME*		pst)
{
	struct tm*	ptm				= NULL;
	INT			iDays			= 0;
	time_t		ttTime;

	if (pgptime > 0)
	{
		ttTime = PGPGetStdTimeFromPGPTime (pgptime);
		ptm = localtime (&ttTime);
	}

	if (ptm)
	{
		CLTimeToSystemTime (ptm, pst);
		PGPclSystemTimeToDays (pst, &iDays);
	}

	return iDays;
}

//	____________________________________
//
//  return DSA key size on basis of requested ElGamal key size

static ULONG
sGetDSAKeySize (ULONG ulRequested)
{
	ULONG ulActualBits = ulRequested;

	if (ulRequested > 1024)
		ulActualBits = 1024;

	return ulActualBits;
}


//	____________________________________
//
//  load up admin prefs

static BOOL
sValidateConfiguration (PKEYGENINFO pkgi)
{
	BOOL		bPrefsOK	= TRUE;
	PGPPrefRef	prefref		= kInvalidPGPPrefRef;

	PGPError	err;
	PGPUInt32	u;

	PGPByte*	pbyte;
	PGPSize		size;
	PGPBoolean	b;

	pkgi->uMinKeySize		= DEFAULT_MIN_KEY_SIZE;
	pkgi->uSubKeySize		= DEFAULT_SUBKEYSIZE;
	pkgi->pszPassPhrase		= NULL;
	pkgi->uPhraseFlags		= 0;
	pkgi->lRandomBitsNeeded	= 0;
	pkgi->lOriginalEntropy	= 0;
	pkgi->keyNew			= kInvalidPGPKeyDBObjRef;
	pkgi->bFinishSelected	= FALSE;
	pkgi->bCancelPending	= FALSE;
	pkgi->bCancelled		= FALSE;
	pkgi->bInGeneration		= FALSE;
	pkgi->bTaskComplete		= FALSE;
	pkgi->iFinalResult		= 0;

	err = PGPclPeekClientLibPrefRefs (&prefref, NULL);
	if (IsPGPError (err))
		return FALSE;

	// Use preset keygen parameters?
	if(PGPlnEnterprise())
	{
	b = FALSE;
	PGPGetPrefBoolean (prefref, kPGPPrefPresetKeyGeneration, &b);
	pkgi->bPreset = b;
	}
	else
	{
	pkgi->bPreset = FALSE;
	}

	// Preset algorithm
	if(PGPlnEnterprise())
	{
		u = DEFAULT_KEYALGORITHM;
		PGPGetPrefNumber (prefref, kPGPPrefKeyPublicKeyAlgorithm, &u);
		pkgi->uKeyType = u;
	}
	else
	{
		pkgi->uKeyType = DEFAULT_KEYALGORITHM;
	}
	if (pkgi->uKeyType == kPGPPublicKeyAlgorithm_DSA)
	{
		pkgi->uKeyAlg = kPGPPublicKeyAlgorithm_DSA;
		pkgi->uKeyFlags = kPGPKeyPropertyFlags_UsageSign;
		pkgi->uSubKeyAlg = kPGPPublicKeyAlgorithm_ElGamal;
		pkgi->uSubKeyFlags = kPGPKeyPropertyFlags_UsageEncrypt;
	}
	else if (pkgi->uKeyType == kPGPPublicKeyAlgorithm_RSASignOnly)
	{
		pkgi->uKeyAlg = kPGPPublicKeyAlgorithm_RSA;
		pkgi->uKeyFlags = kPGPKeyPropertyFlags_UsageSign;
		pkgi->uSubKeyAlg = kPGPPublicKeyAlgorithm_RSA;
		pkgi->uSubKeyFlags = kPGPKeyPropertyFlags_UsageEncrypt;
	}
	else if (pkgi->uKeyType == kPGPPublicKeyAlgorithm_RSA)
	{
		pkgi->uKeyAlg = kPGPPublicKeyAlgorithm_RSA;
		pkgi->uKeyFlags = 0;
		pkgi->uSubKeyAlg = 0;
		pkgi->uSubKeyFlags = 0;
	}
	else
	{
		pkgi->uKeyAlg = kPGPPublicKeyAlgorithm_ECSign;
		pkgi->uKeyFlags = kPGPKeyPropertyFlags_UsageSign;
		pkgi->uSubKeyAlg = kPGPPublicKeyAlgorithm_ECEncrypt;
		pkgi->uSubKeyFlags = kPGPKeyPropertyFlags_UsageEncrypt;
	}

	// Preset size
	u = DEFAULT_KEYSIZE;
	if(PGPlnEnterprise())
	{
		PGPGetPrefNumber (prefref, kPGPPrefKeySize, &u);
	}
	if (pkgi->uKeyType == kPGPPublicKeyAlgorithm_DSA)
	{
	 	pkgi->uKeySize = sGetDSAKeySize (u);
	 	pkgi->uSubKeySize = u;
	}
	else
	{
	 	pkgi->uKeySize = u;
	 	pkgi->uSubKeySize = u;
	}

	// Preset expiration
	if(PGPlnEnterprise())
	{
		u = 0;
		PGPGetPrefNumber (prefref, kPGPPrefKeyExpiration, &u);
		if (u != 0)
		{
			u += PGPGetTime ();
			u = (UINT) sPGPTimeToDays ((PGPTime)u, &pkgi->stPresetExpiration);
		}
		pkgi->uExpireDays = u;
	}
	else
	{
//#else
		pkgi->uExpireDays = DEFAULT_EXPIREDAYS;
	}

	// Smart Card
	if(PGPlnEnterprise())
	{
		b = FALSE;
		PGPGetPrefBoolean (prefref, kPGPPrefGenerateKeysOnSmartCard, &b);
		pkgi->bOnToken = b;
		if (pkgi->bOnToken)
		{
			CHAR	szDriver[MAX_PATH];
			PGPGetPrefStringBuffer (prefref, kPGPPrefPKCS11DrvFile,
					sizeof (szDriver), szDriver);

			if (szDriver[0] == '\0')
			{
				bPrefsOK = FALSE;
				PGPclMessageBox (pkgi->hwndParent, IDS_CAPTIONERROR,
						IDS_SMARTCARDNOTCONFIGURED, MB_OK|MB_ICONSTOP);
			}
			else
			{
				UINT	uNumTokens;

				PGPCountTokens (g_context, &uNumTokens);
				if (uNumTokens == 0)
				{
					bPrefsOK = FALSE;
					PGPclMessageBox (pkgi->hwndParent, IDS_CAPTIONERROR,
							IDS_SMARTCARDNOTINSERTED, MB_OK|MB_ICONSTOP);
				}
			}
		}
	}
	else
	{
		pkgi->bOnToken = FALSE;
	}

	// minimum passphrase length
	if(PGPlnEnterprise())
	{
		b = FALSE;
		PGPGetPrefBoolean (prefref, kPGPPrefEnforceMinChars, &b);
		pkgi->bMinPhraseLength = b;
		if (b)
		{

⌨️ 快捷键说明

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