com.enterprisedt.net.ftp.ssl
Class SSLFTPCertificateStore

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

public class SSLFTPCertificateStore
extends java.lang.Object
implements java.util.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 Summary
SSLFTPCertificateStore()
          Creates a empty certificate store.
 
Method Summary
 void add(int index, java.lang.Object certificate)
          Adds the given certificate SSLFTPCertificate to the store at the given position.
 boolean add(java.lang.Object certificate)
          Adds the given certificate SSLFTPCertificate to the store.
 boolean addAll(java.util.Collection certificates)
          Add all the SSLFTPCertificates in the given collection to the store.
 boolean addAll(int index, java.util.Collection certificates)
          Add all the SSLFTPCertificates in the given collection to the store at the given position.
 void clear()
          Removes all certificates from the collection.
 boolean contains(java.lang.Object certificate)
          Returns true if the given certificate is present in the collection.
 boolean containsAll(java.util.Collection certificates)
          Returns true if all of the given certificates are present in the collection.
 void exportKeyStore(java.security.KeyStore keyStore)
          Exports all certificates in the store to the given Java KeyStore.
 void exportPEMFile(java.io.OutputStream outputStream)
          Writes all the certificates in the store to the output-stream.
 void exportPEMFile(java.lang.String fileName)
          Writes all the certificates in the store to the given file.
 java.lang.Object get(int index)
          Returns the requested certificate in the store as an Object reference.
 SSLFTPCertificate getCertificate(int index)
          Returns the requested certificate in the store as an SSLFTPCertificate reference.
 void importCertificates(java.lang.String fileName)
          Import certificates from PEM file (see importPEMFile(String)) or from Java key store (see importPEMFile(String)).
 void importDefaultKeyStore()
          Imports all certificates in the default key-store for the platform on which the software is running.
 void importKeyStore(java.security.KeyStore keyStore)
          Imports all the certificates from the given Java KeyStore into this certificate store.
 void importKeyStore(java.lang.String keystoreFile)
          Imports all the certificates from the given Java KeyStore file into this certificate store.
 void importPEMFile(java.io.InputStream inputStream)
          Imports all the certificates in the given input-stream, into the store.
 void importPEMFile(java.lang.String fileName)
          Imports all the certificates in the given file into the store.
 int indexOf(java.lang.Object certificate)
          Returns the index of the given certificate or -1 if it's not in the store.
 boolean isEmpty()
          Returns true if the store is empty.
 java.util.Iterator iterator()
          Returns an Iterator that may be used to iterate through all the certificates in the store.
 int lastIndexOf(java.lang.Object certificate)
          Returns the index of the given certificate or -1 if it's not in the store.
 java.util.ListIterator listIterator()
          Returns an ListIterator that may be used to iterate through all the certificates in the store.
 java.util.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.
 java.lang.Object remove(int index)
          Removes the certificate at the given index.
 boolean remove(java.lang.Object certificate)
          Removes the given certificate from the collection.
 boolean removeAll(java.util.Collection certificates)
          Removes from this Vector all of its elements that are contained in the specified Collection.
 SSLFTPCertificate removeCertificate(int index)
          Removes the certificate at the given index.
 boolean retainAll(java.util.Collection certificates)
          Retains only the elements in this Vector that are contained in the specified Collection.
 java.lang.Object set(int index, java.lang.Object certificate)
          Places the given certificate at the given position in the store, replacing any existing certifcate at the position.
 int size()
          Returns the number of components in this vector.
 java.util.List subList(int fromIndex, int toIndex)
          Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this Vector in the correct order.
 java.lang.Object[] toArray(java.lang.Object[] certificates)
          Returns an array containing all of the elements in this Vector in the correct order.
 SSLFTPCertificate[] toCertificateArray()
          Returns an SSLFTPCertificate array containing all of the elements in this Vector in the correct order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode
 

Constructor Detail

SSLFTPCertificateStore

public SSLFTPCertificateStore()
Creates a empty certificate store.

Method Detail

importCertificates

public void importCertificates(java.lang.String fileName)
                        throws java.io.FileNotFoundException,
                               java.io.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:
java.io.FileNotFoundException
java.io.IOException

importPEMFile

public void importPEMFile(java.lang.String fileName)
                   throws java.io.FileNotFoundException,
                          java.io.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:
java.io.FileNotFoundException - Thrown if the file could not be found.
java.io.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(java.io.InputStream inputStream)
                   throws java.io.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:
java.io.FileNotFoundException - Thrown if the file could not be found.
java.io.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(java.lang.String fileName)
                   throws java.io.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:
java.io.IOException - Thrown if the file could not be created.

exportPEMFile

public void exportPEMFile(java.io.OutputStream outputStream)
                   throws java.io.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:
java.io.IOException - Thrown if there was an error writing to the stream.

importKeyStore

public void importKeyStore(java.security.KeyStore keyStore)
                    throws java.security.KeyStoreException,
                           SSLFTPCertificateException
Imports all the certificates from the given Java KeyStore into this certificate store.

Parameters:
keyStore - KeyStore to import from.
Throws:
java.security.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(java.lang.String keystoreFile)
                    throws java.security.KeyStoreException,
                           java.io.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:
java.security.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.
java.io.IOException - Thrown if there was a problem while accessing the file.
SSLFTPCertificateException

exportKeyStore

public void exportKeyStore(java.security.KeyStore keyStore)
                    throws java.security.cert.CertificateException,
                           java.security.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:
java.security.cert.CertificateException - Thrown if there was a problem with the certificate being extracted from the certificate store.
java.security.KeyStoreException - Thrown if there was an error while adding the certificate to the keystore.

importDefaultKeyStore

public void importDefaultKeyStore()
                           throws java.security.KeyStoreException,
                                  java.io.IOException,
                                  SSLFTPException
Imports all certificates in the default key-store for the platform on which the software is running. The file that is imported is {java.home}/lib/security/cacerts

Throws:
java.security.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.
java.io.IOException - Thrown if there was a problem while accessing the file.

add

public void add(int index,
                java.lang.Object certificate)
Adds the given certificate SSLFTPCertificate to the store at the given position.

Specified by:
add in interface java.util.List
Parameters:
index - Position to add the certificate at.
certificate - SSLFTPCertificate to add.
Throws:
java.lang.ArrayStoreException - Thrown if an object other than SSLFTPCertificate is added.

add

public boolean add(java.lang.Object certificate)
Adds the given certificate SSLFTPCertificate to the store.

Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.List
Parameters:
certificate - SSLFTPCertificate to add.
Throws:
java.lang.ArrayStoreException - Thrown if an object other than SSLFTPCertificate is added.

addAll

public boolean addAll(java.util.Collection certificates)
Add all the SSLFTPCertificates in the given collection to the store.

Specified by:
addAll in interface java.util.Collection
Specified by:
addAll in interface java.util.List
Parameters:
certificates - SSLFTPCertificates to add.
Throws:
java.lang.ArrayStoreException - Thrown if an object other than SSLFTPCertificate is added.

addAll

public boolean addAll(int index,
                      java.util.Collection certificates)
Add all the SSLFTPCertificates in the given collection to the store at the given position.

Specified by:
addAll in interface java.util.List
Parameters:
index - Position at which to add the certificates.
certificates - SSLFTPCertificates to add.
Throws:
java.lang.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 java.util.Collection
Specified by:
clear in interface java.util.List

contains

public boolean contains(java.lang.Object certificate)
Returns true if the given certificate is present in the collection.

Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.List

containsAll

public boolean containsAll(java.util.Collection certificates)
Returns true if all of the given certificates are present in the collection.

Specified by:
containsAll in interface java.util.Collection
Specified by:
containsAll in interface java.util.List

get

public java.lang.Object get(int index)
Returns the requested certificate in the store as an Object reference.

Specified by:
get in interface java.util.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(java.lang.Object certificate)
Returns the index of the given certificate or -1 if it's not in the store.

Specified by:
indexOf in interface java.util.List

lastIndexOf

public int lastIndexOf(java.lang.Object certificate)
Returns the index of the given certificate or -1 if it's not in the store.

Specified by:
lastIndexOf in interface java.util.List

isEmpty

public boolean isEmpty()
Returns true if the store is empty.

Specified by:
isEmpty in interface java.util.Collection
Specified by:
isEmpty in interface java.util.List

iterator

public java.util.Iterator iterator()
Returns an Iterator that may be used to iterate through all the certificates in the store.

Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Specified by:
iterator in interface java.util.List

listIterator

public java.util.ListIterator listIterator()
Returns an ListIterator that may be used to iterate through all the certificates in the store.

Specified by:
listIterator in interface java.util.List

listIterator

public java.util.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 java.util.List
Parameters:
index - Index of first certificate to return.

remove

public java.lang.Object remove(int index)
Removes the certificate at the given index.

Specified by:
remove in interface java.util.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(java.lang.Object certificate)
Removes the given certificate from the collection.

Specified by:
remove in interface java.util.Collection
Specified by:
remove in interface java.util.List
Parameters:
certificate - reference of certificate to remove.
Returns:
true if the certificate was removed and false otherwise.

removeAll

public boolean removeAll(java.util.Collection certificates)
Removes from this Vector all of its elements that are contained in the specified Collection.

Specified by:
removeAll in interface java.util.Collection
Specified by:
removeAll in interface java.util.List

retainAll

public boolean retainAll(java.util.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 java.util.Collection
Specified by:
retainAll in interface java.util.List
Parameters:
certificates - Certificates to retain.
Returns:
true if this Vector changed as a result of the call.

set

public java.lang.Object set(int index,
                            java.lang.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 java.util.List
Parameters:
index - Position to place the certificate at.
certificate - Certificate to place.
Throws:
java.lang.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 java.util.Collection
Specified by:
size in interface java.util.List
Returns:
The number of components in this vector.

subList

public java.util.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 java.util.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 java.lang.Object[] toArray()
Returns an array containing all of the elements in this Vector in the correct order.

Specified by:
toArray in interface java.util.Collection
Specified by:
toArray in interface java.util.List

toArray

public java.lang.Object[] toArray(java.lang.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 java.util.Collection
Specified by:
toArray in interface java.util.List

toCertificateArray

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



Copyright © 2001-2006 Enterprise Distributed Technologies Ltd. All Rights Reserved.