Added partial implementation of SSL3 protocol (not finished yet). Renamed TlsAbstract...
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls.Handshake.Client / TlsServerHello.cs
1 /* Transport Security Layer (TLS)
2  * Copyright (c) 2003 Carlos Guzmán Álvarez
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 TlsProtocol                             protocol;
34                 private TlsCompressionMethod    compressionMethod;
35                 private byte[]                                  random;
36                 private byte[]                                  sessionId;
37                 private CipherSuite     cipherSuite;
38                 
39                 #endregion
40
41                 #region CONSTRUCTORS
42
43                 public TlsServerHello(TlsSession session, byte[] buffer) 
44                         : base(session, TlsHandshakeType.ServerHello, buffer)
45                 {
46                 }
47
48                 #endregion
49
50                 #region METHODS
51
52                 public override void UpdateSession()
53                 {
54                         base.UpdateSession();
55
56                         this.Session.SetSessionId(this.sessionId);
57                         this.Session.Context.ServerRandom               = this.random;
58                         this.Session.Context.Cipher                             = this.cipherSuite;
59                         this.Session.Context.CompressionMethod  = this.compressionMethod;
60                         this.Session.Context.Cipher.Context             = this.Session.Context;
61
62                         // Compute ClientRandom + ServerRandom
63                         TlsStream random = new TlsStream();
64                         random.Write(this.Session.Context.ClientRandom);
65                         random.Write(this.Session.Context.ServerRandom);
66                         this.Session.Context.RandomCS = random.ToArray();
67
68                         // Server Random + Client Random
69                         random.Reset();
70                         random.Write(this.Session.Context.ServerRandom);
71                         random.Write(this.Session.Context.ClientRandom);
72
73                         this.Session.Context.RandomSC = random.ToArray();
74                         random.Reset();
75                 }
76
77                 #endregion
78
79                 #region PROTECTED_METHODS
80
81                 protected override void ProcessAsSsl3()
82                 {
83                         #warning "Check that the protocol sent by the server is supported"
84                         // Read protocol version
85                         this.protocol   = (TlsProtocol)this.ReadInt16();
86                         
87                         // Read random  - Unix time + Random bytes
88                         this.random             = this.ReadBytes(32);
89                         
90                         // Read Session id
91                         int length = (int)ReadByte();
92                         if (length > 0)
93                         {
94                                 this.sessionId = this.ReadBytes(length);
95                         }
96
97                         // Read cipher suite
98                         short cipherCode = this.ReadInt16();
99                         if (this.Session.Context.SupportedCiphers.IndexOf(cipherCode) == -1)
100                         {
101                                 // The server has sent an invalid ciphersuite
102                                 throw new TlsException("Invalid cipher suite received from server");
103                         }
104                         this.cipherSuite = this.Session.Context.SupportedCiphers[cipherCode];
105                         
106                         // Read compression methods ( always 0 )
107                         this.compressionMethod = (TlsCompressionMethod)this.ReadByte();
108                 }
109
110                 protected override void ProcessAsTls1()
111                 {
112                         // Read protocol version
113                         this.protocol   = (TlsProtocol)this.ReadInt16();
114                         
115                         // Read random  - Unix time + Random bytes
116                         this.random             = this.ReadBytes(32);
117                         
118                         // Read Session id
119                         int length = (int)ReadByte();
120                         if (length > 0)
121                         {
122                                 this.sessionId = this.ReadBytes(length);
123                         }
124
125                         // Read cipher suite
126                         short cipherCode = this.ReadInt16();
127                         if (this.Session.Context.SupportedCiphers.IndexOf(cipherCode) == -1)
128                         {
129                                 // The server has sent an invalid ciphersuite
130                                 throw new TlsException("Invalid cipher suite received from server");
131                         }
132                         this.cipherSuite = this.Session.Context.SupportedCiphers[cipherCode];
133                         
134                         // Read compression methods ( always 0 )
135                         this.compressionMethod = (TlsCompressionMethod)this.ReadByte();
136                 }
137
138                 #endregion
139         }
140 }