Skip to content

Commit 977b9b4

Browse files
committed
fix: use BC trust managers when resolving SSL context
1 parent 166d83c commit 977b9b4

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

  • google-http-client/src/main/java/com/google/api/client/util

google-http-client/src/main/java/com/google/api/client/util/SslUtils.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1818
import java.security.GeneralSecurityException;
1919
import java.security.KeyStore;
20+
import java.security.KeyStoreException;
2021
import java.security.NoSuchAlgorithmException;
2122
import java.security.NoSuchProviderException;
2223
import java.security.Provider;
@@ -164,8 +165,8 @@ public static KeyManagerFactory getPkixKeyManagerFactory() throws NoSuchAlgorith
164165
public static SSLContext initSslContext(
165166
SSLContext sslContext, KeyStore trustStore, TrustManagerFactory trustManagerFactory)
166167
throws GeneralSecurityException {
167-
trustManagerFactory.init(trustStore);
168-
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
168+
sslContext.init(
169+
null, getCompatibleTrustManagers(sslContext, trustStore, trustManagerFactory), null);
169170
return sslContext;
170171
}
171172

@@ -195,13 +196,38 @@ public static SSLContext initSslContext(
195196
String mtlsKeyStorePassword,
196197
KeyManagerFactory keyManagerFactory)
197198
throws GeneralSecurityException {
198-
trustManagerFactory.init(trustStore);
199199
keyManagerFactory.init(mtlsKeyStore, mtlsKeyStorePassword.toCharArray());
200200
sslContext.init(
201-
keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
201+
keyManagerFactory.getKeyManagers(),
202+
getCompatibleTrustManagers(sslContext, trustStore, trustManagerFactory),
203+
null);
202204
return sslContext;
203205
}
204206

207+
/**
208+
* Resolves trust managers compatible with the active security provider. If the SSLContext is
209+
* managed by the Bouncy Castle JJSSE provider, it retrieves Bouncy Castle's native trust managers
210+
* instead of standard JDK trust managers. This prevents JCA trust manager wrapping mismatches and
211+
* unresolved peer host certificate exceptions on strict JVMs (e.g., Java 8/21).
212+
*/
213+
private static TrustManager[] getCompatibleTrustManagers(
214+
SSLContext sslContext, KeyStore trustStore, TrustManagerFactory trustManagerFactory)
215+
throws GeneralSecurityException {
216+
if (sslContext.getProvider() instanceof BouncyCastleJsseProvider) {
217+
try {
218+
TrustManagerFactory bcTmf =
219+
TrustManagerFactory.getInstance(
220+
trustManagerFactory.getAlgorithm(), sslContext.getProvider());
221+
bcTmf.init(trustStore);
222+
return bcTmf.getTrustManagers();
223+
} catch (KeyStoreException | NoSuchAlgorithmException e) {
224+
// Fallback to default trust managers
225+
}
226+
}
227+
trustManagerFactory.init(trustStore);
228+
return trustManagerFactory.getTrustManagers();
229+
}
230+
205231
/**
206232
* {@link Beta} <br>
207233
* Returns an SSL context in which all X.509 certificates are trusted.

0 commit comments

Comments
 (0)