Skip to content

Commit 53f005e

Browse files
kofemannmksahakyan
authored andcommitted
pool: use SSLTrustManagerWithHostnameChecking to initialize CAnL
Motivation: JVM uses X509ExtendedTrustManager class to validate the host certificate. The CANL provides two implementations of TrustManagers: SSLTrustManagerWithHostnameChecking, which extends X509ExtendedTrustManager, and SSLTrustManager, which extends X509TrustManager. In case of the latter one, JDK will wrap with a AbstractTrustManagerWrapper implementation, which enforces additional checks, which are not desired. Modification: Update RemoteHttpTransferService to use SSLTrustManagerWithHostnameChecking to initialize CAnL for remote endpoint certificate validation. Result: TPC-HTTP remote endpoint validation performed based on local trusted store and host name. Fixes: #7927 Acked-by: Karen Hoyos Target: master, 11.1, 11.0, 10.2 Require-book: no Require-notes: yes (cherry picked from commit c483a3c) Signed-off-by: Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
1 parent a91e167 commit 53f005e

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

modules/common-security/src/main/java/org/dcache/security/trust/AggregateX509TrustManager.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* dCache - http://www.dcache.org/
22
*
3-
* Copyright (C) 2021 Deutsches Elektronen-Synchrotron
3+
* Copyright (C) 2021 - 2025 Deutsches Elektronen-Synchrotron
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU Affero General Public License as
@@ -19,28 +19,31 @@
1919

2020
import static java.util.Objects.requireNonNull;
2121

22+
import java.net.Socket;
2223
import java.security.cert.CertificateException;
2324
import java.security.cert.X509Certificate;
2425
import java.util.Arrays;
2526
import java.util.List;
27+
import javax.net.ssl.SSLEngine;
28+
import javax.net.ssl.X509ExtendedTrustManager;
2629
import javax.net.ssl.X509TrustManager;
2730

2831
/**
2932
* Aggregate multiple X509TrustManager instances where a certificate chain is accepted if at least
30-
* one of the X509TrustManager instances accepts it.
33+
* one of the X509ExtendedTrustManager instances accepts it.
3134
*/
32-
public class AggregateX509TrustManager implements X509TrustManager {
35+
public class AggregateX509TrustManager extends X509ExtendedTrustManager {
3336

34-
private final List<X509TrustManager> trustManagers;
37+
private final List<X509ExtendedTrustManager> trustManagers;
3538

36-
public AggregateX509TrustManager(List<X509TrustManager> managers) {
39+
public AggregateX509TrustManager(List<X509ExtendedTrustManager> managers) {
3740
trustManagers = requireNonNull(managers);
3841
}
3942

4043
@FunctionalInterface
4144
private interface CertificateCheck {
4245

43-
void appliedTo(X509TrustManager manager) throws CertificateException;
46+
void appliedTo(X509ExtendedTrustManager manager) throws CertificateException;
4447
}
4548

4649
private void genericCheck(CertificateCheck check) throws CertificateException {
@@ -84,6 +87,26 @@ public void checkServerTrusted(X509Certificate[] chain, String authType)
8487
genericCheck(tm -> tm.checkServerTrusted(chain, authType));
8588
}
8689

90+
@Override
91+
public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
92+
genericCheck(tm -> tm.checkServerTrusted(chain, authType, socket));
93+
}
94+
95+
@Override
96+
public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) throws CertificateException {
97+
genericCheck(tm -> tm.checkServerTrusted(chain, authType, socket));
98+
}
99+
100+
@Override
101+
public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
102+
genericCheck(tm -> tm.checkServerTrusted(chain, authType, engine));
103+
}
104+
105+
@Override
106+
public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) throws CertificateException {
107+
genericCheck(tm -> tm.checkServerTrusted(chain, authType, engine));
108+
}
109+
87110
@Override
88111
public X509Certificate[] getAcceptedIssuers() {
89112
return trustManagers.stream()

modules/common-security/src/test/java/org/dcache/security/trust/AggregateX509TrustManagerTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* dCache - http://www.dcache.org/
22
*
3-
* Copyright (C) 2021 Deutsches Elektronen-Synchrotron
3+
* Copyright (C) 2021 - 2025 Deutsches Elektronen-Synchrotron
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU Affero General Public License as
@@ -31,6 +31,7 @@
3131
import java.util.Arrays;
3232
import java.util.List;
3333
import java.util.stream.Collectors;
34+
import javax.net.ssl.X509ExtendedTrustManager;
3435
import javax.net.ssl.X509TrustManager;
3536
import org.junit.Before;
3637
import org.junit.Test;
@@ -39,7 +40,7 @@
3940
public class AggregateX509TrustManagerTest {
4041

4142
private X509TrustManager manager;
42-
private List<X509TrustManager> inner;
43+
private List<X509ExtendedTrustManager> inner;
4344

4445
@Before
4546
public void setup() {
@@ -251,7 +252,7 @@ private MockX509TrustManagerBuilder aTrustManager() {
251252
*/
252253
private static class MockX509TrustManagerBuilder {
253254

254-
private final X509TrustManager manager = mock(X509TrustManager.class);
255+
private final X509ExtendedTrustManager manager = mock(X509ExtendedTrustManager.class);
255256

256257
public MockX509TrustManagerBuilder thatFailsClientsWith(CertificateException e) {
257258
try {
@@ -276,7 +277,7 @@ public MockX509TrustManagerBuilder thatAcceptsIssuers(X509Certificate... issuers
276277
return this;
277278
}
278279

279-
public X509TrustManager build() {
280+
public X509ExtendedTrustManager build() {
280281
return manager;
281282
}
282283
}

modules/dcache/src/main/java/org/dcache/pool/classic/RemoteHttpTransferService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* dCache - http://www.dcache.org/
22
*
3-
* Copyright (C) 2015-2020 Deutsches Elektronen-Synchrotron
3+
* Copyright (C) 2015-2025 Deutsches Elektronen-Synchrotron
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU Affero General Public License as
@@ -26,7 +26,8 @@
2626
import eu.emi.security.authn.x509.ProxySupport;
2727
import eu.emi.security.authn.x509.RevocationParameters;
2828
import eu.emi.security.authn.x509.X509Credential;
29-
import eu.emi.security.authn.x509.helpers.ssl.SSLTrustManager;
29+
import eu.emi.security.authn.x509.helpers.ssl.EnforcingNameMismatchCallback;
30+
import eu.emi.security.authn.x509.helpers.ssl.SSLTrustManagerWithHostnameChecking;
3031
import eu.emi.security.authn.x509.impl.OpensslCertChainValidator;
3132
import eu.emi.security.authn.x509.impl.ValidatorParams;
3233
import java.io.IOException;
@@ -45,6 +46,7 @@
4546
import javax.annotation.PostConstruct;
4647
import javax.net.ssl.KeyManager;
4748
import javax.net.ssl.SSLContext;
49+
import javax.net.ssl.X509ExtendedTrustManager;
4850
import javax.net.ssl.X509TrustManager;
4951
import org.apache.http.HttpRequest;
5052
import org.apache.http.HttpResponse;
@@ -205,7 +207,7 @@ protected SSLContext buildSSLContext(@Nullable KeyManager keyManager)
205207
return context;
206208
}
207209

208-
private X509TrustManager buildTrustManager(Path path) {
210+
private X509ExtendedTrustManager buildTrustManager(Path path) {
209211
var ocspParameters = new OCSPParametes(getOcspCheckingMode());
210212
var revocationParams = new RevocationParameters(getCrlCheckingMode(), ocspParameters);
211213
var validatorParams = new ValidatorParams(revocationParams, ProxySupport.ALLOW);
@@ -214,7 +216,7 @@ private X509TrustManager buildTrustManager(Path path) {
214216
var validator = new OpensslCertChainValidator(path.toString(), true,
215217
getNamespaceMode(), updateInterval, validatorParams, false);
216218
onShutdownTasks.add(validator::dispose);
217-
return new SSLTrustManager(validator);
219+
return new SSLTrustManagerWithHostnameChecking(validator, new EnforcingNameMismatchCallback());
218220
}
219221

220222
@Override

0 commit comments

Comments
 (0)