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.entities.Conversation;
 23import eu.siacs.conversations.entities.Message;
 24import eu.siacs.conversations.persistance.DatabaseBackend;
 25import eu.siacs.conversations.services.XmppConnectionService;
 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.session.InstanceTag;
 33import net.java.otr4j.session.SessionID;
 34import net.java.otr4j.session.SessionImpl;
 35import net.java.otr4j.session.SessionStatus;
 36
 37public class OtrEngine 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 (NoSuchAlgorithmException e) {
 95			e.printStackTrace();
 96		} catch (InvalidKeySpecException e) {
 97			e.printStackTrace();
 98		}
 99
100	}
101
102	@Override
103	public void askForSecret(SessionID arg0, InstanceTag arg1, String arg2) {
104		// TODO Auto-generated method stub
105
106	}
107
108	@Override
109	public void finishedSessionMessage(SessionID arg0, String arg1)
110			throws OtrException {
111
112	}
113
114	@Override
115	public String getFallbackMessage(SessionID arg0) {
116		return "I would like to start a private (OTR encrypted) conversation but your client doesn’t seem to support that";
117	}
118
119	@Override
120	public byte[] getLocalFingerprintRaw(SessionID arg0) {
121		// TODO Auto-generated method stub
122		return null;
123	}
124
125	public PublicKey getPublicKey() {
126		if (this.keyPair == null) {
127			return null;
128		}
129		return this.keyPair.getPublic();
130	}
131
132	@Override
133	public KeyPair getLocalKeyPair(SessionID arg0) throws OtrException {
134		if (this.keyPair == null) {
135			KeyPairGenerator kg;
136			try {
137				kg = KeyPairGenerator.getInstance("DSA");
138				this.keyPair = kg.genKeyPair();
139				this.saveKey();
140				mXmppConnectionService.databaseBackend.updateAccount(account);
141			} catch (NoSuchAlgorithmException e) {
142				Log.d(Config.LOGTAG,
143						"error generating key pair " + e.getMessage());
144			}
145		}
146		return this.keyPair;
147	}
148
149	@Override
150	public String getReplyForUnreadableMessage(SessionID arg0) {
151		// TODO Auto-generated method stub
152		return null;
153	}
154
155	@Override
156	public OtrPolicy getSessionPolicy(SessionID arg0) {
157		return otrPolicy;
158	}
159
160	@Override
161	public void injectMessage(SessionID session, String body)
162			throws OtrException {
163		MessagePacket packet = new MessagePacket();
164		packet.setFrom(account.getFullJid());
165		if (session.getUserID().isEmpty()) {
166			packet.setTo(session.getAccountID());
167		} else {
168			packet.setTo(session.getAccountID() + "/" + session.getUserID());
169		}
170		packet.setBody(body);
171		packet.addChild("private", "urn:xmpp:carbons:2");
172		packet.addChild("no-copy", "urn:xmpp:hints");
173		packet.setType(MessagePacket.TYPE_CHAT);
174		account.getXmppConnection().sendMessagePacket(packet);
175	}
176
177	@Override
178	public void messageFromAnotherInstanceReceived(SessionID id) {
179		String jid = id.getAccountID();
180		Conversation conversation = mXmppConnectionService
181				.findOrCreateConversation(account, jid, false);
182		Message error = new Message(conversation, null, Message.ENCRYPTION_OTR);
183		conversation.getMessages().add(error);
184		error.setStatus(Message.STATUS_RECEPTION_FAILED);
185		mXmppConnectionService.databaseBackend.createMessage(error);
186		SessionImpl session = conversation.getOtrSession();
187		if (session != null
188				&& session.getSessionStatus() != SessionStatus.ENCRYPTED) {
189			try {
190				session.startSession();
191			} catch (OtrException e) {
192			}
193		}
194	}
195
196	@Override
197	public void multipleInstancesDetected(SessionID arg0) {
198		// TODO Auto-generated method stub
199
200	}
201
202	@Override
203	public void requireEncryptedMessage(SessionID arg0, String arg1)
204			throws OtrException {
205		// TODO Auto-generated method stub
206
207	}
208
209	@Override
210	public void showError(SessionID arg0, String arg1) throws OtrException {
211		// TODO Auto-generated method stub
212
213	}
214
215	@Override
216	public void smpAborted(SessionID arg0) throws OtrException {
217		// TODO Auto-generated method stub
218
219	}
220
221	@Override
222	public void smpError(SessionID arg0, int arg1, boolean arg2)
223			throws OtrException {
224		throw new OtrException(new Exception("smp error"));
225	}
226
227	@Override
228	public void unencryptedMessageReceived(SessionID arg0, String arg1)
229			throws OtrException {
230		throw new OtrException(new Exception("unencrypted message received"));
231	}
232
233	@Override
234	public void unreadableMessageReceived(SessionID arg0) throws OtrException {
235		throw new OtrException(new Exception("unreadable message received"));
236	}
237
238	@Override
239	public void unverify(SessionID arg0, String arg1) {
240		// TODO Auto-generated method stub
241
242	}
243
244	@Override
245	public void verify(SessionID arg0, String arg1, boolean arg2) {
246		// TODO Auto-generated method stub
247
248	}
249
250}