📄 pgptestencode.c
字号:
pb.ioFCBIndx = 0;
pb.ioRefNum = refNum;
pb.ioNamePtr = spec->name;
err = PBGetFCBInfoSync( &pb );
if ( IsntPGPError( err ) )
{
spec->vRefNum = pb.ioFCBVRefNum;
spec->parID = pb.ioFCBParID;
}
return( err );
}
/*____________________________________________________________________________
Create a file ref from the specified path.
For macintosh, the path is assumed to be just a file name in the local
directory.
____________________________________________________________________________*/
static PGPError
sGetFileRef(
PGPContextRef context,
char const * fileName,
PGPFileSpecRef * outRef )
{
PGPError err;
FSSpec spec;
err = GetSpecFromRefNum( CurResFile(), &spec );
if ( IsntPGPError( err ) )
{
CToPString( fileName, spec.name );
err = PGPNewFileSpecFromFSSpec( context, &spec, outRef );
}
return( err );
}
#else /* ] PGP_MACINTOSH [ */
static PGPError
sGetFileRef(
PGPContextRef context,
char const * path,
PGPFileSpecRef * outRef )
{
return( PGPNewFileSpecFromFullPath( context, path, outRef ) );
}
#endif /* ] PGP_MACINTOSH */
/*
* Options:
* -d Decrypt
* -s <username> Sign it specified user name
* -b Break off as separate signature
* -a Ascii armor the output
* -t Treat as text mode
* -c <passphrase> Use pass phrase for encrypting/decrypting
* -u Warn on untrusted keys
* -z <passphrase> Key passphrase for signing/decrypting
* -e <username> Encrypt to username (may have multiple -e switches)
* -m Use memory buffer functions rather than file functions
* -v Do verify, infile is sig, outfile is text (no output)
* -l List keyring contents. Filenames not used.
*
* Following are not implemented
* -y AnalYze to see what the file is. Output file not used.
* -k Add keys from infile into outfile
* -x Extract key ("infile" param) to outfile
*/
static void
RunTest(
PGPContextRef context,
int argc,
const char ** argv )
{
char const * infile = NULL;
char const * outfile = NULL;
PGPByte fdecrypt=0, fkeypass=0,
fsign=0, fencrypt=0, fconv=0,
fmem=0, fverify=0, fanalyze=0, faddkey=0, fextkey=0;
PGPLocalEncodingFlags fLocalEncode = kPGPLocalEncoding_None;
char const* skeypass=0;
int c;
PGPByte * inbuf;
PGPByte * outbuf;
size_t inbufsize, outbufsize;
PGPKeySetRef keySet = NULL, encryptKeySet = NULL;
PGPKeyDBRef keys = NULL;
PGPKeyDBObjRef signKey = NULL;
PGPOptionListRef opts = NULL;
PGPError err = kPGPError_NoErr;
PGPFileSpecRef inref=NULL, outref=NULL;
PGPFileSpecRef pubspec=NULL, privspec=NULL;
MyState myState;
/* Set up and initialize context */
err = PGPNewFileSpecFromFullPath( context, "testfiles/pubring.pkr", &pubspec );
if( IsPGPError( err ) )
goto error;
err = PGPNewFileSpecFromFullPath( context, "testfiles/secring.skr", &privspec );
if( IsPGPError( err ) )
goto error;
err = PGPOpenKeyDBFile( context,
kPGPOpenKeyDBFileOptions_Mutable, pubspec, privspec, &keys );
if( IsPGPError( err ) )
goto error;
memset( &myState, 0, sizeof(myState) );
PGPBuildOptionList( context, &opts,
PGPOEventHandler(context, myEvents, &myState),
PGPOLastOption( context ) );
PGPNewKeySet( keys, &encryptKeySet );
pgpoptind = 0;
while ((c = pgpgetopt(argc,
argv, "daykxls:btc:uz:e:mvi:")) != EOF) {
switch (c) {
case 'd': fdecrypt = TRUE;
break;
case 'a': PGPAppendOptionList( opts,
PGPOArmorOutput(context, TRUE),
PGPOLastOption( context ) );
break;
case 'b': PGPAppendOptionList( opts,
PGPODetachedSig( context,
PGPOLastOption ( context ) ),
PGPOLastOption( context ) );
break;
case 't': PGPAppendOptionList( opts,
PGPODataIsASCII(context, TRUE),
PGPOLastOption( context ) );
break;
case 'u': PGPAppendOptionList( opts,
PGPOWarnBelowValidity( context,
kPGPValidity_Complete ),
PGPOLastOption ( context ) );
break;
case 'm': fmem = TRUE;
break;
case 'c': PGPAppendOptionList( opts,
PGPOConventionalEncrypt( context,
PGPOPassphrase( context, pgpoptarg ),
PGPOLastOption ( context ) ),
PGPOLastOption( context ) );
fconv = TRUE;
break;
case 'z': fkeypass = TRUE;
skeypass = pgpoptarg;
break;
case 's':
{
PGPUInt32 numKeys;
PGPFilterRef filter;
err = PGPNewKeyDBObjDataFilter( context,
kPGPUserIDProperty_Name, pgpoptarg,
strlen( pgpoptarg ),
kPGPMatchCriterion_SubString, &filter );
if ( IsntPGPError( err ) ) {
err = PGPFilterKeyDB( keys, filter, &keySet);
if ( IsPGPError( err ) )
goto error;
PGPFreeFilter( filter );
filter = NULL;
}
if ( IsPGPError( err ) )
goto error;
PGPCountKeys( keySet, &numKeys );
if( numKeys < 1 ) {
fprintf (stderr, "Unable to find a key for %s\n",
pgpoptarg);
} else if( numKeys > 1 ) {
fprintf (stderr, "Ambiguous key choice: %s\n",
pgpoptarg);
} else {
PGPKeyIterRef kiter;
PGPNewKeyIterFromKeySet( keySet, &kiter );
PGPKeyIterNextKeyDBObj( kiter, kPGPKeyDBObjType_Key,
&signKey );
PGPFreeKeyIter( kiter );
fsign = TRUE;
}
PGPFreeKeySet( keySet );
break;
}
case 'e':
{
PGPUInt32 numKeys;
PGPFilterRef filter;
err = PGPNewKeyDBObjDataFilter( context,
kPGPUserIDProperty_Name, pgpoptarg,
strlen( pgpoptarg ),
kPGPMatchCriterion_SubString, &filter );
if ( IsntPGPError( err ) ) {
err = PGPFilterKeyDB( keys, filter, &keySet);
if ( IsPGPError( err ) )
goto error;
PGPFreeFilter( filter );
filter = NULL;
}
if ( IsPGPError( err ) )
goto error;
PGPCountKeys( keySet, &numKeys );
if( numKeys < 1 ) {
fprintf (stderr, "Unable to find a key for %s\n",
pgpoptarg);
} else if( numKeys > 1 ) {
fprintf (stderr, "Ambiguous key choice: %s\n",
pgpoptarg);
} else {
PGPAddKeys( keySet, encryptKeySet );
fencrypt = TRUE;
}
PGPFreeKeySet( keySet );
break;
}
case 'i': /* local encode (MacBinary for Mac) */
{
fLocalEncode = kPGPLocalEncoding_None;
if ( strstr( pgpoptarg, "force" ) != NULL )
fLocalEncode |= kPGPLocalEncoding_Force;
else if ( strstr( pgpoptarg, "auto" ) != NULL )
fLocalEncode |= kPGPLocalEncoding_Auto;
else
fLocalEncode |= kPGPLocalEncoding_Auto;
if ( strstr( pgpoptarg, "nocrcok" ) != NULL )
fLocalEncode |= kPGPLocalEncoding_NoMacBinCRCOkay;
PGPAppendOptionList( opts,
PGPOLocalEncoding( context, fLocalEncode ),
PGPOLastOption( context ) );
break;
}
case 'v': fverify = TRUE;
break;
case 'y': fanalyze = TRUE;
break;
case 'k': faddkey = TRUE;
break;
case 'x': fextkey = TRUE;
break;
case 'l': printf ("Keyring contents:\n");
printKeys (PGPPeekKeyDBRootKeySet(keys), " ", stdout);
printf ("\n");
goto done;
break;
case ':':
case '?': userr ("");
default: userr ("");
}
}
/* Check for some incompatible options and that we have something to do */
if (fdecrypt + fencrypt + fconv + fverify + fanalyze
+ faddkey + fextkey > 1)
userr ("");
if (fdecrypt + fencrypt + fconv + fverify + fsign
+ fanalyze + faddkey + fextkey < 1)
userr ("");
if (fsign && (fdecrypt || fverify || fanalyze || faddkey || fextkey))
userr ("");
/* Check that we have an input and an output file spec as needed */
if (!(infile = argv[pgpoptind++]))
userr ("");
if( fmem ) {
inbuf = fileread (infile, &inbufsize);
PGPAppendOptionList( opts,
PGPOInputBuffer( context, inbuf, inbufsize ),
PGPOLastOption ( context ) );
} else {
if( IsPGPError( sGetFileRef( context, infile, &inref ) ) )
goto error;
PGPAppendOptionList( opts, PGPOInputFile( context, inref ),
PGPOLastOption ( context ) );
}
if (!fanalyze) {
if (!(outfile = argv[pgpoptind++]))
userr ("");
if( fverify ) {
/* Don't set up an output file, handle 2nd input below */
} else if( fmem ) {
PGPAppendOptionList( opts,
PGPOAllocatedOutputBuffer( context,
(void **)&outbuf, MAX_PGPUInt32, &outbufsize ),
PGPOLastOption ( context ) );
} else {
if( IsPGPError( sGetFileRef( context, outfile, &outref ) ) )
goto error;
PGPAppendOptionList( opts,
PGPOOutputFile( context, outref ),
PGPOLastOption ( context ) );
}
}
if ( pgpoptind < argc )
userr ("");
/* Handle signing option */
if( fsign ) {
if( fkeypass ) {
PGPAppendOptionList( opts,
PGPOSignWithKey( context, signKey,
PGPOPassphrase( context, skeypass ),
PGPOLastOption ( context ) ),
PGPOLastOption ( context ) );
} else {
PGPAppendOptionList( opts,
PGPOSignWithKey( context, signKey,
PGPOLastOption ( context ) ),
PGPOLastOption ( context ) );
}
}
if (fencrypt || fconv || fsign) {
if( fencrypt ) {
PGPAppendOptionList( opts,
PGPOEncryptToKeySet( context, encryptKeySet ),
PGPOLastOption ( context ) );
}
if( IsPGPError( err = PGPEncode( context, opts,
PGPOLastOption ( context ) ) ) )
goto error;
if( fmem )
filewrite (outfile, outbuf, outbufsize);
} else if (fdecrypt || fverify) {
if (fverify) {
/* infile is signature, outfile should hold data */
if (fmem) {
myState.dataBuf = fileread (outfile, &myState.dataBufSize);
} else {
if( IsPGPError( sGetFileRef( context, outfile, &outref ) ) )
goto error;
myState.fileRef = outref;
myState.fileName = outfile;
}
myState.fLocalEncode = fLocalEncode;
}
if( fkeypass ) {
PGPAppendOptionList( opts,
PGPOPassphrase( context, skeypass ),
PGPOLastOption ( context ) );
}
if( IsPGPError( err = PGPDecode( context, opts,
PGPOKeyDBRef( context, keys ),
PGPOLastOption ( context ) ) ) )
goto error;
if( fmem && !fverify )
filewrite (outfile, outbuf, outbufsize);
#if 0
} else if (fanalyze) {
if (fmem) {
inbuf = fileread (infile, &inbufsize);
rslt = SimplePGPAnalyzeBuffer (inbuf, inbufsize);
} else {
rslt = SimplePGPAnalyzeFile (infile);
}
} else if (faddkey) {
char *keyfile = outfile;
if (fmem) {
inbuf = fileread (infile, &inbufsize);
rslt = SimplePGPAddKeyBuffer (NULL, inbuf, inbufsize, keyfile);
} else {
rslt = SimplePGPAddKey (NULL, infile, keyfile);
}
} else if (fextkey) {
char *userid = infile;
if (fmem) {
outbufsize = 1;
outbuf = (PGPByte *)malloc(outbufsize);
rslt = SimplePGPExtractKeyBuffer (NULL, userid, (char *)outbuf,
&outbufsize, NULL);
outbuf = (PGPByte *)malloc(outbufsize);
rslt = SimplePGPExtractKeyBuffer (NULL, userid, outbuf,
&outbufsize, NULL);
if (!rslt)
filewrite (outfile, outbuf, outbufsize);
} else {
rslt = SimplePGPExtractKey (NULL, userid, outfile, NULL);
}
#endif
}
error:
done:
if( IsntNull( opts ) ) {
PGPFreeOptionList( opts );
opts = NULL;
}
if( IsntNull( inref ) ) {
PGPFreeFileSpec( inref );
inref = NULL;
}
if( IsntNull( outref ) ) {
PGPFreeFileSpec( outref );
outref = NULL;
}
if( IsntNull( pubspec ) ) {
PGPFreeFileSpec( pubspec );
pubspec = NULL;
}
if( IsntNull( privspec ) ) {
PGPFreeFileSpec( privspec );
privspec = NULL;
}
if( IsntNull( encryptKeySet ) ) {
PGPFreeKeySet( encryptKeySet );
encryptKeySet = NULL;
}
if ( IsntPGPError( err ) )
printf ("Finished, no errors.\n");
else
{
printf ("!!!!!!!!!!!!!!\n");
pgpTestAssertNoErr( err );
printf ("!!!!!!!!!!!!!!\n");
}
PGPFreeKeyDB( keys );
pgpTestAssertNoErr( err );
}
/*____________________________________________________________________________
Decode a command line and put it into argc, argv form.
argv[ 0 ] is set to a dummy arg.
Caller should deallocate argv with free()
____________________________________________________________________________*/
static void
DecodeCommandLine(
char * buffer,
int * argcOut,
char *** argvOut)
{
int argc;
char ** argv;
int lastlen;
char * nextArg;
char * curBuffer;
char programName[ 64 ];
#define kMaxArgs 100
*argvOut = NULL;
*argcOut = 0;
argv = (char **)malloc( sizeof( char *) * kMaxArgs );
if ( IsNull( argv ) )
return;
strcpy( programName, "testEncode" );
argv[ 0 ] = programName; /* simulate program name */
argc = 1;
curBuffer = buffer;
while ( argc < kMaxArgs &&
IsntNull(nextArg = strtok( curBuffer, " ")))
{
curBuffer = 0; /* strange, but required for strok */
argv[ argc ] = nextArg;
++argc;
}
/* get last arg and turn EOL into null */
if ( argc > 1 )
{
char * lastArg = argv[ argc - 1 ];
lastlen = strlen( lastArg );
if ( isspace( lastArg[ lastlen-1 ] ) )
lastArg[ lastlen-1 ] = '\0';
/* why check this? */
if ( argc == 2 && argv[1][0]=='\0')
argc = 1;
}
*argcOut = argc;
*argvOut = argv;
}
/*____________________________________________________________________________
Loop, accepting a command line, until "q" or "Q" is entered.
____________________________________________________________________________*/
void
TestEncodeInteractive( PGPContextRef context )
{
for(;;)
{
char buffer[ 1024 ];
fputs( "Command -> ", stdout);
fgets( buffer, sizeof(buffer), stdin);
/* enable a quit command */
if ( buffer[ 0 ] == 'q' || buffer[ 0 ] =='Q' )
return;
TestEncodeCmdLine( context, buffer );
}
}
/*____________________________________________________________________________
Run an encode test given an argc, argv setup as gotten from a Unix main()
routine.
____________________________________________________________________________*/
void
TestEncodeArgs(
PGPContextRef context,
int argc,
const char ** argv )
{
RunTest( context, argc, argv );
}
/*____________________________________________________________________________
Run an encode test given a command line string. The string will be parsed
for options.
____________________________________________________________________________*/
void
TestEncodeCmdLine(
PGPContextRef context,
const char * commandLine )
{
int argc;
char ** argv;
char temp[ 1024 ];
printf( "***** Command: %s *****\n", commandLine );
/* make copy so that we won't modify original */
if ( strlen( commandLine ) >= sizeof( temp ) )
return;
strcpy( temp, commandLine );
DecodeCommandLine( temp, &argc, &argv );
/* first arg is always program name, so we require two or more */
if ( argc >= 2 )
{
TestEncodeArgs( context, argc, (const char **)argv );
free( argv );
}
}
/*
* Local Variables:
* tab-width: 4
* End:
* vi: ts=4 sw=4
* vim: si
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -