2015-05-29 09:17:26 +00:00
|
|
|
package eu.siacs.conversations.crypto.axolotl;
|
|
|
|
|
2015-06-25 14:56:34 +00:00
|
|
|
import android.util.Base64;
|
2015-07-31 15:59:41 +00:00
|
|
|
import android.util.Log;
|
2018-01-18 19:58:55 +00:00
|
|
|
import android.util.SparseArray;
|
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-07-31 19:12:34 +00:00
|
|
|
import java.util.List;
|
2015-06-25 14:56:34 +00:00
|
|
|
|
|
|
|
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;
|
2018-09-17 15:47:36 +00:00
|
|
|
import eu.siacs.conversations.utils.Compatibility;
|
2015-06-25 14:56:34 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2015-06-25 14:56:34 +00:00
|
|
|
|
2015-05-29 09:17:26 +00:00
|
|
|
public class XmppAxolotlMessage {
|
2015-07-31 19:12:34 +00:00
|
|
|
public static final String CONTAINERTAG = "encrypted";
|
2018-10-04 20:32:37 +00:00
|
|
|
private static final String HEADER = "header";
|
|
|
|
private static final String SOURCEID = "sid";
|
|
|
|
private static final String KEYTAG = "key";
|
|
|
|
private static final String REMOTEID = "rid";
|
|
|
|
private static final String IVTAG = "iv";
|
|
|
|
private static final String PAYLOAD = "payload";
|
2015-07-31 15:59:41 +00:00
|
|
|
|
|
|
|
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;
|
2017-01-09 20:47:07 +00:00
|
|
|
private byte[] authtagPlusInnerKey = null;
|
2015-07-31 15:59:41 +00:00
|
|
|
private byte[] iv = null;
|
2018-01-18 19:58:55 +00:00
|
|
|
private final SparseArray<XmppAxolotlSession.AxolotlKey> keys;
|
2015-07-03 11:27:35 +00:00
|
|
|
private final Jid from;
|
2015-06-26 13:41:02 +00:00
|
|
|
private final int sourceDeviceId;
|
|
|
|
|
|
|
|
public static class XmppAxolotlPlaintextMessage {
|
|
|
|
private final String plaintext;
|
2015-07-09 12:23:17 +00:00
|
|
|
private final String fingerprint;
|
2015-06-26 13:41:02 +00:00
|
|
|
|
2018-10-04 20:32:37 +00:00
|
|
|
XmppAxolotlPlaintextMessage(String plaintext, String fingerprint) {
|
2015-06-26 13:41:02 +00:00
|
|
|
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-09 12:23:17 +00:00
|
|
|
public String getFingerprint() {
|
|
|
|
return fingerprint;
|
|
|
|
}
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 21:28:09 +00:00
|
|
|
public static class XmppAxolotlKeyTransportMessage {
|
|
|
|
private final String fingerprint;
|
|
|
|
private final byte[] key;
|
|
|
|
private final byte[] iv;
|
|
|
|
|
2018-10-04 20:32:37 +00:00
|
|
|
XmppAxolotlKeyTransportMessage(String fingerprint, byte[] key, byte[] iv) {
|
2015-07-31 21:28:09 +00:00
|
|
|
this.fingerprint = fingerprint;
|
|
|
|
this.key = key;
|
|
|
|
this.iv = iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFingerprint() {
|
|
|
|
return fingerprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getKey() {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getIv() {
|
|
|
|
return iv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 12:26:35 +00:00
|
|
|
public static int parseSourceId(final Element axolotlMessage) throws IllegalArgumentException {
|
|
|
|
final Element header = axolotlMessage.findChild(HEADER);
|
|
|
|
if (header == null) {
|
|
|
|
throw new IllegalArgumentException("No header found");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return Integer.parseInt(header.getAttribute(SOURCEID));
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
throw new IllegalArgumentException("invalid source id");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 19:12:34 +00:00
|
|
|
private XmppAxolotlMessage(final Element axolotlMessage, final Jid from) 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);
|
2016-10-06 20:05:18 +00:00
|
|
|
try {
|
|
|
|
this.sourceDeviceId = Integer.parseInt(header.getAttribute(SOURCEID));
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
throw new IllegalArgumentException("invalid source id");
|
|
|
|
}
|
2015-07-31 19:12:34 +00:00
|
|
|
List<Element> keyElements = header.getChildren();
|
2018-01-18 19:58:55 +00:00
|
|
|
this.keys = new SparseArray<>();
|
2015-07-31 19:12:34 +00:00
|
|
|
for (Element keyElement : keyElements) {
|
2015-07-31 16:05:32 +00:00
|
|
|
switch (keyElement.getName()) {
|
2015-07-31 19:12:34 +00:00
|
|
|
case KEYTAG:
|
|
|
|
try {
|
|
|
|
Integer recipientId = Integer.parseInt(keyElement.getAttribute(REMOTEID));
|
2016-06-29 15:18:57 +00:00
|
|
|
byte[] key = Base64.decode(keyElement.getContent().trim(), Base64.DEFAULT);
|
2017-01-09 19:20:02 +00:00
|
|
|
boolean isPreKey =keyElement.getAttributeAsBoolean("prekey");
|
|
|
|
this.keys.put(recipientId, new XmppAxolotlSession.AxolotlKey(key,isPreKey));
|
2015-07-31 19:12:34 +00:00
|
|
|
} catch (NumberFormatException e) {
|
2016-10-06 20:05:18 +00:00
|
|
|
throw new IllegalArgumentException("invalid remote id");
|
2015-07-31 19:12:34 +00:00
|
|
|
}
|
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");
|
|
|
|
}
|
2016-06-29 15:18:57 +00:00
|
|
|
iv = Base64.decode(keyElement.getContent().trim(), 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) {
|
2016-06-29 15:18:57 +00:00
|
|
|
ciphertext = Base64.decode(payloadElement.getContent().trim(), Base64.DEFAULT);
|
2015-07-31 15:59:41 +00:00
|
|
|
}
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 20:32:37 +00:00
|
|
|
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;
|
2018-01-18 19:58:55 +00:00
|
|
|
this.keys = new SparseArray<>();
|
2015-07-31 15:59:41 +00:00
|
|
|
this.iv = generateIv();
|
|
|
|
this.innerKey = generateKey();
|
|
|
|
}
|
|
|
|
|
2015-07-31 19:12:34 +00:00
|
|
|
public static XmppAxolotlMessage fromElement(Element element, Jid from) {
|
|
|
|
return new XmppAxolotlMessage(element, from);
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2018-11-21 15:44:24 +00:00
|
|
|
byte[] iv = new byte[16];
|
2015-07-31 15:59:41 +00:00
|
|
|
random.nextBytes(iv);
|
|
|
|
return iv;
|
|
|
|
}
|
|
|
|
|
2018-01-18 19:58:55 +00:00
|
|
|
public boolean hasPayload() {
|
|
|
|
return ciphertext != null;
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:32:37 +00:00
|
|
|
void encrypt(String plaintext) throws CryptoFailedException {
|
2015-07-31 15:59:41 +00:00
|
|
|
try {
|
|
|
|
SecretKey secretKey = new SecretKeySpec(innerKey, KEYTYPE);
|
2015-07-20 23:15:32 +00:00
|
|
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
2018-10-11 02:36:32 +00:00
|
|
|
Cipher cipher = Compatibility.twentyEight() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
|
2015-07-20 23:15:32 +00:00
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
|
2016-12-20 15:12:12 +00:00
|
|
|
this.ciphertext = cipher.doFinal(Config.OMEMO_PADDING ? getPaddedBytes(plaintext) : plaintext.getBytes());
|
2017-01-09 20:47:07 +00:00
|
|
|
if (Config.PUT_AUTH_TAG_INTO_KEY && this.ciphertext != null) {
|
|
|
|
this.authtagPlusInnerKey = new byte[16+16];
|
|
|
|
byte[] ciphertext = new byte[this.ciphertext.length - 16];
|
|
|
|
System.arraycopy(this.ciphertext,0,ciphertext,0,ciphertext.length);
|
|
|
|
System.arraycopy(this.ciphertext,ciphertext.length,authtagPlusInnerKey,16,16);
|
|
|
|
System.arraycopy(this.innerKey,0,authtagPlusInnerKey,0,this.innerKey.length);
|
|
|
|
this.ciphertext = ciphertext;
|
|
|
|
}
|
2015-06-26 13:41:02 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 15:12:12 +00:00
|
|
|
private static byte[] getPaddedBytes(String plaintext) {
|
|
|
|
int plainLength = plaintext.getBytes().length;
|
|
|
|
int pad = Math.max(64,(plainLength / 32 + 1) * 32) - plainLength;
|
|
|
|
SecureRandom random = new SecureRandom();
|
|
|
|
int left = random.nextInt(pad);
|
|
|
|
int right = pad - left;
|
|
|
|
StringBuilder builder = new StringBuilder(plaintext);
|
|
|
|
for(int i = 0; i < left; ++i) {
|
|
|
|
builder.insert(0,random.nextBoolean() ? "\t" : " ");
|
|
|
|
}
|
|
|
|
for(int i = 0; i < right; ++i) {
|
|
|
|
builder.append(random.nextBoolean() ? "\t" : " ");
|
|
|
|
}
|
|
|
|
return builder.toString().getBytes();
|
|
|
|
}
|
|
|
|
|
2015-07-03 11:27:35 +00:00
|
|
|
public Jid getFrom() {
|
|
|
|
return this.from;
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 07:24:31 +00:00
|
|
|
int getSenderDeviceId() {
|
2015-06-26 13:41:02 +00:00
|
|
|
return sourceDeviceId;
|
|
|
|
}
|
|
|
|
|
2018-05-09 07:24:31 +00:00
|
|
|
void addDevice(XmppAxolotlSession session) {
|
|
|
|
addDevice(session, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addDevice(XmppAxolotlSession session, boolean ignoreSessionTrust) {
|
2017-01-09 20:47:07 +00:00
|
|
|
XmppAxolotlSession.AxolotlKey key;
|
|
|
|
if (authtagPlusInnerKey != null) {
|
2018-05-09 07:24:31 +00:00
|
|
|
key = session.processSending(authtagPlusInnerKey, ignoreSessionTrust);
|
2017-01-09 20:47:07 +00:00
|
|
|
} else {
|
2018-05-09 07:24:31 +00:00
|
|
|
key = session.processSending(innerKey, ignoreSessionTrust);
|
2017-01-09 20:47:07 +00:00
|
|
|
}
|
2015-07-31 19:12:34 +00:00
|
|
|
if (key != null) {
|
|
|
|
keys.put(session.getRemoteAddress().getDeviceId(), key);
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-31 19:12:34 +00:00
|
|
|
public Element toElement() {
|
|
|
|
Element encryptionElement = new Element(CONTAINERTAG, AxolotlService.PEP_PREFIX);
|
2015-07-31 15:59:41 +00:00
|
|
|
Element headerElement = encryptionElement.addChild(HEADER);
|
|
|
|
headerElement.setAttribute(SOURCEID, sourceDeviceId);
|
2018-01-18 19:58:55 +00:00
|
|
|
for(int i = 0; i < keys.size(); ++i) {
|
2015-07-31 19:12:34 +00:00
|
|
|
Element keyElement = new Element(KEYTAG);
|
2018-01-18 19:58:55 +00:00
|
|
|
keyElement.setAttribute(REMOTEID, keys.keyAt(i));
|
|
|
|
if (keys.valueAt(i).prekey) {
|
2017-01-09 19:20:02 +00:00
|
|
|
keyElement.setAttribute("prekey","true");
|
|
|
|
}
|
2018-01-18 19:58:55 +00:00
|
|
|
keyElement.setContent(Base64.encodeToString(keys.valueAt(i).key, Base64.NO_WRAP));
|
2015-07-31 19:12:34 +00:00
|
|
|
headerElement.addChild(keyElement);
|
2015-07-31 15:59:41 +00:00
|
|
|
}
|
2016-11-17 09:58:44 +00:00
|
|
|
headerElement.addChild(IVTAG).setContent(Base64.encodeToString(iv, Base64.NO_WRAP));
|
2015-07-31 16:05:32 +00:00
|
|
|
if (ciphertext != null) {
|
2015-07-31 15:59:41 +00:00
|
|
|
Element payload = encryptionElement.addChild(PAYLOAD);
|
2016-11-17 09:58:44 +00:00
|
|
|
payload.setContent(Base64.encodeToString(ciphertext, Base64.NO_WRAP));
|
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
|
|
|
}
|
|
|
|
|
2017-01-14 17:10:04 +00:00
|
|
|
private byte[] unpackKey(XmppAxolotlSession session, Integer sourceDeviceId) throws CryptoFailedException {
|
2017-01-09 19:20:02 +00:00
|
|
|
XmppAxolotlSession.AxolotlKey encryptedKey = keys.get(sourceDeviceId);
|
2017-01-14 17:10:04 +00:00
|
|
|
if (encryptedKey == null) {
|
2018-04-04 07:11:53 +00:00
|
|
|
throw new NotEncryptedForThisDeviceException();
|
2017-01-14 17:10:04 +00:00
|
|
|
}
|
|
|
|
return session.processReceiving(encryptedKey);
|
2015-07-31 19:12:34 +00:00
|
|
|
}
|
2015-06-26 13:41:02 +00:00
|
|
|
|
2018-10-04 20:32:37 +00:00
|
|
|
XmppAxolotlKeyTransportMessage getParameters(XmppAxolotlSession session, Integer sourceDeviceId) throws CryptoFailedException {
|
2017-01-14 17:10:04 +00:00
|
|
|
return new XmppAxolotlKeyTransportMessage(session.getFingerprint(), unpackKey(session, sourceDeviceId), getIV());
|
2015-07-31 21:28:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 19:12:34 +00:00
|
|
|
public XmppAxolotlPlaintextMessage decrypt(XmppAxolotlSession session, Integer sourceDeviceId) throws CryptoFailedException {
|
2015-06-26 13:41:02 +00:00
|
|
|
XmppAxolotlPlaintextMessage plaintextMessage = null;
|
2015-07-31 19:12:34 +00:00
|
|
|
byte[] key = unpackKey(session, sourceDeviceId);
|
|
|
|
if (key != null) {
|
|
|
|
try {
|
2017-01-09 20:47:07 +00:00
|
|
|
if (key.length >= 32) {
|
|
|
|
int authtaglength = key.length - 16;
|
|
|
|
Log.d(Config.LOGTAG,"found auth tag as part of omemo key");
|
|
|
|
byte[] newCipherText = new byte[key.length - 16 + ciphertext.length];
|
|
|
|
byte[] newKey = new byte[16];
|
|
|
|
System.arraycopy(ciphertext, 0, newCipherText, 0, ciphertext.length);
|
|
|
|
System.arraycopy(key, 16, newCipherText, ciphertext.length, authtaglength);
|
|
|
|
System.arraycopy(key,0,newKey,0,newKey.length);
|
|
|
|
ciphertext = newCipherText;
|
|
|
|
key = newKey;
|
|
|
|
}
|
|
|
|
|
2018-10-11 02:36:32 +00:00
|
|
|
Cipher cipher = Compatibility.twentyEight() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
|
2015-07-31 19:12:34 +00:00
|
|
|
SecretKeySpec keySpec = new SecretKeySpec(key, KEYTYPE);
|
|
|
|
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
|
|
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
|
|
|
|
|
|
|
|
String plaintext = new String(cipher.doFinal(ciphertext));
|
2016-12-20 15:12:12 +00:00
|
|
|
plaintextMessage = new XmppAxolotlPlaintextMessage(Config.OMEMO_PADDING ? plaintext.trim() : plaintext, session.getFingerprint());
|
2015-07-31 19:12:34 +00:00
|
|
|
|
|
|
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
|
|
|
| InvalidAlgorithmParameterException | IllegalBlockSizeException
|
|
|
|
| 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
|
|
|
}
|