| | | 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.Net; |
| | | 6 | | using System.Security.Authentication.ExtendedProtection; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF |
| | | 10 | | { |
| | | 11 | | public sealed class HttpTransportSecurity |
| | | 12 | | { |
| | | 13 | | internal const HttpClientCredentialType DefaultClientCredentialType = HttpClientCredentialType.None; |
| | | 14 | | internal const string DefaultRealm = HttpTransportDefaults.Realm; |
| | | 15 | | private HttpClientCredentialType _clientCredentialType; |
| | | 16 | | private ExtendedProtectionPolicy _extendedProtectionPolicy; |
| | | 17 | | |
| | 613 | 18 | | public HttpTransportSecurity() |
| | | 19 | | { |
| | 613 | 20 | | _clientCredentialType = DefaultClientCredentialType; |
| | 613 | 21 | | Realm = DefaultRealm; |
| | 613 | 22 | | _extendedProtectionPolicy = ChannelBindingUtility.DefaultPolicy; |
| | 613 | 23 | | } |
| | | 24 | | |
| | | 25 | | public HttpClientCredentialType ClientCredentialType |
| | | 26 | | { |
| | 6178 | 27 | | get { return _clientCredentialType; } |
| | | 28 | | set |
| | | 29 | | { |
| | 167 | 30 | | if (!HttpClientCredentialTypeHelper.IsDefined(value)) |
| | | 31 | | { |
| | 0 | 32 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 33 | | } |
| | 167 | 34 | | _clientCredentialType = value; |
| | 167 | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | 5986 | 38 | | public bool AlwaysUseAuthorizationPolicySupport { get; set; } |
| | | 39 | | |
| | 2282 | 40 | | public string Realm { get; set; } |
| | | 41 | | |
| | | 42 | | public ExtendedProtectionPolicy ExtendedProtectionPolicy |
| | | 43 | | { |
| | | 44 | | get |
| | | 45 | | { |
| | 306 | 46 | | return _extendedProtectionPolicy; |
| | | 47 | | } |
| | | 48 | | set |
| | | 49 | | { |
| | 0 | 50 | | if (value == null) |
| | | 51 | | { |
| | 0 | 52 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | if (value.PolicyEnforcement == PolicyEnforcement.Always && |
| | 0 | 56 | | !ExtendedProtectionPolicy.OSSupportsExtendedProtection) |
| | | 57 | | { |
| | 0 | 58 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 59 | | new PlatformNotSupportedException(SR.ExtendedProtectionNotSupported)); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | _extendedProtectionPolicy = value; |
| | 0 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | internal void ConfigureTransportProtectionOnly(HttpsTransportBindingElement https) |
| | | 67 | | { |
| | 218 | 68 | | DisableAuthentication(https); |
| | 218 | 69 | | https.RequireClientCertificate = false; |
| | 218 | 70 | | } |
| | | 71 | | |
| | | 72 | | private void ConfigureAuthentication(HttpTransportBindingElement http) |
| | | 73 | | { |
| | 1513 | 74 | | http.AuthenticationScheme = HttpClientCredentialTypeHelper.MapToAuthenticationScheme(_clientCredentialType); |
| | 1513 | 75 | | http.Realm = Realm; |
| | 1513 | 76 | | http.ExtendedProtectionPolicy = _extendedProtectionPolicy; |
| | 1513 | 77 | | } |
| | | 78 | | |
| | | 79 | | private static void ConfigureAuthentication(HttpTransportBindingElement http, HttpTransportSecurity transportSec |
| | | 80 | | { |
| | 0 | 81 | | transportSecurity._clientCredentialType = HttpClientCredentialTypeHelper.MapToClientCredentialType(http.Auth |
| | 0 | 82 | | transportSecurity.Realm = http.Realm; |
| | 0 | 83 | | transportSecurity._extendedProtectionPolicy = http.ExtendedProtectionPolicy; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | private void DisableAuthentication(HttpTransportBindingElement http) |
| | | 87 | | { |
| | 3690 | 88 | | http.AuthenticationScheme = AuthenticationSchemes.Anonymous; |
| | 3690 | 89 | | http.Realm = DefaultRealm; |
| | | 90 | | //ExtendedProtectionPolicy is always copied - even for security mode None, Message and TransportWithMessageC |
| | | 91 | | //because the settings for ExtendedProtectionPolicy are always below the <security><transport> element |
| | | 92 | | //http.ExtendedProtectionPolicy = extendedProtectionPolicy; |
| | 3690 | 93 | | } |
| | | 94 | | |
| | | 95 | | //static bool IsDisabledAuthentication(HttpTransportBindingElement http) |
| | | 96 | | //{ |
| | | 97 | | // return http.AuthenticationScheme == AuthenticationSchemes.Anonymous && http.ProxyAuthenticationScheme == A |
| | | 98 | | //} |
| | | 99 | | |
| | | 100 | | internal void ConfigureTransportProtectionAndAuthentication(HttpsTransportBindingElement https) |
| | | 101 | | { |
| | 539 | 102 | | ConfigureAuthentication(https); |
| | 539 | 103 | | https.RequireClientCertificate = (_clientCredentialType == HttpClientCredentialType.Certificate); |
| | 539 | 104 | | } |
| | | 105 | | |
| | | 106 | | internal static void ConfigureTransportProtectionAndAuthentication(HttpsTransportBindingElement https, HttpTrans |
| | | 107 | | { |
| | 0 | 108 | | ConfigureAuthentication(https, transportSecurity); |
| | 0 | 109 | | if (https.RequireClientCertificate) |
| | | 110 | | { |
| | 0 | 111 | | transportSecurity.ClientCredentialType = HttpClientCredentialType.Certificate; |
| | | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | internal void ConfigureTransportAuthentication(HttpTransportBindingElement http) |
| | | 116 | | { |
| | 974 | 117 | | if (_clientCredentialType == HttpClientCredentialType.Certificate) |
| | | 118 | | { |
| | 0 | 119 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.CertificateUn |
| | | 120 | | } |
| | | 121 | | |
| | 974 | 122 | | ConfigureAuthentication(http); |
| | 974 | 123 | | } |
| | | 124 | | |
| | | 125 | | //internal static bool IsConfiguredTransportAuthentication(HttpTransportBindingElement http, HttpTransportSecuri |
| | | 126 | | //{ |
| | | 127 | | // if (HttpClientCredentialTypeHelper.MapToClientCredentialType(http.AuthenticationScheme) == HttpClientCrede |
| | | 128 | | // return false; |
| | | 129 | | // ConfigureAuthentication(http, transportSecurity); |
| | | 130 | | // return true; |
| | | 131 | | //} |
| | | 132 | | |
| | | 133 | | internal void DisableTransportAuthentication(HttpTransportBindingElement http) |
| | | 134 | | { |
| | 3472 | 135 | | DisableAuthentication(http); |
| | 3472 | 136 | | } |
| | | 137 | | |
| | | 138 | | //internal static bool IsDisabledTransportAuthentication(HttpTransportBindingElement http) |
| | | 139 | | //{ |
| | | 140 | | // return IsDisabledAuthentication(http); |
| | | 141 | | //} |
| | | 142 | | } |
| | | 143 | | } |