2015-05-29 09:17:26 +00:00
|
|
|
package eu.siacs.conversations.crypto.axolotl;
|
|
|
|
|
2015-07-20 20:18:24 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2015-06-25 14:56:34 +00:00
|
|
|
import android.util.Base64;
|
2015-07-31 15:59:41 +00:00
|
|
|
import android.util.Log;
|
2015-06-25 14:56:34 +00:00
|
|
|
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.InvalidKeyException;
|
2015-07-20 12:26:29 +00:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
2015-07-20 23:15:32 +00:00
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
import java.security.SecureRandom;
|
2015-06-25 14:56:34 +00:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import javax.crypto.BadPaddingException;
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
|
|
import javax.crypto.KeyGenerator;
|
|
|
|
import javax.crypto.NoSuchPaddingException;
|
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2015-06-25 14:56:34 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2015-07-03 11:27:35 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2015-06-25 14:56:34 +00:00
|
|
|
|
2015-05-29 09:17:26 +00:00
|
|
|
public class XmppAxolotlMessage {
|
2015-07-31 15:59:41 +00:00
|
|
|
public static final String TAGNAME = "encrypted";
|
|
|
|
public static final String HEADER = "header";
|
|
|
|
public static final String SOURCEID = "sid";
|
|
|
|
public static final String IVTAG = "iv";
|
|
|
|
public static final String PAYLOAD = "payload";
|
|
|
|
|
|
|
|
private static final String KEYTYPE = "AES";
|
|
|
|
private static final String CIPHERMODE = "AES/GCM/NoPadding";
|
|
|
|
private static final String PROVIDER = "BC";
|
|
|
|
|
2015-06-26 13:41:02 +00:00
|
|
|
private byte[] innerKey;
|
2015-07-31 15:59:41 +00:00
|
|
|
private byte[] ciphertext = null;
|
|
|
|
private byte[] iv = null;
|
|
|
|
private final Set<XmppAxolotlKeyElement> keyElements;
|
2015-07-03 11:27:35 +00:00
|
|
|
private final Jid from;
|
2015-06-26 13:41:02 +00:00
|
|
|
private final int sourceDeviceId;
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
public static class XmppAxolotlKeyElement {
|
|
|
|
public static final String TAGNAME = "key";
|
|
|
|
public static final String REMOTEID = "rid";
|
|
|
|
|
2015-06-26 13:41:02 +00:00
|
|
|
private final int recipientDeviceId;
|
|
|
|
private final byte[] content;
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
public XmppAxolotlKeyElement(int deviceId, byte[] content) {
|
2015-06-26 13:41:02 +00:00
|
|
|
this.recipientDeviceId = deviceId;
|
|
|
|
this.content = content;
|
|
|
|
}
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
public XmppAxolotlKeyElement(Element keyElement) {
|
2015-07-31 16:05:32 +00:00
|
|
|
if (TAGNAME.equals(keyElement.getName())) {
|
2015-07-31 15:59:41 +00:00
|
|
|
this.recipientDeviceId = Integer.parseInt(keyElement.getAttribute(REMOTEID));
|
2015-07-31 16:05:32 +00:00
|
|
|
this.content = Base64.decode(keyElement.getContent(), Base64.DEFAULT);
|
2015-06-26 13:41:02 +00:00
|
|
|
} else {
|
2015-07-31 16:05:32 +00:00
|
|
|
throw new IllegalArgumentException("Argument not a <" + TAGNAME + "> Element!");
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getRecipientDeviceId() {
|
|
|
|
return recipientDeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getContents() {
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element toXml() {
|
2015-07-31 15:59:41 +00:00
|
|
|
Element keyElement = new Element(TAGNAME);
|
|
|
|
keyElement.setAttribute(REMOTEID, getRecipientDeviceId());
|
|
|
|
keyElement.setContent(Base64.encodeToString(getContents(), Base64.DEFAULT));
|
|
|
|
return keyElement;
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class XmppAxolotlPlaintextMessage {
|
2015-07-28 20:00:54 +00:00
|
|
|
private final XmppAxolotlSession session;
|
2015-06-26 13:41:02 +00:00
|
|
|
private final String plaintext;
|
2015-07-09 12:23:17 +00:00
|
|
|
private final String fingerprint;
|
2015-06-26 13:41:02 +00:00
|
|
|
|
2015-07-28 20:00:54 +00:00
|
|
|
public XmppAxolotlPlaintextMessage(XmppAxolotlSession session, String plaintext, String fingerprint) {
|
2015-06-26 13:41:02 +00:00
|
|
|
this.session = session;
|
|
|
|
this.plaintext = plaintext;
|
2015-07-09 12:23:17 +00:00
|
|
|
this.fingerprint = fingerprint;
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPlaintext() {
|
|
|
|
return plaintext;
|
|
|
|
}
|
2015-06-29 12:25:23 +00:00
|
|
|
|
2015-07-28 20:00:54 +00:00
|
|
|
public XmppAxolotlSession getSession() {
|
2015-06-29 12:25:23 +00:00
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2015-07-09 12:23:17 +00:00
|
|
|
public String getFingerprint() {
|
|
|
|
return fingerprint;
|
|
|
|
}
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
public XmppAxolotlMessage(Jid from, Element axolotlMessage) throws IllegalArgumentException {
|
2015-07-03 11:27:35 +00:00
|
|
|
this.from = from;
|
2015-07-31 15:59:41 +00:00
|
|
|
Element header = axolotlMessage.findChild(HEADER);
|
|
|
|
this.sourceDeviceId = Integer.parseInt(header.getAttribute(SOURCEID));
|
|
|
|
this.keyElements = new HashSet<>();
|
2015-07-31 16:05:32 +00:00
|
|
|
for (Element keyElement : header.getChildren()) {
|
|
|
|
switch (keyElement.getName()) {
|
2015-07-31 15:59:41 +00:00
|
|
|
case XmppAxolotlKeyElement.TAGNAME:
|
|
|
|
keyElements.add(new XmppAxolotlKeyElement(keyElement));
|
2015-06-26 13:41:02 +00:00
|
|
|
break;
|
2015-07-31 15:59:41 +00:00
|
|
|
case IVTAG:
|
2015-07-31 16:05:32 +00:00
|
|
|
if (this.iv != null) {
|
2015-07-31 15:59:41 +00:00
|
|
|
throw new IllegalArgumentException("Duplicate iv entry");
|
|
|
|
}
|
2015-07-31 16:05:32 +00:00
|
|
|
iv = Base64.decode(keyElement.getContent(), Base64.DEFAULT);
|
2015-06-26 13:41:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-07-31 16:05:32 +00:00
|
|
|
Log.w(Config.LOGTAG, "Unexpected element in header: " + keyElement.toString());
|
2015-06-26 13:41:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 15:59:41 +00:00
|
|
|
Element payloadElement = axolotlMessage.findChild(PAYLOAD);
|
2015-07-31 16:05:32 +00:00
|
|
|
if (payloadElement != null) {
|
2015-07-31 15:59:41 +00:00
|
|
|
ciphertext = Base64.decode(payloadElement.getContent(), Base64.DEFAULT);
|
|
|
|
}
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
public XmppAxolotlMessage(Jid from, int sourceDeviceId) {
|
2015-07-03 11:27:35 +00:00
|
|
|
this.from = from;
|
2015-06-26 13:41:02 +00:00
|
|
|
this.sourceDeviceId = sourceDeviceId;
|
2015-07-31 15:59:41 +00:00
|
|
|
this.keyElements = new HashSet<>();
|
|
|
|
this.iv = generateIv();
|
|
|
|
this.innerKey = generateKey();
|
|
|
|
}
|
|
|
|
|
2015-07-31 16:05:32 +00:00
|
|
|
public XmppAxolotlMessage(Jid from, int sourceDeviceId, String plaintext) throws CryptoFailedException {
|
2015-07-31 15:59:41 +00:00
|
|
|
this(from, sourceDeviceId);
|
2015-06-26 13:41:02 +00:00
|
|
|
this.encrypt(plaintext);
|
|
|
|
}
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
private static byte[] generateKey() {
|
2015-06-26 13:41:02 +00:00
|
|
|
try {
|
2015-07-31 15:59:41 +00:00
|
|
|
KeyGenerator generator = KeyGenerator.getInstance(KEYTYPE);
|
2015-06-26 13:41:02 +00:00
|
|
|
generator.init(128);
|
2015-07-31 15:59:41 +00:00
|
|
|
return generator.generateKey().getEncoded();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
Log.e(Config.LOGTAG, e.getMessage());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static byte[] generateIv() {
|
|
|
|
SecureRandom random = new SecureRandom();
|
|
|
|
byte[] iv = new byte[16];
|
|
|
|
random.nextBytes(iv);
|
|
|
|
return iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void encrypt(String plaintext) throws CryptoFailedException {
|
|
|
|
try {
|
|
|
|
SecretKey secretKey = new SecretKeySpec(innerKey, KEYTYPE);
|
2015-07-20 23:15:32 +00:00
|
|
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
2015-07-31 15:59:41 +00:00
|
|
|
Cipher cipher = Cipher.getInstance(CIPHERMODE, PROVIDER);
|
2015-07-20 23:15:32 +00:00
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
|
2015-06-26 13:41:02 +00:00
|
|
|
this.innerKey = secretKey.getEncoded();
|
|
|
|
this.ciphertext = cipher.doFinal(plaintext.getBytes());
|
|
|
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
2015-07-20 23:15:32 +00:00
|
|
|
| IllegalBlockSizeException | BadPaddingException | NoSuchProviderException
|
|
|
|
| InvalidAlgorithmParameterException e) {
|
|
|
|
throw new CryptoFailedException(e);
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 11:27:35 +00:00
|
|
|
public Jid getFrom() {
|
|
|
|
return this.from;
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getSenderDeviceId() {
|
|
|
|
return sourceDeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getCiphertext() {
|
|
|
|
return ciphertext;
|
|
|
|
}
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
public Set<XmppAxolotlKeyElement> getKeyElements() {
|
|
|
|
return keyElements;
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
public void addKeyElement(@Nullable XmppAxolotlKeyElement keyElement) {
|
|
|
|
if (keyElement != null) {
|
|
|
|
keyElements.add(keyElement);
|
2015-07-20 20:18:24 +00:00
|
|
|
}
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 16:05:32 +00:00
|
|
|
public byte[] getInnerKey() {
|
2015-06-26 13:41:02 +00:00
|
|
|
return innerKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getIV() {
|
|
|
|
return this.iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element toXml() {
|
2015-07-31 16:05:32 +00:00
|
|
|
Element encryptionElement = new Element(TAGNAME, AxolotlService.PEP_PREFIX);
|
2015-07-31 15:59:41 +00:00
|
|
|
Element headerElement = encryptionElement.addChild(HEADER);
|
|
|
|
headerElement.setAttribute(SOURCEID, sourceDeviceId);
|
2015-07-31 16:05:32 +00:00
|
|
|
for (XmppAxolotlKeyElement header : keyElements) {
|
2015-07-31 15:59:41 +00:00
|
|
|
headerElement.addChild(header.toXml());
|
|
|
|
}
|
|
|
|
headerElement.addChild(IVTAG).setContent(Base64.encodeToString(iv, Base64.DEFAULT));
|
2015-07-31 16:05:32 +00:00
|
|
|
if (ciphertext != null) {
|
2015-07-31 15:59:41 +00:00
|
|
|
Element payload = encryptionElement.addChild(PAYLOAD);
|
|
|
|
payload.setContent(Base64.encodeToString(ciphertext, Base64.DEFAULT));
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
2015-07-31 15:59:41 +00:00
|
|
|
return encryptionElement;
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-28 20:00:54 +00:00
|
|
|
public XmppAxolotlPlaintextMessage decrypt(XmppAxolotlSession session, byte[] key, String fingerprint) throws CryptoFailedException {
|
2015-06-26 13:41:02 +00:00
|
|
|
XmppAxolotlPlaintextMessage plaintextMessage = null;
|
|
|
|
try {
|
|
|
|
|
2015-07-31 15:59:41 +00:00
|
|
|
Cipher cipher = Cipher.getInstance(CIPHERMODE, PROVIDER);
|
|
|
|
SecretKeySpec keySpec = new SecretKeySpec(key, KEYTYPE);
|
2015-06-26 13:41:02 +00:00
|
|
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
|
|
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
|
|
|
|
|
|
|
|
String plaintext = new String(cipher.doFinal(ciphertext));
|
2015-07-09 12:23:17 +00:00
|
|
|
plaintextMessage = new XmppAxolotlPlaintextMessage(session, plaintext, fingerprint);
|
2015-06-26 13:41:02 +00:00
|
|
|
|
|
|
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
|
|
|
| InvalidAlgorithmParameterException | IllegalBlockSizeException
|
2015-07-20 23:15:32 +00:00
|
|
|
| BadPaddingException | NoSuchProviderException e) {
|
|
|
|
throw new CryptoFailedException(e);
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
return plaintextMessage;
|
|
|
|
}
|
2015-05-29 09:17:26 +00:00
|
|
|
}
|