public override void Parse(TreeBuilder tree) { S2K = new S2K(); Version = tree.ReadByte("Version"); S2K.SymAlgo = tree.ReadByte("Symmetric Algorithm", SymmetricAlgorithmTypes.Get); byte S2KSpecifier = tree.ReadByte("S2K Specifier", S2KTypes.Get); if (S2KSpecifier != S2KTypes.Salted && S2KSpecifier != S2KTypes.Simple && S2KSpecifier != S2KTypes.IteratedAndSalted) { throw new InvalidDataException("Invalid S2K Specifier"); } S2K.HashAlgorithm = tree.ReadByte("Hash Algorithm", HashAlgorithmTypes.Get); if (S2KSpecifier == S2KTypes.Salted || S2KSpecifier == S2KTypes.IteratedAndSalted) { S2K.Salt = tree.ReadBytes("Salt", 8); if (S2KSpecifier == S2KTypes.IteratedAndSalted) { byte CodedCount = tree.ReadByte("Coded Iteration"); S2K.ByteCount = (16 + (CodedCount & 15)) << ((CodedCount >> 4) + 6); } } if (tree.IsMoreData()) { EncryptedSessionKey = tree.ReadBytes("Encrypted Session Key"); } else { EncryptedSessionKey = null; } }
public override void Parse(TreeBuilder tree) { tree.ReadByte("Compression Algorithm", CompressionAlgorithmTypes.Get); tree.SkipBytes("Compressed Data"); ThisBlock = tree.CurrentBlock; ThisBlock.ProcessBlock += ExtractData; }
public override void LoadPublicKey(TreeBuilder tree) { tree.AddNode("RSA Public Key"); tree.PushByteBlock(); ModN = tree.ReadMPIBytes("Mod n"); Exp = tree.ReadMPIBytes("Exp"); tree.PopByteBlock(); }
public override void Parse(TreeBuilder tree) { Version = tree.ReadByte("Version"); var block = tree.SkipBytes("Encrypted Data"); block.ProcessBlock += ExtractData; ThisBlock = block; }
public override void LoadPublicKey(TreeBuilder tree) { tree.AddNode("Elgamal Public Key"); tree.PushByteBlock(); P = tree.ReadMPIBytes("Elgamal Prime p"); G = tree.ReadMPIBytes("Elgamal group generator g"); Y = tree.ReadMPIBytes("Elgamal public key value y"); tree.PopByteBlock(); }
public override void LoadPublicKey(TreeBuilder tree) { tree.AddNode("DSA Public Key"); tree.PushByteBlock(); P = tree.ReadMPIBytes("DSA Prime p"); Q = tree.ReadMPIBytes("DSA group order q"); G = tree.ReadMPIBytes("DSA group generator g"); Y = tree.ReadMPIBytes("DSA public key value y"); tree.PopByteBlock(); }
public override ITransformedData LoadPublicKeyTransformedData(TreeBuilder tree) { var Data = new PublicKeyTransformedRSAData(); tree.AddNode("RSA Encrypted Data"); tree.PushByteBlock(); Data.M_E_ModN = tree.ReadMPIBytes("m ^ e Mod n"); tree.PopByteBlock(); return(Data); }
public override void Parse(TreeBuilder tree) { tree.SetBookMark(); Version = tree.ReadByte("Version"); tree.ReadFormatted("Creation Time", BlockFormat.UnixTime); if (Version == 2 || Version == 3 || Version == 4) { if (Version != 4) { tree.ReadNumber("Days Valid", 2); } } else { throw new NotImplementedException("Unknown Public Key Packet Version Code: " + Version.ToString()); } byte AlgoCode = tree.ReadByte("Public Key Algorithm", PKAlgorithmTypes.Get); PublicKeyAlgorithm = PKAlgorithm.CreatePKAlgorithm(AlgoCode); if (PublicKeyAlgorithm == null) { tree.ReadBytes("Unknown Public Key Algorithm"); return; } PublicKeyAlgorithm.LoadPublicKey(tree); PacketDataPublicKey = tree.ReadBytesFromBookMark(); if (Version < 4) { // Only RSA is supported var ModN = ((RSA)PublicKeyAlgorithm).ModN; //KeyId = BitConverter.ToString(ModN, ModN.Length - 8); KeyId = ModN.SubArray(ModN.Length - 8, 8); } else { int l = PacketDataPublicKey.Length; SHA1 shaM = new SHA1Managed(); byte[] HashContext = new byte[l + 3]; HashContext[0] = 0x99; HashContext[1] = (byte)((l & 0xFF00) >> 8); HashContext[2] = (byte)(l & 0x00FF); Array.Copy(PacketDataPublicKey, 0, HashContext, 3, l); byte[] result = shaM.ComputeHash(HashContext); //KeyId = BitConverter.ToString(result, result.Length - 8); KeyId = result.SubArray(result.Length - 8, 8); } tree.AddCalculated("Key Id", BitConverter.ToString(KeyId), KeyId); }
// Signature Value // ??? public override ITransformedData LoadPublicKeyTransformedData(TreeBuilder tree) { var Data = new PublicKeyTransformedElgamalData(); tree.AddNode("Elgamal Encrypted Data"); tree.PushByteBlock(); Data.G_K_ModP = tree.ReadMPIBytes("g ^ k Mod p"); Data.M_Y_K_ModP = tree.ReadMPIBytes("m * y ^ k Mod p"); tree.PopByteBlock(); return(Data); }
public override ITransformedData LoadSecretKeyTransformedData(TreeBuilder tree) { var Data = new SecretKeyTransformedRSAData(); tree.AddNode("RSA Signed Data"); tree.PushByteBlock(); Data.M_D_ModN = tree.ReadMPIBytes("m ^ d Mod n"); tree.PopByteBlock(); return(Data); }
public override ITransformedData LoadSecretKeyTransformedData(TreeBuilder tree) { var Data = new SecretKeyTransformedDSAData(); tree.AddNode("DSA Signed Data"); tree.PushByteBlock(); Data.R = tree.ReadMPIBytes("r"); Data.S = tree.ReadMPIBytes("r"); tree.PopByteBlock(); return(Data); }
public override void Parse(TreeBuilder tree) { tree.ReadByte("Data Format", true); byte FileNameLength = tree.ReadByte(); byte[] FileNameBytes = tree.ReadBytes("File Name", FileNameLength, true); ExtractFileName = System.Text.Encoding.UTF8.GetString(FileNameBytes); tree.ReadFormatted("Date/Time", BlockFormat.UnixTime); tree.RemainingBytes("Literal Data"); ThisBlock = tree.CurrentBlock; ThisBlock.ProcessBlock += ExtractData; }
private void ReadByteTags(TreeBuilder tree, string Label, int DataLength, Func <byte, string> DescFunc) { tree.AddNode(Label); tree.PushByteBlock(); //tree.AddChildLevel = true; while (DataLength-- > 0) { tree.SetBookMark(); byte ByteCode = tree.ReadByte(); tree.GoToBookMark(); //tree.Seek(-1); tree.ReadByte(DescFunc(ByteCode)); } tree.PopByteBlock(); }
//public HashAlgorithm HashAlgo { private set; get; } public override void Parse(TreeBuilder tree) { Version = tree.ReadByte("Version"); SignatureType = tree.ReadByte("Signature Type", SignatureTypes.Get); HashAlgorithm = tree.ReadByte("Hash Algorithm", HashAlgorithmTypes.Get); //try //{ // HashAlgo = null; // HashAlgo = HashAlgorithmTypes.GetHashAlgoManaged(HashAlgorithm); //} //catch { } PKAlgorithm = tree.ReadByte("Primary Key Algorithm", PKAlgorithmTypes.Get); tree.ReadBytes("Key ID", 8); Flag = tree.ReadByte("Flag"); }
public override void Parse(TreeBuilder tree) { Version = tree.ReadByte("Version"); KeyId = tree.ReadBytes("Key Id", 8); PKAlgoCode = tree.ReadByte("PK Algorithm", PKAlgorithmTypes.Get); var PublicKeyAlgorithm = PKAlgorithm.CreatePKAlgorithm(PKAlgoCode); PublicKeyTransformedData = null; if (PublicKeyAlgorithm == null) { tree.ReadBytes("Unknowon Encrypted Session Key"); } else { PublicKeyTransformedData = PublicKeyAlgorithm.LoadPublicKeyTransformedData(tree); } }
public override void Parse(TreeBuilder tree) { base.Parse(tree); bool IsEncrypted = true; tree.SetBookMark(); var S2K = new S2K { S2KUsage = tree.ReadByte() }; if (S2K.S2KUsage == 254 || S2K.S2KUsage == 255) { S2K.SymAlgo = tree.ReadByte("Symmetric Algorithm", SymmetricAlgorithmTypes.Get); byte S2KSpecifier = tree.ReadByte("S2K Specifier", S2KTypes.Get); if (S2KSpecifier != S2KTypes.Salted && S2KSpecifier != S2KTypes.Simple && S2KSpecifier != S2KTypes.IteratedAndSalted) { //tree.AddCalculated("Invalid S2K", S2KSpecifier.ToString()); tree.AddCalculated("Unable to Process", S2KSpecifier.ToString(), ByteBlockType.CalculatedError); return; } S2K.HashAlgorithm = tree.ReadByte("Hash Algorithm", HashAlgorithmTypes.Get); if (S2KSpecifier == S2KTypes.Salted || S2KSpecifier == S2KTypes.IteratedAndSalted) { S2K.Salt = tree.ReadBytes("Salt", 8); if (S2KSpecifier == S2KTypes.IteratedAndSalted) { byte CodedCount = tree.ReadByte("Coded Iteration"); S2K.ByteCount = (16 + (CodedCount & 15)) << ((CodedCount >> 4) + 6); } } int BlockSizeBytes = SymmetricAlgorithmTypes.GetBlockSize(S2K.SymAlgo) / 8; S2K.IV = tree.ReadBytes("IV", BlockSizeBytes); } else { byte SymAlgo = S2K.S2KUsage; S2K.SymAlgo = SymAlgo; if (SymAlgo != 0) { tree.GoToBookMark(); tree.ReadByte("Symmetric Algorithm", SymmetricAlgorithmTypes.Get); int BlockSize = SymmetricAlgorithmTypes.GetBlockSize(SymAlgo) / 8; S2K.IV = tree.ReadBytes("IV", BlockSize); } else { IsEncrypted = false; } } PublicKeyAlgorithm.S2K = S2K; if (IsEncrypted) { byte[] Encrypted = tree.ReadBytes("Encrypted Secret Key"); PublicKeyAlgorithm.EncryptedPrivateKey = Encrypted; tree.CurrentBlock.ProcessBlock += ExtractPrivateKey; SecretKeyNode = tree.CurrentBlock; } else { byte[] ClearBytes = tree.ReadBytes("Unencrypted Secret Key"); var SecBlockClear = PublicKeyAlgorithm.SetPrivate(ClearBytes); tree.AddChild(PublicKeyAlgorithm.GetPrivateByteBlocks()); } }
public override ITransformedData LoadSecretKeyTransformedData(TreeBuilder tree) { throw new NotImplementedException(); }
public abstract ITransformedData LoadSecretKeyTransformedData(TreeBuilder tree);
public abstract void Parse(TreeBuilder tree);
public override void Parse(TreeBuilder tree) { tree.ReadBytes("SHA-1", 20); }
public override void Parse(TreeBuilder tree) { UserIDBytes = tree.ReadBytes("User Id", true); }
public override void Parse(TreeBuilder tree) { }
public void Parse(TreeBuilder tree, int SubPacketsLength) { long SubPacketsEnd = tree.BaseReader.Position + SubPacketsLength; tree.PushByteBlock(); //if (tree.Position < SubPacketsEnd) // tree.AddChildLevel = true; while (tree.BaseReader.Position < SubPacketsEnd) { byte LengthFinder = tree.ReadByte(); uint SubPacketLength = 0; if (LengthFinder < 192) { SubPacketLength = LengthFinder; } else if (LengthFinder < 255) { byte b = tree.ReadByte(); SubPacketLength = (uint)((LengthFinder - 192) << 8) + b + 192; } else { byte[] b = new byte[4]; b[0] = tree.ReadByte(); b[1] = tree.ReadByte(); b[2] = tree.ReadByte(); b[3] = tree.ReadByte(); SubPacketLength = Program.GetBigEndian(b, 0, 4); } byte SubPacketType = tree.ReadByte(); string Label = SignatureSubPacketTypes.Get(SubPacketType); int DataLength = (int)SubPacketLength - 1; if (SubPacketType == SignatureSubPacketTypes.SignatureCreationTime) { tree.ReadFormatted(Label, BlockFormat.UnixTime); } else if (SubPacketType == SignatureSubPacketTypes.Issuer) { Issuer = tree.ReadBytes(Label, 8); } else if (SubPacketType == SignatureSubPacketTypes.KeyExpirationTime) { tree.ReadFormatted(Label, BlockFormat.Days); } else if (SubPacketType == SignatureSubPacketTypes.PreferredSymmetricAlgorithm) { ReadByteTags(tree, Label, DataLength, SymmetricAlgorithmTypes.Get); } else if (SubPacketType == SignatureSubPacketTypes.PreferredHashAlgorithm) { ReadByteTags(tree, Label, DataLength, HashAlgorithmTypes.Get); } else if (SubPacketType == SignatureSubPacketTypes.PreferredCompressionAlgorithm) { ReadByteTags(tree, Label, DataLength, CompressionAlgorithmTypes.Get); } else if (SubPacketType == SignatureSubPacketTypes.KeyFlags) { ReadByteTags(tree, Label, DataLength, KeyFlagsTypes.Get); } else if (SubPacketType == SignatureSubPacketTypes.PrimaryUserId) { tree.ReadBytes(Label, DataLength); } else if (SubPacketType == SignatureSubPacketTypes.ReasonForRevocation) { tree.AddNode(Label); tree.PushByteBlock(); tree.ReadByte("Reason", RevocationReasonTypes.Get); tree.ReadBytes("Message", DataLength - 1); tree.PopByteBlock(); } else { tree.ReadBytes(Label, DataLength); } } tree.PopByteBlock(); }
public abstract void LoadPublicKey(TreeBuilder tree);
public static ByteBlock Process(string PGPFile) { var Root = new ByteBlock(); PGPPacket pgp; PublicKeyPacket PrimaryKeyPacket = null; PublicKeyPacket SubKeyPacket = null; UserIDPacket UIDPacket = null; Stack <OnePassSignaturePacket> OPSigPacketStack = new Stack <OnePassSignaturePacket>(); HashAlgorithm[] HashAlgorithms = null; using (var PacketReader = new PGPReader(PGPFile)) { PacketReader.DoIndexUpdate += IndexUpdate; while ((pgp = PacketReader.ReadNextPacket()) != null) { TreeBuilder Tree = new TreeBuilder(PacketReader); pgp.Parse(Tree); if (pgp is LiteralDataPacket litPgp) { if (HashAlgorithms == null) { HashAlgorithms = GetHashAlgorithms(OPSigPacketStack); } litPgp.ProgressUpdate += StatusUpdate; litPgp.DoHash(PacketReader, HashAlgorithms); } else { if (pgp is OnePassSignaturePacket OPSig) { OPSigPacketStack.Push(OPSig); } else if (pgp.PacketTag == 6 || pgp.PacketTag == 5) { PrimaryKeyPacket = (PublicKeyPacket)pgp; } else if (pgp is UserIDPacket) { UIDPacket = (UserIDPacket)pgp; } else if (pgp.PacketTag == 14 || pgp.PacketTag == 7) { SubKeyPacket = (PublicKeyPacket)pgp; } else if (pgp is SignaturePacket Sig) { if ((Sig.SignatureType == 0x18 || Sig.SignatureType == 0x19) && PrimaryKeyPacket != null && SubKeyPacket != null && SubKeyPacket.PacketDataPublicKey != null) { Sig.GenerateSubKeyBindingHash(PrimaryKeyPacket, SubKeyPacket); } if (Sig.SignatureType >= 0x10 && Sig.SignatureType <= 0x13 && PrimaryKeyPacket != null && UIDPacket != null) { Sig.GenerateCertifyHash(PrimaryKeyPacket, UIDPacket); } // Signature of a Binary Document if (Sig.SignatureType == 0x00) { // Are we generating hash for One Pass Signature Packet if (OPSigPacketStack.Count() > 0) { var OnePassSignaturePacket = OPSigPacketStack.Pop(); if (OnePassSignaturePacket.HashAlgorithm == Sig.HashAlgorithm) { Sig.GenerateBinaryHash(HashAlgorithmTypes.GetHashAlgoManaged(Sig.HashAlgorithm, HashAlgorithms)); } } } } } PacketBlock pb = new PacketBlock(pgp); pb.AddChildBlock(Tree.StartBlock); Root.AddChildBlock(pb); _PacketNodes.Add(pb); } } return(Root.ChildBlock); }
public override void Parse(TreeBuilder tree) { Version = tree.ReadByte("Version"); SignatureType = tree.ReadByte("Signature Type", SignatureTypes.Get); PKAlgo = tree.ReadByte("Public Key Algorithm", PKAlgorithmTypes.Get); if (Version == 4) { uint SubPacketLength; HashAlgorithm = tree.ReadByte("Hash Algorithm", HashAlgorithmTypes.Get); SubPacketLength = tree.ReadNumber(2); tree.SetBookMark(); HashedSubPacketBytes = tree.ReadBytes("Hashed Sub Packet", (int)SubPacketLength); //tree.Seek(-SubPacketLength); tree.GoToBookMark(); var HashedSubPackets = new SignatureSubPackets(); HashedSubPackets.Parse(tree, (int)SubPacketLength); SubPacketLength = tree.ReadNumber(2); tree.SetBookMark(); UnHashedSubPacketBytes = tree.ReadBytes("Unhashed Sub Packet", (int)SubPacketLength); //tree.Seek(-SubPacketLength); tree.GoToBookMark(); var UnHashedSubPackets = new SignatureSubPackets(); UnHashedSubPackets.Parse(tree, (int)SubPacketLength); Issuer = UnHashedSubPackets.Issuer; tree.ReadBytes("Left 16 Bit Hash", 2); var PublicKeyAlgorithm = PKAlgorithm.CreatePKAlgorithm(PKAlgo); SecretKeyTransformedData = null; if (PublicKeyAlgorithm == null) { tree.ReadBytes("Unknowon Signature"); } else { SecretKeyTransformedData = PublicKeyAlgorithm.LoadSecretKeyTransformedData(tree); } //if (PublicKeySignature != null) //{ // tree.AddNode("Signature"); // tree.AddChild(PublicKeySignature.LoadSignatureValue(tree.ReadMPIBytes)); //} //else // tree.ReadBytes("Signature"); } else { throw new NotImplementedException("Not Implemented"); } }