Package com.enterprisedt.net.ftp.ssl
Class SSLFTPCertificateStore
java.lang.Object
com.enterprisedt.net.ftp.ssl.SSLFTPCertificateStore
- All Implemented Interfaces:
Iterable,Collection,List,SequencedCollection
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the given certificateSSLFTPCertificateto the store at the given position.booleanAdds the given certificateSSLFTPCertificateto the store.booleanaddAll(int index, Collection certificates) Add all theSSLFTPCertificates in the given collection to the store at the given position.booleanaddAll(Collection certificates) Add all theSSLFTPCertificates in the given collection to the store.voidclear()Removes all certificates from the collection.booleanReturnstrueif the given certificate is present in the collection.booleancontainsAll(Collection certificates) Returnstrueif all of the given certificates are present in the collection.voidexportKeyStore(KeyStore keyStore) Exports all certificates in the store to the given Java KeyStore.voidexportPEMFile(OutputStream outputStream) Writes all the certificates in the store to the output-stream.voidexportPEMFile(String fileName) Writes all the certificates in the store to the given file.get(int index) Returns the requested certificate in the store as anObjectreference.getCertificate(int index) Returns the requested certificate in the store as anSSLFTPCertificatereference.voidimportCertificates(String fileName) Import certificates from PEM file (seeimportPEMFile(String)) or from Java key store (seeimportPEMFile(String)).voidImports all certificates in the default key-store for the platform on which the software is running.voidimportKeyStore(String keystoreFile) Imports all the certificates from the given Java KeyStore file into this certificate store.voidimportKeyStore(String keystoreFile, String password) Imports all the certificates from the given Java KeyStore file into this certificate store.voidimportKeyStore(KeyStore keyStore) Imports all the certificates from the given Java KeyStore into this certificate store.voidimportPEMFile(InputStream inputStream) Imports all the certificates in the given input-stream, into the store.voidimportPEMFile(String fileName) Imports all the certificates in the given file into the store.intReturns the index of the given certificate or -1 if it's not in the store.booleanisEmpty()Returnstrueif the store is empty.iterator()Returns anIteratorthat may be used to iterate through all the certificates in the store.intlastIndexOf(Object certificate) Returns the index of the given certificate or -1 if it's not in the store.Returns anListIteratorthat may be used to iterate through all the certificates in the store.listIterator(int index) Returns anListIteratorthat may be used to iterate through all the certificates in the store that come after the given index.remove(int index) Removes the certificate at the given index.booleanRemoves the given certificate from the collection.booleanremoveAll(Collection certificates) Removes from this Vector all of its elements that are contained in the specified Collection.removeCertificate(int index) Removes the certificate at the given index.booleanretainAll(Collection certificates) Retains only the elements in this Vector that are contained in the specified Collection.Places the given certificate at the given position in the store, replacing any existing certifcate at the position.intsize()Returns the number of components in this vector.subList(int fromIndex, int toIndex) Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.Object[]toArray()Returns an array containing all of the elements in this Vector in the correct order.Object[]Returns an array containing all of the elements in this Vector in the correct order.Returns anSSLFTPCertificatearray 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, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addFirst, addLast, equals, getFirst, getLast, hashCode, removeFirst, removeLast, replaceAll, reversed, sort, spliterator
-
Constructor Details
-
SSLFTPCertificateStore
public SSLFTPCertificateStore()Creates a empty certificate store.
-
-
Method Details
-
importCertificates
Import certificates from PEM file (seeimportPEMFile(String)) or from Java key store (seeimportPEMFile(String)).- Parameters:
fileName- path to certificate file or key store- Throws:
FileNotFoundExceptionIOException
-
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
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
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
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
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 importpassword- 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
Exports all certificates in the store to the given Java KeyStore. The alias of each certificate will be its CommonName (CN) if it is notnullor 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
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
Adds the given certificateSSLFTPCertificateto the store at the given position.- Specified by:
addin interfaceList- Parameters:
index- Position to add the certificate at.certificate-SSLFTPCertificateto add.- Throws:
ArrayStoreException- Thrown if an object other thanSSLFTPCertificateis added.
-
add
Adds the given certificateSSLFTPCertificateto the store.- Specified by:
addin interfaceCollection- Specified by:
addin interfaceList- Parameters:
certificate-SSLFTPCertificateto add.- Throws:
ArrayStoreException- Thrown if an object other thanSSLFTPCertificateis added.
-
addAll
Add all theSSLFTPCertificates in the given collection to the store.- Specified by:
addAllin interfaceCollection- Specified by:
addAllin interfaceList- Parameters:
certificates-SSLFTPCertificates to add.- Throws:
ArrayStoreException- Thrown if an object other thanSSLFTPCertificateis added.
-
addAll
Add all theSSLFTPCertificates in the given collection to the store at the given position.- Specified by:
addAllin interfaceList- Parameters:
index- Position at which to add the certificates.certificates-SSLFTPCertificates to add.- Throws:
ArrayStoreException- Thrown if an object other thanSSLFTPCertificateis added.
-
clear
public void clear()Removes all certificates from the collection.- Specified by:
clearin interfaceCollection- Specified by:
clearin interfaceList
-
contains
Returnstrueif the given certificate is present in the collection.- Specified by:
containsin interfaceCollection- Specified by:
containsin interfaceList
-
containsAll
Returnstrueif all of the given certificates are present in the collection.- Specified by:
containsAllin interfaceCollection- Specified by:
containsAllin interfaceList
-
get
Returns the requested certificate in the store as anObjectreference. -
getCertificate
Returns the requested certificate in the store as anSSLFTPCertificatereference.- Parameters:
index- Position of certificate to return.
-
indexOf
Returns the index of the given certificate or -1 if it's not in the store. -
lastIndexOf
Returns the index of the given certificate or -1 if it's not in the store.- Specified by:
lastIndexOfin interfaceList
-
isEmpty
public boolean isEmpty()Returnstrueif the store is empty.- Specified by:
isEmptyin interfaceCollection- Specified by:
isEmptyin interfaceList
-
iterator
Returns anIteratorthat may be used to iterate through all the certificates in the store. -
listIterator
Returns anListIteratorthat may be used to iterate through all the certificates in the store.- Specified by:
listIteratorin interfaceList
-
listIterator
Returns anListIteratorthat may be used to iterate through all the certificates in the store that come after the given index.- Specified by:
listIteratorin interfaceList- Parameters:
index- Index of first certificate to return.
-
remove
Removes the certificate at the given index. -
removeCertificate
Removes the certificate at the given index.- Parameters:
index- Index of certificate to remove.
-
remove
Removes the given certificate from the collection.- Specified by:
removein interfaceCollection- Specified by:
removein interfaceList- Parameters:
certificate- reference of certificate to remove.- Returns:
trueif the certificate was removed andfalseotherwise.
-
removeAll
Removes from this Vector all of its elements that are contained in the specified Collection.- Specified by:
removeAllin interfaceCollection- Specified by:
removeAllin interfaceList
-
retainAll
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:
retainAllin interfaceCollection- Specified by:
retainAllin interfaceList- Parameters:
certificates- Certificates to retain.- Returns:
trueif this Vector changed as a result of the call.
-
set
Places the given certificate at the given position in the store, replacing any existing certifcate at the position.- Specified by:
setin interfaceList- Parameters:
index- Position to place the certificate at.certificate- Certificate to place.- Throws:
ArrayStoreException- Thrown if an object other thanSSLFTPCertificateis set.
-
size
public int size()Returns the number of components in this vector.- Specified by:
sizein interfaceCollection- Specified by:
sizein interfaceList- Returns:
- The number of components in this vector.
-
subList
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. -
toArray
Returns an array containing all of the elements in this Vector in the correct order.- Specified by:
toArrayin interfaceCollection- Specified by:
toArrayin interfaceList
-
toArray
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:
toArrayin interfaceCollection- Specified by:
toArrayin interfaceList
-
toCertificateArray
Returns anSSLFTPCertificatearray containing all of the elements in this Vector in the correct order.
-