merge -r 61110:61111
[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
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.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                         TlsStream random = new TlsStream();
67                         random.Write(this.Context.ClientRandom);
68                         random.Write(this.Context.ServerRandom);
69                         this.Context.RandomCS = random.ToArray();
70                         
71                         // Server Random + Client Random
72                         random.Reset();
73                         random.Write(this.Context.ServerRandom);
74                         random.Write(this.Context.ClientRandom);
75
76                         this.Context.RandomSC = random.ToArray();
77                         random.Reset();
78                 }
79
80                 #endregion
81
82                 #region Protected Methods
83
84                 protected override void ProcessAsSsl3()
85                 {
86                         this.ProcessAsTls1();
87                 }
88
89                 protected override void ProcessAsTls1()
90                 {
91                         // Read protocol version
92                         this.processProtocol(this.ReadInt16());
93                         
94                         // Read random  - Unix time + Random bytes
95                         this.random     = this.ReadBytes(32);
96
97                         // Read Session id
98                         int length = (int) ReadByte ();
99                         if (length > 0)
100                         {
101                                 this.sessionId = this.ReadBytes(length);
102                                 ClientSessionCache.Add (this.Context.ClientSettings.TargetHost, this.sessionId);
103                                 this.Context.AbbreviatedHandshake = CompareSessionId (this.sessionId, this.Context.SessionId);
104                         } 
105                         else
106                         {
107                                 this.Context.AbbreviatedHandshake = false;
108                         }
109
110                         // Read cipher suite
111                         short cipherCode = this.ReadInt16();
112                         if (this.Context.SupportedCiphers.IndexOf(cipherCode) == -1)
113                         {
114                                 // The server has sent an invalid ciphersuite
115                                 throw new TlsException(AlertDescription.InsuficientSecurity, "Invalid cipher suite received from server");
116                         }
117                         this.cipherSuite = this.Context.SupportedCiphers[cipherCode];
118                         
119                         // Read compression methods ( always 0 )
120                         this.compressionMethod = (SecurityCompressionType)this.ReadByte();
121                 }
122
123                 #endregion
124
125                 #region Private Methods
126
127                 private bool CompareSessionId (byte[] id1, byte[] id2)
128                 {
129                         // in our case both null can't exist (or be valid)
130                         if ((id1 == null) || (id2 == null))
131                                 return false;
132
133                         if (id1.Length != id2.Length)
134                                 return false;
135
136                         for (int i = 0; i < id1.Length; i++) {
137                                 if (id1 [i] != id2 [i])
138                                         return false;
139                         }
140                         return true;
141                 }
142
143                 private void processProtocol(short protocol)
144                 {
145                         SecurityProtocolType serverProtocol = this.Context.DecodeProtocolCode(protocol);
146
147                         if ((serverProtocol & this.Context.SecurityProtocolFlags) == serverProtocol ||
148                                 (this.Context.SecurityProtocolFlags & SecurityProtocolType.Default) == SecurityProtocolType.Default)
149                         {
150                                 this.Context.SecurityProtocol = serverProtocol;
151                                 this.Context.SupportedCiphers.Clear();
152                                 this.Context.SupportedCiphers = null;
153                                 this.Context.SupportedCiphers = CipherSuiteFactory.GetSupportedCiphers(serverProtocol);
154
155                                 DebugHelper.WriteLine("Selected protocol {0}", serverProtocol);
156                         }
157                         else
158                         {
159                                 throw new TlsException(
160                                         AlertDescription.ProtocolVersion,
161                                         "Incorrect protocol version received from server");
162                         }
163                 }
164
165                 #endregion
166         }
167 }