2004-02-20 Carlos Guzm��n ��lvarez <carlosga@telefonica.net>
[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  * Permission is hereby granted, free of charge, to any person 
5  * obtaining a copy of this software and associated documentation 
6  * files (the "Software"), to deal in the Software without restriction, 
7  * including without limitation the rights to use, copy, modify, merge, 
8  * publish, distribute, sublicense, and/or sell copies of the Software, 
9  * and to permit persons to whom the Software is furnished to do so, 
10  * subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice shall be included 
13  * in all copies or substantial portions of the Software.
14  * 
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 using System;
26
27 namespace Mono.Security.Protocol.Tls.Handshake.Client
28 {
29         internal class TlsServerHello : TlsHandshakeMessage
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(TlsContext context, byte[] buffer) 
43                         : base(context, TlsHandshakeType.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.Cipher.Context             = this.Context;
60
61                         // Compute ClientRandom + ServerRandom
62                         TlsStream random = new TlsStream();
63                         random.Write(this.Context.ClientRandom);
64                         random.Write(this.Context.ServerRandom);
65                         this.Context.RandomCS = random.ToArray();
66
67                         // Server Random + Client Random
68                         random.Reset();
69                         random.Write(this.Context.ServerRandom);
70                         random.Write(this.Context.ClientRandom);
71
72                         this.Context.RandomSC = random.ToArray();
73                         random.Reset();
74                 }
75
76                 #endregion
77
78                 #region Protected Methods
79
80                 protected override void ProcessAsSsl3()
81                 {
82                         this.ProcessAsTls1();
83                 }
84
85                 protected override void ProcessAsTls1()
86                 {
87                         // Read protocol version
88                         this.processProtocol(this.ReadInt16());
89                         
90                         // Read random  - Unix time + Random bytes
91                         this.random     = this.ReadBytes(32);
92                         
93                         // Read Session id
94                         int length = (int)ReadByte();
95                         if (length > 0)
96                         {
97                                 this.sessionId = this.ReadBytes(length);
98                         }
99
100                         // Read cipher suite
101                         short cipherCode = this.ReadInt16();
102                         if (this.Context.SupportedCiphers.IndexOf(cipherCode) == -1)
103                         {
104                                 // The server has sent an invalid ciphersuite
105                                 throw new TlsException("Invalid cipher suite received from server");
106                         }
107                         this.cipherSuite = this.Context.SupportedCiphers[cipherCode];
108                         
109                         // Read compression methods ( always 0 )
110                         this.compressionMethod = (SecurityCompressionType)this.ReadByte();
111                 }
112
113                 #endregion
114
115                 #region Private Methods
116
117                 private void processProtocol(short protocol)
118                 {
119                         SecurityProtocolType serverProtocol = this.Context.DecodeProtocolCode(protocol);
120
121                         if ((serverProtocol & this.Context.SecurityProtocolFlags) == serverProtocol)
122                         {
123                                 this.Context.SecurityProtocol = serverProtocol;
124                                 this.Context.SupportedCiphers.Clear();
125                                 this.Context.SupportedCiphers = null;
126                                 this.Context.SupportedCiphers = TlsCipherSuiteFactory.GetSupportedCiphers(serverProtocol);
127                         }
128                         else
129                         {
130                                 throw this.Context.CreateException("Incorrect protocol version received from server");
131                         }
132                 }
133
134                 #endregion
135         }
136 }