2017-08-06 18:15:47 +02:00
|
|
|
package me.impy.aegis;
|
|
|
|
|
2017-08-13 23:38:38 +02:00
|
|
|
import android.Manifest;
|
|
|
|
import android.content.Context;
|
2017-08-18 22:12:45 +02:00
|
|
|
import android.content.DialogInterface;
|
2017-08-06 18:15:47 +02:00
|
|
|
import android.content.Intent;
|
2017-08-13 23:38:38 +02:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.hardware.fingerprint.FingerprintManager;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.support.v4.app.ActivityCompat;
|
2017-08-18 22:12:45 +02:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2017-08-06 18:15:47 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
2017-08-13 23:38:38 +02:00
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
2017-08-06 18:15:47 +02:00
|
|
|
|
2017-08-19 16:48:57 +02:00
|
|
|
import com.mattprecious.swirl.SwirlView;
|
|
|
|
|
2017-08-06 18:15:47 +02:00
|
|
|
import java.lang.reflect.UndeclaredThrowableException;
|
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
|
2017-08-13 23:38:38 +02:00
|
|
|
import me.impy.aegis.crypto.KeyStoreHandle;
|
2017-08-06 18:15:47 +02:00
|
|
|
import me.impy.aegis.crypto.MasterKey;
|
2017-08-13 23:38:38 +02:00
|
|
|
import me.impy.aegis.crypto.slots.FingerprintSlot;
|
2017-08-06 18:15:47 +02:00
|
|
|
import me.impy.aegis.crypto.slots.PasswordSlot;
|
|
|
|
import me.impy.aegis.crypto.slots.Slot;
|
|
|
|
import me.impy.aegis.crypto.slots.SlotCollection;
|
2017-08-13 23:38:38 +02:00
|
|
|
import me.impy.aegis.finger.FingerprintUiHelper;
|
|
|
|
import me.impy.aegis.helpers.AuthHelper;
|
2017-08-06 18:15:47 +02:00
|
|
|
|
2017-11-27 18:24:55 +01:00
|
|
|
public class AuthActivity extends AppCompatActivity implements FingerprintUiHelper.Callback, SlotCollectionTask.Callback {
|
2017-08-06 18:15:47 +02:00
|
|
|
public static final int RESULT_OK = 0;
|
|
|
|
public static final int RESULT_EXCEPTION = 1;
|
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
private EditText _textPassword;
|
2017-08-06 18:15:47 +02:00
|
|
|
|
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
|
|
|
|
2017-08-06 18:15:47 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_auth);
|
2017-08-26 21:15:53 +02:00
|
|
|
_textPassword = (EditText) findViewById(R.id.text_password);
|
|
|
|
LinearLayout boxFingerprint = (LinearLayout) findViewById(R.id.box_fingerprint);
|
|
|
|
SwirlView imgFingerprint = (SwirlView) findViewById(R.id.img_fingerprint);
|
|
|
|
TextView textFingerprint = (TextView) findViewById(R.id.text_fingerprint);
|
2017-08-06 18:15:47 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
Context context = getApplicationContext();
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
FingerprintManager manager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
|
|
|
|
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED && manager.isHardwareDetected()) {
|
2017-08-26 21:15:53 +02:00
|
|
|
if (_slots.has(FingerprintSlot.class)) {
|
2017-08-13 23:38:38 +02:00
|
|
|
try {
|
|
|
|
KeyStoreHandle handle = new KeyStoreHandle();
|
|
|
|
if (handle.keyExists()) {
|
|
|
|
SecretKey key = handle.getKey();
|
2017-08-26 21:15:53 +02:00
|
|
|
_fingerCipher = Slot.createCipher(key, Cipher.DECRYPT_MODE);
|
|
|
|
_fingerHelper = new FingerprintUiHelper(manager, imgFingerprint, textFingerprint, this);
|
2017-08-13 23:38:38 +02:00
|
|
|
boxFingerprint.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new UndeclaredThrowableException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-06 18:15:47 +02:00
|
|
|
|
|
|
|
Button button = (Button) findViewById(R.id.button_decrypt);
|
|
|
|
button.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2017-11-27 18:24:55 +01:00
|
|
|
char[] password = AuthHelper.getPassword(_textPassword, true);
|
|
|
|
trySlots(PasswordSlot.class, password);
|
2017-08-06 18:15:47 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-19 13:50:33 +02:00
|
|
|
private void showError() {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
2017-12-03 22:06:03 +01:00
|
|
|
builder.setTitle("Decryption error");
|
2017-08-19 13:50:33 +02:00
|
|
|
builder.setMessage("Master key integrity check failed for every slot. Make sure you didn't mistype your password.");
|
|
|
|
builder.setCancelable(false);
|
|
|
|
builder.setPositiveButton("OK",
|
2017-08-18 22:12:45 +02:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int id) {
|
|
|
|
dialog.cancel();
|
|
|
|
}
|
|
|
|
});
|
2017-08-19 13:50:33 +02:00
|
|
|
builder.create().show();
|
|
|
|
}
|
|
|
|
|
2017-11-27 18:24:55 +01:00
|
|
|
private <T extends Slot> void trySlots(Class<T> type, Object obj) {
|
2017-12-03 22:06:03 +01:00
|
|
|
new SlotCollectionTask<>(type, this, this).execute(new SlotCollectionTask.Params(){{
|
2017-11-27 18:24:55 +01:00
|
|
|
Slots = _slots;
|
|
|
|
Obj = obj;
|
|
|
|
}});
|
2017-08-19 13:50:33 +02:00
|
|
|
}
|
2017-08-18 22:12:45 +02:00
|
|
|
|
2017-08-19 13:50:33 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-08-13 19:55:53 +02:00
|
|
|
@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-06 18:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 23:38:38 +02:00
|
|
|
@Override
|
|
|
|
public void onAuthenticated() {
|
2017-11-27 18:24:55 +01:00
|
|
|
trySlots(FingerprintSlot.class, _fingerCipher);
|
2017-08-13 23:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError() {
|
|
|
|
|
2017-08-06 18:15:47 +02:00
|
|
|
}
|
2017-11-27 18:24:55 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTaskFinished(MasterKey key) {
|
|
|
|
if (key != null) {
|
|
|
|
setKey(key);
|
|
|
|
} else {
|
|
|
|
showError();
|
|
|
|
}
|
|
|
|
}
|
2017-08-06 18:15:47 +02:00
|
|
|
}
|