2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.ServiceModel / System.ServiceModel.Channels / SecurityRequestChannel.cs
blob3a94b59fdd9f8aa86c1d32b271f72319ab5fd060
1 //
2 // SecurityRequestChannel.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 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.
29 using System;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.IdentityModel.Selectors;
33 using System.IdentityModel.Tokens;
34 using System.Runtime.Serialization;
35 using System.Security.Cryptography;
36 using System.Security.Cryptography.X509Certificates;
37 using System.Security.Cryptography.Xml;
38 using System.ServiceModel;
39 using System.ServiceModel.Channels;
40 using System.ServiceModel.Description;
41 using System.ServiceModel.Security;
42 using System.ServiceModel.Security.Tokens;
43 using System.Xml;
44 using System.Xml.XPath;
46 using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
48 namespace System.ServiceModel.Channels
50 interface ISecurityChannelSource
52 MessageSecurityBindingSupport Support { get; }
55 class SecurityRequestChannel : SecurityRequestChannelBase
57 SecurityChannelFactory<IRequestChannel> source;
59 public SecurityRequestChannel (IRequestChannel innerChannel, SecurityChannelFactory<IRequestChannel> source)
60 : base (innerChannel)
62 this.source = source;
63 InitializeSecurityFunctionality (source.SecuritySupport);
66 public override ChannelFactoryBase Factory {
67 get { return source; }
71 class SecurityRequestSessionChannel : SecurityRequestChannelBase
73 SecurityChannelFactory<IRequestSessionChannel> source;
75 public SecurityRequestSessionChannel (IRequestSessionChannel innerChannel, SecurityChannelFactory<IRequestSessionChannel> source)
76 : base (innerChannel)
78 this.source = source;
79 InitializeSecurityFunctionality (source.SecuritySupport);
82 public override ChannelFactoryBase Factory {
83 get { return source; }
87 abstract class SecurityRequestChannelBase : LayeredRequestChannel
89 InitiatorMessageSecurityBindingSupport security;
91 protected SecurityRequestChannelBase (IRequestChannel innerChannel)
92 : base (innerChannel)
94 Opened += new EventHandler (AcquireSecurityKey);
95 Closing += new EventHandler (ReleaseSecurityKey);
98 protected void InitializeSecurityFunctionality (InitiatorMessageSecurityBindingSupport security)
100 this.security = security;
103 SecurityMessageProperty secprop;
105 protected override IAsyncResult OnBeginRequest (Message message, TimeSpan timeout, AsyncCallback callback, object state)
107 // FIXME: make it really async
108 Message secure = SecureMessage (message);
109 secprop = secure.Properties.Security;
110 return base.BeginRequest (secure, timeout, callback, state);
113 protected override Message OnEndRequest (IAsyncResult result)
115 // FIXME: it must be also asynchronized.
116 Message raw = base.EndRequest (result);
117 return ProcessReply (raw, secprop);
120 protected override Message OnRequest (Message message, TimeSpan timeout)
122 Message secure = SecureMessage (message);
123 Message raw = base.OnRequest (secure, timeout);
124 return ProcessReply (raw, secure.Properties.Security);
127 Message SecureMessage (Message msg)
129 return new InitiatorMessageSecurityGenerator (msg, security, RemoteAddress).SecureMessage ();
132 Message ProcessReply (Message message, SecurityMessageProperty secprop)
134 // FIXME: provide correct parameters
135 return new InitiatorSecureMessageDecryptor (message, secprop, security).DecryptMessage ();
138 void AcquireSecurityKey (object o, EventArgs e)
140 security.Prepare (Factory, RemoteAddress);
143 void ReleaseSecurityKey (object o, EventArgs e)
145 security.Release ();