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