| | | 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 System.Xml; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Description; |
| | | 10 | | using CoreWCF.Diagnostics; |
| | | 11 | | using CoreWCF.IdentityModel; |
| | | 12 | | using CoreWCF.IdentityModel.Tokens; |
| | | 13 | | using CoreWCF.Security.Tokens; |
| | | 14 | | using DictionaryManager = CoreWCF.IdentityModel.DictionaryManager; |
| | | 15 | | using IPrefixGenerator = CoreWCF.IdentityModel.IPrefixGenerator; |
| | | 16 | | using ISecurityElement = CoreWCF.IdentityModel.ISecurityElement; |
| | | 17 | | using ISignatureValueSecurityElement = CoreWCF.IdentityModel.ISignatureValueSecurityElement; |
| | | 18 | | |
| | | 19 | | namespace CoreWCF.Security |
| | | 20 | | { |
| | | 21 | | internal abstract class SendSecurityHeader : SecurityHeader, IMessageHeaderWithSharedNamespace |
| | | 22 | | { |
| | | 23 | | private bool _basicTokenEncrypted; |
| | | 24 | | private bool _primarySignatureDone; |
| | | 25 | | private bool _encryptSignature; |
| | | 26 | | private SignatureConfirmations _signatureValuesGenerated; |
| | | 27 | | private SignatureConfirmations _signatureConfirmationsToSend; |
| | | 28 | | private int _idCounter; |
| | | 29 | | private string _idPrefix; |
| | | 30 | | private MessagePartSpecification _signatureParts; |
| | | 31 | | private MessagePartSpecification _encryptionParts; |
| | | 32 | | private SecurityTokenParameters _encryptingTokenParameters; |
| | | 33 | | private List<SecurityToken> _basicTokens = null; |
| | | 34 | | private List<SecurityTokenParameters> _basicSupportingTokenParameters = null; |
| | | 35 | | private List<SecurityTokenParameters> _endorsingTokenParameters = null; |
| | | 36 | | private List<SecurityTokenParameters> _signedEndorsingTokenParameters = null; |
| | | 37 | | private List<SecurityTokenParameters> _signedTokenParameters = null; |
| | | 38 | | private SecurityToken _encryptingToken; |
| | | 39 | | private bool _skipKeyInfoForEncryption; |
| | | 40 | | private bool _shouldProtectTokens; |
| | | 41 | | private BufferManager _bufferManager; |
| | | 42 | | private SecurityProtocolCorrelationState _correlationState; |
| | 63 | 43 | | private bool _signThenEncrypt = true; |
| | 2 | 44 | | private static readonly string[] s_ids = new string[] { "_0", "_1", "_2", "_3", "_4", "_5", "_6", "_7", "_8", "_ |
| | | 45 | | |
| | | 46 | | protected SendSecurityHeader(Message message, string actor, bool mustUnderstand, bool relay, |
| | | 47 | | SecurityStandardsManager standardsManager, |
| | | 48 | | SecurityAlgorithmSuite algorithmSuite, |
| | | 49 | | MessageDirection transferDirection) |
| | 63 | 50 | | : base(message, actor, mustUnderstand, relay, standardsManager, algorithmSuite, transferDirection) |
| | | 51 | | { |
| | 63 | 52 | | ElementContainer = new SendSecurityHeaderElementContainer(); |
| | 63 | 53 | | } |
| | | 54 | | |
| | 2394 | 55 | | public SendSecurityHeaderElementContainer ElementContainer { get; } |
| | | 56 | | |
| | | 57 | | public SecurityProtocolCorrelationState CorrelationState |
| | | 58 | | { |
| | 0 | 59 | | get { return _correlationState; } |
| | | 60 | | set |
| | | 61 | | { |
| | 0 | 62 | | ThrowIfProcessingStarted(); |
| | 0 | 63 | | _correlationState = value; |
| | 0 | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public BufferManager StreamBufferManager |
| | | 68 | | { |
| | | 69 | | get |
| | | 70 | | { |
| | 0 | 71 | | if (_bufferManager == null) |
| | | 72 | | { |
| | 0 | 73 | | _bufferManager = BufferManager.CreateBufferManager(0, int.MaxValue); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | return _bufferManager; |
| | | 77 | | } |
| | | 78 | | set |
| | | 79 | | { |
| | 63 | 80 | | _bufferManager = value; |
| | 63 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public MessagePartSpecification EncryptionParts |
| | | 85 | | { |
| | 0 | 86 | | get { return _encryptionParts; } |
| | | 87 | | set |
| | | 88 | | { |
| | 0 | 89 | | ThrowIfProcessingStarted(); |
| | 0 | 90 | | if (value == null) |
| | | 91 | | { |
| | 0 | 92 | | throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(value)), Message); |
| | | 93 | | } |
| | 0 | 94 | | if (!value.IsReadOnly) |
| | | 95 | | { |
| | 0 | 96 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.MessagePartSpecificationMustBeI |
| | | 97 | | } |
| | 0 | 98 | | _encryptionParts = value; |
| | 0 | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | public bool EncryptPrimarySignature |
| | | 103 | | { |
| | 63 | 104 | | get { return _encryptSignature; } |
| | | 105 | | set |
| | | 106 | | { |
| | 0 | 107 | | ThrowIfProcessingStarted(); |
| | 0 | 108 | | _encryptSignature = value; |
| | 0 | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | internal byte[] PrimarySignatureValue { get; private set; } = null; |
| | | 113 | | |
| | 0 | 114 | | protected internal SecurityTokenParameters SigningTokenParameters { get; private set; } |
| | | 115 | | |
| | 185 | 116 | | protected bool ShouldSignToHeader { get; private set; } = false; |
| | | 117 | | |
| | | 118 | | public string IdPrefix |
| | | 119 | | { |
| | 0 | 120 | | get { return _idPrefix; } |
| | | 121 | | set |
| | | 122 | | { |
| | 0 | 123 | | ThrowIfProcessingStarted(); |
| | 0 | 124 | | _idPrefix = string.IsNullOrEmpty(value) || value == "_" ? null : value; |
| | 0 | 125 | | } |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | public override string Name => StandardsManager.SecurityVersion.HeaderName.Value; |
| | | 129 | | |
| | 68 | 130 | | public override string Namespace => StandardsManager.SecurityVersion.HeaderNamespace.Value; |
| | | 131 | | |
| | 63 | 132 | | protected SecurityAppliedMessage SecurityAppliedMessage => (SecurityAppliedMessage)Message; |
| | | 133 | | |
| | | 134 | | public bool SignThenEncrypt |
| | | 135 | | { |
| | 378 | 136 | | get { return _signThenEncrypt; } |
| | | 137 | | set |
| | | 138 | | { |
| | 0 | 139 | | ThrowIfProcessingStarted(); |
| | 0 | 140 | | _signThenEncrypt = value; |
| | 0 | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | public bool ShouldProtectTokens |
| | | 145 | | { |
| | 0 | 146 | | get { return _shouldProtectTokens; } |
| | | 147 | | set |
| | | 148 | | { |
| | 0 | 149 | | ThrowIfProcessingStarted(); |
| | 0 | 150 | | _shouldProtectTokens = value; |
| | 0 | 151 | | } |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | public MessagePartSpecification SignatureParts |
| | | 155 | | { |
| | 0 | 156 | | get { return _signatureParts; } |
| | | 157 | | set |
| | | 158 | | { |
| | 0 | 159 | | ThrowIfProcessingStarted(); |
| | 0 | 160 | | if (value == null) |
| | | 161 | | { |
| | 0 | 162 | | throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(value)), Message); |
| | | 163 | | } |
| | 0 | 164 | | if (!value.IsReadOnly) |
| | | 165 | | { |
| | 0 | 166 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 167 | | SR.MessagePartSpecificationMustBeImmutable), Message); |
| | | 168 | | } |
| | 0 | 169 | | _signatureParts = value; |
| | 0 | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | public SecurityTimestamp Timestamp => ElementContainer.Timestamp; |
| | | 174 | | |
| | 0 | 175 | | public bool HasSignedTokens { get; private set; } |
| | | 176 | | |
| | 0 | 177 | | public bool HasEncryptedTokens { get; private set; } |
| | | 178 | | |
| | | 179 | | public void AddPrerequisiteToken(SecurityToken token) |
| | | 180 | | { |
| | 0 | 181 | | ThrowIfProcessingStarted(); |
| | 0 | 182 | | ElementContainer.PrerequisiteToken = token ?? throw TraceUtility.ThrowHelperError(new Exception(nameof(token |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | private void AddParameters(ref List<SecurityTokenParameters> list, SecurityTokenParameters item) |
| | | 186 | | { |
| | 0 | 187 | | if (list == null) |
| | | 188 | | { |
| | 0 | 189 | | list = new List<SecurityTokenParameters>(); |
| | | 190 | | } |
| | 0 | 191 | | list.Add(item); |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | public abstract void ApplyBodySecurity(XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator); |
| | | 195 | | |
| | | 196 | | public abstract ValueTask ApplyBodySecurityAsync(XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator); |
| | | 197 | | |
| | | 198 | | public abstract void ApplySecurityAndWriteHeaders(MessageHeaders headers, XmlDictionaryWriter writer, IPrefixGen |
| | | 199 | | |
| | | 200 | | public abstract ValueTask ApplySecurityAndWriteHeadersAsync(MessageHeaders headers, XmlDictionaryWriter writer, |
| | | 201 | | |
| | 0 | 202 | | protected virtual bool HasSignedEncryptedMessagePart => false; |
| | | 203 | | |
| | | 204 | | public void SetSigningToken(SecurityToken token, SecurityTokenParameters tokenParameters) |
| | | 205 | | { |
| | 0 | 206 | | ThrowIfProcessingStarted(); |
| | 0 | 207 | | if ((token == null && tokenParameters != null) || (token != null && tokenParameters == null)) |
| | | 208 | | { |
| | 0 | 209 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.TokenMustBe |
| | | 210 | | } |
| | 0 | 211 | | ElementContainer.SourceSigningToken = token; |
| | 0 | 212 | | SigningTokenParameters = tokenParameters; |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | public void SetEncryptionToken(SecurityToken token, SecurityTokenParameters tokenParameters) |
| | | 216 | | { |
| | 0 | 217 | | ThrowIfProcessingStarted(); |
| | 0 | 218 | | if ((token == null && tokenParameters != null) || (token != null && tokenParameters == null)) |
| | | 219 | | { |
| | 0 | 220 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.TokenMustBe |
| | | 221 | | } |
| | 0 | 222 | | ElementContainer.SourceEncryptionToken = token; |
| | 0 | 223 | | _encryptingTokenParameters = tokenParameters; |
| | 0 | 224 | | } |
| | | 225 | | |
| | | 226 | | public void AddBasicSupportingToken(SecurityToken token, SecurityTokenParameters parameters) |
| | | 227 | | { |
| | 0 | 228 | | if (token == null) |
| | | 229 | | { |
| | 0 | 230 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | if (parameters == null) |
| | | 234 | | { |
| | 0 | 235 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | ThrowIfProcessingStarted(); |
| | 0 | 239 | | SendSecurityHeaderElement tokenElement = new SendSecurityHeaderElement(token.Id, new TokenElement(token, Sta |
| | 0 | 240 | | { |
| | 0 | 241 | | MarkedForEncryption = true |
| | 0 | 242 | | }; |
| | 0 | 243 | | ElementContainer.AddBasicSupportingToken(tokenElement); |
| | 0 | 244 | | HasEncryptedTokens = true; |
| | 0 | 245 | | HasSignedTokens = true; |
| | 0 | 246 | | AddParameters(ref _basicSupportingTokenParameters, parameters); |
| | 0 | 247 | | if (_basicTokens == null) |
| | | 248 | | { |
| | 0 | 249 | | _basicTokens = new List<SecurityToken>(); |
| | | 250 | | } |
| | | 251 | | // We maintain a list of the basic tokens for the SignThenEncrypt case as we will |
| | | 252 | | // need this token to write STR entry on OnWriteHeaderContents. |
| | 0 | 253 | | _basicTokens.Add(token); |
| | 0 | 254 | | } |
| | | 255 | | |
| | | 256 | | public void AddEndorsingSupportingToken(SecurityToken token, SecurityTokenParameters parameters) |
| | | 257 | | { |
| | 0 | 258 | | if (token == null) |
| | | 259 | | { |
| | 0 | 260 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 261 | | } |
| | | 262 | | |
| | 0 | 263 | | if (parameters == null) |
| | | 264 | | { |
| | 0 | 265 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | ThrowIfProcessingStarted(); |
| | 0 | 269 | | ElementContainer.AddEndorsingSupportingToken(token); |
| | | 270 | | // The ProviderBackedSecurityToken was added for the ChannelBindingToken (CBT) effort for win7. |
| | | 271 | | // We can assume the key is of type symmetric key. |
| | | 272 | | // |
| | | 273 | | // Asking for the key type from the token will cause the ProviderBackedSecurityToken |
| | | 274 | | // to attempt to resolve the token and the nego will start. |
| | | 275 | | // |
| | | 276 | | // We don't want that. |
| | | 277 | | // We want to defer the nego until after the CBT is available in SecurityAppliedMessage.OnWriteMessage. |
| | | 278 | | //TODO |
| | | 279 | | //if (!(token is ProviderBackedSecurityToken)) |
| | | 280 | | //{ |
| | | 281 | | // this.shouldSignToHeader |= (!this.RequireMessageProtection) && (SecurityUtils.GetSecurityKey<Asymmetri |
| | | 282 | | //} |
| | 0 | 283 | | AddParameters(ref _endorsingTokenParameters, parameters); |
| | 0 | 284 | | } |
| | | 285 | | |
| | | 286 | | public void AddSignedEndorsingSupportingToken(SecurityToken token, SecurityTokenParameters parameters) |
| | | 287 | | { |
| | 0 | 288 | | if (token == null) |
| | | 289 | | { |
| | 0 | 290 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 291 | | } |
| | | 292 | | |
| | 0 | 293 | | if (parameters == null) |
| | | 294 | | { |
| | 0 | 295 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 296 | | } |
| | | 297 | | |
| | 0 | 298 | | ThrowIfProcessingStarted(); |
| | 0 | 299 | | ElementContainer.AddSignedEndorsingSupportingToken(token); |
| | 0 | 300 | | HasSignedTokens = true; |
| | 0 | 301 | | ShouldSignToHeader |= (!RequireMessageProtection) && (SecurityUtils.GetSecurityKey<AsymmetricSecurityKey>(to |
| | 0 | 302 | | AddParameters(ref _signedEndorsingTokenParameters, parameters); |
| | 0 | 303 | | } |
| | | 304 | | |
| | | 305 | | public void AddSignedSupportingToken(SecurityToken token, SecurityTokenParameters parameters) |
| | | 306 | | { |
| | 0 | 307 | | if (token == null) |
| | | 308 | | { |
| | 0 | 309 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | if (parameters == null) |
| | | 313 | | { |
| | 0 | 314 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 315 | | } |
| | | 316 | | |
| | 0 | 317 | | ThrowIfProcessingStarted(); |
| | 0 | 318 | | ElementContainer.AddSignedSupportingToken(token); |
| | 0 | 319 | | HasSignedTokens = true; |
| | 0 | 320 | | AddParameters(ref _signedTokenParameters, parameters); |
| | 0 | 321 | | } |
| | | 322 | | |
| | | 323 | | public void AddSignatureConfirmations(SignatureConfirmations confirmations) |
| | | 324 | | { |
| | 0 | 325 | | ThrowIfProcessingStarted(); |
| | 0 | 326 | | _signatureConfirmationsToSend = confirmations; |
| | 0 | 327 | | } |
| | | 328 | | |
| | | 329 | | public void AddTimestamp(TimeSpan timestampValidityDuration) |
| | | 330 | | { |
| | 63 | 331 | | DateTime now = DateTime.UtcNow; |
| | 63 | 332 | | string id = RequireMessageProtection ? SecurityUtils.GenerateId() : GenerateId(); |
| | 63 | 333 | | AddTimestamp(new SecurityTimestamp(now, now + timestampValidityDuration, id)); |
| | 63 | 334 | | } |
| | | 335 | | |
| | | 336 | | public void AddTimestamp(SecurityTimestamp timestamp) |
| | | 337 | | { |
| | 63 | 338 | | ThrowIfProcessingStarted(); |
| | 63 | 339 | | if (ElementContainer.Timestamp != null) |
| | | 340 | | { |
| | 0 | 341 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.TimestampAlreadySetForSecurityHeade |
| | | 342 | | } |
| | | 343 | | |
| | 63 | 344 | | ElementContainer.Timestamp = timestamp ?? throw TraceUtility.ThrowHelperArgumentNull(nameof(timestamp), Mess |
| | 63 | 345 | | } |
| | | 346 | | |
| | | 347 | | protected virtual ISignatureValueSecurityElement[] CreateSignatureConfirmationElements(SignatureConfirmations si |
| | | 348 | | { |
| | 0 | 349 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 350 | | SR.Format(SR.SignatureConfirmationNotSupported))); |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | private void StartEncryption() |
| | | 354 | | { |
| | 63 | 355 | | if (ElementContainer.SourceEncryptionToken == null) |
| | | 356 | | { |
| | 63 | 357 | | return; |
| | | 358 | | } |
| | | 359 | | // determine the key identifier clause to use for the source |
| | 0 | 360 | | SecurityTokenReferenceStyle sourceEncryptingKeyReferenceStyle = GetTokenReferenceStyle(_encryptingTokenParam |
| | 0 | 361 | | bool encryptionTokenSerialized = sourceEncryptingKeyReferenceStyle == SecurityTokenReferenceStyle.Internal; |
| | 0 | 362 | | SecurityKeyIdentifierClause sourceEncryptingKeyIdentifierClause = _encryptingTokenParameters.CreateKeyIdenti |
| | 0 | 363 | | if (sourceEncryptingKeyIdentifierClause == null) |
| | | 364 | | { |
| | 0 | 365 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.TokenManagerCannotCreateTokenReferen |
| | | 366 | | } |
| | | 367 | | SecurityToken sourceToken; |
| | | 368 | | SecurityKeyIdentifierClause sourceTokenIdentifierClause; |
| | | 369 | | |
| | | 370 | | // if the source token cannot do symmetric crypto, create a wrapped key |
| | 0 | 371 | | if (!SecurityUtils.HasSymmetricSecurityKey(ElementContainer.SourceEncryptionToken)) |
| | | 372 | | { |
| | 0 | 373 | | int keyLength = Math.Max(128, AlgorithmSuite.DefaultSymmetricKeyLength); |
| | 0 | 374 | | CryptoHelper.ValidateSymmetricKeyLength(keyLength, AlgorithmSuite); |
| | 0 | 375 | | byte[] key = new byte[keyLength / 8]; |
| | 0 | 376 | | CryptoHelper.FillRandomBytes(key); |
| | 0 | 377 | | AlgorithmSuite.GetKeyWrapAlgorithm(ElementContainer.SourceEncryptionToken, out string keyWrapAlgorithm, |
| | 0 | 378 | | WrappedKeySecurityToken wrappedKey = new WrappedKeySecurityToken(GenerateId(), key, keyWrapAlgorithm, ke |
| | 0 | 379 | | ElementContainer.SourceEncryptionToken, new SecurityKeyIdentifier(sourceEncryptingKeyIdentifierClaus |
| | 0 | 380 | | ElementContainer.WrappedEncryptionToken = wrappedKey; |
| | 0 | 381 | | sourceToken = wrappedKey; |
| | 0 | 382 | | sourceTokenIdentifierClause = new LocalIdKeyIdentifierClause(wrappedKey.Id, wrappedKey.GetType()); |
| | 0 | 383 | | encryptionTokenSerialized = true; |
| | | 384 | | } |
| | | 385 | | else |
| | | 386 | | { |
| | 0 | 387 | | sourceToken = ElementContainer.SourceEncryptionToken; |
| | 0 | 388 | | sourceTokenIdentifierClause = sourceEncryptingKeyIdentifierClause; |
| | | 389 | | } |
| | | 390 | | |
| | | 391 | | // determine if a key needs to be derived |
| | | 392 | | SecurityKeyIdentifierClause encryptingKeyIdentifierClause; |
| | | 393 | | // determine if a token needs to be derived |
| | 0 | 394 | | if (_encryptingTokenParameters.RequireDerivedKeys) |
| | | 395 | | { |
| | 0 | 396 | | string derivationAlgorithm = AlgorithmSuite.GetEncryptionKeyDerivationAlgorithm(sourceToken, StandardsMa |
| | 0 | 397 | | string expectedDerivationAlgorithm = SecurityUtils.GetKeyDerivationAlgorithm(StandardsManager.MessageSec |
| | 0 | 398 | | if (derivationAlgorithm == expectedDerivationAlgorithm) |
| | | 399 | | { |
| | 0 | 400 | | DerivedKeySecurityToken derivedEncryptingToken = new DerivedKeySecurityToken(-1, 0, |
| | 0 | 401 | | AlgorithmSuite.GetEncryptionKeyDerivationLength(sourceToken, StandardsManager.MessageSecurityVer |
| | 0 | 402 | | _encryptingToken = ElementContainer.DerivedEncryptionToken = derivedEncryptingToken; |
| | 0 | 403 | | encryptingKeyIdentifierClause = new LocalIdKeyIdentifierClause(derivedEncryptingToken.Id, derivedEnc |
| | | 404 | | } |
| | | 405 | | else |
| | | 406 | | { |
| | 0 | 407 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Uns |
| | | 408 | | } |
| | | 409 | | } |
| | | 410 | | else |
| | | 411 | | { |
| | 0 | 412 | | _encryptingToken = sourceToken; |
| | 0 | 413 | | encryptingKeyIdentifierClause = sourceTokenIdentifierClause; |
| | | 414 | | } |
| | | 415 | | |
| | 0 | 416 | | _skipKeyInfoForEncryption = encryptionTokenSerialized && EncryptedKeyContainsReferenceList && (_encryptingTo |
| | | 417 | | SecurityKeyIdentifier identifier; |
| | 0 | 418 | | if (_skipKeyInfoForEncryption) |
| | | 419 | | { |
| | 0 | 420 | | identifier = null; |
| | | 421 | | } |
| | | 422 | | else |
| | | 423 | | { |
| | 0 | 424 | | identifier = new SecurityKeyIdentifier(encryptingKeyIdentifierClause); |
| | | 425 | | } |
| | | 426 | | |
| | 0 | 427 | | StartEncryptionCore(_encryptingToken, identifier); |
| | 0 | 428 | | } |
| | | 429 | | |
| | | 430 | | private void CompleteEncryption() |
| | | 431 | | { |
| | 63 | 432 | | ISecurityElement referenceList = CompleteEncryptionCore( |
| | 63 | 433 | | ElementContainer.PrimarySignature, |
| | 63 | 434 | | ElementContainer.GetBasicSupportingTokens(), |
| | 63 | 435 | | ElementContainer.GetSignatureConfirmations(), |
| | 63 | 436 | | ElementContainer.GetEndorsingSignatures()); |
| | | 437 | | |
| | 63 | 438 | | if (referenceList == null) |
| | | 439 | | { |
| | | 440 | | // null out all the encryption fields since there is no encryption needed |
| | 63 | 441 | | ElementContainer.SourceEncryptionToken = null; |
| | 63 | 442 | | ElementContainer.WrappedEncryptionToken = null; |
| | 63 | 443 | | ElementContainer.DerivedEncryptionToken = null; |
| | 63 | 444 | | return; |
| | | 445 | | } |
| | | 446 | | |
| | 0 | 447 | | if (_skipKeyInfoForEncryption) |
| | | 448 | | { |
| | 0 | 449 | | WrappedKeySecurityToken wrappedKeyToken = _encryptingToken as WrappedKeySecurityToken; |
| | 0 | 450 | | wrappedKeyToken.EnsureEncryptedKeySetUp(); |
| | 0 | 451 | | wrappedKeyToken.EncryptedKey.ReferenceList = (ReferenceList)referenceList; |
| | | 452 | | } |
| | | 453 | | else |
| | | 454 | | { |
| | 0 | 455 | | ElementContainer.ReferenceList = referenceList; |
| | | 456 | | } |
| | 0 | 457 | | _basicTokenEncrypted = true; |
| | 0 | 458 | | } |
| | | 459 | | |
| | | 460 | | internal void StartSecurityApplication() |
| | | 461 | | { |
| | 63 | 462 | | if (SignThenEncrypt) |
| | | 463 | | { |
| | 63 | 464 | | StartSignature(); |
| | 63 | 465 | | StartEncryption(); |
| | | 466 | | } |
| | | 467 | | else |
| | | 468 | | { |
| | 0 | 469 | | StartEncryption(); |
| | 0 | 470 | | StartSignature(); |
| | | 471 | | } |
| | 0 | 472 | | } |
| | | 473 | | |
| | | 474 | | internal void CompleteSecurityApplication() |
| | | 475 | | { |
| | 63 | 476 | | if (SignThenEncrypt) |
| | | 477 | | { |
| | 63 | 478 | | CompleteSignature(); |
| | 63 | 479 | | SignWithSupportingTokens(); |
| | 63 | 480 | | CompleteEncryption(); |
| | | 481 | | } |
| | | 482 | | else |
| | | 483 | | { |
| | 0 | 484 | | CompleteEncryption(); |
| | 0 | 485 | | CompleteSignature(); |
| | 0 | 486 | | SignWithSupportingTokens(); |
| | | 487 | | } |
| | | 488 | | |
| | 63 | 489 | | if (_correlationState != null) |
| | | 490 | | { |
| | 0 | 491 | | _correlationState.SignatureConfirmations = GetSignatureValues(); |
| | | 492 | | } |
| | 63 | 493 | | } |
| | | 494 | | |
| | | 495 | | public void RemoveSignatureEncryptionIfAppropriate() |
| | | 496 | | { |
| | 63 | 497 | | if (SignThenEncrypt && |
| | 63 | 498 | | EncryptPrimarySignature && |
| | 63 | 499 | | (SecurityAppliedMessage.BodyProtectionMode != MessagePartProtectionMode.SignThenEncrypt) && |
| | 63 | 500 | | (_basicSupportingTokenParameters == null || _basicSupportingTokenParameters.Count == 0) && |
| | 63 | 501 | | (_signatureConfirmationsToSend == null || _signatureConfirmationsToSend.Count == 0 || !_signatureConfirm |
| | 63 | 502 | | !HasSignedEncryptedMessagePart) |
| | | 503 | | { |
| | 0 | 504 | | _encryptSignature = false; |
| | | 505 | | } |
| | 63 | 506 | | } |
| | | 507 | | |
| | | 508 | | public string GenerateId() |
| | | 509 | | { |
| | 63 | 510 | | int id = _idCounter++; |
| | | 511 | | |
| | 63 | 512 | | if (_idPrefix != null) |
| | | 513 | | { |
| | 0 | 514 | | return _idPrefix + id; |
| | | 515 | | } |
| | | 516 | | |
| | 63 | 517 | | if (id < s_ids.Length) |
| | | 518 | | { |
| | 63 | 519 | | return s_ids[id]; |
| | | 520 | | } |
| | | 521 | | else |
| | | 522 | | { |
| | 0 | 523 | | return "_" + id; |
| | | 524 | | } |
| | | 525 | | } |
| | | 526 | | |
| | | 527 | | private SignatureConfirmations GetSignatureValues() |
| | | 528 | | { |
| | 0 | 529 | | return _signatureValuesGenerated; |
| | | 530 | | } |
| | | 531 | | |
| | | 532 | | protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion) |
| | | 533 | | { |
| | 63 | 534 | | StandardsManager.SecurityVersion.WriteStartHeader(writer); |
| | 63 | 535 | | WriteHeaderAttributes(writer, messageVersion); |
| | 63 | 536 | | } |
| | | 537 | | |
| | | 538 | | internal static bool ShouldSerializeToken(SecurityTokenParameters parameters, MessageDirection transferDirection |
| | | 539 | | { |
| | 0 | 540 | | switch (parameters.InclusionMode) |
| | | 541 | | { |
| | | 542 | | case SecurityTokenInclusionMode.AlwaysToInitiator: |
| | 0 | 543 | | return (transferDirection == MessageDirection.Output); |
| | | 544 | | case SecurityTokenInclusionMode.Once: |
| | | 545 | | case SecurityTokenInclusionMode.AlwaysToRecipient: |
| | 0 | 546 | | return (transferDirection == MessageDirection.Input); |
| | | 547 | | case SecurityTokenInclusionMode.Never: |
| | 0 | 548 | | return false; |
| | | 549 | | default: |
| | 0 | 550 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Uns |
| | | 551 | | } |
| | | 552 | | } |
| | | 553 | | |
| | | 554 | | protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) |
| | | 555 | | { |
| | 63 | 556 | | if (_basicSupportingTokenParameters != null && _basicSupportingTokenParameters.Count > 0 |
| | 63 | 557 | | && RequireMessageProtection && !_basicTokenEncrypted) |
| | | 558 | | { |
| | 0 | 559 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.BasicTokenCannotBeWrittenWithoutEnc |
| | | 560 | | } |
| | | 561 | | |
| | 63 | 562 | | if (ElementContainer.Timestamp != null && Layout != SecurityHeaderLayout.LaxTimestampLast) |
| | | 563 | | { |
| | 63 | 564 | | StandardsManager.WSUtilitySpecificationVersion.WriteTimestamp(writer, ElementContainer.Timestamp); |
| | | 565 | | } |
| | 63 | 566 | | if (ElementContainer.PrerequisiteToken != null) |
| | | 567 | | { |
| | 0 | 568 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, ElementContainer.PrerequisiteToken); |
| | | 569 | | } |
| | 63 | 570 | | if (ElementContainer.SourceSigningToken != null) |
| | | 571 | | { |
| | 0 | 572 | | if (ShouldSerializeToken(SigningTokenParameters, MessageDirection)) |
| | | 573 | | { |
| | 0 | 574 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, ElementContainer.SourceSigningToken); |
| | | 575 | | |
| | | 576 | | // Implement Protect token |
| | | 577 | | // NOTE: The spec says sign the primary token if it is not included in the message. But we currently |
| | | 578 | | // as we do not support STR-Transform for external references. Hence we can not sign the token which |
| | | 579 | | // This only affects the messages from service to client where |
| | | 580 | | // 1. allowSerializedSigningTokenOnReply is false. |
| | | 581 | | // 2. SymmetricSecurityBindingElement with IssuedTokens binding where the issued token has a symmetr |
| | | 582 | | |
| | 0 | 583 | | if (ShouldProtectTokens) |
| | | 584 | | { |
| | 0 | 585 | | WriteSecurityTokenReferencyEntry(writer, ElementContainer.SourceSigningToken, SigningTokenParame |
| | | 586 | | } |
| | | 587 | | } |
| | | 588 | | } |
| | 63 | 589 | | if (ElementContainer.DerivedSigningToken != null) |
| | | 590 | | { |
| | 0 | 591 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, ElementContainer.DerivedSigningToken); |
| | | 592 | | } |
| | 63 | 593 | | if (ElementContainer.SourceEncryptionToken != null && ElementContainer.SourceEncryptionToken != ElementConta |
| | | 594 | | { |
| | 0 | 595 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, ElementContainer.SourceEncryptionToken); |
| | | 596 | | } |
| | 63 | 597 | | if (ElementContainer.WrappedEncryptionToken != null) |
| | | 598 | | { |
| | 0 | 599 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, ElementContainer.WrappedEncryptionToken); |
| | | 600 | | } |
| | 63 | 601 | | if (ElementContainer.DerivedEncryptionToken != null) |
| | | 602 | | { |
| | 0 | 603 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, ElementContainer.DerivedEncryptionToken); |
| | | 604 | | } |
| | 63 | 605 | | if (SignThenEncrypt) |
| | | 606 | | { |
| | 63 | 607 | | if (ElementContainer.ReferenceList != null) |
| | | 608 | | { |
| | 0 | 609 | | ElementContainer.ReferenceList.WriteTo(writer, ServiceModelDictionaryManager.Instance); |
| | | 610 | | } |
| | | 611 | | } |
| | | 612 | | |
| | 63 | 613 | | SecurityToken[] signedTokens = ElementContainer.GetSignedSupportingTokens(); |
| | 63 | 614 | | if (signedTokens != null) |
| | | 615 | | { |
| | 0 | 616 | | for (int i = 0; i < signedTokens.Length; ++i) |
| | | 617 | | { |
| | 0 | 618 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, signedTokens[i]); |
| | 0 | 619 | | WriteSecurityTokenReferencyEntry(writer, signedTokens[i], _signedTokenParameters[i]); |
| | | 620 | | } |
| | | 621 | | } |
| | 63 | 622 | | SendSecurityHeaderElement[] basicTokensXml = ElementContainer.GetBasicSupportingTokens(); |
| | 63 | 623 | | if (basicTokensXml != null) |
| | | 624 | | { |
| | 0 | 625 | | for (int i = 0; i < basicTokensXml.Length; ++i) |
| | | 626 | | { |
| | 0 | 627 | | basicTokensXml[i].Item.WriteTo(writer, ServiceModelDictionaryManager.Instance); |
| | 0 | 628 | | if (SignThenEncrypt) |
| | | 629 | | { |
| | 0 | 630 | | WriteSecurityTokenReferencyEntry(writer, _basicTokens[i], _basicSupportingTokenParameters[i]); |
| | | 631 | | } |
| | | 632 | | } |
| | | 633 | | } |
| | 63 | 634 | | SecurityToken[] endorsingTokens = ElementContainer.GetEndorsingSupportingTokens(); |
| | 63 | 635 | | if (endorsingTokens != null) |
| | | 636 | | { |
| | 0 | 637 | | for (int i = 0; i < endorsingTokens.Length; ++i) |
| | | 638 | | { |
| | 0 | 639 | | if (ShouldSerializeToken(_endorsingTokenParameters[i], MessageDirection)) |
| | | 640 | | { |
| | 0 | 641 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, endorsingTokens[i]); |
| | | 642 | | } |
| | | 643 | | } |
| | | 644 | | } |
| | 63 | 645 | | SecurityToken[] endorsingDerivedTokens = ElementContainer.GetEndorsingDerivedSupportingTokens(); |
| | 63 | 646 | | if (endorsingDerivedTokens != null) |
| | | 647 | | { |
| | 0 | 648 | | for (int i = 0; i < endorsingDerivedTokens.Length; ++i) |
| | | 649 | | { |
| | 0 | 650 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, endorsingDerivedTokens[i]); |
| | | 651 | | } |
| | | 652 | | } |
| | 63 | 653 | | SecurityToken[] signedEndorsingTokens = ElementContainer.GetSignedEndorsingSupportingTokens(); |
| | 63 | 654 | | if (signedEndorsingTokens != null) |
| | | 655 | | { |
| | 0 | 656 | | for (int i = 0; i < signedEndorsingTokens.Length; ++i) |
| | | 657 | | { |
| | 0 | 658 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, signedEndorsingTokens[i]); |
| | 0 | 659 | | WriteSecurityTokenReferencyEntry(writer, signedEndorsingTokens[i], _signedEndorsingTokenParameters[i |
| | | 660 | | } |
| | | 661 | | } |
| | 63 | 662 | | SecurityToken[] signedEndorsingDerivedTokens = ElementContainer.GetSignedEndorsingDerivedSupportingTokens(); |
| | 63 | 663 | | if (signedEndorsingDerivedTokens != null) |
| | | 664 | | { |
| | 0 | 665 | | for (int i = 0; i < signedEndorsingDerivedTokens.Length; ++i) |
| | | 666 | | { |
| | 0 | 667 | | StandardsManager.SecurityTokenSerializer.WriteToken(writer, signedEndorsingDerivedTokens[i]); |
| | | 668 | | } |
| | | 669 | | } |
| | 63 | 670 | | SendSecurityHeaderElement[] signatureConfirmations = ElementContainer.GetSignatureConfirmations(); |
| | 63 | 671 | | if (signatureConfirmations != null) |
| | | 672 | | { |
| | 0 | 673 | | for (int i = 0; i < signatureConfirmations.Length; ++i) |
| | | 674 | | { |
| | 0 | 675 | | signatureConfirmations[i].Item.WriteTo(writer, ServiceModelDictionaryManager.Instance); |
| | | 676 | | } |
| | | 677 | | } |
| | 63 | 678 | | if (ElementContainer.PrimarySignature != null && ElementContainer.PrimarySignature.Item != null) |
| | | 679 | | { |
| | 0 | 680 | | ElementContainer.PrimarySignature.Item.WriteTo(writer, ServiceModelDictionaryManager.Instance); |
| | | 681 | | } |
| | 63 | 682 | | SendSecurityHeaderElement[] endorsingSignatures = ElementContainer.GetEndorsingSignatures(); |
| | 63 | 683 | | if (endorsingSignatures != null) |
| | | 684 | | { |
| | 0 | 685 | | for (int i = 0; i < endorsingSignatures.Length; ++i) |
| | | 686 | | { |
| | 0 | 687 | | endorsingSignatures[i].Item.WriteTo(writer, ServiceModelDictionaryManager.Instance); |
| | | 688 | | } |
| | | 689 | | } |
| | 63 | 690 | | if (!SignThenEncrypt) |
| | | 691 | | { |
| | 0 | 692 | | if (ElementContainer.ReferenceList != null) |
| | | 693 | | { |
| | 0 | 694 | | ElementContainer.ReferenceList.WriteTo(writer, ServiceModelDictionaryManager.Instance); |
| | | 695 | | } |
| | | 696 | | } |
| | 63 | 697 | | if (ElementContainer.Timestamp != null && Layout == SecurityHeaderLayout.LaxTimestampLast) |
| | | 698 | | { |
| | 0 | 699 | | StandardsManager.WSUtilitySpecificationVersion.WriteTimestamp(writer, ElementContainer.Timestamp); |
| | | 700 | | } |
| | 63 | 701 | | } |
| | | 702 | | |
| | | 703 | | protected abstract void WriteSecurityTokenReferencyEntry(XmlDictionaryWriter writer, SecurityToken securityToken |
| | | 704 | | |
| | | 705 | | public Message SetupExecution() |
| | | 706 | | { |
| | 63 | 707 | | ThrowIfProcessingStarted(); |
| | 63 | 708 | | SetProcessingStarted(); |
| | | 709 | | |
| | 63 | 710 | | bool signBody = false; |
| | 63 | 711 | | if (ElementContainer.SourceSigningToken != null) |
| | | 712 | | { |
| | 0 | 713 | | if (_signatureParts == null) |
| | | 714 | | { |
| | 0 | 715 | | throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(SignatureParts)), Message); |
| | | 716 | | } |
| | 0 | 717 | | signBody = _signatureParts.IsBodyIncluded; |
| | | 718 | | } |
| | | 719 | | |
| | 63 | 720 | | bool encryptBody = false; |
| | 63 | 721 | | if (ElementContainer.SourceEncryptionToken != null) |
| | | 722 | | { |
| | 0 | 723 | | if (_encryptionParts == null) |
| | | 724 | | { |
| | 0 | 725 | | throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(EncryptionParts)), Message); |
| | | 726 | | } |
| | 0 | 727 | | encryptBody = _encryptionParts.IsBodyIncluded; |
| | | 728 | | } |
| | | 729 | | |
| | 63 | 730 | | SecurityAppliedMessage message = new SecurityAppliedMessage(Message, this, signBody, encryptBody); |
| | 63 | 731 | | Message = message; |
| | 63 | 732 | | return message; |
| | | 733 | | } |
| | | 734 | | |
| | | 735 | | protected internal SecurityTokenReferenceStyle GetTokenReferenceStyle(SecurityTokenParameters parameters) |
| | | 736 | | { |
| | 0 | 737 | | return (ShouldSerializeToken(parameters, MessageDirection)) ? SecurityTokenReferenceStyle.Internal : Securit |
| | | 738 | | } |
| | | 739 | | |
| | | 740 | | private void StartSignature() |
| | | 741 | | { |
| | 63 | 742 | | if (ElementContainer.SourceSigningToken == null) |
| | | 743 | | { |
| | 63 | 744 | | return; |
| | | 745 | | } |
| | | 746 | | |
| | | 747 | | // determine the key identifier clause to use for the source |
| | 0 | 748 | | SecurityTokenReferenceStyle sourceSigningKeyReferenceStyle = GetTokenReferenceStyle(SigningTokenParameters); |
| | 0 | 749 | | SecurityKeyIdentifierClause sourceSigningKeyIdentifierClause = SigningTokenParameters.CreateKeyIdentifierCla |
| | 0 | 750 | | if (sourceSigningKeyIdentifierClause == null) |
| | | 751 | | { |
| | 0 | 752 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.TokenManagerCannotCreateTokenReferen |
| | | 753 | | } |
| | | 754 | | |
| | | 755 | | SecurityToken signingToken; |
| | | 756 | | SecurityKeyIdentifierClause signingKeyIdentifierClause; |
| | | 757 | | |
| | | 758 | | // determine if a token needs to be derived |
| | 0 | 759 | | if (SigningTokenParameters.RequireDerivedKeys && !SigningTokenParameters.HasAsymmetricKey) |
| | | 760 | | { |
| | 0 | 761 | | string derivationAlgorithm = AlgorithmSuite.GetSignatureKeyDerivationAlgorithm(ElementContainer.SourceSi |
| | 0 | 762 | | string expectedDerivationAlgorithm = SecurityUtils.GetKeyDerivationAlgorithm(StandardsManager.MessageSec |
| | 0 | 763 | | if (derivationAlgorithm == expectedDerivationAlgorithm) |
| | | 764 | | { |
| | 0 | 765 | | DerivedKeySecurityToken derivedSigningToken = new DerivedKeySecurityToken(-1, 0, AlgorithmSuite.GetS |
| | 0 | 766 | | sourceSigningKeyIdentifierClause, derivationAlgorithm, GenerateId()); |
| | 0 | 767 | | signingToken = ElementContainer.DerivedSigningToken = derivedSigningToken; |
| | 0 | 768 | | signingKeyIdentifierClause = new LocalIdKeyIdentifierClause(signingToken.Id, signingToken.GetType()) |
| | | 769 | | } |
| | | 770 | | else |
| | | 771 | | { |
| | 0 | 772 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Uns |
| | | 773 | | } |
| | | 774 | | } |
| | | 775 | | else |
| | | 776 | | { |
| | 0 | 777 | | signingToken = ElementContainer.SourceSigningToken; |
| | 0 | 778 | | signingKeyIdentifierClause = sourceSigningKeyIdentifierClause; |
| | | 779 | | } |
| | | 780 | | |
| | 0 | 781 | | SecurityKeyIdentifier signingKeyIdentifier = new SecurityKeyIdentifier(signingKeyIdentifierClause); |
| | | 782 | | |
| | 0 | 783 | | if (_signatureConfirmationsToSend != null && _signatureConfirmationsToSend.Count > 0) |
| | | 784 | | { |
| | | 785 | | ISecurityElement[] signatureConfirmationElements; |
| | 0 | 786 | | signatureConfirmationElements = CreateSignatureConfirmationElements(_signatureConfirmationsToSend); |
| | 0 | 787 | | for (int i = 0; i < signatureConfirmationElements.Length; ++i) |
| | | 788 | | { |
| | 0 | 789 | | SendSecurityHeaderElement sigConfElement = new SendSecurityHeaderElement(signatureConfirmationElemen |
| | 0 | 790 | | { |
| | 0 | 791 | | MarkedForEncryption = _signatureConfirmationsToSend.IsMarkedForEncryption |
| | 0 | 792 | | }; |
| | 0 | 793 | | ElementContainer.AddSignatureConfirmation(sigConfElement); |
| | | 794 | | } |
| | | 795 | | } |
| | | 796 | | |
| | 0 | 797 | | bool generateTargettablePrimarySignature = ((_endorsingTokenParameters != null) || (_signedEndorsingTokenPar |
| | 0 | 798 | | StartPrimarySignatureCore(signingToken, signingKeyIdentifier, _signatureParts, generateTargettablePrimarySig |
| | 0 | 799 | | } |
| | | 800 | | |
| | | 801 | | private void CompleteSignature() |
| | | 802 | | { |
| | 63 | 803 | | ISignatureValueSecurityElement signedXml = CompletePrimarySignatureCore( |
| | 63 | 804 | | ElementContainer.GetSignatureConfirmations(), ElementContainer.GetSignedEndorsingSupportingTokens(), |
| | 63 | 805 | | ElementContainer.GetSignedSupportingTokens(), ElementContainer.GetBasicSupportingTokens(), true); |
| | 63 | 806 | | if (signedXml == null) |
| | | 807 | | { |
| | 63 | 808 | | return; |
| | | 809 | | } |
| | 0 | 810 | | ElementContainer.PrimarySignature = new SendSecurityHeaderElement(signedXml.Id, signedXml) |
| | 0 | 811 | | { |
| | 0 | 812 | | MarkedForEncryption = _encryptSignature |
| | 0 | 813 | | }; |
| | 0 | 814 | | AddGeneratedSignatureValue(signedXml.GetSignatureValue(), EncryptPrimarySignature); |
| | 0 | 815 | | _primarySignatureDone = true; |
| | 0 | 816 | | PrimarySignatureValue = signedXml.GetSignatureValue(); |
| | 0 | 817 | | } |
| | | 818 | | |
| | | 819 | | protected abstract void StartPrimarySignatureCore(SecurityToken token, SecurityKeyIdentifier identifier, Message |
| | | 820 | | |
| | | 821 | | protected abstract ISignatureValueSecurityElement CompletePrimarySignatureCore(SendSecurityHeaderElement[] signa |
| | | 822 | | SecurityToken[] signedEndorsingTokens, SecurityToken[] signedTokens, SendSecurityHeaderElement[] basicTokens, |
| | | 823 | | |
| | | 824 | | protected abstract ISignatureValueSecurityElement CreateSupportingSignature(SecurityToken token, SecurityKeyIden |
| | | 825 | | |
| | | 826 | | protected abstract ISignatureValueSecurityElement CreateSupportingSignature(SecurityToken token, SecurityKeyIden |
| | | 827 | | |
| | | 828 | | protected abstract void StartEncryptionCore(SecurityToken token, SecurityKeyIdentifier keyIdentifier); |
| | | 829 | | |
| | | 830 | | protected abstract ISecurityElement CompleteEncryptionCore(SendSecurityHeaderElement primarySignature, |
| | | 831 | | SendSecurityHeaderElement[] basicTokens, SendSecurityHeaderElement[] signatureConfirmations, SendSecurityHea |
| | | 832 | | |
| | | 833 | | private void SignWithSupportingToken(SecurityToken token, SecurityKeyIdentifierClause identifierClause) |
| | | 834 | | { |
| | 0 | 835 | | if (token == null) |
| | | 836 | | { |
| | 0 | 837 | | throw TraceUtility.ThrowHelperArgumentNull(nameof(token), Message); |
| | | 838 | | } |
| | 0 | 839 | | if (identifierClause == null) |
| | | 840 | | { |
| | 0 | 841 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.TokenManagerCannotCreateTokenReferen |
| | | 842 | | } |
| | 0 | 843 | | if (!RequireMessageProtection) |
| | | 844 | | { |
| | 0 | 845 | | if (ElementContainer.Timestamp == null) |
| | | 846 | | { |
| | 0 | 847 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 848 | | SR.SigningWithoutPrimarySignatureRequiresTimestamp), Message); |
| | | 849 | | } |
| | | 850 | | } |
| | | 851 | | else |
| | | 852 | | { |
| | 0 | 853 | | if (!_primarySignatureDone) |
| | | 854 | | { |
| | 0 | 855 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 856 | | SR.PrimarySignatureMustBeComputedBeforeSupportingTokenSignatures), Message); |
| | | 857 | | } |
| | 0 | 858 | | if (ElementContainer.PrimarySignature.Item == null) |
| | | 859 | | { |
| | 0 | 860 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 861 | | SR.Format(SR.SupportingTokenSignaturesNotExpected)), Message); |
| | | 862 | | } |
| | | 863 | | } |
| | | 864 | | |
| | 0 | 865 | | SecurityKeyIdentifier identifier = new SecurityKeyIdentifier(identifierClause); |
| | | 866 | | ISignatureValueSecurityElement supportingSignature; |
| | 0 | 867 | | if (!RequireMessageProtection) |
| | | 868 | | { |
| | 0 | 869 | | supportingSignature = CreateSupportingSignature(token, identifier); |
| | | 870 | | } |
| | | 871 | | else |
| | | 872 | | { |
| | 0 | 873 | | supportingSignature = CreateSupportingSignature(token, identifier, ElementContainer.PrimarySignature.Ite |
| | | 874 | | } |
| | 0 | 875 | | AddGeneratedSignatureValue(supportingSignature.GetSignatureValue(), _encryptSignature); |
| | 0 | 876 | | SendSecurityHeaderElement supportingSignatureElement = new SendSecurityHeaderElement(supportingSignature.Id, |
| | 0 | 877 | | { |
| | 0 | 878 | | MarkedForEncryption = _encryptSignature |
| | 0 | 879 | | }; |
| | 0 | 880 | | ElementContainer.AddEndorsingSignature(supportingSignatureElement); |
| | 0 | 881 | | } |
| | | 882 | | |
| | | 883 | | private void SignWithSupportingTokens() |
| | | 884 | | { |
| | 63 | 885 | | SecurityToken[] endorsingTokens = ElementContainer.GetEndorsingSupportingTokens(); |
| | 63 | 886 | | if (endorsingTokens != null) |
| | | 887 | | { |
| | 0 | 888 | | for (int i = 0; i < endorsingTokens.Length; ++i) |
| | | 889 | | { |
| | 0 | 890 | | SecurityToken source = endorsingTokens[i]; |
| | 0 | 891 | | SecurityKeyIdentifierClause sourceKeyClause = _endorsingTokenParameters[i].CreateKeyIdentifierClause |
| | 0 | 892 | | if (sourceKeyClause == null) |
| | | 893 | | { |
| | 0 | 894 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.TokenManagerCannot |
| | | 895 | | } |
| | | 896 | | SecurityToken signingToken; |
| | | 897 | | SecurityKeyIdentifierClause signingKeyClause; |
| | 0 | 898 | | if (_endorsingTokenParameters[i].RequireDerivedKeys && !_endorsingTokenParameters[i].HasAsymmetricKe |
| | | 899 | | { |
| | 0 | 900 | | string derivationAlgorithm = SecurityUtils.GetKeyDerivationAlgorithm(StandardsManager.MessageSec |
| | 0 | 901 | | DerivedKeySecurityToken dkt = new DerivedKeySecurityToken(-1, 0, |
| | 0 | 902 | | AlgorithmSuite.GetSignatureKeyDerivationLength(source, StandardsManager.MessageSecurityVersi |
| | 0 | 903 | | DerivedKeySecurityToken.DefaultNonceLength, source, sourceKeyClause, derivationAlgorithm, Ge |
| | 0 | 904 | | signingToken = dkt; |
| | 0 | 905 | | signingKeyClause = new LocalIdKeyIdentifierClause(dkt.Id, dkt.GetType()); |
| | 0 | 906 | | ElementContainer.AddEndorsingDerivedSupportingToken(dkt); |
| | | 907 | | } |
| | | 908 | | else |
| | | 909 | | { |
| | 0 | 910 | | signingToken = source; |
| | 0 | 911 | | signingKeyClause = sourceKeyClause; |
| | | 912 | | } |
| | 0 | 913 | | SignWithSupportingToken(signingToken, signingKeyClause); |
| | | 914 | | } |
| | | 915 | | } |
| | 63 | 916 | | SecurityToken[] signedEndorsingSupportingTokens = ElementContainer.GetSignedEndorsingSupportingTokens(); |
| | 63 | 917 | | if (signedEndorsingSupportingTokens != null) |
| | | 918 | | { |
| | 0 | 919 | | for (int i = 0; i < signedEndorsingSupportingTokens.Length; ++i) |
| | | 920 | | { |
| | 0 | 921 | | SecurityToken source = signedEndorsingSupportingTokens[i]; |
| | 0 | 922 | | SecurityKeyIdentifierClause sourceKeyClause = _signedEndorsingTokenParameters[i].CreateKeyIdentifier |
| | 0 | 923 | | if (sourceKeyClause == null) |
| | | 924 | | { |
| | 0 | 925 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.TokenManagerCannot |
| | | 926 | | } |
| | | 927 | | SecurityToken signingToken; |
| | | 928 | | SecurityKeyIdentifierClause signingKeyClause; |
| | 0 | 929 | | if (_signedEndorsingTokenParameters[i].RequireDerivedKeys && !_signedEndorsingTokenParameters[i].Has |
| | | 930 | | { |
| | 0 | 931 | | string derivationAlgorithm = SecurityUtils.GetKeyDerivationAlgorithm(StandardsManager.MessageSec |
| | 0 | 932 | | DerivedKeySecurityToken dkt = new DerivedKeySecurityToken(-1, 0, |
| | 0 | 933 | | AlgorithmSuite.GetSignatureKeyDerivationLength(source, StandardsManager.MessageSecurityVersi |
| | 0 | 934 | | DerivedKeySecurityToken.DefaultNonceLength, source, sourceKeyClause, derivationAlgorithm, Ge |
| | 0 | 935 | | signingToken = dkt; |
| | 0 | 936 | | signingKeyClause = new LocalIdKeyIdentifierClause(dkt.Id, dkt.GetType()); |
| | 0 | 937 | | ElementContainer.AddSignedEndorsingDerivedSupportingToken(dkt); |
| | | 938 | | } |
| | | 939 | | else |
| | | 940 | | { |
| | 0 | 941 | | signingToken = source; |
| | 0 | 942 | | signingKeyClause = sourceKeyClause; |
| | | 943 | | } |
| | 0 | 944 | | SignWithSupportingToken(signingToken, signingKeyClause); |
| | | 945 | | } |
| | | 946 | | } |
| | 63 | 947 | | } |
| | | 948 | | |
| | | 949 | | protected bool ShouldUseStrTransformForToken(SecurityToken securityToken, int position, SecurityTokenAttachmentM |
| | | 950 | | { |
| | 0 | 951 | | keyIdentifierClause = null; |
| | | 952 | | |
| | | 953 | | IssuedSecurityTokenParameters tokenParams; |
| | | 954 | | switch (mode) |
| | | 955 | | { |
| | | 956 | | case SecurityTokenAttachmentMode.SignedEndorsing: |
| | 0 | 957 | | tokenParams = _signedEndorsingTokenParameters[position] as IssuedSecurityTokenParameters; |
| | 0 | 958 | | break; |
| | | 959 | | case SecurityTokenAttachmentMode.Signed: |
| | 0 | 960 | | tokenParams = _signedTokenParameters[position] as IssuedSecurityTokenParameters; |
| | 0 | 961 | | break; |
| | | 962 | | case SecurityTokenAttachmentMode.SignedEncrypted: |
| | 0 | 963 | | tokenParams = _basicSupportingTokenParameters[position] as IssuedSecurityTokenParameters; |
| | 0 | 964 | | break; |
| | | 965 | | default: |
| | 0 | 966 | | return false; |
| | | 967 | | } |
| | | 968 | | |
| | 0 | 969 | | if (tokenParams != null && tokenParams.UseStrTransform) |
| | | 970 | | { |
| | 0 | 971 | | keyIdentifierClause = tokenParams.CreateKeyIdentifierClause(securityToken, GetTokenReferenceStyle(tokenP |
| | 0 | 972 | | if (keyIdentifierClause == null) |
| | | 973 | | { |
| | 0 | 974 | | throw TraceUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.TokenManagerCannotCrea |
| | | 975 | | } |
| | | 976 | | |
| | 0 | 977 | | return true; |
| | | 978 | | } |
| | 0 | 979 | | return false; |
| | | 980 | | } |
| | | 981 | | |
| | 63 | 982 | | XmlDictionaryString IMessageHeaderWithSharedNamespace.SharedNamespace => XD.UtilityDictionary.Namespace; |
| | | 983 | | |
| | 63 | 984 | | XmlDictionaryString IMessageHeaderWithSharedNamespace.SharedPrefix => XD.UtilityDictionary.Prefix; |
| | | 985 | | |
| | | 986 | | private void AddGeneratedSignatureValue(byte[] signatureValue, bool wasEncrypted) |
| | | 987 | | { |
| | | 988 | | // cache outgoing signatures only on the client side |
| | 0 | 989 | | if (MaintainSignatureConfirmationState && (_signatureConfirmationsToSend == null)) |
| | | 990 | | { |
| | 0 | 991 | | if (_signatureValuesGenerated == null) |
| | | 992 | | { |
| | 0 | 993 | | _signatureValuesGenerated = new SignatureConfirmations(); |
| | | 994 | | } |
| | 0 | 995 | | _signatureValuesGenerated.AddConfirmation(signatureValue, wasEncrypted); |
| | | 996 | | } |
| | 0 | 997 | | } |
| | | 998 | | } |
| | | 999 | | |
| | | 1000 | | internal class TokenElement : ISecurityElement |
| | | 1001 | | { |
| | | 1002 | | private readonly SecurityStandardsManager _standardsManager; |
| | | 1003 | | |
| | | 1004 | | public TokenElement(SecurityToken token, SecurityStandardsManager standardsManager) |
| | | 1005 | | { |
| | | 1006 | | Token = token; |
| | | 1007 | | _standardsManager = standardsManager; |
| | | 1008 | | } |
| | | 1009 | | |
| | | 1010 | | public override bool Equals(object item) |
| | | 1011 | | { |
| | | 1012 | | return (item is TokenElement element && Token == element.Token && _standardsManager == element._standardsMan |
| | | 1013 | | } |
| | | 1014 | | |
| | | 1015 | | public override int GetHashCode() |
| | | 1016 | | { |
| | | 1017 | | return Token.GetHashCode() ^ _standardsManager.GetHashCode(); |
| | | 1018 | | } |
| | | 1019 | | |
| | | 1020 | | public bool HasId => true; |
| | | 1021 | | |
| | | 1022 | | public string Id => Token.Id; |
| | | 1023 | | |
| | | 1024 | | public SecurityToken Token { get; } |
| | | 1025 | | |
| | | 1026 | | public void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager) |
| | | 1027 | | { |
| | | 1028 | | _standardsManager.SecurityTokenSerializer.WriteToken(writer, Token); |
| | | 1029 | | } |
| | | 1030 | | } |
| | | 1031 | | } |