|
16 | 16 | package io.netty.handler.ssl; |
17 | 17 |
|
18 | 18 | import io.netty.buffer.ByteBufAllocator; |
| 19 | +import io.netty.util.IllegalReferenceCountException; |
19 | 20 |
|
20 | 21 | import javax.net.ssl.X509KeyManager; |
21 | 22 | import java.util.concurrent.ConcurrentHashMap; |
22 | 23 |
|
23 | 24 | /** |
24 | | - * {@link OpenSslKeyMaterialProvider} that will cache the |
25 | | - * {@link OpenSslKeyMaterial} to reduce the overhead of parsing the chain |
26 | | - * and the key for generation of the material. |
| 25 | + * {@link OpenSslKeyMaterialProvider} that will cache the {@link OpenSslKeyMaterial} to reduce the overhead |
| 26 | + * of parsing the chain and the key for generation of the material. |
27 | 27 | * <p> |
28 | | - * All cache operations that access or evict entries perform retain/release |
29 | | - * atomically via {@link ConcurrentHashMap}'s per-bucket locking. |
30 | | - * When the cache is full on a cache miss, stale entries (whose alias is no |
31 | | - * longer owned by the {@link X509KeyManager}) are evicted to make room |
32 | | - * before inserting new material. |
| 28 | + * Cache reads are lock-free ({@link ConcurrentHashMap#get} + {@code retain()}); |
| 29 | + * if a concurrent eviction releases the material in between, {@code retain()} |
| 30 | + * detects the dead reference count and the read falls back to a cache miss. |
| 31 | + * Mutations (insert, evict, destroy) use {@link ConcurrentHashMap#compute} / |
| 32 | + * {@link ConcurrentHashMap#computeIfPresent} for atomicity. |
33 | 33 | */ |
34 | 34 | final class OpenSslCachingKeyMaterialProvider extends OpenSslKeyMaterialProvider { |
35 | 35 |
|
36 | | - /** |
37 | | - * A cache that performs retain/release atomically with map operations, |
38 | | - * using {@link ConcurrentHashMap}'s per-bucket locking to prevent |
39 | | - * use-after-free races between concurrent reads and evictions. |
40 | | - */ |
41 | | - private static final class KeyMaterialCache { |
42 | | - |
43 | | - /** |
44 | | - * Backing store for cached key materials. |
45 | | - */ |
46 | | - private final ConcurrentHashMap<String, OpenSslKeyMaterial> map = |
47 | | - new ConcurrentHashMap<String, OpenSslKeyMaterial>(); |
48 | | - |
49 | | - /** |
50 | | - * Returns the material for the given alias with its reference count |
51 | | - * incremented, or {@code null} if absent. |
52 | | - * |
53 | | - * @param alias the alias to look up |
54 | | - * @return the retained material, or {@code null} if not cached |
55 | | - */ |
56 | | - OpenSslKeyMaterial getAndRetain(final String alias) { |
57 | | - final OpenSslKeyMaterial[] result = {null}; |
58 | | - map.computeIfPresent(alias, (final String k, final OpenSslKeyMaterial v) -> { |
59 | | - v.retain(); |
60 | | - result[0] = v; |
61 | | - return v; |
62 | | - }); |
63 | | - return result[0]; |
64 | | - } |
| 36 | + private final int maxCachedEntries; |
| 37 | + private final ConcurrentHashMap<String, OpenSslKeyMaterial> cache = |
| 38 | + new ConcurrentHashMap<String, OpenSslKeyMaterial>(); |
65 | 39 |
|
66 | | - /** |
67 | | - * Inserts {@code material} if absent and returns it retained. If a |
68 | | - * concurrent insert won, returns the existing entry retained instead; |
69 | | - * the caller must then release {@code material}. |
70 | | - * |
71 | | - * @param alias the alias to insert under |
72 | | - * @param material the material to insert |
73 | | - * @return the retained material now in the cache |
74 | | - */ |
75 | | - OpenSslKeyMaterial putIfAbsentAndRetain(final String alias, final OpenSslKeyMaterial material) { |
76 | | - final OpenSslKeyMaterial[] result = {null}; |
77 | | - map.compute(alias, (final String k, final OpenSslKeyMaterial existing) -> { |
78 | | - if (existing != null) { |
79 | | - existing.retain(); |
80 | | - result[0] = existing; |
81 | | - return existing; |
82 | | - } |
83 | | - // Retain for the caller; the map holds the original reference. |
84 | | - material.retain(); |
85 | | - result[0] = material; |
86 | | - return material; |
87 | | - }); |
88 | | - return result[0]; |
89 | | - } |
| 40 | + OpenSslCachingKeyMaterialProvider(X509KeyManager keyManager, String password, int maxEntries) { |
| 41 | + super(keyManager, password); |
| 42 | + maxCachedEntries = maxEntries; |
| 43 | + } |
90 | 44 |
|
91 | | - /** |
92 | | - * Removes and releases the entry for the given alias, if present. |
93 | | - * |
94 | | - * @param alias the alias whose entry should be removed and released |
95 | | - */ |
96 | | - void removeAndRelease(final String alias) { |
97 | | - map.computeIfPresent(alias, (final String k, final OpenSslKeyMaterial v) -> { |
98 | | - v.release(); |
| 45 | + /** |
| 46 | + * Lock-free cache lookup. If a concurrent eviction releases the material between |
| 47 | + * {@code get} and {@code retain}, the dead reference count is detected and treated |
| 48 | + * as a cache miss. |
| 49 | + */ |
| 50 | + private OpenSslKeyMaterial getAndRetain(String alias) { |
| 51 | + OpenSslKeyMaterial m = cache.get(alias); |
| 52 | + if (m != null) { |
| 53 | + try { |
| 54 | + return m.retain(); |
| 55 | + } catch (IllegalReferenceCountException e) { |
99 | 56 | return null; |
100 | | - }); |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * Returns the number of entries in the cache. |
105 | | - * |
106 | | - * @return the cache size |
107 | | - */ |
108 | | - int size() { |
109 | | - return map.size(); |
110 | | - } |
111 | | - |
112 | | - /** |
113 | | - * Returns {@code true} if the cache contains no entries. |
114 | | - * |
115 | | - * @return {@code true} if empty |
116 | | - */ |
117 | | - boolean isEmpty() { |
118 | | - return map.isEmpty(); |
119 | | - } |
120 | | - |
121 | | - /** |
122 | | - * Returns a view of the aliases currently in the cache. |
123 | | - * |
124 | | - * @return the set of aliases |
125 | | - */ |
126 | | - Iterable<String> aliases() { |
127 | | - return map.keySet(); |
| 57 | + } |
128 | 58 | } |
| 59 | + return null; |
129 | 60 | } |
130 | 61 |
|
131 | 62 | /** |
132 | | - * Maximum number of entries to hold in the cache. |
| 63 | + * Atomically inserts material if absent, or retains the existing entry. |
133 | 64 | */ |
134 | | - private final int maxCachedEntries; |
| 65 | + private OpenSslKeyMaterial putIfAbsentAndRetain(String alias, OpenSslKeyMaterial material) { |
| 66 | + return cache.compute(alias, (k, existing) -> { |
| 67 | + if (existing != null) { |
| 68 | + existing.retain(); |
| 69 | + return existing; |
| 70 | + } |
| 71 | + material.retain(); |
| 72 | + return material; |
| 73 | + }); |
| 74 | + } |
135 | 75 |
|
136 | 76 | /** |
137 | | - * The key material cache. |
| 77 | + * Atomically removes and releases the entry for the given alias. |
138 | 78 | */ |
139 | | - private final KeyMaterialCache cache = new KeyMaterialCache(); |
140 | | - |
141 | | - OpenSslCachingKeyMaterialProvider(final X509KeyManager keyManager, final String password, final int maxEntries) { |
142 | | - super(keyManager, password); |
143 | | - maxCachedEntries = maxEntries; |
| 79 | + private void removeAndRelease(String alias) { |
| 80 | + cache.computeIfPresent(alias, (k, v) -> { |
| 81 | + v.release(); |
| 82 | + return null; |
| 83 | + }); |
144 | 84 | } |
145 | 85 |
|
146 | | - /** |
147 | | - * Removes cached entries whose alias is no longer recognized by the key |
148 | | - * manager. Evicting only when full avoids per-insert overhead, which is |
149 | | - * acceptable for most cases where {@code maxCachedEntries} is reasonably |
150 | | - * sized. |
151 | | - */ |
152 | 86 | private void evictStaleEntries() { |
153 | | - for (String alias : cache.aliases()) { |
| 87 | + for (String alias : cache.keySet()) { |
154 | 88 | if (keyManager().getCertificateChain(alias) == null) { |
155 | | - cache.removeAndRelease(alias); |
| 89 | + removeAndRelease(alias); |
156 | 90 | } |
157 | 91 | } |
158 | 92 | } |
159 | 93 |
|
160 | 94 | @Override |
161 | | - OpenSslKeyMaterial chooseKeyMaterial(final ByteBufAllocator allocator, final String alias) throws Exception { |
162 | | - OpenSslKeyMaterial material = cache.getAndRetain(alias); |
| 95 | + OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) throws Exception { |
| 96 | + OpenSslKeyMaterial material = getAndRetain(alias); |
163 | 97 | if (material == null) { |
164 | 98 | material = super.chooseKeyMaterial(allocator, alias); |
165 | 99 | if (material == null) { |
166 | | - // No keymaterial should be used. |
167 | 100 | return null; |
168 | 101 | } |
169 | 102 |
|
170 | 103 | if (cache.size() >= maxCachedEntries) { |
171 | | - // Cache is full; try to evict stale entries to make room. |
172 | 104 | evictStaleEntries(); |
173 | 105 | if (cache.size() >= maxCachedEntries) { |
174 | | - // Still full after eviction, do not cache. |
175 | 106 | return material; |
176 | 107 | } |
177 | 108 | } |
178 | | - OpenSslKeyMaterial old = cache.putIfAbsentAndRetain(alias, material); |
| 109 | + // Returns the newly created material, or an existing entry if another thread inserted first. |
| 110 | + OpenSslKeyMaterial old = putIfAbsentAndRetain(alias, material); |
179 | 111 | if (old != material) { |
180 | | - // Another operation inserted first; release our copy. |
181 | 112 | material.release(); |
182 | 113 | material = old; |
183 | 114 | } |
@@ -191,10 +122,9 @@ int cacheSize() { |
191 | 122 |
|
192 | 123 | @Override |
193 | 124 | void destroy() { |
194 | | - // Remove and release all entries. |
195 | 125 | do { |
196 | | - for (String alias : cache.aliases()) { |
197 | | - cache.removeAndRelease(alias); |
| 126 | + for (String alias : cache.keySet()) { |
| 127 | + removeAndRelease(alias); |
198 | 128 | } |
199 | 129 | } while (!cache.isEmpty()); |
200 | 130 | } |
|