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.util.Log;
 18
 19import eu.siacs.conversations.Config;
 20import eu.siacs.conversations.entities.Account;
 21import eu.siacs.conversations.services.XmppConnectionService;
 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 Account account;
 34	private OtrPolicy otrPolicy;
 35	private KeyPair keyPair;
 36	private XmppConnectionService mXmppConnectionService;
 37
 38	public OtrEngine(XmppConnectionService service, Account account) {
 39		this.account = account;
 40		this.otrPolicy = new OtrPolicyImpl();
 41		this.otrPolicy.setAllowV1(false);
 42		this.otrPolicy.setAllowV2(true);
 43		this.otrPolicy.setAllowV3(true);
 44		this.keyPair = loadKey(account.getKeys());
 45		this.mXmppConnectionService = service;
 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 (final NoSuchAlgorithmException | InvalidKeySpecException e) {
 89			e.printStackTrace();
 90		}
 91
 92    }
 93
 94	@Override
 95	public void askForSecret(SessionID arg0, InstanceTag arg1, String arg2) {
 96		// TODO Auto-generated method stub
 97
 98	}
 99
100	@Override
101	public void finishedSessionMessage(SessionID arg0, String arg1)
102			throws OtrException {
103
104	}
105
106	@Override
107	public String getFallbackMessage(SessionID arg0) {
108		return "I would like to start a private (OTR encrypted) conversation but your client doesn’t seem to support that";
109	}
110
111	@Override
112	public byte[] getLocalFingerprintRaw(SessionID arg0) {
113		// TODO Auto-generated method stub
114		return null;
115	}
116
117	public PublicKey getPublicKey() {
118		if (this.keyPair == null) {
119			return null;
120		}
121		return this.keyPair.getPublic();
122	}
123
124	@Override
125	public KeyPair getLocalKeyPair(SessionID arg0) throws OtrException {
126		if (this.keyPair == null) {
127			KeyPairGenerator kg;
128			try {
129				kg = KeyPairGenerator.getInstance("DSA");
130				this.keyPair = kg.genKeyPair();
131				this.saveKey();
132				mXmppConnectionService.databaseBackend.updateAccount(account);
133			} catch (NoSuchAlgorithmException e) {
134				Log.d(Config.LOGTAG,
135						"error generating key pair " + e.getMessage());
136			}
137		}
138		return this.keyPair;
139	}
140
141	@Override
142	public String getReplyForUnreadableMessage(SessionID arg0) {
143		// TODO Auto-generated method stub
144		return null;
145	}
146
147	@Override
148	public OtrPolicy getSessionPolicy(SessionID arg0) {
149		return otrPolicy;
150	}
151
152	@Override
153	public void injectMessage(SessionID session, String body)
154			throws OtrException {
155		MessagePacket packet = new MessagePacket();
156		packet.setFrom(account.getFullJid());
157		if (session.getUserID().isEmpty()) {
158			packet.setAttribute("to", session.getAccountID());
159		} else {
160			packet.setAttribute("to", session.getAccountID() + "/" + session.getUserID());
161		}
162		packet.setBody(body);
163		packet.addChild("private", "urn:xmpp:carbons:2");
164		packet.addChild("no-copy", "urn:xmpp:hints");
165		packet.setType(MessagePacket.TYPE_CHAT);
166		account.getXmppConnection().sendMessagePacket(packet);
167	}
168
169	@Override
170	public void messageFromAnotherInstanceReceived(SessionID id) {
171		Log.d(Config.LOGTAG,
172				"unreadable message received from " + id.getAccountID());
173	}
174
175	@Override
176	public void multipleInstancesDetected(SessionID arg0) {
177		// TODO Auto-generated method stub
178
179	}
180
181	@Override
182	public void requireEncryptedMessage(SessionID arg0, String arg1)
183			throws OtrException {
184		// TODO Auto-generated method stub
185
186	}
187
188	@Override
189	public void showError(SessionID arg0, String arg1) throws OtrException {
190		// TODO Auto-generated method stub
191
192	}
193
194	@Override
195	public void smpAborted(SessionID arg0) throws OtrException {
196		// TODO Auto-generated method stub
197
198	}
199
200	@Override
201	public void smpError(SessionID arg0, int arg1, boolean arg2)
202			throws OtrException {
203		throw new OtrException(new Exception("smp error"));
204	}
205
206	@Override
207	public void unencryptedMessageReceived(SessionID arg0, String arg1)
208			throws OtrException {
209		throw new OtrException(new Exception("unencrypted message received"));
210	}
211
212	@Override
213	public void unreadableMessageReceived(SessionID arg0) throws OtrException {
214		throw new OtrException(new Exception("unreadable message received"));
215	}
216
217	@Override
218	public void unverify(SessionID arg0, String arg1) {
219		// TODO Auto-generated method stub
220
221	}
222
223	@Override
224	public void verify(SessionID arg0, String arg1, boolean arg2) {
225		// TODO Auto-generated method stub
226
227	}
228
229}