Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / lib / vendor / web-socket-js / flash-src / com / hurlant / crypto / tls / TLSConnectionState.as
1 /**\r
2  * TLSConnectionState\r
3  * \r
4  * This class encapsulates the read or write state of a TLS connection,\r
5  * and implementes the encrypting and hashing of packets. \r
6  * Copyright (c) 2007 Henri Torgemane\r
7  * \r
8  * See LICENSE.txt for full license information.\r
9  */\r
10 package com.hurlant.crypto.tls {\r
11         import flash.utils.IDataInput;\r
12         import flash.utils.ByteArray;\r
13         import com.hurlant.crypto.hash.MD5;\r
14         import com.hurlant.crypto.hash.HMAC;\r
15         import com.hurlant.crypto.hash.IHash;\r
16         import com.hurlant.crypto.symmetric.ICipher;\r
17         import com.hurlant.crypto.symmetric.IVMode;\r
18         import com.hurlant.util.Hex;\r
19         import com.hurlant.util.ArrayUtil;\r
20         \r
21         public class TLSConnectionState implements IConnectionState {\r
22 \r
23 \r
24                 // compression state\r
25                 \r
26                 // cipher state\r
27                 private var bulkCipher:uint;\r
28                 private var cipherType:uint;\r
29                 private var CIPHER_key:ByteArray;\r
30                 private var CIPHER_IV:ByteArray;\r
31                 private var cipher:ICipher;\r
32                 private var ivmode:IVMode;\r
33                 \r
34                 // mac secret\r
35                 private var macAlgorithm:uint;\r
36                 private var MAC_write_secret:ByteArray;\r
37                 private var hmac:HMAC;\r
38                 \r
39                 // sequence number. uint64\r
40                 private var seq_lo:uint;\r
41                 private var seq_hi:uint;\r
42                 \r
43 \r
44 \r
45                 public function TLSConnectionState(\r
46                                 bulkCipher:uint=0, cipherType:uint=0, macAlgorithm:uint=0,\r
47                                 mac:ByteArray=null, key:ByteArray=null, IV:ByteArray=null) {\r
48                         this.bulkCipher = bulkCipher;\r
49                         this.cipherType = cipherType;\r
50                         this.macAlgorithm = macAlgorithm;\r
51                         MAC_write_secret = mac;\r
52                         hmac = MACs.getHMAC(macAlgorithm);\r
53                         CIPHER_key = key;\r
54                         CIPHER_IV = IV;\r
55                         cipher = BulkCiphers.getCipher(bulkCipher, key, 0x0301);\r
56                         if (cipher is IVMode) {\r
57                                 ivmode = cipher as IVMode;\r
58                                 ivmode.IV = IV;\r
59                         }\r
60                 }\r
61                 \r
62                 public function decrypt(type:uint, length:uint, p:ByteArray):ByteArray {\r
63                         // decompression is a nop.\r
64                         \r
65                         if (cipherType == BulkCiphers.STREAM_CIPHER) {\r
66                                 if (bulkCipher == BulkCiphers.NULL) {\r
67                                         // no-op\r
68                                 } else {\r
69                                         cipher.decrypt(p);\r
70                                 }\r
71                         } else {\r
72                                 // block cipher\r
73                                 var nextIV:ByteArray = new ByteArray;\r
74                                 nextIV.writeBytes(p, p.length-CIPHER_IV.length, CIPHER_IV.length);\r
75                                 \r
76                                 cipher.decrypt(p);\r
77 \r
78 \r
79                                 CIPHER_IV = nextIV;\r
80                                 ivmode.IV = nextIV;\r
81                         }\r
82                         if (macAlgorithm!=MACs.NULL) {\r
83                                 var data:ByteArray = new ByteArray;\r
84                                 var len:uint = p.length - hmac.getHashSize();\r
85                                 data.writeUnsignedInt(seq_hi);\r
86                                 data.writeUnsignedInt(seq_lo);\r
87                                 data.writeByte(type);\r
88                                 data.writeShort(TLSSecurityParameters.PROTOCOL_VERSION);\r
89                                 data.writeShort(len);\r
90                                 if (len!=0) {\r
91                                         data.writeBytes(p, 0, len);\r
92                                 }\r
93                                 var mac:ByteArray = hmac.compute(MAC_write_secret, data);\r
94                                 // compare "mac" with the last X bytes of p.\r
95                                 var mac_received:ByteArray = new ByteArray;\r
96                                 mac_received.writeBytes(p, len, hmac.getHashSize());\r
97                                 if (ArrayUtil.equals(mac, mac_received)) {\r
98                                         // happy happy joy joy\r
99                                 } else {\r
100                                         throw new TLSError("Bad Mac Data", TLSError.bad_record_mac);\r
101                                 }\r
102                                 p.length = len;\r
103                                 p.position = 0;\r
104                         }\r
105                         // increment seq\r
106                         seq_lo++;\r
107                         if (seq_lo==0) seq_hi++;\r
108                         return p;\r
109                 }\r
110                 public function encrypt(type:uint, p:ByteArray):ByteArray {\r
111                         var mac:ByteArray = null;\r
112                         if (macAlgorithm!=MACs.NULL) {\r
113                                 var data:ByteArray = new ByteArray;\r
114                                 data.writeUnsignedInt(seq_hi);\r
115                                 data.writeUnsignedInt(seq_lo);\r
116                                 data.writeByte(type);\r
117                                 data.writeShort(TLSSecurityParameters.PROTOCOL_VERSION);\r
118                                 data.writeShort(p.length);\r
119                                 if (p.length!=0) {\r
120                                         data.writeBytes(p, 0, p.length);\r
121                                 }\r
122                                 mac = hmac.compute(MAC_write_secret, data);\r
123                                 p.position = p.length;\r
124                                 p.writeBytes(mac);\r
125                         }\r
126                         p.position = 0;\r
127                         if (cipherType == BulkCiphers.STREAM_CIPHER) {\r
128                                 // stream cipher\r
129                                 if (bulkCipher == BulkCiphers.NULL) {\r
130                                         // no-op\r
131                                 } else {\r
132                                         cipher.encrypt(p);\r
133                                 }\r
134                         } else {\r
135                                 // block cipher\r
136                                 cipher.encrypt(p);\r
137                                 // adjust IV\r
138                                 var nextIV:ByteArray = new ByteArray;\r
139                                 nextIV.writeBytes(p, p.length-CIPHER_IV.length, CIPHER_IV.length);\r
140                                 CIPHER_IV = nextIV;\r
141                                 ivmode.IV = nextIV;\r
142                         }\r
143                         // increment seq\r
144                         seq_lo++;\r
145                         if (seq_lo==0) seq_hi++;\r
146                         // compression is a nop.\r
147                         return p;\r
148                 }\r
149                 \r
150         }\r
151 }