android: Add KeyStoreSpi implementation that uses LocalCertificateStore
[strongswan.git] / src / frontends / android / src / org / strongswan / android / security / LocalCertificateKeyStoreSpi.java
1 /*
2 * Copyright (C) 2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.security;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.security.Key;
22 import java.security.KeyStoreException;
23 import java.security.KeyStoreSpi;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.UnrecoverableKeyException;
26 import java.security.cert.Certificate;
27 import java.security.cert.CertificateException;
28 import java.util.Collections;
29 import java.util.Date;
30 import java.util.Enumeration;
31
32 public class LocalCertificateKeyStoreSpi extends KeyStoreSpi
33 {
34 private final LocalCertificateStore mStore = new LocalCertificateStore();
35
36 @Override
37 public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException
38 {
39 return null;
40 }
41
42 @Override
43 public Certificate[] engineGetCertificateChain(String alias)
44 {
45 return null;
46 }
47
48 @Override
49 public Certificate engineGetCertificate(String alias)
50 {
51 return mStore.getCertificate(alias);
52 }
53
54 @Override
55 public Date engineGetCreationDate(String alias)
56 {
57 return mStore.getCreationDate(alias);
58 }
59
60 @Override
61 public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] chain) throws KeyStoreException
62 {
63 throw new UnsupportedOperationException();
64 }
65
66 @Override
67 public void engineSetKeyEntry(String alias, byte[] key, Certificate[] chain) throws KeyStoreException
68 {
69 throw new UnsupportedOperationException();
70 }
71
72 @Override
73 public void engineSetCertificateEntry(String alias, Certificate cert) throws KeyStoreException
74 {
75 /* we ignore the given alias as the store calculates it on its own,
76 * duplicates are replaced */
77 if (!mStore.addCertificate(cert))
78 {
79 throw new KeyStoreException();
80 }
81 }
82
83 @Override
84 public void engineDeleteEntry(String alias) throws KeyStoreException
85 {
86 mStore.deleteCertificate(alias);
87 }
88
89 @Override
90 public Enumeration<String> engineAliases()
91 {
92 return Collections.enumeration(mStore.aliases());
93 }
94
95 @Override
96 public boolean engineContainsAlias(String alias)
97 {
98 return mStore.containsAlias(alias);
99 }
100
101 @Override
102 public int engineSize()
103 {
104 return mStore.aliases().size();
105 }
106
107 @Override
108 public boolean engineIsKeyEntry(String alias)
109 {
110 return false;
111 }
112
113 @Override
114 public boolean engineIsCertificateEntry(String alias)
115 {
116 return engineContainsAlias(alias);
117 }
118
119 @Override
120 public String engineGetCertificateAlias(Certificate cert)
121 {
122 return mStore.getCertificateAlias(cert);
123 }
124
125 @Override
126 public void engineStore(OutputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException
127 {
128 throw new UnsupportedOperationException();
129 }
130
131 @Override
132 public void engineLoad(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException
133 {
134 if (stream != null)
135 {
136 throw new UnsupportedOperationException();
137 }
138 }
139 }