WSTrustDec2005.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Description;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Diagnostics;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Xml;
  18. using System.IdentityModel.Claims;
  19. using System.IdentityModel.Policy;
  20. using System.IdentityModel.Tokens;
  21. using System.Security.Cryptography.X509Certificates;
  22. using System.ServiceModel.Security.Tokens;
  23. using HexBinary = System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary;
  24. using System.ServiceModel.Channels;
  25. using System.ServiceModel.Security;
  26. using System.Runtime.Serialization;
  27. using System.ServiceModel.Dispatcher;
  28. using KeyIdentifierEntry = WSSecurityTokenSerializer.KeyIdentifierEntry;
  29. using KeyIdentifierClauseEntry = WSSecurityTokenSerializer.KeyIdentifierClauseEntry;
  30. using TokenEntry = WSSecurityTokenSerializer.TokenEntry;
  31. using StrEntry = WSSecurityTokenSerializer.StrEntry;
  32. class WSTrustDec2005 : WSTrustFeb2005
  33. {
  34. public WSTrustDec2005(WSSecurityTokenSerializer tokenSerializer)
  35. : base(tokenSerializer)
  36. {
  37. }
  38. public override TrustDictionary SerializerDictionary
  39. {
  40. get { return DXD.TrustDec2005Dictionary; }
  41. }
  42. public class DriverDec2005 : WSTrustFeb2005.DriverFeb2005
  43. {
  44. public DriverDec2005(SecurityStandardsManager standardsManager)
  45. : base(standardsManager)
  46. {
  47. }
  48. public override TrustDictionary DriverDictionary
  49. {
  50. get
  51. {
  52. return DXD.TrustDec2005Dictionary;
  53. }
  54. }
  55. public override XmlDictionaryString RequestSecurityTokenResponseFinalAction
  56. {
  57. get
  58. {
  59. return DXD.TrustDec2005Dictionary.RequestSecurityTokenCollectionIssuanceFinalResponse;
  60. }
  61. }
  62. public override XmlElement CreateKeyTypeElement(SecurityKeyType keyType)
  63. {
  64. if (keyType == SecurityKeyType.BearerKey)
  65. {
  66. XmlDocument doc = new XmlDocument();
  67. XmlElement result = doc.CreateElement(this.DriverDictionary.Prefix.Value, this.DriverDictionary.KeyType.Value,
  68. this.DriverDictionary.Namespace.Value);
  69. result.AppendChild(doc.CreateTextNode(DXD.TrustDec2005Dictionary.BearerKeyType.Value));
  70. return result;
  71. }
  72. return base.CreateKeyTypeElement(keyType);
  73. }
  74. public override bool TryParseKeyTypeElement(XmlElement element, out SecurityKeyType keyType)
  75. {
  76. if (element == null)
  77. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element");
  78. if (element.LocalName == this.DriverDictionary.KeyType.Value
  79. && element.NamespaceURI == this.DriverDictionary.Namespace.Value
  80. && element.InnerText == DXD.TrustDec2005Dictionary.BearerKeyType.Value)
  81. {
  82. keyType = SecurityKeyType.BearerKey;
  83. return true;
  84. }
  85. return base.TryParseKeyTypeElement(element, out keyType);
  86. }
  87. public override XmlElement CreateRequiredClaimsElement(IEnumerable<XmlElement> claimsList)
  88. {
  89. XmlElement result = base.CreateRequiredClaimsElement(claimsList);
  90. XmlAttribute dialectAttribute = result.OwnerDocument.CreateAttribute(DXD.TrustDec2005Dictionary.Dialect.Value);
  91. dialectAttribute.Value = DXD.TrustDec2005Dictionary.DialectType.Value;
  92. result.Attributes.Append(dialectAttribute);
  93. return result;
  94. }
  95. public override IChannelFactory<IRequestChannel> CreateFederationProxy(EndpointAddress address, Binding binding, KeyedByTypeCollection<IEndpointBehavior> channelBehaviors)
  96. {
  97. if (channelBehaviors == null)
  98. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("channelBehaviors");
  99. ChannelFactory<IWsTrustDec2005SecurityTokenService> result = new ChannelFactory<IWsTrustDec2005SecurityTokenService>(binding, address);
  100. SetProtectionLevelForFederation(result.Endpoint.Contract.Operations);
  101. // remove the default client credentials that gets added to channel factories
  102. result.Endpoint.Behaviors.Remove<ClientCredentials>();
  103. for (int i = 0; i < channelBehaviors.Count; ++i)
  104. {
  105. result.Endpoint.Behaviors.Add(channelBehaviors[i]);
  106. }
  107. // add a behavior that removes the UI channel initializer added by the client credentials since there should be no UI
  108. // initializer popped up as part of obtaining the federation token (the UI should already have been popped up for the main channel)
  109. result.Endpoint.Behaviors.Add(new WSTrustFeb2005.DriverFeb2005.InteractiveInitializersRemovingBehavior());
  110. return new WSTrustFeb2005.DriverFeb2005.RequestChannelFactory<IWsTrustDec2005SecurityTokenService>(result);
  111. }
  112. public override Collection<XmlElement> ProcessUnknownRequestParameters(Collection<XmlElement> unknownRequestParameters, Collection<XmlElement> originalRequestParameters)
  113. {
  114. // For WS-Trust 1.3 we want everything in the requestSecurityTokenTemplate parameters to endup as Addtional parameters.
  115. // The parameters will appear as a child element under a XmlElement named secondaryParameters.
  116. if (originalRequestParameters == null)
  117. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("originalRequestParameters");
  118. if (originalRequestParameters.Count > 0 && originalRequestParameters[0] != null && originalRequestParameters[0].OwnerDocument != null)
  119. {
  120. XmlElement secondaryParamElement = originalRequestParameters[0].OwnerDocument.CreateElement(DXD.TrustDec2005Dictionary.Prefix.Value, DXD.TrustDec2005Dictionary.SecondaryParameters.Value, DXD.TrustDec2005Dictionary.Namespace.Value);
  121. for (int i = 0; i < originalRequestParameters.Count; ++i)
  122. {
  123. secondaryParamElement.AppendChild(originalRequestParameters[i]);
  124. }
  125. Collection<XmlElement> tempCollection = new Collection<XmlElement>();
  126. tempCollection.Add(secondaryParamElement);
  127. return tempCollection;
  128. }
  129. return originalRequestParameters;
  130. }
  131. internal virtual bool IsSecondaryParametersElement(XmlElement element)
  132. {
  133. return ((element.LocalName == DXD.TrustDec2005Dictionary.SecondaryParameters.Value) &&
  134. (element.NamespaceURI == DXD.TrustDec2005Dictionary.Namespace.Value));
  135. }
  136. public virtual XmlElement CreateKeyWrapAlgorithmElement(string keyWrapAlgorithm)
  137. {
  138. if (keyWrapAlgorithm == null)
  139. {
  140. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("keyWrapAlgorithm");
  141. }
  142. XmlDocument doc = new XmlDocument();
  143. XmlElement result = doc.CreateElement(DXD.TrustDec2005Dictionary.Prefix.Value, DXD.TrustDec2005Dictionary.KeyWrapAlgorithm.Value,
  144. DXD.TrustDec2005Dictionary.Namespace.Value);
  145. result.AppendChild(doc.CreateTextNode(keyWrapAlgorithm));
  146. return result;
  147. }
  148. internal override bool IsKeyWrapAlgorithmElement(XmlElement element, out string keyWrapAlgorithm)
  149. {
  150. return CheckElement(element, DXD.TrustDec2005Dictionary.KeyWrapAlgorithm.Value, DXD.TrustDec2005Dictionary.Namespace.Value, out keyWrapAlgorithm);
  151. }
  152. [ServiceContract]
  153. internal interface IWsTrustDec2005SecurityTokenService
  154. {
  155. [OperationContract(IsOneWay = false,
  156. Action = TrustDec2005Strings.RequestSecurityTokenIssuance,
  157. ReplyAction = TrustDec2005Strings.RequestSecurityTokenCollectionIssuanceFinalResponse)]
  158. [FaultContract(typeof(string), Action = "*", ProtectionLevel = System.Net.Security.ProtectionLevel.Sign)]
  159. Message RequestToken(Message message);
  160. }
  161. }
  162. }
  163. }