📄 expiredkey.c
字号:
/*____________________________________________________________________________
ExpiredKey.c
Copyright (C) 2003,2004 PGP Corporation
All rights reserved.
This file contains functions which are used to test
how PGP handles expired subkeys.
$Id: ExpiredKey.c 48493 2006-10-12 21:19:56Z vinnie $
____________________________________________________________________________*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pgpFeatures.h"
#include "pgpErrors.h"
#include "pgpUtilities.h"
#include "pgpRandomPool.h"
#include "pgpKeys.h"
#include "pgpPublicKey.h"
#include "pgpEncode.h"
#include "pgpRandomPool.h"
#include "pgpMemoryMgr.h"
#include "optest.h"
static const char encryptedMessage[] =
"-----BEGIN PGP MESSAGE-----\r\n"
"Version: PGP SDK 3.2.0\r\n"
"Comment: PGP Test message\r\n"
"\r\n"
"qANQR1DBwE4Du2teGwYL5pYQBADKDnR7iSli0nseD9WuVYQ9X0EseeKCywg54zw3\r\n"
"VFs2f6k4f6p8y3xXtbXRJSsyTygIitlaIOr5OwXSbftpxBDto22jpanO59yDBy00\r\n"
"Qfv6GB8rwUEWYwPUEWVGDLnsyHRK1JkUSKf10iwjV1LQ0gFWTFryO/Mu5NDgayBr\r\n"
"e9NUYQP/blh/RcMMR7QHhxw7Y7X0wu6cFZ3X2+Ef9E1/Zx1u07LQnbRV3EUQ1iyj\r\n"
"IIK5WaWLZ3wSG8Yp4I8rMO3VLVrve7qJ7W8cZ7rgU4Zb+IkRZZj0OsCtSUkg/vRN\r\n"
"P7oAWsJpHKYtwCzlXAZ8XWiTsndeQr2gSU3wRx+knTRkflwiKCLJXa2ILnXqAY6V\r\n"
"FwVlzc0EIQNWDs7CPYljL+iWcCa2dQZv7yFE8HInmCOfVGuZv/M+Ufb4ACPzgp3K\r\n"
"hMqc1tiZd+cwhhbscZXakPjLF1kWFo5NJGLtWuIGozUW4gs/pQ==\r\n"
"=V6YV\r\n"
"-----END PGP MESSAGE-----\r\n";
PGPError TestExpiredKey (PGPContextRef context )
{
PGPError err = kPGPError_NoErr;
PGPKeyDBRef keyDB = kInvalidPGPKeyDBRef;
PGPKeySetRef keyset = kInvalidPGPKeySetRef;
PGPKeyDBObjRef encryptKey = kInvalidPGPKeyDBObjRef;
PGPKeyDBObjRef theKey = kInvalidPGPKeyDBObjRef;
PGPKeyDBObjRef subKey = kInvalidPGPKeyDBObjRef;
PGPKeyIterRef iter = kInvalidPGPKeyIterRef;
int i;
char buffer[256];
PGPSize bufSize;
PGPTime createTime,expTime;
PGPUInt16 year, month, day;
PGPInt32 algorithm = 0;
PGPInt32 keysize = 0;
PGPKeyID theKeyID;
PGPKeyID encryptKeyID;
DecodeInfo decodeInfo;
InitDecodeInfo(&decodeInfo);
/* read in sample keys from file */
err = importKeys(context,gTestKeysPath, kPGPInputFormat_PGP, &keyDB); CKERR;
err = PGPNewKeySet( keyDB, &keyset );CKERR;
err = PGPNewKeyIterFromKeyDB(keyDB,&iter); CKERR;
/* We must perform the KeyRing Sig check for expiration dates to work */
err = PGPCheckKeyRingSigs(keyset,keyDB, TRUE, NULL,NULL );CKERR;
/* Find test Key */
err = PGPNewKeyIDFromString( kAlicesKeyIDString, kPGPPublicKeyAlgorithm_DSA, &theKeyID); CKERR;
err = PGPFindKeyByKeyID( keyDB, &theKeyID, &theKey); CKERR;
COPY( &theKeyID, &decodeInfo.key[0].keyID, sizeof(PGPKeyID));
decodeInfo.key[0].passPhrase = kAlicesPassPhrase;
decodeInfo.keyCount = 1;
err = PGPGetKeyDBObjNumericProperty(theKey, kPGPKeyProperty_AlgorithmID, &algorithm); CKERR;
err = PGPGetKeyDBObjNumericProperty(theKey, kPGPKeyProperty_Bits, &keysize); CKERR;
/* Get key date */
err = PGPGetKeyDBObjTimeProperty( theKey, kPGPKeyProperty_Creation, &createTime); CKERR;
PGPGetYMDFromPGPTime(createTime, &year, &month, &day);
/* get the primary key ID (signing key) name */
PGPGetKeyID( theKey, &theKeyID );
PGPGetKeyIDString( &theKeyID, kPGPKeyIDString_Full, buffer);
OPTESTPrintF("\tSigning KeyID %s (%s,%d)",buffer, key_algor_table(algorithm),keysize );
err = PGPGetPrimaryUserIDName(theKey, buffer, sizeof(buffer), &bufSize);
OPTESTPrintF("\"%s\"\n",buffer);
OPTESTPrintF("\tCreated: %02d/%02d/%04d Expires: ", month,day,year);
/* Get expiry date */
err = PGPGetKeyDBObjTimeProperty( theKey, kPGPKeyProperty_Expiration, &expTime); CKERR;
if(expTime == kPGPExpirationTime_Never)
OPTESTPrintF("Never\n");
else
{
PGPGetYMDFromPGPTime(expTime, &year, &month, &day);
OPTESTPrintF("%02d/%02d/%04d %s\n", month,day,year, (expTime <= PGPGetTime()?"Expired":"" ));
}
/* Test if passphrase is correct */
OPTESTPrintF("\tTesting passphrase...");
if(!PGPPassphraseIsValid(theKey,
PGPOPassphrase( context, kAlicesPassPhrase ),
PGPOLastOption( context )))
{
err = kPGPError_BadPassphrase;
goto done;
}
OPTESTPrintF("OK\n");
PGPGetKeyForUsage( theKey, kPGPKeyPropertyFlags_UsageEncryptCommunications, &encryptKey);
if(encryptKey != kInvalidPGPKeyDBObjRef)
PGPGetKeyID( encryptKey, &encryptKeyID );
OPTESTPrintF("\n\tEnumerate the subkeys\n");
for( i = 1, PGPKeyIterSeek(iter, theKey);
IsntPGPError( PGPKeyIterNextKeyDBObj( iter, kPGPKeyDBObjType_SubKey, &subKey) );
i++ )
{
err = PGPGetKeyID( subKey, &theKeyID ); CKERR;
PGPGetKeyIDString( &theKeyID, kPGPKeyIDString_Abbreviated, buffer);
OPTESTPrintF("\t\t%d %s ",i, buffer);
err = PGPGetKeyDBObjTimeProperty( subKey, kPGPKeyProperty_Creation, &createTime); CKERR;
PGPGetYMDFromPGPTime(createTime, &year, &month, &day);
OPTESTPrintF("%02d/%02d/%04d - ", month,day,year);
err = PGPGetKeyDBObjTimeProperty( subKey, kPGPKeyProperty_Expiration, &expTime); CKERR;
if(expTime == kPGPExpirationTime_Never)
OPTESTPrintF("%-10s", "Never");
else
{
PGPGetYMDFromPGPTime(expTime, &year, &month, &day);
OPTESTPrintF("%02d/%02d/%04d", month,day,year);
if(expTime <= PGPGetTime())
OPTESTPrintF(" Expired");
}
if( PGPCompareKeyIDs(&encryptKeyID, &theKeyID) == 0)
OPTESTPrintF(" <- Selected as Encryption Key");
OPTESTPrintF("\n");
}
if(encryptKey == kInvalidPGPKeyDBObjRef)
{
OPTESTPrintF("No valid key for encryption found\n");
}
OPTESTPrintF("\n\tDecoding prebuilt message encrypted with expired key\n");
err = PGPDecode( context,
PGPOInputBuffer ( context, encryptedMessage, sizeof(encryptedMessage) ),
PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
PGPOKeyDBRef(context, keyDB),
PGPOLastOption ( context ) ); CKERR;
done:
/* Cleanup this mess */
CleanUpDecodeInfo(&decodeInfo);
if( PGPKeyIterRefIsValid( iter ) )
PGPFreeKeyIter( iter );
if( PGPKeySetRefIsValid (keyset) )
PGPFreeKeySet(keyset);
if( PGPKeyDBRefIsValid( keyDB ) )
PGPFreeKeyDB( keyDB );
return err;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -