clcache.c

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

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

	CLcache.c - passphrase caching wrapper routines

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

#include "PGPclx.h"


//_____________________________________________________
//
//	This routine is called to get either cached phrase
//	(if available) or prompt user for phrase.

PGPError PGPclExport
PGPclGetCachedDecryptionPhrase (
		PGPContextRef		context,
		PGPtlsContextRef	tlsContext,
		PGPKeyDBRef			keydbMain,
		HWND				hwnd,
		LPSTR				szPrompt,
		BOOL				bForceUserInput,
		LPSTR*				ppszBuffer,
		PGPKeySetRef		keysetEncryptedTo,
		PGPKeyID*			pkeyidEncryptedTo,
		UINT				uKeyIDCount,
		PGPByte**			ppPasskeyBuffer,
		PGPUInt32*			piPasskeyLength,
		PGPKeyDBRef*		pkeydbAdded,
		char *				szTitle)
{
	PGPUInt32		uCacheType			= kPGPPrefCacheTypeNone;
	PGPUInt32		uCacheSecs			= 0;
	PGPBoolean		bCacheGlobal		= FALSE;
	PGPError		err					= kPGPError_NoErr;
	LPSTR			psz					= NULL;
	PGPByte*		ppasskey			= NULL;
	PGPUInt32		ilen				= 0;
	PGPKeyDBObjRef	key					= kInvalidPGPKeyDBObjRef;
	PGPPrefRef		prefref				= kInvalidPGPPrefRef;

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

	// get phrase from user
	err = PGPclGetPhrase (context, keydbMain, hwnd, szPrompt, &psz,
			keysetEncryptedTo, pkeyidEncryptedTo, uKeyIDCount,
			&key, NULL, kPGPclDecryption, &ppasskey, &ilen,
			0, 0, tlsContext, pkeydbAdded, szTitle);

	// user entered phrase -- setup cache
	if ((IsntPGPError (err)) &&
		(psz || ppasskey))
	{
		// copy data to caller's buffers
		*ppszBuffer = psz;
		*ppPasskeyBuffer = ppasskey;
		*piPasskeyLength = ilen;

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

			PGPPassphraseIsValid (key, 
					psz ?
						PGPOPassphrase (context, psz) :
						PGPOPasskeyBuffer (context, ppasskey, ilen),
					PGPOCachePassphrase (context, uCacheSecs, bCacheGlobal),
					PGPOLastOption (context));

			PGPCacheKeyDB (PGPPeekKeyDBObjKeyDB (key), uCacheSecs);
		}
	}

	// problem (cancel or other error)
	else 
	{
		if (err == kPGPError_NoErr)
			err = kPGPError_UserAbort;
	}

	return err;
}

//_____________________________________________________
//
//	This routine is called to get either signing cached phrase
//	(if available) or prompt user for phrase.

PGPError PGPclExport
PGPclGetCachedSigningPhrase (
		PGPContextRef		context,
		PGPtlsContextRef	tlsContext,
		HWND				hwnd,
		LPSTR				szPrompt,
		BOOL				bForceUserInput,
		LPSTR*				ppszBuffer,
		PGPKeyDBRef			keydbSigning,
		PGPKeyDBObjRef*		pkeySigning,
		UINT*				puOptions,
		UINT				uFlags,
		PGPByte**			ppPasskeyBuffer,
		PGPUInt32*			piPasskeyLength,
		PGPKeyDBRef*		pkeydbAdded,
		char *				szTitle)
{
	PGPUInt32		uCacheType			= kPGPPrefCacheTypeNone;
	PGPUInt32		uCacheSecs			= 0;
	PGPBoolean		bCacheGlobal		= FALSE;
	PGPBoolean		bNeedsPhrase		= TRUE;
	PGPError		err					= kPGPError_NoErr;
	LPSTR			psz					= NULL;
	PGPByte*		ppasskey			= NULL;
	PGPUInt32		ilen				= 0;
	BOOL			bAskUser			= TRUE;
	PGPKeyDBObjRef	key					= kInvalidPGPKeyDBObjRef;
	PGPPrefRef		prefref				= kInvalidPGPPrefRef;

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

	// if caching is enabled ...
	if ((!bForceUserInput) &&
		(uCacheType != kPGPPrefCacheTypeNone))
	{
		// see if default key's phrase is cached 
		err = PGPclGetDefaultPrivateKey (keydbSigning, &key);

		if (IsntPGPError (err))
		{
			err = PGPGetKeyDBObjBooleanProperty (key, 
					kPGPKeyProperty_NeedsPassphrase, &bNeedsPhrase);

			if ((IsntPGPError (err))&&(bNeedsPhrase))
			{
				if (PGPPassphraseIsValid (key, PGPOLastOption (context)))
				{
					bAskUser = FALSE;
				}
			}
		}
	}

	// otherwise, get phrase from user
	if (bAskUser)
	{
		// call PGPcl routine to post passphrase dialog
		err = PGPclGetPhrase (context, keydbSigning, 
				hwnd, szPrompt, &psz,
				NULL, NULL, 0, pkeySigning, puOptions,
				uFlags, &ppasskey, &ilen,
				0, 0, tlsContext, pkeydbAdded, szTitle);

		// user entered phrase -- setup cache
		if ((IsntPGPError (err)) &&
			(psz || ppasskey))
		{
			// copy data to caller's buffers
			*ppszBuffer = psz;
			*ppPasskeyBuffer = ppasskey;
			*piPasskeyLength = ilen;

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

				PGPPassphraseIsValid (*pkeySigning, 
						psz ?
							PGPOPassphrase (context, psz) :
							PGPOPasskeyBuffer (context, ppasskey, ilen),
						PGPOCachePassphrase (context, uCacheSecs, bCacheGlobal),
						PGPOLastOption (context));

				PGPCacheKeyDB (PGPPeekKeyDBObjKeyDB (key), uCacheSecs);
			}
		}

		// problem (cancel or other error)
		else
		{
			if (err == kPGPError_NoErr)
				err = kPGPError_UserAbort;
		}
	}
	else
	{
		if (pkeySigning)
			*pkeySigning = key;

		*ppszBuffer = NULL;
		*ppPasskeyBuffer = NULL;
		*piPasskeyLength = 0;
	}

	return err;
}


//________________________________________________
//
//	Called to wipe and free the phrase returned by
//	PGPGetCachedPhrase.

VOID PGPclExport
PGPclFreeCachedPhrase (LPSTR szPhrase)
{
	pgpClearMemory (szPhrase, lstrlen (szPhrase));
	PGPFreeData (szPhrase);
}

⌨️ 快捷键说明

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