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

📄 optestutils.c

📁 PGP SDK 包括大范围的标准加密、数字签名和编解码技术
💻 C
📖 第 1 页 / 共 4 页
字号:
		sprintf(tempBuf, "<Unknown>");
	}
	
	OPTESTPrintF("%s%s %6s ",(header?header:""),tempBuf, key_algor_table(algorithm));
	getFlagString(&keyflags, tempBuf);
	OPTESTPrintF("%s ",tempBuf);
	
	if(VALUE(kFlag_X_509))
	{
		void* certInfo = NULL;
 		PGPGetKeyDBObjAllocatedDataProperty(sig, kPGPSigProperty_X509IssuerLongName, &certInfo, &length);
		if(length)
		{
			OPTESTPrintF("\"%s\" ", (char*)certInfo) ;
			PGPFreeData(certInfo);
		}
	}
	
 	err = PGPGetKeyDBObjTimeProperty( sig, kPGPSigProperty_Creation,  &createTime); CKERR;
	PGPGetYMDFromPGPTime(createTime, &year, &month, &day);
	OPTESTPrintF("%02d/%02d/%04d - ", month,day,year); 

	err = PGPGetKeyDBObjTimeProperty( sig, kPGPSigProperty_Expiration,  &expireTime); CKERR;
	if(expireTime == kPGPExpirationTime_Never)
	{
		OPTESTPrintF( "%-10s ", "Never ");
	}
	else 
	{
		PGPGetYMDFromPGPTime(expireTime, &year, &month, &day);
		OPTESTPrintF( "%02d/%02d/%04d ", month,day,year);	
	}
 
	 
 
done:

	printf("\n");
	return err;
}


PGPError printKeyInfo( char *header, PGPKeyDBObjRef theKey)
{
	PGPError		err			= kPGPError_NoErr;
	PGPKeyDBObjRef	useKey 	= kInvalidPGPKeyDBObjRef;
	char 		buffer[256];
	char 		props[8];
	PGPKeyID 	keyID;
	PGPSize		bufSize;
	PGPTime		theTime;
	PGPUInt16	year, month, day;
	PGPInt32	algorithm	= 0;
	PGPInt32	version		= 0;
	PGPInt32	bits		= 0;
 
 	/* get the key creation date */
	err = PGPGetKeyDBObjTimeProperty( theKey, kPGPKeyProperty_Creation,  &theTime); CKERR;
	PGPGetYMDFromPGPTime(theTime, &year, &month, &day);

	PGPGetKeyDBObjNumericProperty(theKey, kPGPKeyProperty_AlgorithmID, &algorithm);
	PGPGetKeyDBObjNumericProperty(theKey, kPGPKeyProperty_Version,  &version);
	PGPGetKeyDBObjNumericProperty(theKey, kPGPKeyProperty_Bits,  &bits);
	
 	/* get the primary key ID (signing key) name */
	err = PGPGetPrimaryUserID(theKey, &useKey); CKERR;
	
	PGPGetKeyID(theKey, &keyID);
	PGPGetKeyIDString(&keyID, kPGPKeyIDString_Full, (void*)&buffer);
	OPTESTPrintF("%s%s", header,  buffer);

	err = PGPGetKeyDBObjDataProperty( useKey, kPGPUserIDProperty_Name,
							 buffer, sizeof(buffer), &bufSize); CKERR;
	getKeyUsageString(theKey, props);
	
	OPTESTPrintF(" %s %d %s %02d/%02d/%04d \"%s\"\n",
			key_algor_table(algorithm), bits,  props, month,day,year,buffer);
  done:

	return err;
}

PGPError printKeyDetails( char *header, PGPBoolean biometric, PGPKeyDBObjRef theKey)
{
	PGPError			err			= kPGPError_NoErr;
	char*				strBuf = NULL;
 
	err = GetAllocatedKeyInfoString(theKey, header,biometric, (void*)&strBuf, NULL); CKERR;
	OPTESTPrintF("%s",  strBuf); 
	FREE(strBuf);  

done:
	return err;
 }



typedef struct MemBuf
{
	void*		data;   
	size_t		used;
	size_t		alloc;
 } MemBuf;

static void	memBufInit(MemBuf *buf)
{
	buf->data = NULL;
	buf->used = 0;
	buf->alloc = 0;
}


PGPError  memBufPrintf(MemBuf *buf, const char *fmt, ...  )
{
#define ALLOC_QUANTUM 256

	PGPError			err 		= kPGPError_NoErr;
	va_list marker;
	char s[1024];
	int	len;
 
	va_start( marker, fmt );     
	len = vsprintf( s, fmt, marker );
	va_end( marker ); 
 
	if(!buf->data) 
	{
		buf->data = ALLOC(ALLOC_QUANTUM); CKNULL(buf->data);
		*((char*)buf->data) = '\0';
		buf->alloc = ALLOC_QUANTUM;
		
	}
	
	while(buf->used + len  >= buf->alloc)
	{   
		buf->data = REALLOC(buf->data, buf->alloc + ALLOC_QUANTUM); CKNULL(buf->data);
		buf->alloc += ALLOC_QUANTUM;
 	}

	strcat( buf->data, s);
	buf->used = strlen(buf->data);
 	
done:
	return err;
	
}



static PGPError getKeyProps(PGPKeyDBObjRef theKey, PGPKeyDBObjRef encryptKey, PGPKeyDBObjRef signKey,  MemBuf *outBuf)
{
	PGPError			err 		= kPGPError_NoErr;
	PGPKeyID			keyID;
 	char				tempBuf[256];
	PGPInt32			algorithm	= 0;
	PGPInt32			keysize		= 0;
  	
	PGPInt32			flags       = 0;
	PGPTime				createTime, expireTime;
	PGPUInt16			year, month, day;
 	FlagBits			keyflags;
  	
	SetupFlags(&keyflags);
	
	/* Get Key ID */
	PGPGetKeyID(theKey, &keyID);
	PGPGetKeyIDString(&keyID, kPGPKeyIDString_Abbreviated, (void*)tempBuf);
 
		/* Get Key Properties */
	err = PGPGetKeyDBObjNumericProperty(theKey, kPGPKeyProperty_AlgorithmID, &algorithm); CKERR;
	err = PGPGetKeyDBObjNumericProperty(theKey, kPGPKeyProperty_Bits, &keysize); CKERR;
 
	err = memBufPrintf(outBuf, "%s %7s/%4d ", tempBuf, key_algor_table(algorithm), keysize);CKERR;
   	
	/* get the key creation date */
	err = PGPGetKeyDBObjTimeProperty( theKey, kPGPKeyProperty_Creation,  &createTime); CKERR;
	PGPGetYMDFromPGPTime(createTime, &year, &month, &day);
	err = memBufPrintf(outBuf, "%02d/%02d/%04d ", month,day,year);CKERR;

	err = PGPGetKeyDBObjTimeProperty( theKey, kPGPKeyProperty_Expiration,  &expireTime); CKERR;
	if(expireTime == kPGPExpirationTime_Never)
	{
		err = memBufPrintf(outBuf, "%-9s ", "Never");CKERR;
	}
	else 
	{
		PGPGetYMDFromPGPTime(expireTime, &year, &month, &day);
		err = memBufPrintf(outBuf, "%02d/%02d/%04d", month,day,year);CKERR;
	}
		
 	
	ClearFlags(&keyflags);
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPKeyProperty_IsRevoked,	FLAG(kFlag_Revoked));		CKERR;
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPKeyProperty_IsExpired,	FLAG(kFlag_Expired));		CKERR;
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPKeyProperty_IsRevocable,	FLAG(kFlag_Revocable));		CKERR;
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPKeyProperty_HasThirdPartyRevocation,	FLAG(kFlag_ThirdPartyRevocation));	CKERR;
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPKeyProperty_IsOnToken,	FLAG(kFlag_Token));		CKERR;
 	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPKeyProperty_IsSecret,	FLAG(kFlag_Priv));		CKERR;
 	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPKeyProperty_IsNotCorrupt,	FLAG(kFlag_Corrupt));	CKERR;
	*FLAG(kFlag_Corrupt) = !*(FLAG(kFlag_Corrupt));
		
 	flags = 0;
	if(IsntPGPError( PGPGetKeyDBObjNumericProperty(theKey, kPGPKeyProperty_Flags, (PGPInt32*)&flags) ))
	{
		SET_FLAG(kFlag_SignUserIDs, flags &		kPGPKeyPropertyFlags_UsageSignUserIDs);
		SET_FLAG(kFlag_SignMessages, flags &	kPGPKeyPropertyFlags_UsageSignMessages);
		SET_FLAG(kFlag_EncryptComm, flags &		kPGPKeyPropertyFlags_UsageEncryptCommunications);
		SET_FLAG(kFlag_EncryptStorage, flags&	kPGPKeyPropertyFlags_UsageEncryptStorage);
	}
 
	getFlagString(&keyflags, tempBuf);
	err = memBufPrintf(outBuf, " %-12s ",tempBuf);CKERR;

done:
  
	return err;

}

static PGPError getSubKeyProps(PGPKeyDBObjRef theKey, PGPKeyDBObjRef encryptKey, PGPKeyDBObjRef signKey,  MemBuf *outBuf)
{
	PGPError			err 		= kPGPError_NoErr;
	PGPKeyID			keyID;
 	char				tempBuf[256];
	PGPInt32			algorithm	= 0;
	PGPInt32			keysize		= 0;
  	
	PGPInt32			flags       = 0;
	PGPTime				createTime, expireTime;
	PGPUInt16			year, month, day;
 	FlagBits			keyflags;
  	
	SetupFlags(&keyflags);
	
	/* Get Key ID */
	PGPGetKeyID(theKey, &keyID);
	PGPGetKeyIDString(&keyID, kPGPKeyIDString_Abbreviated, (void*)tempBuf);

	/* Get Key Properties */
	err = PGPGetKeyDBObjNumericProperty(theKey, kPGPSubKeyProperty_AlgorithmID, &algorithm); CKERR;
	err = PGPGetKeyDBObjNumericProperty(theKey, kPGPSubKeyProperty_Bits, &keysize); CKERR;
 
	err = memBufPrintf(outBuf, "%s %7s/%4d ", tempBuf, key_algor_table(algorithm), keysize);CKERR;
   	
	/* get the key creation date */
	err = PGPGetKeyDBObjTimeProperty( theKey, kPGPSubKeyProperty_Creation,  &createTime); CKERR;
	PGPGetYMDFromPGPTime(createTime, &year, &month, &day);
	err = memBufPrintf(outBuf, "%02d/%02d/%04d ", month,day,year);CKERR;

	err = PGPGetKeyDBObjTimeProperty( theKey, kPGPSubKeyProperty_Expiration,  &expireTime); CKERR;
	if(expireTime == kPGPExpirationTime_Never)
	{
		err = memBufPrintf(outBuf, "%-9s ", "Never");CKERR;
	}
	else 
	{
		PGPGetYMDFromPGPTime(expireTime, &year, &month, &day);
		err = memBufPrintf(outBuf, "%02d/%02d/%04d", month,day,year);CKERR;
	}
		
 	ClearFlags(&keyflags);
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPSubKeyProperty_IsRevoked,	FLAG(kFlag_Revoked));		CKERR;
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPSubKeyProperty_IsExpired,	FLAG(kFlag_Expired));		CKERR;
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPSubKeyProperty_IsRevocable,	FLAG(kFlag_Revocable));		CKERR;
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPSubKeyProperty_HasThirdPartyRevocation,	FLAG(kFlag_ThirdPartyRevocation));	CKERR;
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPSubKeyProperty_IsOnToken,	FLAG(kFlag_Token));		CKERR;
 	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPSubKeyProperty_IsX509Certificate,	FLAG(kFlag_X_509));		CKERR;
	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPSubKeyProperty_IsSecret,	FLAG(kFlag_Priv));		CKERR;
 	err = PGPGetKeyDBObjBooleanProperty(theKey, kPGPSubKeyProperty_IsNotCorrupt,	FLAG(kFlag_Corrupt));	CKERR;
	*FLAG(kFlag_Corrupt) = !*(FLAG(kFlag_Corrupt));
		
 	flags = 0;
	if(IsntPGPError( PGPGetKeyDBObjNumericProperty(theKey, kPGPSubKeyProperty_Flags, (PGPInt32*)&flags) ))
	{
		SET_FLAG(kFlag_SignUserIDs, flags &		kPGPKeyPropertyFlags_UsageSignUserIDs);
		SET_FLAG(kFlag_SignMessages, flags &	kPGPKeyPropertyFlags_UsageSignMessages);
		SET_FLAG(kFlag_EncryptComm, flags &		kPGPKeyPropertyFlags_UsageEncryptCommunications);
		SET_FLAG(kFlag_EncryptStorage, flags&	kPGPKeyPropertyFlags_UsageEncryptStorage);
	}
 
	getFlagString(&keyflags, tempBuf);
	err = memBufPrintf(outBuf, " %-12s ",tempBuf);CKERR;

done:
  
	return err;

}


PGPError GetAllocatedKeyInfoString(PGPKeyDBObjRef theKey,  char *header, PGPBoolean biometric,void **buffer, PGPSize *dataSize)
{
	PGPError			err 		= kPGPError_NoErr;
 	PGPKeyDBObjRef		encryptKey	= kInvalidPGPKeyDBObjRef;
 	PGPKeyDBObjRef		signKey		= kInvalidPGPKeyDBObjRef;
 	PGPKeyDBObjRef		priUserID	= kInvalidPGPKeyDBObjRef;
	PGPKeyDBObjRef		userID		= kInvalidPGPKeyDBObjRef;
	PGPKeyDBObjRef		subKey		= kInvalidPGPKeyDBObjRef;
	PGPKeyDBObjRef		sig			= kInvalidPGPKeyDBObjRef;
	PGPKeyDBObjRef		signerKey	= kInvalidPGPKeyDBObjRef;
	PGPKeySetRef		adkset		= kInvalidPGPKeySetRef;
	PGPKeySetRef		revokerset	= kInvalidPGPKeySetRef;
 	PGPKeyIterRef		iter	 	= kInvalidPGPKeyIterRef;
 	PGPKeyIterRef		revokeIter	= kInvalidPGPKeyIterRef;
	PGPKeyIterRef		adkIter	 	= kInvalidPGPKeyIterRef;

	MemBuf				outBuf;
 	FlagBits			keyflags;
	
 	char				tempBuf[256];
	PGPByte				propBuf[64];
 	PGPSize				length;

	PGPKeyID			mainKeyID;
	PGPKeyID			keyID;
	
	PGPInt32			algorithm	= 0;
  	PGPUInt32			count		= 0;
 	
 	PGPTime				createTime, expireTime;
	PGPUInt16			year, month, day;

	SetupFlags(&keyflags);
	
 	if(dataSize) *dataSize = 0;
	
 	memBufInit(&outBuf);
  	err = PGPNewKeyIterFromKeyDB(PGPPeekKeyDBObjKeyDB(theKey) , &iter); CKERR;
 	
	err = memBufPrintf(&outBuf, "\n%s --- Top Key ---\n",header  );CKERR;
	  
  	PGPGetKeyForUsage(theKey, kPGPKeyPropertyFlags_UsageEncrypt, &encryptKey);
	PGPGetKeyForUsage(theKey, kPGPKeyPropertyFlags_UsageSignMessages, &signKey);

	/* Get GetKey ID */
  	PGPGetKeyID(theKey, &mainKeyID);
 	err = memBufPrintf(&outBuf, "%*s", (int) strlen(header)," ");CKERR;

	/* default Key usage */
	err = memBufPrintf(&outBuf,"%s%s ", (theKey == encryptKey?"E":" "),  (theKey == signKey?"S":" ") );CKERR;
		
	err = getKeyProps(theKey, encryptKey, signKey, &outBuf); CKERR;
	err = memBufPrintf(&outBuf, "\n");CKERR;
  
  	/* Enumerate SubKeys   */
	for( PGPKeyIterSeek( iter, theKey ), count = 0;
		 IsntPGPError( PGPKeyIterNextKeyDBObj(iter, kPGPKeyDBObjType_SubKey, &subKey )); count++)
  	{
 		if(count == 0)
		{
			err = memBufPrintf(&outBuf, "\n%*s ---- Sub Keys ----\n", (int) strlen(header)," ");CKERR;
		}

		err = memBufPrintf(&outBuf, "%*s", (int) strlen(header)," ");CKERR;
		/* default Key usage */
		err = memBufPrintf(&outBuf,"%s%s ", (subKey == encryptKey?"E":" "),  (subKey == signKey?"S":" ") );CKERR;
		
		err = getSubKeyProps(subKey,encryptKey, signKey,  &outBuf); CKERR;
		err = memBufPrintf(&outBuf, "\n");CKERR;
 	}
  
 
	/* get any revokers */
	 err = PGPNewEmptyInclusiveKeySet (PGPPeekKeyDBObjKeyDB(theKey) , &revokerset); CKERR;
	 
 	if( IsntPGPError(PGPGetRevocationKeys(theKey, revokerset))
		&& IsntPGPError(PGPCountKeys(revokerset, &count))
		&& count > 0)
	{
		PGPKeyDBObjRef	revokeKey = kInvalidPGPKeyDBObjRef;
		
		/* sanity check */
		err = PGPCountRevocationKeys(theKey, &count); ;CKERR;
		if(!count) FAIL("PGPCountRevocationKeys Failed");
		
		err = PGPNewKeyIterFromKeySet( revokerset ,&revokeIter); CKERR
 		err = memBufPrintf(&outBuf, "\n%*s ---- Revokation Keys ----\n", (int) strlen(header)," ");CKERR;
	
		while(IsntPGPError( PGPKeyIterNextKeyDBObj( revokeIter, kPGPKeyDBObjType_Key, &revokeKey) ))
		{
			PGPKeyID revokeKeyID;
			
 		  	PGPGetKeyID(revokeKey, &revokeKeyID);
 			PGPGetKeyIDString( &revokeKeyID, kPGPKeyIDString_Abbreviated, (void*)tempBuf);
			err = memBufPrintf(&outBuf, "%*s%s", (int) strlen(header)," ", tempBuf);CKERR;
 			err = PGPGetPrimaryUserIDName(revokeKey, tempBuf, sizeof(tempBuf), &length); CKERR;
			err = memBufPrintf(&outBuf, " %s\n",tempBuf );CKERR;
 	  	}
 	}

 	
	/* Get the primary user ID */
	err = PGPGetPrimaryUserID (theKey, &priUserID); CKERR;
 
	err = memBufPrintf(&outBuf, "\n%*s --- Users ---\n", (int) strlen(header)," ");CKERR;
	
	/* Enumerate User Names  */
 	for( PGPKeyIterSeek( iter, theKey ), count = 0 ;
		 IsntPGPError( PGPKeyIterNextKeyDBObj(iter, kPGPKeyDBObjType_UserID, &userID )); count++)
	{
		PGPBoolean	isAttributeID;

		err =  PGPGetKeyDBObjBooleanProperty( userID, kPGPUserIDProperty_IsAttribute, &isAttributeID ); CKERR;
		
		if(!isAttributeID)
		{
 			err = memBufPrintf(&outBuf, "%s%*s%s",(count?"\n":""), (int) strlen(header) -2," ", (userID == priUserID ?"->":"  ") );
 			PGPGetKeyDBObjDataProperty( userID, kPGPUserIDProperty_CommonName, tempBuf, sizeof(tempBuf), &length); 
			if(length)
			{
				err = memBufPrintf(&outBuf, "\"%s\" ",tempBuf); CKERR;
			}
			
			PGPGetKeyDBObjDataProperty( userID, kPGPUserIDProperty_EmailAddress, tempBuf, sizeof(tempBuf), &length); 
			if(length) 
			{	
				err = memBufPrintf(&outBuf, "<%s> ",tempBuf);CKERR;
			}
			err = memBufPrintf(&outBuf, " \n");CKERR;			
		}
			
		/* Enumerate Certs per user  */
		while( IsntPGPError( PGPKeyIterNextKeyDBObj(iter, kPGPKeyDBObjType_Signature, &sig ))) 
		{
			PGPBoolean selfCert = FALSE;
			PGPBoolean hasKeyID  = TRUE;
			
 			err = memBufPrintf(&outBuf, "%*s   ", (int) strlen(header)," " );CKERR;

			/* get cert info */
			ClearFlags(&keyflags);
			err = PGPGetKeyDBObjBooleanProperty(sig, kPGPSigProperty_IsX509,		FLAG(kFlag_X_509));		CKERR;
			err = PGPGetKeyDBObjBooleanProperty(sig, kPGPSigProperty_IsRevoked,		FLAG(kFlag_Revoked));	CKERR;
			err = PGPGetKeyDBObjBooleanProperty(sig, kPGPSigProperty_IsExpired,		FLAG(kFlag_Expired));	CKERR;
			err = PGPGetKeyDBObjBooleanProperty(sig, kPGPSigProperty_IsNotCorrupt,	FLAG(kFlag_Corrupt));	CKERR;
			*FLAG(kFlag_Corrupt) = !*(FLAG(kFlag_Corrupt));
 			err = PGPGetKeyDBObjNumericProperty(sig, kPGPSigProperty_AlgorithmID,	&algorithm);		CKERR;
 	
			err = PGPGetKeyDBObjDataProperty(sig, kPGPSigProperty_KeyID, &keyID, sizeof(keyID), &length);  
			if(IsntPGPError(err))
			{
				PGPGetKeyIDString(&keyID, kPGPKeyIDString_Abbreviated, (void*)tempBuf);
			}
			else
			{
				sprintf(tempBuf, "<Unknown>");
				hasKeyID = FALSE;
 			}
			err = memBufPrintf(&outBuf, "%s %8s ",tempBuf,key_algor_table(algorithm) );CKERR;
				
			getFlagString(&keyflags, tempBuf);
			err = memBufPrintf(&outBuf, "%s ",tempBuf);CKERR;

			/* get the sig creation date */
			err = PGPGetKeyDBObjTimeProperty( sig, kPGPSigProperty_Creation,  &createTime); CKERR;
			PGPGetYMDFromPGPTime(createTime, &year, &month, &day);
			err = memBufPrintf(&outBuf, "%02d/%02d/%04d - ", month,day,year);CKERR;

			err = PGPGetKeyDBObjTimeProperty( sig, kPGPSigProperty_Expiration,  &expireTime); CKERR;
			if(expireTime == kPGPExpirationTime_Never)
			{
				err = memBufPrintf(&outBuf, "%-10s ", "Never ");CKERR;
			}
			else 
			{
				PGPGetYMDFromPGPTime(expireTime, &year, &month, &day);
				err = memBufPrintf(&outBuf, "%02d/%02d/%04d ", month,day,year);CKERR;
			}
		
			if(hasKeyID)
			{
				selfCert = (PGPCompareKeyIDs(&mainKeyID,&keyID) == 0);
				
				if(selfCert)
				{
					err = memBufPrintf(&outBuf, "< SELF CERT >") ;
				}
				else
				{
					if( IsntPGPError( PGPFindKeyByKeyID( PGPPeekKeyDBObjKeyDB(theKey) , &keyID, &signerKey)))
					{
						
						err = PGPGetPrimaryUserIDName(signerKey, tempBuf, sizeof(tempBuf), &length); CKERR;

⌨️ 快捷键说明

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