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

📄 cpassphrasedialog.cpp

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		rect.Top() + delta, 0, 0, SWP_NOSIZE | SWP_NOZORDER);

	mInfoBar1.ResizeRedraw();
	mCurHeightInfoBar1 = heightInfoBar;

	InvalidateRect(NULL, TRUE);
}

void 
CPassphraseDialog::Wiggle()
{
	CRect	rectCenter;
	GetWindowRect(rectCenter);

	CRect	rectLeft, rectRight;

	rectLeft = rectRight = rectCenter;
	rectLeft.Offset(-3, 0);
	rectRight.Offset(3, 0);

	for (PGPUInt32 i = 0; i < kNumWiggles; i++)
	{
		CWindow	parent(GetParent());

		SetWindowPos(HWND_TOP, rectLeft.Left(), rectLeft.Top(), 0, 0, 
			SWP_NOSIZE | SWP_NOZORDER);

		UpdateWindow();
		parent.UpdateWindow();

		Sleep(kWiggleDelayMs);

		SetWindowPos(HWND_TOP, rectCenter.Left(), rectCenter.Top(), 0, 0, 
			SWP_NOSIZE | SWP_NOZORDER);

		UpdateWindow();
		parent.UpdateWindow();

		Sleep(kWiggleDelayMs);

		SetWindowPos(HWND_TOP, rectRight.Left(), rectRight.Top(), 0, 0, 
			SWP_NOSIZE | SWP_NOZORDER);

		UpdateWindow();
		parent.UpdateWindow();

		Sleep(kWiggleDelayMs);

		SetWindowPos(HWND_TOP, rectCenter.Left(), rectCenter.Top(), 0, 0, 
			SWP_NOSIZE | SWP_NOZORDER);

		UpdateWindow();
		parent.UpdateWindow();

		Sleep(kWiggleDelayMs);
	}
}

void 
CPassphraseDialog::TellBadPassphrase(PGPBoolean warn)
{
	PGPInt32	curIndex	= mInfoBar1.GetIndexOfId(kBadPassStringId);

	if (warn && (curIndex == -1))
	{
		mInfoBar1.AddString(0, UCommonStrings::Get(
			UCommonStrings::kTellBadPassphrase), kBadPassStringId);

		mInfoBar1.ResizeRedraw();
		Resize();
	}
	else if (!warn && (curIndex != -1))
	{
		mInfoBar1.RemoveString(curIndex);

		mInfoBar1.ResizeRedraw();
		Resize();
	}
}

void 
CPassphraseDialog::WarnCapsLock(PGPBoolean warn)
{
	PGPInt32	curIndex	= mInfoBar2.GetIndexOfId(kCapsLockStringId);

	if (warn && (curIndex == -1))
	{
		mInfoBar2.AddString(0, UCommonStrings::Get(
			UCommonStrings::kWarnCapsLockDown), kCapsLockStringId);
		mInfoBar2.ResizeRedraw();
	}
	else if (!warn && (curIndex != -1))
	{
		mInfoBar2.RemoveString(curIndex);
		mInfoBar2.ResizeRedraw();
	}
}

void 
CPassphraseDialog::SetCapsLockMessageState()
{
	if (GetKeyState(VK_CAPITAL) & 1)
		WarnCapsLock(TRUE);
	else
		WarnCapsLock(FALSE);
}

void 
CPassphraseDialog::JoinAKey()
{
	CPGPKey			splitKey;
	CSecureString	secPassphrase;
	PGPByte			*pPassphrase=NULL;
	PGPUInt32		length=0;
	PGPBoolean		bPassOK;
		
	CPGPKeyJoiner::AskForJoinedKey(*mPContext, Handle(), *mPSplitKeys, 
		splitKey, secPassphrase, &pPassphrase, &length);

//	mPassphraseEdit.SetContents(secPassphrase);

	bPassOK=ValidateJoinedPassphrase(pPassphrase,length);

	if(pPassphrase!=NULL)
	{
		memset(pPassphrase,0x00,length);
		PGPFreeData(pPassphrase);
	}

	if(bPassOK)
		CDialog::OnOk();
}

PGPBoolean 
CPassphraseDialog::ValidateJoinedPassphrase(
	PGPByte			*pPassphrase,
	PGPUInt32		length)
{
	CWaitCursor	waitCursor(UModuleInstance::Get());
	CComboError	reportErr;

	PGPBoolean	foundMatch			= FALSE;
	PGPBoolean	sawKeyNotInRing		= FALSE;
	PGPBoolean	sawUnexpectedError	= FALSE;

	// Put passphrase in secure buffer.
//	CSecureArray<char>	passphrase;
//	mPassphraseEdit.Contents().Extract(passphrase);

	CPGPdiskUserIter	userIter(*mPDiskUsers);
	CPGPdiskUser		user;

	// See if passphrase will unlock any of the users. Return the winner.
	while (userIter.Next(user))
	{
		if (user.IsDisabled())
			continue;

		try
		{
			user.EnableCiphering(*mPDisk, (const char *)pPassphrase,length);
			foundMatch = TRUE;
			break;
		}
		catch (CComboError& error)
		{
			if (error.pgpErr == kPGPClientError_BadDiskPassKeyNotInRing)
			{
				sawKeyNotInRing = TRUE;
			}
			else if (error.pgpErr != kPGPClientError_BadDiskPassphrase)
			{
				sawUnexpectedError = TRUE;
				reportErr = error;
			}
		}
	}

	if (foundMatch)
	{
		PGPUInt32	uCacheType;

		// utilize cache
		CPGPclPrefs	prefs(mPContext->PGPclPrefs());

		uCacheType = prefs.GetNumber(kPGPPrefCacheType);

		if (user.HasPublicKey() && IsntNull(pPassphrase) && 
			(uCacheType != kPGPPrefCacheTypeNone))
		{
			PGPUInt32	uCacheSecs;
			CPGPKeyID	pubKeyID;
			user.GetPGPKeyID(pubKeyID);

			CPGPKey	pubKey(mPContext->PGPKeyDB(), pubKeyID);

			uCacheSecs = prefs.GetNumber(kPGPPrefCacheSeconds);
			if (uCacheType == kPGPPrefCacheTypeLogon)
				uCacheSecs = kPGPMaxTimeInterval;

			PGPPassphraseIsValid(pubKey, 
				PGPOPasskeyBuffer(mPContext->PGPContext(), pPassphrase, length), 
				PGPOCachePassphrase(mPContext->PGPContext(), 
					uCacheSecs, prefs.GetBoolean(kPGPPrefShareCache)),
				PGPOLastOption(mPContext->PGPContext()));
		}

		mPUser->Attach(user);
		return TRUE;
	}
	else
	{
		using namespace UCommonStrings;

		// On passphrase match failure, let the user try again.
		if (sawUnexpectedError)
		{
			ReportError(kTellCantVerifyPassphrase, kSinglePassMsgBoxTitle, 
				reportErr, Handle());
		}
		else if (sawKeyNotInRing)
		{
			CMessageBox	messageBox;
			messageBox.Display(Get(kTellBadPassAndKeyNotInRing), 
				Get(kSinglePassMsgBoxTitle), Handle(), 
				CMessageBox::kWarningTone);
		}
		else
		{
			Wiggle();
		}

		Wiggle();
		TellBadPassphrase(TRUE);

		mPassphraseEdit.ClearContents();
		mPassphraseEdit.SetFocus();

		return FALSE;
	}
}

PGPBoolean 
CPassphraseDialog::ValidatePassphrase()
{
	CWaitCursor	waitCursor(UModuleInstance::Get());
	CComboError	reportErr;

	PGPBoolean	foundMatch			= FALSE;
	PGPBoolean	sawKeyNotInRing		= FALSE;
	PGPBoolean	sawUnexpectedError	= FALSE;

	// Put passphrase in secure buffer.
	CSecureArray<char>	passphrase;
	mPassphraseEdit.Contents().Extract(passphrase);

	CPGPdiskUserIter	userIter(*mPDiskUsers);
	CPGPdiskUser		user;

	// See if passphrase will unlock any of the users. Return the winner.
	while (userIter.Next(user))
	{
		if (user.IsDisabled())
			continue;

		try
		{
			user.EnableCiphering(*mPDisk, passphrase.Get(),0);
			foundMatch = TRUE;
			break;
		}
		catch (CComboError& error)
		{
			if (error.pgpErr == kPGPClientError_BadDiskPassKeyNotInRing)
			{
				sawKeyNotInRing = TRUE;
			}
			else if (error.pgpErr != kPGPClientError_BadDiskPassphrase)
			{
				sawUnexpectedError = TRUE;
				reportErr = error;
			}
		}
	}

	if (foundMatch)
	{
		PGPUInt32	uCacheType;

		// utilize cache
		CPGPclPrefs	prefs(mPContext->PGPclPrefs());

		uCacheType = prefs.GetNumber(kPGPPrefCacheType);

		if (user.HasPublicKey() && IsntNull(passphrase.Get()) && 
			(uCacheType != kPGPPrefCacheTypeNone))
		{
			PGPUInt32	uCacheSecs;
			CPGPKeyID	pubKeyID;
			user.GetPGPKeyID(pubKeyID);

			CPGPKey	pubKey(mPContext->PGPKeyDB(), pubKeyID);

			uCacheSecs = prefs.GetNumber(kPGPPrefCacheSeconds);
			if (uCacheType == kPGPPrefCacheTypeLogon)
				uCacheSecs = kPGPMaxTimeInterval;

			PGPPassphraseIsValid(pubKey, 
				PGPOPassphrase(mPContext->PGPContext(), passphrase.Get()), 
				PGPOCachePassphrase(mPContext->PGPContext(), 
					uCacheSecs, prefs.GetBoolean(kPGPPrefShareCache)),
				PGPOLastOption(mPContext->PGPContext()));
		}

		mPUser->Attach(user);
		return TRUE;
	}
	else
	{
		using namespace UCommonStrings;

		// On passphrase match failure, let the user try again.
		if (sawUnexpectedError)
		{
			ReportError(kTellCantVerifyPassphrase, kSinglePassMsgBoxTitle, 
				reportErr, Handle());
		}
		else if (sawKeyNotInRing)
		{
			CMessageBox	messageBox;
			messageBox.Display(Get(kTellBadPassAndKeyNotInRing), 
				Get(kSinglePassMsgBoxTitle), Handle(), 
				CMessageBox::kWarningTone);
		}
		else
		{
			Wiggle();
		}

		Wiggle();
		TellBadPassphrase(TRUE);

		mPassphraseEdit.ClearContents();
		mPassphraseEdit.SetFocus();

		return FALSE;
	}
}

void 
CPassphraseDialog::OnHideTypeCheck()
{
	mPassphraseEdit.SetHideTypingPref(mHideTypingCheck.GetCheck() == 
		BST_CHECKED);
	mPassphraseEdit.SetFocus();
}

void 
CPassphraseDialog::OnJoinButton()
{
	try
	{
		JoinAKey();
	}
	catch (CComboError& error)
	{
		if (error.pgpErr != kPGPError_UserAbort)
			throw;
	}
}

void 
CPassphraseDialog::OnOk()
{
	if (USecurity::IsRemoteSession() && 
		UWinVersion::IsWin2000Compatible())
	{
		using namespace UCommonStrings;

		UMountPoint::FixWarnRoot(mMountPoint, TRUE, TRUE, 
			Handle());

		if (mMountPoint.IsPlainLocalRoot())
		{
			CMessageBox	messageBox;
			messageBox.Display(Get(kTellChoseDriveInWin2kTerm), 
				Get(kNewDiskMsgBoxTitle), Handle());

			mDrive = mRoot.GetDriveLet();
			mChoseDrive = FALSE;
			mMountPoint.Empty();

			OnOptionsButton();
			return;
		}
	}

	if (!ValidatePassphrase())
		return;

	CDialog::OnOk();
}

void 
CPassphraseDialog::OnOptionsButton()
{
	try
	{
		CMountOptionsDialog	mountOptDlg;

		mountOptDlg.Display(Handle(), mDrive, mMountPoint, mChoseDrive, 
			mChoseRO, mForceRO);

		if (mChoseDrive)
			mRoot.MakePlainLocalRoot(mDrive);
		else
			mRoot = mMountPoint;

		mPassphraseEdit.SetFocus();
	}
	catch (CComboError& error)
	{
		if (error.pgpErr != kPGPError_UserAbort)
			throw;
	}
}

BOOL 
CPassphraseDialog::OnCommand(
	PGPUInt16	notifyCode, 
	PGPUInt16	itemId, 
	HWND		ctrl)
{
	switch (notifyCode)
	{
	case BN_CLICKED:
		switch (itemId)
		{
		case IDC_JOIN_BUTTON:
			OnJoinButton();
			return TRUE;

		case IDC_HIDETYPE_CHECK:
			OnHideTypeCheck();
			return TRUE;

		case IDC_OPTIONS_BUTTON:
			OnOptionsButton();
			return TRUE;
		}
		break;

	case EN_SETFOCUS:
		SetCapsLockMessageState();
		break;
	}

	return CDialog::OnCommand(notifyCode, itemId, ctrl);
}

void 
CPassphraseDialog::OnContextMenu(HWND menuWnd, CPoint coords)
{
	CHelpShower::ShowContextMenu(menuWnd, SinglePassDlgHelpIDs);
}

void 
CPassphraseDialog::OnHelp(HELPINFO *pInfo)
{
	CHelpShower::ShowForControl(static_cast<HWND>(pInfo->hItemHandle), 
		SinglePassDlgHelpIDs);
}

BOOL 
CPassphraseDialog::OnInitDialog(HWND focusCtrl)
{
	CDialog::OnInitDialog(focusCtrl);

	// Attach controls.
	mCancelButton.Subclass(GetDlgItem(IDCANCEL));
	mHideTypingCheck.Subclass(GetDlgItem(IDC_HIDETYPE_CHECK));
	mJoinButton.Subclass(GetDlgItem(IDC_JOIN_BUTTON));
	mOkButton.Subclass(GetDlgItem(IDOK));
	mOptionsButton.Subclass(GetDlgItem(IDC_OPTIONS_BUTTON));
	mPassphraseEdit.Subclass(GetDlgItem(IDC_PASSPHRASE_EDIT),mPContext->PGPContext());

	mCaptionText.Subclass(GetDlgItem(IDC_PASSPHRASE_CAPTION));
	mMessageText.Subclass(GetDlgItem(IDC_MESSAGE_TEXT));

	// Prepare controls.
	PrepareDialogText();
	PrepareInfoBars();
	PrepareJoinButton();

	if (!mShowOptions)
		mOptionsButton.ShowWindow(SW_HIDE);

	mHideTypingCheck.SetCheck(BST_CHECKED);
	SetCapsLockMessageState();

	mPassphraseEdit.SetMaxSizeContents(kPGPdiskMaxPassLength);
	mPassphraseEdit.SetFocus();

	// Display dialog.
	Resize();
	Center();
	SetForegroundWindow();

	return FALSE;
}

void 
CPassphraseDialog::OnKeyUp(PGPUInt32 vKey, PGPUInt32 keyData)
{
	SetCapsLockMessageState();
	CDialog::OnKeyUp(vKey, keyData);
}

void 
CPassphraseDialog::OnNcDestroy()
{
	if (IsntNull(mPSplitKeys))
	{
		delete mPSplitKeys;
		mPSplitKeys = NULL;
	}
}

PGPUInt32 
CPassphraseDialog::OnSecureEditEvent(HWND view, PGPUInt32 event)
{
	switch (event)
	{
	case CSecureRichEdit::kSawCapsLockKeyEvent:
		SetCapsLockMessageState();
		break;

	case CSecureRichEdit::kSawReturnKeyEvent:
//		OnOk();
		break;
	}

	return TRUE;
}

PGPBoolean 
CPassphraseDialog::PreProcess(const CMessage& msg, LRESULT& result)
{
	if (msg.Message() == CSecureRichEdit::WM_SECUREEDIT_EVENT)
	{
		result = OnSecureEditEvent(reinterpret_cast<HWND>(msg.WParam()), 
			msg.LParam());
		return TRUE;
	}

	return FALSE;
}

⌨️ 快捷键说明

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