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.content.Context;
18import android.util.Log;
19
20import eu.siacs.conversations.entities.Account;
21import eu.siacs.conversations.persistance.DatabaseBackend;
22import eu.siacs.conversations.xml.Element;
23import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
24
25import net.java.otr4j.OtrEngineHost;
26import net.java.otr4j.OtrException;
27import net.java.otr4j.OtrPolicy;
28import net.java.otr4j.OtrPolicyImpl;
29import net.java.otr4j.session.InstanceTag;
30import net.java.otr4j.session.SessionID;
31
32public class OtrEngine implements OtrEngineHost {
33
34 private static final String LOGTAG = "xmppService";
35
36 private Account account;
37 private OtrPolicy otrPolicy;
38 private KeyPair keyPair;
39 private Context context;
40
41 public OtrEngine(Context context, Account account) {
42 this.account = account;
43 this.otrPolicy = new OtrPolicyImpl();
44 this.otrPolicy.setAllowV1(false);
45 this.otrPolicy.setAllowV2(true);
46 this.otrPolicy.setAllowV3(true);
47 this.keyPair = loadKey(account.getKeys());
48 }
49
50 private KeyPair loadKey(JSONObject keys) {
51 if (keys == null) {
52 return null;
53 }
54 try {
55 BigInteger x = new BigInteger(keys.getString("otr_x"),16);
56 BigInteger y = new BigInteger(keys.getString("otr_y"),16);
57 BigInteger p = new BigInteger(keys.getString("otr_p"),16);
58 BigInteger q = new BigInteger(keys.getString("otr_q"),16);
59 BigInteger g = new BigInteger(keys.getString("otr_g"),16);
60 KeyFactory keyFactory = KeyFactory.getInstance("DSA");
61 DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(y, p, q, g);
62 DSAPrivateKeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
63 PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
64 PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
65 return new KeyPair(publicKey, privateKey);
66 } catch (JSONException e) {
67 return null;
68 } catch (NoSuchAlgorithmException e) {
69 return null;
70 } catch (InvalidKeySpecException e) {
71 return null;
72 }
73 }
74
75 private void saveKey() {
76 PublicKey publicKey = keyPair.getPublic();
77 PrivateKey privateKey = keyPair.getPrivate();
78 KeyFactory keyFactory;
79 try {
80 keyFactory = KeyFactory.getInstance("DSA");
81 DSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec(privateKey, DSAPrivateKeySpec.class);
82 DSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(publicKey, DSAPublicKeySpec.class);
83 this.account.setKey("otr_x",privateKeySpec.getX().toString(16));
84 this.account.setKey("otr_g",privateKeySpec.getG().toString(16));
85 this.account.setKey("otr_p",privateKeySpec.getP().toString(16));
86 this.account.setKey("otr_q",privateKeySpec.getQ().toString(16));
87 this.account.setKey("otr_y",publicKeySpec.getY().toString(16));
88 } catch (NoSuchAlgorithmException e) {
89 e.printStackTrace();
90 } catch (InvalidKeySpecException e) {
91 e.printStackTrace();
92 }
93
94 }
95
96 @Override
97 public void askForSecret(SessionID arg0, InstanceTag arg1, String arg2) {
98 // TODO Auto-generated method stub
99
100 }
101
102 @Override
103 public void finishedSessionMessage(SessionID arg0, String arg1)
104 throws OtrException {
105 // TODO Auto-generated method stub
106
107 }
108
109 @Override
110 public String getFallbackMessage(SessionID arg0) {
111 // TODO Auto-generated method stub
112 return null;
113 }
114
115 @Override
116 public byte[] getLocalFingerprintRaw(SessionID arg0) {
117 // TODO Auto-generated method stub
118 return null;
119 }
120
121 public PublicKey getPublicKey() {
122 if (this.keyPair == null) {
123 return null;
124 }
125 return this.keyPair.getPublic();
126 }
127
128 @Override
129 public KeyPair getLocalKeyPair(SessionID arg0) throws OtrException {
130 if (this.keyPair==null) {
131 KeyPairGenerator kg;
132 try {
133 kg = KeyPairGenerator.getInstance("DSA");
134 this.keyPair = kg.genKeyPair();
135 this.saveKey();
136 DatabaseBackend.getInstance(context).updateAccount(account);
137 } catch (NoSuchAlgorithmException e) {
138 Log.d(LOGTAG,"error generating key pair "+e.getMessage());
139 }
140 }
141 return this.keyPair;
142 }
143
144 @Override
145 public String getReplyForUnreadableMessage(SessionID arg0) {
146 // TODO Auto-generated method stub
147 return null;
148 }
149
150 @Override
151 public OtrPolicy getSessionPolicy(SessionID arg0) {
152 return otrPolicy;
153 }
154
155 @Override
156 public void injectMessage(SessionID session, String body) throws OtrException {
157 MessagePacket packet = new MessagePacket();
158 packet.setFrom(account.getFullJid()); //sender
159 packet.setTo(session.getAccountID()+"/"+session.getUserID()); //reciepient
160 packet.setBody(body);
161 Element privateTag = new Element("private");
162 privateTag.setAttribute("xmlns","urn:xmpp:carbons:2");
163 packet.addChild(privateTag);
164 packet.setType(MessagePacket.TYPE_CHAT);
165 account.getXmppConnection().sendMessagePacket(packet);
166 }
167
168 @Override
169 public void messageFromAnotherInstanceReceived(SessionID arg0) {
170 // TODO Auto-generated method stub
171
172 }
173
174 @Override
175 public void multipleInstancesDetected(SessionID arg0) {
176 // TODO Auto-generated method stub
177
178 }
179
180 @Override
181 public void requireEncryptedMessage(SessionID arg0, String arg1)
182 throws OtrException {
183 // TODO Auto-generated method stub
184
185 }
186
187 @Override
188 public void showError(SessionID arg0, String arg1) throws OtrException {
189 // TODO Auto-generated method stub
190
191 }
192
193 @Override
194 public void smpAborted(SessionID arg0) throws OtrException {
195 // TODO Auto-generated method stub
196
197 }
198
199 @Override
200 public void smpError(SessionID arg0, int arg1, boolean arg2)
201 throws OtrException {
202 // TODO Auto-generated method stub
203
204 }
205
206 @Override
207 public void unencryptedMessageReceived(SessionID arg0, String arg1)
208 throws OtrException {
209 // TODO Auto-generated method stub
210
211 }
212
213 @Override
214 public void unreadableMessageReceived(SessionID arg0) throws OtrException {
215 throw new OtrException(new Exception("unreadable message received"));
216 }
217
218 @Override
219 public void unverify(SessionID arg0, String arg1) {
220 // TODO Auto-generated method stub
221
222 }
223
224 @Override
225 public void verify(SessionID arg0, String arg1, boolean arg2) {
226 // TODO Auto-generated method stub
227
228 }
229
230}