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

📄 cpgpdiskapppassphrase.cpp

📁 vc环境下的pgp源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////////////
// CPGPdiskAppPassphrase.cpp
//
// Functions for adding, changing, and removing passphrases.
//////////////////////////////////////////////////////////////////////////////

// $Id: CPGPdiskAppPassphrase.cpp,v 1.9 1999/02/26 04:09:57 nryan Exp $

// Copyright (C) 1998 by Network Associates, Inc.
// All rights reserved.

#include "StdAfx.h"
#include <math.h>

#include "Required.h"
#include "GlobalPGPContext.h"
#include "PGPdiskHighLevelUtils.h"
#include "PGPdiskPublicKeyUtils.h"
#include "StringAssociation.h"

#include "CConfirmPassphraseDialog.h"
#include "CMainDialog.h"
#include "COpenPGPdiskDialog.h"
#include "CPGPdiskApp.h"
#include "CPGPdiskAppPassphrase.h"

#include "pgpSDKPrefs.h"
#include "pgpUtilities.h"

#include "CPublicKeyDialog.h"
#include "CSinglePassphraseDialog.h"
#include "Globals.h"
#include "PGPdisk.h"


////////
// Types
////////

// ScoreStruct/Range is used by the CalcPassphraseQuality routine.

typedef struct ScoreStruct
{
	PGPUInt8	firstChar;
	PGPUInt8	lastChar;

} Range;


///////////////////////////////////////////////////////////////
// High-Level Passphrase Addition, Change, and Removal Routines
///////////////////////////////////////////////////////////////

// AskUserAddPassphrase asks to user to pick the PGPdisk for which he wants
// to add a passphrase.

void 
CPGPdiskApp::AskUserAddPassphrase()
{
	AppCommandInfo		ACI;
	COpenPGPdiskDialog	selectPGPdiskDlg(kODT_Select);
	CString				path;
	DualErr				derr;
	PGPBoolean			wasMainEnabled;

	// Forcibly disable main window so it doesn't flicker between dialogs.
	if (mMainDialog)
		wasMainEnabled = !mMainDialog->EnableWindow(FALSE);
	
	// Show the dialog.
	derr = selectPGPdiskDlg.AskForPath(&path);

	if (derr.IsntError())
	{
		// Now dispatch the command.
		ACI.op		= kAppOp_AddPassphrase;
		ACI.flags	= NULL;
		ACI.drive	= kInvalidDrive;
		strcpy(ACI.path, path);

		DispatchAppCommandInfo(&ACI);
	}

	if (derr.IsError())
	{
		ReportError(kPGDMajorError_PGPdiskAddPassFailed, derr);
	}

	// Re-enable main dialog if it was disabled.
	if (IsntNull(mMainDialog) && wasMainEnabled)
		mMainDialog->EnableWindow(TRUE);
}

// AskUserChangePassphrase asks to user to pick the PGPdisk for which he
// wants to change a passphrase.

void 
CPGPdiskApp::AskUserChangePassphrase()
{
	AppCommandInfo		ACI;
	COpenPGPdiskDialog	selectPGPdiskDlg(kODT_Select);
	CString				path;
	DualErr				derr;
	PGPBoolean			wasMainEnabled;

	// Forcibly disable main window so it doesn't flicker between dialogs.
	if (mMainDialog)
		wasMainEnabled = !mMainDialog->EnableWindow(FALSE);

	// Show the dialog.
	derr = selectPGPdiskDlg.AskForPath(&path);

	if (derr.IsntError())
	{
		// Now dispatch the command.
		ACI.op		= kAppOp_ChangePassphrase;
		ACI.flags	= NULL;
		ACI.drive	= kInvalidDrive;
		strcpy(ACI.path, path);

		DispatchAppCommandInfo(&ACI);
	}

	if (derr.IsError())
	{
		ReportError(kPGDMajorError_PGPdiskChangePassFailed, derr);
	}

	// Re-enable main dialog if it was disabled.
	if (IsntNull(mMainDialog) && wasMainEnabled)
		mMainDialog->EnableWindow(TRUE);
}

// AskUserRemovePassphrase asks to user to pick the PGPdisk for which he wants
// to remove a passphrase.

void 
CPGPdiskApp::AskUserRemovePassphrase(PGPBoolean removeAll)
{
	AppCommandInfo		ACI;
	COpenPGPdiskDialog	selectPGPdiskDlg(kODT_Select);
	CString				path;
	DualErr				derr;
	PGPBoolean			wasMainEnabled;

	// Forcibly disable main window so it doesn't flicker between dialogs.
	if (mMainDialog)
		wasMainEnabled = !mMainDialog->EnableWindow(FALSE);

	// Show the dialog.
	derr = selectPGPdiskDlg.AskForPath(&path);

	if (derr.IsntError())
	{
		// Now dispatch the command.
		ACI.op		= (removeAll ? 
			kAppOp_RemoveAlternates : kAppOp_RemovePassphrase);
		ACI.flags	= NULL;
		ACI.drive	= kInvalidDrive;
		strcpy(ACI.path, path);

		DispatchAppCommandInfo(&ACI);
	}

	if (derr.IsError())
	{
		if (removeAll)
			ReportError(kPGDMajorError_PGPdiskRemoveAllPassesFailed, derr);
		else
			ReportError(kPGDMajorError_PGPdiskRemoveOnePassFailed, derr);
	}

	// Re-enable main dialog if it was disabled.
	if (IsntNull(mMainDialog) && wasMainEnabled)
		mMainDialog->EnableWindow(TRUE);
}

// AskUserAddRemovePublicKeys asks the user for a PGPdisk from which to add
// or remove public keys.

void 
CPGPdiskApp::AskUserAddRemovePublicKeys()
{
	AppCommandInfo		ACI;
	COpenPGPdiskDialog	selectPGPdiskDlg(kODT_Select);
	CString				path;
	DualErr				derr;
	PGPBoolean			wasMainEnabled;

	// Forcibly disable main window so it doesn't flicker between dialogs.
	if (mMainDialog)
		wasMainEnabled = !mMainDialog->EnableWindow(FALSE);

	// Show the dialog.
	derr = selectPGPdiskDlg.AskForPath(&path);

	if (derr.IsntError())
	{
		// Now dispatch the command.
		ACI.op		= kAppOp_AddRemovePublicKeys;
		ACI.flags	= NULL;
		ACI.drive	= kInvalidDrive;
		strcpy(ACI.path, path);

		DispatchAppCommandInfo(&ACI);
	}

	if (derr.IsError())
	{
		ReportError(kPGDMajorError_AddRemovePublicKeysFailed, derr);
	}

	// Re-enable main dialog if it was disabled.
	if (IsntNull(mMainDialog) && wasMainEnabled)
		mMainDialog->EnableWindow(TRUE);
}


//////////////////////////////////////////////////////////////
// Low-Level Passphrase Addition, Change, and Removal Routines
//////////////////////////////////////////////////////////////

// AddPGPdiskPassphrase adds a new passphrase to the specified PGPdisk.

	DualErr 
CPGPdiskApp::AddPGPdiskPassphrase(LPCSTR path)
{
	CConfirmPassphraseDialog	addPassDlg(kCPDT_Add);
	CSinglePassphraseDialog		masterPassDlg(kSPDT_Master);
	DualErr						derr;
	PGPUInt32					numAlts;

	pgpAssertStrValid(path);

	// Did our dialogs initialize correctly?
	derr = addPassDlg.mInitErr;

	if (derr.IsntError())
	{
		derr = masterPassDlg.mInitErr;
	}
	
	// Make sure this PGPdisk isn't mounted already.
	if (derr.IsntError())
	{
		if (mPGPdisks.FindPGPdisk(path))
		{
			ReportError(kPGDMajorError_TriedPassOpOnMountedPGPdisk);
			derr = DualErr(kPGDMinorError_FailSilently);
		}
	}

	// Validate the PGPdisk.
	if (derr.IsntError())
	{
		derr = ValidatePGPdisk(path);
	}
	
	// Can't add passphrases to read-only PGPdisks.
	if (derr.IsntError())
	{
		if (IsFileReadOnly(path))
			derr = DualErr(kPGDMinorError_PGPdiskIsWriteProtected);
	}

	// Display the master passphrase dialog.
	if (derr.IsntError())
	{
		derr = masterPassDlg.AskForPassphrase(path);
	}

	// Verify that there is space for another passphrase.
	if (derr.IsntError())
	{
		derr = HowManyAlternatePassphrases(path, &numAlts);
	}

	if (derr.IsntError())
	{
		if (numAlts == kMaxAlternatePassphrases)
		{
			derr = DualErr(kPGDMinorError_AllAlternatesTaken);
		}
	}

	// Display the new passphrase dialog.
	if (derr.IsntError())
	{
		derr = addPassDlg.AskForPassphrase(path);
	}

	if (derr.IsntError())
	{
		// Add the passphrase.
		derr = AddPassphrase(path, 
			masterPassDlg.mPassphraseEdit.mContents, 
			addPassDlg.mPassphraseEdit.mContents, 
			addPassDlg.mReadOnlyValue);
	}

	return derr;
}

// ChangePGPdiskPassphrase changes a passphrase on the specified PGPdisk.

DualErr 
CPGPdiskApp::ChangePGPdiskPassphrase(LPCSTR path)
{
	CConfirmPassphraseDialog	newPassDlg(kCPDT_Change);
	CSinglePassphraseDialog		changePassDlg(kSPDT_Change);
	DualErr						derr;

	pgpAssertStrValid(path);

	// Make sure our passphrase dialog initialized correctly.
	derr = newPassDlg.mInitErr;

	if (derr.IsntError())
	{
		derr = changePassDlg.mInitErr;
	}

	// Make sure this PGPdisk isn't mounted already.
	if (derr.IsntError())
	{
		if (mPGPdisks.FindPGPdisk(path))
		{
			ReportError(kPGDMajorError_TriedPassOpOnMountedPGPdisk);
			derr = DualErr(kPGDMinorError_FailSilently);
		}
	}

	// Validate the PGPdisk.
	if (derr.IsntError())
	{
		derr = ValidatePGPdisk(path);
	}
	
	// Can't change passphrases on read-only PGPdisks.
	if (derr.IsntError())
	{
		if (IsFileReadOnly(path))
			derr = DualErr(kPGDMinorError_PGPdiskIsWriteProtected);
	}

	// Display the dialogs.
	if (derr.IsntError())
	{
		derr = changePassDlg.AskForPassphrase(path);
	}

	if (derr.IsntError())
	{
		derr = newPassDlg.AskForPassphrase(path);
	}
	
	if (derr.IsntError())
	{
		// Change the passphrase.
		derr = ChangePassphrase(path, 
			changePassDlg.mPassphraseEdit.mContents, 
			newPassDlg.mPassphraseEdit.mContents);
	}

	return derr;
}

// RemovePGPdiskPassphrase removes a passphrase from the specified PGPdisk.

DualErr 
CPGPdiskApp::RemovePGPdiskPassphrase(LPCSTR path, PGPBoolean removeAll)
{
	CSinglePassphraseDialog	removePassDlg(kSPDT_Remove);
	DualErr					derr;

	pgpAssertStrValid(path);

	// Did our dialog initialize correctly?

⌨️ 快捷键说明

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