Skip to content

Commit

Permalink
fixes #16 invalid reading and writing in SslFilter, refs #15 add clie…
Browse files Browse the repository at this point in the history
…nt SSL tests
  • Loading branch information
longshine committed Oct 12, 2015
1 parent 016c1cc commit 5d6fcbd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
12 changes: 8 additions & 4 deletions Mina.NET/Filter/Ssl/SslFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public SslFilter(String targetHost, X509CertificateCollection clientCertificates
TargetHost = targetHost;
ClientCertificates = clientCertificates;
UseClientMode = true;
CheckCertificateRevocation = true;
CheckCertificateRevocation = false;
}

/// <summary>
Expand Down Expand Up @@ -86,7 +86,8 @@ public X509Certificate Certificate
/// <summary>
/// Gets or sets a <see cref="Boolean"/> value that specifies
/// whether the certificate revocation list is checked during authentication.
/// The default value is <code>true</code>.
/// The default value is <code>true</code> in server mode,
/// <code>false</code> in client mode.
/// </summary>
public Boolean CheckCertificateRevocation { get; set; }

Expand Down Expand Up @@ -446,6 +447,9 @@ public void ScheduleFilterWrite(INextFilter nextFilter, IWriteRequest writeReque
}

IoBuffer buf = (IoBuffer)writeRequest.Message;
if (!buf.HasRemaining)

This comment has been minimized.

Copy link
@longshine

longshine Oct 12, 2015

Author Owner

Client sending an empty buffer into the SslStream will make the stream in the server hang up:
the following call to _sslStream.Read in SslHandler.ReadBufer will never return.
Don't know why : (

// empty message will break this SSL stream?
return;
lock (this)
{
ArraySegment<Byte> array = buf.GetRemaining();
Expand Down Expand Up @@ -507,7 +511,7 @@ private IoBuffer ReadBuffer()
while (true)
{
ArraySegment<Byte> array = buf.GetRemaining();
Int32 bytesRead = _sslStream.Read(array.Array, array.Offset, buf.Capacity);
Int32 bytesRead = _sslStream.Read(array.Array, array.Offset, array.Count);

This comment has been minimized.

Copy link
@longshine

longshine Oct 12, 2015

Author Owner

refs #16 incorrect count parameter for _sslStream.Read().
the offset + count should <= buf.Capacity

if (bytesRead <= 0)
break;
buf.Position += bytesRead;
Expand All @@ -517,7 +521,7 @@ private IoBuffer ReadBuffer()
else
{
// We have to grow the target buffer, it's too small.
buf.Capacity <<= buf.Capacity;

This comment has been minimized.

Copy link
@longshine

longshine Oct 12, 2015

Author Owner

my bad : (

buf.Capacity <<= 1;
buf.Limit = buf.Capacity;
}
}
Expand Down
50 changes: 49 additions & 1 deletion Mina.Test/Filter/Ssl/SslTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
using Mina.Core.Filterchain;
using Mina.Core.Buffer;
using Mina.Filter.Codec;
using Mina.Filter.Codec.TextLine;
using Mina.Transport.Socket;
Expand Down Expand Up @@ -79,6 +80,10 @@ private static void StartServer()
Debug.WriteLine("Server got: 'send', sending 'data'");
e.Session.Write("data");
}
else
{
Debug.WriteLine("Server got: " + line);
}
};

acceptor.Bind(new IPEndPoint(IPAddress.Any, port));
Expand All @@ -89,6 +94,49 @@ private static void StartClient()
ConnectAndSend();

ConnectAndSend();

ClientConnect();
}

private static void ClientConnect()
{
using (var client = new AsyncSocketConnector())
using (var ready = new System.Threading.ManualResetEventSlim(false))
{
client.FilterChain.AddLast("ssl", new SslFilter("TempCert", null));
client.FilterChain.AddLast("text", new ProtocolCodecFilter(new TextLineCodecFactory()));

client.MessageReceived += (s, e) =>
{
Debug.WriteLine("Client got: " + e.Message);
ready.Set();
};

var session = client.Connect(new IPEndPoint(IPAddress.Loopback, port)).Await().Session;

Debug.WriteLine("Client sending: hello");
session.Write("hello ");

Debug.WriteLine("Client sending: send");
session.Write("send");

ready.Wait(3000);
Assert.IsTrue(ready.IsSet);

ready.Reset();
Assert.IsFalse(ready.IsSet);

Debug.WriteLine("Client sending: hello");
session.Write(IoBuffer.Wrap(Encoding.UTF8.GetBytes("hello \n")));

Debug.WriteLine("Client sending: send");
session.Write(IoBuffer.Wrap(Encoding.UTF8.GetBytes("send\n")));

ready.Wait(3000);
Assert.IsTrue(ready.IsSet);

session.Close(true);
}
}

private static void ConnectAndSend()
Expand All @@ -104,7 +152,6 @@ private static void ConnectAndSend()
// The server name must match the name on the server certificate.
sslStream.AuthenticateAsClient("TempCert");


Debug.WriteLine("Client sending: hello");
sslStream.Write(Encoding.UTF8.GetBytes("hello \n"));
sslStream.Flush();
Expand All @@ -116,6 +163,7 @@ private static void ConnectAndSend()
String line = ReadMessage(sslStream);
Debug.WriteLine("Client got: " + line);
client.Close();
Assert.IsTrue(!String.IsNullOrEmpty(line));
}

static string ReadMessage(SslStream sslStream)
Expand Down

0 comments on commit 5d6fcbd

Please sign in to comment.