-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
CachingKeyResolver.cs
193 lines (160 loc) · 6.55 KB
/
CachingKeyResolver.cs
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
184
185
186
187
188
189
190
191
192
193
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.KeyVault.Core;
namespace Microsoft.Azure.KeyVault
{
/// <summary>
/// A simple caching Key Resolver using a LRU cache
/// </summary>
public class CachingKeyResolver : IKeyResolver, IDisposable
{
private LRUCache<string, IKey> _cache;
private IKeyResolver _inner;
private bool _isDisposed;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="capacity">The maximim capacity for the cache</param>
/// <param name="inner">The IKeyResolver to wrap</param>
public CachingKeyResolver( int capacity, IKeyResolver inner )
{
if ( inner == null )
throw new ArgumentNullException( "inner" );
_cache = new LRUCache<string, IKey>( capacity );
_inner = inner;
}
#region IKeyResolver
/// <summary>
/// Resolve a key indicated by its ID to the corresponding <see cref="IKey"/>
/// </summary>
/// <param name="kid"> the key identifier </param>
/// <param name="token"> the cancellation token </param>
/// <returns> task result of the <see cref="IKey"/></returns>
public async Task<IKey> ResolveKeyAsync( string kid, CancellationToken token )
{
if ( _isDisposed )
throw new ObjectDisposedException( "CachingKeyResolver" );
if ( string.IsNullOrWhiteSpace( kid ) )
throw new ArgumentNullException( "kid" );
IKey result = _cache.Get( kid );
if ( result == null )
{
result = await _inner.ResolveKeyAsync( kid, token ).ConfigureAwait( false );
if ( result != null )
{
// Cache the resolved key using the result's Kid.
// This is especially for the case when the resolved key contains information about the key version
var cacheKid = string.IsNullOrWhiteSpace( result.Kid ) ? kid : result.Kid;
var cachedKey = new CacheKey(result);
_cache.Add( cacheKid, cachedKey );
return cachedKey;
}
}
return result;
}
#endregion
/// <summary>
/// Disposes the object
/// </summary>
public void Dispose()
{
Dispose( true );
GC.SuppressFinalize( this );
}
protected virtual void Dispose( bool disposing )
{
if ( disposing )
{
if ( !_isDisposed )
{
_isDisposed = true;
foreach (var cacheKey in _cache.OfType<CacheKey>())
{
cacheKey.Dispose(true);
}
_cache.Dispose();
_cache = null;
}
}
}
# region CacheKey class
/// <summary>
/// This class wraps the key that is cached using <see cref="CachingKeyResolver"/>
/// The main purpose of <see cref="CacheKey"/> is to evict disposing cached key from the cache.
/// </summary>
class CacheKey : IKey
{
private readonly IKey _key;
public CacheKey(IKey key)
{
_key = key;
}
public string Kid
{
get { return _key.Kid; }
}
public string DefaultEncryptionAlgorithm
{
get { return _key.DefaultEncryptionAlgorithm; }
}
public string DefaultKeyWrapAlgorithm
{
get { return _key.DefaultKeyWrapAlgorithm; }
}
public string DefaultSignatureAlgorithm
{
get { return _key.DefaultSignatureAlgorithm; }
}
public Task<byte[]> DecryptAsync(byte[] ciphertext, byte[] iv, byte[] authenticationData = null, byte[] authenticationTag = null, string algorithm = null, CancellationToken token = default(CancellationToken))
{
return _key.DecryptAsync(ciphertext, iv, authenticationData, authenticationTag, algorithm, token);
}
public Task<Tuple<byte[], byte[], string>> EncryptAsync(byte[] plaintext, byte[] iv, byte[] authenticationData = null, string algorithm = null, CancellationToken token = default(CancellationToken))
{
return _key.EncryptAsync(plaintext, iv, authenticationData, algorithm, token);
}
public Task<Tuple<byte[], string>> WrapKeyAsync(byte[] key, string algorithm = null, CancellationToken token = default(CancellationToken))
{
return _key.WrapKeyAsync(key, algorithm, token);
}
public Task<byte[]> UnwrapKeyAsync(byte[] encryptedKey, string algorithm = null, CancellationToken token = default(CancellationToken))
{
return _key.UnwrapKeyAsync(encryptedKey, algorithm, token);
}
public Task<Tuple<byte[], string>> SignAsync(byte[] digest, string algorithm = null, CancellationToken token = default(CancellationToken))
{
return _key.SignAsync(digest, algorithm, token);
}
public Task<bool> VerifyAsync(byte[] digest, byte[] signature, string algorithm = null, CancellationToken token = default(CancellationToken))
{
return _key.VerifyAsync(digest, signature, algorithm, token);
}
public void Dispose()
{
// do not dispose because there may be multiple references to the cached object
}
/// <summary>
/// Disposes the cached key only when cache is disposing
/// </summary>
/// <param name="force"> whether to force dispose </param>
internal void Dispose(bool force)
{
Dispose(true, force);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing, bool force)
{
if (disposing & force)
{
_key.Dispose();
}
}
}
# endregion CacheKey class
}
}