Overview

This component provides a api to invoke the keytool java program. As the keytool is not compatible from a jdk to another one.

There is implementation for jdk 1.5 and 1.6+. (jdk 1.6 and more are compatible)

Dependency declaration

To use it according to the jdk installed on your system, use some profile activated by the jdk version:

<profile>
  <id>api-1.5</id>
  <activation>
    <jdk>1.5</jdk>
  </activation>

  <dependencies>
    <dependency>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>keytool-api-1.5</artifactId>
      <version>1.6-SNAPSHOT</version>
    </dependency>
  </dependencies>
</profile>

<profile>
  <id>api-1.6</id>
  <activation>
    <jdk>1.6</jdk>
  </activation>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>keytool-api-1.6</artifactId>
      <version>1.6-SNAPSHOT</version>
    </dependency>
  </dependencies>
</profile>

<profile>
  <id>api-1.7</id>
  <activation>
    <jdk>1.7</jdk>
  </activation>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>keytool-api-1.6</artifactId>
      <version>1.6-SNAPSHOT</version>
    </dependency>
  </dependencies>
</profile>

API

Change an alias inside a keystore

You must construct a KeyToolChangeAliasRequest. See javadoc for more available options.

KeyToolChangeAliasRequest request = new KeyToolChangeAliasRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setAlias( "foo_alias" );
request.setKeypass( "key-passwd" );
request.setDestalias( "dest-alias" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Change a key password

You must construct a KeyToolChangeKeyPasswordRequest. See javadoc for more available options.

KeyToolChangeKeyPasswordRequest request = new KeyToolChangeKeyPasswordRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setAlias( "foo_alias" );
request.setKeypass( "key-passwd" );
request.setNewPassword( "new-key-passwd" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Change a key store password

You must construct a KeyToolChangeStorePasswordRequest. See javadoc for more available options.

KeyToolChangeStorePasswordRequest request = new KeyToolChangeStorePasswordRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setNewPassword( "new-changeit" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Delete a key

You must construct a KeyToolDeleteRequest. See javadoc for more available options.

KeyToolDeleteRequest request = new KeyToolDeleteRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setAlias( "foo_alias" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Export a certificate

You must construct a KeyToolExportCertificateRequest. See javadoc for more available options.

KeyToolExportCertificateRequest request = new KeyToolExportCertificateRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setAlias( "foo_alias" );
request.setFile( "outputFile" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Generate a certificate from a certificate request

You must construct a KeyToolGenerateCertificateRequest. See javadoc for more available options.

KeyToolGenerateCertificateRequest request = new KeyToolGenerateCertificateRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setAlias( "foo_alias" );
request.setKeypass( "key-passwd" );
request.setInfile( inFile );
request.setOutfile( "outputFile" );
request.setSigalg( "SHA1withDSA" );
request.setDname( "CN=Me, OU=Unknown, O=Codehaus, L=Unknown, ST=Unknown, C=France" );
request.setStartdate( "2011/11/11" );
request.setValidity( "100" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Generate a certificate request

You must construct a KeyToolGenerateCertificateRequestRequest. See javadoc for more available options.

KeyToolGenerateCertificateRequestRequest request = new KeyToolGenerateCertificateRequestRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setAlias( "foo_alias" );
request.setKeypass( "key-passwd" );
request.setFile( outputFile );
request.setSigalg( "SHA1withDSA" );
request.setDname( "CN=Me, OU=Unknown, O=Codehaus, L=Unknown, ST=Unknown, C=France" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Generate a key pair

You must construct a KeyToolGenerateKeyPairRequest. See javadoc for more available options.

KeyToolGenerateKeyPairRequest request = new KeyToolGenerateKeyPairRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setAlias( "foo_alias" );
request.setKeypass( "key-passwd" );
request.setSigalg( "SHA1withDSA" );
request.setDname( "CN=Me, OU=Unknown, O=Codehaus, L=Unknown, ST=Unknown, C=France" );
request.setValidity( "100" );
request.setStartdate( "2011/11/11" );
request.setKeyalg( "DSA" );
request.setKeysize( "1024" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Generate a secret key

You must construct a KeyToolGenerateSecretKeyRequest. See javadoc for more available options.

KeyToolGenerateSecretKeyRequest request = new KeyToolGenerateSecretKeyRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setAlias( "foo_alias" );
request.setKeypass( "key-passwd" );
request.setKeyalg( "DES" );
request.setKeysize( "56" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Import a certificate into a key store

You must construct a KeyToolImportCertificateRequest. See javadoc for more available options.

KeyToolImportCertificateRequest request = new KeyToolImportCertificateRequest();
request.setVerbose( true );
request.setKeystore( "keystore" );
request.setStorepass( "changeit" );
request.setAlias( "foo_alias" );
request.setKeypass( "key-passwd" );
request.setFile( "certificate" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Import a key store to another one

You must construct a KeyToolImportKeystoreRequest. See javadoc for more available options.

KeyToolImportKeystoreRequest request = new KeyToolImportKeystoreRequest();
request.setVerbose( true );
request.setSrckeystore( "keystore" );
request.setDestkeystore( "dest-keystore" );
request.setSrcstorepass( "changeit" );
request.setDeststorepass( "changeit" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

List the content of a key store

You must construct a KeyToolListRequest. See javadoc for more available options.

KeyToolListRequest request = new KeyToolListRequest();
request.setVerbose( true );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Display content of a certificate

You must construct a KeyToolPrintCertificateRequest. See javadoc for more available options.

KeyToolPrintCertificateRequest request = new KeyToolPrintCertificateRequest();
request.setVerbose( true );
request.setFile( "certificate" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Display content of a certificate request

You must construct a KeyToolPrintCertificateRequestRequest. See javadoc for more available options.

KeyToolPrintCertificateRequestRequest request = new KeyToolPrintCertificateRequestRequest();
request.setVerbose( true );
request.setFile( "certificate-request" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()

Display content of a CRL file

You must construct a KeyToolPrintCRLFileRequest. See javadoc for more available options.

KeyToolPrintCRLFileRequest request = new KeyToolPrintCRLFileRequest();
request.setVerbose( true );
request.setFile( "crf-file" );

Now you can use the component to sign your jar:

JavaToolResult result = keyTool.execute( request );
// control the execution result
result.getExitCode()
// get exception
result.getExecutionException()