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 NegotiateVersionNot38Test(string version) { using (var stream = new TestStream()) { // Mimick the client negotiating RFB 3.8 VncStream clientStream = new VncStream(stream.Input); clientStream.SendString(version); stream.Input.Position = 0; VncServerSession session = new VncServerSession(); session.Connect(stream, null, startThread: false); Assert.True(session.NegotiateVersion(out AuthenticationMethod[] methods)); Assert.Empty(methods); stream.Output.Position = 0; Assert.Equal(Encoding.UTF8.GetBytes("RFB 003.008\n"), ((MemoryStream)stream.Output).ToArray()); } }
public void NegotiateVersionBoth38Test(VncServerSessionOptions sessionOptions, AuthenticationMethod expectedAuthenticationMethod) { using (var stream = new TestStream()) { // Mimick the client negotiating RFB 3.8 VncStream clientStream = new VncStream(stream.Input); clientStream.SendString("RFB 003.008\n"); stream.Input.Position = 0; VncServerSession session = new VncServerSession(); session.Connect(stream, sessionOptions, startThread: false); Assert.True(session.NegotiateVersion(out AuthenticationMethod[] methods)); Assert.Collection( methods, (m) => Assert.Equal(expectedAuthenticationMethod, m)); stream.Output.Position = 0; Assert.Equal(Encoding.UTF8.GetBytes("RFB 003.008\n"), ((MemoryStream)stream.Output).ToArray()); } }