|
17 | 17 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
18 | 18 | import java.security.GeneralSecurityException; |
19 | 19 | import java.security.KeyStore; |
| 20 | +import java.security.KeyStoreException; |
20 | 21 | import java.security.NoSuchAlgorithmException; |
21 | 22 | import java.security.NoSuchProviderException; |
22 | 23 | import java.security.Provider; |
@@ -164,8 +165,8 @@ public static KeyManagerFactory getPkixKeyManagerFactory() throws NoSuchAlgorith |
164 | 165 | public static SSLContext initSslContext( |
165 | 166 | SSLContext sslContext, KeyStore trustStore, TrustManagerFactory trustManagerFactory) |
166 | 167 | throws GeneralSecurityException { |
167 | | - trustManagerFactory.init(trustStore); |
168 | | - sslContext.init(null, trustManagerFactory.getTrustManagers(), null); |
| 168 | + sslContext.init( |
| 169 | + null, getCompatibleTrustManagers(sslContext, trustStore, trustManagerFactory), null); |
169 | 170 | return sslContext; |
170 | 171 | } |
171 | 172 |
|
@@ -195,13 +196,38 @@ public static SSLContext initSslContext( |
195 | 196 | String mtlsKeyStorePassword, |
196 | 197 | KeyManagerFactory keyManagerFactory) |
197 | 198 | throws GeneralSecurityException { |
198 | | - trustManagerFactory.init(trustStore); |
199 | 199 | keyManagerFactory.init(mtlsKeyStore, mtlsKeyStorePassword.toCharArray()); |
200 | 200 | sslContext.init( |
201 | | - keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); |
| 201 | + keyManagerFactory.getKeyManagers(), |
| 202 | + getCompatibleTrustManagers(sslContext, trustStore, trustManagerFactory), |
| 203 | + null); |
202 | 204 | return sslContext; |
203 | 205 | } |
204 | 206 |
|
| 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 | + |
205 | 231 | /** |
206 | 232 | * {@link Beta} <br> |
207 | 233 | * Returns an SSL context in which all X.509 certificates are trusted. |
|
0 commit comments