mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 02:20:39 +00:00
Extract pre key generation to KeyUtils
This commit is contained in:
parent
bc47c0d5d6
commit
c9fa28d844
2 changed files with 62 additions and 59 deletions
|
@ -74,12 +74,9 @@ import org.whispersystems.libsignal.IdentityKeyPair;
|
|||
import org.whispersystems.libsignal.InvalidKeyException;
|
||||
import org.whispersystems.libsignal.InvalidMessageException;
|
||||
import org.whispersystems.libsignal.InvalidVersionException;
|
||||
import org.whispersystems.libsignal.ecc.Curve;
|
||||
import org.whispersystems.libsignal.ecc.ECKeyPair;
|
||||
import org.whispersystems.libsignal.ecc.ECPublicKey;
|
||||
import org.whispersystems.libsignal.state.PreKeyRecord;
|
||||
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
||||
import org.whispersystems.libsignal.util.Medium;
|
||||
import org.whispersystems.libsignal.util.Pair;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
import org.whispersystems.signalservice.api.KeyBackupService;
|
||||
|
@ -299,21 +296,15 @@ public class Manager implements Closeable {
|
|||
}
|
||||
|
||||
public void checkAccountState() throws IOException {
|
||||
if (account.isRegistered()) {
|
||||
if (accountManager.getPreKeysCount() < ServiceConfig.PREKEY_MINIMUM_COUNT) {
|
||||
refreshPreKeys();
|
||||
account.save();
|
||||
}
|
||||
if (account.getUuid() == null) {
|
||||
account.setUuid(accountManager.getOwnUuid());
|
||||
account.save();
|
||||
}
|
||||
updateAccountAttributes();
|
||||
if (accountManager.getPreKeysCount() < ServiceConfig.PREKEY_MINIMUM_COUNT) {
|
||||
refreshPreKeys();
|
||||
account.save();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRegistered() {
|
||||
return account.isRegistered();
|
||||
if (account.getUuid() == null) {
|
||||
account.setUuid(accountManager.getOwnUuid());
|
||||
account.save();
|
||||
}
|
||||
updateAccountAttributes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -396,43 +387,6 @@ public class Manager implements Closeable {
|
|||
account.save();
|
||||
}
|
||||
|
||||
private List<PreKeyRecord> generatePreKeys() {
|
||||
List<PreKeyRecord> records = new ArrayList<>(ServiceConfig.PREKEY_BATCH_SIZE);
|
||||
|
||||
final int offset = account.getPreKeyIdOffset();
|
||||
for (int i = 0; i < ServiceConfig.PREKEY_BATCH_SIZE; i++) {
|
||||
int preKeyId = (offset + i) % Medium.MAX_VALUE;
|
||||
ECKeyPair keyPair = Curve.generateKeyPair();
|
||||
PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
|
||||
|
||||
records.add(record);
|
||||
}
|
||||
|
||||
account.addPreKeys(records);
|
||||
account.save();
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
|
||||
try {
|
||||
ECKeyPair keyPair = Curve.generateKeyPair();
|
||||
byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(),
|
||||
keyPair.getPublicKey().serialize());
|
||||
SignedPreKeyRecord record = new SignedPreKeyRecord(account.getNextSignedPreKeyId(),
|
||||
System.currentTimeMillis(),
|
||||
keyPair,
|
||||
signature);
|
||||
|
||||
account.addSignedPreKey(record);
|
||||
account.save();
|
||||
|
||||
return record;
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setRegistrationLockPin(Optional<String> pin) throws IOException, UnauthenticatedResponseException {
|
||||
if (pin.isPresent()) {
|
||||
final MasterKey masterKey = account.getPinMasterKey() != null
|
||||
|
@ -464,6 +418,26 @@ public class Manager implements Closeable {
|
|||
accountManager.setPreKeys(identityKeyPair.getPublicKey(), signedPreKeyRecord, oneTimePreKeys);
|
||||
}
|
||||
|
||||
private List<PreKeyRecord> generatePreKeys() {
|
||||
final int offset = account.getPreKeyIdOffset();
|
||||
|
||||
List<PreKeyRecord> records = KeyUtils.generatePreKeyRecords(offset, ServiceConfig.PREKEY_BATCH_SIZE);
|
||||
account.addPreKeys(records);
|
||||
account.save();
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
|
||||
final int signedPreKeyId = account.getNextSignedPreKeyId();
|
||||
|
||||
SignedPreKeyRecord record = KeyUtils.generateSignedPreKeyRecord(identityKeyPair, signedPreKeyId);
|
||||
account.addSignedPreKey(record);
|
||||
account.save();
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
private SignalServiceMessagePipe getOrCreateMessagePipe() {
|
||||
if (messagePipe == null) {
|
||||
messagePipe = messageReceiver.createMessagePipe();
|
||||
|
@ -496,10 +470,6 @@ public class Manager implements Closeable {
|
|||
ServiceConfig.MAX_ENVELOPE_SIZE);
|
||||
}
|
||||
|
||||
private SignalServiceProfile getEncryptedRecipientProfile(SignalServiceAddress address) throws IOException {
|
||||
return profileHelper.retrieveProfileSync(address, SignalServiceProfile.RequestType.PROFILE).getProfile();
|
||||
}
|
||||
|
||||
private SignalProfile getRecipientProfile(
|
||||
SignalServiceAddress address
|
||||
) {
|
||||
|
@ -560,7 +530,8 @@ public class Manager implements Closeable {
|
|||
private SignalProfile retrieveRecipientProfile(
|
||||
SignalServiceAddress address, ProfileKey profileKey
|
||||
) throws IOException {
|
||||
final SignalServiceProfile encryptedProfile = getEncryptedRecipientProfile(address);
|
||||
final SignalServiceProfile encryptedProfile = profileHelper.retrieveProfileSync(address,
|
||||
SignalServiceProfile.RequestType.PROFILE).getProfile();
|
||||
|
||||
return decryptProfile(address, profileKey, encryptedProfile);
|
||||
}
|
||||
|
|
|
@ -5,12 +5,19 @@ import org.signal.zkgroup.InvalidInputException;
|
|||
import org.signal.zkgroup.profiles.ProfileKey;
|
||||
import org.whispersystems.libsignal.IdentityKey;
|
||||
import org.whispersystems.libsignal.IdentityKeyPair;
|
||||
import org.whispersystems.libsignal.InvalidKeyException;
|
||||
import org.whispersystems.libsignal.ecc.Curve;
|
||||
import org.whispersystems.libsignal.ecc.ECKeyPair;
|
||||
import org.whispersystems.libsignal.ecc.ECPrivateKey;
|
||||
import org.whispersystems.libsignal.state.PreKeyRecord;
|
||||
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
||||
import org.whispersystems.libsignal.util.Medium;
|
||||
import org.whispersystems.signalservice.api.kbs.MasterKey;
|
||||
import org.whispersystems.util.Base64;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class KeyUtils {
|
||||
|
||||
private KeyUtils() {
|
||||
|
@ -24,6 +31,31 @@ public class KeyUtils {
|
|||
return new IdentityKeyPair(djbIdentityKey, djbPrivateKey);
|
||||
}
|
||||
|
||||
public static List<PreKeyRecord> generatePreKeyRecords(final int offset, final int batchSize) {
|
||||
List<PreKeyRecord> records = new ArrayList<>(batchSize);
|
||||
for (int i = 0; i < batchSize; i++) {
|
||||
int preKeyId = (offset + i) % Medium.MAX_VALUE;
|
||||
ECKeyPair keyPair = Curve.generateKeyPair();
|
||||
PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
|
||||
|
||||
records.add(record);
|
||||
}
|
||||
return records;
|
||||
}
|
||||
|
||||
public static SignedPreKeyRecord generateSignedPreKeyRecord(
|
||||
final IdentityKeyPair identityKeyPair, final int signedPreKeyId
|
||||
) {
|
||||
ECKeyPair keyPair = Curve.generateKeyPair();
|
||||
byte[] signature;
|
||||
try {
|
||||
signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
return new SignedPreKeyRecord(signedPreKeyId, System.currentTimeMillis(), keyPair, signature);
|
||||
}
|
||||
|
||||
public static String createSignalingKey() {
|
||||
return getSecret(52);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue