OtrEngine.java

  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.Config;
 21import eu.siacs.conversations.entities.Account;
 22import eu.siacs.conversations.persistance.DatabaseBackend;
 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 Account account;
 35	private OtrPolicy otrPolicy;
 36	private KeyPair keyPair;
 37	private Context context;
 38
 39	public OtrEngine(Context context, Account account) {
 40		this.account = account;
 41		this.otrPolicy = new OtrPolicyImpl();
 42		this.otrPolicy.setAllowV1(false);
 43		this.otrPolicy.setAllowV2(true);
 44		this.otrPolicy.setAllowV3(true);
 45		this.keyPair = loadKey(account.getKeys());
 46	}
 47
 48	private KeyPair loadKey(JSONObject keys) {
 49		if (keys == null) {
 50			return null;
 51		}
 52		try {
 53			BigInteger x = new BigInteger(keys.getString("otr_x"), 16);
 54			BigInteger y = new BigInteger(keys.getString("otr_y"), 16);
 55			BigInteger p = new BigInteger(keys.getString("otr_p"), 16);
 56			BigInteger q = new BigInteger(keys.getString("otr_q"), 16);
 57			BigInteger g = new BigInteger(keys.getString("otr_g"), 16);
 58			KeyFactory keyFactory = KeyFactory.getInstance("DSA");
 59			DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(y, p, q, g);
 60			DSAPrivateKeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
 61			PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
 62			PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
 63			return new KeyPair(publicKey, privateKey);
 64		} catch (JSONException e) {
 65			return null;
 66		} catch (NoSuchAlgorithmException e) {
 67			return null;
 68		} catch (InvalidKeySpecException e) {
 69			return null;
 70		}
 71	}
 72
 73	private void saveKey() {
 74		PublicKey publicKey = keyPair.getPublic();
 75		PrivateKey privateKey = keyPair.getPrivate();
 76		KeyFactory keyFactory;
 77		try {
 78			keyFactory = KeyFactory.getInstance("DSA");
 79			DSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec(
 80					privateKey, DSAPrivateKeySpec.class);
 81			DSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(publicKey,
 82					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(Config.LOGTAG,
139						"error generating key pair " + e.getMessage());
140			}
141		}
142		return this.keyPair;
143	}
144
145	@Override
146	public String getReplyForUnreadableMessage(SessionID arg0) {
147		// TODO Auto-generated method stub
148		return null;
149	}
150
151	@Override
152	public OtrPolicy getSessionPolicy(SessionID arg0) {
153		return otrPolicy;
154	}
155
156	@Override
157	public void injectMessage(SessionID session, String body)
158			throws OtrException {
159		MessagePacket packet = new MessagePacket();
160		packet.setFrom(account.getFullJid());
161		if (session.getUserID().isEmpty()) {
162			packet.setTo(session.getAccountID());
163		} else {
164			packet.setTo(session.getAccountID() + "/" + session.getUserID());
165		}
166		packet.setBody(body);
167		packet.addChild("private", "urn:xmpp:carbons:2");
168		packet.addChild("no-copy", "urn:xmpp:hints");
169		packet.setType(MessagePacket.TYPE_CHAT);
170		account.getXmppConnection().sendMessagePacket(packet);
171	}
172
173	@Override
174	public void messageFromAnotherInstanceReceived(SessionID arg0) {
175		// TODO Auto-generated method stub
176
177	}
178
179	@Override
180	public void multipleInstancesDetected(SessionID arg0) {
181		// TODO Auto-generated method stub
182
183	}
184
185	@Override
186	public void requireEncryptedMessage(SessionID arg0, String arg1)
187			throws OtrException {
188		// TODO Auto-generated method stub
189
190	}
191
192	@Override
193	public void showError(SessionID arg0, String arg1) throws OtrException {
194		// TODO Auto-generated method stub
195
196	}
197
198	@Override
199	public void smpAborted(SessionID arg0) throws OtrException {
200		// TODO Auto-generated method stub
201
202	}
203
204	@Override
205	public void smpError(SessionID arg0, int arg1, boolean arg2)
206			throws OtrException {
207		throw new OtrException(new Exception("smp error"));
208	}
209
210	@Override
211	public void unencryptedMessageReceived(SessionID arg0, String arg1)
212			throws OtrException {
213		throw new OtrException(new Exception("unencrypted message received"));
214	}
215
216	@Override
217	public void unreadableMessageReceived(SessionID arg0) throws OtrException {
218		throw new OtrException(new Exception("unreadable message received"));
219	}
220
221	@Override
222	public void unverify(SessionID arg0, String arg1) {
223		// TODO Auto-generated method stub
224
225	}
226
227	@Override
228	public void verify(SessionID arg0, String arg1, boolean arg2) {
229		// TODO Auto-generated method stub
230
231	}
232
233}