package ve.gob.cenditel.tibisaymovil; import java.io.Serializable; import java.security.PrivateKey; import java.security.cert.X509Certificate; import android.os.Build; import android.os.Build.VERSION_CODES; /** * Accesses the KeyChain in a unified way both in Android versions post and pre 4.0. Android 4.0 and later has an * operating system level keychain. This keychain is shared across the operating system and all applications. In prior * versions a custom, application private keychain will be used. * * @author José M. Prieto (jmprieto@emergya.com) */ public abstract class KeyChainStrategy implements Serializable { private static final long serialVersionUID = -5156264665372692833L; protected static KeyChainStrategy theInstance = null; protected static KeyChainClientActivity activity; public static KeyChainStrategy getInstance() { if (theInstance == null) { if (Build.VERSION.SDK_INT < VERSION_CODES.ICE_CREAM_SANDWICH) { // < Android 4.0. Does not have KeyChain API return CustomKeyChain.getInstance(); } else { // >= Android 4.0. KeyChain available! return AndroidKeyChain.getInstance(); } } return theInstance; } /** * Set the KeyChainClientActivity that will be notified of the alias selected by the user. The notification is done * via the {@link KeyChainClientActivity#setAlias(String)} callback. * * @param activity the receiver of notifications */ public void setClientActivity(KeyChainClientActivity activity) { KeyChainStrategy.activity = activity; } /** * Launches an Activity for the user to select the alias for a private key and certificate pair for authentication. * The selected alias or null will be returned to the {@link KeyChainClientActivity} passed to the previous call to * {@link #setClientActivity(KeyChainClientActivity)} via the * {@link KeyChainClientActivity#setAlias(String)} callback. */ public abstract void choosePrivateKeyAlias(); /** * Same as before, but only to modify alias list. */ public abstract void choosePrivateKeyList(); /** * Get a certificate chain from the keystore. * This method requires the caller to hold the permission USE_CREDENTIALS on Android >= 4.0. * * @param alias The alias of the desired certificate chain * @return the X509Certificate chain for the requested alias, or null if no there is no result * @throws InterruptedException if the operation has been interrupted before completion * @throws KeystoreException when there is some error accessing the keystore */ public abstract X509Certificate[] getCertificateChain(String alias) throws InterruptedException, KeystoreException; /** * Get a private key from the keystore. * * @param alias the alias of the desired private key * @return the PrivateKey for the requested alias, or null if no there is no result * @throws InterruptedException if the operation has been interrupted before completion * @throws KeystoreException when there is some error accessing the keystore */ public abstract PrivateKey getPrivateKey(String alias) throws InterruptedException, KeystoreException; /** * Delete a certificate from the keystore. * * @param alias the alias of the certificate chain to delete * @throws InterruptedException if the operation has been interrupted before completion * @throws KeystoreException when there is some error accessing the keystore */ public abstract int deleteCertificate(String alias) throws InterruptedException, KeystoreException; }