Class CertificateUtil
- java.lang.Object
-
- com.norconex.commons.lang.security.CertificateUtil
-
public final class CertificateUtil extends Object
Certificate-related (e.g., SSL) utility methods.- Since:
- 2.0.0
- Author:
- Pascal Essiembre
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static List<X509Certificate>
fetchCertificates(String url)
Fetches certificates associated with the URL host.static List<X509Certificate>
fetchCertificates(String host, int port)
Fetches certificates associated with the given host and port.static boolean
isTrusted(String host, int port, KeyStore keyStore)
Gets whether a host is trusted by the given key store.static String
toString(List<X509Certificate> certificates)
Returns a friendly string display of certificates.static int
trustHost(String host, int port, KeyStore keyStore)
Trusts a host.static int
trustHost(String url, KeyStore keyStore)
Trusts the URL host.
-
-
-
Method Detail
-
fetchCertificates
public static List<X509Certificate> fetchCertificates(String url) throws GeneralSecurityException, IOException
Fetches certificates associated with the URL host.- Parameters:
url
- url from which to fetch certificates- Returns:
- certificates
- Throws:
GeneralSecurityException
- certificate exceptionIOException
- I/O exception
-
fetchCertificates
public static List<X509Certificate> fetchCertificates(String host, int port) throws GeneralSecurityException, IOException
Fetches certificates associated with the given host and port.- Parameters:
host
- from which to fetch certificatesport
- host port- Returns:
- certificates
- Throws:
GeneralSecurityException
- certificate exceptionIOException
- I/O exception
-
isTrusted
public static boolean isTrusted(String host, int port, KeyStore keyStore) throws GeneralSecurityException, IOException
Gets whether a host is trusted by the given key store.- Parameters:
host
- host to verify for trustport
- host portkeyStore
- key store used to establish trust- Returns:
true
if trusted- Throws:
GeneralSecurityException
- certificate exceptionIOException
- I/O exception
-
trustHost
public static int trustHost(String url, KeyStore keyStore) throws GeneralSecurityException, IOException
Trusts the URL host. Adds the host certificates to the supplied key store if not already trusted.- Parameters:
url
- url from which to get the hostkeyStore
- key store used for trust verification and to store new certificates- Returns:
- the number of new certificates added to the key store (0 if already trusted)
- Throws:
GeneralSecurityException
- certificate exceptionIOException
- I/O exception
-
trustHost
public static int trustHost(String host, int port, KeyStore keyStore) throws GeneralSecurityException, IOException
Trusts a host. Adds the host certificates to the supplied key store if not already trusted.- Parameters:
host
- the host to trustport
- the host portkeyStore
- key store used for trust verification and to store new certificates- Returns:
- the number of new certificates added to the key store (0 if already trusted)
- Throws:
GeneralSecurityException
- certificate exceptionIOException
- I/O exception
-
toString
public static String toString(List<X509Certificate> certificates) throws GeneralSecurityException
Returns a friendly string display of certificates.- Parameters:
certificates
- certificates to convert to string- Returns:
- string display of certificates
- Throws:
GeneralSecurityException
- certificate exception
-
-