Package org.omegazero.net.util
Class SSLUtil
java.lang.Object
org.omegazero.net.util.SSLUtil
Contains utility methods for managing SSL/TLS objects and keys/certificates.
-
Method Summary
Modifier and TypeMethodDescriptionstatic SSLServerSocketFactory
getSecureServerSocketFactory
(String keyFile, String certFile) Deprecated.static ServerSocketFactory
Deprecated.static SSLContext
getSSLContextFromKeyStore
(String filename, String password, String keypassword) Creates aSSLContext
with the "TLS
" protocol from a Java key store stored in a file identified by filename.static SSLContext
getSSLContextFromKeyStore
(String protocol, String filename, String password, String keypassword) Creates aSSLContext
from a Java key store stored in a file identified by filename.static SSLContext
getSSLContextFromPEM
(String keyFile, String certFile) Creates aSSLContext
with the "TLS
" protocol from PEM-encoded key and certificate files.static SSLContext
getSSLContextFromPEM
(String protocol, String keyFile, String certFile) Creates aSSLContext
from PEM-encoded key and certificate files.static SSLEngine
getSSLEngineWithPEM
(String keyFile, String certFile) Deprecated.static X509Certificate
loadCertificateFromPEM
(String certFile) Reads a singleX509Certificate
from the given certFile.static X509Certificate[]
loadCertificatesFromPEM
(String certFile) Reads one or moreX509Certificate
from the given certFile.static PrivateKey
loadPrivateKeyFromPEM
(String keyFile) Reads aPrivateKey
from the given keyFile.static List
<byte[]> readCertificatePEM
(String data) Reads one or more encoded certificates from the given string data and returns the decoded bytes.
-
Method Details
-
getServerSocketFactory
Deprecated.Returns the default server socket factory.- Returns:
- The default server socket factory
-
getSSLContextFromKeyStore
public static SSLContext getSSLContextFromKeyStore(String filename, String password, String keypassword) throws GeneralSecurityException, IOException Creates aSSLContext
with the "TLS
" protocol from a Java key store stored in a file identified by filename.- Parameters:
filename
- The key store filepassword
- The file passwordkeypassword
- The key password- Returns:
- The
SSLContext
- Throws:
GeneralSecurityException
- If an SSL error occursIOException
- If an IO error occurs while reading the file- See Also:
-
getSSLContextFromKeyStore
public static SSLContext getSSLContextFromKeyStore(String protocol, String filename, String password, String keypassword) throws GeneralSecurityException, IOException Creates aSSLContext
from a Java key store stored in a file identified by filename.- Parameters:
protocol
- The name of the protocol theSSLContext
will be used forfilename
- The key store filepassword
- The file passwordkeypassword
- The key password- Returns:
- The
SSLContext
- Throws:
GeneralSecurityException
- If an SSL error occursIOException
- If an IO error occurs while reading the file- See Also:
-
getSSLContextFromPEM
public static SSLContext getSSLContextFromPEM(String keyFile, String certFile) throws GeneralSecurityException, IOException Creates aSSLContext
with the "TLS
" protocol from PEM-encoded key and certificate files.- Parameters:
keyFile
- The key filecertFile
- The certificate file- Returns:
- The
SSLContext
- Throws:
GeneralSecurityException
- If an SSL error occursIOException
- If an IO error occurs while reading the file- See Also:
-
getSSLContextFromPEM
public static SSLContext getSSLContextFromPEM(String protocol, String keyFile, String certFile) throws GeneralSecurityException, IOException Creates aSSLContext
from PEM-encoded key and certificate files.- Parameters:
protocol
- The name of the protocol theSSLContext
will be used forkeyFile
- The key filecertFile
- The certificate file- Returns:
- The
SSLContext
- Throws:
GeneralSecurityException
- If an SSL error occursIOException
- If an IO error occurs while reading the file- See Also:
-
getSecureServerSocketFactory
@Deprecated public static SSLServerSocketFactory getSecureServerSocketFactory(String keyFile, String certFile) throws GeneralSecurityException, IOException Deprecated.Creates aSSLServerSocketFactory
from PEM-encoded key and certificate files.- Parameters:
keyFile
- The key filecertFile
- The certificate file- Returns:
- The
SSLServerSocketFactory
- Throws:
GeneralSecurityException
- If an SSL error occursIOException
- If an IO error occurs while reading the file
-
getSSLEngineWithPEM
@Deprecated public static SSLEngine getSSLEngineWithPEM(String keyFile, String certFile) throws GeneralSecurityException, IOException Deprecated.Creates aSSLEngine
from PEM-encoded key and certificate files.- Parameters:
keyFile
- The key filecertFile
- The certificate file- Returns:
- The
SSLEngine
- Throws:
GeneralSecurityException
- If an SSL error occursIOException
- If an IO error occurs while reading the file
-
readCertificatePEM
Reads one or more encoded certificates from the given string data and returns the decoded bytes.The given data is expected to contain one or more blocks of the following format:
-----BEGIN CERTIFICATE----- <base64-encoded certificate data> -----END CERTIFICATE-----
- Parameters:
data
- The string data- Returns:
- The list of decoded certificate blocks
- Throws:
IOException
- If the format is invalid
-
loadPrivateKeyFromPEM
public static PrivateKey loadPrivateKeyFromPEM(String keyFile) throws GeneralSecurityException, IOException Reads aPrivateKey
from the given keyFile.- Parameters:
keyFile
- The key file- Returns:
- The
PrivateKey
- Throws:
GeneralSecurityException
- If an SSL error occursIOException
- If an IO error occurs while reading the file- See Also:
-
loadCertificateFromPEM
public static X509Certificate loadCertificateFromPEM(String certFile) throws GeneralSecurityException, IOException Reads a singleX509Certificate
from the given certFile.- Parameters:
certFile
- The certificate file- Returns:
- The
X509Certificate
- Throws:
GeneralSecurityException
- If an SSL error occursIOException
- If an IO error occurs while reading the file
-
loadCertificatesFromPEM
public static X509Certificate[] loadCertificatesFromPEM(String certFile) throws GeneralSecurityException, IOException Reads one or moreX509Certificate
from the given certFile.- Parameters:
certFile
- The certificate file- Returns:
- The
X509Certificate
s - Throws:
GeneralSecurityException
- If an SSL error occursIOException
- If an IO error occurs while reading the file
-
getSSLContextFromPEM(String, String)
andSSLContext.getServerSocketFactory()