1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 01:55:50 +01:00

Huawei: Fix for encrypted file downloads

Necessary for the Huawei Band 3 Pro GPS download.
This commit is contained in:
Martin.JM 2024-11-23 23:16:54 +01:00 committed by José Rebelo
parent 5e62bd3cc6
commit 48a6e1aa95
2 changed files with 31 additions and 7 deletions

View File

@ -16,6 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
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);
}
}

View File

@ -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
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