Package ej.net.util.ssl
Class SslContextBuilder
- java.lang.Object
-
- ej.net.util.ssl.SslContextBuilder
-
public class SslContextBuilder extends Object
Helps to create an SSL context.
-
-
Field Summary
Fields Modifier and Type Field Description static StringTLS_VERSION_1_2TLS version 1.2.static StringTLS_VERSION_1_3TLS version 1.3.
-
Constructor Summary
Constructors Constructor Description SslContextBuilder()Creates an SSL context builder.SslContextBuilder(String protocol)Creates an SSL context builder for the given TLS protocol version.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddClientKey(byte[] privateKey, InputStream certificate)Adds the given client key and the associated self-signed certificate.voidaddClientKey(byte[] privateKey, InputStream certificate, InputStream... certificationChain)Adds the given client key and the associated certificate and certification chain to the key store.voidaddClientKey(InputStream privateKey, InputStream certificate)Adds the given client key and the associated self-signed certificate.voidaddClientKey(InputStream privateKey, InputStream certificate, InputStream... certificationChain)Adds the given client key and the associated certificate and certification chain to the key store.voidaddClientKey(String privateKeyPath, String certificatePath, String... certificationChainPaths)Adds the given client key and the associated certificate and certification chain to the key store.voidaddServerCertificate(InputStream certificate)Adds the given server certificate to the trust store.voidaddServerCertificate(String certificatePath)Adds the given server certificate to the trust store.SSLContextbuild()Creates an SSL context using the populated trust store and an empty key store.SSLContextbuild(String keyStorePassword)Creates an SSL context using the populated trust store and an the populated key store, using the given key store password.
-
-
-
Field Detail
-
TLS_VERSION_1_2
public static final String TLS_VERSION_1_2
TLS version 1.2.- See Also:
- Constant Field Values
-
TLS_VERSION_1_3
public static final String TLS_VERSION_1_3
TLS version 1.3.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SslContextBuilder
public SslContextBuilder() throws GeneralSecurityExceptionCreates an SSL context builder.- Throws:
GeneralSecurityException- if an exception occurs while initializing the trust store or the key store.
-
SslContextBuilder
public SslContextBuilder(String protocol) throws GeneralSecurityException
Creates an SSL context builder for the given TLS protocol version.- Parameters:
protocol- the TLS protocol version string.- Throws:
GeneralSecurityException- if an exception occurs while initializing the trust store or the key store.- See Also:
SSLContext.getInstance(String)
-
-
Method Detail
-
addServerCertificate
public void addServerCertificate(InputStream certificate) throws GeneralSecurityException
Adds the given server certificate to the trust store.- Parameters:
certificate- the certificate to add.- Throws:
GeneralSecurityException- if the certificate could not be loaded or added.
-
addServerCertificate
public void addServerCertificate(String certificatePath) throws GeneralSecurityException, IOException
Adds the given server certificate to the trust store. Tries to find the resource in the application's classpath (usingClass.getResourceAsStream(String)).- Parameters:
certificatePath- the path to the certificate to add.- Throws:
GeneralSecurityException- if the certificate could not be loaded or added.IOException- if an I/O occurs when loading the resource.
-
addClientKey
public void addClientKey(byte[] privateKey, InputStream certificate, InputStream... certificationChain) throws GeneralSecurityExceptionAdds the given client key and the associated certificate and certification chain to the key store.- Parameters:
privateKey- the private key to add.certificate- the associated certificate.certificationChain- the certification chain, ordered, the root certificate at the end.- Throws:
GeneralSecurityException- if the certificates could not be loaded or the private key could not be added.
-
addClientKey
public void addClientKey(byte[] privateKey, InputStream certificate) throws GeneralSecurityExceptionAdds the given client key and the associated self-signed certificate.- Parameters:
privateKey- the private key to add.certificate- the associated certificate.- Throws:
GeneralSecurityException- if the certificates could not be loaded or the private key could not be added.
-
addClientKey
public void addClientKey(InputStream privateKey, InputStream certificate, InputStream... certificationChain) throws IOException, GeneralSecurityException
Adds the given client key and the associated certificate and certification chain to the key store.- Parameters:
privateKey- the private key to add.certificate- the associated certificate.certificationChain- the certification chain, ordered, the root certificate at the end.- Throws:
GeneralSecurityException- if the certificate could not be loaded or the private key could not be added.IOException- if the given privateKey could not be read.
-
addClientKey
public void addClientKey(InputStream privateKey, InputStream certificate) throws IOException, GeneralSecurityException
Adds the given client key and the associated self-signed certificate.- Parameters:
privateKey- the private key to add.certificate- the associated certificate.- Throws:
GeneralSecurityException- if the certificate could not be loaded or the private key could not be added.IOException- if the given privateKey could not be read.
-
addClientKey
public void addClientKey(String privateKeyPath, String certificatePath, String... certificationChainPaths) throws IOException, GeneralSecurityException
Adds the given client key and the associated certificate and certification chain to the key store. Tries to find the resource in the application's classpath (usingClass.getResourceAsStream(String)).- Parameters:
privateKeyPath- the path to the private key to add.certificatePath- the path to the associated certificate.certificationChainPaths- the paths to the certification chain, ordered, the root certificate at the end.- Throws:
GeneralSecurityException- if the certificate could not be loaded or the private key could not be added.IOException- if the given privateKey could not be read.
-
build
public SSLContext build() throws GeneralSecurityException
Creates an SSL context using the populated trust store and an empty key store.- Returns:
- the created SSL context.
- Throws:
GeneralSecurityException- if an exception occurred while creating the SSL context.
-
build
public SSLContext build(String keyStorePassword) throws GeneralSecurityException
Creates an SSL context using the populated trust store and an the populated key store, using the given key store password.- Parameters:
keyStorePassword- the password of the key store.- Returns:
- the created SSL context.
- Throws:
GeneralSecurityException- if an exception occurred while creating the SSL context.
-
-