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

📄 pgprecipientdialogcommon.cpp

📁 vc环境下的pgp源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*____________________________________________________________________________
	Copyright (C) 1997 Network Associates Inc. and affiliated companies.
	All rights reserved.
	
	$Id: pgpRecipientDialogCommon.cpp,v 1.35.6.1 1999/06/04 01:12:09 heller Exp $
____________________________________________________________________________*/

#include <ctype.h>
#include <string.h>

#include "pgpErrors.h"
#include "pgpKeys.h"
#include "pgpMem.h"
#include "pgpUtilities.h"

#include "pgpDialogs.h"
#include "pgpRecipientDialogCommon.h"

#define	UserIsVisible(user)												\
	( IsntNull( user ) &&												\
		( user->location == kPGPRecipientUserLocation_UserList ) ||		\
		( user->location == kPGPRecipientUserLocation_RecipientList ) )

#define kMandatoryARRMask		0x80
#define	IsMandatoryARRClass(x)	(((x) & kMandatoryARRMask) != 0)

	static PGPUInt32
GetNextMarkValue(void)
{
	static PGPUInt32	sMarkValue = 0;
	
	return( ++sMarkValue );
}

	static const char *
FindSubstring(
	const char * inBuffer,
	const char * inSearchStr)
{
	const char *	currBuffPointer = inBuffer;
	
	while (*currBuffPointer != 0)
	{
		const char * compareOne = currBuffPointer;
		const char * compareTwo = inSearchStr;
		
		while ( tolower(*compareOne) == tolower(*compareTwo) )
		{
			compareOne++;
			compareTwo++;
			if (*compareTwo == 0)
			{
				return (char *) currBuffPointer;
			}
		}
		currBuffPointer++;
	}
	return NULL;
}

	static PGPError
AllocateNewUsers(PGPRecipientsList *recipients)
{
	PGPError				err = kPGPError_NoErr;
	PGPRecipientUserList	*userList;
	
	userList = (PGPRecipientUserList *) PGPNewData( recipients->memoryMgr,
							sizeof( *userList ), 0 );
	if( IsntNull( userList ) )
	{
		PGPUInt32	userIndex;
		
		// Add user list to recipients.
		userList->nextUserList	= recipients->userLists;
		recipients->userLists	= userList;
	
		// Thread new users into recipients free list
		for( userIndex = 0; userIndex < kPGPNumUserListUsers; userIndex++ )
		{
			userList->users[userIndex].nextUser = recipients->freeUsers;
			recipients->freeUsers				= &userList->users[userIndex];
		}
	}
	else
	{
		err = kPGPError_OutOfMemory;
	}
	
	return( err );
}

	static PGPError
NewUser(
	PGPRecipientsList	*recipients,
	PGPRecipientUser	**newUser)
{
	PGPError	err = kPGPError_NoErr;
	
	*newUser = NULL;
	
	if( IsNull( recipients->freeUsers ) )
	{
		err = AllocateNewUsers( recipients );
	}
	
	if( IsntPGPError( err ) )
	{
		pgpAssert( IsntNull( recipients->freeUsers ) );
		
		*newUser				= recipients->freeUsers;
		recipients->freeUsers 	= recipients->freeUsers->nextUser;
		
		pgpClearMemory( *newUser, sizeof( **newUser ) );
		
		(**newUser).recipients = recipients;
	}
	
	return( err );
}

	static PGPError
AllocateNewKeys(PGPRecipientsList *recipients)
{
	PGPError			err = kPGPError_NoErr;
	PGPRecipientKeyList	*keyList;
	
	keyList = (PGPRecipientKeyList *) PGPNewData( recipients->memoryMgr,
							sizeof( *keyList ), 0 );
	if( IsntNull( keyList ) )
	{
		PGPUInt32	keyIndex;
		
		// Add key list to recipients.
		keyList->nextKeyList	= recipients->keyLists;
		recipients->keyLists	= keyList;
	
		// Thread new keys into recipients free list
		for( keyIndex = 0; keyIndex < kPGPNumKeyListKeys; keyIndex++ )
		{
			keyList->keys[keyIndex].nextKey = recipients->freeKeys;
			recipients->freeKeys			= &keyList->keys[keyIndex];
		}
	}
	else
	{
		err = kPGPError_OutOfMemory;
	}
	
	return( err );
}

	static PGPError
NewKey(
	PGPRecipientsList	*recipients,
	PGPRecipientKey		**newKey)
{
	PGPError	err = kPGPError_NoErr;
	
	*newKey = NULL;
	
	if( IsNull( recipients->freeKeys ) )
	{
		err = AllocateNewKeys( recipients );
	}
	
	if( IsntPGPError( err ) )
	{
		pgpAssert( IsntNull( recipients->freeKeys ) );

		*newKey 				= recipients->freeKeys;
		recipients->freeKeys 	= recipients->freeKeys->nextKey;
		
		pgpClearMemory( *newKey, sizeof( **newKey ) );
	}
	
	return( err );
}

	static void
FreeKey(PGPRecipientKey *key)
{
	pgpAssert( IsntNull( key ) );
	
	if( IsntNull( key->arrKeys ) )
	{
		PGPFreeData( key->arrKeys );
		key->arrKeys = NULL;
	}
	
	/* The key is not individually allocated, so no disposal is necessary */
}

	static PGPError
RememberName(
	PGPRecipientsList	*recipients,
	const char			*name,
	PGPUInt32			*nameOffset)
{
	PGPError	err = kPGPError_NoErr;
	PGPUInt32	nameLength;
	
	pgpAssert( IsntNull( recipients) );
	pgpAssert( IsntNull( name) );
	pgpAssert( IsntNull( nameOffset) );
	pgpAssert( recipients->nextNameOffset <= recipients->nameListSize );

	*nameOffset	= 0;
	nameLength 	= strlen( name ) + 1;	/* Account for trailing '\0' */

	/* Limit the names to 128 characters in case name is corrupted */
	if( nameLength > 128 )
		nameLength = 128;
	
	if( nameLength > recipients->nameListSize -
				recipients->nextNameOffset )
	{
		PGPUInt32	newNameListSize;
		char		*newNameList;
		
		// Need to grow/allocate the names list.
		
		#define kNameListGrowSize	1024L
	
		newNameListSize = recipients->nameListSize + kNameListGrowSize;
		
		newNameList = (char *) PGPNewData( recipients->memoryMgr,
									newNameListSize, 0 );
		if( IsntNull( newNameList ) )
		{
			if( IsntNull( recipients->nameList ) )
			{
				pgpCopyMemory( recipients->nameList, newNameList,
						recipients->nameListSize );
						
				PGPFreeData( recipients->nameList );
			}
			else
			{
				// Special case. Offset zero is invalid and is an empty string
				// Start at offset 1
				
				pgpAssert( recipients->nextNameOffset == 0 );
				
				recipients->nextNameOffset = 1;
				newNameList[0] = 0;
			}
			
			recipients->nameList 		= newNameList;
			recipients->nameListSize	= newNameListSize;
		}
		else
		{
			err = kPGPError_OutOfMemory;
		}
	}
	
	if( IsntPGPError( err ) )
	{
		pgpAssert( nameLength <= recipients->nameListSize -
										recipients->nextNameOffset );
		
		pgpCopyMemory( name, &recipients->nameList[recipients->nextNameOffset],
							nameLength );
		
		*nameOffset = recipients->nextNameOffset;
		recipients->nextNameOffset += nameLength;
	}
	
	return( err );
}

	static PGPError
FindARRKeys(
	PGPRecipientsList	*recipients,
	PGPRecipientKey		*theKey)
{
	PGPError	err = kPGPError_NoErr;
	
	if( IsntNull( theKey->arrKeys ) )
	{
		PGPFreeData( theKey->arrKeys );
		
		theKey->arrKeys 		= NULL;
		theKey->numARRKeys		= 0;
		theKey->haveMissingARRs	= FALSE;
	}
	
	if( recipients->arrEnforcement != kPGPARREnforcement_None )
	{
		PGPUInt32	numARRKeys;

		err = PGPCountAdditionalRecipientRequests( theKey->keyRef,
						&numARRKeys );
		if( IsntPGPError( err ) && numARRKeys != 0)
		{
			theKey->numARRKeys = (PGPUInt16) numARRKeys;

			theKey->arrKeys = (PGPRecipientKeyARRInfo *) PGPNewData(
										recipients->memoryMgr,
										theKey->numARRKeys *
										sizeof( theKey->arrKeys[0] ),
										kPGPMemoryMgrFlags_Clear );
			if( IsntNull( theKey->arrKeys ) )
			{
				PGPUInt32	arrIndex;
				
				for( arrIndex = 0; arrIndex < theKey->numARRKeys;
							++arrIndex )
				{
					PGPRecipientKeyARRInfo	*arrInfo;
					PGPKeyRef				arrKeyRef = kInvalidPGPKeyRef;
					
					arrInfo = &theKey->arrKeys[arrIndex];
					
					err = PGPGetIndexedAdditionalRecipientRequestKey(
								theKey->keyRef, recipients->keySet, arrIndex,
								&arrKeyRef, &arrInfo->keyID,
								&arrInfo->arrClass );
					if( IsntPGPError( err ) )
					{
						if( PGPKeyRefIsValid( arrKeyRef ) )
						{
							err = PGPGetKeyUserVal( arrKeyRef,
									(PGPUserValue *) &arrInfo->key );
						}
						
						if( IsNull( arrInfo->key ) )
						{
							arrInfo->keyMissing = TRUE;
						}
						else if( ! arrInfo->key->isVisible )
						{
							arrInfo->key = NULL;
						}
					
						if( IsMandatoryARRClass( arrInfo->arrClass ) &&
							IsNull( arrInfo->key ) )
						{
							theKey->haveMissingARRs = TRUE;
						}
					}
					
					if( IsPGPError( err ) )
					break;
				}
			}
			else
			{
				err = kPGPError_OutOfMemory;
			}
		}
	}
	
	return( err );
}

	static PGPError
AddKey(
	PGPRecipientsList	*recipients,
	PGPKeyIterRef		iterator,
	PGPKeyRef			keyRef,
	PGPBoolean			isDefaultKey,
	PGPBoolean			markAsNew,
	PGPRecipientKey		**recipientKey)
{
	PGPError			err = kPGPError_NoErr;
	PGPRecipientKey		*theKey;
	
	err = NewKey( recipients, &theKey );
	if( IsntPGPError( err ) )
	{
		PGPUserIDRef	primaryUserID;
		
		theKey->keyRef			= keyRef;
		theKey->isDefaultKey	= isDefaultKey;
		theKey->isNewKey		= markAsNew;
		theKey->isVisible		= FALSE;		/* Assume not visible */
		
		(void) PGPGetPrimaryUserID( keyRef, &primaryUserID );
		
		if( recipients->arrEnforcement != kPGPARREnforcement_None )
		{
			err = FindARRKeys( recipients, theKey );
		}
		
		if( IsntPGPError( err ) )
		{
			PGPInt32	algorithm;

			err = PGPGetKeyNumber( keyRef, kPGPKeyPropAlgID, &algorithm );
		
			theKey->algorithm = (PGPPublicKeyAlgorithm)algorithm;
		}
		
		if( IsntPGPError( err ) )
		{
			PGPBoolean	isAxiomaticKey;
			
			err = PGPGetKeyBoolean( keyRef, kPGPKeyPropIsAxiomatic,
								&isAxiomaticKey );
								
			theKey->isAxiomaticKey = isAxiomaticKey;
		}

		if( IsntPGPError( err ) )
		{
			PGPBoolean	isSecretKey;
			
			err = PGPGetKeyBoolean( keyRef, kPGPKeyPropIsSecret,
						&isSecretKey );
			
			theKey->isSecretKey = isSecretKey;
		}	
		
		if( IsntPGPError( err ) )
		{
			PGPBoolean	canEncrypt;
			
			err = PGPGetKeyBoolean( keyRef, kPGPKeyPropCanEncrypt,
						&canEncrypt );
								
			theKey->canEncrypt = canEncrypt;
		}

		if( IsntPGPError( err ) )
		{
			PGPInt32	keyBits;
			
			err = PGPGetKeyNumber( keyRef, kPGPKeyPropBits, &keyBits );

			theKey->keyBits = (PGPUInt16) keyBits;
		}
			
		if( IsntPGPError( err ) )
		{
			// Iterate our one-key set to the first key
			
			if( theKey->algorithm == kPGPPublicKeyAlgorithm_DSA )
			{
				PGPSubKeyRef	subKey;

				// Get the subkey to determine the
				// encryption key bits.
					
				err = PGPKeyIterNextSubKey( iterator, &subKey );
				if( IsntPGPError( err ) )
				{
					PGPInt32	keyBits;
					
					err = PGPGetSubKeyNumber( subKey, kPGPKeyPropBits,
								&keyBits );
					
					theKey->subKeyBits = (PGPUInt16) keyBits;
				}
				
				if( err == kPGPError_EndOfIteration )
					err = kPGPError_NoErr;
			}
			
			if( IsntPGPError( err ) )
			{
				PGPUserIDRef	curUserID;
				
				err = PGPKeyIterNextUserID( iterator, &curUserID );
				while( IsntPGPError( err ) )
				{
					PGPRecipientUser	*user;
					
					err = NewUser( recipients, &user );
					if( IsntPGPError( err ) )
					{
						PGPSize		bufferSize;
						char		tempName[ 256 ];

						user->kind				= kPGPRecipientUserKind_Key;
						user->userInfo.key 		= theKey;
						user->userInfo.userID	= curUserID;
						
						if( theKey->canEncrypt )
						{
							user->location =
									kPGPRecipientUserLocation_UserList;
						}
						else
						{
							user->location =
									kPGPRecipientUserLocation_Hidden;
						}
						
						if( curUserID == primaryUserID )
							user->userInfo.isPrimaryUser = TRUE;
						
						tempName[0] = 0;
						
						err = PGPGetUserIDStringBuffer( curUserID,
									kPGPUserIDPropName, sizeof( tempName ),
									tempName, &bufferSize );
						if( IsntPGPError( err ) ||
							err == kPGPError_BufferTooSmall )
						{
							err = RememberName( recipients, tempName, 
										&user->nameOffset );
						}
						
						if( IsntPGPError( err ) )
						{
							// Link the user to the key
							user->nextUser 	= theKey->users;
							theKey->users 	= user;
							
							if( UserIsVisible( user ) )
							{
								theKey->isVisible = TRUE;
								
								if( user->userInfo.isPrimaryUser )
								{
									theKey->primaryUser = user;
								}
								else if( IsNull( theKey->primaryUser ))
								{
									theKey->primaryUser = user;
								}
							}
						}
						
						err = PGPKeyIterNextUserID( iterator, &curUserID );
					}
				}
			
				if( err == kPGPError_EndOfIteration )
					err = kPGPError_NoErr;
			}
		}
		
		if( IsntPGPError( err ) )
		{
			pgpAssert( IsntNull( theKey->users ) );
			
			// Set the primary user to the first user in the list if
			// no explicit or visible primary user was found.
			
			if( IsNull( theKey->primaryUser ) )
				theKey->primaryUser = theKey->users;
				
			// Finally, link key into the list
			theKey->nextKey		= recipients->keys;
			recipients->keys	= theKey;
		}
		else
		{	
			FreeKey( theKey );
			theKey = NULL;
		}
	}
	
	*recipientKey = theKey;
	
	return( err );
}

	static PGPError
UpdateDynamicRecipientValues(PGPRecipientsList *recipients)
{
	PGPRecipientKey		*curKey;
	PGPRecipientUser	*curUser;
	PGPError			err = kPGPError_NoErr;
	
	curKey = recipients->keys;
	while( IsntNull( curKey ) && IsntPGPError( err ) )
	{
		curUser = curKey->users;
		while( IsntNull( curUser ) )
		{
			PGPInt32	userValidity;
							
			if( IsntPGPError( PGPGetUserIDNumber( curUser->userInfo.userID,
							kPGPUserIDPropValidity, &userValidity ) ) )
			{
				curUser->validity = (PGPValidity) userValidity;
			}
			else
			{
				curUser->validity = kPGPValidity_Invalid;
			}
								
			curUser = curUser->nextUser;
		}
		
		if( recipients->arrEnforcement != kPGPARREnforcement_None )
		{
			err = FindARRKeys( recipients, curKey );
			if( IsPGPError( err ) )
				break;
		}
		
		curKey = curKey->nextKey;
	}

	curUser = recipients->groups;
	while( IsntNull( curUser ) && IsntPGPError( err ) )
	{

⌨️ 快捷键说明

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