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

📄 x509test.c

📁 PGP SDK 包括大范围的标准加密、数字签名和编解码技术
💻 C
📖 第 1 页 / 共 3 页
字号:
/*____________________________________________________________________________
X509test.c

Copyright (C) 2005 PGP Corporation
All rights reserved.

X509test.c - This file contains functions which are used to verify
the  various X509 function of the PGPsdk.

$Id: X509test.c 48495 2006-10-12 21:30:17Z vinnie $
____________________________________________________________________________*/
 
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
 
#include "pgpEncode.h"
#include "pgpErrors.h"
#include "pgpKeys.h"
#include "pgpPublicKey.h"
#include "pgpHash.h"
#include "pgpUtilities.h"
 #include "optest.h"
  

static const PGPByte P12Certs[] =
{	0x30, 0x82, 0x05, 0x48, 0x02, 0x01, 0x03, 0x30, 0x82, 0x05, 0x02, 0x06, 0x09, 0x2a, 0x86, 0x48,
	0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82, 0x04, 0xf3, 0x04, 0x82, 0x04, 0xef, 0x30, 0x82,
	0x04, 0xeb, 0x30, 0x82, 0x04, 0xe7, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07,
	0x06, 0xa0, 0x82, 0x04, 0xd8, 0x30, 0x82, 0x04, 0xd4, 0x02, 0x01, 0x00, 0x30, 0x82, 0x04, 0xcd,
	0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x1c, 0x06, 0x0a, 0x2a,
	0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0e, 0x04, 0x08, 0x8b, 0x01, 0x27,
	0x57, 0xf2, 0x70, 0xe8, 0xf8, 0x02, 0x02, 0x07, 0xd0, 0x80, 0x82, 0x04, 0xa0, 0xaa, 0x11, 0x0c,
	0x00, 0x22, 0x3b, 0xca, 0x1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x7e, 0x3b, 0x67, 0x02, 0xa2, 0x54, 0x9b, 0x5f, 0xaf,
	0xb3, 0x2a, 0x51, 0x94, 0xd6, 0x11, 0xbf, 0xcb, 0x5d, 0x00, 0xe6, 0x98, 0xd7, 0x44, 0x5e, 0xe6,
	0x60, 0x83, 0xb7, 0x3a, 0xe4, 0x2b, 0xb1, 0xe2, 0x7c, 0xdb, 0xc4, 0x58, 0xf0, 0x44, 0x22, 0x57,
	0x0a, 0x90, 0xb4, 0x1c, 0xbc, 0x95, 0x40, 0x26, 0x59, 0x7e, 0x11, 0xae, 0xef, 0x42, 0x25, 0x09,
	0x25, 0x4a, 0xc0, 0x5d, 0x98, 0xcc, 0x7f, 0x31, 0xba, 0xf5, 0x7f, 0x8b, 0x4d, 0xc0, 0xc4, 0xd2,
	0xf0, 0x0b, 0xd8, 0x72, 0x35, 0xe6, 0x77, 0xd1, 0x0b, 0xee, 0x34, 0xfc, 0x5b, 0x30, 0x3d, 0x30,
	0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0xee, 0xd0,
	0xaf, 0x7a, 0x95, 0x24, 0x44, 0xfc, 0x15, 0x9e, 0xdc, 0xfc, 0x59, 0x24, 0x64, 0xab, 0x07, 0xdf,
	0x5a, 0x88, 0x04, 0x14, 0xd1, 0xe3, 0xc7, 0x24, 0xd6, 0xfc, 0x52, 0xda, 0xa3, 0x8c, 0x21, 0x35,
	0x59, 0x4d, 0x05, 0x8a, 0x9a, 0x16, 0x18, 0xd2, 0x02, 0x02, 0x07, 0xd0 };
 
static const char key1DER[] =
"-----BEGIN CERTIFICATE-----\r"
"MIIBmjCCAQOgAwIBAAIKek1zPkldltl2sjANBgkqhkiG9w0BAQUFADANMQswCQYD\r"
"VQQDDAJDQTAeFw0wNTA5MTYxNDQ4NDBaFw0wNzA5MTYxNDQ4NDBaMBAxDjAMBgNV\r"
"BAMMBXVzZXIxMIGfMA0GCSqGS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+bE1zcPqL3nATTW73Tp\r"
"-----END CERTIFICATE-----\r";

 
#define kCAKeyIDString		"0xA3379D0C"
#define kKey1KeyIDString	"0x83B4250B" 
#define kKey2KeyIDString	"0xAAC26E2E"



static PGPError  CountKnownCAs( PGPContextRef context, PGPBoolean detailed )
{
 	PGPError				err			= kPGPError_NoErr;
 	PGPKeyDBRef				knownCAs 	= kInvalidPGPKeyDBRef;
	PGPKeyIterRef			iter	 	= kInvalidPGPKeyIterRef;
	PGPKeyDBObjRef			theKey		= kInvalidPGPKeyDBObjRef;
 	PGPUInt32				numKeys;
	int						i;

	OPTESTPrintF("   Access List of Know CAs... ");
 	err = PGPGetKnownX509CAs(context, &knownCAs);
	
	err = PGPCountKeysInKeyDB(knownCAs, &numKeys); CKERR;
	OPTESTPrintF("found %d\n", numKeys );

 	/* make an iterator */
 	err = PGPNewKeyIterFromKeyDB( knownCAs, &iter); CKERR;

	/* Iterate through each key */
	if(detailed)
		for(i = 1;  IsntPGPError( PGPKeyIterNextKeyDBObj( iter, kPGPKeyDBObjType_Key, &theKey) ); i++)
		{
			PGPKeyDBObjRef	priUserID	= kInvalidPGPKeyDBObjRef;
			char			tempBuf[256];
			PGPSize			length;
			
			/* Get the primary user ID */
			err = PGPGetPrimaryUserID (theKey, &priUserID); CKERR;
			OPTESTPrintF( "     [%2d] ", i);
		
			PGPGetKeyDBObjDataProperty( priUserID, kPGPUserIDProperty_Name, tempBuf, sizeof(tempBuf), &length); 
			printTruncated(tempBuf, length, 64);
		}
	OPTESTPrintF("\n"); 
	
done:
  		
	if( PGPKeyIterRefIsValid( iter ) )
		PGPFreeKeyIter( iter );

	if( PGPKeyDBRefIsValid( knownCAs ) )
 		PGPFreeKeyDB( knownCAs );

	return err;
 
}

static const char MessageToBeSigned[] = 
"PGP uses symmetric keys of 128-bits and asymmetric keys of up to 4096-bits irrespective of the locale."
" Thus users of PGP can communicate securely globally whereas S/MIME users currently can't.";

static PGPError  sTestX509Key( PGPContextRef context, PGPKeyDBObjRef key, PGPKeyDBObjRef signingCert, const PGPChar8 *passphrase )
{
 	PGPError				err			= kPGPError_NoErr;
   	PGPKeyDBObjRef			useKey 	= kInvalidPGPKeyDBObjRef;
 	DecodeInfo				decodeInfo;
 	
/* Cipher Text */
	void *					cBuf		= NULL;
 	PGPSize					cBufSize;

/* Result Text */
	void *					dBuf		= NULL;
 	PGPSize					dBufSize;
		
	InitDecodeInfo(&decodeInfo);
 	OPTESTPrintF("\tEncrypt in S/MIME format\n");
	err = PGPGetKeyForUsage( key, kPGPKeyPropertyFlags_UsageEncryptCommunications,  &useKey); CKERR;
	err =  PGPEncode( context, 
			PGPOInputBuffer ( context, MessageToBeSigned, sizeof(MessageToBeSigned)-1 ),
 			PGPOAllocatedOutputBuffer( context, &cBuf, MAX_PGPSize, &cBufSize),
    		PGPOEncryptToKeyDBObj(context,useKey),
 			PGPOOutputFormat(context, kPGPOutputFormat_SMIMEBodyEncryptedData),
 			PGPOLastOption( context ) ); CKERR;

	OPTESTPrintF("\tEncoded %ld byte Message is %d bytes\n", sizeof(MessageToBeSigned)-1 , (int)cBufSize );
 	if(gVerbose_flag)
		 dumpHex( cBuf,cBufSize, 0  );

	OPTESTPrintF("\tDecoding & Comparing Message\n");
	InitDecodeInfo(&decodeInfo);
	PGPGetKeyID(key, &decodeInfo.key[0].keyID);
 	decodeInfo.key[0].passPhrase = passphrase;
 	decodeInfo.keyCount = 1;
 	
  	err = PGPDecode( context, 
				PGPOInputBuffer ( context, cBuf, cBufSize ),
				PGPOAllocatedOutputBuffer( context, &dBuf, MAX_PGPSize, &dBufSize),
				PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				PGPOSMIMEMatchCriterion(context,kPGPSMIMEMatchCriterion_Any ),
				PGPOInputFormat(context, kPGPOutputFormat_SMIMEBodyEncryptedData),
 				PGPOKeyDBRef(context, PGPPeekKeyDBObjKeyDB(key)),
 				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;

	OPTESTPrintF("\n\tSign in S/MIME format\n");
	err = PGPGetKeyForUsage( key, kPGPKeyPropertyFlags_UsageSignMessages,  &useKey); CKERR;
	err =  PGPEncode( context, 
			PGPOInputBuffer ( context, MessageToBeSigned, sizeof(MessageToBeSigned)-1 ),
 			PGPOAllocatedOutputBuffer( context, &cBuf, MAX_PGPSize, &cBufSize),
 			PGPOSignWithKey ( context, useKey,
								( signingCert ? PGPOSMIMESigner(context, signingCert) : PGPONullOption(context)),
								PGPOPassphrase( context, passphrase), 
								PGPOLastOption ( context ) ),
 			PGPOOutputFormat(context, kPGPOutputFormat_SMIMEBodySignedData),
 			PGPOLastOption( context ) ); CKERR;

	OPTESTPrintF("\tEncoded %ld byte Message is %d bytes\n", sizeof(MessageToBeSigned)-1 , (int)cBufSize );
	if(gVerbose_flag)
		 dumpHex( cBuf,cBufSize, 0  );

 	OPTESTPrintF("\tDecoding & Validating Message\n");
	InitDecodeInfo(&decodeInfo);
   	err = PGPDecode( context, 
				PGPOInputBuffer ( context, cBuf, cBufSize ),
				PGPOAllocatedOutputBuffer( context, &dBuf, MAX_PGPSize, &dBufSize),
				PGPOEventHandler( context, OptestEventHandler, &decodeInfo),
				PGPOInputFormat(context, kPGPOutputFormat_SMIMEBodySignedData),
 				PGPOKeyDBRef(context, PGPPeekKeyDBObjKeyDB(key)),
 				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 );

	
done:
  	CleanUpDecodeInfo(&decodeInfo);

 	if( dBuf != NULL)
		PGPFreeData(dBuf);

⌨️ 快捷键说明

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