Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MultipleStreamReader"/> class.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="args">The args.</param>
        public MultipleStreamReader(Stream input, MultipleStreamCreateArgs args)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            else if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            _input            = input;
            _reader           = new QQnBinaryReader(_input);
            _verificationMode = args.VerificationMode;

            /*_maxCount =*/ _reader.ReadInt32();
            int count = _reader.ReadInt32();

            long nextHeader = _reader.ReadInt64();

            for (int i = 0; i < count; i++)
            {
                _items.Add(new MultiStreamItemHeader(_reader));
            }
        }
Beispiel #2
0
        /*public AssuredStreamHeader(Stream source)
         *      : this(source, VerificationMode.Full)
         * {
         * }*/

        public AssuredStreamHeader(Stream source, VerificationMode mode)
        {
            QQnBinaryReader br             = new QQnBinaryReader(source);
            uint            vFileSignature = br.ReadUInt32();

            if (vFileSignature != FileSignature)
            {
                throw new FormatException();
            }

            _fileType      = br.ReadString();
            _hashType      = (HashType)br.ReadByte();
            _fileHash      = br.ReadByteArray();
            _hashSignature = br.ReadByteArray();
            byte[] publicKey = br.ReadByteArray();

            if (publicKey.Length > 0)
            {
                _snk = StrongNameKey.LoadFrom(publicKey);

                if (mode != VerificationMode.None)
                {
                    if (!_snk.VerifyHash(_fileHash, _hashSignature))
                    {
                        throw new CryptographicException("Stream hash verification failed");
                    }
                }
            }
            _guid       = new Guid(br.ReadBytes(16));
            _bodyLength = br.ReadInt64();

            _hashPosition = source.Position;
        }
Beispiel #3
0
        public const int ItemSize = (1 + 8 + 4 + 4) + 1;         // 1 more than length of fields

        internal MultiStreamItemHeader(QQnBinaryReader reader)
        {
            byte version = reader.ReadByte();

            if (version == 1)
            {
                _offset   = reader.ReadInt64();
                _length   = reader.ReadUInt32();               // As uint
                _itemType = reader.ReadInt32();
            }
            else if (version == 2)
            {
                // Define some format which allows +4GB substream
                // When this is used we will need some more padding space; but it probably will never be written anyway
                // At least we can read them with this version

                _offset   = reader.ReadInt64();
                _length   = reader.ReadInt64();               // As long
                _itemType = reader.ReadInt32();
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AssuredSubStream"/> class.
        /// </summary>
        /// <param name="parentStream">The parent stream.</param>
        /// <param name="verificationMode">The verification mode.</param>
        public AssuredSubStream(Stream parentStream, VerificationMode verificationMode)
            : base(parentStream, true)
        {
            AssuredStream signedParent = GetService <AssuredStream>();

            if (signedParent != null)
            {
                _snk      = signedParent.AssemblyStrongNameKey;
                _hashType = signedParent.HashType;
            }
            else
            {
                _hashType = HashType.SHA1;
            }

            _headerPosition = parentStream.Position;

            _streamHash    = new byte[QQnCryptoHelpers.GetHashBits(_hashType) / 8];
            _hashSignature = new byte[(_snk != null) ? _snk.SignatureLength : 0];

            if (parentStream.CanWrite && parentStream.CanSeek && parentStream.Position == parentStream.Length)
            {
                _updating = true;
                WriteHeader();
            }
            else
            {
                QQnBinaryReader br = new QQnBinaryReader(BaseStream);
                _streamHash    = br.ReadBytes(_streamHash.Length);
                _hashSignature = br.ReadBytes(_hashSignature.Length);
                _hashLength    = br.ReadInt64();
                _hashPosition  = BaseStream.Position;

                if (verificationMode != VerificationMode.None)
                {
                    if (_snk != null && !_snk.VerifyHash(_streamHash, _hashSignature))
                    {
                        throw new CryptographicException("Stream hash verification failed");
                    }
                }

                if (verificationMode == VerificationMode.Full)
                {
                    if (!VerifyHash())
                    {
                        throw new CryptographicException("Invalid hash value");
                    }
                }
            }
        }