2013-11-10 12:15:29 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2013 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
2013-11-10 12:15:29 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore;
|
2015-01-15 21:35:35 +00:00
|
|
|
import org.thoughtcrime.securesms.util.JsonUtils;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2014-07-05 19:47:01 +00:00
|
|
|
import org.whispersystems.libaxolotl.IdentityKeyPair;
|
|
|
|
import org.whispersystems.libaxolotl.InvalidKeyException;
|
|
|
|
import org.whispersystems.libaxolotl.InvalidKeyIdException;
|
|
|
|
import org.whispersystems.libaxolotl.ecc.Curve;
|
2014-04-21 15:40:19 +00:00
|
|
|
import org.whispersystems.libaxolotl.ecc.ECKeyPair;
|
2014-04-23 04:31:57 +00:00
|
|
|
import org.whispersystems.libaxolotl.state.PreKeyRecord;
|
|
|
|
import org.whispersystems.libaxolotl.state.PreKeyStore;
|
2015-01-15 21:35:35 +00:00
|
|
|
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
|
|
|
|
import org.whispersystems.libaxolotl.state.SignedPreKeyStore;
|
2014-04-23 04:31:57 +00:00
|
|
|
import org.whispersystems.libaxolotl.util.Medium;
|
2013-08-15 15:25:30 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
2013-08-19 17:07:07 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2013-08-15 15:25:30 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class PreKeyUtil {
|
|
|
|
|
2013-11-28 01:50:38 +00:00
|
|
|
public static final int BATCH_SIZE = 100;
|
2013-08-15 15:25:30 +00:00
|
|
|
|
|
|
|
public static List<PreKeyRecord> generatePreKeys(Context context, MasterSecret masterSecret) {
|
2014-04-23 04:31:57 +00:00
|
|
|
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context, masterSecret);
|
|
|
|
List<PreKeyRecord> records = new LinkedList<>();
|
2013-08-19 17:07:07 +00:00
|
|
|
int preKeyIdOffset = 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);
|
|
|
|
}
|
|
|
|
|
2013-08-19 17:07:07 +00:00
|
|
|
setNextPreKeyId(context, (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE);
|
2013-08-15 15:25:30 +00:00
|
|
|
return records;
|
|
|
|
}
|
|
|
|
|
2014-07-11 17:35:41 +00:00
|
|
|
public static SignedPreKeyRecord generateSignedPreKey(Context context, MasterSecret masterSecret,
|
|
|
|
IdentityKeyPair identityKeyPair)
|
2014-07-05 19:47:01 +00:00
|
|
|
{
|
|
|
|
try {
|
2014-07-11 17:35:41 +00:00
|
|
|
SignedPreKeyStore signedPreKeyStore = new TextSecurePreKeyStore(context, masterSecret);
|
|
|
|
int signedPreKeyId = 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);
|
|
|
|
setNextSignedPreKeyId(context, (signedPreKeyId + 1) % Medium.MAX_VALUE);
|
2014-07-05 19:47:01 +00:00
|
|
|
|
|
|
|
return record;
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 22:35:30 +00:00
|
|
|
public static PreKeyRecord generateLastResortKey(Context context, MasterSecret masterSecret) {
|
2014-04-23 04:31:57 +00:00
|
|
|
PreKeyStore preKeyStore = new TextSecurePreKeyStore(context, masterSecret);
|
|
|
|
|
2014-07-05 19:47:01 +00:00
|
|
|
if (preKeyStore.containsPreKey(Medium.MAX_VALUE)) {
|
2013-08-28 22:35:30 +00:00
|
|
|
try {
|
2014-07-05 19:47:01 +00:00
|
|
|
return preKeyStore.loadPreKey(Medium.MAX_VALUE);
|
2013-08-28 22:35:30 +00:00
|
|
|
} catch (InvalidKeyIdException e) {
|
|
|
|
Log.w("PreKeyUtil", e);
|
2014-07-05 19:47:01 +00:00
|
|
|
preKeyStore.removePreKey(Medium.MAX_VALUE);
|
2013-08-28 22:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:25:19 +00:00
|
|
|
ECKeyPair keyPair = Curve.generateKeyPair();
|
2014-04-24 22:39:55 +00:00
|
|
|
PreKeyRecord record = new PreKeyRecord(Medium.MAX_VALUE, keyPair);
|
2013-08-28 22:35:30 +00:00
|
|
|
|
2014-07-05 19:47:01 +00:00
|
|
|
preKeyStore.storePreKey(Medium.MAX_VALUE, record);
|
2013-08-28 22:35:30 +00:00
|
|
|
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
2013-08-19 17:07:07 +00:00
|
|
|
private static void setNextPreKeyId(Context context, int id) {
|
2013-08-15 15:25:30 +00:00
|
|
|
try {
|
2013-08-19 17:07:07 +00:00
|
|
|
File nextFile = new File(getPreKeysDirectory(context), PreKeyIndex.FILE_NAME);
|
|
|
|
FileOutputStream fout = new FileOutputStream(nextFile);
|
2015-01-15 21:35:35 +00:00
|
|
|
fout.write(JsonUtils.toJson(new PreKeyIndex(id)).getBytes());
|
2013-08-19 17:07:07 +00:00
|
|
|
fout.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("PreKeyUtil", e);
|
|
|
|
}
|
|
|
|
}
|
2013-08-15 15:25:30 +00:00
|
|
|
|
2014-07-11 17:35:41 +00:00
|
|
|
private static void setNextSignedPreKeyId(Context context, int id) {
|
2014-07-05 19:47:01 +00:00
|
|
|
try {
|
2014-07-11 17:35:41 +00:00
|
|
|
File nextFile = new File(getSignedPreKeysDirectory(context), SignedPreKeyIndex.FILE_NAME);
|
2014-07-05 19:47:01 +00:00
|
|
|
FileOutputStream fout = new FileOutputStream(nextFile);
|
2015-01-15 21:35:35 +00:00
|
|
|
fout.write(JsonUtils.toJson(new SignedPreKeyIndex(id)).getBytes());
|
2014-07-05 19:47:01 +00:00
|
|
|
fout.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("PreKeyUtil", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 17:07:07 +00:00
|
|
|
private static int getNextPreKeyId(Context context) {
|
|
|
|
try {
|
|
|
|
File nextFile = new File(getPreKeysDirectory(context), PreKeyIndex.FILE_NAME);
|
|
|
|
|
2014-03-19 18:14:15 +00:00
|
|
|
if (!nextFile.exists()) {
|
2013-08-19 17:07:07 +00:00
|
|
|
return Util.getSecureRandom().nextInt(Medium.MAX_VALUE);
|
|
|
|
} else {
|
|
|
|
InputStreamReader reader = new InputStreamReader(new FileInputStream(nextFile));
|
2015-01-15 21:35:35 +00:00
|
|
|
PreKeyIndex index = JsonUtils.fromJson(reader, PreKeyIndex.class);
|
2013-08-19 17:07:07 +00:00
|
|
|
reader.close();
|
|
|
|
return index.nextPreKeyId;
|
2013-08-15 15:25:30 +00:00
|
|
|
}
|
2013-08-19 17:07:07 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("PreKeyUtil", e);
|
|
|
|
return Util.getSecureRandom().nextInt(Medium.MAX_VALUE);
|
2013-08-15 15:25:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 17:35:41 +00:00
|
|
|
private static int getNextSignedPreKeyId(Context context) {
|
2014-07-05 19:47:01 +00:00
|
|
|
try {
|
2014-07-11 17:35:41 +00:00
|
|
|
File nextFile = new File(getSignedPreKeysDirectory(context), SignedPreKeyIndex.FILE_NAME);
|
2014-07-05 19:47:01 +00:00
|
|
|
|
|
|
|
if (!nextFile.exists()) {
|
|
|
|
return Util.getSecureRandom().nextInt(Medium.MAX_VALUE);
|
|
|
|
} else {
|
|
|
|
InputStreamReader reader = new InputStreamReader(new FileInputStream(nextFile));
|
2015-01-15 21:35:35 +00:00
|
|
|
SignedPreKeyIndex index = JsonUtils.fromJson(reader, SignedPreKeyIndex.class);
|
2014-07-05 19:47:01 +00:00
|
|
|
reader.close();
|
2014-07-11 17:35:41 +00:00
|
|
|
return index.nextSignedPreKeyId;
|
2014-07-05 19:47:01 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("PreKeyUtil", e);
|
|
|
|
return Util.getSecureRandom().nextInt(Medium.MAX_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-15 15:25:30 +00:00
|
|
|
private static File getPreKeysDirectory(Context context) {
|
2014-07-05 19:47:01 +00:00
|
|
|
return getKeysDirectory(context, TextSecurePreKeyStore.PREKEY_DIRECTORY);
|
|
|
|
}
|
|
|
|
|
2014-07-11 17:35:41 +00:00
|
|
|
private static File getSignedPreKeysDirectory(Context context) {
|
|
|
|
return getKeysDirectory(context, TextSecurePreKeyStore.SIGNED_PREKEY_DIRECTORY);
|
2014-07-05 19:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static File getKeysDirectory(Context context, String name) {
|
|
|
|
File directory = new File(context.getFilesDir(), name);
|
2013-08-15 15:25:30 +00:00
|
|
|
|
|
|
|
if (!directory.exists())
|
|
|
|
directory.mkdirs();
|
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2013-08-19 17:07:07 +00:00
|
|
|
private static class PreKeyIndex {
|
|
|
|
public static final String FILE_NAME = "index.dat";
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
@JsonProperty
|
2013-08-19 17:07:07 +00:00
|
|
|
private int nextPreKeyId;
|
|
|
|
|
|
|
|
public PreKeyIndex() {}
|
|
|
|
|
|
|
|
public PreKeyIndex(int nextPreKeyId) {
|
|
|
|
this.nextPreKeyId = nextPreKeyId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 17:35:41 +00:00
|
|
|
private static class SignedPreKeyIndex {
|
2014-07-05 19:47:01 +00:00
|
|
|
public static final String FILE_NAME = "index.dat";
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
@JsonProperty
|
2014-07-11 17:35:41 +00:00
|
|
|
private int nextSignedPreKeyId;
|
2014-07-05 19:47:01 +00:00
|
|
|
|
2014-07-11 17:35:41 +00:00
|
|
|
public SignedPreKeyIndex() {}
|
2014-07-05 19:47:01 +00:00
|
|
|
|
2014-07-11 17:35:41 +00:00
|
|
|
public SignedPreKeyIndex(int nextSignedPreKeyId) {
|
|
|
|
this.nextSignedPreKeyId = nextSignedPreKeyId;
|
2014-07-05 19:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-15 15:25:30 +00:00
|
|
|
}
|