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

📄 clmisc.c

📁 vc环境下的pgp源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*____________________________________________________________________________
	Copyright (C) 1998 Network Associates, Inc.
	All rights reserved.
	
	CLmisc.c - PGP ClientLib DLL miscellaneous routines
	

	$Id: CLmisc.c,v 1.49 1999/04/13 12:35:37 pbj Exp $
____________________________________________________________________________*/
#include "pgpPFLConfig.h"	

// project header files
#include "pgpclx.h"
#include "pgpkmx.h"
#include "..\include\PGPversion.h"
#include "..\include\PGPpk.h"

// constants
#define TEXTXPOS	0.65
#define TEXTYPOS	0.73

typedef struct {
	PGPContextRef			context;
	LPSTR					pszRemoteHost;
	PGPKeyRef				keyAuthenticating;
	PGPtlsCipherSuiteNum	tlsCipher;
	PGPKeySetRef			keysetMain;
	CHAR					szName[kPGPMaxUserIDSize];
	UINT					uFlags;
	PGPValidity				validityThreshold;
} CONFIRMAUTHSTRUCT, *PCONFIRMAUTHSTRUCT;

// external globals
extern HINSTANCE g_hInst;
extern CHAR g_szHelpFile[MAX_PATH];

// local globals
static DWORD aIds[] = {			// Help IDs
    IDC_SERVERNAME,	IDH_PGPCLMISC_AUTHSERVERNAME, 
    IDC_KEYNAME,	IDH_PGPCLMISC_AUTHKEYNAME, 
    IDC_FINGERPRINT,IDH_PGPCLMISC_AUTHKEYFINGERPRINT, 
    IDC_VALIDITY,	IDH_PGPCLMISC_AUTHKEYVALIDITY, 
    IDC_IMPORTKEY,	IDH_PGPCLMISC_AUTHIMPORTKEY, 
	IDC_CERTIFICATE,IDH_PGPCLMISC_AUTHCERTIFICATE,
	IDC_SIGNATURE,	IDH_PGPCLMISC_AUTHSIGNATURE,
	IDC_EXCHANGE,	IDH_PGPCLMISC_AUTHEXCHANGE,
	IDC_CIPHER,		IDH_PGPCLMISC_AUTHCIPHER,
	IDC_HASH,		IDH_PGPCLMISC_AUTHHASH,
	IDOK,			IDH_PGPCLMISC_AUTHCONFIRM,
    0,0 
}; 

//	___________________________________________________
//
//	Internal memory allocation routines
//

VOID* 
clAlloc (UINT uBytes) 
{
	VOID* p;
	p = malloc (uBytes);
	if (p) {
		memset (p, 0, uBytes);
	}
	return p;
}


VOID 
clFree (VOID* p) 
{
	if (p)
		free (p);
}

//	___________________________________________________
//
//	Message box routine using string table resource IDs

LRESULT 
PGPclMessageBox (
		 HWND	hWnd, 
		 INT	iCaption, 
		 INT	iMessage,
		 ULONG	ulFlags) 
{
	CHAR szCaption [128];
	CHAR szMessage [256];

	LoadString (g_hInst, iCaption, szCaption, sizeof(szCaption));
	LoadString (g_hInst, iMessage, szMessage, sizeof(szMessage));

	ulFlags |= MB_SETFOREGROUND;
	return (MessageBox (hWnd, szMessage, szCaption, ulFlags));
}

//	___________________________________________________
//
//	convert SYSTEMTIME structure to number of days from today

PGPError PGPclExport
PGPclSystemTimeToDays (
		 SYSTEMTIME*	pst, 
		 INT*			piDays) 
{
	SYSTEMTIME	stToday;
	struct tm	tmstruct;
	time_t		timeToday;
	time_t		timeInQuestion;
	UINT		uDayToday;
	UINT		uDayInQuestion;

	pgpAssert (pst != NULL);
	pgpAssert (piDays != NULL);

	*piDays = -1;

	if (pst->wYear > 2037) 
		return kPGPError_BadParams;

	GetLocalTime (&stToday);

	tmstruct.tm_mday = stToday.wDay;
	tmstruct.tm_mon = stToday.wMonth -1;
	tmstruct.tm_year = stToday.wYear -1900;
	tmstruct.tm_hour = 12;
	tmstruct.tm_min = 0;
	tmstruct.tm_sec = 0;
	tmstruct.tm_isdst = -1;

	timeToday = mktime (&tmstruct);
	if (timeToday == (time_t)-1) return kPGPError_BadParams;

	tmstruct.tm_mday = pst->wDay;
	tmstruct.tm_mon = pst->wMonth -1;
	tmstruct.tm_year = pst->wYear -1900;
	tmstruct.tm_hour = 12;
	tmstruct.tm_min = 0;
	tmstruct.tm_sec = 0;
	tmstruct.tm_isdst = -1;

	timeInQuestion = mktime (&tmstruct);
	if (timeInQuestion == (time_t)-1) return kPGPError_BadParams;

	uDayToday = timeToday / 86400; 
	uDayInQuestion = timeInQuestion / 86400; 

	*piDays = uDayInQuestion - uDayToday;

	return kPGPError_NoErr;
}


//	___________________________________________________
//
//	create logical palette from bitmap color table

static HPALETTE 
sCreateDIBPalette (
		  LPBITMAPINFO	lpbmi, 
		  LPINT			lpiNumColors) 
{
	LPBITMAPINFOHEADER	lpbi;
	LPLOGPALETTE		lpPal;
	HANDLE				hLogPal;
	HPALETTE			hPal = NULL;
	INT					i;
 
	lpbi = (LPBITMAPINFOHEADER)lpbmi;
	if (lpbi->biBitCount <= 8)
		*lpiNumColors = (1 << lpbi->biBitCount);
	else
		*lpiNumColors = 0;  // No palette needed for 24 BPP DIB
 
	if (*lpiNumColors) {
		hLogPal = GlobalAlloc (GHND, sizeof (LOGPALETTE) +
                             sizeof (PALETTEENTRY) * (*lpiNumColors));
		lpPal = (LPLOGPALETTE) GlobalLock (hLogPal);
		lpPal->palVersion = 0x300;
		lpPal->palNumEntries = *lpiNumColors;
 
		for (i = 0;  i < *lpiNumColors;  i++) {
			lpPal->palPalEntry[i].peRed   = lpbmi->bmiColors[i].rgbRed;
			lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen;
			lpPal->palPalEntry[i].peBlue  = lpbmi->bmiColors[i].rgbBlue;
			lpPal->palPalEntry[i].peFlags = 0;
		}
		hPal = CreatePalette (lpPal);
		GlobalUnlock (hLogPal);
		GlobalFree (hLogPal);
   }
   return hPal;
}
 
//	___________________________________________________
//
//	Load DIB bitmap and associated palette

HBITMAP
CLLoadResourceBitmap (
		HINSTANCE		hInstance, 
		LPSTR			lpString,
		HPALETTE FAR*	lphPalette) 
{
	HRSRC				hRsrc;
	HGLOBAL				hGlobal;
	HBITMAP				hBitmapFinal = NULL;
	LPBITMAPINFOHEADER	lpbi;
	HDC					hdc;
    INT					iNumColors;
 
	if (hRsrc = FindResource (hInstance, lpString, RT_BITMAP)) {
		hGlobal = LoadResource (hInstance, hRsrc);
		lpbi = (LPBITMAPINFOHEADER)LockResource (hGlobal);
 
		hdc = GetDC(NULL);
		*lphPalette =  sCreateDIBPalette ((LPBITMAPINFO)lpbi, &iNumColors);
		if (*lphPalette) {
			SelectPalette (hdc,*lphPalette,FALSE);
			RealizePalette (hdc);
		}
 
		hBitmapFinal = CreateDIBitmap (hdc,
                   (LPBITMAPINFOHEADER)lpbi,
                   (LONG)CBM_INIT,
                   (LPSTR)lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD),
                   (LPBITMAPINFO)lpbi,
                   DIB_RGB_COLORS );
 
		ReleaseDC (NULL,hdc);
		UnlockResource (hGlobal);
		FreeResource (hGlobal);
	}
	return (hBitmapFinal);
}
 

//	___________________________________________________
//
//	Paint user info and registration number 

#define YTEXTINC	14

VOID 
CLPaintUserInfo (
		PGPMemoryMgrRef	memMgr,
		HWND			hwnd,
		HDC				hdc, 
		LPSTR			pszVersion)
{
	HFONT		hFontOld;
	UINT		uTAOld;
	CHAR		sz[256];
	PGPError	err;
	PGPPrefRef	PrefRef;
	RECT		rc;
	INT			iXpos, iYpos;

	GetClientRect (hwnd, &rc);
	iXpos = (INT)(TEXTXPOS * rc.right);
	iYpos = (INT)(TEXTYPOS * rc.bottom);

	SetTextColor (hdc, RGB (255, 255, 255));
	SetBkMode (hdc, TRANSPARENT);
	uTAOld = SetTextAlign (hdc, TA_LEFT);
	hFontOld = SelectObject (hdc, GetStockObject (DEFAULT_GUI_FONT));

	sz[0] = '\0';
#if PGP_BUSINESS_SECURITY
	if (PGPclOpenAdminPrefs (memMgr, 
			&PrefRef, PGPclIsAdminInstall()) != kPGPError_NoErr)
		return;

	err = PGPGetPrefStringBuffer (PrefRef, kPGPPrefAdminCompanyName, 
			sizeof(sz), sz);

	if (IsPGPError (err)) 
		sz[0] = '\0';

	PGPclCloseAdminPrefs (PrefRef, FALSE);
#endif

	if (PGPclOpenClientPrefs (memMgr, &PrefRef) != kPGPError_NoErr)
		return;

	// company name
	if (sz[0] == '\0') {
		err = PGPGetPrefStringBuffer (PrefRef, kPGPPrefCompanyName, 
										sizeof(sz), sz);
	}
	if (IsPGPError (err)) sz[0] = '\0';
	rc.left = iXpos;
	rc.right -= 4;
	rc.top = iYpos + (2*YTEXTINC);
	rc.bottom = rc.top + YTEXTINC;
	DrawTextEx (hdc, sz, lstrlen (sz), &rc, DT_END_ELLIPSIS, NULL);

	// user name
	sz[0] = '\0';
	err = PGPGetPrefStringBuffer (PrefRef, kPGPPrefOwnerName, 
										sizeof(sz), sz);
	rc.top = iYpos + (1*YTEXTINC);
	rc.bottom = rc.top + YTEXTINC;
	DrawTextEx (hdc, sz, lstrlen (sz), &rc, DT_END_ELLIPSIS, NULL);

	// version number
	if (pszVersion) 
		lstrcpy (sz, pszVersion);
	else
		lstrcpy (sz, PGPVERSIONSTRING);
	TextOut (hdc, iXpos, iYpos, sz, lstrlen (sz));

	// labels
	uTAOld = SetTextAlign (hdc, TA_RIGHT);
	LoadString (g_hInst, IDS_VERSION, sz, sizeof(sz));
	TextOut (hdc, iXpos-8, iYpos, sz, lstrlen (sz));

	LoadString (g_hInst, IDS_LICENSEDTO, sz, sizeof(sz));
	TextOut (hdc, iXpos-8, iYpos+YTEXTINC, sz, lstrlen (sz));

	SetTextAlign (hdc, uTAOld);
	SelectObject (hdc, hFontOld);
	PGPclCloseClientPrefs (PrefRef, FALSE);
}

//	___________________________________________________
//
//	Broadcast reload message

VOID PGPclExport 
PGPclNotifyKeyringChanges (LPARAM lParam) 
{
	UINT uMessageID;

	uMessageID = RegisterWindowMessage (RELOADKEYRINGMSG);
	PostMessage (HWND_BROADCAST, uMessageID, 0, lParam);
}

//	___________________________________________________
//
//	Broadcast reload message

VOID PGPclExport 
PGPclNotifyPrefsChanges (LPARAM lParam) 
{
	UINT uMessageID;

	uMessageID = RegisterWindowMessage (RELOADPREFSMSG);
	PostMessage (HWND_BROADCAST, uMessageID, 0, lParam);
}

//	___________________________________________________
//
//	Broadcast messages indicating keyserver prefs may have changed

VOID PGPclExport 
PGPclNotifyKeyserverPrefsChanges (LPARAM lParam) 
{
	UINT uMessageID;

	uMessageID = RegisterWindowMessage (RELOADKEYSERVERPREFSMSG);
	PostMessage (HWND_BROADCAST, uMessageID, 0, lParam);
}

//	___________________________________________________
//
//	get path of PGP installation from registry key 
//	note: includes trailing '\'

PGPError PGPclExport 
PGPclGetPGPPath (LPSTR szPath, UINT uLen) 
{
	HKEY		hKey;
	LONG		lResult;
	DWORD		dwValueType, dwSize;
	CHAR		szKey[128];
	PGPError	err;

	err = kPGPError_FileNotFound;

	lstrcpy (szPath, "");

	LoadString (g_hInst, IDS_REGISTRYKEY, szKey, sizeof(szKey));
	lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szKey, 0, KEY_READ, &hKey);

	if (lResult == ERROR_SUCCESS) {
		err = kPGPError_OutputBufferTooSmall;
		dwSize = uLen;
		lResult = RegQueryValueEx (hKey, "InstallPath", 0, &dwValueType, 
			(LPBYTE)szPath, &dwSize);
		RegCloseKey (hKey);
		if (lResult == ERROR_SUCCESS) 
		{
			err = kPGPError_NoErr;
		}
	}

	return err;
}

//	___________________________________________________
//
//	get keyring and randseed file paths from SDK 

PGPError PGPclExport 
PGPclGetSDKFilePaths (
		LPSTR	pszPubRingPath,
		INT		iPubRingLen,
		LPSTR	pszPrivRingPath,
		INT		iPrivRingLen,
		LPSTR	pszRandSeedPath,
		INT		iRandSeedLen) 
{
	PGPError err;
	PGPFileSpecRef	fileref;
	PGPContextRef	context;
	LPSTR			lpsz;

	err = PGPNewContext ( kPGPsdkAPIVersion, &context);
	PGPsdkLoadDefaultPrefs (context);

	if (IsntPGPError (err)) {
	
		if (pszPubRingPath) {
			err = PGPsdkPrefGetFileSpec (context, kPGPsdkPref_PublicKeyring,
									&fileref);
			if (IsntPGPError (err) && fileref) {
				err = PGPGetFullPathFromFileSpec (fileref, &lpsz);
				if (IsntPGPError (err)) {
					lstrcpyn (pszPubRingPath, lpsz, iPubRingLen);
					PGPFreeData (lpsz);
				}
				PGPFreeFileSpec (fileref);
			}
		}

		if (pszPrivRingPath && IsntPGPError (err)) {
			err = PGPsdkPrefGetFileSpec (context, kPGPsdkPref_PrivateKeyring,
									&fileref);
			if (IsntPGPError (err) && fileref) {
				err = PGPGetFullPathFromFileSpec (fileref, &lpsz);
				if (IsntPGPError (err)) {
					lstrcpyn (pszPrivRingPath, lpsz, iPrivRingLen);
					PGPFreeData (lpsz);
				}
				PGPFreeFileSpec (fileref);
			}
		}

		if (pszRandSeedPath && IsntPGPError (err)) {
			err = PGPsdkPrefGetFileSpec (context, kPGPsdkPref_RandomSeedFile,
									&fileref);
			if (IsntPGPError (err) && fileref) {
				err = PGPGetFullPathFromFileSpec (fileref, &lpsz);
				if (IsntPGPError (err)) {
					lstrcpyn (pszRandSeedPath, lpsz, iRandSeedLen);
					PGPFreeData (lpsz);
				}
				PGPFreeFileSpec (fileref);
			}
		}
		PGPFreeContext (context);
	}

	return err;
}


//	____________________________________
//
//	get the current user's keyrings and set controls to them

static VOID
sSavePGPnetSDKPrefsFile (
		PGPContextRef	contextUser)
{
	PFLFileSpecRef	prefsSpec	= NULL;
	PGPContextRef	contextPGPnet;
	PGPFileSpecRef	fileRef;
	PGPError		err;
	CHAR			szPath[MAX_PATH];
	CHAR			szFile[32];

	LoadString (g_hInst, IDS_PGPNETSDKPREFSFILE, szFile, sizeof(szFile));
	PGPclGetPGPPath (szPath, sizeof(szPath));
	lstrcat (szPath, szFile);

	err = PGPNewContext (kPGPsdkAPIVersion, &contextPGPnet);
	if (IsntPGPError (err))
	{
		err = PFLNewFileSpecFromFullPath (
				PGPGetContextMemoryMgr (contextPGPnet), szPath, &prefsSpec);
		if (IsntPGPError (err))
		{
			err	= PGPsdkLoadPrefs (contextPGPnet, (PGPFileSpecRef)prefsSpec);
			if (IsntPGPError (err))
			{
				PGPsdkPrefGetFileSpec (
						contextUser, kPGPsdkPref_PublicKeyring, &fileRef);
				PGPsdkPrefSetFileSpec (
						contextPGPnet, kPGPsdkPref_PublicKeyring, fileRef);
				PGPFreeFileSpec (fileRef);

				PGPsdkPrefGetFileSpec (
						contextUser, kPGPsdkPref_PrivateKeyring, &fileRef);
				PGPsdkPrefSetFileSpec (
						contextPGPnet, kPGPsdkPref_PrivateKeyring, fileRef);
				PGPFreeFileSpec (fileRef);

				PGPsdkPrefGetFileSpec (
						contextUser, kPGPsdkPref_RandomSeedFile, &fileRef);
				PGPsdkPrefSetFileSpec (
						contextPGPnet, kPGPsdkPref_RandomSeedFile, fileRef);
				PGPFreeFileSpec (fileRef);

				PGPsdkSavePrefs (contextPGPnet);
			}
			PFLFreeFileSpec (prefsSpec);
		}
		PGPFreeContext (contextPGPnet);
	}
}

⌨️ 快捷键说明

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