Skip to content

Commit

Permalink
Update libsignal-service-java
Browse files Browse the repository at this point in the history
Uses new device link endpoint

Fixes #1399
  • Loading branch information
AsamK committed Dec 15, 2023
1 parent 2210373 commit 727f90d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 33 deletions.
10 changes: 9 additions & 1 deletion graalvm-config-dir/reflect-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
{
"name":"java.lang.Record",
"allDeclaredFields":true,
"allDeclaredClasses":true,
"queryAllDeclaredMethods":true
},
{
Expand Down Expand Up @@ -297,7 +298,7 @@
},
{
"name":"java.lang.reflect.RecordComponent",
"methods":[{"name":"getName","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
"methods":[{"name":"getAccessor","parameterTypes":[] }, {"name":"getName","parameterTypes":[] }, {"name":"getType","parameterTypes":[] }]
},
{
"name":"java.net.NetPermission"
Expand Down Expand Up @@ -2436,6 +2437,13 @@
"name":"org.whispersystems.signalservice.internal.push.KyberPreKeyEntity$KEMPublicKeySerializer",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"org.whispersystems.signalservice.internal.push.LinkDeviceRequest",
"allDeclaredFields":true,
"allDeclaredClasses":true,
"queryAllDeclaredMethods":true,
"methods":[{"name":"accountAttributes","parameterTypes":[] }, {"name":"aciPqLastResortPreKey","parameterTypes":[] }, {"name":"aciSignedPreKey","parameterTypes":[] }, {"name":"pniPqLastResortPreKey","parameterTypes":[] }, {"name":"pniSignedPreKey","parameterTypes":[] }, {"name":"verificationCode","parameterTypes":[] }]
},
{
"name":"org.whispersystems.signalservice.internal.push.MismatchedDevices",
"allDeclaredFields":true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
import org.whispersystems.signalservice.api.util.DeviceNameUtil;
import org.whispersystems.signalservice.internal.push.ConfirmCodeMessage;
import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider;

import java.io.IOException;
Expand All @@ -45,7 +44,7 @@
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;

import static org.asamk.signal.manager.config.ServiceConfig.getCapabilities;
import static org.asamk.signal.manager.util.KeyUtils.generatePreKeysForType;

public class ProvisioningManagerImpl implements ProvisioningManager {

Expand Down Expand Up @@ -140,20 +139,21 @@ public String finishDeviceLink(String deviceName) throws IOException, TimeoutExc
encryptedDeviceName,
ret.getAciIdentity(),
ret.getPniIdentity(),
profileKey);
profileKey,
ret.getMasterKey());

account.getConfigurationStore().setReadReceipts(ret.isReadReceipts());

final var aciPreKeys = generatePreKeysForType(account.getAccountData(ServiceIdType.ACI));
final var pniPreKeys = generatePreKeysForType(account.getAccountData(ServiceIdType.PNI));

logger.debug("Finishing new device registration");
var deviceId = accountManager.finishNewDeviceRegistration(ret.getProvisioningCode(),
new ConfirmCodeMessage(false,
true,
account.getAccountData(ServiceIdType.ACI).getLocalRegistrationId(),
account.getAccountData(ServiceIdType.PNI).getLocalRegistrationId(),
encryptedDeviceName,
getCapabilities(false)));

account.finishLinking(deviceId);
account.getAccountAttributes(null),
aciPreKeys,
pniPreKeys);

account.finishLinking(deviceId, aciPreKeys, pniPreKeys);

ManagerImpl m = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import java.io.IOException;
import java.util.function.Consumer;

import static org.asamk.signal.manager.util.KeyUtils.generatePreKeysForType;

public class RegistrationManagerImpl implements RegistrationManager {

private final static Logger logger = LoggerFactory.getLogger(RegistrationManagerImpl.class);
Expand Down Expand Up @@ -133,8 +135,8 @@ public void verifyAccount(
account.setPniIdentityKeyPair(KeyUtils.generateIdentityKeyPair());
}

final var aciPreKeys = generatePreKeysForType(ServiceIdType.ACI);
final var pniPreKeys = generatePreKeysForType(ServiceIdType.PNI);
final var aciPreKeys = generatePreKeysForType(account.getAccountData(ServiceIdType.ACI));
final var pniPreKeys = generatePreKeysForType(account.getAccountData(ServiceIdType.PNI));
final var result = NumberVerificationUtils.verifyNumber(account.getSessionId(account.getNumber()),
verificationCode,
pin,
Expand Down Expand Up @@ -248,21 +250,6 @@ private VerifyAccountResponse verifyAccountWithCode(
true));
}

private PreKeyCollection generatePreKeysForType(ServiceIdType serviceIdType) {
final var accountData = account.getAccountData(serviceIdType);
final var keyPair = accountData.getIdentityKeyPair();
final var preKeyMetadata = accountData.getPreKeyMetadata();

final var nextSignedPreKeyId = preKeyMetadata.getNextSignedPreKeyId();
final var signedPreKey = KeyUtils.generateSignedPreKeyRecord(nextSignedPreKeyId, keyPair.getPrivateKey());

final var privateKey = keyPair.getPrivateKey();
final var kyberPreKeyIdOffset = preKeyMetadata.getNextKyberPreKeyId();
final var lastResortKyberPreKey = KeyUtils.generateKyberPreKeyRecord(kyberPreKeyIdOffset, privateKey);

return new PreKeyCollection(keyPair.getPublicKey(), signedPreKey, lastResortKyberPreKey);
}

@Override
public void close() {
if (account != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ public void setProvisioningData(
final String encryptedDeviceName,
final IdentityKeyPair aciIdentity,
final IdentityKeyPair pniIdentity,
final ProfileKey profileKey
final ProfileKey profileKey,
final MasterKey masterKey
) {
this.deviceId = 0;
this.number = number;
Expand All @@ -288,7 +289,7 @@ public void setProvisioningData(
this.registered = false;
this.isMultiDevice = true;
getKeyValueStore().storeEntry(lastReceiveTimestamp, 0L);
this.pinMasterKey = null;
this.pinMasterKey = masterKey;
getKeyValueStore().storeEntry(storageManifestVersion, -1L);
this.setStorageManifest(null);
this.storageKey = null;
Expand All @@ -302,9 +303,13 @@ public void setProvisioningData(
save();
}

public void finishLinking(final int deviceId) {
public void finishLinking(
final int deviceId, final PreKeyCollection aciPreKeys, final PreKeyCollection pniPreKeys
) {
this.registered = true;
this.deviceId = deviceId;
setPreKeys(ServiceIdType.ACI, aciPreKeys);
setPreKeys(ServiceIdType.PNI, pniPreKeys);
save();
}

Expand Down
16 changes: 16 additions & 0 deletions lib/src/main/java/org/asamk/signal/manager/util/KeyUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.asamk.signal.manager.util;

import org.asamk.signal.manager.storage.SignalAccount;
import org.signal.libsignal.protocol.IdentityKey;
import org.signal.libsignal.protocol.IdentityKeyPair;
import org.signal.libsignal.protocol.InvalidKeyException;
Expand All @@ -12,6 +13,7 @@
import org.signal.libsignal.protocol.state.SignedPreKeyRecord;
import org.signal.libsignal.zkgroup.InvalidInputException;
import org.signal.libsignal.zkgroup.profiles.ProfileKey;
import org.whispersystems.signalservice.api.account.PreKeyCollection;
import org.whispersystems.signalservice.api.kbs.MasterKey;

import java.security.SecureRandom;
Expand Down Expand Up @@ -125,4 +127,18 @@ public static byte[] getSecretBytes(int size) {
public static int getRandomInt(int bound) {
return secureRandom.nextInt(bound);
}

public static PreKeyCollection generatePreKeysForType(final SignalAccount.AccountData<?> accountData) {
final var keyPair = accountData.getIdentityKeyPair();
final var preKeyMetadata = accountData.getPreKeyMetadata();

final var nextSignedPreKeyId = preKeyMetadata.getNextSignedPreKeyId();
final var signedPreKey = generateSignedPreKeyRecord(nextSignedPreKeyId, keyPair.getPrivateKey());

final var privateKey = keyPair.getPrivateKey();
final var kyberPreKeyIdOffset = preKeyMetadata.getNextKyberPreKeyId();
final var lastResortKyberPreKey = generateKyberPreKeyRecord(kyberPreKeyIdOffset, privateKey);

return new PreKeyCollection(keyPair.getPublicKey(), signedPreKey, lastResortKyberPreKey);
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencyResolutionManagement {
library("logback", "ch.qos.logback", "logback-classic").version("1.4.11")


library("signalservice", "com.github.turasa", "signal-service-java").version("2.15.3_unofficial_89")
library("signalservice", "com.github.turasa", "signal-service-java").version("2.15.3_unofficial_90")
library("sqlite", "org.xerial", "sqlite-jdbc").version("3.44.0.0")
library("hikari", "com.zaxxer", "HikariCP").version("5.1.0")
library("junit.jupiter", "org.junit.jupiter", "junit-jupiter").version("5.10.1")
Expand Down

0 comments on commit 727f90d

Please sign in to comment.