| | | 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.Threading.Tasks; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | using CoreWCF.IdentityModel.Selectors; |
| | | 9 | | using CoreWCF.Security.Tokens; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Security |
| | | 12 | | { |
| | | 13 | | internal abstract class MessageSecurityProtocolFactory : SecurityProtocolFactory |
| | | 14 | | { |
| | | 15 | | internal const MessageProtectionOrder defaultMessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt; |
| | | 16 | | internal const bool defaultDoRequestSignatureConfirmation = false; |
| | 0 | 17 | | private bool _applyIntegrity = true; |
| | 0 | 18 | | private bool _applyConfidentiality = true; |
| | | 19 | | private bool _doRequestSignatureConfirmation = defaultDoRequestSignatureConfirmation; |
| | | 20 | | private IdentityVerifier _identityVerifier; |
| | | 21 | | private MessageProtectionOrder _messageProtectionOrder = defaultMessageProtectionOrder; |
| | 0 | 22 | | private bool _requireIntegrity = true; |
| | 0 | 23 | | private bool _requireConfidentiality = true; |
| | | 24 | | |
| | 0 | 25 | | protected MessageSecurityProtocolFactory() |
| | | 26 | | { |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | internal MessageSecurityProtocolFactory(MessageSecurityProtocolFactory factory) |
| | 0 | 30 | | : base(factory) |
| | | 31 | | { |
| | 0 | 32 | | if (factory == null) |
| | | 33 | | { |
| | 0 | 34 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(factory)); |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | _applyIntegrity = factory._applyIntegrity; |
| | 0 | 38 | | _applyConfidentiality = factory._applyConfidentiality; |
| | 0 | 39 | | _identityVerifier = factory._identityVerifier; |
| | 0 | 40 | | ProtectionRequirements = new ChannelProtectionRequirements(factory.ProtectionRequirements); |
| | 0 | 41 | | _messageProtectionOrder = factory._messageProtectionOrder; |
| | 0 | 42 | | _requireIntegrity = factory._requireIntegrity; |
| | 0 | 43 | | _requireConfidentiality = factory._requireConfidentiality; |
| | 0 | 44 | | _doRequestSignatureConfirmation = factory._doRequestSignatureConfirmation; |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public bool ApplyConfidentiality |
| | | 48 | | { |
| | | 49 | | get |
| | | 50 | | { |
| | 0 | 51 | | return _applyConfidentiality; |
| | | 52 | | } |
| | | 53 | | set |
| | | 54 | | { |
| | 0 | 55 | | ThrowIfImmutable(); |
| | 0 | 56 | | _applyConfidentiality = value; |
| | 0 | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public bool ApplyIntegrity |
| | | 61 | | { |
| | | 62 | | get |
| | | 63 | | { |
| | 0 | 64 | | return _applyIntegrity; |
| | | 65 | | } |
| | | 66 | | set |
| | | 67 | | { |
| | 0 | 68 | | ThrowIfImmutable(); |
| | 0 | 69 | | _applyIntegrity = value; |
| | 0 | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public bool DoRequestSignatureConfirmation |
| | | 74 | | { |
| | | 75 | | get |
| | | 76 | | { |
| | 0 | 77 | | return _doRequestSignatureConfirmation; |
| | | 78 | | } |
| | | 79 | | set |
| | | 80 | | { |
| | 0 | 81 | | ThrowIfImmutable(); |
| | 0 | 82 | | _doRequestSignatureConfirmation = value; |
| | 0 | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public IdentityVerifier IdentityVerifier |
| | | 87 | | { |
| | | 88 | | get |
| | | 89 | | { |
| | 0 | 90 | | return _identityVerifier; |
| | | 91 | | } |
| | | 92 | | set |
| | | 93 | | { |
| | 0 | 94 | | ThrowIfImmutable(); |
| | 0 | 95 | | _identityVerifier = value; |
| | 0 | 96 | | } |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | public ChannelProtectionRequirements ProtectionRequirements { get; } = new ChannelProtectionRequirements(); |
| | | 100 | | |
| | | 101 | | public MessageProtectionOrder MessageProtectionOrder |
| | | 102 | | { |
| | | 103 | | get |
| | | 104 | | { |
| | 0 | 105 | | return _messageProtectionOrder; |
| | | 106 | | } |
| | | 107 | | set |
| | | 108 | | { |
| | 0 | 109 | | ThrowIfImmutable(); |
| | 0 | 110 | | _messageProtectionOrder = value; |
| | 0 | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | public bool RequireIntegrity |
| | | 115 | | { |
| | | 116 | | get |
| | | 117 | | { |
| | 0 | 118 | | return _requireIntegrity; |
| | | 119 | | } |
| | | 120 | | set |
| | | 121 | | { |
| | 0 | 122 | | ThrowIfImmutable(); |
| | 0 | 123 | | _requireIntegrity = value; |
| | 0 | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | public bool RequireConfidentiality |
| | | 128 | | { |
| | | 129 | | get |
| | | 130 | | { |
| | 0 | 131 | | return _requireConfidentiality; |
| | | 132 | | } |
| | | 133 | | set |
| | | 134 | | { |
| | 0 | 135 | | ThrowIfImmutable(); |
| | 0 | 136 | | _requireConfidentiality = value; |
| | 0 | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | internal List<SecurityTokenAuthenticator> WrappedKeySecurityTokenAuthenticator { get; private set; } |
| | | 141 | | |
| | | 142 | | protected virtual void ValidateCorrelationSecuritySettings() |
| | | 143 | | { |
| | 0 | 144 | | if (ActAsInitiator && SupportsRequestReply) |
| | | 145 | | { |
| | 0 | 146 | | bool savesCorrelationTokenOnRequest = ApplyIntegrity || ApplyConfidentiality; |
| | 0 | 147 | | bool needsCorrelationTokenOnReply = RequireIntegrity || RequireConfidentiality; |
| | 0 | 148 | | if (!savesCorrelationTokenOnRequest && needsCorrelationTokenOnReply) |
| | | 149 | | { |
| | 0 | 150 | | OnPropertySettingsError(nameof(ApplyIntegrity), false); |
| | | 151 | | } |
| | | 152 | | } |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | public override Task OnOpenAsync(TimeSpan timeout) |
| | | 156 | | { |
| | 0 | 157 | | base.OnOpenAsync(timeout); |
| | 0 | 158 | | ProtectionRequirements.MakeReadOnly(); |
| | | 159 | | |
| | 0 | 160 | | if (DetectReplays && !RequireIntegrity) |
| | | 161 | | { |
| | 0 | 162 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(RequireIntegrity), SR.ForReplayDetec |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | if (DoRequestSignatureConfirmation) |
| | | 166 | | { |
| | 0 | 167 | | if (!SupportsRequestReply) |
| | | 168 | | { |
| | 0 | 169 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SignatureConfirmationRequiresRequest |
| | | 170 | | } |
| | | 171 | | //TODO fix below |
| | | 172 | | //if (!this.StandardsManager.SecurityVersion.SupportsSignatureConfirmation) |
| | | 173 | | //{ |
| | | 174 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityVersionDoesNotSu |
| | | 175 | | //} |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | WrappedKeySecurityTokenAuthenticator = new List<SecurityTokenAuthenticator>(1); |
| | 0 | 179 | | SecurityTokenAuthenticator authenticator = new NonValidatingSecurityTokenAuthenticator<WrappedKeySecurityTok |
| | 0 | 180 | | WrappedKeySecurityTokenAuthenticator.Add(authenticator); |
| | | 181 | | |
| | 0 | 182 | | ValidateCorrelationSecuritySettings(); |
| | 0 | 183 | | return Task.CompletedTask; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | private static MessagePartSpecification ExtractMessageParts(string action, |
| | | 187 | | ScopedMessagePartSpecification scopedParts, bool isForSignature) |
| | | 188 | | { |
| | 0 | 189 | | if (scopedParts.TryGetParts(action, out MessagePartSpecification parts)) |
| | | 190 | | { |
| | 0 | 191 | | return parts; |
| | | 192 | | } |
| | 0 | 193 | | else if (scopedParts.TryGetParts(MessageHeaders.WildcardAction, out parts)) |
| | | 194 | | { |
| | 0 | 195 | | return parts; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | // send back a fault indication that the action is unknown |
| | 0 | 199 | | SecurityVersion wss = MessageSecurityVersion.Default.SecurityVersion; |
| | 0 | 200 | | FaultCode subCode = new FaultCode(wss.InvalidSecurityFaultCode.Value, wss.HeaderNamespace.Value); |
| | 0 | 201 | | FaultCode senderCode = FaultCode.CreateSenderFaultCode(subCode); |
| | 0 | 202 | | FaultReason reason = new FaultReason(SR.Format(SR.InvalidOrUnrecognizedAction, action), System.Globalization |
| | 0 | 203 | | MessageFault fault = MessageFault.CreateFault(senderCode, reason); |
| | 0 | 204 | | if (isForSignature) |
| | | 205 | | { |
| | 0 | 206 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.NoSi |
| | | 207 | | } |
| | | 208 | | else |
| | | 209 | | { |
| | 0 | 210 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.NoEn |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | internal MessagePartSpecification GetIncomingEncryptionParts(string action) |
| | | 215 | | { |
| | 0 | 216 | | if (RequireConfidentiality) |
| | | 217 | | { |
| | 0 | 218 | | if (IsDuplexReply) |
| | | 219 | | { |
| | 0 | 220 | | return ExtractMessageParts(action, ProtectionRequirements.OutgoingEncryptionParts, false); |
| | | 221 | | } |
| | | 222 | | else |
| | | 223 | | { |
| | 0 | 224 | | return ExtractMessageParts(action, (ActAsInitiator) ? ProtectionRequirements.OutgoingEncryptionParts |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | else |
| | | 228 | | { |
| | 0 | 229 | | return MessagePartSpecification.NoParts; |
| | | 230 | | } |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | internal MessagePartSpecification GetIncomingSignatureParts(string action) |
| | | 234 | | { |
| | 0 | 235 | | if (RequireIntegrity) |
| | | 236 | | { |
| | 0 | 237 | | if (IsDuplexReply) |
| | | 238 | | { |
| | 0 | 239 | | return ExtractMessageParts(action, ProtectionRequirements.OutgoingSignatureParts, true); |
| | | 240 | | } |
| | | 241 | | else |
| | | 242 | | { |
| | 0 | 243 | | return ExtractMessageParts(action, (ActAsInitiator) ? ProtectionRequirements.OutgoingSignatureParts |
| | | 244 | | } |
| | | 245 | | } |
| | | 246 | | else |
| | | 247 | | { |
| | 0 | 248 | | return MessagePartSpecification.NoParts; |
| | | 249 | | } |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | internal MessagePartSpecification GetOutgoingEncryptionParts(string action) |
| | | 253 | | { |
| | 0 | 254 | | if (ApplyConfidentiality) |
| | | 255 | | { |
| | 0 | 256 | | if (IsDuplexReply) |
| | | 257 | | { |
| | 0 | 258 | | return ExtractMessageParts(action, ProtectionRequirements.OutgoingEncryptionParts, false); |
| | | 259 | | } |
| | | 260 | | else |
| | | 261 | | { |
| | 0 | 262 | | return ExtractMessageParts(action, (ActAsInitiator) ? ProtectionRequirements.IncomingEncryptionParts |
| | | 263 | | } |
| | | 264 | | } |
| | | 265 | | else |
| | | 266 | | { |
| | 0 | 267 | | return MessagePartSpecification.NoParts; |
| | | 268 | | } |
| | | 269 | | } |
| | | 270 | | |
| | | 271 | | internal MessagePartSpecification GetOutgoingSignatureParts(string action) |
| | | 272 | | { |
| | 0 | 273 | | if (ApplyIntegrity) |
| | | 274 | | { |
| | 0 | 275 | | if (IsDuplexReply) |
| | | 276 | | { |
| | 0 | 277 | | return ExtractMessageParts(action, ProtectionRequirements.OutgoingSignatureParts, true); |
| | | 278 | | } |
| | | 279 | | else |
| | | 280 | | { |
| | 0 | 281 | | return ExtractMessageParts(action, (ActAsInitiator) ? ProtectionRequirements.IncomingSignatureParts |
| | | 282 | | } |
| | | 283 | | } |
| | | 284 | | else |
| | | 285 | | { |
| | 0 | 286 | | return MessagePartSpecification.NoParts; |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | } |
| | | 290 | | } |