2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Security / Mono.Security.Protocol.Tls.Handshake.Server / TlsServerHello.cs
blobc02c24c57a29bf4d238131ee7739145ef964d288
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;
27 namespace Mono.Security.Protocol.Tls.Handshake.Server
29 internal class TlsServerHello : HandshakeMessage
31 #region Private Fields
33 private int unixTime;
34 private byte[] random;
36 #endregion
38 #region Constructors
40 public TlsServerHello(Context context)
41 : base(context, HandshakeType.ServerHello)
45 #endregion
47 #region Methods
49 public override void Update()
51 base.Update();
53 TlsStream random = new TlsStream();
55 // Compute Server Random
56 random.Write(this.unixTime);
57 random.Write(this.random);
59 this.Context.ServerRandom = random.ToArray();
61 // Compute ClientRandom + ServerRandom
62 random.Reset();
63 random.Write(this.Context.ClientRandom);
64 random.Write(this.Context.ServerRandom);
66 this.Context.RandomCS = random.ToArray();
68 // Server Random + Client Random
69 random.Reset();
70 random.Write(this.Context.ServerRandom);
71 random.Write(this.Context.ClientRandom);
73 this.Context.RandomSC = random.ToArray();
75 random.Reset();
78 #endregion
80 #region Protected Methods
82 protected override void ProcessAsSsl3()
84 this.ProcessAsTls1();
87 protected override void ProcessAsTls1()
89 // Write protocol version
90 this.Write(this.Context.Protocol);
92 // Write Unix time
93 this.unixTime = this.Context.GetUnixTime();
94 this.Write(this.unixTime);
96 // Write Random bytes
97 random = this.Context.GetSecureRandomBytes(28);
98 this.Write(this.random);
100 if (this.Context.SessionId == null)
102 this.WriteByte(0);
104 else
106 // Write Session ID length
107 this.WriteByte((byte)this.Context.SessionId.Length);
109 // Write Session ID
110 this.Write(this.Context.SessionId);
113 // Write selected cipher suite
114 this.Write(this.Context.Negotiating.Cipher.Code);
116 // Write selected compression method
117 this.WriteByte((byte)this.Context.CompressionMethod);
120 #endregion