/*____________________________________________________________________________
	Allocate a page of memory from the VM system and make it non-pageable
	if possible.
____________________________________________________________________________*/
	static void *
sAllocatePages(
	HANDLE			hDriver,	/* may be NULL */
	PGPUInt32		numPages,
	PGPBoolean *	isNonPageable )
{
	void *	result	= NULL;
	
	pgpAssert( IsntNull( isNonPageable ) );
	
	*isNonPageable	= FALSE;
	
#if PGP_WIN32
	/* since the first parameter to VirtualAlloc is NULL, it automatically 
	   rounds allocation size up to next page boundary */
	result = VirtualAlloc ( NULL,
				numPages * kPageSize, MEM_COMMIT, PAGE_READWRITE );
	
	if ( IsntNull( result ) )
	{
		sLockMemory( hDriver, result, numPages * kPageSize, isNonPageable );
	}
#else
	(void)hDriver;
	result	= sInternalAlloc( numPages * kPageSize );
#endif
	
	return( result );
}
Esempio n. 2
0
/*____________________________________________________________________________
	Initialize contexts.
	If key is NULL, the current key is not changed.
	if iv is NULL, the IV is set to all zero.
____________________________________________________________________________*/
	static void
pgpCBCInit(
	PGPCBCContext *		ref,
	void const *		key,
	void const *		iv)
{
	PGPSize			blockSize;
	void const *	curIV	= iv;
	
	PGPGetSymmetricCipherSizes( ref->symmetricRef, NULL, &blockSize );
	
	if ( IsntNull( key ) )
	{
		PGPInitSymmetricCipher( ref->symmetricRef, key );
	}

	pgpClearMemory( ref->iv1, sizeof( ref->iv1 )  );
	pgpClearMemory( ref->iv2, sizeof( ref->iv2 )  );
	ref->iv = ref->iv1;

	if ( IsntNull( iv ) )
	{
		pgpCopyMemory( curIV, ref->iv, blockSize );
	}

	/* rely on the symmetric cipher to know whether it has been inited */
	/* also, iv of NULL is OK, since semantics say that means zeroes */
	ref->CBCInited		= TRUE;
}
	static void
sRemoveAndDisposeHeap(
	MyData *	myData,
	PageHeap *	heap )
{
	PageHeap *	next;
	
	if ( heap == myData->lastHeapUsed )
		myData->lastHeapUsed	= NULL;
		
	sHeapValidate( heap );
	
	next	= heap->next;
	
	if ( heap == myData->heapList )
	{
		myData->heapList	= next;
	}

	if ( IsntNull( heap->prev ) )
	{
		heap->prev->next	= heap->next;
	}
	if ( IsntNull( heap->next ) )
	{
		heap->next->prev	= heap->prev;
	}
	
	sDisposeHeap( myData->hDriver, heap );
}
Esempio n. 4
0
	PGPOptionListRef
PGPOEventHandler(
	PGPContextRef	context,
	PGPEventHandlerProcPtr	handler,
	PGPUserValue			userValue)
{
	PGPOptionListRef		optionList;
	PGPOEventHandlerDesc	*descriptor;
	
	pgpValidateOptionContext( context );
	pgpValidateOptionParam( IsntNull( handler ) );
	
	descriptor = (PGPOEventHandlerDesc *)
						pgpContextMemAlloc( context, sizeof(*descriptor), 0);
	if( IsntNull( descriptor ) )
	{
		PGPOptionValue	value;
		
		descriptor->handlerProc = handler;
		descriptor->userValue 	= userValue;
		
		value.asPtr = descriptor;
		
		optionList = pgpCreateStandardValueOptionList( context,  
							kPGPOptionType_EventHandler,
							&value, sizeof( *descriptor ),
							AllocatedOptionHandlerProc );
	}
	else
	{
		optionList = kPGPOutOfMemoryOptionListRef;
	}

	return( optionList );
}
Esempio n. 5
0
/*____________________________________________________________________________
	Caution: these exported IDs are used in client software.  If changed,
	they will break things like groups, which uses exported key IDs.
____________________________________________________________________________*/
PGPError
PGPExportKeyID(
    PGPKeyID const *	keyID,
    PGPByte				exportedData[ kPGPMaxExportedKeyIDSize ],
    PGPSize *			exportedLength )
{
    PGPError				err	= kPGPError_NoErr;
    PGPSize					size	= 0;
    PGPKeyIDType			type;
    PGPKeyIDPriv const *	priv	= (PGPKeyIDPriv const *)keyID;

    if ( IsntNull( exportedLength ) )
        *exportedLength	= 0;
    PGPValidatePtr( exportedData );
    pgpClearMemory( exportedData, kPGPMaxExportedKeyIDSize );
    PGPValidateKeyID( keyID );

    /* output one byte to indicate type, followed by bytes */
    size		= 1 + priv->length;
    type		= ( priv->length == 4 ) ? kKeyID4Byte : kKeyID8Byte;
    exportedData[ 0 ]	= (PGPByte)type;
    pgpCopyMemory( priv->bytes, &exportedData[ 1 ], priv->length );

    if ( IsntNull( exportedLength ) )
        *exportedLength	= size;

    return( err );
}
Esempio n. 6
0
/*
 * If bytefifo fails to take the requested data, assume that memory is
 * full.  Switch to using a file fifo and transfer all data from the
 * byte fifo to the file.  Hopefully this will free up enough memory
 * that we can complete.
 *
 * We can also be configured to have a maximum size for the byte fifo
 * and if we exceed that we will switch over.
 */
static size_t
flexFifoWrite(PGPFifoContext *fifo, PGPByte const *buf, size_t len)
{
	PGPByte *xbuf = NULL;
	size_t written;
	PGPSize	n;
	long err;
	PGPContextRef		cdkContext;
	
	pgpAssertAddrValid( fifo, PGPFifoContext );
	cdkContext	= fifo->context;

	if (fifo->infile)
		return pgpFifoWrite(ff, fifo->filefifo, buf, len);

	written = 0;
	if (fifo->bytefifosize + len <= fifo->maxbytefifosize)
		written = pgpFifoWrite(bf, fifo->bytefifo, buf, len);
	fifo->bytefifosize += written;
	if (written < len) {
		/* switch to file fifo */
		if (xbuf == NULL) {
			xbuf = (PGPByte *)pgpContextMemAlloc( cdkContext,
				kPGPFIFOBufSize * 2, 0);
			if (xbuf == NULL)
				return kPGPError_OutOfMemory;
		}
		/* Try to free up some memory from byte fifo */
		n = pgpFifoRead(bf, fifo->bytefifo, xbuf, sizeof(xbuf));
		fifo->filefifo = pgpFifoCreate( fifo->context, ff);
		if (!fifo->filefifo) {
			if( IsntNull( xbuf ) )
				pgpContextMemFree( cdkContext, xbuf);
			pgpFifoDestroy(bf, fifo->bytefifo);
			return kPGPError_OutOfMemory;
		}
		/* Transfer data from byte to file fifo */
		do {
			pgpAssert (n <= sizeof(xbuf));
			err = (long)pgpFifoWrite(ff, fifo->filefifo, xbuf, n);
			if (err < 0)
			{
				if( IsntNull( xbuf ) )
					pgpContextMemFree( cdkContext, xbuf);
				return (size_t)err;
			}
		} while ((n = pgpFifoRead(bf, fifo->bytefifo, xbuf,
					  sizeof(xbuf))) > 0);
		pgpFifoDestroy(bf, fifo->bytefifo);
		fifo->bytefifo = NULL;
		fifo->bytefifosize = 0;
		fifo->infile = 1;
		written += pgpFifoWrite(ff, fifo->filefifo, buf+written,
					len-written);
	}
	if( IsntNull( xbuf ) )
		pgpContextMemFree( cdkContext, xbuf);
	return written;
}
/*____________________________________________________________________________
	Free an allocation within a heap of specified size.
____________________________________________________________________________*/
	static void
sHeapFreeChunks(
	PageHeap *	heap,
	void *		allocation,
	PGPUInt32 	allocChunks )
{
	ChunkHeader *	chunkHeader;
	ChunkHeader *	cur;
	ChunkHeader *	preceeding	= NULL;
	
	pgpAssert( sHeapOwnsAllocation( heap, allocation ) );
	
	sHeapValidate( heap );
	
	heap->numFreeChunks	+= allocChunks;
	
	chunkHeader	= (ChunkHeader *)allocation;
	chunkHeader->numChunks	= allocChunks;
	
	/* find the first chunk which precedes this one */
	cur	= heap->freeList;
	while ( IsntNull( cur ) )
	{
		if ( chunkHeader < cur )
		{
			preceeding	= cur->prev;
			break;
		}
		cur	= cur->next;
	}
	
	if ( IsNull( preceeding ) )
	{
		chunkHeader->next	= heap->freeList;
		chunkHeader->prev	= NULL;
		if ( IsntNull( chunkHeader->next ) )
			chunkHeader->next->prev	= chunkHeader;
		heap->freeList		= chunkHeader;
	}
	else
	{
		chunkHeader->prev	= preceeding;
		chunkHeader->next	= preceeding->next;
		if ( IsntNull( preceeding->next ) )
			preceeding->next->prev	= chunkHeader;
		preceeding->next	= chunkHeader;
	}
	
	sMergeFree( chunkHeader );
	
	sHeapValidate( heap );
}
Esempio n. 8
0
	PGPOptionListRef
PGPOPGPMIMEEncoding(
	PGPContextRef	context,
	PGPBoolean		mimeEncoding,
	PGPSize *		mimeBodyOffset,
	char *			mimeSeparator
	)
{
	PGPOptionListRef			optionList;
	PGPOPGPMIMEEncodingDesc		*descriptor;

	pgpValidateOptionContext( context );
	if ( mimeEncoding )
	{
		if ( IsntNull( mimeBodyOffset ) )
			*mimeBodyOffset	= 0;
		if ( IsntNull( mimeSeparator ) )
			*mimeSeparator	= '\0';
			
		pgpValidateOptionParam( IsntNull( mimeBodyOffset ) );
		pgpValidateOptionParam( IsntNull( mimeSeparator ) );
		pgpDebugWhackMemory( mimeSeparator, kPGPMimeSeparatorSize );
	}
	else
	{
		/* mime off--ignore other params */
	}

	descriptor = (PGPOPGPMIMEEncodingDesc *)
						pgpContextMemAlloc( context, sizeof(*descriptor), 0 );
	if( IsntNull( descriptor ) )
	{
		PGPOptionValue	value;

		descriptor->mimeEncoding	= (PGPUInt32) mimeEncoding;
		descriptor->mimeBodyOffset 	= mimeEncoding ? mimeBodyOffset : NULL;
		descriptor->mimeSeparator 	= mimeEncoding ? mimeSeparator : NULL;

		value.asPtr = descriptor;

		optionList = pgpCreateStandardValueOptionList( context,
							kPGPOptionType_PGPMIMEEncoding,
							&value, sizeof( *descriptor ),
							AllocatedOptionHandlerProc );
	}
	else
	{
		optionList = kPGPOutOfMemoryOptionListRef;
	}
	
	return( optionList );
}
	static void
sUnlockMemory(
	HANDLE		hDriver,
	void *		mem,
	PGPSize		numBytes )
{
	PGPMEMLOCKSTRUCT	mls;
	DWORD				dw;
	BOOL				bDIOreturn;

	if ( IsntNull( hDriver ) )
	{
		mls.pMem = mem;
		mls.ulNumBytes = numBytes;
		bDIOreturn = DeviceIoControl(	hDriver, 
										IOCTL_PGPMEMLOCK_UNLOCK_MEMORY, 
										&mls, 
										sizeof( mls ), 
										&mls, 
										sizeof( mls ), 
										&dw, 
										NULL );
		pgpAssertMsg ( bDIOreturn, "DIOC error from page-locking driver" );
		if ( bDIOreturn )
		{
			pgpAssertMsg ( mls.ulError == 0, 
						  "Internal error in page-locking driver" );
		}
	}
	else 
	{
	 	VirtualUnlock ( mem, numBytes );	/* call for good measure*/
	}
}
Esempio n. 10
0
/*____________________________________________________________________________
	Dispose MyData data structure contents and free it.
____________________________________________________________________________*/
	static PGPError
sCleanupMyData( MyData *myData )
{
	PageHeap *	heap	= NULL;
	
	pgpAssertMsg( IsNull( myData->heapList ),
		"sCleanupMyData(): WARNING: secure memory is being leaked" );

	while ( IsntNull( myData->heapList ) )
	{
		sRemoveAndDisposeHeap( myData, myData->heapList );
	}
	
#if PGP_WIN32
	if ( myData->hDriver != NULL ) 
	{
		CloseHandle ( myData->hDriver );
		myData->hDriver = NULL;
	}
#endif

	sInternalFree( myData );

	return kPGPError_NoErr;
}
Esempio n. 11
0
/* Creates a new bignum */
	PGPError
PGPNewBigNum(
	PGPMemoryMgrRef	mgr,
	PGPBoolean		secure,
	PGPBigNumRef *	newBN )
{
	PGPError		err	= kPGPError_NoErr;
	PGPBigNumRef	ref;
	
	PGPValidatePtr( newBN );
	*newBN	= kPGPInvalidBigNumRef;
	
	ref	= (PGPBigNumRef) PGPNewData( mgr,
			sizeof( **newBN ), kPGPMemoryMgrFlags_Clear);
	if ( IsntNull( ref ) )
	{
		bnBegin( &ref->bn, mgr, secure );
		*newBN	= ref;
	}
	else
	{
		err	= kPGPError_OutOfMemory;
	}
	
	return( err );
}
Esempio n. 12
0
	static PageHeap *
sCreateAndAddHeap(
	MyData *	myData,
	PGPSize		heapSize )
{
	PGPError	err	= kPGPError_NoErr;
	PageHeap *	heap	= NULL;
	
	pgpAssert( ( heapSize % kPageSize ) == 0 );
		
	heap	= sCreateHeap( myData->hDriver, heapSize);
	if ( IsntNull( heap ) )
	{
		heap->prev	= NULL;
		heap->next	= NULL;
			
		if ( IsNull( myData->heapList ) )
		{
			myData->heapList	= heap;
		}
		else
		{
			heap->next	= myData->heapList;
			myData->heapList->prev	= heap;
			myData->heapList		= heap;
		}
		sHeapValidate( heap );
	}
	
	return( heap );
}
Esempio n. 13
0
/*____________________________________________________________________________
____________________________________________________________________________*/
	PGPError 
PGPInitCBC(
	PGPCBCContextRef	ref,
	const void *		key,
	const void *		initializationVector )
{
	PGPError			err	= kPGPError_NoErr;
	
	PGPValidateCBC( ref );
	/* at least one param must be non-nil */
	PGPValidateParam( IsntNull( key ) || IsntNull( initializationVector ) );
		
	pgpCBCInit( ref, key, initializationVector);
	
	return( err );
}
Esempio n. 14
0
	static void
sFreeBytes(
	MyData *		myData,
	void *			allocation,
	PGPSize			allocationSize )
{
	PageHeap *	heap;
	PGPSize		allocChunks;
	
	allocChunks	= sSizeToChunks( allocationSize );
	
	heap	= myData->heapList;
	while ( IsntNull( heap ) )
	{
		sHeapValidate( heap );
		
		if ( sHeapOwnsAllocation( heap, allocation ) )
		{
			sHeapFreeChunks( heap, allocation, allocChunks );
			
			if ( sHeapIsEmpty( heap ) )
			{
				sRemoveAndDisposeHeap( myData, heap );
			}
			break;
		}
		heap	= heap->next;
	}
}
Esempio n. 15
0
File: c4hash.c Progetto: rhardman/C4
C4Err HASH_Import(void *inData, size_t bufSize, HASH_ContextRef * ctx)
{
    C4Err        err = kC4Err_NoErr;
    HASH_Context*   hashCTX = NULL;
    
    ValidateParam(ctx);
    *ctx = NULL;
    
    
    if(sizeof(HASH_Context) != bufSize)
        RETERR( kC4Err_BadParams);
    
    hashCTX = XMALLOC(sizeof (HASH_Context)); CKNULL(hashCTX);
    
    COPY( inData, hashCTX, sizeof(HASH_Context));
    
    validateHASHContext(hashCTX);
    
    *ctx = hashCTX;
    
done:
    
    if(IsC4Err(err))
    {
        if(IsntNull(hashCTX))
        {
            XFREE(hashCTX);
        }
    }
    
    return err;
}
Esempio n. 16
0
/*
 * Return the StringToKey type for unlocking the given key.  We use
 * kPGPStringToKey_Literal to flag a secret split unlocking buffer.
 * Returns kPGPStringToKey_Simple if key has no passphrase.
 */
static PGPError
dsaS2KType(
	PGPSecKey const *seckey,
	PGPStringToKeyType *s2kType
	)
{
	DSAsecPlus *sec = (DSAsecPlus *)seckey->priv;
	PGPByte alg;
	int i;

	ASSERTDSA(seckey->pkAlg);

	/* note that 0 is a valid type, but use it as default anyway */
	if( IsntNull( s2kType ) )
		*s2kType = (PGPStringToKeyType) 0;

	/* Check packet for basic consistency */
	i = pgpBnParse(sec->cryptkey, sec->cklen, 4, NULL, NULL, NULL, NULL);
	if (i < 0)
		return (PGPError)i;

	/* Get the encryption algorithm (cipher number).  0 == no encryption */
	alg = sec->cryptkey[i] & 255;

	if (alg == 255) {
		/* New style has 255 then algorithm value then S2K */
		*s2kType = (PGPStringToKeyType) sec->cryptkey[i+2];
	} else {
		/* Unencrypted or old-style simple encryption */
		*s2kType = kPGPStringToKey_Simple;
	}

	return kPGPError_NoErr;
}
Esempio n. 17
0
PGPError PGPNewSKEP(PGPContextRef context, 
				PGPtlsContextRef tlsContext,
				PGPskepRef *skep)
{
	PGPMemoryMgrRef memoryMgr = NULL;
	PGPError		err = kPGPError_NoErr;

	if (IsntNull(skep))
		*skep = NULL;

	PGPValidateParam(PGPContextRefIsValid(context));
	PGPValidatePtr(skep);

	memoryMgr = PGPGetContextMemoryMgr(context);
	
	pgpAssert(PGPMemoryMgrIsValid(memoryMgr));
	if (!PGPMemoryMgrIsValid(memoryMgr))
		return kPGPError_BadParams;

	*skep = (PGPskepRef) PGPNewData(memoryMgr, sizeof(PGPskep), 
							kPGPMemoryMgrFlags_Clear);

	pgpAssert(IsntNull(*skep));
	if (IsNull(*skep))
		return kPGPError_OutOfMemory;

	if (PGPtlsContextRefIsValid(tlsContext))
	{
		(*skep)->tlsContext = tlsContext;
		(*skep)->freeTLSContext = FALSE;
	}
	else
	{
		PGPNewTLSContext(context, &((*skep)->tlsContext));
		(*skep)->freeTLSContext = TRUE;
	}

	(*skep)->magic = kPGPskepMagic;
	(*skep)->context = context;
	(*skep)->socketOut = NULL;
	(*skep)->socketIn = NULL;
	(*skep)->eventHandler = NULL;
	(*skep)->userValue = NULL;
	(*skep)->cancel = FALSE;

	return err;
}
Esempio n. 18
0
PGPBoolean
pgpKeyIDIsValid( PGPKeyID const * id )
{
    PGPKeyIDPriv const *	priv	= (PGPKeyIDPriv const *)id;

    return( IsntNull( priv ) &&
            ( priv->length == 4 || priv->length == 8 ) );
}
Esempio n. 19
0
static bool sCBC_ContextIsValid( const CBC_ContextRef  ref)
{
    bool       valid	= false;
    
    valid	= IsntNull( ref ) && ref->magic	 == kCBC_ContextMagic;
    
    return( valid );
}
Esempio n. 20
0
File: c4ecc.c Progetto: rhardman/C4
bool sECC_ContextIsValid( const ECC_ContextRef  ref)
{
    bool       valid	= false;
    
    valid	= IsntNull( ref ) && ref->magic	 == kECC_ContextMagic;
    
    return( valid );
}
Esempio n. 21
0
	static void
sMergeFree( ChunkHeader *chunkHeader )
{
	ChunkHeader *	prev	= NULL;
	
	pgpAssert( IsntNull( chunkHeader ) );
	
	if ( sChunksAreAdjacent( chunkHeader ) )
	{
		sMergeChunks( chunkHeader );
	}
	prev	= chunkHeader->prev;
	if ( IsntNull( prev ) && sChunksAreAdjacent( prev ) )
	{
		sMergeChunks( prev );
		chunkHeader	= NULL;
	}
}
Esempio n. 22
0
	PGPOptionListRef
PGPOAllocatedOutputBuffer(
	PGPContextRef	context,
	void			**buffer,
	PGPSize			maximumBufferSize,
	PGPSize			*actualBufferSize)
{
	PGPOptionListRef				optionList;
	PGPOAllocatedOutputBufferDesc	*descriptor;
	
	if ( IsntNull( buffer ) )
		*buffer	= NULL;
	if ( IsntNull( actualBufferSize ) )
		*actualBufferSize	= 0;
	pgpValidateOptionParam( IsntNull( buffer ) );
	pgpValidateOptionParam( IsntNull( actualBufferSize ) );
	pgpValidateOptionParam( maximumBufferSize > 0 );
	pgpValidateOptionContext( context );
	
	descriptor = (PGPOAllocatedOutputBufferDesc *)
			pgpContextMemAlloc( context,
				sizeof(*descriptor), kPGPMemoryMgrFlags_Clear);
	if( IsntNull( descriptor ) )
	{
		PGPOptionValue	value;
		
		descriptor->buffer 				= buffer;
		descriptor->maximumBufferSize 	= maximumBufferSize;
		descriptor->actualBufferSize	= actualBufferSize;
		
		value.asPtr = descriptor;
		
		optionList = pgpCreateStandardValueOptionList( context,  
							kPGPOptionType_OutputAllocatedBuffer,
							&value, sizeof( *descriptor ),
							AllocatedOptionHandlerProc );
	}
	else
	{
		optionList = kPGPOutOfMemoryOptionListRef;
	}
	
	return( optionList );
}
Esempio n. 23
0
/*____________________________________________________________________________
	Create a heap of the specified size.
____________________________________________________________________________*/
	static PageHeap *
sCreateHeap(
	HANDLE		hDriver,
	PGPSize		heapSize)
{
	PGPError	err	= kPGPError_NoErr;
	PageHeap *	heap	= NULL;
	PGPUInt32	numPages;
	PGPUInt32	numChunks;
	
	numPages	= heapSize / kPageSize;
	if ( ( heapSize % kPageSize ) != 0 )
		numPages	+= 1;
	
	numChunks	= ( numPages * kPageSize ) / kChunkSize;
	
	heap	= (PageHeap *)sInternalAlloc( sizeof( *heap ) );
	if ( IsntNull( heap ) )
	{
		heap->hDriver		= hDriver;
		heap->numPages		= numPages;
		heap->numFreeChunks	= numChunks;
		
		heap->pages	= sAllocatePages( hDriver,
						numPages, &heap->isNonPageable );
		if ( IsntNull( heap->pages ) )
		{
			ChunkHeader *	chunkHeader	= (ChunkHeader *)heap->pages;
			
			chunkHeader->next		= NULL;
			chunkHeader->prev		= NULL;
			chunkHeader->numChunks	= numChunks;
			heap->freeList	= chunkHeader;
		}
		else
		{
			sInternalFree( heap );
			heap	= NULL;
			err	= kPGPError_OutOfMemory;
		}
	}
	
	return( heap );
}
Esempio n. 24
0
/*
 * Return the algorithm and (symmetric) key size used for locking/unlocking
 * the secret key.
 */
static PGPError
dsaLockingAlgorithm(
	PGPSecKey const *seckey,
	PGPCipherAlgorithm *pAlg,
	PGPSize *pAlgKeySize
	)
{
	DSAsecPlus *sec = (DSAsecPlus *)seckey->priv;
	PGPCipherVTBL const *cipher;
	PGPByte alg;
	int i;

	ASSERTDSA(seckey->pkAlg);

	if( IsntNull( pAlg ) )
		*pAlg = (PGPCipherAlgorithm) 0;
	if( IsntNull( pAlgKeySize ) )
		*pAlgKeySize = (PGPSize) 0;

	/* Check packet for basic consistency */
	i = pgpBnParse(sec->cryptkey, sec->cklen, 4, NULL, NULL, NULL, NULL);
	if (i < 0)
		return (PGPError)i;

	/* Get the encryption algorithm (cipher number).  0 == no encryption */
	alg = sec->cryptkey[i] & 255;

	/* New style has 255 then algorithm value */
	if (alg == 255)
		alg = sec->cryptkey[i+1] & 255;

	cipher = pgpCipherGetVTBL( (PGPCipherAlgorithm)alg);
	if (!cipher)
		return kPGPError_BadCipherNumber;

	/* Success */
	if( IsntNull( pAlg ) )
		*pAlg = (PGPCipherAlgorithm) alg;
	if( IsntNull( pAlgKeySize ) )
		*pAlgKeySize = cipher->keysize;

	return kPGPError_NoErr;
}
Esempio n. 25
0
	static PGPBoolean
pgpCBCIsValid( const PGPCBCContext * ref)
{
	PGPBoolean	valid	= FALSE;
	
	valid	= IsntNull( ref ) && ref->magic	 == kCBCMagic
				&& (ref->iv == ref->iv1  ||  ref->iv == ref->iv2);
	
	return( valid );
}
Esempio n. 26
0
	PGPError 
PGPNewCBCContext(
	PGPSymmetricCipherContextRef	symmetricRef,
	PGPCBCContextRef *				outRef )
{
	PGPCBCContextRef				newRef	= NULL;
	PGPError						err	= kPGPError_NoErr;
	PGPMemoryMgrRef					memoryMgr	= NULL;
	
	PGPValidatePtr( outRef );
	*outRef	= NULL;
	PGPValidatePtr( symmetricRef );
	
	memoryMgr	= pgpGetSymmetricCipherMemoryMgr( symmetricRef );
	newRef	= (PGPCBCContextRef)
			PGPNewData( memoryMgr,
				sizeof( *newRef ), 0 | kPGPMemoryMgrFlags_Clear);
			
	if ( IsntNull( newRef ) )
	{
#if PGP_DEBUG
		/* make original invalid to enforce semantics */
		PGPSymmetricCipherContextRef	tempRef;
		err	= PGPCopySymmetricCipherContext( symmetricRef, &tempRef );
		if ( IsntPGPError( err ) )
		{
			PGPFreeSymmetricCipherContext( symmetricRef );
			symmetricRef	= tempRef;
		}
		err	= kPGPError_NoErr;
#endif

		newRef->magic			= kCBCMagic;
		newRef->CBCInited		= FALSE;
		newRef->symmetricRef	= symmetricRef;
		newRef->iv				= newRef->iv1;
		newRef->memoryMgr		= memoryMgr;
		
		/* make sure we clean up */
		if ( IsPGPError( err ) )
		{
			PGPFreeCBCContext( newRef );
			newRef	= NULL;
		}
	}
	else
	{
		/* we own it, so dispose it */
		PGPFreeSymmetricCipherContext( symmetricRef );
		err	= kPGPError_OutOfMemory;
	}
	
	*outRef	= newRef;
	return( err );
}
Esempio n. 27
0
	PGPOptionListRef
PGPOOutputBuffer(
	PGPContextRef	context,
	void			*buffer,
	PGPSize			bufferSize,
	PGPSize			*outputDataLength)
{
	PGPOptionListRef		optionList;
	PGPOOutputBufferDesc	*descriptor;
	
	pgpValidateOptionParam( IsntNull( outputDataLength ) );
	*outputDataLength	= 0;
	pgpValidateOptionContext( context );
	pgpValidateOptionParam( IsntNull( buffer ) && bufferSize > 0 );

	pgpDebugWhackMemory( buffer, bufferSize );
	
	descriptor = (PGPOOutputBufferDesc *)
		pgpContextMemAlloc( context, sizeof(*descriptor),
		kPGPMemoryMgrFlags_Clear);
	if( IsntNull( descriptor ) )
	{
		PGPOptionValue	value;
		
		descriptor->buffer 				= buffer;
		descriptor->bufferSize 			= bufferSize;
		descriptor->outputDataLength	= outputDataLength;
		
		value.asPtr = descriptor;
		
		optionList = pgpCreateStandardValueOptionList( context, 
							kPGPOptionType_OutputBuffer,
							&value, sizeof( *descriptor ),
							AllocatedOptionHandlerProc );
	}
	else
	{
		optionList = kPGPOutOfMemoryOptionListRef;
	}
	
	return( optionList );
}
Esempio n. 28
0
	PGPError 
pgpenvCreate(
	PGPContextRef	context,
	PGPEnv **		outEnv)
{
	PGPEnv *	env	= NULL;
	PGPError	err	= kPGPError_NoErr;

	pgpAssertAddrValid( outEnv, PGPEnv * );
	
	*outEnv	= NULL;
	
	env = (PGPEnv *)pgpContextMemAlloc( context, sizeof(*env),
		kPGPMemoryMgrFlags_Clear);
	if ( IsntNull( env ) )
	{
		env->context	= context;

		/* Now initialize the environment pointers as appropriate */
		pgpenvSetInt (env, PGPENV_CIPHER, kPGPCipherAlgorithm_IDEA,
			      PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_HASH, kPGPHashAlgorithm_MD5,
						PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_COMPRESS, 1, PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_COMPRESSALG, PGP_COMPRESSALG_ZIP,
			      PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_COMPRESSQUAL, 5, PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_ARMORLINES, 0, PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_CERTDEPTH, 4, PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_COMPLETES, 1, PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_MARGINALS, 2, PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_TRUSTED, 120, PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_CLEARSIG, 1, PGPENV_PRI_PUBDEFAULT);
		pgpenvSetInt (env, PGPENV_VERSION, PGPVERSION_3,
			      PGPENV_PRI_PUBDEFAULT);

		pgpenvSetString (env, PGPENV_PGPPATH, "", PGPENV_PRI_PUBDEFAULT);
		pgpenvSetString (env, PGPENV_UPATH, 
				 "/home/ch/keyserver", PGPENV_PRI_PUBDEFAULT);

	#ifdef MSDOS
		pgpenvSetString (env, PGPENV_CHARSET, "cp850", PGPENV_PRI_PUBDEFAULT);
	#else
		pgpenvSetString (env, PGPENV_CHARSET, "noconv", PGPENV_PRI_PUBDEFAULT);
	#endif
	}
	else
	{
		err	= kPGPError_OutOfMemory;
	}
	
	*outEnv	= env;
	return err;
}
Esempio n. 29
0
	static void
sMergeChunks( ChunkHeader *chunkHeader )
{
	ChunkHeader *	next	= chunkHeader->next;
	
	pgpAssert( sChunksAreAdjacent( chunkHeader ) );
	
	chunkHeader->numChunks	+= next->numChunks;
	if ( IsntNull( next->next ) )
		next->next->prev	= chunkHeader;
	chunkHeader->next	= next->next;
}
Esempio n. 30
0
	static void *
sInternalAlloc( PGPSize numBytes )
{
	void *	result	= malloc( numBytes );
	
	if ( IsntNull( result ) )
	{
		pgpClearMemory( result, numBytes );
	}
	
	return( result );
}