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