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 (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
106	}
107
108	@Override
109	public String getFallbackMessage(SessionID arg0) {
110		return "I would like to start a private (OTR encrypted) conversation but your client doesn’t seem to support that";
111	}
112
113	@Override
114	public byte[] getLocalFingerprintRaw(SessionID arg0) {
115		// TODO Auto-generated method stub
116		return null;
117	}
118
119	public PublicKey getPublicKey() {
120		if (this.keyPair == null) {
121			return null;
122		}
123		return this.keyPair.getPublic();
124	}
125
126	@Override
127	public KeyPair getLocalKeyPair(SessionID arg0) throws OtrException {
128		if (this.keyPair == null) {
129			KeyPairGenerator kg;
130			try {
131				kg = KeyPairGenerator.getInstance("DSA");
132				this.keyPair = kg.genKeyPair();
133				this.saveKey();
134				mXmppConnectionService.databaseBackend.updateAccount(account);
135			} catch (NoSuchAlgorithmException e) {
136				Log.d(Config.LOGTAG,
137						"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)
156			throws OtrException {
157		MessagePacket packet = new MessagePacket();
158		packet.setFrom(account.getFullJid());
159		if (session.getUserID().isEmpty()) {
160			packet.setTo(session.getAccountID());
161		} else {
162			packet.setTo(session.getAccountID() + "/" + session.getUserID());
163		}
164		packet.setBody(body);
165		packet.addChild("private", "urn:xmpp:carbons:2");
166		packet.addChild("no-copy", "urn:xmpp:hints");
167		packet.setType(MessagePacket.TYPE_CHAT);
168		account.getXmppConnection().sendMessagePacket(packet);
169	}
170
171	@Override
172	public void messageFromAnotherInstanceReceived(SessionID id) {
173		Log.d(Config.LOGTAG,
174				"unreadable message received from " + id.getAccountID());
175	}
176
177	@Override
178	public void multipleInstancesDetected(SessionID arg0) {
179		// TODO Auto-generated method stub
180
181	}
182
183	@Override
184	public void requireEncryptedMessage(SessionID arg0, String arg1)
185			throws OtrException {
186		// TODO Auto-generated method stub
187
188	}
189
190	@Override
191	public void showError(SessionID arg0, String arg1) throws OtrException {
192		// TODO Auto-generated method stub
193
194	}
195
196	@Override
197	public void smpAborted(SessionID arg0) throws OtrException {
198		// TODO Auto-generated method stub
199
200	}
201
202	@Override
203	public void smpError(SessionID arg0, int arg1, boolean arg2)
204			throws OtrException {
205		throw new OtrException(new Exception("smp error"));
206	}
207
208	@Override
209	public void unencryptedMessageReceived(SessionID arg0, String arg1)
210			throws OtrException {
211		throw new OtrException(new Exception("unencrypted message received"));
212	}
213
214	@Override
215	public void unreadableMessageReceived(SessionID arg0) throws OtrException {
216		throw new OtrException(new Exception("unreadable message received"));
217	}
218
219	@Override
220	public void unverify(SessionID arg0, String arg1) {
221		// TODO Auto-generated method stub
222
223	}
224
225	@Override
226	public void verify(SessionID arg0, String arg1, boolean arg2) {
227		// TODO Auto-generated method stub
228
229	}
230
231}