cpgpexchmessageevents.cpp

来自「PGP8.0源码 请认真阅读您的文件包然后写出其具体功能」· C++ 代码 · 共 2,164 行 · 第 1/4 页

CPP
2,164 行
字号
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pAttach));
		if(SUCCEEDED(hrRet) && (NULL != pAttach))
		{
			hrRet = pAttach->SetProps(5, pVal, NULL);
			pgpAssert(SUCCEEDED(hrRet));

			pgpAssert(NULL == pstrmAttach);
			hrRet = pAttach->OpenProperty(PR_ATTACH_DATA_BIN, &IID_IStream, 0, 
				MAPI_CREATE | MAPI_MODIFY, (LPUNKNOWN *) &pstrmAttach);
			pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmAttach));
			if(SUCCEEDED(hrRet) && (NULL != pstrmAttach))
			{
				hrRet = pstrmAttach->Seek(li, STREAM_SEEK_SET, NULL);
				pgpAssert(SUCCEEDED(hrRet));

				hrRet = pstrmAttach->Write(pFormatOutput, nFormatOutSize, NULL);
				pgpAssert(SUCCEEDED(hrRet));

				hrRet = pstrmAttach->SetSize(uli);
				pgpAssert(SUCCEEDED(hrRet));

				hrRet = pstrmAttach->Commit(STGC_DEFAULT);
				pgpAssert(SUCCEEDED(hrRet));

				pstrmAttach->Release();
				pstrmAttach = NULL;
			}

			hrRet = pAttach->SaveChanges(0);
			pgpAssert(SUCCEEDED(hrRet));
		}
	}

	cleanup:
	
	if(NULL != pCPID)
	{
		hrRet = MAPIFreeBuffer(pCPID);
		pgpAssert(SUCCEEDED(hrRet));
		pCPID = NULL;
	}
	if (NULL != pTextInput)
	{
		free(pTextInput);
		pTextInput = NULL;
	}
	if (NULL != pOutput)
	{
		PGPFreeData(pOutput);
		pOutput = NULL;
	}
	if (NULL != pFormatOutput)
	{
		PGPFreeData(pFormatOutput);
		pFormatOutput = NULL;
	}
	if (NULL != pOutHeader)
	{
		free(pOutHeader);
		pOutHeader = NULL;
	}
	if (NULL != pInput)
	{
		free(pInput);
		pInput = NULL;
	}

	if (NULL != pstrmBody)
	{
		pstrmBody->Release();
		pstrmBody = NULL;
	}

	if(NULL != prAttach)
	{
		FreeProws(prAttach);
		prAttach = NULL;
	}

	if (NULL != ptableAttach)
	{
		ptableAttach->Release();
		ptableAttach = NULL;
	}

	if (NULL != pstrmRTF)
	{
		pstrmRTF->Release();
		pstrmRTF = NULL;
	}
	
	if (NULL != pstrmRTFInput)
	{
		pstrmRTFInput->Release();
		pstrmRTFInput = NULL;
	}
	
	if(NULL != hRTFInput)
	{
		//NOTE: we make this handle GlobalFree'ed automatically upon
		//pstrmRTFInput release. we lock it once to get at the buffer
		//in the handle. so we need to unlock if we did that
		if(NULL != pRTFInput)
		{
			BOOL bRet = GlobalUnlock(hRTFInput);
			pgpAssert(0 != bRet);
		}

#ifdef _DEBUG
		//verify that the handle is invalid. no locks on this handle. 
		UINT uiRet = GlobalFlags(hRTFInput);
		pgpAssert(GMEM_INVALID_HANDLE == uiRet);
		if(GMEM_INVALID_HANDLE != uiRet)
			pgpAssert(0 == (uiRet&GMEM_LOCKCOUNT));
#endif
	}

	return nError;
}


int CPGPexch::DecryptVerifyMessage(HWND hwnd, IMessage *pmsg, 
								   IMsgStore *pMsgStore)
{
	IStream *pstrmBody = NULL;
	IStream *pstrmRTFComp = NULL;
	STATSTG StreamStats = {0};
	DWORD dwInSize = 0;
	UINT nOutSize = 0;
	char *pInput = NULL;
	char *pOutput = NULL;
	PGPError nError = kPGPError_NoErr;
	char szFile[256] = {0};
	char szName[256] = {0};
	char szRTFheader[] = "{\\fromhtml1\n";
	char szRTFfooter[] = "}\0";
	LPMAPITABLE ptableAttach = 0;
	SizedSPropTagArray(1, tagaTable) = {1, {PR_ATTACH_NUM}};
	SRowSet *prAttach = NULL;
	BOOL FYEO = FALSE;
	BOOL bUseSecureViewer = PGPscGetSecureViewerPref(_pgpContext);
	BOOL bNeedsBackup = TRUE;
	SPropValue val = {0};
	HRESULT hrRet = S_OK;
	SizedSPropTagArray(4, tagaAttach) = {
		4, {PR_ATTACH_METHOD, PR_ATTACH_FILENAME, PR_ATTACH_LONG_FILENAME, 
			PR_DISPLAY_NAME}};
	enum {ivalAttachMethod=0, ivalFilename, ivalLongFilename, ivalDisplayName};
	ULONG ulRow = 0;
	ULONG ulAttIndex = 0;
	SPropValue *pval = NULL;
	ULONG ulNumVals = 0;
	LPATTACH pAttach = 0;
	DWORD dwFormatInfoSize = 0;
	char *pFormatInfo = NULL;
	DWORD dwFormatInfoType = 0;
			
	UIGetString(szName, sizeof(szName), IDS_LOGNAME);
	UIGetString(szFile, sizeof(szFile), IDS_DLL);
	
	//make sure that this message has not been already decrypted
	/*hrRet = HrGetOneProp(pmsg, PR_PGPEXCH_DECRYPTED, &pval);
	pgpAssert(FAILED(hrRet) || ((NULL != pval) && (FALSE == pval->Value.b)));
	if(SUCCEEDED(hrRet) && (NULL != pval) && (TRUE == pval->Value.b))
	{
		UIDisplayStringID(hwnd, IDS_E_ALREADYDECRYPTED);
		goto cleanup;//we have already decrypted this message
	}

	//free up the returned buffer since we reuse this pointer later
	hrRet = MAPIFreeBuffer(pval);
	pgpAssert(S_OK == hrRet);
	pval = NULL;*/

	val.ulPropTag = PR_PGPEXCH_HTML;
	val.Value.b = FALSE;
	hrRet = pmsg->SetProps(1, &val, NULL);
	pgpAssert(SUCCEEDED(hrRet));
	
	//if there is already a message backup associated with this object
	if(NULL != m_pBackupMsg)
	{
		//release the reference
		m_pBackupMsg->BackupRelease();
		m_pBackupMsg = NULL;
	}

	//find if there is a backup of this message already
	pgpAssert(NULL == m_pBackupMsg);
	hrRet = CPGPMsgBackup::IsAlreadyBackedUp(pMsgStore,	pmsg, m_pBackupMsg);
	pgpAssert(SUCCEEDED(hrRet));
	if(SUCCEEDED(hrRet))
	{
		
		//we dont need to back this up if there is already a backup and 
		//we need to back up if there is no back up we could find
		bNeedsBackup = !(NULL != m_pBackupMsg);
		if(NULL != m_pBackupMsg)
		{
#ifdef _DEBUG
			ExchPluginTrace("CPGPexch(%#x) - DecryptVerifyMessage(found another backup at "
						"%#x made for CPGPexch(%#x))\n", this, m_pBackupMsg, 
						m_pBackupMsg->GetCreator());
#endif
			pgpAssert(!IsBadWritePtr(m_pBackupMsg, sizeof(CPGPMsgBackup)));

			m_pBackupMsg->BackupAddRef();//add ref count

			//get the format information from backup
			hrRet = m_pBackupMsg->GetFormatInfoIfAny((void *&)pFormatInfo, 
						dwFormatInfoSize, dwFormatInfoType);
			pgpAssert(SUCCEEDED(hrRet));
			
			//if we did not find any formatted info 
			if(SUCCEEDED(hrRet) && (0 == dwFormatInfoSize))
			{
				//this must be a plain text message. get the PR_BODY off of
				//the backup of message
				hrRet = m_pBackupMsg->GetMsgBody((void *&)pInput, dwInSize);
				pgpAssert(SUCCEEDED(hrRet));
			}

		}
	}

	//if we need to create backup object for this message
	if(TRUE == bNeedsBackup)
	{
		//let us read the body of the message then

		//read the message body into a buffer of appropriate length
		pgpAssert(NULL == pInput);
		pgpAssert(0 == dwInSize);
		hrRet = ReadMsgBodyToBuf(pmsg, (void *&)pInput, dwInSize);
		if(FAILED(hrRet))//could not get the body in buffer
			goto cleanup;
		
		//see if the body buffer has pgp headers. otherwise we are done !
		if (FALSE == DoesBufHavePGPHeaders(pInput))
			goto cleanup;

		pgpAssert(NULL == m_pBackupMsg);
		m_pBackupMsg = new CPGPMsgBackup((void *) this, _memoryMgr, pMsgStore, pmsg);
		pgpAssert(NULL != m_pBackupMsg);
		if(NULL == m_pBackupMsg)
			goto cleanup;

		//get the entry id of the message
		hrRet = HrGetOneProp(pmsg, PR_ENTRYID, &pval);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != pval) && 
				  (NULL != pval->Value.bin.lpb) && (0 < pval->Value.bin.cb));
		//if we got the entryid successfully
		if(SUCCEEDED(hrRet) && (NULL != pval) && 
		   (NULL != pval->Value.bin.lpb) && (0 < pval->Value.bin.cb))
		{
			//remember in the backup object that it stands for this entry id
			hrRet = m_pBackupMsg->SetMessageEntryID(pval->Value.bin.lpb, pval->Value.bin.cb);
			pgpAssert(SUCCEEDED(hrRet));
		}

		//free up the returned buffer since we reuse this pointer later
		hrRet = MAPIFreeBuffer(pval);
		pgpAssert(S_OK == hrRet);
		pval = NULL;

		//set the backup object to watch on this message
		hrRet = m_pBackupMsg->WatchMsgForDelOrMove(TRUE);
		pgpAssert(SUCCEEDED(hrRet));

		if (AutoDecrypt(_memoryMgr))
		{
			hrRet = m_pBackupMsg->AddMessage(pInput, dwInSize);
			pgpAssert(SUCCEEDED(hrRet));
		}

		//backup the formatting information if any for html/rtf emails
		hrRet = BackupFormatInfoIfAny(pmsg, (void *&)pFormatInfo, 
					dwFormatInfoSize, dwFormatInfoType, ulAttIndex);
		pgpAssert(SUCCEEDED(hrRet));
		pgpAssert((NULL != pFormatInfo)?dwFormatInfoSize > 0:TRUE);
	}

	//if we did find the formatting information
	if(NULL != pFormatInfo)
	{
		if (AutoDecrypt(_memoryMgr) && !bUseSecureViewer)
		{
			//we reuse buffer pInput here
			if(NULL != pInput)
			{
				free(pInput);
				pInput = NULL;
			}

			//copy the attachment text to pInput
			pInput = pFormatInfo;
			pgpAssert(NULL != pInput);
			pFormatInfo = NULL;//forget the pointer

			dwInSize = dwFormatInfoSize;
			pgpAssert(dwFormatInfoSize > 0);
		}
			
		if(NULL != pFormatInfo)
		{
			free(pFormatInfo);
			pFormatInfo = NULL;
		}
	}
	
	//if we got formatting info and we are backing up now 
	if ((0 < dwFormatInfoSize) && (TRUE == bNeedsBackup))
	{
		pgpAssert(TRUE == bNeedsBackup);//we must be needing backup since this seems like the first time

		//we delete the plug-in attachment from the mail so that user does not see that
		hrRet = pmsg->DeleteAttach(ulAttIndex, 0, NULL, 0);
		pgpAssert(SUCCEEDED(hrRet));

		hrRet = pmsg->SaveChanges(KEEP_OPEN_READWRITE);
		pgpAssert(SUCCEEDED(hrRet));
	}
	
	if (AutoDecrypt(_memoryMgr))
	{
		PGPPrefRef prefs;
		PGPUInt32 nCacheType;
		
		PGPclPeekClientLibPrefRefs(&prefs, NULL);
		PGPGetPrefNumber(prefs, kPGPPrefCacheType, &nCacheType);
		if (nCacheType == kPGPPrefCacheTypeNone)
		{
			nError = PGPSetPrefNumber(prefs, kPGPPrefCacheType, kPGPPrefCacheTypeLogon);
			pgpAssert(IsntPGPError(nError));

			nError = PGPclFlushClientLibPrefs(prefs, NULL);
			pgpAssert(IsntPGPError(nError));

			nError = PGPPurgePassphraseCache(_pgpContext);
			pgpAssert(IsntPGPError(nError));

			nError = PGPPurgeKeyDBCache(_pgpContext);
			pgpAssert(IsntPGPError(nError));
		}
		
		nError = DecryptVerifyBuffer(UIGetInstance(), hwnd, _pgpContext, 
					_tlsContext, szName, szFile, pInput, dwInSize, FALSE, 
					FALSE, (0 != dwFormatInfoSize), (VOID ** )&pOutput, &nOutSize,
					&FYEO);
		
		if (IsntPGPError(nError) && (nOutSize > 0) && (pOutput != NULL))
		{
			LARGE_INTEGER li = {0,0};
			ULARGE_INTEGER uli = {nOutSize, 0};
			BOOL fPartied;

			if(FYEO || bUseSecureViewer)
			{
				PGPscTempestViewer((void *)_pgpContext, hwnd, pOutput, nOutSize, FYEO);
			}
			else
			{
				if (SFPA_FOUND_HTML_DATA != dwFormatInfoType)//if we did not get html body as an attachment
				{
					//make sure PR_BODY_HTML is nullified
					pgpAssert(NULL == pstrmBody);
					hrRet = pmsg->OpenProperty(PR_BODY_HTML, &IID_IStream, STGM_READWRITE, 
								MAPI_MODIFY, (IUnknown**)&pstrmBody); 
					pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
					if (SUCCEEDED(hrRet) && (NULL != pstrmBody))
					{
						hrRet = NullifyStream(pstrmBody);
						pgpAssert(SUCCEEDED(hrRet));

						pstrmBody->Release();
						pstrmBody = NULL;
					}
				}

				if (SFPA_FOUND_RTF_DATA != dwFormatInfoType)//if we did not get RTF body as an attachment
				{
					pgpAssert(NULL == pstrmBody);

					if (SFPA_FOUND_HTML_DATA == dwFormatInfoType)
						hrRet = pmsg->OpenProperty(PR_BODY_HTML, &IID_IStream, 
							STGM_READWRITE, MAPI_CREATE | MAPI_MODIFY, (IUnknown**)&pstrmBody);
					else
						hrRet = pmsg->OpenProperty(PR_BODY, &IID_IStream, 
							STGM_READWRITE, MAPI_MODIFY, (IUnknown**)&pstrmBody);
					pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
					if(SUCCEEDED(hrRet) && (NULL != pstrmBody))
					{
						hrRet = pstrmBody->Seek(li, STREAM_SEEK_SET, NULL);
						pgpAssert(SUCCEEDED(hrRet));

						hrRet = pstrmBody->Write(pOutput, strlen(pOutput), NULL);
						pgpAssert(SUCCEEDED(hrRet));

						hrRet = pstrmBody->SetSize(uli);
						pgpAssert(SUCCEEDED(hrRet));

						hrRet = pstrmBody->Commit(STGC_DEFAULT);
						pgpAssert(SUCCEEDED(hrRet));

						pstrmBody->Release();
						pstrmBody = NULL;
					}

					hrRet = RTFSync(pmsg, RTF_SYNC_BODY_CHANGED, &fPartied);
					pgpAssert(SUCCEEDED(hrRet));
				}
				else
				{
					pgpAssert(NULL == pstrmRTFComp);
					hrRet = pmsg->OpenProperty(PR_RTF_COMPRESSED, &IID_IStream, 
						STGM_READWRITE, MAPI_CREATE | MAPI_MODIFY, (IUnknown**)&pstrmRTFComp);
					pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmRTFComp));
					
					if(SUCCEEDED(hrRet) && (NULL != pstrmRTFComp))
					{
						pgpAssert(NULL == pstrmBody);
						hrRet = WrapCompressedRTFStream(pstrmRTFComp, MAPI_MODIFY, &pstrmBody);
						pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmBody));
						
						//	if (strnicmp(szBuffer, "[\\rtf", 5) &&
						//	strnicmp(szBuffer, "[\\fromhtml", 10))
						//	{
						//	pstrmBody->Write(szRTFheader, 
						//	strlen(szRTFheader), NULL);
						//	
						//	  uli.LowPart += strlen(szRTFheader);
						//	  }
						
						if(SUCCEEDED(hrRet) && (NULL != pstrmBody))
						{
							hrRet = pstrmBody->Write(pOutput, strlen(pOutput), NULL);
							pgpAssert(SUCCEEDED(hrRet));
							
							
							//  if (strnicmp(szBuffer, "[\\rtf", 5) &&
							//  strnicmp(szBuffer, "[\\fromhtml", 10))
							//  {
							//  pstrmBody->Write(szRTFfooter, 
							//  strlen(szRTFfooter) + 1, NULL);
							//
							//  uli.LowPart += strlen(szRTFfooter) + 1;
							//  }
							
							hrRet = pstrmBody->SetSize(uli);
							pgpAssert(SUCCEEDED(hrRet));

							hrRet = pstrmBody->Commit(STGC_DEFAULT);
							pgpAssert(SUCCEEDED(hrRet));

						}

						pstrmRTFComp->Release();
						pstrmRTFComp = NULL;

						hrRet = RTFSync(pmsg, RTF_SYNC_RTF_CHANGED, &fPartied);
						pgpAssert(SUCCEEDED(hrRet));
					}
				}
			}
		}
		
		pgpAssert(NULL == ptableAttach);
		hrRet = pmsg->GetAttachmentTable(0, &ptableAttach);
		pgpAssert(SUCCEEDED(hrRet) && (NULL != ptableAttach));
		if(SUCCEEDED(hrRet) && (NULL != ptableAttach))
		{
			if(NULL != prAttach)
			{
				FreeProws(prAttach);
				prAttach = NULL;
			}
			
			//NOTE: we reuse prAttach again
			pgpAssert(NULL == prAttach);
			hrRet = HrQueryAllRows(ptableAttach, (SPropTagArray *)&tagaTable, 
						NULL, NULL, 0, &prAttach);
			pgpAssert(SUCCEEDED(hrRet) && (NULL != prAttach));
			
			if (prAttach->cRows && IsntPGPError(nError) && !bUseSecureViewer)
			{
				nError = DecryptVerifyRestoreAttachment(hwnd, pmsg, prAttach, 
							FALSE);
				pgpAssert(TRUE == IsntPGPError(nError));
			}
			
			ptableAttach->Release();
			ptableAttach = NULL;
		}
		
		if (nCacheType == kPGPPrefCacheTypeNone)
		{
			nError = PGPclPeekClientLibPrefRefs(&prefs, NULL);
			pgpAssert(IsntPGPError(nError));

			nError = PGPSetPrefNumber(prefs, kPGPPrefCacheType, nCacheType);
			pgpAssert(IsntPGPError(nError));

			nError = PGPclFlushClientLibPrefs(prefs, NULL);
			pgpAssert(IsntPGPError(nError));

			nError = PGPPurgePassphraseCache(_pgpContext);
			pgpAssert(IsntPGPError(nError));

			nError = PGPPurgeKeyDBCache(_pgpContext);
			pgpAssert(IsntPGPError(nError));
		}
	}//end if we are autodecrypting
	else if (0 < dwFormatInfoSize)
	{
		val.ulPropTag = PR_PGPEXCH_HTML;
		val.Value.b = TRUE;
		hrRet = pmsg->SetProps(1, &val, NULL);
		pgpAssert(SUCCEEDED(hrRet));
	}
	
	cleanup:

	if(NULL != pstrmBody)
	{
		pstrmBody->Release();
		pstrmBody = NULL;
	}

	if(NULL != pstrmRTFComp)
	{
		pstrmRTFComp->Release();
		pstrmRTFComp = NULL;
	}

	if(NULL != pInput)
	{
		free(pInput);
		pInput = NULL;
	}
	
	if(NULL != pOutput)

⌨️ 快捷键说明

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