public class SslConf extends Object
This class is basically a builder for SSLContext
from key-store and/or trust-store files.
For example:
SSLContext context = new SslConf() .keyStoreFile("my-certs.jks) .keyStorePass("password") .keyManagerFactoryAlgorithm("PKIX") .createContext();
Constructor and Description |
---|
SslConf()
Create an SslConf with default values.
|
Instance Methods | |
---|---|
SslConf |
keyStoreFile(String keyStoreFile)
The key store file path.
|
SslConf |
keyStorePass(String keyStorePass)
The key store file password.
|
SslConf |
keyStoreType(String keyStoreType)
The keys tore file type.
|
SslConf |
keyManagerFactoryAlgorithm(String keyManagerFactoryAlgorithm)
Algorithm for KeyManagerFactory.
|
SslConf |
trustStoreFile(String trustStoreFile)
The trust store file path.
|
SslConf |
trustStorePass(String trustStorePass)
The trust store file password.
|
SslConf |
trustStoreType(String trustStoreType)
The trust store file type.
|
SslConf |
trustManagerFactoryAlgorithm(String trustManagerFactoryAlgorithm)
Algorithm for KeyManagerFactory.
|
SslConf |
trustAll()
Use a TrustManager that accept all peer certificates, including all self-signed ones.
|
SslConf |
contextProtocol(String contextProtocol)
The protocol for
SSLContext.getInstance(protocol) |
SSLContext |
createContext()
Create an SSLContext.
|
KeyManager[] |
createKeyManagers()
Create key managers.
|
TrustManager[] |
createTrustManagers()
Create trust managers.
|
public SslConf keyStoreFile(String keyStoreFile)
default: null (none)
This file contains private keys used for local certificates.
For server, this field usually should be non-null.
For client, this field usually is null, unless client certificate is required.
public SslConf keyStorePass(String keyStorePass)
default: null
This field must be non-null if `keyStoreFile` is non-null.
This password is both for the file, and for all private keys in the file.
public SslConf keyStoreType(String keyStoreType)
KeyStore.getInstance(type)
.
default:
KeyStore.getDefaultType()
See standard values, including "jks", "pkcs12" etc.
On Sun/Oracle JRE, the factory-default value is "jks".
public SslConf keyManagerFactoryAlgorithm(String keyManagerFactoryAlgorithm)
KeyManagerFactory.getInstance(algorithm)
default:
KeyManagerFactory.getDefaultAlgorithm()
See standard values, including "PKIX".
On Sun/Oracle JRE, supported values include "PKIX", "SunX509", "NewSunX509". The factory-default value is "SunX509", which is probably fine for most use cases; However, apparently "SunX509" does not support SNI at this point, and "PKIX" does. Try "PKIX" if SNI support is required.
public SslConf trustStoreFile(String trustStoreFile)
default: null (system default)
This file contains root certificates for validating peer certificates.
If null, a system default trust store is used, which is usually
"JAVA-HOME/lib/security/cacerts"
.
See details at
JSSE Guide, starting at
"Note: If a null KeyStore parameter is passed to ..."
If null, `trustStorePass`, `trustStoreType`, and `trustManagerFactoryAlgorithm` configurations are irrelevant and ignored.
For server, this field usually can be null, unless the server needs to validate client certificates with non-standard CAs.
For client, this field usually can be null, unless the client needs to validate server certificates with non-standard CAs.
Note that the same key store file for the server can be used as the trust store for the client; this is convenient for local testing with self-signed certificates.
See also trustAll()
.
public SslConf trustStorePass(String trustStorePass)
default: null
This password is only used to check the integrity of the trust store file. It is not required even if `trustStoreFile` is non-null.
Note that the factory-default password for
"JAVA-HOME/lib/security/cacerts"
is "changeit".
public SslConf trustStoreType(String trustStoreType)
KeyStore.getInstance(type)
(note that KeyStore class can also represent trust stores).
default:
KeyStore.getDefaultType()
See standard values, including "jks", "pkcs12" etc.
On Sun/Oracle JRE, the factory-default value is "jks".
public SslConf trustManagerFactoryAlgorithm(String trustManagerFactoryAlgorithm)
TrustManagerFactory.getInstance(algorithm)
default:
TrustManagerFactory.getDefaultAlgorithm()
See standard values, including "PKIX".
On Sun/Oracle JRE, the factory-default value is "PKIX", which is probably fine for most use cases.
public SslConf trustAll()
Some applications may opt to accept all peer certificates during handshake.
`trustAll` and `trustStoreFile
` override each other;
whichever specified last is the effective setting.
public SslConf contextProtocol(String contextProtocol)
SSLContext.getInstance(protocol)
default: "TLS"
See standard values.
The default value "TLS" should be fine for most use cases.
public SSLContext createContext() throws Exception
This method depends on field
contextProtocol
and methods
createKeyManagers()
createTrustManagers()
Exception
public KeyManager[] createKeyManagers() throws Exception
This method depends on fields
keyStoreFile
keyStorePass
keyStoreType
keyManagerFactoryAlgorithm
Exception
public TrustManager[] createTrustManagers() throws Exception
If trustAll()
is specified, return a TrustManager that trust all certificates.
If trustStoreFile
==null,
return system default TrustManagers.
Otherwise, this method depends on fields
trustStoreFile
trustStorePass
trustStoreType
trustManagerFactoryAlgorithm
Exception