Skip to content

Commit

Permalink
Remote wipe: user interface
Browse files Browse the repository at this point in the history
  • Loading branch information
M66B committed Mar 6, 2022
1 parent d4a93de commit 94ed34b
Show file tree
Hide file tree
Showing 31 changed files with 9,782 additions and 1 deletion.
1 change: 1 addition & 0 deletions ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ FairEmail uses:
* [Cousine font](https://fonts.google.com/specimen/Cousine). By Steve Matteson. [Apache License 2.0](https://fonts.google.com/specimen/Cousine#license).
* [Lato font](https://fonts.google.com/specimen/Lato). By Łukasz Dziedzic. [Apache License 2.0](https://fonts.google.com/specimen/Lato#license).
* [Caladea font](https://fonts.google.com/specimen/Caladea). By Andrés Torresi, Carolina Giovanolli. [Apache License 2.0](https://fonts.google.com/specimen/Caladea#license).
* [BIP39](https://github.com/NovaCrypto/BIP39). Copyright (C) 2017-2019 Alan Evans, NovaCrypto. [GNU General Public License v3.0](https://github.com/NovaCrypto/BIP39/blob/master/LICENCE.txt).
1 change: 1 addition & 0 deletions app/src/main/assets/ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ FairEmail uses:
* [Cousine font](https://fonts.google.com/specimen/Cousine). By Steve Matteson. [Apache License 2.0](https://fonts.google.com/specimen/Cousine#license).
* [Lato font](https://fonts.google.com/specimen/Lato). By Łukasz Dziedzic. [Apache License 2.0](https://fonts.google.com/specimen/Lato#license).
* [Caladea font](https://fonts.google.com/specimen/Caladea). By Andrés Torresi, Carolina Giovanolli. [Apache License 2.0](https://fonts.google.com/specimen/Caladea#license).
* [BIP39](https://github.com/NovaCrypto/BIP39). Copyright (C) 2017-2019 Alan Evans, NovaCrypto. [GNU General Public License v3.0](https://github.com/NovaCrypto/BIP39/blob/master/LICENCE.txt).
39 changes: 38 additions & 1 deletion app/src/main/java/eu/faircode/email/FragmentOptionsPrivacy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/

import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
Expand All @@ -44,6 +46,7 @@
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -93,7 +96,9 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
private SwitchCompat swDisconnectAutoUpdate;
private SwitchCompat swDisconnectLinks;
private SwitchCompat swDisconnectImages;
private SwitchCompat swMnemonic;
private Button btnClearAll;
private TextView tvMnemonic;

private Group grpSafeBrowsing;

Expand All @@ -104,7 +109,8 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
"pin", "biometrics", "biometrics_timeout", "autolock", "autolock_nav",
"client_id", "display_hidden", "incognito_keyboard", "secure",
"generic_ua", "safe_browsing", "load_emoji",
"disconnect_auto_update", "disconnect_links", "disconnect_images"
"disconnect_auto_update", "disconnect_links", "disconnect_images",
"wipe_mnemonic"
};

@Override
Expand Down Expand Up @@ -151,7 +157,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
swDisconnectAutoUpdate = view.findViewById(R.id.swDisconnectAutoUpdate);
swDisconnectLinks = view.findViewById(R.id.swDisconnectLinks);
swDisconnectImages = view.findViewById(R.id.swDisconnectImages);
swMnemonic = view.findViewById(R.id.swMnemonic);
btnClearAll = view.findViewById(R.id.btnClearAll);
tvMnemonic = view.findViewById(R.id.tvMnemonic);

grpSafeBrowsing = view.findViewById(R.id.grpSafeBrowsing);

Expand Down Expand Up @@ -446,6 +454,31 @@ public void onClick(View v) {
}
});

swMnemonic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
byte[] entropy = MnemonicHelper.generate();
String mnemonic = MnemonicHelper.get(entropy);
prefs.edit().putString("wipe_mnemonic", Helper.hex(entropy)).apply();
tvMnemonic.setText(mnemonic);

Context context = compoundButton.getContext();
ClipboardManager cbm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (cbm == null)
return;

ClipData clip = ClipData.newPlainText(getString(R.string.app_name), mnemonic);
cbm.setPrimaryClip(clip);
ToastEx.makeText(context, R.string.title_clipboard_copied, Toast.LENGTH_LONG).show();

} else {
prefs.edit().remove("wipe_mnemonic").apply();
tvMnemonic.setText(null);
}
}
});

// Initialize
FragmentDialogTheme.setBackground(getContext(), view, false);

Expand Down Expand Up @@ -546,6 +579,10 @@ private void setOptions() {
swDisconnectAutoUpdate.setChecked(prefs.getBoolean("disconnect_auto_update", false));
swDisconnectLinks.setChecked(prefs.getBoolean("disconnect_links", true));
swDisconnectImages.setChecked(prefs.getBoolean("disconnect_images", false));

String mnemonic = prefs.getString("wipe_mnemonic", null);
swMnemonic.setChecked(mnemonic != null);
tvMnemonic.setText(mnemonic == null ? null : MnemonicHelper.get(mnemonic));
}

public static class FragmentDialogPin extends FragmentDialogBase {
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/eu/faircode/email/MnemonicHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package eu.faircode.email;

/*
This file is part of FairEmail.
FairEmail 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.
FairEmail 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 FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/

import java.security.SecureRandom;

import io.github.novacrypto.bip39.MnemonicGenerator;
import io.github.novacrypto.bip39.Words;
import io.github.novacrypto.bip39.wordlists.English;

public class MnemonicHelper {
// https://github.com/NovaCrypto/BIP39

static String get(byte[] entropy) {
StringBuilder sb = new StringBuilder();
new SecureRandom().nextBytes(entropy);
new MnemonicGenerator(English.INSTANCE).createMnemonic(entropy, sb::append);
return sb.toString();
}

static String get(String hex) {
return get(fromHex(hex));
}

static byte[] generate() {
byte[] entropy = new byte[Words.TWELVE.byteLength()];
new SecureRandom().nextBytes(entropy);
return entropy;
}

private static byte[] fromHex(String hex) {
int len = hex.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] =
(byte) ((Character.digit(hex.charAt(i), 16) << 4) +
Character.digit(hex.charAt(i + 1), 16));
}
return data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* BIP39 library, a Java implementation of BIP39
* Copyright (C) 2017-2019 Alan Evans, NovaCrypto
*
* 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 <https://www.gnu.org/licenses/>.
*
* Original source: https://github.com/NovaCrypto/BIP39
* You can contact the authors via github issues.
*/

package io.github.novacrypto.bip39;

import java.util.Comparator;

enum CharSequenceComparators implements Comparator<CharSequence> {

ALPHABETICAL {
@Override
public int compare(final CharSequence o1, final CharSequence o2) {
final int length1 = o1.length();
final int length2 = o2.length();
final int length = Math.min(length1, length2);
for (int i = 0; i < length; i++) {
final int compare = Character.compare(o1.charAt(i), o2.charAt(i));
if (compare != 0) return compare;
}
return Integer.compare(length1, length2);
}
}

}
58 changes: 58 additions & 0 deletions app/src/main/java/io/github/novacrypto/bip39/ByteUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* BIP39 library, a Java implementation of BIP39
* Copyright (C) 2017-2019 Alan Evans, NovaCrypto
*
* 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 <https://www.gnu.org/licenses/>.
*
* Original source: https://github.com/NovaCrypto/BIP39
* You can contact the authors via github issues.
*/

package io.github.novacrypto.bip39;

final class ByteUtils {

static int next11Bits(byte[] bytes, int offset) {
final int skip = offset / 8;
final int lowerBitsToRemove = (3 * 8 - 11) - (offset % 8);
return (((int) bytes[skip] & 0xff) << 16 |
((int) bytes[skip + 1] & 0xff) << 8 |
(lowerBitsToRemove < 8
? ((int) bytes[skip + 2] & 0xff)
: 0)) >> lowerBitsToRemove & (1 << 11) - 1;
}

static void writeNext11(byte[] bytes, int value, int offset) {
int skip = offset / 8;
int bitSkip = offset % 8;
{//byte 0
byte firstValue = bytes[skip];
byte toWrite = (byte) (value >> (3 + bitSkip));
bytes[skip] = (byte) (firstValue | toWrite);
}

{//byte 1
byte valueInByte = bytes[skip + 1];
final int i = 5 - bitSkip;
byte toWrite = (byte) (i > 0 ? (value << i) : (value >> -i));
bytes[skip + 1] = (byte) (valueInByte | toWrite);
}

if (bitSkip >= 6) {//byte 2
byte valueInByte = bytes[skip + 2];
byte toWrite = (byte) (value << 13 - bitSkip);
bytes[skip + 2] = (byte) (valueInByte | toWrite);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* BIP39 library, a Java implementation of BIP39
* Copyright (C) 2017-2019 Alan Evans, NovaCrypto
*
* 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 <https://www.gnu.org/licenses/>.
*
* Original source: https://github.com/NovaCrypto/BIP39
* You can contact the authors via github issues.
*/

package io.github.novacrypto.bip39;

import java.util.LinkedList;
import java.util.List;

final class CharSequenceSplitter {

private final char separator1;
private final char separator2;

CharSequenceSplitter(final char separator1, final char separator2) {
this.separator1 = separator1;
this.separator2 = separator2;
}

List<CharSequence> split(final CharSequence charSequence) {
final LinkedList<CharSequence> list = new LinkedList<>();
int start = 0;
final int length = charSequence.length();
for (int i = 0; i < length; i++) {
final char c = charSequence.charAt(i);
if (c == separator1 || c == separator2) {
list.add(charSequence.subSequence(start, i));
start = i + 1;
}
}
list.add(charSequence.subSequence(start, length));
return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* BIP39 library, a Java implementation of BIP39
* Copyright (C) 2017-2019 Alan Evans, NovaCrypto
*
* 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 <https://www.gnu.org/licenses/>.
*
* Original source: https://github.com/NovaCrypto/BIP39
* You can contact the authors via github issues.
*/

package io.github.novacrypto.bip39;

import io.github.novacrypto.toruntime.CheckedExceptionToRuntime;

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

import static io.github.novacrypto.toruntime.CheckedExceptionToRuntime.toRuntime;

/**
* Not available in all Java implementations, for example will not find the implementation before Android API 26+.
* See https://developer.android.com/reference/javax/crypto/SecretKeyFactory.html for more details.
*/
public enum JavaxPBKDF2WithHmacSHA512 implements PBKDF2WithHmacSHA512 {
INSTANCE;

private SecretKeyFactory skf = getPbkdf2WithHmacSHA512();

@Override
public byte[] hash(char[] chars, byte[] salt) {
final PBEKeySpec spec = new PBEKeySpec(chars, salt, 2048, 512);
final byte[] encoded = generateSecretKey(spec).getEncoded();
spec.clearPassword();
return encoded;
}

private SecretKey generateSecretKey(final PBEKeySpec spec) {
return toRuntime(new CheckedExceptionToRuntime.Func<SecretKey>() {
@Override
public SecretKey run() throws Exception {
return skf.generateSecret(spec);
}
});
}

private static SecretKeyFactory getPbkdf2WithHmacSHA512() {
return toRuntime(new CheckedExceptionToRuntime.Func<SecretKeyFactory>() {
@Override
public SecretKeyFactory run() throws Exception {
return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
}
});
}
}
Loading

0 comments on commit 94ed34b

Please sign in to comment.