| | | 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.Generic; |
| | | 6 | | using System.Security.Cryptography.X509Certificates; |
| | | 7 | | using CoreWCF.IdentityModel.Selectors; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Security |
| | | 10 | | { |
| | | 11 | | public class IssuedTokenServiceCredential |
| | | 12 | | { |
| | | 13 | | internal const bool DefaultAllowUntrustedRsaIssuers = false; |
| | | 14 | | internal const AudienceUriMode DefaultAudienceUriMode = AudienceUriMode.Always; |
| | | 15 | | internal const X509CertificateValidationMode DefaultCertificateValidationMode = X509CertificateValidationMode.Ch |
| | | 16 | | internal const X509RevocationMode DefaultRevocationMode = X509RevocationMode.Online; |
| | | 17 | | internal const StoreLocation DefaultTrustedStoreLocation = StoreLocation.LocalMachine; |
| | | 18 | | private readonly List<string> _allowedAudienceUris; |
| | 118 | 19 | | private AudienceUriMode _audienceUriMode = DefaultAudienceUriMode; |
| | | 20 | | private readonly List<X509Certificate2> _knownCertificates; |
| | | 21 | | private SamlSerializer _samlSerializer; |
| | 118 | 22 | | private X509CertificateValidationMode _certificateValidationMode = DefaultCertificateValidationMode; |
| | 118 | 23 | | private X509RevocationMode _revocationMode = DefaultRevocationMode; |
| | 118 | 24 | | private StoreLocation _trustedStoreLocation = DefaultTrustedStoreLocation; |
| | | 25 | | private X509CertificateValidator _customCertificateValidator = null; |
| | | 26 | | private bool _allowUntrustedRsaIssuers = DefaultAllowUntrustedRsaIssuers; |
| | | 27 | | private bool _isReadOnly; |
| | | 28 | | |
| | 52 | 29 | | internal IssuedTokenServiceCredential() |
| | | 30 | | { |
| | 52 | 31 | | _allowedAudienceUris = new List<string>(); |
| | 52 | 32 | | _knownCertificates = new List<X509Certificate2>(); |
| | 52 | 33 | | } |
| | | 34 | | |
| | 66 | 35 | | internal IssuedTokenServiceCredential(IssuedTokenServiceCredential other) |
| | | 36 | | { |
| | 66 | 37 | | _audienceUriMode = other._audienceUriMode; |
| | 66 | 38 | | _allowedAudienceUris = new List<string>(other._allowedAudienceUris); |
| | 66 | 39 | | _samlSerializer = other._samlSerializer; |
| | 66 | 40 | | _knownCertificates = new List<X509Certificate2>(other._knownCertificates); |
| | 66 | 41 | | _certificateValidationMode = other._certificateValidationMode; |
| | 66 | 42 | | _customCertificateValidator = other._customCertificateValidator; |
| | 66 | 43 | | _trustedStoreLocation = other._trustedStoreLocation; |
| | 66 | 44 | | _revocationMode = other._revocationMode; |
| | 66 | 45 | | _allowUntrustedRsaIssuers = other._allowUntrustedRsaIssuers; |
| | 66 | 46 | | _isReadOnly = other._isReadOnly; |
| | 66 | 47 | | } |
| | | 48 | | |
| | | 49 | | public IList<string> AllowedAudienceUris |
| | | 50 | | { |
| | | 51 | | get |
| | | 52 | | { |
| | 0 | 53 | | if (_isReadOnly) |
| | | 54 | | { |
| | 0 | 55 | | return _allowedAudienceUris.AsReadOnly(); |
| | | 56 | | } |
| | | 57 | | else |
| | | 58 | | { |
| | 0 | 59 | | return _allowedAudienceUris; |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public AudienceUriMode AudienceUriMode |
| | | 65 | | { |
| | | 66 | | get |
| | | 67 | | { |
| | 0 | 68 | | return _audienceUriMode; |
| | | 69 | | } |
| | | 70 | | set |
| | | 71 | | { |
| | 0 | 72 | | ThrowIfImmutable(); |
| | 0 | 73 | | AudienceUriModeValidationHelper.Validate(_audienceUriMode); |
| | 0 | 74 | | _audienceUriMode = value; |
| | 0 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | |
| | | 79 | | public IList<X509Certificate2> KnownCertificates |
| | | 80 | | { |
| | | 81 | | get |
| | | 82 | | { |
| | 0 | 83 | | if (_isReadOnly) |
| | | 84 | | { |
| | 0 | 85 | | return _knownCertificates.AsReadOnly(); |
| | | 86 | | } |
| | | 87 | | else |
| | | 88 | | { |
| | 0 | 89 | | return _knownCertificates; |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public SamlSerializer SamlSerializer |
| | | 95 | | { |
| | | 96 | | get |
| | | 97 | | { |
| | 2 | 98 | | return _samlSerializer; |
| | | 99 | | } |
| | | 100 | | set |
| | | 101 | | { |
| | 0 | 102 | | ThrowIfImmutable(); |
| | 0 | 103 | | _samlSerializer = value; |
| | 0 | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | public X509CertificateValidationMode CertificateValidationMode |
| | | 108 | | { |
| | | 109 | | get |
| | | 110 | | { |
| | 0 | 111 | | return _certificateValidationMode; |
| | | 112 | | } |
| | | 113 | | set |
| | | 114 | | { |
| | 2 | 115 | | X509CertificateValidationModeHelper.Validate(value); |
| | 2 | 116 | | ThrowIfImmutable(); |
| | 2 | 117 | | _certificateValidationMode = value; |
| | 2 | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public X509RevocationMode RevocationMode |
| | | 122 | | { |
| | | 123 | | get |
| | | 124 | | { |
| | 0 | 125 | | return _revocationMode; |
| | | 126 | | } |
| | | 127 | | set |
| | | 128 | | { |
| | 2 | 129 | | ThrowIfImmutable(); |
| | 2 | 130 | | _revocationMode = value; |
| | 2 | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public StoreLocation TrustedStoreLocation |
| | | 135 | | { |
| | | 136 | | get |
| | | 137 | | { |
| | 0 | 138 | | return _trustedStoreLocation; |
| | | 139 | | } |
| | | 140 | | set |
| | | 141 | | { |
| | 0 | 142 | | ThrowIfImmutable(); |
| | 0 | 143 | | _trustedStoreLocation = value; |
| | 0 | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | public X509CertificateValidator CustomCertificateValidator |
| | | 148 | | { |
| | | 149 | | get |
| | | 150 | | { |
| | 0 | 151 | | return _customCertificateValidator; |
| | | 152 | | } |
| | | 153 | | set |
| | | 154 | | { |
| | 0 | 155 | | ThrowIfImmutable(); |
| | 0 | 156 | | _customCertificateValidator = value; |
| | 0 | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | public bool AllowUntrustedRsaIssuers |
| | | 161 | | { |
| | | 162 | | get |
| | | 163 | | { |
| | 0 | 164 | | return _allowUntrustedRsaIssuers; |
| | | 165 | | } |
| | | 166 | | set |
| | | 167 | | { |
| | 0 | 168 | | ThrowIfImmutable(); |
| | 0 | 169 | | _allowUntrustedRsaIssuers = value; |
| | 0 | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | internal X509CertificateValidator GetCertificateValidator() |
| | | 174 | | { |
| | 0 | 175 | | if (_certificateValidationMode == X509CertificateValidationMode.None) |
| | | 176 | | { |
| | 0 | 177 | | return X509CertificateValidator.None; |
| | | 178 | | } |
| | 0 | 179 | | else if (_certificateValidationMode == X509CertificateValidationMode.PeerTrust) |
| | | 180 | | { |
| | 0 | 181 | | return X509CertificateValidator.PeerTrust; |
| | | 182 | | } |
| | 0 | 183 | | else if (_certificateValidationMode == X509CertificateValidationMode.Custom) |
| | | 184 | | { |
| | 0 | 185 | | if (_customCertificateValidator == null) |
| | | 186 | | { |
| | 0 | 187 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 188 | | } |
| | 0 | 189 | | return _customCertificateValidator; |
| | | 190 | | } |
| | | 191 | | else |
| | | 192 | | { |
| | 0 | 193 | | bool useMachineContext = _trustedStoreLocation == StoreLocation.LocalMachine; |
| | 0 | 194 | | X509ChainPolicy chainPolicy = new X509ChainPolicy |
| | 0 | 195 | | { |
| | 0 | 196 | | RevocationMode = _revocationMode |
| | 0 | 197 | | }; |
| | 0 | 198 | | if (_certificateValidationMode == X509CertificateValidationMode.ChainTrust) |
| | | 199 | | { |
| | 0 | 200 | | return X509CertificateValidator.CreateChainTrustValidator(useMachineContext, chainPolicy); |
| | | 201 | | } |
| | | 202 | | else |
| | | 203 | | { |
| | 0 | 204 | | return X509CertificateValidator.CreatePeerOrChainTrustValidator(useMachineContext, chainPolicy); |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | internal void MakeReadOnly() |
| | | 210 | | { |
| | 0 | 211 | | _isReadOnly = true; |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | private void ThrowIfImmutable() |
| | | 215 | | { |
| | 4 | 216 | | if (_isReadOnly) |
| | | 217 | | { |
| | 0 | 218 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Obj |
| | | 219 | | } |
| | 4 | 220 | | } |
| | | 221 | | } |
| | | 222 | | } |