public PgpPublicKeyEncryptedKey(PgpPacket FromPacket) : base(FromPacket) { _abEncryptedKey = _abPublicKeyId = null; _iEncryptedKeyBits = 0; _sUserId = string.Empty; _ePublicKeyAlgorithm = nPublicKeyAlgorithm.RsaEncryptOrSign; _MatchedPublicKey = null; Parse(); }
public PgpPublicKey(ISlotInfo SlotInfo, byte[] abId, nPacketTag ePacketTag, DateTime Created, nPublicKeyAlgorithm ePublicKeyAlgorithm, byte[] abModulus, byte[] abExponent) : this(ePacketTag) { _SlotInfo = SlotInfo; _abId = abId; _Created = Created; _ePublicKeyAlgorithm = ePublicKeyAlgorithm; InitialiseKeyParameters(abModulus, abExponent); EncodeRawBytes(); }
private void Initialise() { _abId = null; _iModulusBits = _iExponentBits = 0; _Created = DateTime.UtcNow; _SlotInfo = null; _Signature = null; _ePublicKeyAlgorithm = nPublicKeyAlgorithm.RsaEncryptOrSign; _KeyParameters = new RSAParameters(); }
public PgpPublicKeyUtility(nPacketTag ePacketTag, DateTime Created, nPublicKeyAlgorithm ePublicKeyAlgorithm, byte[] abModulus, byte[] abExponent) : base(ePacketTag) { _eStatus = nStatus.OK; _Created = Created; _ePublicKeyAlgorithm = ePublicKeyAlgorithm; _iModulusBits = CountUsedBits(abModulus); _iExponentBits = CountUsedBits(abExponent); _abModulus = abModulus; _abExponent = abExponent; EncodeRawBytes(); }
/// <summary></summary> public void Parse() { byte bVersion; int i, iEncryptedKeyBytes; if ((_abRawBytes == null) || (_abRawBytes.Length < _iHeaderLength + ciEncryptedKeyHeaderLength) || (_ePacketTag != nPacketTag.PublicKeyEncryptedKey)) { _eStatus = nStatus.ParseError; } else if (_eStatus == nStatus.OK) { bVersion = _abRawBytes[_iHeaderLength]; if (bVersion == cbAllowedVersion) { _abPublicKeyId = new Byte[ciKeyIdLength]; for (i = 0; i < ciKeyIdLength; i++) { _abPublicKeyId[i] = _abRawBytes[_iHeaderLength + 1 + i]; } _ePublicKeyAlgorithm = (nPublicKeyAlgorithm)_abRawBytes[_iHeaderLength + 9]; if ((_ePublicKeyAlgorithm == nPublicKeyAlgorithm.RsaEncryptOrSign) || (_ePublicKeyAlgorithm == nPublicKeyAlgorithm.RsaEncryptOnly)) { _iEncryptedKeyBits = (_abRawBytes[_iHeaderLength + 10] << 8) | _abRawBytes[_iHeaderLength + 11]; iEncryptedKeyBytes = (_iEncryptedKeyBits + 7) >> 3; if (_abRawBytes.Length == _iHeaderLength + ciEncryptedKeyHeaderLength + iEncryptedKeyBytes) { _abEncryptedKey = new byte[iEncryptedKeyBytes]; for (i = 0; i < iEncryptedKeyBytes; i++) { _abEncryptedKey[i] = _abRawBytes[_iHeaderLength + ciEncryptedKeyHeaderLength + i]; } } else { _eStatus = nStatus.ParseError; } } else { _eStatus = nStatus.AlgorithmNotSupported; } } else { _eStatus = nStatus.VersionNotSupported; } } // else keep the value of _eStatus from PgpPacketBase }
/// <summary></summary> private void Initialise(nSignatureType eSignatureType) { _abFingerprint = _abHashFingerprint = _abSignature = null; _iHashedLength = _iSignatureBits = 0; _eVerified = nVerified.Pending; _eSignatureType = eSignatureType; _eTranslatedKeyFlags = nTranslatedKeyFlags.None; _ePublicKeyAlgorithm = nPublicKeyAlgorithm.RsaEncryptOrSign; _eHashAlgorithm = nHashAlgorithm.Sha512; _PrivateKeyPacket = null; _PublicKeyPacket = null; _UserIdPacket = null; _Cryptography = null; _ltSubpackets = new List <PgpSignatureSubpacket>(); }
public PgpPublicKey(PgpPacket FromPacket) : base(FromPacket) { PgpPublicKeyUtility KeyUtility; Initialise(); KeyUtility = new PgpPublicKeyUtility(_abRawBytes); _eStatus = KeyUtility.eStatus; if (_eStatus == nStatus.OK) { _Created = KeyUtility.Created; _ePublicKeyAlgorithm = KeyUtility.ePublicKeyAlgorithm; InitialiseKeyParameters(KeyUtility.abModulus, KeyUtility.abExponent); } }
public PgpPublicKeyUtility(byte[] abPacketBytes) : base(abPacketBytes, 0) { byte bVersion; int iModulusBytes, iModulusOffset, iExponentBytes, iExponentOffset; _iModulusBits = _iExponentBits = 0; _Created = DateTime.UtcNow; _ePublicKeyAlgorithm = nPublicKeyAlgorithm.RsaEncryptOrSign; _abModulus = _abExponent = null; if (_eStatus == PgpPacketBase.nStatus.OK) { iModulusOffset = _iHeaderLength + ciModulusOffset; if ((_abRawBytes == null) || (_abRawBytes.Length < iModulusOffset) || ((_ePacketTag != nPacketTag.PublicKey) && (_ePacketTag != nPacketTag.PublicSubkey) && (_ePacketTag != nPacketTag.PrivateKey) && (_ePacketTag != nPacketTag.PrivateSubkey))) { _eStatus = nStatus.ParseError; } else if (_eStatus == nStatus.OK) { bVersion = _abRawBytes[_iHeaderLength]; if (bVersion == cbAllowedVersion) { _Created = GetDateAt(_iHeaderLength + 1); _ePublicKeyAlgorithm = (nPublicKeyAlgorithm)_abRawBytes[_iHeaderLength + 5]; if ((_ePublicKeyAlgorithm == nPublicKeyAlgorithm.RsaEncryptOrSign) || (_ePublicKeyAlgorithm == nPublicKeyAlgorithm.RsaEncryptOnly) || (_ePublicKeyAlgorithm == nPublicKeyAlgorithm.RsaSignOnly)) { _iModulusBits = _abRawBytes[iModulusOffset - 2] << 8 | _abRawBytes[iModulusOffset - 1]; iModulusBytes = (_iModulusBits + 7) >> 3; iExponentOffset = iModulusOffset + iModulusBytes + 2; if ((_abRawBytes.Length < iExponentOffset) || (_Created.Year < 1970) || (_Created.Year > DateTime.Now.Year)) { _eStatus = nStatus.ParseError; } else { _iExponentBits = _abRawBytes[iExponentOffset - 2] << 8 | _abRawBytes[iExponentOffset - 1]; iExponentBytes = (_iExponentBits + 7) >> 3; if ((((_ePacketTag == nPacketTag.PublicKey) || (_ePacketTag == nPacketTag.PublicSubkey)) && (_abRawBytes.Length == iExponentOffset + iExponentBytes)) || (((_ePacketTag == nPacketTag.PrivateKey) || (_ePacketTag == nPacketTag.PrivateSubkey)) && (_abRawBytes.Length > iExponentOffset + iExponentBytes))) { _abModulus = CopyFromRawBytes(iModulusOffset, iModulusBytes); _abExponent = CopyFromRawBytes(iExponentOffset, iExponentBytes); if (_abRawBytes.Length > iExponentOffset + iExponentBytes) EncodeRawBytes(); // re-encode to truncate the raw bytes to only the public key } else _eStatus = nStatus.ParseError; } } else _eStatus = nStatus.AlgorithmNotSupported; } else _eStatus = nStatus.VersionNotSupported; } } }
/// <summary></summary> public void Parse(PgpPublicKey MasterKey, List <PgpToken> ltTokens = null) { bool isThisMasterKey = true; byte bVersion; byte[] abMasterFingerprint; int i, iSubpacketPointer, iSignatureBytes, iUnhashedLength; PgpSignature FoundSubkey; PgpSignatureSubpacket DecodedPgpSubpacket, RawPgpSubpacket; if ((_abRawBytes == null) || (_abRawBytes.Length < _iHeaderLength + ciSignatureHeaderLength) || (_ePacketTag != nPacketTag.Signature)) { _eStatus = nStatus.ParseError; } else if (_eStatus == nStatus.OK) { bVersion = _abRawBytes[_iHeaderLength]; if (bVersion == cbAllowedVersion) { _eSignatureType = (nSignatureType)_abRawBytes[_iHeaderLength + 1]; _ePublicKeyAlgorithm = (nPublicKeyAlgorithm)_abRawBytes[_iHeaderLength + 2]; _eHashAlgorithm = (nHashAlgorithm)_abRawBytes[_iHeaderLength + 3]; _iHashedLength = (_abRawBytes[_iHeaderLength + 4] << 8) | _abRawBytes[_iHeaderLength + 5]; if (((_ePublicKeyAlgorithm == nPublicKeyAlgorithm.RsaEncryptOrSign) || (_ePublicKeyAlgorithm == nPublicKeyAlgorithm.RsaSignOnly)) && (_eHashAlgorithm == nHashAlgorithm.Sha1) || (_eHashAlgorithm == nHashAlgorithm.Sha256) || (_eHashAlgorithm == nHashAlgorithm.Sha384) || (_eHashAlgorithm == nHashAlgorithm.Sha512)) { iSubpacketPointer = _iHeaderLength + ciSignatureHeaderLength; while ((_eStatus == nStatus.OK) && (iSubpacketPointer < _iHeaderLength + ciSignatureHeaderLength + _iHashedLength)) { RawPgpSubpacket = new PgpSignatureSubpacket(_abRawBytes, iSubpacketPointer); if (RawPgpSubpacket.eStatus == nStatus.OK) { switch (RawPgpSubpacket.eType) { case PgpSignatureSubpacket.nSubpacketType.CreationTime: DecodedPgpSubpacket = new PgpCreationTime(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.KeyExpireTime: DecodedPgpSubpacket = new PgpKeyExpireTime(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.PreferredSymmetricAlgorithms: DecodedPgpSubpacket = new PgpPreferredSymmetricAlgorithms(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.IssuerKeyId: DecodedPgpSubpacket = new PgpIssuerKeyId(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.PreferredHashAlgorithms: DecodedPgpSubpacket = new PgpPreferredHashAlgorithms(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.PreferredCompressionAlgorithms: DecodedPgpSubpacket = new PgpPreferredCompressionAlgorithms(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.KeyServerPreferences: DecodedPgpSubpacket = new PgpKeyServerPreferences(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.PrimaryUserId: DecodedPgpSubpacket = new PgpPrimaryUserId(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.KeyFlags: DecodedPgpSubpacket = new PgpKeyFlags(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.RevocationReason: DecodedPgpSubpacket = new PgpRevocationReason(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.Features: DecodedPgpSubpacket = new PgpFeatures(RawPgpSubpacket); break; case PgpSignatureSubpacket.nSubpacketType.MasterKeyFingerprint: DecodedPgpSubpacket = new PgpMasterKeyFingerprint(RawPgpSubpacket); break; default: DecodedPgpSubpacket = null; Console.WriteLine("not implemented: eType=" + RawPgpSubpacket.eType.ToString()); break; } if ((DecodedPgpSubpacket == null) || (DecodedPgpSubpacket.eStatus != nStatus.OK)) { _eStatus = nStatus.ParseError; } else { // Console.WriteLine("found eType=" + RawPgpSubpacket.eType.ToString()); _ltSubpackets.Add(DecodedPgpSubpacket); iSubpacketPointer += (RawPgpSubpacket.iHeaderLength + RawPgpSubpacket.iDataLength); } } else { _eStatus = nStatus.ParseError; } } iUnhashedLength = (_abRawBytes[iSubpacketPointer] << 8) | _abRawBytes[iSubpacketPointer + 1]; iSubpacketPointer += 2; while ((_eStatus == nStatus.OK) && (iSubpacketPointer < _iHashedLength + iUnhashedLength + 8)) { RawPgpSubpacket = new PgpSignatureSubpacket(_abRawBytes, iSubpacketPointer); if (RawPgpSubpacket.eStatus == nStatus.OK) { switch (RawPgpSubpacket.eType) { case PgpSignatureSubpacket.nSubpacketType.IssuerKeyId: DecodedPgpSubpacket = new PgpIssuerKeyId(RawPgpSubpacket); break; default: DecodedPgpSubpacket = null; Console.WriteLine("not implemented: eType=" + RawPgpSubpacket.eType.ToString()); break; } if ((DecodedPgpSubpacket == null) || (DecodedPgpSubpacket.eStatus != nStatus.OK)) { _eStatus = nStatus.ParseError; } else { _ltSubpackets.Add(DecodedPgpSubpacket); iSubpacketPointer += (RawPgpSubpacket.iHeaderLength + RawPgpSubpacket.iDataLength); } } else { _eStatus = nStatus.ParseError; } } _abFingerprint = ComputeFingerprint(); _eTranslatedKeyFlags = TranslateKeyFlags(); if (_abRawBytes.Length > iSubpacketPointer + 2) { _abHashFingerprint = CopyFromRawBytes(iSubpacketPointer, 2); _iSignatureBits = _abRawBytes[iSubpacketPointer + 2] << 8 | _abRawBytes[iSubpacketPointer + 3]; iSignatureBytes = (_iSignatureBits + 7) >> 3; iSubpacketPointer += 4; if (_abRawBytes.Length == iSubpacketPointer + iSignatureBytes) { _abSignature = CopyFromRawBytes(iSubpacketPointer, iSignatureBytes); if (_PublicKeyPacket != null) { _PublicKeyPacket.Signature = this; } abMasterFingerprint = abMasterKeyFingerprint; // do the underlying checks only once if ((abMasterFingerprint != null) && (abMasterFingerprint.Length == ciSha1FingerprintLength) && (MasterKey.Signature != null) && (MasterKey.Signature.abFingerprint != null) && (MasterKey.Signature.abFingerprint.Length == ciSha1FingerprintLength)) { for (i = 0; i < ciSha1FingerprintLength; i++) { isThisMasterKey = isThisMasterKey && (abMasterFingerprint[i] == MasterKey.Signature.abFingerprint[i]); } if (!isThisMasterKey) { MasterKey = null; if (ltTokens != null) { foreach (PgpToken Token in ltTokens) { if (MasterKey == null) { FoundSubkey = Token.GetSubkey(abMasterFingerprint); if (FoundSubkey != null) { MasterKey = FoundSubkey.PublicKeyPacket; } } } } } } if (MasterKey == null) { _eVerified = nVerified.KeyNotFound; } else if (Verify(MasterKey)) { _eVerified = nVerified.True; } else { _eVerified = nVerified.False; } } else { _eStatus = nStatus.ParseError; } } else { _eStatus = nStatus.ParseError; } } else { _eStatus = nStatus.AlgorithmNotSupported; } } else { _eStatus = nStatus.VersionNotSupported; } } // else keep the value of _eStatus from PgpPacketBase }