Skip to content

Avoid TrustManagerFacotry.init(ManagerFactoryParameters var1) if no O… #1157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.Security;
import java.security.cert.CertificateException;
import java.security.cert.PKIXBuilderParameters;
Expand Down Expand Up @@ -84,14 +86,37 @@ private static SSLContext configureSSLContext( File customCertFile, RevocationSt
loadSystemCertificates( trustedKeyStore );
}

// Configure certificate revocation checking (X509CertSelector() selects all certificates)
PKIXBuilderParameters pkixBuilderParameters = new PKIXBuilderParameters( trustedKeyStore, new X509CertSelector() );
PKIXBuilderParameters pkixBuilderParameters = configurePKIXBuilderParameters( trustedKeyStore, revocationStrategy );

// sets checking of stapled ocsp response
pkixBuilderParameters.setRevocationEnabled( requiresRevocationChecking( revocationStrategy ) );
SSLContext sslContext = SSLContext.getInstance( "TLS" );
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );

if ( pkixBuilderParameters == null )
{
trustManagerFactory.init( trustedKeyStore );
}
else
{
trustManagerFactory.init( new CertPathTrustManagerParameters( pkixBuilderParameters ) );
}

sslContext.init( new KeyManager[0], trustManagerFactory.getTrustManagers(), null );

return sslContext;
}

private static PKIXBuilderParameters configurePKIXBuilderParameters( KeyStore trustedKeyStore, RevocationStrategy revocationStrategy ) throws InvalidAlgorithmParameterException, KeyStoreException
{
PKIXBuilderParameters pkixBuilderParameters = null;

if ( requiresRevocationChecking( revocationStrategy ) )
{
// Configure certificate revocation checking (X509CertSelector() selects all certificates)
pkixBuilderParameters = new PKIXBuilderParameters( trustedKeyStore, new X509CertSelector() );

// sets checking of stapled ocsp response
pkixBuilderParameters.setRevocationEnabled( true );

// enables status_request extension in client hello
System.setProperty( "jdk.tls.client.enableStatusRequestExtension", "true" );

Expand All @@ -101,14 +126,7 @@ private static SSLContext configureSSLContext( File customCertFile, RevocationSt
Security.setProperty( "ocsp.enable", "true" );
}
}

SSLContext sslContext = SSLContext.getInstance( "TLS" );

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
trustManagerFactory.init( new CertPathTrustManagerParameters( pkixBuilderParameters ) );
sslContext.init( new KeyManager[0], trustManagerFactory.getTrustManagers(), null );

return sslContext;
return pkixBuilderParameters;
}

private static void loadSystemCertificates( KeyStore trustedKeyStore ) throws GeneralSecurityException, IOException
Expand Down