Package com.norconex.commons.lang.security

# 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

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static 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 boolean`
  `isTrusted(@NonNull String host, int port, KeyStore keyStore)`
  Gets whether a host is trusted by the given key store.
  `static String`
  `toString(@NonNull List<X509Certificate> certificates)`
  Returns a friendly string display of certificates.
  `static int`
  `trustHost(@NonNull String host, int port, KeyStore keyStore)`
  Trusts a host.
  `static int`
  `trustHost(@NonNull String url, KeyStore keyStore)`
  Trusts the URL host.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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 exception
      `IOException` - 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 certificates
      `port` - host port
    Returns:
      certificates
    Throws:
      `GeneralSecurityException` - certificate exception
      `IOException` - 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 trust
      `port` - host port
      `keyStore` - key store used to establish trust
    Returns:
      `true` if trusted
    Throws:
      `GeneralSecurityException` - certificate exception
      `IOException` - 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 host
      `keyStore` - 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 exception
      `IOException` - 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 trust
      `port` - the host port
      `keyStore` - 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 exception
      `IOException` - 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