Skip to content

Commit

Permalink
[core] Ensure EncryptorFactory actually receives the data we requested
Browse files Browse the repository at this point in the history
If the initial buffer is unencrypted we can return it straight away
if plain text connections are supported.

If the initial buffer is encrypted, then it will be consumed as part
of the normal encrypted connection negotiation, so we'll need to
receive the actual data before returning.
  • Loading branch information
alanmcgovern committed Aug 15, 2019
1 parent e0cbf35 commit 69eaf7b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ static async Task<EncryptorResult> DoCheckIncomingConnectionAsync(IConnection co
message.Decode(buffer, 0, buffer.Length);

if (message.ProtocolString == VersionInfo.ProtocolStringV100) {
if (supportsPlainText) {
if (supportsPlainText)
return new EncryptorResult (PlainTextEncryption.Instance, PlainTextEncryption.Instance, buffer);
}
}
else if (supportsRC4Header || supportsRC4Full)
{
Expand All @@ -96,7 +95,15 @@ static async Task<EncryptorResult> DoCheckIncomingConnectionAsync(IConnection co
if (encSocket.Decryptor is RC4 && !supportsRC4Full)
throw new EncryptionException("Decryptor was RC4Full but that is not allowed");

// As the connection was encrypted, the data we got from the initial Receive call will have
// been consumed during the crypto handshake process. Now that the encrypted handshake has
// been established, we should ensure we read the data again.
var data = encSocket.InitialData?.Length > 0 ? encSocket.InitialData : null;
if (data == null && bytesToReceive > 0) {
data = buffer;
await NetworkIO.ReceiveAsync (connection, data, 0, data.Length, null, null, null);
encSocket.Decryptor.Decrypt (data);
}
return new EncryptorResult (encSocket.Decryptor, encSocket.Encryptor, data);
}

Expand Down
18 changes: 2 additions & 16 deletions src/MonoTorrent/MonoTorrent.Client/Managers/ListenManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,9 @@ private async void ConnectionReceived(object sender, NewConnectionEventArgs e)
var result = await EncryptorFactory.CheckIncomingConnectionAsync(id.Connection, id.Peer.Encryption, Engine.Settings, HandshakeMessage.HandshakeLength, skeys.ToArray());
id.Decryptor = result.Decryptor;
id.Encryptor = result.Encryptor;
var initialData = result.InitialData;
if (initialData != null && initialData.Length != HandshakeMessage.HandshakeLength)
{
e.Connection.Dispose();
return;
}

HandshakeMessage handshake;
if (initialData == null)
{
handshake = await PeerIO.ReceiveHandshakeAsync(id.Connection, id.Decryptor);
}
else
{
handshake = new HandshakeMessage();
handshake.Decode(initialData, 0, initialData.Length);
}
var handshake = new HandshakeMessage();
handshake.Decode(result.InitialData, 0, result.InitialData.Length);
if (!await HandleHandshake(id, handshake))
e.Connection.Dispose();
}
Expand Down

0 comments on commit 69eaf7b

Please sign in to comment.