| | | 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.Security.Cryptography.Xml; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using System.Xml; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Security |
| | | 9 | | { |
| | | 10 | | internal class LaxModeSecurityHeaderElementInferenceEngine : SecurityHeaderElementInferenceEngine |
| | | 11 | | { |
| | 2 | 12 | | protected LaxModeSecurityHeaderElementInferenceEngine() { } |
| | | 13 | | |
| | 7 | 14 | | internal static LaxModeSecurityHeaderElementInferenceEngine Instance { get; } = new LaxModeSecurityHeaderElement |
| | | 15 | | |
| | | 16 | | public override async ValueTask ExecuteProcessingPassesAsync(ReceiveSecurityHeader securityHeader, XmlDictionary |
| | | 17 | | { |
| | | 18 | | // pass 1 |
| | 6 | 19 | | await securityHeader.ExecuteReadingPassAsync(reader); |
| | | 20 | | |
| | | 21 | | // pass 1.5 |
| | 5 | 22 | | securityHeader.ExecuteDerivedKeyTokenStubPass(false); |
| | | 23 | | |
| | | 24 | | // pass 2 |
| | 5 | 25 | | await securityHeader.ExecuteSubheaderDecryptionPassAsync(); |
| | | 26 | | |
| | | 27 | | // pass 2.5 |
| | 5 | 28 | | securityHeader.ExecuteDerivedKeyTokenStubPass(true); |
| | | 29 | | |
| | | 30 | | // layout-specific inferences |
| | 5 | 31 | | MarkElements(securityHeader.ElementManager, securityHeader.RequireMessageProtection); |
| | | 32 | | |
| | | 33 | | // pass 3 |
| | 5 | 34 | | await securityHeader.ExecuteSignatureEncryptionProcessingPassAsync(); |
| | 5 | 35 | | } |
| | | 36 | | |
| | | 37 | | public override void MarkElements(ReceiveSecurityHeaderElementManager elementManager, bool messageSecurityMode) |
| | | 38 | | { |
| | 5 | 39 | | bool primarySignatureFound = false; |
| | 30 | 40 | | for (int position = 0; position < elementManager.Count; position++) |
| | | 41 | | { |
| | 10 | 42 | | elementManager.GetElementEntry(position, out ReceiveSecurityHeaderEntry entry); |
| | 10 | 43 | | if (entry.elementCategory == ReceiveSecurityHeaderElementCategory.Signature) |
| | | 44 | | { |
| | 0 | 45 | | if (!messageSecurityMode) |
| | | 46 | | { |
| | 0 | 47 | | elementManager.SetBindingMode(position, ReceiveSecurityHeaderBindingModes.Endorsing); |
| | 0 | 48 | | continue; |
| | | 49 | | } |
| | 0 | 50 | | SignedXml signedXml = (SignedXml)entry.element; |
| | | 51 | | |
| | 0 | 52 | | SignedInfo signedInfo = signedXml.Signature.SignedInfo; |
| | 0 | 53 | | bool targetsSignature = false; |
| | 0 | 54 | | if (signedInfo.References.Count == 1) |
| | | 55 | | { |
| | 0 | 56 | | Reference signedXmlReference = (Reference)signedInfo.References[0]; |
| | 0 | 57 | | string uri = signedXmlReference.Uri; |
| | | 58 | | string id; |
| | 0 | 59 | | if (uri != null && uri.Length > 1 && uri[0] == '#') |
| | | 60 | | { |
| | 0 | 61 | | id = uri.Substring(1); |
| | | 62 | | } |
| | | 63 | | else |
| | | 64 | | { |
| | 0 | 65 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 66 | | new MessageSecurityException(SR.Format(SR.UnableToResolveReferenceUriForSignature, uri)) |
| | | 67 | | } |
| | 0 | 68 | | for (int j = 0; j < elementManager.Count; j++) |
| | | 69 | | { |
| | 0 | 70 | | elementManager.GetElementEntry(j, out ReceiveSecurityHeaderEntry inner); |
| | 0 | 71 | | if (j != position && inner.elementCategory == ReceiveSecurityHeaderElementCategory.Signature |
| | | 72 | | { |
| | 0 | 73 | | targetsSignature = true; |
| | 0 | 74 | | break; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |
| | 0 | 78 | | if (targetsSignature) |
| | | 79 | | { |
| | 0 | 80 | | elementManager.SetBindingMode(position, ReceiveSecurityHeaderBindingModes.Endorsing); |
| | 0 | 81 | | continue; |
| | | 82 | | } |
| | | 83 | | else |
| | | 84 | | { |
| | 0 | 85 | | if (primarySignatureFound) |
| | | 86 | | { |
| | 0 | 87 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Fo |
| | | 88 | | } |
| | 0 | 89 | | primarySignatureFound = true; |
| | 0 | 90 | | elementManager.SetBindingMode(position, ReceiveSecurityHeaderBindingModes.Primary); |
| | | 91 | | continue; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |
| | 5 | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |