2
0

ChannelProtectionRequirements.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // ChannelProtectionRequirements.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-2006 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.Net.Security;
  30. using System.Collections.Generic;
  31. using System.ServiceModel;
  32. using System.ServiceModel.Description;
  33. using System.Xml;
  34. namespace System.ServiceModel.Security
  35. {
  36. // Represents sp:SignedParts and sp:EncryptedParts in
  37. // sp:SupportingTokens/ws:Policy/.
  38. public class ChannelProtectionRequirements
  39. {
  40. bool is_readonly;
  41. ScopedMessagePartSpecification in_enc, in_sign, out_enc, out_sign;
  42. public ChannelProtectionRequirements ()
  43. {
  44. in_enc = new ScopedMessagePartSpecification ();
  45. out_enc = new ScopedMessagePartSpecification ();
  46. in_sign = new ScopedMessagePartSpecification ();
  47. out_sign = new ScopedMessagePartSpecification ();
  48. }
  49. public ChannelProtectionRequirements (
  50. ChannelProtectionRequirements other)
  51. {
  52. if (other == null)
  53. throw new ArgumentNullException ("other");
  54. in_enc = new ScopedMessagePartSpecification (other.in_enc);
  55. out_enc = new ScopedMessagePartSpecification (other.out_enc);
  56. in_sign = new ScopedMessagePartSpecification (other.in_sign);
  57. out_sign = new ScopedMessagePartSpecification (other.out_sign);
  58. }
  59. public bool IsReadOnly {
  60. get { return is_readonly; }
  61. }
  62. public ScopedMessagePartSpecification IncomingEncryptionParts {
  63. get { return in_enc; }
  64. }
  65. public ScopedMessagePartSpecification IncomingSignatureParts {
  66. get { return in_sign; }
  67. }
  68. public ScopedMessagePartSpecification OutgoingEncryptionParts {
  69. get { return out_enc; }
  70. }
  71. public ScopedMessagePartSpecification OutgoingSignatureParts {
  72. get { return out_sign; }
  73. }
  74. public void Add (
  75. ChannelProtectionRequirements protectionRequirements)
  76. {
  77. Add (protectionRequirements, false);
  78. }
  79. public void Add (
  80. ChannelProtectionRequirements protectionRequirements,
  81. bool channelScopeOnly)
  82. {
  83. if (is_readonly)
  84. throw new InvalidOperationException ("This ChannelProtectionRequirements is read-only.");
  85. AddScopedParts (
  86. protectionRequirements.IncomingEncryptionParts,
  87. IncomingEncryptionParts,
  88. channelScopeOnly);
  89. AddScopedParts (
  90. protectionRequirements.IncomingSignatureParts,
  91. IncomingSignatureParts,
  92. channelScopeOnly);
  93. AddScopedParts (
  94. protectionRequirements.OutgoingEncryptionParts,
  95. OutgoingEncryptionParts,
  96. channelScopeOnly);
  97. AddScopedParts (
  98. protectionRequirements.OutgoingSignatureParts,
  99. OutgoingSignatureParts,
  100. channelScopeOnly);
  101. }
  102. void AddScopedParts (ScopedMessagePartSpecification src, ScopedMessagePartSpecification dst, bool channelOnly)
  103. {
  104. dst.AddParts (src.ChannelParts);
  105. if (channelOnly)
  106. return;
  107. foreach (string a in src.Actions) {
  108. MessagePartSpecification m;
  109. src.TryGetParts (a, out m);
  110. src.AddParts (m);
  111. }
  112. }
  113. public ChannelProtectionRequirements CreateInverse ()
  114. {
  115. ChannelProtectionRequirements r =
  116. new ChannelProtectionRequirements ();
  117. AddScopedParts (in_enc, r.out_enc, false);
  118. AddScopedParts (in_sign, r.out_sign, false);
  119. AddScopedParts (out_enc, r.in_enc, false);
  120. AddScopedParts (out_sign, r.in_sign, false);
  121. return r;
  122. }
  123. public void MakeReadOnly ()
  124. {
  125. is_readonly = true;
  126. in_enc.MakeReadOnly ();
  127. in_sign.MakeReadOnly ();
  128. out_enc.MakeReadOnly ();
  129. out_sign.MakeReadOnly ();
  130. }
  131. internal static ChannelProtectionRequirements CreateFromContract (ContractDescription cd)
  132. {
  133. ChannelProtectionRequirements cp =
  134. new ChannelProtectionRequirements ();
  135. List<XmlQualifiedName> enc = new List<XmlQualifiedName> ();
  136. List<XmlQualifiedName> sig = new List<XmlQualifiedName> ();
  137. if (cd.HasProtectionLevel) {
  138. switch (cd.ProtectionLevel) {
  139. case ProtectionLevel.EncryptAndSign:
  140. cp.IncomingEncryptionParts.ChannelParts.IsBodyIncluded = true;
  141. cp.OutgoingEncryptionParts.ChannelParts.IsBodyIncluded = true;
  142. goto case ProtectionLevel.Sign;
  143. case ProtectionLevel.Sign:
  144. cp.IncomingSignatureParts.ChannelParts.IsBodyIncluded = true;
  145. cp.OutgoingSignatureParts.ChannelParts.IsBodyIncluded = true;
  146. break;
  147. }
  148. }
  149. foreach (OperationDescription od in cd.Operations) {
  150. foreach (MessageDescription md in od.Messages) {
  151. enc.Clear ();
  152. sig.Clear ();
  153. ProtectionLevel mplv =
  154. md.HasProtectionLevel ? md.ProtectionLevel :
  155. od.HasProtectionLevel ? od.ProtectionLevel :
  156. ProtectionLevel.EncryptAndSign; // default
  157. foreach (MessageHeaderDescription hd in md.Headers)
  158. AddPartProtectionRequirements (enc, sig, hd, cp);
  159. ScopedMessagePartSpecification spec;
  160. bool includeBodyEnc = mplv == ProtectionLevel.EncryptAndSign;
  161. bool includeBodySig = mplv != ProtectionLevel.None;
  162. // enc
  163. spec = md.Direction == MessageDirection.Input ?
  164. cp.IncomingEncryptionParts :
  165. cp.OutgoingEncryptionParts;
  166. spec.AddParts (new MessagePartSpecification (includeBodyEnc, enc.ToArray ()), md.Action);
  167. // sig
  168. spec = md.Direction == MessageDirection.Input ?
  169. cp.IncomingSignatureParts :
  170. cp.OutgoingSignatureParts;
  171. spec.AddParts (new MessagePartSpecification (includeBodySig, sig.ToArray ()), md.Action);
  172. }
  173. }
  174. return cp;
  175. }
  176. static void AddPartProtectionRequirements (List<XmlQualifiedName> enc,
  177. List<XmlQualifiedName> sig,
  178. MessageHeaderDescription pd,
  179. ChannelProtectionRequirements cp)
  180. {
  181. if (!pd.HasProtectionLevel)
  182. return; // no specific part indication
  183. switch (pd.ProtectionLevel) {
  184. case ProtectionLevel.EncryptAndSign:
  185. enc.Add (new XmlQualifiedName (pd.Name, pd.Namespace));
  186. goto case ProtectionLevel.Sign;
  187. case ProtectionLevel.Sign:
  188. sig.Add (new XmlQualifiedName (pd.Name, pd.Namespace));
  189. break;
  190. }
  191. }
  192. }
  193. }