aboutsummaryrefslogtreecommitdiffstats
path: root/signaling-server/node_modules/socket.io/node_modules/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/tls/SSLSecurityParameters.as
blob: 3549461845c4f20af28db302230a8f3606fad1a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
 * TLSSecurityParameters
 * 
 * This class encapsulates all the security parameters that get negotiated
 * during the TLS handshake. It also holds all the key derivation methods.
 * Copyright (c) 2007 Henri Torgemane
 * 
 * See LICENSE.txt for full license information.
 */
package com.hurlant.crypto.tls {
	import com.hurlant.crypto.hash.MD5;
	import com.hurlant.crypto.hash.SHA1;
	import com.hurlant.util.Hex;
	
	import flash.utils.ByteArray;
	
	public class SSLSecurityParameters implements ISecurityParameters {
		
		// COMPRESSION
		public static const COMPRESSION_NULL:uint = 0;
		
		private var entity:uint; // SERVER | CLIENT
		private var bulkCipher:uint; // BULK_CIPHER_*
		private var cipherType:uint; // STREAM_CIPHER | BLOCK_CIPHER
		private var keySize:uint;
		private var keyMaterialLength:uint;
		private var keyBlock:ByteArray;
		private var IVSize:uint;
		private var MAC_length:uint;
		private var macAlgorithm:uint; // MAC_*
		private var hashSize:uint;
		private var compression:uint; // COMPRESSION_NULL
		private var masterSecret:ByteArray; // 48 bytes
		private var clientRandom:ByteArray; // 32 bytes
		private var serverRandom:ByteArray; // 32 bytes
		private var pad_1:ByteArray; // varies
		private var pad_2:ByteArray; // varies
		private var ignoreCNMismatch:Boolean = true;
		private var trustAllCerts:Boolean = false;
		private var trustSelfSigned:Boolean = false;
		public static const PROTOCOL_VERSION:uint = 0x0300;
		
		// not strictly speaking part of this, but yeah.
		public var keyExchange:uint;
		
		public function get version() : uint { 
			return PROTOCOL_VERSION;
		}
		public function SSLSecurityParameters(entity:uint, localCert:ByteArray = null, localKey:ByteArray = null) {
			this.entity = entity;
			reset();
		}
		
		public function reset():void {
			bulkCipher = BulkCiphers.NULL;
			cipherType = BulkCiphers.BLOCK_CIPHER;
			macAlgorithm = MACs.NULL;
			compression = COMPRESSION_NULL;
			masterSecret = null;
		}
		
		public function getBulkCipher():uint {
			return bulkCipher;
		}
		public function getCipherType():uint {
			return cipherType;
		}
		public function getMacAlgorithm():uint {
			return macAlgorithm;
		}
		
		public function setCipher(cipher:uint):void {
			bulkCipher = CipherSuites.getBulkCipher(cipher);
			cipherType = BulkCiphers.getType(bulkCipher);
			keySize = BulkCiphers.getExpandedKeyBytes(bulkCipher);   // 8
			keyMaterialLength = BulkCiphers.getKeyBytes(bulkCipher); // 5
			IVSize = BulkCiphers.getIVSize(bulkCipher);


			keyExchange = CipherSuites.getKeyExchange(cipher);
			
			macAlgorithm = CipherSuites.getMac(cipher);
			hashSize = MACs.getHashSize(macAlgorithm);
			pad_1 = new ByteArray();
			pad_2 = new ByteArray();
			for (var x:int = 0; x < 48; x++) {
				pad_1.writeByte(0x36);
				pad_2.writeByte(0x5c);
			}			
		}
		public function setCompression(algo:uint):void {
			compression = algo;
		}
		
		public function setPreMasterSecret(secret:ByteArray):void {
			/* Warning! Following code may cause madness
				 Tread not here, unless ye be men of valor.
			
			***** Official Prophylactic Comment ******
				(to protect the unwary...this code actually works, that's all you need to know)
			
			This does two things, computes the master secret, and generates the keyBlock
			
			
			To compute the master_secret, the following algorithm is used.
			 for SSL 3, this means
			 master = MD5( premaster + SHA1('A' + premaster + client_random + server_random ) ) +
						MD5( premaster + SHA1('BB' + premaster + client_random + server_random ) ) +
						MD5( premaster + SHA1('CCC' + premaster + client_random + server_random ) )
			*/		
			var tempHashA:ByteArray = new ByteArray(); // temporary hash, gets reused a lot
			var tempHashB:ByteArray = new ByteArray(); // temporary hash, gets reused a lot
			
			var shaHash:ByteArray;
			var mdHash:ByteArray;
			
			var i:int;
			var j:int;
			
			var sha:SHA1 = new SHA1();
			var md:MD5 = new MD5();
					
			var k:ByteArray = new ByteArray();
			
			k.writeBytes(secret);
			k.writeBytes(clientRandom);
			k.writeBytes(serverRandom);
			
			masterSecret = new ByteArray();
			var pad_char:uint = 0x41;
			
			for ( i = 0; i < 3; i++) {
				// SHA portion
				tempHashA.position = 0;
								
				for ( j = 0; j < i + 1; j++) {
					tempHashA.writeByte(pad_char);
				}
				pad_char++;
				
				tempHashA.writeBytes(k);
				shaHash = sha.hash(tempHashA);
				
				// MD5 portion
				tempHashB.position = 0;
				tempHashB.writeBytes(secret); 
				tempHashB.writeBytes(shaHash); 
				mdHash = md.hash(tempHashB);
				
				// copy into my key
				masterSecret.writeBytes(mdHash);
			}
			
			// *************** END MASTER SECRET **************
			
			// More prophylactic comments
			// *************** START KEY BLOCK ****************
			
			// So here, I'm setting up the keyBlock array that I will derive MACs, keys, and IVs from.
			// Rebuild k (hash seed)
			 
			k.position = 0; 
			k.writeBytes(masterSecret);
			k.writeBytes(serverRandom);
			k.writeBytes(clientRandom);
			
			keyBlock = new ByteArray(); 
			
			tempHashA = new ByteArray();
			tempHashB = new ByteArray();
			// now for 16 iterations to get 256 bytes (16 * 16), better to have more than not enough
			pad_char = 0x41;
			for ( i = 0; i < 16; i++) {
				tempHashA.position = 0; 
				
				for ( j = 0; j < i + 1; j++) {
					tempHashA.writeByte(pad_char);
				}
				pad_char++;
				tempHashA.writeBytes(k);
				shaHash = sha.hash(tempHashA);	
				
				tempHashB.position = 0; 
				tempHashB.writeBytes(masterSecret);
				tempHashB.writeBytes(shaHash, 0);
				mdHash = md.hash(tempHashB);
				
				keyBlock.writeBytes(mdHash); 
			}
		}
		
		public function setClientRandom(secret:ByteArray):void {
			clientRandom = secret;
		}
		public function setServerRandom(secret:ByteArray):void {
			serverRandom = secret;
		}
		
		public function get useRSA():Boolean {
			return KeyExchanges.useRSA(keyExchange);
		}
		
		// This is the Finished message
		// if you value your sanity, stay away...far away
		public function computeVerifyData(side:uint, handshakeMessages:ByteArray):ByteArray {
			// for SSL 3.0, this consists of
			// 	finished = md5( masterSecret + pad2 + md5( handshake + sender + masterSecret + pad1 ) ) +
			//			   sha1( masterSecret + pad2 + sha1( handshake + sender + masterSecret + pad1 ) )
			
			// trace("Handshake messages: " + Hex.fromArray(handshakeMessages));
			var sha:SHA1 = new SHA1();
			var md:MD5 = new MD5();
			var k:ByteArray = new ByteArray(); // handshake + sender + masterSecret + pad1
			var j:ByteArray = new ByteArray(); // masterSecret + pad2 + k
			
			var innerKey:ByteArray;
			var outerKey:ByteArray = new ByteArray();
			
			var hashSha:ByteArray;
			var hashMD:ByteArray;
			
			var sideBytes:ByteArray = new ByteArray();
			if (side == TLSEngine.CLIENT) {
			 	sideBytes.writeUnsignedInt(0x434C4E54);
			 } else {
				sideBytes.writeUnsignedInt(0x53525652);
			}
			
			// Do the SHA1 part of the routine first
			masterSecret.position = 0;
			k.writeBytes(handshakeMessages);
			k.writeBytes(sideBytes);
			k.writeBytes(masterSecret);
			k.writeBytes(pad_1, 0, 40); // limited to 40 chars for SHA1
				
			innerKey = sha.hash(k);
			// trace("Inner SHA Key: " + Hex.fromArray(innerKey));
			
			j.writeBytes(masterSecret);
			j.writeBytes(pad_2, 0, 40); // limited to 40 chars for SHA1
			j.writeBytes(innerKey);
			
			hashSha = sha.hash(j);
			// trace("Outer SHA Key: " + Hex.fromArray(hashSha));
			
			// Rebuild k for MD5
			k = new ByteArray();
			
			k.writeBytes(handshakeMessages);
			k.writeBytes(sideBytes);
			k.writeBytes(masterSecret);
			k.writeBytes(pad_1); // Take the whole length of pad_1 & pad_2 for MD5
			
			innerKey = md.hash(k);
			// trace("Inner MD5 Key: " + Hex.fromArray(innerKey));
			
			j = new ByteArray();
			j.writeBytes(masterSecret);
			j.writeBytes(pad_2); // see above re: 48 byte pad
			j.writeBytes(innerKey); 
			
			hashMD = md.hash(j);
			// trace("Outer MD5 Key: " + Hex.fromArray(hashMD));
			
			outerKey.writeBytes(hashMD, 0, hashMD.length);
			outerKey.writeBytes(hashSha, 0, hashSha.length);
			var out:String = Hex.fromArray(outerKey);
			// trace("Finished Message: " + out);
			outerKey.position = 0;
			
			return outerKey;
		
		}
		
		public function computeCertificateVerify( side:uint, handshakeMessages:ByteArray ):ByteArray {
			// TODO: Implement this, but I don't forsee it being necessary at this point in time, since for purposes
			// of the override, I'm only going to use TLS
			return null;  
		}
		
		public function getConnectionStates():Object {
			
			if (masterSecret != null) {
				// so now, I have to derive the actual keys from the keyblock that I generated in setPremasterSecret.
				// for MY purposes, I need RSA-AES 128/256 + SHA
				// so I'm gonna have keylen = 32, minlen = 32, mac_length = 20, iv_length = 16
				// but...I can get this data from the settings returned in the constructor when this object is 
				// It strikes me that TLS does this more elegantly...
				
				var mac_length:int = hashSize as Number;
				var key_length:int = keySize as Number;
				var iv_length:int = IVSize as Number;
				
				var client_write_MAC:ByteArray = new ByteArray();
				var server_write_MAC:ByteArray = new ByteArray();
				var client_write_key:ByteArray = new ByteArray();
				var server_write_key:ByteArray = new ByteArray();
				var client_write_IV:ByteArray = new ByteArray();
				var server_write_IV:ByteArray = new ByteArray();
		
				// Derive the keys from the keyblock
				// Get the MACs first
				keyBlock.position = 0;
				keyBlock.readBytes(client_write_MAC, 0, mac_length);
				keyBlock.readBytes(server_write_MAC, 0, mac_length);
				
				// keyBlock.position is now at MAC_length * 2
				// then get the keys
				keyBlock.readBytes(client_write_key, 0, key_length);
				keyBlock.readBytes(server_write_key, 0, key_length);
				
				// keyBlock.position is now at (MAC_length * 2) + (keySize * 2) 
				// and then the IVs
				keyBlock.readBytes(client_write_IV, 0, iv_length);
				keyBlock.readBytes(server_write_IV, 0, iv_length);
				
				// reset this in case it's needed, for some reason or another, but I doubt it
				keyBlock.position = 0;
				
				var client_write:SSLConnectionState = new SSLConnectionState(
						bulkCipher, cipherType, macAlgorithm,
						client_write_MAC, client_write_key, client_write_IV);
				var server_write:SSLConnectionState = new SSLConnectionState(
						bulkCipher, cipherType, macAlgorithm,
						server_write_MAC, server_write_key, server_write_IV);
				
				if (entity == TLSEngine.CLIENT) {
					return {read:server_write, write:client_write};
				} else {
					return {read:client_write, write:server_write};
				}


			} else {
				return {read:new SSLConnectionState, write:new SSLConnectionState};
			}
		}
		
	}
}