1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
//----------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Security
{
using System.Runtime;
using System.ServiceModel;
using System.ServiceModel.Channels;
sealed class DuplexSecurityProtocolFactory : SecurityProtocolFactory
{
SecurityProtocolFactory forwardProtocolFactory;
SecurityProtocolFactory reverseProtocolFactory;
bool requireSecurityOnBothDuplexDirections = true;
public DuplexSecurityProtocolFactory()
: base()
{
}
public DuplexSecurityProtocolFactory(SecurityProtocolFactory forwardProtocolFactory, SecurityProtocolFactory reverseProtocolFactory)
: this()
{
this.forwardProtocolFactory = forwardProtocolFactory;
this.reverseProtocolFactory = reverseProtocolFactory;
}
public SecurityProtocolFactory ForwardProtocolFactory
{
get
{
return this.forwardProtocolFactory;
}
set
{
ThrowIfImmutable();
this.forwardProtocolFactory = value;
}
}
SecurityProtocolFactory ProtocolFactoryForIncomingMessages
{
get
{
return this.ActAsInitiator ? this.ReverseProtocolFactory : this.ForwardProtocolFactory;
}
}
SecurityProtocolFactory ProtocolFactoryForOutgoingMessages
{
get
{
return this.ActAsInitiator ? this.ForwardProtocolFactory : this.ReverseProtocolFactory;
}
}
// If RequireSecurityOnBothDuplexDirections is set to false,
// one or both among ForwardProtocolFactory and
// ReverseProtocolFactory will be allowed to be null. The
// message directions corresponding to the null
// ProtocolFactory will have no security applied or verified.
// This mode may be used for GetPolicy message exchanges, for
// example.
public bool RequireSecurityOnBothDuplexDirections
{
get
{
return this.requireSecurityOnBothDuplexDirections;
}
set
{
ThrowIfImmutable();
this.requireSecurityOnBothDuplexDirections = value;
}
}
public SecurityProtocolFactory ReverseProtocolFactory
{
get
{
return this.reverseProtocolFactory;
}
set
{
ThrowIfImmutable();
this.reverseProtocolFactory = value;
}
}
public override bool SupportsDuplex
{
get
{
return true;
}
}
public override bool SupportsReplayDetection
{
get
{
return this.ForwardProtocolFactory != null && this.ForwardProtocolFactory.SupportsReplayDetection &&
this.ReverseProtocolFactory != null && this.ReverseProtocolFactory.SupportsReplayDetection;
}
}
public override bool SupportsRequestReply
{
get
{
return false;
}
}
public override EndpointIdentity GetIdentityOfSelf()
{
SecurityProtocolFactory factory = this.ProtocolFactoryForIncomingMessages;
if (factory != null)
{
return factory.GetIdentityOfSelf();
}
else
{
return base.GetIdentityOfSelf();
}
}
public override void OnAbort()
{
if (this.forwardProtocolFactory != null)
{
this.forwardProtocolFactory.Close(true, TimeSpan.Zero);
}
if (this.reverseProtocolFactory != null)
{
this.reverseProtocolFactory.Close(true, TimeSpan.Zero);
}
}
public override void OnClose(TimeSpan timeout)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
if (this.forwardProtocolFactory != null)
{
this.forwardProtocolFactory.Close(false, timeoutHelper.RemainingTime());
}
if (this.reverseProtocolFactory != null)
{
this.reverseProtocolFactory.Close(false, timeoutHelper.RemainingTime());
}
// no need to the close the base as it has no settings.
}
protected override SecurityProtocol OnCreateSecurityProtocol(EndpointAddress target, Uri via, object listenerSecurityState, TimeSpan timeout)
{
SecurityProtocolFactory outgoingFactory = this.ProtocolFactoryForOutgoingMessages;
SecurityProtocolFactory incomingFactory = this.ProtocolFactoryForIncomingMessages;
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
SecurityProtocol outgoing = outgoingFactory == null ? null : outgoingFactory.CreateSecurityProtocol(target, via, listenerSecurityState, false, timeoutHelper.RemainingTime());
SecurityProtocol incoming = incomingFactory == null ? null : incomingFactory.CreateSecurityProtocol(null, null, listenerSecurityState, false, timeoutHelper.RemainingTime());
return new DuplexSecurityProtocol(outgoing, incoming);
}
public override void OnOpen(TimeSpan timeout)
{
if (this.ForwardProtocolFactory != null && ReferenceEquals(this.ForwardProtocolFactory, this.ReverseProtocolFactory))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("ReverseProtocolFactory",
SR.GetString(SR.SameProtocolFactoryCannotBeSetForBothDuplexDirections));
}
if (this.forwardProtocolFactory != null)
{
this.forwardProtocolFactory.ListenUri = this.ListenUri;
}
if (this.reverseProtocolFactory != null)
{
this.reverseProtocolFactory.ListenUri = this.ListenUri;
}
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
Open(this.ForwardProtocolFactory, this.ActAsInitiator, "ForwardProtocolFactory", timeoutHelper.RemainingTime());
Open(this.ReverseProtocolFactory, !this.ActAsInitiator, "ReverseProtocolFactory", timeoutHelper.RemainingTime());
// no need to the open the base as it has no settings.
}
void Open(SecurityProtocolFactory factory, bool actAsInitiator, string propertyName, TimeSpan timeout)
{
if (factory != null)
{
factory.Open(actAsInitiator, timeout);
}
else if (this.RequireSecurityOnBothDuplexDirections)
{
OnPropertySettingsError(propertyName, true);
}
}
sealed class DuplexSecurityProtocol : SecurityProtocol
{
readonly SecurityProtocol outgoingProtocol;
readonly SecurityProtocol incomingProtocol;
public DuplexSecurityProtocol(SecurityProtocol outgoingProtocol, SecurityProtocol incomingProtocol)
: base(incomingProtocol.SecurityProtocolFactory, null, null)
{
this.outgoingProtocol = outgoingProtocol;
this.incomingProtocol = incomingProtocol;
}
public override void OnOpen(TimeSpan timeout)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
this.outgoingProtocol.Open(timeoutHelper.RemainingTime());
this.incomingProtocol.Open(timeoutHelper.RemainingTime());
}
public override void OnClose(TimeSpan timeout)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
this.outgoingProtocol.Close(false, timeoutHelper.RemainingTime());
this.incomingProtocol.Close(false, timeoutHelper.RemainingTime());
}
public override void OnAbort()
{
this.outgoingProtocol.Close(true, TimeSpan.Zero);
this.incomingProtocol.Close(true, TimeSpan.Zero);
}
public override IAsyncResult BeginSecureOutgoingMessage(Message message, TimeSpan timeout, AsyncCallback callback, object state)
{
if (this.outgoingProtocol != null)
{
return this.outgoingProtocol.BeginSecureOutgoingMessage(message, timeout, callback, state);
}
else
{
return new CompletedAsyncResult<Message>(message, callback, state);
}
}
public override IAsyncResult BeginSecureOutgoingMessage(Message message, TimeSpan timeout, SecurityProtocolCorrelationState correlationState,
AsyncCallback callback, object state)
{
if (this.outgoingProtocol != null)
{
return this.outgoingProtocol.BeginSecureOutgoingMessage(message, timeout, correlationState, callback, state);
}
else
{
return new CompletedAsyncResult<Message, SecurityProtocolCorrelationState>(message, null, callback, state);
}
}
public override void EndSecureOutgoingMessage(IAsyncResult result, out Message message)
{
if (this.outgoingProtocol != null)
{
this.outgoingProtocol.EndSecureOutgoingMessage(result, out message);
}
else
{
message = CompletedAsyncResult<Message>.End(result);
}
}
public override void EndSecureOutgoingMessage(IAsyncResult result,
out Message message, out SecurityProtocolCorrelationState newCorrelationState)
{
if (this.outgoingProtocol != null)
{
this.outgoingProtocol.EndSecureOutgoingMessage(result, out message, out newCorrelationState);
}
else
{
message = CompletedAsyncResult<Message, SecurityProtocolCorrelationState>.End(result, out newCorrelationState);
}
}
public override void SecureOutgoingMessage(ref Message message, TimeSpan timeout)
{
if (this.outgoingProtocol != null)
{
this.outgoingProtocol.SecureOutgoingMessage(ref message, timeout);
}
}
public override SecurityProtocolCorrelationState SecureOutgoingMessage(ref Message message, TimeSpan timeout, SecurityProtocolCorrelationState correlationState)
{
if (this.outgoingProtocol != null)
{
return this.outgoingProtocol.SecureOutgoingMessage(ref message, timeout, correlationState);
}
else
{
return null;
}
}
public override void VerifyIncomingMessage(ref Message message, TimeSpan timeout)
{
if (this.incomingProtocol != null)
{
this.incomingProtocol.VerifyIncomingMessage(ref message, timeout);
}
}
public override SecurityProtocolCorrelationState VerifyIncomingMessage(ref Message message, TimeSpan timeout,
params SecurityProtocolCorrelationState[] correlationStates)
{
if (this.incomingProtocol != null)
{
return this.incomingProtocol.VerifyIncomingMessage(ref message, timeout, correlationStates);
}
else
{
return null;
}
}
}
}
}
|