Search in sources :

Example 1 with EncryptedPackage

use of im.actor.core.network.mtp.entity.EncryptedPackage 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();
    }
}
Also used : DataInput(im.actor.runtime.bser.DataInput) EncryptedPackage(im.actor.core.network.mtp.entity.EncryptedPackage) ProtoMessage(im.actor.core.network.mtp.entity.ProtoMessage) EncryptedCBCPackage(im.actor.core.network.mtp.entity.EncryptedCBCPackage) IOException(java.io.IOException) IOException(java.io.IOException) AutoreleasePool(com.google.j2objc.annotations.AutoreleasePool)

Example 2 with EncryptedPackage

use of im.actor.core.network.mtp.entity.EncryptedPackage 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();
    }
}
Also used : DataOutput(im.actor.runtime.bser.DataOutput) EncryptedPackage(im.actor.core.network.mtp.entity.EncryptedPackage) EncryptedCBCPackage(im.actor.core.network.mtp.entity.EncryptedCBCPackage) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

EncryptedCBCPackage (im.actor.core.network.mtp.entity.EncryptedCBCPackage)2 EncryptedPackage (im.actor.core.network.mtp.entity.EncryptedPackage)2 IOException (java.io.IOException)2 AutoreleasePool (com.google.j2objc.annotations.AutoreleasePool)1 ProtoMessage (im.actor.core.network.mtp.entity.ProtoMessage)1 DataInput (im.actor.runtime.bser.DataInput)1 DataOutput (im.actor.runtime.bser.DataOutput)1