FileDocCategorySizeDatePackage
DebugKeyProvider.javaAPI DocAndroid 1.5 API7834Wed May 06 22:41:10 BST 2009com.android.jarutils

DebugKeyProvider

public class DebugKeyProvider extends Object
A provider of a dummy key to sign Android application for debugging purpose.

This provider uses a custom keystore to create and store a key with a known password.

Fields Summary
private static final String
PASSWORD_STRING
private static final char[]
PASSWORD_CHAR
private static final String
DEBUG_ALIAS
private static final String
CERTIFICATE_DESC
private KeyStore$PrivateKeyEntry
mEntry
Constructors Summary
public DebugKeyProvider(String osKeyStorePath, String storeType, IKeyGenOutput output)
Creates a provider using a keystore at the given location.

The keystore, and a new random android debug key are created if they do not yet exist.

Password for the store/key is android, and the key alias is AndroidDebugKey.

param
osKeyStorePath the OS path to the keystore, or null if the default one is to be used.
param
storeType an optional keystore type, or null if the default is to be used.
param
output an optional {@link IKeyGenOutput} object to get the stdout and stderr of the keytool process call.
throws
KeytoolException If the creation of the debug key failed.
throws
AndroidLocationException

        
        if (osKeyStorePath == null) {
            osKeyStorePath = getDefaultKeyStoreOsPath();
        }
        
        if (loadKeyEntry(osKeyStorePath, storeType) == false) {
            // create the store with the key
            createNewStore(osKeyStorePath, storeType, output);
        }
    
Methods Summary
private voidcreateNewStore(java.lang.String osKeyStorePath, java.lang.String storeType, com.android.jarutils.DebugKeyProvider$IKeyGenOutput output)
Creates a new store

param
osKeyStorePath the location of the store
param
storeType an optional keystore type, or null if the default is to be used.
param
output an optional {@link IKeyGenOutput} object to get the stdout and stderr of the keytool process call.
throws
KeyStoreException
throws
NoSuchAlgorithmException
throws
CertificateException
throws
UnrecoverableEntryException
throws
IOException
throws
KeytoolException

        
        if (KeystoreHelper.createNewStore(osKeyStorePath, storeType, PASSWORD_STRING, DEBUG_ALIAS,
                PASSWORD_STRING, CERTIFICATE_DESC, 1 /* validity*/, output)) {
            loadKeyEntry(osKeyStorePath, storeType);
        }
    
public java.security.cert.CertificategetCertificate()
Returns the debug {@link Certificate} to use to sign applications for debug purpose.

return
the certificate or null if its creation failed.

        if (mEntry != null) {
            return mEntry.getCertificate();
        }

        return null;
    
public java.security.PrivateKeygetDebugKey()
Returns the debug {@link PrivateKey} to use to sign applications for debug purpose.

return
the private key or null if its creation failed.

        if (mEntry != null) {
            return mEntry.getPrivateKey();
        }
        
        return null;
    
public static java.lang.StringgetDefaultKeyStoreOsPath()
Returns the OS path to the default debug keystore.

return
The OS path to the default debug keystore.
throws
KeytoolException
throws
AndroidLocationException

        String folder = AndroidLocation.getFolder();
        if (folder == null) {
            throw new KeytoolException("Failed to get HOME directory!\n");
        }
        String osKeyStorePath = folder + "debug.keystore";

        return osKeyStorePath;
    
private booleanloadKeyEntry(java.lang.String osKeyStorePath, java.lang.String storeType)
Loads the debug key from the keystore.

param
osKeyStorePath the OS path to the keystore.
param
storeType an optional keystore type, or null if the default is to be used.
return
true if success, false if the keystore does not exist.

        try {
            KeyStore keyStore = KeyStore.getInstance(
                    storeType != null ? storeType : KeyStore.getDefaultType());
            FileInputStream fis = new FileInputStream(osKeyStorePath);
            keyStore.load(fis, PASSWORD_CHAR);
            fis.close();
            mEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry(
                    DEBUG_ALIAS, new KeyStore.PasswordProtection(PASSWORD_CHAR));
        } catch (FileNotFoundException e) {
            return false;
        }
        
        return true;