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

📄 pgpdialogs.cpp

📁 vc环境下的pgp源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		if( IsntPGPError( err ) )
		{
			if( numSearchOptions == 0 )
			{
				pgpDebugMsg( "No key server subjects found" );
				err = kPGPError_BadParams;
			}
			else
			{
				pgpAssert( PGPFilterRefIsValid( filter ) );
					
				mFilter = filter;
			
				if( numSearchOptions == 1 && keyDescription[0] != 0 )
					strcpy( mKeyDescription, keyDescription );
			}
		}
		else
		{
			if( PGPFilterRefIsValid( filter ) )
				PGPFreeFilter( filter );
		}
	}

	return( err );
}

CPGPSendToKeyServerDialogOptions::CPGPSendToKeyServerDialogOptions(void)
{
	mKeysToSend	= kInvalidPGPKeySetRef;
	mFailedKeys	= NULL;
}

CPGPSendToKeyServerDialogOptions::~CPGPSendToKeyServerDialogOptions(void)
{
}

CPGPPassphraseDialogOptions::CPGPPassphraseDialogOptions(void)
{
	mPassphrasePtr			= NULL;
	mDialogOptions			= kInvalidPGPOptionListRef;
	mMinPassphraseLength	= 0;
	mMinPassphraseQuality	= 0;
}

CPGPPassphraseDialogOptions::~CPGPPassphraseDialogOptions(void)
{
}

	PGPError
CPGPPassphraseDialogOptions::GatherOptions(
	PGPContextRef 		context,
	PGPOptionListRef	optionList)
{
	PGPError	err = kPGPError_NoErr;
	
	PGPValidateContext( context );
	PGPValidateParam( pgpOptionListIsValid( optionList ) );

	err = CPGPDialogOptions::GatherOptions( context, optionList );
	if( IsntPGPError( err ) )
	{
		err = pgpFindOptionArgs( optionList,
						 kPGPOptionType_OutputPassphrase, TRUE,
						 "%p", &mPassphrasePtr );
	}

	if( IsntPGPError( err ) )
	{
		PGPOption	optionData;
		
		err = pgpSearchOptionSingle( optionList,
					kPGPOptionType_DialogOptions, &optionData );
		if( IsntPGPError( err ) &&
			optionData.type == kPGPOptionType_DialogOptions &&
			PGPOptionListRefIsValid( optionData.subOptions ) )
		{
			mDialogOptions = optionData.subOptions;
		}
	}

	if( IsntPGPError( err ) )
	{
		err = pgpFindOptionArgs( optionList,
						 kPGPOptionType_MinPassphraseLength, FALSE,
						 "%d", &mMinPassphraseLength );
	}

	if( IsntPGPError( err ) )
	{
		err = pgpFindOptionArgs( optionList,
						 kPGPOptionType_MinPassphraseQuality, FALSE,
						 "%d", &mMinPassphraseQuality );
	}

	return( err );
}

CPGPConfirmationPassphraseDialogOptions::
	CPGPConfirmationPassphraseDialogOptions(void)
{
	mShowPassphraseQuality = TRUE;
}

CPGPConfirmationPassphraseDialogOptions::
	~CPGPConfirmationPassphraseDialogOptions(void)
{
}

	PGPError
CPGPConfirmationPassphraseDialogOptions::GatherOptions(
	PGPContextRef 		context,
	PGPOptionListRef	optionList)
{
	PGPError	err;
	
	PGPValidateContext( context );
	PGPValidateParam( pgpOptionListIsValid( optionList ) );
	
	err = CPGPPassphraseDialogOptions::GatherOptions( context, optionList );
	if( IsntPGPError( err ) )
	{
		PGPBoolean	haveOption;
		PGPUInt32	showPassphraseQuality;

		err = pgpFindOptionArgs( optionList,
						 kPGPOptionType_ShowPassphraseQuality, FALSE,
						 "%b%d", &haveOption, &showPassphraseQuality );
	
		if( haveOption )
		{	 
			mShowPassphraseQuality = ( showPassphraseQuality != 0 );
		}
	}
	
	return( err );
}

CPGPKeyPassphraseDialogOptions::CPGPKeyPassphraseDialogOptions(void)
{
	mVerifyPassphrase 	= TRUE;
	mDefaultKey			= kInvalidPGPKeyRef;
}

CPGPKeyPassphraseDialogOptions::~CPGPKeyPassphraseDialogOptions(void)
{
}

	PGPError
CPGPKeyPassphraseDialogOptions::GatherOptions(
	PGPContextRef 		context,
	PGPOptionListRef	optionList)
{
	PGPError	err;
	
	PGPValidateContext( context );
	PGPValidateParam( pgpOptionListIsValid( optionList ) );

	err = CPGPPassphraseDialogOptions::GatherOptions( context, optionList );
	if( IsntPGPError( err ) )
	{
		err = pgpFindOptionArgs( optionList,
						 kPGPOptionType_DefaultKey, FALSE,
						 "%p", &mDefaultKey );
	}

	if( IsntPGPError( err ) )
	{
		PGPBoolean	haveOption;
		PGPUInt32	verifyPassphrase;
		
		err = pgpFindOptionArgs( optionList,
						 kPGPOptionType_VerifyPassphrase, FALSE,
						 "%b%d", &haveOption, &verifyPassphrase );
		
		if( haveOption )
		{	 
			mVerifyPassphrase = ( verifyPassphrase != 0 );
		}
	}
		
	return( err );
}

CPGPKeySetPassphraseDialogOptions::CPGPKeySetPassphraseDialogOptions(void)
{
	mFindMatchingKey	= TRUE;
	mKeySet				= kInvalidPGPKeySetRef;
	mPassphraseKeyPtr	= NULL;
}

CPGPKeySetPassphraseDialogOptions::~CPGPKeySetPassphraseDialogOptions(void)
{
}

	PGPError
CPGPKeySetPassphraseDialogOptions::GatherOptions(
	PGPContextRef 		context,
	PGPOptionListRef	optionList)
{
	PGPError	err;
	
	PGPValidateContext( context );
	PGPValidateParam( pgpOptionListIsValid( optionList ) );

	err = CPGPKeyPassphraseDialogOptions::GatherOptions(context, optionList);
	if( IsntPGPError( err ) )
	{
		PGPBoolean	haveOption;
		PGPUInt32	findMatchingKey;
		
		err = pgpFindOptionArgs( optionList,
						 kPGPOptionType_FindMatchingKey, FALSE,
						 "%b%d", &haveOption, &findMatchingKey );
		
		if( haveOption )
		{	 
			mFindMatchingKey = ( findMatchingKey != 0 );
		}
	}
		
	return( err );
}

CPGPSigningPassphraseDialogOptions::CPGPSigningPassphraseDialogOptions(void)
{
}

CPGPSigningPassphraseDialogOptions::~CPGPSigningPassphraseDialogOptions(void)
{
}

CPGPDecryptionPassphraseDialogOptions::
	CPGPDecryptionPassphraseDialogOptions(void)
{
	mMissingKeyIDList 	= NULL;
	mMissingKeyIDCount	= 0;
}

CPGPDecryptionPassphraseDialogOptions::
	~CPGPDecryptionPassphraseDialogOptions(void)
{
	if( IsntNull( mMissingKeyIDList ) )
		PGPFreeData( mMissingKeyIDList );
}

	PGPError
CPGPDecryptionPassphraseDialogOptions::RemoveFoundKeysFromSet(
	PGPKeySetRef keySet)
{
	PGPInt32	keyIDIndex;
	PGPError	err = kPGPError_NoErr;
	
	/*
	** Process keySet and remove any keys in the set from the missing
	** keys list. The following is ugly N-squared behavior. We need to
	** walk the key ID list and eliminate keys already in mKeySet, however
	** PGPGetKeyByKeyID requires an algorithm, which we do not have.
	** In addition, the key ID list contains subkey IDs, not the
	** signing key IDs. There is no lookup function for this case.
	** Thus, we walk the key id list and then iterate the key set
	** to see if we have the key. If not, the key ID is added to the
	** list of missing keys.
	*/

	for( keyIDIndex = mMissingKeyIDCount - 1; keyIDIndex >= 0; keyIDIndex-- )
	{
		PGPKeyListRef	keyList;
		PGPBoolean		foundKeyID = FALSE;
		
		err = PGPOrderKeySet( keySet, kPGPAnyOrdering, &keyList );
		if( IsntPGPError( err ) )
		{
			PGPKeyIterRef	iter;
			
			err = PGPNewKeyIter( keyList, &iter );
			if( IsntPGPError( err ) )
			{
				PGPKeyRef	theKey;
				
				err = PGPKeyIterNext( iter, &theKey );
				while( IsntPGPError( err ) )
				{
					PGPKeyID	keyID;
								
					err = PGPGetKeyIDFromKey( theKey, &keyID );
					if( IsntPGPError( err ) )
					{
						if( PGPCompareKeyIDs( &keyID,
									&mMissingKeyIDList[keyIDIndex] ) == 0 )
						{
							foundKeyID = TRUE;
						}
						else
						{
							PGPSubKeyRef	subKey;
							
							err = PGPKeyIterNextSubKey( iter, &subKey );
							while( IsntPGPError( err ) )
							{
								err = PGPGetKeyIDFromSubKey( subKey, &keyID );
								if( IsntPGPError( err ) )
								{
									if( PGPCompareKeyIDs( &keyID,
										&mMissingKeyIDList[keyIDIndex] ) == 0 )
									{
										foundKeyID = TRUE;
										break;
									}

									err = PGPKeyIterNextSubKey(iter, &subKey);
								}
							}

							if( err == kPGPError_EndOfIteration )
								err = kPGPError_NoErr;
						}
					}
						
					if( IsntPGPError( err ) )
						err = PGPKeyIterNext( iter, &theKey );
				}
				
				if( err == kPGPError_EndOfIteration )
					err = kPGPError_NoErr;
					
				PGPFreeKeyIter( iter );
			}
			
			PGPFreeKeyList( keyList );
		}
	
		if( IsntPGPError( err ) && foundKeyID )
		{
			PGPUInt32	keysToMove;
			
			keysToMove = mMissingKeyIDCount - keyIDIndex - 1;
			if( keysToMove != 0 )
			{
				pgpCopyMemory( &mMissingKeyIDList[keyIDIndex + 1],
						&mMissingKeyIDList[keyIDIndex],
						keysToMove * sizeof( mMissingKeyIDList[0] ) );
			}
			
			--mMissingKeyIDCount;
		}
	
		if( IsPGPError( err ) )
			break;
	}
	
	return( err );
}

	PGPError
CPGPDecryptionPassphraseDialogOptions::GatherMissingKeys()
{
	PGPError	err = kPGPError_NoErr;

	if( IsntNull( mMissingKeyIDList ) )
	{
		PGPFreeData( mMissingKeyIDList );
		
		mMissingKeyIDList 	= NULL;
		mMissingKeyIDCount	= 0;
	}

	if( IsntNull( mKeyIDList ) && mKeyIDCount != 0 )
	{
		PGPSize	dataSize = mKeyIDCount * sizeof( mKeyIDList[0] );
		
		/* Assume all keys are missing. Make a copy of the key ID list */
		
		mMissingKeyIDList = (PGPKeyID *) PGPNewData(
										PGPGetContextMemoryMgr( mContext ),
										dataSize, kPGPMemoryMgrFlags_Clear );
		if( IsntNull( mMissingKeyIDList ) )
		{
			pgpCopyMemory( mKeyIDList, mMissingKeyIDList, dataSize );
			mMissingKeyIDCount = mKeyIDCount;
			
			err = RemoveFoundKeysFromSet( mKeySet );
			if( IsntPGPError( err ) &&
				IsntNull( mNewKeys ) &&
				PGPKeySetRefIsValid( *mNewKeys ) )
			{
				err = RemoveFoundKeysFromSet( *mNewKeys );
			}
			
			if( IsPGPError( err ) || mMissingKeyIDCount == 0 )
			{
				PGPFreeData( mMissingKeyIDList );
				mMissingKeyIDList 	= NULL;
				mMissingKeyIDCount 	= 0;
			}
		}
		else
		{
			err = kPGPError_OutOfMemory;
		}
	}
	
	return( err );
}

	PGPError
CPGPDecryptionPassphraseDialogOptions::SearchForMissingKeys(
	void		*hwndParent,
	PGPBoolean	*foundNewKeys)
{
#if PGP_MACINTOSH
	(void) hwndParent;
#endif

	PGPError	err = kPGPError_NoErr;
	
	(void) hwndParent;
	
	pgpAssert( IsntNull( foundNewKeys ) );
	*foundNewKeys = FALSE;

	if( IsntNull( mServerList ) &&
		IsntNull( mMissingKeyIDList ) &&
		IsntNull( mNewKeys ) )
	{
		pgpAssert( ! PGPKeySetRefIsValid( *mNewKeys ) );
		
		err = PGPNewKeySet( mContext, mNewKeys );
		if( IsntPGPError( err ) )
		{
			PGPUInt32	serverIndex;
			PGPUInt32	numNewKeys = 0;
			
			for( serverIndex = 0; serverIndex < mServerCount; serverIndex++ )
			{
				if( IsntNull( mMissingKeyIDList ) )
				{
					PGPKeyServerType		serverType;
					const PGPKeyServerSpec	*serverSpec;
					
					serverSpec = &mServerList[serverIndex];
					
					err = PGPGetKeyServerType( serverSpec->server,
								&serverType );
					if( IsntPGPError( err ) &&
						serverType != kPGPKeyServerType_HTTP &&
						serverType != kPGPKeyServerType_HTTPS )
					{
						PGPFilterRef	filter = kInvalidPGPFilterRef;
						PGPUInt32		keyIndex;
						
						for( keyIndex = 0; keyIndex < mMissingKeyIDCount;
								keyIndex++ )
						{
							PGPFilterRef	curFilter;
							
							err = PGPNewSubKeyIDFilter( mContext,
										&mMissingKeyIDList[keyIndex],
										&curFilter );
							if( IsntPGPError( err ) )
							{
								if( PGPFilterRefIsValid( filter ) )
								{
									PGPFilterRef	newFilter;
									
									err = PGPUnionFilters( curFilter, filter,
												&newFilter );
									if( IsntPGPError( err ) )
									{
										filter = newFilter;
									}
								}
								else
								{
									filter = curFilter;
								}
							}
							
							if( IsPGPError( err ) )
								break;
						}
						
						if( IsntPGPError( err ) )
						{
							PGPKeySetRef	foundKeys = kInvalidPGPKeySetRef;
							
							err = PGPSearchKeyServerDialog( mContext, 1,
									serverSpec, mTLSContext, TRUE, &foundKeys,
									PGPOUIKeyServerSearchFilter( mContext,
										filter ),
			#if PGP_WIN32
									PGPOUIParentWindowHandle( mContext,
										(HWND) hwndParent),
			#endif
									PGPOLastOption( mContext ) );
							if( IsntPGPError( err ) &&
								PGPKeySetRefIsValid( foundKeys ) )
							{
								err = PGPAddKeys( foundKeys, *mNewKeys );
								if( IsntPGPError( err ) )
								{
									err = PGPCommitKeyRingChanges( *mNewKeys );
									if( IsntPGPError( err ) )
									{
										err = GatherMissingKeys();
									}
								}
								
								PGPFreeKeySet( foundKeys );
							}
						}
						
						if( PGPFilterRefIsValid( filter ) )
							PGPFreeFilter( filter );
					}
					
				} 
			
				if( IsPGPError( err ) )
					break;
			}
			
			(void) PGPCountKeys( *mNewKeys, &numNewKeys );
			if( numNewKeys != 0 )
			{
				*foundNewKeys = TRUE;
			}
			else
			{
				PGPFreeKeySet( *mNewKeys );
				*mNewKeys = kInvalidPGPKeySetRef;
			}
		}
	}
	
	return( err );
}

CPGPOptionsDialogOptions::CPGPOptionsDialogOptions(void)
{
}

CPGPOptionsDialogOptions::~CPGPOptionsDialogOptions(void)
{
}

static const PGPOptionType sPassphraseOptionSet[] =
{
	kCommonAllowedPassphraseOptions
};

	static PGPError
pgpPassphraseDialog(
	PGPContextRef		context,
	PGPOptionListRef 	optionList)
{
	PGPError	err;

	pgpAssert( pgpContextIsValid( context ) );
	pgpAssert( pgpOptionListIsValid( optionList ) );
	
	err = pgpGetOptionListError( optionList );
	if( IsntPGPError( err ) )
	{
		err = pgpCheckOptionsInSet( optionList, sPassphraseOptionSet,
					elemsof( sPassphraseOptionSet ) );
		if( IsntPGPError( err ) )
		{
			CPGPPassphraseDialogOptions	options;
			
			err = options.GatherOptions( context, optionList );
			if( IsntPGPError( err ) )
			{
				err = pgpPassphraseDialogPlatform( context, &options );
			}
		}
	}
	
	return( err );
}

	PGPError
PGPPassphraseDialog(
	PGPContextRef		context,
	PGPOptionListRef	firstOption,
	...)
{
	PGPError	err = kPGPError_NoErr;
	va_list		args;

	pgpAssert( pgpContextIsValid( context ) );
	
	if( pgpContextIsValid( context ) )
	{
		PGPOptionListRef	optionList;
		
		va_start( args, firstOption );

⌨️ 快捷键说明

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