Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls.Handshake.Server / TlsClientHello.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 using System.Security.Cryptography;
27
28 namespace Mono.Security.Protocol.Tls.Handshake.Server
29 {
30         internal class TlsClientHello : HandshakeMessage
31         {
32                 #region Private Fields
33
34                 private byte[]  random;
35                 private byte[]  sessionId;
36                 private short[] cipherSuites;
37                 private byte[]  compressionMethods;
38
39                 #endregion
40
41                 #region Constructors
42
43                 public TlsClientHello(Context context, byte[] buffer)
44                         : base(context, HandshakeType.ClientHello, buffer)
45                 {
46                 }
47
48                 #endregion
49
50                 #region Methods
51
52                 public override void Update()
53                 {
54                         base.Update();
55
56                         this.selectCipherSuite();
57                         this.selectCompressionMethod();
58
59                         this.Context.SessionId                  = this.sessionId;
60                         this.Context.ClientRandom               = this.random;
61                         this.Context.ProtocolNegotiated = true;
62                 }
63
64                 #endregion
65
66                 #region Protected Methods
67
68                 protected override void ProcessAsSsl3()
69                 {
70                         this.ProcessAsTls1();
71                 }
72
73                 protected override void ProcessAsTls1()
74                 {
75                         // Client Version
76                         this.processProtocol(this.ReadInt16());
77                                                                 
78                         // Random bytes - Unix time + Radom bytes [28]
79                         this.random = this.ReadBytes(32);
80                         
81                         // Session id
82                         // Send the session ID empty
83                         this.sessionId = this.ReadBytes(this.ReadByte());
84                         
85                         // Read Supported Cipher Suites count
86                         this.cipherSuites = new short[this.ReadInt16()/2];
87
88                         // Read Cipher Suites
89                         for (int i = 0; i < this.cipherSuites.Length; i++)
90                         {
91                                 this.cipherSuites[i] = this.ReadInt16();
92                         }
93
94                         // Compression methods length
95                         this.compressionMethods = new byte[this.ReadByte()];
96                         
97                         for (int i = 0; i < this.compressionMethods.Length; i++)
98                         {
99                                 this.compressionMethods[i] = this.ReadByte();
100                         }
101                 }
102
103                 #endregion
104
105                 #region Private Methods
106
107                 private void processProtocol(short protocol)
108                 {
109                         // a server MUST reply with the hight version supported (`true` for fallback)
110                         // so a TLS 1.2 client (like Google Chrome) will be returned that the server uses TLS 1.0
111                         // instead of an alert about the protocol
112                         SecurityProtocolType clientProtocol = Context.DecodeProtocolCode (protocol, true);
113
114                         if ((clientProtocol & this.Context.SecurityProtocolFlags) == clientProtocol ||
115                                 (this.Context.SecurityProtocolFlags & SecurityProtocolType.Default) == SecurityProtocolType.Default)
116                         {
117                                 this.Context.SecurityProtocol = clientProtocol;
118                                 this.Context.SupportedCiphers.Clear();
119                                 this.Context.SupportedCiphers = null;
120                                 this.Context.SupportedCiphers = CipherSuiteFactory.GetSupportedCiphers(clientProtocol);
121                         }
122                         else
123                         {
124                                 throw new TlsException(AlertDescription.ProtocolVersion, "Incorrect protocol version received from server");
125                         }
126                 }
127
128                 private void selectCipherSuite()
129                 {
130                         int index = 0;
131
132                         for (int i = 0; i < this.cipherSuites.Length; i++)
133                         {
134                                 if ((index = this.Context.SupportedCiphers.IndexOf(this.cipherSuites[i])) != -1)        
135                                 {
136                                         this.Context.Negotiating.Cipher = this.Context.SupportedCiphers[index];
137                                         break;
138                                 }
139                         }
140
141                         if (this.Context.Negotiating.Cipher == null)
142                         {
143                                 throw new TlsException(AlertDescription.InsuficientSecurity, "Insuficient Security");
144                         }
145                 }
146
147                 private void selectCompressionMethod()
148                 {
149                         this.Context.CompressionMethod = SecurityCompressionType.None;
150                 }
151
152                 #endregion
153         }
154 }