implement saving with KeepassXC-Challenge (#4)
This commit is contained in:
@@ -6,7 +6,7 @@ using Exception = System.Exception;
|
||||
|
||||
namespace keepass2android
|
||||
{
|
||||
class ChallengeXCKey : IUserKey, ISeedBasedUserKey
|
||||
public class ChallengeXCKey : IUserKey, ISeedBasedUserKey
|
||||
{
|
||||
private readonly int _requestCode;
|
||||
|
||||
@@ -51,9 +51,15 @@ namespace keepass2android
|
||||
Thread.Sleep(50);
|
||||
}
|
||||
if (Error != null)
|
||||
throw new Exception("YubiChallenge failed: " + Error);
|
||||
{
|
||||
var error = Error;
|
||||
Error = null;
|
||||
throw new Exception("YubiChallenge failed: " + error);
|
||||
}
|
||||
|
||||
return new ProtectedBinary(true, CryptoUtil.HashSha256(Response));
|
||||
var result = CryptoUtil.HashSha256(Response);
|
||||
Response = null;
|
||||
return new ProtectedBinary(true, result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -167,7 +167,7 @@ namespace keepass2android
|
||||
|
||||
}
|
||||
|
||||
if (resultCode == Result.Ok)
|
||||
if ((GroupEditActivity.RequestCodeGroupEdit == requestCode) && (resultCode == Result.Ok))
|
||||
{
|
||||
String groupName = data.Extras.GetString(GroupEditActivity.KeyName);
|
||||
int groupIconId = data.Extras.GetInt(GroupEditActivity.KeyIconId);
|
||||
|
||||
@@ -52,6 +52,8 @@ namespace keepass2android
|
||||
|
||||
}
|
||||
|
||||
public const int RequestCodeGroupEdit = 9713;
|
||||
|
||||
|
||||
public static void Launch(Activity act, PwGroup parentGroup)
|
||||
{
|
||||
@@ -60,7 +62,7 @@ namespace keepass2android
|
||||
PwGroup parent = parentGroup;
|
||||
i.PutExtra(KeyParent, parent.Uuid.ToHexString());
|
||||
|
||||
act.StartActivityForResult(i, 0);
|
||||
act.StartActivityForResult(i, RequestCodeGroupEdit);
|
||||
}
|
||||
|
||||
public static void Launch(Activity act, PwGroup parentGroup, PwGroup groupToEdit)
|
||||
|
||||
@@ -49,6 +49,8 @@ namespace keepass2android
|
||||
if (xcKey != null)
|
||||
{
|
||||
xcKey.Activity = this;
|
||||
_currentlyWaitingKey = xcKey;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -64,7 +66,8 @@ namespace keepass2android
|
||||
if (xcKey != null)
|
||||
{
|
||||
//don't store a pointer to this activity in the static database object to avoid memory leak
|
||||
xcKey.Activity = null;
|
||||
if (xcKey.Activity == this) //don't reset if another activity has come to foreground already
|
||||
xcKey.Activity = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -88,6 +91,37 @@ namespace keepass2android
|
||||
TimeoutHelper.Resume(this);
|
||||
}
|
||||
|
||||
public const int RequestCodeChallengeYubikey = 793;
|
||||
|
||||
protected ChallengeXCKey _currentlyWaitingKey;
|
||||
|
||||
|
||||
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
|
||||
{
|
||||
base.OnActivityResult(requestCode, resultCode, data);
|
||||
if ((requestCode == RequestCodeChallengeYubikey) && (_currentlyWaitingKey != null))
|
||||
{
|
||||
if (resultCode == Result.Ok)
|
||||
{
|
||||
byte[] challengeResponse = data.GetByteArrayExtra("response");
|
||||
if ((challengeResponse != null) && (challengeResponse.Length > 0))
|
||||
{
|
||||
_currentlyWaitingKey.Response = challengeResponse;
|
||||
}
|
||||
else
|
||||
_currentlyWaitingKey.Error = "Did not receive a valid response.";
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentlyWaitingKey.Error = "Cancelled Yubichallenge.";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Intent TryGetYubichallengeIntentOrPrompt(byte[] challenge, bool promptToInstall)
|
||||
{
|
||||
|
||||
@@ -110,7 +110,7 @@ namespace keepass2android
|
||||
|
||||
private const int RequestCodePrepareDbFile = 1000;
|
||||
private const int RequestCodePrepareOtpAuxFile = 1001;
|
||||
private const int RequestCodeChallengeYubikey = 1002;
|
||||
|
||||
private const int RequestCodeSelectKeyfile = 1003;
|
||||
private const int RequestCodePrepareKeyFile = 1004;
|
||||
private const int RequestCodeSelectAuxFile = 1005;
|
||||
@@ -119,8 +119,6 @@ namespace keepass2android
|
||||
private Task<MemoryStream> _loadDbFileTask;
|
||||
private bool _loadDbTaskOffline; //indicate if preloading was started with offline mode
|
||||
|
||||
private ChallengeXCKey _currentlyWaitingKey;
|
||||
|
||||
private IOConnectionInfo _ioConnection;
|
||||
private String _keyFileOrProvider;
|
||||
bool _showPassword;
|
||||
@@ -371,39 +369,24 @@ namespace keepass2android
|
||||
}
|
||||
if (requestCode == RequestCodeChallengeYubikey)
|
||||
{
|
||||
if (resultCode == Result.Ok)
|
||||
if (_currentlyWaitingKey != null)
|
||||
{
|
||||
//ActivityResult was handled in base class already
|
||||
return;
|
||||
}
|
||||
|
||||
if (resultCode == Result.Ok)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
byte[] challengeResponse = data.GetByteArrayExtra("response");
|
||||
if (_currentlyWaitingKey != null)
|
||||
{
|
||||
if ((challengeResponse != null) && (challengeResponse.Length > 0))
|
||||
{
|
||||
_currentlyWaitingKey.Response = challengeResponse;
|
||||
}
|
||||
else
|
||||
_currentlyWaitingKey.Error = "Did not receive a valid response.";
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
_challengeProv = new KeeChallengeProv();
|
||||
_challengeSecret = _challengeProv.GetSecret(_chalInfo, challengeResponse);
|
||||
Array.Clear(challengeResponse, 0, challengeResponse.Length);
|
||||
}
|
||||
|
||||
_challengeProv = new KeeChallengeProv();
|
||||
_challengeSecret = _challengeProv.GetSecret(_chalInfo, challengeResponse);
|
||||
Array.Clear(challengeResponse, 0, challengeResponse.Length);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (_currentlyWaitingKey != null)
|
||||
{
|
||||
_currentlyWaitingKey.Error = e.Message;
|
||||
}
|
||||
Kp2aLog.Log(e.ToString());
|
||||
Toast.MakeText(this, "Error: " + e.Message, ToastLength.Long).Show();
|
||||
return;
|
||||
@@ -449,11 +432,6 @@ namespace keepass2android
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_currentlyWaitingKey != null)
|
||||
_currentlyWaitingKey.Error = "Cancelled Yubichallenge.";
|
||||
}
|
||||
}
|
||||
|
||||
private AuxFileLoader GetAuxFileLoader()
|
||||
|
||||
Reference in New Issue
Block a user