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

📄 decryptverify.cpp

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

	$Id: DecryptVerify.cpp,v 1.3 2002/11/20 19:03:18 sdas Exp $
____________________________________________________________________________*/

// System Headers
#include <windows.h>
#include <stdio.h>

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

// Shared Headers 
#include "pgpClientLib.h"
#include "pgpLnLib.h"
#include "PGPsc.h"
#include "SigEvent.h"
#include "Working.h"
#include "Prefs.h"
#include "DecryptVerify.h"
#include "ParseMime.h"
#include "VerificationBlock.h"
#include "WorkingResource.h"
#include "SharedStrings.h"
#include "pgpWin32Errors.h"

#define CONTENTS_ENCRYPTED	0x01
#define CONTENTS_SIGNED		0x02

#define BEGIN_RTFHTML		"\\viewkind4\\uc1\\pard\\f0\\fs18 "
#define BEGIN_RTFHTMLALT	"\\uc1\\pard\\ulnone\\f0\\fs18 "
#define END_RTFHTML			"}\0"


typedef struct _VerificationBlock VerificationBlock;

struct _VerificationBlock
{
	VerificationBlock *		next;
	VerificationBlock *		previous;
	char *					szBlockBegin;
	char *					szBlockEnd;
	void *					pOutput;
	PGPSize					outSize;
	PGPBoolean				bEncrypted;
	PGPBoolean				FYEO;
};

//NOTE: copy of this data structure exists in notes plugin. please
//update the copy of this structure accordingly when this is changed.
typedef struct _DecodeEventData
{
	HINSTANCE			hInst;
	HWND				hwnd;
	HWND				hwndWorking;
	PGPtlsContextRef	tlsContext;
	char *				szName;
	PGPKeyDBRef			keyDB;
	PGPKeyDBRef			keydbNew;
	PGPKeySetRef		recipients;
	PGPUInt32			keyCount;
	PGPKeyID			*keyIDArray;
	VerificationBlock *	pVerBlock;
	PGPByte				contents;
	PGPFileSpecRef		fsrFileRef;
} DecodeEventData;

static PGPError DecryptVerify(HINSTANCE hInst, HWND hwnd, 
							  PGPContextRef context, 
							  PGPtlsContextRef tlsContext, 
							  char *szName, char *szModule,
							  PGPOptionListRef options, BOOL bMIME,
							  VerificationBlock *pVerBlock, 
							  BOOL bImportIfKeysOnly, BOOL *bKeysOnly);

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

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

static char * sFindFormatBegin(char *szString, BOOL *pbHTML, BOOL *pbRTF, 
							   BOOL *pbRTFHTML);
static char * sFindFormatEnd(char *szString, BOOL bRTF);

static void	WriteDecryptedMimeParts(PGPMemoryMgrRef mgr,
									VerificationBlock *pVerBlock, 
									MimePart *pMimeList,
									MimePart **ppMimeFooter,
									HANDLE hFile,
									char **szOutBuffer);

static void	WriteDecryptedMimeFooter(PGPMemoryMgrRef mgr,
									 VerificationBlock *pVerBlock, 
									 MimePart *pMimeList,
									 HANDLE hFile,
									 char **szOutBuffer);


PGPError DecryptVerifyBuffer(HINSTANCE hInst, HWND hwnd, 
							 PGPContextRef context, 
							 PGPtlsContextRef tlsContext, 
							 char *szName, char *szModule,
							 void *pInput, DWORD dwInSize,
							 BOOL bMIME, BOOL bBinary, BOOL bHTML,
							 void **ppOutput, PGPSize *pOutSize, BOOL *FYEO)
{
	PGPError			err			= kPGPError_NoErr;
	PGPOptionListRef	options		= NULL;
	VerificationBlock *	pVerBlock	= NULL;
	VerificationBlock * pTempBlock	= NULL;
	PGPMemoryMgrRef		memoryMgr	= NULL;
	VerificationBlock * pVerIndex	= NULL;
	OUTBUFFLIST	      * obl			= NULL;
	OUTBUFFLIST       * nobl		= NULL;
	BOOL				bFYEO		= FALSE;
	BOOL				bGotHTML	= FALSE;
	BOOL				bGotRTF		= FALSE;
	BOOL				bGotRTFHTML	= FALSE;
	PGPByte				contents	= 0;
	BOOL				bKeysOnly	= FALSE;
	char			  * szStart		= NULL;
	char			  * szEnd		= NULL;
	char			  * szTemp		= NULL;
	char			  * szHeader	= NULL;
	UINT				nTempLength = 0;
	UINT				nFrom		= 0;
	UINT				nIndex		= 0;

	pgpAssert(pInput != NULL);
	pgpAssert(ppOutput != NULL);
	pgpAssert(pOutSize != NULL);
	pgpAssert(szName != NULL);
	pgpAssert(szModule != NULL);
	pgpAssert(context != NULL);
	pgpAssert(tlsContext != NULL);

	*ppOutput = NULL;
	*pOutSize = 0;

	memoryMgr = PGPPeekContextMemoryMgr(context);

	if (bBinary)
		err = PGPBuildOptionList(context, &options, 
				PGPOInputBuffer(context, pInput, dwInSize),
				PGPOAllocatedOutputBuffer(context, ppOutput, INT_MAX, pOutSize),
				PGPOLastOption(context));
	else
		err = PGPBuildOptionList(context, &options, 
				PGPOInputBuffer(context, pInput, dwInSize),
				PGPOLastOption(context));

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

	if (!bBinary)
	{
		pVerBlock = (VerificationBlock *) PGPNewData(memoryMgr,
											sizeof(VerificationBlock),
											kPGPMemoryMgrFlags_Clear);
		
		pVerBlock->next = NULL;
		pVerBlock->szBlockBegin = NULL;
		pVerBlock->szBlockEnd = NULL;
		pVerBlock->pOutput = NULL;
		pVerBlock->outSize = 0;
		pVerBlock->bEncrypted = FALSE;
		pVerBlock->FYEO = FALSE;
	}

	err = DecryptVerify(hInst, hwnd, context, tlsContext, szName, szModule, 
			options, bMIME, pVerBlock, TRUE, &bKeysOnly);

	pVerIndex=pVerBlock;
	szHeader = BEGIN_RTFHTML;

	if (!bBinary)
	{
		// Convert pVerBlock to OUTBUFFLIST
		do
		{
			if (bMIME && (pVerIndex->szBlockBegin != NULL))
			{
				BOOL bMultiMixed = FALSE;
				BOOL bMultiAlt = FALSE;
				BOOL bFirstMimePart = FALSE;
				MimePart *pMimeList = NULL;
				MimePart *pFirstMimeList = NULL;
				
				ParseMime((char *) pVerBlock->pOutput, &pMimeList);
				
				if (pMimeList != NULL)
				{
					char szTemp[] = "Mime-Version: 1.0\r\n";
					char *szOutBuffer;

					szOutBuffer = (char *) PGPNewData(memoryMgr,
											strlen(szTemp) + 1,
											kPGPMemoryMgrFlags_Clear);

					strcpy(szOutBuffer, szTemp);
					
					WriteDecryptedMimeParts(memoryMgr, pVerBlock, pMimeList, 
						NULL, NULL, &szOutBuffer);

					nobl=PGPscMakeOutBuffItem(&obl);
					nobl->pBuff=szOutBuffer;
					nobl->dwBuffSize=strlen(szOutBuffer);
					FreeMimeList(pMimeList);
				}
				
				PGPFreeData(pVerBlock->szBlockBegin);
				PGPFreeData(pVerBlock->pOutput);
				PGPFreeData(pVerBlock->szBlockEnd);
			}
			else if (pVerIndex->szBlockBegin != NULL)
			{
				if (bHTML)
				{
					if (pVerIndex->pOutput != NULL)
					{
						szStart = sFindFormatBegin((char *)pVerIndex->pOutput,
									&bGotHTML, &bGotRTF, &bGotRTFHTML);

						if (szStart != NULL)
						{
							if (strncmp(szStart, "\\uc", 3))
								szHeader = BEGIN_RTFHTMLALT;

							nTempLength =	szStart - 
											(char *) pVerIndex->pOutput;

							szTemp = (char *) PGPNewData(memoryMgr,
												nTempLength + 1,
												kPGPMemoryMgrFlags_Clear);

							strncpy(szTemp, (char *)pVerIndex->pOutput, nTempLength);

							nobl=PGPscMakeOutBuffItem(&obl);
							nobl->pBuff=szTemp;
							nobl->dwBuffSize=nTempLength;

							szEnd = sFindFormatEnd((char *)pVerIndex->pOutput, 
										bGotRTF);
						}
					}

					szTemp = (char *) PGPNewData(memoryMgr,
										strlen(pVerIndex->szBlockBegin)*4
										+ strlen(szHeader),
										kPGPMemoryMgrFlags_Clear);

					nTempLength = 0;
					nFrom = 0;

					if (bGotRTF)
					{
						strcpy(szTemp, szHeader);
						nTempLength = strlen(szHeader);
					}

					for (nIndex=0; nIndex<strlen(pVerIndex->szBlockBegin);
					nIndex++)
					{
						if (pVerIndex->szBlockBegin[nIndex] == '\n')
						{
							memcpy(&(szTemp[nTempLength]),
								&(pVerIndex->szBlockBegin[nFrom]),
								nIndex - nFrom + 1);

							nTempLength += nIndex - nFrom + 1;
							nFrom = nIndex;
							if (bGotRTF)
							{
								if (bGotRTFHTML)
								{
									memcpy(&(szTemp[nTempLength]), 
										"{\\*\\htmltag0 <br> }", 19);
									nTempLength += 19;
								}
								else
								{
									memcpy(&(szTemp[nTempLength]), 
										"\\par", 4);
									nTempLength += 4;
								}
							}
							else
							{
								memcpy(&(szTemp[nTempLength]), "<br>", 4);
								nTempLength += 4;
							}
						}
					}

					nobl=PGPscMakeOutBuffItem(&obl);
					nobl->pBuff=szTemp;
					nobl->dwBuffSize=strlen(szTemp);
					PGPFreeData(pVerIndex->szBlockBegin);
				}
				else
				{
					nobl=PGPscMakeOutBuffItem(&obl);
					nobl->pBuff=pVerIndex->szBlockBegin;
					nobl->dwBuffSize=strlen(pVerIndex->szBlockBegin);
				}
			}
			
			if ((!bMIME || (pVerIndex->szBlockBegin == NULL)) && 
				(pVerIndex->pOutput != NULL))
			{
				if (bHTML && (szStart != NULL))
				{
					if (szEnd != NULL)
						nTempLength = szEnd - szStart;
					else
						nTempLength = strlen(szStart);

					szTemp = (char *) PGPNewData(memoryMgr,
										nTempLength + 1,
										kPGPMemoryMgrFlags_Clear);

					strncpy(szTemp, szStart, nTempLength);

					nobl=PGPscMakeOutBuffItem(&obl);
					nobl->pBuff=szTemp;
					nobl->dwBuffSize=nTempLength;

					if (szEnd == NULL)
						PGPFreeData(pVerIndex->pOutput);
				}
				else
				{
					nobl=PGPscMakeOutBuffItem(&obl);
					nobl->pBuff=(char *)pVerIndex->pOutput;
					nobl->dwBuffSize=strlen((char *)pVerIndex->pOutput);
				}
				
				if (PGPscGetSecureViewerPref((void *)context))
					nobl->FYEO = TRUE;
				else
				{
					nobl->FYEO=pVerIndex->FYEO;
					if (nobl->FYEO)
						bFYEO = TRUE;
				}
			}
			
			if (!bMIME && (pVerIndex->szBlockEnd != NULL))
			{
				if (bHTML)
				{
					szTemp = (char *) PGPNewData(memoryMgr,
										strlen(pVerIndex->szBlockEnd)*4
										+ strlen(szHeader)
										+ strlen(END_RTFHTML),
										kPGPMemoryMgrFlags_Clear);

					nTempLength = 0;
					nFrom = 0;

					if (bGotRTF)
					{
						strcpy(szTemp, szHeader);
						nTempLength = strlen(szHeader);
					}

					for (nIndex=0; nIndex<strlen(pVerIndex->szBlockEnd);
					nIndex++)
					{
						if (pVerIndex->szBlockEnd[nIndex] == '\n')
						{
							memcpy(&(szTemp[nTempLength]),
								&(pVerIndex->szBlockEnd[nFrom]),
								nIndex - nFrom + 1);

							nTempLength += nIndex - nFrom + 1;
							nFrom = nIndex;
							if (bGotRTF)
							{
								if (bGotRTFHTML)
								{
									memcpy(&(szTemp[nTempLength]), 
										"{\\*\\htmltag0 <br> }", 19);
									nTempLength += 19;
								}
								else
								{
									memcpy(&(szTemp[nTempLength]), 
										"\\par", 4);
									nTempLength += 4;
								}
							}
							else
							{
								memcpy(&(szTemp[nTempLength]), "<br>", 4);
								nTempLength += 4;
							}
						}
					}

					if (bGotRTF)
					{
						strcat(szTemp, END_RTFHTML);
						nTempLength += strlen(END_RTFHTML);
					}

					nobl=PGPscMakeOutBuffItem(&obl);
					nobl->pBuff=szTemp;
					nobl->dwBuffSize=strlen(szTemp);
					PGPFreeData(pVerIndex->szBlockEnd);
				}
				else
				{
					nobl=PGPscMakeOutBuffItem(&obl);
					nobl->pBuff=pVerIndex->szBlockEnd;
					nobl->dwBuffSize=strlen(pVerIndex->szBlockEnd);
				}
				
				if (bHTML)
				{
					if (szEnd != NULL)
					{
						nTempLength = strlen(szEnd);

						szTemp = (char *) PGPNewData(memoryMgr,
											nTempLength + 1,
											kPGPMemoryMgrFlags_Clear);

						strncpy(szTemp, szEnd, nTempLength);

						nobl=PGPscMakeOutBuffItem(&obl);
						nobl->pBuff=szTemp;
						nobl->dwBuffSize=nTempLength;

						PGPFreeData(pVerIndex->pOutput);
					}
				}
			}
			
			pTempBlock = pVerIndex;
			pVerIndex=pVerIndex->next;
			PGPFreeData(pTempBlock);
		}
		while (pVerIndex != NULL);
		
		// Concatinate them to ppOutput
		PGPscConcatOutBuffList((void *)context,
			obl,
			(char **)ppOutput,
			(unsigned long *)pOutSize,
			FYEO);
	}

	/* We don't want to show the FYEO warning if the user has
	   "Always use Secure Viewer" on */

	*FYEO = bFYEO;

DecryptVerifyBufferError:

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

	return err;
}


PGPError DecryptVerifyFile(HINSTANCE hInst, HWND hwnd, PGPContextRef context, 
						   PGPtlsContextRef tlsContext, 
						   char *szName, char *szModule,
						   char *szInFile, BOOL bMIME, BOOL bBinary,
						   char **pszOutFile, void **ppOutput, 
						   PGPSize *pOutSize, BOOL *FYEO)
{
	PGPError			err			= kPGPError_NoErr;
	PGPMemoryMgrRef		memoryMgr	= NULL;
	PGPOptionListRef	options		= NULL;
	PGPFileSpecRef		inputFile	= NULL;
	PGPFileSpecRef		outputFile	= NULL;
	PGPFileSpecRef		finalFile	= NULL;
	PGPFileSpecRef		dataFile	= NULL;
	PGPUInt32			macCreator	= 0;
	PGPUInt32			macType		= 0;
	VerificationBlock *	pVerBlock	= NULL;
	VerificationBlock * pTempBlock	= NULL;
	char *				szExtension = NULL;
	char *				szOutFile	= NULL;
	OUTBUFFLIST	      * obl			= NULL;
	OUTBUFFLIST       * nobl		= NULL;
	BOOL				bFYEO		= FALSE;
	BOOL				bDoFYEO		= FALSE;
	PGPByte				contents	= 0;
	BOOL				bKeysOnly	= FALSE;

	pgpAssert(szInFile != NULL);
	pgpAssert(pszOutFile != NULL);
	pgpAssert(szName != NULL);
	pgpAssert(szModule != NULL);
	pgpAssert(context != NULL);
	pgpAssert(tlsContext != NULL);

	if (ppOutput != NULL)
		*ppOutput = NULL;

	if (pOutSize != NULL)
		*pOutSize = 0;

	memoryMgr = PGPPeekContextMemoryMgr(context);

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

	szOutFile = (char *) calloc(sizeof(char), strlen(szInFile)+5);
	if (szOutFile == NULL)
	{
		err = kPGPError_OutOfMemory;
		DisplayErrorCode(__FILE__, __LINE__, szModule, err, FALSE, hwnd);
		goto DecryptVerifyFileError;
	}

	strcpy(szOutFile, szInFile);
 	szExtension = strrchr(szOutFile, '.');
	if (szExtension != NULL)
	{
		if (!strcmp(szExtension, ".asc") || !strcmp(szExtension, ".pgp"))
			*szExtension = '\0';
		else
			strcat(szOutFile, ".tmp");
	}
	else
		strcat(szOutFile, ".tmp");

	SetFileAttributes(szOutFile, FILE_ATTRIBUTE_NORMAL);

	if (bBinary)
	{
		err = PGPNewFileSpecFromFullPath(context, szOutFile, &outputFile);
		if (IsPGPError(err))
		{

⌨️ 快捷键说明

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