Class ODLKeyTool


  • public class ODLKeyTool
    extends Object
    ODLKeyTool implements the basic operations that manage the Java keyStores such as create, generate, add and delete certificates.
    Author:
    mserngawy
    • Constructor Detail

      • ODLKeyTool

        protected ODLKeyTool()
      • ODLKeyTool

        public ODLKeyTool​(String workingDirectory)
    • Method Detail

      • addCertificate

        public KeyStore addCertificate​(KeyStore keyStore,
                                       String certificate,
                                       String alias,
                                       boolean deleteOld)
        Add certificate to the given keystore.
        Parameters:
        keyStore - java keystore object
        certificate - to add as string
        alias - of the certificate
        deleteOld - true to delete the old certificate that has the same alias otherwise it will fail if there is a certificate has same given alias.
        Returns:
        the given Keystore containing the certificate otherwise return null.
      • convertKeystoreToBytes

        public byte[] convertKeystoreToBytes​(KeyStore keyStore,
                                             String keystorePassword)
        Convert the given java keystore object to byte array.
        Parameters:
        keyStore - object
        keystorePassword - the password of the given keystore
        Returns:
        byte array
      • createKeyStoreWithSelfSignCert

        public KeyStore createKeyStoreWithSelfSignCert​(String keyStoreName,
                                                       String keystorePassword,
                                                       String distinguishedName,
                                                       String keyAlias,
                                                       int validity)
        Create a keystore that has self sign private/public keys using the default key algorithm (RSA), size (2048) and signing algorithm (SHA1WithRSAEncryption).
        Parameters:
        keyStoreName - the keystore name
        keystorePassword - the keystore password
        distinguishedName - the generated key's Distinguished Name
        keyAlias - the private key alias
        validity - the key validity
        Returns:
        keystore object
      • createKeyStoreWithSelfSignCert

        public KeyStore createKeyStoreWithSelfSignCert​(String keyStoreName,
                                                       String keystorePassword,
                                                       String distinguishedName,
                                                       String keyAlias,
                                                       int validity,
                                                       String keyAlg,
                                                       int keySize,
                                                       String signAlg)
        Create a keystore that has self sign private/public keys.
        Parameters:
        keyStoreName - the keystore name
        keystorePassword - the keystore password
        distinguishedName - the generated key's Distinguished Name
        keyAlias - the private key alias
        validity - the key validity
        keyAlg - the algorithm that will be used to generate the key
        keySize - the key size
        signAlg - the signing algorithm
        Returns:
        keystore object
      • createEmptyKeyStore

        public KeyStore createEmptyKeyStore​(String keystorePassword)
        Create empty keystore does not has private or public key.
        Parameters:
        keystorePassword - the keystore password
        Returns:
        keystore object
      • exportKeystore

        public boolean exportKeystore​(KeyStore keystore,
                                      String keystorePassword,
                                      String fileName)
        Export the given keystore as a file under the working directory.
        Parameters:
        keystore - object
        keystorePassword - the keystore password
        fileName - of the keystore
        Returns:
        true if successes to export the keystore
      • generateCertificateReq

        public String generateCertificateReq​(KeyStore keyStore,
                                             String keystorePassword,
                                             String keyAlias,
                                             String signAlg,
                                             boolean withTag)
        Generate a certificate signing request based on the given keystore private/public key.
        Parameters:
        keyStore - object
        keystorePassword - the keystore password
        keyAlias - Alias of the given keystore's private key.
        signAlg - the signing algorithm
        withTag - true to add the certificate request tag to the certificate request string.
        Returns:
        certificate request as string.
      • getCertificate

        public String getCertificate​(KeyStore keyStore,
                                     String certAlias,
                                     boolean withTag)
        Get a certificate as String based on the given alias.
        Parameters:
        keyStore - keystore that has the certificate
        certAlias - certificate alias
        withTag - true to add the certificate tag to the certificate string.
        Returns:
        certificate as string.
      • loadKeyStore

        public KeyStore loadKeyStore​(byte[] keyStoreBytes,
                                     String keystorePassword)
        Load the keystore object from the given byte array.
        Parameters:
        keyStoreBytes - array of byte contain keystore object
        keystorePassword - the keystore password
        Returns:
        keystore object otherwise return null if it fails to load.
      • loadKeyStore

        public KeyStore loadKeyStore​(String keyStoreName,
                                     String keystorePassword)
        Load the keystore from the working directory.
        Parameters:
        keyStoreName - keystore file name
        keystorePassword - keystore password
        Returns:
        keystore object otherwise return null if it fails to load.