Rename 'device key' to 'signed prekey'.

This commit is contained in:
Moxie Marlinspike
2014-07-11 10:35:41 -07:00
parent 07fd17ccda
commit 0d532afd8e
26 changed files with 573 additions and 574 deletions

View File

@@ -28,8 +28,8 @@ import org.whispersystems.libaxolotl.InvalidKeyIdException;
import org.whispersystems.libaxolotl.ecc.Curve;
import org.whispersystems.libaxolotl.ecc.Curve25519;
import org.whispersystems.libaxolotl.ecc.ECKeyPair;
import org.whispersystems.libaxolotl.state.DeviceKeyRecord;
import org.whispersystems.libaxolotl.state.DeviceKeyStore;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyStore;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.libaxolotl.state.PreKeyStore;
import org.whispersystems.libaxolotl.util.Medium;
@@ -66,18 +66,18 @@ public class PreKeyUtil {
return records;
}
public static DeviceKeyRecord generateDeviceKey(Context context, MasterSecret masterSecret,
IdentityKeyPair identityKeyPair)
public static SignedPreKeyRecord generateSignedPreKey(Context context, MasterSecret masterSecret,
IdentityKeyPair identityKeyPair)
{
try {
DeviceKeyStore deviceKeyStore = new TextSecurePreKeyStore(context, masterSecret);
int deviceKeyId = getNextDeviceKeyId(context);
ECKeyPair keyPair = Curve25519.generateKeyPair(true);
byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
DeviceKeyRecord record = new DeviceKeyRecord(deviceKeyId, System.currentTimeMillis(), keyPair, signature);
SignedPreKeyStore signedPreKeyStore = new TextSecurePreKeyStore(context, masterSecret);
int signedPreKeyId = getNextSignedPreKeyId(context);
ECKeyPair keyPair = Curve25519.generateKeyPair(true);
byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
SignedPreKeyRecord record = new SignedPreKeyRecord(signedPreKeyId, System.currentTimeMillis(), keyPair, signature);
deviceKeyStore.storeDeviceKey(deviceKeyId, record);
setNextDeviceKeyId(context, (deviceKeyId + 1) % Medium.MAX_VALUE);
signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
setNextSignedPreKeyId(context, (signedPreKeyId + 1) % Medium.MAX_VALUE);
return record;
} catch (InvalidKeyException e) {
@@ -116,11 +116,11 @@ public class PreKeyUtil {
}
}
private static void setNextDeviceKeyId(Context context, int id) {
private static void setNextSignedPreKeyId(Context context, int id) {
try {
File nextFile = new File(getDeviceKeysDirectory(context), DeviceKeyIndex.FILE_NAME);
File nextFile = new File(getSignedPreKeysDirectory(context), SignedPreKeyIndex.FILE_NAME);
FileOutputStream fout = new FileOutputStream(nextFile);
fout.write(new Gson().toJson(new DeviceKeyIndex(id)).getBytes());
fout.write(new Gson().toJson(new SignedPreKeyIndex(id)).getBytes());
fout.close();
} catch (IOException e) {
Log.w("PreKeyUtil", e);
@@ -145,17 +145,17 @@ public class PreKeyUtil {
}
}
private static int getNextDeviceKeyId(Context context) {
private static int getNextSignedPreKeyId(Context context) {
try {
File nextFile = new File(getDeviceKeysDirectory(context), DeviceKeyIndex.FILE_NAME);
File nextFile = new File(getSignedPreKeysDirectory(context), SignedPreKeyIndex.FILE_NAME);
if (!nextFile.exists()) {
return Util.getSecureRandom().nextInt(Medium.MAX_VALUE);
} else {
InputStreamReader reader = new InputStreamReader(new FileInputStream(nextFile));
DeviceKeyIndex index = new Gson().fromJson(reader, DeviceKeyIndex.class);
SignedPreKeyIndex index = new Gson().fromJson(reader, SignedPreKeyIndex.class);
reader.close();
return index.nextDeviceKeyId;
return index.nextSignedPreKeyId;
}
} catch (IOException e) {
Log.w("PreKeyUtil", e);
@@ -167,8 +167,8 @@ public class PreKeyUtil {
return getKeysDirectory(context, TextSecurePreKeyStore.PREKEY_DIRECTORY);
}
private static File getDeviceKeysDirectory(Context context) {
return getKeysDirectory(context, TextSecurePreKeyStore.DEVICE_KEY_DIRECTORY);
private static File getSignedPreKeysDirectory(Context context) {
return getKeysDirectory(context, TextSecurePreKeyStore.SIGNED_PREKEY_DIRECTORY);
}
private static File getKeysDirectory(Context context, String name) {
@@ -192,15 +192,15 @@ public class PreKeyUtil {
}
}
private static class DeviceKeyIndex {
private static class SignedPreKeyIndex {
public static final String FILE_NAME = "index.dat";
private int nextDeviceKeyId;
private int nextSignedPreKeyId;
public DeviceKeyIndex() {}
public SignedPreKeyIndex() {}
public DeviceKeyIndex(int nextDeviceKeyId) {
this.nextDeviceKeyId = nextDeviceKeyId;
public SignedPreKeyIndex(int nextSignedPreKeyId) {
this.nextSignedPreKeyId = nextSignedPreKeyId;
}
}

View File

@@ -4,10 +4,10 @@ import com.google.thoughtcrimegson.GsonBuilder;
public class PreKeyResponseItem {
private int deviceId;
private int registrationId;
private DeviceKeyEntity deviceKey;
private PreKeyEntity preKey;
private int deviceId;
private int registrationId;
private SignedPreKeyEntity signedPreKey;
private PreKeyEntity preKey;
public int getDeviceId() {
return deviceId;
@@ -17,8 +17,8 @@ public class PreKeyResponseItem {
return registrationId;
}
public DeviceKeyEntity getDeviceKey() {
return deviceKey;
public SignedPreKeyEntity getSignedPreKey() {
return signedPreKey;
}
public PreKeyEntity getPreKey() {
@@ -26,6 +26,6 @@ public class PreKeyResponseItem {
}
public static GsonBuilder forBuilder(GsonBuilder builder) {
return DeviceKeyEntity.forBuilder(builder);
return SignedPreKeyEntity.forBuilder(builder);
}
}

View File

@@ -11,21 +11,21 @@ public class PreKeyState {
private IdentityKey identityKey;
private List<PreKeyEntity> preKeys;
private PreKeyEntity lastResortKey;
private DeviceKeyEntity deviceKey;
private SignedPreKeyEntity signedPreKey;
public PreKeyState(List<PreKeyEntity> preKeys, PreKeyEntity lastResortKey,
DeviceKeyEntity deviceKey, IdentityKey identityKey)
SignedPreKeyEntity signedPreKey, IdentityKey identityKey)
{
this.preKeys = preKeys;
this.lastResortKey = lastResortKey;
this.deviceKey = deviceKey;
this.signedPreKey = signedPreKey;
this.identityKey = identityKey;
}
public static String toJson(PreKeyState state) {
GsonBuilder builder = new GsonBuilder();
return DeviceKeyEntity.forBuilder(builder)
return SignedPreKeyEntity.forBuilder(builder)
.registerTypeAdapter(IdentityKey.class, new PreKeyResponse.IdentityKeyJsonAdapter())
.create().toJson(state);
}

View File

@@ -25,7 +25,7 @@ import com.google.thoughtcrimegson.JsonParseException;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import org.whispersystems.libaxolotl.IdentityKey;
import org.whispersystems.libaxolotl.ecc.ECPublicKey;
import org.whispersystems.libaxolotl.state.DeviceKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.state.PreKeyBundle;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.textsecure.util.Base64;
@@ -129,7 +129,7 @@ public class PushServiceSocket {
public void registerPreKeys(IdentityKey identityKey,
PreKeyRecord lastResortKey,
DeviceKeyRecord deviceKey,
SignedPreKeyRecord signedPreKey,
List<PreKeyRecord> records)
throws IOException
{
@@ -145,12 +145,13 @@ public class PushServiceSocket {
PreKeyEntity lastResortEntity = new PreKeyEntity(lastResortKey.getId(),
lastResortKey.getKeyPair().getPublicKey());
DeviceKeyEntity deviceKeyEntity = new DeviceKeyEntity(deviceKey.getId(),
deviceKey.getKeyPair().getPublicKey(),
deviceKey.getSignature());
SignedPreKeyEntity signedPreKeyEntity = new SignedPreKeyEntity(signedPreKey.getId(),
signedPreKey.getKeyPair().getPublicKey(),
signedPreKey.getSignature());
makeRequest(String.format(PREKEY_PATH, ""), "PUT",
PreKeyState.toJson(new PreKeyState(entities, lastResortEntity, deviceKeyEntity, identityKey)));
PreKeyState.toJson(new PreKeyState(entities, lastResortEntity,
signedPreKeyEntity, identityKey)));
}
public int getAvailablePreKeys() throws IOException {
@@ -178,16 +179,16 @@ public class PushServiceSocket {
List<PreKeyBundle> bundles = new LinkedList<>();
for (PreKeyResponseItem device : response.getDevices()) {
ECPublicKey preKey = null;
ECPublicKey deviceKey = null;
byte[] deviceKeySignature = null;
int preKeyId = -1;
int deviceKeyId = -1;
ECPublicKey preKey = null;
ECPublicKey signedPreKey = null;
byte[] signedPreKeySignature = null;
int preKeyId = -1;
int signedPreKeyId = -1;
if (device.getDeviceKey() != null) {
deviceKey = device.getDeviceKey().getPublicKey();
deviceKeyId = device.getDeviceKey().getKeyId();
deviceKeySignature = device.getDeviceKey().getSignature();
if (device.getSignedPreKey() != null) {
signedPreKey = device.getSignedPreKey().getPublicKey();
signedPreKeyId = device.getSignedPreKey().getKeyId();
signedPreKeySignature = device.getSignedPreKey().getSignature();
}
if (device.getPreKey() != null) {
@@ -196,7 +197,7 @@ public class PushServiceSocket {
}
bundles.add(new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), preKeyId,
preKey, deviceKeyId, deviceKey, deviceKeySignature,
preKey, signedPreKeyId, signedPreKey, signedPreKeySignature,
response.getIdentityKey()));
}
@@ -223,26 +224,26 @@ public class PushServiceSocket {
if (response.getDevices() == null || response.getDevices().size() < 1)
throw new IOException("Empty prekey list");
PreKeyResponseItem device = response.getDevices().get(0);
ECPublicKey preKey = null;
ECPublicKey deviceKey = null;
byte[] deviceKeySignature = null;
int preKeyId = -1;
int deviceKeyId = -1;
PreKeyResponseItem device = response.getDevices().get(0);
ECPublicKey preKey = null;
ECPublicKey signedPreKey = null;
byte[] signedPreKeySignature = null;
int preKeyId = -1;
int signedPreKeyId = -1;
if (device.getPreKey() != null) {
preKeyId = device.getPreKey().getKeyId();
preKey = device.getPreKey().getPublicKey();
}
if (device.getDeviceKey() != null) {
deviceKeyId = device.getDeviceKey().getKeyId();
deviceKey = device.getDeviceKey().getPublicKey();
deviceKeySignature = device.getDeviceKey().getSignature();
if (device.getSignedPreKey() != null) {
signedPreKeyId = device.getSignedPreKey().getKeyId();
signedPreKey = device.getSignedPreKey().getPublicKey();
signedPreKeySignature = device.getSignedPreKey().getSignature();
}
return new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), preKeyId, preKey,
deviceKeyId, deviceKey, deviceKeySignature, response.getIdentityKey());
signedPreKeyId, signedPreKey, signedPreKeySignature, response.getIdentityKey());
} catch (JsonParseException e) {
throw new IOException(e);
} catch (NotFoundException nfe) {

View File

@@ -15,13 +15,13 @@ import org.whispersystems.textsecure.util.Base64;
import java.io.IOException;
import java.lang.reflect.Type;
public class DeviceKeyEntity extends PreKeyEntity {
public class SignedPreKeyEntity extends PreKeyEntity {
private byte[] signature;
public DeviceKeyEntity() {}
public SignedPreKeyEntity() {}
public DeviceKeyEntity(int keyId, ECPublicKey publicKey, byte[] signature) {
public SignedPreKeyEntity(int keyId, ECPublicKey publicKey, byte[] signature) {
super(keyId, publicKey);
this.signature = signature;
}

View File

@@ -5,8 +5,8 @@ import android.util.Log;
import org.whispersystems.libaxolotl.InvalidKeyIdException;
import org.whispersystems.libaxolotl.InvalidMessageException;
import org.whispersystems.libaxolotl.state.DeviceKeyRecord;
import org.whispersystems.libaxolotl.state.DeviceKeyStore;
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
import org.whispersystems.libaxolotl.state.SignedPreKeyStore;
import org.whispersystems.libaxolotl.state.PreKeyRecord;
import org.whispersystems.libaxolotl.state.PreKeyStore;
import org.whispersystems.textsecure.crypto.MasterCipher;
@@ -22,10 +22,10 @@ import java.nio.channels.FileChannel;
import java.util.LinkedList;
import java.util.List;
public class TextSecurePreKeyStore implements PreKeyStore, DeviceKeyStore {
public class TextSecurePreKeyStore implements PreKeyStore, SignedPreKeyStore {
public static final String PREKEY_DIRECTORY = "prekeys";
public static final String DEVICE_KEY_DIRECTORY = "device_keys";
public static final String PREKEY_DIRECTORY = "prekeys";
public static final String SIGNED_PREKEY_DIRECTORY = "signed_prekeys";
private static final int CURRENT_VERSION_MARKER = 1;
@@ -53,10 +53,10 @@ public class TextSecurePreKeyStore implements PreKeyStore, DeviceKeyStore {
}
@Override
public DeviceKeyRecord loadDeviceKey(int deviceKeyId) throws InvalidKeyIdException {
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
synchronized (FILE_LOCK) {
try {
return new DeviceKeyRecord(loadSerializedRecord(getDeviceKeyFile(deviceKeyId)));
return new SignedPreKeyRecord(loadSerializedRecord(getSignedPreKeyFile(signedPreKeyId)));
} catch (IOException | InvalidMessageException e) {
Log.w(TAG, e);
throw new InvalidKeyIdException(e);
@@ -65,14 +65,14 @@ public class TextSecurePreKeyStore implements PreKeyStore, DeviceKeyStore {
}
@Override
public List<DeviceKeyRecord> loadDeviceKeys() {
public List<SignedPreKeyRecord> loadSignedPreKeys() {
synchronized (FILE_LOCK) {
File directory = getDeviceKeyDirectory();
List<DeviceKeyRecord> results = new LinkedList<>();
File directory = getSignedPreKeyDirectory();
List<SignedPreKeyRecord> results = new LinkedList<>();
for (File deviceKeyFile : directory.listFiles()) {
for (File signedPreKeyFile : directory.listFiles()) {
try {
results.add(new DeviceKeyRecord(loadSerializedRecord(deviceKeyFile)));
results.add(new SignedPreKeyRecord(loadSerializedRecord(signedPreKeyFile)));
} catch (IOException | InvalidMessageException e) {
Log.w(TAG, e);
}
@@ -94,10 +94,10 @@ public class TextSecurePreKeyStore implements PreKeyStore, DeviceKeyStore {
}
@Override
public void storeDeviceKey(int deviceKeyId, DeviceKeyRecord record) {
public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) {
synchronized (FILE_LOCK) {
try {
storeSerializedRecord(getDeviceKeyFile(deviceKeyId), record.serialize());
storeSerializedRecord(getSignedPreKeyFile(signedPreKeyId), record.serialize());
} catch (IOException e) {
throw new AssertionError(e);
}
@@ -111,8 +111,8 @@ public class TextSecurePreKeyStore implements PreKeyStore, DeviceKeyStore {
}
@Override
public boolean containsDeviceKey(int deviceKeyId) {
File record = getDeviceKeyFile(deviceKeyId);
public boolean containsSignedPreKey(int signedPreKeyId) {
File record = getSignedPreKeyFile(signedPreKeyId);
return record.exists();
}
@@ -124,8 +124,8 @@ public class TextSecurePreKeyStore implements PreKeyStore, DeviceKeyStore {
}
@Override
public void removeDeviceKey(int deviceKeyId) {
File record = getDeviceKeyFile(deviceKeyId);
public void removeSignedPreKey(int signedPreKeyId) {
File record = getSignedPreKeyFile(signedPreKeyId);
record.delete();
}
@@ -159,16 +159,16 @@ public class TextSecurePreKeyStore implements PreKeyStore, DeviceKeyStore {
return new File(getPreKeyDirectory(), String.valueOf(preKeyId));
}
private File getDeviceKeyFile(int deviceKeyId) {
return new File(getDeviceKeyDirectory(), String.valueOf(deviceKeyId));
private File getSignedPreKeyFile(int signedPreKeyId) {
return new File(getSignedPreKeyDirectory(), String.valueOf(signedPreKeyId));
}
private File getPreKeyDirectory() {
return getRecordsDirectory(PREKEY_DIRECTORY);
}
private File getDeviceKeyDirectory() {
return getRecordsDirectory(DEVICE_KEY_DIRECTORY);
private File getSignedPreKeyDirectory() {
return getRecordsDirectory(SIGNED_PREKEY_DIRECTORY);
}
private File getRecordsDirectory(String directoryName) {