SpnegoSecurityTokenProvider.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // SpnegoSecurityTokenProvider.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.Net;
  29. using System.Security.Principal;
  30. using System.IdentityModel.Selectors;
  31. using System.IdentityModel.Tokens;
  32. using System.ServiceModel.Channels;
  33. using System.ServiceModel.Description;
  34. using System.ServiceModel.Security.Tokens;
  35. using System.Xml;
  36. using Mono.Security;
  37. // mhm, why is this class not in S.SM.S.Tokens??
  38. namespace System.ServiceModel.Security
  39. {
  40. // Anyways we won't support SSPI until it becomes open.
  41. internal class SpnegoSecurityTokenProvider : CommunicationSecurityTokenProvider
  42. {
  43. ClientCredentialsSecurityTokenManager manager;
  44. SecurityTokenRequirement requirement;
  45. SpnegoCommunicationObject comm;
  46. public SpnegoSecurityTokenProvider (ClientCredentialsSecurityTokenManager manager, SecurityTokenRequirement requirement)
  47. {
  48. this.manager = manager;
  49. comm = new SpnegoCommunicationObject (this);
  50. }
  51. public ClientCredentialsSecurityTokenManager Manager {
  52. get { return manager; }
  53. }
  54. public override ProviderCommunicationObject Communication {
  55. get { return comm; }
  56. }
  57. public override SecurityToken GetOnlineToken (TimeSpan timeout)
  58. {
  59. return comm.GetToken (timeout);
  60. }
  61. }
  62. class SpnegoCommunicationObject : ProviderCommunicationObject
  63. {
  64. SpnegoSecurityTokenProvider owner;
  65. public SpnegoCommunicationObject (SpnegoSecurityTokenProvider owner)
  66. {
  67. this.owner = owner;
  68. }
  69. WSTrustSecurityTokenServiceProxy proxy;
  70. protected internal override TimeSpan DefaultCloseTimeout {
  71. get { throw new NotImplementedException (); }
  72. }
  73. protected internal override TimeSpan DefaultOpenTimeout {
  74. get { throw new NotImplementedException (); }
  75. }
  76. protected override void OnAbort ()
  77. {
  78. throw new NotImplementedException ();
  79. }
  80. protected override void OnOpen (TimeSpan timeout)
  81. {
  82. if (State == CommunicationState.Opened)
  83. throw new InvalidOperationException ("Already opened.");
  84. EnsureProperties ();
  85. proxy = new WSTrustSecurityTokenServiceProxy (
  86. IssuerBinding, IssuerAddress);
  87. }
  88. protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
  89. {
  90. throw new NotImplementedException ();
  91. }
  92. protected override void OnEndOpen (IAsyncResult result)
  93. {
  94. throw new NotImplementedException ();
  95. }
  96. protected override void OnClose (TimeSpan timeout)
  97. {
  98. if (proxy != null)
  99. proxy.Close ();
  100. }
  101. protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
  102. {
  103. throw new NotImplementedException ();
  104. }
  105. protected override void OnEndClose (IAsyncResult result)
  106. {
  107. throw new NotImplementedException ();
  108. }
  109. public SecurityToken GetToken (TimeSpan timeout)
  110. {
  111. bool gss = (TargetAddress.Identity == null);
  112. SspiClientSession sspi = new SspiClientSession ();
  113. WstRequestSecurityToken rst =
  114. new WstRequestSecurityToken ();
  115. // send MessageType1
  116. rst.BinaryExchange = new WstBinaryExchange (Constants.WstBinaryExchangeValueGss);
  117. // When the TargetAddress does not contain the endpoint
  118. // identity, then .net seems to use Kerberos instead of
  119. // raw NTLM.
  120. if (gss)
  121. rst.BinaryExchange.Value = sspi.ProcessSpnegoInitialContextTokenRequest ();
  122. else
  123. rst.BinaryExchange.Value = sspi.ProcessMessageType1 ();
  124. Message request = Message.CreateMessage (IssuerBinding.MessageVersion, Constants.WstIssueAction, rst);
  125. request.Headers.MessageId = new UniqueId ();
  126. request.Headers.ReplyTo = new EndpointAddress (Constants.WsaAnonymousUri);
  127. request.Headers.To = TargetAddress.Uri;
  128. MessageBuffer buffer = request.CreateBufferedCopy (0x10000);
  129. // tlsctx.StoreMessage (buffer.CreateMessage ().GetReaderAtBodyContents ());
  130. // receive MessageType2
  131. Message response = proxy.Issue (buffer.CreateMessage ());
  132. buffer = response.CreateBufferedCopy (0x10000);
  133. // tlsctx.StoreMessage (buffer.CreateMessage ().GetReaderAtBodyContents ());
  134. WSTrustRequestSecurityTokenResponseReader reader =
  135. new WSTrustRequestSecurityTokenResponseReader (Constants.WstSpnegoProofTokenType, buffer.CreateMessage ().GetReaderAtBodyContents (), SecurityTokenSerializer, null);
  136. reader.Read ();
  137. byte [] raw = reader.Value.BinaryExchange.Value;
  138. if (gss)
  139. sspi.ProcessSpnegoInitialContextTokenResponse (raw);
  140. else
  141. sspi.ProcessMessageType2 (raw);
  142. // send MessageType3
  143. WstRequestSecurityTokenResponse rstr =
  144. new WstRequestSecurityTokenResponse (SecurityTokenSerializer);
  145. rstr.Context = reader.Value.Context;
  146. rstr.BinaryExchange = new WstBinaryExchange (Constants.WstBinaryExchangeValueGss);
  147. NetworkCredential cred = owner.Manager.ClientCredentials.Windows.ClientCredential;
  148. string user = string.IsNullOrEmpty (cred.UserName) ? Environment.UserName : cred.UserName;
  149. string pass = cred.Password ?? String.Empty;
  150. if (gss)
  151. rstr.BinaryExchange.Value = sspi.ProcessSpnegoProcessContextToken (user, pass);
  152. else
  153. rstr.BinaryExchange.Value = sspi.ProcessMessageType3 (user, pass);
  154. request = Message.CreateMessage (IssuerBinding.MessageVersion, Constants.WstIssueReplyAction, rstr);
  155. request.Headers.MessageId = new UniqueId ();
  156. request.Headers.ReplyTo = new EndpointAddress (Constants.WsaAnonymousUri);
  157. request.Headers.To = TargetAddress.Uri;
  158. buffer = request.CreateBufferedCopy (0x10000);
  159. // tlsctx.StoreMessage (buffer.CreateMessage ().GetReaderAtBodyContents ());
  160. proxy = new WSTrustSecurityTokenServiceProxy (
  161. IssuerBinding, IssuerAddress);
  162. response = proxy.IssueReply (buffer.CreateMessage ());
  163. // FIXME: use correct limitation
  164. buffer = response.CreateBufferedCopy (0x10000);
  165. // don't store this message for ckhash (it's not part
  166. // of exchange)
  167. Console.WriteLine (buffer.CreateMessage ());
  168. throw new NotImplementedException ();
  169. }
  170. }
  171. }