📄 pgpcontext.c
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: pgpContext.c,v 1.58 2002/08/06 20:11:14 dallen Exp $
____________________________________________________________________________*/
#include <stdio.h>
#include "pgpErrors.h"
#include "pgpMem.h"
#include "pgpPFLPriv.h"
#include "pgpSDKPriv.h"
#include "pgpContext.h"
#include "pgpKeys.h"
#include "pgpEnv.h"
#include "pgpRandomPoolPriv.h"
#include "pgpRandomContext.h"
#include "pgpRandomX9_17.h"
#include "pgpRnd.h"
#include "pgpRndSeed.h"
#include "pgpUtilitiesPriv.h"
#include "pgpMacFileMapping.h"
#include "bn.h"
#include "pgpMemoryMgrPriv.h"
#include "pgpMemPool.h"
#include "pgpTimeBomb.h"
#include "pgpPassCach.h"
#include "pgpKeyPriv.h"
#include "pgpThreads.h"
#if PGP_MACINTOSH
#include "PGPsdkDriverAPI.h"
#include "PGPsdkBackEndAPI.h"
#endif
#if PGP_WIN32
#include <windows.h>
#include "PGPsdkDriver.h"
#include "PGPsdkBackEndAPI.h"
#endif
#if PGP_UNIX
#include "pgpRPCBackEndAPI.h"
#endif
/* Global variable to keep track of all contexts in use */
static PGPContextRef pgpsdkFirstContext;
/* Global to record keydb update notification callback */
static PGPNotificationHandlerProc sNotificationProc;
static PGPUserValue sNotificationUserValue;
/* Global mutex lock defined in utilities/pgpPassCach.c */
extern PGPMutex_t gPassCacheMutex;
enum
{
kPGPContextMagic = 0x436e7478 /* 'Cntx' */
};
struct PGPContext
{
PGPContextRef nextContext;
PGPMemoryMgrRef memoryMgr;
PGPMemoryMgrRef memPoolMgr;
MemPool memPool;
PGPUInt32 magic; /* Always kPGPContextMagic */
PGPUInt32 clientAPIVersion; /* version client compiled against */
PGPUserValue userValue; /* Client storage */
PGPEnv * pgpEnvironment; /* State for within library */
PGPRandomContext * randomPoolContext; /* ctxt around global rand pool*/
PGPRandomContext * randomPoolX9_17; /* uses 'randomPoolContext' */
PGPRandomContext * dummyRandomContext;
PGPKeyDBRef keyDB; /* List of keydbs */
PGPConnectRef connect_ref; /* Back end ref for front end */
struct PGPCacheHeader *passphraseCache; /* Passphrase cache header */
PGPUInt32 currentThread; /* thread owning context */
PGPBoolean updateNeeded; /* Out of sync with backend */
};
typedef struct PGPContext PGPContext;
/*____________________________________________________________________________
This function validates a context data structure.
____________________________________________________________________________*/
PGPBoolean
pgpContextIsValid(PGPContextRef context)
{
return( context != NULL &&
context->magic == kPGPContextMagic );
}
/*____________________________________________________________________________
Allocate a new PGPContext using custom memory allocators.
This function es exported for the client library and should never be
called by the shared library.
____________________________________________________________________________*/
static PGPError
FinishInitContext( PGPContextRef context )
{
PGPError err = kPGPError_NoErr;
err = pgpenvCreate( context, &context->pgpEnvironment );
/* Create a mempool and prime it with a small allocation */
memPoolInit( context, &context->memPool );
memPoolAlloc( &context->memPool, sizeof(void *), sizeof(void *) );
context->memPoolMgr = pgpMemPoolMemoryMgr( &context->memPool );
#if PGP_WIN32
context->currentThread = GetCurrentThreadId();
#endif
return( err );
}
/*____________________________________________________________________________
Allocate a new PGPContext using custom memory allocators.
This function es exported for the client library and should never be
called by the shared library.
____________________________________________________________________________*/
PGPError
pgpNewContextCustomInternal(
PGPCustomContextInfo const * custom,
PGPContextRef* newContext)
{
PGPError err = kPGPError_NoErr;
PGPContextRef context = NULL;
PGPValidatePtr( newContext );
*newContext = NULL;
PGPValidatePtr( custom );
/* Note: if the struct grows, then
sizeof( PGPCustomContextInfo ) will need to be a fixed number */
PGPValidateParam( PGPMemoryMgrIsValid( custom->memoryMgr ) );
if ( ! pgpsdkIsInited() )
{
return( kPGPError_ImproperInitialization );
}
if ( custom->sdkAPIVersion != kPGPsdkAPIVersion )
{
return( kPGPError_IncompatibleAPI );
}
#if PGP_TIME_BOMB
if ( pgpTimeBombHasExpired() )
{
*newContext = NULL;
return( kPGPError_FeatureNotAvailable );
}
#endif
context = (PGPContext *)PGPNewData( custom->memoryMgr,
sizeof( PGPContext ), kPGPMemoryMgrFlags_Clear );
if( IsntPGPError( err ) )
{
context->memoryMgr = custom->memoryMgr;
context->magic = kPGPContextMagic;
context->clientAPIVersion = custom->sdkAPIVersion;
err = FinishInitContext( context );
if( IsPGPError( err ) )
{
PGPFreeContext( context );
context = kInvalidPGPContextRef;
} else {
context->nextContext = pgpsdkFirstContext;
pgpsdkFirstContext = context;
}
}
else
{
err = kPGPError_OutOfMemory;
}
*newContext = context;
return( err );
}
/*____________________________________________________________________________
Allocate a new PGPContext using the default PGPsdk memory allocators.
____________________________________________________________________________*/
PGPError
PGPNewContext(
PGPUInt32 clientAPIVersion,
PGPContextRef * newContext)
{
PGPCustomContextInfo custom;
PGPError err = kPGPError_NoErr;
PGPValidatePtr( newContext );
*newContext = NULL;
pgpEnterPGPErrorFunction();
pgpClearMemory( &custom, sizeof( custom ) );
custom.sdkAPIVersion = clientAPIVersion;
err = PGPNewMemoryMgr( 0, &custom.memoryMgr );
if ( IsntPGPError( err ) )
{
err = pgpNewContextCustomInternal( &custom, newContext );
}
return( err );
}
/*____________________________________________________________________________
Allocate a new custom PGPContext.
____________________________________________________________________________*/
PGPError
PGPNewContextCustom(
PGPCustomContextInfo const * custom,
PGPContextRef *newContext)
{
PGPError err = kPGPError_NoErr;
PGPValidatePtr( newContext );
*newContext = kInvalidPGPContextRef;
PGPValidatePtr( custom );
pgpEnterPGPErrorFunction();
err = pgpNewContextCustomInternal( custom, newContext );
return( err );
}
static void
sDestroyContext( PGPContextRef context )
{
if( IsntNull( context->memPoolMgr ) )
{
PGPFreeMemoryMgr( context->memPoolMgr );
}
memPoolEmpty( &context->memPool );
if( IsntNull( context->pgpEnvironment ) )
{
pgpenvDestroy( context->pgpEnvironment );
context->pgpEnvironment = NULL;
}
if ( IsntNull( context->randomPoolContext ) )
{
pgpRandomDestroy( context->randomPoolContext );
context->randomPoolContext = NULL;
}
if ( IsntNull( context->randomPoolX9_17 ) )
{
pgpRandomDestroy( context->randomPoolX9_17 );
context->randomPoolX9_17 = NULL;
}
if ( IsntNull( context->dummyRandomContext ) )
{
pgpRandomDestroy( context->dummyRandomContext );
context->dummyRandomContext = NULL;
}
if( IsntNull( context->passphraseCache ) )
{
PGPPurgePassphraseCache( context );
}
}
/*____________________________________________________________________________
Delete an existing PGPContext and all resources associated with it.
____________________________________________________________________________*/
PGPError
PGPFreeContext(PGPContextRef context)
{
PGPError err = kPGPError_NoErr;
PGPMemoryMgrRef mgr;
PGPValidateContext( context );
pgpEnterPGPErrorFunction();
/* Unlink context from static list */
if( context == pgpsdkFirstContext )
{
pgpsdkFirstContext = context->nextContext;
} else {
PGPContextRef prevContext = pgpsdkFirstContext;
while( prevContext->nextContext != context )
{
pgpAssert( IsntNull( prevContext->nextContext ) );
prevContext = prevContext->nextContext;
}
prevContext->nextContext = context->nextContext;
}
/* Get current time into rand pool */
PGPGlobalRandomPoolAddKeystroke( PGPGetTime() );
err = pgpSaveGlobalRandomPool( );
sDestroyContext( context );
mgr = context->memoryMgr;
PGPFreeData( context );
PGPFreeMemoryMgr( mgr );
/* Tell backend */
return( err );
}
/*____________________________________________________________________________
Allocate a block of memory using the allocator stored in a PGPContext.
____________________________________________________________________________*/
void *
pgpContextMemAlloc(
PGPContextRef context,
PGPSize requestSize,
PGPMemoryMgrFlags flags)
{
void * allocation = NULL;
PGPSize allocationSize = requestSize;
pgpAssert( pgpContextIsValid( context ) );
allocation = PGPNewData( context->memoryMgr,
allocationSize, flags );
return( allocation );
}
/*____________________________________________________________________________
Same as pgpContextMemAlloc(), but copies data into newly allocated block.
____________________________________________________________________________*/
void *
PGPContextMemAllocCopy(
PGPContextRef context,
PGPSize requestSize,
PGPMemoryMgrFlags flags,
const void * dataToCopy )
{
void * allocation;
pgpAssert( pgpContextIsValid( context ) );
allocation = PGPNewData( context->memoryMgr,
requestSize, flags & ~kPGPMemoryMgrFlags_Clear );
if ( IsntNull( allocation ) )
{
pgpCopyMemory( dataToCopy, allocation, requestSize );
}
return( allocation );
}
/*____________________________________________________________________________
Allocate a block of memory using the allocator stored in a PGPContext.
____________________________________________________________________________*/
PGPError
pgpContextMemRealloc(
PGPContextRef context,
void **allocation,
PGPSize requestSize,
PGPMemoryMgrFlags flags)
{
PGPError err = kPGPError_NoErr;
err = PGPReallocData( context->memoryMgr,
allocation, requestSize, flags );
return( err );
}
/*____________________________________________________________________________
Free a block of memory using the deallocator stored in a PGPContext.
____________________________________________________________________________*/
PGPError
pgpContextMemFree(
PGPContextRef context,
void * allocation)
{
PGPError err;
PGPValidateContext( context );
(void)context;
err = PGPFreeData( allocation );
return( err );
}
/*____________________________________________________________________________
"Lightweight" versions of memory allocators above, based on mempools.
Do cutback on the mempool to free all that has been allocated.
____________________________________________________________________________*/
void *
pgpContextMemPoolAlloc(
PGPContextRef context,
PGPSize requestSize,
PGPMemoryMgrFlags flags)
{
void * allocation = NULL;
PGPSize allocationSize = requestSize;
pgpAssert( pgpContextIsValid( context ) );
allocation = PGPNewData( context->memPoolMgr, allocationSize, flags );
return( allocation );
}
PGPError
pgpContextMemPoolRealloc(
PGPContextRef context,
void **allocation,
PGPSize requestSize,
PGPMemoryMgrFlags flags)
{
PGPError err = kPGPError_NoErr;
pgpAssert( pgpContextIsValid( context ) );
err = PGPReallocData( context->memPoolMgr, allocation, requestSize,
flags );
return( err );
}
/* Use this to free all data allocated since doing
* cutpool = *pgpPeekContextMemPool( context );
*/
void
pgpContextMemPoolCutBack(
PGPContextRef context,
MemPool const *cutpool)
{
pgpAssert( pgpContextIsValid( context ) );
memPoolCutBack( &context->memPool, cutpool );
}
/*____________________________________________________________________________
UserValue and other miscellaneous functions
____________________________________________________________________________*/
PGPError
PGPGetContextUserValue(
PGPContextRef context,
PGPUserValue *userValue)
{
PGPError err = kPGPError_NoErr;
PGPValidatePtr( userValue );
*userValue = NULL;
PGPValidateContext( context );
pgpEnterPGPErrorFunction();
*userValue = context->userValue;
return( err );
}
PGPMemoryMgrRef
PGPPeekContextMemoryMgr( PGPContextRef context )
{
if ( ! pgpContextIsValid( context ) )
return( NULL );
pgpEnterZeroFunction();
return( context->memoryMgr );
}
MemPool *
pgpPeekContextMemPool( PGPContextRef context )
{
if ( ! pgpContextIsValid( context ) )
return( NULL );
return( &context->memPool );
}
PGPMemoryMgrRef
pgpPeekContextMemPoolMgr( PGPContextRef context )
{
if ( ! pgpContextIsValid( context ) )
return( NULL );
pgpEnterZeroFunction();
return( context->memPoolMgr );
}
/* Iterate over all context values. If called with null, return first */
PGPContextRef
pgpContextNextContext( PGPContextRef ctx )
{
if( IsNull( ctx ) )
return pgpsdkFirstContext;
pgpAssert( pgpContextIsValid( ctx ) );
return ctx->nextContext;
}
PGPError
PGPSetContextUserValue(
PGPContextRef context,
PGPUserValue userValue)
{
PGPError err = kPGPError_NoErr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -