TlsClientHello.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Transport Security Layer (TLS)
  2. // Copyright (c) 2003-2004 Carlos Guzman Alvarez
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Security.Cryptography;
  25. namespace Mono.Security.Protocol.Tls.Handshake.Client
  26. {
  27. internal class TlsClientHello : HandshakeMessage
  28. {
  29. #region Fields
  30. private byte[] random;
  31. #endregion
  32. #region Constructors
  33. public TlsClientHello(Context context)
  34. : base(context, HandshakeType.ClientHello)
  35. {
  36. }
  37. #endregion
  38. #region Methods
  39. public override void Update()
  40. {
  41. ClientContext context = (ClientContext)this.Context;
  42. base.Update();
  43. context.ClientRandom = random;
  44. context.ClientHelloProtocol = this.Context.Protocol;
  45. random = null;
  46. }
  47. #endregion
  48. #region Protected Methods
  49. protected override void ProcessAsSsl3()
  50. {
  51. this.ProcessAsTls1();
  52. }
  53. protected override void ProcessAsTls1()
  54. {
  55. // Client Version
  56. this.Write(this.Context.Protocol);
  57. // Random bytes - Unix time + Radom bytes [28]
  58. TlsStream clientRandom = new TlsStream();
  59. clientRandom.Write(this.Context.GetUnixTime());
  60. clientRandom.Write(this.Context.GetSecureRandomBytes(28));
  61. this.random = clientRandom.ToArray();
  62. clientRandom.Reset();
  63. this.Write(this.random);
  64. // Session id
  65. // Check if we have a cache session we could reuse
  66. this.Context.SessionId = ClientSessionCache.FromHost (this.Context.ClientSettings.TargetHost);
  67. if (this.Context.SessionId != null)
  68. {
  69. this.Write((byte)this.Context.SessionId.Length);
  70. if (this.Context.SessionId.Length > 0)
  71. {
  72. this.Write(this.Context.SessionId);
  73. }
  74. }
  75. else
  76. {
  77. this.Write((byte)0);
  78. }
  79. // Write length of Cipher suites
  80. this.Write((short)(this.Context.SupportedCiphers.Count*2));
  81. // Write Supported Cipher suites
  82. for (int i = 0; i < this.Context.SupportedCiphers.Count; i++)
  83. {
  84. this.Write((short)this.Context.SupportedCiphers[i].Code);
  85. }
  86. // Compression methods length
  87. this.Write((byte)1);
  88. // Compression methods ( 0 = none )
  89. this.Write((byte)this.Context.CompressionMethod);
  90. }
  91. #endregion
  92. }
  93. }