Merge pull request #216 from ilkerde/master
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls.Handshake.Client / TlsServerHello.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
27 namespace Mono.Security.Protocol.Tls.Handshake.Client
28 {
29         internal class TlsServerHello : HandshakeMessage
30         {
31                 #region Fields
32
33                 private SecurityCompressionType compressionMethod;
34                 private byte[]                                  random;
35                 private byte[]                                  sessionId;
36                 private CipherSuite     cipherSuite;
37                 
38                 #endregion
39
40                 #region Constructors
41
42                 public TlsServerHello(Context context, byte[] buffer) 
43                         : base(context, HandshakeType.ServerHello, buffer)
44                 {
45                 }
46
47                 #endregion
48
49                 #region Methods
50
51                 public override void Update()
52                 {
53                         base.Update();
54
55                         this.Context.SessionId                  = this.sessionId;
56                         this.Context.ServerRandom               = this.random;
57                         this.Context.Negotiating.Cipher = this.cipherSuite;
58                         this.Context.CompressionMethod  = this.compressionMethod;
59                         this.Context.ProtocolNegotiated = true;
60
61                         DebugHelper.WriteLine("Selected Cipher Suite {0}", this.cipherSuite.Name);
62                         DebugHelper.WriteLine("Client random", this.Context.ClientRandom);
63                         DebugHelper.WriteLine("Server random", this.Context.ServerRandom);
64                         
65                         // Compute ClientRandom + ServerRandom
66                         int clen = this.Context.ClientRandom.Length;
67                         int slen = this.Context.ServerRandom.Length;
68                         int rlen = clen + slen;
69                         byte[] cs = new byte[rlen];
70                         Buffer.BlockCopy (this.Context.ClientRandom, 0, cs, 0, clen);
71                         Buffer.BlockCopy (this.Context.ServerRandom, 0, cs, clen, slen);
72                         this.Context.RandomCS = cs;
73                         
74                         // Server Random + Client Random
75                         byte[] sc = new byte[rlen];
76                         Buffer.BlockCopy (this.Context.ServerRandom, 0, sc, 0, slen);
77                         Buffer.BlockCopy (this.Context.ClientRandom, 0, sc, slen, clen);
78                         this.Context.RandomSC = sc;
79                 }
80
81                 #endregion
82
83                 #region Protected Methods
84
85                 protected override void ProcessAsSsl3()
86                 {
87                         this.ProcessAsTls1();
88                 }
89
90                 protected override void ProcessAsTls1()
91                 {
92                         // Read protocol version
93                         this.processProtocol(this.ReadInt16());
94                         
95                         // Read random  - Unix time + Random bytes
96                         this.random     = this.ReadBytes(32);
97
98                         // Read Session id
99                         int length = (int) ReadByte ();
100                         if (length > 0)
101                         {
102                                 this.sessionId = this.ReadBytes(length);
103                                 ClientSessionCache.Add (this.Context.ClientSettings.TargetHost, this.sessionId);
104                                 this.Context.AbbreviatedHandshake = Compare (this.sessionId, this.Context.SessionId);
105                         } 
106                         else
107                         {
108                                 this.Context.AbbreviatedHandshake = false;
109                         }
110
111                         // Read cipher suite
112                         short cipherCode = this.ReadInt16();
113                         if (this.Context.SupportedCiphers.IndexOf(cipherCode) == -1)
114                         {
115                                 // The server has sent an invalid ciphersuite
116                                 throw new TlsException(AlertDescription.InsuficientSecurity, "Invalid cipher suite received from server");
117                         }
118                         this.cipherSuite = this.Context.SupportedCiphers[cipherCode];
119                         
120                         // Read compression methods ( always 0 )
121                         this.compressionMethod = (SecurityCompressionType)this.ReadByte();
122                 }
123
124                 #endregion
125
126                 #region Private Methods
127
128                 private void processProtocol(short protocol)
129                 {
130                         SecurityProtocolType serverProtocol = this.Context.DecodeProtocolCode(protocol);
131
132                         if ((serverProtocol & this.Context.SecurityProtocolFlags) == serverProtocol ||
133                                 (this.Context.SecurityProtocolFlags & SecurityProtocolType.Default) == SecurityProtocolType.Default)
134                         {
135                                 this.Context.SecurityProtocol = serverProtocol;
136                                 this.Context.SupportedCiphers.Clear();
137                                 this.Context.SupportedCiphers = null;
138                                 this.Context.SupportedCiphers = CipherSuiteFactory.GetSupportedCiphers(serverProtocol);
139
140                                 DebugHelper.WriteLine("Selected protocol {0}", serverProtocol);
141                         }
142                         else
143                         {
144                                 throw new TlsException(
145                                         AlertDescription.ProtocolVersion,
146                                         "Incorrect protocol version received from server");
147                         }
148                 }
149
150                 #endregion
151         }
152 }