📄 cpgpkey.h
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: CPGPKey.h,v 1.15 2002/11/11 19:15:59 wjb Exp $
____________________________________________________________________________*/
#ifndef Included_CPGPKey_h // [
#define Included_CPGPKey_h
#include "pgpKeys.h"
#include "pgpPublicKey.h"
#if PGP_WIN32
#include "pgpUnicodeWin32.h"
#include "pgpclientlib.h"
#endif
#include "CPGPData.h"
#include "CString.h"
_PGP_BEGIN
// Types
class CPGPKeyID;
// Class CPGPKeyDBObj
class CPGPKeyDBObj
{
NOT_COPYABLE(CPGPKeyDBObj);
friend class CPGPKeyIter;
public:
CPGPKeyDBObj();
CPGPKeyDBObj(PGPKeyDBObjRef dbObj);
virtual ~CPGPKeyDBObj();
CPGPKeyDBObj& operator=(PGPKeyDBObjRef dbObj);
operator PGPKeyDBObjRef() const {return mKeyDBObj;}
PGPKeyDBObjRef Get() const {return mKeyDBObj;}
PGPContextRef Context() const;
PGPBoolean IsAttached() const;
PGPBoolean GetBooleanProp(PGPKeyDBObjProperty propName) const;
PGPInt32 GetNumericProp(PGPKeyDBObjProperty propName) const;
void GetTimeProp(PGPKeyDBObjProperty propName,
PGPTime& pgpTime) const;
void GetDataProp(PGPKeyDBObjProperty propName, void *pData,
PGPUInt32 availLength, PGPUInt32& usedLength) const;
void Attach(PGPKeyDBObjRef dbObj);
void Clear();
protected:
PGPKeyDBObjRef mKeyDBObj;
};
// Class CPGPKey
class CPGPKey : public CPGPKeyDBObj
{
NOT_COPYABLE(CPGPKey);
public:
CPGPKey() { }
CPGPKey(PGPKeyDBRef keyDB, PGPKeyID keyID);
CPGPKey(PGPKeyDBObjRef key) : CPGPKeyDBObj(key) { }
~CPGPKey() { }
void GetKeyID(CPGPKeyID& keyID) const;
PGPBoolean PassphraseIsValid(const char *passphrase,int passkeylen) const;
PGPBoolean GetKeyForUsage(CPGPKey& subKey, PGPUInt32 flags =
kPGPKeyPropertyFlags_UsageEncryptStorage) const;
void GetPrimaryUserIDName(CString& userID) const;
void FindKeyByKeyID(PGPKeyDBRef keyDB, PGPKeyID keyID);
};
// Class CPGPKeyID
class CPGPKeyID
{
public:
CPGPKeyID() { }
CPGPKeyID(const PGPKeyID& keyID) : mKeyID(keyID) { }
~CPGPKeyID() { }
operator PGPKeyID() const {return mKeyID;}
const PGPKeyID& Get() const {return mKeyID;}
PGPBoolean IsEqual(PGPKeyID keyID) const;
PGPPublicKeyAlgorithm Algorithm() const;
void GetKeyIDString(PGPKeyIDStringType type, CString& idString) const;
void SetKeyIDFromBytes(const PGPByte *pKeyIDBytes, PGPSize length,
PGPPublicKeyAlgorithm algorithm);
void SetKeyIDFromKey(PGPKeyDBObjRef key);
void SetKeyIDFromString(const char *string,
PGPPublicKeyAlgorithm algorithm);
private:
PGPKeyID mKeyID;
};
// Class CPGPPublicKeyContext
class CPGPPublicKeyContext
{
NOT_COPYABLE(CPGPPublicKeyContext)
public:
CPGPPublicKeyContext(PGPKeyDBObjRef key, PGPPublicKeyMessageFormat
messageFormat = kPGPPublicKeyMessageFormat_PGP);
~CPGPPublicKeyContext();
void GetPublicKeyOperationSizes(PGPSize& maxDecryptedBufferSize,
PGPSize& maxEncryptedBufferSize, PGPSize& maxSignatureSize) const;
void PublicKeyEncrypt(const void *pDecryptedData,
PGPSize decryptedDataSize, void *pEncryptedData,
PGPSize& encryptedDataSize) const;
private:
PGPPublicKeyContextRef mPubKeyContext;
};
// Class CPGPPrivateKeyContext
class CPGPPrivateKeyContext
{
NOT_COPYABLE(CPGPPrivateKeyContext)
public:
CPGPPrivateKeyContext(PGPKeyDBObjRef key, const char *passphrase,
int passkeylen,
PGPPublicKeyMessageFormat messageFormat =
kPGPPublicKeyMessageFormat_PGP);
~CPGPPrivateKeyContext();
void GetPrivateKeyOperationSizes(PGPSize& maxDecryptedBufferSize,
PGPSize& maxEncryptedBufferSize, PGPSize& maxSignatureSize) const;
void PrivateKeyDecrypt(const void *pEncryptedData,
PGPSize encryptedDataSize, void *pDecryptedData,
PGPSize& decryptedDataSize) const;
private:
PGPPrivateKeyContextRef mPrivKeyContext;
};
// Class CPGPKeyDBObj member functions
inline
CPGPKeyDBObj::CPGPKeyDBObj() : mKeyDBObj(kInvalidPGPKeyDBObjRef)
{
}
inline
CPGPKeyDBObj::CPGPKeyDBObj(PGPKeyDBObjRef dbObj) :
mKeyDBObj(kInvalidPGPKeyDBObjRef)
{
Attach(dbObj);
}
inline
CPGPKeyDBObj::~CPGPKeyDBObj()
{
try
{
Clear();
}
catch (CComboError&) { }
}
inline
CPGPKeyDBObj&
CPGPKeyDBObj::operator=(PGPKeyDBObjRef dbObj)
{
Attach(dbObj);
return *this;
}
inline
PGPContextRef
CPGPKeyDBObj::Context() const
{
return PGPPeekKeyDBObjContext(mKeyDBObj);
}
inline
PGPBoolean
CPGPKeyDBObj::IsAttached() const
{
return PGPKeyDBObjRefIsValid(mKeyDBObj);
}
inline
PGPBoolean
CPGPKeyDBObj::GetBooleanProp(PGPKeyDBObjProperty propName) const
{
PGPBoolean keyBool;
pgpAssert(IsAttached());
PGPError pgpErr = PGPGetKeyDBObjBooleanProperty(mKeyDBObj, propName,
&keyBool);
THROW_IF_PGPERROR(pgpErr);
return keyBool;
}
inline
PGPInt32
CPGPKeyDBObj::GetNumericProp(PGPKeyDBObjProperty propName) const
{
PGPInt32 keyNum;
pgpAssert(IsAttached());
PGPError pgpErr = PGPGetKeyDBObjNumericProperty(mKeyDBObj, propName,
&keyNum);
THROW_IF_PGPERROR(pgpErr);
return keyNum;
}
inline
void
CPGPKeyDBObj::GetTimeProp(
PGPKeyDBObjProperty propName,
PGPTime& pgpTime) const
{
pgpAssert(IsAttached());
PGPError pgpErr = PGPGetKeyDBObjTimeProperty(mKeyDBObj, propName,
&pgpTime);
THROW_IF_PGPERROR(pgpErr);
}
inline
void
CPGPKeyDBObj::GetDataProp(
PGPKeyDBObjProperty propName,
void *pData,
PGPUInt32 availLength,
PGPUInt32& usedLength) const
{
pgpAssert(IsAttached());
pgpAssertAddrValid(pData, VoidAlign);
PGPError pgpErr = PGPGetKeyDBObjDataProperty(mKeyDBObj, propName,
pData, availLength, &usedLength);
THROW_IF_PGPERROR(pgpErr);
}
inline
void
CPGPKeyDBObj::Attach(PGPKeyDBObjRef dbObj)
{
pgpAssert(PGPKeyDBObjRefIsValid(dbObj));
Clear();
mKeyDBObj = dbObj;
}
inline
void
CPGPKeyDBObj::Clear()
{
mKeyDBObj = kInvalidPGPKeyDBObjRef;
}
// Class CPGPKey member functions
inline
CPGPKey::CPGPKey(PGPKeyDBRef keyDB, PGPKeyID keyID)
{
FindKeyByKeyID(keyDB, keyID);
}
inline
void
CPGPKey::GetKeyID(CPGPKeyID& keyID) const
{
pgpAssert(IsAttached());
keyID.SetKeyIDFromKey(Get());
}
inline
PGPBoolean
CPGPKey::PassphraseIsValid(const char *passphrase,int passkeylen) const
{
pgpAssert(IsAttached());
// NULL passphrase means try cache
if (IsNull(passphrase))
{
return PGPPassphraseIsValid(Get(), PGPOLastOption(Context()));
}
else
{
if(passkeylen==0)
{
return PGPPassphraseIsValid(Get(), PGPOPassphrase(
PGPPeekKeyDBObjContext(Get()), passphrase),
PGPOLastOption(Context()));
}
else
{
return PGPPassphraseIsValid(Get(), PGPOPasskeyBuffer(
PGPPeekKeyDBObjContext(Get()), passphrase, passkeylen),
PGPOLastOption(Context()));
}
}
}
inline
PGPBoolean
CPGPKey::GetKeyForUsage(CPGPKey& subKey, PGPUInt32 flags) const
{
PGPKeyDBObjRef subKeyRef;
PGPError pgpErr = PGPGetKeyForUsage(mKeyDBObj, flags, &subKeyRef);
if (IsPGPError (pgpErr))
return FALSE;
subKey.Attach(subKeyRef);
return TRUE;
}
inline
void
CPGPKey::FindKeyByKeyID(PGPKeyDBRef keyDB, PGPKeyID keyID)
{
pgpAssert(PGPKeyDBRefIsValid(keyDB));
PGPError pgpErr = PGPFindKeyByKeyID(keyDB, &keyID, &mKeyDBObj);
THROW_IF_PGPERROR(pgpErr);
}
inline
void
CPGPKey::GetPrimaryUserIDName(CString& userID) const
{
pgpAssert(IsAttached());
PGPSize usedLength;
#if PGP_WIN32
PGPError pgpErr = PGPclGetPrimaryUserIDNameUTF8(Get(),
userID.GetBuffer(kPGPMaxUserIDSize),
kPGPMaxUserIDSize, &usedLength);
#else
PGPError pgpErr = PGPGetPrimaryUserIDName(Get(),
userID.GetBuffer(kPGPMaxUserIDSize),
kPGPMaxUserIDSize, &usedLength);
#endif
userID.ReleaseBuffer();
THROW_IF_PGPERROR(pgpErr);
}
// Class CPGPKeyID member functions
inline
PGPBoolean
CPGPKeyID::IsEqual(PGPKeyID keyID) const
{
return (PGPCompareKeyIDs(&mKeyID, &keyID) == 0);
}
inline
PGPPublicKeyAlgorithm
CPGPKeyID::Algorithm() const
{
PGPPublicKeyAlgorithm algorithm;
PGPError pgpErr = PGPGetKeyIDAlgorithm(&mKeyID, &algorithm);
THROW_IF_PGPERROR(pgpErr);
return algorithm;
}
inline
void
CPGPKeyID::GetKeyIDString(PGPKeyIDStringType type, CString& idString) const
{
PGPError pgpErr = PGPGetKeyIDString(&mKeyID, type,
idString.GetBuffer(kPGPMaxKeyIDStringSize));
idString.ReleaseBuffer();
THROW_IF_PGPERROR(pgpErr);
}
inline
void
CPGPKeyID::SetKeyIDFromBytes(
const PGPByte *pKeyIDBytes,
PGPSize length,
PGPPublicKeyAlgorithm algorithm)
{
PGPError pgpErr = PGPNewKeyID(pKeyIDBytes, length, algorithm,
&mKeyID);
THROW_IF_PGPERROR(pgpErr);
}
inline
void
CPGPKeyID::SetKeyIDFromKey(PGPKeyDBObjRef key)
{
pgpAssert(PGPKeyDBObjRefIsValid(key));
PGPError pgpErr = PGPGetKeyID(key, &mKeyID);
THROW_IF_PGPERROR(pgpErr);
}
inline
void
CPGPKeyID::SetKeyIDFromString(
const char *string,
PGPPublicKeyAlgorithm algorithm)
{
pgpAssertStrValid(string);
PGPError pgpErr = PGPNewKeyIDFromString(string, algorithm, &mKeyID);
THROW_IF_PGPERROR(pgpErr);
}
// Class CPGPPublicKeyContext member functions
inline
CPGPPublicKeyContext::CPGPPublicKeyContext(
PGPKeyDBObjRef key,
PGPPublicKeyMessageFormat messageFormat)
{
PGPError pgpErr = PGPNewPublicKeyContext(key, messageFormat,
&mPubKeyContext);
THROW_IF_PGPERROR(pgpErr);
}
inline
CPGPPublicKeyContext::~CPGPPublicKeyContext()
{
PGPFreePublicKeyContext(mPubKeyContext);
}
inline
void
CPGPPublicKeyContext::GetPublicKeyOperationSizes(
PGPSize& maxDecryptedBufferSize,
PGPSize& maxEncryptedBufferSize,
PGPSize& maxSignatureSize) const
{
PGPError pgpErr = PGPGetPublicKeyOperationSizes(mPubKeyContext,
&maxDecryptedBufferSize, &maxEncryptedBufferSize, &maxSignatureSize);
THROW_IF_PGPERROR(pgpErr);
}
inline
void
CPGPPublicKeyContext::PublicKeyEncrypt(
const void *pDecryptedData,
PGPSize decryptedDataSize,
void *pEncryptedData,
PGPSize& encryptedDataSize) const
{
pgpAssertAddrValid(pDecryptedData, VoidAlign);
pgpAssertAddrValid(pEncryptedData, VoidAlign);
PGPError pgpErr = PGPPublicKeyEncrypt(mPubKeyContext, pDecryptedData,
decryptedDataSize, pEncryptedData, &encryptedDataSize);
THROW_IF_PGPERROR(pgpErr);
}
// Class CPGPPrivateKeyContext member functions
inline
CPGPPrivateKeyContext::CPGPPrivateKeyContext(
PGPKeyDBObjRef key,
const char *passphrase,
int passkeylen,
PGPPublicKeyMessageFormat messageFormat)
{
// NULL pasphrase means use the cache
PGPError pgpErr = kPGPError_NoErr;
if (IsNull(passphrase))
{
pgpErr = PGPNewPrivateKeyContext(key, messageFormat,
&mPrivKeyContext, PGPOLastOption(PGPPeekKeyDBObjContext(key)));
}
else
{
if(passkeylen==0)
{
pgpErr = PGPNewPrivateKeyContext(key, messageFormat,
&mPrivKeyContext, PGPOPassphrase(PGPPeekKeyDBObjContext(key),
passphrase), PGPOLastOption(PGPPeekKeyDBObjContext(key)));
}
else
{
pgpErr = PGPNewPrivateKeyContext(key, messageFormat,
&mPrivKeyContext, PGPOPasskeyBuffer(PGPPeekKeyDBObjContext(key),
passphrase, passkeylen), PGPOLastOption(PGPPeekKeyDBObjContext(key)));
}
}
THROW_IF_PGPERROR(pgpErr);
}
inline
CPGPPrivateKeyContext::~CPGPPrivateKeyContext()
{
PGPFreePrivateKeyContext(mPrivKeyContext);
}
inline
void
CPGPPrivateKeyContext::GetPrivateKeyOperationSizes(
PGPSize& maxDecryptedBufferSize,
PGPSize& maxEncryptedBufferSize,
PGPSize& maxSignatureSize) const
{
PGPError pgpErr = PGPGetPrivateKeyOperationSizes(mPrivKeyContext,
&maxDecryptedBufferSize, &maxEncryptedBufferSize, &maxSignatureSize);
THROW_IF_PGPERROR(pgpErr);
}
inline
void
CPGPPrivateKeyContext::PrivateKeyDecrypt(
const void *pEncryptedData,
PGPSize encryptedDataSize,
void *pDecryptedData,
PGPSize& decryptedDataSize) const
{
pgpAssertAddrValid(pEncryptedData, VoidAlign);
pgpAssertAddrValid(pDecryptedData, VoidAlign);
PGPError pgpErr = PGPPrivateKeyDecrypt(mPrivKeyContext,
pEncryptedData, encryptedDataSize, pDecryptedData,
&decryptedDataSize);
THROW_IF_PGPERROR(pgpErr);
}
_PGP_END
#endif // ] Included_CPGPKey_h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -