GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid re-parsing openssl key material with non-cached provider (#16759) · netty/netty@edffed0

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • OpenSslCachingKeyMaterialProvider.java
      • OpenSslKeyMaterialProvider.java
      • OpenSslX509KeyManagerFactory.java
    • test/java/io/netty/handler/ssl
      • OpenSslCachingKeyMaterialProviderTest.java
      • OpenSslCertificateCompressionTest.java
      • OpenSslKeyMaterialProviderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/OpenSslCachingKeyMaterialProvider.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSslCachingKeyMaterialProvider.java
+12-8Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) t
6767

6868
@Override
6969
void destroy() {
70-
// Remove and release all entries.
71-
do {
72-
Iterator<OpenSslKeyMaterial> iterator = cache.values().iterator();
73-
while (iterator.hasNext()) {
74-
iterator.next().release();
75-
iterator.remove();
76-
}
77-
} while (!cache.isEmpty());
70+
try {
71+
// Remove and release all entries.
72+
do {
73+
Iterator<OpenSslKeyMaterial> iterator = cache.values().iterator();
74+
while (iterator.hasNext()) {
75+
iterator.next().release();
76+
iterator.remove();
77+
}
78+
} while (!cache.isEmpty());
79+
} finally {
80+
super.destroy();
81+
}
7882
}
7983
}
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/OpenSslKeyMaterialProvider.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSslKeyMaterialProvider.java
+91-1Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,30 @@
1818
import io.netty.buffer.ByteBufAllocator;
1919
import io.netty.buffer.UnpooledByteBufAllocator;
2020
import io.netty.internal.tcnative.SSL;
21+
import io.netty.util.IllegalReferenceCountException;
2122

2223
import javax.net.ssl.SSLException;
2324
import javax.net.ssl.X509KeyManager;
2425
import java.security.PrivateKey;
2526
import java.security.cert.X509Certificate;
27+
import java.util.concurrent.atomic.AtomicReference;
2628

2729
import static io.netty.handler.ssl.ReferenceCountedOpenSslContext.toBIO;
2830

2931
/**
3032
* Provides {@link OpenSslKeyMaterial} for a given alias.
3133
*/
3234
class OpenSslKeyMaterialProvider {
35+
private static final MaterialCache SENTINEL_DESTROYED = new MaterialCache(null, null, null);
3336

3437
private final X509KeyManager keyManager;
3538
private final String password;
39+
private final AtomicReference<MaterialCache> cache;
3640

3741
OpenSslKeyMaterialProvider(X509KeyManager keyManager, String password) {
3842
this.keyManager = keyManager;
3943
this.password = password;
44+
cache = new AtomicReference<>();
4045
}
4146

4247
static void validateKeyMaterialSupported(X509Certificate[] keyCertChain, PrivateKey key, String keyPassword,
@@ -117,6 +122,36 @@ OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) t
117122
}
118123

119124
PrivateKey key = keyManager.getPrivateKey(alias);
125+
MaterialCache materialCache = cache.get();
126+
if (materialCache != null && materialCache != SENTINEL_DESTROYED && materialCache.retain()) {
127+
if (materialCache.sameInstances(key, certificates)) {
128+
return materialCache.material(); // We already called `retain()`
129+
} else {
130+
// No match on this cache. Release and build a new one from scratch.
131+
materialCache.release();
132+
}
133+
}
134+
135+
OpenSslKeyMaterial keyMaterial = createKeyMaterial(allocator, certificates, key);
136+
materialCache = new MaterialCache(key, certificates, keyMaterial);
137+
138+
// Retain the new material to put in the cache, then replace and release the old material.
139+
materialCache.retain();
140+
MaterialCache oldMaterial = cache.getAndSet(materialCache);
141+
if (oldMaterial != null) {
142+
if (oldMaterial == SENTINEL_DESTROYED) {
143+
destroyCache(); // Call `destroyCache()` instead of `destroy()` to avoid duplicating other effects.
144+
} else {
145+
oldMaterial.release();
146+
}
147+
}
148+
149+
return keyMaterial;
150+
}
151+
152+
private OpenSslKeyMaterial createKeyMaterial(
153+
ByteBufAllocator allocator, X509Certificate[] certificates, PrivateKey key)
154+
throws Exception {
120155
PemEncoded encoded = PemX509Certificate.toPEM(allocator, true, certificates);
121156
long chainBio = 0;
122157
long pkeyBio = 0;
@@ -157,6 +192,61 @@ OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) t
157192
* Will be invoked once the provider should be destroyed.
158193
*/
159194
void destroy() {
160-
// NOOP.
195+
destroyCache();
196+
}
197+
198+
private void destroyCache() {
199+
MaterialCache oldMaterial;
200+
while ((oldMaterial = cache.getAndSet(SENTINEL_DESTROYED)) != SENTINEL_DESTROYED) {
201+
if (oldMaterial != null) {
202+
oldMaterial.release();
203+
}
204+
}
205+
}
206+
207+
private static final class MaterialCache {
208+
private final PrivateKey key;
209+
private final X509Certificate[] certs;
210+
private final OpenSslKeyMaterial material;
211+
212+
private MaterialCache(PrivateKey key, X509Certificate[] certs, OpenSslKeyMaterial material) {
213+
this.key = key;
214+
this.certs = certs;
215+
this.material = material;
216+
}
217+
218+
OpenSslKeyMaterial material() {
219+
return material;
220+
}
221+
222+
boolean sameInstances(PrivateKey key, X509Certificate[] certs) {
223+
X509Certificate[] existingCerts = this.certs;
224+
int length = existingCerts.length;
225+
if (this.key != key || length != certs.length) {
226+
return false;
227+
}
228+
for (int i = 0; i < length; i++) {
229+
if (certs[i] != existingCerts[i]) {
230+
return false;
231+
}
232+
}
233+
return true;
234+
}
235+
236+
boolean retain() {
237+
if (material.refCnt() != 0) {
238+
try {
239+
material.retain();
240+
return true;
241+
} catch (IllegalReferenceCountException ignore) {
242+
// Fall through to the `return false` below.
243+
}
244+
}
245+
return false;
246+
}
247+
248+
void release() {
249+
material.release();
250+
}
161251
}
162252
}
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/OpenSslX509KeyManagerFactory.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSslX509KeyManagerFactory.java
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,14 @@ OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) t
225225

226226
@Override
227227
void destroy() {
228-
for (Object material: materialMap.values()) {
229-
ReferenceCountUtil.release(material);
228+
try {
229+
for (Object material: materialMap.values()) {
230+
ReferenceCountUtil.release(material);
231+
}
232+
materialMap.clear();
233+
} finally {
234+
super.destroy();
230235
}
231-
materialMap.clear();
232236
}
233237
}
234238
}
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/OpenSslCachingKeyMaterialProviderTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/OpenSslCachingKeyMaterialProviderTest.java
+3-8Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ protected OpenSslKeyMaterialProvider newMaterialProvider(KeyManagerFactory facto
4040
factory.getKeyManagers()), password, Integer.MAX_VALUE);
4141
}
4242

43-
@Override
44-
protected void assertRelease(OpenSslKeyMaterial material) {
45-
assertFalse(material.release());
46-
}
47-
4843
@Test
4944
public void testMaterialCached() throws Exception {
5045
OpenSslKeyMaterialProvider provider = newMaterialProvider(newKeyManagerFactory(), PASSWORD);
@@ -53,14 +48,14 @@ public void testMaterialCached() throws Exception {
5348
assertNotNull(material);
5449
assertNotEquals(0, material.certificateChainAddress());
5550
assertNotEquals(0, material.privateKeyAddress());
56-
assertEquals(2, material.refCnt());
51+
assertEquals(3, material.refCnt());
5752

5853
OpenSslKeyMaterial material2 = provider.chooseKeyMaterial(UnpooledByteBufAllocator.DEFAULT, EXISTING_ALIAS);
5954
assertNotNull(material2);
6055
assertEquals(material.certificateChainAddress(), material2.certificateChainAddress());
6156
assertEquals(material.privateKeyAddress(), material2.privateKeyAddress());
62-
assertEquals(3, material.refCnt());
63-
assertEquals(3, material2.refCnt());
57+
assertEquals(4, material.refCnt());
58+
assertEquals(4, material2.refCnt());
6459

6560
assertFalse(material.release());
6661
assertFalse(material2.release());
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/OpenSslCertificateCompressionTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/OpenSslCertificateCompressionTest.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
3232
import io.netty.handler.ssl.util.SelfSignedCertificate;
3333
import io.netty.internal.tcnative.CertificateCompressionAlgo;
34+
import io.netty.util.concurrent.Future;
3435
import io.netty.util.concurrent.Promise;
3536
import io.netty.util.ReferenceCountUtil;
3637
import org.junit.jupiter.api.Assertions;
@@ -335,9 +336,10 @@ public void runCertCompressionTest(SslContext clientSslContext, SslContext serve
335336
clientChannel.close().syncUninterruptibly();
336337
serverChannel.close().syncUninterruptibly();
337338
} finally {
338-
group.shutdownGracefully();
339+
Future<?> future = group.shutdownGracefully(0, 10, TimeUnit.SECONDS);
339340
ReferenceCountUtil.release(clientSslContext);
340341
ReferenceCountUtil.release(serverSslContext);
342+
future.sync();
341343
}
342344
}
343345

Collapse file

‎handler/src/test/java/io/netty/handler/ssl/OpenSslKeyMaterialProviderTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/OpenSslKeyMaterialProviderTest.java
+8-4Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.security.cert.X509Certificate;
3434

3535
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
import static org.junit.jupiter.api.Assertions.assertFalse;
3637
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3738
import static org.junit.jupiter.api.Assertions.assertNotNull;
3839
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -72,7 +73,7 @@ protected OpenSslKeyMaterialProvider newMaterialProvider(KeyManagerFactory facto
7273
}
7374

7475
protected void assertRelease(OpenSslKeyMaterial material) {
75-
assertTrue(material.release());
76+
assertFalse(material.release());
7677
}
7778

7879
@Test
@@ -89,6 +90,8 @@ public void testChooseKeyMaterial() throws Exception {
8990
assertRelease(material);
9091

9192
provider.destroy();
93+
94+
assertEquals(0, material.refCnt());
9295
}
9396

9497
/**
@@ -173,13 +176,14 @@ public void testChooseOpenSslPrivateKeyMaterial() throws Exception {
173176
OpenSslKeyMaterial material = provider.chooseKeyMaterial(ByteBufAllocator.DEFAULT, keyAlias);
174177
assertNotNull(material);
175178
assertEquals(2, sslPrivateKey.refCnt());
176-
assertEquals(1, material.refCnt());
177-
assertTrue(material.release());
178-
assertEquals(1, sslPrivateKey.refCnt());
179+
assertEquals(2, material.refCnt());
180+
assertFalse(material.release());
181+
assertEquals(2, sslPrivateKey.refCnt());
179182
// Can get material multiple times from the same key
180183
material = provider.chooseKeyMaterial(ByteBufAllocator.DEFAULT, keyAlias);
181184
assertNotNull(material);
182185
assertEquals(2, sslPrivateKey.refCnt());
186+
provider.destroy(); // Destroy single-entry cache.
183187
assertTrue(material.release());
184188
assertTrue(sslPrivateKey.release());
185189
assertEquals(0, sslPrivateKey.refCnt());

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.