From 0ce0fe099db5d690d8b8a0606239a10b4ba65a3b Mon Sep 17 00:00:00 2001 From: Malloc of Kuzkycyziklistan Date: Fri, 26 May 2017 15:28:02 -0500 Subject: [PATCH] lotta stoof wew --- client/lib/check.js | 30 +++++++-- client/src/Connection.ts | 9 ++- client/src/Crypto.ts | 75 +++++++++++++++++++++- client/src/Entrypoint.ts | 4 +- client/src/Extensions.ts | 14 ++-- client/src/Packet.ts | 4 +- client/src/RenderContext.ts | 4 +- client/src/Utilities.ts | 12 ++++ client/src/def/BigInt.d.ts | 24 +++---- server/Encryption/KeyExchange.cs | 4 +- server/Libraries/Square/ArrayExtensions.cs | 2 +- server/Socks/Packet.cs | 11 +++- server/Socks/PendingConnection.cs | 4 +- 13 files changed, 160 insertions(+), 37 deletions(-) diff --git a/client/lib/check.js b/client/lib/check.js index fc57629..b7815bf 100644 --- a/client/lib/check.js +++ b/client/lib/check.js @@ -13,13 +13,33 @@ window.onload = function() { // check for webgl support var canvas = document.getElementById("cs"); - if(!(canvas.getContext("webgl") || canvas.getContext("experimental-webgl"))) - support.webgl = false; + Rendering.context = canvas.getContext("webgl"); + if(!Rendering.context) { + Rendering.context = canvas.getContext("experimental-webgl"); + if(!Rendering.context) + support.webgl = false; + } + + // check for animation frame support + window.requestAnimationFrame = window.requestAnimationFrame + || window.webkitRequestAnimationFrame + || window.mozRequestAnimationFrame + || window.oRequestAnimationFrame + || window.msRequestAnimationFrame; + if(!window.requestAnimationFrame) + support.anim = false; // check for indexedDB support - window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; - window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBransaction; - window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; + window.indexedDB = window.indexedDB + || window.mozIndexedDB + || window.webkitIndexedDB + || window.msIndexedDB; + window.IDBTransaction = window.IDBTransaction + || window.webkitIDBTransaction + || window.msIDBransaction; + window.IDBKeyRange = window.IDBKeyRange + || window.webkitIDBKeyRange + || window.msIDBKeyRange; if(!window.indexedDB) support.idb = false; diff --git a/client/src/Connection.ts b/client/src/Connection.ts index dbfb57f..41bdf6d 100644 --- a/client/src/Connection.ts +++ b/client/src/Connection.ts @@ -21,6 +21,10 @@ class Connection { Connection.sock.onclose = Connection.onClose; } + public static send(msg: Packet) { + Connection.sock.send(msg.getBytes()); + } + private static onOpen(event: any): void { Connection._isOpen = true; @@ -32,11 +36,12 @@ class Connection { var raw = new Uint8Array(event.data); var msg = Packet.fromBytes(raw); console.log(msg); - console.log(msg[1].toString()); switch(msg.id) { case kPacketId.KeyExchange: - + var response = Key.generateResponsePacket(msg); + Connection.send(response); + console.log(response); break; case kPacketId.LoginAttempt: diff --git a/client/src/Crypto.ts b/client/src/Crypto.ts index 6f132eb..b40d301 100644 --- a/client/src/Crypto.ts +++ b/client/src/Crypto.ts @@ -1,3 +1,74 @@ -class KeyExchange { - +class Key { + private static secret: bigInt; + private static _privateKey: bigInt = new bigInt(0); + private static get privateKey(): bigInt { + return Key._privateKey; + } + + public static get succeeded(): boolean { + return !Key._privateKey.eq(new bigInt(0)); + } + + public static init(): void { + Key.secret = Random.generatePrime(512); + } + + public static generateResponsePacket(request: Packet): Packet { + var generator = new bigInt(request[0].toString(), 16); + var modulus = new bigInt(request[1].toString(), 16); + var serverKey = new bigInt(request[2].toString(), 16); + + Key._privateKey = serverKey.modPow(Key.secret, modulus); + return Packet.create(kPacketId.KeyExchange, [generator.modPow(Key.secret, modulus).toString(16)]); + } +} + +class Cipher { + private static key: Uint8Array; + private static state: Uint8Array; + + 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; + }); + + var i, j = 0, t; + for(i = 0; i < 256; ++i) { + j = (j + Cipher.state[i] + Cipher.key[i % Cipher.key.length]) % 256; + + t = Cipher.state[i]; + Cipher.state[i] = Cipher.state[j]; + Cipher.state[j] = t; + } + + Cipher.generateStream(1024); + } + + private static generateStream(length: number): Uint8Array { + var stream = new Uint8Array(length); + var i = 0, j = 0, x, t; + + for(x = 0; x < length; ++x) { + i = (i + 1) % 256; + j = (j + Cipher.state[i]) % 256; + + t = Cipher.state[i]; + Cipher.state[i] = Cipher.state[j]; + Cipher.state[j] = t; + + stream[x] = Cipher.state[(Cipher.state[i] + Cipher.state[j]) % 256]; + } + + return stream; + } + + public static parse(data: Uint8Array): Uint8Array { + var stream = Cipher.generateStream(data.length); + for(var i = 0; i < data.length; ++i) + data[i] = data[i] ^ stream[i]; + + return data; + } } \ No newline at end of file diff --git a/client/src/Entrypoint.ts b/client/src/Entrypoint.ts index c92133e..6e17cf6 100644 --- a/client/src/Entrypoint.ts +++ b/client/src/Entrypoint.ts @@ -13,7 +13,7 @@ class Entrypoint { } public static start(): void { - Connection.open(); + Key.init(); FileCache.initCache( // SUCCESS @@ -27,6 +27,8 @@ class Entrypoint { CriticalStop.redirect(error); } ); + + Connection.open(); } private static ready(): void { diff --git a/client/src/Extensions.ts b/client/src/Extensions.ts index 5499fc6..6510c5f 100644 --- a/client/src/Extensions.ts +++ b/client/src/Extensions.ts @@ -221,15 +221,19 @@ Uint8Array.prototype.toHexString = function(): string { // ** BIGINT EXTENSIONS ** \\ interface bigInt { - toByteArray(): Uint8Array; + toByteArray(byteCount: number): Uint8Array; } -bigInt.prototype.toByteArray = function(): Uint8Array { +bigInt.prototype.toByteArray = function(byteCount: number): Uint8Array { var hexString: string = this.toString(16); - var byteCount = Math.ceil(hexString.length / 2); + var loopCount = Math.ceil(hexString.length / 2); var byteArray = new Uint8Array(byteCount); - for(var i = 0; i < byteCount; ++i) { - byteArray[i] = parseInt(hexString.substr(Math.max(0, hexString.length - 2*(i+1)), hexString.length - 2*i), 16); + loopCount = Math.min(loopCount, byteCount); + for(var i = 0; i < loopCount; ++i) { + var byte = hexString.substring(Math.max(0, hexString.length - 2*(i+1)), hexString.length - 2*i); + byteArray[i] = parseInt(byte, 16); } + + return byteArray; } \ No newline at end of file diff --git a/client/src/Packet.ts b/client/src/Packet.ts index 118280e..8148802 100644 --- a/client/src/Packet.ts +++ b/client/src/Packet.ts @@ -79,9 +79,9 @@ class Packet { bodySize += region.byteLength; ++headerSize; - if(region.byteLength >= 254 && region.byteLength <= 0xFFFF) + if(region.byteLength >= 0xFE && region.byteLength <= 0xFFFF) headerSize += 2; - else + else if(region.byteLength > 0xFFFF) headerSize += 4; }); diff --git a/client/src/RenderContext.ts b/client/src/RenderContext.ts index 2db7518..b236912 100644 --- a/client/src/RenderContext.ts +++ b/client/src/RenderContext.ts @@ -1,3 +1,5 @@ -class RenderContext { +class Rendering { + private static context: WebGLRenderingContext; + } \ No newline at end of file diff --git a/client/src/Utilities.ts b/client/src/Utilities.ts index 5de5ba6..163e25d 100644 --- a/client/src/Utilities.ts +++ b/client/src/Utilities.ts @@ -2,4 +2,16 @@ class CriticalStop { public static redirect(message: string): void { window.location.href = "error.html?txt="+ encodeURIComponent(message) +"&rterr"; } +} + +class Random { + public static generatePrime(bitCount: number = 512): bigInt { + var lower = new bigInt(2).pow(bitCount - 1); + var upper = new bigInt(2).pow(bitCount).prev(); + var prime = new bigInt(4); + while(!prime.isProbablePrime()) + prime = bigInt.randBetween(lower, upper); + + return prime; + } } \ No newline at end of file diff --git a/client/src/def/BigInt.d.ts b/client/src/def/BigInt.d.ts index 9c6c128..bf76056 100644 --- a/client/src/def/BigInt.d.ts +++ b/client/src/def/BigInt.d.ts @@ -24,13 +24,13 @@ declare class bigInt { public divide(x: any): bigInt; public divmod(x: any): bigInt; - public eq(x: any): bigInt; - public equals(x: any): bigInt; + public eq(x: any): boolean; + public equals(x: any): boolean; - public geq(x: any): bigInt; - public greater(x: any): bigInt; - public greaterOrEquals(x: any): bigInt; - public gt(x: any): bigInt; + public geq(x: any): boolean; + public greater(x: any): boolean; + public greaterOrEquals(x: any): boolean; + public gt(x: any): boolean; public isDivisibleBy(x: any): boolean; public isEven(): boolean; @@ -42,10 +42,10 @@ declare class bigInt { public isUnit(): boolean; public isZero(): boolean; - public leq(x: any): bigInt; - public lesser(x: any): bigInt; - public lesserOrEquals(x: any): bigInt; - public lt(x: any): bigInt; + public leq(x: any): boolean; + public lesser(x: any): boolean; + public lesserOrEquals(x: any): boolean; + public lt(x: any): boolean; public minus(x: any): bigInt; public mod(x: any): bigInt; @@ -53,10 +53,10 @@ declare class bigInt { public modPow(exp: any, mod: any): bigInt; public multiply(x: any): bigInt; - public neq(x: any): bigInt; + public neq(x: any): boolean; public next(): bigInt; public not(): bigInt; - public notEquals(x: any): bigInt; + public notEquals(x: any): boolean; public or(x: any): bigInt; public over(x: any): bigInt; diff --git a/server/Encryption/KeyExchange.cs b/server/Encryption/KeyExchange.cs index ff0beaa..f45c3db 100644 --- a/server/Encryption/KeyExchange.cs +++ b/server/Encryption/KeyExchange.cs @@ -7,7 +7,7 @@ using System.Numerics; using Square; namespace CircleScape.Encryption { - class KeyExchange { + class Key { private BigInteger Secret; public BigInteger Generator { get; private set; } = 2; public BigInteger Modulus { get; private set; } @@ -16,7 +16,7 @@ namespace CircleScape.Encryption { get => !PrivateKey.IsZero; } - public KeyExchange() { + public Key() { Secret = RNG.NextPrime(512 / 8); Modulus = RNG.NextPrime(512 / 8); } diff --git a/server/Libraries/Square/ArrayExtensions.cs b/server/Libraries/Square/ArrayExtensions.cs index 305e8a1..daeac73 100644 --- a/server/Libraries/Square/ArrayExtensions.cs +++ b/server/Libraries/Square/ArrayExtensions.cs @@ -12,7 +12,7 @@ namespace Square { if(offset > 0) arrEnum = arrEnum.Skip(offset); if(count > 0 && count < arr.Length) - arrEnum = arr.Take(count); + arrEnum = arrEnum.Take(count); return arrEnum.ToArray(); } diff --git a/server/Socks/Packet.cs b/server/Socks/Packet.cs index 4fd0795..a623293 100644 --- a/server/Socks/Packet.cs +++ b/server/Socks/Packet.cs @@ -22,13 +22,14 @@ namespace CircleScape { return ErrorPacket; Packet packet = new Packet(); - if(!Enum.IsDefined(typeof(kId), raw[0])) + if(!Enum.IsDefined(typeof(kId), (int)raw[0])) return ErrorPacket; packet.Id = (kId)raw[0]; var regionCount = raw[1]; var regionLengths = new List(); var headerPtr = 2; for(var i = 0; i < regionCount; ++i) { + regionLengths.Add(0); var first = raw[headerPtr]; if(first < 254) { regionLengths[i] = first; @@ -49,7 +50,7 @@ namespace CircleScape { return ErrorPacket; } - if(headerPtr + regionLengths.Sum(x => x) < raw.Length) + if(headerPtr + regionLengths.Sum(x => x) > raw.Length) return ErrorPacket; long bodyPtr = headerPtr; @@ -125,6 +126,9 @@ namespace CircleScape { } public static implicit operator byte[] (Region region) => region.Data; + public string Bytes { + get => this; + } public static implicit operator string(Region region) { try { @@ -133,6 +137,9 @@ namespace CircleScape { return Encoding.ASCII.GetString(region.Data); } } + public string Str { + get => this; + } } } } diff --git a/server/Socks/PendingConnection.cs b/server/Socks/PendingConnection.cs index a55bbb5..66da8be 100644 --- a/server/Socks/PendingConnection.cs +++ b/server/Socks/PendingConnection.cs @@ -11,14 +11,14 @@ using CircleScape.Encryption; namespace CircleScape { class PendingConnection : Connection { private DateTime ConnectionOpened; - private KeyExchange Key; + private Key Key; private Cipher Encryptor; protected override void OnOpen() { ConnectionOpened = DateTime.UtcNow; - Key = new KeyExchange(); + Key = new Key(); Send(Key.GenerateRequestPacket().GetBytes()); }