Esempio n. 1
0
        private void SendNegotiateFrame()
        {
            ClientNegotiateFrame frame = new ClientNegotiateFrame();

            frame.MinVersion = MIN_VERSION;
            frame.MaxVersion = MAX_VERSION;
            frame.Nonce      = ServerNonce;
            frame.IsMutual   = MutualAuthenticationRequired;
            frame.Difficulty = ChallengeDifficulty;

            SendFrame(frame);
        }
Esempio n. 2
0
        private void HandleNegotiateFrame(ClientNegotiateFrame frame)
        {
            if (frame.MinVersion > MAX_VERSION || frame.MaxVersion < MIN_VERSION)
            {
                OnExceptionThrown(new InvalidDataException("Invalid protocol version."));
                return;
            }

            if (frame.Difficulty > MAX_DIFFICULTY)
            {
                OnExceptionThrown(new InvalidDataException("Challenge difficulty is too high."));
                return;
            }

            if (frame.IsMutual && LocalTag == null)
            {
                OnExceptionThrown(new InvalidOperationException("Server requires mutual authentication."));
                return;
            }

            HashPuzzle puzzle  = new HashPuzzle(HashAlgorithmName.SHA256, frame.Difficulty, frame.Nonce);
            bool       success = puzzle.FindSolution();

            if (success)
            {
                Version     = Math.Min(frame.MaxVersion, MAX_VERSION);
                ServerNonce = frame.Nonce;
                ClientNonce = CreateNonce(NONCE_LENGTH);

                BufferFrame(new ClientExchangeFrame(), FrameState.ClientExchange);
            }
            else
            {
                BufferFrame(new ClientNegotiateFrame(), FrameState.ClientNegotiate);
            }

            SendExchangeFrame(success, puzzle.Solution);
        }