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.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());
158		if (session.getUserID().isEmpty()) {
159			packet.setTo(session.getAccountID());
160		} else {
161			packet.setTo(session.getAccountID()+"/"+session.getUserID());
162		}
163		packet.setBody(body);
164		packet.addChild("private","urn:xmpp:carbons:2");
165		packet.addChild("no-copy","urn:xmpp:hints");
166		packet.setType(MessagePacket.TYPE_CHAT);
167		account.getXmppConnection().sendMessagePacket(packet);
168	}
169
170	@Override
171	public void messageFromAnotherInstanceReceived(SessionID arg0) {
172		// TODO Auto-generated method stub
173
174	}
175
176	@Override
177	public void multipleInstancesDetected(SessionID arg0) {
178		// TODO Auto-generated method stub
179
180	}
181
182	@Override
183	public void requireEncryptedMessage(SessionID arg0, String arg1)
184			throws OtrException {
185		// TODO Auto-generated method stub
186
187	}
188
189	@Override
190	public void showError(SessionID arg0, String arg1) throws OtrException {
191		// TODO Auto-generated method stub
192
193	}
194
195	@Override
196	public void smpAborted(SessionID arg0) throws OtrException {
197		// TODO Auto-generated method stub
198
199	}
200
201	@Override
202	public void smpError(SessionID arg0, int arg1, boolean arg2)
203			throws OtrException {
204		throw new OtrException(new Exception("smp error"));
205	}
206
207	@Override
208	public void unencryptedMessageReceived(SessionID arg0, String arg1)
209			throws OtrException {
210		throw new OtrException(new Exception("unencrypted message received"));
211	}
212
213	@Override
214	public void unreadableMessageReceived(SessionID arg0) throws OtrException {
215		throw new OtrException(new Exception("unreadable message received"));
216	}
217
218	@Override
219	public void unverify(SessionID arg0, String arg1) {
220		// TODO Auto-generated method stub
221
222	}
223
224	@Override
225	public void verify(SessionID arg0, String arg1, boolean arg2) {
226		// TODO Auto-generated method stub
227
228	}
229
230}