2
0

TlsServerSession.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // TlsServerSession.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Security.Cryptography.X509Certificates;
  31. using Mono.Security.Protocol.Tls;
  32. using Mono.Security.Protocol.Tls.Handshake;
  33. using Mono.Security.Protocol.Tls.Handshake.Server;
  34. namespace System.ServiceModel.Security.Tokens
  35. {
  36. internal class TlsServerSession : TlsSession
  37. {
  38. SslServerStream ssl;
  39. MemoryStream stream;
  40. bool mutual;
  41. public TlsServerSession (X509Certificate2 cert, bool mutual)
  42. {
  43. this.mutual = mutual;
  44. stream = new MemoryStream ();
  45. ssl = new SslServerStream (stream, cert, mutual, true, SecurityProtocolType.Tls);
  46. ssl.PrivateKeyCertSelectionDelegate = delegate (X509Certificate c, string host) {
  47. if (c.GetCertHashString () == cert.GetCertHashString ())
  48. return cert.PrivateKey;
  49. return null;
  50. };
  51. ssl.ClientCertValidationDelegate = delegate (X509Certificate certificate, int[] certificateErrors) {
  52. // FIXME: use X509CertificateValidator
  53. return true;
  54. };
  55. }
  56. protected override Context Context {
  57. get { return ssl.context; }
  58. }
  59. protected override RecordProtocol Protocol {
  60. get { return ssl.protocol; }
  61. }
  62. public void ProcessClientHello (byte [] raw)
  63. {
  64. Context.SupportedCiphers = CipherSuiteFactory.GetSupportedCiphers (Context.SecurityProtocol);
  65. Context.HandshakeState = HandshakeState.Started;
  66. stream.Write (raw, 0, raw.Length);
  67. stream.Seek (0, SeekOrigin.Begin);
  68. Protocol.ReceiveRecord (stream);
  69. }
  70. // ServerHello, ServerCertificate and ServerHelloDone
  71. public byte [] ProcessServerHello ()
  72. {
  73. Context.SessionId = Context.GetSecureRandomBytes (32);
  74. #if false
  75. // so, can I send handshake batch with RecordProtocol?
  76. stream.SetLength (0);
  77. Protocol.SendRecord (HandshakeType.ServerHello);
  78. Protocol.SendRecord (HandshakeType.Certificate);
  79. Protocol.SendRecord (HandshakeType.ServerHelloDone);
  80. stream.Flush ();
  81. return stream.ToArray ();
  82. #else
  83. MemoryStream ms = new MemoryStream ();
  84. WriteHandshake (ms);
  85. if (mutual)
  86. WriteOperations (ms,
  87. new TlsServerHello (ssl.context),
  88. new TlsServerCertificate (ssl.context),
  89. new TlsServerCertificateRequest (ssl.context),
  90. new TlsServerHelloDone (ssl.context));
  91. else
  92. WriteOperations (ms,
  93. new TlsServerHello (ssl.context),
  94. new TlsServerCertificate (ssl.context),
  95. new TlsServerHelloDone (ssl.context));
  96. return ms.ToArray ();
  97. #endif
  98. }
  99. public void ProcessClientKeyExchange (byte [] raw)
  100. {
  101. stream.SetLength (0);
  102. stream.Write (raw, 0, raw.Length);
  103. stream.Seek (0, SeekOrigin.Begin);
  104. if (mutual)
  105. Protocol.ReceiveRecord (stream); // Certificate
  106. Protocol.ReceiveRecord (stream); // ClientKeyExchange
  107. Protocol.ReceiveRecord (stream); // ChangeCipherSpec
  108. Protocol.ReceiveRecord (stream); // ClientFinished
  109. if (stream.Position != stream.Length)
  110. throw new SecurityNegotiationException (String.Format ("Unexpected SSL negotiation binary: {0} bytes of excess in {1} bytes of the octets", stream.Length - stream.Position, stream.Length));
  111. }
  112. public byte [] ProcessServerFinished ()
  113. {
  114. stream.SetLength (0);
  115. Protocol.SendChangeCipherSpec ();
  116. Protocol.SendRecord (HandshakeType.Finished);
  117. stream.Flush ();
  118. return stream.ToArray ();
  119. }
  120. public byte [] ProcessApplicationData (byte [] raw)
  121. {
  122. stream.SetLength (0);
  123. Protocol.SendRecord (ContentType.ApplicationData, raw);
  124. stream.Flush ();
  125. return stream.ToArray ();
  126. }
  127. }
  128. }