cybervillains.ca
Class KeyStoreManager

java.lang.Object
  extended by cybervillains.ca.KeyStoreManager

public class KeyStoreManager
extends Object

This is the main entry point into the Cybervillains CA. This class handles generation, storage and the persistent mapping of input to duplicated certificates and mapped public keys. Default setting is to immediately persist changes to the store by writing out the keystore and mapping file every time a new certificate is added. This behavior can be disabled if desired, to enhance performance or allow temporary testing without modifying the certificate store. Copyright (c) 2007, Information Security Partners, LLC All rights reserved. In a special exception, Selenium/OpenQA is allowed to use this code under the Apache License 2.0.

Author:
Brad Hill

Field Summary
static String _caPrivKeyAlias
           
 KeyPairGenerator _dsaKpg
           
 KeyPairGenerator _rsaKpg
           
 String DSA_KEYGEN_ALGO
           
 String RSA_KEYGEN_ALGO
           
 
Constructor Summary
KeyStoreManager(File root, String certificateRevocationListPath)
           
 
Method Summary
 void addCertAndPrivateKey(String hostname, X509Certificate cert, PrivateKey privKey)
          Stores a new certificate and its associated private key in the keystore.
protected  void createKeystore()
          Creates, writes and loads a new keystore and CA root certificate.
 X509Certificate getCertificateByAlias(String alias)
          Returns the aliased certificate.
 X509Certificate getCertificateByHostname(String hostname)
          Returns the aliased certificate.
 KeyPair getDSAKeyPair()
          Generate a DSA Key Pair
 KeyStore getKeyStore()
           
 X509Certificate getMappedCertificate(X509Certificate cert)
          This method returns the duplicated certificate mapped to the passed in cert, or creates and returns one if no mapping has yet been performed.
 X509Certificate getMappedCertificateForHostname(String hostname)
          This method returns the mapped certificate for a hostname, or generates a "standard" SSL server certificate issued by the CA to the supplied subject if no mapping has been created.
 PublicKey getMappedPublicKey(PublicKey original)
          If we get a KeyValue with a given public key, then later see an X509Data with the same public key, we shouldn't split this in our MITM impl.
 boolean getPersistImmediately()
          Whether updates are immediately written to disk.
 PrivateKey getPrivateKey(PublicKey pk)
          Returns the private key for a public key we have generated.
 PrivateKey getPrivateKeyForLocalCert(X509Certificate cert)
          For a cert we have generated, return the private key.
 KeyPair getRSAKeyPair()
          Generate an RSA Key Pair
 X509Certificate getSigningCert()
          Gets the authority root signing cert.
 PrivateKey getSigningPrivateKey()
          Gets the authority private signing key.
 void mapPublicKeys(PublicKey original, PublicKey substitute)
          Stores a public key mapping.
 void persist()
          Writes the keystore and certificate/keypair mappings to disk.
 void setPersistImmediately(boolean persistImmediately)
          Whether updates are immediately written to disk.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_caPrivKeyAlias

public static final String _caPrivKeyAlias
See Also:
Constant Field Values

RSA_KEYGEN_ALGO

public final String RSA_KEYGEN_ALGO
See Also:
Constant Field Values

DSA_KEYGEN_ALGO

public final String DSA_KEYGEN_ALGO
See Also:
Constant Field Values

_rsaKpg

public final KeyPairGenerator _rsaKpg

_dsaKpg

public final KeyPairGenerator _dsaKpg
Constructor Detail

KeyStoreManager

public KeyStoreManager(File root,
                       String certificateRevocationListPath)
Method Detail

createKeystore

protected void createKeystore()
Creates, writes and loads a new keystore and CA root certificate.


addCertAndPrivateKey

public void addCertAndPrivateKey(String hostname,
                                 X509Certificate cert,
                                 PrivateKey privKey)
                          throws KeyStoreException,
                                 CertificateException,
                                 NoSuchAlgorithmException
Stores a new certificate and its associated private key in the keystore.

Parameters:
hostname -
cert -
privKey - @throws KeyStoreException
Throws:
CertificateException
NoSuchAlgorithmException
KeyStoreException

persist

public void persist()
             throws KeyStoreException,
                    NoSuchAlgorithmException,
                    CertificateException
Writes the keystore and certificate/keypair mappings to disk.

Throws:
KeyStoreException
NoSuchAlgorithmException
CertificateException

getCertificateByAlias

public X509Certificate getCertificateByAlias(String alias)
                                      throws KeyStoreException
Returns the aliased certificate. Certificates are aliased by their SHA1 digest.

Parameters:
alias -
Returns:
Throws:
KeyStoreException
See Also:
ThumbprintUtil

getCertificateByHostname

public X509Certificate getCertificateByHostname(String hostname)
                                         throws KeyStoreException,
                                                CertificateParsingException,
                                                InvalidKeyException,
                                                CertificateExpiredException,
                                                CertificateNotYetValidException,
                                                SignatureException,
                                                CertificateException,
                                                NoSuchAlgorithmException,
                                                NoSuchProviderException,
                                                UnrecoverableKeyException
Returns the aliased certificate. Certificates are aliased by their hostname.

Parameters:
alias -
Returns:
Throws:
KeyStoreException
UnrecoverableKeyException
NoSuchProviderException
NoSuchAlgorithmException
CertificateException
SignatureException
CertificateNotYetValidException
CertificateExpiredException
InvalidKeyException
CertificateParsingException
See Also:
ThumbprintUtil

getSigningCert

public X509Certificate getSigningCert()
                               throws KeyStoreException
Gets the authority root signing cert.

Returns:
Throws:
KeyStoreException

getSigningPrivateKey

public PrivateKey getSigningPrivateKey()
                                throws KeyStoreException,
                                       NoSuchAlgorithmException,
                                       UnrecoverableKeyException
Gets the authority private signing key.

Returns:
Throws:
KeyStoreException
NoSuchAlgorithmException
UnrecoverableKeyException

getPersistImmediately

public boolean getPersistImmediately()
Whether updates are immediately written to disk.

Returns:

setPersistImmediately

public void setPersistImmediately(boolean persistImmediately)
Whether updates are immediately written to disk.

Parameters:
persistImmediately -

getMappedCertificate

public X509Certificate getMappedCertificate(X509Certificate cert)
                                     throws CertificateEncodingException,
                                            InvalidKeyException,
                                            CertificateException,
                                            CertificateNotYetValidException,
                                            NoSuchAlgorithmException,
                                            NoSuchProviderException,
                                            SignatureException,
                                            KeyStoreException,
                                            UnrecoverableKeyException
This method returns the duplicated certificate mapped to the passed in cert, or creates and returns one if no mapping has yet been performed. If a naked public key has already been mapped that matches the key in the cert, the already mapped keypair will be reused for the mapped cert.

Parameters:
cert -
Returns:
Throws:
CertificateEncodingException
InvalidKeyException
CertificateException
CertificateNotYetValidException
NoSuchAlgorithmException
NoSuchProviderException
SignatureException
KeyStoreException
UnrecoverableKeyException

getMappedCertificateForHostname

public X509Certificate getMappedCertificateForHostname(String hostname)
                                                throws CertificateParsingException,
                                                       InvalidKeyException,
                                                       CertificateExpiredException,
                                                       CertificateNotYetValidException,
                                                       SignatureException,
                                                       CertificateException,
                                                       NoSuchAlgorithmException,
                                                       NoSuchProviderException,
                                                       KeyStoreException,
                                                       UnrecoverableKeyException
This method returns the mapped certificate for a hostname, or generates a "standard" SSL server certificate issued by the CA to the supplied subject if no mapping has been created. This is not a true duplication, just a shortcut method that is adequate for web browsers.

Parameters:
hostname -
Returns:
Throws:
CertificateParsingException
InvalidKeyException
CertificateExpiredException
CertificateNotYetValidException
SignatureException
CertificateException
NoSuchAlgorithmException
NoSuchProviderException
KeyStoreException
UnrecoverableKeyException

getPrivateKeyForLocalCert

public PrivateKey getPrivateKeyForLocalCert(X509Certificate cert)
                                     throws CertificateEncodingException,
                                            KeyStoreException,
                                            UnrecoverableKeyException,
                                            NoSuchAlgorithmException
For a cert we have generated, return the private key.

Parameters:
cert -
Returns:
Throws:
CertificateEncodingException
KeyStoreException
UnrecoverableKeyException
NoSuchAlgorithmException

getRSAKeyPair

public KeyPair getRSAKeyPair()
Generate an RSA Key Pair

Returns:

getDSAKeyPair

public KeyPair getDSAKeyPair()
Generate a DSA Key Pair

Returns:

mapPublicKeys

public void mapPublicKeys(PublicKey original,
                          PublicKey substitute)
Stores a public key mapping.

Parameters:
original -
substitute -

getMappedPublicKey

public PublicKey getMappedPublicKey(PublicKey original)
If we get a KeyValue with a given public key, then later see an X509Data with the same public key, we shouldn't split this in our MITM impl. So when creating a new cert, we should check if we've already assigned a substitute key and re-use it, and vice-versa.

Parameters:
pk -
Returns:

getPrivateKey

public PrivateKey getPrivateKey(PublicKey pk)
Returns the private key for a public key we have generated.

Parameters:
pk -
Returns:

getKeyStore

public KeyStore getKeyStore()


Copyright © 2012. All Rights Reserved.