Esempio n. 1
0
        //public bool SeekPacket(int PacketIdx)
        //{
        //    Reset();

        //    while (ReadNextPacket() != null && PacketIdx != _PacketIndex) ;

        //    return (PacketIdx == _PacketIndex);
        //}

        //public byte[] ReadPacketBytes()
        //{
        //    // Todo fix for partial packets
        //    long len = _NextIdx - _Idx;
        //    long OldPos = _fs.Position;

        //    byte[] Buffer = new byte[len];
        //    _fs.Seek(_Idx, SeekOrigin.Begin);
        //    _fs.Read(Buffer, 0, (int)len);
        //    _fs.Seek(OldPos, SeekOrigin.Begin);

        //    return Buffer;

        //}




        private PGPPacket CreatePGPPacket(byte PacketTag)
        {
            PGPPacket PGPPacket;

            if (PacketTag == 1)
                PGPPacket = new PKEncSessionKeyPacket();
            else if (PacketTag == 2)
                PGPPacket = new SignaturePacket();
            else if (PacketTag == 3)
                PGPPacket = new SymEncSessionKeyPacket();
            else if (PacketTag == 4)
                PGPPacket = new OnePassSignaturePacket();
            else if (PacketTag == 5)
                PGPPacket = new SecretKeyPacket();
            else if (PacketTag == 6)
                PGPPacket = new PublicKeyPacket();
            else if (PacketTag == 7)
                PGPPacket = new SecretKeyPacket();
            else if (PacketTag == 8)
                PGPPacket = new CompressedDataPacket();
            else if (PacketTag == 9)
                PGPPacket = new SymEncDataPacket();
            else if (PacketTag == 10)
                PGPPacket = new MarkerPacket();
            else if (PacketTag == 11)
                PGPPacket = new LiteralDataPacket();
            else if (PacketTag == 12)
                PGPPacket = new TrustPacket();
            else if (PacketTag == 13)
                PGPPacket = new UserIDPacket();
            else if (PacketTag == 14)
                PGPPacket = new PublicKeyPacket();
            else if (PacketTag == 17)
                PGPPacket = new UserAttributePacket();
            else if (PacketTag == 18)
                PGPPacket = new SymEncIPDataPacket();
            else if (PacketTag == 19)
                PGPPacket = new MDCPacket();
            else
                PGPPacket = new UnknownPacket();

            Length = BytesRemaining;
            _PacketOffset = (int)( _fs.Position - _Idx);

            PGPPacket.Length = this.Length;
            PGPPacket.PacketTag = PacketTag;
            PGPPacket.PacketIndex = _PacketIndex;
            PGPPacket.FileName = this.FileName;

            return PGPPacket;
        }
Esempio n. 2
0
        public void GenerateCertifyHash(PublicKeyPacket PKP, UserIDPacket UIDP)
        {
            SignaturePacket SigP = this;

            int HashContextLength = 3 + PKP.PacketDataPublicKey.Length +
                                    5 + UIDP.UserIDBytes.Length +
                                    6 + SigP.HashedSubPacketBytes.Length
                                    + 6;

            int idx = 3 + PKP.PacketDataPublicKey.Length;

            byte[] HashContext = new byte[HashContextLength];

            HashContext[0] = 0x99;
            HashContext[1] = (byte)((PKP.PacketDataPublicKey.Length & 0xFF00) >> 8);
            HashContext[2] = (byte)(PKP.PacketDataPublicKey.Length & 0x00FF);
            Array.Copy(PKP.PacketDataPublicKey, 0, HashContext, 3, PKP.PacketDataPublicKey.Length);

            HashContext[idx++] = 0xB4;
            HashContext[idx++] = (byte)((UIDP.UserIDBytes.Length & 0xFF000000) >> 24);
            HashContext[idx++] = (byte)((UIDP.UserIDBytes.Length & 0x00FF0000) >> 16);
            HashContext[idx++] = (byte)((UIDP.UserIDBytes.Length & 0x0000FF00) >> 8);
            HashContext[idx++] = (byte)(UIDP.UserIDBytes.Length & 0x000000FF);
            Array.Copy(UIDP.UserIDBytes, 0, HashContext, idx, UIDP.UserIDBytes.Length);
            idx += UIDP.UserIDBytes.Length;

            HashContext[idx++] = SigP.Version;
            HashContext[idx++] = SigP.SignatureType;
            HashContext[idx++] = SigP.PKAlgo;
            HashContext[idx++] = SigP.HashAlgorithm;  // Version 4
            HashContext[idx++] = (byte)((SigP.HashedSubPacketBytes.Length & 0xFF00) >> 8);
            HashContext[idx++] = (byte)(SigP.HashedSubPacketBytes.Length & 0x00FF);
            Array.Copy(SigP.HashedSubPacketBytes, 0, HashContext, idx, SigP.HashedSubPacketBytes.Length);
            var HashedSubPacketLength = SigP.HashedSubPacketBytes.Length;

            idx += HashedSubPacketLength;

            HashContext[idx++] = 0x04;
            HashContext[idx++] = 0xFF;

            HashedSubPacketLength += 6; // Add the bytes before the hashed data

            HashContext[idx++] = (byte)((HashedSubPacketLength & 0xFF000000) >> 24);
            HashContext[idx++] = (byte)((HashedSubPacketLength & 0x00FF0000) >> 16);
            HashContext[idx++] = (byte)((HashedSubPacketLength & 0x0000FF00) >> 8);
            HashContext[idx++] = (byte)(HashedSubPacketLength & 0x000000FF);

            HashedData = ComputeHash(HashContext);
        }
Esempio n. 3
0
        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);
        }