Class CertificateUtil
java.lang.Object
com.norconex.commons.lang.security.CertificateUtil
Certificate-related (e.g., SSL) utility methods.
- Since:
- 2.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic List<X509Certificate> fetchCertificates(@NonNull String url) Fetches certificates associated with the URL host.static List<X509Certificate> fetchCertificates(@NonNull String host, int port) Fetches certificates associated with the given host and port.static booleanGets whether a host is trusted by the given key store.static StringtoString(@NonNull List<X509Certificate> certificates) Returns a friendly string display of certificates.static intTrusts a host.static intTrusts the URL host.
-
Method Details
-
fetchCertificates
public static List<X509Certificate> fetchCertificates(@NonNull @NonNull 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(@NonNull @NonNull 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(@NonNull @NonNull 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:
trueif trusted- Throws:
GeneralSecurityException- certificate exceptionIOException- I/O exception
-
trustHost
public static int trustHost(@NonNull @NonNull 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(@NonNull @NonNull 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(@NonNull @NonNull 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
-