2018-02-16 04:33:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2018 Open Whisper Systems
|
2013-11-10 12:15:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
package org.thoughtcrime.securesms.crypto;
|
2013-08-15 15:25:30 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
2013-11-10 12:15:29 +00:00
|
|
|
|
2019-06-04 02:12:40 +00:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore;
|
2018-02-16 04:33:10 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.IdentityKeyPair;
|
|
|
|
import org.whispersystems.libsignal.InvalidKeyException;
|
2019-06-04 00:10:41 +00:00
|
|
|
import org.whispersystems.libsignal.InvalidKeyIdException;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.ecc.Curve;
|
|
|
|
import org.whispersystems.libsignal.ecc.ECKeyPair;
|
|
|
|
import org.whispersystems.libsignal.state.PreKeyRecord;
|
|
|
|
import org.whispersystems.libsignal.state.PreKeyStore;
|
|
|
|
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
|
|
|
import org.whispersystems.libsignal.state.SignedPreKeyStore;
|
|
|
|
import org.whispersystems.libsignal.util.Medium;
|
2013-08-15 15:25:30 +00:00
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class PreKeyUtil {
|
|
|
|
|
2018-02-16 04:33:10 +00:00
|
|
|
@SuppressWarnings("unused")
|
2018-12-06 20:14:20 +00:00
|
|
|
private static final String TAG = PreKeyUtil.class.getSimpleName();
|
2017-01-06 17:19:58 +00:00
|
|
|
|
|
|
|
private static final int BATCH_SIZE = 100;
|
2013-08-15 15:25:30 +00:00
|
|
|
|
2018-02-16 04:33:10 +00:00
|
|
|
public synchronized static List<PreKeyRecord> generatePreKeys(Context context) {
|
2015-07-07 00:36:49 +00:00
|
|
|
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
|
2014-04-23 04:31:57 +00:00
|
|
|
List<PreKeyRecord> records = new LinkedList<>();
|
2018-02-16 04:33:10 +00:00
|
|
|
int preKeyIdOffset = TextSecurePreferences.getNextPreKeyId(context);
|
2013-08-15 15:25:30 +00:00
|
|
|
|
|
|
|
for (int i=0;i<BATCH_SIZE;i++) {
|
2013-08-19 17:07:07 +00:00
|
|
|
int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE;
|
2015-03-02 16:25:19 +00:00
|
|
|
ECKeyPair keyPair = Curve.generateKeyPair();
|
2014-04-24 22:39:55 +00:00
|
|
|
PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
|
2013-08-15 15:25:30 +00:00
|
|
|
|
2014-07-05 19:47:01 +00:00
|
|
|
preKeyStore.storePreKey(preKeyId, record);
|
2013-08-15 15:25:30 +00:00
|
|
|
records.add(record);
|
|
|
|
}
|
|
|
|
|
2018-02-16 04:33:10 +00:00
|
|
|
TextSecurePreferences.setNextPreKeyId(context, (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
|
|
|
|
|
2013-08-15 15:25:30 +00:00
|
|
|
return records;
|
|
|
|
}
|
|
|
|
|
2018-02-16 04:33:10 +00:00
|
|
|
public synchronized static SignedPreKeyRecord generateSignedPreKey(Context context, IdentityKeyPair identityKeyPair, boolean active) {
|
2014-07-05 19:47:01 +00:00
|
|
|
try {
|
2015-07-07 00:36:49 +00:00
|
|
|
SignedPreKeyStore signedPreKeyStore = new TextSecurePreKeyStore(context);
|
2018-02-16 04:33:10 +00:00
|
|
|
int signedPreKeyId = TextSecurePreferences.getNextSignedPreKeyId(context);
|
2015-03-02 16:25:19 +00:00
|
|
|
ECKeyPair keyPair = Curve.generateKeyPair();
|
2014-07-11 17:35:41 +00:00
|
|
|
byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
|
|
|
|
SignedPreKeyRecord record = new SignedPreKeyRecord(signedPreKeyId, System.currentTimeMillis(), keyPair, signature);
|
2014-07-05 19:47:01 +00:00
|
|
|
|
2014-07-11 17:35:41 +00:00
|
|
|
signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
|
2018-02-16 04:33:10 +00:00
|
|
|
TextSecurePreferences.setNextSignedPreKeyId(context, (signedPreKeyId + 1) % Medium.MAX_VALUE);
|
2014-07-05 19:47:01 +00:00
|
|
|
|
2017-01-06 17:19:58 +00:00
|
|
|
if (active) {
|
2018-02-16 04:33:10 +00:00
|
|
|
TextSecurePreferences.setActiveSignedPreKeyId(context, signedPreKeyId);
|
2017-01-06 17:19:58 +00:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:47:01 +00:00
|
|
|
return record;
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 17:19:58 +00:00
|
|
|
public static synchronized void setActiveSignedPreKeyId(Context context, int id) {
|
2018-02-16 04:33:10 +00:00
|
|
|
TextSecurePreferences.setActiveSignedPreKeyId(context, id);
|
2017-01-06 17:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized int getActiveSignedPreKeyId(Context context) {
|
2018-02-16 04:33:10 +00:00
|
|
|
return TextSecurePreferences.getActiveSignedPreKeyId(context);
|
2014-07-05 19:47:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 00:10:41 +00:00
|
|
|
// region - Loki
|
|
|
|
|
2019-06-04 02:12:40 +00:00
|
|
|
public static synchronized @Nullable SignedPreKeyRecord getActiveSignedPreKey(Context context) {
|
|
|
|
SignedPreKeyStore signedPreKeyStore = new TextSecurePreKeyStore(context);
|
|
|
|
try {
|
|
|
|
return signedPreKeyStore.loadSignedPreKey(getActiveSignedPreKeyId(context));
|
|
|
|
} catch (InvalidKeyIdException e) {
|
|
|
|
// Fall through
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-06-04 00:10:41 +00:00
|
|
|
public synchronized static List<PreKeyRecord> generatePreKeys(Context context, int amount) {
|
|
|
|
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
|
|
|
|
List<PreKeyRecord> records = new LinkedList<>();
|
|
|
|
int preKeyIdOffset = TextSecurePreferences.getNextPreKeyId(context);
|
|
|
|
|
|
|
|
for (int i = 0; i < amount; i++) {
|
|
|
|
int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE;
|
|
|
|
ECKeyPair keyPair = Curve.generateKeyPair();
|
|
|
|
PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
|
|
|
|
|
|
|
|
preKeyStore.storePreKey(preKeyId, record);
|
|
|
|
records.add(record);
|
|
|
|
}
|
|
|
|
|
|
|
|
TextSecurePreferences.setNextPreKeyId(context, (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
|
|
|
|
|
|
|
|
return records;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized static void storePreKeyRecords(Context context, List<PreKeyRecord> records) {
|
|
|
|
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
|
|
|
|
for (PreKeyRecord record : records) {
|
|
|
|
preKeyStore.storePreKey(record.getId(), record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized static PreKeyRecord loadPreKey(Context context, int preKeyId) throws InvalidKeyIdException {
|
|
|
|
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context);
|
|
|
|
return preKeyStore.loadPreKey(preKeyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// endregion
|
|
|
|
|
2013-08-15 15:25:30 +00:00
|
|
|
}
|