| | | 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.Runtime.CompilerServices; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Description; |
| | | 10 | | using CoreWCF.IdentityModel; |
| | | 11 | | using CoreWCF.IdentityModel.Selectors; |
| | | 12 | | using CoreWCF.Security.Tokens; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Security |
| | | 15 | | { |
| | | 16 | | internal class SecurityStandardsManager |
| | | 17 | | { |
| | | 18 | | private static SecurityStandardsManager s_instance; |
| | | 19 | | private WSSecurityTokenSerializer _wsSecurityTokenSerializer; |
| | | 20 | | |
| | | 21 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 22 | | public SecurityStandardsManager() |
| | 3 | 23 | | : this(WSSecurityTokenSerializer.DefaultInstance) |
| | | 24 | | { |
| | 3 | 25 | | } |
| | | 26 | | |
| | | 27 | | public SecurityStandardsManager(SecurityTokenSerializer tokenSerializer) |
| | 3 | 28 | | : this(MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolic |
| | | 29 | | { |
| | 3 | 30 | | } |
| | | 31 | | |
| | 131 | 32 | | public SecurityStandardsManager(MessageSecurityVersion messageSecurityVersion, SecurityTokenSerializer tokenSeri |
| | | 33 | | { |
| | 131 | 34 | | MessageSecurityVersion = messageSecurityVersion ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError |
| | 131 | 35 | | SecurityTokenSerializer = tokenSerializer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNul |
| | 131 | 36 | | if (messageSecurityVersion.SecureConversationVersion == SecureConversationVersion.WSSecureConversation13) |
| | | 37 | | { |
| | 67 | 38 | | SecureConversationDriver = new WSSecureConversationDec2005.DriverDec2005(); |
| | | 39 | | } |
| | | 40 | | else |
| | | 41 | | { |
| | 64 | 42 | | SecureConversationDriver = new WSSecureConversationFeb2005.DriverFeb2005(); |
| | | 43 | | } |
| | | 44 | | |
| | 131 | 45 | | if (SecurityVersion == SecurityVersion.WSSecurity10 || SecurityVersion == SecurityVersion.WSSecurity11) |
| | | 46 | | { |
| | 131 | 47 | | IdManager = WSSecurityJan2004.IdManager.Instance; |
| | | 48 | | } |
| | | 49 | | else |
| | | 50 | | { |
| | 0 | 51 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(message |
| | | 52 | | } |
| | | 53 | | |
| | 131 | 54 | | WSUtilitySpecificationVersion = WSUtilitySpecificationVersion.Default; |
| | 131 | 55 | | if (messageSecurityVersion.MessageSecurityTokenVersion.TrustVersion == TrustVersion.WSTrust13) |
| | | 56 | | { |
| | 67 | 57 | | TrustDriver = new WSTrustDec2005.DriverDec2005(this); |
| | | 58 | | } |
| | | 59 | | else |
| | | 60 | | { |
| | 64 | 61 | | TrustDriver = new WSTrustFeb2005.DriverFeb2005(this); |
| | | 62 | | } |
| | 64 | 63 | | } |
| | | 64 | | |
| | | 65 | | public static SecurityStandardsManager DefaultInstance |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | 59 | 69 | | if (s_instance == null) |
| | | 70 | | { |
| | 3 | 71 | | s_instance = new SecurityStandardsManager(); |
| | | 72 | | } |
| | | 73 | | |
| | 59 | 74 | | return s_instance; |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | public SecurityVersion SecurityVersion |
| | | 79 | | { |
| | 712 | 80 | | get { return MessageSecurityVersion?.SecurityVersion; } |
| | | 81 | | } |
| | | 82 | | |
| | 787 | 83 | | public MessageSecurityVersion MessageSecurityVersion { get; } |
| | | 84 | | |
| | | 85 | | public TrustVersion TrustVersion |
| | | 86 | | { |
| | 0 | 87 | | get { return MessageSecurityVersion?.TrustVersion; } |
| | | 88 | | } |
| | | 89 | | |
| | 177 | 90 | | internal SecurityTokenSerializer SecurityTokenSerializer { get; } |
| | | 91 | | |
| | 345 | 92 | | internal WSUtilitySpecificationVersion WSUtilitySpecificationVersion { get; } |
| | | 93 | | |
| | 0 | 94 | | internal SignatureTargetIdManager IdManager { get; } |
| | | 95 | | |
| | 255 | 96 | | internal SecureConversationDriver SecureConversationDriver { get; } |
| | | 97 | | |
| | 210 | 98 | | internal TrustDriver TrustDriver { get; } |
| | | 99 | | |
| | | 100 | | private WSSecurityTokenSerializer WSSecurityTokenSerializer |
| | | 101 | | { |
| | | 102 | | get |
| | | 103 | | { |
| | 0 | 104 | | if (_wsSecurityTokenSerializer == null) |
| | | 105 | | { |
| | 0 | 106 | | if (!(SecurityTokenSerializer is WSSecurityTokenSerializer wsSecurityTokenSerializer)) |
| | | 107 | | { |
| | 0 | 108 | | wsSecurityTokenSerializer = new WSSecurityTokenSerializer(SecurityVersion); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | _wsSecurityTokenSerializer = wsSecurityTokenSerializer; |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | return _wsSecurityTokenSerializer; |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | internal bool TryCreateKeyIdentifierClauseFromTokenXml(XmlElement element, SecurityTokenReferenceStyle tokenRefe |
| | | 119 | | { |
| | 0 | 120 | | return WSSecurityTokenSerializer.TryCreateKeyIdentifierClauseFromTokenXml(element, tokenReferenceStyle, out |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | internal SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXml(XmlElement element, SecurityTokenRefe |
| | | 124 | | { |
| | 0 | 125 | | return WSSecurityTokenSerializer.CreateKeyIdentifierClauseFromTokenXml(element, tokenReferenceStyle); |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | internal SendSecurityHeader CreateSendSecurityHeader(Message message, |
| | | 129 | | string actor, bool mustUnderstand, bool relay, |
| | | 130 | | SecurityAlgorithmSuite algorithmSuite, MessageDirection direction) |
| | | 131 | | { |
| | 63 | 132 | | return SecurityVersion.CreateSendSecurityHeader(message, actor, mustUnderstand, relay, this, algorithmSuite, |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | internal ReceiveSecurityHeader TryCreateReceiveSecurityHeader(Message message, |
| | | 136 | | string actor, |
| | | 137 | | SecurityAlgorithmSuite algorithmSuite, MessageDirection direction) |
| | | 138 | | { |
| | 94 | 139 | | return SecurityVersion.TryCreateReceiveSecurityHeader(message, actor, this, algorithmSuite, direction); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | internal bool DoesMessageContainSecurityHeader(Message message) |
| | | 143 | | { |
| | 23 | 144 | | return SecurityVersion.DoesMessageContainSecurityHeader(message); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | internal bool TryGetSecurityContextIds(Message message, string[] actors, bool isStrictMode, ICollection<UniqueId |
| | | 148 | | { |
| | 20 | 149 | | if (results == null) |
| | | 150 | | { |
| | 0 | 151 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(results)); |
| | | 152 | | } |
| | 20 | 153 | | SecureConversationDriver driver = SecureConversationDriver; |
| | 20 | 154 | | int securityHeaderIndex = SecurityVersion.FindIndexOfSecurityHeader(message, actors); |
| | 20 | 155 | | if (securityHeaderIndex < 0) |
| | | 156 | | { |
| | 0 | 157 | | return false; |
| | | 158 | | } |
| | 20 | 159 | | bool addedContextIds = false; |
| | 20 | 160 | | using (XmlDictionaryReader reader = message.Headers.GetReaderAtHeader(securityHeaderIndex)) |
| | | 161 | | { |
| | 20 | 162 | | if (!reader.IsStartElement()) |
| | | 163 | | { |
| | 0 | 164 | | return false; |
| | | 165 | | } |
| | 20 | 166 | | if (reader.IsEmptyElement) |
| | | 167 | | { |
| | 0 | 168 | | return false; |
| | | 169 | | } |
| | 20 | 170 | | reader.ReadStartElement(); |
| | 40 | 171 | | while (reader.IsStartElement()) |
| | | 172 | | { |
| | 40 | 173 | | if (driver.IsAtSecurityContextToken(reader)) |
| | | 174 | | { |
| | 20 | 175 | | results.Add(driver.GetSecurityContextTokenId(reader)); |
| | 20 | 176 | | addedContextIds = true; |
| | 20 | 177 | | if (isStrictMode) |
| | | 178 | | { |
| | 20 | 179 | | break; |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | else |
| | | 183 | | { |
| | 20 | 184 | | reader.Skip(); |
| | | 185 | | } |
| | | 186 | | } |
| | 0 | 187 | | } |
| | 20 | 188 | | return addedContextIds; |
| | 0 | 189 | | } |
| | | 190 | | } |
| | | 191 | | } |