├── client ├── js-ws │ ├── flash-src │ │ ├── .gitignore │ │ ├── build.properties.sample │ │ ├── src │ │ │ └── net │ │ │ │ └── gimite │ │ │ │ └── websocket │ │ │ │ ├── IWebSocketLogger.as │ │ │ │ ├── WebSocketFrame.as │ │ │ │ ├── WebSocketMainInsecure.as │ │ │ │ ├── WebSocketEvent.as │ │ │ │ └── WebSocketMain.as │ │ ├── third-party │ │ │ └── com │ │ │ │ └── hurlant │ │ │ │ ├── math │ │ │ │ ├── bi_internal.as │ │ │ │ ├── IReduction.as │ │ │ │ ├── NullReduction.as │ │ │ │ ├── ClassicReduction.as │ │ │ │ ├── BarrettReduction.as │ │ │ │ └── MontgomeryReduction.as │ │ │ │ ├── crypto │ │ │ │ ├── symmetric │ │ │ │ │ ├── IMode.as │ │ │ │ │ ├── ICipher.as │ │ │ │ │ ├── IStreamCipher.as │ │ │ │ │ ├── NullPad.as │ │ │ │ │ ├── IPad.as │ │ │ │ │ ├── aeskey.pl │ │ │ │ │ ├── ISymmetricKey.as │ │ │ │ │ ├── PKCS5.as │ │ │ │ │ ├── OFBMode.as │ │ │ │ │ ├── SSLPad.as │ │ │ │ │ ├── TLSPad.as │ │ │ │ │ ├── CTRMode.as │ │ │ │ │ ├── CBCMode.as │ │ │ │ │ ├── SimpleIVMode.as │ │ │ │ │ ├── CFB8Mode.as │ │ │ │ │ ├── CFBMode.as │ │ │ │ │ ├── ECBMode.as │ │ │ │ │ ├── TripleDESKey.as │ │ │ │ │ ├── XTeaKey.as │ │ │ │ │ └── IVMode.as │ │ │ │ ├── tls │ │ │ │ │ ├── IConnectionState.as │ │ │ │ │ ├── KeyExchanges.as │ │ │ │ │ ├── SSLEvent.as │ │ │ │ │ ├── TLSSocketEvent.as │ │ │ │ │ ├── TLSEvent.as │ │ │ │ │ ├── MACs.as │ │ │ │ │ ├── ISecurityParameters.as │ │ │ │ │ ├── TLSError.as │ │ │ │ │ ├── TLSConfig.as │ │ │ │ │ ├── BulkCiphers.as │ │ │ │ │ ├── TLSConnectionState.as │ │ │ │ │ ├── SSLConnectionState.as │ │ │ │ │ ├── CipherSuites.as │ │ │ │ │ └── TLSTest.as │ │ │ │ ├── hash │ │ │ │ │ ├── IHash.as │ │ │ │ │ ├── IHMAC.as │ │ │ │ │ ├── SHA224.as │ │ │ │ │ ├── SHABase.as │ │ │ │ │ ├── HMAC.as │ │ │ │ │ ├── SHA1.as │ │ │ │ │ ├── SHA256.as │ │ │ │ │ ├── MAC.as │ │ │ │ │ └── MD2.as │ │ │ │ ├── prng │ │ │ │ │ ├── IPRNG.as │ │ │ │ │ ├── ARC4.as │ │ │ │ │ ├── Random.as │ │ │ │ │ └── TLSPRF.as │ │ │ │ ├── tests │ │ │ │ │ ├── ITestHarness.as │ │ │ │ │ ├── TestCase.as │ │ │ │ │ ├── BigIntegerTest.as │ │ │ │ │ ├── ARC4Test.as │ │ │ │ │ ├── MD2Test.as │ │ │ │ │ ├── TLSPRFTest.as │ │ │ │ │ ├── MD5Test.as │ │ │ │ │ ├── TripleDESKeyTest.as │ │ │ │ │ ├── SHA224Test.as │ │ │ │ │ ├── XTeaKeyTest.as │ │ │ │ │ ├── SHA256Test.as │ │ │ │ │ ├── CFB8ModeTest.as │ │ │ │ │ ├── DESKeyTest.as │ │ │ │ │ ├── RSAKeyTest.as │ │ │ │ │ ├── CFBModeTest.as │ │ │ │ │ ├── OFBModeTest.as │ │ │ │ │ ├── BlowFishKeyTest.as │ │ │ │ │ ├── CTRModeTest.as │ │ │ │ │ └── ECBModeTest.as │ │ │ │ └── cert │ │ │ │ │ └── X509CertificateCollection.as │ │ │ │ └── util │ │ │ │ ├── der │ │ │ │ ├── IAsn1Type.as │ │ │ │ ├── Set.as │ │ │ │ ├── ByteString.as │ │ │ │ ├── Integer.as │ │ │ │ ├── PrintableString.as │ │ │ │ ├── OID.as │ │ │ │ ├── UTCTime.as │ │ │ │ ├── Sequence.as │ │ │ │ ├── Type.as │ │ │ │ ├── ObjectIdentifier.as │ │ │ │ └── PEM.as │ │ │ │ ├── ArrayUtil.as │ │ │ │ ├── Memory.as │ │ │ │ └── Hex.as │ │ ├── build.sh │ │ └── build.xml │ └── WebSocketMain.swf └── index.html ├── server ├── config.ini ├── server.php └── WebSocket │ ├── Application │ ├── ExampleApplication.php │ └── Application.php │ └── Socket.php └── README.md /client/js-ws/flash-src/.gitignore: -------------------------------------------------------------------------------- 1 | build.properties 2 | -------------------------------------------------------------------------------- /client/js-ws/WebSocketMain.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GulDmitry/php-websocket-server/HEAD/client/js-ws/WebSocketMain.swf -------------------------------------------------------------------------------- /client/js-ws/flash-src/build.properties.sample: -------------------------------------------------------------------------------- 1 | # Point this to your Flex SDK directory. 2 | FLEX_HOME=/usr/local/share/flex_sdk_4 3 | -------------------------------------------------------------------------------- /server/config.ini: -------------------------------------------------------------------------------- 1 | ; server address 2 | address = 127.0.0.1 3 | ; if use Flash Socket Policy don't change port and exec script as root. 4 | ; or you can run stand alone script with Flash Socket Policy XML 5 | port = 843 6 | ; show connection log 7 | debug = 1 -------------------------------------------------------------------------------- /client/js-ws/flash-src/src/net/gimite/websocket/IWebSocketLogger.as: -------------------------------------------------------------------------------- 1 | // Copyright: Hiroshi Ichikawa 2 | // License: New BSD License 3 | 4 | package net.gimite.websocket { 5 | 6 | public interface IWebSocketLogger { 7 | function log(message:String):void; 8 | function error(message:String):void; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/math/bi_internal.as: -------------------------------------------------------------------------------- 1 | /** 2 | * bi_internal 3 | * 4 | * A namespace. w00t. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.math { 10 | public namespace bi_internal = "http://crypto.hurlant.com/BigInteger"; 11 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/math/IReduction.as: -------------------------------------------------------------------------------- 1 | package com.hurlant.math 2 | { 3 | internal interface IReduction 4 | { 5 | function convert(x:BigInteger):BigInteger; 6 | function revert(x:BigInteger):BigInteger; 7 | function reduce(x:BigInteger):void; 8 | function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void; 9 | function sqrTo(x:BigInteger, r:BigInteger):void; 10 | } 11 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/IMode.as: -------------------------------------------------------------------------------- 1 | /** 2 | * IMode 3 | * 4 | * An interface for confidentiality modes to implement 5 | * This could become deprecated at some point. 6 | * Copyright (c) 2007 Henri Torgemane 7 | * 8 | * See LICENSE.txt for full license information. 9 | */ 10 | package com.hurlant.crypto.symmetric 11 | { 12 | public interface IMode extends ICipher 13 | { 14 | } 15 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tls/IConnectionState.as: -------------------------------------------------------------------------------- 1 | /** 2 | * IConnectionState 3 | * 4 | * Interface for TLS/SSL Connection states. 5 | * 6 | * See LICENSE.txt for full license information. 7 | */ 8 | package com.hurlant.crypto.tls { 9 | import flash.utils.ByteArray; 10 | public interface IConnectionState { 11 | function decrypt(type:uint, length:uint, p:ByteArray) : ByteArray; 12 | function encrypt(type:uint, p:ByteArray) : ByteArray; 13 | } 14 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/der/IAsn1Type.as: -------------------------------------------------------------------------------- 1 | /** 2 | * IAsn1Type 3 | * 4 | * An interface for Asn-1 types. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util.der 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public interface IAsn1Type 14 | { 15 | function getType():uint; 16 | function getLength():uint; 17 | 18 | function toDER():ByteArray; 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/hash/IHash.as: -------------------------------------------------------------------------------- 1 | /** 2 | * IHash 3 | * 4 | * An interface for each hash function to implement 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.hash 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public interface IHash 14 | { 15 | function getInputSize():uint; 16 | function getHashSize():uint; 17 | function hash(src:ByteArray):ByteArray; 18 | function toString():String; 19 | function getPadSize():int; 20 | } 21 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/src/net/gimite/websocket/WebSocketFrame.as: -------------------------------------------------------------------------------- 1 | // Copyright: Hiroshi Ichikawa 2 | // License: New BSD License 3 | 4 | package net.gimite.websocket { 5 | 6 | import flash.utils.ByteArray; 7 | 8 | public class WebSocketFrame { 9 | 10 | public var fin:Boolean = true; 11 | public var rsv:int = 0; 12 | public var opcode:int = -1; 13 | public var payload:ByteArray; 14 | 15 | // Fields below are not used when used as a parameter of sendFrame(). 16 | public var length:uint = 0; 17 | public var mask:Boolean = false; 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/prng/IPRNG.as: -------------------------------------------------------------------------------- 1 | /** 2 | * IPRNG 3 | * 4 | * An interface for classes that can be used a pseudo-random number generators 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.prng 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public interface IPRNG { 14 | function getPoolSize():uint; 15 | function init(key:ByteArray):void; 16 | function next():uint; 17 | function dispose():void; 18 | function toString():String; 19 | } 20 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/ICipher.as: -------------------------------------------------------------------------------- 1 | /** 2 | * ICipher 3 | * 4 | * A generic interface to use symmetric ciphers 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public interface ICipher 14 | { 15 | function getBlockSize():uint; 16 | function encrypt(src:ByteArray):void; 17 | function decrypt(src:ByteArray):void; 18 | function dispose():void; 19 | function toString():String; 20 | } 21 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/ITestHarness.as: -------------------------------------------------------------------------------- 1 | /** 2 | * ITestHarness 3 | * 4 | * An interface to specify what's available for test cases to use. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | public interface ITestHarness 12 | { 13 | function beginTestCase(name:String):void; 14 | function endTestCase():void; 15 | 16 | function beginTest(name:String):void; 17 | function passTest():void; 18 | function failTest(msg:String):void; 19 | } 20 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/IStreamCipher.as: -------------------------------------------------------------------------------- 1 | /** 2 | * IStreamCipher 3 | * 4 | * A "marker" interface for stream ciphers. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric { 10 | 11 | /** 12 | * A marker to indicate how this cipher works. 13 | * A stream cipher: 14 | * - does not use initialization vector 15 | * - keeps some internal state between calls to encrypt() and decrypt() 16 | * 17 | */ 18 | public interface IStreamCipher extends ICipher { 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/src/net/gimite/websocket/WebSocketMainInsecure.as: -------------------------------------------------------------------------------- 1 | // Copyright: Hiroshi Ichikawa 2 | // License: New BSD License 3 | 4 | package net.gimite.websocket { 5 | 6 | import flash.system.Security; 7 | 8 | public class WebSocketMainInsecure extends WebSocketMain { 9 | 10 | public function WebSocketMainInsecure() { 11 | Security.allowDomain("*"); 12 | // Also allows HTTP -> HTTPS call. Since we have already allowed arbitrary domains, allowing 13 | // HTTP -> HTTPS would not be more dangerous. 14 | Security.allowInsecureDomain("*"); 15 | super(); 16 | } 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /server/server.php: -------------------------------------------------------------------------------- 1 | registerApplication('example', WA\ExampleApplication::getInstance()); 18 | // show Connection log (connect, disconnect, data receive...) 19 | $server->setDebug($config['debug']); 20 | $server->run(); -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/ArrayUtil.as: -------------------------------------------------------------------------------- 1 | /** 2 | * ArrayUtil 3 | * 4 | * A class that allows to compare two ByteArrays. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util { 10 | import flash.utils.ByteArray; 11 | 12 | 13 | public class ArrayUtil { 14 | 15 | public static function equals(a1:ByteArray, a2:ByteArray):Boolean { 16 | if (a1.length != a2.length) return false; 17 | var l:int = a1.length; 18 | for (var i:int=0;i=0) { 16 | return x.mod(m); 17 | } 18 | return x; 19 | } 20 | public function revert(x:BigInteger):BigInteger { 21 | return x; 22 | } 23 | public function reduce(x:BigInteger):void { 24 | x.divRemTo(m, null,x); 25 | } 26 | public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void { 27 | x.multiplyTo(y,r); 28 | reduce(r); 29 | } 30 | public function sqrTo(x:BigInteger, r:BigInteger):void { 31 | x.squareTo(r); 32 | reduce(r); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/TestCase.as: -------------------------------------------------------------------------------- 1 | /** 2 | * TestCase 3 | * 4 | * Embryonic unit test support class. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | public class TestCase 12 | { 13 | public var harness:ITestHarness; 14 | 15 | public function TestCase(h:ITestHarness, title:String) { 16 | harness = h; 17 | harness.beginTestCase(title); 18 | } 19 | 20 | 21 | public function assert(msg:String, value:Boolean):void { 22 | if (value) { 23 | // TestHarness.print("+ ",msg); 24 | return; 25 | } 26 | throw new Error("Test Failure:"+msg); 27 | } 28 | 29 | public function runTest(f:Function, title:String):void { 30 | harness.beginTest(title); 31 | try { 32 | f(); 33 | } catch (e:Error) { 34 | trace("EXCEPTION THROWN: "+e); 35 | trace(e.getStackTrace()); 36 | harness.failTest(e.toString()); 37 | return; 38 | } 39 | harness.passTest(); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tls/MACs.as: -------------------------------------------------------------------------------- 1 | /** 2 | * MACs 3 | * 4 | * An enumeration of MACs implemented for TLS 1.0/SSL 3.0 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tls { 10 | import com.hurlant.crypto.Crypto; 11 | import com.hurlant.crypto.hash.HMAC; 12 | import com.hurlant.crypto.hash.MAC; 13 | 14 | public class MACs { 15 | public static const NULL:uint = 0; 16 | public static const MD5:uint = 1; 17 | public static const SHA1:uint = 2; 18 | 19 | public static function getHashSize(hash:uint):uint { 20 | return [0,16,20][hash]; 21 | } 22 | 23 | public static function getPadSize(hash:uint):int { 24 | return [0, 48, 40][hash]; 25 | } 26 | 27 | public static function getHMAC(hash:uint):HMAC { 28 | if (hash==NULL) return null; 29 | return Crypto.getHMAC(['',"md5","sha1"][hash]); 30 | } 31 | 32 | public static function getMAC(hash:uint):MAC { 33 | return Crypto.getMAC(['', "md5", "sha1"][hash]); 34 | } 35 | 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/der/ByteString.as: -------------------------------------------------------------------------------- 1 | /** 2 | * ByteString 3 | * 4 | * An ASN1 type for a ByteString, represented with a ByteArray 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util.der 10 | { 11 | import flash.utils.ByteArray; 12 | import com.hurlant.util.Hex; 13 | 14 | public class ByteString extends ByteArray implements IAsn1Type 15 | { 16 | private var type:uint; 17 | private var len:uint; 18 | 19 | public function ByteString(type:uint = 0x04, length:uint = 0x00) { 20 | this.type = type; 21 | this.len = length; 22 | } 23 | 24 | public function getLength():uint 25 | { 26 | return len; 27 | } 28 | 29 | public function getType():uint 30 | { 31 | return type; 32 | } 33 | 34 | public function toDER():ByteArray { 35 | return DER.wrapDER(type, this); 36 | } 37 | 38 | override public function toString():String { 39 | return DER.indent+"ByteString["+type+"]["+len+"]["+Hex.fromArray(this)+"]"; 40 | } 41 | 42 | } 43 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/der/Integer.as: -------------------------------------------------------------------------------- 1 | /** 2 | * Integer 3 | * 4 | * An ASN1 type for an Integer, represented with a BigInteger 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util.der 10 | { 11 | import com.hurlant.math.BigInteger; 12 | import flash.utils.ByteArray; 13 | 14 | public class Integer extends BigInteger implements IAsn1Type 15 | { 16 | private var type:uint; 17 | private var len:uint; 18 | 19 | public function Integer(type:uint, length:uint, b:ByteArray) { 20 | this.type = type; 21 | this.len = length; 22 | super(b); 23 | } 24 | 25 | public function getLength():uint 26 | { 27 | return len; 28 | } 29 | 30 | public function getType():uint 31 | { 32 | return type; 33 | } 34 | 35 | override public function toString(radix:Number=0):String { 36 | return DER.indent+"Integer["+type+"]["+len+"]["+super.toString(16)+"]"; 37 | } 38 | 39 | public function toDER():ByteArray { 40 | return null; 41 | } 42 | 43 | } 44 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/ISymmetricKey.as: -------------------------------------------------------------------------------- 1 | /** 2 | * ISymmetricKey 3 | * 4 | * An interface for symmetric encryption keys to implement. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public interface ISymmetricKey 14 | { 15 | /** 16 | * Returns the block size used by this particular encryption algorithm 17 | */ 18 | function getBlockSize():uint; 19 | /** 20 | * Encrypt one block of data in "block", starting at "index", of length "getBlockSize()" 21 | */ 22 | function encrypt(block:ByteArray, index:uint=0):void; 23 | /** 24 | * Decrypt one block of data in "block", starting at "index", of length "getBlockSize()" 25 | */ 26 | function decrypt(block:ByteArray, index:uint=0):void; 27 | /** 28 | * Attempts to destroy sensitive information from memory, such as encryption keys. 29 | * Note: This is not guaranteed to work given the Flash sandbox model. 30 | */ 31 | function dispose():void; 32 | 33 | function toString():String; 34 | } 35 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/BigIntegerTest.as: -------------------------------------------------------------------------------- 1 | /** 2 | * BigIntegerTest 3 | * 4 | * A test class for BigInteger 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | import com.hurlant.math.BigInteger; 12 | import com.hurlant.util.Hex; 13 | 14 | public class BigIntegerTest extends TestCase 15 | { 16 | public function BigIntegerTest(h:ITestHarness) 17 | { 18 | super(h, "BigInteger Tests"); 19 | runTest(testAdd, "BigInteger Addition"); 20 | h.endTestCase(); 21 | } 22 | 23 | public function testAdd():void { 24 | var n1:BigInteger = BigInteger.nbv(25); 25 | var n2:BigInteger = BigInteger.nbv(1002); 26 | var n3:BigInteger = n1.add(n2); 27 | var v:int = n3.valueOf(); 28 | assert("25+1002 = "+v, 25+1002==v); 29 | 30 | var p:BigInteger = new BigInteger(Hex.toArray("e564d8b801a61f47")); 31 | var xp:BigInteger = new BigInteger(Hex.toArray("99246db2a3507fa")); 32 | 33 | xp = xp.add(p); 34 | 35 | assert("xp==eef71f932bdb2741", xp.toString(16)=="eef71f932bdb2741"); 36 | } 37 | 38 | } 39 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/der/PrintableString.as: -------------------------------------------------------------------------------- 1 | /** 2 | * PrintableString 3 | * 4 | * An ASN1 type for a PrintableString, held within a String 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util.der 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public class PrintableString implements IAsn1Type 14 | { 15 | protected var type:uint; 16 | protected var len:uint; 17 | protected var str:String; 18 | 19 | public function PrintableString(type:uint, length:uint) { 20 | this.type = type; 21 | this.len = length; 22 | } 23 | 24 | public function getLength():uint 25 | { 26 | return len; 27 | } 28 | 29 | public function getType():uint 30 | { 31 | return type; 32 | } 33 | 34 | public function setString(s:String):void { 35 | str = s; 36 | } 37 | public function getString():String { 38 | return str; 39 | } 40 | 41 | public function toString():String { 42 | return DER.indent+str; 43 | } 44 | 45 | public function toDER():ByteArray { 46 | return null; // XXX not implemented 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tls/ISecurityParameters.as: -------------------------------------------------------------------------------- 1 | /** 2 | * ISecurityParameters 3 | * 4 | * This class encapsulates all the security parameters that get negotiated 5 | * during the TLS handshake. It also holds all the key derivation methods. 6 | * Copyright (c) 2007 Henri Torgemane 7 | * 8 | * See LICENSE.txt for full license information. 9 | */ 10 | package com.hurlant.crypto.tls { 11 | import flash.utils.ByteArray; 12 | 13 | public interface ISecurityParameters { 14 | function get version() : uint; 15 | function reset():void; 16 | function getBulkCipher():uint; 17 | function getCipherType():uint; 18 | function getMacAlgorithm():uint; 19 | function setCipher(cipher:uint):void; 20 | function setCompression(algo:uint):void; 21 | function setPreMasterSecret(secret:ByteArray):void; 22 | function setClientRandom(secret:ByteArray):void; 23 | function setServerRandom(secret:ByteArray):void; 24 | function get useRSA():Boolean; 25 | function computeVerifyData(side:uint, handshakeMessages:ByteArray):ByteArray; 26 | function computeCertificateVerify( side:uint, handshakeRecords:ByteArray):ByteArray; 27 | function getConnectionStates():Object; 28 | } 29 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/PKCS5.as: -------------------------------------------------------------------------------- 1 | /** 2 | * PKCS5 3 | * 4 | * A padding implementation of PKCS5. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public class PKCS5 implements IPad 14 | { 15 | private var blockSize:uint; 16 | 17 | public function PKCS5(blockSize:uint=0) { 18 | this.blockSize = blockSize; 19 | } 20 | 21 | public function pad(a:ByteArray):void { 22 | var c:uint = blockSize-a.length%blockSize; 23 | for (var i:uint=0;i0;i--) { 32 | var v:uint = a[a.length-1]; 33 | a.length--; 34 | if (c!=v) throw new Error("PKCS#5:unpad: Invalid padding value. expected ["+c+"], found ["+v+"]"); 35 | } 36 | // that is all. 37 | } 38 | 39 | public function setBlockSize(bs:uint):void { 40 | blockSize = bs; 41 | } 42 | 43 | } 44 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/src/net/gimite/websocket/WebSocketEvent.as: -------------------------------------------------------------------------------- 1 | package net.gimite.websocket { 2 | 3 | import flash.events.Event; 4 | 5 | /** 6 | * This class represents a generic websocket event. It contains the standard "type" 7 | * parameter as well as a "message" parameter. 8 | */ 9 | public class WebSocketEvent extends Event { 10 | 11 | public static const OPEN:String = "open"; 12 | public static const CLOSE:String = "close"; 13 | public static const MESSAGE:String = "message"; 14 | public static const ERROR:String = "error"; 15 | 16 | public var message:String; 17 | public var wasClean:Boolean; 18 | public var code:int; 19 | public var reason:String; 20 | 21 | public function WebSocketEvent( 22 | type:String, message:String = null, bubbles:Boolean = false, cancelable:Boolean = false) { 23 | super(type, bubbles, cancelable); 24 | this.message = message; 25 | } 26 | 27 | public override function clone():Event { 28 | var event:WebSocketEvent = new WebSocketEvent( 29 | this.type, this.message, this.bubbles, this.cancelable); 30 | event.wasClean = wasClean; 31 | event.code = code; 32 | event.reason = reason; 33 | return event; 34 | } 35 | 36 | public override function toString():String { 37 | return "WebSocketEvent: " + this.type + ": " + this.message; 38 | } 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/OFBMode.as: -------------------------------------------------------------------------------- 1 | /** 2 | * OFBMode 3 | * 4 | * An ActionScript 3 implementation of the OFB confidentiality mode 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public class OFBMode extends IVMode implements IMode 14 | { 15 | public function OFBMode(key:ISymmetricKey, padding:IPad=null) 16 | { 17 | super(key, null); 18 | } 19 | 20 | public function encrypt(src:ByteArray):void 21 | { 22 | var vector:ByteArray = getIV4e(); 23 | core(src, vector); 24 | } 25 | 26 | public function decrypt(src:ByteArray):void 27 | { 28 | var vector:ByteArray = getIV4d(); 29 | core(src, vector); 30 | } 31 | 32 | private function core(src:ByteArray, iv:ByteArray):void { 33 | var l:uint = src.length; 34 | var tmp:ByteArray = new ByteArray; 35 | for (var i:uint=0;i0;i--) { 32 | var v:uint = a[a.length-1]; 33 | a.length--; 34 | // But LOOK! SSL 3.0 doesn't care about this, bytes are arbitrary! 35 | // if (c!=v) throw new TLSError("SSLPad:unpad: Invalid padding value. expected ["+c+"], found ["+v+"]", TLSError.bad_record_mac); 36 | } 37 | a.length--; 38 | 39 | } 40 | public function setBlockSize(bs:uint):void { 41 | blockSize = bs; 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/TLSPad.as: -------------------------------------------------------------------------------- 1 | /** 2 | * TLSPad 3 | * 4 | * A padding implementation used by TLS 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric { 10 | import flash.utils.ByteArray; 11 | import com.hurlant.util.Hex; 12 | import com.hurlant.crypto.tls.TLSError; 13 | 14 | public class TLSPad implements IPad { 15 | private var blockSize:uint; 16 | 17 | public function TLSPad(blockSize:uint=0) { 18 | this.blockSize = blockSize; 19 | } 20 | public function pad(a:ByteArray):void { 21 | var c:uint = blockSize - (a.length+1)%blockSize; 22 | for (var i:uint=0;i<=c;i++) { 23 | a[a.length] = c; 24 | } 25 | } 26 | public function unpad(a:ByteArray):void { 27 | var c:uint = a.length%blockSize; 28 | if (c!=0) throw new TLSError("TLSPad::unpad: ByteArray.length isn't a multiple of the blockSize", TLSError.bad_record_mac); 29 | c = a[a.length-1]; 30 | for (var i:uint=c;i>0;i--) { 31 | var v:uint = a[a.length-1]; 32 | a.length--; 33 | if (c!=v) throw new TLSError("TLSPad:unpad: Invalid padding value. expected ["+c+"], found ["+v+"]", TLSError.bad_record_mac); 34 | } 35 | a.length--; 36 | // mostly ripped off from PKCS5.as, but with subtle differences 37 | } 38 | public function setBlockSize(bs:uint):void { 39 | blockSize = bs; 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/der/OID.as: -------------------------------------------------------------------------------- 1 | /** 2 | * OID 3 | * 4 | * A list of various ObjectIdentifiers. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util.der 10 | { 11 | public class OID 12 | { 13 | 14 | public static const RSA_ENCRYPTION:String = "1.2.840.113549.1.1.1"; 15 | public static const MD2_WITH_RSA_ENCRYPTION:String = "1.2.840.113549.1.1.2"; 16 | public static const MD5_WITH_RSA_ENCRYPTION:String = "1.2.840.113549.1.1.4"; 17 | public static const SHA1_WITH_RSA_ENCRYPTION:String = "1.2.840.113549.1.1.5"; 18 | public static const MD2_ALGORITHM:String = "1.2.840.113549.2.2"; 19 | public static const MD5_ALGORITHM:String = "1.2.840.113549.2.5"; 20 | public static const DSA:String = "1.2.840.10040.4.1"; 21 | public static const DSA_WITH_SHA1:String = "1.2.840.10040.4.3"; 22 | public static const DH_PUBLIC_NUMBER:String = "1.2.840.10046.2.1"; 23 | public static const SHA1_ALGORITHM:String = "1.3.14.3.2.26"; 24 | 25 | public static const COMMON_NAME:String = "2.5.4.3"; 26 | public static const SURNAME:String = "2.5.4.4"; 27 | public static const COUNTRY_NAME:String = "2.5.4.6"; 28 | public static const LOCALITY_NAME:String = "2.5.4.7"; 29 | public static const STATE_NAME:String = "2.5.4.8"; 30 | public static const ORGANIZATION_NAME:String = "2.5.4.10"; 31 | public static const ORG_UNIT_NAME:String = "2.5.4.11"; 32 | public static const TITLE:String = "2.5.4.12"; 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/CTRMode.as: -------------------------------------------------------------------------------- 1 | /** 2 | * CTRMode 3 | * 4 | * An ActionScript 3 implementation of the counter confidentiality mode 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public class CTRMode extends IVMode implements IMode 14 | { 15 | 16 | public function CTRMode(key:ISymmetricKey, padding:IPad = null) { 17 | super(key, padding); 18 | } 19 | 20 | public function encrypt(src:ByteArray):void 21 | { 22 | padding.pad(src); 23 | var vector:ByteArray = getIV4e(); 24 | core(src, vector); 25 | } 26 | 27 | public function decrypt(src:ByteArray):void 28 | { 29 | var vector:ByteArray = getIV4d(); 30 | core(src, vector); 31 | padding.unpad(src); 32 | } 33 | 34 | private function core(src:ByteArray, iv:ByteArray):void { 35 | var X:ByteArray = new ByteArray; 36 | var Xenc:ByteArray = new ByteArray; 37 | X.writeBytes(iv); 38 | for (var i:uint=0;i=0;--j) { 47 | X[j]++; 48 | if (X[j]!=0) 49 | break; 50 | } 51 | } 52 | } 53 | public function toString():String { 54 | return key.toString()+"-ctr"; 55 | } 56 | 57 | } 58 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/der/UTCTime.as: -------------------------------------------------------------------------------- 1 | /** 2 | * UTCTime 3 | * 4 | * An ASN1 type for UTCTime, represented as a Date 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util.der 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public class UTCTime implements IAsn1Type 14 | { 15 | protected var type:uint; 16 | protected var len:uint; 17 | public var date:Date; 18 | 19 | public function UTCTime(type:uint, len:uint) 20 | { 21 | this.type = type; 22 | this.len = len; 23 | } 24 | 25 | public function getLength():uint 26 | { 27 | return len; 28 | } 29 | 30 | public function getType():uint 31 | { 32 | return type; 33 | } 34 | 35 | public function setUTCTime(str:String):void { 36 | 37 | var year:uint = parseInt(str.substr(0, 2)); 38 | if (year<50) { 39 | year+=2000; 40 | } else { 41 | year+=1900; 42 | } 43 | var month:uint = parseInt(str.substr(2,2)); 44 | var day:uint = parseInt(str.substr(4,2)); 45 | var hour:uint = parseInt(str.substr(6,2)); 46 | var minute:uint = parseInt(str.substr(8,2)); 47 | // XXX this could be off by up to a day. parse the rest. someday. 48 | date = new Date(year, month-1, day, hour, minute); 49 | } 50 | 51 | 52 | public function toString():String { 53 | return DER.indent+"UTCTime["+type+"]["+len+"]["+date+"]"; 54 | } 55 | 56 | public function toDER():ByteArray { 57 | return null // XXX not implemented 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/CBCMode.as: -------------------------------------------------------------------------------- 1 | /** 2 | * CBCMode 3 | * 4 | * An ActionScript 3 implementation of the CBC confidentiality mode 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | /** 14 | * CBC confidentiality mode. why not. 15 | */ 16 | public class CBCMode extends IVMode implements IMode 17 | { 18 | 19 | public function CBCMode(key:ISymmetricKey, padding:IPad = null) { 20 | super(key, padding); 21 | } 22 | 23 | public function encrypt(src:ByteArray):void { 24 | padding.pad(src); 25 | var vector:ByteArray = getIV4e(); 26 | for (var i:uint=0;ibroadcastServer('Join!'); 17 | // } 18 | // 19 | // public function onDisconnect($client) 20 | // { 21 | // $client->broadcastServer('Bye-bye!'); 22 | // 23 | // parent::onDisconnect($client); 24 | // } 25 | // 26 | // public function onTick() 27 | // { 28 | // $this->sendApp('Approximately every second second within the application.'); 29 | // } 30 | 31 | public function onData($jData, $client) 32 | { 33 | $data = json_decode($jData); 34 | 35 | switch (isset($data->type) ? $data->type : null) { 36 | 37 | // add to group or disconnect 38 | case 'join': 39 | 40 | isset($data->group_id) ? 41 | $client->setGroup($data->group_id) : 42 | $client->onDisconnect(); 43 | return; 44 | break; 45 | 46 | case 'close': 47 | $client->onDisconnect(); 48 | return; 49 | break; 50 | 51 | default: 52 | break; 53 | } 54 | 55 | // to current socket 56 | // $client->send($data->comment); 57 | // 58 | // to all server sockets 59 | $client->sendServer($data->comment); 60 | 61 | // to all server sockets exclude sender 62 | // $client->broadcastServer($data->comment); 63 | // 64 | // Group 65 | // 66 | // $client->sendGroup($data->comment); 67 | // $client->broadcastGroup($data->comment); 68 | // $client->getConnectionsByGroupKey(1); 69 | // 70 | // Application 71 | // 72 | // $this->sendApp($data->comment); 73 | // $this->broadcastApp($client, $data->comment); 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/math/BarrettReduction.as: -------------------------------------------------------------------------------- 1 | package com.hurlant.math 2 | { 3 | use namespace bi_internal; 4 | 5 | internal class BarrettReduction implements IReduction 6 | { 7 | private var m:BigInteger; 8 | private var r2:BigInteger; 9 | private var q3:BigInteger; 10 | private var mu:BigInteger; 11 | 12 | public function BarrettReduction(m:BigInteger) { 13 | // setup Barrett 14 | r2 = new BigInteger; 15 | q3 = new BigInteger; 16 | BigInteger.ONE.dlShiftTo(2*m.t, r2); 17 | mu = r2.divide(m); 18 | this.m = m; 19 | } 20 | 21 | public function revert(x:BigInteger):BigInteger 22 | { 23 | return x; 24 | } 25 | 26 | /** 27 | * 28 | * @param x 29 | * @param y 30 | * @param r = x*y mod m; x != r 31 | * 32 | */ 33 | public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void 34 | { 35 | x.multiplyTo(y, r); 36 | reduce(r); 37 | } 38 | 39 | /** 40 | * 41 | * @param x 42 | * @param r = x^2 mod m; x != r 43 | * 44 | */ 45 | public function sqrTo(x:BigInteger, r:BigInteger):void 46 | { 47 | x.squareTo(r); 48 | reduce(r); 49 | } 50 | 51 | public function convert(x:BigInteger):BigInteger 52 | { 53 | if (x.s<0 || x.t>2*m.t) { 54 | return x.mod(m); 55 | } else if (x.compareTo(m)<0) { 56 | return x; 57 | } else { 58 | var r:BigInteger = new BigInteger; 59 | x.copyTo(r); 60 | reduce(r); 61 | return r; 62 | } 63 | } 64 | 65 | /** 66 | * 67 | * @param x = x mod m (HAC 14.42) 68 | * 69 | */ 70 | public function reduce(lx:BigInteger):void 71 | { 72 | var x:BigInteger = lx as BigInteger; 73 | x.drShiftTo(m.t-1,r2); 74 | if (x.t>m.t+1) { 75 | x.t = m.t+1; 76 | x.clamp(); 77 | } 78 | mu.multiplyUpperTo(r2, m.t+1, q3); 79 | m.multiplyLowerTo(q3, m.t+1, r2); 80 | while (x.compareTo(r2)<0) { 81 | x.dAddOffset(1, m.t+1); 82 | } 83 | x.subTo(r2,x); 84 | while (x.compareTo(m)>=0) { 85 | x.subTo(m,x); 86 | } 87 | } 88 | 89 | } 90 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/XTeaKeyTest.as: -------------------------------------------------------------------------------- 1 | /** 2 | * XTeaKeyTest 3 | * 4 | * A test class for XTeaKey 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | import com.hurlant.crypto.prng.Random; 12 | import com.hurlant.crypto.symmetric.ECBMode; 13 | import com.hurlant.crypto.symmetric.XTeaKey; 14 | import com.hurlant.util.Hex; 15 | 16 | import flash.utils.ByteArray; 17 | import flash.utils.getTimer; 18 | 19 | public class XTeaKeyTest extends TestCase 20 | { 21 | public function XTeaKeyTest(h:ITestHarness) { 22 | super(h, "XTeaKey Test"); 23 | runTest(testGetBlockSize, "XTea Block Size"); 24 | runTest(testVectors, "XTea Test Vectors"); 25 | 26 | h.endTestCase(); 27 | } 28 | 29 | public function testGetBlockSize():void { 30 | var tea:XTeaKey = new XTeaKey(Hex.toArray("deadbabecafebeefdeadbabecafebeef")); 31 | assert("tea blocksize", tea.getBlockSize()==8); 32 | } 33 | 34 | public function testVectors():void { 35 | // blah. 36 | // can't find working test vectors. 37 | // algorithms should not get published without vectors :( 38 | var keys:Array=[ 39 | "00000000000000000000000000000000", 40 | "2b02056806144976775d0e266c287843"]; 41 | var pts:Array=[ 42 | "0000000000000000", 43 | "74657374206d652e"]; 44 | var cts:Array=[ 45 | "2dc7e8d3695b0538", 46 | "7909582138198783"]; 47 | // self-fullfilling vectors. 48 | // oh well, at least I can decrypt what I produce. :( 49 | 50 | for (var i:uint=0;i>15; 20 | um = (1<<(BigInteger.DB-15))-1; 21 | mt2 = 2*m.t; 22 | } 23 | /** 24 | * xR mod m 25 | */ 26 | public function convert(x:BigInteger):BigInteger { 27 | var r:BigInteger = new BigInteger; 28 | x.abs().dlShiftTo(m.t, r); 29 | r.divRemTo(m, null, r); 30 | if (x.s<0 && r.compareTo(BigInteger.ZERO)>0) { 31 | m.subTo(r,r); 32 | } 33 | return r; 34 | } 35 | /** 36 | * x/R mod m 37 | */ 38 | public function revert(x:BigInteger):BigInteger { 39 | var r:BigInteger = new BigInteger; 40 | x.copyTo(r); 41 | reduce(r); 42 | return r; 43 | } 44 | /** 45 | * x = x/R mod m (HAC 14.32) 46 | */ 47 | public function reduce(x:BigInteger):void { 48 | while (x.t<=mt2) { // pad x so am has enough room later 49 | x.a[x.t++] = 0; 50 | } 51 | for (var i:int=0; i>15)*mpl)&um)<<15))&BigInteger.DM; 55 | // use am to combine the multiply-shift-add into one call 56 | j = i+m.t; 57 | x.a[j] += m.am(0, u0, x, i, 0, m.t); 58 | // propagate carry 59 | while (x.a[j]>=BigInteger.DV) { 60 | x.a[j] -= BigInteger.DV; 61 | x.a[++j]++; 62 | } 63 | } 64 | x.clamp(); 65 | x.drShiftTo(m.t, x); 66 | if (x.compareTo(m)>=0) { 67 | x.subTo(m,x); 68 | } 69 | } 70 | /** 71 | * r = "x^2/R mod m"; x != r 72 | */ 73 | public function sqrTo(x:BigInteger, r:BigInteger):void { 74 | x.squareTo(r); 75 | reduce(r); 76 | } 77 | /** 78 | * r = "xy/R mod m"; x,y != r 79 | */ 80 | public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void { 81 | x.multiplyTo(y,r); 82 | reduce(r); 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/der/Sequence.as: -------------------------------------------------------------------------------- 1 | /** 2 | * Sequence 3 | * 4 | * An ASN1 type for a Sequence, implemented as an Array 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util.der 10 | { 11 | import flash.utils.ByteArray; 12 | 13 | public dynamic class Sequence extends Array implements IAsn1Type 14 | { 15 | protected var type:uint; 16 | protected var len:uint; 17 | 18 | public function Sequence(type:uint = 0x30, length:uint = 0x00) { 19 | this.type = type; 20 | this.len = length; 21 | } 22 | 23 | public function getLength():uint 24 | { 25 | return len; 26 | } 27 | 28 | public function getType():uint 29 | { 30 | return type; 31 | } 32 | 33 | public function toDER():ByteArray { 34 | var tmp:ByteArray = new ByteArray; 35 | for (var i:int=0;ihash.getInputSize()) { 46 | hashKey = hash.hash(key); 47 | } else { 48 | hashKey = new ByteArray; 49 | hashKey.writeBytes(key); 50 | } 51 | while (hashKey.length0 && bits<8*outerHash.length) { 69 | outerHash.length = bits/8; 70 | } 71 | return outerHash; 72 | } 73 | public function dispose():void { 74 | hash = null; 75 | bits = 0; 76 | } 77 | public function toString():String { 78 | return "hmac-"+(bits>0?bits+"-":"")+hash.toString(); 79 | } 80 | 81 | } 82 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/ECBMode.as: -------------------------------------------------------------------------------- 1 | /** 2 | * ECBMode 3 | * 4 | * An ActionScript 3 implementation of the ECB confidentiality mode 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric 10 | { 11 | import flash.utils.ByteArray; 12 | import com.hurlant.util.Memory; 13 | import com.hurlant.util.Hex; 14 | 15 | /** 16 | * ECB mode. 17 | * This uses a padding and a symmetric key. 18 | * If no padding is given, PKCS#5 is used. 19 | */ 20 | public class ECBMode implements IMode, ICipher 21 | { 22 | private var key:ISymmetricKey; 23 | private var padding:IPad; 24 | 25 | public function ECBMode(key:ISymmetricKey, padding:IPad = null) { 26 | this.key = key; 27 | if (padding == null) { 28 | padding = new PKCS5(key.getBlockSize()); 29 | } else { 30 | padding.setBlockSize(key.getBlockSize()); 31 | } 32 | this.padding = padding; 33 | } 34 | 35 | public function getBlockSize():uint { 36 | return key.getBlockSize(); 37 | } 38 | 39 | public function encrypt(src:ByteArray):void { 40 | padding.pad(src); 41 | src.position = 0; 42 | var blockSize:uint = key.getBlockSize(); 43 | var tmp:ByteArray = new ByteArray; 44 | var dst:ByteArray = new ByteArray; 45 | for (var i:uint=0;i 9 | * @author Nico Kaiser 10 | * @version 0.2 11 | */ 12 | 13 | /** 14 | * This is the main socket class 15 | */ 16 | class Socket 17 | { 18 | /** 19 | * @var Socket Holds the master socket 20 | */ 21 | protected $_master; 22 | 23 | /** 24 | * @var array Holds all connected sockets 25 | */ 26 | protected $_allsockets = array(); 27 | 28 | public function __construct($host = 'localhost', $port = 8000, $max = 100) 29 | { 30 | ob_implicit_flush(true); 31 | $this->_createSocket($host, $port); 32 | } 33 | 34 | /** 35 | * Create a socket on given host/port 36 | * 37 | * @param string $host The host/bind address to use 38 | * @param int $port The actual port to bind on 39 | */ 40 | private function _createSocket($host, $port) 41 | { 42 | if (($this->_master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { 43 | die("socket_create() failed, reason: " . socket_strerror($this->_master)); 44 | } 45 | 46 | self::console("Socket {$this->_master} created."); 47 | 48 | socket_set_option($this->_master, SOL_SOCKET, SO_REUSEADDR, 1); 49 | #socket_set_option($master,SOL_SOCKET,SO_KEEPALIVE,1); 50 | 51 | if (($ret = socket_bind($this->_master, $host, $port)) < 0) { 52 | die("socket_bind() failed, reason: " . socket_strerror($ret)); 53 | } 54 | 55 | self::console("Socket bound to {$host}:{$port}."); 56 | 57 | if (($ret = socket_listen($this->_master, 5)) < 0) { 58 | die("socket_listen() failed, reason: " . socket_strerror($ret)); 59 | } 60 | 61 | self::console('Start listening on Socket.'); 62 | 63 | $this->_allsockets[] = $this->_master; 64 | } 65 | 66 | /** 67 | * Log a message 68 | * 69 | * @param string $msg The message 70 | * @param string $type The type of the message 71 | */ 72 | public function console($msg, $type='System') 73 | { 74 | /* $msg = explode("\n", $msg); 75 | foreach ($msg as $line) 76 | echo date('Y-m-d H:i:s') . " {$type}: {$line}\n"; */ 77 | } 78 | 79 | /** 80 | * Sends a message over the socket 81 | * @param socket $client The destination socket 82 | * @param string $msg The message 83 | */ 84 | public function send($client, $msg) 85 | { 86 | socket_write($client, $msg, strlen($msg)); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tls/TLSConfig.as: -------------------------------------------------------------------------------- 1 | /** 2 | * TLSConfig 3 | * 4 | * A set of configuration parameters for use by a TLSSocket or a TLSEngine. 5 | * Most parameters are optional and will be set to appropriate default values for most use. 6 | * 7 | * Copyright (c) 2007 Henri Torgemane 8 | * 9 | * See LICENSE.txt for full license information. 10 | */ 11 | package com.hurlant.crypto.tls { 12 | import flash.utils.ByteArray; 13 | import com.hurlant.util.der.PEM; 14 | import com.hurlant.crypto.rsa.RSAKey; 15 | import com.hurlant.crypto.cert.X509CertificateCollection; 16 | import com.hurlant.crypto.cert.MozillaRootCertificates; 17 | 18 | public class TLSConfig { 19 | public var entity:uint; // SERVER | CLIENT 20 | 21 | public var certificate:ByteArray; 22 | public var privateKey:RSAKey; 23 | 24 | public var cipherSuites:Array; 25 | 26 | public var compressions:Array; 27 | public var ignoreCommonNameMismatch:Boolean = false; 28 | public var trustAllCertificates:Boolean = false; 29 | public var trustSelfSignedCertificates:Boolean = false; 30 | public var promptUserForAcceptCert:Boolean = false; 31 | public var CAStore:X509CertificateCollection; 32 | public var localKeyStore:X509CertificateCollection; 33 | public var version:uint; 34 | 35 | public function TLSConfig( entity:uint, cipherSuites:Array = null, compressions:Array = null, 36 | certificate:ByteArray = null, privateKey:RSAKey = null, CAStore:X509CertificateCollection = null, ver:uint = 0x00) { 37 | this.entity = entity; 38 | this.cipherSuites = cipherSuites; 39 | this.compressions = compressions; 40 | this.certificate = certificate; 41 | this.privateKey = privateKey; 42 | this.CAStore = CAStore; 43 | this.version = ver; 44 | // default settings. 45 | if (cipherSuites==null) { 46 | this.cipherSuites = CipherSuites.getDefaultSuites(); 47 | } 48 | if (compressions==null) { 49 | this.compressions = [TLSSecurityParameters.COMPRESSION_NULL]; 50 | } 51 | 52 | if (CAStore==null) { 53 | this.CAStore = new MozillaRootCertificates; 54 | } 55 | 56 | if (ver==0x00) { 57 | // Default to TLS 58 | this.version = TLSSecurityParameters.PROTOCOL_VERSION; 59 | } 60 | } 61 | 62 | public function setPEMCertificate(cert:String, key:String = null):void { 63 | if (key == null) { 64 | key = cert; // for folks who like to concat those two in one file. 65 | } 66 | certificate = PEM.readCertIntoArray(cert); 67 | privateKey = PEM.readRSAPrivateKey(key); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/der/Type.as: -------------------------------------------------------------------------------- 1 | /** 2 | * Type 3 | * 4 | * A few Asn-1 structures 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util.der 10 | { 11 | import com.hurlant.util.Hex; 12 | 13 | public class Type 14 | { 15 | public static const TLS_CERT:Array = [ 16 | {name:"signedCertificate", extract:true, value: [ 17 | {name:"versionHolder", optional:true, value: [ 18 | {name:"version"} 19 | ], defaultValue: function():Sequence{ 20 | var s:Sequence = new Sequence(0, 0); 21 | var v:Integer = new Integer(2,1, Hex.toArray("00")); 22 | s.push(v); 23 | s.version = v; 24 | return s; 25 | }() 26 | }, 27 | {name:"serialNumber"}, 28 | {name:"signature", value: [ 29 | {name:"algorithmId"} 30 | ]}, 31 | {name:"issuer", extract:true, value: [ 32 | {name:"type"}, 33 | {name:"value"} 34 | ]}, 35 | {name:"validity", value: [ 36 | {name:"notBefore"}, 37 | {name:"notAfter"} 38 | ]}, 39 | {name:"subject", extract:true, value: [ 40 | ]}, 41 | {name:"subjectPublicKeyInfo", value: [ 42 | {name:"algorithm", value: [ 43 | {name:"algorithmId"} 44 | ]}, 45 | {name:"subjectPublicKey"} 46 | ]}, 47 | {name:"extensions", value: [ 48 | ]} 49 | ]}, 50 | {name:"algorithmIdentifier",value:[ 51 | {name:"algorithmId"} 52 | ]}, 53 | {name:"encrypted", value:null} 54 | ]; 55 | public static const CERTIFICATE:Array = [ 56 | {name:"tbsCertificate", value:[ 57 | {name:"tag0", value:[ 58 | {name:"version"} 59 | ]}, 60 | {name:"serialNumber"}, 61 | {name:"signature"}, 62 | {name:"issuer", value:[ 63 | {name:"type"}, 64 | {name:"value"} 65 | ]}, 66 | {name:"validity", value:[ 67 | {name:"notBefore"}, 68 | {name:"notAfter"} 69 | ]}, 70 | {name:"subject"}, 71 | {name:"subjectPublicKeyInfo", value:[ 72 | {name:"algorithm"}, 73 | {name:"subjectPublicKey"} 74 | ]}, 75 | {name:"issuerUniqueID"}, 76 | {name:"subjectUniqueID"}, 77 | {name:"extensions"} 78 | ]}, 79 | {name:"signatureAlgorithm"}, 80 | {name:"signatureValue"} 81 | ]; 82 | public static const RSA_PUBLIC_KEY:Array = [ 83 | {name:"modulus"}, 84 | {name:"publicExponent"} 85 | ]; 86 | public static const RSA_SIGNATURE:Array = [ 87 | {name:"algorithm", value:[ 88 | {name:"algorithmId"} 89 | ]}, 90 | {name:"hash"} 91 | ]; 92 | 93 | } 94 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/TripleDESKey.as: -------------------------------------------------------------------------------- 1 | /** 2 | * TripleDESKey 3 | * 4 | * An Actionscript 3 implementation of Triple DES 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * Derived from: 8 | * The Bouncy Castle Crypto package, 9 | * Copyright (c) 2000-2004 The Legion Of The Bouncy Castle 10 | * (http://www.bouncycastle.org) 11 | * 12 | * See LICENSE.txt for full license information. 13 | */ 14 | package com.hurlant.crypto.symmetric 15 | { 16 | import flash.utils.ByteArray; 17 | import com.hurlant.util.Memory; 18 | import com.hurlant.util.Hex; 19 | 20 | public class TripleDESKey extends DESKey 21 | { 22 | protected var encKey2:Array; 23 | protected var encKey3:Array; 24 | protected var decKey2:Array; 25 | protected var decKey3:Array; 26 | 27 | /** 28 | * This supports 2TDES and 3TDES. 29 | * If the key passed is 128 bits, 2TDES is used. 30 | * If the key has 192 bits, 3TDES is used. 31 | * Other key lengths give "undefined" results. 32 | */ 33 | public function TripleDESKey(key:ByteArray) 34 | { 35 | super(key); 36 | encKey2 = generateWorkingKey(false, key, 8); 37 | decKey2 = generateWorkingKey(true, key, 8); 38 | if (key.length>16) { 39 | encKey3 = generateWorkingKey(true, key, 16); 40 | decKey3 = generateWorkingKey(false, key, 16); 41 | } else { 42 | encKey3 = encKey; 43 | decKey3 = decKey; 44 | } 45 | } 46 | 47 | public override function dispose():void 48 | { 49 | super.dispose(); 50 | var i:uint = 0; 51 | if (encKey2!=null) { 52 | for (i=0;i 9 | * @author Dmitry Gulyakevich 10 | */ 11 | abstract class Application 12 | { 13 | 14 | protected static $instances = array(); 15 | private $_clients = array(); 16 | 17 | /** 18 | * Singleton. 19 | */ 20 | protected function __construct() 21 | { 22 | 23 | } 24 | 25 | /** 26 | * Singleton. 27 | */ 28 | final private function __clone() 29 | { 30 | 31 | } 32 | 33 | /* 34 | * Singleton. 35 | */ 36 | 37 | final public static function getInstance() 38 | { 39 | $calledClassName = get_called_class(); 40 | if (!isset(self::$instances[$calledClassName])) { 41 | self::$instances[$calledClassName] = new $calledClassName(); 42 | } 43 | 44 | return self::$instances[$calledClassName]; 45 | } 46 | 47 | /** 48 | * 49 | * @param object $connection Connection. 50 | */ 51 | public function onConnect($connection) 52 | { 53 | $this->_clients[] = $connection; 54 | } 55 | 56 | /** 57 | * 58 | * @param object $connection Connection. 59 | */ 60 | public function onDisconnect($connection) 61 | { 62 | $key = array_search($connection, $this->_clients); 63 | if ($key) { 64 | unset($this->_clients[$key]); 65 | } 66 | } 67 | 68 | /** 69 | * Send to application sockets. 70 | * 71 | * @param string $message 72 | */ 73 | public function sendApp($message) 74 | { 75 | foreach ($this->_clients as $v) { 76 | 77 | $v->send($message); 78 | } 79 | } 80 | 81 | /** 82 | * Send to application sockets with exclude. 83 | * 84 | * @param Connection $excludeConnection 85 | * @param string $message 86 | */ 87 | public function broadcastApp($excludeConnection, $message) 88 | { 89 | $excludeKey = array_search($excludeConnection, $this->_clients); 90 | 91 | foreach ($this->_clients as $k => $v) { 92 | 93 | if ($k == $excludeKey) { 94 | continue; 95 | } 96 | 97 | $v->send($message); 98 | } 99 | } 100 | 101 | /** 102 | * Tick. 103 | */ 104 | public function onTick() 105 | { 106 | 107 | } 108 | 109 | /** 110 | * Get all application conections. 111 | * 112 | * @return array Array of objects (Connection) 113 | */ 114 | public function getConnections() 115 | { 116 | return $this->_clients; 117 | } 118 | 119 | abstract public function onData($data, $client); 120 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/XTeaKey.as: -------------------------------------------------------------------------------- 1 | /** 2 | * XTeaKey 3 | * 4 | * An ActionScript 3 implementation of the XTea algorithm 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric 10 | { 11 | import com.hurlant.crypto.prng.Random; 12 | import com.hurlant.util.Memory; 13 | 14 | import flash.utils.ByteArray; 15 | 16 | 17 | public class XTeaKey implements ISymmetricKey 18 | { 19 | public const NUM_ROUNDS:uint = 64; 20 | private var k:Array; 21 | 22 | public function XTeaKey(a:ByteArray) { 23 | a.position=0; 24 | k = [a.readUnsignedInt(),a.readUnsignedInt(),a.readUnsignedInt(),a.readUnsignedInt()]; 25 | } 26 | /** 27 | * K is an hex string with 32 digits. 28 | */ 29 | public static function parseKey(K:String):XTeaKey { 30 | var a:ByteArray = new ByteArray; 31 | a.writeUnsignedInt(parseInt(K.substr(0,8),16)); 32 | a.writeUnsignedInt(parseInt(K.substr(8,8),16)); 33 | a.writeUnsignedInt(parseInt(K.substr(16,8),16)); 34 | a.writeUnsignedInt(parseInt(K.substr(24,8),16)); 35 | a.position = 0; 36 | return new XTeaKey(a); 37 | } 38 | 39 | public function getBlockSize():uint { 40 | return 8; 41 | } 42 | 43 | public function encrypt(block:ByteArray, index:uint=0):void { 44 | block.position = index; 45 | var v0:uint = block.readUnsignedInt(); 46 | var v1:uint = block.readUnsignedInt(); 47 | var i:uint; 48 | var sum:uint =0; 49 | var delta:uint = 0x9E3779B9; 50 | for (i=0; i> 5)) + v1) ^ (sum + k[sum & 3]); 52 | sum += delta; 53 | v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); 54 | } 55 | block.position-=8; 56 | block.writeUnsignedInt(v0); 57 | block.writeUnsignedInt(v1); 58 | } 59 | 60 | public function decrypt(block:ByteArray, index:uint=0):void { 61 | block.position = index; 62 | var v0:uint = block.readUnsignedInt(); 63 | var v1:uint = block.readUnsignedInt(); 64 | var i:uint; 65 | var delta:uint = 0x9E3779B9; 66 | var sum:uint = delta*NUM_ROUNDS; 67 | for (i=0; i> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); 69 | sum -= delta; 70 | v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); 71 | } 72 | block.position-=8; 73 | block.writeUnsignedInt(v0); 74 | block.writeUnsignedInt(v1); 75 | } 76 | 77 | public function dispose():void { 78 | //private var k:Array; 79 | var r:Random = new Random; 80 | for (var i:uint=0;i0) { 45 | o = b.readUnsignedByte(); 46 | var last:Boolean = (o&0x80)==0; 47 | o &= 0x7f; 48 | v = v*128 + o; 49 | if (last) { 50 | a.push(v); 51 | v = 0; 52 | } 53 | } 54 | oid = a; 55 | } 56 | 57 | public function getLength():uint 58 | { 59 | return len; 60 | } 61 | 62 | public function getType():uint 63 | { 64 | return type; 65 | } 66 | 67 | public function toDER():ByteArray { 68 | var tmp:Array = []; 69 | tmp[0] = oid[0]*40 + oid[1]; 70 | for (var i:int=2;i>7)|0x80 ); 76 | tmp.push( v&0x7f ); 77 | } else if (v<128*128*128) { 78 | tmp.push( (v>>14)|0x80 ); 79 | tmp.push( (v>>7)&0x7f | 0x80 ); 80 | tmp.push( v&0x7f); 81 | } else if (v<128*128*128*128) { 82 | tmp.push( (v>>21)|0x80 ); 83 | tmp.push( (v>>14) & 0x7f | 0x80 ); 84 | tmp.push( (v>>7) & 0x7f | 0x80 ); 85 | tmp.push( v & 0x7f ); 86 | } else { 87 | throw new Error("OID element bigger than we thought. :("); 88 | } 89 | } 90 | len = tmp.length; 91 | if (type==0) { 92 | type = 6; 93 | } 94 | tmp.unshift(len); // assume length is small enough to fit here. 95 | tmp.unshift(type); 96 | var b:ByteArray = new ByteArray; 97 | for (i=0;i> 5] |= 0x80 << (24 - len % 32); 32 | x[((len + 64 >> 9) << 4) + 15] = len; 33 | 34 | var w:Array = []; 35 | var a:uint = 0x67452301; //1732584193; 36 | var b:uint = 0xEFCDAB89; //-271733879; 37 | var c:uint = 0x98BADCFE; //-1732584194; 38 | var d:uint = 0x10325476; //271733878; 39 | var e:uint = 0xC3D2E1F0; //-1009589776; 40 | 41 | for(var i:uint = 0; i < x.length; i += 16) 42 | { 43 | 44 | var olda:uint = a; 45 | var oldb:uint = b; 46 | var oldc:uint = c; 47 | var oldd:uint = d; 48 | var olde:uint = e; 49 | 50 | for(var j:uint = 0; j < 80; j++) 51 | { 52 | if (j < 16) { 53 | w[j] = x[i + j] || 0; 54 | } else { 55 | w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); 56 | } 57 | var t:uint = rol(a,5) + ft(j,b,c,d) + e + w[j] + kt(j); 58 | e = d; 59 | d = c; 60 | c = rol(b, 30); 61 | b = a; 62 | a = t; 63 | } 64 | a += olda; 65 | b += oldb; 66 | c += oldc; 67 | d += oldd; 68 | e += olde; 69 | } 70 | return [ a, b, c, d, e ]; 71 | 72 | } 73 | 74 | /* 75 | * Bitwise rotate a 32-bit number to the left. 76 | */ 77 | private function rol(num:uint, cnt:uint):uint 78 | { 79 | return (num << cnt) | (num >>> (32 - cnt)); 80 | } 81 | 82 | /* 83 | * Perform the appropriate triplet combination function for the current 84 | * iteration 85 | */ 86 | private function ft(t:uint, b:uint, c:uint, d:uint):uint 87 | { 88 | if(t < 20) return (b & c) | ((~b) & d); 89 | if(t < 40) return b ^ c ^ d; 90 | if(t < 60) return (b & c) | (b & d) | (c & d); 91 | return b ^ c ^ d; 92 | } 93 | 94 | /* 95 | * Determine the appropriate additive constant for the current iteration 96 | */ 97 | private function kt(t:uint):uint 98 | { 99 | return (t < 20) ? 0x5A827999 : (t < 40) ? 0x6ED9EBA1 : 100 | (t < 60) ? 0x8F1BBCDC : 0xCA62C1D6; 101 | } 102 | public override function toString():String { 103 | return "sha1"; 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/symmetric/IVMode.as: -------------------------------------------------------------------------------- 1 | /** 2 | * IVMode 3 | * 4 | * An abstract class for confidentialy modes that rely on an initialization vector. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.symmetric 10 | { 11 | import com.hurlant.crypto.prng.Random; 12 | import com.hurlant.crypto.tests.TestCase; 13 | import com.hurlant.util.Memory; 14 | 15 | import flash.utils.ByteArray; 16 | 17 | /** 18 | * An "abtract" class to avoid redundant code in subclasses 19 | */ 20 | public class IVMode 21 | { 22 | protected var key:ISymmetricKey; 23 | protected var padding:IPad; 24 | // random generator used to generate IVs 25 | protected var prng:Random; 26 | // optional static IV. used for testing only. 27 | protected var iv:ByteArray; 28 | // generated IV is stored here. 29 | protected var lastIV:ByteArray; 30 | protected var blockSize:uint; 31 | 32 | 33 | public function IVMode(key:ISymmetricKey, padding:IPad = null) { 34 | this.key = key; 35 | blockSize = key.getBlockSize(); 36 | if (padding == null) { 37 | padding = new PKCS5(blockSize); 38 | } else { 39 | padding.setBlockSize(blockSize); 40 | } 41 | this.padding = padding; 42 | 43 | prng = new Random; 44 | iv = null; 45 | lastIV = new ByteArray; 46 | } 47 | 48 | public function getBlockSize():uint { 49 | return key.getBlockSize(); 50 | } 51 | public function dispose():void { 52 | var i:uint; 53 | if (iv != null) { 54 | for (i=0;i>> 8; 42 | pool[pptr++] = t&255; 43 | } 44 | pptr=0; 45 | seed(); 46 | } 47 | 48 | public function seed(x:int = 0):void { 49 | if (x==0) { 50 | x = new Date().getTime(); 51 | } 52 | pool[pptr++] ^= x & 255; 53 | pool[pptr++] ^= (x>>8)&255; 54 | pool[pptr++] ^= (x>>16)&255; 55 | pool[pptr++] ^= (x>>24)&255; 56 | pptr %= psize; 57 | seeded = true; 58 | } 59 | 60 | /** 61 | * Gather anything we have that isn't entirely predictable: 62 | * - memory used 63 | * - system capabilities 64 | * - timing stuff 65 | * - installed fonts 66 | */ 67 | public function autoSeed():void { 68 | var b:ByteArray = new ByteArray; 69 | b.writeUnsignedInt(System.totalMemory); 70 | b.writeUTF(Capabilities.serverString); 71 | b.writeUnsignedInt(getTimer()); 72 | b.writeUnsignedInt((new Date).getTime()); 73 | var a:Array = Font.enumerateFonts(true); 74 | for each (var f:Font in a) { 75 | b.writeUTF(f.fontName); 76 | b.writeUTF(f.fontStyle); 77 | b.writeUTF(f.fontType); 78 | } 79 | b.position=0; 80 | while (b.bytesAvailable>=4) { 81 | seed(b.readUnsignedInt()); 82 | } 83 | } 84 | 85 | 86 | public function nextBytes(buffer:ByteArray, length:int):void { 87 | while (length--) { 88 | buffer.writeByte(nextByte()); 89 | } 90 | } 91 | public function nextByte():int { 92 | if (!ready) { 93 | if (!seeded) { 94 | autoSeed(); 95 | } 96 | state.init(pool); 97 | pool.length = 0; 98 | pptr = 0; 99 | ready = true; 100 | } 101 | return state.next(); 102 | } 103 | public function dispose():void { 104 | for (var i:uint=0;i 2 | 3 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 26 | true 27 | 10 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 42 | true 43 | 10 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 58 | true 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 75 | true 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 90 | true 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/DESKeyTest.as: -------------------------------------------------------------------------------- 1 | /** 2 | * DesKeyTest 3 | * 4 | * A test class for DesKey 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | import com.hurlant.crypto.symmetric.DESKey; 12 | import com.hurlant.util.Hex; 13 | import flash.utils.ByteArray; 14 | 15 | public class DESKeyTest extends TestCase 16 | { 17 | public function DESKeyTest(h:ITestHarness) 18 | { 19 | super(h, "DESKey Test"); 20 | runTest(testECB,"DES ECB Test Vectors"); 21 | h.endTestCase(); 22 | } 23 | 24 | /** 25 | * Test vectors mostly grabbed from 26 | * http://csrc.nist.gov/publications/nistpubs/800-17/800-17.pdf 27 | * (Appendix A and B) 28 | * incomplete. 29 | */ 30 | public function testECB():void { 31 | var keys:Array = [ 32 | "3b3898371520f75e", // grabbed from the output of some js implementation out there 33 | "10316E028C8F3B4A", // appendix A vector 34 | "0101010101010101", // appendix B Table 1, round 0 35 | "0101010101010101", // round 1 36 | "0101010101010101", // 2 37 | "0101010101010101", 38 | "0101010101010101", 39 | "0101010101010101", 40 | "0101010101010101", 41 | "0101010101010101", 42 | "0101010101010101", // round 8 43 | "8001010101010101", // app B, tbl 2, round 0 44 | "4001010101010101", 45 | "2001010101010101", 46 | "1001010101010101", 47 | "0801010101010101", 48 | "0401010101010101", 49 | "0201010101010101", 50 | "0180010101010101", 51 | "0140010101010101", // round 8 52 | ]; 53 | var pts:Array = [ 54 | "0000000000000000", // js 55 | "0000000000000000", // App A 56 | "8000000000000000", // App B, tbl 1, rnd0 57 | "4000000000000000", 58 | "2000000000000000", 59 | "1000000000000000", 60 | "0800000000000000", // rnd 4 61 | "0400000000000000", 62 | "0200000000000000", 63 | "0100000000000000", 64 | "0080000000000000", // round 8 65 | "0000000000000000", // App B, tbl2, rnd0 66 | "0000000000000000", 67 | "0000000000000000", 68 | "0000000000000000", 69 | "0000000000000000", 70 | "0000000000000000", 71 | "0000000000000000", 72 | "0000000000000000", 73 | "0000000000000000", // rnd 8 74 | ]; 75 | var cts:Array = [ 76 | "83A1E814889253E0", // js 77 | "82DCBAFBDEAB6602", // App A 78 | "95F8A5E5DD31D900", // App b, tbl 1, rnd 0 79 | "DD7F121CA5015619", 80 | "2E8653104F3834EA", 81 | "4BD388FF6CD81D4F", 82 | "20B9E767B2FB1456", 83 | "55579380D77138EF", 84 | "6CC5DEFAAF04512F", 85 | "0D9F279BA5D87260", 86 | "D9031B0271BD5A0A", // rnd 8 87 | "95A8D72813DAA94D", // App B, tbl 2, rnd 0 88 | "0EEC1487DD8C26D5", 89 | "7AD16FFB79C45926", 90 | "D3746294CA6A6CF3", 91 | "809F5F873C1FD761", 92 | "C02FAFFEC989D1FC", 93 | "4615AA1D33E72F10", 94 | "2055123350C00858", 95 | "DF3B99D6577397C8", // rnd 8 96 | ]; 97 | 98 | for (var i:uint=0;i 2 | 3 | 4 | 5 | Sample of WebSocket 6 | 7 | 8 | 9 | 10 | 11 | 12 | 98 | 99 | 100 |
101 | 102 | 103 | 104 |
105 |
106 | 107 | 108 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/CFBModeTest.as: -------------------------------------------------------------------------------- 1 | /** 2 | * CFBModeTest 3 | * 4 | * A test class for CFBMode 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | import com.hurlant.crypto.symmetric.AESKey; 12 | import com.hurlant.crypto.symmetric.CFBMode; 13 | import com.hurlant.crypto.symmetric.NullPad; 14 | import com.hurlant.util.Hex; 15 | 16 | import flash.utils.ByteArray; 17 | 18 | public class CFBModeTest extends TestCase 19 | { 20 | public function CFBModeTest(h:ITestHarness) 21 | { 22 | super(h, "CFBMode Test"); 23 | runTest(testCFB_AES128, "CFB AES-128 Test Vectors"); 24 | runTest(testCFB_AES192, "CFB AES-192 Test Vectors"); 25 | runTest(testCFB_AES256, "CFB AES-256 Test Vectors"); 26 | h.endTestCase(); 27 | } 28 | 29 | /** 30 | * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf 31 | */ 32 | public function testCFB_AES128():void { 33 | var key:ByteArray = Hex.toArray("2b7e151628aed2a6abf7158809cf4f3c"); 34 | var pt:ByteArray = Hex.toArray( 35 | "6bc1bee22e409f96e93d7e117393172a" + 36 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 37 | "30c81c46a35ce411e5fbc1191a0a52ef" + 38 | "f69f2445df4f9b17ad2b417be66c3710"); 39 | var ct:ByteArray = Hex.toArray( 40 | "3b3fd92eb72dad20333449f8e83cfb4a" + 41 | "c8a64537a0b3a93fcde3cdad9f1ce58b" + 42 | "26751f67a3cbb140b1808cf187a4f4df" + 43 | "c04b05357c5d1c0eeac4c66f9ff7f2e6"); 44 | var cfb:CFBMode = new CFBMode(new AESKey(key), new NullPad); 45 | cfb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f"); 46 | var src:ByteArray = new ByteArray; 47 | src.writeBytes(pt); 48 | cfb.encrypt(src); 49 | assert("CFB_AES128 test 1", Hex.fromArray(src)==Hex.fromArray(ct)); 50 | cfb.decrypt(src); 51 | assert("CFB_AES128 test 2", Hex.fromArray(src)==Hex.fromArray(pt)); 52 | } 53 | public function testCFB_AES192():void { 54 | var key:ByteArray = Hex.toArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"); 55 | var pt:ByteArray = Hex.toArray( 56 | "6bc1bee22e409f96e93d7e117393172a" + 57 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 58 | "30c81c46a35ce411e5fbc1191a0a52ef" + 59 | "f69f2445df4f9b17ad2b417be66c3710"); 60 | var ct:ByteArray = Hex.toArray( 61 | "cdc80d6fddf18cab34c25909c99a4174" + 62 | "67ce7f7f81173621961a2b70171d3d7a" + 63 | "2e1e8a1dd59b88b1c8e60fed1efac4c9" + 64 | "c05f9f9ca9834fa042ae8fba584b09ff"); 65 | var cfb:CFBMode = new CFBMode(new AESKey(key), new NullPad); 66 | cfb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f"); 67 | var src:ByteArray = new ByteArray; 68 | src.writeBytes(pt); 69 | cfb.encrypt(src); 70 | assert("CFB_AES192 test 1", Hex.fromArray(src)==Hex.fromArray(ct)); 71 | cfb.decrypt(src); 72 | assert("CFB_AES192 test 2", Hex.fromArray(src)==Hex.fromArray(pt)); 73 | } 74 | public function testCFB_AES256():void { 75 | var key:ByteArray = Hex.toArray( 76 | "603deb1015ca71be2b73aef0857d7781" + 77 | "1f352c073b6108d72d9810a30914dff4"); 78 | var pt:ByteArray = Hex.toArray( 79 | "6bc1bee22e409f96e93d7e117393172a" + 80 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 81 | "30c81c46a35ce411e5fbc1191a0a52ef" + 82 | "f69f2445df4f9b17ad2b417be66c3710"); 83 | var ct:ByteArray = Hex.toArray( 84 | "dc7e84bfda79164b7ecd8486985d3860" + 85 | "39ffed143b28b1c832113c6331e5407b" + 86 | "df10132415e54b92a13ed0a8267ae2f9" + 87 | "75a385741ab9cef82031623d55b1e471"); 88 | var cfb:CFBMode = new CFBMode(new AESKey(key), new NullPad); 89 | cfb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f"); 90 | var src:ByteArray = new ByteArray; 91 | src.writeBytes(pt); 92 | cfb.encrypt(src); 93 | assert("CFB_AES256 test 1", Hex.fromArray(src)==Hex.fromArray(ct)); 94 | cfb.decrypt(src); 95 | assert("CFB_AES256 test 2", Hex.fromArray(src)==Hex.fromArray(pt)); 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/hash/SHA256.as: -------------------------------------------------------------------------------- 1 | /** 2 | * SHA256 3 | * 4 | * An ActionScript 3 implementation of Secure Hash Algorithm, SHA-256, as defined 5 | * in FIPS PUB 180-2 6 | * Copyright (c) 2007 Henri Torgemane 7 | * 8 | * Derived from: 9 | * A JavaScript implementation of the Secure Hash Standard 10 | * Version 0.3 Copyright Angel Marin 2003-2004 - http://anmar.eu.org/ 11 | * Derived from: 12 | * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined 13 | * in FIPS PUB 180-1 14 | * Version 2.1a Copyright Paul Johnston 2000 - 2002. 15 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet 16 | * 17 | * See LICENSE.txt for full license information. 18 | */ 19 | package com.hurlant.crypto.hash 20 | { 21 | 22 | 23 | public class SHA256 extends SHABase implements IHash 24 | { 25 | 26 | protected static const k:Array = [ 27 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 28 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 29 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 30 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 31 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 32 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 33 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 34 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]; 35 | protected var h:Array = [ 36 | 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 37 | ]; 38 | 39 | public function SHA256(){ 40 | } 41 | 42 | public override function getHashSize():uint 43 | { 44 | return 32; 45 | } 46 | 47 | protected override function core(x:Array, len:uint):Array { 48 | /* append padding */ 49 | x[len >> 5] |= 0x80 << (24 - len % 32); 50 | x[((len + 64 >> 9) << 4) + 15] = len; 51 | 52 | var w:Array = []; 53 | var a:uint = h[0]; 54 | var b:uint = h[1]; 55 | var c:uint = h[2]; 56 | var d:uint = h[3]; 57 | var e:uint = h[4]; 58 | var f:uint = h[5]; 59 | var g:uint = h[6]; 60 | var h:uint = h[7]; 61 | 62 | for (var i:uint=0; i>>3); 77 | var s1:uint = rrol(w[j-2], 17)^rrol(w[j-2],19)^(w[j-2]>>>10); 78 | w[j] = w[j-16] + s0 + w[j-7] + s1; 79 | } 80 | var t2:uint = (rrol(a,2) ^ rrol(a,13) ^ rrol(a,22)) + ((a&b) ^ (a&c) ^ (b&c)); 81 | var t1:uint = h + (rrol(e,6) ^ rrol(e,11) ^ rrol(e,25)) + ((e&f)^(g&~e)) + k[j] + w[j] 82 | h = g; 83 | g = f; 84 | f = e; 85 | e = d + t1; 86 | d = c; 87 | c = b; 88 | b = a; 89 | a = t1 + t2; 90 | 91 | } 92 | a += olda; 93 | b += oldb; 94 | c += oldc; 95 | d += oldd; 96 | e += olde; 97 | f += oldf; 98 | g += oldg; 99 | h += oldh; 100 | } 101 | return [ a,b,c,d,e,f,g,h ]; 102 | } 103 | 104 | /* 105 | * Bitwise rotate a 32-bit number to the right. 106 | */ 107 | protected function rrol(num:uint, cnt:uint):uint { 108 | return (num << (32-cnt)) | (num >>> cnt); 109 | } 110 | 111 | public override function toString():String { 112 | return "sha256"; 113 | } 114 | } 115 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tls/BulkCiphers.as: -------------------------------------------------------------------------------- 1 | /** 2 | * BulkCiphers 3 | * 4 | * An enumeration of bulk ciphers available for TLS, along with their properties, 5 | * with a few convenience methods to go with it. 6 | * Copyright (c) 2007 Henri Torgemane 7 | * 8 | * See LICENSE.txt for full license information. 9 | */ 10 | package com.hurlant.crypto.tls { 11 | import com.hurlant.crypto.Crypto; 12 | import flash.utils.ByteArray; 13 | import com.hurlant.crypto.symmetric.ICipher; 14 | import com.hurlant.crypto.symmetric.TLSPad; 15 | import com.hurlant.crypto.symmetric.SSLPad; 16 | 17 | public class BulkCiphers { 18 | public static const STREAM_CIPHER:uint = 0; 19 | public static const BLOCK_CIPHER:uint = 1; 20 | 21 | public static const NULL:uint = 0; 22 | public static const RC4_40:uint = 1; 23 | public static const RC4_128:uint = 2 24 | public static const RC2_CBC_40:uint = 3; // XXX I don't have that one. 25 | public static const DES_CBC:uint = 4; 26 | public static const DES3_EDE_CBC:uint = 5; 27 | public static const DES40_CBC:uint = 6; 28 | public static const IDEA_CBC:uint = 7; // XXX I don't have that one. 29 | public static const AES_128:uint = 8; 30 | public static const AES_256:uint = 9; 31 | 32 | private static const algos:Array = 33 | ['', 'rc4', 'rc4', '', 'des-cbc', '3des-cbc', 'des-cbc', '', 'aes', 'aes']; 34 | 35 | private static var _props:Array; 36 | 37 | init(); 38 | private static function init():void { 39 | _props = []; 40 | _props[NULL] = new BulkCiphers(STREAM_CIPHER, 0, 0, 0, 0, 0); 41 | _props[RC4_40] = new BulkCiphers(STREAM_CIPHER, 5, 16, 40, 0, 0); 42 | _props[RC4_128] = new BulkCiphers(STREAM_CIPHER, 16, 16, 128, 0, 0); 43 | _props[RC2_CBC_40] = new BulkCiphers( BLOCK_CIPHER, 5, 16, 40, 8, 8); 44 | _props[DES_CBC] = new BulkCiphers( BLOCK_CIPHER, 8, 8, 56, 8, 8); 45 | _props[DES3_EDE_CBC] = new BulkCiphers( BLOCK_CIPHER, 24, 24, 168, 8, 8); 46 | _props[DES40_CBC] = new BulkCiphers( BLOCK_CIPHER, 5, 8, 40, 8, 8); 47 | _props[IDEA_CBC] = new BulkCiphers( BLOCK_CIPHER, 16, 16, 128, 8, 8); 48 | _props[AES_128] = new BulkCiphers( BLOCK_CIPHER, 16, 16, 128, 16, 16); 49 | _props[AES_256] = new BulkCiphers( BLOCK_CIPHER, 32, 32, 256, 16, 16); 50 | } 51 | 52 | private static function getProp(cipher:uint):BulkCiphers { 53 | var p:BulkCiphers = _props[cipher]; 54 | if (p==null) { 55 | throw new Error("Unknown bulk cipher "+cipher.toString(16)); 56 | } 57 | return p; 58 | } 59 | public static function getType(cipher:uint):uint { 60 | return getProp(cipher).type; 61 | } 62 | public static function getKeyBytes(cipher:uint):uint { 63 | return getProp(cipher).keyBytes; 64 | } 65 | public static function getExpandedKeyBytes(cipher:uint):uint { 66 | return getProp(cipher).expandedKeyBytes; 67 | } 68 | public static function getEffectiveKeyBits(cipher:uint):uint { 69 | return getProp(cipher).effectiveKeyBits; 70 | } 71 | public static function getIVSize(cipher:uint):uint { 72 | return getProp(cipher).IVSize; 73 | } 74 | public static function getBlockSize(cipher:uint):uint { 75 | return getProp(cipher).blockSize; 76 | } 77 | public static function getCipher(cipher:uint, key:ByteArray, proto:uint):ICipher { 78 | if (proto == TLSSecurityParameters.PROTOCOL_VERSION) { 79 | return Crypto.getCipher(algos[cipher], key, new TLSPad); 80 | } else { 81 | return Crypto.getCipher(algos[cipher], key, new SSLPad); 82 | } 83 | } 84 | 85 | 86 | private var type:uint; 87 | private var keyBytes:uint; 88 | private var expandedKeyBytes:uint; 89 | private var effectiveKeyBits:uint; 90 | private var IVSize:uint; 91 | private var blockSize:uint; 92 | 93 | public function BulkCiphers(t:uint, kb:uint, ekb:uint, fkb:uint, ivs:uint, bs:uint) { 94 | type = t; 95 | keyBytes = kb; 96 | expandedKeyBytes = ekb; 97 | effectiveKeyBits = fkb; 98 | IVSize = ivs; 99 | blockSize = bs; 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/util/der/PEM.as: -------------------------------------------------------------------------------- 1 | /** 2 | * PEM 3 | * 4 | * A class to parse some PEM stuff. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.util.der 10 | { 11 | import com.hurlant.crypto.rsa.RSAKey; 12 | import com.hurlant.math.BigInteger; 13 | import com.hurlant.util.Base64; 14 | 15 | import flash.utils.ByteArray; 16 | import com.hurlant.util.Hex; 17 | 18 | public class PEM 19 | { 20 | private static const RSA_PRIVATE_KEY_HEADER:String = "-----BEGIN RSA PRIVATE KEY-----"; 21 | private static const RSA_PRIVATE_KEY_FOOTER:String = "-----END RSA PRIVATE KEY-----"; 22 | private static const RSA_PUBLIC_KEY_HEADER:String = "-----BEGIN PUBLIC KEY-----"; 23 | private static const RSA_PUBLIC_KEY_FOOTER:String = "-----END PUBLIC KEY-----"; 24 | private static const CERTIFICATE_HEADER:String = "-----BEGIN CERTIFICATE-----"; 25 | private static const CERTIFICATE_FOOTER:String = "-----END CERTIFICATE-----"; 26 | 27 | 28 | 29 | /** 30 | * 31 | * Read a structure encoded according to 32 | * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc 33 | * section 11.1.2 34 | * 35 | * @param str 36 | * @return 37 | * 38 | */ 39 | public static function readRSAPrivateKey(str:String):RSAKey { 40 | var der:ByteArray = extractBinary(RSA_PRIVATE_KEY_HEADER, RSA_PRIVATE_KEY_FOOTER, str); 41 | if (der==null) return null; 42 | var obj:* = DER.parse(der); 43 | if (obj is Array) { 44 | var arr:Array = obj as Array; 45 | // arr[0] is Version. should be 0. should be checked. shoulda woulda coulda. 46 | return new RSAKey( 47 | arr[1], // N 48 | arr[2].valueOf(), // E 49 | arr[3], // D 50 | arr[4], // P 51 | arr[5], // Q 52 | arr[6], // DMP1 53 | arr[7], // DMQ1 54 | arr[8]); // IQMP 55 | } else { 56 | // dunno 57 | return null; 58 | } 59 | } 60 | 61 | 62 | /** 63 | * Read a structure encoded according to some spec somewhere 64 | * Also, follows some chunk from 65 | * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc 66 | * section 11.1 67 | * 68 | * @param str 69 | * @return 70 | * 71 | */ 72 | public static function readRSAPublicKey(str:String):RSAKey { 73 | var der:ByteArray = extractBinary(RSA_PUBLIC_KEY_HEADER, RSA_PUBLIC_KEY_FOOTER, str); 74 | if (der==null) return null; 75 | var obj:* = DER.parse(der); 76 | if (obj is Array) { 77 | var arr:Array = obj as Array; 78 | // arr[0] = [ , null ]; ( apparently, that's an X-509 Algorithm Identifier. 79 | if (arr[0][0].toString()!=OID.RSA_ENCRYPTION) { 80 | return null; 81 | } 82 | // arr[1] is a ByteArray begging to be parsed as DER 83 | arr[1].position = 1; // there's a 0x00 byte up front. find out why later. like, read a spec. 84 | obj = DER.parse(arr[1]); 85 | if (obj is Array) { 86 | arr = obj as Array; 87 | // arr[0] = modulus 88 | // arr[1] = public expt. 89 | return new RSAKey(arr[0], arr[1]); 90 | } else { 91 | return null; 92 | } 93 | } else { 94 | // dunno 95 | return null; 96 | } 97 | } 98 | 99 | public static function readCertIntoArray(str:String):ByteArray { 100 | var tmp:ByteArray = extractBinary(CERTIFICATE_HEADER, CERTIFICATE_FOOTER, str); 101 | return tmp; 102 | } 103 | 104 | private static function extractBinary(header:String, footer:String, str:String):ByteArray { 105 | var i:int = str.indexOf(header); 106 | if (i==-1) return null; 107 | i += header.length; 108 | var j:int = str.indexOf(footer); 109 | if (j==-1) return null; 110 | var b64:String = str.substring(i, j); 111 | // remove whitesapces. 112 | b64 = b64.replace(/\s/mg, ''); 113 | // decode 114 | return Base64.decodeToByteArray(b64); 115 | } 116 | 117 | } 118 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/OFBModeTest.as: -------------------------------------------------------------------------------- 1 | /** 2 | * OFBModeTest 3 | * 4 | * A test class for OFBMode 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | import flash.utils.ByteArray; 12 | import com.hurlant.util.Hex; 13 | import com.hurlant.crypto.symmetric.OFBMode; 14 | import com.hurlant.crypto.symmetric.AESKey; 15 | import com.hurlant.crypto.symmetric.NullPad; 16 | 17 | public class OFBModeTest extends TestCase 18 | { 19 | public function OFBModeTest(h:ITestHarness) 20 | { 21 | super(h, "OFBMode Test"); 22 | runTest(testOFB_AES128,"OFB AES-128 Test Vectors"); 23 | runTest(testOFB_AES192,"OFB AES-192 Test Vectors"); 24 | runTest(testOFB_AES256,"OFB AES-256 Test Vectors"); 25 | h.endTestCase(); 26 | } 27 | 28 | /** 29 | * Nist Vectors: http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf 30 | * Section F.4.1 and below. 31 | */ 32 | public function testOFB_AES128():void { 33 | var key:ByteArray = Hex.toArray("2b7e151628aed2a6abf7158809cf4f3c"); 34 | var pt:ByteArray = Hex.toArray( 35 | "6bc1bee22e409f96e93d7e117393172a" + 36 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 37 | "30c81c46a35ce411e5fbc1191a0a52ef" + 38 | "f69f2445df4f9b17ad2b417be66c3710"); 39 | var ct:ByteArray = Hex.toArray( 40 | "3b3fd92eb72dad20333449f8e83cfb4a" + 41 | "7789508d16918f03f53c52dac54ed825" + 42 | "9740051e9c5fecf64344f7a82260edcc" + 43 | "304c6528f659c77866a510d9c1d6ae5e"); 44 | var ofb:OFBMode = new OFBMode(new AESKey(key), new NullPad); 45 | ofb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f"); 46 | var src:ByteArray = new ByteArray; 47 | src.writeBytes(pt); 48 | ofb.encrypt(src); 49 | assert("OFB_AES128 test 1", Hex.fromArray(src)==Hex.fromArray(ct)); 50 | ofb.decrypt(src); 51 | assert("OFB_AES128 test 2", Hex.fromArray(src)==Hex.fromArray(pt)); 52 | } 53 | 54 | public function testOFB_AES192():void { 55 | var key:ByteArray = Hex.toArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"); 56 | var pt:ByteArray = Hex.toArray( 57 | "6bc1bee22e409f96e93d7e117393172a" + 58 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 59 | "30c81c46a35ce411e5fbc1191a0a52ef" + 60 | "f69f2445df4f9b17ad2b417be66c3710"); 61 | var ct:ByteArray = Hex.toArray( 62 | "cdc80d6fddf18cab34c25909c99a4174" + 63 | "fcc28b8d4c63837c09e81700c1100401" + 64 | "8d9a9aeac0f6596f559c6d4daf59a5f2" + 65 | "6d9f200857ca6c3e9cac524bd9acc92a"); 66 | var ofb:OFBMode = new OFBMode(new AESKey(key), new NullPad); 67 | ofb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f"); 68 | var src:ByteArray = new ByteArray; 69 | src.writeBytes(pt); 70 | ofb.encrypt(src); 71 | assert("OFB_AES192 test 1", Hex.fromArray(src)==Hex.fromArray(ct)); 72 | ofb.decrypt(src); 73 | assert("OFB_AES192 test 2", Hex.fromArray(src)==Hex.fromArray(pt)); 74 | } 75 | 76 | public function testOFB_AES256():void { 77 | var key:ByteArray = Hex.toArray( 78 | "603deb1015ca71be2b73aef0857d7781" + 79 | "1f352c073b6108d72d9810a30914dff4"); 80 | var pt:ByteArray = Hex.toArray( 81 | "6bc1bee22e409f96e93d7e117393172a" + 82 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 83 | "30c81c46a35ce411e5fbc1191a0a52ef" + 84 | "f69f2445df4f9b17ad2b417be66c3710"); 85 | var ct:ByteArray = Hex.toArray( 86 | "dc7e84bfda79164b7ecd8486985d3860" + 87 | "4febdc6740d20b3ac88f6ad82a4fb08d" + 88 | "71ab47a086e86eedf39d1c5bba97c408" + 89 | "0126141d67f37be8538f5a8be740e484"); 90 | var ofb:OFBMode = new OFBMode(new AESKey(key), new NullPad); 91 | ofb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f"); 92 | var src:ByteArray = new ByteArray; 93 | src.writeBytes(pt); 94 | ofb.encrypt(src); 95 | assert("OFB_AES256 test 1", Hex.fromArray(src)==Hex.fromArray(ct)); 96 | ofb.decrypt(src); 97 | assert("OFB_AES256 test 2", Hex.fromArray(src)==Hex.fromArray(pt)); 98 | } 99 | 100 | } 101 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/prng/TLSPRF.as: -------------------------------------------------------------------------------- 1 | /** 2 | * TLSPRF 3 | * 4 | * An ActionScript 3 implementation of a pseudo-random generator 5 | * that follows the TLS specification 6 | * Copyright (c) 2007 Henri Torgemane 7 | * 8 | * See LICENSE.txt for full license information. 9 | */ 10 | package com.hurlant.crypto.prng 11 | { 12 | import flash.utils.ByteArray; 13 | import com.hurlant.crypto.hash.HMAC; 14 | import com.hurlant.crypto.hash.MD5; 15 | import com.hurlant.crypto.hash.SHA1; 16 | import com.hurlant.util.Memory; 17 | import com.hurlant.util.Hex; 18 | import flash.utils.IDataOutput; 19 | 20 | /** 21 | * There's "Random", and then there's TLS Random. 22 | * . 23 | * Still Pseudo-random, though. 24 | */ 25 | public class TLSPRF 26 | { 27 | // XXX WAY TOO MANY STRUCTURES HERE 28 | 29 | // seed 30 | private var seed:ByteArray; 31 | // P_MD5's secret 32 | private var s1:ByteArray; 33 | // P_SHA-1's secret 34 | private var s2:ByteArray; 35 | // HMAC_MD5's A 36 | private var a1:ByteArray; 37 | // HMAC_SHA1's A 38 | private var a2:ByteArray; 39 | // Pool for P_MD5 40 | private var p1:ByteArray; 41 | // Pool for P_SHA1 42 | private var p2:ByteArray; 43 | // Data for HMAC_MD5 44 | private var d1:ByteArray; 45 | // Data for HMAC_SHA1 46 | private var d2:ByteArray; 47 | 48 | 49 | private var hmac_md5:HMAC; 50 | private var hmac_sha1:HMAC; 51 | 52 | public function TLSPRF(secret:ByteArray, label:String, seed:ByteArray) { 53 | var l:int = Math.ceil(secret.length/2); 54 | var s1:ByteArray = new ByteArray; 55 | var s2:ByteArray = new ByteArray; 56 | s1.writeBytes(secret, 0, l); 57 | s2.writeBytes(secret, secret.length-l, l); 58 | var s:ByteArray = new ByteArray; 59 | s.writeUTFBytes(label); 60 | s.writeBytes(seed); 61 | this.seed = s; 62 | this.s1 = s1; 63 | this.s2 = s2; 64 | hmac_md5 = new HMAC(new MD5); 65 | hmac_sha1 = new HMAC(new SHA1); 66 | 67 | this.a1 = hmac_md5.compute(s1, this.seed); 68 | this.a2 = hmac_sha1.compute(s2, this.seed); 69 | 70 | p1 = new ByteArray; 71 | p2 = new ByteArray; 72 | 73 | d1 = new ByteArray; 74 | d2 = new ByteArray; 75 | d1.position = MD5.HASH_SIZE; 76 | d1.writeBytes(this.seed); 77 | d2.position = SHA1.HASH_SIZE; 78 | d2.writeBytes(this.seed); 79 | } 80 | 81 | // XXX HORRIBLY SLOW. REWRITE. 82 | public function nextBytes(buffer:IDataOutput, length:int):void { 83 | while (length--) { 84 | buffer.writeByte(nextByte()); 85 | } 86 | } 87 | public function nextByte():int { 88 | if (p1.bytesAvailable==0) { 89 | more_md5(); 90 | } 91 | if (p2.bytesAvailable==0) { 92 | more_sha1(); 93 | } 94 | return p1.readUnsignedByte()^p2.readUnsignedByte(); 95 | } 96 | public function dispose():void { 97 | seed = dba(seed); 98 | s1 = dba(s1); 99 | s2 = dba(s2); 100 | a1 = dba(a1); 101 | a2 = dba(a2); 102 | p1 = dba(p1); 103 | p2 = dba(p2); 104 | d1 = dba(d1); 105 | d2 = dba(d2); 106 | hmac_md5.dispose(); 107 | hmac_md5 = null; 108 | hmac_sha1.dispose(); 109 | hmac_sha1 = null; 110 | Memory.gc(); 111 | } 112 | public function toString():String { 113 | return "tls-prf"; 114 | } 115 | private function dba(ba:ByteArray):ByteArray { 116 | for (var i:uint=0;i hash.getInputSize()) { 85 | hashKey = hash.hash(key); 86 | } else { 87 | hashKey = new ByteArray; 88 | hashKey.writeBytes(key); 89 | } 90 | 91 | while (hashKey.length < hash.getInputSize() ) { 92 | hashKey[hashKey.length] = 0; 93 | } */ 94 | // Henri's conventions work just fine here.. 95 | 96 | innerKey.length = 0; 97 | outerKey.length = 0; 98 | // trace("MAC Key: " + Hex.fromArray(key)); 99 | // trace("Key Length: " + key.length); 100 | // trace("Pad_1 : " + Hex.fromArray(pad_1)); 101 | // inner hash calc 102 | innerKey.writeBytes(key); 103 | innerKey.writeBytes(pad_1); 104 | innerKey.writeBytes(data); 105 | // trace("MAC Inner Key: " + Hex.fromArray(innerKey)); 106 | 107 | innerHash = hash.hash(innerKey); 108 | // trace("MAC Inner Hash: " + Hex.fromArray(innerHash)); 109 | 110 | // outer hash calc 111 | outerKey.writeBytes(key); 112 | outerKey.writeBytes(pad_2); 113 | outerKey.writeBytes(innerHash); 114 | 115 | // trace("MAC Outer Key: " + Hex.fromArray(outerKey)); 116 | outerHash = hash.hash(outerKey); 117 | 118 | 119 | if (bits > 0 && bits < 8*outerHash.length) { 120 | outerHash.length = bits/8; 121 | } 122 | 123 | // trace("MAC for record: " + Hex.fromArray(outerHash)); 124 | return outerHash; 125 | 126 | } 127 | 128 | public function dispose():void { 129 | hash = null; 130 | bits = 0; 131 | } 132 | public function toString():String { 133 | return "mac-"+(bits>0?bits+"-":"")+hash.toString(); 134 | } 135 | 136 | } 137 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/BlowFishKeyTest.as: -------------------------------------------------------------------------------- 1 | /** 2 | * BlowFishKeyTest 3 | * 4 | * A test class for BlowFishKey 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | import com.hurlant.crypto.symmetric.BlowFishKey; 12 | import com.hurlant.util.Hex; 13 | import flash.utils.ByteArray; 14 | 15 | public class BlowFishKeyTest extends TestCase 16 | { 17 | public function BlowFishKeyTest(h:ITestHarness) 18 | { 19 | super(h, "BlowFishKey Test"); 20 | runTest(testECB,"BlowFish ECB Test Vectors"); 21 | h.endTestCase(); 22 | } 23 | 24 | /** 25 | * Test vectors from http://www.schneier.com/code/vectors.txt 26 | */ 27 | public function testECB():void { 28 | var keys:Array = [ 29 | "0000000000000000", 30 | "FFFFFFFFFFFFFFFF", 31 | "3000000000000000", 32 | "1111111111111111", 33 | "0123456789ABCDEF", 34 | "1111111111111111", 35 | "0000000000000000", 36 | "FEDCBA9876543210", 37 | "7CA110454A1A6E57", 38 | "0131D9619DC1376E", 39 | "07A1133E4A0B2686", 40 | "3849674C2602319E", 41 | "04B915BA43FEB5B6", 42 | "0113B970FD34F2CE", 43 | "0170F175468FB5E6", 44 | "43297FAD38E373FE", 45 | "07A7137045DA2A16", 46 | "04689104C2FD3B2F", 47 | "37D06BB516CB7546", 48 | "1F08260D1AC2465E", 49 | "584023641ABA6176", 50 | "025816164629B007", 51 | "49793EBC79B3258F", 52 | "4FB05E1515AB73A7", 53 | "49E95D6D4CA229BF", 54 | "018310DC409B26D6", 55 | "1C587F1C13924FEF", 56 | "0101010101010101", 57 | "1F1F1F1F0E0E0E0E", 58 | "E0FEE0FEF1FEF1FE", 59 | "0000000000000000", 60 | "FFFFFFFFFFFFFFFF", 61 | "0123456789ABCDEF", 62 | "FEDCBA9876543210" ]; 63 | var pts:Array = [ 64 | "0000000000000000", 65 | "FFFFFFFFFFFFFFFF", 66 | "1000000000000001", 67 | "1111111111111111", 68 | "1111111111111111", 69 | "0123456789ABCDEF", 70 | "0000000000000000", 71 | "0123456789ABCDEF", 72 | "01A1D6D039776742", 73 | "5CD54CA83DEF57DA", 74 | "0248D43806F67172", 75 | "51454B582DDF440A", 76 | "42FD443059577FA2", 77 | "059B5E0851CF143A", 78 | "0756D8E0774761D2", 79 | "762514B829BF486A", 80 | "3BDD119049372802", 81 | "26955F6835AF609A", 82 | "164D5E404F275232", 83 | "6B056E18759F5CCA", 84 | "004BD6EF09176062", 85 | "480D39006EE762F2", 86 | "437540C8698F3CFA", 87 | "072D43A077075292", 88 | "02FE55778117F12A", 89 | "1D9D5C5018F728C2", 90 | "305532286D6F295A", 91 | "0123456789ABCDEF", 92 | "0123456789ABCDEF", 93 | "0123456789ABCDEF", 94 | "FFFFFFFFFFFFFFFF", 95 | "0000000000000000", 96 | "0000000000000000", 97 | "FFFFFFFFFFFFFFFF" ]; 98 | var cts:Array = [ 99 | "4EF997456198DD78", 100 | "51866FD5B85ECB8A", 101 | "7D856F9A613063F2", 102 | "2466DD878B963C9D", 103 | "61F9C3802281B096", 104 | "7D0CC630AFDA1EC7", 105 | "4EF997456198DD78", 106 | "0ACEAB0FC6A0A28D", 107 | "59C68245EB05282B", 108 | "B1B8CC0B250F09A0", 109 | "1730E5778BEA1DA4", 110 | "A25E7856CF2651EB", 111 | "353882B109CE8F1A", 112 | "48F4D0884C379918", 113 | "432193B78951FC98", 114 | "13F04154D69D1AE5", 115 | "2EEDDA93FFD39C79", 116 | "D887E0393C2DA6E3", 117 | "5F99D04F5B163969", 118 | "4A057A3B24D3977B", 119 | "452031C1E4FADA8E", 120 | "7555AE39F59B87BD", 121 | "53C55F9CB49FC019", 122 | "7A8E7BFA937E89A3", 123 | "CF9C5D7A4986ADB5", 124 | "D1ABB290658BC778", 125 | "55CB3774D13EF201", 126 | "FA34EC4847B268B2", 127 | "A790795108EA3CAE", 128 | "C39E072D9FAC631D", 129 | "014933E0CDAFF6E4", 130 | "F21E9A77B71C49BC", 131 | "245946885754369A", 132 | "6B5C5A9C5D9E0A5A" ]; 133 | 134 | for (var i:uint=0;i 12 | * > Rogier and Chauvaud (1997) described collisions of MD2's compression function, 13 | * > although they were unable to extend the attack to the full MD2. 14 | * > 15 | * > In 2004, MD2 was shown to be vulnerable to a preimage attack with time 16 | * > complexity equivalent to 2104 applications of the compression function 17 | * > (Muller, 2004). 18 | * > The author concludes, "MD2 can no longer be considered a secure one-way 19 | * > hash function". 20 | * 21 | * also, this implementaton is quite slow. 22 | */ 23 | 24 | package com.hurlant.crypto.hash 25 | { 26 | import flash.utils.ByteArray; 27 | 28 | public class MD2 implements IHash 29 | { 30 | public static const HASH_SIZE:int = 16; 31 | public var pad_size:int = 48; // probably will never get used, only here for SSL 3.0 support 32 | 33 | private static const S:Array = [ // PI Digits 34 | 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 19, 35 | 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202, 36 | 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18, 37 | 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 47, 238, 122, 38 | 169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33, 39 | 128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3, 40 | 255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 41 | 79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 42 | 69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 43 | 27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, 44 | 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38, 45 | 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82, 46 | 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74, 47 | 120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57, 48 | 242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10, 49 | 49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 ]; 50 | 51 | 52 | public function MD2() { } 53 | 54 | public function getInputSize():uint 55 | { 56 | return 16; 57 | } 58 | 59 | public function getPadSize():int { 60 | return pad_size; 61 | } 62 | 63 | public function getHashSize():uint 64 | { 65 | return HASH_SIZE; 66 | } 67 | 68 | public function hash(src:ByteArray):ByteArray 69 | { 70 | var savedLength:uint = src.length; 71 | 72 | // 3.1 Step 1. Padding 73 | var i:uint = (16-src.length%16) || 16; 74 | do { 75 | src[src.length]=i; 76 | } while (src.length%16!=0); 77 | 78 | // 3.2 Step 2. Checksum 79 | var len:uint = src.length; 80 | var checksum:ByteArray = new ByteArray; 81 | var L:uint = 0; 82 | for (i = 0;i - hybi 10 encode 151 | - [web-socket-js](http://github.com/gimite/web-socket-js) by Hiroshi Ichikawa (example) 152 | - [jQuery](http://jquery.com/) (example) 153 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/CTRModeTest.as: -------------------------------------------------------------------------------- 1 | /** 2 | * CTRModeTest 3 | * 4 | * A test class for CTRMode 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | import flash.utils.ByteArray; 12 | import com.hurlant.util.Hex; 13 | import com.hurlant.crypto.symmetric.CTRMode; 14 | import com.hurlant.crypto.symmetric.AESKey; 15 | import com.hurlant.crypto.symmetric.NullPad; 16 | 17 | public class CTRModeTest extends TestCase 18 | { 19 | public function CTRModeTest(h:ITestHarness) 20 | { 21 | super(h, "CTRMode Test"); 22 | runTest(testCTR_AES128, "CTR AES-128 Test Vectors"); 23 | runTest(testCTR_AES192, "CTR AES-192 Test Vectors"); 24 | runTest(testCTR_AES256, "CTR AES-256 Test Vectors"); 25 | h.endTestCase(); 26 | } 27 | 28 | /** 29 | * Vectors from http://csrc.nist.gov/CryptoToolkit/modes/800-38_Series_Publications/SP800-38A.pdf 30 | * Section F.5.1 and below. 31 | */ 32 | public function testCTR_AES128():void { 33 | var key:ByteArray = Hex.toArray("2b7e151628aed2a6abf7158809cf4f3c"); 34 | var pt:ByteArray = Hex.toArray( 35 | "6bc1bee22e409f96e93d7e117393172a" + 36 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 37 | "30c81c46a35ce411e5fbc1191a0a52ef" + 38 | "f69f2445df4f9b17ad2b417be66c3710"); 39 | var ct:ByteArray = Hex.toArray( 40 | "874d6191b620e3261bef6864990db6ce" + 41 | "9806f66b7970fdff8617187bb9fffdff" + 42 | "5ae4df3edbd5d35e5b4f09020db03eab" + 43 | "1e031dda2fbe03d1792170a0f3009cee"); 44 | var ctr:CTRMode = new CTRMode(new AESKey(key), new NullPad); 45 | ctr.IV = Hex.toArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"); 46 | var src:ByteArray = new ByteArray; 47 | src.writeBytes(pt); 48 | ctr.encrypt(src); 49 | var hsrc:String = Hex.fromArray(src); 50 | var hct:String = Hex.fromArray(ct); 51 | assert("CTR_AES128 test 1: "+hsrc+" != "+hct, hsrc==hct); 52 | ctr.decrypt(src); 53 | hsrc = Hex.fromArray(src); 54 | var hpt:String = Hex.fromArray(pt); 55 | assert("CTR_AES128 test 2: "+hsrc+" != "+hpt, hsrc==hpt); 56 | } 57 | public function testCTR_AES192():void { 58 | var key:ByteArray = Hex.toArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"); 59 | var pt:ByteArray = Hex.toArray( 60 | "6bc1bee22e409f96e93d7e117393172a" + 61 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 62 | "30c81c46a35ce411e5fbc1191a0a52ef" + 63 | "f69f2445df4f9b17ad2b417be66c3710"); 64 | var ct:ByteArray = Hex.toArray( 65 | "1abc932417521ca24f2b0459fe7e6e0b" + 66 | "090339ec0aa6faefd5ccc2c6f4ce8e94" + 67 | "1e36b26bd1ebc670d1bd1d665620abf7" + 68 | "4f78a7f6d29809585a97daec58c6b050"); 69 | var ctr:CTRMode = new CTRMode(new AESKey(key), new NullPad); 70 | ctr.IV = Hex.toArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"); 71 | var src:ByteArray = new ByteArray; 72 | src.writeBytes(pt); 73 | ctr.encrypt(src); 74 | var hsrc:String = Hex.fromArray(src); 75 | var hct:String = Hex.fromArray(ct); 76 | assert("CTR_AES192 test 1: "+hsrc+" != "+hct, hsrc==hct); 77 | ctr.decrypt(src); 78 | hsrc = Hex.fromArray(src); 79 | var hpt:String = Hex.fromArray(pt); 80 | assert("CTR_AES192 test 2: "+hsrc+" != "+hpt, hsrc==hpt); 81 | } 82 | public function testCTR_AES256():void { 83 | var key:ByteArray = Hex.toArray("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"); 84 | var pt:ByteArray = Hex.toArray( 85 | "6bc1bee22e409f96e93d7e117393172a" + 86 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 87 | "30c81c46a35ce411e5fbc1191a0a52ef" + 88 | "f69f2445df4f9b17ad2b417be66c3710"); 89 | var ct:ByteArray = Hex.toArray( 90 | "601ec313775789a5b7a7f504bbf3d228" + 91 | "f443e3ca4d62b59aca84e990cacaf5c5" + 92 | "2b0930daa23de94ce87017ba2d84988d" + 93 | "dfc9c58db67aada613c2dd08457941a6"); 94 | var ctr:CTRMode = new CTRMode(new AESKey(key), new NullPad); 95 | ctr.IV = Hex.toArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"); 96 | var src:ByteArray = new ByteArray; 97 | src.writeBytes(pt); 98 | ctr.encrypt(src); 99 | var hsrc:String = Hex.fromArray(src); 100 | var hct:String = Hex.fromArray(ct); 101 | assert("CTR_AES256 test 1: "+hsrc+" != "+hct, hsrc==hct); 102 | ctr.decrypt(src); 103 | hsrc = Hex.fromArray(src); 104 | var hpt:String = Hex.fromArray(pt); 105 | assert("CTR_AES256 test 2: "+hsrc+" != "+hpt, hsrc==hpt); 106 | } 107 | 108 | } 109 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tls/TLSConnectionState.as: -------------------------------------------------------------------------------- 1 | /** 2 | * TLSConnectionState 3 | * 4 | * This class encapsulates the read or write state of a TLS connection, 5 | * and implementes the encrypting and hashing of packets. 6 | * Copyright (c) 2007 Henri Torgemane 7 | * 8 | * See LICENSE.txt for full license information. 9 | */ 10 | package com.hurlant.crypto.tls { 11 | import flash.utils.IDataInput; 12 | import flash.utils.ByteArray; 13 | import com.hurlant.crypto.hash.MD5; 14 | import com.hurlant.crypto.hash.HMAC; 15 | import com.hurlant.crypto.hash.IHash; 16 | import com.hurlant.crypto.symmetric.ICipher; 17 | import com.hurlant.crypto.symmetric.IVMode; 18 | import com.hurlant.util.Hex; 19 | import com.hurlant.util.ArrayUtil; 20 | 21 | public class TLSConnectionState implements IConnectionState { 22 | 23 | 24 | // compression state 25 | 26 | // cipher state 27 | private var bulkCipher:uint; 28 | private var cipherType:uint; 29 | private var CIPHER_key:ByteArray; 30 | private var CIPHER_IV:ByteArray; 31 | private var cipher:ICipher; 32 | private var ivmode:IVMode; 33 | 34 | // mac secret 35 | private var macAlgorithm:uint; 36 | private var MAC_write_secret:ByteArray; 37 | private var hmac:HMAC; 38 | 39 | // sequence number. uint64 40 | private var seq_lo:uint; 41 | private var seq_hi:uint; 42 | 43 | 44 | 45 | public function TLSConnectionState( 46 | bulkCipher:uint=0, cipherType:uint=0, macAlgorithm:uint=0, 47 | mac:ByteArray=null, key:ByteArray=null, IV:ByteArray=null) { 48 | this.bulkCipher = bulkCipher; 49 | this.cipherType = cipherType; 50 | this.macAlgorithm = macAlgorithm; 51 | MAC_write_secret = mac; 52 | hmac = MACs.getHMAC(macAlgorithm); 53 | CIPHER_key = key; 54 | CIPHER_IV = IV; 55 | cipher = BulkCiphers.getCipher(bulkCipher, key, 0x0301); 56 | if (cipher is IVMode) { 57 | ivmode = cipher as IVMode; 58 | ivmode.IV = IV; 59 | } 60 | } 61 | 62 | public function decrypt(type:uint, length:uint, p:ByteArray):ByteArray { 63 | // decompression is a nop. 64 | 65 | if (cipherType == BulkCiphers.STREAM_CIPHER) { 66 | if (bulkCipher == BulkCiphers.NULL) { 67 | // no-op 68 | } else { 69 | cipher.decrypt(p); 70 | } 71 | } else { 72 | // block cipher 73 | var nextIV:ByteArray = new ByteArray; 74 | nextIV.writeBytes(p, p.length-CIPHER_IV.length, CIPHER_IV.length); 75 | 76 | cipher.decrypt(p); 77 | 78 | 79 | CIPHER_IV = nextIV; 80 | ivmode.IV = nextIV; 81 | } 82 | if (macAlgorithm!=MACs.NULL) { 83 | var data:ByteArray = new ByteArray; 84 | var len:uint = p.length - hmac.getHashSize(); 85 | data.writeUnsignedInt(seq_hi); 86 | data.writeUnsignedInt(seq_lo); 87 | data.writeByte(type); 88 | data.writeShort(TLSSecurityParameters.PROTOCOL_VERSION); 89 | data.writeShort(len); 90 | if (len!=0) { 91 | data.writeBytes(p, 0, len); 92 | } 93 | var mac:ByteArray = hmac.compute(MAC_write_secret, data); 94 | // compare "mac" with the last X bytes of p. 95 | var mac_received:ByteArray = new ByteArray; 96 | mac_received.writeBytes(p, len, hmac.getHashSize()); 97 | if (ArrayUtil.equals(mac, mac_received)) { 98 | // happy happy joy joy 99 | } else { 100 | throw new TLSError("Bad Mac Data", TLSError.bad_record_mac); 101 | } 102 | p.length = len; 103 | p.position = 0; 104 | } 105 | // increment seq 106 | seq_lo++; 107 | if (seq_lo==0) seq_hi++; 108 | return p; 109 | } 110 | public function encrypt(type:uint, p:ByteArray):ByteArray { 111 | var mac:ByteArray = null; 112 | if (macAlgorithm!=MACs.NULL) { 113 | var data:ByteArray = new ByteArray; 114 | data.writeUnsignedInt(seq_hi); 115 | data.writeUnsignedInt(seq_lo); 116 | data.writeByte(type); 117 | data.writeShort(TLSSecurityParameters.PROTOCOL_VERSION); 118 | data.writeShort(p.length); 119 | if (p.length!=0) { 120 | data.writeBytes(p, 0, p.length); 121 | } 122 | mac = hmac.compute(MAC_write_secret, data); 123 | p.position = p.length; 124 | p.writeBytes(mac); 125 | } 126 | p.position = 0; 127 | if (cipherType == BulkCiphers.STREAM_CIPHER) { 128 | // stream cipher 129 | if (bulkCipher == BulkCiphers.NULL) { 130 | // no-op 131 | } else { 132 | cipher.encrypt(p); 133 | } 134 | } else { 135 | // block cipher 136 | cipher.encrypt(p); 137 | // adjust IV 138 | var nextIV:ByteArray = new ByteArray; 139 | nextIV.writeBytes(p, p.length-CIPHER_IV.length, CIPHER_IV.length); 140 | CIPHER_IV = nextIV; 141 | ivmode.IV = nextIV; 142 | } 143 | // increment seq 144 | seq_lo++; 145 | if (seq_lo==0) seq_hi++; 146 | // compression is a nop. 147 | return p; 148 | } 149 | 150 | } 151 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tls/SSLConnectionState.as: -------------------------------------------------------------------------------- 1 | /** 2 | * TLSConnectionState 3 | * 4 | * This class encapsulates the read or write state of a TLS connection, 5 | * and implementes the encrypting and hashing of packets. 6 | * Copyright (c) 2007 Henri Torgemane 7 | * 8 | * See LICENSE.txt for full license information. 9 | */ 10 | package com.hurlant.crypto.tls { 11 | import flash.utils.IDataInput; 12 | import flash.utils.ByteArray; 13 | import com.hurlant.crypto.hash.MD5; 14 | import com.hurlant.crypto.hash.MAC; 15 | import com.hurlant.crypto.hash.IHash; 16 | import com.hurlant.crypto.symmetric.ICipher; 17 | import com.hurlant.crypto.symmetric.IVMode; 18 | import com.hurlant.util.Hex; 19 | import com.hurlant.util.ArrayUtil; 20 | 21 | public class SSLConnectionState implements IConnectionState { 22 | 23 | // compression state 24 | 25 | // cipher state 26 | private var bulkCipher:uint; 27 | private var cipherType:uint; 28 | private var CIPHER_key:ByteArray; 29 | private var CIPHER_IV:ByteArray; 30 | private var cipher:ICipher; 31 | private var ivmode:IVMode; 32 | 33 | // mac secret 34 | private var macAlgorithm:uint; 35 | private var MAC_write_secret:ByteArray; 36 | private var mac:MAC; 37 | 38 | // sequence number. uint64 39 | 40 | private var seq_lo:uint = 0x0; 41 | private var seq_hi:uint = 0x0; 42 | 43 | public function SSLConnectionState( 44 | bulkCipher:uint=0, cipherType:uint=0, macAlgorithm:uint=0, 45 | mac_enc:ByteArray=null, key:ByteArray=null, IV:ByteArray=null) { 46 | this.bulkCipher = bulkCipher; 47 | this.cipherType = cipherType; 48 | this.macAlgorithm = macAlgorithm; 49 | MAC_write_secret = mac_enc; 50 | mac = MACs.getMAC(macAlgorithm); 51 | 52 | CIPHER_key = key; 53 | CIPHER_IV = IV; 54 | cipher = BulkCiphers.getCipher(bulkCipher, key, 0x0300); 55 | if (cipher is IVMode) { 56 | ivmode = cipher as IVMode; 57 | ivmode.IV = IV; 58 | } 59 | 60 | } 61 | 62 | public function decrypt(type:uint, length:uint, p:ByteArray):ByteArray { 63 | // decompression is a nop. 64 | 65 | if (cipherType == BulkCiphers.STREAM_CIPHER) { 66 | if (bulkCipher == BulkCiphers.NULL) { 67 | // no-op 68 | } else { 69 | cipher.decrypt(p); 70 | } 71 | } else { 72 | p.position = 0; 73 | // block cipher 74 | if (bulkCipher == BulkCiphers.NULL) { 75 | 76 | } else { 77 | var nextIV:ByteArray = new ByteArray; 78 | nextIV.writeBytes(p, p.length-CIPHER_IV.length, CIPHER_IV.length); 79 | p.position = 0; 80 | cipher.decrypt(p); 81 | 82 | CIPHER_IV = nextIV; 83 | ivmode.IV = nextIV; 84 | } 85 | } 86 | 87 | if (macAlgorithm!=MACs.NULL) { 88 | // there will be CTX delay here as well, 89 | // I should probably optmize the hell out of it 90 | var data:ByteArray = new ByteArray; 91 | var len:uint = p.length - mac.getHashSize(); 92 | data.writeUnsignedInt(seq_hi); 93 | data.writeUnsignedInt(seq_lo); 94 | 95 | data.writeByte(type); 96 | data.writeShort(len); 97 | if (len!=0) { 98 | data.writeBytes(p, 0, len); 99 | } 100 | var mac_enc:ByteArray = mac.compute(MAC_write_secret, data); 101 | // compare "mac" with the last X bytes of p. 102 | var mac_received:ByteArray = new ByteArray; 103 | mac_received.writeBytes(p, len, mac.getHashSize()); 104 | if (ArrayUtil.equals(mac_enc, mac_received)) { 105 | // happy happy joy joy 106 | } else { 107 | throw new TLSError("Bad Mac Data", TLSError.bad_record_mac); 108 | } 109 | p.length = len; 110 | p.position = 0; 111 | } 112 | // increment seq 113 | seq_lo++; 114 | if (seq_lo==0) seq_hi++; 115 | return p; 116 | } 117 | public function encrypt(type:uint, p:ByteArray):ByteArray { 118 | var mac_enc:ByteArray = null; 119 | if (macAlgorithm!=MACs.NULL) { 120 | var data:ByteArray = new ByteArray; 121 | // data.writeUnsignedInt(seq); 122 | 123 | // Sequence 124 | data.writeUnsignedInt(seq_hi); 125 | data.writeUnsignedInt(seq_lo); 126 | 127 | // Type 128 | data.writeByte(type); 129 | 130 | // Length 131 | data.writeShort(p.length); 132 | 133 | // The data 134 | if (p.length!=0) { 135 | data.writeBytes(p); 136 | } 137 | 138 | // trace("data for the MAC: " + Hex.fromArray(data)); 139 | mac_enc = mac.compute(MAC_write_secret, data); 140 | // trace("MAC: " + Hex.fromArray( mac_enc )); 141 | p.position = p.length; 142 | p.writeBytes(mac_enc); 143 | } 144 | 145 | // trace("Record to encrypt: " + Hex.fromArray(p)); 146 | 147 | p.position = 0; 148 | if (cipherType == BulkCiphers.STREAM_CIPHER) { 149 | // stream cipher 150 | if (bulkCipher == BulkCiphers.NULL) { 151 | // no-op 152 | } else { 153 | cipher.encrypt(p); 154 | } 155 | } else { 156 | // block cipher 157 | cipher.encrypt(p); 158 | // adjust IV 159 | var nextIV:ByteArray = new ByteArray; 160 | nextIV.writeBytes(p, p.length-CIPHER_IV.length, CIPHER_IV.length); 161 | CIPHER_IV = nextIV; 162 | ivmode.IV = nextIV; 163 | } 164 | // increment seq 165 | seq_lo++; 166 | if (seq_lo==0) seq_hi++; 167 | return p; 168 | } 169 | 170 | } 171 | } -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tls/CipherSuites.as: -------------------------------------------------------------------------------- 1 | /** 2 | * CipherSuites 3 | * 4 | * An enumeration of cipher-suites available for TLS to use, along with 5 | * their properties, and some convenience methods 6 | * Copyright (c) 2007 Henri Torgemane 7 | * 8 | * See LICENSE.txt for full license information. 9 | */ 10 | package com.hurlant.crypto.tls { 11 | import com.hurlant.crypto.hash.MD5; 12 | import com.hurlant.crypto.hash.SHA1; 13 | 14 | public class CipherSuites { 15 | 16 | 17 | // only the lines marked "ok" are currently implemented. 18 | 19 | // rfc 2246 20 | 21 | public static const TLS_NULL_WITH_NULL_NULL:uint = 0x0000; // ok 22 | public static const TLS_RSA_WITH_NULL_MD5:uint = 0x0001; // ok 23 | public static const TLS_RSA_WITH_NULL_SHA:uint = 0x0002; // ok 24 | public static const TLS_RSA_WITH_RC4_128_MD5:uint = 0x0004; // ok 25 | public static const TLS_RSA_WITH_RC4_128_SHA:uint = 0x0005; // ok 26 | public static const TLS_RSA_WITH_IDEA_CBC_SHA:uint = 0x0007; 27 | public static const TLS_RSA_WITH_DES_CBC_SHA:uint = 0x0009; // ok 28 | public static const TLS_RSA_WITH_3DES_EDE_CBC_SHA:uint = 0x000A; // ok 29 | 30 | public static const TLS_DH_DSS_WITH_DES_CBC_SHA:uint = 0x000C; 31 | public static const TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA:uint = 0x000D; 32 | public static const TLS_DH_RSA_WITH_DES_CBC_SHA:uint = 0x000F; 33 | public static const TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA:uint = 0x0010; 34 | public static const TLS_DHE_DSS_WITH_DES_CBC_SHA:uint = 0x0012; 35 | public static const TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:uint = 0x0013; 36 | public static const TLS_DHE_RSA_WITH_DES_CBC_SHA:uint = 0x0015; 37 | public static const TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:uint = 0x0016; 38 | 39 | public static const TLS_DH_anon_WITH_RC4_128_MD5:uint = 0x0018; 40 | public static const TLS_DH_anon_WITH_DES_CBC_SHA:uint = 0x001A; 41 | public static const TLS_DH_anon_WITH_3DES_EDE_CBC_SHA:uint = 0x001B; 42 | 43 | // rfc3268 44 | 45 | public static const TLS_RSA_WITH_AES_128_CBC_SHA:uint = 0x002F; // ok 46 | public static const TLS_DH_DSS_WITH_AES_128_CBC_SHA:uint = 0x0030; 47 | public static const TLS_DH_RSA_WITH_AES_128_CBC_SHA:uint = 0x0031; 48 | public static const TLS_DHE_DSS_WITH_AES_128_CBC_SHA:uint = 0x0032; 49 | public static const TLS_DHE_RSA_WITH_AES_128_CBC_SHA:uint = 0x0033; 50 | public static const TLS_DH_anon_WITH_AES_128_CBC_SHA:uint = 0x0034; 51 | 52 | public static const TLS_RSA_WITH_AES_256_CBC_SHA:uint = 0x0035; // ok 53 | public static const TLS_DH_DSS_WITH_AES_256_CBC_SHA:uint = 0x0036; 54 | public static const TLS_DH_RSA_WITH_AES_256_CBC_SHA:uint = 0x0037; 55 | public static const TLS_DHE_DSS_WITH_AES_256_CBC_SHA:uint = 0x0038; 56 | public static const TLS_DHE_RSA_WITH_AES_256_CBC_SHA:uint = 0x0039; 57 | public static const TLS_DH_anon_WITH_AES_256_CBC_SHA:uint = 0x003A; 58 | 59 | private static var _props:Array; 60 | 61 | init(); 62 | private static function init():void { 63 | _props = []; 64 | _props[TLS_NULL_WITH_NULL_NULL] = new CipherSuites(BulkCiphers.NULL, MACs.NULL, KeyExchanges.NULL); 65 | _props[TLS_RSA_WITH_NULL_MD5] = new CipherSuites(BulkCiphers.NULL, MACs.MD5, KeyExchanges.RSA); 66 | _props[TLS_RSA_WITH_NULL_SHA] = new CipherSuites(BulkCiphers.NULL, MACs.SHA1, KeyExchanges.RSA); 67 | _props[TLS_RSA_WITH_RC4_128_MD5] = new CipherSuites(BulkCiphers.RC4_128, MACs.MD5, KeyExchanges.RSA); 68 | _props[TLS_RSA_WITH_RC4_128_SHA] = new CipherSuites(BulkCiphers.RC4_128, MACs.SHA1, KeyExchanges.RSA); 69 | _props[TLS_RSA_WITH_DES_CBC_SHA] = new CipherSuites(BulkCiphers.DES_CBC, MACs.SHA1, KeyExchanges.RSA); 70 | _props[TLS_RSA_WITH_3DES_EDE_CBC_SHA] = new CipherSuites(BulkCiphers.DES3_EDE_CBC, MACs.SHA1, KeyExchanges.RSA); 71 | _props[TLS_RSA_WITH_AES_128_CBC_SHA] = new CipherSuites(BulkCiphers.AES_128, MACs.SHA1, KeyExchanges.RSA); 72 | _props[TLS_RSA_WITH_AES_256_CBC_SHA] = new CipherSuites(BulkCiphers.AES_256, MACs.SHA1, KeyExchanges.RSA); 73 | 74 | // ... 75 | // more later 76 | } 77 | 78 | private static function getProp(cipher:uint):CipherSuites { 79 | var p:CipherSuites = _props[cipher]; 80 | if (p==null) { 81 | throw new Error("Unknown cipher "+cipher.toString(16)); 82 | } 83 | return p; 84 | } 85 | public static function getBulkCipher(cipher:uint):uint { 86 | return getProp(cipher).cipher; 87 | } 88 | public static function getMac(cipher:uint):uint { 89 | return getProp(cipher).hash; 90 | } 91 | public static function getKeyExchange(cipher:uint):uint { 92 | return getProp(cipher).key; 93 | } 94 | 95 | public static function getDefaultSuites():Array { 96 | // a list of acceptable ciphers, sorted by preference. 97 | return [ 98 | TLS_RSA_WITH_AES_256_CBC_SHA, 99 | TLS_RSA_WITH_3DES_EDE_CBC_SHA, 100 | TLS_RSA_WITH_AES_128_CBC_SHA, 101 | TLS_RSA_WITH_RC4_128_SHA, 102 | TLS_RSA_WITH_RC4_128_MD5, 103 | TLS_RSA_WITH_DES_CBC_SHA 104 | ]; 105 | } 106 | 107 | public var cipher:uint; 108 | public var hash:uint; 109 | public var key:uint; 110 | 111 | public function CipherSuites(cipher:uint, hash:uint, key:uint) { 112 | this.cipher = cipher; 113 | this.hash = hash; 114 | this.key = key; 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/src/net/gimite/websocket/WebSocketMain.as: -------------------------------------------------------------------------------- 1 | // Copyright: Hiroshi Ichikawa 2 | // License: New BSD License 3 | 4 | package net.gimite.websocket { 5 | 6 | import flash.display.Sprite; 7 | import flash.external.ExternalInterface; 8 | import flash.system.Security; 9 | import flash.utils.setTimeout; 10 | 11 | import mx.utils.URLUtil; 12 | 13 | /** 14 | * Provides JavaScript API of WebSocket. 15 | */ 16 | public class WebSocketMain extends Sprite implements IWebSocketLogger{ 17 | 18 | private var callerUrl:String; 19 | private var debug:Boolean = false; 20 | private var manualPolicyFileLoaded:Boolean = false; 21 | private var webSockets:Array = []; 22 | private var eventQueue:Array = []; 23 | 24 | public function WebSocketMain() { 25 | ExternalInterface.addCallback("setCallerUrl", setCallerUrl); 26 | ExternalInterface.addCallback("setDebug", setDebug); 27 | ExternalInterface.addCallback("create", create); 28 | ExternalInterface.addCallback("send", send); 29 | ExternalInterface.addCallback("close", close); 30 | ExternalInterface.addCallback("loadManualPolicyFile", loadManualPolicyFile); 31 | ExternalInterface.addCallback("receiveEvents", receiveEvents); 32 | ExternalInterface.call("WebSocket.__onFlashInitialized"); 33 | } 34 | 35 | public function setCallerUrl(url:String):void { 36 | callerUrl = url; 37 | } 38 | 39 | public function setDebug(val:Boolean):void { 40 | debug = val; 41 | if (val) { 42 | log("debug enabled"); 43 | } 44 | } 45 | 46 | private function loadDefaultPolicyFile(wsUrl:String):void { 47 | var policyUrl:String = "xmlsocket://" + URLUtil.getServerName(wsUrl) + ":843"; 48 | log("policy file: " + policyUrl); 49 | Security.loadPolicyFile(policyUrl); 50 | } 51 | 52 | public function loadManualPolicyFile(policyUrl:String):void { 53 | log("policy file: " + policyUrl); 54 | Security.loadPolicyFile(policyUrl); 55 | manualPolicyFileLoaded = true; 56 | } 57 | 58 | public function log(message:String):void { 59 | if (debug) { 60 | ExternalInterface.call("WebSocket.__log", encodeURIComponent("[WebSocket] " + message)); 61 | } 62 | } 63 | 64 | public function error(message:String):void { 65 | ExternalInterface.call("WebSocket.__error", encodeURIComponent("[WebSocket] " + message)); 66 | } 67 | 68 | private function parseEvent(event:WebSocketEvent):Object { 69 | var webSocket:WebSocket = event.target as WebSocket; 70 | var eventObj:Object = {}; 71 | eventObj.type = event.type; 72 | eventObj.webSocketId = webSocket.getId(); 73 | eventObj.readyState = webSocket.getReadyState(); 74 | eventObj.protocol = webSocket.getAcceptedProtocol(); 75 | if (event.message !== null) { 76 | eventObj.message = event.message; 77 | } 78 | if (event.wasClean) { 79 | eventObj.wasClean = event.wasClean; 80 | } 81 | if (event.code) { 82 | eventObj.code = event.code; 83 | } 84 | if (event.reason !== null) { 85 | eventObj.reason = event.reason; 86 | } 87 | return eventObj; 88 | } 89 | 90 | public function create( 91 | webSocketId:int, 92 | url:String, protocols:Array, 93 | proxyHost:String = null, proxyPort:int = 0, 94 | headers:String = null):void { 95 | if (!manualPolicyFileLoaded) { 96 | loadDefaultPolicyFile(url); 97 | } 98 | var newSocket:WebSocket = new WebSocket( 99 | webSocketId, url, protocols, getOrigin(), proxyHost, proxyPort, 100 | getCookie(url), headers, this); 101 | newSocket.addEventListener("open", onSocketEvent); 102 | newSocket.addEventListener("close", onSocketEvent); 103 | newSocket.addEventListener("error", onSocketEvent); 104 | newSocket.addEventListener("message", onSocketEvent); 105 | webSockets[webSocketId] = newSocket; 106 | } 107 | 108 | public function send(webSocketId:int, encData:String):int { 109 | var webSocket:WebSocket = webSockets[webSocketId]; 110 | return webSocket.send(encData); 111 | } 112 | 113 | public function close(webSocketId:int):void { 114 | var webSocket:WebSocket = webSockets[webSocketId]; 115 | webSocket.close(); 116 | } 117 | 118 | public function receiveEvents():Object { 119 | var result:Object = eventQueue; 120 | eventQueue = []; 121 | return result; 122 | } 123 | 124 | private function getOrigin():String { 125 | return (URLUtil.getProtocol(this.callerUrl) + "://" + 126 | URLUtil.getServerNameWithPort(this.callerUrl)).toLowerCase(); 127 | } 128 | 129 | private function getCookie(url:String):String { 130 | if (URLUtil.getServerName(url).toLowerCase() == 131 | URLUtil.getServerName(this.callerUrl).toLowerCase()) { 132 | return ExternalInterface.call("function(){return document.cookie}"); 133 | } else { 134 | return ""; 135 | } 136 | } 137 | 138 | /** 139 | * Socket event handler. 140 | */ 141 | public function onSocketEvent(event:WebSocketEvent):void { 142 | var eventObj:Object = parseEvent(event); 143 | eventQueue.push(eventObj); 144 | processEvents(); 145 | } 146 | 147 | /** 148 | * Process our event queue. If javascript is unresponsive, set 149 | * a timeout and try again. 150 | */ 151 | public function processEvents():void { 152 | if (eventQueue.length == 0) return; 153 | if (!ExternalInterface.call("WebSocket.__onFlashEvent")) { 154 | setTimeout(processEvents, 500); 155 | } 156 | } 157 | 158 | } 159 | 160 | } 161 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tls/TLSTest.as: -------------------------------------------------------------------------------- 1 | /** 2 | * TLSTest 3 | * 4 | * A test class for TLS. Not a finished product. 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tls { 10 | import com.hurlant.crypto.cert.X509Certificate; 11 | import com.hurlant.crypto.cert.X509CertificateCollection; 12 | import com.hurlant.util.Hex; 13 | import com.hurlant.util.der.PEM; 14 | 15 | import flash.events.Event; 16 | import flash.events.ProgressEvent; 17 | import flash.net.Socket; 18 | import flash.utils.ByteArray; 19 | import flash.utils.getTimer; 20 | 21 | public class TLSTest { 22 | 23 | 24 | public var myDebugData:String; 25 | 26 | //[Embed(source="/src/host.cert",mimeType="application/octet-stream")] 27 | public var myCert:Class; 28 | //[Embed(source="/src/host.key",mimeType="application/octet-stream")] 29 | public var myKey:Class; 30 | 31 | public function TLSTest(host:String = null, port:int = 0, type:int = 0 ) { 32 | //loopback(); 33 | if (host != null) { 34 | if (type == 0) { // SSL 3.0 35 | connectLoginYahooCom(); 36 | // connectLocalSSL(host, port); 37 | } else { 38 | connectLocalTLS(host, port); 39 | } 40 | } else { 41 | testSocket(); 42 | } 43 | } 44 | 45 | public function connectLoginYahooCom():void { 46 | trace("Connecting test socket"); 47 | var s:Socket = new Socket("esx.bluebearllc.net", 903); 48 | 49 | var clientConfig:TLSConfig = new TLSConfig(TLSEngine.CLIENT, 50 | null, 51 | null, 52 | null, 53 | null, 54 | null, 55 | SSLSecurityParameters.PROTOCOL_VERSION); 56 | 57 | var client:TLSEngine = new TLSEngine(clientConfig, s, s); 58 | // hook some events. 59 | s.addEventListener(ProgressEvent.SOCKET_DATA, client.dataAvailable); 60 | client.addEventListener(ProgressEvent.SOCKET_DATA, function(e:*):void { s.flush(); }); 61 | client.start(); 62 | 63 | } 64 | public function connectLocalTLS(host:String, port:int):void { 65 | var s:Socket = new Socket(host, port); 66 | 67 | var clientConfig:TLSConfig = new TLSConfig(TLSEngine.CLIENT); 68 | 69 | var client:TLSEngine = new TLSEngine(clientConfig, s, s); 70 | // hook some events. 71 | s.addEventListener(ProgressEvent.SOCKET_DATA, client.dataAvailable); 72 | client.addEventListener(ProgressEvent.SOCKET_DATA, function(e:*):void { s.flush(); }); 73 | 74 | client.start(); 75 | 76 | } 77 | public function connectLocalSSL(host:String, port:int):void { 78 | var s:Socket = new Socket(host, port); 79 | 80 | var clientConfig:TLSConfig = new TLSConfig(TLSEngine.CLIENT, 81 | null, 82 | null, 83 | null, 84 | null, 85 | null, 86 | SSLSecurityParameters.PROTOCOL_VERSION); 87 | 88 | var client:TLSEngine = new TLSEngine(clientConfig, s, s); 89 | // hook some events. 90 | s.addEventListener(ProgressEvent.SOCKET_DATA, client.dataAvailable); 91 | client.addEventListener(ProgressEvent.SOCKET_DATA, function(e:*):void { s.flush(); }); 92 | 93 | client.start(); 94 | } 95 | 96 | public function loopback():void { 97 | 98 | var server_write:ByteArray = new ByteArray; 99 | var client_write:ByteArray = new ByteArray; 100 | var server_write_cursor:uint = 0; 101 | var client_write_cursor:uint = 0; 102 | 103 | var clientConfig:TLSConfig = new TLSConfig(TLSEngine.CLIENT, null, null, null, null, null, SSLSecurityParameters.PROTOCOL_VERSION); 104 | var serverConfig:TLSConfig = new TLSConfig(TLSEngine.SERVER, null, null, null, null, null, SSLSecurityParameters.PROTOCOL_VERSION); 105 | 106 | 107 | var cert:ByteArray = new myCert; 108 | var key:ByteArray = new myKey; 109 | serverConfig.setPEMCertificate(cert.readUTFBytes(cert.length), key.readUTFBytes(key.length)); 110 | // tmp, for debugging. currently useless 111 | cert.position = 0; 112 | key.position = 0; 113 | clientConfig.setPEMCertificate(cert.readUTFBytes(cert.length), key.readUTFBytes(key.length)); 114 | // put the server cert in the client's trusted store, to keep things happy. 115 | clientConfig.CAStore = new X509CertificateCollection; 116 | cert.position = 0; 117 | var x509:X509Certificate = new X509Certificate(PEM.readCertIntoArray(cert.readUTFBytes(cert.length))); 118 | clientConfig.CAStore.addCertificate(x509); 119 | 120 | 121 | var server:TLSEngine = new TLSEngine(serverConfig, client_write, server_write); 122 | var client:TLSEngine = new TLSEngine(clientConfig, server_write, client_write); 123 | 124 | server.addEventListener(ProgressEvent.SOCKET_DATA, function(e:*=null):void { 125 | trace("server wrote something!"); 126 | trace(Hex.fromArray(server_write)); 127 | var l:uint = server_write.position; 128 | server_write.position = server_write_cursor; 129 | client.dataAvailable(e); 130 | server_write.position = l; 131 | server_write_cursor = l; 132 | }); 133 | client.addEventListener(ProgressEvent.SOCKET_DATA, function(e:*=null):void { 134 | trace("client wrote something!"); 135 | trace(Hex.fromArray(client_write)); 136 | var l:uint = client_write.position; 137 | client_write.position = client_write_cursor; 138 | server.dataAvailable(e); 139 | client_write.position = l; 140 | client_write_cursor = l; 141 | }); 142 | 143 | server.start(); 144 | client.start(); 145 | } 146 | 147 | public function testSocket():void { 148 | var hosts:Array = [ 149 | "bugs.adobe.com", // apache 150 | "login.yahoo.com", // apache, bigger response 151 | "login.live.com", // IIS-6, chain of 3 certs 152 | "banking.wellsfargo.com", // custom, sends its CA cert along for the ride. 153 | "www.bankofamerica.com" // sun-one, chain of 3 certs 154 | ]; 155 | var i:int =0; 156 | (function next():void { 157 | testHost(hosts[i++], next); 158 | })(); 159 | } 160 | 161 | private function testHost(host:String, next:Function):void { 162 | if (host==null) return; 163 | var t1:int = getTimer(); 164 | 165 | var host:String = host; 166 | var t:TLSSocket = new TLSSocket; 167 | t.connect(host, 4433); 168 | t.writeUTFBytes("GET / HTTP/1.0\nHost: "+host+"\n\n"); 169 | t.addEventListener(Event.CLOSE, function(e:*):void { 170 | var s:String = t.readUTFBytes(t.bytesAvailable); 171 | trace("Response from "+host+": "+s.length+" characters"); 172 | var bytes:ByteArray = new ByteArray(); 173 | t.readBytes(bytes, 0, t.bytesAvailable); 174 | trace(Hex.fromArray(bytes)); 175 | trace("Time used = "+(getTimer()-t1)+"ms"); 176 | next(); 177 | }); 178 | } 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /client/js-ws/flash-src/third-party/com/hurlant/crypto/tests/ECBModeTest.as: -------------------------------------------------------------------------------- 1 | /** 2 | * ECBModeTest 3 | * 4 | * A test class for ECBMode 5 | * Copyright (c) 2007 Henri Torgemane 6 | * 7 | * See LICENSE.txt for full license information. 8 | */ 9 | package com.hurlant.crypto.tests 10 | { 11 | import com.hurlant.crypto.symmetric.AESKey; 12 | import com.hurlant.crypto.symmetric.ECBMode; 13 | import com.hurlant.crypto.symmetric.NullPad; 14 | import com.hurlant.crypto.symmetric.XTeaKey; 15 | import com.hurlant.util.Hex; 16 | 17 | import flash.utils.ByteArray; 18 | 19 | public class ECBModeTest extends TestCase 20 | { 21 | public function ECBModeTest(h:ITestHarness) { 22 | super(h, "ECBMode Test"); 23 | runTest(testAES,"ECB AES Test Vectors"); 24 | runTest(testXTea,"ECB XTea Test Vectors"); 25 | runTest(testECB_AES128,"ECB AES-128 Test Vectors"); 26 | runTest(testECB_AES192,"ECB AES-192 Test Vectors"); 27 | runTest(testECB_AES256,"ECB AES-256 Test Vectors"); 28 | h.endTestCase(); 29 | } 30 | /** 31 | * For now the main goal is to show we can decrypt what we encrypt in this mode. 32 | * Eventually, this should get correlated with some well known vectors. 33 | * yay. found hawt test vectors: http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf 34 | */ 35 | public function testECB_AES128():void { 36 | var key:ByteArray = Hex.toArray("2b7e151628aed2a6abf7158809cf4f3c"); 37 | var pt:ByteArray = Hex.toArray( 38 | "6bc1bee22e409f96e93d7e117393172a" + 39 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 40 | "30c81c46a35ce411e5fbc1191a0a52ef" + 41 | "f69f2445df4f9b17ad2b417be66c3710"); 42 | var ct:ByteArray = Hex.toArray( 43 | "3ad77bb40d7a3660a89ecaf32466ef97" + 44 | "f5d3d58503b9699de785895a96fdbaaf" + 45 | "43b1cd7f598ece23881b00e3ed030688" + 46 | "7b0c785e27e8ad3f8223207104725dd4"); 47 | var ecb:ECBMode = new ECBMode(new AESKey(key), new NullPad); 48 | var src:ByteArray = new ByteArray; 49 | src.writeBytes(pt); 50 | ecb.encrypt(src); 51 | assert("ECB_AES128 test 1", Hex.fromArray(src)==Hex.fromArray(ct)); 52 | ecb.decrypt(src); 53 | assert("ECB_AES128 test 2", Hex.fromArray(src)==Hex.fromArray(pt)); 54 | } 55 | public function testECB_AES192():void { 56 | var key:ByteArray = Hex.toArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"); 57 | var pt:ByteArray = Hex.toArray( 58 | "6bc1bee22e409f96e93d7e117393172a" + 59 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 60 | "30c81c46a35ce411e5fbc1191a0a52ef" + 61 | "f69f2445df4f9b17ad2b417be66c3710"); 62 | var ct:ByteArray = Hex.toArray( 63 | "bd334f1d6e45f25ff712a214571fa5cc" + 64 | "974104846d0ad3ad7734ecb3ecee4eef" + 65 | "ef7afd2270e2e60adce0ba2face6444e" + 66 | "9a4b41ba738d6c72fb16691603c18e0e"); 67 | var ecb:ECBMode = new ECBMode(new AESKey(key), new NullPad); 68 | var src:ByteArray = new ByteArray; 69 | src.writeBytes(pt); 70 | ecb.encrypt(src); 71 | assert("ECB_AES192 test 1", Hex.fromArray(src)==Hex.fromArray(ct)); 72 | ecb.decrypt(src); 73 | assert("ECB_AES192 test 2", Hex.fromArray(src)==Hex.fromArray(pt)); 74 | } 75 | public function testECB_AES256():void { 76 | var key:ByteArray = Hex.toArray( 77 | "603deb1015ca71be2b73aef0857d7781" + 78 | "1f352c073b6108d72d9810a30914dff4"); 79 | var pt:ByteArray = Hex.toArray( 80 | "6bc1bee22e409f96e93d7e117393172a" + 81 | "ae2d8a571e03ac9c9eb76fac45af8e51" + 82 | "30c81c46a35ce411e5fbc1191a0a52ef" + 83 | "f69f2445df4f9b17ad2b417be66c3710"); 84 | var ct:ByteArray = Hex.toArray( 85 | "f3eed1bdb5d2a03c064b5a7e3db181f8" + 86 | "591ccb10d410ed26dc5ba74a31362870" + 87 | "b6ed21b99ca6f4f9f153e7b1beafed1d" + 88 | "23304b7a39f9f3ff067d8d8f9e24ecc7"); 89 | var ecb:ECBMode = new ECBMode(new AESKey(key), new NullPad); 90 | var src:ByteArray = new ByteArray; 91 | src.writeBytes(pt); 92 | ecb.encrypt(src); 93 | assert("ECB_AES256 test 1", Hex.fromArray(src)==Hex.fromArray(ct)); 94 | ecb.decrypt(src); 95 | assert("ECB_AES256 test 2", Hex.fromArray(src)==Hex.fromArray(pt)); 96 | } 97 | // crappier, older testing. keeping around for no good reason. 98 | public function testAES():void { 99 | var keys:Array = [ 100 | "00010203050607080A0B0C0D0F101112", 101 | "14151617191A1B1C1E1F202123242526"]; 102 | var pts:Array = [ 103 | "506812A45F08C889B97F5980038B8359506812A45F08C889B97F5980038B8359506812A45F08C889B97F5980038B8359", 104 | "5C6D71CA30DE8B8B00549984D2EC7D4B5C6D71CA30DE8B8B00549984D2EC7D4B5C6D71CA30DE8B8B00549984D2EC7D4B"]; 105 | var cts:Array = [ 106 | "D8F532538289EF7D06B506A4FD5BE9C9D8F532538289EF7D06B506A4FD5BE9C9D8F532538289EF7D06B506A4FD5BE9C96DE5F607AB7EB8202F3957703B04E8B5", 107 | "59AB30F4D4EE6E4FF9907EF65B1FB68C59AB30F4D4EE6E4FF9907EF65B1FB68C59AB30F4D4EE6E4FF9907EF65B1FB68C2993487785CB1CFDA6BB4F0F345F76C7"]; 108 | 109 | for (var i:uint=0;i