| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Microsoft.Azure.KeyVault.Core; |
| | 10 | |
|
| | 11 | | namespace Microsoft.Azure.KeyVault |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// A simple caching Key Resolver using a LRU cache |
| | 15 | | /// </summary> |
| | 16 | | public class CachingKeyResolver : IKeyResolver, IDisposable |
| | 17 | | { |
| | 18 | | private LRUCache<string, IKey> _cache; |
| | 19 | | private IKeyResolver _inner; |
| | 20 | | private bool _isDisposed; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Constructor. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="capacity">The maximim capacity for the cache</param> |
| | 26 | | /// <param name="inner">The IKeyResolver to wrap</param> |
| 12 | 27 | | public CachingKeyResolver( int capacity, IKeyResolver inner ) |
| | 28 | | { |
| 12 | 29 | | if ( inner == null ) |
| 0 | 30 | | throw new ArgumentNullException( "inner" ); |
| | 31 | |
|
| 12 | 32 | | _cache = new LRUCache<string, IKey>( capacity ); |
| 12 | 33 | | _inner = inner; |
| 12 | 34 | | } |
| | 35 | |
|
| | 36 | | #region IKeyResolver |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Resolve a key indicated by its ID to the corresponding <see cref="IKey"/> |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="kid"> the key identifier </param> |
| | 42 | | /// <param name="token"> the cancellation token </param> |
| | 43 | | /// <returns> task result of the <see cref="IKey"/></returns> |
| | 44 | | public async Task<IKey> ResolveKeyAsync( string kid, CancellationToken token ) |
| | 45 | | { |
| 108 | 46 | | if ( _isDisposed ) |
| 0 | 47 | | throw new ObjectDisposedException( "CachingKeyResolver" ); |
| | 48 | |
|
| 108 | 49 | | if ( string.IsNullOrWhiteSpace( kid ) ) |
| 0 | 50 | | throw new ArgumentNullException( "kid" ); |
| | 51 | |
|
| 108 | 52 | | IKey result = _cache.Get( kid ); |
| | 53 | |
|
| 108 | 54 | | if ( result == null ) |
| | 55 | | { |
| 36 | 56 | | result = await _inner.ResolveKeyAsync( kid, token ).ConfigureAwait( false ); |
| 32 | 57 | | if ( result != null ) |
| | 58 | | { |
| | 59 | | // Cache the resolved key using the result's Kid. |
| | 60 | | // This is especially for the case when the resolved key contains information about the key version |
| 32 | 61 | | var cacheKid = string.IsNullOrWhiteSpace( result.Kid ) ? kid : result.Kid; |
| | 62 | |
|
| 32 | 63 | | var cachedKey = new CacheKey(result); |
| 32 | 64 | | _cache.Add( cacheKid, cachedKey ); |
| 32 | 65 | | return cachedKey; |
| | 66 | | } |
| | 67 | | } |
| | 68 | |
|
| 72 | 69 | | return result; |
| 104 | 70 | | } |
| | 71 | |
|
| | 72 | | #endregion |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Disposes the object |
| | 76 | | /// </summary> |
| | 77 | | public void Dispose() |
| | 78 | | { |
| 14 | 79 | | Dispose( true ); |
| 14 | 80 | | GC.SuppressFinalize( this ); |
| 14 | 81 | | } |
| | 82 | |
|
| | 83 | | protected virtual void Dispose( bool disposing ) |
| | 84 | | { |
| 14 | 85 | | if ( disposing ) |
| | 86 | | { |
| 14 | 87 | | if ( !_isDisposed ) |
| | 88 | | { |
| 12 | 89 | | _isDisposed = true; |
| | 90 | |
|
| 52 | 91 | | foreach (var cacheKey in _cache.OfType<CacheKey>()) |
| | 92 | | { |
| 14 | 93 | | cacheKey.Dispose(true); |
| | 94 | | } |
| | 95 | |
|
| 12 | 96 | | _cache.Dispose(); |
| 12 | 97 | | _cache = null; |
| | 98 | | } |
| | 99 | | } |
| 14 | 100 | | } |
| | 101 | |
|
| | 102 | | # region CacheKey class |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// This class wraps the key that is cached using <see cref="CachingKeyResolver"/> |
| | 106 | | /// The main purpose of <see cref="CacheKey"/> is to evict disposing cached key from the cache. |
| | 107 | | /// </summary> |
| | 108 | | class CacheKey : IKey |
| | 109 | | { |
| | 110 | | private readonly IKey _key; |
| | 111 | |
|
| 32 | 112 | | public CacheKey(IKey key) |
| | 113 | | { |
| 32 | 114 | | _key = key; |
| 32 | 115 | | } |
| | 116 | |
|
| | 117 | | public string Kid |
| | 118 | | { |
| 0 | 119 | | get { return _key.Kid; } |
| | 120 | | } |
| | 121 | |
|
| | 122 | | public string DefaultEncryptionAlgorithm |
| | 123 | | { |
| 0 | 124 | | get { return _key.DefaultEncryptionAlgorithm; } |
| | 125 | | } |
| | 126 | |
|
| | 127 | | public string DefaultKeyWrapAlgorithm |
| | 128 | | { |
| 0 | 129 | | get { return _key.DefaultKeyWrapAlgorithm; } |
| | 130 | | } |
| | 131 | |
|
| | 132 | | public string DefaultSignatureAlgorithm |
| | 133 | | { |
| 0 | 134 | | get { return _key.DefaultSignatureAlgorithm; } |
| | 135 | | } |
| | 136 | |
|
| | 137 | | public Task<byte[]> DecryptAsync(byte[] ciphertext, byte[] iv, byte[] authenticationData = null, byte[] auth |
| | 138 | | { |
| 0 | 139 | | return _key.DecryptAsync(ciphertext, iv, authenticationData, authenticationTag, algorithm, token); |
| | 140 | | } |
| | 141 | |
|
| | 142 | | public Task<Tuple<byte[], byte[], string>> EncryptAsync(byte[] plaintext, byte[] iv, byte[] authenticationDa |
| | 143 | | { |
| 0 | 144 | | return _key.EncryptAsync(plaintext, iv, authenticationData, algorithm, token); |
| | 145 | | } |
| | 146 | |
|
| | 147 | | public Task<Tuple<byte[], string>> WrapKeyAsync(byte[] key, string algorithm = null, CancellationToken token |
| | 148 | | { |
| 0 | 149 | | return _key.WrapKeyAsync(key, algorithm, token); |
| | 150 | | } |
| | 151 | |
|
| | 152 | | public Task<byte[]> UnwrapKeyAsync(byte[] encryptedKey, string algorithm = null, CancellationToken token = d |
| | 153 | | { |
| 0 | 154 | | return _key.UnwrapKeyAsync(encryptedKey, algorithm, token); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | public Task<Tuple<byte[], string>> SignAsync(byte[] digest, string algorithm = null, CancellationToken token |
| | 158 | | { |
| 0 | 159 | | return _key.SignAsync(digest, algorithm, token); |
| | 160 | | } |
| | 161 | |
|
| | 162 | | public Task<bool> VerifyAsync(byte[] digest, byte[] signature, string algorithm = null, CancellationToken to |
| | 163 | | { |
| 0 | 164 | | return _key.VerifyAsync(digest, signature, algorithm, token); |
| | 165 | | } |
| | 166 | |
|
| | 167 | | public void Dispose() |
| | 168 | | { |
| | 169 | | // do not dispose because there may be multiple references to the cached object |
| 2 | 170 | | } |
| | 171 | |
|
| | 172 | | /// <summary> |
| | 173 | | /// Disposes the cached key only when cache is disposing |
| | 174 | | /// </summary> |
| | 175 | | /// <param name="force"> whether to force dispose </param> |
| | 176 | | internal void Dispose(bool force) |
| | 177 | | { |
| 14 | 178 | | Dispose(true, force); |
| 14 | 179 | | GC.SuppressFinalize(this); |
| 14 | 180 | | } |
| | 181 | |
|
| | 182 | | private void Dispose(bool disposing, bool force) |
| | 183 | | { |
| 14 | 184 | | if (disposing & force) |
| | 185 | | { |
| 14 | 186 | | _key.Dispose(); |
| | 187 | | } |
| 14 | 188 | | } |
| | 189 | | } |
| | 190 | |
|
| | 191 | | # endregion CacheKey class |
| | 192 | | } |
| | 193 | | } |