Update for seed changes

This commit is contained in:
Niels Andriesse 2019-08-19 15:34:21 +10:00
parent 24a03cd5bc
commit ae54c641a9
7 changed files with 61 additions and 55 deletions

View File

@ -465,7 +465,7 @@
android:windowSoftInputMode="stateUnchanged"
android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout|screenSize"/>
<activity android:name="org.thoughtcrime.securesms.loki.KeyPairActivity"
<activity android:name="org.thoughtcrime.securesms.loki.SeedActivity"
android:launchMode="singleTask"
android:theme="@style/TextSecure.DarkRegistrationTheme"
android:windowSoftInputMode="stateUnchanged"

View File

@ -5,7 +5,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".loki.KeyPairActivity">
tools:context=".loki.SeedActivity">
<LinearLayout
android:layout_width="match_parent"

View File

@ -311,8 +311,12 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredActionBarA
break;
case PREFERENCE_CATEGORY_SEED:
File languageFileDirectory = new File(getContext().getApplicationInfo().dataDir);
String hexEncodedPrivateKey = SerializationKt.getHexEncodedPrivateKey(IdentityKeyUtil.getIdentityKeyPair(getContext()));
String seed = new MnemonicCodec(languageFileDirectory).encode(hexEncodedPrivateKey, MnemonicCodec.Language.Configuration.Companion.getEnglish());
try {
String hexEncodedSeed = IdentityKeyUtil.retrieve(getContext(), IdentityKeyUtil.lokiSeedKey);
if (hexEncodedSeed == null) {
hexEncodedSeed = SerializationKt.getHexEncodedPrivateKey(IdentityKeyUtil.getIdentityKeyPair(getContext())); // Legacy account
}
String seed = new MnemonicCodec(languageFileDirectory).encode(hexEncodedSeed, MnemonicCodec.Language.Configuration.Companion.getEnglish());
new AlertDialog.Builder(getContext())
.setTitle(R.string.activity_settings_seed_dialog_title)
.setMessage(seed)
@ -324,6 +328,9 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredActionBarA
})
.setNeutralButton(R.string.activity_settings_seed_dialog_ok_button_title, null)
.show();
} catch (Exception e) {
// Do nothing
}
break;
default:
throw new AssertionError();

View File

@ -65,7 +65,7 @@ public class PassphraseCreateActivity extends PassphraseActivity {
passphrase);
MasterSecretUtil.generateAsymmetricMasterSecret(PassphraseCreateActivity.this, masterSecret);
IdentityKeyUtil.generateIdentityKeys(PassphraseCreateActivity.this);
IdentityKeyUtil.generateIdentityKeyPair(PassphraseCreateActivity.this);
VersionTracker.updateLastSeenVersion(PassphraseCreateActivity.this);
TextSecurePreferences.setLastExperienceVersionCode(PassphraseCreateActivity.this, Util.getCanonicalVersionCode());

View File

@ -24,14 +24,12 @@ import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.backup.BackupProtos;
import org.thoughtcrime.securesms.util.Base64;
import org.thoughtcrime.securesms.util.Hex;
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;
import org.whispersystems.libsignal.logging.Log;
import java.io.IOException;
import java.util.LinkedList;
@ -54,6 +52,8 @@ public class IdentityKeyUtil {
private static final String IDENTITY_PUBLIC_KEY_PREF = "pref_identity_public_v3";
private static final String IDENTITY_PRIVATE_KEY_PREF = "pref_identity_private_v3";
public static final String lokiSeedKey = "loki_seed";
public static boolean hasIdentityKey(Context context) {
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
@ -86,25 +86,21 @@ public class IdentityKeyUtil {
}
}
public static void generateIdentityKeyPair(Context context, String hexEncodedPrivateKey) {
try {
byte[] privateKey = Hex.fromStringCondensed(hexEncodedPrivateKey);
ECKeyPair keyPair = Curve.generateKeyPair(privateKey);
public static void generateIdentityKeyPair(Context context, byte[] seed) {
ECKeyPair keyPair;
if (seed != null) {
keyPair = Curve.generateKeyPair(seed);
} else {
keyPair = Curve.generateKeyPair();
}
IdentityKey publicKey = new IdentityKey(keyPair.getPublicKey());
ECPrivateKey privateKey = keyPair.getPrivateKey();
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() + ".");
}
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(privateKey.serialize()));
}
public static void generateIdentityKeys(Context context) {
ECKeyPair djbKeyPair = Curve.generateKeyPair();
IdentityKey djbIdentityKey = new IdentityKey(djbKeyPair.getPublicKey());
ECPrivateKey djbPrivateKey = djbKeyPair.getPrivateKey();
save(context, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(djbIdentityKey.serialize()));
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(djbPrivateKey.serialize()));
public static void generateIdentityKeyPair(Context context) {
generateIdentityKeyPair(context, null);
}
public static void migrateIdentityKeys(@NonNull Context context,
@ -120,7 +116,7 @@ public class IdentityKeyUtil {
delete(context, IDENTITY_PUBLIC_KEY_CIPHERTEXT_LEGACY_PREF);
delete(context, IDENTITY_PRIVATE_KEY_CIPHERTEXT_LEGACY_PREF);
} else {
generateIdentityKeys(context);
generateIdentityKeyPair(context);
}
}
}
@ -163,12 +159,12 @@ public class IdentityKeyUtil {
}
}
private static String retrieve(Context context, String key) {
public static String retrieve(Context context, String key) {
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
return preferences.getString(key, null);
}
private static void save(Context context, String key, String value) {
public static void save(Context context, String key, String value) {
SharedPreferences preferences = context.getSharedPreferences(MasterSecretUtil.PREFERENCES_NAME, 0);
Editor preferencesEditor = preferences.edit();

View File

@ -29,7 +29,7 @@ class AccountDetailsActivity : BaseActionBarActivity() {
}
val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(nameEditText.windowToken, 0)
startActivity(Intent(this, KeyPairActivity::class.java))
startActivity(Intent(this, SeedActivity::class.java))
finish()
}
}

View File

@ -8,8 +8,8 @@ import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_key_pair.*
import network.loki.messenger.R;
import kotlinx.android.synthetic.main.activity_seed.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.BaseActionBarActivity
import org.thoughtcrime.securesms.ConversationListActivity
@ -17,20 +17,20 @@ import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.IdentityDatabase
import org.thoughtcrime.securesms.util.Hex
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.libsignal.IdentityKeyPair
import org.whispersystems.curve25519.Curve25519
import org.whispersystems.libsignal.util.KeyHelper
import org.whispersystems.signalservice.loki.crypto.MnemonicCodec
import org.whispersystems.signalservice.loki.utilities.hexEncodedPrivateKey
import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey
import java.io.File
import java.io.FileOutputStream
class KeyPairActivity : BaseActionBarActivity() {
class SeedActivity : BaseActionBarActivity() {
private lateinit var languageFileDirectory: File
private var mode = Mode.Register
set(newValue) { field = newValue; updateUI() }
private var keyPair: IdentityKeyPair? = null
private var seed: ByteArray? = null
set(newValue) { field = newValue; updateMnemonic() }
private var mnemonic: String? = null
set(newValue) { field = newValue; updateMnemonicTextView() }
@ -42,9 +42,9 @@ class KeyPairActivity : BaseActionBarActivity() {
// region Lifecycle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_key_pair)
setContentView(R.layout.activity_seed)
setUpLanguageFileDirectory()
updateKeyPair()
updateSeed()
copyButton.setOnClickListener { copy() }
toggleModeButton.setOnClickListener { toggleMode() }
registerOrRestoreButton.setOnClickListener { registerOrRestore() }
@ -75,9 +75,8 @@ class KeyPairActivity : BaseActionBarActivity() {
// endregion
// region Updating
private fun updateKeyPair() {
IdentityKeyUtil.generateIdentityKeys(this)
keyPair = IdentityKeyUtil.getIdentityKeyPair(this)
private fun updateSeed() {
seed = Curve25519.getInstance("best").generateSeed(16)
}
private fun updateUI() {
@ -100,7 +99,8 @@ class KeyPairActivity : BaseActionBarActivity() {
}
private fun updateMnemonic() {
mnemonic = MnemonicCodec(languageFileDirectory).encode(keyPair!!.hexEncodedPrivateKey)
val hexEncodedSeed = Hex.toStringCondensed(seed)
mnemonic = MnemonicCodec(languageFileDirectory).encode(hexEncodedSeed)
}
private fun updateMnemonicTextView() {
@ -124,21 +124,24 @@ class KeyPairActivity : BaseActionBarActivity() {
}
private fun registerOrRestore() {
val keyPair: IdentityKeyPair
var seed: ByteArray
when (mode) {
Mode.Register -> keyPair = this.keyPair!!
Mode.Register -> seed = this.seed!!
Mode.Restore -> {
val mnemonic = mnemonicEditText.text.toString()
try {
val hexEncodedPrivateKey = MnemonicCodec(languageFileDirectory).decode(mnemonic)
IdentityKeyUtil.generateIdentityKeyPair(this, hexEncodedPrivateKey)
keyPair = IdentityKeyUtil.getIdentityKeyPair(this)
val hexEncodedSeed = MnemonicCodec(languageFileDirectory).decode(mnemonic)
seed = Hex.fromStringCondensed(hexEncodedSeed)
} catch (e: Exception) {
val message = if (e is MnemonicCodec.DecodingError) e.description else MnemonicCodec.DecodingError.Generic.description
return Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
}
IdentityKeyUtil.save(this, IdentityKeyUtil.lokiSeedKey, Hex.toStringCondensed(seed))
if (seed.count() == 16) seed = seed + seed
IdentityKeyUtil.generateIdentityKeyPair(this, seed)
val keyPair = IdentityKeyUtil.getIdentityKeyPair(this)
val publicKey = keyPair.publicKey
val hexEncodedPublicKey = keyPair.hexEncodedPublicKey
val registrationID = KeyHelper.generateRegistrationId(false)