2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Security / Mono.Security.Protocol.Tls.Handshake.Server / TlsClientHello.cs
blobe328e2acae3cb9e98b866e5445652457fde4e617
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.
25 using System;
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;
39 #endregion
41 #region Constructors
43 public TlsClientHello(Context context, byte[] buffer)
44 : base(context, HandshakeType.ClientHello, buffer)
48 #endregion
50 #region Methods
52 public override void Update()
54 base.Update();
56 this.selectCipherSuite();
57 this.selectCompressionMethod();
59 this.Context.SessionId = this.sessionId;
60 this.Context.ClientRandom = this.random;
61 this.Context.ProtocolNegotiated = true;
64 #endregion
66 #region Protected Methods
68 protected override void ProcessAsSsl3()
70 this.ProcessAsTls1();
73 protected override void ProcessAsTls1()
75 // Client Version
76 this.processProtocol(this.ReadInt16());
78 // Random bytes - Unix time + Radom bytes [28]
79 this.random = this.ReadBytes(32);
81 // Session id
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];
88 // Read Cipher Suites
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();
103 #endregion
105 #region Private Methods
107 private void processProtocol(short protocol)
109 SecurityProtocolType clientProtocol = this.Context.DecodeProtocolCode(protocol);
111 if ((clientProtocol & this.Context.SecurityProtocolFlags) == clientProtocol ||
112 (this.Context.SecurityProtocolFlags & SecurityProtocolType.Default) == SecurityProtocolType.Default)
114 this.Context.SecurityProtocol = clientProtocol;
115 this.Context.SupportedCiphers.Clear();
116 this.Context.SupportedCiphers = null;
117 this.Context.SupportedCiphers = CipherSuiteFactory.GetSupportedCiphers(clientProtocol);
119 else
121 throw new TlsException(AlertDescription.ProtocolVersion, "Incorrect protocol version received from server");
125 private void selectCipherSuite()
127 int index = 0;
129 for (int i = 0; i < this.cipherSuites.Length; i++)
131 if ((index = this.Context.SupportedCiphers.IndexOf(this.cipherSuites[i])) != -1)
133 this.Context.Negotiating.Cipher = this.Context.SupportedCiphers[index];
134 break;
138 if (this.Context.Negotiating.Cipher == null)
140 throw new TlsException(AlertDescription.InsuficientSecurity, "Insuficient Security");
144 private void selectCompressionMethod()
146 this.Context.CompressionMethod = SecurityCompressionType.None;
149 #endregion