rc4 done also client restructure

woom oom om m
This commit is contained in:
Malloc of Kuzkycyziklistan 2017-05-30 12:44:27 -05:00
parent 88714ec13a
commit 8960453143
10 changed files with 53 additions and 16 deletions

View file

@ -34,14 +34,24 @@ class Connection {
private static onMessage(event: any): void {
var raw = new Uint8Array(event.data);
var msg = Packet.fromBytes(raw);
console.log(msg);
var msg: Packet;
try {
msg = !Cipher.ready ? Packet.fromBytes(raw)
: Packet.fromBytes(Cipher.parse(raw));
} catch(e) {
close();
return;
}
console.log(msg);
switch(msg.id) {
case kPacketId.KeyExchange:
var response = Key.generateResponsePacket(msg);
Connection.send(response);
console.log(response);
if(Key.succeeded) {
Cipher.init(Key.privateKey);
Connection.send(response);
} else
CriticalStop.redirect("Could not establish an encrypted connection with the server.");
break;
case kPacketId.LoginAttempt:
@ -54,6 +64,7 @@ class Connection {
private static onClose(event: any): void {
Connection._isOpen = false;
Cipher.close();
if(Connection.onCloseFunc)
Connection.onCloseFunc();

View file

@ -1,7 +1,7 @@
class Key {
private static secret: bigInt;
private static _privateKey: bigInt = new bigInt(0);
private static get privateKey(): bigInt {
public static get privateKey(): bigInt {
return Key._privateKey;
}
@ -17,9 +17,10 @@ class Key {
var generator = new bigInt(request[0].toString(), 16);
var modulus = new bigInt(request[1].toString(), 16);
var serverKey = new bigInt(request[2].toString(), 16);
var clientKey = generator.modPow(Key.secret, modulus);
Key._privateKey = serverKey.modPow(serverKey, modulus);
return Packet.create(kPacketId.KeyExchange, [generator.modPow(Key.secret, modulus).toString(16)]);
Key._privateKey = serverKey.modPow(Key.secret, modulus);
return Packet.create(kPacketId.KeyExchange, [clientKey.toString(16)]);
}
}
@ -27,12 +28,17 @@ class Cipher {
private static key: Uint8Array;
private static state: Uint8Array;
private static _ready: boolean = false;
public static get ready(): boolean {
return Cipher._ready;
}
public static init(key: bigInt) {
Cipher.key = key.toByteArray(512 / 8);
Cipher.state = new Uint8Array(256);
Cipher.state.map((value: number, index: number): number => {
return index;
});
for(var stateIndex = 0; stateIndex < Cipher.state.length; ++stateIndex)
Cipher.state[stateIndex] = stateIndex;
var i, j = 0, t;
for(i = 0; i < 256; ++i) {
@ -44,6 +50,7 @@ class Cipher {
}
Cipher.generateStream(1024);
Cipher._ready = true;
}
private static generateStream(length: number): Uint8Array {
@ -65,10 +72,17 @@ class Cipher {
}
public static parse(data: Uint8Array): Uint8Array {
if(!Cipher._ready)
return null;
var stream = Cipher.generateStream(data.length);
for(var i = 0; i < data.length; ++i)
data[i] = data[i] ^ stream[i];
return data;
}
public static close(): void {
Cipher._ready = false;
}
}

View file

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Numerics;
using Square;
using System.Globalization;
namespace CircleScape.Encryption {
class Key {
@ -31,12 +32,12 @@ namespace CircleScape.Encryption {
}
public BigInteger ParseResponsePacket(Packet packet) {
if(packet.Id == Packet.kId.KeyExchange && packet.RegionCount != 1)
if(packet.Id != Packet.kId.KeyExchange || packet.RegionCount != 1)
return -1;
if(!BigInteger.TryParse(packet[0], out BigInteger ClientKey))
if(!BigInteger.TryParse(packet[0], NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out BigInteger ClientKey))
return -1;
return (PrivateKey = BigInteger.ModPow(ClientKey, Secret, Modulus));
}
}

View file

@ -27,7 +27,7 @@ namespace Square {
=> Convert.ToBase64String(bytes);
public static string ToHexString(this byte[] bytes)
=> BitConverter.ToString(bytes).Replace("-", "");
=> BitConverter.ToString(bytes).Replace("-", " ");
public static string GetString(this byte[] bytes, bool isUtf8 = true)
=> isUtf8 ? Encoding.UTF8.GetString(bytes)

View file

@ -4,9 +4,17 @@ using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Kneesocks;
using CircleScape.Encryption;
namespace CircleScape {
class ActiveConnection : Kneesocks.Connection {
private Cipher Encryptor;
public void Initialize(PendingConnection conn) {
Initialize(conn, false);
Encryptor = conn.Encryptor;
}
}
}

View file

@ -12,7 +12,7 @@ namespace CircleScape {
class PendingConnection : Connection {
private DateTime ConnectionOpened;
private Key Key;
private Cipher Encryptor;
public Cipher Encryptor { get; private set; } = null;
protected override void OnOpen() {
ConnectionOpened = DateTime.UtcNow;
@ -28,7 +28,10 @@ namespace CircleScape {
}
protected override void OnReceive(byte[] data) {
var packet = Packet.FromBytes(data);
Packet packet =
Encryptor == null ? Packet.FromBytes(data)
: Packet.FromBytes(Encryptor.Parse(data));
if(!packet.IsLegal) {
Disconnect(Frame.kClosingReason.ProtocolError, "Packet received was not legal.");
return;