📄 pgpdialogs.cpp
字号:
va_end( args );
err = kPGPError_BadParams;
}
return( err );
}
static const PGPOptionType sRecipientOptionSet[] =
{
kCommonAllowedOptions,
kPGPOptionType_DialogPrompt,
kPGPOptionType_DialogOptions,
kPGPOptionType_DefaultRecipients,
kPGPOptionType_DisplayMarginalValidity,
kPGPOptionType_IgnoreMarginalValidity,
kPGPOptionType_RecipientGroups,
kPGPOptionType_ARREnforcement,
kPGPOptionType_KeyServerUpdateParams,
kPGPOptionType_RecipientList
};
static PGPError
pgpRecipientDialog(
PGPContextRef context,
PGPKeySetRef allKeys,
PGPBoolean alwaysDisplay,
PGPOptionListRef optionList,
PGPKeyDBRef *recipientKeys)
{
PGPError err;
pgpAssert( pgpContextIsValid( context ) );
pgpAssert( pgpOptionListIsValid( optionList ) );
err = pgpGetOptionListError( optionList );
if( IsntPGPError( err ) )
{
err = pgpCheckOptionsInSet( optionList, sRecipientOptionSet,
elemsof( sRecipientOptionSet ) );
if( IsntPGPError( err ) )
{
CPGPRecipientDialogOptions options;
err = options.GatherOptions( context, optionList );
if( IsntPGPError( err ) )
{
PGPKeyDBRef workingKeyDB;
// Copy the set of known keys into an in-memory DB so we can
// make additions via key server searches
err = PGPNewKeyDB( context, &workingKeyDB );
if( IsntPGPError( err ) )
{
PGPKeySetRef workingKeySet;
err = PGPCopyKeys ( allKeys, workingKeyDB, &workingKeySet );
if( IsntPGPError( err ) )
{
PGPKeyDBRef allKeysDB = PGPPeekKeySetKeyDB( allKeys );
err = PGPCheckKeyRingSigs( workingKeySet, allKeysDB, FALSE,
NULL, 0 );
if( IsntPGPError( err ) )
{
err = PGPCalculateTrust( workingKeySet, allKeysDB );
if( IsntPGPError( err ) &&
PGPKeyDBObjRefIsValid( options.mDefaultKey ) )
{
err = pgpFindKeyInKeyDB( options.mDefaultKey,
workingKeyDB, &options.mDefaultKey );
}
}
}
if( IsntPGPError( err ) )
{
options.mClientKeySet = workingKeySet;
options.mAlwaysDisplay = alwaysDisplay;
options.mRecipientKeysPtr = recipientKeys;
#if HAVE_TEXT_UI
if (options.mTextUI)
{
err = pgpRecipientDialogTTY(context,&options);
}
else
#endif
{
err = pgpRecipientDialogPlatform(context,&options);
}
if( IsPGPError( err ) &&
IsntNull( options.mNewKeys ) &&
PGPKeyDBRefIsValid( *options.mNewKeys ) )
{
PGPFreeKeyDB( *options.mNewKeys );
*options.mNewKeys = kInvalidPGPKeyDBRef;
}
PGPFreeKeySet( workingKeySet );
}
PGPFreeKeyDB( workingKeyDB );
}
}
}
}
return( err );
}
PGPError
PGPRecipientDialog(
PGPContextRef context,
PGPKeyDBRef allKeys,
PGPBoolean alwaysDisplayDialog,
PGPKeyDBRef *recipientKeys,
PGPOptionListRef firstOption,
...)
{
PGPError err;
va_list args;
pgpAssert( pgpContextIsValid( context ) );
pgpAssert( PGPKeyDBRefIsValid( allKeys ) );
pgpAssert( IsntNull( recipientKeys ) );
if( pgpContextIsValid( context ) &&
PGPKeyDBRefIsValid( allKeys ) &&
IsntNull( recipientKeys ) )
{
PGPOptionListRef optionList;
PGPKeySetRef allKeysSet;
va_start( args, firstOption );
optionList = pgpBuildOptionListArgs( context, FALSE, firstOption,
args );
va_end( args );
err = PGPNewKeySet( allKeys, &allKeysSet );
if( IsntPGPError( err ) )
{
err = pgpRecipientDialog( context, allKeysSet,
alwaysDisplayDialog, optionList, recipientKeys );
PGPFreeKeySet( allKeysSet );
}
PGPFreeOptionList( optionList );
}
else
{
va_start( args, firstOption );
pgpFreeVarArgOptionList( firstOption, args );
va_end( args );
err = kPGPError_BadParams;
}
return( err );
}
static const PGPOptionType sRandomDataOptionSet[] =
{
kCommonAllowedOptions,
kPGPOptionType_DialogPrompt
};
static PGPError
pgpCollectRandomDataDialog(
PGPContextRef context,
PGPUInt32 neededEntropyBits,
PGPOptionListRef optionList)
{
PGPError err;
pgpAssert( pgpContextIsValid( context ) );
pgpAssert( pgpOptionListIsValid( optionList ) );
err = pgpGetOptionListError( optionList );
if( IsntPGPError( err ) )
{
err = pgpCheckOptionsInSet( optionList, sRandomDataOptionSet,
elemsof( sRandomDataOptionSet ) );
if( IsntPGPError( err ) )
{
CPGPRandomDataDialogOptions options;
err = options.GatherOptions( context, optionList );
if( IsntPGPError( err ) )
{
options.mNeededEntropyBits = neededEntropyBits;
err = pgpCollectRandomDataDialogPlatform( context, &options );
}
}
}
return( err );
}
PGPError
PGPCollectRandomDataDialog(
PGPContextRef context,
PGPUInt32 neededEntropyBits,
PGPOptionListRef firstOption,
... )
{
PGPError err;
va_list args;
pgpAssert( pgpContextIsValid( context ) );
if( pgpContextIsValid( context ) )
{
PGPOptionListRef optionList;
va_start( args, firstOption );
optionList = pgpBuildOptionListArgs( context, FALSE, firstOption,
args );
va_end( args );
err = pgpCollectRandomDataDialog( context, neededEntropyBits,
optionList );
PGPFreeOptionList( optionList );
}
else
{
va_start( args, firstOption );
pgpFreeVarArgOptionList( firstOption, args );
va_end( args );
err = kPGPError_BadParams;
}
return( err );
}
static const PGPOptionType sSearchKeyServerOptionSet[] =
{
kCommonAllowedKeyServerOptions,
kPGPOptionType_KeyServerSearchFilter,
kPGPOptionType_KeyServerSearchKeyIDList,
kPGPOptionType_KeyServerSearchKey,
kPGPOptionType_KeyServerSearchKeySet
};
#if PGP_WIN32 || PGP_OSX
PGPError
pgpCheckNetworklibAvailability(void)
{
// We're simply static linking for now.
return( kPGPError_NoErr );
}
#endif
static PGPError
pgpSearchKeyServerDialog(
PGPContextRef context,
const PGPKeyServerSpec serverList[],
PGPUInt32 serverCount,
PGPtlsContextRef tlsContext,
PGPBoolean searchAllServers,
PGPKeyDBRef *foundKeys,
PGPOptionListRef optionList)
{
PGPError err;
pgpAssert( pgpOptionListIsValid( optionList ) );
err = pgpGetOptionListError( optionList );
if( IsntPGPError( err ) )
{
err = pgpCheckNetworklibAvailability();
if( IsntPGPError( err ) )
{
err = pgpCheckOptionsInSet( optionList, sSearchKeyServerOptionSet,
elemsof( sSearchKeyServerOptionSet ) );
if( IsntPGPError( err ) )
{
CPGPSearchKeyServerDialogOptions options;
err = options.GatherOptions( context, optionList );
if( IsntPGPError( err ) )
{
options.mServerList = serverList;
options.mServerCount = serverCount;
options.mTLSContext = tlsContext;
options.mSearchAllServers = searchAllServers;
options.mNewKeys = foundKeys;
err = pgpSearchKeyServerDialogPlatform(context, &options);
}
}
}
}
return( err );
}
PGPError
PGPSearchKeyServerDialog(
PGPContextRef context,
PGPUInt32 serverCount,
const PGPKeyServerSpec serverList[],
PGPtlsContextRef tlsContext,
PGPBoolean searchAllServers,
PGPKeyDBRef *foundKeys,
PGPOptionListRef firstOption,
... )
{
PGPError err;
va_list args;
pgpAssert( pgpContextIsValid( context ) );
pgpAssert( IsntNull( serverList ) );
pgpAssert( serverCount >= 1 );
pgpAssert( IsntNull( foundKeys ) );
if( IsntNull( foundKeys ) )
*foundKeys = kInvalidPGPKeyDBRef;
if( pgpContextIsValid( context ) &&
IsntNull( serverList ) &&
serverCount >= 1 &&
IsntNull( foundKeys ) )
{
PGPOptionListRef optionList;
va_start( args, firstOption );
optionList = pgpBuildOptionListArgs( context, FALSE, firstOption,
args );
va_end( args );
err = pgpSearchKeyServerDialog( context, serverList, serverCount,
tlsContext, searchAllServers, foundKeys, optionList );
PGPFreeOptionList( optionList );
}
else
{
va_start( args, firstOption );
pgpFreeVarArgOptionList( firstOption, args );
va_end( args );
err = kPGPError_BadParams;
}
return( err );
}
#if PGP_WIN32
static void
sConvertUTF8ToNonUTF8Passphrase(
void* hwnd,
const char* passphrase,
PGPSize size,
char* passphraseNonUTF8 )
{
PGPUInt32 uLen = 0;
pgpUTF8StringToLocal( kPGPUnicodeFlag_Secure,
pgpGetCodePageFromWindow (hwnd),
passphrase, size,
passphraseNonUTF8, size+1, &uLen );
passphraseNonUTF8[uLen] = '\0';
}
#endif
PGPKeyDBObjRef
GetKeyForPassphrase(
PGPKeySetRef keySet,
char * passphrase,
#if PGP_WIN32
void * hwnd,
PGPBoolean* nonUTF8,
#endif
PGPBoolean signing)
{
PGPKeyDBObjRef theKey = kInvalidPGPKeyDBObjRef;
char* passphraseNonUTF8 = NULL;
PGPBoolean foundValidKey = FALSE;
PGPError err;
PGPKeyListRef keyListRef;
PGPBoolean onToken;
PGPUInt32 tryToken;
PGPContextRef context;
#if PGP_WIN32
PGPSize sizePassphrase;
#endif
context = PGPPeekKeySetContext( keySet );
#if PGP_WIN32
// later on we set nonUTF8 to TRUE if the non-UTF8 version
// of the passphrase is the one that matches
if (nonUTF8)
*nonUTF8 = FALSE;
// if we have an hwnd, we can determine the codepage,
// so make a non-UTF8 copy of the passphrase
if (hwnd)
{
sizePassphrase = strlen( passphrase );
passphraseNonUTF8 = (char*) PGPNewSecureData(
PGPPeekContextMemoryMgr( context ), sizePassphrase +1, 0 );
sConvertUTF8ToNonUTF8Passphrase( hwnd, passphrase,
sizePassphrase, passphraseNonUTF8 );
if (passphraseNonUTF8)
{
if ((passphraseNonUTF8[0] == '\0') ||
(!strcmp (passphrase, passphraseNonUTF8)))
{
// If the UTF8 version is empty or the two phrases
// are the same, then there's no need to check non-
// UTF8 phrase, so just toss it out now.
PGPFreeData (passphraseNonUTF8);
passphraseNonUTF8 = NULL;
}
}
}
#endif
err = PGPOrderKeySet( keySet, kPGPKeyOrdering_Any, FALSE, &keyListRef );
if( IsntPGPError( err ) )
{
PGPKeyIterRef keyIterator;
/* Loop twice, first try non-token keys then token keys */
for( tryToken=0; tryToken < 2; tryToken++ )
{
err = PGPNewKeyIter( keyListRef, &keyIterator );
if( IsntPGPError( err ) )
{
err = PGPKeyIterNextKeyDBObj( keyIterator, kPGPKeyDBObjType_Key, &theKey );
while( IsntPGPError( err ) )
{
PGPBoolean tryKey = FALSE;
if( signing )
{
PGPBoolean canSign;
if( IsntPGPError( PGPGetKeyDBObjBooleanProperty( theKey,
kPGPKeyProperty_CanSign, &canSign ) ) && canSign )
{
tryKey = TRUE;
}
}
else
{
PGPBoolean canDecrypt;
if( IsntPGPError( PGPGetKeyDBObjBooleanProperty( theKey,
kPGPKeyProperty_CanDecrypt, &canDecrypt ) ) &&
canDecrypt )
{
tryKey = TRUE;
}
}
/*
* Danger with token keys: if we query them with
* bad passphrases (like passphrases for other
* keys) the token can lock up. So we don't query
* them at all for signing keys, and we query them
* last for encryption keys. (Note that signing
* test is done at the end of the tryToken loop.)
*/
if( tryKey
&& IsntPGPError( PGPGetKeyDBObjBooleanProperty( theKey,
kPGPKeyProperty_IsOnToken, &onToken ) ) )
{
tryKey = (!onToken == !tryToken);
}
if ( tryKey )
{
if ( PGPPassphraseIsValid( theKey,
PGPOPassphrase( context, passphrase),
PGPOLastOption( context ) ) )
{
foundValidKey = TRUE;
break;
}
if (passphraseNonUTF8)
{
// see if a non-UTF8 version of the phrase
// unlocks the key
if ( PGPPassphraseIsValid( theKey,
PGPOPassphrase( context, passphraseNonUTF8),
PGPOLastOption( context ) ) )
{
foundValidKey = TRUE;
#if PGP_WIN32
if (nonUTF8)
*nonUTF8 = TRUE;
#endif
// length of non-UTF8 passphrase is
// always <= UTF8 passphrase length
strcpy( passphrase, passphraseNonUTF8 );
break;
}
}
}
err = PGPKeyIterNextKeyDBObj( keyIterator, kPGPKeyDBObjType_Key, &theKey );
}
PGPFreeKeyIter( keyIterator );
}
/* Don't try token keys if have a good one or if signing */
if( foundValidKey || signing )
break;
}
PGPFreeKeyList( keyListRef );
}
if (passphraseNonUTF8)
PGPFreeData( passphraseNonUTF8 );
return( foundValidKey ? theKey : NULL );
}
static const PGPOptionType sSendToKeyServerOptionSet[] =
{
kCommonAllowedKeyServerOptions
};
static PGPError
pgpSendToKeyServerDialog(
PGPContextRef context,
const PGPKeyServerSpec *server,
PGPtlsContextRef tlsContext,
PGPKeySetRef keysToSend,
PGPKeySetRef *failedKeys,
PGPOptionListRef optionList)
{
PGPError err;
pgpAssert( pgpOptionListIsValid( optionList ) );
err = pgpGetOptionListError( optionList );
if( IsntPGPError( err ) )
{
err = pgpCheckNetworklibAvailability();
if( IsntPGPError( err ) )
{
err = pgpCheckOptionsInSet( optionList, sSendToKeyServerOptionSet,
elemsof( sSendToKeyServerOptionSet ) );
if( IsntPGPError( err ) )
{
CPGPSendToKeyServerDialogOptions options;
err = options.GatherOptions( context, optionList );
if( IsntPGPError( err ) )
{
options.mServerList = server;
options.mServerCount = 1;
options.mTLSContext = tlsContext;
options.mKeysToSend = keysToSend;
options.mFailedKeys = failedKeys;
err = pgpSendToKeyServerDialogPlatform(context, &options);
}
}
}
}
return( err );
}
PGPError
PGPSendToKeyServerDialog(
PGPContextRef context,
const PGPKeyServerSpec *server,
PGPtlsContextRef tlsContext,
PGPKeySetRef keysToSend,
PGPKeySetRef *failedKeys,
PGPOptionListRef firstOption,
... )
{
PGPError err;
va_list args;
pgpAssert( pgpContextIsValid( context ) );
pgpAssert( IsntNull( server ) );
pgpAssert( PGPKeySetRefIsValid( keysToSend ) );
pgpAssert( IsntNull( failedKeys ) );
if( IsntNull( failedKeys ) )
*failedKeys = kInvalidPGPKeySetRef;
if( pgpContextIsValid( context ) &&
IsntNull( server ) &&
PGPKeySetRefIsValid( keysToSend ) &&
IsntNull( failedKeys ) )
{
PGPOptionListRef optionList;
va_start( args, firstOption );
optionList = pgpBuildOptionListArgs( context, FALSE, firstOption,
args );
va_end( args );
err = pgpSendToKeyServerDialog( context, server, tlsContext,
keysToSend, failedKeys, optionList );
PGPFreeOptionList( optionList );
}
else
{
va_start( args, firstOption );
pgpFreeVarArgOptionList( firstOption, args );
va_end( args );
err = kPGPError_BadParams;
}
return( err );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -