| | | 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.ObjectModel; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Net; |
| | | 8 | | using System.Net.Security; |
| | | 9 | | using System.Security.Authentication; |
| | | 10 | | using System.Security.Claims; |
| | | 11 | | using System.Security.Principal; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | using CoreWCF.Channels.Framing; |
| | | 15 | | using CoreWCF.Description; |
| | | 16 | | using CoreWCF.IdentityModel; |
| | | 17 | | using CoreWCF.IdentityModel.Policy; |
| | | 18 | | using CoreWCF.IdentityModel.Selectors; |
| | | 19 | | using CoreWCF.IdentityModel.Tokens; |
| | | 20 | | using CoreWCF.Security; |
| | | 21 | | |
| | | 22 | | namespace CoreWCF.Channels |
| | | 23 | | { |
| | | 24 | | internal class WindowsStreamSecurityUpgradeProvider : StreamSecurityUpgradeProvider |
| | | 25 | | { |
| | | 26 | | private EndpointIdentity _identity; |
| | | 27 | | private readonly SecurityTokenManager _securityTokenManager; |
| | | 28 | | private readonly Uri _listenUri; |
| | | 29 | | |
| | | 30 | | public WindowsStreamSecurityUpgradeProvider(WindowsStreamSecurityBindingElement bindingElement, BindingContext c |
| | 12 | 31 | | : base(context.Binding) |
| | | 32 | | { |
| | 12 | 33 | | ExtractGroupsForWindowsAccounts = TransportDefaults.ExtractGroupsForWindowsAccounts; |
| | 12 | 34 | | ProtectionLevel = bindingElement.ProtectionLevel; |
| | 12 | 35 | | Scheme = context.Binding.Scheme; |
| | 12 | 36 | | _listenUri = TransportSecurityHelpers.GetListenUri(context.ListenUriBaseAddress, context.ListenUriRelativeAd |
| | | 37 | | |
| | 12 | 38 | | SecurityCredentialsManager credentialProvider = context.BindingParameters.Find<SecurityCredentialsManager>() |
| | 12 | 39 | | if (credentialProvider == null) |
| | | 40 | | { |
| | | 41 | | //if (isClient) |
| | | 42 | | //{ |
| | | 43 | | // credentialProvider = ClientCredentials.CreateDefaultCredentials(); |
| | | 44 | | //} |
| | | 45 | | //else |
| | | 46 | | //{ |
| | 12 | 47 | | credentialProvider = new ServiceCredentials(); //ServiceCredentials.CreateDefaultCredentials(); |
| | | 48 | | //} |
| | | 49 | | } |
| | | 50 | | |
| | 12 | 51 | | if(credentialProvider is ServiceCredentials) |
| | | 52 | | { |
| | 12 | 53 | | ServiceCredentials serviceCred = (ServiceCredentials)credentialProvider; |
| | 12 | 54 | | LdapSettings = serviceCred.WindowsAuthentication.LdapSetting; |
| | | 55 | | } |
| | 12 | 56 | | _securityTokenManager = credentialProvider.CreateSecurityTokenManager(); |
| | 12 | 57 | | } |
| | | 58 | | |
| | 12 | 59 | | public string Scheme { get; } |
| | | 60 | | |
| | 24 | 61 | | internal bool ExtractGroupsForWindowsAccounts { get; private set; } |
| | | 62 | | |
| | | 63 | | public override EndpointIdentity Identity |
| | | 64 | | { |
| | | 65 | | get |
| | | 66 | | { |
| | | 67 | | // If the server credential is null, then we have not been opened yet and have no identity to expose. |
| | 12 | 68 | | if (ServerCredential != null) |
| | | 69 | | { |
| | 12 | 70 | | if (_identity == null) |
| | | 71 | | { |
| | 12 | 72 | | lock (ThisLock) |
| | | 73 | | { |
| | 12 | 74 | | if (_identity == null) |
| | | 75 | | { |
| | 12 | 76 | | _identity = SecurityUtils.CreateWindowsIdentity(ServerCredential); |
| | | 77 | | } |
| | 12 | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |
| | 12 | 81 | | return _identity; |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | 24 | 85 | | internal IdentityVerifier IdentityVerifier { get; private set; } |
| | | 86 | | |
| | 0 | 87 | | public ProtectionLevel ProtectionLevel { get; } |
| | | 88 | | |
| | 48 | 89 | | private NetworkCredential ServerCredential { get; set; } |
| | | 90 | | |
| | 12 | 91 | | protected LdapSettings LdapSettings { get; private set; } |
| | | 92 | | |
| | | 93 | | public override StreamUpgradeAcceptor CreateUpgradeAcceptor() |
| | | 94 | | { |
| | 0 | 95 | | ThrowIfDisposedOrNotOpen(); |
| | 0 | 96 | | return new WindowsStreamSecurityUpgradeAcceptor(this); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | protected override void OnAbort() |
| | | 100 | | { |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | protected override Task OnCloseAsync(CancellationToken token) |
| | | 104 | | { |
| | 0 | 105 | | return Task.CompletedTask; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | protected override async Task OnOpenAsync(CancellationToken token) |
| | | 109 | | { |
| | 12 | 110 | | SecurityTokenRequirement sspiTokenRequirement = TransportSecurityHelpers.CreateSspiTokenRequirement(Scheme, |
| | 12 | 111 | | (ServerCredential, ExtractGroupsForWindowsAccounts) = await |
| | 12 | 112 | | TransportSecurityHelpers.GetSspiCredentialAsync(_securityTokenManager, sspiTokenRequirement, token); |
| | 12 | 113 | | } |
| | | 114 | | |
| | | 115 | | protected override void OnOpened() |
| | | 116 | | { |
| | 12 | 117 | | base.OnOpened(); |
| | | 118 | | |
| | 12 | 119 | | if (IdentityVerifier == null) |
| | | 120 | | { |
| | 12 | 121 | | IdentityVerifier = IdentityVerifier.CreateDefault(); |
| | | 122 | | } |
| | | 123 | | |
| | 12 | 124 | | if (ServerCredential == null) |
| | | 125 | | { |
| | 0 | 126 | | ServerCredential = CredentialCache.DefaultNetworkCredentials; |
| | | 127 | | } |
| | 12 | 128 | | } |
| | | 129 | | |
| | | 130 | | private class WindowsStreamSecurityUpgradeAcceptor : StreamSecurityUpgradeAcceptorBase |
| | | 131 | | { |
| | | 132 | | private readonly WindowsStreamSecurityUpgradeProvider _parent; |
| | | 133 | | private readonly SecurityMessageProperty _clientSecurity; |
| | | 134 | | private readonly LdapSettings _ldapSettings; |
| | | 135 | | |
| | | 136 | | public WindowsStreamSecurityUpgradeAcceptor(WindowsStreamSecurityUpgradeProvider parent) |
| | 0 | 137 | | : base(FramingUpgradeString.Negotiate) |
| | | 138 | | { |
| | 0 | 139 | | _parent = parent; |
| | 0 | 140 | | _clientSecurity = new SecurityMessageProperty(); |
| | 0 | 141 | | _ldapSettings = parent.LdapSettings; |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | protected override async Task<(Stream, SecurityMessageProperty)> OnAcceptUpgradeAsync(Stream stream) |
| | | 145 | | { |
| | | 146 | | // wrap stream |
| | 0 | 147 | | NegotiateStream negotiateStream = new NegotiateStream(stream, true); |
| | | 148 | | |
| | | 149 | | // authenticate |
| | | 150 | | try |
| | | 151 | | { |
| | 0 | 152 | | await negotiateStream.AuthenticateAsServerAsync(_parent.ServerCredential, _parent.ProtectionLevel, |
| | 0 | 153 | | TokenImpersonationLevel.Identification); |
| | 0 | 154 | | } |
| | 0 | 155 | | catch (AuthenticationException exception) |
| | | 156 | | { |
| | 0 | 157 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception |
| | 0 | 158 | | exception)); |
| | | 159 | | } |
| | 0 | 160 | | catch (IOException ioException) |
| | | 161 | | { |
| | 0 | 162 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException( |
| | 0 | 163 | | SR.Format(SR.NegotiationFailedIO, ioException.Message), ioException)); |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | SecurityMessageProperty remoteSecurity = await CreateClientSecurityAsync(negotiateStream, _parent.Extrac |
| | 0 | 167 | | return (negotiateStream, remoteSecurity); |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | private async Task<SecurityMessageProperty> CreateClientSecurityAsync(NegotiateStream negotiateStream, |
| | | 171 | | bool extractGroupsForWindowsAccounts) |
| | | 172 | | { |
| | 0 | 173 | | IIdentity remoteIdentity = negotiateStream.RemoteIdentity; |
| | | 174 | | SecurityToken token; |
| | | 175 | | ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies; |
| | 0 | 176 | | WindowsSecurityTokenAuthenticator authenticator = new WindowsSecurityTokenAuthenticator(extractGroupsFor |
| | 0 | 177 | | if (remoteIdentity is WindowsIdentity) |
| | | 178 | | { |
| | 0 | 179 | | WindowsIdentity windowIdentity = (WindowsIdentity)remoteIdentity; |
| | 0 | 180 | | SecurityUtils.ValidateAnonymityConstraint(windowIdentity, false); |
| | 0 | 181 | | token = new WindowsSecurityToken(windowIdentity, SecurityUniqueId.Create().Value, windowIdentity.Aut |
| | | 182 | | } |
| | | 183 | | else |
| | | 184 | | { |
| | 0 | 185 | | GenericIdentity genericIdentity = (GenericIdentity)remoteIdentity; |
| | 0 | 186 | | ClaimsIdentity claimsIdentity = new ClaimsIdentity(remoteIdentity); |
| | 0 | 187 | | token = new GenericIdentitySecurityToken(genericIdentity, SecurityUniqueId.Create().Value); |
| | | 188 | | } |
| | 0 | 189 | | authorizationPolicies = await authenticator.ValidateTokenAsync(token); |
| | 0 | 190 | | SecurityMessageProperty clientSecurity = new SecurityMessageProperty |
| | 0 | 191 | | { |
| | 0 | 192 | | TransportToken = new SecurityTokenSpecification(token, authorizationPolicies), |
| | 0 | 193 | | ServiceSecurityContext = new ServiceSecurityContext(authorizationPolicies) |
| | 0 | 194 | | }; |
| | 0 | 195 | | return clientSecurity; |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | public override SecurityMessageProperty GetRemoteSecurity() |
| | | 199 | | { |
| | 0 | 200 | | if (_clientSecurity.TransportToken != null) |
| | | 201 | | { |
| | 0 | 202 | | return _clientSecurity; |
| | | 203 | | } |
| | 0 | 204 | | return base.GetRemoteSecurity(); |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | } |