| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using System.Xml; |
| | | 10 | | using CoreWCF.Channels; |
| | | 11 | | using CoreWCF.Configuration; |
| | | 12 | | using CoreWCF.Description; |
| | | 13 | | using CoreWCF.Diagnostics; |
| | | 14 | | using CoreWCF.Dispatcher; |
| | | 15 | | using CoreWCF.IdentityModel; |
| | | 16 | | using CoreWCF.IdentityModel.Claims; |
| | | 17 | | using CoreWCF.IdentityModel.Selectors; |
| | | 18 | | using CoreWCF.IdentityModel.Tokens; |
| | | 19 | | using CoreWCF.Runtime; |
| | | 20 | | using CoreWCF.Security.Tokens; |
| | | 21 | | using Microsoft.Extensions.DependencyInjection; |
| | | 22 | | |
| | | 23 | | namespace CoreWCF.Security |
| | | 24 | | { |
| | | 25 | | internal sealed class SecuritySessionServerSettings : IServiceDispatcherSecureConversationSessionSettings, ISecurity |
| | | 26 | | { |
| | 4 | 27 | | internal static readonly TimeSpan s_defaultKeyRenewalInterval = TimeSpan.FromHours(15); |
| | 4 | 28 | | internal static readonly TimeSpan s_defaultKeyRolloverInterval = TimeSpan.FromMinutes(5); |
| | | 29 | | internal const bool DefaultTolerateTransportFailures = true; |
| | | 30 | | internal const int DefaultMaximumPendingSessions = 128; |
| | 4 | 31 | | internal static readonly TimeSpan s_defaultInactivityTimeout = TimeSpan.FromMinutes(2); |
| | | 32 | | private int _maximumPendingSessions; |
| | | 33 | | private Dictionary<UniqueId, SecurityContextSecurityToken> _pendingSessions1; |
| | | 34 | | private Dictionary<UniqueId, SecurityContextSecurityToken> _pendingSessions2; |
| | | 35 | | private Dictionary<UniqueId, MessageFilter> _sessionFilters; |
| | | 36 | | private IOThreadTimer _inactivityTimer; |
| | | 37 | | private TimeSpan _inactivityTimeout; |
| | | 38 | | private bool _tolerateTransportFailures; |
| | | 39 | | private TimeSpan _maximumKeyRenewalInterval; |
| | | 40 | | private TimeSpan _keyRolloverInterval; |
| | | 41 | | private int _maximumPendingKeysPerSession; |
| | | 42 | | private SecurityProtocolFactory _sessionProtocolFactory; |
| | | 43 | | private readonly Dictionary<UniqueId, IServerSecuritySessionChannel> _activeSessions; |
| | | 44 | | private SecurityServiceDispatcher _securityServiceDispatcher; |
| | | 45 | | private ChannelBuilder _channelBuilder; |
| | | 46 | | private SecurityStandardsManager _standardsManager; |
| | | 47 | | private SecurityTokenParameters _issuedTokenParameters; |
| | | 48 | | private SecurityTokenResolver _sessionTokenResolver; |
| | | 49 | | private bool _acceptNewWork; |
| | | 50 | | private Uri _listenUri; |
| | | 51 | | private SecurityListenerSettingsLifetimeManager _settingsLifetimeManager; |
| | | 52 | | |
| | 22 | 53 | | public SecuritySessionServerSettings() |
| | | 54 | | { |
| | 22 | 55 | | _activeSessions = new Dictionary<UniqueId, IServerSecuritySessionChannel>(); |
| | 22 | 56 | | _maximumKeyRenewalInterval = s_defaultKeyRenewalInterval; |
| | 22 | 57 | | _maximumPendingKeysPerSession = 5; |
| | 22 | 58 | | _keyRolloverInterval = s_defaultKeyRolloverInterval; |
| | 22 | 59 | | _inactivityTimeout = s_defaultInactivityTimeout; |
| | 22 | 60 | | _tolerateTransportFailures = DefaultTolerateTransportFailures; |
| | 22 | 61 | | _maximumPendingSessions = DefaultMaximumPendingSessions; |
| | 22 | 62 | | WrapperCommunicationObj = new WrapperSecurityCommunicationObject(this); |
| | 22 | 63 | | } |
| | | 64 | | |
| | | 65 | | internal ChannelBuilder ChannelBuilder |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | 20 | 69 | | return _channelBuilder; |
| | | 70 | | } |
| | | 71 | | set |
| | | 72 | | { |
| | 22 | 73 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 74 | | _channelBuilder = value; |
| | 22 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 626 | 78 | | internal WrapperSecurityCommunicationObject WrapperCommunicationObj { get; } |
| | | 79 | | |
| | | 80 | | internal SecurityListenerSettingsLifetimeManager SettingsLifetimeManager |
| | | 81 | | { |
| | | 82 | | get |
| | | 83 | | { |
| | 10 | 84 | | return _settingsLifetimeManager; |
| | | 85 | | } |
| | | 86 | | set |
| | | 87 | | { |
| | 22 | 88 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 89 | | _settingsLifetimeManager = value; |
| | 22 | 90 | | } |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | internal SecurityServiceDispatcher SecurityServiceDispatcher |
| | | 94 | | { |
| | | 95 | | get |
| | | 96 | | { |
| | 32 | 97 | | return _securityServiceDispatcher; |
| | | 98 | | } |
| | | 99 | | set |
| | | 100 | | { |
| | 44 | 101 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 44 | 102 | | _securityServiceDispatcher = value; |
| | 44 | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// AcceptorChannelType will help determine if it's a duplex or simple reply channel |
| | | 108 | | /// </summary> |
| | 64 | 109 | | internal Type AcceptorChannelType { get; set; } |
| | | 110 | | |
| | | 111 | | // TODO: Used by security tracing |
| | | 112 | | //private Uri Uri |
| | | 113 | | //{ |
| | | 114 | | // get |
| | | 115 | | // { |
| | | 116 | | // WrapperCommunicationObj.ThrowIfNotOpened(); |
| | | 117 | | // return _listenUri; |
| | | 118 | | // } |
| | | 119 | | //} |
| | | 120 | | |
| | 116 | 121 | | internal object ThisGlobalLock { get; } = new object(); |
| | | 122 | | |
| | 142 | 123 | | public SecurityTokenAuthenticator SessionTokenAuthenticator { get; private set; } |
| | | 124 | | |
| | 32 | 125 | | public ISecurityContextSecurityTokenCache SessionTokenCache { get; private set; } |
| | | 126 | | |
| | 10 | 127 | | public SecurityTokenResolver SessionTokenResolver => _sessionTokenResolver; |
| | | 128 | | |
| | | 129 | | public SecurityTokenParameters IssuedSecurityTokenParameters |
| | | 130 | | { |
| | | 131 | | get |
| | | 132 | | { |
| | 54 | 133 | | return _issuedTokenParameters; |
| | | 134 | | } |
| | | 135 | | set |
| | | 136 | | { |
| | 22 | 137 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 138 | | _issuedTokenParameters = value; |
| | 22 | 139 | | } |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | internal SecurityStandardsManager SecurityStandardsManager |
| | | 143 | | { |
| | | 144 | | get |
| | | 145 | | { |
| | 104 | 146 | | return _standardsManager; |
| | | 147 | | } |
| | | 148 | | set |
| | | 149 | | { |
| | 22 | 150 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 151 | | _standardsManager = value; |
| | 22 | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | public bool TolerateTransportFailures |
| | | 156 | | { |
| | | 157 | | get |
| | | 158 | | { |
| | 0 | 159 | | return _tolerateTransportFailures; |
| | | 160 | | } |
| | | 161 | | set |
| | | 162 | | { |
| | 22 | 163 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 164 | | _tolerateTransportFailures = value; |
| | 22 | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | 44 | 168 | | public bool CanRenewSession { get; set; } = true; |
| | | 169 | | |
| | | 170 | | public int MaximumPendingSessions |
| | | 171 | | { |
| | | 172 | | get |
| | | 173 | | { |
| | 10 | 174 | | return _maximumPendingSessions; |
| | | 175 | | } |
| | | 176 | | set |
| | | 177 | | { |
| | 22 | 178 | | if (value <= 0) |
| | | 179 | | { |
| | 0 | 180 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 181 | | } |
| | | 182 | | |
| | 22 | 183 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 184 | | _maximumPendingSessions = value; |
| | 22 | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | public TimeSpan InactivityTimeout |
| | | 189 | | { |
| | | 190 | | get |
| | | 191 | | { |
| | 0 | 192 | | return _inactivityTimeout; |
| | | 193 | | } |
| | | 194 | | set |
| | | 195 | | { |
| | 22 | 196 | | if (value <= TimeSpan.Zero) |
| | | 197 | | { |
| | 0 | 198 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 199 | | } |
| | | 200 | | |
| | 22 | 201 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 202 | | _inactivityTimeout = value; |
| | 22 | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | public TimeSpan MaximumKeyRenewalInterval |
| | | 207 | | { |
| | | 208 | | get |
| | | 209 | | { |
| | 0 | 210 | | return _maximumKeyRenewalInterval; |
| | | 211 | | } |
| | | 212 | | set |
| | | 213 | | { |
| | 22 | 214 | | if (value <= TimeSpan.Zero) |
| | | 215 | | { |
| | 0 | 216 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 217 | | } |
| | 22 | 218 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 219 | | _maximumKeyRenewalInterval = value; |
| | 22 | 220 | | } |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | public TimeSpan KeyRolloverInterval |
| | | 224 | | { |
| | | 225 | | get |
| | | 226 | | { |
| | 0 | 227 | | return _keyRolloverInterval; |
| | | 228 | | } |
| | | 229 | | set |
| | | 230 | | { |
| | 22 | 231 | | if (value <= TimeSpan.Zero) |
| | | 232 | | { |
| | 0 | 233 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 234 | | } |
| | 22 | 235 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 236 | | _keyRolloverInterval = value; |
| | 22 | 237 | | } |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | public int MaximumPendingKeysPerSession |
| | | 241 | | { |
| | | 242 | | get |
| | | 243 | | { |
| | 0 | 244 | | return _maximumPendingKeysPerSession; |
| | | 245 | | } |
| | | 246 | | set |
| | | 247 | | { |
| | 0 | 248 | | if (value <= 0) |
| | | 249 | | { |
| | 0 | 250 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 251 | | } |
| | 0 | 252 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 0 | 253 | | _maximumPendingKeysPerSession = value; |
| | 0 | 254 | | } |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | public SecurityProtocolFactory SessionProtocolFactory |
| | | 258 | | { |
| | | 259 | | get |
| | | 260 | | { |
| | 42 | 261 | | return _sessionProtocolFactory; |
| | | 262 | | } |
| | | 263 | | set |
| | | 264 | | { |
| | 22 | 265 | | WrapperCommunicationObj.ThrowIfDisposedOrImmutable(); |
| | 22 | 266 | | _sessionProtocolFactory = value; |
| | 22 | 267 | | } |
| | | 268 | | } |
| | | 269 | | |
| | 32 | 270 | | public MessageVersion MessageVersion { get; private set; } |
| | | 271 | | |
| | | 272 | | // ISecurityCommunicationObject members |
| | 44 | 273 | | public TimeSpan DefaultOpenTimeout => ServiceDefaults.OpenTimeout; |
| | | 274 | | |
| | 0 | 275 | | public TimeSpan DefaultCloseTimeout => ServiceDefaults.CloseTimeout; |
| | | 276 | | |
| | | 277 | | public void OnFaulted() |
| | | 278 | | { |
| | 0 | 279 | | } |
| | | 280 | | |
| | | 281 | | public void OnOpened() |
| | | 282 | | { |
| | 0 | 283 | | } |
| | | 284 | | |
| | | 285 | | public void OnOpening() |
| | | 286 | | { |
| | 0 | 287 | | } |
| | | 288 | | |
| | | 289 | | public void OnAbort() |
| | | 290 | | { |
| | 0 | 291 | | TimeoutHelper timeoutHelper = new TimeoutHelper(ServiceDefaults.ServiceHostCloseTimeout); |
| | 0 | 292 | | AbortPendingChannels(timeoutHelper.GetCancellationToken()); |
| | 0 | 293 | | OnAbortCore(); |
| | 0 | 294 | | } |
| | | 295 | | |
| | | 296 | | internal void Abort() |
| | | 297 | | { |
| | 0 | 298 | | WrapperCommunicationObj.Abort(); |
| | 0 | 299 | | } |
| | | 300 | | |
| | | 301 | | private void OnCloseCore(TimeSpan timeout) |
| | | 302 | | { |
| | 0 | 303 | | TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); |
| | 0 | 304 | | ClearPendingSessions(); |
| | 0 | 305 | | ClosePendingChannels(timeoutHelper.GetCancellationToken()); |
| | 0 | 306 | | if (_inactivityTimer != null) |
| | | 307 | | { |
| | 0 | 308 | | _inactivityTimer.Cancel(); |
| | | 309 | | } |
| | 0 | 310 | | if (_sessionProtocolFactory != null) |
| | | 311 | | { |
| | 0 | 312 | | _sessionProtocolFactory.OnCloseAsync(timeoutHelper.RemainingTime()); |
| | | 313 | | } |
| | 0 | 314 | | if (SessionTokenAuthenticator != null) |
| | | 315 | | { |
| | 0 | 316 | | SecurityUtils.CloseTokenAuthenticatorIfRequiredAsync(SessionTokenAuthenticator, timeoutHelper.GetCancell |
| | | 317 | | } |
| | 0 | 318 | | } |
| | | 319 | | |
| | | 320 | | private void OnAbortCore() |
| | | 321 | | { |
| | 0 | 322 | | if (_inactivityTimer != null) |
| | | 323 | | { |
| | 0 | 324 | | _inactivityTimer.Cancel(); |
| | | 325 | | } |
| | 0 | 326 | | if (_sessionProtocolFactory != null) |
| | | 327 | | { |
| | 0 | 328 | | _sessionProtocolFactory.OnCloseAsync(TimeSpan.Zero); |
| | | 329 | | } |
| | 0 | 330 | | if (SessionTokenAuthenticator != null) |
| | | 331 | | { |
| | 0 | 332 | | SecurityUtils.AbortTokenAuthenticatorIfRequired(SessionTokenAuthenticator); |
| | | 333 | | } |
| | 0 | 334 | | } |
| | | 335 | | |
| | | 336 | | private Task SetupSessionTokenAuthenticatorAsync() |
| | | 337 | | { |
| | 22 | 338 | | RecipientServiceModelSecurityTokenRequirement requirement = new RecipientServiceModelSecurityTokenRequiremen |
| | 22 | 339 | | _issuedTokenParameters.InitializeSecurityTokenRequirement(requirement); |
| | 22 | 340 | | requirement.KeyUsage = SecurityKeyUsage.Signature; |
| | 22 | 341 | | requirement.ListenUri = _listenUri; |
| | 22 | 342 | | requirement.SecurityBindingElement = _sessionProtocolFactory.SecurityBindingElement; |
| | 22 | 343 | | requirement.SecurityAlgorithmSuite = _sessionProtocolFactory.IncomingAlgorithmSuite; |
| | 22 | 344 | | requirement.SupportSecurityContextCancellation = true; |
| | 22 | 345 | | requirement.MessageSecurityVersion = _sessionProtocolFactory.MessageSecurityVersion.SecurityTokenVersion; |
| | | 346 | | // requirement.AuditLogLocation = sessionProtocolFactory.AuditLogLocation; |
| | | 347 | | // requirement.SuppressAuditFailure = sessionProtocolFactory.SuppressAuditFailure; |
| | | 348 | | // requirement.MessageAuthenticationAuditLevel = sessionProtocolFactory.MessageAuthenticationAuditLevel; |
| | 22 | 349 | | requirement.Properties[ServiceModelSecurityTokenRequirement.MessageDirectionProperty] = MessageDirection.Inp |
| | 22 | 350 | | if (_sessionProtocolFactory.EndpointFilterTable != null) |
| | | 351 | | { |
| | 0 | 352 | | requirement.Properties[ServiceModelSecurityTokenRequirement.EndpointFilterTableProperty] = _sessionProto |
| | | 353 | | } |
| | 22 | 354 | | SessionTokenAuthenticator = _sessionProtocolFactory.SecurityTokenManager.CreateSecurityTokenAuthenticator(re |
| | 22 | 355 | | if (!(SessionTokenAuthenticator is IIssuanceSecurityTokenAuthenticator)) |
| | | 356 | | { |
| | 0 | 357 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sec |
| | | 358 | | } |
| | 22 | 359 | | if (_sessionTokenResolver == null || (!(_sessionTokenResolver is ISecurityContextSecurityTokenCache))) |
| | | 360 | | { |
| | 0 | 361 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sec |
| | | 362 | | } |
| | 22 | 363 | | SessionTokenCache = (ISecurityContextSecurityTokenCache)_sessionTokenResolver; |
| | 22 | 364 | | return Task.CompletedTask; |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | public void StopAcceptingNewWork() |
| | | 368 | | { |
| | 0 | 369 | | _acceptNewWork = false; |
| | 0 | 370 | | } |
| | | 371 | | |
| | | 372 | | private int GetPendingSessionCount() |
| | | 373 | | { |
| | 10 | 374 | | return _pendingSessions1.Count + _pendingSessions2.Count; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | private void AbortPendingChannels(CancellationToken token) |
| | | 378 | | { |
| | 0 | 379 | | ClosePendingChannels(token); |
| | 0 | 380 | | } |
| | | 381 | | |
| | | 382 | | private async void ClosePendingChannels(CancellationToken token) |
| | | 383 | | { |
| | 0 | 384 | | var tasks = new Task[_activeSessions.Count]; |
| | 0 | 385 | | lock (ThisGlobalLock) |
| | | 386 | | { |
| | 0 | 387 | | int index = 0; |
| | 0 | 388 | | if (typeof(IReplyChannel).Equals(AcceptorChannelType)) |
| | | 389 | | { |
| | 0 | 390 | | foreach (ServerSecuritySimplexSessionChannel securitySessionSimplexChannel in _activeSessions.Values |
| | | 391 | | { |
| | 0 | 392 | | tasks[index] = securitySessionSimplexChannel.CloseAsync(token); |
| | 0 | 393 | | index++; |
| | | 394 | | } |
| | | 395 | | } |
| | 0 | 396 | | } |
| | 0 | 397 | | await Task.WhenAll(tasks); |
| | 0 | 398 | | } |
| | | 399 | | |
| | | 400 | | private void ConfigureSessionSecurityProtocolFactory() |
| | | 401 | | { |
| | | 402 | | //TODO while implementing message security |
| | | 403 | | |
| | | 404 | | //if (this.sessionProtocolFactory is SessionSymmetricMessageSecurityProtocolFactory) |
| | | 405 | | //{ |
| | | 406 | | // AddressingVersion addressing = MessageVersion.Default.Addressing; |
| | | 407 | | // if (this.channelBuilder != null) |
| | | 408 | | // { |
| | | 409 | | // MessageEncodingBindingElement encoding = this.channelBuilder.Binding.Elements.Find<MessageEncoding |
| | | 410 | | // if (encoding != null) |
| | | 411 | | // { |
| | | 412 | | // addressing = encoding.MessageVersion.Addressing; |
| | | 413 | | // } |
| | | 414 | | // } |
| | | 415 | | |
| | | 416 | | // if (addressing != AddressingVersion.WSAddressing10 && addressing != AddressingVersion.WSAddressingAugu |
| | | 417 | | // { |
| | | 418 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | | 419 | | // new ProtocolException(SR.Format(SR.AddressingVersionNotSupported, addressing))); |
| | | 420 | | // } |
| | | 421 | | |
| | | 422 | | // SessionSymmetricMessageSecurityProtocolFactory messagePf = (SessionSymmetricMessageSecurityProtocolFac |
| | | 423 | | // if (!messagePf.ApplyIntegrity || !messagePf.RequireIntegrity) |
| | | 424 | | // { |
| | | 425 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format( |
| | | 426 | | // } |
| | | 427 | | // MessagePartSpecification bodyPart = new MessagePartSpecification(true); |
| | | 428 | | // messagePf.ProtectionRequirements.IncomingSignatureParts.AddParts(bodyPart, this.SecurityStandardsManag |
| | | 429 | | // messagePf.ProtectionRequirements.IncomingSignatureParts.AddParts(bodyPart, this.SecurityStandardsManag |
| | | 430 | | // messagePf.ProtectionRequirements.OutgoingSignatureParts.AddParts(bodyPart, this.SecurityStandardsManag |
| | | 431 | | // messagePf.ProtectionRequirements.OutgoingSignatureParts.AddParts(bodyPart, this.SecurityStandardsManag |
| | | 432 | | // messagePf.ProtectionRequirements.OutgoingSignatureParts.AddParts(bodyPart, addressing.FaultAction); |
| | | 433 | | // messagePf.ProtectionRequirements.OutgoingSignatureParts.AddParts(bodyPart, addressing.DefaultFaultActi |
| | | 434 | | // messagePf.ProtectionRequirements.OutgoingSignatureParts.AddParts(bodyPart, DotNetSecurityStrings.Secur |
| | | 435 | | // if (messagePf.ApplyConfidentiality) |
| | | 436 | | // { |
| | | 437 | | // messagePf.ProtectionRequirements.OutgoingEncryptionParts.AddParts(MessagePartSpecification.NoParts |
| | | 438 | | // messagePf.ProtectionRequirements.OutgoingEncryptionParts.AddParts(MessagePartSpecification.NoParts |
| | | 439 | | // messagePf.ProtectionRequirements.OutgoingEncryptionParts.AddParts(bodyPart, addressing.FaultAction |
| | | 440 | | // messagePf.ProtectionRequirements.OutgoingEncryptionParts.AddParts(bodyPart, addressing.DefaultFaul |
| | | 441 | | // messagePf.ProtectionRequirements.OutgoingEncryptionParts.AddParts(bodyPart, DotNetSecurityStrings. |
| | | 442 | | // } |
| | | 443 | | // if (messagePf.RequireConfidentiality) |
| | | 444 | | // { |
| | | 445 | | // messagePf.ProtectionRequirements.IncomingEncryptionParts.AddParts(MessagePartSpecification.NoParts |
| | | 446 | | // messagePf.ProtectionRequirements.IncomingEncryptionParts.AddParts(MessagePartSpecification.NoParts |
| | | 447 | | // } |
| | | 448 | | // messagePf.SecurityTokenParameters = this.IssuedSecurityTokenParameters; |
| | | 449 | | //} |
| | | 450 | | //else |
| | 22 | 451 | | if (_sessionProtocolFactory is SessionSymmetricTransportSecurityProtocolFactory sessionSymmetricProtocolFact |
| | | 452 | | { |
| | 22 | 453 | | sessionSymmetricProtocolFactory.AddTimestamp = true; |
| | 22 | 454 | | sessionSymmetricProtocolFactory.SecurityTokenParameters = IssuedSecurityTokenParameters; |
| | 22 | 455 | | sessionSymmetricProtocolFactory.SecurityTokenParameters.RequireDerivedKeys = false; |
| | | 456 | | } |
| | | 457 | | else |
| | | 458 | | { |
| | 0 | 459 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 460 | | } |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | private void OnTokenRenewed(SecurityToken newToken, SecurityToken oldToken) |
| | | 464 | | { |
| | 0 | 465 | | WrapperCommunicationObj.ThrowIfClosed(); |
| | 0 | 466 | | if (!_acceptNewWork) |
| | | 467 | | { |
| | 0 | 468 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(SR.SecurityListe |
| | | 469 | | } |
| | 0 | 470 | | if (!(newToken is SecurityContextSecurityToken newSecurityContextToken)) |
| | | 471 | | { |
| | 0 | 472 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.SessionToke |
| | | 473 | | } |
| | 0 | 474 | | if (!(oldToken is SecurityContextSecurityToken oldSecurityContextToken)) |
| | | 475 | | { |
| | 0 | 476 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.SessionToke |
| | | 477 | | } |
| | 0 | 478 | | IServerSecuritySessionChannel sessionChannel = FindSessionChannel(newSecurityContextToken.ContextId); |
| | 0 | 479 | | if (sessionChannel == null) |
| | | 480 | | { |
| | 0 | 481 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(SR.Ca |
| | | 482 | | } |
| | 0 | 483 | | sessionChannel.RenewSessionToken(newSecurityContextToken, oldSecurityContextToken); |
| | 0 | 484 | | } |
| | | 485 | | |
| | | 486 | | /// <summary> |
| | | 487 | | /// This method creates SessionInitiationMessageServiceDispatcher which would act as |
| | | 488 | | /// holder for SecurityReplySessionServiceChannelDispatcher (for Duplex we can implement simillar to ServerSecur |
| | | 489 | | /// Even though the Dispatcher is being added to demuxer, the ServiceChannelDispatcher is lazily initialized(bas |
| | | 490 | | /// When close received, the ServiceChannelDispatcher is cleared as well as the Dispatcher from Demuxer. |
| | | 491 | | /// </summary> |
| | | 492 | | /// <param name="sessionToken"></param> |
| | | 493 | | private void CreateSessionMessageServiceDispatcher(SecurityContextSecurityToken sessionToken, EndpointAddress re |
| | | 494 | | { |
| | 10 | 495 | | lock (ThisGlobalLock) |
| | | 496 | | { |
| | 10 | 497 | | MessageFilter sctFilter = new SecuritySessionFilter(sessionToken.ContextId, _sessionProtocolFactory.Stan |
| | 10 | 498 | | SessionInitiationMessageServiceDispatcher sessionServiceDispatcher |
| | 10 | 499 | | = new SessionInitiationMessageServiceDispatcher(this, sessionToken, sctFilter, remoteAddress); |
| | | 500 | | //logic to separate for Duplex |
| | 10 | 501 | | if (typeof(IReplyChannel).Equals(AcceptorChannelType)) |
| | | 502 | | { |
| | 8 | 503 | | ChannelBuilder.AddServiceDispatcher<IReplyChannel>(sessionServiceDispatcher, new ChannelDemuxerFilte |
| | | 504 | | } |
| | 2 | 505 | | else if (typeof(IDuplexSessionChannel).Equals(AcceptorChannelType)) |
| | | 506 | | { |
| | 2 | 507 | | ChannelBuilder.AddServiceDispatcher<IDuplexSessionChannel>(sessionServiceDispatcher, new ChannelDemu |
| | | 508 | | } |
| | | 509 | | |
| | 10 | 510 | | AddPendingSession(sessionToken.ContextId, sessionToken, sctFilter); |
| | 10 | 511 | | } |
| | 10 | 512 | | } |
| | | 513 | | |
| | | 514 | | private void OnTokenIssued(SecurityToken issuedToken, EndpointAddress tokenRequestor) |
| | | 515 | | { |
| | 10 | 516 | | WrapperCommunicationObj.ThrowIfClosed(); //TODO mark open |
| | 10 | 517 | | if (!_acceptNewWork) |
| | | 518 | | { |
| | 0 | 519 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(SR.SecurityListe |
| | | 520 | | } |
| | 10 | 521 | | if (!(issuedToken is SecurityContextSecurityToken issuedSecurityContextToken)) |
| | | 522 | | { |
| | 0 | 523 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.SessionToke |
| | | 524 | | } |
| | 10 | 525 | | CreateSessionMessageServiceDispatcher(issuedSecurityContextToken, tokenRequestor ?? EndpointAddress.Anonymou |
| | 10 | 526 | | } |
| | | 527 | | |
| | | 528 | | internal SecurityContextSecurityToken GetSecurityContextSecurityToken(UniqueId sessionId) |
| | | 529 | | { |
| | 0 | 530 | | if (_pendingSessions1 != null && _pendingSessions1.ContainsKey(sessionId)) |
| | | 531 | | { |
| | 0 | 532 | | return _pendingSessions1[sessionId]; |
| | | 533 | | } |
| | | 534 | | |
| | 0 | 535 | | if (_pendingSessions2 != null && _pendingSessions2.ContainsKey(sessionId)) |
| | | 536 | | { |
| | 0 | 537 | | return _pendingSessions2[sessionId]; |
| | | 538 | | } |
| | | 539 | | |
| | 0 | 540 | | return null; |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | private void OnTimer(object state) |
| | | 544 | | { |
| | 44 | 545 | | if (WrapperCommunicationObj.State == CommunicationState.Closed |
| | 44 | 546 | | || WrapperCommunicationObj.State == CommunicationState.Faulted) |
| | | 547 | | { |
| | 0 | 548 | | return; |
| | | 549 | | } |
| | | 550 | | try |
| | | 551 | | { |
| | 44 | 552 | | ClearPendingSessions(); |
| | 44 | 553 | | } |
| | | 554 | | catch (Exception e) |
| | | 555 | | { |
| | 0 | 556 | | if (Fx.IsFatal(e)) |
| | | 557 | | { |
| | 0 | 558 | | throw; |
| | | 559 | | } |
| | 0 | 560 | | } |
| | | 561 | | finally |
| | | 562 | | { |
| | 44 | 563 | | if (WrapperCommunicationObj.State != CommunicationState.Closed |
| | 44 | 564 | | && WrapperCommunicationObj.State != CommunicationState.Closing |
| | 44 | 565 | | && WrapperCommunicationObj.State != CommunicationState.Faulted) |
| | | 566 | | { |
| | 44 | 567 | | _inactivityTimer.Set(_inactivityTimeout); |
| | | 568 | | } |
| | 44 | 569 | | } |
| | 44 | 570 | | } |
| | | 571 | | |
| | | 572 | | private void AddPendingSession(UniqueId sessionId, SecurityContextSecurityToken securityToken, MessageFilter fil |
| | | 573 | | { |
| | 10 | 574 | | lock (ThisGlobalLock) |
| | | 575 | | { |
| | 10 | 576 | | if ((GetPendingSessionCount() + 1) > MaximumPendingSessions) |
| | | 577 | | { |
| | 0 | 578 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QuotaExceededException(SR.SecuritySess |
| | | 579 | | } |
| | 10 | 580 | | if (_pendingSessions1.ContainsKey(sessionId) || _pendingSessions2.ContainsKey(sessionId)) |
| | | 581 | | { |
| | 0 | 582 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(S |
| | | 583 | | } |
| | 10 | 584 | | _pendingSessions1.Add(sessionId, securityToken); |
| | 10 | 585 | | _sessionFilters.Add(sessionId, filter); |
| | 10 | 586 | | } |
| | | 587 | | //SecurityTraceRecordHelper.TracePendingSessionAdded(sessionId, this.Uri); |
| | | 588 | | //if (TD.SecuritySessionRatioIsEnabled()) |
| | | 589 | | //{ |
| | | 590 | | // TD.SecuritySessionRatio(GetPendingSessionCount(), this.MaximumPendingSessions); |
| | | 591 | | //} |
| | 10 | 592 | | } |
| | | 593 | | |
| | | 594 | | private void ClearPendingSessions() |
| | | 595 | | { |
| | 44 | 596 | | lock (ThisGlobalLock) |
| | | 597 | | { |
| | 44 | 598 | | if (_pendingSessions1.Count == 0 && _pendingSessions2.Count == 0) |
| | | 599 | | { |
| | 44 | 600 | | return; |
| | | 601 | | } |
| | 0 | 602 | | foreach (UniqueId sessionId in _pendingSessions2.Keys) |
| | | 603 | | { |
| | 0 | 604 | | SecurityContextSecurityToken token = _pendingSessions2[sessionId]; |
| | | 605 | | try |
| | | 606 | | { |
| | | 607 | | //TryCloseBinder(channelBinder, this.CloseTimeout); // Replacing this line with below (being pro |
| | 0 | 608 | | RemoveServiceDispatcher(sessionId); |
| | 0 | 609 | | SessionTokenCache.RemoveAllContexts(sessionId); |
| | 0 | 610 | | } |
| | | 611 | | catch (CommunicationException e) |
| | | 612 | | { |
| | 0 | 613 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 614 | | } |
| | | 615 | | catch (TimeoutException e) |
| | | 616 | | { |
| | | 617 | | //if (TD.CloseTimeoutIsEnabled()) |
| | | 618 | | //{ |
| | | 619 | | // TD.CloseTimeout(e.Message); |
| | | 620 | | //} |
| | 0 | 621 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 622 | | } |
| | | 623 | | catch (ObjectDisposedException e) |
| | | 624 | | { |
| | 0 | 625 | | DiagnosticUtility.TraceHandledException(e, TraceEventType.Information); |
| | 0 | 626 | | } |
| | | 627 | | // SecurityTraceRecordHelper.TracePendingSessionClosed(sessionId, this.Uri); |
| | | 628 | | } |
| | 0 | 629 | | _pendingSessions2.Clear(); |
| | 0 | 630 | | Dictionary<UniqueId, SecurityContextSecurityToken> temp = _pendingSessions2; |
| | 0 | 631 | | _pendingSessions2 = _pendingSessions1; |
| | 0 | 632 | | _pendingSessions1 = temp; |
| | 0 | 633 | | } |
| | 44 | 634 | | } |
| | | 635 | | |
| | | 636 | | internal bool RemovePendingSession(UniqueId sessionId) |
| | | 637 | | { |
| | | 638 | | bool result; |
| | 10 | 639 | | lock (ThisGlobalLock) |
| | | 640 | | { |
| | 10 | 641 | | if (_pendingSessions1.ContainsKey(sessionId)) |
| | | 642 | | { |
| | 10 | 643 | | _pendingSessions1.Remove(sessionId); |
| | 10 | 644 | | result = true; |
| | | 645 | | } |
| | 0 | 646 | | else if (_pendingSessions2.ContainsKey(sessionId)) |
| | | 647 | | { |
| | 0 | 648 | | _pendingSessions2.Remove(sessionId); |
| | 0 | 649 | | result = true; |
| | | 650 | | } |
| | | 651 | | else |
| | | 652 | | { |
| | 0 | 653 | | result = false; |
| | | 654 | | } |
| | 0 | 655 | | } |
| | | 656 | | /* if (result) |
| | | 657 | | { |
| | | 658 | | SecurityTraceRecordHelper.TracePendingSessionActivated(sessionId, this.Uri); |
| | | 659 | | if (TD.SecuritySessionRatioIsEnabled()) |
| | | 660 | | { |
| | | 661 | | TD.SecuritySessionRatio(GetPendingSessionCount(), this.MaximumPendingSessions); |
| | | 662 | | } |
| | | 663 | | }*/ |
| | 10 | 664 | | return result; |
| | | 665 | | } |
| | | 666 | | |
| | | 667 | | private IServerSecuritySessionChannel FindSessionChannel(UniqueId sessionId) |
| | | 668 | | { |
| | | 669 | | IServerSecuritySessionChannel result; |
| | 0 | 670 | | lock (ThisGlobalLock) |
| | | 671 | | { |
| | 0 | 672 | | _activeSessions.TryGetValue(sessionId, out result); |
| | 0 | 673 | | } |
| | 0 | 674 | | return result; |
| | | 675 | | } |
| | | 676 | | |
| | | 677 | | private void AddSessionChannel(UniqueId sessionId, IServerSecuritySessionChannel channel, MessageFilter filter) |
| | | 678 | | { |
| | 10 | 679 | | lock (ThisGlobalLock) |
| | | 680 | | { |
| | 10 | 681 | | _activeSessions.Add(sessionId, channel); |
| | 10 | 682 | | } |
| | 10 | 683 | | } |
| | | 684 | | |
| | | 685 | | internal void RemoveSessionChannel(string sessionId) |
| | | 686 | | { |
| | 10 | 687 | | RemoveSessionChannel(new UniqueId(sessionId)); |
| | 10 | 688 | | } |
| | | 689 | | |
| | | 690 | | private void RemoveSessionChannel(UniqueId sessionId) |
| | | 691 | | { |
| | 10 | 692 | | lock (ThisGlobalLock) |
| | | 693 | | { |
| | 10 | 694 | | RemoveServiceDispatcher(sessionId); |
| | 10 | 695 | | _activeSessions.Remove(sessionId); |
| | 10 | 696 | | _sessionFilters.Remove(sessionId); |
| | 10 | 697 | | } |
| | | 698 | | //SecurityTraceRecordHelper.TraceActiveSessionRemoved(sessionId, this.Uri); |
| | 10 | 699 | | } |
| | | 700 | | |
| | | 701 | | private void RemoveServiceDispatcher(UniqueId sessionId) |
| | | 702 | | { |
| | 10 | 703 | | if (AcceptorChannelType == typeof(IReplyChannel)) |
| | | 704 | | { |
| | 8 | 705 | | ChannelBuilder.RemoveServiceDispatcher<IReplyChannel>(_sessionFilters[sessionId]); |
| | | 706 | | } |
| | 2 | 707 | | else if (AcceptorChannelType == typeof(IDuplexSessionChannel)) |
| | | 708 | | { |
| | 2 | 709 | | ChannelBuilder.RemoveServiceDispatcher<IDuplexSessionChannel>(_sessionFilters[sessionId]); |
| | | 710 | | } |
| | 2 | 711 | | } |
| | | 712 | | |
| | | 713 | | public Task CloseAsync(TimeSpan timeout) |
| | | 714 | | { |
| | 0 | 715 | | return WrapperCommunicationObj.CloseAsync(); |
| | | 716 | | } |
| | | 717 | | public Task OnCloseAsync(TimeSpan timeout) |
| | | 718 | | { |
| | 0 | 719 | | OnCloseCore(timeout); |
| | 0 | 720 | | return Task.CompletedTask; |
| | | 721 | | } |
| | | 722 | | |
| | | 723 | | public Task OpenAsync(TimeSpan timeout) |
| | | 724 | | { |
| | 22 | 725 | | return WrapperCommunicationObj.OpenAsync(); |
| | | 726 | | } |
| | | 727 | | public Task OnOpenAsync(TimeSpan timeout) |
| | | 728 | | { |
| | 22 | 729 | | if (_sessionProtocolFactory == null) |
| | | 730 | | { |
| | 0 | 731 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SecuritySessi |
| | | 732 | | } |
| | 22 | 733 | | if (_standardsManager == null) |
| | | 734 | | { |
| | 0 | 735 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sec |
| | | 736 | | } |
| | 22 | 737 | | if (_issuedTokenParameters == null) |
| | | 738 | | { |
| | 0 | 739 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Iss |
| | | 740 | | } |
| | 22 | 741 | | if (_maximumKeyRenewalInterval < _keyRolloverInterval) |
| | | 742 | | { |
| | 0 | 743 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.KeyRolloverGr |
| | | 744 | | } |
| | 22 | 745 | | if (_securityServiceDispatcher == null) |
| | | 746 | | { |
| | 0 | 747 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sec |
| | | 748 | | } |
| | 22 | 749 | | if (_settingsLifetimeManager == null) |
| | | 750 | | { |
| | 0 | 751 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sec |
| | | 752 | | } |
| | 22 | 753 | | MessageVersion = _channelBuilder.Binding.MessageVersion; |
| | 22 | 754 | | _listenUri = _securityServiceDispatcher.BaseAddress; |
| | 22 | 755 | | TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); |
| | 22 | 756 | | _pendingSessions1 = new Dictionary<UniqueId, SecurityContextSecurityToken>(); |
| | 22 | 757 | | _pendingSessions2 = new Dictionary<UniqueId, SecurityContextSecurityToken>(); |
| | 22 | 758 | | _sessionFilters = new Dictionary<UniqueId, MessageFilter>(); |
| | 22 | 759 | | if (_inactivityTimeout < TimeSpan.MaxValue) |
| | | 760 | | { |
| | 22 | 761 | | _inactivityTimer = new IOThreadTimer(new Action<object>(OnTimer), this, false); |
| | 22 | 762 | | _inactivityTimer.Set(_inactivityTimeout); |
| | | 763 | | } |
| | 22 | 764 | | ConfigureSessionSecurityProtocolFactory(); |
| | 22 | 765 | | _sessionProtocolFactory.OpenAsync(timeoutHelper.RemainingTime()); |
| | 22 | 766 | | SetupSessionTokenAuthenticatorAsync(); |
| | 22 | 767 | | ((IIssuanceSecurityTokenAuthenticator)SessionTokenAuthenticator).IssuedSecurityTokenHandler = OnTokenIssued; |
| | 22 | 768 | | ((IIssuanceSecurityTokenAuthenticator)SessionTokenAuthenticator).RenewedSecurityTokenHandler = OnTokenRenewe |
| | 22 | 769 | | if (SessionTokenAuthenticator is SecuritySessionSecurityTokenAuthenticator securitySessionTokenAuthenticator |
| | | 770 | | { |
| | 22 | 771 | | securitySessionTokenAuthenticator.SecurityServiceDispatcher = SecurityServiceDispatcher; |
| | 0 | 772 | | }else if(SessionTokenAuthenticator is WrappedSessionSecurityTokenAuthenticator wrappedSessionSecurityTokenAu |
| | | 773 | | { |
| | 0 | 774 | | wrappedSessionSecurityTokenAuthenticator.SetSecureServiceDispatcher(SecurityServiceDispatcher); |
| | | 775 | | } |
| | 22 | 776 | | _acceptNewWork = true; |
| | 22 | 777 | | SecurityUtils.OpenTokenAuthenticatorIfRequiredAsync(SessionTokenAuthenticator, timeoutHelper.GetCancellation |
| | 22 | 778 | | return Task.CompletedTask; |
| | | 779 | | } |
| | | 780 | | |
| | | 781 | | public void OnClosed() |
| | | 782 | | { |
| | 0 | 783 | | throw new NotImplementedException(); |
| | | 784 | | } |
| | | 785 | | |
| | | 786 | | public void OnClosing() |
| | | 787 | | { |
| | 0 | 788 | | throw new NotImplementedException(); |
| | | 789 | | } |
| | | 790 | | |
| | | 791 | | //Renaming SessionInitiationMessageHandler to SessionInitiationMessageServiceDispatcher |
| | | 792 | | // |
| | | 793 | | internal class SessionInitiationMessageServiceDispatcher : IServiceDispatcher |
| | | 794 | | { |
| | | 795 | | private readonly SecuritySessionServerSettings _settings; |
| | | 796 | | private readonly SecurityContextSecurityToken _sessionToken; |
| | | 797 | | private volatile IServiceChannelDispatcher _sessionChannelDispatcher; |
| | | 798 | | private readonly MessageFilter _messageFilter; |
| | | 799 | | private readonly EndpointAddress _remoteAddress; |
| | | 800 | | |
| | 10 | 801 | | public SessionInitiationMessageServiceDispatcher(/*IServerReliableChannelBinder channelBinder,*/ SecuritySes |
| | | 802 | | { |
| | 10 | 803 | | _settings = settings; |
| | 10 | 804 | | _sessionToken = sessionToken; |
| | 10 | 805 | | _messageFilter = filter; |
| | 10 | 806 | | _remoteAddress = address; |
| | 10 | 807 | | } |
| | | 808 | | |
| | 0 | 809 | | public Uri BaseAddress => throw new NotImplementedException(); |
| | | 810 | | |
| | 0 | 811 | | public Binding Binding => throw new NotImplementedException(); |
| | | 812 | | |
| | 0 | 813 | | public ServiceHostBase Host => throw new NotImplementedException(); |
| | | 814 | | |
| | 20 | 815 | | public AsyncLock AsyncLock { get; } = new AsyncLock(); |
| | | 816 | | |
| | 0 | 817 | | public IList<Type> SupportedChannelTypes => throw new NotImplementedException(); |
| | | 818 | | |
| | | 819 | | /// <summary> |
| | | 820 | | /// ProcessMessage equivalent in WCF |
| | | 821 | | /// </summary> |
| | | 822 | | /// <returns></returns> |
| | | 823 | | public async Task<IServiceChannelDispatcher> CreateServiceChannelDispatcherAsync(IChannel channel) |
| | | 824 | | { |
| | 20 | 825 | | if (_sessionChannelDispatcher == null) |
| | | 826 | | { |
| | 10 | 827 | | await using (await AsyncLock.TakeLockAsync()) |
| | | 828 | | { |
| | 10 | 829 | | if (_sessionChannelDispatcher == null) |
| | | 830 | | { |
| | 10 | 831 | | if (!_settings.RemovePendingSession(_sessionToken.ContextId)) |
| | | 832 | | { |
| | 0 | 833 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning( |
| | 0 | 834 | | new CommunicationException(SR.Format(SR.SecuritySessionNotPending, |
| | 0 | 835 | | _sessionToken.ContextId))); |
| | | 836 | | } |
| | | 837 | | |
| | 10 | 838 | | ServerSecuritySessionChannel _replySessionChannelDispatcher = null; |
| | 10 | 839 | | if (_settings.AcceptorChannelType == typeof(IDuplexSessionChannel)) |
| | | 840 | | { |
| | 2 | 841 | | _replySessionChannelDispatcher = new ServerSecurityDuplexSessionChannel. |
| | 2 | 842 | | ServerSecurityDuplexSessionChannelDispatcher(_settings, _sessionToken, |
| | 2 | 843 | | null, _settings.SettingsLifetimeManager, channel, _remoteAddress); |
| | | 844 | | |
| | | 845 | | } |
| | 8 | 846 | | else if (_settings.AcceptorChannelType == typeof(IReplyChannel)) |
| | | 847 | | { |
| | 8 | 848 | | _replySessionChannelDispatcher = new ServerSecuritySimplexSessionChannel. |
| | 8 | 849 | | SecurityReplySessionServiceChannelDispatcher(_settings, _sessionToken, |
| | 8 | 850 | | null, _settings.SettingsLifetimeManager, channel, _remoteAddress); |
| | | 851 | | } |
| | | 852 | | |
| | 10 | 853 | | await _replySessionChannelDispatcher.OpenAsync(ServiceDefaults.OpenTimeout); |
| | 10 | 854 | | _sessionChannelDispatcher = (IServiceChannelDispatcher)_replySessionChannelDispatcher; |
| | 10 | 855 | | _settings.AddSessionChannel(_sessionToken.ContextId, _replySessionChannelDispatcher, |
| | 10 | 856 | | _messageFilter); |
| | 10 | 857 | | } |
| | | 858 | | } |
| | | 859 | | } |
| | 20 | 860 | | return _sessionChannelDispatcher; |
| | 20 | 861 | | } |
| | | 862 | | } |
| | | 863 | | |
| | | 864 | | private interface IServerSecuritySessionChannel |
| | | 865 | | { |
| | | 866 | | void RenewSessionToken(SecurityContextSecurityToken newToken, SecurityContextSecurityToken supportingToken); |
| | | 867 | | } |
| | | 868 | | |
| | | 869 | | private abstract class ServerSecuritySessionChannel : /*ChannelBase,*/ IServerSecuritySessionChannel |
| | | 870 | | { |
| | | 871 | | private FaultCode _renewFaultCode; |
| | | 872 | | private FaultReason _renewFaultReason; |
| | | 873 | | private FaultCode _sessionAbortedFaultCode; |
| | | 874 | | private FaultReason _sessionAbortedFaultReason; |
| | | 875 | | |
| | | 876 | | // Double-checked locking pattern requires volatile for read/write synchronization |
| | | 877 | | private bool _areFaultCodesInitialized; |
| | | 878 | | //private readonly IServerReliableChannelBinder _channelBinder; |
| | | 879 | | private readonly SecurityProtocol _securityProtocol; |
| | | 880 | | |
| | | 881 | | // This is used to sign outgoing messages |
| | | 882 | | private SecurityContextSecurityToken _currentSessionToken; |
| | | 883 | | private readonly UniqueId _sessionId; |
| | | 884 | | |
| | | 885 | | // These are renewed tokens that have not been used as yet |
| | | 886 | | private readonly List<SecurityContextSecurityToken> _futureSessionTokens; |
| | | 887 | | private RequestContext _initialRequestContext; |
| | | 888 | | private bool _isInputClosed; |
| | | 889 | | private readonly MessageVersion _messageVersion; |
| | | 890 | | private readonly SecurityListenerSettingsLifetimeManager _settingsLifetimeManager; |
| | | 891 | | private bool _hasSecurityStateReference; |
| | | 892 | | |
| | 10 | 893 | | protected ServerSecuritySessionChannel(SecuritySessionServerSettings settings, |
| | 10 | 894 | | SecurityContextSecurityToken sessionToken, |
| | 10 | 895 | | object listenerSecurityProtocolState, |
| | 10 | 896 | | SecurityListenerSettingsLifetimeManager settingsLifetimeManager, EndpointAddress address) |
| | | 897 | | { |
| | 10 | 898 | | Settings = settings; |
| | 10 | 899 | | _messageVersion = settings.MessageVersion; |
| | | 900 | | // See issue #285 |
| | | 901 | | // channelBinder.Faulted += this.OnInnerFaulted; |
| | 10 | 902 | | _securityProtocol = Settings.SessionProtocolFactory.CreateSecurityProtocol(null, null, true, TimeSpan.Ze |
| | 10 | 903 | | if (!(_securityProtocol is IAcceptorSecuritySessionProtocol)) |
| | | 904 | | { |
| | | 905 | | Fx.Assert("Security protocol must be IAcceptorSecuritySessionProtocol."); |
| | 0 | 906 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 907 | | } |
| | 10 | 908 | | _currentSessionToken = sessionToken; |
| | 10 | 909 | | _sessionId = sessionToken.ContextId; |
| | 10 | 910 | | _futureSessionTokens = new List<SecurityContextSecurityToken>(1); |
| | 10 | 911 | | ((IAcceptorSecuritySessionProtocol)_securityProtocol).SetOutgoingSessionToken(sessionToken); |
| | 10 | 912 | | ((IAcceptorSecuritySessionProtocol)_securityProtocol).SetSessionTokenAuthenticator(_sessionId, Settings. |
| | 10 | 913 | | _settingsLifetimeManager = settingsLifetimeManager; |
| | 10 | 914 | | LocalAddress = address; |
| | 10 | 915 | | LocalLock = new object(); |
| | 10 | 916 | | } |
| | | 917 | | |
| | 254 | 918 | | protected SecuritySessionServerSettings Settings { get; } |
| | | 919 | | |
| | 2 | 920 | | protected virtual bool CanDoSecurityCorrelation => false; |
| | | 921 | | |
| | 0 | 922 | | internal TimeSpan InternalSendTimeout => ServiceDefaults.SendTimeout; |
| | | 923 | | |
| | 2 | 924 | | public EndpointAddress LocalAddress { get; } |
| | | 925 | | |
| | 64 | 926 | | public object LocalLock { get; } |
| | | 927 | | |
| | 66 | 928 | | public CommunicationState State => Settings.WrapperCommunicationObj.State; |
| | | 929 | | |
| | 78 | 930 | | internal SecurityProtocol SecurityProtocol => _securityProtocol; |
| | | 931 | | |
| | | 932 | | public virtual Task OpenAsync(TimeSpan timeout) |
| | | 933 | | { |
| | 10 | 934 | | SecurityProtocol.OpenAsync(timeout); |
| | 10 | 935 | | if (CanDoSecurityCorrelation) |
| | | 936 | | { |
| | 8 | 937 | | ((IAcceptorSecuritySessionProtocol)SecurityProtocol).ReturnCorrelationState = true; |
| | | 938 | | } // if an abort happened concurrently with the open, then return |
| | 10 | 939 | | if (State == CommunicationState.Closed || State == CommunicationState.Closing) |
| | | 940 | | { |
| | 0 | 941 | | return Task.CompletedTask; |
| | | 942 | | } |
| | 10 | 943 | | _settingsLifetimeManager.AddReference(); |
| | 10 | 944 | | _hasSecurityStateReference = true; |
| | 10 | 945 | | return Task.CompletedTask; |
| | | 946 | | } |
| | | 947 | | |
| | | 948 | | protected virtual void AbortCore() |
| | | 949 | | { |
| | 0 | 950 | | if (SecurityProtocol != null) |
| | | 951 | | { |
| | 0 | 952 | | TimeoutHelper timeout = new TimeoutHelper(ServiceDefaults.CloseTimeout); |
| | 0 | 953 | | SecurityProtocol.CloseAsync(true, timeout.RemainingTime()); |
| | | 954 | | } |
| | 0 | 955 | | Settings.SessionTokenCache.RemoveAllContexts(_currentSessionToken.ContextId); |
| | 0 | 956 | | bool abortLifetimeManager = false; |
| | 0 | 957 | | lock (LocalLock) |
| | | 958 | | { |
| | 0 | 959 | | if (_hasSecurityStateReference) |
| | | 960 | | { |
| | 0 | 961 | | abortLifetimeManager = true; |
| | 0 | 962 | | _hasSecurityStateReference = false; |
| | | 963 | | } |
| | 0 | 964 | | } |
| | 0 | 965 | | if (abortLifetimeManager) |
| | | 966 | | { |
| | 0 | 967 | | _settingsLifetimeManager.Abort(); |
| | | 968 | | } |
| | 0 | 969 | | } |
| | | 970 | | |
| | | 971 | | protected virtual async Task CloseCoreAsync(CancellationToken token) |
| | | 972 | | { |
| | | 973 | | try |
| | | 974 | | { |
| | 10 | 975 | | TimeoutHelper helper = new TimeoutHelper(ServiceDefaults.CloseTimeout); |
| | 10 | 976 | | if (SecurityProtocol != null) |
| | | 977 | | { |
| | 10 | 978 | | await SecurityProtocol.CloseAsync(false, helper.RemainingTime()); |
| | | 979 | | } |
| | 10 | 980 | | bool closeLifetimeManager = false; |
| | 10 | 981 | | lock (LocalLock) |
| | | 982 | | { |
| | 10 | 983 | | if (_hasSecurityStateReference) |
| | | 984 | | { |
| | 10 | 985 | | closeLifetimeManager = true; |
| | 10 | 986 | | _hasSecurityStateReference = false; |
| | | 987 | | } |
| | 10 | 988 | | } |
| | 10 | 989 | | if (closeLifetimeManager) |
| | | 990 | | { |
| | 10 | 991 | | await _settingsLifetimeManager.CloseAsync(helper.RemainingTime()); |
| | | 992 | | } |
| | 10 | 993 | | } |
| | 0 | 994 | | catch (CommunicationObjectAbortedException) |
| | | 995 | | { |
| | 0 | 996 | | if (State != CommunicationState.Closed) |
| | | 997 | | { |
| | 0 | 998 | | throw; |
| | | 999 | | } |
| | | 1000 | | // a parallel thread aborted the channel. Ignore the exception |
| | 0 | 1001 | | } |
| | 10 | 1002 | | Settings.SessionTokenCache.RemoveAllContexts(_currentSessionToken.ContextId); |
| | 10 | 1003 | | } |
| | | 1004 | | |
| | | 1005 | | protected abstract void OnCloseMessageReceived(RequestContext requestContext, Message message, SecurityProto |
| | | 1006 | | |
| | | 1007 | | protected abstract void OnCloseResponseMessageReceived(RequestContext requestContext, Message message, Secur |
| | | 1008 | | |
| | | 1009 | | public void RenewSessionToken(SecurityContextSecurityToken newToken, SecurityContextSecurityToken supporting |
| | | 1010 | | { |
| | 0 | 1011 | | ThrowIfClosedOrNotOpen(); |
| | | 1012 | | // enforce that the token being renewed is the current session token |
| | 0 | 1013 | | lock (LocalLock) |
| | | 1014 | | { |
| | 0 | 1015 | | if (supportingToken.ContextId != _currentSessionToken.ContextId || supportingToken.KeyGeneration != |
| | | 1016 | | { |
| | 0 | 1017 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Form |
| | | 1018 | | } |
| | 0 | 1019 | | if (_futureSessionTokens.Count == Settings.MaximumPendingKeysPerSession) |
| | | 1020 | | { |
| | 0 | 1021 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.TooM |
| | | 1022 | | } |
| | 0 | 1023 | | _futureSessionTokens.Add(newToken); |
| | 0 | 1024 | | } |
| | | 1025 | | // SecurityTraceRecordHelper.TraceNewServerSessionKeyIssued(newToken, supportingToken, GetLocalUri()); |
| | 0 | 1026 | | } |
| | | 1027 | | |
| | | 1028 | | //protected Uri GetLocalUri() |
| | | 1029 | | //{ |
| | | 1030 | | // if (_channelBinder.LocalAddress == null) |
| | | 1031 | | // { |
| | | 1032 | | // return null; |
| | | 1033 | | // } |
| | | 1034 | | // else |
| | | 1035 | | // { |
| | | 1036 | | // return _channelBinder.LocalAddress.Uri; |
| | | 1037 | | // } |
| | | 1038 | | //} |
| | | 1039 | | |
| | | 1040 | | // TODO: Wire up the channel binder faults to call this. |
| | | 1041 | | //private void OnInnerFaulted(IReliableChannelBinder sender, Exception exception) |
| | | 1042 | | //{ |
| | | 1043 | | // OnFaulted(exception); |
| | | 1044 | | //} |
| | | 1045 | | |
| | | 1046 | | private SecurityContextSecurityToken GetSessionToken(SecurityMessageProperty securityProperty) |
| | | 1047 | | { |
| | 20 | 1048 | | SecurityContextSecurityToken sct = (securityProperty.ProtectionToken != null) ? securityProperty.Protect |
| | 20 | 1049 | | if (sct != null && sct.ContextId == _sessionId) |
| | | 1050 | | { |
| | 0 | 1051 | | return sct; |
| | | 1052 | | } |
| | 20 | 1053 | | if (securityProperty.HasIncomingSupportingTokens) |
| | | 1054 | | { |
| | 40 | 1055 | | for (int i = 0; i < securityProperty.IncomingSupportingTokens.Count; ++i) |
| | | 1056 | | { |
| | 20 | 1057 | | if (securityProperty.IncomingSupportingTokens[i].SecurityTokenAttachmentMode == SecurityTokenAtt |
| | | 1058 | | { |
| | 20 | 1059 | | sct = (securityProperty.IncomingSupportingTokens[i].SecurityToken as SecurityContextSecurity |
| | 20 | 1060 | | if (sct != null && sct.ContextId == _sessionId) |
| | | 1061 | | { |
| | 20 | 1062 | | return sct; |
| | | 1063 | | } |
| | | 1064 | | } |
| | | 1065 | | } |
| | | 1066 | | } |
| | 0 | 1067 | | return null; |
| | | 1068 | | } |
| | | 1069 | | |
| | | 1070 | | private bool CheckIncomingToken(RequestContext requestContext, Message message, SecurityProtocolCorrelationS |
| | | 1071 | | { |
| | 20 | 1072 | | SecurityMessageProperty securityProperty = message.Properties.Security; |
| | | 1073 | | // this is guaranteed to be non-null and matches the session ID since the binding checked it |
| | 20 | 1074 | | SecurityContextSecurityToken incomingToken = GetSessionToken(securityProperty); |
| | 20 | 1075 | | if (incomingToken == null) |
| | | 1076 | | { |
| | 0 | 1077 | | throw TraceUtility.ThrowHelperWarning(new MessageSecurityException(SR.NoSessionTokenPresentInMessage |
| | | 1078 | | } |
| | | 1079 | | // the incoming token's key should have been issued within keyRenewalPeriod time in the past |
| | | 1080 | | // if not, send back a renewal fault. However if this is a session close message then its ok to not requ |
| | | 1081 | | // to renew the key in order to send the close. |
| | 20 | 1082 | | if (incomingToken.KeyExpirationTime < DateTime.UtcNow && |
| | 20 | 1083 | | message.Headers.Action != Settings.SecurityStandardsManager.SecureConversationDriver.CloseAction.Val |
| | | 1084 | | { |
| | 0 | 1085 | | if (Settings.CanRenewSession) |
| | | 1086 | | { |
| | 0 | 1087 | | SendRenewFault(requestContext, correlationState, timeout); |
| | 0 | 1088 | | return false; |
| | | 1089 | | } |
| | | 1090 | | else |
| | | 1091 | | { |
| | 0 | 1092 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new Exception(SR.Format(SR.SecurityC |
| | | 1093 | | } |
| | | 1094 | | } |
| | | 1095 | | // this is a valid token. If it corresponds to a newly issued session token, make it the current |
| | | 1096 | | // session token. |
| | 20 | 1097 | | lock (LocalLock) |
| | | 1098 | | { |
| | 20 | 1099 | | if (_futureSessionTokens.Count > 0 && incomingToken.KeyGeneration != _currentSessionToken.KeyGenerat |
| | | 1100 | | { |
| | 0 | 1101 | | bool changedCurrentSessionToken = false; |
| | 0 | 1102 | | for (int i = 0; i < _futureSessionTokens.Count; ++i) |
| | | 1103 | | { |
| | 0 | 1104 | | if (_futureSessionTokens[i].KeyGeneration == incomingToken.KeyGeneration) |
| | | 1105 | | { |
| | | 1106 | | // let the current token expire after KeyRollover time interval |
| | 0 | 1107 | | DateTime keyRolloverTime = TimeoutHelper.Add(DateTime.UtcNow, Settings.KeyRolloverInterv |
| | 0 | 1108 | | Settings.SessionTokenCache.UpdateContextCachingTime(_currentSessionToken, keyRolloverTim |
| | 0 | 1109 | | _currentSessionToken = _futureSessionTokens[i]; |
| | 0 | 1110 | | _futureSessionTokens.RemoveAt(i); |
| | 0 | 1111 | | ((IAcceptorSecuritySessionProtocol)SecurityProtocol).SetOutgoingSessionToken(_currentSes |
| | 0 | 1112 | | changedCurrentSessionToken = true; |
| | 0 | 1113 | | break; |
| | | 1114 | | } |
| | | 1115 | | } |
| | 0 | 1116 | | if (changedCurrentSessionToken) |
| | | 1117 | | { |
| | | 1118 | | // SecurityTraceRecordHelper.TraceServerSessionKeyUpdated(this.currentSessionToken, GetLocal |
| | | 1119 | | // remove all renewed tokens that will never be used. |
| | 0 | 1120 | | for (int i = 0; i < _futureSessionTokens.Count; ++i) |
| | | 1121 | | { |
| | 0 | 1122 | | Settings.SessionTokenCache.RemoveContext(_futureSessionTokens[i].ContextId, _futureSessi |
| | | 1123 | | } |
| | 0 | 1124 | | _futureSessionTokens.Clear(); |
| | | 1125 | | } |
| | | 1126 | | } |
| | 20 | 1127 | | } |
| | | 1128 | | |
| | 20 | 1129 | | return true; |
| | | 1130 | | } |
| | | 1131 | | |
| | | 1132 | | public ValueTask<RequestContext> ReceiveRequestAsync(RequestContext initialRequestContext) |
| | | 1133 | | { |
| | 20 | 1134 | | return ReceiveRequestAsync(ServiceDefaults.ReceiveTimeout, initialRequestContext); |
| | | 1135 | | } |
| | | 1136 | | |
| | | 1137 | | public async ValueTask<RequestContext> ReceiveRequestAsync(TimeSpan timeout, RequestContext initialRequestCo |
| | | 1138 | | { |
| | 20 | 1139 | | _initialRequestContext = initialRequestContext; |
| | 20 | 1140 | | (bool success, RequestContext requestContext) receiveRequestTry = await TryReceiveRequestAsync(timeout); |
| | 20 | 1141 | | RequestContext requestContext = receiveRequestTry.requestContext; |
| | | 1142 | | |
| | 20 | 1143 | | if (receiveRequestTry.success) |
| | | 1144 | | { |
| | 20 | 1145 | | return requestContext; |
| | | 1146 | | } |
| | | 1147 | | else |
| | | 1148 | | { |
| | 0 | 1149 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException()); |
| | | 1150 | | } |
| | 20 | 1151 | | } |
| | | 1152 | | |
| | | 1153 | | public async ValueTask<(bool, RequestContext)> TryReceiveRequestAsync(TimeSpan timeout) |
| | | 1154 | | { |
| | 22 | 1155 | | ThrowIfFaulted(); |
| | 22 | 1156 | | TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); |
| | 10 | 1157 | | while (true) |
| | | 1158 | | { |
| | 32 | 1159 | | if (_isInputClosed || State == CommunicationState.Faulted) |
| | | 1160 | | { |
| | | 1161 | | break; |
| | | 1162 | | } |
| | 20 | 1163 | | if (timeoutHelper.RemainingTime() == TimeSpan.Zero) |
| | | 1164 | | { |
| | 0 | 1165 | | return (false, null); |
| | | 1166 | | } |
| | | 1167 | | |
| | | 1168 | | RequestContext innerRequestContext; |
| | 20 | 1169 | | if (_initialRequestContext != null) |
| | | 1170 | | { |
| | 20 | 1171 | | innerRequestContext = _initialRequestContext; |
| | 20 | 1172 | | _initialRequestContext = null; |
| | | 1173 | | } |
| | | 1174 | | else |
| | | 1175 | | { |
| | 0 | 1176 | | return (false, null); |
| | | 1177 | | } |
| | 20 | 1178 | | if (innerRequestContext == null) |
| | | 1179 | | { |
| | | 1180 | | // the channel could have been aborted or closed |
| | | 1181 | | break; |
| | | 1182 | | } |
| | 20 | 1183 | | if (_isInputClosed && innerRequestContext.RequestMessage != null) |
| | | 1184 | | { |
| | 0 | 1185 | | Message message = innerRequestContext.RequestMessage; |
| | | 1186 | | try |
| | | 1187 | | { |
| | 0 | 1188 | | ProtocolException error = ProtocolException.ReceiveShutdownReturnedNonNull(message); |
| | 0 | 1189 | | throw TraceUtility.ThrowHelperWarning(error, message); |
| | | 1190 | | } |
| | | 1191 | | finally |
| | | 1192 | | { |
| | 0 | 1193 | | message.Close(); |
| | 0 | 1194 | | innerRequestContext.Abort(); |
| | | 1195 | | } |
| | | 1196 | | } |
| | | 1197 | | |
| | 20 | 1198 | | (Message message, SecurityProtocolCorrelationState correlationState, bool _) processedRequestContext |
| | 20 | 1199 | | if (processedRequestContext.message != null) |
| | | 1200 | | { |
| | 10 | 1201 | | RequestContext requestContext = new SecuritySessionRequestContext(innerRequestContext, processed |
| | 10 | 1202 | | return (true, requestContext); |
| | | 1203 | | } |
| | 10 | 1204 | | } |
| | 12 | 1205 | | ThrowIfFaulted(); |
| | | 1206 | | |
| | 12 | 1207 | | return (true, null); |
| | 22 | 1208 | | } |
| | | 1209 | | |
| | | 1210 | | private void ThrowIfFaulted() |
| | | 1211 | | { |
| | 44 | 1212 | | Settings.WrapperCommunicationObj.ThrowIfFaulted(); |
| | 44 | 1213 | | } |
| | | 1214 | | |
| | | 1215 | | //public override T GetProperty<T>() |
| | | 1216 | | //{ |
| | | 1217 | | // if (typeof(T) == typeof(FaultConverter) && (this.channelBinder != null)) |
| | | 1218 | | // { |
| | | 1219 | | // return new SecurityChannelFaultConverter(this.channelBinder.Channel) as T; |
| | | 1220 | | // } |
| | | 1221 | | |
| | | 1222 | | // T result = base.GetProperty<T>(); |
| | | 1223 | | // if ((result == null) && (channelBinder != null) && (channelBinder.Channel != null)) |
| | | 1224 | | // { |
| | | 1225 | | // result = channelBinder.Channel.GetProperty<T>(); |
| | | 1226 | | // } |
| | | 1227 | | |
| | | 1228 | | // return result; |
| | | 1229 | | //} |
| | | 1230 | | |
| | | 1231 | | private void SendFaultIfRequired(Exception e, Message unverifiedMessage, RequestContext requestContext, Time |
| | | 1232 | | { |
| | | 1233 | | try |
| | | 1234 | | { |
| | 0 | 1235 | | MessageFault fault = SecurityUtils.CreateSecurityMessageFault(e, SecurityProtocol.SecurityProtocolFa |
| | 0 | 1236 | | if (fault == null) |
| | | 1237 | | { |
| | 0 | 1238 | | return; |
| | | 1239 | | } |
| | 0 | 1240 | | TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); |
| | | 1241 | | try |
| | | 1242 | | { |
| | 0 | 1243 | | using (Message faultMessage = Message.CreateMessage(unverifiedMessage.Version, fault, unverified |
| | | 1244 | | { |
| | 0 | 1245 | | if (unverifiedMessage.Headers.MessageId != null) |
| | | 1246 | | { |
| | 0 | 1247 | | faultMessage.InitializeReply(unverifiedMessage); |
| | | 1248 | | } |
| | | 1249 | | |
| | 0 | 1250 | | requestContext.ReplyAsync(faultMessage, timeoutHelper.GetCancellationToken()); |
| | 0 | 1251 | | requestContext.CloseAsync(timeoutHelper.GetCancellationToken()); |
| | 0 | 1252 | | } |
| | 0 | 1253 | | } |
| | | 1254 | | catch (CommunicationException ex) |
| | | 1255 | | { |
| | 0 | 1256 | | DiagnosticUtility.TraceHandledException(ex, TraceEventType.Information); |
| | 0 | 1257 | | } |
| | | 1258 | | catch (TimeoutException ex) |
| | | 1259 | | { |
| | | 1260 | | //if (TD.CloseTimeoutIsEnabled()) |
| | | 1261 | | //{ |
| | | 1262 | | // TD.CloseTimeout(e.Message); |
| | | 1263 | | //} |
| | 0 | 1264 | | DiagnosticUtility.TraceHandledException(ex, TraceEventType.Information); |
| | 0 | 1265 | | } |
| | | 1266 | | } |
| | | 1267 | | finally |
| | | 1268 | | { |
| | 0 | 1269 | | unverifiedMessage.Close(); |
| | 0 | 1270 | | requestContext.Abort(); |
| | 0 | 1271 | | } |
| | 0 | 1272 | | } |
| | | 1273 | | |
| | | 1274 | | private bool ShouldWrapException(Exception e) |
| | | 1275 | | { |
| | 0 | 1276 | | return ((e is FormatException) || (e is XmlException)); |
| | | 1277 | | } |
| | | 1278 | | |
| | | 1279 | | private async ValueTask<(Message, SecurityProtocolCorrelationState, bool)> ProcessRequestContextAsync(Reques |
| | | 1280 | | { |
| | 20 | 1281 | | SecurityProtocolCorrelationState correlationState = null; |
| | 20 | 1282 | | bool isSecurityProcessingFailure = false; |
| | 20 | 1283 | | if (requestContext == null) |
| | | 1284 | | { |
| | 0 | 1285 | | return (null, correlationState, isSecurityProcessingFailure); |
| | | 1286 | | } |
| | | 1287 | | |
| | 20 | 1288 | | Message result = null; |
| | 20 | 1289 | | Message message = requestContext.RequestMessage; |
| | 20 | 1290 | | bool cleanupContextState = true; |
| | | 1291 | | try |
| | | 1292 | | { |
| | 20 | 1293 | | TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); |
| | 20 | 1294 | | Message unverifiedMessage = message; |
| | 20 | 1295 | | Exception securityException = null; |
| | | 1296 | | try |
| | | 1297 | | { |
| | 20 | 1298 | | (Message message, SecurityProtocolCorrelationState correlationState) verifiedIncomingMessage = a |
| | 20 | 1299 | | message = verifiedIncomingMessage.message; |
| | 20 | 1300 | | correlationState = verifiedIncomingMessage.correlationState; |
| | | 1301 | | // message.Properties.Security |
| | 20 | 1302 | | } |
| | 0 | 1303 | | catch (MessageSecurityException e) |
| | | 1304 | | { |
| | 0 | 1305 | | isSecurityProcessingFailure = true; |
| | 0 | 1306 | | securityException = e; |
| | 0 | 1307 | | throw; |
| | | 1308 | | } |
| | 20 | 1309 | | if (securityException != null) |
| | | 1310 | | { |
| | | 1311 | | // SendFaultIfRequired closes the unverified message and context |
| | 0 | 1312 | | SendFaultIfRequired(securityException, unverifiedMessage, requestContext, timeoutHelper.Remainin |
| | 0 | 1313 | | cleanupContextState = false; |
| | 0 | 1314 | | return (null, correlationState, isSecurityProcessingFailure); |
| | | 1315 | | } |
| | 20 | 1316 | | else if (CheckIncomingToken(requestContext, message, correlationState, timeoutHelper.RemainingTime() |
| | | 1317 | | { |
| | 20 | 1318 | | if (message.Headers.Action == Settings.SecurityStandardsManager.SecureConversationDriver.CloseAc |
| | | 1319 | | { |
| | | 1320 | | // SecurityTraceRecordHelper.TraceServerSessionCloseReceived(this.currentSessionToken, GetL |
| | 10 | 1321 | | _isInputClosed = true; |
| | | 1322 | | // OnCloseMessageReceived is responsible for closing the message and requestContext if requi |
| | 10 | 1323 | | OnCloseMessageReceived(requestContext, message, correlationState, timeoutHelper.GetCancellat |
| | 10 | 1324 | | correlationState = null; |
| | | 1325 | | } |
| | 10 | 1326 | | else if (message.Headers.Action == Settings.SecurityStandardsManager.SecureConversationDriver.Cl |
| | | 1327 | | { |
| | | 1328 | | // SecurityTraceRecordHelper.TraceServerSessionCloseResponseReceived(this.currentSessionToke |
| | 0 | 1329 | | _isInputClosed = true; |
| | | 1330 | | // OnCloseResponseMessageReceived is responsible for closing the message and requestContext |
| | 0 | 1331 | | OnCloseResponseMessageReceived(requestContext, message, correlationState, timeoutHelper.Rema |
| | 0 | 1332 | | correlationState = null; |
| | | 1333 | | } |
| | | 1334 | | else |
| | | 1335 | | { |
| | 10 | 1336 | | result = message; |
| | | 1337 | | } |
| | 20 | 1338 | | cleanupContextState = false; |
| | | 1339 | | } |
| | 20 | 1340 | | } |
| | 0 | 1341 | | catch (Exception e) |
| | | 1342 | | { |
| | 0 | 1343 | | if ((e is CommunicationException) || (e is TimeoutException) || (Fx.IsFatal(e)) || !ShouldWrapExcept |
| | | 1344 | | { |
| | 0 | 1345 | | throw; |
| | | 1346 | | } |
| | 0 | 1347 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.MessageS |
| | | 1348 | | } |
| | | 1349 | | finally |
| | | 1350 | | { |
| | 20 | 1351 | | if (cleanupContextState) |
| | | 1352 | | { |
| | 0 | 1353 | | if (requestContext.RequestMessage != null) |
| | | 1354 | | { |
| | 0 | 1355 | | requestContext.RequestMessage.Close(); |
| | | 1356 | | } |
| | 0 | 1357 | | requestContext.Abort(); |
| | | 1358 | | } |
| | | 1359 | | } |
| | | 1360 | | |
| | 20 | 1361 | | return (result, correlationState, isSecurityProcessingFailure); |
| | 20 | 1362 | | } |
| | | 1363 | | |
| | | 1364 | | internal void CheckOutgoingToken() |
| | | 1365 | | { |
| | 10 | 1366 | | lock (LocalLock) |
| | | 1367 | | { |
| | 10 | 1368 | | if (_currentSessionToken.KeyExpirationTime < DateTime.UtcNow) |
| | | 1369 | | { |
| | 0 | 1370 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Exception(SR.SecuritySessionKeyIsS |
| | | 1371 | | } |
| | 10 | 1372 | | } |
| | 10 | 1373 | | } |
| | | 1374 | | |
| | | 1375 | | internal void SecureApplicationMessage(ref Message message, SecurityProtocolCorrelationState correlationStat |
| | | 1376 | | { |
| | 10 | 1377 | | ThrowIfFaulted(); |
| | 10 | 1378 | | ThrowIfClosedOrNotOpen(); |
| | 10 | 1379 | | CheckOutgoingToken(); |
| | 10 | 1380 | | message = SecurityProtocol.SecureOutgoingMessage(message, token); |
| | 10 | 1381 | | } |
| | | 1382 | | |
| | | 1383 | | private void ThrowIfClosedOrNotOpen() |
| | | 1384 | | { |
| | | 1385 | | //throw new NotImplementedException(); |
| | 10 | 1386 | | } |
| | | 1387 | | |
| | | 1388 | | internal async ValueTask<(Message, SecurityProtocolCorrelationState)> VerifyIncomingMessageAsync(Message mes |
| | | 1389 | | { |
| | 20 | 1390 | | return await SecurityProtocol.VerifyIncomingMessageAsync(message, timeout, null); |
| | 20 | 1391 | | } |
| | | 1392 | | |
| | | 1393 | | private void PrepareReply(Message request, Message reply) |
| | | 1394 | | { |
| | 10 | 1395 | | if (request.Headers.ReplyTo != null) |
| | | 1396 | | { |
| | 10 | 1397 | | request.Headers.ReplyTo.ApplyTo(reply); |
| | | 1398 | | } |
| | 0 | 1399 | | else if (request.Headers.From != null) |
| | | 1400 | | { |
| | 0 | 1401 | | request.Headers.From.ApplyTo(reply); |
| | | 1402 | | } |
| | 10 | 1403 | | if (request.Headers.MessageId != null) |
| | | 1404 | | { |
| | 10 | 1405 | | reply.Headers.RelatesTo = request.Headers.MessageId; |
| | | 1406 | | } |
| | | 1407 | | //TraceUtility.CopyActivity(request, reply); |
| | | 1408 | | //if (TraceUtility.PropagateUserActivity || TraceUtility.ShouldPropagateActivity) |
| | | 1409 | | //{ |
| | | 1410 | | // TraceUtility.AddActivityHeader(reply); |
| | | 1411 | | //} |
| | 10 | 1412 | | } |
| | | 1413 | | |
| | | 1414 | | protected void InitializeFaultCodesIfRequired() |
| | | 1415 | | { |
| | 0 | 1416 | | if (!_areFaultCodesInitialized) |
| | | 1417 | | { |
| | 0 | 1418 | | lock (LocalLock) |
| | | 1419 | | { |
| | 0 | 1420 | | if (!_areFaultCodesInitialized) |
| | | 1421 | | { |
| | 0 | 1422 | | SecurityStandardsManager standardsManager = SecurityProtocol.SecurityProtocolFactory.Standar |
| | 0 | 1423 | | SecureConversationDriver scDriver = standardsManager.SecureConversationDriver; |
| | 0 | 1424 | | _renewFaultCode = FaultCode.CreateSenderFaultCode(scDriver.RenewNeededFaultCode.Value, scDri |
| | 0 | 1425 | | _renewFaultReason = new FaultReason(SR.SecurityRenewFaultReason, System.Globalization.Cultur |
| | 0 | 1426 | | _sessionAbortedFaultCode = FaultCode.CreateSenderFaultCode(DotNetSecurityStrings.SecuritySes |
| | 0 | 1427 | | _sessionAbortedFaultReason = new FaultReason(SR.SecuritySessionAbortedFaultReason, System.Gl |
| | 0 | 1428 | | _areFaultCodesInitialized = true; |
| | | 1429 | | } |
| | 0 | 1430 | | } |
| | | 1431 | | } |
| | 0 | 1432 | | } |
| | | 1433 | | |
| | | 1434 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Ju |
| | | 1435 | | private void SendRenewFault(RequestContext requestContext, SecurityProtocolCorrelationState correlationState |
| | | 1436 | | { |
| | 0 | 1437 | | Message message = requestContext.RequestMessage; |
| | | 1438 | | try |
| | | 1439 | | { |
| | 0 | 1440 | | InitializeFaultCodesIfRequired(); |
| | 0 | 1441 | | MessageFault renewFault = MessageFault.CreateFault(_renewFaultCode, _renewFaultReason); |
| | | 1442 | | Message response; |
| | 0 | 1443 | | if (message.Headers.MessageId != null) |
| | | 1444 | | { |
| | 0 | 1445 | | response = Message.CreateMessage(message.Version, renewFault, DotNetSecurityStrings.SecuritySess |
| | 0 | 1446 | | response.InitializeReply(message); |
| | | 1447 | | } |
| | | 1448 | | else |
| | | 1449 | | { |
| | 0 | 1450 | | response = Message.CreateMessage(message.Version, renewFault, DotNetSecurityStrings.SecuritySess |
| | | 1451 | | } |
| | | 1452 | | try |
| | | 1453 | | { |
| | 0 | 1454 | | PrepareReply(message, response); |
| | 0 | 1455 | | TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); |
| | 0 | 1456 | | response = SecurityProtocol.SecureOutgoingMessage(response, timeoutHelper.GetCancellationToken() |
| | 0 | 1457 | | response.Properties.AllowOutputBatching = false; |
| | 0 | 1458 | | var messageTask = SendMessageAsync(requestContext, response, timeoutHelper.GetCancellationToken( |
| | 0 | 1459 | | messageTask.GetAwaiter().GetResult(); |
| | 0 | 1460 | | } |
| | | 1461 | | finally |
| | | 1462 | | { |
| | 0 | 1463 | | response.Close(); |
| | 0 | 1464 | | } |
| | | 1465 | | // SecurityTraceRecordHelper.TraceSessionRenewalFaultSent(this.currentSessionToken, GetLocalUri(), |
| | 0 | 1466 | | } |
| | 0 | 1467 | | catch (CommunicationException/* e*/) |
| | | 1468 | | { |
| | | 1469 | | //SecurityTraceRecordHelper.TraceRenewFaultSendFailure(this.currentSessionToken, GetLocalUri(), e); |
| | 0 | 1470 | | } |
| | 0 | 1471 | | catch (TimeoutException/* e*/) |
| | | 1472 | | { |
| | | 1473 | | // SecurityTraceRecordHelper.TraceRenewFaultSendFailure(this.currentSessionToken, GetLocalUri(), e); |
| | 0 | 1474 | | } |
| | 0 | 1475 | | } |
| | | 1476 | | |
| | | 1477 | | private Message ProcessCloseRequest(Message request) |
| | | 1478 | | { |
| | | 1479 | | RequestSecurityToken rst; |
| | 10 | 1480 | | XmlDictionaryReader bodyReader = request.GetReaderAtBodyContents(); |
| | 10 | 1481 | | using (bodyReader) |
| | | 1482 | | { |
| | 10 | 1483 | | rst = Settings.SecurityStandardsManager.TrustDriver.CreateRequestSecurityToken(bodyReader); |
| | 10 | 1484 | | request.ReadFromBodyContentsToEnd(bodyReader); |
| | 10 | 1485 | | } |
| | 10 | 1486 | | if (rst.RequestType != null && rst.RequestType != Settings.SecurityStandardsManager.TrustDriver.RequestT |
| | | 1487 | | { |
| | 0 | 1488 | | throw TraceUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(SR.InvalidRstRequestTyp |
| | | 1489 | | } |
| | 10 | 1490 | | if (rst.CloseTarget == null) |
| | | 1491 | | { |
| | 0 | 1492 | | throw TraceUtility.ThrowHelperWarning(new MessageSecurityException(SR.NoCloseTargetSpecified), reque |
| | | 1493 | | } |
| | 10 | 1494 | | if (!(rst.CloseTarget is SecurityContextKeyIdentifierClause sctSkiClause) || !SecuritySessionSecurityTok |
| | | 1495 | | { |
| | 0 | 1496 | | throw TraceUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(SR.BadCloseTarget, rst. |
| | | 1497 | | } |
| | 10 | 1498 | | RequestSecurityTokenResponse rstr = new RequestSecurityTokenResponse(Settings.SecurityStandardsManager) |
| | 10 | 1499 | | { |
| | 10 | 1500 | | Context = rst.Context, |
| | 10 | 1501 | | IsRequestedTokenClosed = true |
| | 10 | 1502 | | }; |
| | 10 | 1503 | | rstr.MakeReadOnly(); |
| | 10 | 1504 | | BodyWriter bodyWriter = rstr; |
| | 10 | 1505 | | if (Settings.SecurityStandardsManager.MessageSecurityVersion.TrustVersion == TrustVersion.WSTrust13) |
| | | 1506 | | { |
| | 4 | 1507 | | List<RequestSecurityTokenResponse> rstrList = new List<RequestSecurityTokenResponse>(1) |
| | 4 | 1508 | | { |
| | 4 | 1509 | | rstr |
| | 4 | 1510 | | }; |
| | 4 | 1511 | | RequestSecurityTokenResponseCollection rstrc = new RequestSecurityTokenResponseCollection(rstrList, |
| | 4 | 1512 | | bodyWriter = rstrc; |
| | | 1513 | | } |
| | 10 | 1514 | | Message response = Message.CreateMessage(request.Version, ActionHeader.Create(Settings.SecurityStandards |
| | 10 | 1515 | | PrepareReply(request, response); |
| | 10 | 1516 | | return response; |
| | | 1517 | | } |
| | | 1518 | | |
| | | 1519 | | internal Message CreateCloseResponse(Message message, SecurityProtocolCorrelationState correlationState, Can |
| | | 1520 | | { |
| | 10 | 1521 | | using (message) |
| | | 1522 | | { |
| | 10 | 1523 | | Message response = ProcessCloseRequest(message); |
| | 10 | 1524 | | response = SecurityProtocol.SecureOutgoingMessage(response, token); |
| | 10 | 1525 | | response.Properties.AllowOutputBatching = false; |
| | 10 | 1526 | | return response; |
| | | 1527 | | } |
| | 10 | 1528 | | } |
| | | 1529 | | |
| | | 1530 | | internal void TraceSessionClosedResponseSuccess() |
| | | 1531 | | { |
| | | 1532 | | // SecurityTraceRecordHelper.TraceSessionClosedResponseSent(this.currentSessionToken, GetLocalUri()); |
| | 10 | 1533 | | } |
| | | 1534 | | |
| | | 1535 | | internal void TraceSessionClosedResponseFailure(Exception e) |
| | | 1536 | | { |
| | | 1537 | | // SecurityTraceRecordHelper.TraceSessionClosedResponseSendFailure(this.currentSessionToken, GetLocalUri |
| | 0 | 1538 | | } |
| | | 1539 | | |
| | | 1540 | | internal void TraceSessionClosedSuccess() |
| | | 1541 | | { |
| | | 1542 | | // SecurityTraceRecordHelper.TraceSessionClosedSent(this.currentSessionToken, GetLocalUri()); |
| | 0 | 1543 | | } |
| | | 1544 | | |
| | | 1545 | | internal void TraceSessionClosedFailure(Exception e) |
| | | 1546 | | { |
| | | 1547 | | // SecurityTraceRecordHelper.TraceSessionCloseSendFailure(this.currentSessionToken, GetLocalUri(), e); |
| | 0 | 1548 | | } |
| | | 1549 | | |
| | | 1550 | | // SendCloseResponse closes the message and underlying context if the operation completes successfully |
| | | 1551 | | protected async Task SendCloseResponseAsync(RequestContext requestContext, Message closeResponse, Cancellati |
| | | 1552 | | { |
| | | 1553 | | try |
| | | 1554 | | { |
| | 10 | 1555 | | using (closeResponse) |
| | | 1556 | | { |
| | 10 | 1557 | | await SendMessageAsync(requestContext, closeResponse, token); |
| | 10 | 1558 | | } |
| | | 1559 | | |
| | 10 | 1560 | | TraceSessionClosedResponseSuccess(); |
| | 10 | 1561 | | } |
| | 0 | 1562 | | catch (CommunicationException e) |
| | | 1563 | | { |
| | 0 | 1564 | | TraceSessionClosedResponseFailure(e); |
| | 0 | 1565 | | } |
| | 0 | 1566 | | catch (TimeoutException e) |
| | | 1567 | | { |
| | 0 | 1568 | | TraceSessionClosedResponseFailure(e); |
| | 0 | 1569 | | } |
| | 10 | 1570 | | } |
| | | 1571 | | |
| | | 1572 | | internal Message CreateCloseMessage(CancellationToken token) |
| | | 1573 | | { |
| | 0 | 1574 | | RequestSecurityToken rst = new RequestSecurityToken(Settings.SecurityStandardsManager) |
| | 0 | 1575 | | { |
| | 0 | 1576 | | RequestType = Settings.SecurityStandardsManager.TrustDriver.RequestTypeClose, |
| | 0 | 1577 | | CloseTarget = Settings.IssuedSecurityTokenParameters.CreateKeyIdentifierClause(_currentSessionToken, |
| | 0 | 1578 | | }; |
| | 0 | 1579 | | rst.MakeReadOnly(); |
| | 0 | 1580 | | Message closeMessage = Message.CreateMessage(_messageVersion, ActionHeader.Create(Settings.SecurityStand |
| | 0 | 1581 | | RequestReplyCorrelator.PrepareRequest(closeMessage); |
| | 0 | 1582 | | if (LocalAddress != null) |
| | | 1583 | | { |
| | 0 | 1584 | | closeMessage.Headers.ReplyTo = LocalAddress; |
| | | 1585 | | } |
| | | 1586 | | else |
| | | 1587 | | { |
| | 0 | 1588 | | if (closeMessage.Version.Addressing == AddressingVersion.WSAddressing10) |
| | | 1589 | | { |
| | 0 | 1590 | | closeMessage.Headers.ReplyTo = null; |
| | | 1591 | | } |
| | 0 | 1592 | | else if (closeMessage.Version.Addressing == AddressingVersion.WSAddressingAugust2004) |
| | | 1593 | | { |
| | 0 | 1594 | | closeMessage.Headers.ReplyTo = EndpointAddress.AnonymousAddress; |
| | | 1595 | | } |
| | | 1596 | | else |
| | | 1597 | | { |
| | 0 | 1598 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 1599 | | new ProtocolException(SR.Format(SR.AddressingVersionNotSupported, closeMessage.Version.Addre |
| | | 1600 | | } |
| | | 1601 | | } |
| | 0 | 1602 | | SecurityProtocol.SecureOutgoingMessage(closeMessage, token); |
| | 0 | 1603 | | closeMessage.Properties.AllowOutputBatching = false; |
| | 0 | 1604 | | return closeMessage; |
| | | 1605 | | } |
| | | 1606 | | |
| | | 1607 | | protected async Task SendCloseAsync(CancellationToken token) |
| | | 1608 | | { |
| | | 1609 | | try |
| | | 1610 | | { |
| | 0 | 1611 | | using (Message closeMessage = CreateCloseMessage(token)) |
| | | 1612 | | { |
| | 0 | 1613 | | await SendMessageAsync(null, closeMessage, token); |
| | 0 | 1614 | | } |
| | 0 | 1615 | | TraceSessionClosedSuccess(); |
| | 0 | 1616 | | } |
| | 0 | 1617 | | catch (CommunicationException e) |
| | | 1618 | | { |
| | 0 | 1619 | | TraceSessionClosedFailure(e); |
| | 0 | 1620 | | } |
| | 0 | 1621 | | catch (TimeoutException e) |
| | | 1622 | | { |
| | 0 | 1623 | | TraceSessionClosedFailure(e); |
| | 0 | 1624 | | } |
| | 0 | 1625 | | } |
| | | 1626 | | |
| | | 1627 | | protected async Task SendMessageAsync(RequestContext requestContext, Message message, CancellationToken toke |
| | | 1628 | | { |
| | 10 | 1629 | | if (requestContext != null) |
| | | 1630 | | { |
| | 10 | 1631 | | await requestContext.ReplyAsync(message, token); |
| | 10 | 1632 | | await requestContext.CloseAsync(token); |
| | | 1633 | | } |
| | 10 | 1634 | | } |
| | | 1635 | | |
| | | 1636 | | internal virtual void OnFaulted(Exception ex) |
| | | 1637 | | { |
| | 0 | 1638 | | Settings.WrapperCommunicationObj.Fault(ex); |
| | 0 | 1639 | | } |
| | | 1640 | | |
| | | 1641 | | protected class SoapSecurityInputSession : ISecureConversationSession, IInputSession |
| | | 1642 | | { |
| | | 1643 | | private readonly ServerSecuritySessionChannel _channel; |
| | | 1644 | | private readonly UniqueId _securityContextTokenId; |
| | | 1645 | | private readonly SecurityKeyIdentifierClause _sessionTokenIdentifier; |
| | | 1646 | | private readonly SecurityStandardsManager _standardsManager; |
| | | 1647 | | |
| | 10 | 1648 | | public SoapSecurityInputSession(SecurityContextSecurityToken sessionToken, |
| | 10 | 1649 | | SecuritySessionServerSettings settings, ServerSecuritySessionChannel channel) |
| | | 1650 | | { |
| | 10 | 1651 | | _channel = channel; |
| | 10 | 1652 | | _securityContextTokenId = sessionToken.ContextId; |
| | 10 | 1653 | | Claim identityClaim = SecurityUtils.GetPrimaryIdentityClaim(sessionToken.AuthorizationPolicies); |
| | 10 | 1654 | | if (identityClaim != null) |
| | | 1655 | | { |
| | 10 | 1656 | | RemoteIdentity = EndpointIdentity.CreateIdentity(identityClaim); |
| | | 1657 | | } |
| | | 1658 | | |
| | 10 | 1659 | | _sessionTokenIdentifier = settings.IssuedSecurityTokenParameters.CreateKeyIdentifierClause(sessionTo |
| | 10 | 1660 | | _standardsManager = settings.SessionProtocolFactory.StandardsManager; |
| | 10 | 1661 | | } |
| | | 1662 | | |
| | 10 | 1663 | | public string Id => _securityContextTokenId.ToString(); |
| | | 1664 | | |
| | 0 | 1665 | | public EndpointIdentity RemoteIdentity { get; } |
| | | 1666 | | |
| | | 1667 | | public void WriteSessionTokenIdentifier(XmlDictionaryWriter writer) |
| | | 1668 | | { |
| | 0 | 1669 | | _standardsManager.SecurityTokenSerializer.WriteKeyIdentifierClause(writer, _sessionTokenIdentifier); |
| | 0 | 1670 | | } |
| | | 1671 | | |
| | | 1672 | | public bool TryReadSessionTokenIdentifier(XmlReader reader) |
| | | 1673 | | { |
| | 0 | 1674 | | if (!_standardsManager.SecurityTokenSerializer.CanReadKeyIdentifierClause(reader)) |
| | | 1675 | | { |
| | 0 | 1676 | | return false; |
| | | 1677 | | } |
| | | 1678 | | |
| | 0 | 1679 | | return _standardsManager.SecurityTokenSerializer.ReadKeyIdentifierClause(reader) is SecurityContextK |
| | | 1680 | | } |
| | | 1681 | | } |
| | | 1682 | | } |
| | | 1683 | | |
| | | 1684 | | private abstract class ServerSecuritySimplexSessionChannel : ServerSecuritySessionChannel |
| | | 1685 | | { |
| | | 1686 | | private readonly SoapSecurityInputSession _session; |
| | | 1687 | | private bool _receivedClose; |
| | | 1688 | | private bool _canSendCloseResponse; |
| | | 1689 | | private bool _sentCloseResponse; |
| | | 1690 | | private RequestContext _closeRequestContext; |
| | | 1691 | | private Message _closeResponse; |
| | | 1692 | | |
| | | 1693 | | public ServerSecuritySimplexSessionChannel( |
| | | 1694 | | SecuritySessionServerSettings settings, |
| | | 1695 | | SecurityContextSecurityToken sessionToken, |
| | | 1696 | | object listenerSecurityState, SecurityListenerSettingsLifetimeManager settingsLifetimeManager, EndpointA |
| | 8 | 1697 | | : base(settings, sessionToken, listenerSecurityState, settingsLifetimeManager, address) |
| | | 1698 | | { |
| | 8 | 1699 | | _session = new SoapSecurityInputSession(sessionToken, settings, this); |
| | 8 | 1700 | | } |
| | | 1701 | | |
| | 0 | 1702 | | public IInputSession Session => _session; |
| | | 1703 | | |
| | | 1704 | | private void CleanupPendingCloseState() |
| | | 1705 | | { |
| | 0 | 1706 | | lock (LocalLock) |
| | | 1707 | | { |
| | 0 | 1708 | | if (_closeResponse != null) |
| | | 1709 | | { |
| | 0 | 1710 | | _closeResponse.Close(); |
| | 0 | 1711 | | _closeResponse = null; |
| | | 1712 | | } |
| | 0 | 1713 | | if (_closeRequestContext != null) |
| | | 1714 | | { |
| | 0 | 1715 | | _closeRequestContext.Abort(); |
| | 0 | 1716 | | _closeRequestContext = null; |
| | | 1717 | | } |
| | 0 | 1718 | | } |
| | 0 | 1719 | | } |
| | | 1720 | | |
| | | 1721 | | protected override void AbortCore() |
| | | 1722 | | { |
| | 0 | 1723 | | base.AbortCore(); |
| | 0 | 1724 | | Settings.RemoveSessionChannel(_session.Id); |
| | 0 | 1725 | | CleanupPendingCloseState(); |
| | 0 | 1726 | | } |
| | | 1727 | | |
| | | 1728 | | protected override async Task CloseCoreAsync(CancellationToken token) |
| | | 1729 | | { |
| | 8 | 1730 | | await base.CloseCoreAsync(token); |
| | 8 | 1731 | | Settings.RemoveSessionChannel(_session.Id); |
| | 8 | 1732 | | } |
| | | 1733 | | |
| | | 1734 | | public virtual Task CloseAsync(CancellationToken token) |
| | | 1735 | | { |
| | 8 | 1736 | | return OnCloseAsync(token); |
| | | 1737 | | } |
| | | 1738 | | protected async Task OnCloseAsync(CancellationToken token) |
| | | 1739 | | { |
| | | 1740 | | // send a close response if one was not sent yet |
| | 8 | 1741 | | bool wasAborted = await SendCloseResponseOnCloseIfRequiredAsync(token); |
| | 8 | 1742 | | if (wasAborted) |
| | | 1743 | | { |
| | 0 | 1744 | | return; |
| | | 1745 | | } |
| | 8 | 1746 | | await CloseCoreAsync(token); |
| | 8 | 1747 | | } |
| | | 1748 | | |
| | | 1749 | | private bool ShouldSendCloseResponseOnClose(out RequestContext pendingCloseRequestContext, out Message pendi |
| | | 1750 | | { |
| | 8 | 1751 | | bool sendCloseResponse = false; |
| | 8 | 1752 | | lock (LocalLock) |
| | | 1753 | | { |
| | 8 | 1754 | | _canSendCloseResponse = true; |
| | 8 | 1755 | | if (!_sentCloseResponse && _receivedClose && _closeResponse != null) |
| | | 1756 | | { |
| | 8 | 1757 | | _sentCloseResponse = true; |
| | 8 | 1758 | | sendCloseResponse = true; |
| | 8 | 1759 | | pendingCloseRequestContext = _closeRequestContext; |
| | 8 | 1760 | | pendingCloseResponse = _closeResponse; |
| | 8 | 1761 | | _closeResponse = null; |
| | 8 | 1762 | | _closeRequestContext = null; |
| | | 1763 | | } |
| | | 1764 | | else |
| | | 1765 | | { |
| | 0 | 1766 | | _canSendCloseResponse = false; |
| | 0 | 1767 | | pendingCloseRequestContext = null; |
| | 0 | 1768 | | pendingCloseResponse = null; |
| | | 1769 | | } |
| | 0 | 1770 | | } |
| | 8 | 1771 | | return sendCloseResponse; |
| | | 1772 | | } |
| | | 1773 | | |
| | | 1774 | | private async Task<bool> SendCloseResponseOnCloseIfRequiredAsync(CancellationToken token) |
| | | 1775 | | { |
| | 8 | 1776 | | bool aborted = false; |
| | 8 | 1777 | | bool sendCloseResponse = ShouldSendCloseResponseOnClose(out RequestContext pendingCloseRequestContext, o |
| | 8 | 1778 | | bool cleanupCloseState = true; |
| | 8 | 1779 | | if (sendCloseResponse) |
| | | 1780 | | { |
| | | 1781 | | try |
| | | 1782 | | { |
| | 8 | 1783 | | await SendCloseResponseAsync(pendingCloseRequestContext, pendingCloseResponse, token); |
| | | 1784 | | // this.inputSessionClosedHandle.Set(); |
| | 8 | 1785 | | cleanupCloseState = false; |
| | 8 | 1786 | | } |
| | 0 | 1787 | | catch (CommunicationObjectAbortedException) |
| | | 1788 | | { |
| | 0 | 1789 | | if (State != CommunicationState.Closed) |
| | | 1790 | | { |
| | 0 | 1791 | | throw; |
| | | 1792 | | } |
| | 0 | 1793 | | aborted = true; |
| | 0 | 1794 | | } |
| | | 1795 | | finally |
| | | 1796 | | { |
| | 8 | 1797 | | if (cleanupCloseState) |
| | | 1798 | | { |
| | 0 | 1799 | | if (pendingCloseResponse != null) |
| | | 1800 | | { |
| | 0 | 1801 | | pendingCloseResponse.Close(); |
| | | 1802 | | } |
| | 0 | 1803 | | if (pendingCloseRequestContext != null) |
| | | 1804 | | { |
| | 0 | 1805 | | pendingCloseRequestContext.Abort(); |
| | | 1806 | | } |
| | | 1807 | | } |
| | | 1808 | | } |
| | | 1809 | | } |
| | | 1810 | | |
| | 8 | 1811 | | return aborted; |
| | 8 | 1812 | | } |
| | | 1813 | | |
| | | 1814 | | protected override void OnCloseResponseMessageReceived(RequestContext requestContext, Message message, Secur |
| | | 1815 | | { |
| | | 1816 | | // we dont expect a close-response for non-duplex security session |
| | 0 | 1817 | | message.Close(); |
| | 0 | 1818 | | requestContext.Abort(); |
| | 0 | 1819 | | Fault(new ProtocolException(SR.UnexpectedSecuritySessionCloseResponse)); |
| | 0 | 1820 | | } |
| | | 1821 | | |
| | | 1822 | | private void Fault(ProtocolException protocolException) |
| | | 1823 | | { |
| | 0 | 1824 | | AbortCore(); |
| | 0 | 1825 | | OnFaulted(protocolException); |
| | 0 | 1826 | | } |
| | | 1827 | | |
| | | 1828 | | protected override void OnCloseMessageReceived(RequestContext requestContext, Message message, SecurityProto |
| | | 1829 | | { |
| | 8 | 1830 | | if (State == CommunicationState.Created) |
| | | 1831 | | { |
| | | 1832 | | Fx.Assert("ServerSecuritySimplexSessionChannel.OnCloseMessageReceived (this.State == Created)"); |
| | 0 | 1833 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 1834 | | } |
| | 8 | 1835 | | if (SendCloseResponseOnCloseReceivedIfRequiredAsync(requestContext, message, correlationState, token).Re |
| | | 1836 | | { |
| | | 1837 | | // inputSessionClosedHandle.Set(); |
| | | 1838 | | } |
| | 8 | 1839 | | } |
| | | 1840 | | |
| | | 1841 | | private async Task<bool> SendCloseResponseOnCloseReceivedIfRequiredAsync(RequestContext requestContext, Mess |
| | | 1842 | | { |
| | 8 | 1843 | | bool sendCloseResponse = false; |
| | | 1844 | | // ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? TraceUtility.ExtractActivity(m |
| | 8 | 1845 | | bool cleanupContext = true; |
| | | 1846 | | try |
| | | 1847 | | { |
| | 8 | 1848 | | Message localCloseResponse = null; |
| | 8 | 1849 | | lock (LocalLock) |
| | | 1850 | | { |
| | 8 | 1851 | | if (!_receivedClose) |
| | | 1852 | | { |
| | 8 | 1853 | | _receivedClose = true; |
| | 8 | 1854 | | localCloseResponse = CreateCloseResponse(message, correlationState, token); |
| | 8 | 1855 | | if (_canSendCloseResponse) |
| | | 1856 | | { |
| | 0 | 1857 | | _sentCloseResponse = true; |
| | 0 | 1858 | | sendCloseResponse = true; |
| | | 1859 | | } |
| | | 1860 | | else |
| | | 1861 | | { |
| | | 1862 | | // save the close requestContext to reply later |
| | 8 | 1863 | | _closeRequestContext = requestContext; |
| | 8 | 1864 | | _closeResponse = localCloseResponse; |
| | 8 | 1865 | | cleanupContext = false; |
| | | 1866 | | } |
| | | 1867 | | } |
| | 8 | 1868 | | } |
| | 8 | 1869 | | if (sendCloseResponse) |
| | | 1870 | | { |
| | 0 | 1871 | | await SendCloseResponseAsync(requestContext, localCloseResponse, token); |
| | 0 | 1872 | | cleanupContext = false; |
| | | 1873 | | } |
| | 8 | 1874 | | else if (cleanupContext) |
| | | 1875 | | { |
| | 0 | 1876 | | await requestContext.CloseAsync(token); |
| | 0 | 1877 | | cleanupContext = false; |
| | | 1878 | | } |
| | 8 | 1879 | | return sendCloseResponse; |
| | | 1880 | | } |
| | | 1881 | | finally |
| | | 1882 | | { |
| | 8 | 1883 | | message.Close(); |
| | 8 | 1884 | | if (cleanupContext) |
| | | 1885 | | { |
| | 0 | 1886 | | requestContext.Abort(); |
| | | 1887 | | } |
| | | 1888 | | //if (DiagnosticUtility.ShouldUseActivity && (activity != null)) |
| | | 1889 | | //{ |
| | | 1890 | | // activity.Stop(); |
| | | 1891 | | //} |
| | | 1892 | | } |
| | 8 | 1893 | | } |
| | | 1894 | | |
| | | 1895 | | //Renamed SecurityReplySessionChannel => SecurityReplySessionServiceChannelDispatcher (implementd IServiceCh |
| | | 1896 | | public class SecurityReplySessionServiceChannelDispatcher : ServerSecuritySimplexSessionChannel, IServiceCha |
| | | 1897 | | { |
| | | 1898 | | private readonly IServiceProvider _serviceProvider; |
| | | 1899 | | private volatile IServiceChannelDispatcher _channelDispatcher; |
| | | 1900 | | public SecurityReplySessionServiceChannelDispatcher( |
| | | 1901 | | SecuritySessionServerSettings settings, |
| | | 1902 | | SecurityContextSecurityToken sessionToken, |
| | | 1903 | | object listenerSecurityState, SecurityListenerSettingsLifetimeManager settingsLifetimeManager |
| | | 1904 | | , IChannel channel, EndpointAddress address) |
| | 8 | 1905 | | : base(settings, |
| | 8 | 1906 | | sessionToken, listenerSecurityState, settingsLifetimeManager, address) |
| | | 1907 | | { |
| | 8 | 1908 | | IncomingChannel = (IReplyChannel)channel; |
| | 8 | 1909 | | _serviceProvider = IncomingChannel.GetProperty<IServiceScopeFactory>().CreateScope().ServiceProvider |
| | 8 | 1910 | | } |
| | | 1911 | | |
| | 32 | 1912 | | public IReplyChannel IncomingChannel { get; set; } |
| | | 1913 | | |
| | 0 | 1914 | | public IServiceChannelDispatcher ChannelDispatcher { get => throw new NotImplementedException(); set => |
| | | 1915 | | |
| | 8 | 1916 | | protected override bool CanDoSecurityCorrelation => true; |
| | | 1917 | | |
| | | 1918 | | #pragma warning disable CS0067 // The event is never used - see issue #287 |
| | | 1919 | | public event EventHandler Closed; |
| | | 1920 | | public event EventHandler Closing; |
| | | 1921 | | public event EventHandler Faulted; |
| | | 1922 | | public event EventHandler Opened; |
| | | 1923 | | public event EventHandler Opening; |
| | | 1924 | | #pragma warning restore CS0067 // The event is never used |
| | | 1925 | | |
| | | 1926 | | public void Abort() |
| | | 1927 | | { |
| | 0 | 1928 | | base.AbortCore(); |
| | 0 | 1929 | | } |
| | | 1930 | | |
| | | 1931 | | public Task CloseAsync() |
| | | 1932 | | { |
| | 0 | 1933 | | TimeoutHelper helper = new TimeoutHelper(ServiceDefaults.CloseTimeout); |
| | 0 | 1934 | | return CloseAsync(helper.GetCancellationToken()); |
| | | 1935 | | } |
| | | 1936 | | |
| | | 1937 | | public override Task CloseAsync(CancellationToken token) |
| | | 1938 | | { |
| | 8 | 1939 | | base.CloseAsync(token); |
| | 8 | 1940 | | return Task.CompletedTask; |
| | | 1941 | | } |
| | | 1942 | | |
| | | 1943 | | public async Task DispatchAsync(RequestContext context) |
| | | 1944 | | { |
| | 16 | 1945 | | RequestContext securityRequestContext = await ReceiveRequestAsync(context); |
| | 16 | 1946 | | await _channelDispatcher.DispatchAsync(securityRequestContext); |
| | 16 | 1947 | | } |
| | | 1948 | | |
| | | 1949 | | public Task DispatchAsync(Message message) |
| | | 1950 | | { |
| | 0 | 1951 | | throw new NotImplementedException(); |
| | | 1952 | | } |
| | | 1953 | | |
| | | 1954 | | public T GetProperty<T>() where T : class |
| | | 1955 | | { |
| | 32 | 1956 | | T tObj = _serviceProvider.GetService<T>(); |
| | 32 | 1957 | | if (tObj == null) |
| | | 1958 | | { |
| | 16 | 1959 | | return IncomingChannel.GetProperty<T>(); |
| | | 1960 | | } |
| | | 1961 | | else |
| | | 1962 | | { |
| | 16 | 1963 | | return tObj; |
| | | 1964 | | } |
| | | 1965 | | } |
| | | 1966 | | |
| | | 1967 | | public Task OpenAsync() |
| | | 1968 | | { |
| | 8 | 1969 | | return Task.CompletedTask; |
| | | 1970 | | } |
| | | 1971 | | |
| | | 1972 | | public override async Task OpenAsync(TimeSpan timeout) |
| | | 1973 | | { |
| | 8 | 1974 | | await base.OpenAsync(timeout); |
| | 8 | 1975 | | _channelDispatcher = await Settings.SecurityServiceDispatcher. |
| | 8 | 1976 | | GetInnerServiceChannelDispatcher(this); |
| | 8 | 1977 | | } |
| | | 1978 | | |
| | | 1979 | | public Task OpenAsync(CancellationToken token) |
| | | 1980 | | { |
| | 0 | 1981 | | return Task.CompletedTask; |
| | | 1982 | | } |
| | | 1983 | | } |
| | | 1984 | | } |
| | | 1985 | | |
| | | 1986 | | private class ServerSecurityDuplexSessionChannel : ServerSecuritySessionChannel //, IDuplexSessionChannel |
| | | 1987 | | { |
| | | 1988 | | private SoapSecurityServerDuplexSession _session; |
| | | 1989 | | private bool _isInputClosed; |
| | | 1990 | | private bool _isOutputClosed; |
| | | 1991 | | private bool _sentClose; |
| | | 1992 | | private bool _receivedClose; |
| | | 1993 | | private RequestContext _closeRequestContext; |
| | | 1994 | | private Message _closeResponseMessage; |
| | 2 | 1995 | | private InterruptibleWaitObject _outputSessionCloseHandle = new InterruptibleWaitObject(true); |
| | 2 | 1996 | | private InterruptibleWaitObject _inputSessionCloseHandle = new InterruptibleWaitObject(false); |
| | | 1997 | | private DuplexCommunication _duplexCommObj; |
| | | 1998 | | private IDuplexSessionChannel _duplexSessionChannel; |
| | | 1999 | | |
| | | 2000 | | public ServerSecurityDuplexSessionChannel( |
| | | 2001 | | SecuritySessionServerSettings settings, |
| | | 2002 | | SecurityContextSecurityToken sessionToken, |
| | | 2003 | | object listenerSecurityState, SecurityListenerSettingsLifetimeManager settingsLifetimeManager, EndpointA |
| | 2 | 2004 | | : base(settings, |
| | 2 | 2005 | | sessionToken, listenerSecurityState, settingsLifetimeManager, address) |
| | | 2006 | | { |
| | 2 | 2007 | | _session = new SoapSecurityServerDuplexSession(sessionToken, settings, this); |
| | 2 | 2008 | | _duplexCommObj = new DuplexCommunication(); |
| | 2 | 2009 | | _duplexSessionChannel = (IDuplexSessionChannel)channel; |
| | 2 | 2010 | | } |
| | | 2011 | | |
| | | 2012 | | public EndpointAddress RemoteAddress |
| | | 2013 | | { |
| | | 2014 | | get |
| | | 2015 | | { |
| | 2 | 2016 | | return LocalAddress; |
| | | 2017 | | } |
| | | 2018 | | } |
| | | 2019 | | |
| | | 2020 | | public Uri Via |
| | | 2021 | | { |
| | | 2022 | | get |
| | | 2023 | | { |
| | 0 | 2024 | | return RemoteAddress.Uri; |
| | | 2025 | | } |
| | | 2026 | | } |
| | | 2027 | | |
| | | 2028 | | public IDuplexSession Session |
| | | 2029 | | { |
| | | 2030 | | get |
| | | 2031 | | { |
| | 0 | 2032 | | return _session; |
| | | 2033 | | } |
| | | 2034 | | } |
| | | 2035 | | |
| | | 2036 | | protected override void AbortCore() |
| | | 2037 | | { |
| | 0 | 2038 | | _duplexSessionChannel.Abort(); |
| | 0 | 2039 | | base.AbortCore(); |
| | 0 | 2040 | | Settings.RemoveSessionChannel(_session.Id); |
| | 0 | 2041 | | CleanupPendingCloseState(); |
| | 0 | 2042 | | _inputSessionCloseHandle.Abort(_duplexCommObj); |
| | 0 | 2043 | | _outputSessionCloseHandle.Abort(_duplexCommObj); |
| | 0 | 2044 | | } |
| | | 2045 | | |
| | | 2046 | | private void CleanupPendingCloseState() |
| | | 2047 | | { |
| | 0 | 2048 | | lock (LocalLock) |
| | | 2049 | | { |
| | 0 | 2050 | | if (_closeResponseMessage != null) |
| | | 2051 | | { |
| | 0 | 2052 | | _closeResponseMessage.Close(); |
| | 0 | 2053 | | _closeResponseMessage = null; |
| | | 2054 | | } |
| | 0 | 2055 | | if (_closeRequestContext != null) |
| | | 2056 | | { |
| | 0 | 2057 | | _closeRequestContext.Abort(); |
| | 0 | 2058 | | _closeRequestContext = null; |
| | | 2059 | | } |
| | 0 | 2060 | | } |
| | 0 | 2061 | | } |
| | | 2062 | | |
| | | 2063 | | public void Abort() |
| | | 2064 | | { |
| | 0 | 2065 | | AbortCore(); |
| | 0 | 2066 | | } |
| | | 2067 | | |
| | | 2068 | | public Task CloseAsync() |
| | | 2069 | | { |
| | 0 | 2070 | | return CloseAsync(new TimeoutHelper(ServiceDefaults.CloseTimeout).GetCancellationToken()); |
| | | 2071 | | } |
| | | 2072 | | |
| | | 2073 | | public async Task CloseAsync(CancellationToken token) |
| | | 2074 | | { |
| | | 2075 | | // step 1: close output session |
| | 2 | 2076 | | await CloseOutputSessionAsync(token); |
| | | 2077 | | |
| | | 2078 | | // if the channel was aborted while closing the output session, return |
| | 2 | 2079 | | if (State == CommunicationState.Closed) |
| | | 2080 | | { |
| | 0 | 2081 | | return; |
| | | 2082 | | } |
| | | 2083 | | // step 2: wait for input session to be closed |
| | | 2084 | | |
| | | 2085 | | bool wasAborted; |
| | | 2086 | | bool didInputSessionClose; |
| | 2 | 2087 | | (didInputSessionClose, wasAborted) = await WaitForInputSessionCloseAsync(ServiceDefaults.CloseTimeout); |
| | 2 | 2088 | | if (wasAborted) |
| | | 2089 | | { |
| | 0 | 2090 | | return; |
| | | 2091 | | } |
| | | 2092 | | |
| | 2 | 2093 | | if (!didInputSessionClose) |
| | | 2094 | | { |
| | 0 | 2095 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new TimeoutException(SR.Format(SR.Servic |
| | | 2096 | | } |
| | | 2097 | | |
| | | 2098 | | // wait for any concurrent CloseOutputSessions to finish |
| | | 2099 | | bool didOutputSessionClose; |
| | 2 | 2100 | | (didOutputSessionClose, wasAborted) = await WaitForOutputSessionCloseAsync(ServiceDefaults.CloseTimeout) |
| | 2 | 2101 | | if (wasAborted) |
| | | 2102 | | { |
| | 0 | 2103 | | return; |
| | | 2104 | | } |
| | | 2105 | | |
| | 2 | 2106 | | if (!didOutputSessionClose) |
| | | 2107 | | { |
| | 0 | 2108 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new TimeoutException(SR.Format(SR.Servic |
| | | 2109 | | } |
| | | 2110 | | |
| | 2 | 2111 | | await CloseDuplexSessionChannelAsync(token); |
| | 2 | 2112 | | await CloseCoreAsync(token); |
| | 2 | 2113 | | Settings.RemoveSessionChannel(_session.Id); |
| | 2 | 2114 | | } |
| | | 2115 | | |
| | | 2116 | | private async Task CloseDuplexSessionChannelAsync(CancellationToken token) |
| | | 2117 | | { |
| | 2 | 2118 | | TimeoutHelper timeoutHelper = new TimeoutHelper(TimeSpan.FromMinutes(30)); |
| | 2 | 2119 | | await ((ISessionChannel<IDuplexSession>)_duplexSessionChannel).Session.CloseOutputSessionAsync(timeoutHe |
| | | 2120 | | |
| | 2 | 2121 | | TimeSpan iterationTimeout = timeoutHelper.RemainingTime(); |
| | 2 | 2122 | | bool lastIteration = (iterationTimeout == TimeSpan.Zero); |
| | | 2123 | | |
| | 0 | 2124 | | while (true) |
| | | 2125 | | { |
| | 2 | 2126 | | Message message = null; |
| | 2 | 2127 | | bool receiveThrowing = true; |
| | | 2128 | | try |
| | | 2129 | | { |
| | 2 | 2130 | | (Message receiveMessage, bool success) = await _duplexSessionChannel.TryReceiveAsync(timeoutHelp |
| | | 2131 | | |
| | 2 | 2132 | | receiveThrowing = false; |
| | 2 | 2133 | | if (success && receiveMessage == null) |
| | | 2134 | | { |
| | 2 | 2135 | | await _duplexSessionChannel.CloseAsync(timeoutHelper.GetCancellationToken()); |
| | 2 | 2136 | | return; |
| | | 2137 | | } |
| | 0 | 2138 | | } |
| | | 2139 | | catch (Exception e) |
| | | 2140 | | { |
| | 0 | 2141 | | if (Fx.IsFatal(e)) |
| | 0 | 2142 | | throw; |
| | | 2143 | | |
| | 0 | 2144 | | if (receiveThrowing) |
| | | 2145 | | { |
| | 0 | 2146 | | receiveThrowing = false; |
| | | 2147 | | } |
| | | 2148 | | else |
| | | 2149 | | { |
| | 0 | 2150 | | throw; |
| | | 2151 | | } |
| | 0 | 2152 | | } |
| | | 2153 | | finally |
| | | 2154 | | { |
| | 2 | 2155 | | if (message != null) |
| | 0 | 2156 | | message.Close(); |
| | | 2157 | | |
| | 2 | 2158 | | if (receiveThrowing) |
| | 0 | 2159 | | _duplexSessionChannel.Abort(); |
| | | 2160 | | } |
| | | 2161 | | |
| | 0 | 2162 | | if (lastIteration || _duplexSessionChannel.State != CommunicationState.Opened) |
| | | 2163 | | break; |
| | | 2164 | | |
| | 0 | 2165 | | iterationTimeout = timeoutHelper.RemainingTime(); |
| | 0 | 2166 | | lastIteration = (iterationTimeout == TimeSpan.Zero); |
| | 0 | 2167 | | } |
| | | 2168 | | |
| | 0 | 2169 | | _duplexSessionChannel.Abort(); |
| | 2 | 2170 | | } |
| | | 2171 | | |
| | | 2172 | | private void DetermineCloseOutputSessionMessage(out bool sendClose, out bool sendCloseResponse, out Message |
| | | 2173 | | { |
| | 2 | 2174 | | sendClose = false; |
| | 2 | 2175 | | sendCloseResponse = false; |
| | 2 | 2176 | | pendingCloseResponseMessage = null; |
| | 2 | 2177 | | pendingCloseRequestContext = null; |
| | 2 | 2178 | | lock (LocalLock) |
| | | 2179 | | { |
| | 2 | 2180 | | if (!_isOutputClosed) |
| | | 2181 | | { |
| | 2 | 2182 | | _isOutputClosed = true; |
| | 2 | 2183 | | if (_receivedClose) |
| | | 2184 | | { |
| | 2 | 2185 | | if (_closeResponseMessage != null) |
| | | 2186 | | { |
| | 2 | 2187 | | pendingCloseResponseMessage = _closeResponseMessage; |
| | 2 | 2188 | | pendingCloseRequestContext = _closeRequestContext; |
| | 2 | 2189 | | _closeResponseMessage = null; |
| | 2 | 2190 | | _closeRequestContext = null; |
| | 2 | 2191 | | sendCloseResponse = true; |
| | | 2192 | | } |
| | | 2193 | | } |
| | | 2194 | | else |
| | | 2195 | | { |
| | 0 | 2196 | | sendClose = true; |
| | 0 | 2197 | | _sentClose = true; |
| | | 2198 | | } |
| | | 2199 | | |
| | 2 | 2200 | | _outputSessionCloseHandle.Reset(); |
| | | 2201 | | } |
| | 2 | 2202 | | } |
| | 2 | 2203 | | } |
| | | 2204 | | |
| | | 2205 | | private async Task CloseOutputSessionAsync(CancellationToken token) |
| | | 2206 | | { |
| | 2 | 2207 | | bool sendClose = false; |
| | 2 | 2208 | | bool sendCloseResponse = false; |
| | | 2209 | | Message pendingCloseResponseMessage; |
| | | 2210 | | RequestContext pendingCloseRequestContext; |
| | | 2211 | | try |
| | | 2212 | | { |
| | 2 | 2213 | | DetermineCloseOutputSessionMessage(out sendClose, out sendCloseResponse, out pendingCloseResponseMes |
| | 2 | 2214 | | if (sendCloseResponse) |
| | | 2215 | | { |
| | 2 | 2216 | | bool cleanupCloseState = true; |
| | | 2217 | | try |
| | | 2218 | | { |
| | 2 | 2219 | | await SendCloseResponseAsync(pendingCloseRequestContext, pendingCloseResponseMessage, token) |
| | 2 | 2220 | | cleanupCloseState = false; |
| | 2 | 2221 | | } |
| | | 2222 | | finally |
| | | 2223 | | { |
| | 2 | 2224 | | if (cleanupCloseState) |
| | | 2225 | | { |
| | 0 | 2226 | | pendingCloseResponseMessage.Close(); |
| | 0 | 2227 | | pendingCloseRequestContext.Abort(); |
| | | 2228 | | } |
| | | 2229 | | } |
| | | 2230 | | } |
| | 0 | 2231 | | else if (sendClose) |
| | | 2232 | | { |
| | 0 | 2233 | | await SendCloseAsync(token); |
| | | 2234 | | } |
| | 2 | 2235 | | } |
| | 0 | 2236 | | catch (CommunicationObjectAbortedException) |
| | | 2237 | | { |
| | 0 | 2238 | | if (State != CommunicationState.Closed) throw; |
| | | 2239 | | // a parallel thread aborted the channel. ignore the exception |
| | 0 | 2240 | | } |
| | | 2241 | | finally |
| | | 2242 | | { |
| | 2 | 2243 | | if (sendClose || sendCloseResponse) |
| | | 2244 | | { |
| | 2 | 2245 | | _outputSessionCloseHandle.Set(); |
| | | 2246 | | } |
| | | 2247 | | } |
| | 2 | 2248 | | } |
| | | 2249 | | |
| | | 2250 | | protected override void OnCloseMessageReceived(RequestContext requestContext, Message message, SecurityProto |
| | | 2251 | | { |
| | 2 | 2252 | | if (State == CommunicationState.Created) |
| | | 2253 | | { |
| | | 2254 | | Fx.Assert("ServerSecurityDuplexSessionChannel.OnCloseMessageReceived (State == Created)"); |
| | 0 | 2255 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 2256 | | } |
| | | 2257 | | |
| | 2 | 2258 | | bool setInputSessionCloseHandle = false; |
| | 2 | 2259 | | bool cleanupContext = true; |
| | | 2260 | | try |
| | | 2261 | | { |
| | 2 | 2262 | | lock (LocalLock) |
| | | 2263 | | { |
| | 2 | 2264 | | _receivedClose = true; |
| | 2 | 2265 | | if (!_isInputClosed) |
| | | 2266 | | { |
| | 2 | 2267 | | _isInputClosed = true; |
| | 2 | 2268 | | setInputSessionCloseHandle = true; |
| | | 2269 | | |
| | 2 | 2270 | | if (!_isOutputClosed) |
| | | 2271 | | { |
| | 2 | 2272 | | _closeRequestContext = requestContext; |
| | | 2273 | | // CreateCloseResponse closes the message passed in |
| | 2 | 2274 | | _closeResponseMessage = CreateCloseResponse(message, null, token); |
| | 2 | 2275 | | cleanupContext = false; |
| | | 2276 | | } |
| | | 2277 | | } |
| | 2 | 2278 | | } |
| | | 2279 | | |
| | 2 | 2280 | | if (setInputSessionCloseHandle) |
| | | 2281 | | { |
| | 2 | 2282 | | _inputSessionCloseHandle.Set(); |
| | | 2283 | | } |
| | 2 | 2284 | | if (cleanupContext) |
| | | 2285 | | { |
| | 0 | 2286 | | requestContext.CloseAsync(); |
| | 0 | 2287 | | cleanupContext = false; |
| | | 2288 | | } |
| | 2 | 2289 | | } |
| | | 2290 | | finally |
| | | 2291 | | { |
| | 2 | 2292 | | message.Close(); |
| | 2 | 2293 | | if (cleanupContext) |
| | | 2294 | | { |
| | 0 | 2295 | | requestContext.Abort(); |
| | | 2296 | | } |
| | 2 | 2297 | | } |
| | 2 | 2298 | | } |
| | | 2299 | | |
| | | 2300 | | protected override void OnCloseResponseMessageReceived(RequestContext requestContext, Message message, Secur |
| | | 2301 | | { |
| | 0 | 2302 | | bool cleanupContext = true; |
| | | 2303 | | try |
| | | 2304 | | { |
| | 0 | 2305 | | bool isCloseResponseExpected = false; |
| | 0 | 2306 | | bool setInputSessionCloseHandle = false; |
| | 0 | 2307 | | lock (LocalLock) |
| | | 2308 | | { |
| | 0 | 2309 | | isCloseResponseExpected = _sentClose; |
| | 0 | 2310 | | if (isCloseResponseExpected && !_isInputClosed) |
| | | 2311 | | { |
| | 0 | 2312 | | _isInputClosed = true; |
| | 0 | 2313 | | setInputSessionCloseHandle = true; |
| | | 2314 | | } |
| | 0 | 2315 | | } |
| | 0 | 2316 | | if (!isCloseResponseExpected) |
| | | 2317 | | { |
| | 0 | 2318 | | _duplexCommObj.Fault(new ProtocolException(SR.Format(SR.UnexpectedSecuritySessionCloseResponse)) |
| | 0 | 2319 | | return; |
| | | 2320 | | } |
| | 0 | 2321 | | if (setInputSessionCloseHandle) |
| | | 2322 | | { |
| | 0 | 2323 | | _inputSessionCloseHandle.Set(); |
| | | 2324 | | } |
| | | 2325 | | |
| | 0 | 2326 | | requestContext.CloseAsync(); |
| | 0 | 2327 | | cleanupContext = false; |
| | 0 | 2328 | | } |
| | | 2329 | | finally |
| | | 2330 | | { |
| | 0 | 2331 | | message.Close(); |
| | 0 | 2332 | | if (cleanupContext) |
| | | 2333 | | { |
| | 0 | 2334 | | requestContext.Abort(); |
| | | 2335 | | } |
| | 0 | 2336 | | } |
| | 0 | 2337 | | } |
| | | 2338 | | |
| | | 2339 | | internal async Task<(bool success, bool wasAborted)> WaitForOutputSessionCloseAsync(TimeSpan timeout) |
| | | 2340 | | { |
| | | 2341 | | try |
| | | 2342 | | { |
| | 2 | 2343 | | return (await _outputSessionCloseHandle.WaitAsync(timeout, false), false); |
| | | 2344 | | } |
| | 0 | 2345 | | catch (CommunicationObjectAbortedException) |
| | | 2346 | | { |
| | 0 | 2347 | | if (State != CommunicationState.Closed) throw; |
| | 0 | 2348 | | return (true, true); |
| | | 2349 | | } |
| | 2 | 2350 | | } |
| | | 2351 | | |
| | | 2352 | | private async Task<(bool success, bool wasAborted)> WaitForInputSessionCloseAsync(TimeSpan timeout) |
| | | 2353 | | { |
| | 2 | 2354 | | TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); |
| | 2 | 2355 | | bool wasAborted = false; |
| | | 2356 | | try |
| | | 2357 | | { |
| | 2 | 2358 | | (bool success, RequestContext requestContext) receivedRequestTry = await TryReceiveRequestAsync(time |
| | 2 | 2359 | | RequestContext context = receivedRequestTry.requestContext; |
| | 2 | 2360 | | if (!receivedRequestTry.success) |
| | | 2361 | | { |
| | 0 | 2362 | | return (false, wasAborted); |
| | | 2363 | | } |
| | | 2364 | | |
| | 2 | 2365 | | if (context != null && context.RequestMessage != null) |
| | | 2366 | | { |
| | 0 | 2367 | | Message message = context.RequestMessage; |
| | 0 | 2368 | | using (message) |
| | | 2369 | | { |
| | 0 | 2370 | | ProtocolException error = ProtocolException.ReceiveShutdownReturnedNonNull(message); |
| | 0 | 2371 | | throw TraceUtility.ThrowHelperWarning(error, message); |
| | | 2372 | | } |
| | | 2373 | | } |
| | | 2374 | | |
| | 2 | 2375 | | bool result = await _inputSessionCloseHandle.WaitAsync(timeoutHelper.RemainingTime(), false); |
| | 2 | 2376 | | if (!result) |
| | | 2377 | | { |
| | 0 | 2378 | | return (false, wasAborted); |
| | | 2379 | | } |
| | | 2380 | | else |
| | | 2381 | | { |
| | 2 | 2382 | | lock (LocalLock) |
| | | 2383 | | { |
| | 2 | 2384 | | if (!(_isInputClosed)) |
| | | 2385 | | { |
| | | 2386 | | Fx.Assert("Shutdown request was not received."); |
| | 0 | 2387 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | | 2388 | | } |
| | 2 | 2389 | | } |
| | | 2390 | | |
| | 2 | 2391 | | return (true, wasAborted); |
| | | 2392 | | } |
| | | 2393 | | } |
| | 0 | 2394 | | catch (CommunicationObjectAbortedException) |
| | | 2395 | | { |
| | 0 | 2396 | | if (State != CommunicationState.Closed) |
| | | 2397 | | { |
| | 0 | 2398 | | throw; |
| | | 2399 | | } |
| | | 2400 | | |
| | 0 | 2401 | | wasAborted = true; |
| | 0 | 2402 | | } |
| | | 2403 | | |
| | 0 | 2404 | | return (false, wasAborted); |
| | 2 | 2405 | | } |
| | | 2406 | | |
| | | 2407 | | private class SoapSecurityServerDuplexSession : SoapSecurityInputSession, IDuplexSession |
| | | 2408 | | { |
| | | 2409 | | private ServerSecurityDuplexSessionChannel _channel; |
| | | 2410 | | |
| | | 2411 | | public SoapSecurityServerDuplexSession(SecurityContextSecurityToken sessionToken, SecuritySessionServerS |
| | 2 | 2412 | | : base(sessionToken, settings, channel) |
| | | 2413 | | { |
| | 2 | 2414 | | _channel = channel; |
| | 2 | 2415 | | } |
| | | 2416 | | |
| | | 2417 | | public Task CloseOutputSessionAsync() |
| | | 2418 | | { |
| | 0 | 2419 | | return CloseOutputSessionAsync(ServiceDefaults.CloseTimeout); ; |
| | | 2420 | | } |
| | | 2421 | | |
| | | 2422 | | public Task CloseOutputSessionAsync(CancellationToken token) |
| | | 2423 | | { |
| | 0 | 2424 | | return CloseOutputSessionAsync(); |
| | | 2425 | | } |
| | | 2426 | | |
| | | 2427 | | private async Task CloseOutputSessionAsync(TimeSpan timeout) |
| | | 2428 | | { |
| | | 2429 | | // channel.ThrowIfFaulted(); |
| | | 2430 | | // channel.ThrowIfNotOpened(); |
| | 0 | 2431 | | Exception pendingException = null; |
| | | 2432 | | try |
| | | 2433 | | { |
| | 0 | 2434 | | await _channel.CloseOutputSessionAsync(new TimeoutHelper(timeout).GetCancellationToken()); |
| | 0 | 2435 | | } |
| | 0 | 2436 | | catch (Exception e) |
| | | 2437 | | { |
| | 0 | 2438 | | if (Fx.IsFatal(e)) |
| | | 2439 | | { |
| | 0 | 2440 | | throw; |
| | | 2441 | | } |
| | | 2442 | | |
| | 0 | 2443 | | pendingException = e; |
| | 0 | 2444 | | } |
| | 0 | 2445 | | if (pendingException != null) |
| | | 2446 | | { |
| | 0 | 2447 | | _channel.OnFaulted(pendingException); |
| | 0 | 2448 | | if (pendingException is CommunicationException) |
| | | 2449 | | { |
| | 0 | 2450 | | throw pendingException; |
| | | 2451 | | } |
| | | 2452 | | else |
| | | 2453 | | { |
| | 0 | 2454 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(pendingException); |
| | | 2455 | | } |
| | | 2456 | | } |
| | 0 | 2457 | | } |
| | | 2458 | | } |
| | | 2459 | | |
| | | 2460 | | public class ServerSecurityDuplexSessionChannelDispatcher : ServerSecurityDuplexSessionChannel, IDuplexSessi |
| | | 2461 | | { |
| | | 2462 | | private readonly IServiceProvider _serviceProvider; |
| | | 2463 | | private volatile IServiceChannelDispatcher _channelDispatcher; |
| | | 2464 | | |
| | | 2465 | | public ServerSecurityDuplexSessionChannelDispatcher( |
| | | 2466 | | SecuritySessionServerSettings settings, |
| | | 2467 | | SecurityContextSecurityToken sessionToken, |
| | | 2468 | | object listenerSecurityState, SecurityListenerSettingsLifetimeManager settingsLifetimeManager |
| | | 2469 | | , IChannel channel, EndpointAddress address) |
| | 2 | 2470 | | : base(settings, |
| | 2 | 2471 | | sessionToken, listenerSecurityState, settingsLifetimeManager, address, channel) |
| | | 2472 | | { |
| | 2 | 2473 | | IncomingChannel = (IDuplexSessionChannel)channel; |
| | 2 | 2474 | | _serviceProvider = IncomingChannel.GetProperty<IServiceScopeFactory>().CreateScope().ServiceProvider |
| | 2 | 2475 | | } |
| | | 2476 | | |
| | 14 | 2477 | | public IDuplexSessionChannel IncomingChannel { get; set; } |
| | 0 | 2478 | | public IServiceChannelDispatcher ChannelDispatcher { get => throw new NotImplementedException(); set => |
| | | 2479 | | |
| | | 2480 | | #pragma warning disable CS0067 // The event is never used |
| | | 2481 | | public event EventHandler Closed; |
| | | 2482 | | public event EventHandler Closing; |
| | | 2483 | | public event EventHandler Faulted; |
| | | 2484 | | public event EventHandler Opened; |
| | | 2485 | | public event EventHandler Opening; |
| | | 2486 | | #pragma warning restore CS0067 // The event is never used |
| | | 2487 | | |
| | | 2488 | | public Task DispatchAsync(RequestContext context) |
| | | 2489 | | { |
| | 0 | 2490 | | return DispatchAsync(context.RequestMessage); |
| | | 2491 | | } |
| | | 2492 | | |
| | | 2493 | | public async Task DispatchAsync(Message message) |
| | | 2494 | | { |
| | 4 | 2495 | | DuplexSessionRequestContext duplexSessionRequestContext = new DuplexSessionRequestContext(IncomingCh |
| | 4 | 2496 | | RequestContext context = await ReceiveRequestAsync(duplexSessionRequestContext); |
| | 4 | 2497 | | await _channelDispatcher.DispatchAsync(context == null ? null : context.RequestMessage); |
| | 4 | 2498 | | } |
| | | 2499 | | |
| | | 2500 | | public T GetProperty<T>() where T : class |
| | | 2501 | | { |
| | 8 | 2502 | | T tObj = _serviceProvider.GetService<T>(); |
| | 8 | 2503 | | if (tObj == null) |
| | | 2504 | | { |
| | 4 | 2505 | | return IncomingChannel.GetProperty<T>(); |
| | | 2506 | | } |
| | | 2507 | | else |
| | | 2508 | | { |
| | 4 | 2509 | | return tObj; |
| | | 2510 | | } |
| | | 2511 | | } |
| | | 2512 | | |
| | | 2513 | | public Task OpenAsync() |
| | | 2514 | | { |
| | 2 | 2515 | | return Task.CompletedTask; |
| | | 2516 | | // NO op, call by Channel Handler in the upper chain from CreateServiceChannel dispatcher from below |
| | | 2517 | | } |
| | | 2518 | | |
| | | 2519 | | public override async Task OpenAsync(TimeSpan timeout) |
| | | 2520 | | { |
| | 2 | 2521 | | await base.OpenAsync(timeout); |
| | 2 | 2522 | | _channelDispatcher = await Settings.SecurityServiceDispatcher. |
| | 2 | 2523 | | GetInnerServiceChannelDispatcher(this); |
| | 2 | 2524 | | } |
| | | 2525 | | |
| | | 2526 | | public Task OpenAsync(CancellationToken token) |
| | | 2527 | | { |
| | 0 | 2528 | | return OpenAsync(); |
| | | 2529 | | } |
| | | 2530 | | |
| | | 2531 | | public Task SendAsync(Message message) |
| | | 2532 | | { |
| | 0 | 2533 | | return SendAsync(message, new TimeoutHelper(ServiceDefaults.SendTimeout).GetCancellationToken()); |
| | | 2534 | | } |
| | | 2535 | | |
| | | 2536 | | public Task SendAsync(Message message, CancellationToken token) |
| | | 2537 | | { |
| | 2 | 2538 | | CheckOutputOpen(); |
| | 2 | 2539 | | SecureApplicationMessage(ref message, null, token); |
| | | 2540 | | // ChannelBinder.Send(message, timeoutHelper.RemainingTime()); |
| | 2 | 2541 | | return IncomingChannel.SendAsync(message, token); |
| | | 2542 | | } |
| | | 2543 | | |
| | 0 | 2544 | | public Task<Message> ReceiveAsync(CancellationToken token) => throw new NotImplementedException(); |
| | | 2545 | | |
| | 0 | 2546 | | public Task<(Message message, bool success)> TryReceiveAsync(CancellationToken token) => throw new NotIm |
| | | 2547 | | } |
| | | 2548 | | |
| | | 2549 | | protected void CheckOutputOpen() |
| | | 2550 | | { |
| | | 2551 | | // ThrowIfClosedOrNotOpen(); |
| | 2 | 2552 | | lock (LocalLock) |
| | | 2553 | | { |
| | 2 | 2554 | | if (_isOutputClosed) |
| | | 2555 | | { |
| | 0 | 2556 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new CommunicationException(SR.Format |
| | | 2557 | | } |
| | 2 | 2558 | | } |
| | 2 | 2559 | | } |
| | | 2560 | | |
| | | 2561 | | internal override void OnFaulted(Exception ex) |
| | | 2562 | | { |
| | 0 | 2563 | | AbortCore(); |
| | 0 | 2564 | | _inputSessionCloseHandle.Fault(_duplexCommObj); |
| | 0 | 2565 | | _outputSessionCloseHandle.Fault(_duplexCommObj); |
| | 0 | 2566 | | base.OnFaulted(ex); |
| | 0 | 2567 | | } |
| | | 2568 | | } |
| | | 2569 | | |
| | | 2570 | | //Dummy class to hold Communication object and not changing API. |
| | | 2571 | | //ServerSecuritySession channel not inheriting from ChannelBase as there is no use. |
| | | 2572 | | private class DuplexCommunication : ChannelManagerBase |
| | | 2573 | | { |
| | 0 | 2574 | | protected override TimeSpan DefaultReceiveTimeout => ServiceDefaults.ReceiveTimeout; |
| | | 2575 | | |
| | 0 | 2576 | | protected override TimeSpan DefaultSendTimeout => ServiceDefaults.SendTimeout; |
| | | 2577 | | |
| | 0 | 2578 | | protected override TimeSpan DefaultCloseTimeout => ServiceDefaults.CloseTimeout; |
| | | 2579 | | |
| | 0 | 2580 | | protected override TimeSpan DefaultOpenTimeout => ServiceDefaults.OpenTimeout; |
| | | 2581 | | |
| | 0 | 2582 | | protected override void OnAbort() { } |
| | 0 | 2583 | | protected override Task OnCloseAsync(CancellationToken token) => throw new NotImplementedException(); |
| | 0 | 2584 | | protected override Task OnOpenAsync(CancellationToken token) => throw new NotImplementedException(); |
| | | 2585 | | } |
| | | 2586 | | |
| | | 2587 | | internal class DuplexSessionRequestContext : RequestContextBase |
| | | 2588 | | { |
| | | 2589 | | private readonly IDuplexChannel _channel; |
| | | 2590 | | public DuplexSessionRequestContext(IDuplexChannel channel, Message request) |
| | 4 | 2591 | | : base(request, ServiceDefaults.CloseTimeout, ServiceDefaults.SendTimeout) |
| | | 2592 | | { |
| | 4 | 2593 | | _channel = channel; |
| | 4 | 2594 | | } |
| | | 2595 | | |
| | 0 | 2596 | | protected override void OnAbort() { } |
| | | 2597 | | |
| | | 2598 | | protected override |
| | | 2599 | | Task OnCloseAsync(CancellationToken token) |
| | | 2600 | | { |
| | 2 | 2601 | | return Task.CompletedTask; |
| | | 2602 | | } |
| | | 2603 | | |
| | | 2604 | | protected override async Task OnReplyAsync(Message message, CancellationToken token) |
| | | 2605 | | { |
| | 2 | 2606 | | if (message != null) |
| | | 2607 | | { |
| | 2 | 2608 | | await _channel.SendAsync(message, token); |
| | | 2609 | | } |
| | 2 | 2610 | | } |
| | | 2611 | | } |
| | | 2612 | | |
| | | 2613 | | private class SecuritySessionRequestContext : RequestContextBase |
| | | 2614 | | { |
| | | 2615 | | private readonly RequestContext _requestContext; |
| | | 2616 | | private readonly ServerSecuritySessionChannel _channel; |
| | | 2617 | | private readonly SecurityProtocolCorrelationState _correlationState; |
| | | 2618 | | |
| | | 2619 | | public SecuritySessionRequestContext(RequestContext requestContext, Message requestMessage, SecurityProtocol |
| | 10 | 2620 | | : base(requestMessage, ServiceDefaults.CloseTimeout, ServiceDefaults.SendTimeout) |
| | | 2621 | | { |
| | 10 | 2622 | | _requestContext = requestContext; |
| | 10 | 2623 | | _correlationState = correlationState; |
| | 10 | 2624 | | _channel = channel; |
| | 10 | 2625 | | } |
| | | 2626 | | |
| | | 2627 | | protected override void OnAbort() |
| | | 2628 | | { |
| | 0 | 2629 | | _requestContext.Abort(); |
| | 0 | 2630 | | } |
| | | 2631 | | |
| | | 2632 | | protected override Task OnCloseAsync(CancellationToken token) |
| | | 2633 | | { |
| | 8 | 2634 | | return _requestContext.CloseAsync(token); |
| | | 2635 | | } |
| | | 2636 | | |
| | | 2637 | | protected override Task OnReplyAsync(Message message, CancellationToken token) |
| | | 2638 | | { |
| | 8 | 2639 | | if (message != null) |
| | | 2640 | | { |
| | 8 | 2641 | | _channel.SecureApplicationMessage(ref message, _correlationState, token); |
| | 8 | 2642 | | return _requestContext.ReplyAsync(message); |
| | | 2643 | | } |
| | | 2644 | | else |
| | | 2645 | | { |
| | 0 | 2646 | | return Task.CompletedTask; |
| | | 2647 | | } |
| | | 2648 | | } |
| | | 2649 | | } |
| | | 2650 | | |
| | | 2651 | | //Failure Demuxer handler |
| | | 2652 | | internal class SecuritySessionDemuxFailureHandler : IChannelDemuxFailureHandler |
| | | 2653 | | { |
| | | 2654 | | private readonly SecurityStandardsManager _standardsManager; |
| | | 2655 | | |
| | 22 | 2656 | | public SecuritySessionDemuxFailureHandler(SecurityStandardsManager standardsManager) |
| | | 2657 | | { |
| | 22 | 2658 | | _standardsManager = standardsManager ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull |
| | 22 | 2659 | | } |
| | | 2660 | | |
| | | 2661 | | public void HandleDemuxFailure(Message message) |
| | | 2662 | | { |
| | 0 | 2663 | | if (message == null) |
| | | 2664 | | { |
| | 0 | 2665 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 2666 | | } |
| | 0 | 2667 | | } |
| | | 2668 | | |
| | | 2669 | | public Message CreateSessionDemuxFaultMessage(Message message) |
| | | 2670 | | { |
| | 0 | 2671 | | MessageFault fault = SecurityUtils.CreateSecurityContextNotFoundFault(_standardsManager, message.Headers |
| | 0 | 2672 | | Message faultMessage = Message.CreateMessage(message.Version, fault, message.Version.Addressing.DefaultF |
| | 0 | 2673 | | if (message.Headers.MessageId != null) |
| | | 2674 | | { |
| | 0 | 2675 | | faultMessage.InitializeReply(message); |
| | | 2676 | | } |
| | | 2677 | | |
| | 0 | 2678 | | return faultMessage; |
| | | 2679 | | } |
| | | 2680 | | |
| | | 2681 | | public Task HandleDemuxFailureAsync(Message message) |
| | | 2682 | | { |
| | 0 | 2683 | | throw new NotImplementedException(); |
| | | 2684 | | } |
| | | 2685 | | |
| | | 2686 | | public async Task HandleDemuxFailureAsync(Message message, RequestContext faultContext) |
| | | 2687 | | { |
| | 0 | 2688 | | HandleDemuxFailure(message); |
| | 0 | 2689 | | Message faultMessage = CreateSessionDemuxFaultMessage(message); |
| | | 2690 | | try |
| | | 2691 | | { |
| | 0 | 2692 | | await faultContext.ReplyAsync(faultMessage); |
| | 0 | 2693 | | } |
| | | 2694 | | catch (Exception ex) |
| | | 2695 | | { |
| | 0 | 2696 | | if (Fx.IsFatal(ex)) |
| | | 2697 | | { |
| | 0 | 2698 | | throw; |
| | | 2699 | | } |
| | 0 | 2700 | | } |
| | | 2701 | | finally |
| | | 2702 | | { |
| | 0 | 2703 | | faultMessage.Close(); |
| | | 2704 | | } |
| | 0 | 2705 | | } |
| | | 2706 | | } |
| | | 2707 | | } |
| | | 2708 | | } |