1 /* Transport Security Layer (TLS)
2 * Copyright (c) 2003-2004 Carlos Guzman Alvarez
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
27 namespace Mono.Security.Protocol.Tls.Handshake.Client
29 internal class TlsServerHello : TlsHandshakeMessage
33 private SecurityProtocolType protocol;
34 private SecurityCompressionType compressionMethod;
35 private byte[] random;
36 private byte[] sessionId;
37 private CipherSuite cipherSuite;
43 public TlsServerHello(TlsContext context, byte[] buffer)
44 : base(context, TlsHandshakeType.ServerHello, buffer)
52 public override void Update()
56 this.Context.SessionId = this.sessionId;
57 this.Context.ServerRandom = this.random;
58 this.Context.Cipher = this.cipherSuite;
59 this.Context.CompressionMethod = this.compressionMethod;
60 this.Context.Cipher.Context = this.Context;
62 // Compute ClientRandom + ServerRandom
63 TlsStream random = new TlsStream();
64 random.Write(this.Context.ClientRandom);
65 random.Write(this.Context.ServerRandom);
66 this.Context.RandomCS = random.ToArray();
68 // Server Random + Client Random
70 random.Write(this.Context.ServerRandom);
71 random.Write(this.Context.ClientRandom);
73 this.Context.RandomSC = random.ToArray();
79 #region Protected Methods
81 protected override void ProcessAsSsl3()
83 // Read protocol version
84 this.protocol = (SecurityProtocolType)this.ReadInt16();
86 // Read random - Unix time + Random bytes
87 this.random = this.ReadBytes(32);
90 int length = (int)ReadByte();
93 this.sessionId = this.ReadBytes(length);
97 short cipherCode = this.ReadInt16();
98 if (this.Context.SupportedCiphers.IndexOf(cipherCode) == -1)
100 // The server has sent an invalid ciphersuite
101 throw new TlsException("Invalid cipher suite received from server");
103 this.cipherSuite = this.Context.SupportedCiphers[cipherCode];
105 // Read compression methods ( always 0 )
106 this.compressionMethod = (SecurityCompressionType)this.ReadByte();
109 protected override void ProcessAsTls1()
111 // Read protocol version
112 this.protocol = (SecurityProtocolType)this.ReadInt16();
114 // Read random - Unix time + Random bytes
115 this.random = this.ReadBytes(32);
118 int length = (int)ReadByte();
121 this.sessionId = this.ReadBytes(length);
125 short cipherCode = this.ReadInt16();
126 if (this.Context.SupportedCiphers.IndexOf(cipherCode) == -1)
128 // The server has sent an invalid ciphersuite
129 throw new TlsException("Invalid cipher suite received from server");
131 this.cipherSuite = this.Context.SupportedCiphers[cipherCode];
133 // Read compression methods ( always 0 )
134 this.compressionMethod = (SecurityCompressionType)this.ReadByte();