TlsServerHello.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. using System;
  25. namespace Mono.Security.Protocol.Tls.Handshake.Server
  26. {
  27. internal class TlsServerHello : HandshakeMessage
  28. {
  29. #region Private Fields
  30. private int unixTime;
  31. private byte[] random;
  32. #endregion
  33. #region Constructors
  34. public TlsServerHello(Context context)
  35. : base(context, HandshakeType.ServerHello)
  36. {
  37. }
  38. #endregion
  39. #region Methods
  40. public override void Update()
  41. {
  42. base.Update();
  43. TlsStream random = new TlsStream();
  44. // Compute Server Random
  45. random.Write(this.unixTime);
  46. random.Write(this.random);
  47. this.Context.ServerRandom = random.ToArray();
  48. // Compute ClientRandom + ServerRandom
  49. random.Reset();
  50. random.Write(this.Context.ClientRandom);
  51. random.Write(this.Context.ServerRandom);
  52. this.Context.RandomCS = random.ToArray();
  53. // Server Random + Client Random
  54. random.Reset();
  55. random.Write(this.Context.ServerRandom);
  56. random.Write(this.Context.ClientRandom);
  57. this.Context.RandomSC = random.ToArray();
  58. random.Reset();
  59. }
  60. #endregion
  61. #region Protected Methods
  62. protected override void ProcessAsSsl3()
  63. {
  64. this.ProcessAsTls1();
  65. }
  66. protected override void ProcessAsTls1()
  67. {
  68. // Write protocol version
  69. this.Write(this.Context.Protocol);
  70. // Write Unix time
  71. this.unixTime = this.Context.GetUnixTime();
  72. this.Write(this.unixTime);
  73. // Write Random bytes
  74. random = this.Context.GetSecureRandomBytes(28);
  75. this.Write(this.random);
  76. if (this.Context.SessionId == null)
  77. {
  78. this.WriteByte(0);
  79. }
  80. else
  81. {
  82. // Write Session ID length
  83. this.WriteByte((byte)this.Context.SessionId.Length);
  84. // Write Session ID
  85. this.Write(this.Context.SessionId);
  86. }
  87. // Write selected cipher suite
  88. this.Write(this.Context.Negotiating.Cipher.Code);
  89. // Write selected compression method
  90. this.WriteByte((byte)this.Context.CompressionMethod);
  91. }
  92. #endregion
  93. }
  94. }