public BcpgOutputStream(Stream outStr, PacketTag tag, byte[] buffer)
        {
            //IL_000e: Unknown result type (might be due to invalid IL or missing references)
            //IL_0064: Unknown result type (might be due to invalid IL or missing references)
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }
            this.outStr = outStr;
            WriteHeader(tag, oldPackets: false, partial: true, 0L);
            partialBuffer = buffer;
            uint num = (uint)partialBuffer.Length;

            partialPower = 0;
            while (num != 1)
            {
                num >>= 1;
                partialPower++;
            }
            if (partialPower > 30)
            {
                throw new IOException("Buffer cannot be greater than 2^30 in length.");
            }
            partialBufferLength = 1 << partialPower;
            partialOffset       = 0;
        }
 internal ExperimentalPacket(
     PacketTag		tag,
     BcpgInputStream	bcpgIn)
 {
     _tag = tag;
     _contents = bcpgIn.ReadAll();
 }
        private void WriteHeader(
            PacketTag tag,
            bool oldPackets,
            bool partial,
            long bodyLen)
        {
            int hdr = 0x80;

            if (partialBuffer != null)
            {
                PartialFlush(true);
                partialBuffer = null;
            }

            if (oldPackets)
            {
                hdr |= ((int)tag) << 2;

                if (partial)
                {
                    this.WriteByte((byte)(hdr | 0x03));
                }
                else
                {
                    if (bodyLen <= 0xff)
                    {
                        this.WriteByte((byte)hdr);
                        this.WriteByte((byte)bodyLen);
                    }
                    else if (bodyLen <= 0xffff)
                    {
                        this.WriteByte((byte)(hdr | 0x01));
                        this.WriteByte((byte)(bodyLen >> 8));
                        this.WriteByte((byte)(bodyLen));
                    }
                    else
                    {
                        this.WriteByte((byte)(hdr | 0x02));
                        this.WriteByte((byte)(bodyLen >> 24));
                        this.WriteByte((byte)(bodyLen >> 16));
                        this.WriteByte((byte)(bodyLen >> 8));
                        this.WriteByte((byte)bodyLen);
                    }
                }
            }
            else
            {
                hdr |= 0x40 | (int)tag;
                this.WriteByte((byte)hdr);

                if (partial)
                {
                    partialOffset = 0;
                }
                else
                {
                    this.WriteNewPacketLength(bodyLen);
                }
            }
        }
        /// <summary>Create a stream representing a general packet.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">Packet tag.</param>
        /// <param name="length">Size of chunks making up the packet.</param>
        /// <param name="oldFormat">If true, the header is written out in old format.</param>
        public BcpgOutputStream(
            Stream outStr,
            PacketTag tag,
            long length,
            bool oldFormat)
        {
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }

            this.outStr = outStr;

            if (length > 0xFFFFFFFFL)
            {
                this.WriteHeader(tag, false, true, 0);
                this.partialBufferLength = 1 << BufferSizePower;
                this.partialBuffer       = new byte[partialBufferLength];
                this.partialPower        = BufferSizePower;
                this.partialOffset       = 0;
            }
            else
            {
                this.WriteHeader(tag, oldFormat, false, length);
            }
        }
Exemple #5
0
        public PgpSecretKeyRing(IPacketReader packetReader)
        {
            this.keys         = new List <PgpSecretKey>();
            this.extraPubKeys = new List <PgpPublicKey>();

            PacketTag initialTag = packetReader.NextPacketTag();

            if (initialTag != PacketTag.SecretKey && initialTag != PacketTag.SecretSubkey)
            {
                throw new PgpUnexpectedPacketException();
            }

            SecretKeyPacket secret = (SecretKeyPacket)packetReader.ReadContainedPacket();

            keys.Add(new PgpSecretKey(packetReader, secret, subKey: false));

            // Read subkeys
            while (packetReader.NextPacketTag() == PacketTag.SecretSubkey || packetReader.NextPacketTag() == PacketTag.PublicSubkey)
            {
                if (packetReader.NextPacketTag() == PacketTag.SecretSubkey)
                {
                    SecretSubkeyPacket sub = (SecretSubkeyPacket)packetReader.ReadContainedPacket();
                    keys.Add(new PgpSecretKey(packetReader, sub, subKey: true));
                }
                else
                {
                    PublicSubkeyPacket sub = (PublicSubkeyPacket)packetReader.ReadContainedPacket();
                    extraPubKeys.Add(new PgpPublicKey(packetReader, sub, subKey: true));
                }
            }
        }
Exemple #6
0
        public PgpPublicKeyRing(
            Stream inputStream)
        {
            this.keys = Platform.CreateArrayList();

            BcpgInputStream bcpgInput = BcpgInputStream.Wrap(inputStream);

            PacketTag initialTag = bcpgInput.NextPacketTag();

            if (initialTag != PacketTag.PublicKey && initialTag != PacketTag.PublicSubkey)
            {
                throw new IOException("public key ring doesn't start with public key tag: "
                                      + "tag 0x" + ((int)initialTag).ToString("X"));
            }

            PublicKeyPacket pubPk   = (PublicKeyPacket)bcpgInput.ReadPacket();;
            TrustPacket     trustPk = ReadOptionalTrustPacket(bcpgInput);

            // direct signatures and revocations
            IList keySigs = ReadSignaturesAndTrust(bcpgInput);

            IList ids, idTrusts, idSigs;

            ReadUserIDs(bcpgInput, out ids, out idTrusts, out idSigs);

            keys.Add(new PgpPublicKey(pubPk, trustPk, keySigs, ids, idTrusts, idSigs));


            // Read subkeys
            while (bcpgInput.NextPacketTag() == PacketTag.PublicSubkey)
            {
                keys.Add(ReadSubkey(bcpgInput));
            }
        }
 internal ExperimentalPacket(
     PacketTag tag,
     BcpgInputStream bcpgIn)
 {
     _tag      = tag;
     _contents = bcpgIn.ReadAll();
 }
Exemple #8
0
 internal ExperimentalPacket(
     PacketTag tag,
     Stream bcpgIn)
 {
     this.tag      = tag;
     this.contents = bcpgIn.ReadAll();
 }
Exemple #9
0
        /// <summary>Create a new style partial input stream buffered into chunks.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">Packet tag.</param>
        /// <param name="buffer">Buffer to use for collecting chunks.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag, byte[] buffer)
        {
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }

            _outStr = outStr;
            WriteHeader(tag, false, true, 0);

            _partialBuffer = buffer;

            var length = (uint)_partialBuffer.Length;

            for (_partialPower = 0; length != 1; _partialPower++)
            {
                length >>= 1;
            }

            if (_partialPower > 30)
            {
                throw new IOException("Buffer cannot be greater than 2^30 in length.");
            }
            _partialBufferLength = 1 << _partialPower;
            _partialOffset       = 0;
        }
        /// <summary>Create a stream representing an old style partial object.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">The packet tag for the object.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag)
        {
            if (outStr == null)
                throw new ArgumentNullException("outStr");

            _outStr = outStr;
            this.WriteHeader(tag, true, true, 0);
        }
 internal void WritePacket(
     PacketTag tag,
     byte[]              body,
     bool oldFormat)
 {
     this.WriteHeader(tag, oldFormat, false, body.Length);
     this.Write(body);
 }
 public BcpgOutputStream(Stream outStr, PacketTag tag)
 {
     if (outStr == null)
     {
         throw new ArgumentNullException("outStr");
     }
     this.outStr = outStr;
     WriteHeader(tag, oldPackets: true, partial: true, 0L);
 }
 public BcpgOutputStream(Stream outStr, PacketTag tag, long length)
 {
     if (outStr == null)
     {
         throw new ArgumentNullException("outStr");
     }
     this.outStr = outStr;
     WriteHeader(tag, oldPackets: false, partial: false, length);
 }
Exemple #14
0
        /// <summary>Create a new style partial input stream buffered into chunks.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">Packet tag.</param>
        /// <param name="length">Size of chunks making up the packet.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag, long length)
        {
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }

            _outStr = outStr;
            this.WriteHeader(tag, false, false, length);
        }
 public BcpgOutputStream(Stream outStr, PacketTag tag, long length)
 {
     //IL_000e: Unknown result type (might be due to invalid IL or missing references)
     if (outStr == null)
     {
         throw new ArgumentNullException("outStr");
     }
     this.outStr = outStr;
     WriteHeader(tag, oldPackets: false, partial: false, length);
 }
        /// <summary>Create a new style partial input stream buffered into chunks.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">Packet tag.</param>
        /// <param name="length">Size of chunks making up the packet.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag, long length)
        {
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }

            _outStr = outStr;
            WriteHeader(tag, false, false, length);
        }
Exemple #17
0
        /// <summary>Create a stream representing an old style partial object.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">The packet tag for the object.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag)
        {
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }

            _outStr = outStr;
            WriteHeader(tag, true, true, 0);
        }
    private void WriteHeader(PacketTag tag, bool oldPackets, bool partial, long bodyLen)
    {
        int num = 128;

        if (partialBuffer != null)
        {
            PartialFlush(isLast: true);
            partialBuffer = null;
        }
        if (oldPackets)
        {
            num |= (int)tag << 2;
            if (partial)
            {
                WriteByte((byte)(num | 3));
            }
            else if (bodyLen <= 255)
            {
                WriteByte((byte)num);
                WriteByte((byte)bodyLen);
            }
            else if (bodyLen <= 65535)
            {
                WriteByte((byte)(num | 1));
                WriteByte((byte)(bodyLen >> 8));
                WriteByte((byte)bodyLen);
            }
            else
            {
                WriteByte((byte)(num | 2));
                WriteByte((byte)(bodyLen >> 24));
                WriteByte((byte)(bodyLen >> 16));
                WriteByte((byte)(bodyLen >> 8));
                WriteByte((byte)bodyLen);
            }
        }
        else
        {
            num |= (int)((PacketTag)64 | tag);
            WriteByte((byte)num);
            if (partial)
            {
                partialOffset = 0;
            }
            else
            {
                WriteNewPacketLength(bodyLen);
            }
        }
    }
Exemple #19
0
        private void WriteHeader(PacketTag tag, bool oldPackets, bool partial, long bodyLen)
        {
            int num = 128;

            if (this.partialBuffer != null)
            {
                this.PartialFlush(true);
                this.partialBuffer = null;
            }
            if (oldPackets)
            {
                num |= (int)((int)tag << 2);
                if (partial)
                {
                    this.WriteByte((byte)(num | 3));
                    return;
                }
                if (bodyLen <= 255L)
                {
                    this.WriteByte((byte)num);
                    this.WriteByte((byte)bodyLen);
                    return;
                }
                if (bodyLen <= 65535L)
                {
                    this.WriteByte((byte)(num | 1));
                    this.WriteByte((byte)(bodyLen >> 8));
                    this.WriteByte((byte)bodyLen);
                    return;
                }
                this.WriteByte((byte)(num | 2));
                this.WriteByte((byte)(bodyLen >> 24));
                this.WriteByte((byte)(bodyLen >> 16));
                this.WriteByte((byte)(bodyLen >> 8));
                this.WriteByte((byte)bodyLen);
                return;
            }
            else
            {
                num |= (int)((PacketTag)64 | tag);
                this.WriteByte((byte)num);
                if (partial)
                {
                    this.partialOffset = 0;
                    return;
                }
                this.WriteNewPacketLength(bodyLen);
                return;
            }
        }
Exemple #20
0
        }        //IL_0002: Unknown result type (might be due to invalid IL or missing references)

        //IL_000c: Expected O, but got Unknown


        public PgpSecretKeyRing(Stream inputStream)
        {
            //IL_004b: Unknown result type (might be due to invalid IL or missing references)
            keys         = Platform.CreateArrayList();
            extraPubKeys = Platform.CreateArrayList();
            BcpgInputStream bcpgInputStream = BcpgInputStream.Wrap(inputStream);
            PacketTag       packetTag       = bcpgInputStream.NextPacketTag();

            if (packetTag != PacketTag.SecretKey && packetTag != PacketTag.SecretSubkey)
            {
                int num = (int)packetTag;
                throw new IOException("secret key ring doesn't start with secret key tag: tag 0x" + num.ToString("X"));
            }
            SecretKeyPacket secretKeyPacket = (SecretKeyPacket)bcpgInputStream.ReadPacket();

            while (bcpgInputStream.NextPacketTag() == PacketTag.Experimental2)
            {
                bcpgInputStream.ReadPacket();
            }
            TrustPacket trustPk = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);

            global::System.Collections.IList keySigs = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
            PgpKeyRing.ReadUserIDs(bcpgInputStream, out var ids, out var idTrusts, out var idSigs);
            keys.Add((object)new PgpSecretKey(secretKeyPacket, new PgpPublicKey(secretKeyPacket.PublicKeyPacket, trustPk, keySigs, ids, idTrusts, idSigs)));
            while (bcpgInputStream.NextPacketTag() == PacketTag.SecretSubkey || bcpgInputStream.NextPacketTag() == PacketTag.PublicSubkey)
            {
                if (bcpgInputStream.NextPacketTag() == PacketTag.SecretSubkey)
                {
                    SecretSubkeyPacket secretSubkeyPacket = (SecretSubkeyPacket)bcpgInputStream.ReadPacket();
                    while (bcpgInputStream.NextPacketTag() == PacketTag.Experimental2)
                    {
                        bcpgInputStream.ReadPacket();
                    }
                    TrustPacket trustPk2 = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
                    global::System.Collections.IList sigs = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
                    keys.Add((object)new PgpSecretKey(secretSubkeyPacket, new PgpPublicKey(secretSubkeyPacket.PublicKeyPacket, trustPk2, sigs)));
                }
                else
                {
                    PublicSubkeyPacket publicPk            = (PublicSubkeyPacket)bcpgInputStream.ReadPacket();
                    TrustPacket        trustPk3            = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
                    global::System.Collections.IList sigs2 = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
                    extraPubKeys.Add((object)new PgpPublicKey(publicPk, trustPk3, sigs2));
                }
            }
        }
    public PgpSecretKeyRing(Stream inputStream)
    {
        keys         = Platform.CreateArrayList();
        extraPubKeys = Platform.CreateArrayList();
        BcpgInputStream bcpgInputStream = BcpgInputStream.Wrap(inputStream);
        PacketTag       packetTag       = bcpgInputStream.NextPacketTag();

        if (packetTag != PacketTag.SecretKey && packetTag != PacketTag.SecretSubkey)
        {
            int num = (int)packetTag;
            throw new IOException("secret key ring doesn't start with secret key tag: tag 0x" + num.ToString("X"));
        }
        SecretKeyPacket secretKeyPacket = (SecretKeyPacket)bcpgInputStream.ReadPacket();

        while (bcpgInputStream.NextPacketTag() == PacketTag.Experimental2)
        {
            bcpgInputStream.ReadPacket();
        }
        TrustPacket trustPk = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
        IList       keySigs = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);

        PgpKeyRing.ReadUserIDs(bcpgInputStream, out IList ids, out IList idTrusts, out IList idSigs);
        keys.Add(new PgpSecretKey(secretKeyPacket, new PgpPublicKey(secretKeyPacket.PublicKeyPacket, trustPk, keySigs, ids, idTrusts, idSigs)));
        while (bcpgInputStream.NextPacketTag() == PacketTag.SecretSubkey || bcpgInputStream.NextPacketTag() == PacketTag.PublicSubkey)
        {
            if (bcpgInputStream.NextPacketTag() == PacketTag.SecretSubkey)
            {
                SecretSubkeyPacket secretSubkeyPacket = (SecretSubkeyPacket)bcpgInputStream.ReadPacket();
                while (bcpgInputStream.NextPacketTag() == PacketTag.Experimental2)
                {
                    bcpgInputStream.ReadPacket();
                }
                TrustPacket trustPk2 = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
                IList       sigs     = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
                keys.Add(new PgpSecretKey(secretSubkeyPacket, new PgpPublicKey(secretSubkeyPacket.PublicKeyPacket, trustPk2, sigs)));
            }
            else
            {
                PublicSubkeyPacket publicPk = (PublicSubkeyPacket)bcpgInputStream.ReadPacket();
                TrustPacket        trustPk3 = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
                IList sigs2 = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
                extraPubKeys.Add(new PgpPublicKey(publicPk, trustPk3, sigs2));
            }
        }
    }
Exemple #22
0
 public BcpgOutputStream(Stream outStr, PacketTag tag, long length, bool oldFormat)
 {
     if (outStr == null)
     {
         throw new ArgumentNullException("outStr");
     }
     this.outStr = outStr;
     if (length > (long)((ulong)-1))
     {
         this.WriteHeader(tag, false, true, 0L);
         this.partialBufferLength = 65536;
         this.partialBuffer       = new byte[this.partialBufferLength];
         this.partialPower        = 16;
         this.partialOffset       = 0;
         return;
     }
     this.WriteHeader(tag, oldFormat, false, length);
 }
Exemple #23
0
        private static void WriteHeader(Stream outputStream, PacketTag tag, long bodyLen, bool partial = false, bool useOldPacket = false)
        {
            int hdr = 0x80;

            if (useOldPacket)
            {
                hdr |= ((int)tag) << 2;

                if (partial)
                {
                    outputStream.WriteByte((byte)(hdr | 0x03));
                }
                else if (bodyLen <= 0xff)
                {
                    outputStream.WriteByte((byte)hdr);
                    outputStream.WriteByte((byte)bodyLen);
                }
                else if (bodyLen <= 0xffff)
                {
                    outputStream.WriteByte((byte)(hdr | 0x01));
                    outputStream.WriteByte((byte)(bodyLen >> 8));
                    outputStream.WriteByte((byte)(bodyLen));
                }
                else
                {
                    outputStream.WriteByte((byte)(hdr | 0x02));
                    outputStream.WriteByte((byte)(bodyLen >> 24));
                    outputStream.WriteByte((byte)(bodyLen >> 16));
                    outputStream.WriteByte((byte)(bodyLen >> 8));
                    outputStream.WriteByte((byte)bodyLen);
                }
            }
            else
            {
                hdr |= 0x40 | (int)tag;
                outputStream.WriteByte((byte)hdr);
                if (!partial)
                {
                    WriteNewPacketLength(outputStream, bodyLen);
                }
            }
        }
 public BcpgOutputStream(Stream outStr, PacketTag tag, long length, bool oldFormat)
 {
     if (outStr == null)
     {
         throw new ArgumentNullException("outStr");
     }
     this.outStr = outStr;
     if (length > uint.MaxValue)
     {
         WriteHeader(tag, oldPackets: false, partial: true, 0L);
         partialBufferLength = 65536;
         partialBuffer       = new byte[partialBufferLength];
         partialPower        = 16;
         partialOffset       = 0;
     }
     else
     {
         WriteHeader(tag, oldFormat, partial: false, length);
     }
 }
        /// <summary>Create a stream representing a general packet.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">Packet tag.</param>
        /// <param name="length">Size of chunks making up the packet.</param>
        /// <param name="oldFormat">If true, the header is written out in old format.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag, long length, bool oldFormat)
        {
            if (outStr == null)
                throw new ArgumentNullException("outStr");

            _outStr = outStr;

            if (length > 0xFFFFFFFFL)
            {
                this.WriteHeader(tag, false, true, 0);
                _partialBufferLength = 1 << BufferSizePower;
                _partialBuffer = new byte[_partialBufferLength];
                _partialPower = BufferSizePower;
                _partialOffset = 0;
            }
            else
            {
                this.WriteHeader(tag, oldFormat, false, length);
            }
        }
 public BcpgOutputStream(Stream outStr, PacketTag tag, long length, bool oldFormat)
 {
     //IL_000e: Unknown result type (might be due to invalid IL or missing references)
     if (outStr == null)
     {
         throw new ArgumentNullException("outStr");
     }
     this.outStr = outStr;
     if (length > 4294967295u)
     {
         WriteHeader(tag, oldPackets: false, partial: true, 0L);
         partialBufferLength = 65536;
         partialBuffer       = new byte[partialBufferLength];
         partialPower        = 16;
         partialOffset       = 0;
     }
     else
     {
         WriteHeader(tag, oldFormat, partial: false, length);
     }
 }
    public PgpPublicKeyRing(Stream inputStream)
    {
        keys = Platform.CreateArrayList();
        BcpgInputStream bcpgInputStream = BcpgInputStream.Wrap(inputStream);
        PacketTag       packetTag       = bcpgInputStream.NextPacketTag();

        if (packetTag != PacketTag.PublicKey && packetTag != PacketTag.PublicSubkey)
        {
            int num = (int)packetTag;
            throw new IOException("public key ring doesn't start with public key tag: tag 0x" + num.ToString("X"));
        }
        PublicKeyPacket publicPk = (PublicKeyPacket)bcpgInputStream.ReadPacket();
        TrustPacket     trustPk  = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
        IList           keySigs  = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);

        PgpKeyRing.ReadUserIDs(bcpgInputStream, out IList ids, out IList idTrusts, out IList idSigs);
        keys.Add(new PgpPublicKey(publicPk, trustPk, keySigs, ids, idTrusts, idSigs));
        while (bcpgInputStream.NextPacketTag() == PacketTag.PublicSubkey)
        {
            keys.Add(ReadSubkey(bcpgInputStream));
        }
    }
Exemple #28
0
        public PgpPublicKeyRing(IPacketReader packetReader)
        {
            this.keys = new List <PgpPublicKey>();

            PacketTag initialTag = packetReader.NextPacketTag();

            if (initialTag != PacketTag.PublicKey && initialTag != PacketTag.PublicSubkey)
            {
                throw new PgpUnexpectedPacketException();
            }

            PublicKeyPacket pubPk = (PublicKeyPacket)packetReader.ReadContainedPacket();

            keys.Add(new PgpPublicKey(packetReader, pubPk, subKey: false));

            // Read subkeys
            while (packetReader.NextPacketTag() == PacketTag.PublicSubkey)
            {
                pubPk = (PublicSubkeyPacket)packetReader.ReadContainedPacket();
                keys.Add(new PgpPublicKey(packetReader, pubPk, subKey: true));
            }
        }
Exemple #29
0
        public PgpPublicKeyRing(Stream inputStream)
        {
            //IL_0041: Unknown result type (might be due to invalid IL or missing references)
            keys = Platform.CreateArrayList();
            BcpgInputStream bcpgInputStream = BcpgInputStream.Wrap(inputStream);
            PacketTag       packetTag       = bcpgInputStream.NextPacketTag();

            if (packetTag != PacketTag.PublicKey && packetTag != PacketTag.PublicSubkey)
            {
                int num = (int)packetTag;
                throw new IOException("public key ring doesn't start with public key tag: tag 0x" + num.ToString("X"));
            }
            PublicKeyPacket publicPk = (PublicKeyPacket)bcpgInputStream.ReadPacket();
            TrustPacket     trustPk  = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);

            global::System.Collections.IList keySigs = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
            PgpKeyRing.ReadUserIDs(bcpgInputStream, out var ids, out var idTrusts, out var idSigs);
            keys.Add((object)new PgpPublicKey(publicPk, trustPk, keySigs, ids, idTrusts, idSigs));
            while (bcpgInputStream.NextPacketTag() == PacketTag.PublicSubkey)
            {
                keys.Add((object)ReadSubkey(bcpgInputStream));
            }
        }
Exemple #30
0
            public PacketOutputStream(
                PacketWriter writer,
                Stream outputStream,
                PacketTag tag,
                bool canBePartial    = false,
                bool preferOldFormat = false)
            {
                if (outputStream == null)
                {
                    throw new ArgumentNullException(nameof(outputStream));
                }

                this.writer              = writer;
                this.outputStream        = outputStream;
                this.packetTag           = tag;
                this.canBePartial        = canBePartial;
                this.oldFormat           = preferOldFormat && (int)tag < 16;
                this.delayedHeader       = true;
                this.partialBufferLength = 1 << BufferSizePower;
                this.partialBuffer       = ArrayPool <byte> .Shared.Rent(partialBufferLength);

                this.partialPower  = BufferSizePower;
                this.partialOffset = 0;
            }
    public BcpgOutputStream(Stream outStr, PacketTag tag, byte[] buffer)
    {
        if (outStr == null)
        {
            throw new ArgumentNullException("outStr");
        }
        this.outStr = outStr;
        WriteHeader(tag, oldPackets: false, partial: true, 0L);
        partialBuffer = buffer;
        uint num = (uint)partialBuffer.Length;

        partialPower = 0;
        while (num != 1)
        {
            num >>= 1;
            partialPower++;
        }
        if (partialPower > 30)
        {
            throw new IOException("Buffer cannot be greater than 2^30 in length.");
        }
        partialBufferLength = 1 << partialPower;
        partialOffset       = 0;
    }
Exemple #32
0
        public Packet ReadPacket()
        {
            int hdr = this.ReadByte();

            if (hdr < 0)
            {
                return(null);
            }

            if ((hdr & 0x80) == 0)
            {
                throw new IOException("invalid header encountered");
            }

            bool      newPacket = (hdr & 0x40) != 0;
            PacketTag tag       = 0;
            int       bodyLen   = 0;
            bool      partial   = false;

            if (newPacket)
            {
                tag = (PacketTag)(hdr & 0x3f);

                int l = this.ReadByte();

                if (l < 192)
                {
                    bodyLen = l;
                }
                else if (l <= 223)
                {
                    int b = m_in.ReadByte();
                    bodyLen = ((l - 192) << 8) + (b) + 192;
                }
                else if (l == 255)
                {
                    bodyLen = (m_in.ReadByte() << 24) | (m_in.ReadByte() << 16)
                              | (m_in.ReadByte() << 8) | m_in.ReadByte();
                }
                else
                {
                    partial = true;
                    bodyLen = 1 << (l & 0x1f);
                }
            }
            else
            {
                int lengthType = hdr & 0x3;

                tag = (PacketTag)((hdr & 0x3f) >> 2);

                switch (lengthType)
                {
                case 0:
                    bodyLen = this.ReadByte();
                    break;

                case 1:
                    bodyLen = (this.ReadByte() << 8) | this.ReadByte();
                    break;

                case 2:
                    bodyLen = (this.ReadByte() << 24) | (this.ReadByte() << 16)
                              | (this.ReadByte() << 8) | this.ReadByte();
                    break;

                case 3:
                    partial = true;
                    break;

                default:
                    throw new IOException("unknown length type encountered");
                }
            }

            BcpgInputStream objStream;

            if (bodyLen == 0 && partial)
            {
                objStream = this;
            }
            else
            {
                PartialInputStream pis = new PartialInputStream(this, partial, bodyLen);
                objStream = new BcpgInputStream(pis);
            }

            switch (tag)
            {
            case PacketTag.Reserved:
                return(new InputStreamPacket(objStream));

            case PacketTag.PublicKeyEncryptedSession:
                return(new PublicKeyEncSessionPacket(objStream));

            case PacketTag.Signature:
                return(new SignaturePacket(objStream));

            case PacketTag.SymmetricKeyEncryptedSessionKey:
                return(new SymmetricKeyEncSessionPacket(objStream));

            case PacketTag.OnePassSignature:
                return(new OnePassSignaturePacket(objStream));

            case PacketTag.SecretKey:
                return(new SecretKeyPacket(objStream));

            case PacketTag.PublicKey:
                return(new PublicKeyPacket(objStream));

            case PacketTag.SecretSubkey:
                return(new SecretSubkeyPacket(objStream));

            case PacketTag.CompressedData:
                return(new CompressedDataPacket(objStream));

            case PacketTag.SymmetricKeyEncrypted:
                return(new SymmetricEncDataPacket(objStream));

            case PacketTag.Marker:
                return(new MarkerPacket(objStream));

            case PacketTag.LiteralData:
                return(new LiteralDataPacket(objStream));

            case PacketTag.Trust:
                return(new TrustPacket(objStream));

            case PacketTag.UserId:
                return(new UserIdPacket(objStream));

            case PacketTag.UserAttribute:
                return(new UserAttributePacket(objStream));

            case PacketTag.PublicSubkey:
                return(new PublicSubkeyPacket(objStream));

            case PacketTag.SymmetricEncryptedIntegrityProtected:
                return(new SymmetricEncIntegrityPacket(objStream));

            case PacketTag.ModificationDetectionCode:
                return(new ModDetectionCodePacket(objStream));

            case PacketTag.Experimental1:
            case PacketTag.Experimental2:
            case PacketTag.Experimental3:
            case PacketTag.Experimental4:
                return(new ExperimentalPacket(tag, objStream));

            default:
                throw new IOException("unknown packet type encountered: " + tag);
            }
        }
        /// <summary>Create a new style partial input stream buffered into chunks.</summary>
        /// <param name="outStr">Output stream to write to.</param>
        /// <param name="tag">Packet tag.</param>
        /// <param name="buffer">Buffer to use for collecting chunks.</param>
        public BcpgOutputStream(Stream outStr, PacketTag tag, byte[] buffer)
        {
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }

            _outStr = outStr;
            WriteHeader(tag, false, true, 0);

            _partialBuffer = buffer;

            var length = (uint) _partialBuffer.Length;
            for (_partialPower = 0; length != 1; _partialPower++)
            {
                length >>= 1;
            }

            if (_partialPower > 30)
            {
                throw new IOException("Buffer cannot be greater than 2^30 in length.");
            }
            _partialBufferLength = 1 << _partialPower;
            _partialOffset = 0;
        }
 public void WritePacket(PacketTag tag, byte[] body, bool oldFormat)
 {
     this.WriteHeader(tag, oldFormat, false, body.Length);
     this.Write(body);
 }
 internal void WritePacket(PacketTag tag, byte[] body, bool oldFormat)
 {
     WriteHeader(tag, oldFormat, false, body.Length);
     Write(body);
 }
        private void WriteHeader(PacketTag tag, bool oldPackets, bool partial, long bodyLen)
        {
            int hdr = 0x80;

            if (_partialBuffer != null)
            {
                PartialFlush(true);
                _partialBuffer = null;
            }

            if (oldPackets)
            {
                hdr |= ((int) tag) << 2;

                if (partial)
                {
                    WriteByte((byte) (hdr | 0x03));
                }
                else
                {
                    if (bodyLen <= 0xff)
                    {
                        WriteByte((byte) hdr);
                        WriteByte((byte) bodyLen);
                    }
                    else if (bodyLen <= 0xffff)
                    {
                        WriteByte((byte) (hdr | 0x01));
                        WriteByte((byte) (bodyLen >> 8));
                        WriteByte((byte) (bodyLen));
                    }
                    else
                    {
                        WriteByte((byte) (hdr | 0x02));
                        WriteByte((byte) (bodyLen >> 24));
                        WriteByte((byte) (bodyLen >> 16));
                        WriteByte((byte) (bodyLen >> 8));
                        WriteByte((byte) bodyLen);
                    }
                }
            }
            else
            {
                hdr |= 0x40 | (int) tag;
                WriteByte((byte) hdr);

                if (partial)
                {
                    _partialOffset = 0;
                }
                else
                {
                    WriteNewPacketLength(bodyLen);
                }
            }
        }
        /// <summary>Return the next object in the stream, or null if the end is reached.</summary>
        /// <exception cref="IOException">On a parse error</exception>
        public PgpObject NextPgpObject()
        {
            PacketTag tag = bcpgIn.NextPacketTag();

            if ((int)tag == -1)
            {
                return(null);
            }

            switch (tag)
            {
            case PacketTag.Signature:
            {
                IList l = Platform.CreateArrayList();

                while (bcpgIn.NextPacketTag() == PacketTag.Signature)
                {
                    try
                    {
                        l.Add(new PgpSignature(bcpgIn));
                    }
                    catch (PgpException e)
                    {
                        throw new IOException("can't create signature object: " + e);
                    }
                }

                PgpSignature[] sigs = new PgpSignature[l.Count];
                for (int i = 0; i < l.Count; ++i)
                {
                    sigs[i] = (PgpSignature)l[i];
                }
                return(new PgpSignatureList(sigs));
            }

            case PacketTag.SecretKey:
                try
                {
                    return(new PgpSecretKeyRing(bcpgIn));
                }
                catch (PgpException e)
                {
                    throw new IOException("can't create secret key object: " + e);
                }

            case PacketTag.PublicKey:
                return(new PgpPublicKeyRing(bcpgIn));

            // TODO Make PgpPublicKey a PgpObject or return a PgpPublicKeyRing
//				case PacketTag.PublicSubkey:
//					return PgpPublicKeyRing.ReadSubkey(bcpgIn);
            case PacketTag.CompressedData:
                return(new PgpCompressedData(bcpgIn));

            case PacketTag.LiteralData:
                return(new PgpLiteralData(bcpgIn));

            case PacketTag.PublicKeyEncryptedSession:
            case PacketTag.SymmetricKeyEncryptedSessionKey:
                return(new PgpEncryptedDataList(bcpgIn));

            case PacketTag.OnePassSignature:
            {
                IList l = Platform.CreateArrayList();

                while (bcpgIn.NextPacketTag() == PacketTag.OnePassSignature)
                {
                    try
                    {
                        l.Add(new PgpOnePassSignature(bcpgIn));
                    }
                    catch (PgpException e)
                    {
                        throw new IOException("can't create one pass signature object: " + e);
                    }
                }

                PgpOnePassSignature[] sigs = new PgpOnePassSignature[l.Count];
                for (int i = 0; i < l.Count; ++i)
                {
                    sigs[i] = (PgpOnePassSignature)l[i];
                }
                return(new PgpOnePassSignatureList(sigs));
            }

            case PacketTag.Marker:
                return(new PgpMarker(bcpgIn));

            case PacketTag.Experimental1:
            case PacketTag.Experimental2:
            case PacketTag.Experimental3:
            case PacketTag.Experimental4:
                return(new PgpExperimental(bcpgIn));
            }

            throw new IOException("unknown object in stream " + bcpgIn.NextPacketTag());
        }
Exemple #38
0
        public PgpSecretKeyRing(
            Stream inputStream)
        {
            this.keys         = Platform.CreateArrayList();
            this.extraPubKeys = Platform.CreateArrayList();

            BcpgInputStream bcpgInput = BcpgInputStream.Wrap(inputStream);

            PacketTag initialTag = bcpgInput.NextPacketTag();

            if (initialTag != PacketTag.SecretKey && initialTag != PacketTag.SecretSubkey)
            {
                throw new IOException("secret key ring doesn't start with secret key tag: "
                                      + "tag 0x" + ((int)initialTag).ToString("X"));
            }

            SecretKeyPacket secret = (SecretKeyPacket)bcpgInput.ReadPacket();

            //
            // ignore GPG comment packets if found.
            //
            while (bcpgInput.NextPacketTag() == PacketTag.Experimental2)
            {
                bcpgInput.ReadPacket();
            }

            TrustPacket trust = ReadOptionalTrustPacket(bcpgInput);

            // revocation and direct signatures
            IList keySigs = ReadSignaturesAndTrust(bcpgInput);

            IList ids, idTrusts, idSigs;

            ReadUserIDs(bcpgInput, out ids, out idTrusts, out idSigs);

            keys.Add(new PgpSecretKey(secret, new PgpPublicKey(secret.PublicKeyPacket, trust, keySigs, ids, idTrusts, idSigs)));


            // Read subkeys
            while (bcpgInput.NextPacketTag() == PacketTag.SecretSubkey ||
                   bcpgInput.NextPacketTag() == PacketTag.PublicSubkey)
            {
                if (bcpgInput.NextPacketTag() == PacketTag.SecretSubkey)
                {
                    SecretSubkeyPacket sub = (SecretSubkeyPacket)bcpgInput.ReadPacket();

                    //
                    // ignore GPG comment packets if found.
                    //
                    while (bcpgInput.NextPacketTag() == PacketTag.Experimental2)
                    {
                        bcpgInput.ReadPacket();
                    }

                    TrustPacket subTrust = ReadOptionalTrustPacket(bcpgInput);
                    IList       sigList  = ReadSignaturesAndTrust(bcpgInput);

                    keys.Add(new PgpSecretKey(sub, new PgpPublicKey(sub.PublicKeyPacket, subTrust, sigList)));
                }
                else
                {
                    PublicSubkeyPacket sub = (PublicSubkeyPacket)bcpgInput.ReadPacket();

                    TrustPacket subTrust = ReadOptionalTrustPacket(bcpgInput);
                    IList       sigList  = ReadSignaturesAndTrust(bcpgInput);

                    extraPubKeys.Add(new PgpPublicKey(sub, subTrust, sigList));
                }
            }
        }