Пример #1
0
        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);
        }
Пример #2
0
        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());
            }
        }