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

📄 pgptar.c

📁 PGP.Corporation.PGP.SDK.v3.7.2.rar 此前从本站下载的那个缺少几个lib文件
💻 C
📖 第 1 页 / 共 2 页
字号:
 	
	OPTESTPrintF("\tCreating New Tar Cache \n");
 	err = PGPOpenTARCacheFile(context,kPGPOpenTARCacheFileOptions_Create,
								cache, newEsk,SESSION_KEY_BUFFER_SIZE, newEskSize, &tar, 
 								PGPOPreferredAlgorithms(context, algors, 1),  
 								PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
								PGPOLastOption( context ) );  	CKERR;
   	
	OPTESTPrintF ("\t   Cache is encrypted with the %s Algorithm\n", cipher_algor_table(newEsk[0]));
 	OPTESTPrintF ("\t   Session Key: [%d] ", (int)*newEskSize);
 	for( i=0; i< *newEskSize; i++ ) 
		OPTESTPrintF("%02x%s", newEsk[i], ((i+1)%4?"":" "));
	OPTESTPrintF("\n");
  
 	testFile = malloc(strlen(gTestVectorsDirectory) + strlen(TEST_FILE)+ 2 );
	strcpy(testFile, gTestVectorsDirectory);
	strcat(testFile, "/");
	strcat(testFile, TEST_FILE);

	testDIR = malloc(strlen(gTestVectorsDirectory) + strlen(TEST_DIR)+ 2 );
	strcpy(testDIR, gTestVectorsDirectory);
 	strcat(testDIR, "/");
	strcat(testDIR, TEST_DIR);

	zeroLengthFile = malloc(strlen(gTestVectorsDirectory) + strlen(ZERO_LENGTH_FILE)+ 2 );
	strcpy(zeroLengthFile, gTestVectorsDirectory);
 	strcat(zeroLengthFile, "/");
	strcat(zeroLengthFile, ZERO_LENGTH_FILE);

  	err = PGPNewFileSpecFromFullPath(context,gTestVectorsDirectory, &rootSpec); CKERR;
  	err = PGPNewFileSpecFromFullPath(context,"myPath", &relativeSpec); CKERR;
  	err = PGPNewFileSpecFromFullPath(context,testDIR, &dirSpec); CKERR;
	err = PGPNewFileSpecFromFullPath(context,testFile, &fileSpec); CKERR;
	err = PGPNewFileSpecFromFullPath(context,zeroLengthFile, &fileSpec1); CKERR;

 	/* import file */
	OPTESTPrintF("\t   Importing file %s \n", TEST_FILE);
  	err = PGPImportTARCacheObj(tar,  fileSpec, &obj2, 
 								PGPORootPath(context, rootSpec),
 								PGPORelativePath(context, relativeSpec),
								PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
								PGPOLastOption( context ) ); CKERR;
 	
  	OPTESTPrintF("\t   Importing zero length file  \n");
  	err = PGPImportTARCacheObj(tar,  fileSpec1, &obj3, 
 								PGPORootPath(context, rootSpec),
 								PGPORelativePath(context, relativeSpec),
								PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
								PGPOLastOption( context ) ); CKERR;
 						
 	/* import directory */
	OPTESTPrintF("\t   Importing directory %s \n", TEST_DIR);
 	err = PGPImportTARCacheObj(tar,  dirSpec, &obj1, 
								PGPORootPath(context, rootSpec),
								PGPORelativePath(context, relativeSpec),
								PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
								PGPOLastOption( context ) ); CKERR;
 
  
	OPTESTPrintF("\t   Rename file to %s\n", RENAME_FILE);
 	err =  PGPSetTARCacheObjDataProperty(obj3, kPGPTARCacheObjProperty_Name,
											RENAME_FILE, sizeof(RENAME_FILE)); CKERR;


	err =  PGPSetTARCacheObjDataProperty(obj2, kPGPTARCacheObjProperty_Name,
											LONG_NAME, sizeof(LONG_NAME)); CKERR;

	OPTESTPrintF("\n");

 done:
	
	if(zeroLengthFile) free(zeroLengthFile);
	if(testFile) free(testFile);
	if(testDIR) free(testDIR);
  	if(name) PGPFreeData(name);
 	if(relativeSpec) PGPFreeFileSpec(relativeSpec);
 	if(rootSpec) PGPFreeFileSpec(rootSpec);
	if(fileSpec) PGPFreeFileSpec(fileSpec);
	if(fileSpec1) PGPFreeFileSpec(fileSpec1);
	if(dirSpec) PGPFreeFileSpec(dirSpec);

   	if( PGPTARCacheRefIsValid (tar) )
			PGPFreeTARCache(tar);
	return err;
}
 
 

 PGPError TestTarEncode(PGPContextRef context)
{
	PGPError		err			= kPGPError_NoErr;
	PGPFileSpecRef 	inDirSpec 	= kInvalidPGPFileSpecRef;
	PGPFileSpecRef 	outSpec 	 = kInvalidPGPFileSpecRef;
	PGPFileSpecRef 	tarCacheSpec = kInvalidPGPFileSpecRef;

	PGPKeyDBRef		keyDB	 	= kInvalidPGPKeyDBRef;
 	PGPKeySetRef	keyset		= kInvalidPGPKeySetRef;
	PGPKeyID 		theKeyID;

 	PGPKeyDBObjRef	BobsKey 	= kInvalidPGPKeyDBObjRef;
 	PGPKeyDBObjRef	AlicesKey 	= kInvalidPGPKeyDBObjRef;
 	PGPKeyDBObjRef	signKey 	= kInvalidPGPKeyDBObjRef;
 	PGPKeyDBObjRef	encryptKey 	= kInvalidPGPKeyDBObjRef;

 	void			*cBuf		= NULL;
 	PGPSize			cBufSize;
  
	static PGPByte		newEsk[SESSION_KEY_BUFFER_SIZE];
	static PGPSize		newEskSize;
	
	char			outputDir[MAX_PATH];
 	
	DecodeInfo		decodeInfo;
 	InitDecodeInfo(&decodeInfo);
	
	sprintf(outputDir, "%s%sTarTest", gOutputDirectory,SLASH);
	if(makeDirectory(outputDir) )
 		FAIL("Failed to create test directory\n");
 	decodeInfo.outDir = (PGPByte*)outputDir;
  	
/* Read in the test key and get a ref to it */
	err = importKeys(context,gTestKeysPath, kPGPInputFormat_PGP, &keyDB); CKERR;
   	
/* We must perform the KeyRing Sig check for expiration dates to work */
	err = PGPNewKeySet( keyDB, &keyset );CKERR;
 	err = PGPCheckKeyRingSigs(keyset,keyDB, TRUE, NULL,NULL  );CKERR;
   
/* Find Bobs Key  */
  	err =  PGPNewKeyIDFromString( kBobsKeyIDString,  kPGPPublicKeyAlgorithm_DSA,  &theKeyID); CKERR;
  	err =  PGPFindKeyByKeyID( keyDB, &theKeyID, &BobsKey); CKERR;
	

	err = PGPGetKeyForUsage( BobsKey, kPGPKeyPropertyFlags_UsageSignMessages,  &signKey); CKERR;
 	COPY( &theKeyID, &decodeInfo.key[0].keyID, sizeof(PGPKeyID));
	decodeInfo.key[0].passPhrase = kBobsPassPhrase;
	
/* Find Alices Key */
	err =  PGPNewKeyIDFromString( kAlicesKeyIDString, 
							kPGPPublicKeyAlgorithm_DSA,  &theKeyID); CKERR;
	err =  PGPFindKeyByKeyID( keyDB, &theKeyID, &AlicesKey); CKERR;

	err = PGPGetKeyForUsage( AlicesKey, kPGPKeyPropertyFlags_UsageEncryptCommunications, 
							&encryptKey); CKERR;
	COPY( &theKeyID, &decodeInfo.key[1].keyID, sizeof(PGPKeyID));
	decodeInfo.key[1].passPhrase = kAlicesPassPhrase;
	decodeInfo.keyCount = 2;

	err = PGPNewFileSpecFromFullPath(context, gTestVectorsDirectory, &inDirSpec); CKERR;
 	err = PGPNewFileSpecFromFullPath(context, OUTPUT_FILE, &outSpec); CKERR;
 
#if HAVE_PRAGMA_MARK
#pragma mark	-- Simple Directory Encoding 
#endif
	{
		OPTESTPrintF("\nPGP TAR test 1, -- Simple Directory Encoding\n");
		OPTESTPrintF("\tEncoding Directory \"%s\"\n",gTestVectorsDirectory);

	/* Encode Directory to PGP file */
		decodeInfo.option = kDecode_NoOption;
		err =  PGPEncode( context, 
				PGPOInputFile ( context, inDirSpec),
	#if TEST1_USES_FILE
				PGPOOutputFile ( context, outSpec),
	#else
				PGPOAllocatedOutputBuffer( context, &cBuf, MAX_PGPSize, &cBufSize),
	#endif
				PGPOEncryptToKeyDBObj(context,encryptKey),
				PGPOSignWithKey ( context, signKey, PGPOPassphrase( context, kBobsPassPhrase),  PGPOLastOption ( context ) ),
				PGPOCommentString ( context, "Detached Sig for intar"), 
				PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
	 //  			PGPOSendNullEvents(context, 100000),
				PGPOLastOption    ( context ) ); CKERR;
		
	#if TEST1_USES_FILE
		{
			struct stat st;
			if( stat(OUTPUT_FILE, &st) == 0 )
				OPTESTPrintF("\tEncoded Message in file \"%s\" is %d bytes\n\n", OUTPUT_FILE,  (int)st.st_size);
		}
	#else
		OPTESTPrintF("\tEncoded Message is %d bytes\n",   (int)cBufSize  );
	#endif
	 
		OPTESTPrintF("\tUnpacking Encoded Tar\n");
		decodeInfo.option = kDecode_UnpackTar;

		err = PGPDecode( context, 
	#if TEST1_USES_FILE
				PGPOInputFile ( context, outSpec),
	#else
				PGPOInputBuffer ( context, cBuf, cBufSize ),
	#endif
				PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				PGPOKeyDBRef(context, keyDB),
				PGPOLastOption  ( context ) ); CKERR;
	   
		
	#if HAVE_PRAGMA_MARK
	#pragma mark	-- Detached Signature of  Directory 
	#endif
	#if DETACHED_TAR_SIG_TEST     
		OPTESTPrintF("\nPGP TAR test 1A, -- Detached Signature of  Directory\n");
		OPTESTPrintF("\tEncoding Directory \"%s\"\n",gTestVectorsDirectory);
		decodeInfo.option = kDecode_NoOption;
		err =  PGPEncode( context, 
				PGPOInputFile ( context, inDirSpec),
				PGPOOutputFile ( context, outSpec),
				PGPOSignWithKey ( context, signKey, PGPOPassphrase( context, kBobsPassPhrase),  PGPOLastOption ( context ) ),
				PGPODetachedSig(context, PGPOLastOption ( context ) ),
	 // 			PGPODataIsASCII ( context, TRUE),  
	 //			PGPOOutputLineEndType ( context,kPGPLineEnd_LF),
				PGPOArmorOutput(context, TRUE),
				PGPOCommentString ( context, "Detached Sig for intar"), 
				PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				PGPOLastOption    ( context ) ); CKERR;
		
		OPTESTPrintF("\n\tVerifing  Encoded Tar\n");
		decodeInfo.option = kDecode_Ignore_Output;
		err = PGPDecode( context, 
	//   			PGPOInputBuffer ( context, cBuf, cBufSize ),
				PGPOInputFile ( context, outSpec),
				PGPODetachedSig(context, PGPOInputFile ( context, inDirSpec), PGPOLastOption ( context ) ),
				PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				PGPOKeyDBRef(context, keyDB),
				PGPOLastOption  ( context ) ); CKERR;
	#endif
	}
	
#if HAVE_PRAGMA_MARK
#pragma mark -- TAR Cache operations 
#endif
	{
		char testFile[MAX_PATH];
 		sprintf(testFile, "%s%sintar.tarcache", outputDir, SLASH);

		OPTESTPrintF("\nPGP TAR test 2, -- TAR Cache operations\n");
		OPTESTPrintF("\tCreating Tar Cache from Encrypted Message\n");
		decodeInfo.option = kDecode_MakeTarCache;
		err = PGPDecode( context, 
	#if TEST1_USES_FILE
				PGPOInputFile ( context, outSpec),
	#else
				PGPOInputBuffer ( context, cBuf, cBufSize ),
	#endif
			   PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
			   PGPOKeyDBRef(context, keyDB),
			   PGPOLastOption  ( context ) ); CKERR;
		
		err = ListTarCache(context, decodeInfo.outFile, decodeInfo.sessionKey,  decodeInfo.sessionKeySize); CKERR;
		err = ExportandDeletefromTarCache(context,  decodeInfo.outFile, outputDir,  decodeInfo.sessionKey,  decodeInfo.sessionKeySize); CKERR;
	//	dumpTarCacheContent(context,testFile, decodeInfo.sessionKey, FALSE);

		err = ListTarCache(context, decodeInfo.outFile, decodeInfo.sessionKey,  decodeInfo.sessionKeySize); CKERR;
	 
		OPTESTPrintF("\tRe-encoding Tar Cache\n");
		if(cBuf != NULL) PGPFreeData(cBuf); cBuf = NULL;
		decodeInfo.option = kDecode_NoOption;
		err =  PGPEncode( context, 
				 PGPOInputTARCache (context, decodeInfo.outFile, decodeInfo.sessionKey,  decodeInfo.sessionKeySize ),
				 PGPOFileNameString(context, "fifi_doodle.tar"),	/* override tar name */
				 PGPOAllocatedOutputBuffer( context, &cBuf, MAX_PGPSize, &cBufSize),
				 PGPOEncryptToKeyDBObj(context,encryptKey),
				 PGPOSignWithKey ( context, signKey,
								   PGPOPassphrase( context, kBobsPassPhrase), 
								   PGPOLastOption ( context ) ),
				 PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				 PGPOSendNullEvents(context, 50),
				 PGPOOutputLineEndType ( context,kPGPLineEnd_LF),
				 PGPOArmorOutput(context, TRUE),
				 PGPOCommentString ( context, "Sample PGPTAR output"),  
				 PGPOLastOption    ( context ) ); CKERR;
		OPTESTPrintF("\tEncoded Message is %d bytes\n\n",   (int)cBufSize );
	  
		OPTESTPrintF("\tDumping Encrypted Tar Cache\n");
		decodeInfo.option = kDecode_DumpTar;
		err = PGPDecode( context, 
				  PGPOInputBuffer ( context, cBuf, cBufSize ),
				  PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				  PGPOSendNullEvents(context, 50),
				  PGPOKeyDBRef(context, keyDB),
				  PGPOLastOption  ( context ) ); CKERR;
	 
		OPTESTPrintF("\n\tUnpacking Tar Cache\n");
		decodeInfo.option = kDecode_UnpackTar;
		err = PGPDecode( context, 
				  PGPOInputBuffer ( context, cBuf, cBufSize ),
				  PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				  PGPOSendNullEvents(context, 50),
				  PGPOKeyDBRef(context, keyDB),
				  PGPOLastOption  ( context ) ); CKERR;

		PGPFreeData(cBuf); cBuf = NULL;
		CleanUpDecodeInfo(&decodeInfo);
	}
	
  	{
		char testFile[MAX_PATH];
		OPTESTPrintF("\nPGP TAR test 3, -- TAR Cache creation\n");
 		sprintf(testFile, "%s%stest.tarcache", outputDir,SLASH);

		err = PGPNewFileSpecFromFullPath(context, testFile, &tarCacheSpec); CKERR;
		err = CreateTarCacheTest(context,tarCacheSpec, newEsk, &newEskSize); CKERR;
		if(gVerbose_flag) 
			dumpTarCacheContent(context, testFile, newEsk, gDebug_flag);
		err = ListTarCache(context, tarCacheSpec, newEsk,  newEskSize); CKERR;
	 
		OPTESTPrintF("\tRe-encoding Tar Cache\n");
		decodeInfo.option = kDecode_NoOption;
		err =  PGPEncode( context, 
					PGPOInputTARCache (context, tarCacheSpec, newEsk, newEskSize ),
					PGPOAllocatedOutputBuffer( context, &cBuf, MAX_PGPSize, &cBufSize),
					PGPOEncryptToKeyDBObj(context,encryptKey),
					PGPOSignWithKey ( context, signKey,
									PGPOPassphrase( context, kBobsPassPhrase), 
									PGPOLastOption ( context ) ),
					PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
					PGPOSendNullEvents(context, 50),
 					PGPOOutputLineEndType ( context,kPGPLineEnd_LF),
					PGPOArmorOutput(context, TRUE),
					PGPOCommentString ( context, "Sample PGPTAR output"),  
					PGPOLastOption    ( context ) ); CKERR;
		  OPTESTPrintF("\tEncoded Message is %d bytes\n",   (int)cBufSize );
	  
		OPTESTPrintF("\n\tDumping Encrypted Tar Cache\n");
		decodeInfo.option = kDecode_DumpTar;
		err = PGPDecode( context, 
				   PGPOInputBuffer ( context, cBuf, cBufSize ),
				   PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				  PGPOSendNullEvents(context, 50),
				   PGPOKeyDBRef(context, keyDB),
				   PGPOLastOption  ( context ) ); CKERR;
	 
		OPTESTPrintF("\n\tUnpacking Encrypted Tar Cache\n");
		decodeInfo.option = kDecode_UnpackTar;
		err = PGPDecode( context, 
				   PGPOInputBuffer ( context, cBuf, cBufSize ),
				   PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				  PGPOSendNullEvents(context, 50),
				   PGPOKeyDBRef(context, keyDB),
				   PGPOLastOption  ( context ) ); CKERR;
 }
 
 done:
	CleanUpDecodeInfo(&decodeInfo);
   
	if( cBuf != NULL)
		PGPFreeData(cBuf);
  			  
   if( PGPFileSpecRefIsValid(outSpec))
	  PGPFreeFileSpec(outSpec);

   if( PGPFileSpecRefIsValid(tarCacheSpec))
	  PGPFreeFileSpec(tarCacheSpec);
			  
	if( PGPFileSpecRefIsValid(inDirSpec))
			 PGPFreeFileSpec(inDirSpec);
 	
 	if( PGPKeySetRefIsValid (keyset) )
		 PGPFreeKeySet(keyset);

	if( PGPKeyDBRefIsValid( keyDB ) )
		PGPFreeKeyDB( keyDB );
	
    return err;
}


⌨️ 快捷键说明

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