1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
3 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
26 using System.Security.Cryptography;
28 namespace Mono.Security.Protocol.Tls.Handshake.Server
30 internal class TlsClientHello : HandshakeMessage
32 #region Private Fields
34 private byte[] random;
35 private byte[] sessionId;
36 private short[] cipherSuites;
37 private byte[] compressionMethods;
43 public TlsClientHello(Context context, byte[] buffer)
44 : base(context, HandshakeType.ClientHello, buffer)
52 public override void Update()
56 this.selectCipherSuite();
57 this.selectCompressionMethod();
59 this.Context.SessionId = this.sessionId;
60 this.Context.ClientRandom = this.random;
61 this.Context.ProtocolNegotiated = true;
66 #region Protected Methods
68 protected override void ProcessAsSsl3()
73 protected override void ProcessAsTls1()
76 this.processProtocol(this.ReadInt16());
78 // Random bytes - Unix time + Radom bytes [28]
79 this.random = this.ReadBytes(32);
82 // Send the session ID empty
83 this.sessionId = this.ReadBytes(this.ReadByte());
85 // Read Supported Cipher Suites count
86 this.cipherSuites = new short[this.ReadInt16()/2];
89 for (int i = 0; i < this.cipherSuites.Length; i++)
91 this.cipherSuites[i] = this.ReadInt16();
94 // Compression methods length
95 this.compressionMethods = new byte[this.ReadByte()];
97 for (int i = 0; i < this.compressionMethods.Length; i++)
99 this.compressionMethods[i] = this.ReadByte();
105 #region Private Methods
107 private void processProtocol(short protocol)
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);
114 if ((clientProtocol & this.Context.SecurityProtocolFlags) == clientProtocol ||
115 (this.Context.SecurityProtocolFlags & SecurityProtocolType.Default) == SecurityProtocolType.Default)
117 this.Context.SecurityProtocol = clientProtocol;
118 this.Context.SupportedCiphers.Clear();
119 this.Context.SupportedCiphers = null;
120 this.Context.SupportedCiphers = CipherSuiteFactory.GetSupportedCiphers(clientProtocol);
124 throw new TlsException(AlertDescription.ProtocolVersion, "Incorrect protocol version received from server");
128 private void selectCipherSuite()
132 for (int i = 0; i < this.cipherSuites.Length; i++)
134 if ((index = this.Context.SupportedCiphers.IndexOf(this.cipherSuites[i])) != -1)
136 this.Context.Negotiating.Cipher = this.Context.SupportedCiphers[index];
141 if (this.Context.Negotiating.Cipher == null)
143 throw new TlsException(AlertDescription.InsuficientSecurity, "Insuficient Security");
147 private void selectCompressionMethod()
149 this.Context.CompressionMethod = SecurityCompressionType.None;