PageRenderTime 70ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/tls/TLSTest.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 180 lines | 134 code | 29 blank | 17 comment | 7 complexity | de5d712c11122dfb969b6afcdab0cace MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. import flash.events.Event;
  15. import flash.events.ProgressEvent;
  16. import flash.net.Socket;
  17. import flash.utils.ByteArray;
  18. import flash.utils.getTimer;
  19. public class TLSTest {
  20. public var myDebugData:String;
  21. //[Embed(source="/src/host.cert",mimeType="application/octet-stream")]
  22. public var myCert:Class;
  23. //[Embed(source="/src/host.key",mimeType="application/octet-stream")]
  24. public var myKey:Class;
  25. public function TLSTest(host:String = null, port:int = 0, type:int = 0 ) {
  26. //loopback();
  27. if (host != null) {
  28. if (type == 0) { // SSL 3.0
  29. connectLoginYahooCom();
  30. // connectLocalSSL(host, port);
  31. } else {
  32. connectLocalTLS(host, port);
  33. }
  34. } else {
  35. testSocket();
  36. }
  37. }
  38. public function connectLoginYahooCom():void {
  39. trace("Connecting test socket");
  40. var s:Socket = new Socket("esx.bluebearllc.net", 903);
  41. var clientConfig:TLSConfig = new TLSConfig(TLSEngine.CLIENT,
  42. null,
  43. null,
  44. null,
  45. null,
  46. null,
  47. SSLSecurityParameters.PROTOCOL_VERSION);
  48. var client:TLSEngine = new TLSEngine(clientConfig, s, s);
  49. // hook some events.
  50. s.addEventListener(ProgressEvent.SOCKET_DATA, client.dataAvailable);
  51. client.addEventListener(ProgressEvent.SOCKET_DATA, function(e:*):void { s.flush(); });
  52. client.start();
  53. }
  54. public function connectLocalTLS(host:String, port:int):void {
  55. var s:Socket = new Socket(host, port);
  56. var clientConfig:TLSConfig = new TLSConfig(TLSEngine.CLIENT);
  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. public function connectLocalSSL(host:String, port:int):void {
  64. var s:Socket = new Socket(host, port);
  65. var clientConfig:TLSConfig = new TLSConfig(TLSEngine.CLIENT,
  66. null,
  67. null,
  68. null,
  69. null,
  70. null,
  71. SSLSecurityParameters.PROTOCOL_VERSION);
  72. var client:TLSEngine = new TLSEngine(clientConfig, s, s);
  73. // hook some events.
  74. s.addEventListener(ProgressEvent.SOCKET_DATA, client.dataAvailable);
  75. client.addEventListener(ProgressEvent.SOCKET_DATA, function(e:*):void { s.flush(); });
  76. client.start();
  77. }
  78. public function loopback():void {
  79. var server_write:ByteArray = new ByteArray;
  80. var client_write:ByteArray = new ByteArray;
  81. var server_write_cursor:uint = 0;
  82. var client_write_cursor:uint = 0;
  83. var clientConfig:TLSConfig = new TLSConfig(TLSEngine.CLIENT, null, null, null, null, null, SSLSecurityParameters.PROTOCOL_VERSION);
  84. var serverConfig:TLSConfig = new TLSConfig(TLSEngine.SERVER, null, null, null, null, null, SSLSecurityParameters.PROTOCOL_VERSION);
  85. var cert:ByteArray = new myCert;
  86. var key:ByteArray = new myKey;
  87. serverConfig.setPEMCertificate(cert.readUTFBytes(cert.length), key.readUTFBytes(key.length));
  88. // tmp, for debugging. currently useless
  89. cert.position = 0;
  90. key.position = 0;
  91. clientConfig.setPEMCertificate(cert.readUTFBytes(cert.length), key.readUTFBytes(key.length));
  92. // put the server cert in the client's trusted store, to keep things happy.
  93. clientConfig.CAStore = new X509CertificateCollection;
  94. cert.position = 0;
  95. var x509:X509Certificate = new X509Certificate(PEM.readCertIntoArray(cert.readUTFBytes(cert.length)));
  96. clientConfig.CAStore.addCertificate(x509);
  97. var server:TLSEngine = new TLSEngine(serverConfig, client_write, server_write);
  98. var client:TLSEngine = new TLSEngine(clientConfig, server_write, client_write);
  99. server.addEventListener(ProgressEvent.SOCKET_DATA, function(e:*=null):void {
  100. trace("server wrote something!");
  101. trace(Hex.fromArray(server_write));
  102. var l:uint = server_write.position;
  103. server_write.position = server_write_cursor;
  104. client.dataAvailable(e);
  105. server_write.position = l;
  106. server_write_cursor = l;
  107. });
  108. client.addEventListener(ProgressEvent.SOCKET_DATA, function(e:*=null):void {
  109. trace("client wrote something!");
  110. trace(Hex.fromArray(client_write));
  111. var l:uint = client_write.position;
  112. client_write.position = client_write_cursor;
  113. server.dataAvailable(e);
  114. client_write.position = l;
  115. client_write_cursor = l;
  116. });
  117. server.start();
  118. client.start();
  119. }
  120. public function testSocket():void {
  121. var hosts:Array = [
  122. "bugs.adobe.com", // apache
  123. "login.yahoo.com", // apache, bigger response
  124. "login.live.com", // IIS-6, chain of 3 certs
  125. "banking.wellsfargo.com", // custom, sends its CA cert along for the ride.
  126. "www.bankofamerica.com" // sun-one, chain of 3 certs
  127. ];
  128. var i:int =0;
  129. (function next():void {
  130. testHost(hosts[i++], next);
  131. })();
  132. }
  133. private function testHost(host:String, next:Function):void {
  134. if (host==null) return;
  135. var t1:int = getTimer();
  136. var host:String = host;
  137. var t:TLSSocket = new TLSSocket;
  138. t.connect(host, 4433);
  139. t.writeUTFBytes("GET / HTTP/1.0\nHost: "+host+"\n\n");
  140. t.addEventListener(Event.CLOSE, function(e:*):void {
  141. var s:String = t.readUTFBytes(t.bytesAvailable);
  142. trace("Response from "+host+": "+s.length+" characters");
  143. var bytes:ByteArray = new ByteArray();
  144. t.readBytes(bytes, 0, t.bytesAvailable);
  145. trace(Hex.fromArray(bytes));
  146. trace("Time used = "+(getTimer()-t1)+"ms");
  147. next();
  148. });
  149. }
  150. }
  151. }