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
.Description
;
35 using System
.ServiceModel
.Security
;
36 using System
.ServiceModel
.Security
.Tokens
;
38 using ReqType
= System
.ServiceModel
.Security
.Tokens
.ServiceModelSecurityTokenRequirement
;
40 namespace System
.ServiceModel
.Channels
42 public sealed class SymmetricSecurityBindingElement
43 : SecurityBindingElement
, IPolicyExportExtension
45 public SymmetricSecurityBindingElement ()
46 : this ((SecurityTokenParameters
) null)
50 public SymmetricSecurityBindingElement (
51 SecurityTokenParameters protectionTokenParameters
)
53 ProtectionTokenParameters
= protectionTokenParameters
;
56 private SymmetricSecurityBindingElement (
57 SymmetricSecurityBindingElement other
)
60 msg_protection_order
= other
.msg_protection_order
;
61 require_sig_confirm
= other
.require_sig_confirm
;
62 if (other
.protection_token_params
!= null)
63 protection_token_params
= other
.protection_token_params
.Clone ();
66 MessageProtectionOrder msg_protection_order
=
67 MessageProtectionOrder
.SignBeforeEncryptAndEncryptSignature
;
68 SecurityTokenParameters protection_token_params
;
69 bool require_sig_confirm
;
70 // make sure that they are also cloned.
73 public MessageProtectionOrder MessageProtectionOrder
{
74 get { return msg_protection_order; }
75 set { msg_protection_order = value; }
78 public SecurityTokenParameters ProtectionTokenParameters
{
79 get { return protection_token_params; }
80 set { protection_token_params = value; }
84 public bool RequireSignatureConfirmation
{
85 get { return require_sig_confirm; }
86 set { require_sig_confirm = value; }
89 public override void SetKeyDerivation (bool requireDerivedKeys
)
91 base.SetKeyDerivation (requireDerivedKeys
);
92 if (ProtectionTokenParameters
!= null)
93 ProtectionTokenParameters
.RequireDerivedKeys
= requireDerivedKeys
;
97 public override string ToString ()
99 return base.ToString ();
103 protected override IChannelFactory
<TChannel
>
104 BuildChannelFactoryCore
<TChannel
> (
105 BindingContext context
)
107 if (ProtectionTokenParameters
== null)
108 throw new InvalidOperationException ("Protection token parameters must be set before building channel factory.");
110 SetIssuerBindingContextIfRequired (ProtectionTokenParameters
, context
);
112 ClientCredentials cred
= context
.BindingParameters
.Find
<ClientCredentials
> ();
114 // it happens when there is no ChannelFactory<T>.
115 cred
= new ClientCredentials ();
116 SecurityTokenManager manager
= cred
.CreateSecurityTokenManager ();
117 ChannelProtectionRequirements requirements
=
118 context
.BindingParameters
.Find
<ChannelProtectionRequirements
> ();
120 return new SecurityChannelFactory
<TChannel
> (
121 context
.BuildInnerChannelFactory
<TChannel
> (), new InitiatorMessageSecurityBindingSupport (GetCapabilities (), manager
, requirements
));
125 protected override IChannelListener
<TChannel
>
126 BuildChannelListenerCore
<TChannel
> (
127 BindingContext context
)
129 if (ProtectionTokenParameters
== null)
130 throw new InvalidOperationException ("Protection token parameters must be set before building channel factory.");
132 SetIssuerBindingContextIfRequired (ProtectionTokenParameters
, context
);
134 ServiceCredentials cred
= context
.BindingParameters
.Find
<ServiceCredentials
> ();
136 // it happens when there is no ChannelFactory<T>.
137 cred
= new ServiceCredentials ();
138 ServiceCredentialsSecurityTokenManager manager
= (ServiceCredentialsSecurityTokenManager
) cred
.CreateSecurityTokenManager ();
139 ChannelProtectionRequirements requirements
=
140 context
.BindingParameters
.Find
<ChannelProtectionRequirements
> ();
142 return new SecurityChannelListener
<TChannel
> (
143 context
.BuildInnerChannelListener
<TChannel
> (), new RecipientMessageSecurityBindingSupport (GetCapabilities (), manager
, requirements
));
146 public override BindingElement
Clone ()
148 return new SymmetricSecurityBindingElement (this);
152 public override T GetProperty
<T
> (BindingContext context
)
155 throw new ArgumentNullException ("context");
156 if (typeof (T
) == typeof (ISecurityCapabilities
))
157 return (T
) (object) GetCapabilities ();
158 if (typeof (T
) == typeof (IdentityVerifier
))
159 throw new NotImplementedException ();
160 return context
.GetInnerProperty
<T
> ();
163 SymmetricSecurityCapabilities
GetCapabilities ()
165 return new SymmetricSecurityCapabilities (this);
168 #region explicit interface implementations
170 void IPolicyExportExtension
.ExportPolicy (
171 MetadataExporter exporter
,
172 PolicyConversionContext policyContext
)
174 throw new NotImplementedException ();