PageRenderTime 37ms CodeModel.GetById 23ms 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/TLSConnectionState.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 151 lines | 111 code | 15 blank | 25 comment | 21 complexity | fb6374e292af96fc7348847809405b94 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. public class TLSConnectionState implements IConnectionState {
  21. // compression state
  22. // cipher state
  23. private var bulkCipher:uint;
  24. private var cipherType:uint;
  25. private var CIPHER_key:ByteArray;
  26. private var CIPHER_IV:ByteArray;
  27. private var cipher:ICipher;
  28. private var ivmode:IVMode;
  29. // mac secret
  30. private var macAlgorithm:uint;
  31. private var MAC_write_secret:ByteArray;
  32. private var hmac:HMAC;
  33. // sequence number. uint64
  34. private var seq_lo:uint;
  35. private var seq_hi:uint;
  36. public function TLSConnectionState(
  37. bulkCipher:uint=0, cipherType:uint=0, macAlgorithm:uint=0,
  38. mac:ByteArray=null, key:ByteArray=null, IV:ByteArray=null) {
  39. this.bulkCipher = bulkCipher;
  40. this.cipherType = cipherType;
  41. this.macAlgorithm = macAlgorithm;
  42. MAC_write_secret = mac;
  43. hmac = MACs.getHMAC(macAlgorithm);
  44. CIPHER_key = key;
  45. CIPHER_IV = IV;
  46. cipher = BulkCiphers.getCipher(bulkCipher, key, 0x0301);
  47. if (cipher is IVMode) {
  48. ivmode = cipher as IVMode;
  49. ivmode.IV = IV;
  50. }
  51. }
  52. public function decrypt(type:uint, length:uint, p:ByteArray):ByteArray {
  53. // decompression is a nop.
  54. if (cipherType == BulkCiphers.STREAM_CIPHER) {
  55. if (bulkCipher == BulkCiphers.NULL) {
  56. // no-op
  57. } else {
  58. cipher.decrypt(p);
  59. }
  60. } else {
  61. // block cipher
  62. var nextIV:ByteArray = new ByteArray;
  63. nextIV.writeBytes(p, p.length-CIPHER_IV.length, CIPHER_IV.length);
  64. cipher.decrypt(p);
  65. CIPHER_IV = nextIV;
  66. ivmode.IV = nextIV;
  67. }
  68. if (macAlgorithm!=MACs.NULL) {
  69. var data:ByteArray = new ByteArray;
  70. var len:uint = p.length - hmac.getHashSize();
  71. data.writeUnsignedInt(seq_hi);
  72. data.writeUnsignedInt(seq_lo);
  73. data.writeByte(type);
  74. data.writeShort(TLSSecurityParameters.PROTOCOL_VERSION);
  75. data.writeShort(len);
  76. if (len!=0) {
  77. data.writeBytes(p, 0, len);
  78. }
  79. var mac:ByteArray = hmac.compute(MAC_write_secret, data);
  80. // compare "mac" with the last X bytes of p.
  81. var mac_received:ByteArray = new ByteArray;
  82. mac_received.writeBytes(p, len, hmac.getHashSize());
  83. if (ArrayUtil.equals(mac, mac_received)) {
  84. // happy happy joy joy
  85. } else {
  86. throw new TLSError("Bad Mac Data", TLSError.bad_record_mac);
  87. }
  88. p.length = len;
  89. p.position = 0;
  90. }
  91. // increment seq
  92. seq_lo++;
  93. if (seq_lo==0) seq_hi++;
  94. return p;
  95. }
  96. public function encrypt(type:uint, p:ByteArray):ByteArray {
  97. var mac:ByteArray = null;
  98. if (macAlgorithm!=MACs.NULL) {
  99. var data:ByteArray = new ByteArray;
  100. data.writeUnsignedInt(seq_hi);
  101. data.writeUnsignedInt(seq_lo);
  102. data.writeByte(type);
  103. data.writeShort(TLSSecurityParameters.PROTOCOL_VERSION);
  104. data.writeShort(p.length);
  105. if (p.length!=0) {
  106. data.writeBytes(p, 0, p.length);
  107. }
  108. mac = hmac.compute(MAC_write_secret, data);
  109. p.position = p.length;
  110. p.writeBytes(mac);
  111. }
  112. p.position = 0;
  113. if (cipherType == BulkCiphers.STREAM_CIPHER) {
  114. // stream cipher
  115. if (bulkCipher == BulkCiphers.NULL) {
  116. // no-op
  117. } else {
  118. cipher.encrypt(p);
  119. }
  120. } else {
  121. // block cipher
  122. cipher.encrypt(p);
  123. // adjust IV
  124. var nextIV:ByteArray = new ByteArray;
  125. nextIV.writeBytes(p, p.length-CIPHER_IV.length, CIPHER_IV.length);
  126. CIPHER_IV = nextIV;
  127. ivmode.IV = nextIV;
  128. }
  129. // increment seq
  130. seq_lo++;
  131. if (seq_lo==0) seq_hi++;
  132. // compression is a nop.
  133. return p;
  134. }
  135. }
  136. }