mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-02-17 21:06:48 +01:00
[Huawei] Change bond request behaviour
This commit is contained in:
parent
05c11cbd14
commit
bb5fe00643
@ -226,8 +226,9 @@ public class HuaweiCrypto {
|
|||||||
return Arrays.copyOfRange(finalMixedKeyHash, 0, 16);
|
return Arrays.copyOfRange(finalMixedKeyHash, 0, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] encryptBondingKey(byte[] data, String mac, byte[] iv) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IllegalArgumentException {
|
public byte[] encryptBondingKey(byte encryptMethod, byte[] data, byte[] encryptionKey, byte[] iv) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IllegalArgumentException {
|
||||||
byte[] encryptionKey = createSecretKey(mac);
|
if (encryptMethod == 0x01)
|
||||||
|
return CryptoUtils.encryptAES_GCM_NoPad(data, encryptionKey, iv, null);
|
||||||
return CryptoUtils.encryptAES_CBC_Pad(data, encryptionKey, iv);
|
return CryptoUtils.encryptAES_CBC_Pad(data, encryptionKey, iv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,26 +469,21 @@ public class DeviceConfig {
|
|||||||
public Request(
|
public Request(
|
||||||
ParamsProvider paramsProvider,
|
ParamsProvider paramsProvider,
|
||||||
byte[] clientSerial,
|
byte[] clientSerial,
|
||||||
String mac,
|
byte[] key,
|
||||||
HuaweiCrypto huaweiCrypto
|
byte[] iv
|
||||||
) throws CryptoException {
|
) {
|
||||||
super(paramsProvider);
|
super(paramsProvider);
|
||||||
this.serviceId = DeviceConfig.id;
|
this.serviceId = DeviceConfig.id;
|
||||||
this.commandId = id;
|
this.commandId = id;
|
||||||
byte[] iv = paramsProvider.getIv();
|
|
||||||
|
|
||||||
try {
|
this.tlv = new HuaweiTLV()
|
||||||
this.tlv = new HuaweiTLV()
|
.put(0x01)
|
||||||
.put(0x01)
|
.put(0x03, (byte) 0x00)
|
||||||
.put(0x03, (byte) 0x00)
|
.put(0x05, clientSerial)
|
||||||
.put(0x05, clientSerial)
|
.put(0x06, key)
|
||||||
.put(0x06, huaweiCrypto.encryptBondingKey(paramsProvider.getSecretKey(), mac, iv))
|
.put(0x07, iv);
|
||||||
.put(0x07, iv);
|
this.isEncrypted = false;
|
||||||
this.isEncrypted = false;
|
this.complete = true;
|
||||||
this.complete = true;
|
|
||||||
} catch (InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | BadPaddingException | InvalidKeyException e) {
|
|
||||||
throw new CryptoException("Bonding key creation exception", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,11 +19,20 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.crypto.BadPaddingException;
|
||||||
|
import javax.crypto.IllegalBlockSizeException;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiCrypto;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
|
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.DeviceConfig;
|
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.DeviceConfig;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
|
||||||
public class GetBondRequest extends Request {
|
public class GetBondRequest extends Request {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(GetBondRequest.class);
|
private static final Logger LOG = LoggerFactory.getLogger(GetBondRequest.class);
|
||||||
@ -39,14 +48,25 @@ public class GetBondRequest extends Request {
|
|||||||
@Override
|
@Override
|
||||||
protected List<byte[]> createRequest() throws RequestCreationException {
|
protected List<byte[]> createRequest() throws RequestCreationException {
|
||||||
try {
|
try {
|
||||||
|
byte[] iv = paramsProvider.getIv();
|
||||||
|
huaweiCrypto = new HuaweiCrypto(paramsProvider.getAuthVersion());
|
||||||
|
byte[] encryptionKey;
|
||||||
|
if (paramsProvider.getDeviceSupportType() == 0x02) { //HiChainLite
|
||||||
|
encryptionKey = paramsProvider.getFirstKey();
|
||||||
|
} else {
|
||||||
|
encryptionKey = huaweiCrypto.createSecretKey(supportProvider.getDeviceMac());
|
||||||
|
}
|
||||||
|
byte[] key = huaweiCrypto.encryptBondingKey(paramsProvider.getEncryptMethod(), paramsProvider.getSecretKey(), encryptionKey, iv);
|
||||||
|
LOG.debug("key: " + GB.hexdump(key));
|
||||||
return new DeviceConfig.Bond.Request(
|
return new DeviceConfig.Bond.Request(
|
||||||
paramsProvider,
|
paramsProvider,
|
||||||
supportProvider.getSerial(),
|
supportProvider.getSerial(),
|
||||||
supportProvider.getDeviceMac(),
|
key,
|
||||||
huaweiCrypto
|
iv
|
||||||
).serialize();
|
).serialize();
|
||||||
} catch (HuaweiPacket.CryptoException e) {
|
} catch (HuaweiPacket.CryptoException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | IllegalBlockSizeException |
|
||||||
throw new RequestCreationException(e);
|
BadPaddingException e) {
|
||||||
|
throw new RequestCreationException(e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user