1 | package ve.gob.cenditel.tibisaymovil; |
---|
2 | |
---|
3 | import java.io.Serializable; |
---|
4 | import java.security.PrivateKey; |
---|
5 | import java.security.cert.X509Certificate; |
---|
6 | |
---|
7 | import android.os.Build; |
---|
8 | import android.os.Build.VERSION_CODES; |
---|
9 | |
---|
10 | /** |
---|
11 | * Accesses the KeyChain in a unified way both in Android versions post and pre 4.0. Android 4.0 and later has an |
---|
12 | * operating system level keychain. This keychain is shared across the operating system and all applications. In prior |
---|
13 | * versions a custom, application private keychain will be used. |
---|
14 | * |
---|
15 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
16 | */ |
---|
17 | public abstract class KeyChainStrategy implements Serializable { |
---|
18 | |
---|
19 | private static final long serialVersionUID = -5156264665372692833L; |
---|
20 | protected static KeyChainStrategy theInstance = null; |
---|
21 | |
---|
22 | protected static KeyChainClientActivity activity; |
---|
23 | |
---|
24 | public static KeyChainStrategy getInstance() { |
---|
25 | |
---|
26 | if (theInstance == null) { |
---|
27 | if (Build.VERSION.SDK_INT < VERSION_CODES.ICE_CREAM_SANDWICH) { |
---|
28 | // < Android 4.0. Does not have KeyChain API |
---|
29 | return CustomKeyChain.getInstance(); |
---|
30 | } else { |
---|
31 | // >= Android 4.0. KeyChain available! |
---|
32 | return AndroidKeyChain.getInstance(); |
---|
33 | } |
---|
34 | } |
---|
35 | return theInstance; |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * Set the KeyChainClientActivity that will be notified of the alias selected by the user. The notification is done |
---|
40 | * via the {@link KeyChainClientActivity#setAlias(String)} callback. |
---|
41 | * |
---|
42 | * @param activity the receiver of notifications |
---|
43 | */ |
---|
44 | public void setClientActivity(KeyChainClientActivity activity) { |
---|
45 | KeyChainStrategy.activity = activity; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Launches an Activity for the user to select the alias for a private key and certificate pair for authentication. |
---|
50 | * The selected alias or null will be returned to the {@link KeyChainClientActivity} passed to the previous call to |
---|
51 | * {@link #setClientActivity(KeyChainClientActivity)} via the |
---|
52 | * {@link KeyChainClientActivity#setAlias(String)} callback. |
---|
53 | */ |
---|
54 | public abstract void choosePrivateKeyAlias(); |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | * Same as before, but only to modify alias list. |
---|
59 | */ |
---|
60 | public abstract void choosePrivateKeyList(); |
---|
61 | |
---|
62 | |
---|
63 | /** |
---|
64 | * Get a certificate chain from the keystore. |
---|
65 | * This method requires the caller to hold the permission USE_CREDENTIALS on Android >= 4.0. |
---|
66 | * |
---|
67 | * @param alias The alias of the desired certificate chain |
---|
68 | * @return the X509Certificate chain for the requested alias, or null if no there is no result |
---|
69 | * @throws InterruptedException if the operation has been interrupted before completion |
---|
70 | * @throws KeystoreException when there is some error accessing the keystore |
---|
71 | */ |
---|
72 | public abstract X509Certificate[] getCertificateChain(String alias) |
---|
73 | throws InterruptedException, KeystoreException; |
---|
74 | |
---|
75 | /** |
---|
76 | * Get a private key from the keystore. |
---|
77 | * |
---|
78 | * @param alias the alias of the desired private key |
---|
79 | * @return the PrivateKey for the requested alias, or null if no there is no result |
---|
80 | * @throws InterruptedException if the operation has been interrupted before completion |
---|
81 | * @throws KeystoreException when there is some error accessing the keystore |
---|
82 | */ |
---|
83 | public abstract PrivateKey getPrivateKey(String alias) |
---|
84 | throws InterruptedException, KeystoreException; |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | /** |
---|
90 | * Delete a certificate from the keystore. |
---|
91 | * |
---|
92 | * @param alias the alias of the certificate chain to delete |
---|
93 | * @throws InterruptedException if the operation has been interrupted before completion |
---|
94 | * @throws KeystoreException when there is some error accessing the keystore |
---|
95 | */ |
---|
96 | public abstract int deleteCertificate(String alias) |
---|
97 | throws InterruptedException, KeystoreException; |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | } |
---|