1
0
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:
Damien 'Psolyca' Gaignon 2024-02-05 20:16:23 +01:00
parent 05c11cbd14
commit bb5fe00643
No known key found for this signature in database
GPG Key ID: 9E9404E5D9E11843
3 changed files with 38 additions and 22 deletions

View File

@ -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);
} }

View File

@ -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);
}
} }
} }

View File

@ -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());
} }
} }