ServerRecordProtocol.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.Server;
  28. namespace Mono.Security.Protocol.Tls
  29. {
  30. internal class ServerRecordProtocol : RecordProtocol
  31. {
  32. #region Constructors
  33. public ServerRecordProtocol(
  34. Stream innerStream,
  35. ServerContext context) : base(innerStream, context)
  36. {
  37. }
  38. #endregion
  39. #region Send Messages
  40. public override HandshakeMessage GetMessage(HandshakeType type)
  41. {
  42. // Create and process the record message
  43. HandshakeMessage msg = this.createServerHandshakeMessage(type);
  44. return msg;
  45. }
  46. #endregion
  47. #region Handshake Processing Methods
  48. protected override void ProcessHandshakeMessage(TlsStream handMsg)
  49. {
  50. HandshakeType handshakeType = (HandshakeType)handMsg.ReadByte();
  51. HandshakeMessage message = null;
  52. // Read message length
  53. int length = handMsg.ReadInt24();
  54. // Read message data
  55. byte[] data = new byte[length];
  56. handMsg.Read(data, 0, length);
  57. // Create and process the server message
  58. message = this.createClientHandshakeMessage(handshakeType, data);
  59. message.Process();
  60. // Update the last handshake message
  61. this.Context.LastHandshakeMsg = handshakeType;
  62. // Update session
  63. if (message != null)
  64. {
  65. message.Update();
  66. this.Context.HandshakeMessages.WriteByte ((byte) handshakeType);
  67. this.Context.HandshakeMessages.WriteInt24 (length);
  68. this.Context.HandshakeMessages.Write (data, 0, data.Length);
  69. }
  70. }
  71. #endregion
  72. #region Server Handshake Message Factories
  73. private HandshakeMessage createClientHandshakeMessage(
  74. HandshakeType type, byte[] buffer)
  75. {
  76. switch (type)
  77. {
  78. case HandshakeType.ClientHello:
  79. return new TlsClientHello(this.context, buffer);
  80. case HandshakeType.Certificate:
  81. return new TlsClientCertificate(this.context, buffer);
  82. case HandshakeType.ClientKeyExchange:
  83. return new TlsClientKeyExchange(this.context, buffer);
  84. case HandshakeType.CertificateVerify:
  85. return new TlsClientCertificateVerify(this.context, buffer);
  86. case HandshakeType.Finished:
  87. return new TlsClientFinished(this.context, buffer);
  88. default:
  89. throw new TlsException(
  90. AlertDescription.UnexpectedMessage,
  91. String.Format(CultureInfo.CurrentUICulture,
  92. "Unknown server handshake message received ({0})",
  93. type.ToString()));
  94. }
  95. }
  96. private HandshakeMessage createServerHandshakeMessage(
  97. HandshakeType type)
  98. {
  99. switch (type)
  100. {
  101. case HandshakeType.HelloRequest:
  102. this.SendRecord(HandshakeType.ClientHello);
  103. return null;
  104. case HandshakeType.ServerHello:
  105. return new TlsServerHello(this.context);
  106. case HandshakeType.Certificate:
  107. return new TlsServerCertificate(this.context);
  108. case HandshakeType.ServerKeyExchange:
  109. return new TlsServerKeyExchange(this.context);
  110. case HandshakeType.CertificateRequest:
  111. return new TlsServerCertificateRequest(this.context);
  112. case HandshakeType.ServerHelloDone:
  113. return new TlsServerHelloDone(this.context);
  114. case HandshakeType.Finished:
  115. return new TlsServerFinished(this.context);
  116. default:
  117. throw new InvalidOperationException("Unknown server handshake message type: " + type.ToString() );
  118. }
  119. }
  120. #endregion
  121. }
  122. }