1 package net.java.otr4j; |
|
2 |
|
3 import java.io.BufferedInputStream; |
|
4 import java.io.File; |
|
5 import java.io.FileInputStream; |
|
6 import java.io.FileNotFoundException; |
|
7 import java.io.FileOutputStream; |
|
8 import java.io.IOException; |
|
9 import java.io.InputStream; |
|
10 import java.io.OutputStream; |
|
11 import java.security.KeyFactory; |
|
12 import java.security.KeyPair; |
|
13 import java.security.KeyPairGenerator; |
|
14 import java.security.NoSuchAlgorithmException; |
|
15 import java.security.PrivateKey; |
|
16 import java.security.PublicKey; |
|
17 import java.security.spec.InvalidKeySpecException; |
|
18 import java.security.spec.PKCS8EncodedKeySpec; |
|
19 import java.security.spec.X509EncodedKeySpec; |
|
20 import java.util.List; |
|
21 import java.util.Properties; |
|
22 import java.util.Vector; |
|
23 |
|
24 import org.bouncycastle2.util.encoders.Base64; |
|
25 |
|
26 import net.java.otr4j.crypto.OtrCryptoEngineImpl; |
|
27 import net.java.otr4j.crypto.OtrCryptoException; |
|
28 import net.java.otr4j.session.SessionID; |
|
29 |
|
30 public class OtrKeyManagerImpl implements OtrKeyManager { |
|
31 |
|
32 private OtrKeyManagerStore store; |
|
33 |
|
34 public OtrKeyManagerImpl(OtrKeyManagerStore store) { |
|
35 this.store = store; |
|
36 } |
|
37 |
|
38 class DefaultPropertiesStore implements OtrKeyManagerStore { |
|
39 private final Properties properties = new Properties(); |
|
40 private String filepath; |
|
41 |
|
42 public DefaultPropertiesStore(String filepath) throws IOException { |
|
43 if (filepath == null || filepath.length() < 1) |
|
44 throw new IllegalArgumentException(); |
|
45 this.filepath = filepath; |
|
46 properties.clear(); |
|
47 |
|
48 InputStream in = new BufferedInputStream(new FileInputStream( |
|
49 getConfigurationFile())); |
|
50 try { |
|
51 properties.load(in); |
|
52 } finally { |
|
53 in.close(); |
|
54 } |
|
55 } |
|
56 |
|
57 private File getConfigurationFile() throws IOException { |
|
58 File configFile = new File(filepath); |
|
59 if (!configFile.exists()) |
|
60 configFile.createNewFile(); |
|
61 return configFile; |
|
62 } |
|
63 |
|
64 public void setProperty(String id, boolean value) { |
|
65 properties.setProperty(id, "true"); |
|
66 try { |
|
67 this.store(); |
|
68 } catch (Exception e) { |
|
69 e.printStackTrace(); |
|
70 } |
|
71 } |
|
72 |
|
73 private void store() throws FileNotFoundException, IOException { |
|
74 OutputStream out = new FileOutputStream(getConfigurationFile()); |
|
75 properties.store(out, null); |
|
76 out.close(); |
|
77 } |
|
78 |
|
79 public void setProperty(String id, byte[] value) { |
|
80 properties.setProperty(id, new String(Base64.encode(value))); |
|
81 try { |
|
82 this.store(); |
|
83 } catch (Exception e) { |
|
84 e.printStackTrace(); |
|
85 } |
|
86 } |
|
87 |
|
88 public void removeProperty(String id) { |
|
89 properties.remove(id); |
|
90 |
|
91 } |
|
92 |
|
93 public byte[] getPropertyBytes(String id) { |
|
94 String value = properties.getProperty(id); |
|
95 if (value == null) |
|
96 return null; |
|
97 return Base64.decode(value); |
|
98 } |
|
99 |
|
100 public boolean getPropertyBoolean(String id, boolean defaultValue) { |
|
101 try { |
|
102 return Boolean.valueOf(properties.get(id).toString()); |
|
103 } catch (Exception e) { |
|
104 return defaultValue; |
|
105 } |
|
106 } |
|
107 } |
|
108 |
|
109 public OtrKeyManagerImpl(String filepath) throws IOException { |
|
110 this.store = new DefaultPropertiesStore(filepath); |
|
111 } |
|
112 |
|
113 private List<OtrKeyManagerListener> listeners = new Vector<OtrKeyManagerListener>(); |
|
114 |
|
115 public void addListener(OtrKeyManagerListener l) { |
|
116 synchronized (listeners) { |
|
117 if (!listeners.contains(l)) |
|
118 listeners.add(l); |
|
119 } |
|
120 } |
|
121 |
|
122 public void removeListener(OtrKeyManagerListener l) { |
|
123 synchronized (listeners) { |
|
124 listeners.remove(l); |
|
125 } |
|
126 } |
|
127 |
|
128 public void generateLocalKeyPair(SessionID sessionID) { |
|
129 if (sessionID == null) |
|
130 return; |
|
131 |
|
132 String accountID = sessionID.getAccountID(); |
|
133 KeyPair keyPair; |
|
134 try { |
|
135 keyPair = KeyPairGenerator.getInstance("DSA").genKeyPair(); |
|
136 } catch (NoSuchAlgorithmException e) { |
|
137 e.printStackTrace(); |
|
138 return; |
|
139 } |
|
140 |
|
141 // Store Public Key. |
|
142 PublicKey pubKey = keyPair.getPublic(); |
|
143 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey |
|
144 .getEncoded()); |
|
145 |
|
146 this.store.setProperty(accountID + ".publicKey", x509EncodedKeySpec |
|
147 .getEncoded()); |
|
148 |
|
149 // Store Private Key. |
|
150 PrivateKey privKey = keyPair.getPrivate(); |
|
151 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec( |
|
152 privKey.getEncoded()); |
|
153 |
|
154 this.store.setProperty(accountID + ".privateKey", pkcs8EncodedKeySpec |
|
155 .getEncoded()); |
|
156 } |
|
157 |
|
158 public String getLocalFingerprint(SessionID sessionID) { |
|
159 KeyPair keyPair = loadLocalKeyPair(sessionID); |
|
160 |
|
161 if (keyPair == null) |
|
162 return null; |
|
163 |
|
164 PublicKey pubKey = keyPair.getPublic(); |
|
165 |
|
166 try { |
|
167 return new OtrCryptoEngineImpl().getFingerprint(pubKey); |
|
168 } catch (OtrCryptoException e) { |
|
169 e.printStackTrace(); |
|
170 return null; |
|
171 } |
|
172 } |
|
173 |
|
174 public String getRemoteFingerprint(SessionID sessionID) { |
|
175 PublicKey remotePublicKey = loadRemotePublicKey(sessionID); |
|
176 if (remotePublicKey == null) |
|
177 return null; |
|
178 try { |
|
179 return new OtrCryptoEngineImpl().getFingerprint(remotePublicKey); |
|
180 } catch (OtrCryptoException e) { |
|
181 e.printStackTrace(); |
|
182 return null; |
|
183 } |
|
184 } |
|
185 |
|
186 public boolean isVerified(SessionID sessionID) { |
|
187 if (sessionID == null) |
|
188 return false; |
|
189 |
|
190 return this.store.getPropertyBoolean(sessionID.getUserID() |
|
191 + ".publicKey.verified", false); |
|
192 } |
|
193 |
|
194 public KeyPair loadLocalKeyPair(SessionID sessionID) { |
|
195 if (sessionID == null) |
|
196 return null; |
|
197 |
|
198 String accountID = sessionID.getAccountID(); |
|
199 // Load Private Key. |
|
200 byte[] b64PrivKey = this.store.getPropertyBytes(accountID |
|
201 + ".privateKey"); |
|
202 if (b64PrivKey == null) |
|
203 return null; |
|
204 |
|
205 PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(b64PrivKey); |
|
206 |
|
207 // Load Public Key. |
|
208 byte[] b64PubKey = this.store |
|
209 .getPropertyBytes(accountID + ".publicKey"); |
|
210 if (b64PubKey == null) |
|
211 return null; |
|
212 |
|
213 X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(b64PubKey); |
|
214 |
|
215 PublicKey publicKey; |
|
216 PrivateKey privateKey; |
|
217 |
|
218 // Generate KeyPair. |
|
219 KeyFactory keyFactory; |
|
220 try { |
|
221 keyFactory = KeyFactory.getInstance("DSA"); |
|
222 publicKey = keyFactory.generatePublic(publicKeySpec); |
|
223 privateKey = keyFactory.generatePrivate(privateKeySpec); |
|
224 } catch (NoSuchAlgorithmException e) { |
|
225 e.printStackTrace(); |
|
226 return null; |
|
227 } catch (InvalidKeySpecException e) { |
|
228 e.printStackTrace(); |
|
229 return null; |
|
230 } |
|
231 |
|
232 return new KeyPair(publicKey, privateKey); |
|
233 } |
|
234 |
|
235 public PublicKey loadRemotePublicKey(SessionID sessionID) { |
|
236 if (sessionID == null) |
|
237 return null; |
|
238 |
|
239 String userID = sessionID.getUserID(); |
|
240 |
|
241 byte[] b64PubKey = this.store.getPropertyBytes(userID + ".publicKey"); |
|
242 if (b64PubKey == null) |
|
243 return null; |
|
244 |
|
245 X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(b64PubKey); |
|
246 |
|
247 // Generate KeyPair. |
|
248 KeyFactory keyFactory; |
|
249 try { |
|
250 keyFactory = KeyFactory.getInstance("DSA"); |
|
251 return keyFactory.generatePublic(publicKeySpec); |
|
252 } catch (NoSuchAlgorithmException e) { |
|
253 e.printStackTrace(); |
|
254 return null; |
|
255 } catch (InvalidKeySpecException e) { |
|
256 e.printStackTrace(); |
|
257 return null; |
|
258 } |
|
259 } |
|
260 |
|
261 public void savePublicKey(SessionID sessionID, PublicKey pubKey) { |
|
262 if (sessionID == null) |
|
263 return; |
|
264 |
|
265 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey |
|
266 .getEncoded()); |
|
267 |
|
268 String userID = sessionID.getUserID(); |
|
269 this.store.setProperty(userID + ".publicKey", x509EncodedKeySpec |
|
270 .getEncoded()); |
|
271 |
|
272 this.store.removeProperty(userID + ".publicKey.verified"); |
|
273 } |
|
274 |
|
275 public void unverify(SessionID sessionID) { |
|
276 if (sessionID == null) |
|
277 return; |
|
278 |
|
279 if (!isVerified(sessionID)) |
|
280 return; |
|
281 |
|
282 this.store |
|
283 .removeProperty(sessionID.getUserID() + ".publicKey.verified"); |
|
284 |
|
285 for (OtrKeyManagerListener l : listeners) |
|
286 l.verificationStatusChanged(sessionID); |
|
287 |
|
288 } |
|
289 |
|
290 public void verify(SessionID sessionID) { |
|
291 if (sessionID == null) |
|
292 return; |
|
293 |
|
294 if (this.isVerified(sessionID)) |
|
295 return; |
|
296 |
|
297 this.store.setProperty(sessionID.getUserID() + ".publicKey.verified", |
|
298 true); |
|
299 |
|
300 for (OtrKeyManagerListener l : listeners) |
|
301 l.verificationStatusChanged(sessionID); |
|
302 } |
|
303 |
|
304 } |
|