| | | 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.Collections.ObjectModel; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.IdentityModel; |
| | | 10 | | using CoreWCF.IdentityModel.Policy; |
| | | 11 | | using CoreWCF.IdentityModel.Tokens; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Security.Tokens |
| | | 14 | | { |
| | | 15 | | public class SecurityContextSecurityToken : SecurityToken, IDisposable, TimeBoundedCache.IExpirableItem |
| | | 16 | | { |
| | | 17 | | private DateTime _tokenEffectiveTime; |
| | | 18 | | private DateTime _tokenExpirationTime; |
| | | 19 | | private byte[] _key; |
| | | 20 | | private string _keyString; |
| | | 21 | | private ReadOnlyCollection<IAuthorizationPolicy> _authorizationPolicies; |
| | | 22 | | private ReadOnlyCollection<SecurityKey> _securityKeys; |
| | | 23 | | private readonly string _id; |
| | | 24 | | private bool _disposed = false; |
| | | 25 | | |
| | | 26 | | public SecurityContextSecurityToken(UniqueId contextId, byte[] key, DateTime validFrom, DateTime validTo) |
| | 0 | 27 | | : this(contextId, SecurityUtils.GenerateId(), key, validFrom, validTo) |
| | 0 | 28 | | { } |
| | | 29 | | |
| | | 30 | | public SecurityContextSecurityToken(UniqueId contextId, string id, byte[] key, DateTime validFrom, DateTime vali |
| | 0 | 31 | | : this(contextId, id, key, validFrom, validTo, null) |
| | 0 | 32 | | { } |
| | | 33 | | |
| | | 34 | | public SecurityContextSecurityToken(UniqueId contextId, string id, byte[] key, DateTime validFrom, DateTime vali |
| | 0 | 35 | | : base() |
| | | 36 | | { |
| | 0 | 37 | | _id = id; |
| | 0 | 38 | | Initialize(contextId, key, validFrom, validTo, authorizationPolicies, false, null, validFrom, validTo); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | public SecurityContextSecurityToken(UniqueId contextId, string id, byte[] key, DateTime validFrom, DateTime vali |
| | 10 | 42 | | : base() |
| | | 43 | | { |
| | 10 | 44 | | _id = id; |
| | 10 | 45 | | Initialize(contextId, key, validFrom, validTo, authorizationPolicies, false, keyGeneration, keyEffectiveTime |
| | 10 | 46 | | } |
| | | 47 | | |
| | | 48 | | internal SecurityContextSecurityToken(SecurityContextSecurityToken sourceToken, string id) |
| | 0 | 49 | | : this(sourceToken, id, sourceToken._key, sourceToken.KeyGeneration, sourceToken.KeyEffectiveTime, sourceTok |
| | | 50 | | { |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | internal SecurityContextSecurityToken(SecurityContextSecurityToken sourceToken, string id, byte[] key, UniqueId |
| | 0 | 54 | | : base() |
| | | 55 | | { |
| | 0 | 56 | | _id = id; |
| | 0 | 57 | | Initialize(sourceToken.ContextId, key, sourceToken.ValidFrom, sourceToken.ValidTo, authorizationPolicies, so |
| | 0 | 58 | | CookieBlob = sourceToken.CookieBlob; |
| | 0 | 59 | | BootstrapMessageProperty = (sourceToken.BootstrapMessageProperty == null) ? null : (SecurityMessageProperty) |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | internal SecurityContextSecurityToken(UniqueId contextId, string id, byte[] key, DateTime validFrom, DateTime va |
| | 0 | 63 | | : this(contextId, id, key, validFrom, validTo, authorizationPolicies, isCookieMode, cookieBlob, null, validF |
| | | 64 | | { |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | internal SecurityContextSecurityToken(UniqueId contextId, string id, byte[] key, DateTime validFrom, DateTime va |
| | | 68 | | UniqueId keyGeneration, DateTime keyEffectiveTime, DateTime keyExpirationTime) |
| | 0 | 69 | | : base() |
| | | 70 | | { |
| | 0 | 71 | | _id = id; |
| | 0 | 72 | | Initialize(contextId, key, validFrom, validTo, authorizationPolicies, isCookieMode, keyGeneration, keyEffect |
| | 0 | 73 | | CookieBlob = cookieBlob; |
| | 0 | 74 | | } |
| | | 75 | | |
| | 30 | 76 | | private SecurityContextSecurityToken(SecurityContextSecurityToken from) |
| | | 77 | | { |
| | 30 | 78 | | ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies = SecurityUtils.CloneAuthorizationPoliciesIfN |
| | 30 | 79 | | _id = from._id; |
| | 30 | 80 | | Initialize(from.ContextId, from._key, from._tokenEffectiveTime, from._tokenExpirationTime, authorizationPoli |
| | 30 | 81 | | CookieBlob = from.CookieBlob; |
| | 30 | 82 | | BootstrapMessageProperty = (from.BootstrapMessageProperty == null) ? null : (SecurityMessageProperty)from.Bo |
| | 30 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Gets or Sets the SecurityMessageProperty extracted from |
| | | 87 | | /// the Bootstrap message. This will contain the original tokens |
| | | 88 | | /// that the client used to Authenticate with the service. By |
| | | 89 | | /// default, this is turned off. To turn this feature on, add a custom |
| | | 90 | | /// ServiceCredentialsSecurityTokenManager and override |
| | | 91 | | /// CreateSecurityTokenManager. Create the SecurityContextToken Authenticator by calling |
| | | 92 | | /// ServiceCredentialsSecurityTokenManager.CreateSecureConversationTokenAuthenticator |
| | | 93 | | /// with 'preserveBootstrapTokens' parameter to true. |
| | | 94 | | /// If there are any UserNameSecurityToken in the bootstrap message, the password in |
| | | 95 | | /// these tokens will be removed. When 'Cookie' mode SCT is enabled the BootstrapMessageProperty |
| | | 96 | | /// is not preserved in the Cookie. To preserve the bootstrap tokens in the CookieMode case |
| | | 97 | | /// write a custom Serializer and serialize the property as part of the cookie. |
| | | 98 | | /// </summary> |
| | 70 | 99 | | public SecurityMessageProperty BootstrapMessageProperty { get; set; } |
| | | 100 | | |
| | 100 | 101 | | public override string Id => _id; |
| | | 102 | | |
| | 230 | 103 | | public UniqueId ContextId { get; private set; } = null; |
| | | 104 | | |
| | 120 | 105 | | public UniqueId KeyGeneration { get; private set; } = null; |
| | | 106 | | |
| | 160 | 107 | | public DateTime KeyEffectiveTime { get; private set; } |
| | | 108 | | |
| | 190 | 109 | | public DateTime KeyExpirationTime { get; private set; } |
| | | 110 | | |
| | | 111 | | public ReadOnlyCollection<IAuthorizationPolicy> AuthorizationPolicies |
| | | 112 | | { |
| | | 113 | | get |
| | | 114 | | { |
| | 30 | 115 | | ThrowIfDisposed(); |
| | 30 | 116 | | return _authorizationPolicies; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | internal set |
| | | 120 | | { |
| | 0 | 121 | | _authorizationPolicies = value; |
| | 0 | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | 40 | 125 | | public override ReadOnlyCollection<SecurityKey> SecurityKeys => _securityKeys; |
| | | 126 | | |
| | 10 | 127 | | public override DateTime ValidFrom => _tokenEffectiveTime; |
| | | 128 | | |
| | 40 | 129 | | public override DateTime ValidTo => _tokenExpirationTime; |
| | | 130 | | |
| | 30 | 131 | | internal byte[] CookieBlob { get; } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// This is set by the issuer when creating the SCT to be sent in the RSTR |
| | | 135 | | /// The SecurityContextTokenManager examines this property to determine how to write |
| | | 136 | | /// out the SCT |
| | | 137 | | /// This field is set to true when the issuer reads in a cookie mode SCT |
| | | 138 | | /// </summary> |
| | 90 | 139 | | public bool IsCookieMode { get; private set; } = false; |
| | | 140 | | |
| | 30 | 141 | | DateTime TimeBoundedCache.IExpirableItem.ExpirationTime => ValidTo; |
| | | 142 | | |
| | | 143 | | internal string GetBase64KeyString() |
| | | 144 | | { |
| | 0 | 145 | | if (_keyString == null) |
| | | 146 | | { |
| | 0 | 147 | | _keyString = Convert.ToBase64String(_key); |
| | | 148 | | } |
| | 0 | 149 | | return _keyString; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | internal byte[] GetKeyBytes() |
| | | 153 | | { |
| | 0 | 154 | | byte[] retval = new byte[_key.Length]; |
| | 0 | 155 | | Buffer.BlockCopy(_key, 0, retval, 0, _key.Length); |
| | 0 | 156 | | return retval; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | public override string ToString() |
| | | 160 | | { |
| | 0 | 161 | | return string.Format(CultureInfo.CurrentCulture, "SecurityContextSecurityToken(Identifier='{0}', KeyGenerati |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | private void Initialize(UniqueId contextId, byte[] key, DateTime validFrom, DateTime validTo, ReadOnlyCollection |
| | | 165 | | UniqueId keyGeneration, DateTime keyEffectiveTime, DateTime keyExpirationTime) |
| | | 166 | | { |
| | 40 | 167 | | if (key == null || key.Length == 0) |
| | | 168 | | { |
| | 0 | 169 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 170 | | } |
| | | 171 | | |
| | 40 | 172 | | DateTime tokenEffectiveTimeUtc = validFrom.ToUniversalTime(); |
| | 40 | 173 | | DateTime tokenExpirationTimeUtc = validTo.ToUniversalTime(); |
| | 40 | 174 | | if (tokenEffectiveTimeUtc > tokenExpirationTimeUtc) |
| | | 175 | | { |
| | 0 | 176 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(validFrom), SR.EffectiveGreaterThanE |
| | | 177 | | } |
| | 40 | 178 | | _tokenEffectiveTime = tokenEffectiveTimeUtc; |
| | 40 | 179 | | _tokenExpirationTime = tokenExpirationTimeUtc; |
| | | 180 | | |
| | 40 | 181 | | KeyEffectiveTime = keyEffectiveTime.ToUniversalTime(); |
| | 40 | 182 | | KeyExpirationTime = keyExpirationTime.ToUniversalTime(); |
| | 40 | 183 | | if (KeyEffectiveTime > KeyExpirationTime) |
| | | 184 | | { |
| | 0 | 185 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(keyEffectiveTime), SR.EffectiveGreat |
| | | 186 | | } |
| | 40 | 187 | | if ((KeyEffectiveTime < tokenEffectiveTimeUtc) || (KeyExpirationTime > tokenExpirationTimeUtc)) |
| | | 188 | | { |
| | 0 | 189 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.KeyLifetimeNotWithinTokenLifet |
| | | 190 | | } |
| | | 191 | | |
| | 40 | 192 | | _key = new byte[key.Length]; |
| | 40 | 193 | | Buffer.BlockCopy(key, 0, _key, 0, key.Length); |
| | 40 | 194 | | ContextId = contextId ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contextId)) |
| | 40 | 195 | | KeyGeneration = keyGeneration; |
| | 40 | 196 | | _authorizationPolicies = authorizationPolicies ?? EmptyReadOnlyCollection<IAuthorizationPolicy>.Instance; |
| | 40 | 197 | | List<SecurityKey> temp = new List<SecurityKey>(1) |
| | 40 | 198 | | { |
| | 40 | 199 | | new InMemorySymmetricSecurityKey(_key, false) |
| | 40 | 200 | | }; |
| | 40 | 201 | | _securityKeys = temp.AsReadOnly(); |
| | 40 | 202 | | IsCookieMode = isCookieMode; |
| | 40 | 203 | | } |
| | | 204 | | |
| | | 205 | | public override bool CanCreateKeyIdentifierClause<T>() |
| | | 206 | | { |
| | 0 | 207 | | if (typeof(T) == typeof(SecurityContextKeyIdentifierClause)) |
| | | 208 | | { |
| | 0 | 209 | | return true; |
| | | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | return base.CanCreateKeyIdentifierClause<T>(); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | public override T CreateKeyIdentifierClause<T>() |
| | | 216 | | { |
| | 30 | 217 | | if (typeof(T) == typeof(SecurityContextKeyIdentifierClause)) |
| | | 218 | | { |
| | 20 | 219 | | return new SecurityContextKeyIdentifierClause(ContextId, KeyGeneration) as T; |
| | | 220 | | } |
| | | 221 | | |
| | 10 | 222 | | return base.CreateKeyIdentifierClause<T>(); |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | public override bool MatchesKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 226 | | { |
| | 20 | 227 | | if (keyIdentifierClause is SecurityContextKeyIdentifierClause sctKeyIdentifierClause) |
| | | 228 | | { |
| | 0 | 229 | | return sctKeyIdentifierClause.Matches(ContextId, KeyGeneration); |
| | | 230 | | } |
| | | 231 | | |
| | 20 | 232 | | return base.MatchesKeyIdentifierClause(keyIdentifierClause); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | /* |
| | | 236 | | public static SecurityContextSecurityToken CreateCookieSecurityContextToken(UniqueId contextId, string id, byte[ |
| | | 237 | | DateTime validFrom, DateTime validTo, ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies, Securi |
| | | 238 | | { |
| | | 239 | | return CreateCookieSecurityContextToken(contextId, id, key, validFrom, validTo, null, validFrom, validTo, au |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | |
| | | 243 | | public static SecurityContextSecurityToken CreateCookieSecurityContextToken(UniqueId contextId, string id, byte[ |
| | | 244 | | DateTime validFrom, DateTime validTo, UniqueId keyGeneration, DateTime keyEffectiveTime, |
| | | 245 | | DateTime keyExpirationTime, ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies, SecurityStateEnc |
| | | 246 | | { |
| | | 247 | | SecurityContextCookieSerializer cookieSerializer = new SecurityContextCookieSerializer(securityStateEncoder, |
| | | 248 | | byte[] cookieBlob = cookieSerializer.CreateCookieFromSecurityContext(contextId, id, key, validFrom, validTo, |
| | | 249 | | keyEffectiveTime, keyExpirationTime, authorizationPolicies); |
| | | 250 | | |
| | | 251 | | return new SecurityContextSecurityToken(contextId, id, key, validFrom, validTo, |
| | | 252 | | authorizationPolicies, true, cookieBlob, keyGeneration, keyEffectiveTime, keyExpirationTime); |
| | | 253 | | }*/ |
| | | 254 | | |
| | | 255 | | internal SecurityContextSecurityToken Clone() |
| | | 256 | | { |
| | 30 | 257 | | ThrowIfDisposed(); |
| | 30 | 258 | | return new SecurityContextSecurityToken(this); |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | public void Dispose() |
| | | 262 | | { |
| | 10 | 263 | | if (!_disposed) |
| | | 264 | | { |
| | 10 | 265 | | _disposed = true; |
| | 10 | 266 | | SecurityUtils.DisposeAuthorizationPoliciesIfNecessary(_authorizationPolicies); |
| | 10 | 267 | | if (BootstrapMessageProperty != null) |
| | | 268 | | { |
| | 0 | 269 | | BootstrapMessageProperty.Dispose(); |
| | | 270 | | } |
| | | 271 | | } |
| | 10 | 272 | | } |
| | | 273 | | |
| | | 274 | | private void ThrowIfDisposed() |
| | | 275 | | { |
| | 60 | 276 | | if (_disposed) |
| | | 277 | | { |
| | 0 | 278 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().FullName |
| | | 279 | | } |
| | 60 | 280 | | } |
| | | 281 | | } |
| | | 282 | | } |