2003-11-17 Carlos Guzm��n ��lvarez <carlosga@telefonica.net>
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / TlsCipherSuite.cs
1 /* Transport Security Layer (TLS)\r
2  * Copyright (c) 2003 Carlos Guzmán Álvarez\r
3  * \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
11  * \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
14  * \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
23  */\r
24 \r
25 using System;\r
26 using System.IO;\r
27 using System.Text;\r
28 using System.Security.Cryptography;\r
29 using System.Security.Cryptography.X509Certificates;\r
30 \r
31 using Mono.Security;\r
32 using Mono.Security.Cryptography;\r
33 \r
34 namespace Mono.Security.Protocol.Tls\r
35 {\r
36         internal class TlsCipherSuite : CipherSuite\r
37         {\r
38                 #region CONSTRUCTORS\r
39                 \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
49                 {\r
50                 }\r
51 \r
52                 #endregion\r
53 \r
54                 #region MAC_GENERATION_METHOD\r
55 \r
56                 public override byte[] ComputeServerRecordMAC(TlsContentType contentType, byte[] fragment)\r
57                 {\r
58                         TlsStream       data    = new TlsStream();\r
59                         byte[]          result  = null;\r
60 \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
66 \r
67                         result = this.ServerHMAC.ComputeHash(data.ToArray());\r
68 \r
69                         data.Reset();\r
70 \r
71                         return result;\r
72                 }\r
73 \r
74                 public override byte[] ComputeClientRecordMAC(TlsContentType contentType, byte[] fragment)\r
75                 {\r
76                         TlsStream       data    = new TlsStream();\r
77                         byte[]          result  = null;\r
78 \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
84 \r
85                         result = this.ClientHMAC.ComputeHash(data.ToArray());\r
86 \r
87                         data.Reset();\r
88 \r
89                         return result;\r
90                 }\r
91 \r
92                 #endregion\r
93 \r
94                 #region KEY_GENERATION_METODS\r
95 \r
96                 public override void ComputeMasterSecret(byte[] preMasterSecret)\r
97                 {\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
102                 }\r
103 \r
104                 public override void ComputeKeys()\r
105                 {\r
106                         // Create keyblock\r
107                         TlsStream keyBlock = new TlsStream(\r
108                                 this.PRF(\r
109                                 this.Context.MasterSecret, \r
110                                 "key expansion",\r
111                                 this.Context.RandomSC,\r
112                                 this.KeyBlockSize));\r
113 \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
118 \r
119                         if (!this.IsExportable)\r
120                         {\r
121                                 if (this.IvSize != 0)\r
122                                 {\r
123                                         this.Context.ClientWriteIV = keyBlock.ReadBytes(this.IvSize);\r
124                                         this.Context.ServerWriteIV = keyBlock.ReadBytes(this.IvSize);\r
125                                 }\r
126                                 else\r
127                                 {\r
128                                         this.Context.ClientWriteIV = new byte[0];\r
129                                         this.Context.ServerWriteIV = new byte[0];\r
130                                 }\r
131                         }\r
132                         else\r
133                         {\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
137                                 \r
138                                 this.Context.ClientWriteKey     = finalClientWriteKey;\r
139                                 this.Context.ServerWriteKey     = finalServerWriteKey;\r
140 \r
141                                 // Generate IV block\r
142                                 byte[] ivBlock = PRF(new byte[]{}, "IV block", this.Context.RandomCS, this.IvSize*2);\r
143 \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
147 \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
150                         }\r
151 \r
152                         // Clear no more needed data\r
153                         keyBlock.Reset();\r
154                 }\r
155 \r
156                 #endregion\r
157         }\r
158 }