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;
|
|
|
|
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.InvalidKeyException;
|
2015-07-20 12:26:29 +00:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
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;
|
|
|
|
|
|
|
|
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-06-26 13:41:02 +00:00
|
|
|
private byte[] innerKey;
|
|
|
|
private byte[] ciphertext;
|
|
|
|
private byte[] iv;
|
|
|
|
private final Set<XmppAxolotlMessageHeader> headers;
|
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 XmppAxolotlMessageHeader {
|
|
|
|
private final int recipientDeviceId;
|
|
|
|
private final byte[] content;
|
|
|
|
|
|
|
|
public XmppAxolotlMessageHeader(int deviceId, byte[] content) {
|
|
|
|
this.recipientDeviceId = deviceId;
|
|
|
|
this.content = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XmppAxolotlMessageHeader(Element header) {
|
|
|
|
if("header".equals(header.getName())) {
|
|
|
|
this.recipientDeviceId = Integer.parseInt(header.getAttribute("rid"));
|
|
|
|
this.content = Base64.decode(header.getContent(),Base64.DEFAULT);
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Argument not a <header> Element!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getRecipientDeviceId() {
|
|
|
|
return recipientDeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getContents() {
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element toXml() {
|
|
|
|
Element headerElement = new Element("header");
|
|
|
|
// TODO: generate XML
|
|
|
|
headerElement.setAttribute("rid", getRecipientDeviceId());
|
|
|
|
headerElement.setContent(Base64.encodeToString(getContents(), Base64.DEFAULT));
|
|
|
|
return headerElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class XmppAxolotlPlaintextMessage {
|
|
|
|
private final AxolotlService.XmppAxolotlSession session;
|
|
|
|
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-09 12:23:17 +00:00
|
|
|
public XmppAxolotlPlaintextMessage(AxolotlService.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
|
|
|
|
|
|
|
public AxolotlService.XmppAxolotlSession getSession() {
|
|
|
|
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-03 11:27:35 +00:00
|
|
|
public XmppAxolotlMessage(Jid from, Element axolotlMessage) {
|
|
|
|
this.from = from;
|
2015-06-26 13:41:02 +00:00
|
|
|
this.sourceDeviceId = Integer.parseInt(axolotlMessage.getAttribute("id"));
|
|
|
|
this.headers = new HashSet<>();
|
|
|
|
for(Element child:axolotlMessage.getChildren()) {
|
|
|
|
switch(child.getName()) {
|
|
|
|
case "header":
|
|
|
|
headers.add(new XmppAxolotlMessageHeader(child));
|
|
|
|
break;
|
|
|
|
case "message":
|
|
|
|
iv = Base64.decode(child.getAttribute("iv"),Base64.DEFAULT);
|
|
|
|
ciphertext = Base64.decode(child.getContent(),Base64.DEFAULT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 11:27:35 +00:00
|
|
|
public XmppAxolotlMessage(Jid from, int sourceDeviceId, String plaintext) {
|
|
|
|
this.from = from;
|
2015-06-26 13:41:02 +00:00
|
|
|
this.sourceDeviceId = sourceDeviceId;
|
|
|
|
this.headers = new HashSet<>();
|
|
|
|
this.encrypt(plaintext);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void encrypt(String plaintext) {
|
|
|
|
try {
|
|
|
|
KeyGenerator generator = KeyGenerator.getInstance("AES");
|
|
|
|
generator.init(128);
|
|
|
|
SecretKey secretKey = generator.generateKey();
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
|
|
|
this.innerKey = secretKey.getEncoded();
|
|
|
|
this.iv = cipher.getIV();
|
|
|
|
this.ciphertext = cipher.doFinal(plaintext.getBytes());
|
|
|
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
|
|
|
|
| IllegalBlockSizeException | BadPaddingException e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<XmppAxolotlMessageHeader> getHeaders() {
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
2015-07-20 20:18:24 +00:00
|
|
|
public void addHeader(@Nullable XmppAxolotlMessageHeader header) {
|
|
|
|
if (header != null) {
|
|
|
|
headers.add(header);
|
|
|
|
}
|
2015-06-26 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getInnerKey(){
|
|
|
|
return innerKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getIV() {
|
|
|
|
return this.iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element toXml() {
|
|
|
|
// TODO: generate outer XML, add in header XML
|
|
|
|
Element message= new Element("axolotl_message", AxolotlService.PEP_PREFIX);
|
|
|
|
message.setAttribute("id", sourceDeviceId);
|
|
|
|
for(XmppAxolotlMessageHeader header: headers) {
|
|
|
|
message.addChild(header.toXml());
|
|
|
|
}
|
|
|
|
Element payload = message.addChild("message");
|
|
|
|
payload.setAttribute("iv",Base64.encodeToString(iv, Base64.DEFAULT));
|
|
|
|
payload.setContent(Base64.encodeToString(ciphertext,Base64.DEFAULT));
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-09 12:23:17 +00:00
|
|
|
public XmppAxolotlPlaintextMessage decrypt(AxolotlService.XmppAxolotlSession session, byte[] key, String fingerprint) {
|
2015-06-26 13:41:02 +00:00
|
|
|
XmppAxolotlPlaintextMessage plaintextMessage = null;
|
|
|
|
try {
|
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
|
|
|
|
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
|
|
|
|
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
|
|
|
|
| BadPaddingException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
return plaintextMessage;
|
|
|
|
}
|
2015-05-29 09:17:26 +00:00
|
|
|
}
|