Example #1
0
        private void HandleExchangeFrame(ServerExchangeFrame frame)
        {
            if (frame.Success)
            {
                if (frame.Tag.Length > MAX_TAG_LENGTH)
                {
                    OnExceptionThrown(new InvalidDataException("Invalid tag length."));
                    return;
                }

                HashPuzzle puzzle = new HashPuzzle(HashAlgorithmName.SHA256, ChallengeDifficulty, ServerNonce);

                if (!puzzle.VerifySolution(frame.Solution))
                {
                    OnExceptionThrown(new InvalidDataException("Challenge failed."));
                    return;
                }

                if (frame.Tag.Length == 0)
                {
                    if (MutualAuthenticationRequired)
                    {
                        OnExceptionThrown(new InvalidDataException("Tag missing."));
                        return;
                    }
                }
                else
                {
                    _remoteTag = Tag.Deserialize(frame.Tag);

                    if (TrustedIssuers != null && _remoteTag.Verify(TrustedIssuers))
                    {
                        IsMutuallyAuthenticated = true;
                    }
                    if (MutualAuthenticationRequired)
                    {
                        OnExceptionThrown(new InvalidDataException("Invalid tag."));
                        return;
                    }
                }

                ClientNonce = frame.Nonce;

                BufferFrame(new ServerValidateFrame(), FrameState.ServerValidate);
                SendExchangeFrame(GetPublicKey(), IsMutuallyAuthenticated);
            }
            else
            {
                OnChallengeFailed();

                //TODO: check if we're disposed

                ServerNonce = CreateNonce(NONCE_LENGTH);

                BufferFrame(new ServerExchangeFrame(), FrameState.ServerExchange);
                SendNegotiateFrame();
            }
        }
Example #2
0
        private void SendExchangeFrame(bool success, byte[] solution)
        {
            ServerExchangeFrame frame = new ServerExchangeFrame();

            frame.Success  = success;
            frame.Solution = solution;
            frame.Nonce    = ClientNonce;
            frame.Tag      = LocalTag?.RawData;

            SendFrame(frame);
        }