use of im.actor.core.network.mtp.entity.EncryptedCBCPackage in project actor-platform by actorapp.
the class ManagerActor method onInMessage.
@AutoreleasePool
private void onInMessage(byte[] data, int offset, int len) {
// Log.d(TAG, "Received package");
DataInput bis = new DataInput(data, offset, len);
try {
long authId = bis.readLong();
long sessionId = bis.readLong();
if (authId != this.authId || sessionId != this.sessionId) {
throw new IOException("Incorrect header");
}
if (authKey != null) {
EncryptedPackage encryptedPackage = new EncryptedPackage(bis);
int seq = (int) encryptedPackage.getSeqNumber();
if (seq != inSeq) {
throw new IOException("Expected " + inSeq + ", got: " + seq);
}
inSeq++;
// long start = Runtime.getActorTime();
EncryptedCBCPackage usEncryptedPackage = new EncryptedCBCPackage(new DataInput(encryptedPackage.getEncryptedPackage()));
byte[] ruPackage = serverUSDecryptor.decryptPackage(ByteStrings.longToBytes(seq), usEncryptedPackage.getIv(), usEncryptedPackage.getEncryptedContent());
EncryptedCBCPackage ruEncryptedPackage = new EncryptedCBCPackage(new DataInput(ruPackage));
byte[] plainText = serverRUDecryptor.decryptPackage(ByteStrings.longToBytes(seq), ruEncryptedPackage.getIv(), ruEncryptedPackage.getEncryptedContent());
// Log.d(TAG, "Package decrypted in " + (Runtime.getActorTime() - start) + " ms, size: " + len);
DataInput ptInput = new DataInput(plainText);
long messageId = ptInput.readLong();
byte[] ptPayload = ptInput.readProtoBytes();
receiver.send(new ProtoMessage(messageId, ptPayload));
} else {
long messageId = bis.readLong();
byte[] payload = bis.readProtoBytes();
receiver.send(new ProtoMessage(messageId, payload));
}
} catch (IOException e) {
Log.w(TAG, "Closing connection: incorrect package");
Log.e(TAG, e);
if (currentConnection != null) {
try {
currentConnection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
currentConnection = null;
currentConnectionId = 0;
outSeq = 0;
inSeq = 0;
// Log.d(TAG, "Set connection #" + 0);
}
checkConnection();
}
}
use of im.actor.core.network.mtp.entity.EncryptedCBCPackage in project actor-platform by actorapp.
the class ManagerActor method onOutMessage.
private void onOutMessage(byte[] data, int offset, int len) {
// Cleanup bad connection
if (currentConnection != null && currentConnection.isClosed()) {
currentConnection = null;
// Log.d(TAG, "Set connection #" + 0);
currentConnectionId = 0;
outSeq = 0;
inSeq = 0;
checkConnection();
}
try {
if (currentConnection != null) {
if (authKey != null) {
int seq = outSeq++;
// long start = Runtime.getActorTime();
byte[] ruIv = new byte[16];
Crypto.nextBytes(ruIv);
byte[] usIv = new byte[16];
Crypto.nextBytes(usIv);
byte[] ruCipherText = clientRUEncryptor.encryptPackage(ByteStrings.longToBytes(seq), ruIv, ByteStrings.substring(data, offset, len));
byte[] ruPackage = new EncryptedCBCPackage(ruIv, ruCipherText).toByteArray();
byte[] usCipherText = clientUSEncryptor.encryptPackage(ByteStrings.longToBytes(seq), usIv, ruPackage);
byte[] usPackage = new EncryptedCBCPackage(usIv, usCipherText).toByteArray();
EncryptedPackage encryptedPackage = new EncryptedPackage(seq, usPackage);
byte[] cipherData = encryptedPackage.toByteArray();
DataOutput bos = new DataOutput();
bos.writeLong(authId);
bos.writeLong(sessionId);
bos.writeBytes(cipherData, 0, cipherData.length);
byte[] pkg = bos.toByteArray();
currentConnection.post(pkg, 0, pkg.length);
// Log.d(TAG, "Package encrypted in " + (Runtime.getActorTime() - start) + " ms, size: " + len);
} else {
DataOutput bos = new DataOutput();
bos.writeLong(authId);
bos.writeLong(sessionId);
bos.writeBytes(data, offset, len);
byte[] pkg = bos.toByteArray();
currentConnection.post(pkg, 0, pkg.length);
}
// Log.d(TAG, "Posted message to connection #" + currentConnectionId);
}
} catch (IOException e) {
Log.w(TAG, "Closing connection: exception during push");
Log.e(TAG, e);
if (currentConnection != null) {
try {
currentConnection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
currentConnection = null;
currentConnectionId = 0;
outSeq = 0;
inSeq = 0;
// Log.d(TAG, "Set connection #" + 0);
}
checkConnection();
}
}
Aggregations