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

📄 pgprecipientdialogcommon.cpp

📁 可以实现对邮件的加密解密以及签名
💻 CPP
📖 第 1 页 / 共 5 页
字号:
				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 )
			{
				PGPRecipientUser	*curUser;

				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 = PGPGetKeyID( 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->flags & kPGPRecipientSpecFlags_Locked ) != 0 )
			++user->lockRefCount;
		
		switch( spec->type )
		{
			case kPGPRecipientSpecType_UserID:
				strcpy( userIDString, spec->u.userIDStr );
				break;
			
			case kPGPRecipientSpecType_KeyID:
				user->missingUser.keyID = &spec->u.keyID;

#if PGP_UNIX
				err = kPGPError_UnknownError;
#else
				err = pgpGetMissingRecipientKeyIDStringPlatform(
							recipients->context, &spec->u.keyID,
							userIDString );
#endif
				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,
	void					*hwndParent,
	PGPBoolean				syncUnknownKeys)
{
	PGPError	err = kPGPError_NoErr;
	PGPUInt32	recipientIndex;
	PGPKeyDBRef	keyDB = PGPPeekKeySetKeyDB( recipients->keySet );
	
	*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:
			{
				PGPKeyDBObjRef		theKey = kInvalidPGPKeyDBObjRef;
				
				if( curRecipient->type == kPGPRecipientSpecType_Key )
				{
					(void) pgpFindKeyInKeyDB( curRecipient->u.key,
								keyDB, &theKey );
				}
				else
				{
					(void) PGPFindKeyByKeyID( keyDB,
								&curRecipient->u.keyID, &theKey );
				}
				
				if( PGPKeyDBObjRefIsValid( theKey ) )
				{
					PGPRecipientKey	*key;

					key = FindKeyFromKeyRef( recipients, theKey );
					if( IsntNull( key ) && key->isVisible &&
						IsntNull( key->primaryUser ) )
					{
						PGPUInt32	numMovedUsers;
						
						if(syncUnknownKeys)
						{
							// See if there are any ADKs that
							// can be found
							PGPMoveRecipientsARRPrefetch(
								hwndParent,
								recipients,
								1,
								&key->primaryUser);
						}

						err = PGPMoveRecipients( recipients,
									kPGPRecipientUserLocation_RecipientList,
									1, &key->primaryUser, &numMovedUsers,
									&movedAnARR );
						
						if( ( curRecipient->flags & kPGPRecipientSpecFlags_Locked ) != 0 )
							++key->primaryUser->lockRefCount;
					}
				}
				else
				{
					err = AddMissingRecipient( recipients, curRecipient );
				}
				
				break;
			}
			
			case kPGPRecipientSpecType_UserID:
			{
				PGPUInt32			numMatchedUsers;
				PGPRecipientUser	**matchedUserList;
				
				err = FindUsersFromUserID( recipients,
							curRecipient->u.userIDStr, TRUE, &numMatchedUsers,
							&matchedUserList );
				if( IsntPGPError( err ) )
				{
					if( numMatchedUsers != 0 )
					{
						PGPUInt32	numMovedUsers;
					
						if(syncUnknownKeys)
						{
							// See if there are any ADKs that
							// can be found
							PGPMoveRecipientsARRPrefetch(
								hwndParent,
								recipients,
								numMatchedUsers,
								matchedUserList);
						}

						err = PGPMoveRecipients( recipients,
								kPGPRecipientUserLocation_RecipientList,
								numMatchedUsers, matchedUserList,
								&numMovedUsers, &movedAnARR );
					
						if( numMatchedUsers == 1 )
						{
							if( ( curRecipient->flags & kPGPRecipientSpecFlags_Locked ) != 0 )
								++matchedUserList[0]->lockRefCount;
						}
						else
						{
							PGPUInt32	userIndex;
							
							for( userIndex = 0; userIndex < numMatchedUsers;
									++userIndex )
							{
								matchedUserList[userIndex]->multipleMatch =
										TRUE;
							}
						}
					}
					else
					{
						err = AddMissingRecipient( recipients, curRecipient );
					}
					
					PGPFreeData( matchedUserList );
				}
				
				break;
			}
			
			default:
				pgpDebugMsg(
					"MoveDefaultRecipients(): Unknown PGPRecipientSpecType" );
				err = kPGPError_BadParams;
				break;
		}
		
		if( IsPGPError( err ) )
			break;
			
		if( movedAnARR )
			*movedARRs = TRUE;
	}

	return( err );
}
	
	const char *
PGPGetRecipientUserNamePtr(
	const PGPRecipientUser *user)
{
	pgpAssert( IsntNull( user ) );
	
	return( &user->recipients->nameList[user->nameOffset] );
}

	void
PGPGetRecipientUserName(
	const PGPRecipientUser 	*user,
	char					name[256])
{
	pgpAssert( IsntNull( user ) );
	pgpAssert( IsntNull( name ) );
	
	strcpy( name, PGPGetRecipientUserNamePtr( user ) );
}

	PGPError
PGPBuildRecipientsList(
	void *							hwndParent,
	PGPContextRef					context,
	PGPKeySetRef					allKeys,
	PGPGroupSetRef					groupSet,
	PGPUInt32						numDefaultRecipients,
	const PGPRecipientSpec			*defaultRecipients,
	PGPUInt32						serverCount,
	const PGPKeyServerSpec			*serverList,
	PGPtlsContextRef				tlsContext,
	PGPKeyDBObjRef					defaultKey,
	PGPBoolean						syncUnknownKeys,
	PGPAdditionalRecipientRequestEnforcement	arrEnforcement,
	PGPRecipientEventHandlerProcPtr	eventHandler,
	PGPUserValue					eventUserValue,
	PGPRecipientsList				*recipients,
	PGPBoolean						*haveDefaultARRs)
{
	PGPError	err = kPGPError_NoErr;
	
	pgpAssert( PGPContextRefIsValid( context ) );
	pgpAssert( PGPKeySetRefIsValid( allKeys ) );
	pgpAssert( IsntNull( recipients ) );

	pgpClearMemory( recipients, sizeof( *recipients ) );

	recipients->context				= context;
	recipients->memoryMgr			= PGPPeekContextMemoryMgr( context );
	recipients->arrEnforcement		= arrEnforcement;
	recipients->eventHandler		= eventHandler;
	recipients->eventUserValue		= eventUserValue;
	recipients->serverList			= serverList;
	recipients->serverCount			= serverCount;
	recipients->tlsContext			= tlsContext;
	recipients->defaultKey			= defaultKey;
	
	*haveDefaultARRs = FALSE;
	
	err = AddKeySet( recipients, allKeys );
	if( IsntPGPError( err ) && PGPGroupSetRefIsValid( groupSet ) )
	{
		err = AddGroupSet( context, recipients, groupSet );
	}
	
	if( IsntPGPError( err ) )
	{
		err = UpdateDynamicRecipientValues( recipients );
	}

	if( IsntPGPError( err ) && numDefaultRecipients != 0 )
	{
		err = MoveDefaultRecipients( recipients, numDefaultRecipients,
						defaultRecipients, haveDefaultARRs, 
						hwndParent,syncUnknownKeys);
	}

	if( IsntPGPError( err ) && syncUnknownKeys )
	{
		PGPBoolean	haveNewKeys;
		
		PGPUpdateMissingRecipients( hwndParent, recipients,
					&haveNewKeys );
	}

	return( err );
}

	void
PGPDisposeRecipientsList(PGPRecipientsList *recipients)
{
	PGPRecipientKeyList		*curKeyList;
	PGPRecipientUserList	*curUserList;
	PGPRecipientKey			*curKey;
	
	pgpAssert( IsntNull( recipients ) );
	pgpAssert( PGPContextRefIsValid( recipients->context ) );
	pgpAssert( PGPMemoryMgrRefIsValid( recipients->memoryMgr ) );
	
	curKey = recipients->keys;
	while( IsntNull( curKey ) )
	{
		FreeKey( curKey );
		
		curKey = curKey->nextKey;
	}
	
	curKeyList = recipients->keyLists;
	while( IsntNull( curKeyList ) )
	{
		PGPRecipientKeyList	*nextKeyList;
	
		nextKeyList = curKeyList->nextKeyList;
			PGPFreeData( curKeyList );
		curKeyList = nextKeyList;
	}

	curUserList = recipients->userLists;
	while( IsntNull( curUserList ) )
	{
		PGPRecipientUserList	*nextUserList;
	
		nextUserList = curUserList->nextUserList;
			PGPFreeData( curUserList );
		curUserList = nextUserList;
	}
	
	if( IsntNull( recipients->nameList ) )
		PGPFreeData( recipients->nameList );
	
	if( PGPGroupSetRefIsValid( recipients->groupSet ) )
		PGPFreeGroupSet( recipients->groupSet );
		
	pgpClearMemory( recipients, sizeof( *recipients ) );
}

	static PGPError
SendUserEvent(
	PGPRecipientsList		*recipients,
	PGPRecipientEventType	eventType,
	PGPRecipientUser		*user)
{
	PGPError	err = kPGPError_NoErr;
	
	if( IsntNull( recipients->eventHandler ) )
	{
		PGPRecipientEvent	event;
		
		pgpClearMemory( &event, sizeof( event ) );
		
		event.type  = eventType;
		event.user 	= user;
	
		err = (*recipients->eventHandler)( recipients, &event,
						recipients->eventUserValue ); 
	}

	return( err );
}

	static PGPBoolean
MarkKeyARRsForUserToRecipientMove(
	PGPRecipientsList	*recipients,
	PGPRecipientKey		*key,
	PGPUInt32			markValue,
	PGPBoolean			*markedARRs,
	PGPBoolean			*missingARRs)
{
	PGPBoolean		moveUser;
	
	pgpAssert( recipients->arrEnforcement != kPGPARREnforcement_None );

	moveUser 		= TRUE;
	*missingARRs 	= FALSE;
	*markedARRs		= FALSE;
	
	if( key->numARRKeys != 0 )
	{
		PGPBoolean	markARRs = TRUE;
		
		if( key->haveMissingARRs )
		{
			*missingARRs = TRUE;
			
			/*
			** In the strict case, the user cannot be moved because an ARR
			** was not found. In the warn case, we move what we can.
			*/
			
			if( recipients->arrEnforcement == kPGPARREnforcement_Strict )
			{
				moveUser = FALSE;
				markARRs = FALSE;
			}
		}
		
		if( markARRs )
		{
			PGPUInt32	arrIndex;
			
			for( arrIndex = 0; arrIndex < key->numARRKeys; arrIndex++ )
			{
				PGPRecipientKeyARRInfo	*arrInfo;
				
				arrInfo = &key->arrKeys[arrIndex];
				if( IsntNull( arrInfo->key ) )
				{
					PGPRecipientUser	*user = arrInfo->key->primaryUser;
					
					pgpAssert( arrInfo->key->isVisible );
					
					++user->userInfo.arrRefCount;
					user->markValue = markValue;

					*markedARRs = TRUE;
					
					if( IsMandatoryARRClass( arrInfo->arrClass ) )
					{
						++user->userInfo.enforcedARRRefCount;
						
						if( recipients->arrEnforcement ==
								kPGPARREnforcement_Strict)
						{
							++user->lockRefCount;
						}
					}
				}
			}
		}
	}
	
	return( moveUser );
}

	static void
MarkKeyARRsForRecipientToUserMove(
	PGPRecipientsList	*recipients,
	PGPRecipientKey		*key,
	PGPUInt32			markValue)
{
	(void) recipients;

	pgpAssert( recipients->arrEnforcement != kPGPARREnforcement_None );

	if( key->numARRKeys != 0 )
	{

⌨️ 快捷键说明

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