1 | package ve.gob.cenditel.tibisaymovil; |
---|
2 | |
---|
3 | import java.security.PrivateKey; |
---|
4 | import java.security.cert.X509Certificate; |
---|
5 | |
---|
6 | import android.annotation.SuppressLint; |
---|
7 | import android.content.Context; |
---|
8 | import android.security.KeyChain; |
---|
9 | import android.security.KeyChainAliasCallback; |
---|
10 | import android.security.KeyChainException; |
---|
11 | |
---|
12 | @SuppressLint("NewApi") |
---|
13 | public class AndroidKeyChain extends KeyChainStrategy implements KeyChainAliasCallback { |
---|
14 | |
---|
15 | private static final long serialVersionUID = -5618428393037452012L; |
---|
16 | private static Context context; |
---|
17 | |
---|
18 | private AndroidKeyChain() {} |
---|
19 | |
---|
20 | public static AndroidKeyChain getInstance() { |
---|
21 | |
---|
22 | if (KeyChainStrategy.theInstance == null) { |
---|
23 | |
---|
24 | KeyChainStrategy.theInstance = new AndroidKeyChain(); |
---|
25 | } |
---|
26 | return (AndroidKeyChain) KeyChainStrategy.theInstance; |
---|
27 | } |
---|
28 | |
---|
29 | @Override |
---|
30 | public void setClientActivity(KeyChainClientActivity activity) { |
---|
31 | |
---|
32 | super.setClientActivity(activity); |
---|
33 | AndroidKeyChain.context = activity.getApplicationContext(); |
---|
34 | } |
---|
35 | |
---|
36 | @Override |
---|
37 | public void choosePrivateKeyAlias() { |
---|
38 | |
---|
39 | KeyChain.choosePrivateKeyAlias(KeyChainStrategy.activity, this, null, null, null, -1, null); |
---|
40 | } |
---|
41 | |
---|
42 | @Override |
---|
43 | public void choosePrivateKeyList() { |
---|
44 | KeyChain.choosePrivateKeyAlias(KeyChainStrategy.activity, this, null, null, null, -1, null); |
---|
45 | |
---|
46 | } |
---|
47 | |
---|
48 | @Override |
---|
49 | public X509Certificate[] getCertificateChain(String alias) |
---|
50 | throws InterruptedException, KeystoreException { |
---|
51 | |
---|
52 | try { |
---|
53 | return KeyChain.getCertificateChain(AndroidKeyChain.context, alias); |
---|
54 | } catch (KeyChainException e) { |
---|
55 | throw new KeystoreException(e); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | @Override |
---|
60 | public PrivateKey getPrivateKey(String alias) |
---|
61 | throws InterruptedException, KeystoreException { |
---|
62 | |
---|
63 | try { |
---|
64 | return KeyChain.getPrivateKey(AndroidKeyChain.context, alias); |
---|
65 | } catch (KeyChainException e) { |
---|
66 | throw new KeystoreException(e); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | //TODO: |
---|
71 | @Override |
---|
72 | public int deleteCertificate(String alias) throws KeystoreException{ |
---|
73 | /* |
---|
74 | try { |
---|
75 | KeyStore trusted = getKeystore(); |
---|
76 | trusted.deleteEntry(alias); |
---|
77 | } catch (Exception e) { |
---|
78 | throw new KeystoreException(e); |
---|
79 | } |
---|
80 | */ |
---|
81 | return 0; |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | @Override |
---|
86 | public void alias(String alias) { |
---|
87 | |
---|
88 | KeyChainStrategy.activity.setAlias(alias); |
---|
89 | } |
---|
90 | } |
---|