clphrase.c

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

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

	CLphrase.c - glue code to interface with PGPsdkUI

	$Id: CLphrase.c,v 1.19 2002/08/06 20:09:19 dallen Exp $
____________________________________________________________________________*/
#include "pgpPFLConfig.h"

#include "PGPclx.h"
#include "pgpUserInterface.h"

extern HINSTANCE g_hInst;

//	_______________________________________________________
//
//  wipe and free passphrase

VOID PGPclExport
PGPclFreePhrase (
		char*		pszPhrase)
{
	if (pszPhrase)
	{
		FillMemory (pszPhrase, lstrlen (pszPhrase), 0x00);
		PGPFreeData (pszPhrase);
	}
}


//	_______________________________________________________
//
//	free passkey for key

VOID PGPclExport
PGPclFreePasskey (
		PGPByte*	pbyte,
		PGPSize		size)
{
	if (pbyte)
	{
		FillMemory (pbyte, size, 0x00);
		PGPFreeData (pbyte);
	}
}


//	_______________________________________________________
//
//	put the phrase/passkey into the SDK cache, if enabled

PGPError PGPclExport
PGPclCachePhrase (
		PGPContextRef		context,
		PGPKeyDBObjRef		key,
		char*				pszPhrase,
		PGPByte*			pPasskey,
		PGPInt32			iPasskeyLength)
{
	PGPUInt32		uCacheType			= kPGPPrefCacheTypeNone;
	PGPUInt32		uCacheSecs			= 0;
	PGPBoolean		bCacheGlobal		= FALSE;
	PGPError		err					= kPGPError_NoErr;
	PGPPrefRef		prefref				= kInvalidPGPPrefRef;

	if (!PGPKeyDBObjRefIsValid (key))
		return kPGPError_BadParams;

	err = PGPclPeekClientLibPrefRefs (&prefref, NULL);
	if (IsntPGPError (err))
	{
		PGPGetPrefNumber (prefref,
				kPGPPrefCacheType, &uCacheType);
		PGPGetPrefNumber (prefref,
				kPGPPrefCacheSeconds, &uCacheSecs);
		PGPGetPrefBoolean (prefref,
				kPGPPrefShareCache, &bCacheGlobal);
	}

	if (uCacheType != kPGPPrefCacheTypeNone) 
	{
		if (uCacheType == kPGPPrefCacheTypeLogon)
			uCacheSecs = kPGPMaxTimeInterval;

		PGPPassphraseIsValid (key, 
				pszPhrase ?
					PGPOPassphrase (context, pszPhrase) :
					PGPOPasskeyBuffer (context, pPasskey, iPasskeyLength),
				PGPOCachePassphrase (context, uCacheSecs, bCacheGlobal),
				PGPOLastOption (context));

		PGPCacheKeyDB (PGPPeekKeyDBObjKeyDB (key), uCacheSecs);
	}

	return err;
}


//	_______________________________________________________
//
//  Entry point called by app to post dialog and get phrase
//
//  This used to be the main entry point for passphrase
//  dialogs. Now it is used to convert the calling parameters
//  to the new PGP UI Library, and then massage the output
//  back into something the old client code can understand.
//
//  wjb

PGPError PGPclExport
PGPclGetPhrase (
		PGPContextRef	context,
		PGPKeyDBRef		keydbMain,
		HWND			hWndParent,
		LPSTR			szPrompt,
		LPSTR*			ppszPhrase,
		PGPKeySetRef	KeySet,
		PGPKeyID*		pKeyIDs,
		UINT			uKeyCount,
		PGPKeyDBObjRef*	pKey,
		UINT*			puOptions,
		UINT			uFlags,
		PGPByte**		ppPasskeyBuffer,
		PGPUInt32*		piPasskeyLength,
		PGPUInt32		MinLength,
		PGPUInt32		MinQuality,
		PGPtlsContextRef tlsContext,
		PGPKeyDBRef		*keydbAdded,
		char			*szTitle
		)
{
	PGPError					err				= kPGPError_NoErr;
	PGPOptionListRef			optionList		= kInvalidPGPOptionListRef;
	PGPKeyServerEntry			*ksEntries		= NULL;
	PGPUInt32					numKSEntries	= 0;
	PGPKeyServerSpec			*serverList		= NULL;
	PGPKeyDBRef					keydbFound		= kInvalidPGPKeyDBRef;
	PCLIENTSERVERSTRUCT			pcss			= NULL;
	PGPBoolean					bKeyserversInited = FALSE;

	CHAR						StrRes1[100],StrRes2[100],StrRes3[100];
	PGPKeyDBObjRef 				keyDecryption;	// dummy, we don't care

	if (PGPKeyDBRefIsValid (keydbMain))
	{
		if (IsntPGPError(err))
		{
			err = CLInitKeyServerPrefs (kPGPclDefaultServer, NULL,
				hWndParent, context, keydbMain, "",
				&pcss, &ksEntries, &serverList, &numKSEntries);

			if(IsntPGPError(err))
			{
				bKeyserversInited=TRUE;
			}
		}
	}

	optionList = kInvalidPGPOptionListRef;

	// Everybody needs a passphrase buffer and a hwnd
	// If no length quality needed, zeros are default anyway
	err = PGPBuildOptionList( context, &optionList,
			PGPOUIOutputPassphrase( context, ppszPhrase ),
			PGPOUIParentWindowHandle( context, hWndParent ),
			PGPOUIMinimumPassphraseLength(context,MinLength),
			PGPOUIMinimumPassphraseQuality(context,MinQuality),
			PGPOLastOption( context ) );

	if( IsntPGPError( err ) )
	{
		PGPUInt32	detachedSignature	= 0;
		PGPUInt32	textOutput			= 0;
		PGPUInt32	inputIsText			= 0;
		PGPBoolean	haveFileOptions 	= FALSE;

		// If we have a prompt, use it
		if( IsntNull( szPrompt ) )
		{
			err = PGPAppendOptionList( optionList,
				PGPOUIDialogPrompt( context, szPrompt ),
				PGPOLastOption( context ) );
		}

		// If we have a title, use it
		if( IsntNull( szTitle ) )
		{
			err = PGPAppendOptionList( optionList,
				PGPOUIWindowTitle(context,szTitle),
				PGPOLastOption( context ) );
		}

		// If we have options, convert them to new options API
		if( IsntPGPError( err ) && ( puOptions != 0 ))
		{
			haveFileOptions = TRUE;

			if( (*puOptions & kPGPclDetachedSignature) != 0 )
				detachedSignature = 1;

			if( (*puOptions & kPGPclASCIIArmor) != 0 )
				textOutput = 1;

			if( (*puOptions & kPGPclInputIsText) != 0 )
				inputIsText = 1;

			LoadString (g_hInst, IDS_DETACHEDSIG, StrRes1, sizeof(StrRes1));
			LoadString (g_hInst, IDS_TEXTOUTPUT, StrRes2, sizeof(StrRes2));
			LoadString (g_hInst, IDS_INPUTISTEXT, StrRes3, sizeof(StrRes3));

			err = PGPAppendOptionList( optionList,
					PGPOUIDialogOptions( context,
						PGPOUICheckbox( context, 804,
							StrRes1,NULL,
							detachedSignature, &detachedSignature,
							PGPOLastOption( context ) ),
						PGPOUICheckbox( context, 801,
							StrRes2,NULL,
							textOutput, &textOutput,
							PGPOLastOption( context ) ),
						PGPOUICheckbox( context, 807,
							StrRes3,NULL,
							inputIsText, &inputIsText,
							PGPOLastOption( context ) ),
					PGPOLastOption( context ) ),
					PGPOLastOption( context ) );
		}

		if( IsntPGPError( err ) )
		{
			// Conventional encryption passphrase needed
			if(uFlags & kPGPclEncryption)
			{
				PGPBoolean	bShowQuality = TRUE;
				if (uFlags & kPGPclHideQualityIndicator)
					bShowQuality = FALSE;

				PGPAppendOptionList( optionList,
						PGPOUIShowPassphraseQuality(context,bShowQuality),
						PGPOLastOption( context ) );

				err=PGPConventionalEncryptionPassphraseDialog(context,
					optionList,
					PGPOLastOption( context ) );
			}
			// We're decoding......
			else if(uFlags & kPGPclDecryption)
			{
				PGPInt32 numKeys;

				numKeys=0;

				if(PGPKeySetRefIsValid (KeySet))
					PGPCountKeys( KeySet, &numKeys );

				// A conventionally encrypted file
				if((numKeys==0)&&(uKeyCount==0))
				{
					err=PGPConventionalDecryptionPassphraseDialog(context,
						optionList,
						PGPOLastOption( context ) );
				}
				// A RSA or DH encrypted file
				else
				{
					if(pKey==NULL)
						pKey=&keyDecryption;

					err=PGPDecryptionPassphraseDialog(
						context,
						KeySet,
						uKeyCount,
						pKeyIDs,
						pKey, // for recon dialog
						optionList,
						PGPOUIKeyServerUpdateParams(context,
							numKSEntries, serverList,
							tlsContext,FALSE,&keydbFound,
							PGPOLastOption( context ) ),
						PGPOLastOption( context ) );
				}
			}
			else if(uFlags & kPGPclKeyPassphrase)
			{
				err=PGPKeyPassphraseDialog(
					context,
					*pKey,
					optionList,
					PGPOLastOption( context ) );
			}
			// We're signing something and need the combo box
			else
			{
				if (!PGPKeyDBObjRefIsValid (*pKey))
					PGPclGetDefaultPrivateKey (keydbMain, pKey);

				err = PGPSigningPassphraseDialog( context, 
						keydbMain, pKey, optionList,
						PGPOUIDefaultKey( context, *pKey ),
						PGPOLastOption( context ) );
			}
		}

		// Shared key has been selected. Go to reconstitution dialog
		if(((err==kPGPError_KeyUnusableForSignature)||
            (err==kPGPError_KeyUnusableForDecryption)) &&
		    (!(uFlags & kPGPclRejectSplitKeys)))
		{
			if((ppPasskeyBuffer!=NULL)&&(piPasskeyLength!=NULL)&&(pKey!=NULL))
			{
				// check if passkey has been cached
				if (PGPPassphraseIsValid (*pKey, PGPOLastOption (context)))
				{
					err = kPGPError_NoErr;
					// layers above this one need an empty string for
					// cached passphrases, so make up a dummy one
					*ppszPhrase = PGPNewData (PGPPeekContextMemoryMgr (context),
							1, kPGPMemoryMgrFlags_Clear);
				}
				else
				{
					err=PGPclReconstituteKey(
						context,
						tlsContext,
						hWndParent,
						keydbMain,
						*pKey,
						ppPasskeyBuffer,
						piPasskeyLength);
				}
			}
		}

		if( (IsntPGPError(err)) &&
			(ppPasskeyBuffer!=NULL) &&
			(piPasskeyLength!=NULL) &&
			(pKey!=NULL))
		{
			// Convert passphrase to passkey
			if((*ppPasskeyBuffer==NULL)&&(*pKey!=NULL)&&(*ppszPhrase!=NULL))
			{
				if (lstrlen(*ppszPhrase)>0)
				{
					PGPUInt32 uKeyLockingBits;

					PGPGetKeyDBObjNumericProperty (*pKey,
							kPGPKeyProperty_LockingBits, &uKeyLockingBits);

					*piPasskeyLength=(uKeyLockingBits+7)/8; // Bits to bytes
					*ppPasskeyBuffer=
						(PGPByte *)PGPNewSecureData (PGPPeekContextMemoryMgr (context),
										*piPasskeyLength, 0);

					err = PGPGetPasskeyBuffer (*pKey, *ppPasskeyBuffer,
						PGPOPassphrase(context, *ppszPhrase),
						PGPOLastOption(context));
				}
				else
				{
					*piPasskeyLength=0;
					*ppPasskeyBuffer=NULL;
				}
			}
		}

		// If we had options, read the results and send em back
		if( IsntPGPError( err ) && haveFileOptions )
		{
			*puOptions = 0;

			if( detachedSignature != 0 )
				*puOptions |= kPGPclDetachedSignature;

			if( textOutput != 0 )
				*puOptions |= kPGPclASCIIArmor;

			if( inputIsText != 0 )
				*puOptions |= kPGPclInputIsText;
		}

		PGPFreeOptionList( optionList );
	}

	if(bKeyserversInited)
	{
		CLUninitKeyServerPrefs (kPGPclDefaultServer,
			pcss, ksEntries, serverList, numKSEntries);
	}

	if(keydbAdded!=NULL)
	{
		*keydbAdded=keydbFound;
	}
	else
	{
		if(PGPKeyDBRefIsValid(keydbFound))
			PGPFreeKeyDB(keydbFound);
	}

	return(err);
}


//	_______________________________________________________
//
//	get passphrase for key from user

PGPError PGPclExport
PGPclGetKeyPhrase (
		PGPContextRef		context,
		PGPtlsContextRef	tlsContext,
		HWND				hwnd,
		char*				szPrompt,

⌨️ 快捷键说明

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