Class SSLFTPCertificateStore

java.lang.Object
com.enterprisedt.net.ftp.ssl.SSLFTPCertificateStore
All Implemented Interfaces:
Iterable, Collection, List, SequencedCollection

public class SSLFTPCertificateStore extends Object implements List
Manages a collection of SSLFTPCertificates. The primary purchase of SSLFTPCertificateStore is to maintain a collection of root certificates for SSLFTPClient. SSLFTPClient has an instance of SSLFTPCertificateStore (accessible via SSLFTPClient.getRootCertificateStore()) which contains the root certificates that will be used to validate the certificate that a server presents during connection establishment.

SSLFTPCertificateStore implements the List interface and thus provides broad flexibility for managing individual certificates in the collection. It also provides methods for importing from and exporting to Java KeyStores (JKS) and PEM files. Java certificates (i.e. Certificate) may be added to a SSLFTPCertificateStore by wrapping it in an SSLFTPCertificate as follows:

     certStore.add(new SSLFTPCertificate(myJavaCert));
 
where certStore is an instance of SSLFTPCertificateStore and myJavaCert is an instance of Certificate.
Author:
Hans Andersen
  • Constructor Details

    • SSLFTPCertificateStore

      public SSLFTPCertificateStore()
      Creates a empty certificate store.
  • Method Details

    • importCertificates

      public void importCertificates(String fileName) throws FileNotFoundException, IOException
      Import certificates from PEM file (see importPEMFile(String)) or from Java key store (see importPEMFile(String)).
      Parameters:
      fileName - path to certificate file or key store
      Throws:
      FileNotFoundException
      IOException
    • importPEMFile

      public void importPEMFile(String fileName) throws FileNotFoundException, IOException, SSLFTPCertificateException
      Imports all the certificates in the given file into the store. The certificates in the file must be in base-64-encoded DER format and be bracketed as follows:
         -----BEGIN CERTIFICATE-----
         ... first certificate ...
         -----END CERTIFICATE-----
         -----BEGIN CERTIFICATE-----
         ... second certificate ...
         -----END CERTIFICATE-----
         etc
       
      Parameters:
      fileName - Name of the file to import.
      Throws:
      FileNotFoundException - Thrown if the file could not be found.
      IOException - Thrown if there was an error while reading the file.
      SSLFTPException - Thrown if a certificate could not be read.
      SSLFTPCertificateException
    • importPEMFile

      public void importPEMFile(InputStream inputStream) throws IOException, SSLFTPCertificateException
      Imports all the certificates in the given input-stream, into the store. The certificates in the file must be in base-64-encoded DER format and be bracketed as follows:
         -----BEGIN CERTIFICATE-----
         ... first certificate ...
         -----END CERTIFICATE-----
         -----BEGIN CERTIFICATE-----
         ... second certificate ...
         -----END CERTIFICATE-----
         etc
       
      Parameters:
      inputStream - InputStream to read.
      Throws:
      FileNotFoundException - Thrown if the file could not be found.
      IOException - Thrown if there was an error while reading the file.
      SSLFTPException - Thrown if a certificate could not be read.
      SSLFTPCertificateException
    • exportPEMFile

      public void exportPEMFile(String fileName) throws IOException
      Writes all the certificates in the store to the given file. File will be in base-64-encoded DER format and will be bracketed as follows:
         -----BEGIN CERTIFICATE-----
         ... first certificate ...
         -----END CERTIFICATE-----
         -----BEGIN CERTIFICATE-----
         ... second certificate ...
         -----END CERTIFICATE-----
         etc
       
      Parameters:
      fileName - Name of the file to write to.
      Throws:
      IOException - Thrown if the file could not be created.
    • exportPEMFile

      public void exportPEMFile(OutputStream outputStream) throws IOException
      Writes all the certificates in the store to the output-stream. File will be in base-64-encoded DER format and will be bracketed as follows:
         -----BEGIN CERTIFICATE-----
         ... first certificate ...
         -----END CERTIFICATE-----
         -----BEGIN CERTIFICATE-----
         ... second certificate ...
         -----END CERTIFICATE-----
         etc
       
      Parameters:
      outputStream - Output-stream to write to.
      Throws:
      IOException - Thrown if there was an error writing to the stream.
    • importKeyStore

      public void importKeyStore(KeyStore keyStore) throws KeyStoreException, SSLFTPCertificateException
      Imports all the certificates from the given Java KeyStore into this certificate store.
      Parameters:
      keyStore - KeyStore to import from.
      Throws:
      KeyStoreException - Thrown if there was a problem accessing the keystore.
      SSLFTPCertificateException - Thrown if there was an error while inserting a certificate into the certificate store.
    • importKeyStore

      public void importKeyStore(String keystoreFile, String password) throws KeyStoreException, IOException, SSLFTPCertificateException
      Imports all the certificates from the given Java KeyStore file into this certificate store.
      Parameters:
      keystoreFile - name of the file containing the KeyStore to import
      password - keystore password
      Throws:
      KeyStoreException - Thrown if there was a problem accessing the keystore.
      SSLFTPException - Thrown if there was an error while inserting a certificate into the certificate store.
      IOException - Thrown if there was a problem while accessing the file.
      SSLFTPCertificateException
    • importKeyStore

      public void importKeyStore(String keystoreFile) throws KeyStoreException, IOException, SSLFTPCertificateException
      Imports all the certificates from the given Java KeyStore file into this certificate store.
      Parameters:
      keystoreFile - name of the file containing the KeyStore to import.
      Throws:
      KeyStoreException - Thrown if there was a problem accessing the keystore.
      SSLFTPException - Thrown if there was an error while inserting a certificate into the certificate store.
      IOException - Thrown if there was a problem while accessing the file.
      SSLFTPCertificateException
    • exportKeyStore

      public void exportKeyStore(KeyStore keyStore) throws CertificateException, KeyStoreException
      Exports all certificates in the store to the given Java KeyStore. The alias of each certificate will be its CommonName (CN) if it is not null or the Organisation Name if the CN is null. Note that Java KeyStores impose some restrictions on the type of certificate that may be added. It can be useful to experiment with importing certificates using the JDK's keytool utility (-import option).
      Parameters:
      keyStore - Keystore to export certificates to.
      Throws:
      CertificateException - Thrown if there was a problem with the certificate being extracted from the certificate store.
      KeyStoreException - Thrown if there was an error while adding the certificate to the keystore.
    • importDefaultKeyStore

      public void importDefaultKeyStore() throws KeyStoreException, IOException, SSLFTPException
      Imports all certificates in the default key-store for the platform on which the software is running. It first looks for the javax.net.ssl.trustStore property, and uses that path if it is set. Optionally, javax.net.ssl.trustStorePassword can be set to supply a password for the trust store. If javax.net.ssl.trustStore is not set, it tries loading first {java.home}/lib/security/jssecacerts> and then {java.home}/lib/security/cacerts.
      Throws:
      KeyStoreException - Thrown if there was a problem accessing the keystore.
      SSLFTPException - Thrown if there was an error while inserting a certificate into the certificate store.
      IOException - Thrown if there was a problem while accessing the file.
    • add

      public void add(int index, Object certificate)
      Adds the given certificate SSLFTPCertificate to the store at the given position.
      Specified by:
      add in interface List
      Parameters:
      index - Position to add the certificate at.
      certificate - SSLFTPCertificate to add.
      Throws:
      ArrayStoreException - Thrown if an object other than SSLFTPCertificate is added.
    • add

      public boolean add(Object certificate)
      Adds the given certificate SSLFTPCertificate to the store.
      Specified by:
      add in interface Collection
      Specified by:
      add in interface List
      Parameters:
      certificate - SSLFTPCertificate to add.
      Throws:
      ArrayStoreException - Thrown if an object other than SSLFTPCertificate is added.
    • addAll

      public boolean addAll(Collection certificates)
      Add all the SSLFTPCertificates in the given collection to the store.
      Specified by:
      addAll in interface Collection
      Specified by:
      addAll in interface List
      Parameters:
      certificates - SSLFTPCertificates to add.
      Throws:
      ArrayStoreException - Thrown if an object other than SSLFTPCertificate is added.
    • addAll

      public boolean addAll(int index, Collection certificates)
      Add all the SSLFTPCertificates in the given collection to the store at the given position.
      Specified by:
      addAll in interface List
      Parameters:
      index - Position at which to add the certificates.
      certificates - SSLFTPCertificates to add.
      Throws:
      ArrayStoreException - Thrown if an object other than SSLFTPCertificate is added.
    • clear

      public void clear()
      Removes all certificates from the collection.
      Specified by:
      clear in interface Collection
      Specified by:
      clear in interface List
    • contains

      public boolean contains(Object certificate)
      Returns true if the given certificate is present in the collection.
      Specified by:
      contains in interface Collection
      Specified by:
      contains in interface List
    • containsAll

      public boolean containsAll(Collection certificates)
      Returns true if all of the given certificates are present in the collection.
      Specified by:
      containsAll in interface Collection
      Specified by:
      containsAll in interface List
    • get

      public Object get(int index)
      Returns the requested certificate in the store as an Object reference.
      Specified by:
      get in interface List
      Parameters:
      index - Position of certificate to return.
    • getCertificate

      public SSLFTPCertificate getCertificate(int index)
      Returns the requested certificate in the store as an SSLFTPCertificate reference.
      Parameters:
      index - Position of certificate to return.
    • indexOf

      public int indexOf(Object certificate)
      Returns the index of the given certificate or -1 if it's not in the store.
      Specified by:
      indexOf in interface List
    • lastIndexOf

      public int lastIndexOf(Object certificate)
      Returns the index of the given certificate or -1 if it's not in the store.
      Specified by:
      lastIndexOf in interface List
    • isEmpty

      public boolean isEmpty()
      Returns true if the store is empty.
      Specified by:
      isEmpty in interface Collection
      Specified by:
      isEmpty in interface List
    • iterator

      public Iterator iterator()
      Returns an Iterator that may be used to iterate through all the certificates in the store.
      Specified by:
      iterator in interface Collection
      Specified by:
      iterator in interface Iterable
      Specified by:
      iterator in interface List
    • listIterator

      public ListIterator listIterator()
      Returns an ListIterator that may be used to iterate through all the certificates in the store.
      Specified by:
      listIterator in interface List
    • listIterator

      public ListIterator listIterator(int index)
      Returns an ListIterator that may be used to iterate through all the certificates in the store that come after the given index.
      Specified by:
      listIterator in interface List
      Parameters:
      index - Index of first certificate to return.
    • remove

      public Object remove(int index)
      Removes the certificate at the given index.
      Specified by:
      remove in interface List
      Parameters:
      index - Index of certificate to remove.
    • removeCertificate

      public SSLFTPCertificate removeCertificate(int index)
      Removes the certificate at the given index.
      Parameters:
      index - Index of certificate to remove.
    • remove

      public boolean remove(Object certificate)
      Removes the given certificate from the collection.
      Specified by:
      remove in interface Collection
      Specified by:
      remove in interface List
      Parameters:
      certificate - reference of certificate to remove.
      Returns:
      true if the certificate was removed and false otherwise.
    • removeAll

      public boolean removeAll(Collection certificates)
      Removes from this Vector all of its elements that are contained in the specified Collection.
      Specified by:
      removeAll in interface Collection
      Specified by:
      removeAll in interface List
    • retainAll

      public boolean retainAll(Collection certificates)
      Retains only the elements in this Vector that are contained in the specified Collection. In other words, removes from this Vector all of its elements that are not contained in the specified Collection.
      Specified by:
      retainAll in interface Collection
      Specified by:
      retainAll in interface List
      Parameters:
      certificates - Certificates to retain.
      Returns:
      true if this Vector changed as a result of the call.
    • set

      public Object set(int index, Object certificate)
      Places the given certificate at the given position in the store, replacing any existing certifcate at the position.
      Specified by:
      set in interface List
      Parameters:
      index - Position to place the certificate at.
      certificate - Certificate to place.
      Throws:
      ArrayStoreException - Thrown if an object other than SSLFTPCertificate is set.
    • size

      public int size()
      Returns the number of components in this vector.
      Specified by:
      size in interface Collection
      Specified by:
      size in interface List
      Returns:
      The number of components in this vector.
    • subList

      public List subList(int fromIndex, int toIndex)
      Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and ToIndex are equal, the returned List is empty.) The returned List is backed by this List, so changes in the returned List are reflected in this List, and vice-versa. The returned List supports all of the optional List operations supported by this List.
      Specified by:
      subList in interface List
      Parameters:
      fromIndex - low endpoint (inclusive) of the subList.
      toIndex - high endpoint (exclusive) of the subList.
      Returns:
      a view of the specified range within this List.
    • toArray

      public Object[] toArray()
      Returns an array containing all of the elements in this Vector in the correct order.
      Specified by:
      toArray in interface Collection
      Specified by:
      toArray in interface List
    • toArray

      public Object[] toArray(Object[] certificates)
      Returns an array containing all of the elements in this Vector in the correct order. The runtime type of the returned array is that of the specified array. If the Vector fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this Vector.
      Specified by:
      toArray in interface Collection
      Specified by:
      toArray in interface List
    • toCertificateArray

      public SSLFTPCertificate[] toCertificateArray()
      Returns an SSLFTPCertificate array containing all of the elements in this Vector in the correct order.