1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-29 16:26:18 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetAuthRequest.java

110 lines
4.6 KiB
Java
Raw Normal View History

2024-01-10 19:25:13 +01:00
/* Copyright (C) 2024 Damien Gaignon, Martin.JM
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
2024-01-29 21:51:45 +01:00
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiCrypto;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.DeviceConfig;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class GetAuthRequest extends Request {
private static final Logger LOG = LoggerFactory.getLogger(GetAuthRequest.class);
protected final byte[] clientNonce;
protected short authVersion;
2024-01-29 21:51:45 +01:00
protected byte authAlgo;
protected byte[] doubleNonce;
protected byte[] key = null;
2024-01-29 21:51:45 +01:00
protected byte authMode;
public GetAuthRequest(HuaweiSupportProvider support,
Request linkParamsReq) {
super(support);
this.serviceId = DeviceConfig.id;
this.commandId = DeviceConfig.Auth.id;
this.clientNonce = HuaweiCrypto.generateNonce();
doubleNonce = ByteBuffer.allocate(32)
.put(((GetLinkParamsRequest)linkParamsReq).serverNonce)
.put(clientNonce)
.array();
this.authVersion = paramsProvider.getAuthVersion();
2024-01-29 21:51:45 +01:00
this.authAlgo = paramsProvider.getAuthAlgo();
this.authMode = paramsProvider.getAuthMode();
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
2024-01-29 21:51:45 +01:00
huaweiCrypto = new HuaweiCrypto(authVersion, authAlgo, authMode);
byte[] nonce;
try {
2024-01-29 21:51:45 +01:00
if (authMode == 0x02) {
key = paramsProvider.getPinCode();
if (authVersion == 0x02)
key = paramsProvider.getSecretKey();
}
nonce = ByteBuffer.allocate(18)
.putShort(authVersion)
.put(clientNonce)
.array();
byte[] challenge = huaweiCrypto.digestChallenge(key, doubleNonce);
if (challenge == null)
throw new RequestCreationException("Challenge null");
2024-01-29 21:51:45 +01:00
return new DeviceConfig.Auth.Request(paramsProvider, challenge, nonce).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);
2024-01-29 21:51:45 +01:00
} catch (NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException e) {
throw new RequestCreationException("Digest exception", e);
}
}
@Override
protected void processResponse() throws ResponseParseException {
LOG.debug("handle Auth");
if (!(receivedPacket instanceof DeviceConfig.Auth.Response))
throw new ResponseTypeMismatchException(receivedPacket, DeviceConfig.Auth.Response.class);
try {
byte[] expectedAnswer = huaweiCrypto.digestResponse(key, doubleNonce);
if (expectedAnswer == null)
throw new ResponseParseException("Challenge null");
byte[] actualAnswer = ((DeviceConfig.Auth.Response) receivedPacket).challengeResponse;
if (!Arrays.equals(expectedAnswer, actualAnswer)) {
throw new ResponseParseException("Challenge answer mismatch : "
+ StringUtils.bytesToHex(actualAnswer)
+ " != "
+ StringUtils.bytesToHex(expectedAnswer)
);
}
2024-01-29 21:51:45 +01:00
} catch (NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException e) {
throw new ResponseParseException("Challenge response digest exception");
}
}
}