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

📄 pgpkeyback.c

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 C
📖 第 1 页 / 共 4 页
字号:
/*____________________________________________________________________________
        Copyright (C) 2002 PGP Corporation
        All rights reserved.

		Back end functions called from front end
		
        $Id: pgpKeyBack.c,v 1.60 2002/08/06 20:11:00 dallen Exp $
____________________________________________________________________________*/
#include "pgpConfig.h"
#include <string.h>

#include "pgpDebug.h"
#include "pgpKeyPriv.h"
#include "pgpErrors.h"
#include "pgpPassCach.h"
#include "pgpPubKey.h"
#include "pgpPktByte.h"
#include "pgpSigSpec.h"
#include "pgpKeySpec.h"
#include "pgpMem.h"
#include "pgpContext.h"
#include "pgpEnv.h"
#include "pgpTrustPriv.h"
#include "pgpX509Priv.h"
#include "pgpHashPriv.h"
#include "pgpRegExp.h"
#include "pgpRandomPoolPriv.h"
#include "pgpRndSeed.h"

#ifndef NULL
#define NULL 0
#endif

extern PGPByte sP11Module[255];

	PGPError
pgpFetchObjectData_back( PGPContextRef context, PGPUInt32 id,
	PGPByte **bufptr, PGPSize *buflen )
{
	PGPKeyDBObj *obj;
	PGPByte const *data;
	PGPByte *buf;
	PGPSize len;

	if (pgpRPCEnabled())
		return pgpFetchObjectData_backRPC(context, id, bufptr, buflen);
	obj = (PGPKeyDBObj *)id;
	pgpAssert( pgpKeyDBObjIsValid( obj ) );
	data = pgpFetchObject( obj, &len );
	buf = pgpContextMemAlloc( context, len, 0 );
	if( IsNull( buf ) )
		return kPGPError_OutOfMemory;
	pgpCopyMemory( data, buf, len );
	*bufptr = buf;
	*buflen = len;
	return kPGPError_NoErr;
}


	PGPError 
pgpGetKeyByKeyID_back(
	PGPContextRef			context,
	PGPUInt32				dbid,
	PGPKeyID const *		keyIDIn,
	PGPBoolean				dummyOK,
	PGPBoolean				deletedOK,
	PGPUInt32 *				outID )
{
	PGPKeyDB *db;
	PGPKeyDBObj *obj;
	PGPError err;

	if (pgpRPCEnabled())
		return pgpGetKeyByKeyID_backRPC(context, dbid, keyIDIn, dummyOK, deletedOK, outID);
	*outID = 0;
	db = (PGPKeyDB *)dbid;
	pgpAssert( pgpKeyDBIsValid( db ) );
	err = pgpGetKeyByKeyID_internal( db, keyIDIn, dummyOK, deletedOK, &obj );
	if( IsPGPError( err ) )
		return err;
	*outID = (PGPUInt32) obj;
	return kPGPError_NoErr;
}

	PGPError
pgpKeyEncrypt_back( PGPContextRef context, PGPUInt32 id, PGPByte const *inbuf,
	PGPSize inbuflen, PGPPublicKeyMessageFormat format,
	PGPByte **outbuf, PGPSize *outbuflen )
{
	PGPKeyDBObj *	key;

	if (pgpRPCEnabled())
		return pgpKeyEncrypt_backRPC(context, id, inbuf, inbuflen, format, outbuf, outbuflen);
	key = (PGPKeyDBObj *)id;
	pgpAssert( pgpKeyDBObjIsValid( key ) );
	return pgpKeyEncrypt_internal( key, inbuf, inbuflen, format,
								   outbuf, outbuflen );
}

	PGPError
pgpKeyDecrypt_back( PGPContextRef context, PGPUInt32 id,
	PGPByte const *passphrase, PGPSize pplen, PGPBoolean hashedPhrase,
	PGPUInt32 cacheTimeOut, PGPBoolean cacheGlobal,
	PGPByte const *inbuf, PGPSize inbuflen, PGPPublicKeyMessageFormat format,
	PGPByte **outbuf, PGPSize *outbuflen )
{
	PGPKeyDBObj *	key;

	if (pgpRPCEnabled())
		return pgpKeyDecrypt_backRPC(context, id, passphrase, pplen,
								hashedPhrase, cacheTimeOut, cacheGlobal,
								inbuf, inbuflen, format, outbuf, outbuflen);
	key = (PGPKeyDBObj *)id;
	pgpAssert( pgpKeyDBObjIsValid( key ) );
	return pgpKeyDecrypt_internal( key, passphrase, pplen, hashedPhrase,
								   cacheTimeOut, cacheGlobal, inbuf,
								   inbuflen, format, outbuf, outbuflen );
}


	PGPInt32
pgpKeyVerify_back( PGPContextRef context, PGPUInt32 id, PGPByte const *inbuf,
	PGPSize inbuflen, PGPHashAlgorithm hashalg, PGPByte const *hash,
	PGPSize hashlen, PGPPublicKeyMessageFormat format )
{
	PGPKeyDBObj *	key;

	if (pgpRPCEnabled())
		return pgpKeyVerify_backRPC(context, id, inbuf, inbuflen, hashalg, hash, hashlen, format);
	key = (PGPKeyDBObj *)id;
	pgpAssert( pgpKeyDBObjIsValid( key ) );
	return pgpKeyVerify_internal( key, inbuf, inbuflen, hashalg, hash,
								  hashlen, format );
}

	PGPError
pgpKeySign_back( PGPContextRef context, PGPUInt32 id,
	PGPByte const *passphrase, PGPSize pplen, PGPBoolean hashedPhrase,
	PGPUInt32 cacheTimeOut, PGPBoolean cacheGlobal,
	PGPHashAlgorithm hashalg, PGPByte const *hash, PGPSize hashlen,
	PGPPublicKeyMessageFormat format, PGPByte **outbuf, PGPSize *outbuflen )
{
	PGPKeyDBObj *	key;

	if (pgpRPCEnabled())
		return pgpKeySign_backRPC(context, id, passphrase, pplen, hashedPhrase,
					cacheTimeOut, cacheGlobal, hashalg, hash, hashlen, format,
					outbuf, outbuflen);
	key = (PGPKeyDBObj *)id;
	pgpAssert( pgpKeyDBObjIsValid( key ) );
	return pgpKeySign_internal( key, passphrase, pplen, hashedPhrase,
								cacheTimeOut, cacheGlobal, hashalg,
								hash, hashlen, format, outbuf, outbuflen );
}


	PGPBoolean
pgpSecPassphraseOK_back( PGPContextRef context, PGPUInt32 id,
	PGPByte const *passphrase, PGPSize pplen, PGPBoolean hashedPhrase,
	PGPUInt32 cacheTimeOut, PGPBoolean cacheGlobal)
{
	PGPKeyDBObj *	key;

	if (pgpRPCEnabled())
		return pgpSecPassphraseOK_backRPC( context, id, passphrase, pplen,
						hashedPhrase, cacheTimeOut, cacheGlobal );
	key = (PGPKeyDBObj *)id;
	pgpAssert( pgpKeyDBObjIsValid( key ) );
	return pgpSecPassphraseOK_internal( key, passphrase, pplen, hashedPhrase,
										cacheTimeOut, cacheGlobal );
}

	PGPError
pgpKeyMaxSizes_back( PGPContextRef context, PGPUInt32 id,
	PGPUInt32 *maxEncryption, PGPUInt32 *maxDecryption,
	PGPUInt32 *maxSignature, PGPPublicKeyMessageFormat format )
{
	PGPKeyDBObj *	key;

	if (pgpRPCEnabled())
		return pgpKeyMaxSizes_backRPC( context, id, maxEncryption,
					maxDecryption, maxSignature, format);
	key = (PGPKeyDBObj *)id;
	pgpAssert( pgpKeyDBObjIsValid( key ) );
	return pgpKeyMaxSizes_internal( key, maxEncryption, maxDecryption,
									maxSignature, format );
}

	PGPError
pgpSecProperties_back( PGPContextRef context, PGPUInt32 id,
	PGPBoolean *needsPassphrase, PGPBoolean *isSecretShared,
	PGPCipherAlgorithm *lockAlg, PGPUInt32 *lockBits )
{
	PGPKeyDBObj *	key;

	if (pgpRPCEnabled())
		return pgpSecProperties_backRPC( context, id, needsPassphrase, isSecretShared, lockAlg, lockBits );
	key = (PGPKeyDBObj *)id;
	pgpAssert( pgpKeyDBObjIsValid( key ) );
	return pgpSecProperties_internal( key, needsPassphrase, isSecretShared,
									  lockAlg, lockBits );
}



	PGPError
pgpFetchKeyInfo_back( PGPContextRef context, PGPUInt32 id,
	PGPByte **bufptr, PGPSize *bufsize )
{
	PGPKeyDBObj *			obj;
	PGPKeyDBObjInfo *		info;
	PGPSize					infosize = 0;
	PGPByte *				buf;
	PGPUInt32				objtype;

	if (pgpRPCEnabled())
		return pgpFetchKeyInfo_backRPC( context, id, bufptr, bufsize );

	obj = (PGPKeyDBObj *)id;
	pgpAssert( pgpKeyDBObjIsValid( obj ) );
	info = obj->idinfo.info;
	objtype = pgpObjectType( obj );
	switch ( objtype )
	{
	case RINGTYPE_KEY:
	case RINGTYPE_SUBKEY:
		infosize = sizeof (PGPKeyInfo);
		break;
	case RINGTYPE_USERID:
		infosize = sizeof (PGPUserIDInfo);
		break;
	case RINGTYPE_SIG:
		infosize = sizeof (PGPSigInfo);
		break;
	case RINGTYPE_CRL:
		infosize = sizeof (PGPCRLInfo);
		break;
	default:
		infosize = sizeof (PGPUnkInfo);
		break;
	}
	buf = pgpContextMemAlloc( context, infosize, 0 );
	if( IsNull( buf ) )
		return kPGPError_OutOfMemory;
	pgpCopyMemory( (PGPByte *)info, buf, infosize );
	*bufptr = buf;
	*bufsize = infosize;
	return kPGPError_NoErr;
}


/*
 * Create a key array holding the keys and their children.
 * If set is specified, restrict to members of that set.  Use the
 * isMember function directly rather than calling pgpKeySetIsMember because
 * that eliminates deleted objects, and we need to return them here so that
 * the front end is guaranteed to be synched with us.
 */
	static PGPError
pgpCreateKeyArray( PGPContextRef context, PGPKeyDBObj *key, PGPUInt32 nkeys,
	PGPKeySetRef set, PGPUInt32 **objs, PGPSize *objsize )
{
	PGPKeyDBObj *		startkey;
	PGPKeyDBObj *		child;
	PGPKeyDBObj *		gchild;
	PGPUInt32			nentries;
	PGPUInt32			nchildren;
	PGPUInt32			i;
	PGPUInt32 *			buf;
	PGPUInt32 *			bp;

	startkey = key;
	nentries = 0;
	for( i=0; i<nkeys; ++i, key=key->next) 
	{
		pgpAssert( IsntNull( key ) );
		if( IsntNull( set ) && !set->isMember( set, key ) )
			continue;
		nentries += 3;
		for (child = key->down; IsntNull(child); child = child->next)
		{
			if( IsntNull( set ) && !set->isMember( set, child ) )
				continue;
			nentries += OBJISSIG(child) ? 2 : 3;
			for (gchild = child->down; IsntNull(gchild); gchild = gchild->next)
			{
				pgpAssert( OBJISSIG(gchild) );
				if( IsntNull( set ) && !set->isMember( set, gchild ) )
					continue;
				nentries += 2;
			}
		}
	}

	/* Allocate */
	
	buf = (PGPUInt32 *)pgpContextMemAlloc( context,
										   nentries * sizeof(PGPUInt32), 0 );
	if( IsNull( buf ) )
	{
		return kPGPError_OutOfMemory;
	}
	bp = buf;
	key = startkey;

	/* Flatten */
	
	for( i=0; i<nkeys; ++i, key=key->next)
	{
		if( IsntNull( set ) && !set->isMember( set, key ) )
			continue;
		*bp++ = (PGPUInt32) key;
		*bp++ = (PGPUInt32)key->objflags;
		nchildren = 0;
		for (child = key->down; IsntNull(child); child = child->next)
		{
			if( IsntNull( set ) && !set->isMember( set, child ) )
				continue;
			++nchildren;
		}
		*bp++ = nchildren;
		for (child = key->down; IsntNull(child); child = child->next)
		{
			if( IsntNull( set ) && !set->isMember( set, child ) )
				continue;
			*bp++ = (PGPUInt32) child;
			*bp++ = (PGPUInt32)child->objflags;
			if( OBJISSIG( child ) )
				continue;
			nchildren = 0;
			for (gchild = child->down; IsntNull(gchild); gchild = gchild->next)
			{
				if( IsntNull( set ) && !set->isMember( set, gchild ) )
					continue;
				++nchildren;
			}
			*bp++ = nchildren;
			for (gchild = child->down; IsntNull(gchild); gchild = gchild->next)
			{
				if( IsntNull( set ) && !set->isMember( set, gchild ) )
					continue;
				*bp++ = (PGPUInt32) gchild;
				*bp++ = (PGPUInt32)gchild->objflags;
			}
		}
	}

	*objs = buf;
	*objsize = nentries * sizeof(PGPUInt32);
	return kPGPError_NoErr;
}


	PGPError
pgpOpenKeyDBFile_back(
	PGPContextRef			context,
	PGPOpenKeyDBFileOptions	openFlags,
	PFLFileSpecRef			pubFileRef,
	PFLFileSpecRef			privFileRef,
	PGPUInt32 *				kdbid,
	PGPUInt32 *				numKeys,
	PGPUInt32 **			keyArray,
	PGPSize *				keyArraySize )
{
	PGPKeyDB *				kdb;
	PGPKeyDBObj *			key;
	PGPUInt32				nkeys;
	PGPError				err;

	if (pgpRPCEnabled())
		return pgpOpenKeyDBFile_backRPC( context, openFlags, pubFileRef, privFileRef,
					kdbid, numKeys, keyArray, keyArraySize );

	err = pgpOpenKeyDBFile_internal( context, openFlags, pubFileRef,
									 privFileRef, &kdb );

	if( IsPGPError( err ) )
		return err;

	/* Create keyarray for all the keys in the db (dummies too) */
	nkeys = 0;
	for (key = kdb->firstKeyInDB; IsntNull(key); key = key->next)
	{
		++nkeys;
	}
	err = pgpCreateKeyArray( context, kdb->firstKeyInDB, nkeys, NULL, keyArray,
							 keyArraySize );

	if( IsPGPError( err ) )
	{
		PGPFreeKeyDB( kdb );
		return err;
	}

	*kdbid = (PGPUInt32)kdb;
	*numKeys = nkeys;

	return kPGPError_NoErr;
}


	PGPError
pgpImportKeyBinary_back(
	PGPContextRef			context,
	const PGPByte *				buffer,
	PGPSize					length,
	PGPUInt32 *				kdbid,
	PGPUInt32 *				numKeys,
	PGPUInt32 **			keyArray,
	PGPSize *				keyArraySize )
{
	PGPKeyDB *				kdb;
	PGPKeyDBObj *			key;
	PGPUInt32				nkeys;
	PGPError				err;

	if (pgpRPCEnabled())
		return pgpImportKeyBinary_backRPC( context, buffer, length, kdbid,
							numKeys, keyArray, keyArraySize );

	err = pgpImportKeyBinary_internal( context, buffer, length, &kdb );

	if( IsPGPError( err ) )
		return err;

	/* Create keyarray for all the keys in the db (dummies too) */
	nkeys = 0;
	for (key = kdb->firstKeyInDB; IsntNull(key); key = key->next)
	{
		++nkeys;
	}
	err = pgpCreateKeyArray( context, kdb->firstKeyInDB, nkeys, NULL,
							 keyArray, keyArraySize );

	if( IsPGPError( err ) )
	{
		PGPFreeKeyDB( kdb );
		return err;
	}

	*kdbid = (PGPUInt32)kdb;
	*numKeys = nkeys;

	return kPGPError_NoErr;
}

	PGPError
pgpKeyDBArray_back(
	PGPContextRef			context,
	PGPUInt32				kdbid,
	PGPUInt32 *				numKeys,
	PGPUInt32 **			keyArray,
	PGPSize *				keyArraySize )
{
	PGPKeyDB *				kdb = (PGPKeyDB *)kdbid;
	PGPKeyDBObj *			key;
	PGPUInt32				nkeys;
	PGPError				err;

	if (pgpRPCEnabled())
		return pgpKeyDBArray_backRPC( context, kdbid, numKeys,
							keyArray, keyArraySize );

	pgpAssert( pgpKeyDBIsValid( kdb ) );

	/* Create keyarray for all the keys in the db (dummies too) */
	nkeys = 0;
	for (key = kdb->firstKeyInDB; IsntNull(key); key = key->next)
	{
		++nkeys;
	}
	err = pgpCreateKeyArray( context, kdb->firstKeyInDB, nkeys, NULL,
							 keyArray, keyArraySize );

⌨️ 快捷键说明

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