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.xml.Element;
 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 static final String LOGTAG = "xmppService";
 35	
 36	private Account account;
 37	private OtrPolicy otrPolicy;
 38	private KeyPair keyPair;
 39	private Context context;
 40
 41	public OtrEngine(Context context, Account account) {
 42		this.account = account;
 43		this.otrPolicy = new OtrPolicyImpl();
 44		this.otrPolicy.setAllowV1(false);
 45		this.otrPolicy.setAllowV2(true);
 46		this.otrPolicy.setAllowV3(true);
 47		this.keyPair = loadKey(account.getKeys());
 48	}
 49	
 50	private KeyPair loadKey(JSONObject keys) {
 51		if (keys == null) {
 52			return null;
 53		}
 54		try {
 55			BigInteger x = new BigInteger(keys.getString("otr_x"),16);
 56			BigInteger y = new BigInteger(keys.getString("otr_y"),16);
 57			BigInteger p = new BigInteger(keys.getString("otr_p"),16);
 58			BigInteger q = new BigInteger(keys.getString("otr_q"),16);
 59			BigInteger g = new BigInteger(keys.getString("otr_g"),16);
 60			KeyFactory keyFactory = KeyFactory.getInstance("DSA");
 61			DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(y, p, q, g);
 62			DSAPrivateKeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
 63			PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
 64			PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
 65			return new KeyPair(publicKey, privateKey);
 66		} catch (JSONException e) {
 67			return null;
 68		} catch (NoSuchAlgorithmException e) {
 69			return null;
 70		} catch (InvalidKeySpecException e) {
 71			return null;
 72		}
 73	}
 74	
 75	private void saveKey() {
 76		PublicKey publicKey = keyPair.getPublic();
 77		PrivateKey privateKey = keyPair.getPrivate();
 78		KeyFactory keyFactory;
 79		try {
 80			keyFactory = KeyFactory.getInstance("DSA");
 81			DSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec(privateKey, DSAPrivateKeySpec.class);
 82			DSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(publicKey, 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		return this.keyPair.getPublic();
123	}
124	
125	@Override
126	public KeyPair getLocalKeyPair(SessionID arg0) throws OtrException {
127		if (this.keyPair==null) {
128			KeyPairGenerator kg;
129			try {
130			kg = KeyPairGenerator.getInstance("DSA");
131			this.keyPair = kg.genKeyPair();
132			this.saveKey();
133			DatabaseBackend.getInstance(context).updateAccount(account);
134			} catch (NoSuchAlgorithmException e) {
135				Log.d(LOGTAG,"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) throws OtrException {
154		MessagePacket packet = new MessagePacket();
155		packet.setFrom(account.getFullJid()); //sender
156		packet.setTo(session.getAccountID()+"/"+session.getUserID()); //reciepient
157		packet.setBody(body);
158		Element privateTag = new Element("private");
159		privateTag.setAttribute("xmlns","urn:xmpp:carbons:2");
160		packet.addChild(privateTag);
161		packet.setType(MessagePacket.TYPE_CHAT);
162		account.getXmppConnection().sendMessagePacket(packet);
163	}
164
165	@Override
166	public void messageFromAnotherInstanceReceived(SessionID arg0) {
167		// TODO Auto-generated method stub
168
169	}
170
171	@Override
172	public void multipleInstancesDetected(SessionID arg0) {
173		// TODO Auto-generated method stub
174
175	}
176
177	@Override
178	public void requireEncryptedMessage(SessionID arg0, String arg1)
179			throws OtrException {
180		// TODO Auto-generated method stub
181
182	}
183
184	@Override
185	public void showError(SessionID arg0, String arg1) throws OtrException {
186		// TODO Auto-generated method stub
187
188	}
189
190	@Override
191	public void smpAborted(SessionID arg0) throws OtrException {
192		// TODO Auto-generated method stub
193
194	}
195
196	@Override
197	public void smpError(SessionID arg0, int arg1, boolean arg2)
198			throws OtrException {
199		// TODO Auto-generated method stub
200
201	}
202
203	@Override
204	public void unencryptedMessageReceived(SessionID arg0, String arg1)
205			throws OtrException {
206		// TODO Auto-generated method stub
207
208	}
209
210	@Override
211	public void unreadableMessageReceived(SessionID arg0) throws OtrException {
212		throw new OtrException(new Exception("unreadable message received"));
213	}
214
215	@Override
216	public void unverify(SessionID arg0, String arg1) {
217		// TODO Auto-generated method stub
218
219	}
220
221	@Override
222	public void verify(SessionID arg0, String arg1, boolean arg2) {
223		// TODO Auto-generated method stub
224
225	}
226
227}