66import javax .net .ssl .TrustManager ;
77import javax .net .ssl .TrustManagerFactory ;
88import javax .net .ssl .X509ExtendedTrustManager ;
9+ import javax .net .ssl .X509TrustManager ;
910import java .io .File ;
1011import java .io .FileInputStream ;
1112import java .io .IOException ;
3637 * work is fulfilled by whichever {@link java.security.Provider} is registered with the JVM,
3738 * including FIPS-mode providers.
3839 */
39- public final class TrustProvider {
40+ final class TrustProvider {
4041
4142 private final SSLSocketFactory sslSocketFactory ;
42- private final X509ExtendedTrustManager trustManager ;
43+ private final X509TrustManager trustManager ;
4344 private final SSLContext sslContext ;
4445
45- private TrustProvider (SSLContext sslContext , X509ExtendedTrustManager trustManager ) {
46+ private TrustProvider (SSLContext sslContext , X509TrustManager trustManager ) {
4647 this .sslContext = sslContext ;
4748 this .trustManager = trustManager ;
4849 this .sslSocketFactory = sslContext .getSocketFactory ();
4950 }
5051
51- public X509ExtendedTrustManager getTrustManager () {
52+ public X509TrustManager getTrustManager () {
5253 return trustManager ;
5354 }
5455
@@ -91,6 +92,12 @@ public static TrustProvider fromDirectory(String certsDirPath) throws IOExceptio
9192 return builder .build ();
9293 }
9394
95+ public static TrustProvider fromTrustManager (X509TrustManager trustManager ) throws IOException , GeneralSecurityException {
96+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
97+ sslContext .init (new KeyManager [0 ], new TrustManager []{trustManager }, new SecureRandom ());
98+ return new TrustProvider (sslContext , trustManager );
99+ }
100+
94101 /**
95102 * Builds a {@link TrustProvider} that trusts JVM default cacerts plus the trusted-certificate
96103 * entries in the supplied keystore.
@@ -106,21 +113,6 @@ public static TrustProvider fromKeyStore(Path keystorePath, char[] password) thr
106113 return builder ().withDefaultTrustMaterial ().withTrustMaterial (ks ).build ();
107114 }
108115
109- /**
110- * Builds a {@link TrustProvider} that accepts every server certificate. Intended only for
111- * tests and {@code --insecure} CLI flows.
112- */
113- public static TrustProvider insecure () {
114- try {
115- SSLContext ctx = SSLContext .getInstance ("TLS" );
116- X509ExtendedTrustManager trustAll = new InsecureTrustManager ();
117- ctx .init (new KeyManager [0 ], new TrustManager []{trustAll }, new SecureRandom ());
118- return new TrustProvider (ctx , trustAll );
119- } catch (GeneralSecurityException e ) {
120- throw new IllegalStateException ("failed to build insecure TrustProvider" , e );
121- }
122- }
123-
124116 private static KeyStore loadKeyStore (InputStream in , char [] password )
125117 throws IOException , GeneralSecurityException {
126118 // Try JKS first since it remains the JVM default; fall back to PKCS12 which is portable
@@ -179,13 +171,6 @@ public Builder withTrustMaterial(X509Certificate... certs) {
179171 return this ;
180172 }
181173
182- public Builder withTrustMaterial (Collection <X509Certificate > certs ) {
183- if (certs != null ) {
184- this .certificates .addAll (certs );
185- }
186- return this ;
187- }
188-
189174 public Builder withTrustMaterial (KeyStore keyStore ) {
190175 if (keyStore != null ) {
191176 this .keyStores .add (keyStore );
@@ -250,16 +235,4 @@ private static KeyStore newEmptyKeyStore() throws GeneralSecurityException, IOEx
250235 return ks ;
251236 }
252237 }
253-
254- private static final class InsecureTrustManager extends X509ExtendedTrustManager {
255- private static final X509Certificate [] EMPTY = new X509Certificate [0 ];
256-
257- @ Override public void checkClientTrusted (X509Certificate [] chain , String authType ) { }
258- @ Override public void checkClientTrusted (X509Certificate [] chain , String authType , java .net .Socket socket ) { }
259- @ Override public void checkClientTrusted (X509Certificate [] chain , String authType , javax .net .ssl .SSLEngine engine ) { }
260- @ Override public void checkServerTrusted (X509Certificate [] chain , String authType ) { }
261- @ Override public void checkServerTrusted (X509Certificate [] chain , String authType , java .net .Socket socket ) { }
262- @ Override public void checkServerTrusted (X509Certificate [] chain , String authType , javax .net .ssl .SSLEngine engine ) { }
263- @ Override public X509Certificate [] getAcceptedIssuers () { return EMPTY ; }
264- }
265238}
0 commit comments