Aegis/app/src/main/java/me/impy/aegis/ui/AuthActivity.java

178 lines
6.1 KiB
Java
Raw Normal View History

package me.impy.aegis.ui;
import android.content.Intent;
2017-08-13 23:38:38 +02:00
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
2017-08-13 23:38:38 +02:00
import android.widget.LinearLayout;
import android.widget.TextView;
import com.mattprecious.swirl.SwirlView;
import java.lang.reflect.UndeclaredThrowableException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import me.impy.aegis.R;
2017-08-13 23:38:38 +02:00
import me.impy.aegis.crypto.KeyStoreHandle;
import me.impy.aegis.crypto.MasterKey;
import me.impy.aegis.db.slots.FingerprintSlot;
import me.impy.aegis.db.slots.PasswordSlot;
import me.impy.aegis.db.slots.Slot;
import me.impy.aegis.db.slots.SlotCollection;
import me.impy.aegis.helpers.FingerprintHelper;
import me.impy.aegis.helpers.FingerprintUiHelper;
import me.impy.aegis.helpers.EditTextHelper;
import me.impy.aegis.ui.tasks.SlotCollectionTask;
2017-12-24 21:42:08 +01:00
public class AuthActivity extends AegisActivity implements FingerprintUiHelper.Callback, SlotCollectionTask.Callback {
2017-08-26 21:15:53 +02:00
private EditText _textPassword;
2017-08-26 21:15:53 +02:00
private SlotCollection _slots;
private FingerprintUiHelper _fingerHelper;
private Cipher _fingerCipher;
2017-08-13 23:38:38 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
_textPassword = findViewById(R.id.text_password);
LinearLayout boxFingerprint = findViewById(R.id.box_fingerprint);
LinearLayout boxFingerprintInfo = findViewById(R.id.box_fingerprint_info);
TextView textFingerprint = findViewById(R.id.text_fingerprint);
SwirlView imgFingerprint = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ViewGroup insertPoint = findViewById(R.id.img_fingerprint_insert);
imgFingerprint = new SwirlView(this);
insertPoint.addView(imgFingerprint, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
Intent intent = getIntent();
2017-08-26 21:15:53 +02:00
_slots = (SlotCollection) intent.getSerializableExtra("slots");
2017-08-13 23:38:38 +02:00
// only show the fingerprint controls if the api version is new enough, permission is granted, a scanner is found and a fingerprint slot is found
FingerprintManager manager = FingerprintHelper.getManager(this);
if (manager != null && _slots.has(FingerprintSlot.class)) {
boolean invalidated = false;
try {
2018-02-10 17:20:41 +01:00
// find a fingerprint slot with an id that matches an alias in the keystore
for (FingerprintSlot slot : _slots.findAll(FingerprintSlot.class)) {
String id = slot.getUUID().toString();
2018-02-10 17:20:41 +01:00
KeyStoreHandle handle = new KeyStoreHandle();
if (handle.containsKey(id)) {
SecretKey key = handle.getKey(id);
// if 'key' is null, it was permanently invalidated
if (key == null) {
invalidated = true;
continue;
}
2018-02-10 17:20:41 +01:00
_fingerCipher = Slot.createCipher(key, Cipher.DECRYPT_MODE);
_fingerHelper = new FingerprintUiHelper(manager, imgFingerprint, textFingerprint, this);
boxFingerprint.setVisibility(View.VISIBLE);
invalidated = false;
2018-02-10 17:20:41 +01:00
break;
}
2017-08-13 23:38:38 +02:00
}
} catch (Exception e) {
throw new UndeclaredThrowableException(e);
2017-08-13 23:38:38 +02:00
}
// display a help message if a matching invalidated keystore entry was found
if (invalidated) {
boxFingerprintInfo.setVisibility(View.VISIBLE);
}
2017-08-13 23:38:38 +02:00
}
Button button = findViewById(R.id.button_decrypt);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
char[] password = EditTextHelper.getEditTextChars(_textPassword);
trySlots(PasswordSlot.class, password);
}
});
}
2017-12-24 21:42:08 +01:00
@Override
protected void setPreferredTheme(boolean nightMode) {
if (nightMode) {
setTheme(R.style.AppTheme_Dark);
} else {
setTheme(R.style.AppTheme_Default);
}
}
private void showError() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Decryption error");
builder.setMessage("Master key integrity check failed for every slot. Make sure you didn't mistype your password.");
builder.setCancelable(false);
builder.setPositiveButton(android.R.string.ok, null);
builder.create().show();
}
private <T extends Slot> void trySlots(Class<T> type, Object obj) {
new SlotCollectionTask<>(type, this, this).execute(new SlotCollectionTask.Params(){{
Slots = _slots;
Obj = obj;
}});
}
private void setKey(MasterKey key) {
2017-08-13 23:38:38 +02:00
// send the master key back to the main activity
Intent result = new Intent();
result.putExtra("key", key);
setResult(RESULT_OK, result);
finish();
}
@Override
public void onBackPressed() {
// ignore back button presses
}
2017-08-13 23:38:38 +02:00
@Override
public void onResume() {
super.onResume();
2017-08-26 21:15:53 +02:00
if (_fingerHelper != null) {
_fingerHelper.startListening(new FingerprintManager.CryptoObject(_fingerCipher));
2017-08-13 23:38:38 +02:00
}
}
@Override
public void onPause() {
super.onPause();
2017-08-26 21:15:53 +02:00
if (_fingerHelper != null) {
_fingerHelper.stopListening();
}
}
2017-08-13 23:38:38 +02:00
@Override
public void onAuthenticated() {
trySlots(FingerprintSlot.class, _fingerCipher);
2017-08-13 23:38:38 +02:00
}
@Override
public void onError() {
}
@Override
public void onTaskFinished(MasterKey key) {
if (key != null) {
setKey(key);
} else {
showError();
}
}
}