Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CipherSuite.cs « Mono.Security.Protocol.Tls « Mono.Security « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b3926ee28db0577351fa7c3e02c703643c2fc627 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* Transport Security Layer (TLS)
 * Copyright (c) 2003 Carlos Guzmán Álvarez
 * 
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

using Mono.Security;
using Mono.Security.Cryptography;
using Mono.Security.X509;

namespace Mono.Security.Protocol.Tls
{
	internal abstract class CipherSuite
	{
		#region FIELDS

		private short				code;
		private string				name;
		private string				algName;
		private string				hashName;
		private bool				isExportable;
		private CipherMode			cipherMode;
		private byte				keyMaterialSize;
		private byte				expandedKeyMaterialSize;
		private short				effectiveKeyBits;
		private byte				ivSize;
		private byte				blockSize;
		private TlsSessionContext	context;
		private SymmetricAlgorithm	encryptionAlgorithm;
		private ICryptoTransform	encryptionCipher;
		private SymmetricAlgorithm	decryptionAlgorithm;
		private ICryptoTransform	decryptionCipher;
		private KeyedHashAlgorithm	clientHMAC;
		private KeyedHashAlgorithm	serverHMAC;
			
		#endregion

		#region PROTECTED_PROPERTIES

		protected ICryptoTransform EncryptionCipher
		{
			get { return encryptionCipher; }
		}

		protected ICryptoTransform DecryptionCipher
		{
			get { return decryptionCipher; }
		}

		protected KeyedHashAlgorithm ClientHMAC
		{
			get { return clientHMAC; }
		}
		
		protected KeyedHashAlgorithm ServerHMAC
		{
			get { return serverHMAC; }
		}

		#endregion

		#region PROPERTIES

		public short Code
		{
			get { return code; }
		}

		public string Name
		{
			get { return name; }
		}

		public bool IsExportable
		{
			get { return isExportable; }
		}

		public CipherMode CipherMode
		{
			get { return cipherMode; }
		}

		public int HashSize
		{
			get { return (int)(hashName == "MD5" ? 16 : 20); }
		}

		public byte	KeyMaterialSize
		{
			get { return keyMaterialSize; }
		}

		public int KeyBlockSize
		{
			get 
			{
				return keyMaterialSize*2 + HashSize*2 + ivSize*2;
			}
		}

		public byte	ExpandedKeyMaterialSize
		{
			get { return expandedKeyMaterialSize; }
		}

		public byte	EffectiveKeyBits
		{
			get { return EffectiveKeyBits; }
		}
		
		public byte IvSize
		{
			get { return ivSize; }
		}

		public byte	BlockSize
		{
			get { return blockSize; }
		}

		public string HashName
		{
			get { return hashName; }
		}

		public TlsSessionContext Context
		{
			get { return context; }
			set { context = value; }
		}

		#endregion

		#region CONSTRUCTORS
		
		public CipherSuite(short code, string name, string algName, string hashName, bool exportable, bool blockMode, byte keyMaterialSize, byte expandedKeyMaterialSize, short effectiveKeyBytes, byte ivSize, byte blockSize)
		{
			this.code						= code;
			this.name						= name;
			this.algName					= algName;
			this.hashName					= hashName;
			this.isExportable				= exportable;
			if (blockMode)
			{
				this.cipherMode				= CipherMode.CBC;
			}
			this.keyMaterialSize			= keyMaterialSize;
			this.expandedKeyMaterialSize	= expandedKeyMaterialSize;
			this.effectiveKeyBits			= effectiveKeyBits;
			this.ivSize						= ivSize;
			this.blockSize					= blockSize;
		}

		#endregion

		#region METHODS

		public void InitializeCipher()
		{
			createEncryptionCipher();
			createDecryptionCipher();
		}

		public RSA CreateRSA()
		{
			RSA rsa;
			if (this.Context.ServerSettings.ServerKeyExchange)
			{
				rsa = new RSACryptoServiceProvider();
				rsa.ImportParameters(this.Context.ServerSettings.RsaParameters);
			}
			else
			{
				rsa = this.Context.ServerSettings.ServerCertificates[0].RSA;
			}
	
			return rsa;
		}

		public RSACryptoServiceProvider CreateRSA(RSAParameters rsaParams)
		{			
			// BUG: MS BCL 1.0 can't import a key which 
			// isn't the same size as the one present in
			// the container.
			int keySize = (rsaParams.Modulus.Length << 3);
			RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize);
			rsa.ImportParameters(rsaParams);

			return rsa;
		}

		public void UpdateClientCipherIV(byte[] iv)
		{
			if (cipherMode == CipherMode.CBC)
			{
				// Set the new IV
				encryptionAlgorithm.IV	= iv;
			
				// Create encryption cipher with the new IV
				encryptionCipher = encryptionAlgorithm.CreateEncryptor();
			}
		}

		public void UpdateServerCipherIV(byte[] iv)
		{
			if (cipherMode == CipherMode.CBC)
			{
				// Set the new IV
				decryptionAlgorithm.IV	= iv;
			
				// Create encryption cipher with the new IV
				decryptionCipher = decryptionAlgorithm.CreateDecryptor();
			}
		}

		public byte[] EncryptRecord(byte[] fragment, byte[] mac)
		{
			// Encryption ( fragment + mac [+ padding + padding_length] )
			MemoryStream ms = new MemoryStream();
			CryptoStream cs = new CryptoStream(ms, this.EncryptionCipher, CryptoStreamMode.Write);

			cs.Write(fragment, 0, fragment.Length);
			cs.Write(mac, 0, mac.Length);
			if (this.CipherMode == CipherMode.CBC)
			{
				// Calculate padding_length
				int fragmentLength	= fragment.Length + mac.Length + 1;
				int paddingLength	= this.blockSize - fragmentLength % this.blockSize;
				if (paddingLength == this.blockSize)
				{
					paddingLength = 0;
				}

				// Write padding length byte
				for (int i = 0; i < (paddingLength + 1); i++)
				{
					cs.WriteByte((byte)paddingLength);
				}
			}
			// cs.FlushFinalBlock();
			cs.Close();			

			return ms.ToArray();
		}

		public void DecryptRecord(byte[] fragment, ref byte[] dcrFragment, ref byte[] dcrMAC)
		{
			int	fragmentSize	= 0;
			int paddingLength	= 0;

			// Decrypt message fragment ( fragment + mac [+ padding + padding_length] )
			byte[] buffer = new byte[fragment.Length];
			this.DecryptionCipher.TransformBlock(fragment, 0, fragment.Length, buffer, 0);

			// Calculate fragment size
			if (this.CipherMode == CipherMode.CBC)
			{
				// Calculate padding_length
				paddingLength = buffer[buffer.Length - 1];

				/* Review this that is valid way for TLS1 but not for SSL3
				for (int i = (buffer.Length - 1); i > (buffer.Length - (paddingLength + 1)); i--)
				{
					if (buffer[i] != paddingLength)
					{
						paddingLength = 0;
						break;
					}
				}
				*/

				fragmentSize = (buffer.Length - (paddingLength + 1)) - HashSize;
			}
			else
			{
				fragmentSize = buffer.Length - HashSize;
			}

			dcrFragment = new byte[fragmentSize];
			dcrMAC		= new byte[HashSize];

			Buffer.BlockCopy(buffer, 0, dcrFragment, 0, dcrFragment.Length);
			Buffer.BlockCopy(buffer, dcrFragment.Length, dcrMAC, 0, dcrMAC.Length);
		}

		#endregion

		#region ABSTRACT_METHODS

		public abstract byte[] ComputeClientRecordMAC(TlsContentType contentType, byte[] fragment);

		public abstract byte[] ComputeServerRecordMAC(TlsContentType contentType, byte[] fragment);

		public abstract void ComputeMasterSecret(byte[] preMasterSecret);

		public abstract void ComputeKeys();

		#endregion

		#region KEY_GENERATION_METODS

		public byte[] CreatePremasterSecret()
		{
			TlsStream stream = new TlsStream();

			// Write protocol version
			stream.Write((short)this.Context.Protocol);

			// Generate random bytes
			stream.Write(this.context.GetSecureRandomBytes(46));

			byte[] preMasterSecret = stream.ToArray();

			stream.Reset();

			return preMasterSecret;
		}

		public byte[] PRF(byte[] secret, string label, byte[] data, int length)
		{
			MD5CryptoServiceProvider	md5	= new MD5CryptoServiceProvider();
			SHA1CryptoServiceProvider	sha1 = new SHA1CryptoServiceProvider();

			int secretLen = secret.Length / 2;

			// Seed
			TlsStream seedStream = new TlsStream();
			seedStream.Write(Encoding.ASCII.GetBytes(label));
			seedStream.Write(data);
			byte[] seed = seedStream.ToArray();
			seedStream.Reset();

			// Secret 1
			byte[] secret1 = new byte[secretLen];
			System.Array.Copy(secret, 0, secret1, 0, secretLen);

			// Secret2
			byte[] secret2 = new byte[secretLen];
			System.Array.Copy(secret, secretLen, secret2, 0, secretLen);

			// Secret 1 processing
			byte[] p_md5 = Expand("MD5", secret1, seed, length);

			// Secret 2 processing
			byte[] p_sha = Expand("SHA1", secret2, seed, length);

			// Perfor XOR of both results
			byte[] masterSecret = new byte[length];
			for (int i = 0; i < masterSecret.Length; i++)
			{
				masterSecret[i] = (byte)(p_md5[i] ^ p_sha[i]);
			}

			return masterSecret;
		}
		
		public byte[] Expand(string hashName, byte[] secret, byte[] seed, int length)
		{
			int hashLength	= hashName == "MD5" ? 16 : 20;
			int	iterations	= (int)(length / hashLength);
			if ((length % hashLength) > 0)
			{
				iterations++;
			}
			
			HMAC		hmac	= new HMAC(hashName, secret);
			TlsStream	resMacs	= new TlsStream();
			
			byte[][] hmacs = new byte[iterations + 1][];
			hmacs[0] = seed;
			for (int i = 1; i <= iterations; i++)
			{				
				TlsStream hcseed = new TlsStream();
				hmac.TransformFinalBlock(hmacs[i-1], 0, hmacs[i-1].Length);
				hmacs[i] = hmac.Hash;
				hcseed.Write(hmacs[i]);
				hcseed.Write(seed);
				hmac.TransformFinalBlock(hcseed.ToArray(), 0, (int)hcseed.Length);
				resMacs.Write(hmac.Hash);
				hcseed.Reset();
			}

			byte[] res = new byte[length];
			
			System.Array.Copy(resMacs.ToArray(), 0, res, 0, res.Length);

			resMacs.Reset();

			return res;
		}

		#endregion

		#region PRIVATE_METHODS

		// This code is from Mono.Security.X509Certificate class.
		private byte[] getUnsignedBigInteger(byte[] integer) 
		{
			if (integer[0] == 0x00) 
			{
				// this first byte is added so we're sure it's an unsigned integer
				// however we can't feed it into RSAParameters or DSAParameters
				int		length	 = integer.Length - 1;
				byte[]	uinteger = new byte[length];				
				Array.Copy(integer, 1, uinteger, 0, length);

				return uinteger;
			}
			else
			{
				return integer;
			}
		}

		private void createEncryptionCipher()
		{
			// Create and configure the symmetric algorithm
			switch (this.algName)
			{
				case "RC4":
					encryptionAlgorithm = new ARC4Managed();
					break;

				default:
					encryptionAlgorithm = SymmetricAlgorithm.Create(algName);
					break;
			}

			// If it's a block cipher
			if (cipherMode == CipherMode.CBC)
			{
				// Configure encrypt algorithm
				encryptionAlgorithm.Mode		= this.cipherMode;
				encryptionAlgorithm.Padding		= PaddingMode.None;
				encryptionAlgorithm.KeySize		= this.keyMaterialSize * 8;
				encryptionAlgorithm.BlockSize	= this.blockSize * 8;
			}

			// Set the key and IV for the algorithm
			encryptionAlgorithm.Key = context.ClientWriteKey;
			encryptionAlgorithm.IV	= context.ClientWriteIV;
			
			// Create encryption cipher
			encryptionCipher = encryptionAlgorithm.CreateEncryptor();

			// Create the HMAC algorithm for the client
			clientHMAC = new HMAC(hashName, context.ClientWriteMAC);
		}

		private void createDecryptionCipher()
		{
			// Create and configure the symmetric algorithm
			switch (this.algName)
			{
				case "RC4":
					decryptionAlgorithm = new ARC4Managed();
					break;

				default:
					decryptionAlgorithm = SymmetricAlgorithm.Create(algName);
					break;
			}

			// If it's a block cipher
			if (cipherMode == CipherMode.CBC)
			{
				// Configure encrypt algorithm
				decryptionAlgorithm.Mode		= this.cipherMode;
				decryptionAlgorithm.Padding		= PaddingMode.None;
				decryptionAlgorithm.KeySize		= this.keyMaterialSize * 8;
				decryptionAlgorithm.BlockSize	= this.blockSize * 8;
			}

			// Set the key and IV for the algorithm
			decryptionAlgorithm.Key = context.ServerWriteKey;
			decryptionAlgorithm.IV	= context.ServerWriteIV;

			// Create decryption cipher			
			decryptionCipher = decryptionAlgorithm.CreateDecryptor();

			// Create the HMAC algorithm for the server
			serverHMAC = new HMAC(hashName, context.ServerWriteMAC);
		}

		#endregion
	}
}