public BlobChunkVerificationResultInfo Verify(BlobTransportStreamAuthentication expectedAuthentication,
                                                          long blobSize)
            {
                var result = BlobChunkVerificationResultInfo.ValidResult;

                if (BlobSize != blobSize)
                {
                    result = new BlobChunkVerificationResultInfo(BlobChunkVerificationResult.InvalidBlobSize,
                                                                 BlobChunkVerificationResultContext.Footer, BlobSize);
                }
                else if (Authentication < 0 || Authentication >= BlobTransportStreamAuthentication.kMax)
                {
                    result = new BlobChunkVerificationResultInfo(BlobChunkVerificationResult.InvalidAuthentication,
                                                                 BlobChunkVerificationResultContext.Footer, Authentication);
                }
                else if (Authentication != expectedAuthentication)
                {
                    result = new BlobChunkVerificationResultInfo(BlobChunkVerificationResult.AuthenticationMismatch,
                                                                 BlobChunkVerificationResultContext.Footer, Authentication);
                }
                else if (CalculateAssumedAuthenticationDataSize() != Authentication.GetDataSize())
                {
                    result = new BlobChunkVerificationResultInfo(BlobChunkVerificationResult.InvalidAuthenticationSize,
                                                                 BlobChunkVerificationResultContext.Footer, CalculateAssumedAuthenticationDataSize());
                }

                return(result);
            }
Exemple #2
0
 internal BlobChunkVerificationResultInfo(BlobChunkVerificationResult result, BlobChunkVerificationResultContext context,
                                          BlobTransportStreamAuthentication data)
 {
     mResult  = result;
     mContext = context;
     mData    = (uint)data;
 }
Exemple #3
0
        public BlobChunkVerificationResultInfo EnumerateChunks(BlobSystem blobSystem,
                                                               out IEnumerable <BlobObject> objects, bool throwOnUnhandledChunk = true,
                                                               BlobTransportStreamAuthentication expectedAuthentication         = BlobTransportStreamAuthentication.None,
                                                               bool authenticateBlob = true)
        {
            Contract.Requires <ArgumentNullException>(blobSystem != null);
            Contract.Requires <InvalidOperationException>(!IsClosed);
            Contract.Requires <InvalidOperationException>(UnderlyingStream.CanRead);

            objects = null;

            IList <KeyValuePair <BlobChunkHeader, byte[]> > chunks;
            var result_info = EnumerateStream(out chunks, expectedAuthentication,
                                              GetEnumerateStreamResultBytes, getResultValueConsumesChunk: true);

            if (result_info.IsValid &&
                mFooter.Authentication > BlobTransportStreamAuthentication.None && authenticateBlob)
            {
                result_info = VerifyEofAuthentication();
            }

            if (result_info.IsValid)
            {
                objects = EnumerateChunksReadObjectsSync(blobSystem, throwOnUnhandledChunk, chunks);
            }

            return(result_info);
        }
Exemple #4
0
        public async Task <KeyValuePair <BlobChunkVerificationResultInfo, IEnumerable <BlobObject> > > EnumerateChunksAsync(
            BlobSystem blobSystem,
            bool throwOnUnhandledChunk = true,
            BlobTransportStreamAuthentication expectedAuthentication = BlobTransportStreamAuthentication.None,
            bool authenticateBlob = true)
        {
            Contract.Requires <ArgumentNullException>(blobSystem != null);
            Contract.Requires <InvalidOperationException>(!IsClosed);
            Contract.Requires <InvalidOperationException>(UnderlyingStream.CanRead);

            IEnumerable <BlobObject> objects = null;

            IList <KeyValuePair <BlobChunkHeader, byte[]> > chunks;
            var result_info = EnumerateStream(out chunks, expectedAuthentication,
                                              GetEnumerateStreamResultBytes, getResultValueConsumesChunk: true);

            if (result_info.IsValid &&
                mFooter.Authentication > BlobTransportStreamAuthentication.None && authenticateBlob)
            {
                result_info = VerifyEofAuthentication();
            }

            if (result_info.IsValid)
            {
                var tasks = EnumerateChunksReadObjectsAsync(blobSystem, throwOnUnhandledChunk, chunks);
                await Task.WhenAll(tasks).ConfigureAwait(true);

                objects = from task in tasks
                          select task.Result;
            }

            return(new KeyValuePair <BlobChunkVerificationResultInfo, IEnumerable <BlobObject> >(result_info, objects));
        }
Exemple #5
0
        BlobChunkVerificationResultInfo EnumerateStream <T>(out IList <KeyValuePair <BlobChunkHeader, T> > results,
                                                            BlobTransportStreamAuthentication expectedAuthentication,
                                                            Func <BlobTransportStream, BlobChunkHeader, T> getResultValue, bool getResultValueConsumesChunk = false)
        {
            results = new List <KeyValuePair <BlobChunkHeader, T> >();
            var result_info = BlobChunkVerificationResultInfo.ValidResult;

            result_info = VerifyStart();
            BlobChunkHeader chunk_header;
            bool            is_eof = false;

            while (result_info.IsValid)
            {
                T result_value = default(T);
                result_info = EnumerateOneChunk(out chunk_header, ref result_value,
                                                getResultValue, getResultValueConsumesChunk, out is_eof);

                if (result_info.IsValid && !is_eof)
                {
                    results.Add(new KeyValuePair <BlobChunkHeader, T>(chunk_header, result_value));
                }
                else
                {
                    break;
                }
            }

            if (result_info.IsValid && is_eof)
            {
                result_info = VerifyEof(streamFirst: false, expectedAuthentication: expectedAuthentication);
            }

            return(result_info);
        }
Exemple #6
0
        BlobChunkVerificationResultInfo VerifyEof(bool streamFirst = true,
                                                  BlobTransportStreamAuthentication expectedAuthentication = BlobTransportStreamAuthentication.None)
        {
            var result = streamFirst ? VerifyEnoughBytesForChunkOrData(kFooterSize) : BlobChunkVerificationResultInfo.ValidResult;

            if (result.IsValid)
            {
                if (streamFirst)
                {
                    mFooterPosition = BaseStream.Position;
                    UnderlyingStream.Stream(ref mHeader);
                }
                long blob_size = AssumedBlobSize;

                result = mFooter.Verify(expectedAuthentication, blob_size);

                if (streamFirst && result.IsValid)
                {
                    mFooter.SerializeAuthenticationData(UnderlyingStream);
                }
            }

            if (result.IsInvalid)
            {
                result.Context = BlobChunkVerificationResultContext.Footer;
            }

            return(result);
        }
            public StreamFooter(BlobTransportStreamAuthentication authentication, long blobSize)
            {
                BlobSize           = (uint)blobSize;
                Authentication     = authentication;
                AuthenticationData = null;

                Header = new BlobChunkHeader(kSignature, kVersion, kSizeOfDataSansAuthData + Authentication.GetDataSize());
                InitializeData();
            }
Exemple #8
0
        public BlobChunkVerificationResultInfo WriteChunksWithAuthentication(BlobTransportStreamAuthentication authentication,
                                                                             params BlobObject[] objects)
        {
            Contract.Requires <InvalidOperationException>(!IsClosed);
            Contract.Requires <InvalidOperationException>(UnderlyingStream.CanWrite);
            Contract.Requires(objects != null && objects.Length > 0);
            Contract.Requires <ArgumentNullException>(Array.TrueForAll(objects, Predicates.IsNotNull));

            return(WriteChunks(objects, authentication));
        }
Exemple #9
0
        public BlobChunkVerificationResultInfo WriteChunks(IEnumerable <BlobObject> objects,
                                                           BlobTransportStreamAuthentication authentication = BlobTransportStreamAuthentication.None)
        {
            Contract.Requires <InvalidOperationException>(!IsClosed);
            Contract.Requires <InvalidOperationException>(UnderlyingStream.CanWrite);

            var result_info = WriteChunksWriteStart();

            if (result_info.IsValid)
            {
                var array = WriteChunksGetObjectsArray(objects);
                result_info = WriteChunksForObjectsSync(array);
            }

            if (result_info.IsValid)
            {
                result_info = WriteChunksWriteEof(authentication);
            }

            return(result_info);
        }
Exemple #10
0
        BlobChunkVerificationResultInfo WriteChunksWriteEof(BlobTransportStreamAuthentication authentication)
        {
            mFooterPosition = BaseStream.Position;
            mFooter         = new StreamFooter(authentication, AssumedBlobSize);

            var result = VerifyEnoughBytesForChunkOrData(kFooterSize + authentication.GetDataSize());

            if (result.IsValid)
            {
                byte[] auth_data = BuildAuthenticationData();

                if (auth_data != null)
                {
                    Contract.Assert(mFooter.AuthenticationData.Length >= auth_data.Length);
                    Buffer.BlockCopy(auth_data, 0, mFooter.AuthenticationData, 0, auth_data.Length);
                }

                mFooter.Serialize(UnderlyingStream);
                mFooter.SerializeAuthenticationData(UnderlyingStream);
            }

            return(result);
        }