| | | 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.IO; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.Description; |
| | | 11 | | using CoreWCF.Security.Tokens; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Security |
| | | 14 | | { |
| | | 15 | | internal abstract class WSSecurityPolicy |
| | | 16 | | { |
| | 1 | 17 | | public static ContractDescription NullContract = new ContractDescription("null"); |
| | 1 | 18 | | public static ServiceEndpoint NullServiceEndpoint = new ServiceEndpoint(NullContract); |
| | 1 | 19 | | public static XmlDocument doc = new XmlDocument(); |
| | | 20 | | public const string WsspPrefix = "sp"; |
| | | 21 | | public const string WspNamespace = MetadataStrings.WSPolicy.NamespaceUri; //@"http://schemas.xmlsoap.org/ws/2004 |
| | | 22 | | public const string Wsp15Namespace = MetadataStrings.WSPolicy.NamespaceUri15; |
| | | 23 | | public const string WspPrefix = MetadataStrings.WSPolicy.Prefix; //"wsp"; |
| | | 24 | | public const string MsspNamespace = @"http://schemas.microsoft.com/ws/2005/07/securitypolicy"; |
| | | 25 | | public const string MsspPrefix = "mssp"; |
| | | 26 | | public const string PolicyName = MetadataStrings.WSPolicy.Elements.Policy; //"Policy"; |
| | | 27 | | public const string OptionalName = "Optional"; |
| | | 28 | | public const string TrueName = "true"; |
| | | 29 | | public const string FalseName = "false"; |
| | | 30 | | public const string SymmetricBindingName = "SymmetricBinding"; |
| | | 31 | | public const string AsymmetricBindingName = "AsymmetricBinding"; |
| | | 32 | | public const string TransportBindingName = "TransportBinding"; |
| | | 33 | | public const string OnlySignEntireHeadersAndBodyName = "OnlySignEntireHeadersAndBody"; |
| | | 34 | | public const string ProtectionTokenName = "ProtectionToken"; |
| | | 35 | | public const string InitiatorTokenName = "InitiatorToken"; |
| | | 36 | | public const string RecipientTokenName = "RecipientToken"; |
| | | 37 | | public const string TransportTokenName = "TransportToken"; |
| | | 38 | | public const string AlgorithmSuiteName = "AlgorithmSuite"; |
| | | 39 | | public const string LaxName = "Lax"; |
| | | 40 | | public const string LaxTsLastName = "LaxTsLast"; |
| | | 41 | | public const string LaxTsFirstName = "LaxTsFirst"; |
| | | 42 | | public const string StrictName = "Strict"; |
| | | 43 | | public const string IncludeTimestampName = "IncludeTimestamp"; |
| | | 44 | | public const string EncryptBeforeSigningName = "EncryptBeforeSigning"; |
| | | 45 | | public const string ProtectTokens = "ProtectTokens"; |
| | | 46 | | public const string EncryptSignatureName = "EncryptSignature"; |
| | | 47 | | public const string SignedSupportingTokensName = "SignedSupportingTokens"; |
| | | 48 | | public const string EndorsingSupportingTokensName = "EndorsingSupportingTokens"; |
| | | 49 | | public const string SignedEndorsingSupportingTokensName = "SignedEndorsingSupportingTokens"; |
| | | 50 | | public const string Wss10Name = "Wss10"; |
| | | 51 | | public const string MustSupportRefKeyIdentifierName = "MustSupportRefKeyIdentifier"; |
| | | 52 | | public const string MustSupportRefIssuerSerialName = "MustSupportRefIssuerSerial"; |
| | | 53 | | public const string MustSupportRefThumbprintName = "MustSupportRefThumbprint"; |
| | | 54 | | public const string MustSupportRefEncryptedKeyName = "MustSupportRefEncryptedKey"; |
| | | 55 | | public const string RequireSignatureConfirmationName = "RequireSignatureConfirmation"; |
| | | 56 | | public const string MustSupportIssuedTokensName = "MustSupportIssuedTokens"; |
| | | 57 | | public const string RequireClientEntropyName = "RequireClientEntropy"; |
| | | 58 | | public const string RequireServerEntropyName = "RequireServerEntropy"; |
| | | 59 | | public const string Wss11Name = "Wss11"; |
| | | 60 | | public const string Trust10Name = "Trust10"; |
| | | 61 | | public const string Trust13Name = "Trust13"; |
| | | 62 | | public const string RequireAppliesTo = "RequireAppliesTo"; |
| | | 63 | | public const string SignedPartsName = "SignedParts"; |
| | | 64 | | public const string EncryptedPartsName = "EncryptedParts"; |
| | | 65 | | public const string BodyName = "Body"; |
| | | 66 | | public const string HeaderName = "Header"; |
| | | 67 | | public const string NameName = "Name"; |
| | | 68 | | public const string NamespaceName = "Namespace"; |
| | | 69 | | public const string Basic128Name = "Basic128"; |
| | | 70 | | public const string Basic192Name = "Basic192"; |
| | | 71 | | public const string Basic256Name = "Basic256"; |
| | | 72 | | public const string TripleDesName = "TripleDes"; |
| | | 73 | | public const string Basic128Rsa15Name = "Basic128Rsa15"; |
| | | 74 | | public const string Basic192Rsa15Name = "Basic192Rsa15"; |
| | | 75 | | public const string Basic256Rsa15Name = "Basic256Rsa15"; |
| | | 76 | | public const string TripleDesRsa15Name = "TripleDesRsa15"; |
| | | 77 | | public const string Basic128Sha256Name = "Basic128Sha256"; |
| | | 78 | | public const string Basic192Sha256Name = "Basic192Sha256"; |
| | | 79 | | public const string Basic256Sha256Name = "Basic256Sha256"; |
| | | 80 | | public const string TripleDesSha256Name = "TripleDesSha256"; |
| | | 81 | | public const string Basic128Sha256Rsa15Name = "Basic128Sha256Rsa15"; |
| | | 82 | | public const string Basic192Sha256Rsa15Name = "Basic192Sha256Rsa15"; |
| | | 83 | | public const string Basic256Sha256Rsa15Name = "Basic256Sha256Rsa15"; |
| | | 84 | | public const string TripleDesSha256Rsa15Name = "TripleDesSha256Rsa15"; |
| | | 85 | | public const string IncludeTokenName = "IncludeToken"; |
| | | 86 | | public const string KerberosTokenName = "KerberosToken"; |
| | | 87 | | public const string X509TokenName = "X509Token"; |
| | | 88 | | public const string IssuedTokenName = "IssuedToken"; |
| | | 89 | | public const string UsernameTokenName = "UsernameToken"; |
| | | 90 | | public const string RsaTokenName = "RsaToken"; |
| | | 91 | | public const string KeyValueTokenName = "KeyValueToken"; |
| | | 92 | | public const string SpnegoContextTokenName = "SpnegoContextToken"; |
| | | 93 | | public const string SslContextTokenName = "SslContextToken"; |
| | | 94 | | public const string SecureConversationTokenName = "SecureConversationToken"; |
| | | 95 | | public const string WssGssKerberosV5ApReqToken11Name = "WssGssKerberosV5ApReqToken11"; |
| | | 96 | | public const string RequireDerivedKeysName = "RequireDerivedKeys"; |
| | | 97 | | public const string RequireIssuerSerialReferenceName = "RequireIssuerSerialReference"; |
| | | 98 | | public const string RequireKeyIdentifierReferenceName = "RequireKeyIdentifierReference"; |
| | | 99 | | public const string RequireThumbprintReferenceName = "RequireThumbprintReference"; |
| | | 100 | | public const string WssX509V3Token10Name = "WssX509V3Token10"; |
| | | 101 | | public const string WssUsernameToken10Name = "WssUsernameToken10"; |
| | | 102 | | public const string RequestSecurityTokenTemplateName = "RequestSecurityTokenTemplate"; |
| | | 103 | | public const string RequireExternalReferenceName = "RequireExternalReference"; |
| | | 104 | | public const string RequireInternalReferenceName = "RequireInternalReference"; |
| | | 105 | | public const string IssuerName = "Issuer"; |
| | | 106 | | public const string RequireClientCertificateName = "RequireClientCertificate"; |
| | | 107 | | public const string MustNotSendCancelName = "MustNotSendCancel"; |
| | | 108 | | public const string MustNotSendAmendName = "MustNotSendAmend"; |
| | | 109 | | public const string MustNotSendRenewName = "MustNotSendRenew"; |
| | | 110 | | public const string LayoutName = "Layout"; |
| | | 111 | | public const string BootstrapPolicyName = "BootstrapPolicy"; |
| | | 112 | | public const string HttpsTokenName = "HttpsToken"; |
| | | 113 | | public const string HttpBasicAuthenticationName = "HttpBasicAuthentication"; |
| | | 114 | | public const string HttpDigestAuthenticationName = "HttpDigestAuthentication"; |
| | | 115 | | |
| | | 116 | | bool _mustSupportRefKeyIdentifierName = false; |
| | | 117 | | bool _mustSupportRefIssuerSerialName = false; |
| | | 118 | | bool _mustSupportRefThumbprintName = false; |
| | | 119 | | bool _protectionTokenHasAsymmetricKey = false; |
| | | 120 | | |
| | | 121 | | public virtual XmlElement CreateWsspAssertion(string name) |
| | | 122 | | { |
| | 433 | 123 | | return doc.CreateElement(WsspPrefix, name, WsspNamespaceUri); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | public virtual bool IsWsspAssertion(XmlElement assertion) |
| | | 127 | | { |
| | 45 | 128 | | return assertion.NamespaceURI == WsspNamespaceUri; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public virtual XmlElement CreateMsspAssertion(string name) |
| | | 132 | | { |
| | 0 | 133 | | return doc.CreateElement(MsspPrefix, name, MsspNamespace); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public abstract bool IsSecurityVersionSupported(MessageSecurityVersion version); |
| | | 137 | | |
| | | 138 | | public abstract string WsspNamespaceUri { get; } |
| | | 139 | | public abstract TrustDriver TrustDriver { get; } |
| | 19 | 140 | | public virtual string AlwaysToRecipientUri => WsspNamespaceUri + @"/IncludeToken/AlwaysToRecipient"; |
| | 0 | 141 | | public virtual string NeverUri => WsspNamespaceUri + @"/IncludeToken/Never"; |
| | 0 | 142 | | public virtual string OnceUri => WsspNamespaceUri + @"/IncludeToken/Once"; |
| | 0 | 143 | | public virtual string AlwaysToInitiatorUri => WsspNamespaceUri + @"/IncludeToken/AlwaysToInitiator"; |
| | | 144 | | |
| | | 145 | | public virtual XmlElement CreateWspPolicyWrapper(MetadataExporter exporter, params XmlElement[] nestedAssertions |
| | | 146 | | { |
| | 173 | 147 | | XmlElement result = doc.CreateElement(WspPrefix, PolicyName, exporter.PolicyVersion.Namespace); |
| | | 148 | | |
| | 173 | 149 | | if (nestedAssertions != null) |
| | | 150 | | { |
| | 1102 | 151 | | foreach (XmlElement e in nestedAssertions) |
| | | 152 | | { |
| | 378 | 153 | | if (e != null) |
| | | 154 | | { |
| | 260 | 155 | | result.AppendChild(e); |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | 173 | 160 | | return result; |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | public virtual XmlElement CreateWsspSignedPartsAssertion(MessagePartSpecification parts) |
| | | 164 | | { |
| | 17 | 165 | | if (parts == null) |
| | | 166 | | { |
| | 0 | 167 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parts)); |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | XmlElement result; |
| | | 171 | | |
| | 17 | 172 | | if (parts.IsEmpty()) |
| | | 173 | | { |
| | 0 | 174 | | result = null; |
| | | 175 | | } |
| | | 176 | | else |
| | | 177 | | { |
| | 17 | 178 | | result = CreateWsspAssertion(SignedPartsName); |
| | 17 | 179 | | if (parts.IsBodyIncluded) |
| | | 180 | | { |
| | 9 | 181 | | result.AppendChild(CreateWsspAssertion(BodyName)); |
| | | 182 | | } |
| | 176 | 183 | | foreach (XmlQualifiedName header in parts.HeaderTypes) |
| | | 184 | | { |
| | 71 | 185 | | result.AppendChild(CreateWsspHeaderAssertion(header)); |
| | | 186 | | } |
| | | 187 | | } |
| | 17 | 188 | | return result; |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | public virtual XmlElement CreateWsspEncryptedPartsAssertion(MessagePartSpecification parts) |
| | | 192 | | { |
| | 9 | 193 | | if (parts == null) |
| | | 194 | | { |
| | 0 | 195 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parts)); |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | XmlElement result; |
| | | 199 | | |
| | 9 | 200 | | if (parts.IsEmpty()) |
| | | 201 | | { |
| | 0 | 202 | | result = null; |
| | | 203 | | } |
| | | 204 | | else |
| | | 205 | | { |
| | 9 | 206 | | result = CreateWsspAssertion(EncryptedPartsName); |
| | 9 | 207 | | if (parts.IsBodyIncluded) |
| | | 208 | | { |
| | 9 | 209 | | result.AppendChild(CreateWsspAssertion(BodyName)); |
| | | 210 | | } |
| | 18 | 211 | | foreach (XmlQualifiedName header in parts.HeaderTypes) |
| | | 212 | | { |
| | 0 | 213 | | result.AppendChild(CreateWsspHeaderAssertion(header)); |
| | | 214 | | } |
| | | 215 | | } |
| | 9 | 216 | | return result; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | public virtual XmlElement CreateWsspHeaderAssertion(XmlQualifiedName header) |
| | | 220 | | { |
| | 71 | 221 | | XmlElement result = CreateWsspAssertion(HeaderName); |
| | 71 | 222 | | result.SetAttribute(NameName, header.Name); |
| | 71 | 223 | | result.SetAttribute(NamespaceName, header.Namespace); |
| | | 224 | | |
| | 71 | 225 | | return result; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | public virtual XmlElement CreateWsspTransportBindingAssertion(MetadataExporter exporter, TransportSecurityBindin |
| | | 229 | | { |
| | 22 | 230 | | XmlElement result = CreateWsspAssertion(TransportBindingName); |
| | 22 | 231 | | result.AppendChild( |
| | 22 | 232 | | CreateWspPolicyWrapper( |
| | 22 | 233 | | exporter, |
| | 22 | 234 | | CreateWsspTransportTokenAssertion(exporter, transportTokenAssertion), |
| | 22 | 235 | | CreateWsspAlgorithmSuiteAssertion(exporter, binding.DefaultAlgorithmSuite), |
| | 22 | 236 | | CreateWsspLayoutAssertion(exporter, binding.SecurityHeaderLayout), |
| | 22 | 237 | | CreateWsspIncludeTimestampAssertion(binding.IncludeTimestamp) |
| | 22 | 238 | | )); |
| | | 239 | | |
| | 22 | 240 | | return result; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | public virtual XmlElement CreateWsspWssAssertion(MetadataExporter exporter, SecurityBindingElement binding) |
| | | 244 | | { |
| | 19 | 245 | | if (binding == null) |
| | | 246 | | { |
| | 0 | 247 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(binding)); |
| | | 248 | | } |
| | | 249 | | |
| | 19 | 250 | | if (binding.MessageSecurityVersion.SecurityVersion == SecurityVersion.WSSecurity10) |
| | | 251 | | { |
| | 0 | 252 | | return CreateWsspWss10Assertion(exporter); |
| | | 253 | | } |
| | 19 | 254 | | else if (binding.MessageSecurityVersion.SecurityVersion == SecurityVersion.WSSecurity11) |
| | | 255 | | { |
| | 19 | 256 | | if (binding is SymmetricSecurityBindingElement) |
| | | 257 | | { |
| | 0 | 258 | | return CreateWsspWss11Assertion(exporter, ((SymmetricSecurityBindingElement)binding).RequireSignatur |
| | | 259 | | } |
| | | 260 | | /*else if (binding is AsymmetricSecurityBindingElement) |
| | | 261 | | { |
| | | 262 | | return CreateWsspWss11Assertion(exporter, ((AsymmetricSecurityBindingElement)binding).RequireSignatu |
| | | 263 | | }*/ |
| | | 264 | | else |
| | | 265 | | { |
| | 19 | 266 | | return CreateWsspWss11Assertion(exporter, false); |
| | | 267 | | } |
| | | 268 | | } |
| | | 269 | | else |
| | | 270 | | { |
| | 0 | 271 | | return null; |
| | | 272 | | } |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | public virtual XmlElement CreateWsspWss10Assertion(MetadataExporter exporter) |
| | | 276 | | { |
| | 0 | 277 | | XmlElement result = CreateWsspAssertion(Wss10Name); |
| | 0 | 278 | | result.AppendChild( |
| | 0 | 279 | | CreateWspPolicyWrapper( |
| | 0 | 280 | | exporter, |
| | 0 | 281 | | CreateWsspAssertionMustSupportRefKeyIdentifierName(), |
| | 0 | 282 | | CreateWsspAssertionMustSupportRefIssuerSerialName() |
| | 0 | 283 | | )); |
| | | 284 | | |
| | 0 | 285 | | return result; |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | public virtual XmlElement CreateWsspWss11Assertion(MetadataExporter exporter, bool requireSignatureConfirmation) |
| | | 289 | | { |
| | 19 | 290 | | XmlElement result = CreateWsspAssertion(Wss11Name); |
| | 19 | 291 | | result.AppendChild( |
| | 19 | 292 | | CreateWspPolicyWrapper( |
| | 19 | 293 | | exporter, |
| | 19 | 294 | | CreateWsspAssertionMustSupportRefKeyIdentifierName(), |
| | 19 | 295 | | CreateWsspAssertionMustSupportRefIssuerSerialName(), |
| | 19 | 296 | | CreateWsspAssertionMustSupportRefThumbprintName(), |
| | 19 | 297 | | CreateWsspAssertionMustSupportRefEncryptedKeyName(), |
| | 19 | 298 | | CreateWsspRequireSignatureConformationAssertion(requireSignatureConfirmation) |
| | 19 | 299 | | )); |
| | | 300 | | |
| | 19 | 301 | | return result; |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | public virtual XmlElement CreateWsspAssertionMustSupportRefKeyIdentifierName() |
| | | 305 | | { |
| | 19 | 306 | | if (_mustSupportRefKeyIdentifierName) |
| | | 307 | | { |
| | 0 | 308 | | return CreateWsspAssertion(MustSupportRefKeyIdentifierName); |
| | | 309 | | } |
| | | 310 | | else |
| | | 311 | | { |
| | 19 | 312 | | return null; |
| | | 313 | | } |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | public virtual XmlElement CreateWsspAssertionMustSupportRefIssuerSerialName() |
| | | 317 | | { |
| | 19 | 318 | | if (_mustSupportRefIssuerSerialName) |
| | | 319 | | { |
| | 0 | 320 | | return CreateWsspAssertion(MustSupportRefIssuerSerialName); |
| | | 321 | | } |
| | | 322 | | else |
| | | 323 | | { |
| | 19 | 324 | | return null; |
| | | 325 | | } |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | public virtual XmlElement CreateWsspAssertionMustSupportRefThumbprintName() |
| | | 329 | | { |
| | 19 | 330 | | if (_mustSupportRefThumbprintName) |
| | | 331 | | { |
| | 8 | 332 | | return CreateWsspAssertion(MustSupportRefThumbprintName); |
| | | 333 | | } |
| | | 334 | | else |
| | | 335 | | { |
| | 11 | 336 | | return null; |
| | | 337 | | } |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | public virtual XmlElement CreateWsspAssertionMustSupportRefEncryptedKeyName() |
| | | 341 | | { |
| | | 342 | | // protectionTokenHasAsymmetricKey is only set to true for a SymmetricBindingElement having an asymmetric ke |
| | 19 | 343 | | if (_protectionTokenHasAsymmetricKey) |
| | | 344 | | { |
| | 0 | 345 | | return CreateWsspAssertion(MustSupportRefEncryptedKeyName); |
| | | 346 | | } |
| | | 347 | | else |
| | | 348 | | { |
| | 19 | 349 | | return null; |
| | | 350 | | } |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | public virtual XmlElement CreateWsspRequireSignatureConformationAssertion(bool requireSignatureConfirmation) |
| | | 354 | | { |
| | 19 | 355 | | if (requireSignatureConfirmation) |
| | | 356 | | { |
| | 0 | 357 | | return CreateWsspAssertion(RequireSignatureConfirmationName); |
| | | 358 | | } |
| | | 359 | | else |
| | | 360 | | { |
| | 19 | 361 | | return null; |
| | | 362 | | } |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | public abstract XmlElement CreateWsspTrustAssertion(MetadataExporter exporter, SecurityKeyEntropyMode keyEntropy |
| | | 366 | | |
| | | 367 | | protected XmlElement CreateWsspTrustAssertion(string trustName, MetadataExporter exporter, SecurityKeyEntropyMod |
| | | 368 | | { |
| | 19 | 369 | | XmlElement result = CreateWsspAssertion(trustName); |
| | 19 | 370 | | result.AppendChild( |
| | 19 | 371 | | CreateWspPolicyWrapper( |
| | 19 | 372 | | exporter, |
| | 19 | 373 | | CreateWsspAssertion(MustSupportIssuedTokensName), |
| | 19 | 374 | | CreateWsspRequireClientEntropyAssertion(keyEntropyMode), |
| | 19 | 375 | | CreateWsspRequireServerEntropyAssertion(keyEntropyMode) |
| | 19 | 376 | | )); |
| | | 377 | | |
| | 19 | 378 | | return result; |
| | | 379 | | } |
| | | 380 | | |
| | | 381 | | public virtual XmlElement CreateWsspRequireClientEntropyAssertion(SecurityKeyEntropyMode keyEntropyMode) |
| | | 382 | | { |
| | 19 | 383 | | if (keyEntropyMode == SecurityKeyEntropyMode.ClientEntropy || keyEntropyMode == SecurityKeyEntropyMode.Combi |
| | | 384 | | { |
| | 19 | 385 | | return CreateWsspAssertion(RequireClientEntropyName); |
| | | 386 | | } |
| | | 387 | | else |
| | | 388 | | { |
| | 0 | 389 | | return null; |
| | | 390 | | } |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | public virtual XmlElement CreateWsspRequireServerEntropyAssertion(SecurityKeyEntropyMode keyEntropyMode) |
| | | 394 | | { |
| | 19 | 395 | | if (keyEntropyMode == SecurityKeyEntropyMode.ServerEntropy || keyEntropyMode == SecurityKeyEntropyMode.Combi |
| | | 396 | | { |
| | 19 | 397 | | return CreateWsspAssertion(RequireServerEntropyName); |
| | | 398 | | } |
| | | 399 | | else |
| | | 400 | | { |
| | 0 | 401 | | return null; |
| | | 402 | | } |
| | | 403 | | } |
| | | 404 | | |
| | | 405 | | public virtual Collection<XmlElement> CreateWsspSupportingTokensAssertion(MetadataExporter exporter, Collection< |
| | | 406 | | { |
| | 0 | 407 | | return CreateWsspSupportingTokensAssertion(exporter, signed, signedEncrypted, endorsing, signedEndorsing, op |
| | | 408 | | } |
| | | 409 | | |
| | | 410 | | public virtual Collection<XmlElement> CreateWsspSupportingTokensAssertion(MetadataExporter exporter, Collection< |
| | | 411 | | { |
| | 22 | 412 | | Collection<XmlElement> supportingTokenAssertions = new Collection<XmlElement>(); |
| | | 413 | | |
| | | 414 | | // Signed Supporting Tokens |
| | 22 | 415 | | XmlElement supportingTokenAssertion = CreateWsspSignedSupportingTokensAssertion(exporter, signed, signedEncr |
| | 22 | 416 | | if (supportingTokenAssertion != null) |
| | 1 | 417 | | supportingTokenAssertions.Add(supportingTokenAssertion); |
| | | 418 | | |
| | | 419 | | // Endorsing Supporting Tokens. |
| | 22 | 420 | | supportingTokenAssertion = CreateWsspEndorsingSupportingTokensAssertion(exporter, endorsing, optionalEndorsi |
| | 22 | 421 | | if (supportingTokenAssertion != null) |
| | 18 | 422 | | supportingTokenAssertions.Add(supportingTokenAssertion); |
| | | 423 | | |
| | | 424 | | // Signed Endorsing Supporting Tokens. |
| | 22 | 425 | | supportingTokenAssertion = CreateWsspSignedEndorsingSupportingTokensAssertion(exporter, signedEndorsing, opt |
| | 22 | 426 | | if (supportingTokenAssertion != null) |
| | 0 | 427 | | supportingTokenAssertions.Add(supportingTokenAssertion); |
| | | 428 | | |
| | 22 | 429 | | return supportingTokenAssertions; |
| | | 430 | | } |
| | | 431 | | |
| | | 432 | | protected XmlElement CreateWsspSignedSupportingTokensAssertion(MetadataExporter exporter, Collection<SecurityTok |
| | | 433 | | { |
| | | 434 | | XmlElement result; |
| | | 435 | | |
| | 22 | 436 | | if ((signed == null || signed.Count == 0) |
| | 22 | 437 | | && (signedEncrypted == null || signedEncrypted.Count == 0) |
| | 22 | 438 | | && (optionalSigned == null || optionalSigned.Count == 0) |
| | 22 | 439 | | && (optionalSignedEncrypted == null || optionalSignedEncrypted.Count == 0)) |
| | | 440 | | { |
| | 21 | 441 | | result = null; |
| | | 442 | | } |
| | | 443 | | else |
| | | 444 | | { |
| | 1 | 445 | | XmlElement policy = CreateWspPolicyWrapper(exporter); |
| | | 446 | | |
| | 1 | 447 | | if (signed != null) |
| | | 448 | | { |
| | 2 | 449 | | foreach (SecurityTokenParameters p in signed) |
| | | 450 | | { |
| | 0 | 451 | | policy.AppendChild(CreateTokenAssertion(exporter, p)); |
| | | 452 | | } |
| | | 453 | | } |
| | 1 | 454 | | if (signedEncrypted != null) |
| | | 455 | | { |
| | 4 | 456 | | foreach (SecurityTokenParameters p in signedEncrypted) |
| | | 457 | | { |
| | 1 | 458 | | policy.AppendChild(CreateTokenAssertion(exporter, p)); |
| | | 459 | | } |
| | | 460 | | } |
| | 1 | 461 | | if (optionalSigned != null) |
| | | 462 | | { |
| | 2 | 463 | | foreach (SecurityTokenParameters p in optionalSigned) |
| | | 464 | | { |
| | 0 | 465 | | policy.AppendChild(CreateTokenAssertion(exporter, p, true)); |
| | | 466 | | } |
| | | 467 | | } |
| | 1 | 468 | | if (optionalSignedEncrypted != null) |
| | | 469 | | { |
| | 2 | 470 | | foreach (SecurityTokenParameters p in optionalSignedEncrypted) |
| | | 471 | | { |
| | 0 | 472 | | policy.AppendChild(CreateTokenAssertion(exporter, p, true)); |
| | | 473 | | } |
| | | 474 | | } |
| | | 475 | | |
| | 1 | 476 | | result = CreateWsspAssertion(SignedSupportingTokensName); |
| | 1 | 477 | | result.AppendChild(policy); |
| | | 478 | | } |
| | | 479 | | |
| | 22 | 480 | | return result; |
| | | 481 | | } |
| | | 482 | | |
| | | 483 | | protected XmlElement CreateWsspEndorsingSupportingTokensAssertion(MetadataExporter exporter, Collection<Security |
| | | 484 | | { |
| | 22 | 485 | | return CreateWsspiSupportingTokensAssertion(exporter, endorsing, optionalEndorsing, addressingVersion, Endor |
| | | 486 | | } |
| | | 487 | | |
| | | 488 | | protected XmlElement CreateWsspSignedEndorsingSupportingTokensAssertion(MetadataExporter exporter, Collection<Se |
| | | 489 | | { |
| | 22 | 490 | | return CreateWsspiSupportingTokensAssertion(exporter, signedEndorsing, optionalSignedEndorsing, addressingVe |
| | | 491 | | } |
| | | 492 | | |
| | | 493 | | protected XmlElement CreateWsspiSupportingTokensAssertion(MetadataExporter exporter, Collection<SecurityTokenPar |
| | | 494 | | { |
| | | 495 | | XmlElement result; |
| | 44 | 496 | | bool hasAssymetricKey = false; |
| | | 497 | | |
| | 44 | 498 | | if ((endorsing == null || endorsing.Count == 0) |
| | 44 | 499 | | && (optionalEndorsing == null || optionalEndorsing.Count == 0)) |
| | | 500 | | { |
| | 26 | 501 | | result = null; |
| | | 502 | | } |
| | | 503 | | else |
| | | 504 | | { |
| | 18 | 505 | | XmlElement policy = CreateWspPolicyWrapper(exporter); |
| | | 506 | | |
| | 18 | 507 | | if (endorsing != null) |
| | | 508 | | { |
| | 72 | 509 | | foreach (SecurityTokenParameters p in endorsing) |
| | | 510 | | { |
| | 18 | 511 | | if (p.HasAsymmetricKey) |
| | 8 | 512 | | hasAssymetricKey = true; |
| | | 513 | | |
| | 18 | 514 | | policy.AppendChild(CreateTokenAssertion(exporter, p)); |
| | | 515 | | } |
| | | 516 | | } |
| | 18 | 517 | | if (optionalEndorsing != null) |
| | | 518 | | { |
| | 36 | 519 | | foreach (SecurityTokenParameters p in optionalEndorsing) |
| | | 520 | | { |
| | 0 | 521 | | if (p.HasAsymmetricKey) |
| | 0 | 522 | | hasAssymetricKey = true; |
| | | 523 | | |
| | 0 | 524 | | policy.AppendChild(CreateTokenAssertion(exporter, p, true)); |
| | | 525 | | } |
| | | 526 | | } |
| | 18 | 527 | | if (addressingVersion != null && AddressingVersion.None != addressingVersion) |
| | | 528 | | { |
| | | 529 | | // only add assertion to sign the 'To' only if an assymetric key is found |
| | 18 | 530 | | if (hasAssymetricKey) |
| | | 531 | | { |
| | 8 | 532 | | policy.AppendChild( |
| | 8 | 533 | | CreateWsspSignedPartsAssertion( |
| | 8 | 534 | | new MessagePartSpecification(new XmlQualifiedName(AddressingStrings.To, addressingVersio |
| | | 535 | | } |
| | | 536 | | } |
| | | 537 | | |
| | 18 | 538 | | result = CreateWsspAssertion(assertionName); |
| | 18 | 539 | | result.AppendChild(policy); |
| | | 540 | | } |
| | | 541 | | |
| | 44 | 542 | | return result; |
| | | 543 | | } |
| | | 544 | | |
| | | 545 | | public virtual XmlElement CreateWsspIncludeTimestampAssertion(bool includeTimestamp) |
| | | 546 | | { |
| | 22 | 547 | | if (includeTimestamp) |
| | | 548 | | { |
| | 19 | 549 | | return CreateWsspAssertion(IncludeTimestampName); |
| | | 550 | | } |
| | | 551 | | else |
| | | 552 | | { |
| | 3 | 553 | | return null; |
| | | 554 | | } |
| | | 555 | | } |
| | | 556 | | |
| | | 557 | | public virtual XmlElement CreateWsspLayoutAssertion(MetadataExporter exporter, SecurityHeaderLayout layout) |
| | | 558 | | { |
| | 22 | 559 | | XmlElement result = CreateWsspAssertion(LayoutName); |
| | 22 | 560 | | result.AppendChild( |
| | 22 | 561 | | CreateWspPolicyWrapper( |
| | 22 | 562 | | exporter, |
| | 22 | 563 | | CreateLayoutAssertion(layout) |
| | 22 | 564 | | )); |
| | | 565 | | |
| | 22 | 566 | | return result; |
| | | 567 | | } |
| | | 568 | | |
| | | 569 | | public virtual XmlElement CreateLayoutAssertion(SecurityHeaderLayout layout) |
| | | 570 | | { |
| | | 571 | | switch (layout) |
| | | 572 | | { |
| | | 573 | | default: |
| | 0 | 574 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(lay |
| | | 575 | | case SecurityHeaderLayout.Lax: |
| | 0 | 576 | | return CreateWsspAssertion(LaxName); |
| | | 577 | | case SecurityHeaderLayout.LaxTimestampFirst: |
| | 0 | 578 | | return CreateWsspAssertion(LaxTsFirstName); |
| | | 579 | | case SecurityHeaderLayout.LaxTimestampLast: |
| | 0 | 580 | | return CreateWsspAssertion(LaxTsLastName); |
| | | 581 | | case SecurityHeaderLayout.Strict: |
| | 22 | 582 | | return CreateWsspAssertion(StrictName); |
| | | 583 | | } |
| | | 584 | | } |
| | | 585 | | |
| | | 586 | | public virtual XmlElement CreateWsspAlgorithmSuiteAssertion(MetadataExporter exporter, SecurityAlgorithmSuite su |
| | | 587 | | { |
| | 22 | 588 | | XmlElement result = CreateWsspAssertion(AlgorithmSuiteName); |
| | 22 | 589 | | result.AppendChild( |
| | 22 | 590 | | CreateWspPolicyWrapper( |
| | 22 | 591 | | exporter, |
| | 22 | 592 | | CreateAlgorithmSuiteAssertion(suite) |
| | 22 | 593 | | )); |
| | | 594 | | |
| | 22 | 595 | | return result; |
| | | 596 | | } |
| | | 597 | | |
| | | 598 | | public virtual XmlElement CreateAlgorithmSuiteAssertion(SecurityAlgorithmSuite suite) |
| | | 599 | | { |
| | 22 | 600 | | if (suite == null) |
| | | 601 | | { |
| | 0 | 602 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(suite)); |
| | | 603 | | } |
| | | 604 | | |
| | | 605 | | XmlElement result; |
| | | 606 | | |
| | 22 | 607 | | if (suite == SecurityAlgorithmSuite.Basic256) |
| | 12 | 608 | | result = CreateWsspAssertion(Basic256Name); |
| | 10 | 609 | | else if (suite == SecurityAlgorithmSuite.Basic192) |
| | 2 | 610 | | result = CreateWsspAssertion(Basic192Name); |
| | 8 | 611 | | else if (suite == SecurityAlgorithmSuite.Basic128) |
| | 2 | 612 | | result = CreateWsspAssertion(Basic128Name); |
| | 6 | 613 | | else if (suite == SecurityAlgorithmSuite.TripleDes) |
| | 0 | 614 | | result = CreateWsspAssertion(TripleDesName); |
| | 6 | 615 | | else if (suite == SecurityAlgorithmSuite.Basic256Rsa15) |
| | 0 | 616 | | result = CreateWsspAssertion(Basic256Rsa15Name); |
| | 6 | 617 | | else if (suite == SecurityAlgorithmSuite.Basic192Rsa15) |
| | 0 | 618 | | result = CreateWsspAssertion(Basic192Rsa15Name); |
| | 6 | 619 | | else if (suite == SecurityAlgorithmSuite.Basic128Rsa15) |
| | 0 | 620 | | result = CreateWsspAssertion(Basic128Rsa15Name); |
| | 6 | 621 | | else if (suite == SecurityAlgorithmSuite.TripleDesRsa15) |
| | 0 | 622 | | result = CreateWsspAssertion(TripleDesRsa15Name); |
| | 6 | 623 | | else if (suite == SecurityAlgorithmSuite.Basic256Sha256) |
| | 2 | 624 | | result = CreateWsspAssertion(Basic256Sha256Name); |
| | 4 | 625 | | else if (suite == SecurityAlgorithmSuite.Basic192Sha256) |
| | 2 | 626 | | result = CreateWsspAssertion(Basic192Sha256Name); |
| | 2 | 627 | | else if (suite == SecurityAlgorithmSuite.Basic128Sha256) |
| | 2 | 628 | | result = CreateWsspAssertion(Basic128Sha256Name); |
| | 0 | 629 | | else if (suite == SecurityAlgorithmSuite.TripleDesSha256) |
| | 0 | 630 | | result = CreateWsspAssertion(TripleDesSha256Name); |
| | 0 | 631 | | else if (suite == SecurityAlgorithmSuite.Basic256Sha256Rsa15) |
| | 0 | 632 | | result = CreateWsspAssertion(Basic256Sha256Rsa15Name); |
| | 0 | 633 | | else if (suite == SecurityAlgorithmSuite.Basic192Sha256Rsa15) |
| | 0 | 634 | | result = CreateWsspAssertion(Basic192Sha256Rsa15Name); |
| | 0 | 635 | | else if (suite == SecurityAlgorithmSuite.Basic128Sha256Rsa15) |
| | 0 | 636 | | result = CreateWsspAssertion(Basic128Sha256Rsa15Name); |
| | 0 | 637 | | else if (suite == SecurityAlgorithmSuite.TripleDesSha256Rsa15) |
| | 0 | 638 | | result = CreateWsspAssertion(TripleDesSha256Rsa15Name); |
| | | 639 | | else |
| | | 640 | | { |
| | 0 | 641 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(suite)) |
| | | 642 | | } |
| | | 643 | | |
| | 22 | 644 | | return result; |
| | | 645 | | } |
| | | 646 | | |
| | | 647 | | public virtual XmlElement CreateWsspTransportTokenAssertion(MetadataExporter exporter, XmlElement transportToken |
| | | 648 | | { |
| | 22 | 649 | | if (transportTokenAssertion == null) |
| | | 650 | | { |
| | 0 | 651 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(transportTokenAssertion)); |
| | | 652 | | } |
| | | 653 | | |
| | 22 | 654 | | XmlElement result = CreateWsspAssertion(TransportTokenName); |
| | | 655 | | |
| | 22 | 656 | | result.AppendChild( |
| | 22 | 657 | | CreateWspPolicyWrapper( |
| | 22 | 658 | | exporter, |
| | 22 | 659 | | (XmlElement)(doc.ImportNode(transportTokenAssertion, true)) |
| | 22 | 660 | | )); |
| | | 661 | | |
| | 22 | 662 | | return result; |
| | | 663 | | } |
| | | 664 | | |
| | | 665 | | public virtual XmlElement CreateTokenAssertion(MetadataExporter exporter, SecurityTokenParameters parameters) |
| | | 666 | | { |
| | 19 | 667 | | return CreateTokenAssertion(exporter, parameters, false); |
| | | 668 | | } |
| | | 669 | | |
| | | 670 | | public virtual XmlElement CreateTokenAssertion(MetadataExporter exporter, SecurityTokenParameters parameters, bo |
| | | 671 | | { |
| | 19 | 672 | | if (parameters == null) |
| | | 673 | | { |
| | 0 | 674 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 675 | | } |
| | | 676 | | |
| | | 677 | | XmlElement result; |
| | | 678 | | |
| | | 679 | | /*if (parameters is KerberosSecurityTokenParameters) |
| | | 680 | | { |
| | | 681 | | result = CreateWsspKerberosTokenAssertion(exporter, (KerberosSecurityTokenParameters)parameters); |
| | | 682 | | } |
| | | 683 | | else */ |
| | | 684 | | |
| | 19 | 685 | | if (parameters is X509SecurityTokenParameters) |
| | | 686 | | { |
| | 8 | 687 | | result = CreateWsspX509TokenAssertion(exporter, (X509SecurityTokenParameters)parameters); |
| | | 688 | | } |
| | 11 | 689 | | else if (parameters is UserNameSecurityTokenParameters) |
| | | 690 | | { |
| | 1 | 691 | | result = CreateWsspUsernameTokenAssertion(exporter, (UserNameSecurityTokenParameters)parameters); |
| | | 692 | | } |
| | 10 | 693 | | else if (parameters is IssuedSecurityTokenParameters) |
| | | 694 | | { |
| | 0 | 695 | | result = CreateWsspIssuedTokenAssertion(exporter, (IssuedSecurityTokenParameters)parameters); |
| | | 696 | | } |
| | 10 | 697 | | else if (parameters is SspiSecurityTokenParameters) |
| | | 698 | | { |
| | 1 | 699 | | result = CreateWsspSpnegoContextTokenAssertion(exporter, (SspiSecurityTokenParameters)parameters); |
| | | 700 | | } |
| | 9 | 701 | | else if (parameters is SslSecurityTokenParameters) |
| | | 702 | | { |
| | 0 | 703 | | result = CreateMsspSslContextTokenAssertion(exporter, (SslSecurityTokenParameters)parameters); |
| | | 704 | | } |
| | 9 | 705 | | else if (parameters is SecureConversationSecurityTokenParameters) |
| | | 706 | | { |
| | 9 | 707 | | result = CreateWsspSecureConversationTokenAssertion(exporter, (SecureConversationSecurityTokenParameters |
| | | 708 | | } |
| | | 709 | | /*else if (parameters is RsaSecurityTokenParameters) // If this is enabled, CreateWsspRsaTokenAssertion need |
| | | 710 | | { |
| | | 711 | | result = CreateWsspRsaTokenAssertion((RsaSecurityTokenParameters)parameters); |
| | | 712 | | }*/ |
| | | 713 | | else |
| | | 714 | | { |
| | 0 | 715 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(paramet |
| | | 716 | | } |
| | | 717 | | |
| | 19 | 718 | | if (result != null && isOptional) |
| | | 719 | | { |
| | 0 | 720 | | result.SetAttribute(OptionalName, exporter.PolicyVersion.Namespace, TrueName); |
| | | 721 | | } |
| | | 722 | | |
| | 19 | 723 | | return result; |
| | | 724 | | } |
| | | 725 | | |
| | | 726 | | public virtual void SetIncludeTokenValue(XmlElement tokenAssertion, SecurityTokenInclusionMode inclusionMode) |
| | | 727 | | { |
| | | 728 | | switch (inclusionMode) |
| | | 729 | | { |
| | | 730 | | default: |
| | 0 | 731 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("inclusion |
| | | 732 | | case SecurityTokenInclusionMode.AlwaysToInitiator: |
| | 0 | 733 | | tokenAssertion.SetAttribute(IncludeTokenName, WsspNamespaceUri, AlwaysToInitiatorUri); |
| | 0 | 734 | | break; |
| | | 735 | | case SecurityTokenInclusionMode.AlwaysToRecipient: |
| | 19 | 736 | | tokenAssertion.SetAttribute(IncludeTokenName, WsspNamespaceUri, AlwaysToRecipientUri); |
| | 19 | 737 | | break; |
| | | 738 | | case SecurityTokenInclusionMode.Never: |
| | 0 | 739 | | tokenAssertion.SetAttribute(IncludeTokenName, WsspNamespaceUri, NeverUri); |
| | 0 | 740 | | break; |
| | | 741 | | case SecurityTokenInclusionMode.Once: |
| | 0 | 742 | | tokenAssertion.SetAttribute(IncludeTokenName, WsspNamespaceUri, OnceUri); |
| | | 743 | | break; |
| | | 744 | | } |
| | 0 | 745 | | } |
| | | 746 | | |
| | | 747 | | public virtual XmlElement CreateWsspRequireDerivedKeysAssertion(bool requireDerivedKeys) |
| | | 748 | | { |
| | 18 | 749 | | if (requireDerivedKeys) |
| | | 750 | | { |
| | 0 | 751 | | return CreateWsspAssertion(RequireDerivedKeysName); |
| | | 752 | | } |
| | | 753 | | else |
| | | 754 | | { |
| | 18 | 755 | | return null; |
| | | 756 | | } |
| | | 757 | | } |
| | | 758 | | |
| | | 759 | | public virtual XmlElement CreateX509ReferenceStyleAssertion(X509KeyIdentifierClauseType referenceStyle) |
| | | 760 | | { |
| | | 761 | | switch (referenceStyle) |
| | | 762 | | { |
| | | 763 | | default: |
| | 0 | 764 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(ref |
| | | 765 | | case X509KeyIdentifierClauseType.IssuerSerial: |
| | 0 | 766 | | _mustSupportRefIssuerSerialName = true; |
| | 0 | 767 | | return CreateWsspAssertion(RequireIssuerSerialReferenceName); |
| | | 768 | | case X509KeyIdentifierClauseType.SubjectKeyIdentifier: |
| | 0 | 769 | | _mustSupportRefKeyIdentifierName = true; |
| | 0 | 770 | | return CreateWsspAssertion(RequireKeyIdentifierReferenceName); |
| | | 771 | | case X509KeyIdentifierClauseType.Thumbprint: |
| | 8 | 772 | | _mustSupportRefThumbprintName = true; |
| | 8 | 773 | | return CreateWsspAssertion(RequireThumbprintReferenceName); |
| | | 774 | | case X509KeyIdentifierClauseType.Any: |
| | 0 | 775 | | _mustSupportRefIssuerSerialName = true; |
| | 0 | 776 | | _mustSupportRefKeyIdentifierName = true; |
| | 0 | 777 | | _mustSupportRefThumbprintName = true; |
| | 0 | 778 | | return null; |
| | | 779 | | } |
| | | 780 | | } |
| | | 781 | | |
| | | 782 | | public virtual XmlElement CreateWsspX509TokenAssertion(MetadataExporter exporter, X509SecurityTokenParameters pa |
| | | 783 | | { |
| | 8 | 784 | | XmlElement result = CreateWsspAssertion(X509TokenName); |
| | 8 | 785 | | SetIncludeTokenValue(result, parameters.InclusionMode); |
| | 8 | 786 | | result.AppendChild( |
| | 8 | 787 | | CreateWspPolicyWrapper( |
| | 8 | 788 | | exporter, |
| | 8 | 789 | | CreateWsspRequireDerivedKeysAssertion(parameters.RequireDerivedKeys), |
| | 8 | 790 | | CreateX509ReferenceStyleAssertion(parameters.X509ReferenceStyle), |
| | 8 | 791 | | CreateWsspAssertion(WssX509V3Token10Name) |
| | 8 | 792 | | )); |
| | 8 | 793 | | return result; |
| | | 794 | | } |
| | | 795 | | |
| | | 796 | | public virtual XmlElement CreateWsspUsernameTokenAssertion(MetadataExporter exporter, UserNameSecurityTokenParam |
| | | 797 | | { |
| | 1 | 798 | | XmlElement result = CreateWsspAssertion(UsernameTokenName); |
| | 1 | 799 | | SetIncludeTokenValue(result, parameters.InclusionMode); |
| | 1 | 800 | | result.AppendChild( |
| | 1 | 801 | | CreateWspPolicyWrapper( |
| | 1 | 802 | | exporter, |
| | 1 | 803 | | CreateWsspAssertion(WssUsernameToken10Name) |
| | 1 | 804 | | )); |
| | 1 | 805 | | return result; |
| | | 806 | | } |
| | | 807 | | |
| | | 808 | | public virtual XmlElement CreateReferenceStyleAssertion(SecurityTokenReferenceStyle referenceStyle) |
| | | 809 | | { |
| | | 810 | | switch (referenceStyle) |
| | | 811 | | { |
| | | 812 | | default: |
| | 0 | 813 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(ref |
| | | 814 | | case SecurityTokenReferenceStyle.External: |
| | 0 | 815 | | return CreateWsspAssertion(RequireExternalReferenceName); |
| | | 816 | | case SecurityTokenReferenceStyle.Internal: |
| | 0 | 817 | | return CreateWsspAssertion(RequireInternalReferenceName); |
| | | 818 | | } |
| | | 819 | | } |
| | | 820 | | |
| | | 821 | | public virtual XmlElement CreateWsspIssuerElement(EndpointAddress issuerAddress, EndpointAddress issuerMetadataA |
| | | 822 | | { |
| | | 823 | | XmlElement result; |
| | 0 | 824 | | if (issuerAddress == null && issuerMetadataAddress == null) |
| | | 825 | | { |
| | 0 | 826 | | result = null; |
| | | 827 | | } |
| | | 828 | | else |
| | | 829 | | { |
| | | 830 | | EndpointAddress addressToSerialize; |
| | 0 | 831 | | addressToSerialize = issuerAddress == null ? EndpointAddress.AnonymousAddress : issuerAddress; |
| | | 832 | | |
| | | 833 | | MemoryStream stream; |
| | | 834 | | XmlWriter writer; |
| | | 835 | | |
| | 0 | 836 | | if (issuerMetadataAddress != null) |
| | | 837 | | { |
| | 0 | 838 | | MetadataSet metadataSet = new MetadataSet(); |
| | 0 | 839 | | metadataSet.MetadataSections.Add(new MetadataSection(null, null, new MetadataReference(issuerMetadat |
| | | 840 | | |
| | 0 | 841 | | stream = new MemoryStream(); |
| | 0 | 842 | | writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8); |
| | 0 | 843 | | metadataSet.WriteTo(XmlDictionaryWriter.CreateDictionaryWriter(writer)); |
| | 0 | 844 | | writer.Flush(); |
| | 0 | 845 | | stream.Seek(0, SeekOrigin.Begin); |
| | | 846 | | |
| | 0 | 847 | | addressToSerialize = new EndpointAddress( |
| | 0 | 848 | | addressToSerialize.Uri, |
| | 0 | 849 | | addressToSerialize.Identity, |
| | 0 | 850 | | addressToSerialize.Headers, |
| | 0 | 851 | | XmlDictionaryReader.CreateDictionaryReader(XmlReader.Create(stream)), |
| | 0 | 852 | | addressToSerialize.GetReaderAtExtensions()); |
| | | 853 | | } |
| | | 854 | | |
| | 0 | 855 | | stream = new MemoryStream(); |
| | 0 | 856 | | writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8); |
| | 0 | 857 | | writer.WriteStartElement(IssuerName, WsspNamespaceUri); |
| | 0 | 858 | | addressToSerialize.WriteContentsTo(AddressingVersion.WSAddressing10, writer); |
| | 0 | 859 | | writer.WriteEndElement(); |
| | 0 | 860 | | writer.Flush(); |
| | 0 | 861 | | stream.Seek(0, SeekOrigin.Begin); |
| | 0 | 862 | | result = (XmlElement)doc.ReadNode(new XmlTextReader(stream) { DtdProcessing = DtdProcessing.Prohibit }); |
| | | 863 | | } |
| | 0 | 864 | | return result; |
| | | 865 | | } |
| | | 866 | | |
| | | 867 | | public virtual XmlElement CreateWsspIssuedTokenAssertion(MetadataExporter exporter, IssuedSecurityTokenParameter |
| | | 868 | | { |
| | 0 | 869 | | XmlElement result = CreateWsspAssertion(IssuedTokenName); |
| | 0 | 870 | | SetIncludeTokenValue(result, parameters.InclusionMode); |
| | 0 | 871 | | XmlElement issuerAssertion = CreateWsspIssuerElement(parameters.IssuerAddress, parameters.IssuerMetadataAddr |
| | 0 | 872 | | if (issuerAssertion != null) |
| | | 873 | | { |
| | 0 | 874 | | result.AppendChild(issuerAssertion); |
| | | 875 | | } |
| | 0 | 876 | | XmlElement tokenTemplate = CreateWsspAssertion(RequestSecurityTokenTemplateName); |
| | 0 | 877 | | TrustDriver driver = TrustDriver; |
| | 0 | 878 | | foreach (XmlElement p in parameters.CreateRequestParameters(driver)) |
| | | 879 | | { |
| | 0 | 880 | | tokenTemplate.AppendChild(doc.ImportNode(p, true)); |
| | | 881 | | } |
| | 0 | 882 | | result.AppendChild(tokenTemplate); |
| | 0 | 883 | | result.AppendChild( |
| | 0 | 884 | | CreateWspPolicyWrapper( |
| | 0 | 885 | | exporter, |
| | 0 | 886 | | CreateWsspRequireDerivedKeysAssertion(parameters.RequireDerivedKeys), |
| | 0 | 887 | | CreateReferenceStyleAssertion(parameters.ReferenceStyle) |
| | 0 | 888 | | )); |
| | 0 | 889 | | return result; |
| | | 890 | | } |
| | | 891 | | |
| | | 892 | | public virtual XmlElement CreateWsspMustNotSendCancelAssertion(bool requireCancel) |
| | | 893 | | { |
| | 0 | 894 | | if (!requireCancel) |
| | | 895 | | { |
| | 0 | 896 | | XmlElement result = CreateWsspAssertion(MustNotSendCancelName); |
| | 0 | 897 | | return result; |
| | | 898 | | } |
| | | 899 | | else |
| | | 900 | | { |
| | 0 | 901 | | return null; |
| | | 902 | | } |
| | | 903 | | } |
| | | 904 | | |
| | | 905 | | public virtual XmlElement CreateWsspSpnegoContextTokenAssertion(MetadataExporter exporter, SspiSecurityTokenPara |
| | | 906 | | { |
| | 1 | 907 | | XmlElement result = CreateWsspAssertion(SpnegoContextTokenName); |
| | 1 | 908 | | SetIncludeTokenValue(result, parameters.InclusionMode); |
| | 1 | 909 | | result.AppendChild( |
| | 1 | 910 | | CreateWspPolicyWrapper( |
| | 1 | 911 | | exporter, |
| | 1 | 912 | | CreateWsspRequireDerivedKeysAssertion(parameters.RequireDerivedKeys), |
| | 1 | 913 | | CreateWsspMustNotSendCancelAssertion(parameters.RequireCancellation) |
| | 1 | 914 | | )); |
| | 1 | 915 | | return result; |
| | | 916 | | } |
| | | 917 | | |
| | | 918 | | public virtual XmlElement CreateMsspRequireClientCertificateAssertion(bool requireClientCertificate) |
| | | 919 | | { |
| | 0 | 920 | | if (requireClientCertificate) |
| | | 921 | | { |
| | 0 | 922 | | return CreateMsspAssertion(RequireClientCertificateName); |
| | | 923 | | } |
| | | 924 | | else |
| | | 925 | | { |
| | 0 | 926 | | return null; |
| | | 927 | | } |
| | | 928 | | } |
| | | 929 | | |
| | | 930 | | public virtual XmlElement CreateMsspSslContextTokenAssertion(MetadataExporter exporter, SslSecurityTokenParamete |
| | | 931 | | { |
| | 0 | 932 | | XmlElement result = CreateMsspAssertion(SslContextTokenName); |
| | 0 | 933 | | SetIncludeTokenValue(result, parameters.InclusionMode); |
| | 0 | 934 | | result.AppendChild( |
| | 0 | 935 | | CreateWspPolicyWrapper( |
| | 0 | 936 | | exporter, |
| | 0 | 937 | | CreateWsspRequireDerivedKeysAssertion(parameters.RequireDerivedKeys), |
| | 0 | 938 | | CreateWsspMustNotSendCancelAssertion(parameters.RequireCancellation), |
| | 0 | 939 | | CreateMsspRequireClientCertificateAssertion(parameters.RequireClientCertificate) |
| | 0 | 940 | | )); |
| | 0 | 941 | | return result; |
| | | 942 | | } |
| | | 943 | | |
| | | 944 | | public virtual XmlElement CreateWsspBootstrapPolicyAssertion(MetadataExporter exporter, SecurityBindingElement b |
| | | 945 | | { |
| | 9 | 946 | | if (bootstrapSecurity == null) |
| | 0 | 947 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(bootstrapSecurity)); |
| | | 948 | | |
| | 9 | 949 | | WSSecurityPolicy sp = WSSecurityPolicy.GetSecurityPolicyDriver(bootstrapSecurity.MessageSecurityVersion); |
| | | 950 | | |
| | | 951 | | // create complete bootstrap binding |
| | | 952 | | |
| | 9 | 953 | | CustomBinding bootstrapBinding = new CustomBinding(bootstrapSecurity); |
| | 9 | 954 | | if (exporter.State.ContainsKey(SecurityPolicyStrings.SecureConversationBootstrapBindingElementsBelowSecurity |
| | | 955 | | { |
| | 9 | 956 | | BindingElementCollection bindingElementsBelowSecurity = exporter.State[SecurityPolicyStrings.SecureConve |
| | 9 | 957 | | if (bindingElementsBelowSecurity != null) |
| | | 958 | | { |
| | 54 | 959 | | foreach (BindingElement be in bindingElementsBelowSecurity) |
| | | 960 | | { |
| | 18 | 961 | | bootstrapBinding.Elements.Add(be); |
| | | 962 | | } |
| | | 963 | | } |
| | | 964 | | } |
| | | 965 | | |
| | | 966 | | // generate policy for the "how" of security |
| | | 967 | | |
| | 9 | 968 | | ServiceEndpoint bootstrapEndpoint = new ServiceEndpoint(NullContract); |
| | 9 | 969 | | bootstrapEndpoint.Binding = bootstrapBinding; |
| | 9 | 970 | | PolicyConversionContext policyContext = exporter.ExportPolicy(bootstrapEndpoint); |
| | | 971 | | |
| | | 972 | | // generate policy for the "what" of security (protection assertions) |
| | | 973 | | |
| | | 974 | | // hard-coded requirements in V1: sign and encrypt RST and RSTR body |
| | 9 | 975 | | ChannelProtectionRequirements bootstrapProtection = new ChannelProtectionRequirements(); |
| | 9 | 976 | | bootstrapProtection.IncomingEncryptionParts.AddParts(new MessagePartSpecification(true)); |
| | 9 | 977 | | bootstrapProtection.OutgoingEncryptionParts.AddParts(new MessagePartSpecification(true)); |
| | 9 | 978 | | bootstrapProtection.IncomingSignatureParts.AddParts(new MessagePartSpecification(true)); |
| | 9 | 979 | | bootstrapProtection.OutgoingSignatureParts.AddParts(new MessagePartSpecification(true)); |
| | | 980 | | |
| | | 981 | | // add boostrap binding protection requirements (e.g. addressing headers) |
| | 9 | 982 | | ChannelProtectionRequirements cpr = bootstrapBinding.GetProperty<ChannelProtectionRequirements>(new BindingP |
| | 9 | 983 | | if (cpr != null) |
| | | 984 | | { |
| | 9 | 985 | | bootstrapProtection.Add(cpr); |
| | | 986 | | } |
| | | 987 | | |
| | | 988 | | // extract channel-scope protection requirements and union them across request and response |
| | 9 | 989 | | MessagePartSpecification encryption = new MessagePartSpecification(); |
| | 9 | 990 | | encryption.Union(bootstrapProtection.IncomingEncryptionParts.ChannelParts); |
| | 9 | 991 | | encryption.Union(bootstrapProtection.OutgoingEncryptionParts.ChannelParts); |
| | 9 | 992 | | encryption.MakeReadOnly(); |
| | 9 | 993 | | MessagePartSpecification signature = new MessagePartSpecification(); |
| | 9 | 994 | | signature.Union(bootstrapProtection.IncomingSignatureParts.ChannelParts); |
| | 9 | 995 | | signature.Union(bootstrapProtection.OutgoingSignatureParts.ChannelParts); |
| | 9 | 996 | | signature.MakeReadOnly(); |
| | | 997 | | |
| | | 998 | | // create final boostrap policy assertion |
| | | 999 | | |
| | 9 | 1000 | | XmlElement nestedPolicy = CreateWspPolicyWrapper( |
| | 9 | 1001 | | exporter, |
| | 9 | 1002 | | sp.CreateWsspSignedPartsAssertion(signature), |
| | 9 | 1003 | | sp.CreateWsspEncryptedPartsAssertion(encryption)); |
| | 90 | 1004 | | foreach (XmlElement e in sp.FilterWsspPolicyAssertions(policyContext.GetBindingAssertions())) |
| | | 1005 | | { |
| | 36 | 1006 | | nestedPolicy.AppendChild(e); |
| | | 1007 | | } |
| | 9 | 1008 | | XmlElement result = CreateWsspAssertion(BootstrapPolicyName); |
| | 9 | 1009 | | result.AppendChild(nestedPolicy); |
| | | 1010 | | |
| | 9 | 1011 | | return result; |
| | | 1012 | | } |
| | | 1013 | | |
| | | 1014 | | public virtual ICollection<XmlElement> FilterWsspPolicyAssertions(ICollection<XmlElement> policyAssertions) |
| | | 1015 | | { |
| | 9 | 1016 | | Collection<XmlElement> result = new Collection<XmlElement>(); |
| | | 1017 | | |
| | 108 | 1018 | | foreach (XmlElement assertion in policyAssertions) |
| | 45 | 1019 | | if (IsWsspAssertion(assertion)) |
| | 36 | 1020 | | result.Add(assertion); |
| | | 1021 | | |
| | 9 | 1022 | | return result; |
| | | 1023 | | } |
| | | 1024 | | |
| | | 1025 | | public virtual XmlElement CreateWsspSecureConversationTokenAssertion(MetadataExporter exporter, SecureConversati |
| | | 1026 | | { |
| | 9 | 1027 | | XmlElement result = CreateWsspAssertion(SecureConversationTokenName); |
| | 9 | 1028 | | SetIncludeTokenValue(result, parameters.InclusionMode); |
| | 9 | 1029 | | result.AppendChild( |
| | 9 | 1030 | | CreateWspPolicyWrapper( |
| | 9 | 1031 | | exporter, |
| | 9 | 1032 | | CreateWsspRequireDerivedKeysAssertion(parameters.RequireDerivedKeys), |
| | 9 | 1033 | | CreateWsspMustNotSendCancelAssertion(parameters.RequireCancellation), |
| | 9 | 1034 | | CreateWsspBootstrapPolicyAssertion(exporter, parameters.BootstrapSecurityBindingElement) |
| | 9 | 1035 | | )); |
| | 9 | 1036 | | return result; |
| | | 1037 | | } |
| | | 1038 | | |
| | | 1039 | | public static WSSecurityPolicy GetSecurityPolicyDriver(MessageSecurityVersion version) |
| | | 1040 | | { |
| | 50 | 1041 | | SecurityPolicyManager policyManager = new SecurityPolicyManager(); |
| | 50 | 1042 | | return policyManager.GetSecurityPolicyDriver(version); |
| | | 1043 | | } |
| | | 1044 | | |
| | | 1045 | | private class SecurityPolicyManager |
| | | 1046 | | { |
| | | 1047 | | private readonly List<WSSecurityPolicy> _drivers; |
| | | 1048 | | |
| | 50 | 1049 | | public SecurityPolicyManager() |
| | | 1050 | | { |
| | 50 | 1051 | | _drivers = new List<WSSecurityPolicy>(); |
| | 50 | 1052 | | Initialize(); |
| | 50 | 1053 | | } |
| | | 1054 | | |
| | | 1055 | | public void Initialize() |
| | | 1056 | | { |
| | 50 | 1057 | | _drivers.Add(new WSSecurityPolicy11()); |
| | 50 | 1058 | | _drivers.Add(new WSSecurityPolicy12()); |
| | 50 | 1059 | | } |
| | | 1060 | | |
| | | 1061 | | public WSSecurityPolicy GetSecurityPolicyDriver(MessageSecurityVersion version) |
| | | 1062 | | { |
| | 100 | 1063 | | for (int i = 0; i < _drivers.Count; ++i) |
| | | 1064 | | { |
| | 50 | 1065 | | if (_drivers[i].IsSecurityVersionSupported(version)) |
| | | 1066 | | { |
| | 50 | 1067 | | return _drivers[i]; |
| | | 1068 | | } |
| | | 1069 | | } |
| | | 1070 | | |
| | 0 | 1071 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 1072 | | } |
| | | 1073 | | |
| | | 1074 | | } |
| | | 1075 | | } |
| | | 1076 | | |
| | | 1077 | | internal static class SecurityPolicyStrings |
| | | 1078 | | { |
| | | 1079 | | public const string SecureConversationBootstrapBindingElementsBelowSecurityKey = "SecureConversationBootstrapBin |
| | | 1080 | | } |
| | | 1081 | | } |