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

📄 encryptsign.c

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

	$Id: EncryptSign.c,v 1.33 2002/11/20 19:17:36 sdas Exp $
____________________________________________________________________________*/
// System Headers
#include <windows.h> 

// PGPsdk Headers
#include "pgpDebug.h"
#include "pgpKeys.h"
#include "pgpUtilities.h"

// Shared Headers
#include "pgpVersionHeader.h"
#include "pgpClientLib.h"
#include "pgpLnLib.h"
#include "Working.h"
#include "Prefs.h"
#include "BlockUtils.h"
#include "EncryptSign.h"
#include "WorkingResource.h"
#include "SharedStrings.h"
#include "pgpWin32Errors.h"

#ifndef PGP_NOTES
#define PGP_NOTES 0
#endif

static int nProgressCount = 0;

static PGPError EncryptSign(HINSTANCE hInst, HWND hwnd, PGPContextRef context, 
							PGPtlsContextRef tlsContext, 
							char *szName, char *szModule,
							PGPclRecipientDialogStruct *prds,
							PGPOptionListRef ioOptions,
							PGPOptionListRef mimeOptions,
							PGPOptionListRef *pSignOptions,
							BOOL bEncrypt, BOOL bSign, BOOL bBinary, 
							BOOL bHTML);

PGPError EncodeEventHandler(PGPContextRef context, 
							PGPEvent *event, 
							PGPUserValue userValue);

static void DisplayErrorCode(char *szFile, 
							 int nLine, 
							 char *szModule, 
							 int nCode,
							 BOOL bKeyrings,
							 HWND hParentWnd);

BOOL WrapBuffer(char **pszOutText,
				char *szInText,
				PGPUInt16 wrapLength)
{
	BOOL RetVal = FALSE;
	PGPError err;
	char *cmdlgBuffer;

	err=PGPclWrapBuffer(
					szInText,
					wrapLength,
					&cmdlgBuffer);

	if(IsntPGPError (err))
	{
		int memamt,length;

		length=strlen(cmdlgBuffer);
		memamt=length+1;

		*pszOutText=(char *)malloc(memamt);
		memcpy(*pszOutText,cmdlgBuffer,length);
		(*pszOutText)[length]=0;
		PGPclFreeWrapBuffer(cmdlgBuffer);
		RetVal = TRUE;
	}

	return RetVal;
}


PGPError EncryptSignBuffer(HINSTANCE hInst, HWND hwnd, PGPContextRef context, 
						   PGPtlsContextRef tlsContext, 
						   char *szName, char *szModule,
						   void *pInput, DWORD dwInSize, 
						   PGPclRecipientDialogStruct *prds,
						   PGPOptionListRef mimeOptions,
						   PGPOptionListRef *pSignOptions,
						   void **ppOutput, PGPSize *pOutSize, BOOL bEncrypt, 
						   BOOL bSign, BOOL bBinary, BOOL bHTML)
{
	PGPError			err				= kPGPError_NoErr;
	PGPMemoryMgrRef		memoryMgr		= NULL;
	PGPOptionListRef	options			= NULL;
	void *				pFinalInput		= NULL;
	long				lWrapWidth		= 0;
	BOOL				bInputWrapped	= FALSE;

	pgpAssert(pInput != NULL);
	pgpAssert(prds != NULL);
	pgpAssert(prds->keydbOriginal != NULL);
	pgpAssert(pSignOptions != NULL);
	pgpAssert(ppOutput != NULL);
	pgpAssert(pOutSize != NULL);
	pgpAssert(context != NULL);

	memoryMgr = PGPPeekContextMemoryMgr(context);
	pFinalInput = pInput;

	if (!bBinary && !bHTML)
	{
		if (ByDefaultWordWrap(memoryMgr, &lWrapWidth))
		{
			char *szWrapped = NULL;
			char *szPtr = (char *) pInput;
			char cTemp;
			ULONG ulStart = 0;
			ULONG ulSize = (ULONG) dwInSize;
			ULONG ulLength = 0;

			pFinalInput = calloc(1, 1);

			bInputWrapped=TRUE;

			dwInSize = 0;
			do
			{
				if (FindSignedBlock(szPtr, ulSize, &ulStart, &ulLength))
				{
					cTemp = szPtr[ulStart];
					szPtr[ulStart] = 0;
					WrapBuffer(&szWrapped, szPtr, 
										(short) lWrapWidth);
					dwInSize += strlen(szWrapped) + ulLength;
					pFinalInput = realloc(pFinalInput, dwInSize+1);
					strcat(pFinalInput, szWrapped);
					free(szWrapped);
					szPtr[ulStart] = cTemp;
					cTemp = szPtr[ulStart+ulLength];
					szPtr[ulStart+ulLength] = 0;
					strcat(pFinalInput, &szPtr[ulStart]);
					szPtr[ulStart+ulLength] = cTemp;
					szPtr += ulStart+ulLength;
					ulSize -= (ulStart+ulLength);
				}
				else if (FindEncryptedBlock(szPtr, ulSize, &ulStart, &ulLength))
				{
					cTemp = szPtr[ulStart];
					szPtr[ulStart] = 0;
					WrapBuffer(&szWrapped, szPtr, 
										(short) lWrapWidth);
					dwInSize += strlen(szWrapped) + ulLength;
					pFinalInput = realloc(pFinalInput, dwInSize+1);
					strcat(pFinalInput, szWrapped);
					free(szWrapped);
					szPtr[ulStart] = cTemp;
					cTemp = szPtr[ulStart+ulLength];
					szPtr[ulStart+ulLength] = 0;
					strcat(pFinalInput, &szPtr[ulStart]);
					szPtr[ulStart+ulLength] = cTemp;
					szPtr += ulStart+ulLength;
					ulSize -= (ulStart+ulLength);
				}
				else if (FindPGPKeyBlock(szPtr, ulSize, &ulStart, &ulLength))
				{
					cTemp = szPtr[ulStart];
					szPtr[ulStart] = 0;
					WrapBuffer(&szWrapped, szPtr, 
										(short) lWrapWidth);
					dwInSize += strlen(szWrapped) + ulLength;
					pFinalInput = realloc(pFinalInput, dwInSize+1);
					strcat(pFinalInput, szWrapped);
					free(szWrapped);
					szPtr[ulStart] = cTemp;
					cTemp = szPtr[ulStart+ulLength];
					szPtr[ulStart+ulLength] = 0;
					strcat(pFinalInput, &szPtr[ulStart]);
					szPtr[ulStart+ulLength] = cTemp;
					szPtr += ulStart+ulLength;
					ulSize -= (ulStart+ulLength);
				}
				else
				{
					dwInSize += strlen(szPtr);
					pFinalInput = realloc(pFinalInput, dwInSize+1);
					strcat(pFinalInput, szPtr);
					ulLength = 0;
					dwInSize = strlen((char *) pFinalInput);
				}
			}
			while (ulLength > 0);
		}

		if (!dwInSize)
		{
			if (bInputWrapped)
				free(pFinalInput);

			bInputWrapped = FALSE;
			pFinalInput = "\r\n";
			dwInSize = strlen(pFinalInput);
		}
	}

	err = PGPBuildOptionList(context, &options, 
			PGPOInputBuffer(context, pFinalInput, dwInSize),
			PGPOAllocatedOutputBuffer(context, 
				ppOutput, 
				INT_MAX, 
				pOutSize),
			PGPOLastOption(context));

	if (IsPGPError(err))
	{
		DisplayErrorCode(__FILE__, __LINE__, szModule, err, FALSE, hwnd);
		goto EncryptSignBufferError;
	}

	err = EncryptSign(hInst, hwnd, context, tlsContext, szName, szModule, 
			prds, options, mimeOptions, pSignOptions, bEncrypt, bSign, 
			bBinary, bHTML);

EncryptSignBufferError:

	if (bInputWrapped)
	{
		memset(pFinalInput,0x00,dwInSize);
		free(pFinalInput);
	}

	if (options != NULL)
		PGPFreeOptionList(options);

	return err;
}


PGPError EncryptSignFile(HINSTANCE hInst, HWND hwnd, PGPContextRef context, 
						 PGPtlsContextRef tlsContext, 
						 char *szName, char *szModule, char *szInFile, 
						 PGPclRecipientDialogStruct *prds, 
						 PGPOptionListRef mimeOptions,
						 PGPOptionListRef *pSignOptions, char *szOutFile, 
						 BOOL bEncrypt, BOOL bSign, BOOL bBinary)
{
	PGPError			err				= kPGPError_NoErr;
	PGPOptionListRef	options			= NULL;
	PGPFileSpecRef		inputFile		= NULL;
	PGPFileSpecRef		outputFile		= NULL;

	pgpAssert(szInFile != NULL);
	pgpAssert(prds != NULL);
	pgpAssert(prds->keydbOriginal != NULL);
	pgpAssert(pSignOptions != NULL);
	pgpAssert(szOutFile != NULL);
	pgpAssert(context != NULL);

	err = PGPNewFileSpecFromFullPath(context, szInFile, &inputFile);
	if (IsPGPError(err))
	{
		DisplayErrorCode(__FILE__, __LINE__, szModule, err, FALSE, hwnd);
		goto EncryptSignFileError;
	}

	err = PGPNewFileSpecFromFullPath(context, szOutFile, &outputFile);
	if (IsPGPError(err))
	{
		DisplayErrorCode(__FILE__, __LINE__, szModule, err, FALSE, hwnd);
		goto EncryptSignFileError;
	}

	err = PGPBuildOptionList(context, &options, 
			PGPOInputFile(context, inputFile),
			PGPOOutputFile(context, outputFile),
			PGPOLastOption(context));

	if (IsPGPError(err))
	{
		DisplayErrorCode(__FILE__, __LINE__, szModule, err, FALSE, hwnd);
		goto EncryptSignFileError;
	}

	err = EncryptSign(hInst, hwnd, context, tlsContext, szName, szModule, 
			prds, options, mimeOptions, pSignOptions, bEncrypt, bSign, 
			bBinary, FALSE);

EncryptSignFileError:

	if (options != NULL)
		PGPFreeOptionList(options);

	if (inputFile != NULL)
		PGPFreeFileSpec(inputFile);

	if (outputFile != NULL)
		PGPFreeFileSpec(outputFile);

	return err;
}


PGPError EncryptSign(HINSTANCE hInst, HWND hwnd, PGPContextRef context, 
					 PGPtlsContextRef tlsContext, 
					 char *szName, char *szModule,
					 PGPclRecipientDialogStruct *prds,
					 PGPOptionListRef ioOptions,
					 PGPOptionListRef mimeOptions,
					 PGPOptionListRef *pSignOptions,
					 BOOL bEncrypt, BOOL bSign, 
					 BOOL bBinary, BOOL bHTML)
{
	PGPError			err				= kPGPError_NoErr;
	PGPMemoryMgrRef		memoryMgr		= NULL;
	PGPKeyDBObjRef		signKey			= NULL;
	PGPKeyDBRef			addedKeys		= NULL;
	PGPKeySetRef		recipKeySet		= NULL;
	PGPOptionListRef	options			= NULL;
	PGPOptionListRef	encryptOptions	= NULL;
	PGPOptionListRef	tempOptions		= NULL;
	PGPCipherAlgorithm  prefAlg			= kPGPCipherAlgorithm_CAST5;
	PGPCipherAlgorithm	allowedAlgs[20];
	char *				szPassphrase	= NULL;
	char *				szConvPass		= NULL;
	PGPByte *			pPasskey		= NULL;
	PGPUInt32			nPasskeyLength	= 0;
	int					nPassphraseLen	= 0;
	int					nConvPassLen	= 0;
	int					nNumAlgs		= 0;
	BOOL				bGotPassphrase	= FALSE;
	BOOL				bGotConvPass	= FALSE;
	BOOL				bTextDocument	= FALSE;
	HWND				hwndWorking		= NULL;
	char 				szComment[256];
	char 				szWorkingTitle[256];

	UpdateWindow(hwnd);
	memoryMgr = PGPPeekContextMemoryMgr(context);

#if PGP_NOTES
	// Check license number and expiration with clientlib functions.
	// This is to avoid having to compile all the PGPlnLib code into
	// the Notes plugin
	if(!PGPclPGPdesktopEnabled())
	{
		PGPclNoLicense(hwnd,kPGPclLotusNotesPlugin);
		return kPGPError_UserAbort;
	}

	// Check for demo expiration
	if (PGPclIsExpired(hwnd,kPGPclEncryptSignExpired) != kPGPError_NoErr)
	{
		PGPclErrorBox(hwnd, kPGPError_Win32_Expired);
		return kPGPError_UserAbort;
	}
#else
	// Check license number and expiration with static library functions.
	// This makes it more difficult to crack.
	PGPlnLicenseCheckLite (context);
	if(!PGPlnPGPmailEnabled())
	{
		PGPclNoLicense(hwnd,kPGPclPGPmailPlugin);
		return kPGPError_UserAbort;
	}

	// Check for expiration
	if (PGPlnIsExpired(hwnd, kPGPlnOperationStandardExpiration) != kPGPError_NoErr)
		return kPGPError_UserAbort;
#endif

	if (bSign)
	{
		err = PGPclGetDefaultPrivateKey(prds->keydbOriginal, &signKey);
		if (IsPGPError(err))
		{
			PGPKeySetRef	pubKeySet = NULL;
			PGPKeyListRef	pubKeyList = NULL;
			PGPKeyIterRef	pubKeyIter = NULL;
			
			PGPNewKeySet(prds->keydbOriginal, &pubKeySet);
			PGPOrderKeySet(pubKeySet, kPGPKeyOrdering_Trust, FALSE, 
				&pubKeyList);
			PGPNewKeyIter(pubKeyList, &pubKeyIter);
			PGPKeyIterNextKeyDBObj(pubKeyIter, kPGPKeyDBObjType_Key, &signKey);
			PGPFreeKeyIter(pubKeyIter);
			PGPFreeKeyList(pubKeyList);
			PGPFreeKeySet(pubKeySet);
			
			err = kPGPError_NoErr;
		}
	}
	
	err = kPGPError_BadPassphrase;
	while (err == kPGPError_BadPassphrase)
	{
		if (szPassphrase != NULL)
		{
			PGPclFreeCachedPhrase(szPassphrase);
			szPassphrase = NULL;
		}

		if (szConvPass != NULL)
		{
			PGPclFreePhrase(szConvPass);
			szConvPass = NULL;
		}

		if (mimeOptions == NULL)
			err = PGPBuildOptionList(context, &options, 
					PGPOLastOption(context));
		else
			err = PGPBuildOptionList(context, &options, 

⌨️ 快捷键说明

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