Search in sources :

Example 21 with ByteBuf

use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.

the class AuthLayerMessage method deserialize.

public static AuthLayerMessage deserialize(ByteBuf data, BigInteger lastNonce, byte[] key) throws IllegalAccessException, InstantiationException, SightError {
    // Preamble
    data.shift(4);
    int packetLength = data.readUInt16LE();
    // Packet length XOR
    data.shift(2);
    byte[] crcContent = data.getBytes(packetLength - 10);
    byte[] header = data.getBytes(21);
    byte version = data.readByte();
    byte command = data.readByte();
    Class clazz = MESSAGES.get(command);
    if (clazz == null)
        throw new UnknownAuthMessageError(command);
    int dataLength = data.readUInt16LE();
    long commID = data.readUInt32LE();
    byte[] nonceTrailer = data.getBytes(13);
    byte[] nonce = data.readBytesLE(13);
    byte[] payload = data.readBytes(dataLength);
    byte[] trailer = data.readBytes(8);
    boolean crcPacket = CRCAuthLayerMessage.class.isAssignableFrom(clazz);
    BigInteger nonceInt = new BigInteger(nonce);
    if (version != VERSION) {
        throw new InvalidAuthVersionError(version, VERSION);
    } else if (lastNonce != null && lastNonce.equals(BigInteger.ZERO) && nonceInt.compareTo(lastNonce) != 1) {
        throw new InvalidNonceError(nonce, processNonce(lastNonce.add(BigInteger.ONE)));
    } else {
        if (crcPacket) {
            byte[] crcBytes = new byte[2];
            byte[] rawData = new byte[dataLength - 2];
            System.arraycopy(payload, dataLength - 2, crcBytes, 0, 2);
            System.arraycopy(payload, 0, rawData, 0, dataLength - 2);
            payload = rawData;
            int crc = (crcBytes[0] & 0xFF | (crcBytes[1] & 0xFF) << 8);
            int calculatedCRC = Cryptograph.calculateCRC(crcContent);
            if (crc != calculatedCRC)
                throw new InvalidAuthCRCError(crc, calculatedCRC);
        } else {
            payload = Cryptograph.encryptDataCTR(payload, key, nonceTrailer);
            byte[] calculatedTrailer = Cryptograph.produceCCMTag(nonceTrailer, payload, header, key);
            if (!Arrays.equals(trailer, calculatedTrailer))
                throw new InvalidTrailerError(trailer, calculatedTrailer);
        }
        AuthLayerMessage message = (AuthLayerMessage) clazz.newInstance();
        message.nonce = nonceInt;
        message.commID = commID;
        ByteBuf byteBuf = new ByteBuf(payload.length);
        byteBuf.putBytes(payload);
        message.parse(byteBuf);
        return message;
    }
}
Also used : UnknownAuthMessageError(sugar.free.sightparser.error.UnknownAuthMessageError) InvalidAuthVersionError(sugar.free.sightparser.error.InvalidAuthVersionError) InvalidNonceError(sugar.free.sightparser.error.InvalidNonceError) ByteBuf(sugar.free.sightparser.pipeline.ByteBuf) SuppressLint(android.annotation.SuppressLint) InvalidAuthCRCError(sugar.free.sightparser.error.InvalidAuthCRCError) BigInteger(java.math.BigInteger) InvalidTrailerError(sugar.free.sightparser.error.InvalidTrailerError)

Example 22 with ByteBuf

use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.

the class AuthLayerMessage method serialize.

public ByteBuf serialize(BigInteger nonce, long commID, byte[] key) {
    byte[] data = getData();
    byte[] nonceBytes = processNonce(nonce);
    byte[] dataEncrypted = Cryptograph.encryptDataCTR(data, key, nonceBytes);
    int dataLength = dataEncrypted.length;
    int length = (short) (29 + dataLength);
    ByteBuf byteBuf = new ByteBuf(length + 8);
    byteBuf.putBytes(MAGIC_HEADER);
    byteBuf.putUInt16LE(length);
    byteBuf.putUInt16LE(~length);
    byteBuf.putByte(VERSION);
    byteBuf.putByte(getCommand());
    byteBuf.putUInt16LE(dataLength);
    byteBuf.putUInt32LE(commID);
    byteBuf.putBytes(nonceBytes);
    byteBuf.putBytes(dataEncrypted);
    byteBuf.putBytes(Cryptograph.produceCCMTag(byteBuf.getBytes(16, 13), data, byteBuf.getBytes(8, 21), key));
    return byteBuf;
}
Also used : ByteBuf(sugar.free.sightparser.pipeline.ByteBuf) SuppressLint(android.annotation.SuppressLint)

Example 23 with ByteBuf

use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.

the class AuthLayerMessage method processNonce.

static byte[] processNonce(BigInteger nonce) {
    byte[] bytes = nonce.toByteArray();
    ByteBuf byteBuf = new ByteBuf(13);
    byteBuf.putBytesLE(bytes);
    byteBuf.putBytes((byte) 0x00, 13 - bytes.length);
    return byteBuf.getBytes();
}
Also used : ByteBuf(sugar.free.sightparser.pipeline.ByteBuf)

Example 24 with ByteBuf

use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.

the class WriteDateTimeMessage method getData.

@Override
protected byte[] getData() throws Exception {
    ByteBuf byteBuf = new ByteBuf(7);
    byteBuf.putUInt16LE(year);
    byteBuf.putByte((byte) month);
    byteBuf.putByte((byte) day);
    byteBuf.putByte((byte) hour);
    byteBuf.putByte((byte) minute);
    byteBuf.putByte((byte) second);
    return byteBuf.getBytes();
}
Also used : ByteBuf(sugar.free.sightparser.pipeline.ByteBuf)

Example 25 with ByteBuf

use of sugar.free.sightparser.pipeline.ByteBuf in project SightRemote by TebbeUbben.

the class ActivateServiceMessage method getData.

@Override
protected byte[] getData() {
    ByteBuf byteBuf = new ByteBuf(19);
    byteBuf.putByte(serviceID);
    byteBuf.putShort(version);
    byteBuf.putBytes(servicePassword);
    return byteBuf.getBytes();
}
Also used : ByteBuf(sugar.free.sightparser.pipeline.ByteBuf)

Aggregations

ByteBuf (sugar.free.sightparser.pipeline.ByteBuf)35 SuppressLint (android.annotation.SuppressLint)3 InvalidAuthCRCError (sugar.free.sightparser.error.InvalidAuthCRCError)2 InvalidNonceError (sugar.free.sightparser.error.InvalidNonceError)2 InvalidTrailerError (sugar.free.sightparser.error.InvalidTrailerError)2 CustomEvent (com.crashlytics.android.answers.CustomEvent)1 BigInteger (java.math.BigInteger)1 HistoryFrame (sugar.free.sightparser.applayer.descriptors.history_frames.HistoryFrame)1 AppLayerMessage (sugar.free.sightparser.applayer.messages.AppLayerMessage)1 AuthLayerMessage (sugar.free.sightparser.authlayer.AuthLayerMessage)1 CRCAuthLayerMessage (sugar.free.sightparser.authlayer.CRCAuthLayerMessage)1 DataMessage (sugar.free.sightparser.authlayer.DataMessage)1 InvalidAppCRCError (sugar.free.sightparser.error.InvalidAppCRCError)1 InvalidAppVersionError (sugar.free.sightparser.error.InvalidAppVersionError)1 InvalidAuthVersionError (sugar.free.sightparser.error.InvalidAuthVersionError)1 UnknownAppErrorCodeError (sugar.free.sightparser.error.UnknownAppErrorCodeError)1 UnknownAppMessageError (sugar.free.sightparser.error.UnknownAppMessageError)1 UnknownAuthMessageError (sugar.free.sightparser.error.UnknownAuthMessageError)1 UnknownServiceError (sugar.free.sightparser.error.UnknownServiceError)1