2018-02-26 09:58:18 -08:00
|
|
|
/*
|
2011-12-20 10:20:44 -08:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2013-11-10 04:15:29 -08:00
|
|
|
* Copyright (C) 2013 Open Whisper Systems
|
2011-12-20 10:20:44 -08: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/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.crypto;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.SharedPreferences.Editor;
|
2015-07-06 17:36:49 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2018-02-26 09:58:18 -08:00
|
|
|
import org.thoughtcrime.securesms.backup.BackupProtos;
|
2014-11-12 11:15:05 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
2019-07-17 11:31:19 +10:00
|
|
|
import org.thoughtcrime.securesms.util.Hex;
|
2016-03-23 10:34:41 -07:00
|
|
|
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;
|
2019-07-17 11:31:19 +10:00
|
|
|
import org.whispersystems.libsignal.logging.Log;
|
2013-08-17 18:37:18 -07:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-02-26 09:58:18 -08:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2013-08-17 18:37:18 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
/**
|
|
|
|
* Utility class for working with identity keys.
|
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class IdentityKeyUtil {
|
|
|
|
|
2018-02-26 09:58:18 -08:00
|
|
|
@SuppressWarnings("unused")
|
2015-07-06 17:36:49 -07:00
|
|
|
private static final String TAG = IdentityKeyUtil.class.getSimpleName();
|
|
|
|
|
|
|
|
private static final String IDENTITY_PUBLIC_KEY_CIPHERTEXT_LEGACY_PREF = "pref_identity_public_curve25519";
|
|
|
|
private static final String IDENTITY_PRIVATE_KEY_CIPHERTEXT_LEGACY_PREF = "pref_identity_private_curve25519";
|
|
|
|
|
|
|
|
private static final String IDENTITY_PUBLIC_KEY_PREF = "pref_identity_public_v3";
|
|
|
|
private static final String IDENTITY_PRIVATE_KEY_PREF = "pref_identity_private_v3";
|
2014-12-01 18:45:37 +01:00
|
|
|
|
2014-04-09 20:02:46 -07:00
|
|
|
public static boolean hasIdentityKey(Context context) {
|
2011-12-20 10:20:44 -08:00
|
|
|
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
|
2013-11-10 04:15:29 -08:00
|
|
|
|
2014-04-09 20:02:46 -07:00
|
|
|
return
|
2015-07-06 17:36:49 -07:00
|
|
|
preferences.contains(IDENTITY_PUBLIC_KEY_PREF) &&
|
|
|
|
preferences.contains(IDENTITY_PRIVATE_KEY_PREF);
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2014-12-01 18:45:37 +01:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
public static @NonNull IdentityKey getIdentityKey(@NonNull Context context) {
|
|
|
|
if (!hasIdentityKey(context)) throw new AssertionError("There isn't one!");
|
2014-12-01 18:45:37 +01:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
try {
|
2015-07-06 17:36:49 -07:00
|
|
|
byte[] publicKeyBytes = Base64.decode(retrieve(context, IDENTITY_PUBLIC_KEY_PREF));
|
2011-12-20 10:20:44 -08:00
|
|
|
return new IdentityKey(publicKeyBytes, 0);
|
2015-07-06 17:36:49 -07:00
|
|
|
} catch (IOException | InvalidKeyException e) {
|
|
|
|
throw new AssertionError(e);
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
}
|
2013-08-21 17:25:19 -07:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
public static @NonNull IdentityKeyPair getIdentityKeyPair(@NonNull Context context) {
|
|
|
|
if (!hasIdentityKey(context)) throw new AssertionError("There isn't one!");
|
2013-08-21 17:25:19 -07:00
|
|
|
|
|
|
|
try {
|
2015-07-06 17:36:49 -07:00
|
|
|
IdentityKey publicKey = getIdentityKey(context);
|
|
|
|
ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(retrieve(context, IDENTITY_PRIVATE_KEY_PREF)));
|
2013-08-21 17:25:19 -07:00
|
|
|
|
|
|
|
return new IdentityKeyPair(publicKey, privateKey);
|
2015-07-06 17:36:49 -07:00
|
|
|
} catch (IOException e) {
|
2013-11-10 04:15:29 -08:00
|
|
|
throw new AssertionError(e);
|
2013-08-21 17:25:19 -07:00
|
|
|
}
|
|
|
|
}
|
2013-11-10 04:15:29 -08:00
|
|
|
|
2019-07-17 11:31:19 +10:00
|
|
|
public static void generateIdentityKeyPair(Context context, String hexEncodedPrivateKey) {
|
|
|
|
try {
|
|
|
|
byte[] privateKey = Hex.fromStringCondensed(hexEncodedPrivateKey);
|
|
|
|
ECKeyPair keyPair = Curve.generateKeyPair(privateKey);
|
|
|
|
IdentityKey publicKey = new IdentityKey(keyPair.getPublicKey());
|
|
|
|
save(context, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(publicKey.serialize()));
|
|
|
|
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(keyPair.getPrivateKey().serialize()));
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d("Loki", "Couldn't restore key pair from seed due to error: " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
public static void generateIdentityKeys(Context context) {
|
2014-07-26 13:29:40 -07:00
|
|
|
ECKeyPair djbKeyPair = Curve.generateKeyPair();
|
2014-04-09 20:02:46 -07:00
|
|
|
IdentityKey djbIdentityKey = new IdentityKey(djbKeyPair.getPublicKey());
|
2015-07-06 17:36:49 -07:00
|
|
|
ECPrivateKey djbPrivateKey = djbKeyPair.getPrivateKey();
|
2013-11-10 04:15:29 -08:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
save(context, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(djbIdentityKey.serialize()));
|
|
|
|
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(djbPrivateKey.serialize()));
|
2013-11-10 04:15:29 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
public static void migrateIdentityKeys(@NonNull Context context,
|
|
|
|
@NonNull MasterSecret masterSecret)
|
|
|
|
{
|
|
|
|
if (!hasIdentityKey(context)) {
|
|
|
|
if (hasLegacyIdentityKeys(context)) {
|
|
|
|
IdentityKeyPair legacyPair = getLegacyIdentityKeyPair(context, masterSecret);
|
|
|
|
|
2015-07-16 12:56:23 -07:00
|
|
|
save(context, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(legacyPair.getPublicKey().serialize()));
|
|
|
|
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(legacyPair.getPrivateKey().serialize()));
|
2015-07-06 17:36:49 -07:00
|
|
|
|
|
|
|
delete(context, IDENTITY_PUBLIC_KEY_CIPHERTEXT_LEGACY_PREF);
|
|
|
|
delete(context, IDENTITY_PRIVATE_KEY_CIPHERTEXT_LEGACY_PREF);
|
|
|
|
} else {
|
|
|
|
generateIdentityKeys(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 09:58:18 -08:00
|
|
|
public static List<BackupProtos.SharedPreference> getBackupRecord(@NonNull Context context) {
|
|
|
|
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
|
|
|
|
|
|
|
|
return new LinkedList<BackupProtos.SharedPreference>() {{
|
|
|
|
add(BackupProtos.SharedPreference.newBuilder()
|
|
|
|
.setFile(MasterSecretUtil.PREFERENCES_NAME)
|
|
|
|
.setKey(IDENTITY_PUBLIC_KEY_PREF)
|
|
|
|
.setValue(preferences.getString(IDENTITY_PUBLIC_KEY_PREF, null))
|
|
|
|
.build());
|
|
|
|
add(BackupProtos.SharedPreference.newBuilder()
|
|
|
|
.setFile(MasterSecretUtil.PREFERENCES_NAME)
|
|
|
|
.setKey(IDENTITY_PRIVATE_KEY_PREF)
|
|
|
|
.setValue(preferences.getString(IDENTITY_PRIVATE_KEY_PREF, null))
|
|
|
|
.build());
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private static boolean hasLegacyIdentityKeys(Context context) {
|
2013-11-10 04:15:29 -08:00
|
|
|
return
|
2015-07-06 17:36:49 -07:00
|
|
|
retrieve(context, IDENTITY_PUBLIC_KEY_CIPHERTEXT_LEGACY_PREF) != null &&
|
|
|
|
retrieve(context, IDENTITY_PRIVATE_KEY_CIPHERTEXT_LEGACY_PREF) != null;
|
2013-11-10 04:15:29 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private static IdentityKeyPair getLegacyIdentityKeyPair(@NonNull Context context,
|
|
|
|
@NonNull MasterSecret masterSecret)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
|
|
|
byte[] publicKeyBytes = Base64.decode(retrieve(context, IDENTITY_PUBLIC_KEY_CIPHERTEXT_LEGACY_PREF));
|
|
|
|
IdentityKey identityKey = new IdentityKey(publicKeyBytes, 0);
|
|
|
|
ECPrivateKey privateKey = masterCipher.decryptKey(Base64.decode(retrieve(context, IDENTITY_PRIVATE_KEY_CIPHERTEXT_LEGACY_PREF)));
|
2013-11-10 04:15:29 -08:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
return new IdentityKeyPair(identityKey, privateKey);
|
|
|
|
} catch (IOException | InvalidKeyException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2013-11-10 04:15:29 -08:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private static String retrieve(Context context, String key) {
|
2011-12-20 10:20:44 -08:00
|
|
|
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
|
|
|
|
return preferences.getString(key, null);
|
|
|
|
}
|
2014-12-01 18:45:37 +01:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private static void save(Context context, String key, String value) {
|
2011-12-20 10:20:44 -08:00
|
|
|
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
|
|
|
|
Editor preferencesEditor = preferences.edit();
|
2014-12-01 18:45:37 +01:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
preferencesEditor.putString(key, value);
|
2014-05-05 13:48:26 -05:00
|
|
|
if (!preferencesEditor.commit()) throw new AssertionError("failed to save identity key/value to shared preferences");
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2015-07-06 17:36:49 -07:00
|
|
|
|
|
|
|
private static void delete(Context context, String key) {
|
|
|
|
context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0).edit().remove(key).commit();
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|