2009-07-23 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / SymmetricSecurityBindingElement.cs
1 //
2 // SymmetricSecurityBindingElement.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-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.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;
37
38 using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
39
40 namespace System.ServiceModel.Channels
41 {
42         public sealed class SymmetricSecurityBindingElement
43                 : SecurityBindingElement, IPolicyExportExtension
44         {
45                 public SymmetricSecurityBindingElement ()
46                         : this ((SecurityTokenParameters) null)
47                 {
48                 }
49
50                 public SymmetricSecurityBindingElement (
51                         SecurityTokenParameters protectionTokenParameters)
52                 {
53                         ProtectionTokenParameters = protectionTokenParameters;
54                 }
55
56                 private SymmetricSecurityBindingElement (
57                         SymmetricSecurityBindingElement other)
58                         : base (other)
59                 {
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 ();
64                 }
65
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.
71
72                 [MonoTODO]
73                 public MessageProtectionOrder MessageProtectionOrder {
74                         get { return msg_protection_order; }
75                         set { msg_protection_order = value; }
76                 }
77
78                 public SecurityTokenParameters ProtectionTokenParameters {
79                         get { return protection_token_params; }
80                         set { protection_token_params = value; }
81                 }
82
83                 [MonoTODO]
84                 public bool RequireSignatureConfirmation {
85                         get { return require_sig_confirm; }
86                         set { require_sig_confirm = value; }
87                 }
88
89                 public override void SetKeyDerivation (bool requireDerivedKeys)
90                 {
91                         base.SetKeyDerivation (requireDerivedKeys);
92                         if (ProtectionTokenParameters != null)
93                                 ProtectionTokenParameters.RequireDerivedKeys = requireDerivedKeys;
94                 }
95
96                 [MonoTODO]
97                 public override string ToString ()
98                 {
99                         return base.ToString ();
100                 }
101
102                 [MonoTODO]
103                 protected override IChannelFactory<TChannel>
104                         BuildChannelFactoryCore<TChannel> (
105                         BindingContext context)
106                 {
107                         if (ProtectionTokenParameters == null)
108                                 throw new InvalidOperationException ("Protection token parameters must be set before building channel factory.");
109
110                         SetIssuerBindingContextIfRequired (ProtectionTokenParameters, context);
111
112                         ClientCredentials cred = context.BindingParameters.Find<ClientCredentials> ();
113                         if (cred == null)
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> ();
119
120                         return new SecurityChannelFactory<TChannel> (
121                                 context.BuildInnerChannelFactory<TChannel> (), new InitiatorMessageSecurityBindingSupport (GetCapabilities (), manager, requirements));
122                 }
123
124                 [MonoTODO]
125                 protected override IChannelListener<TChannel>
126                         BuildChannelListenerCore<TChannel> (
127                         BindingContext context)
128                 {
129                         if (ProtectionTokenParameters == null)
130                                 throw new InvalidOperationException ("Protection token parameters must be set before building channel factory.");
131
132                         SetIssuerBindingContextIfRequired (ProtectionTokenParameters, context);
133
134                         ServiceCredentials cred = context.BindingParameters.Find<ServiceCredentials> ();
135                         if (cred == null)
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> ();
141
142                         return new SecurityChannelListener<TChannel> (
143                                 context.BuildInnerChannelListener<TChannel> (), new RecipientMessageSecurityBindingSupport (GetCapabilities (), manager, requirements));
144                 }
145
146                 public override BindingElement Clone ()
147                 {
148                         return new SymmetricSecurityBindingElement (this);
149                 }
150
151                 [MonoTODO]
152                 public override T GetProperty<T> (BindingContext context)
153                 {
154                         if (context == null)
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> ();
161                 }
162
163                 SymmetricSecurityCapabilities GetCapabilities ()
164                 {
165                         return new SymmetricSecurityCapabilities (this);
166                 }
167
168                 #region explicit interface implementations
169                 [MonoTODO]
170                 void IPolicyExportExtension.ExportPolicy (
171                         MetadataExporter exporter,
172                         PolicyConversionContext policyContext)
173                 {
174                         throw new NotImplementedException ();
175                 }
176                 #endregion
177         }
178 }