1package eu.siacs.conversations.crypto;
2
3import java.math.BigInteger;
4import java.security.KeyFactory;
5import java.security.KeyPair;
6import java.security.KeyPairGenerator;
7import java.security.NoSuchAlgorithmException;
8import java.security.PrivateKey;
9import java.security.PublicKey;
10import java.security.spec.DSAPrivateKeySpec;
11import java.security.spec.DSAPublicKeySpec;
12import java.security.spec.InvalidKeySpecException;
13
14import org.json.JSONException;
15import org.json.JSONObject;
16
17import android.util.Log;
18
19import eu.siacs.conversations.Config;
20import eu.siacs.conversations.entities.Account;
21import eu.siacs.conversations.entities.Conversation;
22import eu.siacs.conversations.services.XmppConnectionService;
23import eu.siacs.conversations.utils.CryptoHelper;
24import eu.siacs.conversations.xmpp.jid.InvalidJidException;
25import eu.siacs.conversations.xmpp.jid.Jid;
26import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
27
28import net.java.otr4j.OtrEngineHost;
29import net.java.otr4j.OtrException;
30import net.java.otr4j.OtrPolicy;
31import net.java.otr4j.OtrPolicyImpl;
32import net.java.otr4j.crypto.OtrCryptoEngineImpl;
33import net.java.otr4j.crypto.OtrCryptoException;
34import net.java.otr4j.session.InstanceTag;
35import net.java.otr4j.session.SessionID;
36
37public class OtrEngine extends OtrCryptoEngineImpl implements OtrEngineHost {
38
39 private Account account;
40 private OtrPolicy otrPolicy;
41 private KeyPair keyPair;
42 private XmppConnectionService mXmppConnectionService;
43
44 public OtrEngine(XmppConnectionService service, Account account) {
45 this.account = account;
46 this.otrPolicy = new OtrPolicyImpl();
47 this.otrPolicy.setAllowV1(false);
48 this.otrPolicy.setAllowV2(true);
49 this.otrPolicy.setAllowV3(true);
50 this.keyPair = loadKey(account.getKeys());
51 this.mXmppConnectionService = service;
52 }
53
54 private KeyPair loadKey(JSONObject keys) {
55 if (keys == null) {
56 return null;
57 }
58 try {
59 BigInteger x = new BigInteger(keys.getString("otr_x"), 16);
60 BigInteger y = new BigInteger(keys.getString("otr_y"), 16);
61 BigInteger p = new BigInteger(keys.getString("otr_p"), 16);
62 BigInteger q = new BigInteger(keys.getString("otr_q"), 16);
63 BigInteger g = new BigInteger(keys.getString("otr_g"), 16);
64 KeyFactory keyFactory = KeyFactory.getInstance("DSA");
65 DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(y, p, q, g);
66 DSAPrivateKeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
67 PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
68 PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
69 return new KeyPair(publicKey, privateKey);
70 } catch (JSONException e) {
71 return null;
72 } catch (NoSuchAlgorithmException e) {
73 return null;
74 } catch (InvalidKeySpecException e) {
75 return null;
76 }
77 }
78
79 private void saveKey() {
80 PublicKey publicKey = keyPair.getPublic();
81 PrivateKey privateKey = keyPair.getPrivate();
82 KeyFactory keyFactory;
83 try {
84 keyFactory = KeyFactory.getInstance("DSA");
85 DSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec(
86 privateKey, DSAPrivateKeySpec.class);
87 DSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(publicKey,
88 DSAPublicKeySpec.class);
89 this.account.setKey("otr_x", privateKeySpec.getX().toString(16));
90 this.account.setKey("otr_g", privateKeySpec.getG().toString(16));
91 this.account.setKey("otr_p", privateKeySpec.getP().toString(16));
92 this.account.setKey("otr_q", privateKeySpec.getQ().toString(16));
93 this.account.setKey("otr_y", publicKeySpec.getY().toString(16));
94 } catch (final NoSuchAlgorithmException | InvalidKeySpecException e) {
95 e.printStackTrace();
96 }
97
98 }
99
100 @Override
101 public void askForSecret(SessionID id, InstanceTag instanceTag, String question) {
102 try {
103 final Jid jid = Jid.fromSessionID(id);
104 Conversation conversation = this.mXmppConnectionService.find(this.account,jid);
105 if (conversation!=null) {
106 conversation.smp().hint = question;
107 conversation.smp().status = Conversation.Smp.STATUS_CONTACT_REQUESTED;
108 mXmppConnectionService.updateConversationUi();
109 }
110 } catch (InvalidJidException e) {
111 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": smp in invalid session "+id.toString());
112 }
113 }
114
115 @Override
116 public void finishedSessionMessage(SessionID arg0, String arg1)
117 throws OtrException {
118
119 }
120
121 @Override
122 public String getFallbackMessage(SessionID arg0) {
123 return "I would like to start a private (OTR encrypted) conversation but your client doesn’t seem to support that";
124 }
125
126 @Override
127 public byte[] getLocalFingerprintRaw(SessionID arg0) {
128 try {
129 return getFingerprintRaw(getPublicKey());
130 } catch (OtrCryptoException e) {
131 return null;
132 }
133 }
134
135 public PublicKey getPublicKey() {
136 if (this.keyPair == null) {
137 return null;
138 }
139 return this.keyPair.getPublic();
140 }
141
142 @Override
143 public KeyPair getLocalKeyPair(SessionID arg0) throws OtrException {
144 if (this.keyPair == null) {
145 KeyPairGenerator kg;
146 try {
147 kg = KeyPairGenerator.getInstance("DSA");
148 this.keyPair = kg.genKeyPair();
149 this.saveKey();
150 mXmppConnectionService.databaseBackend.updateAccount(account);
151 } catch (NoSuchAlgorithmException e) {
152 Log.d(Config.LOGTAG,
153 "error generating key pair " + e.getMessage());
154 }
155 }
156 return this.keyPair;
157 }
158
159 @Override
160 public String getReplyForUnreadableMessage(SessionID arg0) {
161 // TODO Auto-generated method stub
162 return null;
163 }
164
165 @Override
166 public OtrPolicy getSessionPolicy(SessionID arg0) {
167 return otrPolicy;
168 }
169
170 @Override
171 public void injectMessage(SessionID session, String body)
172 throws OtrException {
173 MessagePacket packet = new MessagePacket();
174 packet.setFrom(account.getJid());
175 if (session.getUserID().isEmpty()) {
176 packet.setAttribute("to", session.getAccountID());
177 } else {
178 packet.setAttribute("to", session.getAccountID() + "/" + session.getUserID());
179 }
180 packet.setBody(body);
181 packet.addChild("private", "urn:xmpp:carbons:2");
182 packet.addChild("no-copy", "urn:xmpp:hints");
183 packet.addChild("no-store", "urn:xmpp:hints");
184 packet.setType(MessagePacket.TYPE_CHAT);
185 account.getXmppConnection().sendMessagePacket(packet);
186 }
187
188 @Override
189 public void messageFromAnotherInstanceReceived(SessionID id) {
190 Log.d(Config.LOGTAG,
191 "unreadable message received from " + id.getAccountID());
192 }
193
194 @Override
195 public void multipleInstancesDetected(SessionID arg0) {
196 // TODO Auto-generated method stub
197
198 }
199
200 @Override
201 public void requireEncryptedMessage(SessionID arg0, String arg1)
202 throws OtrException {
203 // TODO Auto-generated method stub
204
205 }
206
207 @Override
208 public void showError(SessionID arg0, String arg1) throws OtrException {
209 Log.d(Config.LOGTAG,"show error");
210 }
211
212 @Override
213 public void smpAborted(SessionID id) throws OtrException {
214 setSmpStatus(id, Conversation.Smp.STATUS_NONE);
215 }
216
217 private void setSmpStatus(SessionID id, int status) {
218 try {
219 final Jid jid = Jid.fromSessionID(id);
220 Conversation conversation = this.mXmppConnectionService.find(this.account,jid);
221 if (conversation!=null) {
222 conversation.smp().status = status;
223 mXmppConnectionService.updateConversationUi();
224 }
225 } catch (final InvalidJidException ignored) {
226
227 }
228 }
229
230 @Override
231 public void smpError(SessionID id, int arg1, boolean arg2)
232 throws OtrException {
233 setSmpStatus(id, Conversation.Smp.STATUS_NONE);
234 }
235
236 @Override
237 public void unencryptedMessageReceived(SessionID arg0, String arg1)
238 throws OtrException {
239 throw new OtrException(new Exception("unencrypted message received"));
240 }
241
242 @Override
243 public void unreadableMessageReceived(SessionID arg0) throws OtrException {
244 Log.d(Config.LOGTAG,"unreadable message received");
245 throw new OtrException(new Exception("unreadable message received"));
246 }
247
248 @Override
249 public void unverify(SessionID id, String arg1) {
250 setSmpStatus(id, Conversation.Smp.STATUS_FAILED);
251 }
252
253 @Override
254 public void verify(SessionID id, String fingerprint, boolean approved) {
255 Log.d(Config.LOGTAG,"OtrEngine.verify("+id.toString()+","+fingerprint+","+String.valueOf(approved)+")");
256 try {
257 final Jid jid = Jid.fromSessionID(id);
258 Conversation conversation = this.mXmppConnectionService.find(this.account,jid);
259 if (conversation!=null) {
260 if (approved) {
261 conversation.getContact().addOtrFingerprint(fingerprint);
262 }
263 conversation.smp().hint = null;
264 conversation.smp().status = Conversation.Smp.STATUS_VERIFIED;
265 mXmppConnectionService.updateConversationUi();
266 mXmppConnectionService.syncRosterToDisk(conversation.getAccount());
267 }
268 } catch (final InvalidJidException ignored) {
269 }
270 }
271
272}