WSTrustSTSContract.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // WSTrustSTSContract.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006-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.Collections.ObjectModel;
  29. using System.IdentityModel.Claims;
  30. using System.IdentityModel.Selectors;
  31. using System.IdentityModel.Tokens;
  32. using System.Net.Security;
  33. using System.Runtime.Serialization;
  34. using System.Security.Cryptography.Xml;
  35. using System.ServiceModel.Channels;
  36. using System.ServiceModel.Description;
  37. using System.ServiceModel.Security;
  38. using System.ServiceModel.Security.Tokens;
  39. using System.Xml;
  40. namespace System.ServiceModel.Description
  41. {
  42. internal class WSTrustSecurityTokenServiceProxy
  43. : ClientBase<IWSTrustSecurityTokenService>, IWSTrustSecurityTokenService
  44. {
  45. public WSTrustSecurityTokenServiceProxy (Binding binding, EndpointAddress address)
  46. : base (binding, address)
  47. {
  48. }
  49. public Message Issue (Message request)
  50. {
  51. return Channel.Issue (request);
  52. }
  53. public Message IssueReply (Message request)
  54. {
  55. return Channel.IssueReply (request);
  56. }
  57. public Message Renew (Message request)
  58. {
  59. return Channel.Issue (request);
  60. }
  61. public Message Cancel (Message request)
  62. {
  63. return Channel.Issue (request);
  64. }
  65. public Message Validate (Message request)
  66. {
  67. return Channel.Issue (request);
  68. }
  69. }
  70. [ServiceContract]
  71. internal interface IWSTrustSecurityTokenService
  72. {
  73. [OperationContract (Action = Constants.WstIssueAction, ReplyAction = Constants.WstIssueReplyAction, ProtectionLevel = ProtectionLevel.EncryptAndSign)]
  74. Message Issue (Message request);
  75. [OperationContract (Action = Constants.WstIssueReplyAction, ReplyAction = Constants.WstIssueReplyAction, ProtectionLevel = ProtectionLevel.EncryptAndSign)] // needed for token negotiation reply
  76. Message IssueReply (Message request);
  77. [OperationContract (Action = Constants.WstRenewAction, ReplyAction = Constants.WstRenewReplyAction, ProtectionLevel = ProtectionLevel.EncryptAndSign)]
  78. Message Renew (Message request);
  79. [OperationContract (Action = Constants.WstCancelAction, ReplyAction = Constants.WstCancelReplyAction, ProtectionLevel = ProtectionLevel.EncryptAndSign)]
  80. Message Cancel (Message request);
  81. [OperationContract (Action = Constants.WstValidateAction, ReplyAction = Constants.WstValidateReplyAction, ProtectionLevel = ProtectionLevel.EncryptAndSign)]
  82. Message Validate (Message request);
  83. // FIXME: do we need KET here?
  84. }
  85. class WstRequestSecurityToken : WstRequestSecurityTokenBase
  86. {
  87. protected override void OnWriteBodyContents (XmlDictionaryWriter w)
  88. {
  89. w.WriteStartElement ("t", "RequestSecurityToken", Constants.WstNamespace);
  90. WriteBodyContentsCore (w);
  91. w.WriteEndElement ();
  92. }
  93. }
  94. class WstRequestSecurityTokenResponse : WstRequestSecurityTokenBase
  95. {
  96. SecurityTokenSerializer serializer;
  97. public WstRequestSecurityTokenResponse (SecurityTokenSerializer serializer)
  98. {
  99. this.serializer = serializer;
  100. }
  101. public string TokenType;
  102. public SecurityContextSecurityToken RequestedSecurityToken;
  103. public SecurityKeyIdentifierClause RequestedAttachedReference;
  104. public SecurityKeyIdentifierClause RequestedUnattachedReference;
  105. public object RequestedProofToken;
  106. public WstLifetime Lifetime;
  107. public byte [] Authenticator;
  108. // it only supports negotiation so far ...
  109. protected override void OnWriteBodyContents (XmlDictionaryWriter w)
  110. {
  111. string ns = Constants.WstNamespace;
  112. string nsu = Constants.WsuNamespace;
  113. w.WriteStartElement ("t", "RequestSecurityTokenResponse", ns);
  114. w.WriteXmlnsAttribute ("u", nsu);
  115. w.WriteAttributeString ("Context", Context);
  116. if (Authenticator != null) {
  117. w.WriteStartElement ("t", "Authenticator", ns);
  118. w.WriteStartElement ("t", "CombinedHash", ns);
  119. w.WriteBase64 (Authenticator, 0, Authenticator.Length);
  120. w.WriteEndElement ();
  121. w.WriteEndElement ();
  122. }
  123. if (TokenType != null)
  124. w.WriteElementString ("t", "TokenType", ns, TokenType);
  125. if (RequestedSecurityToken != null) {
  126. w.WriteStartElement ("t", "RequestedSecurityToken", ns);
  127. serializer.WriteToken (w, RequestedSecurityToken);
  128. w.WriteEndElement ();
  129. }
  130. if (RequestedAttachedReference != null) {
  131. w.WriteStartElement ("t", "RequestedAttachedReference", ns);
  132. serializer.WriteKeyIdentifierClause (w, RequestedAttachedReference);
  133. w.WriteEndElement ();
  134. }
  135. if (RequestedUnattachedReference != null) {
  136. w.WriteStartElement ("t", "RequestedUnattachedReference", ns);
  137. serializer.WriteKeyIdentifierClause (w, RequestedUnattachedReference);
  138. w.WriteEndElement ();
  139. }
  140. if (RequestedProofToken != null) {
  141. w.WriteStartElement ("t", "RequestedProofToken", ns);
  142. if (RequestedProofToken is SecurityToken)
  143. serializer.WriteToken (w, (SecurityToken) RequestedProofToken);
  144. else if (RequestedProofToken is SecurityKeyIdentifierClause)
  145. serializer.WriteKeyIdentifierClause (w, (SecurityKeyIdentifierClause) RequestedProofToken);
  146. else {
  147. string ens = EncryptedXml.XmlEncNamespaceUrl;
  148. w.WriteStartElement ("e", "EncryptedKey", ens);
  149. w.WriteStartElement ("EncryptionMethod", ens);
  150. w.WriteAttributeString ("Algorithm", Constants.WstTlsnegoProofTokenType);
  151. w.WriteEndElement ();
  152. w.WriteStartElement ("CipherData", ens);
  153. w.WriteStartElement ("CipherValue", ens);
  154. byte [] base64 = (byte []) RequestedProofToken;
  155. w.WriteBase64 (base64, 0, base64.Length);
  156. w.WriteEndElement ();
  157. w.WriteEndElement ();
  158. w.WriteEndElement ();
  159. }
  160. w.WriteEndElement ();
  161. }
  162. if (Lifetime != null) {
  163. w.WriteStartElement ("t", "Lifetime", ns);
  164. if (Lifetime.Created != DateTime.MinValue)
  165. w.WriteElementString ("Created", nsu, XmlConvert.ToString (Lifetime.Created.ToUniversalTime (), Constants.LifetimeFormat));
  166. if (Lifetime.Expires != DateTime.MaxValue)
  167. w.WriteElementString ("Expires", nsu, XmlConvert.ToString (Lifetime.Expires.ToUniversalTime (), Constants.LifetimeFormat));
  168. w.WriteEndElement ();
  169. }
  170. //w.WriteElementString ("t", "KeySize", ns, XmlConvert.ToString (KeySize));
  171. if (BinaryExchange != null)
  172. BinaryExchange.WriteTo (w);
  173. w.WriteEndElement ();
  174. }
  175. }
  176. abstract class WstRequestSecurityTokenBase : BodyWriter
  177. {
  178. protected WstRequestSecurityTokenBase ()
  179. : base (true)
  180. {
  181. }
  182. protected void WriteBodyContentsCore (XmlDictionaryWriter w)
  183. {
  184. string ns = Constants.WstNamespace;
  185. w.WriteAttributeString ("Context", Context);
  186. w.WriteElementString ("t", "TokenType", ns, Constants.WsscContextToken);
  187. w.WriteElementString ("t", "RequestType", ns, Constants.WstIssueRequest);
  188. w.WriteElementString ("t", "KeySize", ns, XmlConvert.ToString (KeySize));
  189. if (BinaryExchange != null)
  190. BinaryExchange.WriteTo (w);
  191. }
  192. public string Context = "uuid-" + Guid.NewGuid (); // UniqueId()-"urn:"
  193. public string RequestType;
  194. public WspAppliesTo AppliesTo;
  195. public SecurityToken Entropy;
  196. public int KeySize = 256;
  197. public string KeyType;
  198. public string ComputedKeyAlgorithm;
  199. public WstBinaryExchange BinaryExchange;
  200. }
  201. class WstRequestSecurityTokenResponseCollection : BodyWriter
  202. {
  203. public WstRequestSecurityTokenResponseCollection ()
  204. : base (true)
  205. {
  206. }
  207. Collection<WstRequestSecurityTokenResponse> responses =
  208. new Collection<WstRequestSecurityTokenResponse> ();
  209. public Collection<WstRequestSecurityTokenResponse> Responses {
  210. get { return responses; }
  211. }
  212. protected override void OnWriteBodyContents (XmlDictionaryWriter w)
  213. {
  214. w.WriteStartElement ("t", "RequestSecurityTokenResponseCollection", Constants.WstNamespace);
  215. foreach (WstRequestSecurityTokenResponse r in Responses)
  216. r.WriteBodyContents (w);
  217. w.WriteEndElement ();
  218. }
  219. public void Read (string negotiationType, XmlDictionaryReader r, SecurityTokenSerializer serializer, SecurityTokenResolver resolver)
  220. {
  221. r.MoveToContent ();
  222. r.ReadStartElement ("RequestSecurityTokenResponseCollection", Constants.WstNamespace);
  223. while (true) {
  224. r.MoveToContent ();
  225. if (r.NodeType != XmlNodeType.Element)
  226. break;
  227. WSTrustRequestSecurityTokenResponseReader rstrr = new WSTrustRequestSecurityTokenResponseReader (negotiationType, r, serializer, resolver);
  228. rstrr.Read ();
  229. responses.Add (rstrr.Value);
  230. }
  231. r.ReadEndElement ();
  232. }
  233. }
  234. class WstLifetime
  235. {
  236. public DateTime Created = DateTime.MinValue;
  237. public DateTime Expires = DateTime.MaxValue;
  238. }
  239. class WstEntropy
  240. {
  241. public object Token;
  242. }
  243. class WspAppliesTo
  244. {
  245. public WsaEndpointReference EndpointReference;
  246. }
  247. class WsaEndpointReference
  248. {
  249. public string Address;
  250. }
  251. class WstBinarySecret
  252. {
  253. public string Id;
  254. public string Type;
  255. public string Value;
  256. }
  257. class WstBinaryExchange
  258. {
  259. public WstBinaryExchange (string valueType)
  260. {
  261. ValueType = valueType;
  262. }
  263. public string ValueType;
  264. public string EncodingType = Constants.WssBase64BinaryEncodingType;
  265. public byte [] Value;
  266. public void WriteTo (XmlWriter w)
  267. {
  268. w.WriteStartElement ("t", "BinaryExchange", Constants.WstNamespace);
  269. w.WriteAttributeString ("ValueType", ValueType);
  270. w.WriteAttributeString ("EncodingType", EncodingType);
  271. w.WriteString (Convert.ToBase64String (Value));
  272. w.WriteEndElement ();
  273. }
  274. }
  275. }