Support plain text databases

This commit is contained in:
Alexander Bakker 2017-08-06 21:45:27 +02:00
parent 722ea50b68
commit 3e2bb5b0b3
12 changed files with 160 additions and 260 deletions

View file

@ -1,5 +1,6 @@
package me.impy.aegis;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
@ -22,6 +23,7 @@ import me.impy.aegis.crypto.slots.PasswordSlot;
import me.impy.aegis.crypto.slots.Slot;
public class CustomAuthenticatedSlide extends SlideFragment {
private int cryptType;
private EditText textPassword;
private EditText textPasswordConfirm;
@ -33,6 +35,29 @@ public class CustomAuthenticatedSlide extends SlideFragment {
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (!isVisibleToUser) {
return;
}
Intent intent = getActivity().getIntent();
cryptType = intent.getIntExtra("cryptType", 1337);
switch(cryptType) {
case CustomAuthenticationSlide.CRYPT_TYPE_NONE:
break;
case CustomAuthenticationSlide.CRYPT_TYPE_PASS:
break;
case CustomAuthenticationSlide.CRYPT_TYPE_FINGER:
break;
default:
throw new RuntimeException();
}
}
@Override
public int backgroundColor() {
return R.color.colorHeaderSuccess;
@ -45,12 +70,21 @@ public class CustomAuthenticatedSlide extends SlideFragment {
@Override
public boolean canMoveFurther() {
char[] password = getEditTextChars(textPassword);
char[] passwordConfirm = getEditTextChars(textPasswordConfirm);
boolean equal = password.length != 0 && Arrays.equals(password, passwordConfirm);
CryptoUtils.zero(password);
CryptoUtils.zero(passwordConfirm);
return equal;
switch(cryptType) {
case CustomAuthenticationSlide.CRYPT_TYPE_NONE:
return true;
case CustomAuthenticationSlide.CRYPT_TYPE_PASS:
char[] password = getEditTextChars(textPassword);
char[] passwordConfirm = getEditTextChars(textPasswordConfirm);
boolean equal = password.length != 0 && Arrays.equals(password, passwordConfirm);
CryptoUtils.zero(password);
CryptoUtils.zero(passwordConfirm);
return equal;
case CustomAuthenticationSlide.CRYPT_TYPE_FINGER:
return false;
default:
throw new RuntimeException();
}
}
@Override
@ -58,6 +92,10 @@ public class CustomAuthenticatedSlide extends SlideFragment {
return "Passwords should be equal and non-empty";
}
public int getCryptType() {
return cryptType;
}
public Cipher getCipher(PasswordSlot slot, int mode)
throws InvalidKeySpecException, NoSuchAlgorithmException,
InvalidKeyException, NoSuchPaddingException {

View file

@ -1,35 +1,77 @@
package me.impy.aegis;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.ActivityCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import agency.tango.materialintroscreen.SlideFragment;
public class CustomAuthenticationSlide extends SlideFragment {
public static final int CRYPT_TYPE_NONE = 0;
public static final int CRYPT_TYPE_PASS = 1;
public static final int CRYPT_TYPE_FINGER = 2;
private RadioGroup buttonGroup;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_authentication_slide, container, false);
buttonGroup = (RadioGroup) view.findViewById(R.id.rg_authenticationMethod);
final Context context = getContext();
RadioButton button = (RadioButton) view.findViewById(R.id.rb_fingerprint);
button.setOnClickListener(new View.OnClickListener() {
buttonGroup = (RadioGroup) view.findViewById(R.id.rg_authenticationMethod);
buttonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onClick(View v) {
if (canMoveFurther()) {
buttonGroup.clearCheck();
Toast.makeText(CustomAuthenticationSlide.this.getActivity(), "Fingerprint is not supported yet", Toast.LENGTH_SHORT).show();
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
if (checkedId == -1) {
return;
}
int id;
switch (checkedId) {
case R.id.rb_none:
id = CRYPT_TYPE_NONE;
break;
case R.id.rb_password:
id = CRYPT_TYPE_PASS;
break;
case R.id.rb_fingerprint:
id = CRYPT_TYPE_FINGER;
// TODO: remove this
group.clearCheck();
Toast.makeText(context, "Fingerprint is not supported yet", Toast.LENGTH_SHORT).show();
break;
default:
throw new RuntimeException();
}
Intent intent = getActivity().getIntent();
intent.putExtra("cryptType", id);
}
});
// only show the fingerprint option if the api version is new enough, permission is granted and a scanner is found
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
FingerprintManager fingerprintManager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED && fingerprintManager.isHardwareDetected()) {
RadioButton button = (RadioButton) view.findViewById(R.id.rb_fingerprint);
TextView text = (TextView) view.findViewById(R.id.text_rb_fingerprint);
button.setVisibility(View.VISIBLE);
text.setVisibility(View.VISIBLE);
}
}
return view;
}

View file

@ -32,6 +32,7 @@ public class IntroActivity extends MaterialIntroActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideBackButton();
addSlide(new SlideFragmentBuilder()
.backgroundColor(R.color.colorPrimary)
@ -84,24 +85,48 @@ public class IntroActivity extends MaterialIntroActivity {
Database database = new Database();
DatabaseFile databaseFile = new DatabaseFile();
MasterKey masterKey;
int cryptType = authenticatedSlide.getCryptType();
// generate the master key
MasterKey masterKey = null;
if (cryptType != CustomAuthenticationSlide.CRYPT_TYPE_NONE) {
try {
masterKey = MasterKey.generate();
} catch (Exception e) {
setException(e);
return;
}
}
if (cryptType != CustomAuthenticationSlide.CRYPT_TYPE_NONE) {
try {
// encrypt the master key with a key derived from the user's password
// and add it to the list of slots
SlotCollection slots = databaseFile.getSlots();
PasswordSlot slot = new PasswordSlot();
Cipher cipher = authenticatedSlide.getCipher(slot, Cipher.ENCRYPT_MODE);
masterKey.encryptSlot(slot, cipher);
slots.add(slot);
} catch (Exception e) {
setException(e);
return;
}
}
if (cryptType != CustomAuthenticationSlide.CRYPT_TYPE_FINGER) {
// TODO
}
// finally, save the database
try {
// generate the master key
masterKey = MasterKey.generate();
// encrypt the master key with a key derived from the user's password
// and add it to the list of slots
SlotCollection slots = databaseFile.getSlots();
PasswordSlot slot = new PasswordSlot();
Cipher cipher = authenticatedSlide.getCipher(slot, Cipher.ENCRYPT_MODE);
masterKey.encryptSlot(slot, cipher);
slots.add(slot);
// finally, save the database
byte[] bytes = database.serialize();
CryptResult result = masterKey.encrypt(bytes);
databaseFile.setContent(result.Data);
databaseFile.setCryptParameters(result.Parameters);
if (cryptType == CustomAuthenticationSlide.CRYPT_TYPE_NONE) {
databaseFile.setContent(bytes);
} else {
CryptResult result = masterKey.encrypt(bytes);
databaseFile.setContent(result.Data);
databaseFile.setCryptParameters(result.Parameters);
}
databaseFile.save(getApplicationContext());
} catch (Exception e) {
setException(e);

View file

@ -84,6 +84,8 @@ public class MainActivity extends AppCompatActivity {
Intent intent = new Intent(this, AuthActivity.class);
intent.putExtra("slots", db.getFile().getSlots());
startActivityForResult(intent, CODE_DECRYPT);
} else {
loadKeyProfiles();
}
}

View file

@ -40,9 +40,13 @@ public class DatabaseManager {
public void save() throws Exception {
assertDecrypted();
byte[] bytes = _db.serialize();
CryptResult result = _key.encrypt(bytes);
_file.setContent(result.Data);
_file.setCryptParameters(result.Parameters);
if (!_file.isEncrypted()) {
_file.setContent(bytes);
} else {
CryptResult result = _key.encrypt(bytes);
_file.setContent(result.Data);
_file.setCryptParameters(result.Parameters);
}
_file.save(_context);
}