1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Description
{
using System.Diagnostics.CodeAnalysis;
using System.IdentityModel.Selectors;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Security;
using System.IdentityModel.Tokens;
public class ClientCredentials : SecurityCredentialsManager, IEndpointBehavior
{
internal const bool SupportInteractiveDefault = true;
UserNamePasswordClientCredential userName;
X509CertificateInitiatorClientCredential clientCertificate;
X509CertificateRecipientClientCredential serviceCertificate;
WindowsClientCredential windows;
HttpDigestClientCredential httpDigest;
IssuedTokenClientCredential issuedToken;
PeerCredential peer;
bool supportInteractive;
bool isReadOnly;
GetInfoCardTokenCallback getInfoCardTokenCallback = null;
bool useIdentityConfiguration = false;
SecurityTokenHandlerCollectionManager securityTokenHandlerCollectionManager = null;
object handlerCollectionLock = new object();
public ClientCredentials()
{
this.supportInteractive = SupportInteractiveDefault;
}
protected ClientCredentials(ClientCredentials other)
{
if (other == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("other");
if (other.userName != null)
this.userName = new UserNamePasswordClientCredential(other.userName);
if (other.clientCertificate != null)
this.clientCertificate = new X509CertificateInitiatorClientCredential(other.clientCertificate);
if (other.serviceCertificate != null)
this.serviceCertificate = new X509CertificateRecipientClientCredential(other.serviceCertificate);
if (other.windows != null)
this.windows = new WindowsClientCredential(other.windows);
if (other.httpDigest != null)
this.httpDigest = new HttpDigestClientCredential(other.httpDigest);
if (other.issuedToken != null)
this.issuedToken = new IssuedTokenClientCredential(other.issuedToken);
if (other.peer != null)
this.peer = new PeerCredential(other.peer);
this.getInfoCardTokenCallback = other.getInfoCardTokenCallback;
this.supportInteractive = other.supportInteractive;
this.securityTokenHandlerCollectionManager = other.securityTokenHandlerCollectionManager;
this.useIdentityConfiguration = other.useIdentityConfiguration;
this.isReadOnly = other.isReadOnly;
}
internal GetInfoCardTokenCallback GetInfoCardTokenCallback
{
get
{
if (this.getInfoCardTokenCallback == null)
{
GetInfoCardTokenCallback gtc = new GetInfoCardTokenCallback(this.GetInfoCardSecurityToken);
this.getInfoCardTokenCallback = gtc;
}
return this.getInfoCardTokenCallback;
}
}
public IssuedTokenClientCredential IssuedToken
{
get
{
if (this.issuedToken == null)
{
this.issuedToken = new IssuedTokenClientCredential();
if (isReadOnly)
this.issuedToken.MakeReadOnly();
}
return this.issuedToken;
}
}
public UserNamePasswordClientCredential UserName
{
get
{
if (this.userName == null)
{
this.userName = new UserNamePasswordClientCredential();
if (isReadOnly)
this.userName.MakeReadOnly();
}
return this.userName;
}
}
public X509CertificateInitiatorClientCredential ClientCertificate
{
get
{
if (this.clientCertificate == null)
{
this.clientCertificate = new X509CertificateInitiatorClientCredential();
if (isReadOnly)
this.clientCertificate.MakeReadOnly();
}
return this.clientCertificate;
}
}
public X509CertificateRecipientClientCredential ServiceCertificate
{
get
{
if (this.serviceCertificate == null)
{
this.serviceCertificate = new X509CertificateRecipientClientCredential();
if (isReadOnly)
this.serviceCertificate.MakeReadOnly();
}
return this.serviceCertificate;
}
}
public WindowsClientCredential Windows
{
get
{
if (this.windows == null)
{
this.windows = new WindowsClientCredential();
if (isReadOnly)
this.windows.MakeReadOnly();
}
return this.windows;
}
}
public HttpDigestClientCredential HttpDigest
{
get
{
if (this.httpDigest == null)
{
this.httpDigest = new HttpDigestClientCredential();
if (isReadOnly)
this.httpDigest.MakeReadOnly();
}
return this.httpDigest;
}
}
public PeerCredential Peer
{
get
{
if (this.peer == null)
{
this.peer = new PeerCredential();
if (isReadOnly)
this.peer.MakeReadOnly();
}
return this.peer;
}
}
/// <summary>
/// The <see cref="SecurityTokenHandlerCollectionManager" /> containing the set of <see cref="SecurityTokenHandler" />
/// objects used for serializing and validating tokens found in WS-Trust messages.
/// </summary>
public SecurityTokenHandlerCollectionManager SecurityTokenHandlerCollectionManager
{
get
{
if (this.securityTokenHandlerCollectionManager == null)
{
lock (this.handlerCollectionLock)
{
if (this.securityTokenHandlerCollectionManager == null)
{
this.securityTokenHandlerCollectionManager = SecurityTokenHandlerCollectionManager.CreateDefaultSecurityTokenHandlerCollectionManager();
}
}
}
return this.securityTokenHandlerCollectionManager;
}
set
{
if (this.isReadOnly)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
}
this.securityTokenHandlerCollectionManager = value;
}
}
public bool UseIdentityConfiguration
{
get
{
return this.useIdentityConfiguration;
}
set
{
if (this.isReadOnly)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
}
this.useIdentityConfiguration = value;
}
}
public bool SupportInteractive
{
get
{
return this.supportInteractive;
}
set
{
if (this.isReadOnly)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
}
this.supportInteractive = value;
}
}
internal static ClientCredentials CreateDefaultCredentials()
{
return new ClientCredentials();
}
public override SecurityTokenManager CreateSecurityTokenManager()
{
return new ClientCredentialsSecurityTokenManager(this.Clone());
}
protected virtual ClientCredentials CloneCore()
{
return new ClientCredentials(this);
}
public ClientCredentials Clone()
{
ClientCredentials result = CloneCore();
if (result == null || result.GetType() != this.GetType())
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.CloneNotImplementedCorrectly, this.GetType(), (result != null) ? result.ToString() : "null")));
}
return result;
}
void IEndpointBehavior.Validate(ServiceEndpoint serviceEndpoint)
{
}
void IEndpointBehavior.AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters)
{
if (bindingParameters == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingParameters");
}
// throw if bindingParameters already has a SecurityCredentialsManager
SecurityCredentialsManager otherCredentialsManager = bindingParameters.Find<SecurityCredentialsManager>();
if (otherCredentialsManager != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MultipleSecurityCredentialsManagersInChannelBindingParameters, otherCredentialsManager)));
}
bindingParameters.Add(this);
}
void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
SR.GetString(SR.SFXEndpointBehaviorUsedOnWrongSide, typeof(ClientCredentials).Name)));
}
[MethodImpl(MethodImplOptions.NoInlining)]
void AddInteractiveInitializers(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
{
CardSpacePolicyElement[] dummyPolicyElements;
Uri dummyRelyingPartyIssuer;
// we add the initializer only if infocard is required. At this point, serviceEndpoint.Address is not populated correctly but that's not needed to
// determine whether infocard is required or not.
if (InfoCardHelper.IsInfocardRequired(serviceEndpoint.Binding, this, this.CreateSecurityTokenManager(), EndpointAddress.AnonymousAddress, out dummyPolicyElements, out dummyRelyingPartyIssuer))
{
behavior.InteractiveChannelInitializers.Add(new InfocardInteractiveChannelInitializer(this, serviceEndpoint.Binding));
}
}
public virtual void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
{
if (serviceEndpoint == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceEndpoint");
}
if (serviceEndpoint.Binding == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceEndpoint.Binding");
}
if (serviceEndpoint.Binding.CreateBindingElements().Find<SecurityBindingElement>() == null)
{
return;
}
try
{
AddInteractiveInitializers(serviceEndpoint, behavior);
}
catch (System.IO.FileNotFoundException)
{
}
}
// RC0 workaround to freeze credentials when the channel factory is opened
internal void MakeReadOnly()
{
this.isReadOnly = true;
if (this.clientCertificate != null)
this.clientCertificate.MakeReadOnly();
if (this.serviceCertificate != null)
this.serviceCertificate.MakeReadOnly();
if (this.userName != null)
this.userName.MakeReadOnly();
if (this.windows != null)
this.windows.MakeReadOnly();
if (this.httpDigest != null)
this.httpDigest.MakeReadOnly();
if (this.issuedToken != null)
this.issuedToken.MakeReadOnly();
if (this.peer != null)
this.peer.MakeReadOnly();
}
// This APTCA method calls CardSpaceSelector.GetToken(..), which is defined in a non-APTCA assembly. It would be a breaking change to add a Demand,
// while we don't have an identified security vulnerability here.
[SuppressMessage(FxCop.Category.Security, FxCop.Rule.AptcaMethodsShouldOnlyCallAptcaMethods)]
internal protected virtual SecurityToken GetInfoCardSecurityToken(bool requiresInfoCard, CardSpacePolicyElement[] chain, SecurityTokenSerializer tokenSerializer)
{
if (!requiresInfoCard)
{
return null;
}
return CardSpaceSelector.GetToken(chain, tokenSerializer);
}
}
}
|