Merge pull request #1748 from rolfbjarne/proclib-fix
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / TlsCipherSuite.cs
1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
3 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
4 //
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:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
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.
23 //
24
25 using System;
26 using System.IO;
27 using System.Security.Cryptography;
28
29 namespace Mono.Security.Protocol.Tls
30 {
31         internal class TlsCipherSuite : CipherSuite
32         {
33                 private const int MacHeaderLength = 13;
34                 private byte[] header;
35                 private object headerLock = new object ();
36
37                 #region Constructors
38                 
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)
48                 {
49                 }
50
51                 #endregion
52
53                 #region MAC Generation Methods
54
55                 public override byte[] ComputeServerRecordMAC(ContentType contentType, byte[] fragment)
56                 {
57                         lock (headerLock) {
58                                 if (header == null)
59                                         header = new byte [MacHeaderLength];
60
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);
66
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);
72                                 return mac.Hash;
73                         }
74                 }
75
76                 public override byte[] ComputeClientRecordMAC(ContentType contentType, byte[] fragment)
77                 {
78                         lock (headerLock) {
79                                 if (header == null)
80                                         header = new byte [MacHeaderLength];
81
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);
87
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);
93                                 return mac.Hash;
94                         }
95                 }
96
97                 #endregion
98
99                 #region Key Generation Methods
100
101                 public override void ComputeMasterSecret(byte[] preMasterSecret)
102                 {
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);
107
108                         DebugHelper.WriteLine(">>>> MasterSecret", this.Context.MasterSecret);
109                 }
110
111                 public override void ComputeKeys()
112                 {
113                         // Create keyblock
114                         TlsStream keyBlock = new TlsStream(
115                                 this.PRF(
116                                 this.Context.MasterSecret, 
117                                 "key expansion",
118                                 this.Context.RandomSC,
119                                 this.KeyBlockSize));
120
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);
125
126                         if (this.IvSize != 0)
127                         {
128                                 this.Context.ClientWriteIV = keyBlock.ReadBytes(this.IvSize);
129                                 this.Context.ServerWriteIV = keyBlock.ReadBytes(this.IvSize);
130                         }
131                         else
132                         {
133                                 this.Context.ClientWriteIV = CipherSuite.EmptyArray;
134                                 this.Context.ServerWriteIV = CipherSuite.EmptyArray;
135                         }
136
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);
144
145                         ClientSessionCache.SetContextInCache (this.Context);
146                         // Clear no more needed data
147                         keyBlock.Reset();
148                 }
149
150                 #endregion
151         }
152 }