1 /* Transport Security Layer (TLS)
\r
2 * Copyright (c) 2003 Carlos Guzmán Álvarez
\r
4 * Permission is hereby granted, free of charge, to any person
\r
5 * obtaining a copy of this software and associated documentation
\r
6 * files (the "Software"), to deal in the Software without restriction,
\r
7 * including without limitation the rights to use, copy, modify, merge,
\r
8 * publish, distribute, sublicense, and/or sell copies of the Software,
\r
9 * and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included
\r
13 * in all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
\r
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
\r
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
\r
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
22 * DEALINGS IN THE SOFTWARE.
\r
28 using System.Security.Cryptography;
\r
29 using System.Security.Cryptography.X509Certificates;
\r
31 using Mono.Security;
\r
32 using Mono.Security.Cryptography;
\r
34 namespace Mono.Security.Protocol.Tls
\r
36 internal class TlsCipherSuite : CipherSuite
\r
38 #region CONSTRUCTORS
\r
40 public TlsCipherSuite(
\r
41 short code, string name, CipherAlgorithmType cipherAlgorithmType,
\r
42 HashAlgorithmType hashAlgorithmType, ExchangeAlgorithmType exchangeAlgorithmType,
\r
43 bool exportable, bool blockMode, byte keyMaterialSize,
\r
44 byte expandedKeyMaterialSize, short effectiveKeyBytes,
\r
45 byte ivSize, byte blockSize) :
\r
46 base(code, name, cipherAlgorithmType, hashAlgorithmType,
\r
47 exchangeAlgorithmType, exportable, blockMode, keyMaterialSize,
\r
48 expandedKeyMaterialSize, effectiveKeyBytes, ivSize, blockSize)
\r
54 #region MAC_GENERATION_METHOD
\r
56 public override byte[] ComputeServerRecordMAC(TlsContentType contentType, byte[] fragment)
\r
58 TlsStream data = new TlsStream();
\r
59 byte[] result = null;
\r
61 data.Write(this.Context.ReadSequenceNumber);
\r
62 data.Write((byte)contentType);
\r
63 data.Write((short)this.Context.Protocol);
\r
64 data.Write((short)fragment.Length);
\r
65 data.Write(fragment);
\r
67 result = this.ServerHMAC.ComputeHash(data.ToArray());
\r
74 public override byte[] ComputeClientRecordMAC(TlsContentType contentType, byte[] fragment)
\r
76 TlsStream data = new TlsStream();
\r
77 byte[] result = null;
\r
79 data.Write(this.Context.WriteSequenceNumber);
\r
80 data.Write((byte)contentType);
\r
81 data.Write((short)this.Context.Protocol);
\r
82 data.Write((short)fragment.Length);
\r
83 data.Write(fragment);
\r
85 result = this.ClientHMAC.ComputeHash(data.ToArray());
\r
94 #region KEY_GENERATION_METODS
\r
96 public override void ComputeMasterSecret(byte[] preMasterSecret)
\r
98 // Create master secret
\r
99 this.Context.MasterSecret = new byte[preMasterSecret.Length];
\r
100 this.Context.MasterSecret = this.PRF(
\r
101 preMasterSecret, "master secret", this.Context.RandomCS, 48);
\r
104 public override void ComputeKeys()
\r
107 TlsStream keyBlock = new TlsStream(
\r
109 this.Context.MasterSecret,
\r
111 this.Context.RandomSC,
\r
112 this.KeyBlockSize));
\r
114 this.Context.ClientWriteMAC = keyBlock.ReadBytes(this.HashSize);
\r
115 this.Context.ServerWriteMAC = keyBlock.ReadBytes(this.HashSize);
\r
116 this.Context.ClientWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize);
\r
117 this.Context.ServerWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize);
\r
119 if (!this.IsExportable)
\r
121 if (this.IvSize != 0)
\r
123 this.Context.ClientWriteIV = keyBlock.ReadBytes(this.IvSize);
\r
124 this.Context.ServerWriteIV = keyBlock.ReadBytes(this.IvSize);
\r
128 this.Context.ClientWriteIV = new byte[0];
\r
129 this.Context.ServerWriteIV = new byte[0];
\r
134 // Generate final write keys
\r
135 byte[] finalClientWriteKey = PRF(this.Context.ClientWriteKey, "client write key", this.Context.RandomCS, this.KeyMaterialSize);
\r
136 byte[] finalServerWriteKey = PRF(this.Context.ServerWriteKey, "server write key", this.Context.RandomCS, this.KeyMaterialSize);
\r
138 this.Context.ClientWriteKey = finalClientWriteKey;
\r
139 this.Context.ServerWriteKey = finalServerWriteKey;
\r
141 // Generate IV block
\r
142 byte[] ivBlock = PRF(new byte[]{}, "IV block", this.Context.RandomCS, this.IvSize*2);
\r
144 // Generate IV keys
\r
145 this.Context.ClientWriteIV = new byte[this.IvSize];
\r
146 System.Array.Copy(ivBlock, 0, this.Context.ClientWriteIV, 0, this.Context.ClientWriteIV.Length);
\r
148 this.Context.ServerWriteIV = new byte[this.IvSize];
\r
149 System.Array.Copy(ivBlock, this.IvSize, this.Context.ServerWriteIV, 0, this.Context.ServerWriteIV.Length);
\r
152 // Clear no more needed data
\r