Exemplo n.º 1
0
        internal ModDetectionCodePacket(
            BcpgInputStream bcpgIn)
        {
            if (bcpgIn == null)
            {
                throw new ArgumentNullException("bcpgIn");
            }

            this.digest = new byte[20];
            bcpgIn.ReadFully(this.digest);
        }
Exemplo n.º 2
0
        public MPInteger(
            BcpgInputStream bcpgIn)
        {
            if (bcpgIn == null)
            {
                throw new ArgumentNullException("bcpgIn");
            }

            int length = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte();

            byte[] bytes = new byte[(length + 7) / 8];

            bcpgIn.ReadFully(bytes);

            this.val = new BigInteger(1, bytes);
        }
Exemplo n.º 3
0
        protected static byte[] ReadBytesOfEncodedLength(
            BcpgInputStream bcpgIn)
        {
            int length = bcpgIn.ReadByte();

            if (length == 0 || length == 0xFF)
            {
                throw new IOException("future extensions not yet implemented.");
            }

            byte[] buffer = new byte[length + 2];
            bcpgIn.ReadFully(buffer, 2, buffer.Length - 2);
            buffer[0] = (byte)0x06;
            buffer[1] = (byte)length;

            return(buffer);
        }
Exemplo n.º 4
0
        internal SecretKeyPacket(
            BcpgInputStream bcpgIn)
        {
            if (this is SecretSubkeyPacket)
            {
                pubKeyPacket = new PublicSubkeyPacket(bcpgIn);
            }
            else
            {
                pubKeyPacket = new PublicKeyPacket(bcpgIn);
            }

            s2kUsage = bcpgIn.ReadByte();

            if (s2kUsage == UsageChecksum || s2kUsage == UsageSha1)
            {
                encAlgorithm = (SymmetricKeyAlgorithmTag)bcpgIn.ReadByte();
                s2k          = new S2k(bcpgIn);
            }
            else
            {
                encAlgorithm = (SymmetricKeyAlgorithmTag)s2kUsage;
            }

            if (!(s2k != null && s2k.Type == S2k.GnuDummyS2K && s2k.ProtectionMode == 0x01))
            {
                if (s2kUsage != 0)
                {
                    if (((int)encAlgorithm) < 7)
                    {
                        iv = new byte[8];
                    }
                    else
                    {
                        iv = new byte[16];
                    }
                    bcpgIn.ReadFully(iv);
                }
            }

            secKeyData = bcpgIn.ReadAll();
        }
Exemplo n.º 5
0
        /// <param name="bcpgIn">The stream to read the packet from.</param>
        public ECDHPublicBcpgKey(
            BcpgInputStream bcpgIn)
            : base(bcpgIn)
        {
            int length = bcpgIn.ReadByte();

            byte[] kdfParameters = new byte[length];
            if (kdfParameters.Length != 3)
            {
                throw new InvalidOperationException("kdf parameters size of 3 expected.");
            }

            bcpgIn.ReadFully(kdfParameters);

            reserved       = kdfParameters[0];
            hashFunctionId = (HashAlgorithmTag)kdfParameters[1];
            symAlgorithmId = (SymmetricKeyAlgorithmTag)kdfParameters[2];

            VerifyHashAlgorithm();
            VerifySymmetricKeyAlgorithm();
        }
Exemplo n.º 6
0
        internal SignaturePacket(
            BcpgInputStream bcpgIn)
        {
            version = bcpgIn.ReadByte();

            if (version == 3 || version == 2)
            {
//                int l =
                bcpgIn.ReadByte();

                signatureType = bcpgIn.ReadByte();
                creationTime  = (((long)bcpgIn.ReadByte() << 24) | ((long)bcpgIn.ReadByte() << 16)
                                 | ((long)bcpgIn.ReadByte() << 8) | (uint)bcpgIn.ReadByte()) * 1000L;

                keyId |= (long)bcpgIn.ReadByte() << 56;
                keyId |= (long)bcpgIn.ReadByte() << 48;
                keyId |= (long)bcpgIn.ReadByte() << 40;
                keyId |= (long)bcpgIn.ReadByte() << 32;
                keyId |= (long)bcpgIn.ReadByte() << 24;
                keyId |= (long)bcpgIn.ReadByte() << 16;
                keyId |= (long)bcpgIn.ReadByte() << 8;
                keyId |= (uint)bcpgIn.ReadByte();

                keyAlgorithm  = (PublicKeyAlgorithmTag)bcpgIn.ReadByte();
                hashAlgorithm = (HashAlgorithmTag)bcpgIn.ReadByte();
            }
            else if (version == 4)
            {
                signatureType = bcpgIn.ReadByte();
                keyAlgorithm  = (PublicKeyAlgorithmTag)bcpgIn.ReadByte();
                hashAlgorithm = (HashAlgorithmTag)bcpgIn.ReadByte();

                int    hashedLength = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte();
                byte[] hashed       = new byte[hashedLength];

                bcpgIn.ReadFully(hashed);

                //
                // read the signature sub packet data.
                //
                SignatureSubpacketsParser sIn = new SignatureSubpacketsParser(
                    new MemoryStream(hashed, false));

                IList v = Platform.CreateArrayList();
                SignatureSubpacket sub;
                while ((sub = sIn.ReadPacket()) != null)
                {
                    v.Add(sub);
                }

                hashedData = new SignatureSubpacket[v.Count];

                for (int i = 0; i != hashedData.Length; i++)
                {
                    SignatureSubpacket p = (SignatureSubpacket)v[i];
                    if (p is IssuerKeyId)
                    {
                        keyId = ((IssuerKeyId)p).KeyId;
                    }
                    else if (p is SignatureCreationTime)
                    {
                        creationTime = DateTimeUtilities.DateTimeToUnixMs(
                            ((SignatureCreationTime)p).GetTime());
                    }

                    hashedData[i] = p;
                }

                int    unhashedLength = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte();
                byte[] unhashed       = new byte[unhashedLength];

                bcpgIn.ReadFully(unhashed);

                sIn = new SignatureSubpacketsParser(new MemoryStream(unhashed, false));

                v.Clear();

                while ((sub = sIn.ReadPacket()) != null)
                {
                    v.Add(sub);
                }

                unhashedData = new SignatureSubpacket[v.Count];

                for (int i = 0; i != unhashedData.Length; i++)
                {
                    SignatureSubpacket p = (SignatureSubpacket)v[i];
                    if (p is IssuerKeyId)
                    {
                        keyId = ((IssuerKeyId)p).KeyId;
                    }

                    unhashedData[i] = p;
                }
            }
            else
            {
                throw new Exception("unsupported version: " + version);
            }

            fingerprint = new byte[2];
            bcpgIn.ReadFully(fingerprint);

            switch (keyAlgorithm)
            {
            case PublicKeyAlgorithmTag.RsaGeneral:
            case PublicKeyAlgorithmTag.RsaSign:
                MPInteger v = new MPInteger(bcpgIn);
                signature = new MPInteger[] { v };
                break;

            case PublicKeyAlgorithmTag.Dsa:
                MPInteger r = new MPInteger(bcpgIn);
                MPInteger s = new MPInteger(bcpgIn);
                signature = new MPInteger[] { r, s };
                break;

            case PublicKeyAlgorithmTag.ElGamalEncrypt:     // yep, this really does happen sometimes.
            case PublicKeyAlgorithmTag.ElGamalGeneral:
                MPInteger p = new MPInteger(bcpgIn);
                MPInteger g = new MPInteger(bcpgIn);
                MPInteger y = new MPInteger(bcpgIn);
                signature = new MPInteger[] { p, g, y };
                break;

            case PublicKeyAlgorithmTag.ECDsa:
                MPInteger ecR = new MPInteger(bcpgIn);
                MPInteger ecS = new MPInteger(bcpgIn);
                signature = new MPInteger[] { ecR, ecS };
                break;

            default:
                if (keyAlgorithm >= PublicKeyAlgorithmTag.Experimental_1 && keyAlgorithm <= PublicKeyAlgorithmTag.Experimental_11)
                {
                    signature = null;
                    MemoryStream bOut = new MemoryStream();
                    int          ch;
                    while ((ch = bcpgIn.ReadByte()) >= 0)
                    {
                        bOut.WriteByte((byte)ch);
                    }
                    signatureEncoding = bOut.ToArray();
                }
                else
                {
                    throw new IOException("unknown signature key algorithm: " + keyAlgorithm);
                }
                break;
            }
        }