2013-07-09 09:59:17 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
2013-08-06 22:21:58 +02:00
|
|
|
using System.Net;
|
2013-12-12 10:24:24 +01:00
|
|
|
using System.Net.Security;
|
2014-11-18 06:24:35 +01:00
|
|
|
using System.Security;
|
2013-07-09 09:59:17 +02:00
|
|
|
using Android.Content;
|
|
|
|
using Android.OS;
|
2013-12-12 10:24:24 +01:00
|
|
|
using Java.Security.Cert;
|
2013-07-09 09:59:17 +02:00
|
|
|
using KeePassLib.Serialization;
|
|
|
|
using KeePassLib.Utility;
|
|
|
|
|
|
|
|
namespace keepass2android.Io
|
|
|
|
{
|
|
|
|
public class BuiltInFileStorage: IFileStorage
|
|
|
|
{
|
2013-12-12 10:24:24 +01:00
|
|
|
public enum CertificateProblem :long
|
|
|
|
{
|
|
|
|
CertEXPIRED = 0x800B0101,
|
|
|
|
CertVALIDITYPERIODNESTING = 0x800B0102,
|
|
|
|
CertROLE = 0x800B0103,
|
|
|
|
CertPATHLENCONST = 0x800B0104,
|
|
|
|
CertCRITICAL = 0x800B0105,
|
|
|
|
CertPURPOSE = 0x800B0106,
|
|
|
|
CertISSUERCHAINING = 0x800B0107,
|
|
|
|
CertMALFORMED = 0x800B0108,
|
|
|
|
CertUNTRUSTEDROOT = 0x800B0109,
|
|
|
|
CertCHAINING = 0x800B010A,
|
|
|
|
CertREVOKED = 0x800B010C,
|
|
|
|
CertUNTRUSTEDTESTROOT = 0x800B010D,
|
|
|
|
CertREVOCATION_FAILURE = 0x800B010E,
|
|
|
|
CertCN_NO_MATCH = 0x800B010F,
|
|
|
|
CertWRONG_USAGE = 0x800B0110,
|
|
|
|
CertUNTRUSTEDCA = 0x800B0112
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private readonly IKp2aApp _app;
|
|
|
|
|
|
|
|
public BuiltInFileStorage(IKp2aApp app)
|
|
|
|
{
|
|
|
|
_app = app;
|
|
|
|
//use the obsolute CertificatePolicy because the ServerCertificateValidationCallback isn't called in Mono for Android (?)
|
2014-02-02 23:36:19 +01:00
|
|
|
//ServicePointManager.CertificatePolicy = new CertificatePolicity(app);
|
|
|
|
IOConnection.CertificateValidationCallback = app.CertificateValidationCallback;
|
|
|
|
|
2013-12-12 10:24:24 +01:00
|
|
|
}
|
|
|
|
|
2013-09-15 20:08:14 +02:00
|
|
|
public IEnumerable<string> SupportedProtocols
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
yield return "file";
|
2013-12-26 06:31:40 +01:00
|
|
|
#if !NoNet
|
2013-09-15 20:08:14 +02:00
|
|
|
yield return "ftp";
|
|
|
|
yield return "http";
|
|
|
|
yield return "https";
|
2013-12-26 06:31:40 +01:00
|
|
|
#endif
|
2013-09-15 20:08:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 07:46:44 +02:00
|
|
|
public void Delete(IOConnectionInfo ioc)
|
2013-07-09 09:59:17 +02:00
|
|
|
{
|
2013-09-28 07:46:44 +02:00
|
|
|
//todo check if directory
|
2013-07-09 09:59:17 +02:00
|
|
|
IOConnection.DeleteFile(ioc);
|
|
|
|
}
|
|
|
|
public bool CheckForFileChangeFast(IOConnectionInfo ioc, string previousFileVersion)
|
|
|
|
{
|
|
|
|
if (!ioc.IsLocalFile())
|
|
|
|
return false;
|
2013-07-11 17:27:10 +02:00
|
|
|
if (previousFileVersion == null)
|
|
|
|
return false;
|
2013-07-09 09:59:17 +02:00
|
|
|
DateTime previousDate;
|
2013-07-11 17:27:10 +02:00
|
|
|
if (!DateTime.TryParse(previousFileVersion, CultureInfo.InvariantCulture, DateTimeStyles.None, out previousDate))
|
2013-07-09 09:59:17 +02:00
|
|
|
return false;
|
2013-07-11 17:27:10 +02:00
|
|
|
DateTime currentModificationDate = File.GetLastWriteTimeUtc(ioc.Path);
|
|
|
|
TimeSpan diff = currentModificationDate - previousDate;
|
|
|
|
return diff > TimeSpan.FromSeconds(1);
|
|
|
|
//don't use > operator because milliseconds are truncated
|
2013-07-30 20:42:16 +02:00
|
|
|
//return File.GetLastWriteTimeUtc(ioc.Path) - previousDate >= TimeSpan.FromSeconds(1);
|
2013-07-09 09:59:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetCurrentFileVersionFast(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ioc.IsLocalFile())
|
|
|
|
{
|
|
|
|
return File.GetLastWriteTimeUtc(ioc.Path).ToString(CultureInfo.InvariantCulture);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return DateTime.MinValue.ToString(CultureInfo.InvariantCulture);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Stream OpenFileForRead(IOConnectionInfo ioc)
|
|
|
|
{
|
2013-08-06 22:21:58 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return IOConnection.OpenRead(ioc);
|
|
|
|
}
|
|
|
|
catch (WebException ex)
|
|
|
|
{
|
2013-12-12 10:24:24 +01:00
|
|
|
ConvertException(ioc, ex);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ConvertException(IOConnectionInfo ioc, WebException ex)
|
|
|
|
{
|
|
|
|
if ((ex.Response is HttpWebResponse) && (((HttpWebResponse) ex.Response).StatusCode == HttpStatusCode.NotFound))
|
|
|
|
{
|
|
|
|
throw new FileNotFoundException(ex.Message, ioc.Path, ex);
|
|
|
|
}
|
|
|
|
if (ex.Status == WebExceptionStatus.TrustFailure)
|
|
|
|
{
|
|
|
|
throw new Exception(_app.GetResourceString(UiStringKey.CertificateFailure), ex);
|
|
|
|
}
|
|
|
|
var inner1 = ex.InnerException as IOException;
|
|
|
|
if (inner1 != null)
|
|
|
|
{
|
|
|
|
var inner2 = inner1.InnerException;
|
|
|
|
if (inner2 != null)
|
2013-08-06 22:21:58 +02:00
|
|
|
{
|
2013-12-12 10:24:24 +01:00
|
|
|
if (inner2.Message.Contains("Invalid certificate received from server."))
|
|
|
|
{
|
|
|
|
throw new Exception(_app.GetResourceString(UiStringKey.CertificateFailure), ex);
|
|
|
|
}
|
2013-08-06 22:21:58 +02:00
|
|
|
}
|
2013-12-12 10:24:24 +01:00
|
|
|
|
2013-08-06 22:21:58 +02:00
|
|
|
}
|
2013-07-09 09:59:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public IWriteTransaction OpenWriteTransaction(IOConnectionInfo ioc, bool useFileTransaction)
|
|
|
|
{
|
2013-12-12 10:24:24 +01:00
|
|
|
return new BuiltInFileTransaction(ioc, useFileTransaction, this);
|
2013-07-09 09:59:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public class BuiltInFileTransaction : IWriteTransaction
|
|
|
|
{
|
2013-12-12 10:24:24 +01:00
|
|
|
private readonly IOConnectionInfo _ioc;
|
|
|
|
private readonly BuiltInFileStorage _fileStorage;
|
2013-07-09 09:59:17 +02:00
|
|
|
private readonly FileTransactionEx _transaction;
|
|
|
|
|
2013-12-12 10:24:24 +01:00
|
|
|
public BuiltInFileTransaction(IOConnectionInfo ioc, bool useFileTransaction, BuiltInFileStorage fileStorage)
|
2013-07-09 09:59:17 +02:00
|
|
|
{
|
2013-12-12 10:24:24 +01:00
|
|
|
_ioc = ioc;
|
|
|
|
_fileStorage = fileStorage;
|
2013-07-09 09:59:17 +02:00
|
|
|
_transaction = new FileTransactionEx(ioc, useFileTransaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Stream OpenFile()
|
|
|
|
{
|
2013-12-12 10:24:24 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return _transaction.OpenWrite();
|
|
|
|
}
|
|
|
|
catch (WebException ex)
|
|
|
|
{
|
|
|
|
_fileStorage.ConvertException(_ioc, ex);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2013-07-09 09:59:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CommitWrite()
|
|
|
|
{
|
2013-12-12 10:24:24 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_transaction.CommitWrite();
|
|
|
|
}
|
|
|
|
catch (WebException ex)
|
|
|
|
{
|
|
|
|
_fileStorage.ConvertException(_ioc, ex);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2013-07-09 09:59:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetFilenameWithoutPathAndExt(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
return UrlUtil.StripExtension(
|
|
|
|
UrlUtil.GetFileName(ioc.Path));
|
|
|
|
|
|
|
|
}
|
2013-09-15 20:08:14 +02:00
|
|
|
|
|
|
|
public bool RequiresCredentials(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
return (!ioc.IsLocalFile()) && (ioc.CredSaveMode != IOCredSaveMode.SaveCred);
|
|
|
|
}
|
2013-09-28 07:46:44 +02:00
|
|
|
|
2013-10-25 04:05:37 +02:00
|
|
|
public void CreateDirectory(IOConnectionInfo ioc, string newDirName)
|
2013-09-28 07:46:44 +02:00
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<FileDescription> ListContents(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2013-09-28 21:14:21 +02:00
|
|
|
|
|
|
|
public FileDescription GetFileDescription(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2013-10-07 06:28:06 +02:00
|
|
|
|
|
|
|
public bool RequiresSetup(IOConnectionInfo ioConnection)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string IocToPath(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
return ioc.Path;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StartSelectFile(IFileStorageSetupInitiatorActivity activity, bool isForSave, int requestCode, string protocolId)
|
|
|
|
{
|
|
|
|
if (protocolId != "file")
|
|
|
|
activity.PerformManualFileSelect(isForSave, requestCode, protocolId);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Intent intent = new Intent();
|
|
|
|
activity.IocToIntent(intent, new IOConnectionInfo() { Path = protocolId+"://"});
|
|
|
|
activity.OnImmediateResult(requestCode, (int) FileStorageResults.FileChooserPrepared, intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:33:35 +01:00
|
|
|
public void PrepareFileUsage(IFileStorageSetupInitiatorActivity activity, IOConnectionInfo ioc, int requestCode, bool alwaysReturnSuccess)
|
2013-10-07 06:28:06 +02:00
|
|
|
{
|
|
|
|
Intent intent = new Intent();
|
|
|
|
activity.IocToIntent(intent, ioc);
|
|
|
|
activity.OnImmediateResult(requestCode, (int) FileStorageResults.FileUsagePrepared, intent);
|
|
|
|
}
|
|
|
|
|
2014-07-30 18:14:59 +02:00
|
|
|
public void PrepareFileUsage(Context ctx, IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
//nothing to do, we're ready to go
|
|
|
|
}
|
|
|
|
|
2013-10-07 06:28:06 +02:00
|
|
|
public void OnCreate(IFileStorageSetupActivity activity, Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnResume(IFileStorageSetupActivity activity)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnStart(IFileStorageSetupActivity activity)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnActivityResult(IFileStorageSetupActivity activity, int requestCode, int resultCode, Intent data)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2013-10-27 15:06:57 +01:00
|
|
|
|
|
|
|
public string GetDisplayName(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
return ioc.GetDisplayName();
|
|
|
|
}
|
2013-10-27 21:55:19 +01:00
|
|
|
|
|
|
|
public string CreateFilePath(string parent, string newFilename)
|
|
|
|
{
|
|
|
|
if (!parent.EndsWith("/"))
|
|
|
|
parent += "/";
|
|
|
|
return parent + newFilename;
|
|
|
|
}
|
2013-11-17 07:17:15 +01:00
|
|
|
|
|
|
|
public IOConnectionInfo GetParentPath(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
return IoUtil.GetParentPath(ioc);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IOConnectionInfo GetFilePath(IOConnectionInfo folderPath, string filename)
|
|
|
|
{
|
|
|
|
IOConnectionInfo res = folderPath.CloneDeep();
|
|
|
|
if (!res.Path.EndsWith("/"))
|
|
|
|
res.Path += "/";
|
|
|
|
res.Path += filename;
|
|
|
|
return res;
|
|
|
|
}
|
2014-11-18 06:24:35 +01:00
|
|
|
|
|
|
|
public bool IsPermanentLocation(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsReadOnlyBecauseKitkatRestrictions(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
if (IsLocalFileFlaggedReadOnly(ioc))
|
|
|
|
return false; //it's not read-only because of the restrictions introduced in kitkat
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//test if we can open
|
|
|
|
//http://www.doubleencore.com/2014/03/android-external-storage/#comment-1294469517
|
|
|
|
using (var writer = new Java.IO.FileOutputStream(ioc.Path, true))
|
|
|
|
{
|
|
|
|
writer.Close();
|
|
|
|
return false; //we can write
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Java.IO.IOException)
|
|
|
|
{
|
|
|
|
//seems like we can't write to that location even though it's not read-only
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsReadOnly(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
if (ioc.IsLocalFile())
|
|
|
|
{
|
|
|
|
if (IsLocalFileFlaggedReadOnly(ioc))
|
|
|
|
return true;
|
|
|
|
if (IsReadOnlyBecauseKitkatRestrictions(ioc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//for remote files assume they can be written: (think positive! :-) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsLocalFileFlaggedReadOnly(IOConnectionInfo ioc)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return new FileInfo(ioc.Path).IsReadOnly;
|
|
|
|
}
|
|
|
|
catch (SecurityException)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 09:59:17 +02:00
|
|
|
}
|
|
|
|
}
|