3a94b59fdd9f8aa86c1d32b271f72319ab5fd060
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / SecurityRequestChannel.cs
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.
27 //
28
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;
45
46 using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
47
48 namespace System.ServiceModel.Channels
49 {
50         interface ISecurityChannelSource
51         {
52                 MessageSecurityBindingSupport Support { get; }
53         }
54
55         class SecurityRequestChannel : SecurityRequestChannelBase
56         {
57                 SecurityChannelFactory<IRequestChannel> source;
58
59                 public SecurityRequestChannel (IRequestChannel innerChannel, SecurityChannelFactory<IRequestChannel> source)
60                         : base (innerChannel)
61                 {
62                         this.source = source;
63                         InitializeSecurityFunctionality (source.SecuritySupport);
64                 }
65
66                 public override ChannelFactoryBase Factory {
67                         get { return source; }
68                 }
69         }
70
71         class SecurityRequestSessionChannel : SecurityRequestChannelBase
72         {
73                 SecurityChannelFactory<IRequestSessionChannel> source;
74
75                 public SecurityRequestSessionChannel (IRequestSessionChannel innerChannel, SecurityChannelFactory<IRequestSessionChannel> source)
76                         : base (innerChannel)
77                 {
78                         this.source = source;
79                         InitializeSecurityFunctionality (source.SecuritySupport);
80                 }
81
82                 public override ChannelFactoryBase Factory {
83                         get { return source; }
84                 }
85         }
86
87         abstract class SecurityRequestChannelBase : LayeredRequestChannel
88         {
89                 InitiatorMessageSecurityBindingSupport security;
90
91                 protected SecurityRequestChannelBase (IRequestChannel innerChannel)
92                         : base (innerChannel)
93                 {
94                         Opened += new EventHandler (AcquireSecurityKey);
95                         Closing += new EventHandler (ReleaseSecurityKey);
96                 }
97
98                 protected void InitializeSecurityFunctionality (InitiatorMessageSecurityBindingSupport security)
99                 {
100                         this.security = security;
101                 }
102
103                 SecurityMessageProperty secprop;
104
105                 protected override IAsyncResult OnBeginRequest (Message message, TimeSpan timeout, AsyncCallback callback, object state)
106                 {
107                         // FIXME: make it really async
108                         Message secure = SecureMessage (message);
109                         secprop = secure.Properties.Security;
110                         return base.BeginRequest (secure, timeout, callback, state);
111                 }
112
113                 protected override Message OnEndRequest (IAsyncResult result)
114                 {
115                         // FIXME: it must be also asynchronized.
116                         Message raw = base.EndRequest (result);
117                         return ProcessReply (raw, secprop);
118                 }
119
120                 protected override Message OnRequest (Message message, TimeSpan timeout)
121                 {
122                         Message secure = SecureMessage (message);
123                         Message raw = base.OnRequest (secure, timeout);
124                         return ProcessReply (raw, secure.Properties.Security);
125                 }
126
127                 Message SecureMessage (Message msg)
128                 {
129                         return new InitiatorMessageSecurityGenerator (msg, security, RemoteAddress).SecureMessage ();
130                 }
131
132                 Message ProcessReply (Message message, SecurityMessageProperty secprop)
133                 {
134                         // FIXME: provide correct parameters
135                         return new InitiatorSecureMessageDecryptor (message, secprop, security).DecryptMessage ();
136                 }
137
138                 void AcquireSecurityKey (object o, EventArgs e)
139                 {
140                         security.Prepare (Factory, RemoteAddress);
141                 }
142
143                 void ReleaseSecurityKey (object o, EventArgs e)
144                 {
145                         security.Release ();
146                 }
147         }
148 }