Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls.Handshake.Server / TlsServerHello.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
27 namespace Mono.Security.Protocol.Tls.Handshake.Server
28 {
29         internal class TlsServerHello : HandshakeMessage
30         {
31                 #region Private Fields
32
33                 private int             unixTime;
34                 private byte[]  random;
35
36                 #endregion
37
38                 #region Constructors
39
40                 public TlsServerHello(Context context) 
41                         : base(context, HandshakeType.ServerHello)
42                 {
43                 }
44
45                 #endregion
46
47                 #region Methods
48
49                 public override void Update()
50                 {
51                         base.Update();
52
53                         TlsStream random = new TlsStream();
54
55                         // Compute Server Random
56                         random.Write(this.unixTime);
57                         random.Write(this.random);
58
59                         this.Context.ServerRandom = random.ToArray();
60
61                         // Compute ClientRandom + ServerRandom
62                         random.Reset();
63                         random.Write(this.Context.ClientRandom);
64                         random.Write(this.Context.ServerRandom);
65
66                         this.Context.RandomCS = random.ToArray();
67
68                         // Server Random + Client Random
69                         random.Reset();
70                         random.Write(this.Context.ServerRandom);
71                         random.Write(this.Context.ClientRandom);
72
73                         this.Context.RandomSC = random.ToArray();
74
75                         random.Reset();
76                 }
77
78                 #endregion
79
80                 #region Protected Methods
81
82                 protected override void ProcessAsSsl3()
83                 {
84                         this.ProcessAsTls1();
85                 }
86
87                 protected override void ProcessAsTls1()
88                 {
89                         // Write protocol version
90                         this.Write(this.Context.Protocol);
91                         
92                         // Write Unix time
93                         this.unixTime = this.Context.GetUnixTime();
94                         this.Write(this.unixTime);
95
96                         // Write Random bytes
97                         random = this.Context.GetSecureRandomBytes(28);
98                         this.Write(this.random);
99                                                 
100                         if (this.Context.SessionId == null)
101                         {
102                                 this.WriteByte(0);
103                         }
104                         else
105                         {
106                                 // Write Session ID length
107                                 this.WriteByte((byte)this.Context.SessionId.Length);
108
109                                 // Write Session ID
110                                 this.Write(this.Context.SessionId);
111                         }
112
113                         // Write selected cipher suite
114                         this.Write(this.Context.Negotiating.Cipher.Code);
115                         
116                         // Write selected compression method
117                         this.WriteByte((byte)this.Context.CompressionMethod);
118                 }
119
120                 #endregion
121         }
122 }