Пример #1
0
        /// <summary>
        /// Unmarshal the segment info message.
        /// </summary>
        /// <param name="byteArr">The payload.</param>
        /// <returns>The unmarshaled segment info message.</returns>
        public static SEGMENT_INFO_MESSAGE DecodeSegmentInfoMessage(byte[] byteArr)
        {
            int index = 0;

            byte[] informationData = GetBytes(byteArr, ref index, byteArr.Length - index);
            int    tempIndex       = 0;
            SEGMENT_INFO_MESSAGE segmentInfoMessage;

            MESSAGE_HEADER msgHeader;

            msgHeader.MinorVersion       = informationData[tempIndex++];
            msgHeader.MajorVersion       = informationData[tempIndex++];
            msgHeader.MsgType            = (PCHC_MESSAGE_TYPE)GetUInt16(informationData, ref tempIndex, false);
            msgHeader.Padding            = GetBytes(informationData, ref tempIndex, 4);
            segmentInfoMessage.MsgHeader = msgHeader;

            CONNECTION_INFORMATION connectionInfo;

            connectionInfo.Port               = GetUInt16(informationData, ref tempIndex, false);
            connectionInfo.Padding            = GetBytes(informationData, ref tempIndex, 6);
            segmentInfoMessage.ConnectionInfo = connectionInfo;

            segmentInfoMessage.ContentTag  = GetBytes(informationData, ref tempIndex, 16);
            segmentInfoMessage.SegmentInfo = PccrcUtility.ParseContentInformation(informationData, ref tempIndex);

            return(segmentInfoMessage);
        }
        public void VerifyHashGenerationV1(byte[] content, Content_Information_Data_Structure contentInfo)
        {
            const int BLOCKBYTECOUNT    = 0x10000;
            const int SEGMENTBLOCKCOUNT = 512;

            dwHashAlgo_Values hashAlgo = contentInfo.dwHashAlgo;

            int blockTotalCount = content.Length / BLOCKBYTECOUNT;

            if (content.Length > BLOCKBYTECOUNT * blockTotalCount)
            {
                blockTotalCount = blockTotalCount + 1;
            }

            int segmentCount = blockTotalCount / SEGMENTBLOCKCOUNT;

            if (blockTotalCount > SEGMENTBLOCKCOUNT * segmentCount)
            {
                segmentCount = segmentCount + 1;
            }

            HashAlgorithm hashAlgorithm;
            HMAC          hmacAlgorithm;
            int           blockHashSize;

            PccrcUtility.GetHashAlgorithm(hashAlgo, out hashAlgorithm, out hmacAlgorithm, out blockHashSize);
            hmacAlgorithm.Key = hashAlgorithm.ComputeHash(testConfig.ServerSecret);

            for (int segmentIndex = 0; segmentIndex < segmentCount; segmentIndex++)
            {
                List <byte> blockHashList = new List <byte>();

                int blockCount = (segmentIndex == segmentCount - 1) ? (blockTotalCount % SEGMENTBLOCKCOUNT) : (SEGMENTBLOCKCOUNT);

                for (int blockIndex = 0; blockIndex < blockCount; blockIndex++)
                {
                    var block = content.Skip(BLOCKBYTECOUNT * SEGMENTBLOCKCOUNT * segmentIndex + BLOCKBYTECOUNT * blockIndex).Take(BLOCKBYTECOUNT).ToArray();

                    byte[] blockHash = hashAlgorithm.ComputeHash(block);

                    testSite.Assert.IsTrue(
                        blockHash.SequenceEqual((contentInfo.blocks[segmentIndex].BlockHashes).Skip(blockIndex * blockHashSize).Take(blockHashSize).ToArray()),
                        "The local calculated block hash in Segment: {0} Block: {1} should cosistent with the received value.", segmentIndex, blockIndex);

                    blockHashList.AddRange(blockHash);
                }

                byte[] hod = hashAlgorithm.ComputeHash(blockHashList.ToArray());

                testSite.Assert.IsTrue(
                    hod.SequenceEqual(contentInfo.segments[segmentIndex].SegmentHashOfData),
                    "The local calculated Hod should cosistent with the received value.");

                byte[] kp = hmacAlgorithm.ComputeHash(hod);

                testSite.Assert.IsTrue(
                    kp.SequenceEqual(contentInfo.segments[segmentIndex].SegmentSecret),
                    "The local calculated Kp should cosistent with the received value.");
            }
        }
        public void VerifyContentInformation(byte[] content, byte[] contentInformation, BranchCacheVersion version)
        {
            switch (version)
            {
            case BranchCacheVersion.V1:
                VerifyHashGenerationV1(content, PccrcUtility.ParseContentInformation(contentInformation));
                break;

            case BranchCacheVersion.V2:
                VerifyHashGenerationV2(content, PccrcUtility.ParseContentInformationV2(contentInformation));
                break;

            default:
                throw new NotImplementedException();
            }
        }
Пример #4
0
        /// <summary>
        /// Generate PCCRTP response message to reply the client request.
        /// </summary>
        /// <param name="resourceLocator">The client request URI.</param>
        /// <param name="serverSecret">The server secret set on the server endpoint.</param>
        /// <returns>Returns the PCCRTP response message.</returns>
        public PccrtpResponse GenerateResponseMessage(string resourceLocator, string serverSecret)
        {
            PccrtpResponse pccrtpResponse = new PccrtpResponse();
            Dictionary <string, string> tempHttpHeader = new Dictionary <string, string>();
            HashGeneration hashHelp = new HashGeneration(serverSecret, dwHashAlgo_Values.SHA256);

            byte[] fileData = PccrcUtility.ReadFile(resourceLocator);
            Content_Information_Data_Structure contentInfo = hashHelp.GenerateContentInformation(fileData);

            tempHttpHeader.Add(CONTENTENCODING, "peerdist");
            tempHttpHeader.Add(XP2PPEERDIST, "Version=1.0, ContentLength=" + fileData.Length);
            pccrtpResponse.HttpHeader = tempHttpHeader;

            pccrtpResponse.PayloadData = contentInfo.ToByteArray();

            return(pccrtpResponse);
        }
        public void VerifyHashGenerationV2(byte[] content, Content_Information_Data_Structure_V2 contentInfoV2)
        {
            dwHashAlgoV2_Values hashAlgo = contentInfoV2.dwHashAlgo;

            HashAlgorithm hashAlgorithm;
            HMAC          hmacAlgorithm;

            PccrcUtility.GetHashAlgorithm(hashAlgo, out hashAlgorithm, out hmacAlgorithm);
            hmacAlgorithm.Key = hashAlgorithm.ComputeHash(testConfig.ServerSecret).Take(32).ToArray();

            // Local calculate SegmentHashOfData and SegmentSecret
            ChunkDescription[] chunkDescription = contentInfoV2.chunks;
            int chunkCount    = contentInfoV2.chunks.Length;
            int segmentOffset = 0;

            for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++)
            {
                SegmentDescriptionV2[] segmentDescription = chunkDescription[chunkIndex].chunkData;
                int segmentCount = segmentDescription.Length;

                for (int segmentIndex = 0; segmentIndex < segmentCount; ++segmentIndex)
                {
                    var segment = content.Skip(segmentOffset).Take((int)(segmentDescription[segmentIndex].cbSegment)).ToArray();

                    segmentOffset += (int)(segmentDescription[segmentIndex].cbSegment);

                    //TRANCATED_SHA_512
                    byte[] hod = hashAlgorithm.ComputeHash(segment).Take(32).ToArray();

                    testSite.Assert.IsTrue(
                        hod.SequenceEqual((chunkDescription[chunkIndex].chunkData)[segmentIndex].SegmentHashOfData),
                        "The local calculated Hod should cosistent with the received value.");

                    byte[] kp = hmacAlgorithm.ComputeHash(hod).Take(32).ToArray();

                    testSite.Assert.IsTrue(
                        kp.SequenceEqual((chunkDescription[chunkIndex].chunkData)[segmentIndex].SegmentSecret),
                        "The local calculated Kp should be consistent with the received value.");
                }
            }
        }
Пример #6
0
        public void HostedCacheServer_BVT_CacheOfferingRetrieval_V1()
        {
            CheckApplicability();

            EventQueue eventQueue = new EventQueue(BaseTestSite);

            eventQueue.Timeout = testConfig.Timeout;

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Trigger hash generation on content server");

            byte[] content = contentInformationUtility.RetrieveContentData();

            Content_Information_Data_Structure contentInformation =
                PccrcUtility.ParseContentInformation(contentInformationUtility.RetrieveContentInformation(BranchCacheVersion.V1));

            CryptoAlgoId_Values cryptoAlgoId = CryptoAlgoId_Values.AES_128;

            PccrrClient pccrrClient = new PccrrClient(testConfig.HostedCacheServerComputerName, testConfig.HostedCacheServerHTTPListenPort);

            for (int i = 0; i < contentInformation.cSegments; ++i)
            {
                var pccrrBlkListRequest = pccrrClient.CreateMsgGetBlkListRequest(
                    contentInformation.GetSegmentId(i),
                    new BLOCK_RANGE[] { new BLOCK_RANGE {
                                            Index = 0, Count = contentInformation.segments[i].BlockCount
                                        } },
                    cryptoAlgoId,
                    MsgType_Values.MSG_GETBLKLIST);
                pccrrClient.SendPacket(
                    pccrrBlkListRequest,
                    testConfig.Timeout);
                var pccrrBlkListResponse
                    = (PccrrBLKLISTResponsePacket)pccrrClient.ExpectPacket();

                BaseTestSite.Assert.AreEqual <uint>(
                    0,
                    pccrrBlkListResponse.MsgBLKLIST.BlockRangeCount,
                    "The server MUST set the BlockRangeCount field to zero if it doesn't have the requested blocks data.");
            }

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Retrieve content information from content server");

            using (PccrrTestServerV1 pccrrTestServer = new PccrrTestServerV1())
            {
                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Start PCCRR server to be ready to serve content to hosted cache server");

                pccrrTestServer.Start(
                    testConfig.ClientContentRetrievalListenPort,
                    cryptoAlgoId,
                    contentInformation,
                    content,
                    eventQueue);

                PCHCClient pchcClient = new PCHCClient(
                    TransferProtocol.HTTPS,
                    testConfig.HostedCacheServerComputerName,
                    testConfig.HostedCacheServerHTTPSListenPort,
                    PchcConsts.HttpsUrl,
                    testConfig.DomainName,
                    testConfig.UserName,
                    testConfig.UserPassword);

                for (int i = 0; i < contentInformation.cSegments; i++)
                {
                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Offer content segment {0} to hosted cache server",
                        i);

                    INITIAL_OFFER_MESSAGE initialOfferMessage = pchcClient.CreateInitialOfferMessage(
                        testConfig.ClientContentRetrievalListenPort,
                        contentInformation.GetSegmentId(i));
                    Microsoft.Protocols.TestTools.StackSdk.BranchCache.Pchc.RESPONSE_MESSAGE responseMessage
                        = pchcClient.SendInitialOfferMessage(initialOfferMessage);

                    TestClassBase.BaseTestSite.Assert.AreEqual <RESPONSE_CODE>(
                        RESPONSE_CODE.INTERESTED,
                        responseMessage.ResponseCode,
                        @"The hosted cache MUST specify a response code of 1 
                        if its list of block hashes associated with the segment is incomplete.");

                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Supply segment info to hosted cache server");

                    SEGMENT_INFO_MESSAGE segmentInfoMessage = pchcClient.CreateSegmentInfoMessage(
                        testConfig.ClientContentRetrievalListenPort,
                        contentInformation,
                        i);
                    responseMessage = pchcClient.SendSegmentInfoMessage(segmentInfoMessage);

                    TestClassBase.BaseTestSite.Assert.AreEqual <RESPONSE_CODE>(
                        RESPONSE_CODE.OK,
                        responseMessage.ResponseCode,
                        @"The hosted cache MUST send a response code of 0 when SEGMENT_INFO_MESSAGE request received");

                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Make sure all blocks in segment {0} are retrieved by hosted cache server",
                        i);

                    int blockCount = 0;
                    TestUtility.DoUntilSucceed(delegate()
                    {
                        eventQueue.Expect <MessageArrivedEventArgs>(typeof(PccrrServer).GetEvent("MessageArrived"), delegate(System.Net.IPEndPoint sender, PccrrPacket pccrrPacket)
                        {
                            var pccrrGetBlksRequest = pccrrPacket as PccrrGETBLKSRequestPacket;

                            if (pccrrGetBlksRequest != null)
                            {
                                blockCount++;
                            }
                        });
                        return(blockCount == contentInformation.segments[i].BlockCount);
                    }, TimeSpan.MaxValue, TimeSpan.Zero);
                }

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Wait until cache is available on hosted cache server");

                TestUtility.DoUntilSucceed(() => sutControlAdapter.IsLocalCacheExisted(testConfig.HostedCacheServerComputerFQDNOrNetBiosName), testConfig.Timeout, testConfig.RetryInterval);
            }

            List <byte> retrievedContent = new List <byte>();

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Negotiate PCCRR version");

            var pccrrNegotiateRequest = pccrrClient.CreateMsgNegoRequest(
                new ProtoVersion {
                MajorVersion = 1, MinorVersion = 0
            },
                new ProtoVersion {
                MajorVersion = 1, MinorVersion = ushort.MaxValue
            },
                cryptoAlgoId,
                MsgType_Values.MSG_NEGO_REQ);

            pccrrClient.SendPacket(
                pccrrNegotiateRequest,
                testConfig.Timeout);
            var pccrrNegotiateResponse
                = (PccrrNegoResponsePacket)pccrrClient.ExpectPacket();

            if (testConfig.SupportBranchCacheV1)
            {
                BaseTestSite.Assert.IsTrue(
                    pccrrNegotiateResponse.MsgNegoResp.MinSupporteProtocolVersion.MajorVersion <= 1 &&
                    pccrrNegotiateResponse.MsgNegoResp.MaxSupporteProtocolVersion.MajorVersion >= 1,
                    "SupportedProtocolVersion doesn't match configuration");
            }

            if (testConfig.SupportBranchCacheV2)
            {
                BaseTestSite.Assert.IsTrue(
                    pccrrNegotiateResponse.MsgNegoResp.MinSupporteProtocolVersion.MajorVersion <= 2 &&
                    pccrrNegotiateResponse.MsgNegoResp.MaxSupporteProtocolVersion.MajorVersion >= 2,
                    "SupportedProtocolVersion doesn't match configuration");
            }

            Aes aes = PccrrUtitlity.CreateAes(cryptoAlgoId);

            for (int i = 0; i < contentInformation.cSegments; i++)
            {
                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Retrieve block list for segment {0}",
                    i);

                var pccrrBlkListRequest = pccrrClient.CreateMsgGetBlkListRequest(
                    contentInformation.GetSegmentId(i),
                    new BLOCK_RANGE[] { new BLOCK_RANGE {
                                            Index = 0, Count = contentInformation.segments[i].BlockCount
                                        } },
                    cryptoAlgoId,
                    MsgType_Values.MSG_GETBLKLIST);
                pccrrClient.SendPacket(
                    pccrrBlkListRequest,
                    testConfig.Timeout);
                var pccrrBlkListResponse
                    = (PccrrBLKLISTResponsePacket)pccrrClient.ExpectPacket();

                BaseTestSite.Assert.AreNotEqual <uint>(
                    0,
                    pccrrBlkListResponse.MsgBLKLIST.BlockRangeCount,
                    "The server MUST set the BlockRangeCount field to a value greater than zero if it has the requested blocks data.");

                for (int j = 0; j < contentInformation.segments[i].BlockCount; j++)
                {
                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Retrieve block {0} for segment {1}",
                        j,
                        i);

                    PccrrGETBLKSRequestPacket pccrrBlkRequest = pccrrClient.CreateMsgGetBlksRequest(
                        contentInformation.GetSegmentId(i),
                        cryptoAlgoId,
                        MsgType_Values.MSG_GETBLKS,
                        (uint)j,
                        1);
                    pccrrClient.SendPacket(
                        pccrrBlkRequest,
                        testConfig.Timeout);
                    PccrrBLKResponsePacket pccrrBlkResponse
                        = (PccrrBLKResponsePacket)pccrrClient.ExpectPacket();

                    BaseTestSite.Assert.AreNotEqual <uint>(
                        0,
                        pccrrBlkResponse.MsgBLK.SizeOfBlock,
                        "The server MUST set the SizeOfBlock field to a value greater than zero if it has the requested blocks data.");

                    byte[] block = pccrrBlkResponse.MsgBLK.Block;

                    if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                    {
                        block = PccrrUtitlity.Decrypt(aes, block, contentInformation.segments[i].SegmentSecret, pccrrBlkResponse.MsgBLK.IVBlock);
                    }

                    retrievedContent.AddRange(block);
                }
            }

            BaseTestSite.Assert.IsTrue(
                Enumerable.SequenceEqual(content, retrievedContent),
                "The retrieved cached data should be the same as server data.");
        }
Пример #7
0
        public void HostedCacheServer_BVT_CacheOfferingRetrieval_V2()
        {
            CheckApplicability();

            EventQueue eventQueue = new EventQueue(BaseTestSite);

            eventQueue.Timeout = testConfig.Timeout;

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Retrieve the original content data from content server");
            byte[] content = contentInformationUtility.RetrieveContentData();

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Retrieve the content information from content server");
            Content_Information_Data_Structure_V2 contentInformation =
                PccrcUtility.ParseContentInformationV2(contentInformationUtility.RetrieveContentInformation(BranchCacheVersion.V2));

            CryptoAlgoId_Values cryptoAlgoId = CryptoAlgoId_Values.AES_128;

            PccrrClient pccrrClient = new PccrrClient(testConfig.HostedCacheServerComputerName, testConfig.HostedCacheServerHTTPListenPort);

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Send PCCRR GetSegList request to HostedCacheServer to make sure that this file does not exist in HostedCacheServer.");
            for (int i = 0; i < contentInformation.chunks.Length; ++i)
            {
                var chunk = contentInformation.chunks[i];
                for (int j = 0; j < chunk.chunkData.Length; j++)
                {
                    var pccrrGetSegListRequest = pccrrClient.CreateMsgGetSegListRequest(
                        cryptoAlgoId,
                        Guid.NewGuid(),
                        new byte[][] { contentInformation.GetSegmentId(i, j) });
                    pccrrClient.SendPacket(
                        pccrrGetSegListRequest,
                        testConfig.Timeout);
                    var pccrrGetSegListResponse
                        = (PccrrSegListResponsePacket)pccrrClient.ExpectPacket();

                    BaseTestSite.Assert.AreEqual <uint>(
                        0,
                        pccrrGetSegListResponse.MsgSegList.SegmentRangeCount,
                        "The server MUST set the SegmentRangeCount field to zero if it doesn't have the requested segments data.");
                }
            }

            using (PccrrTestServerV2 pccrrTestServer = new PccrrTestServerV2())
            {
                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Start PCCRR server to be ready to serve content to hosted cache server");

                pccrrTestServer.Start(
                    testConfig.ClientContentRetrievalListenPort,
                    cryptoAlgoId,
                    contentInformation,
                    content,
                    eventQueue);

                PCHCClient pchcClient = new PCHCClient(
                    TransferProtocol.HTTP,
                    testConfig.HostedCacheServerComputerName,
                    testConfig.ContentServerHTTPListenPort,
                    PchcConsts.HttpUrl,
                    testConfig.DomainName,
                    testConfig.UserName,
                    testConfig.UserPassword);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Offer all content segments to hosted cache server");

                var batchedOfferMessage = pchcClient.CreateBatchedOfferMessage(
                    testConfig.ClientContentRetrievalListenPort,
                    contentInformation);
                var responseMessage = pchcClient.SendBatchedOfferMessage(batchedOfferMessage);

                TestClassBase.BaseTestSite.Assert.AreEqual <RESPONSE_CODE>(
                    RESPONSE_CODE.OK,
                    responseMessage.ResponseCode,
                    @"The hosted cache MUST send a response code of 0 when BATCHED_OFFER_MESSAGE request received");

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Make sure all segments are retrieved by hosted cache server");

                int totalBlockCount = contentInformation.GetBlockCount();
                int blockCount      = 0;
                TestUtility.DoUntilSucceed(delegate()
                {
                    eventQueue.Expect <MessageArrivedEventArgs>(typeof(PccrrServer).GetEvent("MessageArrived"), delegate(System.Net.IPEndPoint sender, PccrrPacket pccrrPacket)
                    {
                        var pccrrGetBlksRequest = pccrrPacket as PccrrGETBLKSRequestPacket;

                        if (pccrrGetBlksRequest != null)
                        {
                            blockCount++;
                        }
                    });
                    return(blockCount == totalBlockCount);
                }, TimeSpan.MaxValue, TimeSpan.Zero);

                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Wait until cache is available on hosted cache server");

                TestUtility.DoUntilSucceed(() => sutControlAdapter.IsLocalCacheExisted(testConfig.HostedCacheServerComputerFQDNOrNetBiosName), testConfig.Timeout, testConfig.RetryInterval);
            }

            List <byte> retrievedContent = new List <byte>();

            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Start PCCRR client to retrieve all the content from hosted cache server");
            BaseTestSite.Log.Add(
                LogEntryKind.Debug,
                "Negotiate PCCRR version");
            var pccrrNegotiateRequest = pccrrClient.CreateMsgNegoRequest(
                ///[MS-PCCRR]Section 2.2.3: ProtVer: Both the Major and Minor version number can express the version range of 0x0000 to 0xFFFF. Currently, the protocol
                ///version number MUST be set to {major = 1 (0x0001), minor = 0 (0x0000)}.
                ///Active TDI#69240: Windows server 2012 Standard RTM sets the major version to 2
                new ProtoVersion {
                MajorVersion = 1, MinorVersion = 0
            },                                                           //MinSupportedProtocolVersion
                new ProtoVersion {
                MajorVersion = 1, MinorVersion = 0
            },                                                           //MaxSupportedProtocolVersion
                cryptoAlgoId,
                MsgType_Values.MSG_NEGO_REQ);

            pccrrClient.SendPacket(
                pccrrNegotiateRequest,
                testConfig.Timeout);
            var pccrrNegotiateResponse
                = (PccrrNegoResponsePacket)pccrrClient.ExpectPacket();

            if (testConfig.SupportBranchCacheV1)
            {
                BaseTestSite.Assert.IsTrue(
                    pccrrNegotiateResponse.MsgNegoResp.MinSupporteProtocolVersion.MajorVersion <= 1 &&
                    pccrrNegotiateResponse.MsgNegoResp.MaxSupporteProtocolVersion.MajorVersion >= 1,
                    "SupportedProtocolVersion doesn't match configuration");
            }

            Aes aes = PccrrUtitlity.CreateAes(cryptoAlgoId);

            for (int i = 0; i < contentInformation.chunks.Length; i++)
            {
                BaseTestSite.Log.Add(
                    LogEntryKind.Debug,
                    "Retrieve all segments in chunk {0}",
                    i);

                var chunk = contentInformation.chunks[i];
                for (int j = 0; j < chunk.chunkData.Length; j++)
                {
                    // Retrieve segment list
                    var pccrrGetSegListRequest = pccrrClient.CreateMsgGetSegListRequest(
                        cryptoAlgoId,
                        Guid.NewGuid(),
                        new byte[][] { contentInformation.GetSegmentId(i, j) });
                    pccrrClient.SendPacket(
                        pccrrGetSegListRequest,
                        testConfig.Timeout);
                    var pccrrGetSegListResponse
                        = (PccrrSegListResponsePacket)pccrrClient.ExpectPacket();

                    BaseTestSite.Assert.AreNotEqual <uint>(
                        0,
                        pccrrGetSegListResponse.MsgSegList.SegmentRangeCount,
                        "The server MUST set the SegmentRangeCount field to a value greater than zero if it has the requested segments data.");

                    BaseTestSite.Log.Add(
                        LogEntryKind.Debug,
                        "Retrieve segment {0} in chunk {1}",
                        j,
                        i);

                    PccrrGETBLKSRequestPacket pccrrBlkRequest = pccrrClient.CreateMsgGetBlksRequest(
                        contentInformation.GetSegmentId(i, j),
                        cryptoAlgoId,
                        MsgType_Values.MSG_GETBLKS,
                        0,
                        1);
                    pccrrClient.SendPacket(
                        pccrrBlkRequest,
                        testConfig.Timeout);
                    PccrrBLKResponsePacket pccrrBlkResponse
                        = (PccrrBLKResponsePacket)pccrrClient.ExpectPacket();

                    BaseTestSite.Assert.AreNotEqual <uint>(
                        0,
                        pccrrBlkResponse.MsgBLK.SizeOfBlock,
                        "The server MUST set the SizeOfBlock field to a value greater than zero if it has the requested blocks data.");

                    byte[] block = pccrrBlkResponse.MsgBLK.Block;

                    if (cryptoAlgoId != CryptoAlgoId_Values.NoEncryption)
                    {
                        block = PccrrUtitlity.Decrypt(aes, block, contentInformation.chunks[i].chunkData[j].SegmentSecret, pccrrBlkResponse.MsgBLK.IVBlock);
                    }

                    retrievedContent.AddRange(block);
                }
            }

            BaseTestSite.Assert.IsTrue(
                Enumerable.SequenceEqual(content, retrievedContent),
                "The retrieved cached data should be the same as server data.");
        }
Пример #8
0
        private void executeButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckInput())
                {
                    return;
                }

                logger.Clear();

                #region Read settings from UI

                var version       = (BranchCacheVersion)branchCacheVersionComboBox.SelectedItem;
                var operationMode = (OperationMode)operationModeComboBox.SelectedItem;
                var transport     = (ContentInformationTransport)transportComboBox.SelectedItem;

                var serverSecret = serverSecretTextBox.Text;
                var filePath     = filePathTextBox.Text;

                var           hashAlgoValue = (dwHashAlgo_Values)hashAlgorithmComboBox.SelectedItem;
                HashAlgorithm hashAlgorithm;
                HMAC          hmacAlgorithm;
                int           hashBlockSize;

                string server        = null;
                string file          = null;
                string sharedFolder  = null;
                Match  filePathMatch = null;
                switch (transport)
                {
                case ContentInformationTransport.PCCRTP:
                    filePathMatch = Regex.Match(filePath, httpFilePathPattern);
                    server        = filePathMatch.Groups["Server"].Value;
                    file          = filePathMatch.Groups["FileName"].Value;
                    break;

                case ContentInformationTransport.SMB2:
                    filePathMatch = Regex.Match(filePath, smb2FilePathPattern);
                    server        = filePathMatch.Groups["Server"].Value;
                    sharedFolder  = filePathMatch.Groups["SharedFolder"].Value;
                    file          = filePathMatch.Groups["FileName"].Value;
                    break;

                default:
                    throw new NotImplementedException();
                }


                SecurityPackageType securityPackageType = (SecurityPackageType)smb2AuthenticationComboBox.SelectedItem;

                string domainName   = domainNameTextBox.Text;
                string userName     = userNameTextBox.Text;
                string userPassword = userPasswordTextBox.Text;

                #endregion

                var timeout = TimeSpan.FromSeconds(60);

                byte[] content;
                byte[] contentInformation;

                Content_Information_Data_Structure    contentInformationStructure   = new Content_Information_Data_Structure();
                Content_Information_Data_Structure_V2 contentInformationStructureV2 = new Content_Information_Data_Structure_V2();

                #region Read content and content information

                if (operationMode == OperationMode.RemoteHashVerification)
                {
                    switch (transport)
                    {
                    case ContentInformationTransport.PCCRTP:
                        PccrtpClient  pccrtpClient  = new PccrtpClient();
                        PccrtpRequest pccrtpRequest = pccrtpClient.CreatePccrtpRequest(
                            server,
                            80,
                            file,
                            version);
                        PccrtpResponse pccrtpResponse = pccrtpClient.SendHttpRequest(
                            HttpVersionType.HttpVersion11,
                            pccrtpRequest,
                            (int)timeout.TotalMilliseconds);

                        if (pccrtpResponse.HttpResponse.ContentEncoding == "peerdist")
                        {
                            contentInformation = pccrtpResponse.PayloadData;

                            content = Utility.DownloadHTTPFile(server, file);
                        }
                        else
                        {
                            content = pccrtpResponse.PayloadData;

                            Thread.Sleep(5000);     // Wait for hash generation

                            pccrtpResponse = pccrtpClient.SendHttpRequest(
                                HttpVersionType.HttpVersion11,
                                pccrtpRequest,
                                (int)timeout.TotalMilliseconds);

                            contentInformation = pccrtpResponse.PayloadData;
                        }

                        break;

                    case ContentInformationTransport.SMB2:
                        using (Smb2ClientTransport smb2Client = new Smb2ClientTransport(timeout))
                        {
                            smb2Client.OpenFile(
                                server,
                                sharedFolder,
                                file,
                                securityPackageType,
                                domainName,
                                userName,
                                userPassword,
                                AccessMask.GENERIC_READ);

                            content = smb2Client.ReadAllBytes();

                            Thread.Sleep(5000);     // Wait for hash generation

                            HASH_HEADER hashHeader;
                            smb2Client.ReadHash(
                                SRV_READ_HASH_Request_HashType_Values.SRV_HASH_TYPE_PEER_DIST,
                                version == BranchCacheVersion.V1 ? SRV_READ_HASH_Request_HashVersion_Values.SRV_HASH_VER_1 : SRV_READ_HASH_Request_HashVersion_Values.SRV_HASH_VER_2,
                                version == BranchCacheVersion.V1 ? SRV_READ_HASH_Request_HashRetrievalType_Values.SRV_HASH_RETRIEVE_HASH_BASED : SRV_READ_HASH_Request_HashRetrievalType_Values.SRV_HASH_RETRIEVE_FILE_BASED,
                                0,
                                uint.MaxValue,
                                out hashHeader,
                                out contentInformation);
                        }

                        break;

                    default:
                        throw new NotImplementedException();
                    }

                    switch (version)
                    {
                    case BranchCacheVersion.V1:
                        contentInformationStructure = PccrcUtility.ParseContentInformation(contentInformation);
                        break;

                    case BranchCacheVersion.V2:
                        contentInformationStructureV2 = PccrcUtility.ParseContentInformationV2(contentInformation);
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }
                else
                {
                    content = File.ReadAllBytes(filePath);
                }

                #endregion

                #region Calculate hash and execute verification

                switch (version)
                {
                case BranchCacheVersion.V1:

                    if (operationMode == OperationMode.RemoteHashVerification)
                    {
                        PccrcUtility.GetHashAlgorithm(contentInformationStructure.dwHashAlgo, out hashAlgorithm, out hmacAlgorithm, out hashBlockSize);
                    }
                    else
                    {
                        PccrcUtility.GetHashAlgorithm(hashAlgoValue, out hashAlgorithm, out hmacAlgorithm, out hashBlockSize);
                    }
                    hmacAlgorithm.Key = hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(serverSecret));

                    logger.LogInfo(
                        "Ks = Hash(ServerSecret): {0}",
                        Utility.ToHexString(hmacAlgorithm.Key));

                    logger.NewLine();

                    int blockTotalCount = content.Length / BLOCKBYTECOUNT;
                    if (content.Length > BLOCKBYTECOUNT * blockTotalCount)
                    {
                        blockTotalCount = blockTotalCount + 1;
                    }

                    int segmentCount = blockTotalCount / SEGMENTBLOCKCOUNT;
                    if (blockTotalCount > SEGMENTBLOCKCOUNT * segmentCount)
                    {
                        segmentCount = segmentCount + 1;
                    }

                    for (int segmentIndex = 0; segmentIndex < segmentCount; segmentIndex++)
                    {
                        logger.LogInfo("Segment{0}", segmentIndex);
                        logger.NewLine();
                        logger.Indent();

                        List <byte> blockHashList = new List <byte>();

                        List <byte> tempList = new List <byte>();

                        int blockCount = (segmentIndex == segmentCount - 1) ? (blockTotalCount % SEGMENTBLOCKCOUNT) : (SEGMENTBLOCKCOUNT);

                        for (int blockIndex = 0; blockIndex < blockCount; blockIndex++)
                        {
                            logger.LogInfo(
                                "Block{0} Offset {1} Length {2}",
                                blockIndex,
                                BLOCKBYTECOUNT * SEGMENTBLOCKCOUNT * segmentIndex + BLOCKBYTECOUNT * blockIndex,
                                BLOCKBYTECOUNT);
                            logger.NewLine();
                            logger.Indent();

                            var block = content.Skip(BLOCKBYTECOUNT * SEGMENTBLOCKCOUNT * segmentIndex + BLOCKBYTECOUNT * blockIndex).Take(BLOCKBYTECOUNT).ToArray();

                            byte[] blockHash = hashAlgorithm.ComputeHash(block);

                            logger.LogInfo("BlockHash{0} = Hash(Block): {1}", blockIndex, Utility.ToHexString(blockHash));

                            if (operationMode == OperationMode.RemoteHashVerification &&
                                !blockHash.SequenceEqual(contentInformationStructure.blocks[segmentIndex].BlockHashes.Skip(blockIndex * hashBlockSize).Take(hashBlockSize)))
                            {
                                logger.LogError("Server Returned: {0}", Utility.ToHexString(contentInformationStructure.blocks[segmentIndex].BlockHashes.Skip(blockIndex * hashBlockSize).Take(hashBlockSize).ToArray()));
                            }

                            blockHashList.AddRange(blockHash);

                            logger.Unindent();
                            logger.NewLine();
                        }

                        byte[] hod = hashAlgorithm.ComputeHash(blockHashList.ToArray());

                        logger.LogInfo(
                            "HoD = Hash(BlockHash0 + BlockHash1 + ... + BlockHashN): {0}",
                            Utility.ToHexString(hod));

                        if (operationMode == OperationMode.RemoteHashVerification &&
                            !hod.SequenceEqual(contentInformationStructure.segments[segmentIndex].SegmentHashOfData))
                        {
                            logger.LogError("Server Returned: {0}", Utility.ToHexString(contentInformationStructure.segments[segmentIndex].SegmentHashOfData));
                        }

                        logger.NewLine();

                        byte[] kp = hmacAlgorithm.ComputeHash(hod);

                        logger.LogInfo(
                            "Kp = HMAC(Ks, HoD): {0}",
                            Utility.ToHexString(kp));

                        if (operationMode == OperationMode.RemoteHashVerification &&
                            !kp.SequenceEqual(contentInformationStructure.segments[segmentIndex].SegmentSecret))
                        {
                            logger.LogError("Server Returned: {0}", Utility.ToHexString(contentInformationStructure.segments[segmentIndex].SegmentSecret));
                        }

                        logger.NewLine();

                        tempList.AddRange(hod);
                        tempList.AddRange(Encoding.Unicode.GetBytes(HOHODK_APPEND_STRING));

                        byte[] hoHoDK = hashAlgorithm.ComputeHash(tempList.ToArray());

                        logger.LogInfo(
                            "hoHoDK = HMAC(HoD + \"MS_P2P_CACHING\"): {0}",
                            Utility.ToHexString(hoHoDK));

                        logger.NewLine();

                        logger.Unindent();
                    }
                    break;

                case BranchCacheVersion.V2:

                    PccrcUtility.GetHashAlgorithm(dwHashAlgoV2_Values.TRUNCATED_SHA512, out hashAlgorithm, out hmacAlgorithm);
                    hmacAlgorithm.Key = hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(serverSecret)).Take(32).ToArray();

                    logger.LogInfo(
                        "Ks = Hash(ServerSecret): {0}",
                        Utility.ToHexString(hmacAlgorithm.Key));

                    logger.NewLine();

                    int segmentLength = BLOCKBYTECOUNT;
                    int chunkCount    = 1;

                    if (operationMode == OperationMode.RemoteHashVerification)
                    {
                        chunkCount = contentInformationStructureV2.chunks.Length;
                    }

                    int segmentOffset = 0;
                    for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++)
                    {
                        logger.LogInfo("Chunk{0}", chunkIndex);
                        logger.NewLine();
                        logger.Indent();

                        segmentCount = content.Length / segmentLength;
                        if (content.Length > segmentCount * segmentLength)
                        {
                            segmentCount++;
                        }

                        if (operationMode == OperationMode.RemoteHashVerification)
                        {
                            segmentCount = contentInformationStructureV2.chunks[chunkIndex].chunkData.Length;
                        }

                        for (int segmentIndex = 0; segmentIndex < segmentCount; ++segmentIndex)
                        {
                            logger.LogInfo(
                                "Segment{0} Offset {1} Length {2}",
                                segmentIndex,
                                segmentOffset,
                                BLOCKBYTECOUNT);
                            logger.NewLine();
                            logger.Indent();

                            if (operationMode == OperationMode.RemoteHashVerification)
                            {
                                segmentLength = (int)contentInformationStructureV2.chunks[chunkIndex].chunkData[segmentIndex].cbSegment;
                            }

                            List <byte> tempList = new List <byte>();

                            var segment = content.Skip(segmentOffset).Take(segmentLength).ToArray();

                            segmentOffset += segmentLength;

                            //TRANCATED_SHA_512
                            byte[] hod = hashAlgorithm.ComputeHash(segment).Take(32).ToArray();

                            logger.LogInfo(
                                "HoD = Hash(Segment): {0}",
                                Utility.ToHexString(hod));

                            if (operationMode == OperationMode.RemoteHashVerification &&
                                !hod.SequenceEqual(contentInformationStructureV2.chunks[chunkIndex].chunkData[segmentIndex].SegmentHashOfData))
                            {
                                logger.LogError("Server Returned: {0}", Utility.ToHexString(contentInformationStructureV2.chunks[chunkIndex].chunkData[segmentIndex].SegmentHashOfData));
                            }

                            logger.NewLine();

                            byte[] kp = hmacAlgorithm.ComputeHash(hod).Take(32).ToArray();

                            logger.LogInfo(
                                "Kp = HMAC(Ks, HoD): {0}",
                                Utility.ToHexString(kp));

                            if (operationMode == OperationMode.RemoteHashVerification &&
                                !kp.SequenceEqual(contentInformationStructureV2.chunks[chunkIndex].chunkData[segmentIndex].SegmentSecret))
                            {
                                logger.LogError("Server Returned: {0}", Utility.ToHexString(contentInformationStructureV2.chunks[chunkIndex].chunkData[segmentIndex].SegmentSecret));
                            }

                            logger.NewLine();

                            tempList.AddRange(hod);
                            tempList.AddRange(Encoding.Unicode.GetBytes(HOHODK_APPEND_STRING));

                            byte[] hoHoDK = hashAlgorithm.ComputeHash(tempList.ToArray());

                            logger.LogInfo(
                                "hoHoDK = HMAC(HoD + \"MS_P2P_CACHING\"): {0}",
                                Utility.ToHexString(hoHoDK));

                            logger.NewLine();

                            logger.Unindent();
                        }
                    }

                    break;

                default:
                    throw new NotImplementedException();
                }

                if (operationMode == OperationMode.RemoteHashVerification)
                {
                    if (logger.HasError)
                    {
                        Utility.ShowMessageBox("Hash verification error found!", MessageBoxIcon.Error);
                    }
                    else
                    {
                        Utility.ShowMessageBox("Hash verification passed!", MessageBoxIcon.Information);
                    }
                }

                #endregion
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(ex.Message + "\r\n\r\n" + ex.StackTrace, MessageBoxIcon.Error);
            }
        }