/// <summary></summary> private void Initialise() { _iDBits = _iPBits = _iQBits = _iInverseQBits = _iOffsetSecretKey = 0; _eStringToKeyUsage = nStringToKeyUsage.Sha1; _eSymmetricKeyAlgorithm = nSymmetricKeyAlgorithm.Aes128; _eStringToKeySpecifier = nStringToKeySpecifier.SaltedAndIterated; _eHashAlgorithm = nHashAlgorithm.Sha1; _Cryptography = null; _PublicKey = null; _KeyParameters = new RSAParameters(); }
/// <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>(); }
/// <summary></summary> public PgpPrivateKey(PgpPacket FromPacket, EncryptionServices Cryptography) : base(FromPacket) { PgpPublicKeyUtility KeyUtility; Initialise(); _Cryptography = Cryptography; KeyUtility = new PgpPublicKeyUtility(_abRawBytes); // extract the public key bytes _eStatus = KeyUtility.eStatus; if (_eStatus == nStatus.OK) { _PublicKey = new PgpPublicKey(new PgpPacket(KeyUtility.abRawBytes, 0)); // and turn them into a public key _KeyParameters.Exponent = KeyUtility.abExponent; _KeyParameters.Modulus = KeyUtility.abModulus; _eStatus = _PublicKey.eStatus; if (_eStatus == nStatus.OK) { _iOffsetSecretKey = _PublicKey.iHeaderLength + _PublicKey.iDataLength; _eStringToKeyUsage = (nStringToKeyUsage)_abRawBytes[_iOffsetSecretKey++]; if ((_eStringToKeyUsage == nStringToKeyUsage.Sha1) || (_eStringToKeyUsage == nStringToKeyUsage.Checksum)) { _eSymmetricKeyAlgorithm = (nSymmetricKeyAlgorithm)_abRawBytes[_iOffsetSecretKey++]; _eStringToKeySpecifier = (nStringToKeySpecifier)_abRawBytes[_iOffsetSecretKey++]; _eHashAlgorithm = (nHashAlgorithm)_abRawBytes[_iOffsetSecretKey++]; if ((_eSymmetricKeyAlgorithm == nSymmetricKeyAlgorithm.Unencrypted) || (_eSymmetricKeyAlgorithm == nSymmetricKeyAlgorithm.Aes128)) { if (_eStringToKeySpecifier == nStringToKeySpecifier.GnuDummy) { throw new NotImplementedException("eStringToKeySpecifier == nStringToKeySpecifier.GnuDummy"); } } else { _eStatus = nStatus.AlgorithmNotSupported; } } else { _eStatus = nStatus.AlgorithmNotSupported; } } } }
/// <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 }