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/>.
|
|
|
|
*/
|
|
|
|
|
2013-08-18 01:37:18 +00:00
|
|
|
package org.whispersystems.textsecure.crypto;
|
2013-08-15 15:25:30 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-08-19 17:07:07 +00:00
|
|
|
import com.google.thoughtcrimegson.Gson;
|
2013-11-10 12:15:29 +00:00
|
|
|
|
|
|
|
import org.whispersystems.textsecure.crypto.ecc.Curve25519;
|
2013-11-28 01:50:38 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.ecc.ECKeyPair;
|
2013-08-18 01:37:18 +00:00
|
|
|
import org.whispersystems.textsecure.storage.InvalidKeyIdException;
|
|
|
|
import org.whispersystems.textsecure.storage.PreKeyRecord;
|
2013-08-19 17:07:07 +00:00
|
|
|
import org.whispersystems.textsecure.util.Medium;
|
|
|
|
import org.whispersystems.textsecure.util.Util;
|
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-18 21:35:23 +00:00
|
|
|
import java.util.Comparator;
|
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) {
|
|
|
|
List<PreKeyRecord> records = new LinkedList<PreKeyRecord>();
|
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;
|
2014-03-19 22:25:50 +00:00
|
|
|
ECKeyPair keyPair = Curve25519.generateKeyPair(true);
|
2013-08-19 17:07:07 +00:00
|
|
|
PreKeyRecord record = new PreKeyRecord(context, masterSecret, preKeyId, keyPair);
|
2013-08-15 15:25:30 +00:00
|
|
|
|
|
|
|
record.save();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-28 22:35:30 +00:00
|
|
|
public static PreKeyRecord generateLastResortKey(Context context, MasterSecret masterSecret) {
|
|
|
|
if (PreKeyRecord.hasRecord(context, Medium.MAX_VALUE)) {
|
|
|
|
try {
|
|
|
|
return new PreKeyRecord(context, masterSecret, Medium.MAX_VALUE);
|
|
|
|
} catch (InvalidKeyIdException e) {
|
|
|
|
Log.w("PreKeyUtil", e);
|
|
|
|
PreKeyRecord.delete(context, Medium.MAX_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-19 22:25:50 +00:00
|
|
|
ECKeyPair keyPair = Curve25519.generateKeyPair(true);
|
2013-08-28 22:35:30 +00:00
|
|
|
PreKeyRecord record = new PreKeyRecord(context, masterSecret, Medium.MAX_VALUE, keyPair);
|
|
|
|
|
|
|
|
record.save();
|
|
|
|
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
2014-03-19 18:14:15 +00:00
|
|
|
// public static List<PreKeyRecord> getPreKeys(Context context, MasterSecret masterSecret) {
|
|
|
|
// List<PreKeyRecord> records = new LinkedList<PreKeyRecord>();
|
|
|
|
// File directory = getPreKeysDirectory(context);
|
|
|
|
// String[] keyRecordIds = directory.list();
|
|
|
|
//
|
|
|
|
// Arrays.sort(keyRecordIds, new PreKeyRecordIdComparator());
|
|
|
|
//
|
|
|
|
// for (String keyRecordId : keyRecordIds) {
|
|
|
|
// try {
|
|
|
|
// if (!keyRecordId.equals(PreKeyIndex.FILE_NAME) && Integer.parseInt(keyRecordId) != Medium.MAX_VALUE) {
|
|
|
|
// records.add(new PreKeyRecord(context, masterSecret, Integer.parseInt(keyRecordId)));
|
|
|
|
// }
|
|
|
|
// } catch (InvalidKeyIdException e) {
|
|
|
|
// Log.w("PreKeyUtil", e);
|
|
|
|
// new File(getPreKeysDirectory(context), keyRecordId).delete();
|
|
|
|
// } catch (NumberFormatException nfe) {
|
|
|
|
// Log.w("PreKeyUtil", nfe);
|
|
|
|
// new File(getPreKeysDirectory(context), keyRecordId).delete();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return records;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public static void clearPreKeys(Context context) {
|
|
|
|
// File directory = getPreKeysDirectory(context);
|
|
|
|
// String[] keyRecords = directory.list();
|
|
|
|
//
|
|
|
|
// for (String keyRecord : keyRecords) {
|
|
|
|
// new File(directory, keyRecord).delete();
|
|
|
|
// }
|
|
|
|
// }
|
2013-08-15 15:25:30 +00:00
|
|
|
|
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);
|
|
|
|
fout.write(new Gson().toJson(new PreKeyIndex(id)).getBytes());
|
|
|
|
fout.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("PreKeyUtil", e);
|
|
|
|
}
|
|
|
|
}
|
2013-08-15 15:25:30 +00:00
|
|
|
|
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));
|
|
|
|
PreKeyIndex index = new Gson().fromJson(reader, PreKeyIndex.class);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static File getPreKeysDirectory(Context context) {
|
|
|
|
File directory = new File(context.getFilesDir(), PreKeyRecord.PREKEY_DIRECTORY);
|
|
|
|
|
|
|
|
if (!directory.exists())
|
|
|
|
directory.mkdirs();
|
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
2013-08-18 21:35:23 +00:00
|
|
|
private static class PreKeyRecordIdComparator implements Comparator<String> {
|
|
|
|
@Override
|
|
|
|
public int compare(String lhs, String rhs) {
|
2013-10-19 05:45:27 +00:00
|
|
|
if (lhs.equals(PreKeyIndex.FILE_NAME)) return -1;
|
|
|
|
else if (rhs.equals(PreKeyIndex.FILE_NAME)) return 1;
|
|
|
|
|
2013-08-18 21:35:23 +00:00
|
|
|
try {
|
|
|
|
long lhsLong = Long.parseLong(lhs);
|
|
|
|
long rhsLong = Long.parseLong(rhs);
|
|
|
|
|
|
|
|
if (lhsLong < rhsLong) return -1;
|
|
|
|
else if (lhsLong > rhsLong) return 1;
|
|
|
|
else return 0;
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 17:07:07 +00:00
|
|
|
private static class PreKeyIndex {
|
|
|
|
public static final String FILE_NAME = "index.dat";
|
|
|
|
|
|
|
|
private int nextPreKeyId;
|
|
|
|
|
|
|
|
public PreKeyIndex() {}
|
|
|
|
|
|
|
|
public PreKeyIndex(int nextPreKeyId) {
|
|
|
|
this.nextPreKeyId = nextPreKeyId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-15 15:25:30 +00:00
|
|
|
}
|