| | | 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.Collections.ObjectModel; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.Description; |
| | | 11 | | using CoreWCF.IdentityModel.Policy; |
| | | 12 | | using CoreWCF.IdentityModel.Selectors; |
| | | 13 | | using CoreWCF.IdentityModel.Tokens; |
| | | 14 | | using CoreWCF.Runtime; |
| | | 15 | | |
| | | 16 | | namespace CoreWCF.Security |
| | | 17 | | { |
| | | 18 | | internal class TransportSecurityProtocol : SecurityProtocol |
| | | 19 | | { |
| | 34 | 20 | | public TransportSecurityProtocol(TransportSecurityProtocolFactory factory, EndpointAddress target, Uri via) : ba |
| | | 21 | | { |
| | 34 | 22 | | } |
| | | 23 | | |
| | | 24 | | public override Message SecureOutgoingMessage(Message message, CancellationToken token) |
| | | 25 | | { |
| | 63 | 26 | | if (message == null) |
| | | 27 | | { |
| | 0 | 28 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 29 | | } |
| | 63 | 30 | | CommunicationObject.ThrowIfClosedOrNotOpen(); |
| | 63 | 31 | | string actor = string.Empty; // message.Version.Envelope.UltimateDestinationActor; |
| | | 32 | | try |
| | | 33 | | { |
| | 63 | 34 | | if (SecurityProtocolFactory.ActAsInitiator) |
| | | 35 | | { |
| | | 36 | | Fx.Assert(false, "Server side code"); |
| | | 37 | | } |
| | | 38 | | else |
| | | 39 | | { |
| | 63 | 40 | | message = SecureOutgoingMessageAtResponder(message, actor); |
| | | 41 | | } |
| | 63 | 42 | | base.OnOutgoingMessageSecured(message); |
| | 63 | 43 | | } |
| | 0 | 44 | | catch |
| | | 45 | | { |
| | 0 | 46 | | base.OnSecureOutgoingMessageFailure(message); |
| | 0 | 47 | | throw; |
| | | 48 | | } |
| | | 49 | | |
| | 63 | 50 | | return message; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | protected virtual Message SecureOutgoingMessageAtResponder(Message message, string actor) |
| | | 54 | | { |
| | 63 | 55 | | if (SecurityProtocolFactory.AddTimestamp && !SecurityProtocolFactory.SecurityBindingElement.EnableUnsecuredR |
| | | 56 | | { |
| | 63 | 57 | | SendSecurityHeader securityHeader = CreateSendSecurityHeaderForTransportProtocol(message, actor, Securit |
| | 63 | 58 | | message = securityHeader.SetupExecution(); |
| | | 59 | | } |
| | 63 | 60 | | return message; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public sealed override async ValueTask<Message> VerifyIncomingMessageAsync(Message message, TimeSpan timeout) |
| | | 64 | | { |
| | 94 | 65 | | if (message == null) |
| | | 66 | | { |
| | 0 | 67 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 68 | | } |
| | 94 | 69 | | CommunicationObject.ThrowIfClosedOrNotOpen(); |
| | | 70 | | try |
| | | 71 | | { |
| | 94 | 72 | | Message verifiedMessage = await VerifyIncomingMessageCoreAsync(message, timeout); |
| | 63 | 73 | | return verifiedMessage; |
| | | 74 | | |
| | | 75 | | } |
| | 2 | 76 | | catch (MessageSecurityException e) |
| | | 77 | | { |
| | 2 | 78 | | base.OnVerifyIncomingMessageFailure(message, e); |
| | 2 | 79 | | throw; |
| | | 80 | | } |
| | 29 | 81 | | catch (Exception e) |
| | | 82 | | { |
| | | 83 | | // Always immediately rethrow fatal exceptions. |
| | 29 | 84 | | if (Fx.IsFatal(e)) |
| | | 85 | | { |
| | 0 | 86 | | throw; |
| | | 87 | | } |
| | | 88 | | |
| | 29 | 89 | | base.OnVerifyIncomingMessageFailure(message, e); |
| | 29 | 90 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.MessageSecurit |
| | | 91 | | } |
| | 63 | 92 | | } |
| | | 93 | | |
| | | 94 | | protected void AttachRecipientSecurityProperty(Message message, IList<SecurityToken> basicTokens, IList<Security |
| | | 95 | | IList<SecurityToken> signedEndorsingTokens, IList<SecurityToken> signedTokens, Dictionary<SecurityToken, Read |
| | | 96 | | { |
| | 63 | 97 | | SecurityMessageProperty security = SecurityMessageProperty.GetOrCreate(message); |
| | 63 | 98 | | AddSupportingTokenSpecification(security, basicTokens, endorsingTokens, signedEndorsingTokens, signedTokens, |
| | 63 | 99 | | security.ServiceSecurityContext = new ServiceSecurityContext(security.GetInitiatorTokenAuthorizationPolicies |
| | 63 | 100 | | } |
| | | 101 | | |
| | | 102 | | protected virtual async ValueTask<Message> VerifyIncomingMessageCoreAsync(Message message, TimeSpan timeout) |
| | | 103 | | { |
| | 74 | 104 | | TransportSecurityProtocolFactory factory = (TransportSecurityProtocolFactory)SecurityProtocolFactory; |
| | 74 | 105 | | string actor = string.Empty; // message.Version.Envelope.UltimateDestinationActor; |
| | | 106 | | |
| | 74 | 107 | | ReceiveSecurityHeader securityHeader = factory.StandardsManager.TryCreateReceiveSecurityHeader(message, acto |
| | 74 | 108 | | factory.IncomingAlgorithmSuite, (factory.ActAsInitiator) ? MessageDirection.Output : MessageDirection.In |
| | 74 | 109 | | IList<SupportingTokenAuthenticatorSpecification> supportingAuthenticators = factory.GetSupportingTokenAuthen |
| | 74 | 110 | | out bool expectSignedTokens, out bool expectBasicTokens, out bool expectEndorsingTokens); |
| | 74 | 111 | | if (securityHeader == null) |
| | | 112 | | { |
| | 0 | 113 | | bool expectSupportingTokens = expectEndorsingTokens || expectSignedTokens || expectBasicTokens; |
| | 0 | 114 | | if ((factory.ActAsInitiator && (!factory.AddTimestamp || factory.SecurityBindingElement.EnableUnsecuredR |
| | 0 | 115 | | || (!factory.ActAsInitiator && !factory.AddTimestamp && !expectSupportingTokens)) |
| | | 116 | | { |
| | 0 | 117 | | return message; |
| | | 118 | | } |
| | | 119 | | else |
| | | 120 | | { |
| | 0 | 121 | | if (string.IsNullOrEmpty(actor)) |
| | | 122 | | { |
| | 0 | 123 | | throw Diagnostics.TraceUtility.ThrowHelperError(new MessageSecurityException( |
| | 0 | 124 | | SR.Format(SR.UnableToFindSecurityHeaderInMessageNoActor)), message); |
| | | 125 | | } |
| | | 126 | | else |
| | | 127 | | { |
| | 0 | 128 | | throw Diagnostics.TraceUtility.ThrowHelperError(new MessageSecurityException( |
| | 0 | 129 | | SR.Format(SR.UnableToFindSecurityHeaderInMessage, actor)), message); |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | 74 | 134 | | securityHeader.RequireMessageProtection = false; |
| | 74 | 135 | | securityHeader.ExpectBasicTokens = expectBasicTokens; |
| | 74 | 136 | | securityHeader.ExpectSignedTokens = expectSignedTokens; |
| | 74 | 137 | | securityHeader.ExpectEndorsingTokens = expectEndorsingTokens; |
| | 74 | 138 | | securityHeader.MaxReceivedMessageSize = factory.SecurityBindingElement.MaxReceivedMessageSize; |
| | 74 | 139 | | securityHeader.ReaderQuotas = factory.SecurityBindingElement.ReaderQuotas; |
| | | 140 | | |
| | | 141 | | // This was behind an app setting on WCF. If this breaks someone, it's because they are setting SecurityHead |
| | | 142 | | // wasn't being applied. The customer fix is to not set the SecurityHeaderLayout as that's what they were ef |
| | 74 | 143 | | securityHeader.Layout = factory.SecurityHeaderLayout; |
| | | 144 | | |
| | 74 | 145 | | TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); |
| | 74 | 146 | | if (!factory.ActAsInitiator) |
| | | 147 | | { |
| | 74 | 148 | | securityHeader.ConfigureTransportBindingServerReceiveHeader(supportingAuthenticators); |
| | 74 | 149 | | securityHeader.ConfigureOutOfBandTokenResolver(MergeOutOfBandResolvers(supportingAuthenticators, EmptyRe |
| | 74 | 150 | | if (factory.ExpectKeyDerivation) |
| | | 151 | | { |
| | 0 | 152 | | securityHeader.DerivedTokenAuthenticator = factory.DerivedKeyTokenAuthenticator; |
| | | 153 | | } |
| | | 154 | | } |
| | 74 | 155 | | securityHeader.ReplayDetectionEnabled = factory.DetectReplays; |
| | 74 | 156 | | securityHeader.SetTimeParameters(factory.NonceCache, factory.ReplayWindow, factory.MaxClockSkew); |
| | 74 | 157 | | await securityHeader.ProcessAsync(timeoutHelper.RemainingTime(), SecurityUtils.GetChannelBindingFromMessage( |
| | 43 | 158 | | Message processedMessage = securityHeader.ProcessedMessage; |
| | 43 | 159 | | if (!factory.ActAsInitiator) |
| | | 160 | | { |
| | 43 | 161 | | AttachRecipientSecurityProperty(processedMessage, securityHeader.BasicSupportingTokens, securityHeader.E |
| | 43 | 162 | | securityHeader.SignedSupportingTokens, securityHeader.SecurityTokenAuthorizationPoliciesMapping); |
| | | 163 | | } |
| | | 164 | | |
| | 43 | 165 | | base.OnIncomingMessageVerified(processedMessage); |
| | | 166 | | |
| | 43 | 167 | | return processedMessage; |
| | 43 | 168 | | } |
| | | 169 | | } |
| | | 170 | | } |