org.apache.commons.ssl

## Class KeyStoreBuilder

- java.lang.Object
- - org.apache.commons.ssl.KeyStoreBuilder

    public class KeyStoreBuilder
    extends java.lang.Object

Builds Java Key Store files out of pkcs12 files, or out of pkcs8 files + certificate chains. Also supports OpenSSL style private keys (encrypted or unencrypted).

Since:
  4-Nov-2006
Author:
  Credit Union Central of British Columbia, www.cucbc.com, juliusdavies@cucbc.com

### Nested Class Summary

Nested Classes
`static class` `KeyStoreBuilder.BuildResult`

### Field Summary

Fields
`private static java.lang.String` `PKCS7_ENCRYPTED`

### Constructor Summary

Constructors
Constructor and Description
`KeyStoreBuilder()`

### Method Summary

Methods
`static java.security.KeyStore` `build(byte[] jksOrCerts, byte[] privateKey, char[] password)`
`static java.security.KeyStore` `build(byte[] jksOrCerts, byte[] privateKey, char[] jksPassword, char[] keyPassword)`
`static java.security.KeyStore` `build(byte[] jksOrCerts, char[] password)`
`static java.security.cert.X509Certificate` `buildChain(java.security.Key key, java.security.cert.Certificate[] chain)` Builds the chain up such that chain[ 0 ] contains the public key corresponding to the supplied private key.
`static void` `main(java.lang.String[] args)`
`static KeyStoreBuilder.BuildResult` `parse(byte[] stuff, char[] jksPass, char[] keyPass)`
`(package private) static KeyStoreBuilder.BuildResult` `parse(byte[] stuff, char[] jksPass, char[] keyPass, boolean forTrustMaterial)`
`private static java.security.cert.X509Certificate[]` `toChain(java.util.Collection certs)`
`private static KeyStoreBuilder.BuildResult` `tryJKS(java.lang.String keystoreType, java.io.ByteArrayInputStream in, char[] jksPassword, char[] keyPassword, boolean forTrustMaterial)`
`static KeyStoreBuilder.BuildResult` `validate(java.security.KeyStore jks, char[] keyPass)`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### PKCS7_ENCRYPTED

    private static final java.lang.String PKCS7_ENCRYPTED

See Also:
  Constant Field Values

### Constructor Detail

#### KeyStoreBuilder

    public KeyStoreBuilder()

### Method Detail

#### build

    public static java.security.KeyStore build(byte[] jksOrCerts,
                               char[] password)
                                        throws java.io.IOException,
                                               java.security.cert.CertificateException,
                                               java.security.KeyStoreException,
                                               java.security.NoSuchAlgorithmException,
                                               java.security.InvalidKeyException,
                                               java.security.NoSuchProviderException,
                                               ProbablyBadPasswordException,
                                               java.security.UnrecoverableKeyException

Throws:
  `java.io.IOException`
  `java.security.cert.CertificateException`
  `java.security.KeyStoreException`
  `java.security.NoSuchAlgorithmException`
  `java.security.InvalidKeyException`
  `java.security.NoSuchProviderException`
  `ProbablyBadPasswordException`
  `java.security.UnrecoverableKeyException`

#### build

    public static java.security.KeyStore build(byte[] jksOrCerts,
                               byte[] privateKey,
                               char[] password)
                                        throws java.io.IOException,
                                               java.security.cert.CertificateException,
                                               java.security.KeyStoreException,
                                               java.security.NoSuchAlgorithmException,
                                               java.security.InvalidKeyException,
                                               java.security.NoSuchProviderException,
                                               ProbablyBadPasswordException,
                                               java.security.UnrecoverableKeyException

Throws:
  `java.io.IOException`
  `java.security.cert.CertificateException`
  `java.security.KeyStoreException`
  `java.security.NoSuchAlgorithmException`
  `java.security.InvalidKeyException`
  `java.security.NoSuchProviderException`
  `ProbablyBadPasswordException`
  `java.security.UnrecoverableKeyException`

#### build

    public static java.security.KeyStore build(byte[] jksOrCerts,
                               byte[] privateKey,
                               char[] jksPassword,
                               char[] keyPassword)
                                        throws java.io.IOException,
                                               java.security.cert.CertificateException,
                                               java.security.KeyStoreException,
                                               java.security.NoSuchAlgorithmException,
                                               java.security.InvalidKeyException,
                                               java.security.NoSuchProviderException,
                                               ProbablyBadPasswordException,
                                               java.security.UnrecoverableKeyException

Throws:
  `java.io.IOException`
  `java.security.cert.CertificateException`
  `java.security.KeyStoreException`
  `java.security.NoSuchAlgorithmException`
  `java.security.InvalidKeyException`
  `java.security.NoSuchProviderException`
  `ProbablyBadPasswordException`
  `java.security.UnrecoverableKeyException`

#### buildChain

    public static java.security.cert.X509Certificate buildChain(java.security.Key key,
                                                java.security.cert.Certificate[] chain)
                                                         throws java.security.cert.CertificateException,
                                                                java.security.KeyStoreException,
                                                                java.security.NoSuchAlgorithmException,
                                                                java.security.InvalidKeyException,
                                                                java.security.NoSuchProviderException

Builds the chain up such that chain[ 0 ] contains the public key corresponding to the supplied private key.

Parameters:
  `key` - private key
  `chain` - array of certificates to build chain from
Returns:
  theOne!
Throws:
  `java.security.KeyStoreException` - no certificates correspond to private key
  `java.security.cert.CertificateException` - java libraries complaining
  `java.security.NoSuchAlgorithmException` - java libraries complaining
  `java.security.InvalidKeyException` - java libraries complaining
  `java.security.NoSuchProviderException` - java libraries complaining

#### validate

    public static KeyStoreBuilder.BuildResult validate(java.security.KeyStore jks,
                                       char[] keyPass)
                                                throws java.security.cert.CertificateException,
                                                       java.security.KeyStoreException,
                                                       java.security.NoSuchAlgorithmException,
                                                       java.security.InvalidKeyException,
                                                       java.security.NoSuchProviderException,
                                                       java.security.UnrecoverableKeyException

Throws:
  `java.security.cert.CertificateException`
  `java.security.KeyStoreException`
  `java.security.NoSuchAlgorithmException`
  `java.security.InvalidKeyException`
  `java.security.NoSuchProviderException`
  `java.security.UnrecoverableKeyException`

#### parse

    public static KeyStoreBuilder.BuildResult parse(byte[] stuff,
                                    char[] jksPass,
                                    char[] keyPass)
                                             throws java.io.IOException,
                                                    java.security.cert.CertificateException,
                                                    java.security.KeyStoreException,
                                                    ProbablyBadPasswordException

Throws:
  `java.io.IOException`
  `java.security.cert.CertificateException`
  `java.security.KeyStoreException`
  `ProbablyBadPasswordException`

#### parse

    static KeyStoreBuilder.BuildResult parse(byte[] stuff,
                                    char[] jksPass,
                                    char[] keyPass,
                                    boolean forTrustMaterial)
                                      throws java.io.IOException,
                                             java.security.cert.CertificateException,
                                             java.security.KeyStoreException,
                                             ProbablyBadPasswordException

Throws:
  `java.io.IOException`
  `java.security.cert.CertificateException`
  `java.security.KeyStoreException`
  `ProbablyBadPasswordException`

#### tryJKS

    private static KeyStoreBuilder.BuildResult tryJKS(java.lang.String keystoreType,
                                     java.io.ByteArrayInputStream in,
                                     char[] jksPassword,
                                     char[] keyPassword,
                                     boolean forTrustMaterial)
                                               throws ProbablyBadPasswordException

Throws:
  `ProbablyBadPasswordException`

#### toChain

    private static java.security.cert.X509Certificate[] toChain(java.util.Collection certs)

#### main

    public static void main(java.lang.String[] args)
                     throws java.lang.Exception

Throws:
  `java.lang.Exception`