📄 pkmisc.c
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
PKMisc.c - miscellaneous routines
$Id: PKMisc.c,v 1.18 2002/08/06 20:09:46 dallen Exp $
____________________________________________________________________________*/
#include "pgpPFLConfig.h" /* or pgpConfig.h in the CDK */
// project header files
#include "PGPkeysx.h"
// typedefs
typedef struct {
PGPUInt32 uWindowShow;
PGPInt32 iWindowPosX;
PGPInt32 iWindowPosY;
PGPInt32 iWindowWidth;
PGPInt32 iWindowHeight;
PGPBoolean bGroupShow;
PGPInt32 iGroupPercent;
PGPInt32 iToolHeight;
} KEYSWINDOWSTRUCT, *PKEYSWINDOWSTRUCT;
// external global variables
extern HINSTANCE g_hinst;
extern PGPContextRef g_context;
extern PGPtlsContextRef g_tlscontext;
extern PGPBoolean g_bExpertMode;
extern PGPBoolean g_bMarginalAsInvalid;
// ____________________________________
//
// Private memory allocation routine
VOID*
pkAlloc (LONG size)
{
void* p;
p = malloc (size);
if (p) {
memset (p, 0, size);
}
return p;
}
// ____________________________________
//
// Private memory deallocation routine
VOID
pkFree (VOID* p)
{
if (p) {
free (p);
}
}
// ____________________________________
//
// Get PGPkeys private info from prefs file
BOOL
PKGetPrivatePrefData (
DWORD* dwShow,
INT* iX,
INT* iY,
INT* iWidth,
INT* iHeight,
BOOL* bGroupShow,
INT* iGroupPercent,
INT* iToolHeight)
{
PGPPrefRef prefref = kInvalidPGPPrefRef;
PKEYSWINDOWSTRUCT pkws;
PGPError err;
PGPSize size;
*dwShow = SW_SHOWNORMAL;
*iX = DEFAULTWINDOWX;
*iY = DEFAULTWINDOWY;
*iWidth = DEFAULTWINDOWWIDTH;
*iHeight = DEFAULTWINDOWHEIGHT;
*bGroupShow = DEFAULTGROUPSHOW;
*iGroupPercent = DEFAULTGROUPPERCENT;
*iToolHeight = DEFAULTTOOLHEIGHT;
if (IsntPGPError (PGPclPeekClientLibPrefRefs (&prefref, NULL)))
{
size = 0;
err = PGPGetPrefData (prefref,
kPGPPrefPGPkeysWinMainWinPos, &size, &pkws);
if (IsntPGPError (err))
{
if (size == sizeof(KEYSWINDOWSTRUCT))
{
*dwShow = pkws->uWindowShow;
*iX = pkws->iWindowPosX;
*iY = pkws->iWindowPosY;
*iWidth = pkws->iWindowWidth;
*iHeight = pkws->iWindowHeight;
*bGroupShow = pkws->bGroupShow;
*iGroupPercent = pkws->iGroupPercent;
*iToolHeight = pkws->iToolHeight;
}
PGPFreeData (pkws);
}
}
return FALSE;
}
// ____________________________________
//
// Put window position information in registry
VOID
PKSetPrivatePrefData (
HWND hWnd,
BOOL bGroupShow,
INT iGroupPercent,
INT iToolHeight)
{
PGPPrefRef prefref = kInvalidPGPPrefRef;
WINDOWPLACEMENT wp;
KEYSWINDOWSTRUCT kws;
wp.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement (hWnd, &wp);
if (IsntPGPError (PGPclPeekClientLibPrefRefs (&prefref, NULL)))
{
kws.uWindowShow = wp.showCmd;
kws.iWindowPosX = wp.rcNormalPosition.left;
kws.iWindowPosY = wp.rcNormalPosition.top;
kws.iWindowWidth =
wp.rcNormalPosition.right - wp.rcNormalPosition.left;
kws.iWindowHeight =
wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
kws.bGroupShow = bGroupShow;
kws.iGroupPercent = iGroupPercent;
kws.iToolHeight = iToolHeight;
PGPSetPrefData (prefref,
kPGPPrefPGPkeysWinMainWinPos, sizeof(kws), &kws);
}
}
// ____________________________________
//
// Put up preferences property sheet
VOID
PKReloadPGPPreferences (PGPKEYSSTRUCT* ppks)
{
PGPPrefRef prefref = kInvalidPGPPrefRef;
PGPBoolean bUpdate = FALSE;
PGPBoolean b;
if (IsPGPError (PGPclPeekClientLibPrefRefs (&prefref, NULL)))
return;
// has the validity dot vs. bar pref changed?
PGPGetPrefBoolean (prefref, kPGPPrefDisplayMarginalValidity, &b);
if (b != g_bExpertMode)
{
g_bExpertMode = b;
bUpdate = TRUE;
if (!g_bExpertMode)
{
ppks->klConfig.uOptions |= kPGPclKeyList_GreenDotMode;
ppks->gmConfig.uOptions |= GMF_NOVICEMODE;
}
else
{
ppks->klConfig.uOptions &= ~kPGPclKeyList_GreenDotMode;
ppks->gmConfig.uOptions &= ~GMF_NOVICEMODE;
}
}
// has the display marginal validity as invalid pref changed?
PGPGetPrefBoolean (prefref, kPGPPrefMarginalIsInvalid, &b);
if (b != g_bMarginalAsInvalid)
{
g_bMarginalAsInvalid = b;
bUpdate = TRUE;
if (g_bMarginalAsInvalid)
{
ppks->klConfig.uOptions |= kPGPclKeyList_MarginalIsInvalid;
ppks->gmConfig.uOptions |= GMF_MARGASINVALID;
}
else
{
ppks->klConfig.uOptions &= ~kPGPclKeyList_MarginalIsInvalid;
ppks->gmConfig.uOptions &= ~GMF_MARGASINVALID;
}
}
// if prefs have changed => redraw the windows
if (bUpdate)
{
ppks->klConfig.uMask = kPGPclKeyList_Options;
PGPclKeyListConfigure (ppks->hKL, &(ppks->klConfig));
PGPclKeyListLoadKeys(ppks->hKL, ppks->keydbMain,
PGPPeekKeyDBRootKeySet (ppks->keydbMain));
PGPgmConfigure (ppks->hGM, &(ppks->gmConfig));
PGPgmLoadGroups (ppks->hGM);
}
}
// ____________________________________
//
// Put up preferences property sheet
BOOL
PKPGPPreferences (
PGPKEYSSTRUCT* ppks,
HWND hWnd,
INT iPage)
{
PGPError err;
EnableWindow (hWnd, FALSE);
err = PGPclPreferences (g_context, hWnd, iPage, ppks->keydbMain);
if (err != kPGPError_Win32_AlreadyOpen)
SetForegroundWindow (hWnd);
EnableWindow (hWnd, TRUE);
SetFocus (ppks->hwndTreeList);
PKReloadPGPPreferences (ppks);
if (err == kPGPError_NoErr)
return TRUE;
else
return FALSE;
}
// ____________________________________
//
// Auto update trusted introducers in keyring
BOOL
PKAutoUpdateIntroducers (
HWND hwnd,
PGPKEYSSTRUCT* ppks,
BOOL bForce)
{
PGPError err;
err = PGPclAutoUpdateIntroducers (
g_context, g_tlscontext, hwnd, ppks->keydbMain, bForce);
if (IsntPGPError (PGPclErrorBox (hwnd, err)))
{
PGPclKeyListReloadKeys (ppks->hKL, FALSE);
return TRUE;
}
else
return FALSE;
}
// ____________________________________
//
// Ask CA for any new CRLs
BOOL
PKUpdateCARevocations (
HWND hwnd,
HKEYLIST hKL,
PGPKeyDBRef keydbMain)
{
PGPError err = kPGPError_NoErr;
PGPInt32 iNumKeys = 0;
BOOL bImported = FALSE;
err = PGPclGetCertificateRevocationsFromServer (g_context,
g_tlscontext, hwnd, keydbMain);
if (err == kPGPError_Win32_NoNewCRL)
{
PKMessageBox (hwnd, IDS_CAPTIONINFO, IDS_NONEWCRLS,
MB_OK|MB_ICONINFORMATION);
}
else if (IsntPGPError (PGPclErrorBox (hwnd, err)))
{
PGPclKeyListReloadKeys (hKL, FALSE);
}
return bImported;
}
// ___________________________________________________
//
// Is this the only userID on the key ?
BOOL
PKIsThisTheOnlyUserID (
PGPKeyDBObjRef useridThis)
{
INT iCount = 0;
PGPKeyIterRef keyiter;
PGPKeyDBRef keydb;
PGPKeyDBObjRef key;
PGPKeyDBObjRef userid;
PGPBoolean bAttrib;
keydb = PGPPeekKeyDBObjKeyDB (useridThis);
key = PGPPeekKeyDBObjKey (useridThis);
if (PGPKeyDBObjRefIsValid (key))
{
PGPNewKeyIterFromKeyDB (keydb, &keyiter);
PGPKeyIterSeek (keyiter, key);
PGPKeyIterNextKeyDBObj (keyiter, kPGPKeyDBObjType_UserID, &userid);
while (userid)
{
PGPGetKeyDBObjBooleanProperty (userid,
kPGPUserIDProperty_IsAttribute, &bAttrib);
if (!bAttrib)
iCount++;
PGPKeyIterNextKeyDBObj (keyiter, kPGPKeyDBObjType_UserID, &userid);
}
PGPFreeKeyIter (keyiter);
}
return (iCount == 1);
}
// ___________________________________________________
//
// Is this the primary userID on the key ?
BOOL
PKIsThisThePrimaryUserID (
PGPKeyDBObjRef userid)
{
PGPKeyDBObjRef useridPrimary = kInvalidPGPKeyDBObjRef;
PGPKeyDBObjRef key;
key = PGPPeekKeyDBObjKey (userid);
if (PGPKeyDBObjRefIsValid (key))
PGPGetPrimaryUserID (key, &useridPrimary);
return (userid == useridPrimary);
}
// ____________________________________
//
// Look for secret keys which aren't on tokens
BOOL
PKCheckForNonTokenSecretKeys (
PGPKeySetRef keyset)
{
PGPKeyIterRef keyiter;
PGPKeyDBObjRef key;
PGPBoolean bSecret, bOnToken;
BOOL bReturnValue;
PGPNewKeyIterFromKeySet (keyset, &keyiter);
bReturnValue = FALSE;
PGPKeyIterNextKeyDBObj (keyiter, kPGPKeyDBObjType_Key, &key);
while (key && !bReturnValue)
{
PGPGetKeyDBObjBooleanProperty (key,
kPGPKeyProperty_IsSecret, &bSecret);
PGPGetKeyDBObjBooleanProperty (key,
kPGPKeyProperty_IsOnToken, &bOnToken);
if (bSecret && !bOnToken)
bReturnValue = TRUE;
PGPKeyIterNextKeyDBObj (keyiter, kPGPKeyDBObjType_Key, &key);
}
PGPFreeKeyIter (keyiter);
return bReturnValue;
}
// ___________________________________________________
//
// Are there existing photoids on the key ?
BOOL
PKExistingPhotoID (
PGPKeyDBObjRef key)
{
BOOL bExistingPhotoID = FALSE;
PGPKeyIterRef keyiter;
PGPKeyDBObjRef userid;
PGPBoolean bAttrib;
PGPInt32 iType;
PGPNewKeyIterFromKeyDB (PGPPeekKeyDBObjKeyDB (key), &keyiter);
PGPKeyIterSeek (keyiter, key);
PGPKeyIterNextKeyDBObj (keyiter, kPGPKeyDBObjType_UserID, &userid);
while (userid)
{
PGPGetKeyDBObjBooleanProperty (userid,
kPGPUserIDProperty_IsAttribute, &bAttrib);
if (bAttrib)
{
PGPGetKeyDBObjNumericProperty (userid,
kPGPUserIDProperty_AttributeType, &iType);
if (iType == kPGPAttribute_Image)
{
bExistingPhotoID = TRUE;
break;
}
}
PGPKeyIterNextKeyDBObj (keyiter, kPGPKeyDBObjType_UserID, &userid);
}
PGPFreeKeyIter (keyiter);
return bExistingPhotoID;
}
// ___________________________________________________
//
// is this file for importing or for opening in a new window
BOOL
PKFileIsForImporting (
LPSTR pszFile,
PGPUInt32* puType,
PGPInputFormat* pformat)
{
PGPInputFormat format = kPGPInputFormat_Unknown;
UINT uType = PK_EXPORTEDKEYFILE;
UINT uLen;
uLen = lstrlen (pszFile);
if (uLen >= 5)
{
if (lstrcmpi (&pszFile[uLen-4], ".pkr") == 0)
return FALSE;
if (lstrcmpi (&pszFile[uLen-4], ".skr") == 0)
return FALSE;
if (lstrcmpi (&pszFile[uLen-4], ".pgr") == 0)
uType = PK_EXPORTEDGROUPFILE;
else if (lstrcmpi (&pszFile[uLen-4], ".pem") == 0)
format = kPGPInputFormat_PEMEncodedX509Cert;
else if (lstrcmpi (&pszFile[uLen-4], ".pfx") == 0)
format = kPGPInputFormat_PKCS12;
else if (lstrcmpi (&pszFile[uLen-4], ".p12") == 0)
format = kPGPInputFormat_PKCS12;
}
if (IsntNull (pformat))
*pformat = format;
if (IsntNull (puType))
*puType = uType;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -