ClientRecordProtocol.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.Globalization;
  25. using System.IO;
  26. using Mono.Security.Protocol.Tls.Handshake;
  27. using Mono.Security.Protocol.Tls.Handshake.Client;
  28. namespace Mono.Security.Protocol.Tls
  29. {
  30. internal class ClientRecordProtocol : RecordProtocol
  31. {
  32. #region Constructors
  33. public ClientRecordProtocol(
  34. Stream innerStream,
  35. ClientContext context) : base(innerStream, context)
  36. {
  37. }
  38. #endregion
  39. #region Send Messages
  40. public override HandshakeMessage GetMessage(HandshakeType type)
  41. {
  42. HandshakeMessage msg = this.createClientHandshakeMessage(type);
  43. return msg;
  44. }
  45. #endregion
  46. #region Handshake Processing Methods
  47. protected override void ProcessHandshakeMessage(TlsStream handMsg)
  48. {
  49. HandshakeType handshakeType = (HandshakeType)handMsg.ReadByte();
  50. HandshakeMessage message = null;
  51. DebugHelper.WriteLine(">>>> Processing Handshake record ({0})", handshakeType);
  52. // Read message length
  53. int length = handMsg.ReadInt24();
  54. // Read message data
  55. byte[] data = null;
  56. if (length > 0)
  57. {
  58. data = new byte[length];
  59. handMsg.Read (data, 0, length);
  60. }
  61. // Create and process the server message
  62. message = this.createServerHandshakeMessage(handshakeType, data);
  63. if (message != null)
  64. {
  65. message.Process();
  66. }
  67. // Update the last handshake message
  68. this.Context.LastHandshakeMsg = handshakeType;
  69. // Update session
  70. if (message != null)
  71. {
  72. message.Update();
  73. this.Context.HandshakeMessages.WriteByte ((byte) handshakeType);
  74. this.Context.HandshakeMessages.WriteInt24 (length);
  75. if (length > 0)
  76. {
  77. this.Context.HandshakeMessages.Write (data, 0, data.Length);
  78. }
  79. }
  80. }
  81. #endregion
  82. #region Client Handshake Message Factories
  83. private HandshakeMessage createClientHandshakeMessage(HandshakeType type)
  84. {
  85. switch (type)
  86. {
  87. case HandshakeType.ClientHello:
  88. return new TlsClientHello(this.context);
  89. case HandshakeType.Certificate:
  90. return new TlsClientCertificate(this.context);
  91. case HandshakeType.ClientKeyExchange:
  92. return new TlsClientKeyExchange(this.context);
  93. case HandshakeType.CertificateVerify:
  94. return new TlsClientCertificateVerify(this.context);
  95. case HandshakeType.Finished:
  96. return new TlsClientFinished(this.context);
  97. default:
  98. throw new InvalidOperationException("Unknown client handshake message type: " + type.ToString() );
  99. }
  100. }
  101. private HandshakeMessage createServerHandshakeMessage(
  102. HandshakeType type, byte[] buffer)
  103. {
  104. ClientContext context = (ClientContext)this.context;
  105. switch (type)
  106. {
  107. case HandshakeType.HelloRequest:
  108. if (context.HandshakeState != HandshakeState.Started)
  109. {
  110. context.HandshakeState = HandshakeState.None;
  111. // re-negotiation will occur at next read/write
  112. // (i.e. not during an existing encode/decode op)
  113. }
  114. else
  115. {
  116. this.SendAlert(
  117. AlertLevel.Warning,
  118. AlertDescription.NoRenegotiation);
  119. }
  120. return null;
  121. case HandshakeType.ServerHello:
  122. return new TlsServerHello(this.context, buffer);
  123. case HandshakeType.Certificate:
  124. return new TlsServerCertificate(this.context, buffer);
  125. case HandshakeType.ServerKeyExchange:
  126. return new TlsServerKeyExchange(this.context, buffer);
  127. case HandshakeType.CertificateRequest:
  128. return new TlsServerCertificateRequest(this.context, buffer);
  129. case HandshakeType.ServerHelloDone:
  130. return new TlsServerHelloDone(this.context, buffer);
  131. case HandshakeType.Finished:
  132. return new TlsServerFinished(this.context, buffer);
  133. default:
  134. throw new TlsException(
  135. AlertDescription.UnexpectedMessage,
  136. String.Format(CultureInfo.CurrentUICulture,
  137. "Unknown server handshake message received ({0})",
  138. type.ToString()));
  139. }
  140. }
  141. #endregion
  142. }
  143. }