📄 encode.c
字号:
/*____________________________________________________________________________
Encode.c
Copyright (C) 2003, 2004 PGP Corporation
All rights reserved.
Encode.c - This file contains functions which are used to verify
the High level Crypto functions of the PGPsdk
$Id: Encode.c 48493 2006-10-12 21:19:56Z vinnie $
____________________________________________________________________________*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pgpFeatures.h"
#include "pgpErrors.h"
#include "pgpEncode.h"
#include "pgpKeys.h"
#include "pgpPublicKey.h"
#include "pgpHash.h"
#include "pgpUtilities.h"
#include "pgpDump.h"
#include "optest.h"
static const char MessageToBeSigned[] =
"Our challenge is to synergistically utilize long-term "
"high-impact cryptographic data to quickly pursue timely "
"catalysts for change such that we may continue to interactively "
"fashion long-term high-impact benefits to meet our customer's "
"needs and stay competitive in tomorrow's world.";
PGPError TestEncodeDecode(PGPContextRef context)
{
PGPError err = kPGPError_NoErr;
PGPKeyDBRef keyDB = kInvalidPGPKeyDBRef;
PGPKeyIterRef iter = kInvalidPGPKeyIterRef;
PGPKeyID theKeyID;
PGPKeyDBObjRef BobsKey = kInvalidPGPKeyDBObjRef;
PGPKeyDBObjRef AlicesKey = kInvalidPGPKeyDBObjRef;
PGPKeyDBObjRef signKey = kInvalidPGPKeyDBObjRef;
PGPKeyDBObjRef encryptKey = kInvalidPGPKeyDBObjRef;
PGPOptionListRef encodeOptions = kInvalidPGPOptionListRef;
PGPOptionListRef savedOptions = kInvalidPGPOptionListRef;
PGPOptionListRef moreOptions = kInvalidPGPOptionListRef;
PGPKeySetRef encryptSet = kInvalidPGPKeySetRef;
DecodeInfo decodeInfo;
PGPBoolean hideRecipents = TRUE;
/* Cipher Text */
void * cBuf = NULL;
PGPSize cBufSize;
/* Result Text */
void * dBuf = NULL;
PGPSize dBufSize;
/* Dump Text */
void* dumpBuf = NULL;
PGPSize dumpBufSize = 0;
InitDecodeInfo(&decodeInfo);
/* Read in the test key and get a ref to it */
OPTESTPrintF("\tImport Demo Keys into Key DB\n");
err = importKeys(context,gTestKeysPath, kPGPInputFormat_PGP, &keyDB); CKERR;
/* Since PGP keys have the concept of a signing key and
a possible encryption (communication) subkey, we need to get both
keys for this demo.. */
/* 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;
/* create encryption keyset */
err = PGPNewOneKeySet(encryptKey, &encryptSet); CKERR;
OPTESTPrintF("\tMessage to encode is %d bytes\n", (int) sizeof(MessageToBeSigned)-1 );
/* this demos the option list building code.. these opts could have been in the PGPEncode call */
err = PGPNewOptionList(context, &encodeOptions);
err = PGPAppendOptionList(encodeOptions,
PGPOCommentString ( context, "FIPS 140-2 Test message"),
PGPOLastOption( context ) );CKERR;
err = PGPBuildOptionList(context, &moreOptions,
PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
PGPOForYourEyesOnly(context,TRUE),
PGPODataIsASCII ( context, FALSE),
(hideRecipents?PGPOObfuscateRecipients(context, TRUE) : PGPONullOption(context)),
PGPOOutputLineEndType ( context,kPGPLineEnd_LF),
PGPOArmorOutput(context, TRUE),
PGPOLastOption( context ) ); CKERR;
err = PGPAppendOptionList( encodeOptions, moreOptions, PGPOLastOption( context ) );CKERR;
err = PGPCopyOptionList(encodeOptions, &savedOptions);CKERR;
/* TE03.14.02 - Encrypt data */
/* TE03.14.02 - Sign data */
decodeInfo.option = kDecode_NoOption;
err = PGPEncode( context,
PGPOInputBuffer ( context, MessageToBeSigned, sizeof(MessageToBeSigned)-1 ),
PGPOAllocatedOutputBuffer( context, &cBuf, MAX_PGPSize, &cBufSize),
PGPOSignWithKey ( context, signKey,
PGPOPassphrase( context, kBobsPassPhrase),
PGPOLastOption ( context ) ),
PGPOEncryptToKeySet(context, encryptSet),
PGPOOutputFormat(context, kPGPOutputFormat_PGP),
encodeOptions,
PGPOLastOption( context ) ); CKERR;
OPTESTPrintF("\tEncoded Message is %d bytes\n", (int)cBufSize );
if(gVerbose_flag)
OPTESTPrintF("\n%s\n", (char*)cBuf );
if(gVerbose_flag)
{
OPTESTPrintF("\n Decoded OpenPGP Packet\n");
err = PGPDump(context,
( kPGPDumpFlags_DumpIntegers
| kPGPDumpFlags_DumpLiteralPackets
| kPGPDumpFlags_DumpMarkerPackets
| kPGPDumpFlags_DumpPrivatePackets),
PGPOInputBuffer( context, cBuf, cBufSize),
PGPOAllocatedOutputBuffer( context, &dumpBuf, MAX_PGPSize, &dumpBufSize),
PGPOLastOption( context ) ); CKERR;
OPTESTPrintF("%s\n",(char*) dumpBuf);
PGPFreeData(dumpBuf); dumpBuf = NULL;
}
OPTESTPrintF("\tDecoding & Comparing Message\n");
/* TE03.14.02 - Decrypt data */
/* TE03.14.02 - Validate signed data */
err = PGPDecode( context,
PGPOInputBuffer ( context, cBuf, cBufSize ),
PGPOAllocatedOutputBuffer( context, &dBuf, MAX_PGPSize, &dBufSize),
PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
PGPOKeyDBRef(context, keyDB),
PGPOLastOption ( context ) ); CKERR;
err = ((dBufSize == sizeof(MessageToBeSigned) -1)
&& pgpMemoryEqual(MessageToBeSigned, dBuf, dBufSize))
? kPGPError_NoErr : kPGPError_SelfTestFailed;
OPTESTPrintF("\tDecoded Message is %d bytes (%s)\n ", (int)dBufSize, (IsntPGPError(err)?"valid":"invalid") );
if(gVerbose_flag) OPTESTPrintF("-----\n%s\n----\n", (char*)dBuf );
PGPFreeData(cBuf); cBuf = NULL;
PGPFreeData(dBuf); dBuf = NULL;
CleanUpDecodeInfo(&decodeInfo);
CKERR;
/* TE03.14.02 - Sign data */
/* create clear signed signature */
OPTESTPrintF("\n\tCreate Clear Signed Signature\n");
cBuf = PGPNewData( PGPGetDefaultMemoryMgr(), 1024, kPGPMemoryMgrFlags_Clear); CKNULL(cBuf);
decodeInfo.option = kDecode_NoOption;
err = PGPEncode( context,
PGPOInputBuffer ( context, MessageToBeSigned, sizeof(MessageToBeSigned)-1 ),
PGPOOutputBuffer( context, cBuf, 1024, &cBufSize),
PGPORawPGPInput(context, FALSE),
PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
PGPOClearSign(context, TRUE),
PGPOSignWithKey ( context, signKey,
PGPOPassphrase( context, kBobsPassPhrase),
PGPOLastOption ( context ) ),
PGPODataIsASCII ( context, TRUE),
PGPOCharsetString(context, "us-ascii"),
PGPOOutputLineEndType ( context,kPGPLineEnd_LF),
PGPOArmorOutput(context, TRUE),
PGPOCommentString ( context, "FIPS 140-2 Test message"),
PGPOLastOption ( context ) ); CKERR;
OPTESTPrintF("\tDetached Signature is %d bytes\n", (int)cBufSize );
if(gVerbose_flag) OPTESTPrintF("\n%s\n", (char*)cBuf );
OPTESTPrintF("\tValidating Signature\n");
/* TE03.14.02 - Validate signed data */
err = PGPDecode( context,
PGPOInputBuffer ( context, cBuf, cBufSize ),
PGPODetachedSig(context,
PGPOInputBuffer ( context, MessageToBeSigned, sizeof(MessageToBeSigned)-1 ),
PGPOLastOption ( context ) ),
PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
PGPOKeyDBRef(context, keyDB),
PGPOLastOption ( context ) );
PGPFreeData(cBuf); cBuf = NULL;
CleanUpDecodeInfo(&decodeInfo);
CKERR;
/* TE03.14.02 - Sign data */
/* create detached signature */
OPTESTPrintF("\n\tCreate Detached Signature\n");
cBuf = PGPNewData( PGPGetDefaultMemoryMgr(), 1024, kPGPMemoryMgrFlags_Clear); CKNULL(cBuf);
decodeInfo.option = kDecode_NoOption;
err = PGPEncode( context,
PGPOInputBuffer ( context, MessageToBeSigned, sizeof(MessageToBeSigned)-1 ),
PGPOOutputBuffer( context, cBuf, 1024, &cBufSize),
PGPORawPGPInput(context, FALSE),
PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
PGPOSignWithKey ( context, signKey,
PGPOPassphrase( context, kBobsPassPhrase),
PGPOLastOption ( context ) ),
PGPODetachedSig(context, PGPOLastOption ( context ) ),
PGPODataIsASCII ( context, TRUE),
PGPOOutputLineEndType ( context,kPGPLineEnd_LF),
PGPOArmorOutput(context, TRUE),
PGPOCommentString ( context, "FIPS 140-2 Test message"),
PGPOLastOption ( context ) ); CKERR;
OPTESTPrintF("\tDetached Signature is %d bytes\n", (int)cBufSize );
if(gVerbose_flag) OPTESTPrintF("\n%s\n", (char*)cBuf );
OPTESTPrintF("\tValidating Signature\n");
/* TE03.14.02 - Validate signed data */
err = PGPDecode( context,
PGPOInputBuffer ( context, cBuf, cBufSize ),
PGPODetachedSig(context,
PGPOInputBuffer ( context, MessageToBeSigned, sizeof(MessageToBeSigned)-1 ),
PGPOLastOption ( context ) ),
PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
PGPOKeyDBRef(context, keyDB),
PGPOLastOption ( context ) );
PGPFreeData(cBuf); cBuf = NULL;
CleanUpDecodeInfo(&decodeInfo);
CKERR;
/* TE03.14.02 - encrypt to a passphrase */
OPTESTPrintF("\n\tEncoding Message Conventionally \n");
err = PGPEncode( context,
PGPOInputBuffer ( context, MessageToBeSigned, sizeof(MessageToBeSigned)-1 ),
PGPOAllocatedOutputBuffer( context, &cBuf, MAX_PGPSize, &cBufSize),
PGPOCipherAlgorithm(context, kPGPCipherAlgorithm_AES256),
PGPOConventionalEncrypt ( context,
PGPOPassphrase( context, kSymmetricPassPhrase),
PGPOLastOption ( context ) ),
PGPOLastOption( context ) ); CKERR;
OPTESTPrintF("\tEncoded Message is %d bytes\n ", (int)cBufSize );
if(gVerbose_flag)
{
OPTESTPrintF("\n Decoded OpenPGP Packet\n");
err = PGPDump(context,
( kPGPDumpFlags_DumpIntegers
| kPGPDumpFlags_DumpLiteralPackets
| kPGPDumpFlags_DumpMarkerPackets
| kPGPDumpFlags_DumpPrivatePackets),
PGPOInputBuffer( context, cBuf, cBufSize),
PGPOAllocatedOutputBuffer( context, &dumpBuf, MAX_PGPSize, &dumpBufSize),
PGPOLastOption( context ) ); CKERR;
OPTESTPrintF("%s\n",(char*) dumpBuf);
}
OPTESTPrintF("\tDecoding & comparing Message\n");
/* TE03.14.02 - Decrypt data */
err = PGPDecode( context,
PGPOInputBuffer ( context, cBuf, cBufSize ),
PGPOAllocatedOutputBuffer( context, &dBuf, MAX_PGPSize, &dBufSize),
PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
PGPOPassphrase( context, kSymmetricPassPhrase),
PGPOKeyDBRef(context, keyDB),
PGPOLastOption ( context ) ); CKERR;
err = ((dBufSize == sizeof(MessageToBeSigned) -1)
&& pgpMemoryEqual(MessageToBeSigned, dBuf, dBufSize))
? kPGPError_NoErr : kPGPError_SelfTestFailed; CKERR;
done:
/* Cleanup this mess */
CleanUpDecodeInfo(&decodeInfo);
if( dBuf != NULL)
PGPFreeData(dBuf);
if( cBuf != NULL)
PGPFreeData(cBuf);
if( dumpBuf != NULL)
PGPFreeData(dumpBuf);
if( PGPKeySetRefIsValid (encryptSet) )
PGPFreeKeySet(encryptSet);
if(PGPOptionListRefIsValid(encodeOptions))
PGPFreeOptionList(encodeOptions);
if(PGPOptionListRefIsValid(moreOptions))
PGPFreeOptionList(moreOptions);
if(PGPOptionListRefIsValid(savedOptions))
PGPFreeOptionList(savedOptions);
if( PGPKeyIterRefIsValid( iter ) )
PGPFreeKeyIter( iter );
if( PGPKeyDBRefIsValid( keyDB ) )
PGPFreeKeyDB( keyDB );
return err;
}
#if 0
err = PGPEncode( context,
PGPOInputFile ( context, fileref),
PGPOConventionalEncrypt(context, PGPOLastOption ( context )),
PGPOPasskeyBuffer(context, buffer, len)
PGPOLastOption ( context ) );
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -