2 // SecurityChannelListener.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 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.Net.Security;
31 using System.IdentityModel.Selectors;
32 using System.IdentityModel.Tokens;
33 using System.Security.Cryptography.X509Certificates;
34 using System.ServiceModel;
35 using System.ServiceModel.Description;
36 using System.ServiceModel.Security;
37 using System.ServiceModel.Security.Tokens;
39 namespace System.ServiceModel.Channels
41 internal class SecurityChannelListener<TChannel> : ChannelListenerBase<TChannel>
42 where TChannel : class, IChannel
44 IChannelListener<TChannel> inner;
45 RecipientMessageSecurityBindingSupport security;
47 public SecurityChannelListener (
48 IChannelListener<TChannel> innerListener,
49 RecipientMessageSecurityBindingSupport security)
51 inner = innerListener;
52 this.security = security;
55 public RecipientMessageSecurityBindingSupport SecuritySupport {
56 get { return security; }
59 public override T GetProperty<T> ()
61 if (typeof (T) == typeof (MessageSecurityBindingSupport))
62 return (T) (object) security;
63 return base.GetProperty<T> ();
66 TChannel CreateSecurityWrapper (TChannel src)
68 if (typeof (TChannel) == typeof (IReplyChannel))
69 return (TChannel) (object) new SecurityReplyChannel ((SecurityChannelListener<IReplyChannel>) (object) this, (IReplyChannel) (object) src);
70 throw new NotImplementedException ();
75 security.Prepare (this);
83 // ChannelListenerBase
85 protected override TChannel OnAcceptChannel (TimeSpan timeout)
87 return CreateSecurityWrapper (inner.AcceptChannel (timeout));
90 protected override IAsyncResult OnBeginAcceptChannel (TimeSpan timeout, AsyncCallback callback, object state)
92 return inner.BeginAcceptChannel (timeout, callback, state);
95 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
97 return CreateSecurityWrapper (inner.EndAcceptChannel (result));
100 protected override bool OnWaitForChannel (TimeSpan timeout)
102 return inner.WaitForChannel (timeout);
105 protected override IAsyncResult OnBeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
107 return inner.BeginWaitForChannel (timeout, callback, state);
110 protected override bool OnEndWaitForChannel (IAsyncResult result)
112 return inner.EndWaitForChannel (result);
115 public override Uri Uri {
116 get { return inner.Uri; }
119 // CommunicationObject
120 protected override void OnAbort ()
125 protected override void OnClose (TimeSpan timeout)
128 inner.Close (timeout);
131 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
134 return inner.BeginClose (timeout, callback, state);
137 protected override void OnEndClose (IAsyncResult result)
139 inner.EndClose (result);
142 protected override void OnOpen (TimeSpan timeout)
145 inner.Open (timeout);
148 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
151 return inner.BeginOpen (timeout, callback, state);
154 protected override void OnEndOpen (IAsyncResult result)
156 inner.EndOpen (result);
160 internal class SecurityReplyChannel : LayeredReplyChannel
162 SecurityChannelListener<IReplyChannel> source;
164 public SecurityReplyChannel (
165 SecurityChannelListener<IReplyChannel> source,
166 IReplyChannel innerChannel)
167 : base (innerChannel)
169 this.source = source;
172 public override ChannelListenerBase Listener {
173 get { return source; }
176 public SecurityChannelListener<IReplyChannel> Source {
177 get { return source; }
182 public override IAsyncResult BeginReceiveRequest (
183 TimeSpan timeout, AsyncCallback callback, object state)
185 throw new NotImplementedException ();
188 public override IAsyncResult BeginTryReceiveRequest (
189 TimeSpan timeout, AsyncCallback callback, object state)
191 throw new NotImplementedException ();
194 public override RequestContext EndReceiveRequest (IAsyncResult result)
196 throw new NotImplementedException ();
199 public override bool EndTryReceiveRequest (IAsyncResult result, out RequestContext context)
201 throw new NotImplementedException ();
204 public override RequestContext ReceiveRequest (TimeSpan timeout)
206 return new SecurityRequestContext (this, base.ReceiveRequest (timeout));
209 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
211 if (!base.TryReceiveRequest (timeout, out context))
213 context = new SecurityRequestContext (this, context);
219 public override T GetProperty<T> ()
222 return base.GetProperty<T> ();