use of net.java.otr4j.io.OtrInputStream in project Zom-Android by zom.
the class SessionImpl method handleDataMessage.
private synchronized String handleDataMessage(DataMessage data, List<TLV> tlvs) throws OtrException {
if (DEBUG_ENABLED)
Log.d(LOG_TAG, getSessionID().getLocalUserId() + " received a data message from " + getSessionID().getRemoteUserId() + ".");
switch(this.getSessionStatus()) {
case ENCRYPTED:
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Message state is ENCRYPTED. Trying to decrypt message.");
// Find matching session keys.
int senderKeyID = data.senderKeyID;
int receipientKeyID = data.recipientKeyID;
SessionKeys matchingKeys = this.getSessionKeysByID(receipientKeyID, senderKeyID);
if (matchingKeys == null) {
throw new OtrException("no matching keys found");
}
// Verify received MAC with a locally calculated MAC.
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Transforming T to byte[] to calculate it's HmacSHA1.");
byte[] serializedT;
try {
serializedT = SerializationUtils.toByteArray(data.getT());
} catch (IOException e) {
throw new OtrException(e);
}
OtrCryptoEngine otrCryptoEngine = new OtrCryptoEngineImpl();
byte[] computedMAC = otrCryptoEngine.sha1Hmac(serializedT, matchingKeys.getReceivingMACKey(), SerializationConstants.TYPE_LEN_MAC);
String decryptedMsgContent = null;
if (!Arrays.equals(computedMAC, data.mac)) {
// throw new OtrException("MAC verification failed, ignoring message");
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "MAC verification failed, ignoring message");
} else {
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Computed HmacSHA1 value matches sent one.");
// Mark this MAC key as old to be revealed.
matchingKeys.setIsUsedReceivingMACKey(true);
matchingKeys.setReceivingCtr(data.ctr);
byte[] dmc = otrCryptoEngine.aesDecrypt(matchingKeys.getReceivingAESKey(), matchingKeys.getReceivingCtr(), data.encryptedMessage);
try {
// Expect bytes to be text encoded in UTF-8.
decryptedMsgContent = new String(dmc, "UTF-8");
if (DEBUG_ENABLED)
Log.d(LOG_TAG, "Decrypted message: \"" + decryptedMsgContent + "\"");
// Handle TLVs
if (tlvs == null) {
tlvs = new ArrayList<TLV>();
}
int tlvIndex = decryptedMsgContent.indexOf((char) 0x0);
if (tlvIndex > -1) {
decryptedMsgContent = decryptedMsgContent.substring(0, tlvIndex);
tlvIndex++;
byte[] tlvsb = new byte[dmc.length - tlvIndex];
System.arraycopy(dmc, tlvIndex, tlvsb, 0, tlvsb.length);
ByteArrayInputStream tin = new ByteArrayInputStream(tlvsb);
while (tin.available() > 0) {
int type;
byte[] tdata;
OtrInputStream eois = new OtrInputStream(tin);
try {
type = eois.readShort();
tdata = eois.readTlvData();
eois.close();
} catch (IOException e) {
throw new OtrException(e);
}
tlvs.add(new TLV(type, tdata));
}
}
if (tlvs.size() > 0) {
for (TLV tlv : tlvs) {
switch(tlv.getType()) {
case TLV.DISCONNECTED:
this.setSessionStatus(SessionStatus.FINISHED);
return null;
default:
for (OtrTlvHandler handler : tlvHandlers) {
handler.processTlv(tlv);
}
}
}
}
} catch (UnsupportedEncodingException e) {
// throw new OtrException(e);
if (DEBUG_ENABLED)
Log.e(LOG_TAG, "unsupported encoding exception", e);
}
}
// Rotate keys if necessary.
SessionKeys mostRecent = this.getMostRecentSessionKeys();
if (mostRecent.getLocalKeyID() == receipientKeyID)
this.rotateLocalSessionKeys();
if (mostRecent.getRemoteKeyID() == senderKeyID)
this.rotateRemoteSessionKeys(data.nextDH);
return decryptedMsgContent;
case FINISHED:
case PLAINTEXT:
showError("Unreadable encrypted message was received.");
refreshSession();
// "You sent me an unreadable encrypted message"));
throw new OtrException("Unreadable encrypted message received");
}
return null;
}
use of net.java.otr4j.io.OtrInputStream in project Zom-Android by zom.
the class IOTest method testIOShort.
public void testIOShort() throws Exception {
int source = 10;
ByteArrayOutputStream out = new ByteArrayOutputStream();
OtrOutputStream oos = new OtrOutputStream(out);
oos.writeShort(source);
oos.close();
byte[] converted = out.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(converted);
OtrInputStream ois = new OtrInputStream(bin);
int result = ois.readShort();
ois.close();
assertEquals(source, result);
}
use of net.java.otr4j.io.OtrInputStream in project Zom-Android by zom.
the class IOTest method testIODHPublicKey.
public void testIODHPublicKey() throws Exception {
KeyPair pair = new OtrCryptoEngineImpl().generateDHKeyPair();
DHPublicKey source = (DHPublicKey) pair.getPublic();
ByteArrayOutputStream out = new ByteArrayOutputStream();
OtrOutputStream oos = new OtrOutputStream(out);
oos.writeDHPublicKey(source);
oos.close();
byte[] converted = out.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(converted);
OtrInputStream ois = new OtrInputStream(bin);
DHPublicKey result = ois.readDHPublicKey();
ois.close();
assertTrue(source.getY().compareTo(result.getY()) == 0);
}
use of net.java.otr4j.io.OtrInputStream in project Zom-Android by zom.
the class IOTest method testIOData.
public void testIOData() throws Exception {
byte[] source = new byte[] { 1, 1, 1, 1 };
ByteArrayOutputStream out = new ByteArrayOutputStream();
OtrOutputStream oos = new OtrOutputStream(out);
oos.writeData(source);
oos.close();
byte[] converted = out.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(converted);
OtrInputStream ois = new OtrInputStream(bin);
byte[] result = ois.readData();
ois.close();
assertTrue(java.util.Arrays.equals(source, result));
}
use of net.java.otr4j.io.OtrInputStream in project Zom-Android by zom.
the class IOTest method testIOBigInt.
public void testIOBigInt() throws Exception {
KeyPair pair = new OtrCryptoEngineImpl().generateDHKeyPair();
BigInteger source = ((DHPublicKey) pair.getPublic()).getY();
ByteArrayOutputStream out = new ByteArrayOutputStream();
OtrOutputStream oos = new OtrOutputStream(out);
oos.writeBigInt(source);
oos.close();
byte[] converted = out.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(converted);
OtrInputStream ois = new OtrInputStream(bin);
BigInteger result = ois.readBigInt();
ois.close();
assertTrue(source.compareTo(result) == 0);
}
Aggregations