Exemple #1
0
        /// <summary>
        /// Authenticates the client, if the password characters match.
        /// </summary>
        /// <param name="password">The characters of the password.</param>
        /// <returns><c>true</c> if authentication succeeded.</returns>
        public bool Accept(char[] password)
        {
            Throw.If.Null(password, "password");

            var response = new byte[16];

            VncPasswordChallenge.GetChallengeResponse(this.challenge, password, response);
            return(this.Test(response));
        }
        void NegotiateSecurity(AuthenticationMethod[] methods)
        {
            _c.SendByte((byte)methods.Length);
            //验证密码
            VncStream.Require(methods.Length > 0,
                              "Client is not allowed in.",
                              VncFailureReason.NoSupportedAuthenticationMethods);
            foreach (var method in methods)
            {
                _c.SendByte((byte)method);
            }

            var selectedMethod = (AuthenticationMethod)_c.ReceiveByte();

            VncStream.Require(methods.Contains(selectedMethod),
                              "Invalid authentication method.",
                              VncFailureReason.UnrecognizedProtocolElement);

            bool success = true;

            if (selectedMethod == AuthenticationMethod.Password)
            {
                var challenge = VncPasswordChallenge.GenerateChallenge();
                using (new Utility.AutoClear(challenge))
                {
                    _c.Send(challenge);

                    var response = _c.Receive(16);
                    using (new Utility.AutoClear(response))
                    {
                        var e = new PasswordProvidedEventArgs(challenge, response);
                        OnPasswordProvided(e);
                        success = e.IsAuthenticated;
                    }
                }
            }

            _c.SendUInt32BE(success ? 0 : (uint)1);

            if (!success)
            {
                _c.SendString("Password authentication failed!", true);
            }

            VncStream.Require(success,
                              "Failed to authenticate.",
                              VncFailureReason.AuthenticationFailed);
        }
        public void GetChallengeResponseTest()
        {
            VncPasswordChallenge challenger = new VncPasswordChallenge();

            var challenge = new byte[]
            {
                0x71, 0x43, 0x19, 0xf2, 0xb3, 0xf6, 0xac, 0xcf,
                0x8c, 0x10, 0xc0, 0x06, 0x6e, 0x73, 0xb1, 0xd9
            };

            var password      = "******".ToCharArray();
            var passwordBytes = VncStream.EncodeString(password, 0, password.Length);
            var response      = new byte[16];

            challenger.GetChallengeResponse(challenge, passwordBytes, response);

            var expectedResponse = new byte[]
            {
                0x71, 0xb3, 0x6f, 0xa2, 0x44, 0x5a, 0xee, 0x4f,
                0x08, 0x70, 0x21, 0x69, 0x6e, 0x32, 0x87, 0x8e
            };

            Assert.Equal(expectedResponse, response);
        }