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

📄 pgprecipientdialogcommon.cpp

📁 vc环境下的pgp源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		PGPUInt32	groupItemIndex;
		
		curUser->validity 					= kPGPValidity_Complete;
		curUser->groupInfo.numMissingKeys	= 0;
		curUser->groupInfo.haveMissingARRs 	= FALSE;
		curUser->groupInfo.numARRKeys		= 0;
		
		for( groupItemIndex = 0; groupItemIndex < curUser->groupInfo.numKeys;
					++groupItemIndex )
		{
			PGPGroupItem	item;
			
			err = PGPGetIndGroupItem( recipients->groupSet,
						curUser->groupInfo.groupID, groupItemIndex, &item );
			if( IsntPGPError( err ) )
			{
				PGPRecipientKey	*key = NULL;

				if( item.userValue == NULL )
				{
					PGPKeyRef	theKey;
					
					if( IsntPGPError( PGPGetKeyByKeyID( recipients->keySet,
										&item.u.key.keyID,
										item.u.key.algorithm, &theKey ) ) )
					{
						err = PGPGetKeyUserVal( theKey,
									(PGPUserValue *) &key );
					}
					
					if( IsntPGPError( err ) && IsntNull( key ) )
					{
						err = PGPSetIndGroupItemUserValue( recipients->groupSet,
									curUser->groupInfo.groupID, groupItemIndex,
									(PGPUserValue) key );
					}
				}
				else
				{
					key = (PGPRecipientKey *) item.userValue;
				}
				
				if( IsntNull( key ) )
				{
					if( key->primaryUser->validity < curUser->validity )
						curUser->validity = key->primaryUser->validity;
						
					if( key->haveMissingARRs )
						curUser->groupInfo.haveMissingARRs = TRUE;
						
					curUser->groupInfo.numARRKeys += key->numARRKeys;
				}
				else
				{
					++curUser->groupInfo.numMissingKeys;
				}
			}
			
			if( IsPGPError( err ) )
				break;
		}
	
		curUser = curUser->nextUser;
	}
	
	return( err );
}

	static PGPError
UpdateNewKeys(
	PGPRecipientsList	*recipients,
	PGPBoolean			markAsNew)
{
	PGPError		err;
	PGPKeyListRef	keyList;

	err = PGPOrderKeySet( recipients->keySet, kPGPAnyOrdering, &keyList );
	if( IsntPGPError( err ) )
	{
		PGPKeyIterRef	iterator;
		PGPKeyRef		defaultKey = kInvalidPGPKeyRef;
		
		(void) PGPGetDefaultPrivateKey( recipients->keySet, &defaultKey );
		
		err = PGPNewKeyIter( keyList, &iterator );
		if( IsntPGPError( err ) )
		{
			PGPKeyRef	theKey;
			
			err = PGPKeyIterNext( iterator, &theKey );
			while( IsntPGPError( err ) )
			{
				PGPRecipientKey	*recipientKey;

				err = PGPGetKeyUserVal( theKey,
								(PGPUserValue *) &recipientKey );
				if( IsntPGPError( err ) && IsNull( recipientKey ) )
				{
					PGPBoolean	isDefault = ( theKey == defaultKey );
				
					err = AddKey( recipients, iterator, theKey, isDefault,
								markAsNew, &recipientKey );
					if( IsntPGPError( err ) )
					{
						err = PGPSetKeyUserVal( theKey, recipientKey );
					}
				}
				
				if( IsntPGPError( err ) )
				{
					err = PGPKeyIterNext( iterator, &theKey );
				}
			}
			
			if( err == kPGPError_EndOfIteration )
				err = kPGPError_NoErr;
				
			PGPFreeKeyIter( iterator );
		}
		
		PGPFreeKeyList( keyList );
	}
	
	return( err );
}

	static PGPError
AddKeySet(
	PGPRecipientsList	*recipients,
	PGPKeySetRef		keySet)
{
	pgpAssert( ! PGPKeySetRefIsValid( recipients->keySet ) );
	
	recipients->keySet = keySet;
	
	return( UpdateNewKeys( recipients, FALSE ) );
}

	static PGPError
AddNewKeys(
	PGPRecipientsList	*recipients,
	PGPKeySetRef		keySet)
{
	PGPError	err;
	
	err = PGPAddKeys( keySet, recipients->keySet );
	if( IsntPGPError( err ) )
	{
		err = PGPCheckKeyRingSigs( keySet, recipients->keySet,
				FALSE, NULL, 0 );
		if( IsntPGPError( err ) )
		{
			err = PGPPropagateTrust( recipients->keySet );
			if( IsntPGPError( err ) )
			{
				err = PGPCommitKeyRingChanges( recipients->keySet );
				if( IsntPGPError( err ) )
				{
					err = UpdateNewKeys( recipients, TRUE );
					if( IsntPGPError( err ) )
					{
						err = UpdateDynamicRecipientValues( recipients );
					}
				}
			}
		}
	}
	
	return( err );
}

	static PGPError
AddGroup(
	PGPRecipientsList	*recipients,
	PGPGroupSetRef		groupSet,
	PGPGroupID			groupID)
{
	PGPError			err;
	PGPRecipientUser	*user;
	
	err = NewUser( recipients, &user );
	if( IsntPGPError( err ) )
	{
		user->kind = kPGPRecipientUserKind_Group;
		
		err = PGPNewFlattenedGroupFromGroup( groupSet, groupID,
					recipients->groupSet, &user->groupInfo.groupID );
		if( IsntPGPError( err ) )
		{
			err = PGPCountGroupItems( recipients->groupSet,
						user->groupInfo.groupID, TRUE,
						&user->groupInfo.numKeys, NULL );
			if( IsntPGPError( err ) && user->groupInfo.numKeys > 0 )
			{
				PGPGroupInfo	info;
			
				err	= PGPGetGroupInfo( groupSet, groupID, &info );
				if( IsntPGPError( err ) )
				{
					err = RememberName( recipients, info.name,
								&user->nameOffset );
				}
			}
		}
		
		if( IsntPGPError( err ) )
		{
			if( user->groupInfo.numKeys > 0 )
			{
				user->location = kPGPRecipientUserLocation_UserList;
			}
			else
			{
				user->location = kPGPRecipientUserLocation_Hidden;
			}
			
			user->nextUser 		= recipients->groups;
			recipients->groups 	= user;
		}
	}
							
	return( err );
}

	static PGPError
AddGroupSet(
	PGPContextRef		context,
	PGPRecipientsList	*recipients,
	PGPGroupSetRef		groupSet)
{
	PGPUInt32	numGroups;
	PGPError	err;
	
	err = PGPNewGroupSet( context, &recipients->groupSet );
	if( IsntPGPError( err ) )
	{
		err	= PGPCountGroupsInSet( groupSet, &numGroups );
		if( IsntPGPError( err ) )
		{
			PGPUInt32	index;
			
			for( index = 0; index < numGroups; ++index )
			{
				PGPGroupID	groupID;
				
				err	= PGPGetIndGroupID( groupSet, index, &groupID );
				if ( IsntPGPError( err ) )
				{
					err = AddGroup( recipients, groupSet, groupID );
				}
				
				if ( IsPGPError( err ) )
					break;
			}
		}
	}
	
	return( err );
}

	static PGPRecipientKey *
FindKeyFromKeyRef(
	const PGPRecipientsList		*recipients,
	PGPKeyRef					searchKeyRef)
{
	PGPRecipientKey	*curKey;

	curKey = recipients->keys;
	while( IsntNull( curKey ) )
	{
		if( PGPCompareKeys( curKey->keyRef, searchKeyRef,
					kPGPKeyIDOrdering ) == 0 )
			break;
		
		curKey = curKey->nextKey;
	}

	return( curKey );
}

/* Must use PGPFreeData to dispose of foundUsers */

	static PGPError
FindUsersFromUserID(
	PGPRecipientsList	*recipients,
	const char			*userID,
	PGPBoolean			matchBestSecretKey,
	PGPUInt32			*numFoundUsers,
	PGPRecipientUser	***foundUsers)
{
	PGPError			err = kPGPError_NoErr;
	PGPRecipientKey		*curKey;
	PGPRecipientKey		*bestSecretKey;
	PGPRecipientUser	*curUser;
	PGPUInt32			markValue;
	
	*numFoundUsers 	= 0;
	*foundUsers		= NULL;
	bestSecretKey	= NULL;
	markValue		= GetNextMarkValue();
	
	/* Tabulate and mark matches first */
	curKey = recipients->keys;
	while( IsntNull( curKey ) )
	{
		PGPRecipientUser	*foundUser = NULL;

		curUser = curKey->users;
		while( IsntNull( curUser ) )
		{
			if( UserIsVisible( curUser ) && 
				IsntNull( FindSubstring( PGPGetRecipientUserNamePtr( curUser ),
								userID ) ) )
			{
				if( curUser->userInfo.isPrimaryUser)
				{
					foundUser = curUser;
					break;
				}
				else if( IsNull( foundUser ) )
				{
					foundUser = curUser;
				}
			}
		
			curUser = curUser->nextUser;
		}
		
		if( IsntNull( foundUser ) )
		{
			if( curKey->isSecretKey && matchBestSecretKey )
			{
				// The best secret key is (1) the default key or
				// (2) a DH/DSA key instead of anything else
				
				if( IsNull( bestSecretKey ) )
				{
					bestSecretKey = curKey;
				}
				else
				{	
					PGPBoolean	switchBestKey = FALSE;
					
					if( curKey->isDefaultKey )
					{
						pgpAssert( ! bestSecretKey->isDefaultKey );
						
						switchBestKey = TRUE;
					}
					else if( curKey->algorithm ==
									kPGPPublicKeyAlgorithm_DSA &&
							 bestSecretKey->algorithm !=
							 		kPGPPublicKeyAlgorithm_DSA )
					{
						switchBestKey = TRUE;
					}
					
					if( switchBestKey )
					{
						bestSecretKey->markValue 	= 0;
						*numFoundUsers 				-= 1;
						
						bestSecretKey = curKey;
					}
					else
					{
						// bestSecretKey is a better match. Skip this one.
						foundUser = NULL;
					}
				}
			}
		}
		
		if( IsntNull( foundUser ) )
		{
			foundUser->markValue	= markValue;
			curKey->markValue 		= markValue;
		
			*numFoundUsers += 1;
		}
		
		curKey = curKey->nextKey;
	}

	curUser = recipients->groups;
	while( IsntNull( curUser ) )
	{
		if( UserIsVisible( curUser ) )
		{
			char	groupUserID[256];
			
			/* Surround the group name with "<>" to simulate an email address */
			
			strcpy( groupUserID, "<" );
			strcat( groupUserID, PGPGetRecipientUserNamePtr( curUser ) );
			strcat( groupUserID, ">" );
			
			if( IsntNull( FindSubstring( groupUserID, userID ) ) )
			{
				curUser->markValue = markValue;
				*numFoundUsers += 1;
			}	
		}
	
		curUser = curUser->nextUser;
	}
	
	*foundUsers = (PGPRecipientUser **) PGPNewData( recipients->memoryMgr,
								*numFoundUsers * sizeof( **foundUsers ),
								kPGPMemoryMgrFlags_Clear );
	if( IsntNull( *foundUsers ) )
	{
		PGPUInt32	userIndex = 0;
		
		curKey = recipients->keys;
		while( IsntNull( curKey ) )
		{
			if( curKey->markValue == markValue )
			{
				curUser = curKey->users;
				while( IsntNull( curUser ) )
				{
					if( curUser->markValue == markValue )
					{
						(*foundUsers)[userIndex] = curUser;
						++userIndex;
					}
				
					curUser = curUser->nextUser;
				}
			}
			
			curKey = curKey->nextKey;
		}

		curUser = recipients->groups;
		while( IsntNull( curUser ) )
		{
			if( curUser->markValue == markValue )
			{
				(*foundUsers)[userIndex] = curUser;
				++userIndex;
			}
		
			curUser = curUser->nextUser;
		}
	}
	else
	{
		err = kPGPError_OutOfMemory;
	}
	
	return( err );
}

	static PGPError
FindUserFromKeyID(
	PGPRecipientsList		*recipients,
	const PGPKeyID			*searchKeyID,
	PGPPublicKeyAlgorithm	searchAlgorithm,
	PGPRecipientUser		**foundUser)
{
	PGPError			err = kPGPError_NoErr;
	PGPRecipientKey		*curKey;
	
	*foundUser = NULL;
	
	curKey = recipients->keys;
	while( IsntNull( curKey ) && IsntPGPError( err ) )
	{
		PGPKeyID	keyID;
		
		err = PGPGetKeyIDFromKey( curKey->keyRef, &keyID );
		if( IsntPGPError( err ) &&
			PGPCompareKeyIDs( &keyID, searchKeyID ) == 0 &&
			curKey->algorithm == searchAlgorithm )
		{
			if( UserIsVisible( curKey->primaryUser ) )
			{
				*foundUser = curKey->primaryUser;
			}
			
			break;
		}
		
		curKey = curKey->nextKey;
	}
	
	return( err );
}

	static PGPError
AddMissingRecipient(
	PGPRecipientsList		*recipients,
	const PGPRecipientSpec	*spec)	// Assumed client allocated
{
	PGPRecipientUser	*user;
	PGPError			err;
	
	err = NewUser( recipients, &user );
	if( IsntPGPError( err ) )
	{
		char	userIDString[256];
		
		user->kind 				= kPGPRecipientUserKind_MissingRecipient;
		user->location 			= kPGPRecipientUserLocation_RecipientList;
		user->missingUser.type 	= spec->type;
		
		if( spec->locked )
			++user->lockRefCount;
		
		switch( spec->type )
		{
			case kPGPRecipientSpecType_UserID:
				strcpy( userIDString, spec->u.userIDStr );
				break;
			
			case kPGPRecipientSpecType_KeyID:
				user->missingUser.keyID		= &spec->u.id.keyID;
				user->missingUser.algorithm	= spec->u.id.algorithm;
				
				err = pgpGetMissingRecipientKeyIDStringPlatform(
							recipients->context, &spec->u.id.keyID,
							userIDString );
				break;
			
			default:
				pgpDebugMsg( "Unknown recipient type" );
				break;
		}

		if( IsntPGPError( err ) )
		{
			err = RememberName( recipients, userIDString, &user->nameOffset );
			if( IsntPGPError( err ) )
			{
				user->nextUser = recipients->missingRecipients;
				recipients->missingRecipients = user;
			}
		}
	}
	
	// Don't need to free user upon error because it is in a larger,
	// allocated block which is freed as a single item
	
	return( err );
}

	static PGPError
MoveDefaultRecipients(
	PGPRecipientsList		*recipients,
	PGPUInt32				numDefaultRecipients,
	const PGPRecipientSpec	defaultRecipients[],
	PGPBoolean				*movedARRs)
{
	PGPError	err = kPGPError_NoErr;
	PGPUInt32	recipientIndex;
	
	*movedARRs = FALSE;
	
	for( recipientIndex = 0; recipientIndex < numDefaultRecipients;
				recipientIndex++ )
	{
		const PGPRecipientSpec	*curRecipient;
		PGPBoolean				movedAnARR = FALSE;
		
		curRecipient = &defaultRecipients[recipientIndex];
		
		switch( curRecipient->type )
		{
			case kPGPRecipientSpecType_Key:
			case kPGPRecipientSpecType_KeyID:
			{
				PGPKeyRef		theKey = kInvalidPGPKeyRef;
				
				if( curRecipient->type == kPGPRecipientSpecType_Key )
				{
					theKey = curRecipient->u.key;
				}
				else
				{
					(void) PGPGetKeyByKeyID( recipients->keySet,
								&curRecipient->u.id.keyID,
								curRecipient->u.id.algorithm, &theKey );
				}
				
				if( PGPKeyRefIsValid( theKey ) )
				{
					PGPRecipientKey	*key;

					key = FindKeyFromKeyRef( recipients, theKey );
					if( IsntNull( key ) && key->isVisible &&
						IsntNull( key->primaryUser ) )
					{
						PGPUInt32	numMovedUsers;
						
						err = PGPMoveRecipients( recipients,
									kPGPRecipientUserLocation_RecipientList,
									1, &key->primaryUser, &numMovedUsers,
									&movedAnARR );
						

⌨️ 快捷键说明

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