Changes for fixing Mono issues

- Detect missing HTTPS certificates
- Give the mozroots command for importing certificates
- Use a specific workaround AesStream class made for Mono
- It only process 128bits blocks because Mono is not CFB-8 ready
This commit is contained in:
ORelio 2014-03-26 10:12:05 +01:00
parent 58d7b0734f
commit 26a2fdd517
3 changed files with 186 additions and 9 deletions

View file

@ -192,10 +192,22 @@ namespace MinecraftClient
}
/// <summary>
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
/// Interface for AES stream
/// Allows to use any object which has a Read() and Write() method.
/// </summary>
public class AesStream : System.IO.Stream
public interface IAesStream
{
int Read(byte[] buffer, int offset, int count);
void Write(byte[] buffer, int offset, int count);
}
/// <summary>
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
/// This is the regular AesStream class used with the regular .NET framework from Microsoft.
/// </summary>
public class AesStream : System.IO.Stream, IAesStream
{
CryptoStream enc;
CryptoStream dec;
@ -286,5 +298,140 @@ namespace MinecraftClient
return cipher;
}
}
/// <summary>
/// An encrypted stream using AES, used for encrypting network data on the fly using AES.
/// This is a mono-compatible adaptation which only sends and receive 16 bytes at a time, and manually transforms blocks.
/// Data is cached before reaching the 128bits block size necessary for mono which is not CFB-8 compatible.
/// </summary>
public class MonoAesStream : System.IO.Stream, IAesStream
{
ICryptoTransform enc;
ICryptoTransform dec;
List<byte> dec_cache = new List<byte>();
List<byte> tosend_cache = new List<byte>();
public MonoAesStream(System.IO.Stream stream, byte[] key)
{
BaseStream = stream;
RijndaelManaged aes = GenerateAES(key);
enc = aes.CreateEncryptor();
dec = aes.CreateDecryptor();
}
public System.IO.Stream BaseStream { get; set; }
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Flush()
{
BaseStream.Flush();
}
public override long Length
{
get { throw new NotSupportedException(); }
}
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public override int ReadByte()
{
byte[] temp = new byte[1];
Read(temp, 0, 1);
return temp[0];
}
public override int Read(byte[] buffer, int offset, int count)
{
while (dec_cache.Count < count)
{
byte[] temp_in = new byte[16];
byte[] temp_out = new byte[16];
int read = 0;
while (read < 16)
read += BaseStream.Read(temp_in, read, 16 - read);
dec.TransformBlock(temp_in, 0, 16, temp_out, 0);
foreach (byte b in temp_out)
dec_cache.Add(b);
}
for (int i = offset; i - offset < count; i++)
{
buffer[i] = dec_cache[0];
dec_cache.RemoveAt(0);
}
return count;
}
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void WriteByte(byte b)
{
Write(new byte[] { b }, 0, 1);
}
public override void Write(byte[] buffer, int offset, int count)
{
for (int i = offset; i - offset < count; i++)
tosend_cache.Add(buffer[i]);
while (tosend_cache.Count > 16)
{
byte[] temp_in = new byte[16];
byte[] temp_out = new byte[16];
for (int i = 0; i < 16; i++)
{
temp_in[i] = tosend_cache[0];
tosend_cache.RemoveAt(0);
}
enc.TransformBlock(temp_in, 0, 16, temp_out, 0);
BaseStream.Write(temp_out, 0, 16);
}
}
private RijndaelManaged GenerateAES(byte[] key)
{
RijndaelManaged cipher = new RijndaelManaged();
cipher.Mode = CipherMode.CFB;
cipher.Padding = PaddingMode.None;
cipher.KeySize = 128;
cipher.FeedbackSize = 8;
cipher.Key = key;
cipher.IV = key;
return cipher;
}
}
}
}

View file

@ -15,7 +15,7 @@ namespace MinecraftClient
{
#region Login to Minecraft.net and get a new session ID
public enum LoginResult { Error, Success, WrongPassword, Blocked, AccountMigrated, NotPremium };
public enum LoginResult { OtherError, SSLError, Success, WrongPassword, Blocked, AccountMigrated, NotPremium };
/// <summary>
/// Allows to login to a premium Minecraft account using the Yggdrasil authentication scheme.
@ -68,7 +68,11 @@ namespace MinecraftClient
}
else return LoginResult.Blocked;
}
else return LoginResult.Error;
else if (e.Status == WebExceptionStatus.SendFailure)
{
return LoginResult.SSLError;
}
else return LoginResult.OtherError;
}
}
@ -99,7 +103,7 @@ namespace MinecraftClient
#endregion
TcpClient c = new TcpClient();
Crypto.AesStream s;
Crypto.IAesStream s;
public bool HasBeenKicked { get { return connectionlost; } }
bool connectionlost = false;
@ -330,7 +334,7 @@ namespace MinecraftClient
public void setVersion(int ver) { protocolversion = ver; }
public void setClient(TcpClient n) { c = n; }
private void setEncryptedClient(Crypto.AesStream n) { s = n; encrypted = true; }
private void setEncryptedClient(Crypto.IAesStream n) { s = n; encrypted = true; }
private void Receive(byte[] buffer, int start, int offset, SocketFlags f)
{
if (encrypted)
@ -506,7 +510,10 @@ namespace MinecraftClient
Send(encryption_response_tosend);
//Start client-side encryption
setEncryptedClient(new Crypto.AesStream(c.GetStream(), secretKey));
Crypto.IAesStream encrypted;
if (Program.isUsingMono) { encrypted = new Crypto.MonoAesStream(c.GetStream(), secretKey); }
else encrypted = new Crypto.AesStream(c.GetStream(), secretKey);
setEncryptedClient(encrypted);
//Get the next packet
readNextVarInt(); //Skip Packet size (not needed)

View file

@ -6,7 +6,7 @@ using System.Text;
namespace MinecraftClient
{
/// <summary>
/// Minecraft Console Client by ORelio (c) 2012-2013.
/// Minecraft Console Client by ORelio (c) 2012-2014.
/// Allows to connect to any Minecraft server, send and receive text, automated scripts.
/// This source code is released under the CDDL 1.0 License.
/// </summary>
@ -279,7 +279,18 @@ namespace MinecraftClient
case MinecraftCom.LoginResult.Blocked: Console.WriteLine("Too many failed logins. Please try again later."); break;
case MinecraftCom.LoginResult.WrongPassword: Console.WriteLine("Incorrect password."); break;
case MinecraftCom.LoginResult.NotPremium: Console.WriteLine("User not premium."); break;
case MinecraftCom.LoginResult.Error: Console.WriteLine("Network error."); break;
case MinecraftCom.LoginResult.OtherError: Console.WriteLine("Network error."); break;
case MinecraftCom.LoginResult.SSLError: Console.WriteLine("SSL Error.");
if (isUsingMono)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("It appears that you are using Mono to run this program."
+ '\n' + "The first time, you have to import HTTPS certificates using:"
+ '\n' + "mozroots --import --ask-remove");
Console.ForegroundColor = ConsoleColor.Gray;
return;
}
break;
}
while (Console.KeyAvailable) { Console.ReadKey(false); }
if (Settings.SingleCommand == "") { ReadLineReconnect(); }
@ -320,6 +331,18 @@ namespace MinecraftClient
else return false;
}
/// <summary>
/// Detect if the user is running Minecraft Console Client through Mono
/// </summary>
public static bool isUsingMono
{
get
{
return Type.GetType("Mono.Runtime") != null;
}
}
/// <summary>
/// Private thread for restarting the program. Called through Restart()
/// </summary>