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 (#1675… · netty/netty@25db4bb

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<MaterialCache>();
4045
}
4146

4247
static void validateKeyMaterialSupported(X509Certificate[] keyCertChain, PrivateKey key, String keyPassword)
@@ -109,6 +114,36 @@ OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) t
109114
}
110115

111116
PrivateKey key = keyManager.getPrivateKey(alias);
117+
MaterialCache materialCache = cache.get();
118+
if (materialCache != null && materialCache != SENTINEL_DESTROYED && materialCache.retain()) {
119+
if (materialCache.sameInstances(key, certificates)) {
120+
return materialCache.material(); // We already called `retain()`
121+
} else {
122+
// No match on this cache. Release and build a new one from scratch.
123+
materialCache.release();
124+
}
125+
}
126+
127+
OpenSslKeyMaterial keyMaterial = createKeyMaterial(allocator, certificates, key);
128+
materialCache = new MaterialCache(key, certificates, keyMaterial);
129+
130+
// Retain the new material to put in the cache, then replace and release the old material.
131+
materialCache.retain();
132+
MaterialCache oldMaterial = cache.getAndSet(materialCache);
133+
if (oldMaterial != null) {
134+
if (oldMaterial == SENTINEL_DESTROYED) {
135+
destroyCache(); // Call `destroyCache()` instead of `destroy()` to avoid duplicating other effects.
136+
} else {
137+
oldMaterial.release();
138+
}
139+
}
140+
141+
return keyMaterial;
142+
}
143+
144+
private OpenSslKeyMaterial createKeyMaterial(
145+
ByteBufAllocator allocator, X509Certificate[] certificates, PrivateKey key)
146+
throws Exception {
112147
PemEncoded encoded = PemX509Certificate.toPEM(allocator, true, certificates);
113148
long chainBio = 0;
114149
long pkeyBio = 0;
@@ -149,6 +184,61 @@ OpenSslKeyMaterial chooseKeyMaterial(ByteBufAllocator allocator, String alias) t
149184
* Will be invoked once the provider should be destroyed.
150185
*/
151186
void destroy() {
152-
// NOOP.
187+
destroyCache();
188+
}
189+
190+
private void destroyCache() {
191+
MaterialCache oldMaterial;
192+
while ((oldMaterial = cache.getAndSet(SENTINEL_DESTROYED)) != SENTINEL_DESTROYED) {
193+
if (oldMaterial != null) {
194+
oldMaterial.release();
195+
}
196+
}
197+
}
198+
199+
private static final class MaterialCache {
200+
private final PrivateKey key;
201+
private final X509Certificate[] certs;
202+
private final OpenSslKeyMaterial material;
203+
204+
private MaterialCache(PrivateKey key, X509Certificate[] certs, OpenSslKeyMaterial material) {
205+
this.key = key;
206+
this.certs = certs;
207+
this.material = material;
208+
}
209+
210+
OpenSslKeyMaterial material() {
211+
return material;
212+
}
213+
214+
boolean sameInstances(PrivateKey key, X509Certificate[] certs) {
215+
X509Certificate[] existingCerts = this.certs;
216+
int length = existingCerts.length;
217+
if (this.key != key || length != certs.length) {
218+
return false;
219+
}
220+
for (int i = 0; i < length; i++) {
221+
if (certs[i] != existingCerts[i]) {
222+
return false;
223+
}
224+
}
225+
return true;
226+
}
227+
228+
boolean retain() {
229+
if (material.refCnt() != 0) {
230+
try {
231+
material.retain();
232+
return true;
233+
} catch (IllegalReferenceCountException ignore) {
234+
// Fall through to the `return false` below.
235+
}
236+
}
237+
return false;
238+
}
239+
240+
void release() {
241+
material.release();
242+
}
153243
}
154244
}
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
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
3131
import io.netty.handler.ssl.util.SelfSignedCertificate;
3232
import io.netty.internal.tcnative.CertificateCompressionAlgo;
33+
import io.netty.util.ReferenceCountUtil;
34+
import io.netty.util.concurrent.Future;
3335
import io.netty.util.concurrent.Promise;
3436
import org.junit.jupiter.api.Assertions;
3537
import org.junit.jupiter.api.BeforeAll;
@@ -333,7 +335,10 @@ public void runCertCompressionTest(SslContext clientSslContext, SslContext serve
333335
clientChannel.close().syncUninterruptibly();
334336
serverChannel.close().syncUninterruptibly();
335337
} finally {
336-
group.shutdownGracefully();
338+
Future<?> future = group.shutdownGracefully(0, 10, TimeUnit.SECONDS);
339+
ReferenceCountUtil.release(clientSslContext);
340+
ReferenceCountUtil.release(serverSslContext);
341+
future.sync();
337342
}
338343
}
339344

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
@@ -32,6 +32,7 @@
3232
import java.security.cert.X509Certificate;
3333

3434
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertFalse;
3536
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3637
import static org.junit.jupiter.api.Assertions.assertNotNull;
3738
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -69,7 +70,7 @@ protected OpenSslKeyMaterialProvider newMaterialProvider(KeyManagerFactory facto
6970
}
7071

7172
protected void assertRelease(OpenSslKeyMaterial material) {
72-
assertTrue(material.release());
73+
assertFalse(material.release());
7374
}
7475

7576
@Test
@@ -86,6 +87,8 @@ public void testChooseKeyMaterial() throws Exception {
8687
assertRelease(material);
8788

8889
provider.destroy();
90+
91+
assertEquals(0, material.refCnt());
8992
}
9093

9194
/**
@@ -164,13 +167,14 @@ public void testChooseOpenSslPrivateKeyMaterial() throws Exception {
164167
OpenSslKeyMaterial material = provider.chooseKeyMaterial(ByteBufAllocator.DEFAULT, keyAlias);
165168
assertNotNull(material);
166169
assertEquals(2, sslPrivateKey.refCnt());
167-
assertEquals(1, material.refCnt());
168-
assertTrue(material.release());
169-
assertEquals(1, sslPrivateKey.refCnt());
170+
assertEquals(2, material.refCnt());
171+
assertFalse(material.release());
172+
assertEquals(2, sslPrivateKey.refCnt());
170173
// Can get material multiple times from the same key
171174
material = provider.chooseKeyMaterial(ByteBufAllocator.DEFAULT, keyAlias);
172175
assertNotNull(material);
173176
assertEquals(2, sslPrivateKey.refCnt());
177+
provider.destroy(); // Destroy single-entry cache.
174178
assertTrue(material.release());
175179
assertTrue(sslPrivateKey.release());
176180
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.