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
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Security
{
using System.IdentityModel.Selectors;
using System.ServiceModel;
using System.Security.Cryptography.X509Certificates;
public class X509ClientCertificateAuthentication
{
internal const X509CertificateValidationMode DefaultCertificateValidationMode = X509CertificateValidationMode.ChainTrust;
internal const X509RevocationMode DefaultRevocationMode = X509RevocationMode.Online;
internal const StoreLocation DefaultTrustedStoreLocation = StoreLocation.LocalMachine;
internal const bool DefaultMapCertificateToWindowsAccount = false;
static X509CertificateValidator defaultCertificateValidator;
X509CertificateValidationMode certificateValidationMode = DefaultCertificateValidationMode;
X509RevocationMode revocationMode = DefaultRevocationMode;
StoreLocation trustedStoreLocation = DefaultTrustedStoreLocation;
X509CertificateValidator customCertificateValidator = null;
bool mapClientCertificateToWindowsAccount = DefaultMapCertificateToWindowsAccount;
bool includeWindowsGroups = SspiSecurityTokenProvider.DefaultExtractWindowsGroupClaims;
bool isReadOnly;
internal X509ClientCertificateAuthentication()
{
}
internal X509ClientCertificateAuthentication(X509ClientCertificateAuthentication other)
{
this.certificateValidationMode = other.certificateValidationMode;
this.customCertificateValidator = other.customCertificateValidator;
this.includeWindowsGroups = other.includeWindowsGroups;
this.mapClientCertificateToWindowsAccount = other.mapClientCertificateToWindowsAccount;
this.trustedStoreLocation = other.trustedStoreLocation;
this.revocationMode = other.revocationMode;
this.isReadOnly = other.isReadOnly;
}
internal static X509CertificateValidator DefaultCertificateValidator
{
get
{
if (defaultCertificateValidator == null)
{
bool useMachineContext = DefaultTrustedStoreLocation == StoreLocation.LocalMachine;
X509ChainPolicy chainPolicy = new X509ChainPolicy();
chainPolicy.RevocationMode = DefaultRevocationMode;
defaultCertificateValidator = X509CertificateValidator.CreateChainTrustValidator(useMachineContext, chainPolicy);
}
return defaultCertificateValidator;
}
}
public X509CertificateValidationMode CertificateValidationMode
{
get
{
return this.certificateValidationMode;
}
set
{
X509CertificateValidationModeHelper.Validate(value);
ThrowIfImmutable();
this.certificateValidationMode = value;
}
}
public X509RevocationMode RevocationMode
{
get
{
return this.revocationMode;
}
set
{
ThrowIfImmutable();
this.revocationMode = value;
}
}
public StoreLocation TrustedStoreLocation
{
get
{
return this.trustedStoreLocation;
}
set
{
ThrowIfImmutable();
this.trustedStoreLocation = value;
}
}
public X509CertificateValidator CustomCertificateValidator
{
get
{
return this.customCertificateValidator;
}
set
{
ThrowIfImmutable();
this.customCertificateValidator = value;
}
}
public bool MapClientCertificateToWindowsAccount
{
get
{
return this.mapClientCertificateToWindowsAccount;
}
set
{
ThrowIfImmutable();
this.mapClientCertificateToWindowsAccount = value;
}
}
public bool IncludeWindowsGroups
{
get
{
return this.includeWindowsGroups;
}
set
{
ThrowIfImmutable();
this.includeWindowsGroups = value;
}
}
internal X509CertificateValidator GetCertificateValidator()
{
if (this.certificateValidationMode == X509CertificateValidationMode.None)
{
return X509CertificateValidator.None;
}
else if (this.certificateValidationMode == X509CertificateValidationMode.PeerTrust)
{
return X509CertificateValidator.PeerTrust;
}
else if (this.certificateValidationMode == X509CertificateValidationMode.Custom)
{
if (this.customCertificateValidator == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MissingCustomCertificateValidator)));
}
return this.customCertificateValidator;
}
else
{
bool useMachineContext = this.trustedStoreLocation == StoreLocation.LocalMachine;
X509ChainPolicy chainPolicy = new X509ChainPolicy();
chainPolicy.RevocationMode = this.revocationMode;
if (this.certificateValidationMode == X509CertificateValidationMode.ChainTrust)
{
return X509CertificateValidator.CreateChainTrustValidator(useMachineContext, chainPolicy);
}
else
{
return X509CertificateValidator.CreatePeerOrChainTrustValidator(useMachineContext, chainPolicy);
}
}
}
internal void MakeReadOnly()
{
this.isReadOnly = true;
}
void ThrowIfImmutable()
{
if (this.isReadOnly)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
}
}
}
}
|