| | | 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.ComponentModel; |
| | | 5 | | using System.Net.Security; |
| | | 6 | | using System.Security.Authentication; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Description; |
| | | 9 | | using CoreWCF.Security; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels |
| | | 12 | | { |
| | | 13 | | public class SslStreamSecurityBindingElement : StreamUpgradeBindingElement, ITransportTokenAssertionProvider, IPolic |
| | | 14 | | { |
| | | 15 | | private IdentityVerifier _identityVerifier; |
| | | 16 | | private SslProtocols _sslProtocols; |
| | | 17 | | |
| | 106 | 18 | | public SslStreamSecurityBindingElement() |
| | | 19 | | { |
| | 106 | 20 | | RequireClientCertificate = TransportDefaults.RequireClientCertificate; |
| | 106 | 21 | | _sslProtocols = TransportDefaults.SslProtocols; |
| | 106 | 22 | | } |
| | | 23 | | |
| | | 24 | | protected SslStreamSecurityBindingElement(SslStreamSecurityBindingElement elementToBeCloned) |
| | 104 | 25 | | : base(elementToBeCloned) |
| | | 26 | | { |
| | 104 | 27 | | _identityVerifier = elementToBeCloned._identityVerifier; |
| | 104 | 28 | | RequireClientCertificate = elementToBeCloned.RequireClientCertificate; |
| | 104 | 29 | | _sslProtocols = elementToBeCloned._sslProtocols; |
| | 104 | 30 | | } |
| | | 31 | | |
| | | 32 | | internal IdentityVerifier IdentityVerifier |
| | | 33 | | { |
| | | 34 | | get |
| | | 35 | | { |
| | 5 | 36 | | if (_identityVerifier == null) |
| | | 37 | | { |
| | 5 | 38 | | _identityVerifier = IdentityVerifier.CreateDefault(); |
| | | 39 | | } |
| | | 40 | | |
| | 5 | 41 | | return _identityVerifier; |
| | | 42 | | } |
| | | 43 | | set |
| | | 44 | | { |
| | 0 | 45 | | _identityVerifier = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(val |
| | 0 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | [DefaultValue(TransportDefaults.RequireClientCertificate)] |
| | 435 | 50 | | public bool RequireClientCertificate { get; set; } |
| | | 51 | | |
| | | 52 | | [DefaultValue(TransportDefaults.SslProtocols)] |
| | | 53 | | public SslProtocols SslProtocols |
| | | 54 | | { |
| | | 55 | | get |
| | | 56 | | { |
| | 5 | 57 | | return _sslProtocols; |
| | | 58 | | } |
| | | 59 | | set |
| | | 60 | | { |
| | 106 | 61 | | SslProtocolsHelper.Validate(value); |
| | 106 | 62 | | _sslProtocols = value; |
| | 106 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public override BindingElement Clone() |
| | | 67 | | { |
| | 104 | 68 | | return new SslStreamSecurityBindingElement(this); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public override T GetProperty<T>(BindingContext context) |
| | | 72 | | { |
| | 24 | 73 | | if (context == null) |
| | | 74 | | { |
| | 0 | 75 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context)); |
| | | 76 | | } |
| | 24 | 77 | | if (typeof(T) == typeof(ISecurityCapabilities)) |
| | | 78 | | { |
| | 5 | 79 | | return (T)(object)new SecurityCapabilities(RequireClientCertificate, true, RequireClientCertificate, |
| | 5 | 80 | | ProtectionLevel.EncryptAndSign, ProtectionLevel.EncryptAndSign); |
| | | 81 | | } |
| | 19 | 82 | | else if (typeof(T) == typeof(IdentityVerifier)) |
| | | 83 | | { |
| | 0 | 84 | | return (T)(object)IdentityVerifier; |
| | | 85 | | } |
| | | 86 | | else |
| | | 87 | | { |
| | 19 | 88 | | return context.GetInnerProperty<T>(); |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public override StreamUpgradeProvider BuildServerStreamUpgradeProvider(BindingContext context) |
| | | 93 | | { |
| | 5 | 94 | | return SslStreamSecurityUpgradeProvider.CreateServerProvider(this, context); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | #region ITransportTokenAssertionProvider Members |
| | | 98 | | |
| | | 99 | | public XmlElement GetTransportTokenAssertion() |
| | | 100 | | { |
| | 0 | 101 | | XmlDocument document = new XmlDocument(); |
| | 0 | 102 | | XmlElement assertion = |
| | 0 | 103 | | document.CreateElement(TransportPolicyConstants.DotNetFramingPrefix, |
| | 0 | 104 | | TransportPolicyConstants.SslTransportSecurityName, |
| | 0 | 105 | | TransportPolicyConstants.DotNetFramingNamespace); |
| | 0 | 106 | | if (RequireClientCertificate) |
| | | 107 | | { |
| | 0 | 108 | | assertion.AppendChild(document.CreateElement(TransportPolicyConstants.DotNetFramingPrefix, |
| | 0 | 109 | | TransportPolicyConstants.RequireClientCertificateName, |
| | 0 | 110 | | TransportPolicyConstants.DotNetFramingNamespace)); |
| | | 111 | | } |
| | 0 | 112 | | return assertion; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | #endregion |
| | | 116 | | |
| | | 117 | | void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext context) |
| | | 118 | | { |
| | 0 | 119 | | if (exporter == null) |
| | | 120 | | { |
| | 0 | 121 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(exporter)); |
| | | 122 | | } |
| | 0 | 123 | | if (context == null) |
| | | 124 | | { |
| | 0 | 125 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context)); |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | SecurityBindingElement.ExportPolicyForTransportTokenAssertionProviders(exporter, context); |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | protected override bool IsMatch(BindingElement b) |
| | | 132 | | { |
| | 0 | 133 | | if (b == null) |
| | | 134 | | { |
| | 0 | 135 | | return false; |
| | | 136 | | } |
| | 0 | 137 | | if (!(b is SslStreamSecurityBindingElement ssl)) |
| | | 138 | | { |
| | 0 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | return RequireClientCertificate == ssl.RequireClientCertificate && _sslProtocols == ssl._sslProtocols; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | private static class TransportPolicyConstants |
| | | 146 | | { |
| | | 147 | | public const string DotNetFramingNamespace = Framing.FramingEncodingString.NamespaceUri + "/policy"; |
| | | 148 | | public const string DotNetFramingPrefix = "msf"; |
| | | 149 | | public const string RequireClientCertificateName = "RequireClientCertificate"; |
| | | 150 | | public const string SslTransportSecurityName = "SslTransportSecurity"; |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | } |