pgpkeys.pas

来自「用DELPHI实现的 PGP 加密算法」· PAS 代码 · 共 1,703 行 · 第 1/5 页

PAS
1,703
字号
		var RevKeyID: TPGPKeyID7): PGPError;
begin
  if PGP7X then
    Result:=PGPGetIndexedRevocationKey7(pPGPKeyDBObj(BaseKey), RevKeyIndex,
					pPGPKeyDBObj(RevKey), RevKeyID)
  else Result:=PGPGetIndexedRevocationKey6(pPGPKey(BaseKey), AllKeys, RevKeyIndex,
					   pPGPKey(RevKey), RevKeyID);
end;

function PGPGetKeyPasskeyBuffer(Key: pPGPKey;
		 pPasskeyBuffer: Pointer;
		 Options: pPGPOptionList;
		 LastOption: pPGPOptionList): PGPError;
begin
  if PGP7X then
    Result:=PGPGetPasskeyBuffer7(Key, pPasskeyBuffer, Options, LastOption)
  else Result:=PGPGetKeyPasskeyBuffer6(Key, pPasskeyBuffer, Options, LastOption);
end;

function PGPGetSubKeyPasskeyBuffer(SubKey: pPGPSubKey;
		 pPasskeyBuffer: Pointer;
		 Options: pPGPOptionList;
		 LastOption: pPGPOptionList): PGPError;
begin
  if PGP7X then
    Result:=PGPGetPasskeyBuffer7(SubKey, pPasskeyBuffer, Options, LastOption)
  else Result:=PGPGetSubKeyPasskeyBuffer6(SubKey, pPasskeyBuffer, Options, LastOption);
end;

// Key type depends on PGP version
function PGPGetPrimaryUserIDNameBuffer(Key: Pointer;
		UserID: PChar;
		BufferSize: PGPSize;
		var FullSize: PGPSize): PGPError;
begin
  if PGP7X then
    Result:=PGPGetPrimaryUserIDName7(pPGPKeyDBObj(Key), UserID, BufferSize, FullSize)
  else Result:=PGPGetPrimaryUserIDNameBuffer6(pPGPKey(Key), BufferSize, UserID, FullSize);
end;

// Keys/Key types depend on PGP version
function PGPGetKeyByKeyID(KeySet: pPGPKeySet;
		const PGPKeyID: TPGPKeyID7;
		PubKeyAlgorithm: PGPPublicKeyAlgorithm;
		var Key: Pointer): PGPError;
begin
  if PGP7X then
    Result:=PGPFindKeyByKeyID7(PGPPeekKeySetKeyDB(KeySet), PGPKeyID, pPGPKeyDBObj(Key))
  else Result:=PGPGetKeyByKeyID6(pPGPKeySet(KeySet), PGPKeyID, PubKeyAlgorithm, pPGPKey(Key));
end;

// no version specific types
function PGPGetKeyIDFromString(KeyID: PChar;
		PubKeyAlgorithm: PGPPublicKeyAlgorithm;
		var PGPkeyID: TPGPKeyID7): PGPError;
begin
  if PGP7X then
    Result:=PGPNewKeyIDFromString7(KeyID, PubKeyAlgorithm, PGPkeyID)
  else Result:=PGPGetKeyIDFromString6(KeyID, PGPkeyID);
end;

// Key type depends on PGP version
function PGPGetKeyIDFromKey(Key: Pointer;
		var PGPKeyID: TPGPKeyID7): PGPError;
begin
  if PGP7X then
    Result:=PGPGetKeyID7(pPGPKeyDBObj(Key), PGPKeyID)
  else Result:=PGPGetKeyIDFromKey6(pPGPKey(Key), PGPKeyID);
end;

// Key type depends on PGP version
function PGPGetKeyIDFromSubKey(Key: Pointer;
		var PGPKeyID: TPGPKeyID7): PGPError;
begin
  if PGP7X then
    Result:=PGPGetKeyID7(pPGPKeyDBObj(Key), PGPKeyID)
  else Result:=PGPGetKeyIDFromSubKey6(pPGPSubKey(Key), PGPKeyID);
end;

// Sig type depends on PGP version
function PGPGetKeyIDOfCertifier(Sig: Pointer;
		var PGPKeyID: TPGPKeyID7): PGPError;
begin
  if PGP7X then
    Result:=PGPGetKeyID7(pPGPKeyDBObj(Sig), PGPKeyID)
  else Result:=PGPGetKeyIDOfCertifier6(pPGPSig(Sig), PGPKeyID);
end;

// takes PGP 7.X PGPKeyDBObjProperty constants
function PGPNewKeyDBObjBooleanFilter(Context: pPGPContext;
		WhichProperty: PGPKeyDBObjProperty; Match: PGPBoolean;
		var OutFilter: pPGPFilter): PGPError;
begin
  if PGP7X then
    Result:=PGPNewKeyDBObjBooleanFilter7(Context, WhichProperty, Match, OutFilter)
  else begin
    // only implements selected filters
    case WhichProperty of
      kPGPKeyProperty_IsSecret: Result:=PGPNewKeyBooleanFilter(Context, WhichProperty, Match, OutFilter);
      kPGPKeyProperty_IsDisabled: Result:=PGPNewKeyDisabledFilter(Context, Match, OutFilter);
      kPGPKeyProperty_CanEncrypt..kPGPKeyProperty_CanVerify: begin
	WhichProperty:=WhichProperty - kPGPKeyProperty_CanEncrypt + kPGPKeyPropCanEncrypt;
	Result:=PGPNewKeyBooleanFilter(Context, WhichProperty, Match, OutFilter);
      end;
    else
      Result:=kPGPError_InvalidProperty;
    end;
  end;
end;

// takes PGP 7.X PGPKeyDBObjProperty constants
function PGPNewKeyDBObjNumericFilter(Context: pPGPContext;
		WhichProperty: PGPKeyDBObjProperty; MatchValue: PGPUInt32;
		MatchCriteria: PGPMatchCriterion; var OutFilter: pPGPFilter): PGPError;
begin
  if PGP7X then
    Result:=PGPNewKeyDBObjNumericFilter7(Context, WhichProperty, MatchValue, MatchCriteria, OutFilter)
  else begin
    // only implements selected filters
    case WhichProperty of
      kPGPKeyProperty_AlgorithmID: Result:=PGPNewKeySigAlgorithmFilter(Context, MatchValue, OutFilter);
      kPGPKeyProperty_Validity: begin
	Result:=PGPNewKeyNumberFilter(Context, kPGPKeyPropValidity, MatchValue, MatchCriteria, OutFilter);
      end;
    else
      Result:=kPGPError_InvalidProperty;
    end;
  end;
end;

// takes PGP 7.X PGPKeyDBObjProperty constants
function PGPNewKeyDBObjTimeFilter(Context: pPGPContext;
		WhichProperty: PGPKeyDBObjProperty; MatchValue: PGPTime;
		MatchCriteria: PGPMatchCriterion; var OutFilter: pPGPFilter): PGPError;
begin
  if PGP7X then
    Result:=PGPNewKeyDBObjTimeFilter7(Context, WhichProperty, MatchValue, MatchCriteria, OutFilter)
  else begin
    // only implements selected filters
    case WhichProperty of
      kPGPKeyProperty_Creation: Result:=PGPNewKeyCreationTimeFilter(Context, MatchValue, MatchCriteria, OutFilter);
      kPGPKeyProperty_Expiration: Result:=PGPNewKeyExpirationTimeFilter(Context, MatchValue, MatchCriteria, OutFilter);
    else
      Result:=kPGPError_InvalidProperty;
    end;
  end;
end;

// takes PGP 7.X PGPKeyDBObjProperty constants
function PGPNewKeyDBObjDataFilter(Context: pPGPContext;
		WhichProperty: PGPKeyDBObjProperty; const MatchData: Pointer;
		MatchDataSize: PGPSize; MatchCriteria: PGPMatchCriterion;
		var OutFilter: pPGPFilter): PGPError;
begin
  if PGP7X then
    Result:=PGPNewKeyDBObjDataFilter7(Context, WhichProperty, MatchData, MatchDataSize, MatchCriteria, OutFilter)
  else begin
    // only implements selected filters
    case WhichProperty of
      kPGPKeyProperty_KeyID: Result:=PGPNewKeyIDFilter(Context, pPGPKeyID7(MatchData)^, OutFilter);
      kPGPSubKeyProperty_KeyID: Result:=PGPNewSubKeyIDFilter(Context, pPGPKeyID7(MatchData)^, OutFilter);
      kPGPSigProperty_KeyID: Result:=PGPNewSigKeyIDFilter(Context, pPGPKeyID7(MatchData)^, OutFilter);
      kPGPUserIDProperty_Name: Result:=PGPNewUserIDStringFilter(Context, MatchData, MatchCriteria, OutFilter);
      kPGPUserIDProperty_CommonName: Result:=PGPNewUserIDNameFilter(Context, MatchData, MatchCriteria, OutFilter);
      kPGPUserIDProperty_EmailAddress: Result:=PGPNewUserIDEmailFilter(Context, MatchData, MatchCriteria, OutFilter);
    else
      Result:=kPGPError_InvalidProperty;
    end;
  end;
end;

// takes PGP 7.X PGPKeyDBObjProperty constants
function PGPGetKeyDBObjBooleanProperty(Obj: Pointer;
		WhichProperty: PGPKeyDBObjProperty; var Prop: PGPBoolean): PGPError;
begin
  if PGP7X then
    Result:=PGPGetKeyDBObjBooleanProperty7(pPGPKeyDBObj(Obj), WhichProperty, Prop)
  else begin
    // only implements compatible properties
    case WhichProperty of
      kPGPKeyProperty_IsSecret..kPGPKeyProperty_HasThirdPartyRevocation: begin
	WhichProperty:=WhichProperty - kPGPKeyProperty_IsSecret + kPGPKeyPropIsSecret;
	Result:=PGPGetKeyBoolean(pPGPKey(Obj), WhichProperty, Prop);
      end;
      kPGPSubKeyProperty_IsRevoked..kPGPSubKeyProperty_HasThirdPartyRevocation: begin
	case WhichProperty of
	  kPGPSubKeyProperty_IsRevoked: WhichProperty:=kPGPKeyPropIsRevoked;
	  kPGPSubKeyProperty_IsNotCorrupt..kPGPSubKeyProperty_HasUnverifiedRevocation: begin
	    WhichProperty:=WhichProperty - kPGPSubKeyProperty_IsNotCorrupt + kPGPKeyPropIsNotCorrupt;
	  end;
	  kPGPSubKeyProperty_IsRevocable..kPGPSubKeyProperty_HasThirdPartyRevocation: begin
	    WhichProperty:=WhichProperty - kPGPSubKeyProperty_IsRevocable + kPGPKeyPropIsRevocable;
	  end;
	end;
	Result:=PGPGetSubKeyBoolean(pPGPSubKey(Obj), WhichProperty, Prop);
      end;
      kPGPUserIDProperty_IsAttribute: begin
	WhichProperty:=kPGPUserIDPropIsAttribute;
	Result:=PGPGetUserIDBoolean(pPGPUserID(Obj), WhichProperty, Prop);
      end;
      kPGPSigProperty_IsRevoked..kPGPSigProperty_IsX509: begin
	WhichProperty:=WhichProperty - kPGPSigProperty_IsRevoked + kPGPSigPropIsRevoked;
	Result:=PGPGetSigBoolean(pPGPSig(Obj), WhichProperty, Prop);
      end;
    else
      Result:=kPGPError_InvalidProperty;
    end;
  end;
end;

// takes PGP 7.X PGPKeyDBObjProperty constants
function PGPGetKeyDBObjNumericProperty(Obj: Pointer;
		WhichProperty: PGPKeyDBObjProperty; var Prop: PGPInt32): PGPError;
begin
  if PGP7X then
    Result:=PGPGetKeyDBObjNumericProperty7(pPGPKeyDBObj(Obj), WhichProperty, Prop)
  else begin
    // only implements compatible properties
    case WhichProperty of
      kPGPKeyProperty_AlgorithmID..kPGPKeyProperty_Flags: begin
	WhichProperty:=WhichProperty - kPGPKeyProperty_AlgorithmID + kPGPKeyPropAlgID;
	Result:=PGPGetKeyNumber(pPGPKey(Obj), WhichProperty, Prop);
      end;
      kPGPKeyProperty_HashAlgorithmID: Result:=PGPGetHashAlgUsed(pPGPKey(Obj), Prop);
      kPGPSubKeyProperty_AlgorithmID..kPGPSubKeyProperty_Flags: begin
	case WhichProperty of
	  kPGPSubKeyProperty_AlgorithmID..kPGPSubKeyProperty_Bits: begin
	    WhichProperty:=WhichProperty - kPGPSubKeyProperty_AlgorithmID + kPGPKeyPropAlgID;
	  end;
	  kPGPSubKeyProperty_LockingAlgorithmID..kPGPSubKeyProperty_LockingBits: begin
	    WhichProperty:=WhichProperty - kPGPSubKeyProperty_LockingAlgorithmID + kPGPKeyPropLockingAlgID;
	  end;
	  kPGPSubKeyProperty_Version: begin
	    Result:=kPGPError_InvalidProperty;
	    Exit;
	  end;
	  kPGPSubKeyProperty_Flags: WhichProperty:=kPGPKeyProperty_Flags;
	end;
	Result:=PGPGetSubKeyNumber(pPGPSubKey(Obj), WhichProperty, Prop);
      end;
      kPGPUserIDProperty_Validity..kPGPUserIDProperty_AttributeType: begin
	WhichProperty:=WhichProperty - kPGPUserIDProperty_Validity + kPGPUserIDPropValidity;
	Result:=PGPGetUserIDNumber(pPGPUserID(Obj), WhichProperty, Prop);
      end;
      kPGPSigProperty_AlgorithmID..kPGPSigProperty_TrustValue: begin
	WhichProperty:=WhichProperty - kPGPSigProperty_AlgorithmID + kPGPSigPropTrustValue;
	Result:=PGPGetSigNumber(pPGPSig(Obj), WhichProperty, Prop);
      end;
    else
      Result:=kPGPError_InvalidProperty;
    end;
  end;
end;

// takes PGP 7.X PGPKeyDBObjProperty constants
function PGPGetKeyDBObjTimeProperty(Obj: Pointer;
		WhichProperty: PGPKeyDBObjProperty; var Prop: PGPTime): PGPError;
begin
  if PGP7X then
    Result:=PGPGetKeyDBObjTimeProperty7(pPGPKeyDBObj(Obj), WhichProperty, Prop)
  else begin
    // only implements compatible properties
    case WhichProperty of
      kPGPKeyProperty_Creation..kPGPKeyProperty_Expiration: begin
	WhichProperty:=WhichProperty - kPGPKeyProperty_Creation + kPGPKeyPropCreation;
	Result:=PGPGetKeyTime(pPGPKey(Obj), WhichProperty, Prop);
      end;
      kPGPSubKeyProperty_Creation..kPGPSubKeyProperty_Expiration: begin
	WhichProperty:=WhichProperty - kPGPSubKeyProperty_Creation + kPGPKeyPropCreation;
	Result:=PGPGetSubKeyTime(pPGPSubKey(Obj), WhichProperty, Prop);
      end;
      kPGPSigProperty_Creation..kPGPSigProperty_Expiration: begin
	WhichProperty:=WhichProperty - kPGPSigProperty_Creation + kPGPSigPropCreation;
	Result:=PGPGetSigTime(pPGPSig(Obj), WhichProperty, Prop);
      end;
    else
      Result:=kPGPError_InvalidProperty;
    end;
  end;
end;

// takes PGP 7.X PGPKeyDBObjProperty constants
function PGPGetKeyDBObjDataProperty(Obj: Pointer;
		WhichProperty: PGPKeyDBObjProperty; Buffer: PChar;
		BufferSize: PGPSize; var DataSize: PGPSize): PGPError;
begin
  if PGP7X then
    Result:=PGPGetKeyDBObjDataProperty7(pPGPKeyDBObj(Obj), WhichProperty, Buffer, BufferSize, DataSize)
  else begin
    // only implements compatible properties
    case WhichProperty of
      kPGPKeyProperty_Fingerprint..kPGPKeyProperty_ThirdPartyRevocationKeyID: begin
	case WhichProperty of
	  kPGPKeyProperty_Fingerprint: WhichProperty:=kPGPKeyPropFingerprint;
	  kPGPKeyProperty_KeyID: begin
	    Result:=kPGPError_InvalidProperty;
	    Exit;
	  end;
	  kPGPKeyProperty_PreferredAlgorithms..kPGPKeyProperty_ThirdPartyRevocationKeyID: begin
	    WhichProperty:=WhichProperty - kPGPKeyProperty_PreferredAlgorithms + kPGPKeyPropPreferredAlgorithms;
	  end;
	end;
	Result:=PGPGetKeyPropertyBuffer(pPGPKey(Obj), WhichProperty, BufferSize, Buffer, DataSize);
      end;
      kPGPUserIDProperty_Name..kPGPUserIDProperty_AttributeData: begin
	WhichProperty:=WhichProperty - kPGPUserIDProperty_Name + kPGPUserIDPropName;
	Result:=PGPGetUserIDStringBuffer(pPGPUserID(Obj), WhichProperty, BufferSize, Buffer, DataSize);
      end;
      kPGPSigProperty_KeyID..kPGPSigProperty_X509Certificate: begin
	WhichProperty:=WhichProperty - kPGPSigProperty_KeyID + kPGPSigPropKeyID;
	Result:=PGPGetSigPropertyBuffer(pPGPSig(Obj), WhichProperty, BufferSize, Buffer, DataSize);
      end;
    else
      Result:=kPGPError_InvalidProperty;
    end;
  end;
end;

initialization

  if PGPInitErrorCode=ieNone then begin
    if PGP7X then begin
      kPGPKeyIDString_Abbreviated:=1;
      kPGPKeyIDString_Full:= 2;
      PGPNewKeyDB:=GetProcAddress(hPGPsdkLib, 'PGPNewKeyDB');
      PGPOpenKeyDBFile:=GetProcAddress(hPGPsdkLib, 'PGPOpenKeyDBFile');
      PGPFreeKeyDB:=GetProcAddress(hPGPsdkLib, 'PGPFreeKeyDB');
      PGPFlushKeyDB:=GetProcAddress(hPGPsdkLib, 'PGPFlushKeyDB');
      PGPIncKeyDBRefCount:=GetProcAddress(hPGPsdkLib, 'PGPIncKeyDBRefCount');
      PGPKeyDBIsMutable:=GetProcAddress(hPGPsdkLib, 'PGPKeyDBIsMutable');
      PGPKeyDBIsUpdated:=GetProcAddress(hPGPsdkLib, 'PGPKeyDBIsUpdated');
      PGPCountKeysInKeyDB:=GetProcAddress(hPGPsdkLib, 'PGPCountKeysInKeyDB');
      PGPPeekKeyDBRootKeySet:=GetProcAddress(hPGPsdkLib, 'PGPPeekKeyDBRootKeySet');
      PGPPeekKeySetKeyDB:=GetProcAddress(hPGPsdkLib, 'PGPPeekKeySetKeyDB');
      PGPNewKeySet:=GetProcAddress(hPGPsdkLib, 'PGPNewKeySet');
      PGPNewEmptyKeySet:=GetProcAddress(hPGPsdkLib, 'PGPNewEmptyKeySet');
      PGPNewSingletonKeySet:=GetProcAddress(hPGPsdkLib, 'PGPNewOneKeySet');
      PGPIncKeySetRefCount:=GetProcAddress(hPGPsdkLib, 'PGPIncKeySetRefCount');
      PGPFreeKeySet:=GetProcAddress(hPGPs

⌨️ 快捷键说明

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