2 // SymmetricSecurityBindingElement.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-2007 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
28 using System.Collections.Generic;
29 using System.Collections.ObjectModel;
30 using System.IdentityModel.Selectors;
31 using System.IdentityModel.Tokens;
32 using System.Net.Security;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Channels.Security;
35 using System.ServiceModel.Description;
36 using System.ServiceModel.Security;
37 using System.ServiceModel.Security.Tokens;
39 using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
41 namespace System.ServiceModel.Channels
43 public sealed class SymmetricSecurityBindingElement
44 : SecurityBindingElement, IPolicyExportExtension
46 public SymmetricSecurityBindingElement ()
47 : this ((SecurityTokenParameters) null)
51 public SymmetricSecurityBindingElement (
52 SecurityTokenParameters protectionTokenParameters)
54 ProtectionTokenParameters = protectionTokenParameters;
57 private SymmetricSecurityBindingElement (
58 SymmetricSecurityBindingElement other)
61 msg_protection_order = other.msg_protection_order;
62 require_sig_confirm = other.require_sig_confirm;
63 if (other.protection_token_params != null)
64 protection_token_params = other.protection_token_params.Clone ();
67 MessageProtectionOrder msg_protection_order =
68 MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature;
69 SecurityTokenParameters protection_token_params;
70 bool require_sig_confirm;
71 // make sure that they are also cloned.
74 public MessageProtectionOrder MessageProtectionOrder {
75 get { return msg_protection_order; }
76 set { msg_protection_order = value; }
79 public SecurityTokenParameters ProtectionTokenParameters {
80 get { return protection_token_params; }
81 set { protection_token_params = value; }
85 public bool RequireSignatureConfirmation {
86 get { return require_sig_confirm; }
87 set { require_sig_confirm = value; }
90 public override void SetKeyDerivation (bool requireDerivedKeys)
92 base.SetKeyDerivation (requireDerivedKeys);
93 if (ProtectionTokenParameters != null)
94 ProtectionTokenParameters.RequireDerivedKeys = requireDerivedKeys;
98 public override string ToString ()
100 return base.ToString ();
104 protected override IChannelFactory<TChannel>
105 BuildChannelFactoryCore<TChannel> (
106 BindingContext context)
108 if (ProtectionTokenParameters == null)
109 throw new InvalidOperationException ("Protection token parameters must be set before building channel factory.");
111 SetIssuerBindingContextIfRequired (ProtectionTokenParameters, context);
113 ClientCredentials cred = context.BindingParameters.Find<ClientCredentials> ();
115 // it happens when there is no ChannelFactory<T>.
116 cred = new ClientCredentials ();
117 SecurityTokenManager manager = cred.CreateSecurityTokenManager ();
118 ChannelProtectionRequirements requirements =
119 context.BindingParameters.Find<ChannelProtectionRequirements> ();
121 return new SecurityChannelFactory<TChannel> (
122 context.BuildInnerChannelFactory<TChannel> (), new InitiatorMessageSecurityBindingSupport (GetCapabilities (), manager, requirements));
126 protected override IChannelListener<TChannel>
127 BuildChannelListenerCore<TChannel> (
128 BindingContext context)
130 if (ProtectionTokenParameters == null)
131 throw new InvalidOperationException ("Protection token parameters must be set before building channel factory.");
133 SetIssuerBindingContextIfRequired (ProtectionTokenParameters, context);
135 ServiceCredentials cred = context.BindingParameters.Find<ServiceCredentials> ();
137 // it happens when there is no ChannelFactory<T>.
138 cred = new ServiceCredentials ();
139 ServiceCredentialsSecurityTokenManager manager = (ServiceCredentialsSecurityTokenManager) cred.CreateSecurityTokenManager ();
140 ChannelProtectionRequirements requirements =
141 context.BindingParameters.Find<ChannelProtectionRequirements> ();
143 return new SecurityChannelListener<TChannel> (
144 context.BuildInnerChannelListener<TChannel> (), new RecipientMessageSecurityBindingSupport (GetCapabilities (), manager, requirements));
147 public override BindingElement Clone ()
149 return new SymmetricSecurityBindingElement (this);
153 public override T GetProperty<T> (BindingContext context)
156 throw new ArgumentNullException ("context");
157 if (typeof (T) == typeof (ISecurityCapabilities))
158 return (T) (object) GetCapabilities ();
159 if (typeof (T) == typeof (IdentityVerifier))
160 throw new NotImplementedException ();
161 return base.GetProperty<T> (context);
164 SymmetricSecurityCapabilities GetCapabilities ()
166 return new SymmetricSecurityCapabilities (this);
169 #region explicit interface implementations
171 void IPolicyExportExtension.ExportPolicy (
172 MetadataExporter exporter,
173 PolicyConversionContext policyContext)
175 throw new NotImplementedException ();