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 — Backport the SslContextBuilder.endpointIdentificationAlgorithm method… · netty/netty@0deca94

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src/main/java/io/netty/handler/ssl
    • Java7SslParametersUtils.java
    • JdkSslClientContext.java
    • JdkSslContext.java
    • OpenSslClientContext.java
    • OpenSslContext.java
    • OpenSslEngine.java
    • ReferenceCountedOpenSslClientContext.java
    • ReferenceCountedOpenSslContext.java
    • ReferenceCountedOpenSslEngine.java
    • ReferenceCountedOpenSslServerContext.java
    • SslContext.java
    • SslContextBuilder.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/Java7SslParametersUtils.java
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ private Java7SslParametersUtils() {
3535
static void setAlgorithmConstraints(SSLParameters sslParameters, Object algorithmConstraints) {
3636
sslParameters.setAlgorithmConstraints((AlgorithmConstraints) algorithmConstraints);
3737
}
38+
39+
/**
40+
* Utility method for calling the {@code setEndpointIdentitificationAlgorithm} method,
41+
* which was introduced in Java 7.
42+
*/
43+
@SuppressJava6Requirement(reason = "Usage guarded by java version check")
44+
static void setEndpointIdentificationAlgorithm(
45+
SSLParameters sslParameters, String endpointIdentificationAlgorithm) {
46+
sslParameters.setEndpointIdentificationAlgorithm(endpointIdentificationAlgorithm);
47+
}
3848
}
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/JdkSslClientContext.java
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,13 @@ public JdkSslClientContext(File trustCertCollectionFile, TrustManagerFactory tru
269269
X509Certificate[] keyCertChain, PrivateKey key, String keyPassword,
270270
KeyManagerFactory keyManagerFactory, Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
271271
ApplicationProtocolConfig apn, String[] protocols, long sessionCacheSize, long sessionTimeout,
272-
SecureRandom secureRandom, String keyStoreType)
272+
SecureRandom secureRandom, String keyStoreType, String endpointIdentificationAlgorithm)
273273
throws SSLException {
274274
super(newSSLContext(sslContextProvider, trustCertCollection, trustManagerFactory,
275275
keyCertChain, key, keyPassword, keyManagerFactory, sessionCacheSize,
276276
sessionTimeout, secureRandom, keyStoreType),
277-
true, ciphers, cipherFilter, toNegotiator(apn, false), ClientAuth.NONE, protocols, false);
277+
true, ciphers, cipherFilter, toNegotiator(apn, false), ClientAuth.NONE, protocols, false,
278+
endpointIdentificationAlgorithm);
278279
}
279280

280281
private static SSLContext newSSLContext(Provider sslContextProvider,
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/JdkSslContext.java
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.netty.buffer.ByteBufAllocator;
2020
import io.netty.util.ReferenceCountUtil;
2121
import io.netty.util.internal.EmptyArrays;
22+
import io.netty.util.internal.PlatformDependent;
2223
import io.netty.util.internal.logging.InternalLogger;
2324
import io.netty.util.internal.logging.InternalLoggerFactory;
2425

@@ -41,11 +42,11 @@
4142
import java.util.LinkedHashSet;
4243
import java.util.List;
4344
import java.util.Set;
44-
4545
import javax.crypto.NoSuchPaddingException;
4646
import javax.net.ssl.KeyManagerFactory;
4747
import javax.net.ssl.SSLContext;
4848
import javax.net.ssl.SSLEngine;
49+
import javax.net.ssl.SSLParameters;
4950
import javax.net.ssl.SSLSessionContext;
5051

5152
import static io.netty.handler.ssl.SslUtils.DEFAULT_CIPHER_SUITES;
@@ -190,6 +191,7 @@ private static boolean isTlsV13Supported(String[] protocols) {
190191
private final ClientAuth clientAuth;
191192
private final SSLContext sslContext;
192193
private final boolean isClient;
194+
private final String endpointIdentificationAlgorithm;
193195

194196
/**
195197
* Creates a new {@link JdkSslContext} from a pre-configured {@link SSLContext}.
@@ -259,10 +261,18 @@ public JdkSslContext(SSLContext sslContext,
259261
@SuppressWarnings("deprecation")
260262
JdkSslContext(SSLContext sslContext, boolean isClient, Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
261263
JdkApplicationProtocolNegotiator apn, ClientAuth clientAuth, String[] protocols, boolean startTls) {
264+
this(sslContext, isClient, ciphers, cipherFilter, apn, clientAuth, protocols, startTls, null);
265+
}
266+
267+
@SuppressWarnings("deprecation")
268+
JdkSslContext(SSLContext sslContext, boolean isClient, Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
269+
JdkApplicationProtocolNegotiator apn, ClientAuth clientAuth, String[] protocols, boolean startTls,
270+
String endpointIdentificationAlgorithm) {
262271
super(startTls);
263272
this.apn = checkNotNull(apn, "apn");
264273
this.clientAuth = checkNotNull(clientAuth, "clientAuth");
265274
this.sslContext = checkNotNull(sslContext, "sslContext");
275+
this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm;
266276

267277
final List<String> defaultCiphers;
268278
final Set<String> supportedCiphers;
@@ -366,6 +376,7 @@ private SSLEngine configureAndWrapEngine(SSLEngine engine, ByteBufAllocator allo
366376
throw new Error("Unknown auth " + clientAuth);
367377
}
368378
}
379+
configureEndpointVerification(engine);
369380
JdkApplicationProtocolNegotiator.SslEngineWrapperFactory factory = apn.wrapperFactory();
370381
if (factory instanceof JdkApplicationProtocolNegotiator.AllocatorAwareSslEngineWrapperFactory) {
371382
return ((JdkApplicationProtocolNegotiator.AllocatorAwareSslEngineWrapperFactory) factory)
@@ -374,6 +385,15 @@ private SSLEngine configureAndWrapEngine(SSLEngine engine, ByteBufAllocator allo
374385
return factory.wrapSslEngine(engine, apn, isServer());
375386
}
376387

388+
private void configureEndpointVerification(SSLEngine engine) {
389+
int version = PlatformDependent.javaVersion();
390+
if (version >= 7) {
391+
SSLParameters params = engine.getSSLParameters();
392+
Java7SslParametersUtils.setEndpointIdentificationAlgorithm(params, endpointIdentificationAlgorithm);
393+
engine.setSSLParameters(params);
394+
}
395+
}
396+
377397
@Override
378398
public final JdkApplicationProtocolNegotiator applicationProtocolNegotiator() {
379399
return apn;
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSslClientContext.java
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,18 @@ public OpenSslClientContext(File trustCertCollectionFile, TrustManagerFactory tr
178178
this(toX509CertificatesInternal(trustCertCollectionFile), trustManagerFactory,
179179
toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword),
180180
keyPassword, keyManagerFactory, ciphers, cipherFilter, apn, null, sessionCacheSize,
181-
sessionTimeout, false, KeyStore.getDefaultType());
181+
sessionTimeout, false, KeyStore.getDefaultType(), null);
182182
}
183183

184184
OpenSslClientContext(X509Certificate[] trustCertCollection, TrustManagerFactory trustManagerFactory,
185185
X509Certificate[] keyCertChain, PrivateKey key, String keyPassword,
186-
KeyManagerFactory keyManagerFactory, Iterable<String> ciphers,
187-
CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn, String[] protocols,
188-
long sessionCacheSize, long sessionTimeout, boolean enableOcsp, String keyStore,
189-
Map.Entry<SslContextOption<?>, Object>... options)
186+
KeyManagerFactory keyManagerFactory, Iterable<String> ciphers,
187+
CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn, String[] protocols,
188+
long sessionCacheSize, long sessionTimeout, boolean enableOcsp, String keyStore,
189+
String endpointIdentificationAlgorithm, Map.Entry<SslContextOption<?>, Object>... options)
190190
throws SSLException {
191191
super(ciphers, cipherFilter, apn, SSL.SSL_MODE_CLIENT, keyCertChain,
192-
ClientAuth.NONE, protocols, false, enableOcsp, options);
192+
ClientAuth.NONE, protocols, false, endpointIdentificationAlgorithm, enableOcsp, options);
193193
boolean success = false;
194194
try {
195195
OpenSslKeyMaterialProvider.validateKeyMaterialSupported(keyCertChain, key, keyPassword);
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSslContext.java
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
public abstract class OpenSslContext extends ReferenceCountedOpenSslContext {
3131
OpenSslContext(Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apnCfg,
3232
int mode, Certificate[] keyCertChain,
33-
ClientAuth clientAuth, String[] protocols, boolean startTls, boolean enableOcsp,
34-
Map.Entry<SslContextOption<?>, Object>... options)
33+
ClientAuth clientAuth, String[] protocols, boolean startTls, String endpointIdentificationAlgorithm,
34+
boolean enableOcsp, Map.Entry<SslContextOption<?>, Object>... options)
3535
throws SSLException {
3636
super(ciphers, cipherFilter, toNegotiator(apnCfg), mode, keyCertChain,
37-
clientAuth, protocols, startTls, enableOcsp, false, options);
37+
clientAuth, protocols, startTls, endpointIdentificationAlgorithm, enableOcsp, false, options);
3838
}
3939

4040
OpenSslContext(Iterable<String> ciphers, CipherSuiteFilter cipherFilter, OpenSslApplicationProtocolNegotiator apn,
@@ -43,12 +43,13 @@ public abstract class OpenSslContext extends ReferenceCountedOpenSslContext {
4343
Map.Entry<SslContextOption<?>, Object>... options)
4444
throws SSLException {
4545
super(ciphers, cipherFilter, apn, mode, keyCertChain,
46-
clientAuth, protocols, startTls, enableOcsp, false, options);
46+
clientAuth, protocols, startTls, null, enableOcsp, false, options);
4747
}
4848

4949
@Override
5050
final SSLEngine newEngine0(ByteBufAllocator alloc, String peerHost, int peerPort, boolean jdkCompatibilityMode) {
51-
return new OpenSslEngine(this, alloc, peerHost, peerPort, jdkCompatibilityMode);
51+
return new OpenSslEngine(this, alloc, peerHost, peerPort, jdkCompatibilityMode,
52+
endpointIdentificationAlgorithm);
5253
}
5354

5455
@Override
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
*/
2929
public final class OpenSslEngine extends ReferenceCountedOpenSslEngine {
3030
OpenSslEngine(OpenSslContext context, ByteBufAllocator alloc, String peerHost, int peerPort,
31-
boolean jdkCompatibilityMode) {
32-
super(context, alloc, peerHost, peerPort, jdkCompatibilityMode, false);
31+
boolean jdkCompatibilityMode, String endpointIdentificationAlgorithm) {
32+
super(context, alloc, peerHost, peerPort, jdkCompatibilityMode, false, endpointIdentificationAlgorithm);
3333
}
3434

3535
@Override
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslClientContext.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public final class ReferenceCountedOpenSslClientContext extends ReferenceCounted
6363
KeyManagerFactory keyManagerFactory, Iterable<String> ciphers,
6464
CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
6565
String[] protocols, long sessionCacheSize, long sessionTimeout,
66-
boolean enableOcsp, String keyStore,
66+
boolean enableOcsp, String keyStore, String endpointIdentificationAlgorithm,
6767
Map.Entry<SslContextOption<?>, Object>... options) throws SSLException {
6868
super(ciphers, cipherFilter, toNegotiator(apn), SSL.SSL_MODE_CLIENT, keyCertChain,
69-
ClientAuth.NONE, protocols, false, enableOcsp, true, options);
69+
ClientAuth.NONE, protocols, false, endpointIdentificationAlgorithm, enableOcsp, true, options);
7070
boolean success = false;
7171
try {
7272
sessionContext = newSessionContext(this, ctx, engineMap, trustCertCollection, trustManagerFactory,
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslContext.java
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ protected void deallocate() {
153153
final Certificate[] keyCertChain;
154154
final ClientAuth clientAuth;
155155
final String[] protocols;
156+
final String endpointIdentificationAlgorithm;
156157
final boolean hasTLSv13Cipher;
157158

158159
final boolean enableOcsp;
@@ -208,7 +209,8 @@ public ApplicationProtocolConfig.SelectedListenerFailureBehavior selectedListene
208209

209210
ReferenceCountedOpenSslContext(Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
210211
OpenSslApplicationProtocolNegotiator apn, int mode, Certificate[] keyCertChain,
211-
ClientAuth clientAuth, String[] protocols, boolean startTls, boolean enableOcsp,
212+
ClientAuth clientAuth, String[] protocols, boolean startTls,
213+
String endpointIdentificationAlgorithm, boolean enableOcsp,
212214
boolean leakDetection, Map.Entry<SslContextOption<?>, Object>... ctxOptions)
213215
throws SSLException {
214216
super(startTls);
@@ -264,6 +266,7 @@ public ApplicationProtocolConfig.SelectedListenerFailureBehavior selectedListene
264266
this.mode = mode;
265267
this.clientAuth = isServer() ? checkNotNull(clientAuth, "clientAuth") : ClientAuth.NONE;
266268
this.protocols = protocols == null ? OpenSsl.defaultProtocols(mode == SSL.SSL_MODE_CLIENT) : protocols;
269+
this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm;
267270
this.enableOcsp = enableOcsp;
268271

269272
this.keyCertChain = keyCertChain == null ? null : keyCertChain.clone();
@@ -491,7 +494,8 @@ protected SslHandler newHandler(ByteBufAllocator alloc, String peerHost, int pee
491494
}
492495

493496
SSLEngine newEngine0(ByteBufAllocator alloc, String peerHost, int peerPort, boolean jdkCompatibilityMode) {
494-
return new ReferenceCountedOpenSslEngine(this, alloc, peerHost, peerPort, jdkCompatibilityMode, true);
497+
return new ReferenceCountedOpenSslEngine(this, alloc, peerHost, peerPort, jdkCompatibilityMode, true,
498+
endpointIdentificationAlgorithm);
495499
}
496500

497501
/**
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java
+19-10Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ protected void deallocate() {
185185

186186
private volatile ClientAuth clientAuth = ClientAuth.NONE;
187187

188-
private String endPointIdentificationAlgorithm;
188+
private String endpointIdentificationAlgorithm;
189189
// Store as object as AlgorithmConstraints only exists since java 7.
190190
private Object algorithmConstraints;
191191
private List<String> sniHostNames;
@@ -225,7 +225,8 @@ protected void deallocate() {
225225
* @param leakDetection {@code true} to enable leak detection of this object.
226226
*/
227227
ReferenceCountedOpenSslEngine(ReferenceCountedOpenSslContext context, final ByteBufAllocator alloc, String peerHost,
228-
int peerPort, boolean jdkCompatibilityMode, boolean leakDetection) {
228+
int peerPort, boolean jdkCompatibilityMode, boolean leakDetection,
229+
String endpointIdentificationAlgorithm) {
229230
super(peerHost, peerPort);
230231
OpenSsl.ensureAvailability();
231232
engineMap = context.engineMap;
@@ -234,6 +235,7 @@ protected void deallocate() {
234235
this.alloc = checkNotNull(alloc, "alloc");
235236
apn = (OpenSslApplicationProtocolNegotiator) context.applicationProtocolNegotiator();
236237
clientMode = context.isClient();
238+
this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm;
237239

238240
if (PlatformDependent.javaVersion() >= 7) {
239241
session = new ExtendedOpenSslSession(new DefaultOpenSslSession(context.sessionContext())) {
@@ -387,6 +389,9 @@ public List<byte[]> getStatusResponses() {
387389
}
388390
// setMode may impact the overhead.
389391
calculateMaxWrapOverhead();
392+
393+
// Configure any endpoint verification specified by the SslContext.
394+
configureEndpointVerification(endpointIdentificationAlgorithm);
390395
} catch (Throwable cause) {
391396
// Call shutdown so we are sure we correctly release all native memory and also guard against the
392397
// case when shutdown() will be called by the finalizer again.
@@ -2188,7 +2193,7 @@ public final synchronized SSLParameters getSSLParameters() {
21882193

21892194
int version = PlatformDependent.javaVersion();
21902195
if (version >= 7) {
2191-
sslParameters.setEndpointIdentificationAlgorithm(endPointIdentificationAlgorithm);
2196+
Java7SslParametersUtils.setEndpointIdentificationAlgorithm(sslParameters, endpointIdentificationAlgorithm);
21922197
Java7SslParametersUtils.setAlgorithmConstraints(sslParameters, algorithmConstraints);
21932198
if (version >= 8) {
21942199
if (sniHostNames != null) {
@@ -2233,20 +2238,24 @@ public final synchronized void setSSLParameters(SSLParameters sslParameters) {
22332238
matchers = sslParameters.getSNIMatchers();
22342239
}
22352240

2236-
final String endPointIdentificationAlgorithm = sslParameters.getEndpointIdentificationAlgorithm();
2241+
final String endpointIdentificationAlgorithm = sslParameters.getEndpointIdentificationAlgorithm();
22372242
if (!isDestroyed) {
2238-
// If the user asks for hostname verification we must ensure we verify the peer.
2239-
// If the user disables hostname verification we leave it up to the user to change the mode manually.
2240-
if (clientMode && isEndPointVerificationEnabled(endPointIdentificationAlgorithm)) {
2241-
SSL.setVerify(ssl, SSL.SSL_CVERIFY_REQUIRED, -1);
2242-
}
2243+
configureEndpointVerification(endpointIdentificationAlgorithm);
22432244
}
2244-
this.endPointIdentificationAlgorithm = endPointIdentificationAlgorithm;
2245+
this.endpointIdentificationAlgorithm = endpointIdentificationAlgorithm;
22452246
algorithmConstraints = sslParameters.getAlgorithmConstraints();
22462247
}
22472248
super.setSSLParameters(sslParameters);
22482249
}
22492250

2251+
private void configureEndpointVerification(String endpointIdentificationAlgorithm) {
2252+
// If the user asks for hostname verification we must ensure we verify the peer.
2253+
// If the user disables hostname verification we leave it up to the user to change the mode manually.
2254+
if (clientMode && isEndPointVerificationEnabled(endpointIdentificationAlgorithm)) {
2255+
SSL.setVerify(ssl, SSL.SSL_CVERIFY_REQUIRED, -1);
2256+
}
2257+
}
2258+
22502259
private static boolean isEndPointVerificationEnabled(String endPointIdentificationAlgorithm) {
22512260
return endPointIdentificationAlgorithm != null && !endPointIdentificationAlgorithm.isEmpty();
22522261
}
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslServerContext.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ cipherFilter, toNegotiator(apn), sessionCacheSize, sessionTimeout, clientAuth, p
7272
boolean enableOcsp, String keyStore, Map.Entry<SslContextOption<?>, Object>... options)
7373
throws SSLException {
7474
super(ciphers, cipherFilter, apn, SSL.SSL_MODE_SERVER, keyCertChain,
75-
clientAuth, protocols, startTls, enableOcsp, true, options);
75+
clientAuth, protocols, startTls,
76+
null, // No endpoint validation for servers.
77+
enableOcsp, true, options);
7678
// Create a new SSL_CTX and configure it.
7779
boolean success = false;
7880
try {

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.