1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
3 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 using System.Security.Cryptography;
29 namespace Mono.Security.Protocol.Tls
31 internal class TlsCipherSuite : CipherSuite
33 private const int MacHeaderLength = 13;
34 private byte[] header;
35 private object headerLock = new object ();
39 public TlsCipherSuite(
40 short code, string name, CipherAlgorithmType cipherAlgorithmType,
41 HashAlgorithmType hashAlgorithmType, ExchangeAlgorithmType exchangeAlgorithmType,
42 bool exportable, bool blockMode, byte keyMaterialSize,
43 byte expandedKeyMaterialSize, short effectiveKeyBytes,
44 byte ivSize, byte blockSize)
45 :base(code, name, cipherAlgorithmType, hashAlgorithmType,
46 exchangeAlgorithmType, exportable, blockMode, keyMaterialSize,
47 expandedKeyMaterialSize, effectiveKeyBytes, ivSize, blockSize)
53 #region MAC Generation Methods
55 public override byte[] ComputeServerRecordMAC(ContentType contentType, byte[] fragment)
59 header = new byte [MacHeaderLength];
61 ulong seqnum = (Context is ClientContext) ? Context.ReadSequenceNumber : Context.WriteSequenceNumber;
62 Write (header, 0, seqnum);
63 header [8] = (byte)contentType;
64 Write (header, 9, this.Context.Protocol);
65 Write (header, 11, (short)fragment.Length);
67 HashAlgorithm mac = this.ServerHMAC;
68 mac.TransformBlock (header, 0, header.Length, header, 0);
69 mac.TransformBlock (fragment, 0, fragment.Length, fragment, 0);
70 // hack, else the method will allocate a new buffer of the same length (negative half the optimization)
71 mac.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0);
76 public override byte[] ComputeClientRecordMAC(ContentType contentType, byte[] fragment)
80 header = new byte [MacHeaderLength];
82 ulong seqnum = (Context is ClientContext) ? Context.WriteSequenceNumber : Context.ReadSequenceNumber;
83 Write (header, 0, seqnum);
84 header [8] = (byte)contentType;
85 Write (header, 9, this.Context.Protocol);
86 Write (header, 11, (short)fragment.Length);
88 HashAlgorithm mac = this.ClientHMAC;
89 mac.TransformBlock (header, 0, header.Length, header, 0);
90 mac.TransformBlock (fragment, 0, fragment.Length, fragment, 0);
91 // hack, else the method will allocate a new buffer of the same length (negative half the optimization)
92 mac.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0);
99 #region Key Generation Methods
101 public override void ComputeMasterSecret(byte[] preMasterSecret)
103 // Create master secret
104 this.Context.MasterSecret = new byte[preMasterSecret.Length];
105 this.Context.MasterSecret = this.PRF(
106 preMasterSecret, "master secret", this.Context.RandomCS, 48);
108 DebugHelper.WriteLine(">>>> MasterSecret", this.Context.MasterSecret);
111 public override void ComputeKeys()
114 TlsStream keyBlock = new TlsStream(
116 this.Context.MasterSecret,
118 this.Context.RandomSC,
121 this.Context.Negotiating.ClientWriteMAC = keyBlock.ReadBytes(this.HashSize);
122 this.Context.Negotiating.ServerWriteMAC = keyBlock.ReadBytes(this.HashSize);
123 this.Context.ClientWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize);
124 this.Context.ServerWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize);
126 if (this.IvSize != 0)
128 this.Context.ClientWriteIV = keyBlock.ReadBytes(this.IvSize);
129 this.Context.ServerWriteIV = keyBlock.ReadBytes(this.IvSize);
133 this.Context.ClientWriteIV = CipherSuite.EmptyArray;
134 this.Context.ServerWriteIV = CipherSuite.EmptyArray;
137 DebugHelper.WriteLine(">>>> KeyBlock", keyBlock.ToArray());
138 DebugHelper.WriteLine(">>>> ClientWriteKey", this.Context.ClientWriteKey);
139 DebugHelper.WriteLine(">>>> ClientWriteIV", this.Context.ClientWriteIV);
140 DebugHelper.WriteLine(">>>> ClientWriteMAC", this.Context.Negotiating.ClientWriteMAC);
141 DebugHelper.WriteLine(">>>> ServerWriteKey", this.Context.ServerWriteKey);
142 DebugHelper.WriteLine(">>>> ServerWriteIV", this.Context.ServerWriteIV);
143 DebugHelper.WriteLine(">>>> ServerWriteMAC", this.Context.Negotiating.ServerWriteMAC);
145 ClientSessionCache.SetContextInCache (this.Context);
146 // Clear no more needed data