-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Builds anSSLContext
using the current builder state.Builds anSSLContext
using the current builder state, and destroys the builder's state, to reduce the chance of information leakage.void
destroy()
Destroys the state of this builder and all key-/trust-related settings specified.static SSLContextBuilder
Creates anSSLContextBuilder
to be used in a client context.static SSLContextBuilder
Creates anSSLContextBuilder
to be used in a server context.static KeyStore
Returns a newPKCS12
KeyStoreException
instance.withDefaultSSLParameters
(Consumer<SSLParameters> consumer) Configures this builder to use the given consumer to configure the default SSL parameters.Configures this builder to use the given function to configure the default SSL parameters.Configures this builder to use the given supplier for {link KeyManager}[]
.withKeyStore
(File path, SSLSupplier<char[]> password) Configures this builder to use the given keystore, identified by path and password.withKeyStore
(URL url, SSLSupplier<char[]> password) Configures this builder to use the given keystore, identified by URL and password.withKeyStoreSupplier
(SSLSupplier<KeyStore> supplier) Configures this builder to use the given supplier to provideKeyStore
instances.Configures this builder to use the given protocol.withProvider
(String id) Configures this builder to use the given provider, identified by ID,null
being the default.Configures this builder to use the given provider,null
being the default.Configures this builder to use the given protocol.Configures this builder to use the givenSocketFactory
to create the underlying insecure sockets.Configures this builder to use the given supplier forTrustManager
[]
.withTrustStore
(File path, SSLSupplier<char[]> password) Configures this builder to use the given truststore, identified by path and password.withTrustStore
(URL url, SSLSupplier<char[]> password) Configures this builder to use the given truststore, identified by path and password.static IOException
For a givenIOException
thrown from withinSSLContextBuilder
, check if it is due to a known JDK bug, and if so, wrap that exception in aKnownJavaBugIOException
with a proper explanation.
-
Method Details
-
forServer
Creates anSSLContextBuilder
to be used in a server context.- Returns:
- The builder instance.
-
forClient
Creates anSSLContextBuilder
to be used in a client context.- Returns:
- The builder instance.
-
withSocketFactory
Configures this builder to use the givenSocketFactory
to create the underlying insecure sockets.- Parameters:
sf
- TheSocketFactory
.- Returns:
- This builder.
-
withProtocol
Configures this builder to use the given protocol. Note that "TLS
" is the default.- Parameters:
p
- The protocol to use, e.g.TLSv1.2
.- Returns:
- This builder.
-
withProvider
Configures this builder to use the given provider,null
being the default.- Parameters:
p
- The provider to use, e.g.BouncyCastleJsseProvider
, ornull
for system default.- Returns:
- This builder.
-
withProvider
Configures this builder to use the given provider, identified by ID,null
being the default.In addition to the standard JSSE IDs, you can specify one or more Provider classnames as a comma-separated list. These providers will be added via
Security.addProvider(Provider)
. The first entry is then attempted to be resolved usingProvider.getName()
, with any optionally remainingProvider
s simply being added to the list of available providers, in case they're actually required by the first one. It is expected that the classes have a public no-arg constructor.This is the case, for example, with BouncyCastle. Specify
org.bouncycastle.jsse.provider.BouncyCastleJsseProvider,org.bouncycastle.jce.provider.BouncyCastleProvider
to enable TLS-secured communication with PKCS12 keys, for example.- Parameters:
id
- The provider to use, e.g.BCJSSE
, ornull
/""
for system default.- Returns:
- This builder.
-
withKeyManagers
Configures this builder to use the given supplier for {link KeyManager}[]
. Note that setting any value other thannull
means that the parameters specified withwithKeyStore(File, SSLSupplier)
, etc. are ignored.- Parameters:
s
- The supplier to use, ornull
to use the built-in default.- Returns:
- This builder.
- Throws:
IllegalStateException
- ifwithKeyStore(File, SSLSupplier)
, etc., was already called.
-
withTrustManagers
Configures this builder to use the given supplier forTrustManager
[]
. Note that setting any value other thannull
means that the parameters specified withwithTrustStore(File, SSLSupplier)
, etc. are ignored.- Parameters:
s
- The supplier to use, ornull
to use the built-in default.- Returns:
- This builder.
-
withSecureRandom
Configures this builder to use the given protocol. Note that "null
" is the default, which means that it's up the SSL implementation whatSecureRandom
to use.- Parameters:
s
- The instance to use, e.g.null
.- Returns:
- This builder.
-
withKeyStoreSupplier
Configures this builder to use the given supplier to provideKeyStore
instances. Ifnull
is specified, the default supplier is used, which is configured forPKCS12
-type keystores. In that case, on Android, it is expected that the BouncyCastle SSL provider (org.bouncycastle.jce.provider.BouncyCastleProvider
) is on the classpath.- Parameters:
supplier
- The supplier, ornull
for default.- Returns:
- This builder.
-
withKeyStore
public SSLContextBuilder withKeyStore(File path, SSLSupplier<char[]> password) throws FileNotFoundException, MalformedURLException Configures this builder to use the given keystore, identified by path and password.- Parameters:
path
- The path to the keystore.password
- The supplier that returns the password to unlock the keystore; the password will be overwritten with blanks immediately after use.- Returns:
- This builder.
- Throws:
FileNotFoundException
- on error.MalformedURLException
- on error.IllegalStateException
- ifwithKeyManagers(SSLFunction)
was already called.- See Also:
-
withKeyStore
public SSLContextBuilder withKeyStore(URL url, SSLSupplier<char[]> password) throws FileNotFoundException Configures this builder to use the given keystore, identified by URL and password.- Parameters:
url
- TheURL
specifying the location of the keystorepassword
- The supplier that returns the password to unlock the keystore; the password will be overwritten with blanks immediately after use.- Returns:
- This builder.
- Throws:
FileNotFoundException
- on error.- See Also:
-
withTrustStore
public SSLContextBuilder withTrustStore(File path, SSLSupplier<char[]> password) throws FileNotFoundException, MalformedURLException Configures this builder to use the given truststore, identified by path and password.- Parameters:
path
- The path to the truststore.password
- The supplier that returns the password to unlock the keystore; the password will be overwritten with blanks immediately after use.- Returns:
- This builder.
- Throws:
FileNotFoundException
- on error.MalformedURLException
- on error.- See Also:
-
withTrustStore
public SSLContextBuilder withTrustStore(URL url, SSLSupplier<char[]> password) throws FileNotFoundException Configures this builder to use the given truststore, identified by path and password.- Parameters:
url
- TheURL
specifying the location of the truststore.password
- The supplier that returns the password to unlock the keystore; the password will be overwritten with blanks immediately after use.- Returns:
- This builder.
- Throws:
FileNotFoundException
- on error.- See Also:
-
withDefaultSSLParameters
Configures this builder to use the given function to configure the default SSL parameters. The function is called with the context's default SSL parameters instance. The function may modify and return the given instance, or return a completely different instance.- Parameters:
function
- The function to configure SSL parameters.- Returns:
- This builder.
-
withDefaultSSLParameters
Configures this builder to use the given consumer to configure the default SSL parameters. The consumer is called with the context's default SSL parameters instance. The consumer may modify or just inspect the given instance.- Parameters:
consumer
- The function to configure SSL parameters.- Returns:
- This builder.
-
wrapIOExceptionIfJDKBug
For a givenIOException
thrown from withinSSLContextBuilder
, check if it is due to a known JDK bug, and if so, wrap that exception in aKnownJavaBugIOException
with a proper explanation.- Parameters:
e
- The exception to check/wrap.- Returns:
- The exception, or a
KnownJavaBugIOException
.
-
build
Builds anSSLContext
using the current builder state.IMPORTANT: Use
buildAndDestroyBuilder()
to ensure sensitive information, such as passwords, are properly destroyed/removed from memory.- Returns:
- The new
SSLContext
instance. - Throws:
GeneralSecurityException
- on error.IOException
- on error.- See Also:
-
buildAndDestroyBuilder
public SSLContext buildAndDestroyBuilder() throws GeneralSecurityException, IOException, DestroyFailedExceptionBuilds anSSLContext
using the current builder state, and destroys the builder's state, to reduce the chance of information leakage.- Returns:
- The new
SSLContext
instance. - Throws:
GeneralSecurityException
- on error.IOException
- on error.DestroyFailedException
- on error.- See Also:
-
destroy
Destroys the state of this builder and all key-/trust-related settings specified.- Throws:
DestroyFailedException
- on error (thrown at the end, to increase level of destruction).
-
newKeyStorePKCS12
Returns a newPKCS12
KeyStoreException
instance.- Returns:
- The keystore instance.
- Throws:
KeyStoreException
- on error.
-