📄 utils.c
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: Utils.c,v 1.13 2002/10/07 16:20:09 wjb Exp $
____________________________________________________________________________*/
#include <windows.h>
#include "PGPadmin.h"
#include <commctrl.h>
#include "resource.h"
#include "Utils.h"
#include "pgpClientLib.h"
#include "pgpUtilities.h"
#include "pgpMem.h"
#include "pgpImageList.h"
#include "pgpUnicode.h"
typedef struct _PGPProductKey
{
char * szRegKey;
InstallerProduct nProduct;
}
PGPProductKey;
static PGPProductKey aProductKeys[] =
{
{"PGP", Installer_PGPds},
{"PGPmail", Installer_PGPmail},
{"PGPdisk", Installer_PGPdisk},
{"PGPvpn", Installer_PGPvpn},
{"PGPfire", Installer_PGPfire}
};
static int nNumProductKeys = sizeof(aProductKeys) / sizeof(PGPProductKey);
void ShowKeyring(HWND hWnd,
int nListCtrl,
PGPContextRef pgpContext,
PGPKeyDBRef keyDB,
PGPKeySetRef onlyThisKeySet,
PGPBoolean bShowRSA,
PGPBoolean bShowV4,
PGPBoolean bMustEncrypt,
PGPBoolean bMustSign)
{
PGPKeyDBObjRef newKey = NULL;
PGPKeyID newKeyID;
PGPKeyDBObjRef newSubKey = NULL;
PGPError err;
PGPKeySetRef userKeySet = NULL;
PGPKeyListRef klist;
PGPKeyIterRef kiter;
unsigned char szUserID[kPGPMaxUserIDSize+1];
char szSize[64];
char szCreation[64];
char szTitle[255];
UINT nLength;
HWND hList;
HDC hDC;
int iNumBits;
LV_ITEM lviKey;
LV_COLUMN lvcKey;
UINT nItem;
HIMAGELIST hImages;
HBITMAP hBmp;
PGPInt32 nSignSize;
PGPInt32 nEncryptSize;
PGPTime tCreation;
PGPUInt16 year, month, day;
PGPBoolean bIsRevoked;
PGPBoolean bIsDisabled;
PGPBoolean bIsExpired;
PGPBoolean bIsEncryptKey;
PGPBoolean bIsSigningKey;
PGPPublicKeyAlgorithm lAlg;
PGPKeyVersion lVersion;
FreeKeyring(hWnd, nListCtrl);
hList = GetDlgItem(hWnd, nListCtrl);
ListView_DeleteAllItems(hList);
ListView_DeleteColumn(hList, 2);
ListView_DeleteColumn(hList, 1);
ListView_DeleteColumn(hList, 0);
hDC = GetDC (NULL); // DC for desktop
iNumBits = GetDeviceCaps (hDC, BITSPIXEL) * GetDeviceCaps (hDC, PLANES);
ReleaseDC (NULL, hDC);
if (iNumBits <= 8)
{
hImages = ImageList_Create (16, 16, ILC_COLOR | ILC_MASK,
NUM_IMAGELIST_ICONS, 0);
hBmp = LoadBitmap (g_hInstance, MAKEINTRESOURCE (IDB_IMAGES4BIT));
ImageList_AddMasked (hImages, hBmp, RGB(255, 0, 255));
DeleteObject (hBmp);
}
else
{
hImages = ImageList_Create (16, 16, ILC_COLOR24 | ILC_MASK,
NUM_IMAGELIST_ICONS, 0);
hBmp = LoadBitmap (g_hInstance, MAKEINTRESOURCE (IDB_IMAGES24BIT));
ImageList_AddMasked (hImages, hBmp, RGB(255, 0, 255));
DeleteObject (hBmp);
}
ListView_SetImageList(hList, hImages, LVSIL_SMALL);
// Setup the list control columns
LoadString(g_hInstance, IDS_USERID, szTitle, 254);
lvcKey.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
lvcKey.fmt = LVCFMT_LEFT;
lvcKey.cx = 325;
lvcKey.pszText = szTitle;
lvcKey.iSubItem = -1;
ListView_InsertColumn(hList, 0, &lvcKey);
LoadString(g_hInstance, IDS_CREATION, szTitle, 254);
lvcKey.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
lvcKey.fmt = LVCFMT_CENTER;
lvcKey.cx = 75;
lvcKey.pszText = szTitle;
lvcKey.iSubItem = 1;
ListView_InsertColumn(hList, 1, &lvcKey);
LoadString(g_hInstance, IDS_KEYSIZE, szTitle, 254);
lvcKey.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
lvcKey.fmt = LVCFMT_CENTER;
lvcKey.cx = 75;
lvcKey.pszText = szTitle;
lvcKey.iSubItem = 2;
ListView_InsertColumn(hList, 2, &lvcKey);
/* Initialize keyrings */
PGPNewKeySet(keyDB, &userKeySet);
PGPOrderKeySet(userKeySet, kPGPKeyOrdering_UserID, FALSE, &klist);
PGPNewKeyIter(klist, &kiter);
PGPKeyIterNextKeyDBObj(kiter, kPGPKeyDBObjType_Key, &newKey);
nItem = 0;
while (newKey)
{
PGPGetKeyDBObjBooleanProperty(newKey, kPGPKeyProperty_IsRevoked,
&bIsRevoked);
PGPGetKeyDBObjBooleanProperty(newKey, kPGPKeyProperty_IsDisabled,
&bIsDisabled);
PGPGetKeyDBObjBooleanProperty(newKey, kPGPKeyProperty_IsExpired,
&bIsExpired);
PGPGetKeyDBObjBooleanProperty(newKey, kPGPKeyProperty_IsEncryptionKey,
&bIsEncryptKey);
PGPGetKeyDBObjBooleanProperty(newKey, kPGPKeyProperty_IsSigningKey,
&bIsSigningKey);
if ((bIsRevoked || bIsDisabled || bIsExpired) ||
(!bIsEncryptKey && bMustEncrypt) ||
(!bIsSigningKey && bMustSign))
{
PGPKeyIterNextKeyDBObj(kiter, kPGPKeyDBObjType_Key, &newKey);
continue;
}
PGPGetKeyDBObjNumericProperty(newKey, kPGPKeyProperty_AlgorithmID,
(PGPInt32 *) &lAlg);
PGPGetKeyDBObjNumericProperty(newKey, kPGPKeyProperty_Version,
(PGPInt32 *) &lVersion);
if ((bShowRSA && (lAlg == kPGPPublicKeyAlgorithm_RSA) &&
(lVersion <= kPGPKeyVersion_V3)) ||
(bShowV4 && ((lAlg == kPGPPublicKeyAlgorithm_DSA) ||
((lAlg == kPGPPublicKeyAlgorithm_RSA) &&
(lVersion >= kPGPKeyVersion_V4)))))
{
if (IsPGPError(PGPclGetPrimaryUserIDNameUTF8(newKey,
(char*) szUserID, kPGPMaxUserIDSize, &nLength)))
{
PGPKeyIterNextKeyDBObj(kiter, kPGPKeyDBObjType_Key, &newKey);
continue;
}
szUserID[nLength] = 0;
if (IsPGPError(PGPGetKeyDBObjNumericProperty(newKey,
kPGPKeyProperty_Bits, &nSignSize)))
{
PGPKeyIterNextKeyDBObj(kiter, kPGPKeyDBObjType_Key, &newKey);
continue;
}
if ((lAlg == kPGPPublicKeyAlgorithm_DSA) ||
((lAlg == kPGPPublicKeyAlgorithm_RSA) &&
(lVersion >= kPGPKeyVersion_V4)))
{
PGPKeyIterNextKeyDBObj(kiter, kPGPKeyDBObjType_SubKey,
&newSubKey);
if (newSubKey) {
if (IsPGPError(PGPGetKeyDBObjNumericProperty(newSubKey,
kPGPSubKeyProperty_Bits, &nEncryptSize)))
{
PGPKeyIterNextKeyDBObj(kiter, kPGPKeyDBObjType_Key,
&newKey);
continue;
}
wsprintf(szSize, "%d/%d", nEncryptSize, nSignSize);
}
else
wsprintf(szSize, "%d", nSignSize);
}
else
wsprintf(szSize, "%d", nSignSize);
if (IsPGPError(PGPGetKeyDBObjTimeProperty(newKey,
kPGPKeyProperty_Creation, &tCreation)))
{
PGPKeyIterNextKeyDBObj(kiter, kPGPKeyDBObjType_Key, &newKey);
continue;
}
PGPGetYMDFromPGPTime(tCreation, &year, &month, &day);
wsprintf(szCreation, "%d/%d/%d", month, day, year);
if (onlyThisKeySet != NULL)
{
if (!PGPKeySetIsMember(newKey, onlyThisKeySet))
{
PGPKeyIterNextKeyDBObj(kiter, kPGPKeyDBObjType_Key,
&newKey);
continue;
}
}
PGPGetKeyID(newKey, &newKeyID);
lviKey.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
lviKey.pszText = (char *) szUserID;
lviKey.iItem = nItem;
lviKey.iSubItem = 0;
if (lAlg == kPGPPublicKeyAlgorithm_RSA)
{
if (lVersion <= kPGPKeyVersion_V3)
lviKey.iImage = IDX_RSAPUBKEY;
else
lviKey.iImage = IDX_RSAV4PUBKEY;
}
if (lAlg == kPGPPublicKeyAlgorithm_DSA)
lviKey.iImage = IDX_DSAPUBKEY;
lviKey.lParam = (long) pgpAlloc(sizeof(newKeyID.opaqueBytes));
memcpy((void *) lviKey.lParam, newKeyID.opaqueBytes,
sizeof(newKeyID.opaqueBytes));
ListView_InsertItem(hList, &lviKey);
lviKey.mask = LVIF_TEXT;
lviKey.pszText = szCreation;
lviKey.iItem = nItem;
lviKey.iSubItem = 1;
ListView_SetItem(hList, &lviKey);
lviKey.mask = LVIF_TEXT;
lviKey.pszText = szSize;
lviKey.iItem = nItem;
lviKey.iSubItem = 2;
ListView_SetItem(hList, &lviKey);
nItem++;
}
PGPKeyIterNextKeyDBObj(kiter, kPGPKeyDBObjType_Key, &newKey);
}
PGPFreeKeyIter( kiter );
PGPFreeKeyList( klist );
/* Everything was OK */
err = kPGPError_NoErr;
PGPFreeKeySet(userKeySet);
return;
}
BOOL SelectKeys(HWND hWnd,
int nListCtrl,
PGPUInt32 nNumKeys,
PGPKeyID *pConfigKeyID)
{
int nIndex;
int nNumItems;
UINT nCount;
HWND hList;
LV_ITEM lviKey;
PGPKeyID * pKeyID;
BOOL bSelected = FALSE;
if (pConfigKeyID == NULL)
return FALSE;
hList = GetDlgItem(hWnd, nListCtrl);
nNumItems = ListView_GetItemCount(hList);
nCount = 0;
for (nIndex=0; nIndex<nNumItems; nIndex++)
{
lviKey.mask = LVIF_PARAM;
lviKey.iItem = nIndex;
lviKey.iSubItem = 0;
ListView_GetItem(hList, &lviKey);
pKeyID = (PGPKeyID *) lviKey.lParam;
for (nCount=0; nCount < nNumKeys; nCount++)
{
if (!memcmp(pKeyID->opaqueBytes, pConfigKeyID[nCount].opaqueBytes,
sizeof(pKeyID->opaqueBytes)))
{
ListView_SetItemState(hList, nIndex, LVIS_SELECTED,
LVIS_SELECTED);
bSelected = TRUE;
}
}
}
return bSelected;
}
void AddSelectedKeys(HWND hWnd,
int nListCtrl,
PGPContextRef pgpContext,
PGPUInt32 *pnNumKeys,
PGPKeyID **pKeyIDs)
{
int nIndex;
int nCount;
LV_ITEM lviKey;
HWND hList;
PGPKeyID * pKeyID;
hList = GetDlgItem(hWnd, nListCtrl);
if (*pKeyIDs != NULL)
PGPFreeData(*pKeyIDs);
*pnNumKeys = ListView_GetSelectedCount(hList);
if (*pnNumKeys == 0)
{
*pKeyIDs = NULL;
return;
}
*pKeyIDs = (PGPKeyID *) PGPNewData(PGPPeekContextMemoryMgr(pgpContext),
sizeof(pKeyID->opaqueBytes) * (*pnNumKeys),
kPGPMemoryMgrFlags_Clear);
nIndex = ListView_GetNextItem(hList, -1, LVNI_SELECTED);
nCount = 0;
while (nIndex > -1)
{
lviKey.mask = LVIF_PARAM;
lviKey.iItem = nIndex;
lviKey.iSubItem = 0;
ListView_GetItem(hList, &lviKey);
pKeyID = (PGPKeyID *) lviKey.lParam;
memcpy((*pKeyIDs)[nCount].opaqueBytes, pKeyID->opaqueBytes,
sizeof(pKeyID->opaqueBytes));
nIndex = ListView_GetNextItem(hList, nIndex, LVNI_SELECTED);
nCount++;
}
return;
}
void FreeKeyring(HWND hWnd,
int nListCtrl)
{
LV_ITEM lviKey;
int nIndex;
int nCount;
HWND hList;
hList = GetDlgItem(hWnd, nListCtrl);
nCount = ListView_GetItemCount(hList);
for (nIndex=0; nIndex<nCount; nIndex++)
{
lviKey.mask = LVIF_PARAM;
lviKey.iItem = nIndex;
lviKey.iSubItem = 0;
ListView_GetItem(hList, &lviKey);
pgpFree((void *) lviKey.lParam);
}
return;
}
void PaintWizBitmap(HWND hWnd, HBITMAP hBitmap)
{
HDC hDC,
hMemDC;
PAINTSTRUCT ps;
HBITMAP hBitmapOld;
BITMAP bm;
UINT uXsize,
uYsize;
UINT uXpos,
uYpos;
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
uXsize = bm.bmWidth;
uYsize = bm.bmHeight;
uXpos = 0;
uYpos = 13;
hDC = BeginPaint (hWnd, &ps);
hMemDC = CreateCompatibleDC (hDC);
hBitmapOld = (HBITMAP) SelectObject (hMemDC, hBitmap);
BitBlt (hDC, uXpos, uYpos, uXsize, uYsize, hMemDC, 0, 0, SRCCOPY);
SelectObject (hMemDC, hBitmapOld);
DeleteDC (hMemDC);
EndPaint (hWnd, &ps);
}
void SecondsToSystemTime(PGPUInt32 nSeconds, SYSTEMTIME *sysTime)
{
GetSystemTime(sysTime);
sysTime->wHour = nSeconds / 3600;
sysTime->wMinute = (nSeconds % 3600) / 60;
sysTime->wSecond = nSeconds % 60;
sysTime->wMilliseconds = 0;
return;
}
void PGPTimeToSystemTime(PGPTime pgpTime, SYSTEMTIME *sysTime)
{
time_t tTime;
struct tm* ptm;
tTime = PGPGetStdTimeFromPGPTime(pgpTime);
ptm = localtime(&tTime);
sysTime->wYear = ptm->tm_year + 1900;
sysTime->wMonth = ptm->tm_mon + 1;
sysTime->wDay = ptm->tm_mday;
sysTime->wDayOfWeek = ptm->tm_wday;
sysTime->wHour = ptm->tm_hour;
sysTime->wMinute = ptm->tm_min;
sysTime->wSecond = ptm->tm_sec;
sysTime->wMilliseconds = 0;
return;
}
void SystemTimeToPGPTime(SYSTEMTIME *sysTime, PGPTime *pgpTime)
{
time_t tTime;
struct tm stm;
stm.tm_year = sysTime->wYear - 1900;
stm.tm_mon = sysTime->wMonth - 1;
stm.tm_mday = sysTime->wDay;
stm.tm_wday = sysTime->wDayOfWeek;
stm.tm_hour = sysTime->wHour;
stm.tm_min = sysTime->wMinute;
stm.tm_sec = sysTime->wSecond;
stm.tm_isdst = -1;
tTime = mktime(&stm);
*pgpTime = PGPGetPGPTimeFromStdTime(tTime);
return;
}
InstallerProduct GetInstalledProduct(char *szProductKey)
{
InstallerProduct nProduct = Installer_None;
int nIndex;
if (szProductKey == NULL)
{
HKEY hKey;
char szKey[256];
LONG lResult;
PGPclGetPath(kPGPclRegistryKey, szKey, sizeof(szKey));
strcat(szKey, "\\InstalledBuilds");
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, KEY_READ, &hKey);
if (lResult == ERROR_SUCCESS)
{
DWORD dwSize;
DWORD dwValueType;
DWORD dwValue;
for (nIndex=0; nIndex<nNumProductKeys; nIndex++)
{
dwSize = sizeof(DWORD);
lResult = RegQueryValueEx(hKey, aProductKeys[nIndex].szRegKey, 0,
&dwValueType, (LPBYTE) &dwValue, &dwSize);
if ((lResult == ERROR_SUCCESS) && dwValue)
nProduct |= aProductKeys[nIndex].nProduct;
}
RegCloseKey(hKey);
}
}
else
{
for (nIndex=0; nIndex<nNumProductKeys; nIndex++)
{
if (!strcmp(szProductKey, aProductKeys[nIndex].szRegKey))
{
nProduct = aProductKeys[nIndex].nProduct;
break;
}
}
}
return nProduct;
}
/*__Editor_settings____
Local Variables:
tab-width: 4
End:
vi: ts=4 sw=4
vim: si
_____________________*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -