2024-01-10 19:25:13 +01:00
|
|
|
/* Copyright (C) 2024 Damien Gaignon, Martin.JM
|
2024-01-07 23:18:08 +01:00
|
|
|
|
|
|
|
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
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2024-01-07 23:18:08 +01:00
|
|
|
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;
|
2024-01-07 23:18:08 +01:00
|
|
|
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;
|
2024-01-07 23:18:08 +01:00
|
|
|
protected byte[] doubleNonce;
|
|
|
|
protected byte[] key = null;
|
2024-01-29 21:51:45 +01:00
|
|
|
protected byte authMode;
|
2024-01-07 23:18:08 +01:00
|
|
|
|
|
|
|
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();
|
2024-01-07 23:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<byte[]> createRequest() throws RequestCreationException {
|
2024-01-29 21:51:45 +01:00
|
|
|
huaweiCrypto = new HuaweiCrypto(authVersion, authAlgo, authMode);
|
2024-01-07 23:18:08 +01:00
|
|
|
byte[] nonce;
|
|
|
|
|
|
|
|
try {
|
2024-01-29 21:51:45 +01:00
|
|
|
if (authMode == 0x02) {
|
2024-01-07 23:18:08 +01:00
|
|
|
key = paramsProvider.getPinCode();
|
|
|
|
if (authVersion == 0x02)
|
|
|
|
key = paramsProvider.getSecretKey();
|
|
|
|
}
|
2024-01-29 21:21:20 +01:00
|
|
|
nonce = ByteBuffer.allocate(18)
|
|
|
|
.putShort(authVersion)
|
|
|
|
.put(clientNonce)
|
|
|
|
.array();
|
2024-01-07 23:18:08 +01:00
|
|
|
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();
|
2024-01-07 23:18:08 +01:00
|
|
|
} catch (HuaweiPacket.CryptoException e) {
|
|
|
|
throw new RequestCreationException(e);
|
2024-01-29 21:51:45 +01:00
|
|
|
} catch (NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException e) {
|
2024-01-07 23:18:08 +01:00
|
|
|
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) {
|
2024-01-07 23:18:08 +01:00
|
|
|
throw new ResponseParseException("Challenge response digest exception");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|