diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/FileDownloadService0A.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/FileDownloadService0A.java index e06db2695..29d551609 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/FileDownloadService0A.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/packets/FileDownloadService0A.java @@ -16,6 +16,7 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets; +import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiCrypto; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiTLV; @@ -236,16 +237,28 @@ public class FileDownloadService0A { public byte number; public byte[] data; + public Exception e = null; + public BlockResponse(ParamsProvider paramsProvider) { super(paramsProvider); } @Override public void parseTlv() throws ParseException { - // Note that this packet does not contain TLV data - this.number = this.payload[2]; - this.data = new byte[this.payload.length - 3]; - System.arraycopy(this.payload, 3, this.data, 0, this.payload.length - 3); + int offset; + // Try to decrypt TLV, and otherwise assume the data is not encrypted + try { + HuaweiTLV tlv = new HuaweiTLV().parse(this.payload, 2 ,this.payload.length - 2); + this.payload = tlv.decryptRaw(paramsProvider); + offset = 0; + } catch (ArrayIndexOutOfBoundsException | HuaweiCrypto.CryptoException e) { + this.e = e; + offset = 2; + } + + this.number = this.payload[offset]; + this.data = new byte[this.payload.length - offset - 1]; + System.arraycopy(this.payload, offset + 1, this.data, 0, this.payload.length - offset - 1); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiFileDownloadManager.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiFileDownloadManager.java index bdeb3f0c7..8c855ec5d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiFileDownloadManager.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiFileDownloadManager.java @@ -291,9 +291,14 @@ public class HuaweiFileDownloadManager { @Override protected void processResponse() throws ResponseParseException { if (this.receivedPacket instanceof FileDownloadService0A.BlockResponse) { + FileDownloadService0A.BlockResponse response = (FileDownloadService0A.BlockResponse) this.receivedPacket; this.newSync = false; - this.number = ((FileDownloadService0A.BlockResponse) this.receivedPacket).number; - this.data = ((FileDownloadService0A.BlockResponse) this.receivedPacket).data; + this.number = response.number; + this.data = response.data; + + if (response.e instanceof HuaweiPacket.CryptoException) { + LOG.warn("Data could be decoded as TLV, but not decrypted.", response.e); + } } else if (this.receivedPacket instanceof FileDownloadService2C.BlockResponse) { this.newSync = true; this.number = ((FileDownloadService2C.BlockResponse) this.receivedPacket).fileId; @@ -629,7 +634,13 @@ public class HuaweiFileDownloadManager { } // Handle file data - currentFileRequest.fileDownloadCallback.downloadComplete(currentFileRequest); + try { + currentFileRequest.fileDownloadCallback.downloadComplete(currentFileRequest); + } catch (Exception e) { + LOG.error("Download complete callback exception.", e); + LOG.warn("File contents: {}", GB.hexdump(currentFileRequest.getData())); + GB.toast("Workout GPX file could not be parsed.",Toast.LENGTH_SHORT, GB.ERROR, e); + } if (!this.currentFileRequest.newSync && !this.fileRequests.isEmpty() && !this.fileRequests.get(0).newSync) { // Old sync can potentially take a shortcut