| | | 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; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Collections.ObjectModel; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | using CoreWCF.Security.Tokens; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Security |
| | | 13 | | { |
| | | 14 | | // This is the in-memory cache used for caching SCTs |
| | | 15 | | internal sealed class SecurityContextTokenCache : TimeBoundedCache |
| | | 16 | | { |
| | | 17 | | // if there are less than lowWaterMark entries, no purging is done |
| | | 18 | | private const int LowWaterMark = 50; |
| | | 19 | | |
| | | 20 | | // frequency of purging the cache of stale entries |
| | | 21 | | // this is set to 10 mins as SCTs are expected to have long lifetimes |
| | 3 | 22 | | private static TimeSpan s_purgingInterval = TimeSpan.FromMinutes(10); |
| | | 23 | | private const double PruningFactor = 0.20; |
| | 23 | 24 | | private readonly bool _replaceOldestEntries = true; |
| | 3 | 25 | | private static readonly SctEffectiveTimeComparer s_sctEffectiveTimeComparer = new SctEffectiveTimeComparer(); |
| | | 26 | | private TimeSpan _clockSkew; |
| | | 27 | | |
| | | 28 | | public SecurityContextTokenCache(int capacity, bool replaceOldestEntries) |
| | 0 | 29 | | : this(capacity, replaceOldestEntries, SecurityProtocolFactory.defaultMaxClockSkew) |
| | | 30 | | { |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public SecurityContextTokenCache(int capacity, bool replaceOldestEntries, TimeSpan clockSkew) |
| | 23 | 34 | | : base(LowWaterMark, capacity, null, PurgingMode.TimerBasedPurge, s_purgingInterval, true) |
| | | 35 | | |
| | | 36 | | { |
| | 23 | 37 | | _replaceOldestEntries = replaceOldestEntries; |
| | 23 | 38 | | _clockSkew = clockSkew; |
| | 23 | 39 | | } |
| | | 40 | | |
| | | 41 | | public void AddContext(SecurityContextSecurityToken token) |
| | | 42 | | { |
| | 10 | 43 | | TryAddContext(token, true); |
| | 10 | 44 | | } |
| | | 45 | | |
| | | 46 | | public bool TryAddContext(SecurityContextSecurityToken token) |
| | | 47 | | { |
| | 0 | 48 | | return TryAddContext(token, false); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | private bool TryAddContext(SecurityContextSecurityToken token, bool throwOnFailure) |
| | | 52 | | { |
| | 10 | 53 | | if (token == null) |
| | | 54 | | { |
| | 0 | 55 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 56 | | } |
| | | 57 | | |
| | 10 | 58 | | if (!SecurityUtils.IsCurrentlyTimeEffective(token.ValidFrom, token.ValidTo, _clockSkew)) |
| | | 59 | | { |
| | 0 | 60 | | if (token.KeyGeneration == null) |
| | | 61 | | { |
| | 0 | 62 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityContextExpiredNoKe |
| | | 63 | | } |
| | | 64 | | else |
| | | 65 | | { |
| | 0 | 66 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityContextExpired, to |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | 10 | 70 | | if (!SecurityUtils.IsCurrentlyTimeEffective(token.KeyEffectiveTime, token.KeyExpirationTime, _clockSkew)) |
| | | 71 | | { |
| | 0 | 72 | | if (token.KeyGeneration == null) |
| | | 73 | | { |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityContextKeyExpiredN |
| | | 75 | | } |
| | | 76 | | else |
| | | 77 | | { |
| | 0 | 78 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityContextKeyExpired, |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | 10 | 82 | | object hashKey = GetHashKey(token.ContextId, token.KeyGeneration); |
| | 10 | 83 | | bool wasTokenAdded = TryAddItem(hashKey, (SecurityContextSecurityToken)token.Clone(), false); |
| | 10 | 84 | | if (!wasTokenAdded) |
| | | 85 | | { |
| | 0 | 86 | | if (throwOnFailure) |
| | | 87 | | { |
| | 0 | 88 | | if (token.KeyGeneration == null) |
| | | 89 | | { |
| | 0 | 90 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 91 | | } |
| | | 92 | | else |
| | | 93 | | { |
| | 0 | 94 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |
| | 10 | 98 | | return wasTokenAdded; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | private object GetHashKey(UniqueId contextId, UniqueId generation) |
| | | 102 | | { |
| | 30 | 103 | | if (generation == null) |
| | | 104 | | { |
| | 30 | 105 | | return contextId; |
| | | 106 | | } |
| | | 107 | | else |
| | | 108 | | { |
| | 0 | 109 | | return new ContextAndGenerationKey(contextId, generation); |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | public void ClearContexts() |
| | | 114 | | { |
| | 0 | 115 | | ClearItems(); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | public SecurityContextSecurityToken GetContext(UniqueId contextId, UniqueId generation) |
| | | 119 | | { |
| | 20 | 120 | | if (contextId == null) |
| | | 121 | | { |
| | 0 | 122 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contextId)); |
| | | 123 | | } |
| | 20 | 124 | | object hashKey = GetHashKey(contextId, generation); |
| | 20 | 125 | | SecurityContextSecurityToken sct = (SecurityContextSecurityToken)GetItem(hashKey); |
| | 20 | 126 | | return sct != null ? (SecurityContextSecurityToken)sct.Clone() : null; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public void RemoveContext(UniqueId contextId, UniqueId generation, bool throwIfNotPresent) |
| | | 130 | | { |
| | 0 | 131 | | if (contextId == null) |
| | | 132 | | { |
| | 0 | 133 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contextId)); |
| | | 134 | | } |
| | 0 | 135 | | object hashKey = GetHashKey(contextId, generation); |
| | 0 | 136 | | if (!TryRemoveItem(hashKey) && throwIfNotPresent) |
| | | 137 | | { |
| | 0 | 138 | | if (generation == null) |
| | | 139 | | { |
| | 0 | 140 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 141 | | } |
| | | 142 | | else |
| | | 143 | | { |
| | 0 | 144 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 145 | | } |
| | | 146 | | } |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | private ArrayList GetMatchingKeys(UniqueId contextId) |
| | | 150 | | { |
| | 10 | 151 | | if (contextId == null) |
| | | 152 | | { |
| | 0 | 153 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contextId)); |
| | | 154 | | } |
| | 10 | 155 | | ArrayList matchingKeys = new ArrayList(2); |
| | | 156 | | |
| | 10 | 157 | | bool lockHeld = false; |
| | | 158 | | try |
| | | 159 | | { |
| | 10 | 160 | | try { } |
| | | 161 | | finally |
| | | 162 | | { |
| | 10 | 163 | | CacheLock.AcquireReaderLock(-1); |
| | 10 | 164 | | lockHeld = true; |
| | 10 | 165 | | } |
| | 40 | 166 | | foreach (object key in Entries.Keys) |
| | | 167 | | { |
| | | 168 | | bool isMatch; |
| | 10 | 169 | | if (key is UniqueId) |
| | | 170 | | { |
| | 10 | 171 | | isMatch = (((UniqueId)key) == contextId); |
| | | 172 | | } |
| | | 173 | | else |
| | | 174 | | { |
| | 0 | 175 | | isMatch = (((ContextAndGenerationKey)key).ContextId == contextId); |
| | | 176 | | } |
| | 10 | 177 | | if (isMatch) |
| | | 178 | | { |
| | 10 | 179 | | matchingKeys.Add(key); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | } |
| | | 183 | | finally |
| | | 184 | | { |
| | 10 | 185 | | if (lockHeld) |
| | | 186 | | { |
| | 10 | 187 | | CacheLock.ReleaseReaderLock(); |
| | | 188 | | } |
| | 10 | 189 | | } |
| | 10 | 190 | | return matchingKeys; |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | public void RemoveAllContexts(UniqueId contextId) |
| | | 194 | | { |
| | 10 | 195 | | ArrayList matchingKeys = GetMatchingKeys(contextId); |
| | 40 | 196 | | for (int i = 0; i < matchingKeys.Count; ++i) |
| | | 197 | | { |
| | 10 | 198 | | TryRemoveItem(matchingKeys[i]); |
| | | 199 | | } |
| | 10 | 200 | | } |
| | | 201 | | |
| | | 202 | | public void UpdateContextCachingTime(SecurityContextSecurityToken token, DateTime expirationTime) |
| | | 203 | | { |
| | 0 | 204 | | if (token.ValidTo <= expirationTime.ToUniversalTime()) |
| | | 205 | | { |
| | 0 | 206 | | return; |
| | | 207 | | } |
| | 0 | 208 | | TryReplaceItem(GetHashKey(token.ContextId, token.KeyGeneration), token, expirationTime); |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | public Collection<SecurityContextSecurityToken> GetAllContexts(UniqueId contextId) |
| | | 212 | | { |
| | 0 | 213 | | ArrayList matchingKeys = GetMatchingKeys(contextId); |
| | | 214 | | |
| | 0 | 215 | | Collection<SecurityContextSecurityToken> matchingContexts = new Collection<SecurityContextSecurityToken>(); |
| | 0 | 216 | | for (int i = 0; i < matchingKeys.Count; ++i) |
| | | 217 | | { |
| | 0 | 218 | | if (GetItem(matchingKeys[i]) is SecurityContextSecurityToken token) |
| | | 219 | | { |
| | 0 | 220 | | matchingContexts.Add(token); |
| | | 221 | | } |
| | | 222 | | } |
| | 0 | 223 | | return matchingContexts; |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | protected override ArrayList OnQuotaReached(Hashtable cacheTable) |
| | | 227 | | { |
| | 0 | 228 | | if (!_replaceOldestEntries) |
| | | 229 | | { |
| | | 230 | | //SecurityTraceRecordHelper.TraceSecurityContextTokenCacheFull(this.Capacity, 0); |
| | 0 | 231 | | return base.OnQuotaReached(cacheTable); |
| | | 232 | | } |
| | | 233 | | else |
| | | 234 | | { |
| | 0 | 235 | | List<SecurityContextSecurityToken> tokens = new List<SecurityContextSecurityToken>(cacheTable.Count); |
| | 0 | 236 | | foreach (IExpirableItem value in cacheTable.Values) |
| | | 237 | | { |
| | 0 | 238 | | SecurityContextSecurityToken token = (SecurityContextSecurityToken)ExtractItem(value); |
| | 0 | 239 | | tokens.Add(token); |
| | | 240 | | } |
| | 0 | 241 | | tokens.Sort(s_sctEffectiveTimeComparer); |
| | 0 | 242 | | int pruningAmount = (int)(((double)Capacity) * PruningFactor); |
| | 0 | 243 | | pruningAmount = pruningAmount <= 0 ? Capacity : pruningAmount; |
| | 0 | 244 | | ArrayList keys = new ArrayList(pruningAmount); |
| | 0 | 245 | | for (int i = 0; i < pruningAmount; ++i) |
| | | 246 | | { |
| | 0 | 247 | | keys.Add(GetHashKey(tokens[i].ContextId, tokens[i].KeyGeneration)); |
| | 0 | 248 | | OnRemove(tokens[i]); |
| | | 249 | | } |
| | | 250 | | // SecurityTraceRecordHelper.TraceSecurityContextTokenCacheFull(this.Capacity, pruningAmount); |
| | 0 | 251 | | return keys; |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | private sealed class SctEffectiveTimeComparer : IComparer<SecurityContextSecurityToken> |
| | | 256 | | { |
| | | 257 | | public int Compare(SecurityContextSecurityToken sct1, SecurityContextSecurityToken sct2) |
| | | 258 | | { |
| | 0 | 259 | | if (sct1 == sct2) |
| | | 260 | | { |
| | 0 | 261 | | return 0; |
| | | 262 | | } |
| | 0 | 263 | | if (sct1.ValidFrom.ToUniversalTime() < sct2.ValidFrom.ToUniversalTime()) |
| | | 264 | | { |
| | 0 | 265 | | return -1; |
| | | 266 | | } |
| | 0 | 267 | | else if (sct1.ValidFrom.ToUniversalTime() > sct2.ValidFrom.ToUniversalTime()) |
| | | 268 | | { |
| | 0 | 269 | | return 1; |
| | | 270 | | } |
| | | 271 | | else |
| | | 272 | | { |
| | | 273 | | // compare the key effective times |
| | 0 | 274 | | if (sct1.KeyEffectiveTime.ToUniversalTime() < sct2.KeyEffectiveTime.ToUniversalTime()) |
| | | 275 | | { |
| | 0 | 276 | | return -1; |
| | | 277 | | } |
| | 0 | 278 | | else if (sct1.KeyEffectiveTime.ToUniversalTime() > sct2.KeyEffectiveTime.ToUniversalTime()) |
| | | 279 | | { |
| | 0 | 280 | | return 1; |
| | | 281 | | } |
| | | 282 | | else |
| | | 283 | | { |
| | 0 | 284 | | return 0; |
| | | 285 | | } |
| | | 286 | | } |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | protected override void OnRemove(object item) |
| | | 291 | | { |
| | 10 | 292 | | ((IDisposable)item).Dispose(); |
| | 10 | 293 | | base.OnRemove(item); |
| | 10 | 294 | | } |
| | | 295 | | |
| | | 296 | | private struct ContextAndGenerationKey |
| | | 297 | | { |
| | | 298 | | public ContextAndGenerationKey(UniqueId contextId, UniqueId generation) |
| | | 299 | | { |
| | | 300 | | Fx.Assert(contextId != null && generation != null, ""); |
| | 0 | 301 | | ContextId = contextId; |
| | 0 | 302 | | Generation = generation; |
| | 0 | 303 | | } |
| | | 304 | | |
| | 0 | 305 | | public UniqueId ContextId { get; } |
| | | 306 | | |
| | 0 | 307 | | public UniqueId Generation { get; } |
| | | 308 | | |
| | | 309 | | public override int GetHashCode() |
| | | 310 | | { |
| | 0 | 311 | | return ContextId.GetHashCode() ^ Generation.GetHashCode(); |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | public override bool Equals(object obj) |
| | | 315 | | { |
| | 0 | 316 | | if (obj is ContextAndGenerationKey key2) |
| | | 317 | | { |
| | 0 | 318 | | return (key2.ContextId == ContextId && key2.Generation == Generation); |
| | | 319 | | } |
| | | 320 | | else |
| | | 321 | | { |
| | 0 | 322 | | return false; |
| | | 323 | | } |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | public static bool operator ==(ContextAndGenerationKey a, ContextAndGenerationKey b) |
| | | 327 | | { |
| | 0 | 328 | | if (ReferenceEquals(a, null)) |
| | | 329 | | { |
| | 0 | 330 | | return ReferenceEquals(b, null); |
| | | 331 | | } |
| | | 332 | | |
| | 0 | 333 | | return (a.Equals(b)); |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | public static bool operator !=(ContextAndGenerationKey a, ContextAndGenerationKey b) |
| | | 337 | | { |
| | 0 | 338 | | return !(a == b); |
| | | 339 | | } |
| | | 340 | | } |
| | | 341 | | } |
| | | 342 | | } |