Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Jan 30, 2019
1 parent ac8de79 commit 8dc3690
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
24 changes: 21 additions & 3 deletions source/NetCoreServer/SslClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public int OptionSendBufferSize
private bool _disconnecting;
private SocketAsyncEventArgs _connectEventArg;
private SslStream _sslStream;
private Guid? _sslStreamId;

/// <summary>
/// Is the client connected?
Expand Down Expand Up @@ -188,6 +189,7 @@ public virtual bool Disconnect()
{
// Dispose the SSL stream & buffer
_sslStream.Dispose();
_sslStreamId = null;

// Shutdown the socket associated with the client
Socket.Shutdown(SocketShutdown.Both);
Expand Down Expand Up @@ -323,7 +325,7 @@ private void TryReceive()
return;

_receiving = true;
result = _sslStream.BeginRead(_receiveBuffer.Data, 0, (int) _receiveBuffer.Capacity, ProcessReceive, this);
result = _sslStream.BeginRead(_receiveBuffer.Data, 0, (int) _receiveBuffer.Capacity, ProcessReceive, _sslStreamId);
} while (result.CompletedSynchronously);

}
Expand Down Expand Up @@ -370,7 +372,7 @@ private void TrySend()
{
// Async write with the write handler
_sending = true;
_sslStream.BeginWrite(_sendBufferFlush.Data, (int)_sendBufferFlushOffset, (int)(_sendBufferFlush.Size - _sendBufferFlushOffset), ProcessSend, this);
_sslStream.BeginWrite(_sendBufferFlush.Data, (int)_sendBufferFlushOffset, (int)(_sendBufferFlush.Size - _sendBufferFlushOffset), ProcessSend, _sslStreamId);
}
catch (ObjectDisposedException) {}
}
Expand Down Expand Up @@ -450,11 +452,12 @@ private void ProcessConnect(SocketAsyncEventArgs e)
try
{
// Create SSL stream
_sslStreamId = Guid.NewGuid();
_sslStream = (Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false);

// Begin the SSL handshake
_handshaking = true;
_sslStream.BeginAuthenticateAsClient(Address, Context.Certificates ?? new X509CertificateCollection(new[] { Context.Certificate }), Context.Protocols, true, ProcessHandshake, this);
_sslStream.BeginAuthenticateAsClient(Address, Context.Certificates ?? new X509CertificateCollection(new[] { Context.Certificate }), Context.Protocols, true, ProcessHandshake, _sslStreamId);
}
catch (Exception)
{
Expand Down Expand Up @@ -482,6 +485,11 @@ private void ProcessHandshake(IAsyncResult result)
if (IsHandshaked)
return;

// Validate SSL stream Id
var sslStreamId = result.AsyncState as Guid?;
if (_sslStreamId != sslStreamId)
return;

// End the SSL handshake
_sslStream.EndAuthenticateAsClient(result);

Expand Down Expand Up @@ -517,6 +525,11 @@ private void ProcessReceive(IAsyncResult result)
if (!IsHandshaked)
return;

// Validate SSL stream Id
var sslStreamId = result.AsyncState as Guid?;
if (_sslStreamId != sslStreamId)
return;

// End the SSL read
long size = _sslStream.EndRead(result);

Expand Down Expand Up @@ -562,6 +575,11 @@ private void ProcessSend(IAsyncResult result)
if (!IsHandshaked)
return;

// Validate SSL stream Id
var sslStreamId = result.AsyncState as Guid?;
if (_sslStreamId != sslStreamId)
return;

// End the SSL write
_sslStream.EndWrite(result);

Expand Down
24 changes: 21 additions & 3 deletions source/NetCoreServer/SslSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public int OptionSendBufferSize

private bool _disconnecting;
private SslStream _sslStream;
private Guid? _sslStreamId;

/// <summary>
/// Is the session connected?
Expand Down Expand Up @@ -127,10 +128,11 @@ internal void Connect(Socket socket)
try
{
// Create SSL stream
_sslStreamId = Guid.NewGuid();
_sslStream = (Server.Context.CertificateValidationCallback != null) ? new SslStream(new NetworkStream(Socket, false), false, Server.Context.CertificateValidationCallback) : new SslStream(new NetworkStream(Socket, false), false);

// Begin the SSL handshake
_sslStream.BeginAuthenticateAsServer(Server.Context.Certificate, Server.Context.ClientCertificateRequired, Server.Context.Protocols, false, ProcessHandshake, this);
_sslStream.BeginAuthenticateAsServer(Server.Context.Certificate, Server.Context.ClientCertificateRequired, Server.Context.Protocols, false, ProcessHandshake, _sslStreamId);
}
catch (Exception)
{
Expand Down Expand Up @@ -158,6 +160,7 @@ public virtual bool Disconnect()
{
// Dispose the SSL stream & buffer
_sslStream.Dispose();
_sslStreamId = null;

// Shutdown the socket associated with the client
Socket.Shutdown(SocketShutdown.Both);
Expand Down Expand Up @@ -284,7 +287,7 @@ private void TryReceive()
return;

_receiving = true;
result = _sslStream.BeginRead(_receiveBuffer.Data, 0, (int) _receiveBuffer.Capacity, ProcessReceive, this);
result = _sslStream.BeginRead(_receiveBuffer.Data, 0, (int) _receiveBuffer.Capacity, ProcessReceive, _sslStreamId);
} while (result.CompletedSynchronously);
}
catch (ObjectDisposedException) {}
Expand Down Expand Up @@ -330,7 +333,7 @@ private void TrySend()
{
// Async write with the write handler
_sending = true;
_sslStream.BeginWrite(_sendBufferFlush.Data, (int)_sendBufferFlushOffset, (int)(_sendBufferFlush.Size - _sendBufferFlushOffset), ProcessSend, this);
_sslStream.BeginWrite(_sendBufferFlush.Data, (int)_sendBufferFlushOffset, (int)(_sendBufferFlush.Size - _sendBufferFlushOffset), ProcessSend, _sslStreamId);
}
catch (ObjectDisposedException) {}
}
Expand Down Expand Up @@ -367,6 +370,11 @@ private void ProcessHandshake(IAsyncResult result)
if (IsHandshaked)
return;

// Validate SSL stream Id
var sslStreamId = result.AsyncState as Guid?;
if (_sslStreamId != sslStreamId)
return;

// End the SSL handshake
_sslStream.EndAuthenticateAsServer(result);

Expand Down Expand Up @@ -405,6 +413,11 @@ private void ProcessReceive(IAsyncResult result)
if (!IsHandshaked)
return;

// Validate SSL stream Id
var sslStreamId = result.AsyncState as Guid?;
if (_sslStreamId != sslStreamId)
return;

// End the SSL read
long size = _sslStream.EndRead(result);

Expand Down Expand Up @@ -448,6 +461,11 @@ private void ProcessSend(IAsyncResult result)
{
_sending = false;

// Validate SSL stream Id
var sslStreamId = result.AsyncState as Guid?;
if (_sslStreamId != sslStreamId)
return;

if (!IsHandshaked)
return;

Expand Down

0 comments on commit 8dc3690

Please sign in to comment.