mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-04 16:10:16 -04:00
WIP KeePassLibPCL - problem with awaitables
This commit is contained in:
@@ -22,9 +22,9 @@ using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
using ModernKeePassLib.Utility;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
public sealed class BinaryReaderEx
|
||||
{
|
||||
|
@@ -21,18 +21,18 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
using System.Threading.Tasks;
|
||||
#else
|
||||
using System.Threading;
|
||||
#endif
|
||||
using System.Diagnostics;
|
||||
|
||||
using ModernKeePassLib.Cryptography;
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Utility;
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
using ModernKeePassLibPCL.Resources;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
public sealed class FileLockException : Exception
|
||||
{
|
||||
@@ -158,7 +158,7 @@ namespace ModernKeePassLib.Serialization
|
||||
byte[] pbID = CryptoRandom.Instance.GetRandomBytes(16);
|
||||
string strTime = TimeUtil.SerializeUtc(DateTime.Now);
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
lfi = new LockFileInfo(Convert.ToBase64String(pbID), strTime,
|
||||
Environment.UserName, Environment.MachineName,
|
||||
Environment.UserDomainName);
|
||||
@@ -243,7 +243,7 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
if(bFileDeleted) break;
|
||||
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
if(bDisposing)
|
||||
Task.Delay(50).Wait();
|
||||
#else
|
||||
|
@@ -23,13 +23,14 @@ using System.Text;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
using System.Security.AccessControl;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Utility;
|
||||
using ModernKeePassLibPCL.Native;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
public sealed class FileTransactionEx
|
||||
{
|
||||
@@ -58,8 +59,8 @@ namespace ModernKeePassLib.Serialization
|
||||
m_bTransacted = bTransacted;
|
||||
m_iocBase = iocBaseFile.CloneDeep();
|
||||
|
||||
// ModernKeePassLib is currently targeting .NET 4.5
|
||||
#if !PCL
|
||||
// ModernKeePassLibPCL is currently targeting .NET 4.5
|
||||
#if !ModernKeePassLibPCL
|
||||
// Prevent transactions for FTP URLs under .NET 4.0 in order to
|
||||
// avoid/workaround .NET bug 621450:
|
||||
// https://connect.microsoft.com/VisualStudio/feedback/details/621450/problem-renaming-file-on-ftp-server-using-ftpwebrequest-in-net-framework-4-0-vs2010-only
|
||||
@@ -101,14 +102,14 @@ namespace ModernKeePassLib.Serialization
|
||||
{
|
||||
bool bMadeUnhidden = UrlUtil.UnhideFile(m_iocBase.Path);
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
FileSecurity bkSecurity = null;
|
||||
bool bEfsEncrypted = false;
|
||||
#endif
|
||||
|
||||
if(IOConnection.FileExists(m_iocBase))
|
||||
{
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
if(m_iocBase.IsLocalFile())
|
||||
{
|
||||
try
|
||||
@@ -130,7 +131,7 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
IOConnection.RenameFile(m_iocTemp, m_iocBase);
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
if(m_iocBase.IsLocalFile())
|
||||
{
|
||||
try
|
||||
|
@@ -19,22 +19,23 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
using System.Linq;
|
||||
using PCLCrypto;
|
||||
#else
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Text;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
using ModernKeePassLibPCL.Native;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
#if KeePassLibSD
|
||||
using KeePassLibSD;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
public sealed class HashedBlockStream : Stream
|
||||
{
|
||||
@@ -98,8 +99,8 @@ namespace ModernKeePassLib.Serialization
|
||||
private void Initialize(Stream sBaseStream, bool bWriting, int nBufferSize,
|
||||
bool bVerify)
|
||||
{
|
||||
if (sBaseStream == null) throw new ArgumentNullException(nameof(sBaseStream));
|
||||
m_sBaseStream = sBaseStream;
|
||||
if (sBaseStream != null) m_sBaseStream = sBaseStream;
|
||||
else throw new ArgumentNullException(nameof(sBaseStream));
|
||||
if (nBufferSize < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(nBufferSize));
|
||||
|
||||
@@ -134,7 +135,7 @@ namespace ModernKeePassLib.Serialization
|
||||
if(m_bWriting) m_bwOutput.Flush();
|
||||
}
|
||||
|
||||
#if PCL || KeePassRT
|
||||
#if ModernKeePassLibPCL || KeePassRT
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if(!disposing) return;
|
||||
@@ -221,11 +222,11 @@ namespace ModernKeePassLib.Serialization
|
||||
throw new InvalidDataException();
|
||||
|
||||
int nBufferSize = 0;
|
||||
/*try {*/ nBufferSize = m_brInput.ReadInt32(); /*}
|
||||
try { nBufferSize = m_brInput.ReadInt32(); }
|
||||
catch(NullReferenceException) // Mono bug workaround (LaunchPad 783268)
|
||||
{
|
||||
if(!NativeLib.IsUnix()) throw;
|
||||
}*/
|
||||
}
|
||||
|
||||
if(nBufferSize < 0)
|
||||
throw new InvalidDataException();
|
||||
@@ -249,16 +250,16 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
if(m_bVerify)
|
||||
{
|
||||
#if PCL
|
||||
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
|
||||
var pbComputedHash = sha256.HashData(m_pbBuffer.AsBuffer()).ToArray();
|
||||
#if ModernKeePassLibPCL
|
||||
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
|
||||
var pbComputedHash = sha256.HashData(m_pbBuffer);
|
||||
#else
|
||||
SHA256Managed sha256 = new SHA256Managed();
|
||||
byte[] pbComputedHash = sha256.ComputeHash(m_pbBuffer);
|
||||
#endif
|
||||
if((pbComputedHash == null) || (pbComputedHash.Length != 32))
|
||||
throw new InvalidOperationException();
|
||||
|
||||
|
||||
for(int iHashPos = 0; iHashPos < 32; ++iHashPos)
|
||||
{
|
||||
if(pbStoredHash[iHashPos] != pbComputedHash[iHashPos])
|
||||
@@ -296,9 +297,9 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
if(m_nBufferPos > 0)
|
||||
{
|
||||
#if PCL
|
||||
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
|
||||
var pbHash = sha256.HashData(m_pbBuffer.Where((x, i) => i < m_nBufferPos).ToArray().AsBuffer()).ToArray();
|
||||
#if ModernKeePassLibPCL
|
||||
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
|
||||
var pbHash = sha256.HashData(m_pbBuffer.Where((x, i) => i < m_nBufferPos).ToArray());
|
||||
#else
|
||||
|
||||
SHA256Managed sha256 = new SHA256Managed();
|
||||
|
@@ -19,31 +19,28 @@
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Diagnostics;
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
using Windows.Storage.Streams;
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
using System.Net.Cache;
|
||||
using System.Net.Security;
|
||||
#endif
|
||||
|
||||
#if !PCL && !KeePassRT
|
||||
#if !ModernKeePassLibPCL && !KeePassRT
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
#endif
|
||||
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
using Windows.Storage;
|
||||
using PCLStorage;
|
||||
#endif
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
internal sealed class IOWebClient : WebClient
|
||||
{
|
||||
protected override WebRequest GetWebRequest(Uri address)
|
||||
@@ -55,7 +52,7 @@ namespace ModernKeePassLib.Serialization
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !PCL
|
||||
#if !ModernKeePassLibPCL
|
||||
internal abstract class WrapperStream : Stream
|
||||
{
|
||||
private readonly Stream m_s;
|
||||
@@ -228,7 +225,7 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
public static class IOConnection
|
||||
{
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
private static ProxyServerType m_pstProxyType = ProxyServerType.System;
|
||||
private static string m_strProxyAddr = string.Empty;
|
||||
private static string m_strProxyPort = string.Empty;
|
||||
@@ -252,7 +249,7 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
public static event EventHandler<IOAccessEventArgs> IOAccessPre;
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
// Allow self-signed certificates, expired certificates, etc.
|
||||
private static bool AcceptCertificate(object sender,
|
||||
X509Certificate certificate, X509Chain chain,
|
||||
@@ -434,17 +431,20 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
private static Stream OpenReadLocal(IOConnectionInfo ioc)
|
||||
{
|
||||
#if PCL
|
||||
/*var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
|
||||
return file.OpenAsync(PCLStorage.FileAccess.Read).Result;*/
|
||||
return ioc.StorageFile.OpenAsync(FileAccessMode.Read).GetResults().AsStream();
|
||||
#if ModernKeePassLibPCL
|
||||
if (ioc.StorageFile != null)
|
||||
{
|
||||
return ioc.StorageFile.OpenAsync(FileAccessMode.Read).GetResults().AsStream();
|
||||
}
|
||||
var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
|
||||
return file.OpenAsync(PCLStorage.FileAccess.Read).Result;
|
||||
#else
|
||||
return new FileStream(ioc.Path, FileMode.Open, FileAccess.Read,
|
||||
FileShare.Read);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
public static Stream OpenWrite(IOConnectionInfo ioc)
|
||||
{
|
||||
if(ioc == null) { Debug.Assert(false); return null; }
|
||||
@@ -477,15 +477,18 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
private static Stream OpenWriteLocal(IOConnectionInfo ioc)
|
||||
{
|
||||
#if PCL
|
||||
return ioc.StorageFile.OpenAsync(FileAccessMode.ReadWrite).GetResults().AsStream();
|
||||
/*var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
|
||||
return file.OpenAsync(FileAccess.ReadAndWrite).Result;*/
|
||||
#if ModernKeePassLibPCL
|
||||
if (ioc.StorageFile != null)
|
||||
{
|
||||
return ioc.StorageFile.OpenAsync(FileAccessMode.ReadWrite).GetResults().AsStream();
|
||||
}
|
||||
var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
|
||||
return file.OpenAsync(FileAccess.ReadAndWrite).Result;
|
||||
#else
|
||||
return new FileStream(ioc.Path, FileMode.Create, FileAccess.Write,
|
||||
FileShare.None);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public static bool FileExists(IOConnectionInfo ioc)
|
||||
{
|
||||
@@ -498,14 +501,14 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
RaiseIOAccessPreEvent(ioc, IOAccessType.Exists);
|
||||
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
if(ioc.IsLocalFile())
|
||||
return ioc.StorageFile.IsAvailable;
|
||||
return (FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result != null);
|
||||
#else
|
||||
if(ioc.IsLocalFile()) return File.Exists(ioc.Path);
|
||||
#endif
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
if(ioc.Path.StartsWith("ftp://", StrUtil.CaseIgnoreCmp))
|
||||
{
|
||||
bool b = SendCommand(ioc, WebRequestMethods.Ftp.GetDateTimestamp);
|
||||
@@ -540,17 +543,16 @@ namespace ModernKeePassLib.Serialization
|
||||
{
|
||||
RaiseIOAccessPreEvent(ioc, IOAccessType.Delete);
|
||||
|
||||
#if PCL
|
||||
if(ioc.IsLocalFile()) {
|
||||
/*var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
|
||||
file.DeleteAsync().RunSynchronously();*/
|
||||
ioc.StorageFile.DeleteAsync(StorageDeleteOption.Default).GetResults();
|
||||
}
|
||||
#if ModernKeePassLibPCL
|
||||
if (!ioc.IsLocalFile()) return;
|
||||
ioc.StorageFile?.DeleteAsync().GetResults();
|
||||
var file = FileSystem.Current.GetFileFromPathAsync(ioc.Path).Result;
|
||||
file.DeleteAsync().RunSynchronously();
|
||||
#else
|
||||
if(ioc.IsLocalFile()) { File.Delete(ioc.Path); return; }
|
||||
#endif
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
WebRequest req = CreateWebRequest(ioc);
|
||||
if(req != null)
|
||||
{
|
||||
@@ -582,18 +584,16 @@ namespace ModernKeePassLib.Serialization
|
||||
{
|
||||
RaiseIOAccessPreEvent(iocFrom, iocTo, IOAccessType.Move);
|
||||
|
||||
#if PCL
|
||||
if(iocFrom.IsLocalFile()) {
|
||||
/*var file = FileSystem.Current.GetFileFromPathAsync(iocFrom.Path).Result;
|
||||
file.MoveAsync(iocTo.Path).RunSynchronously();*/
|
||||
iocFrom.StorageFile.RenameAsync(iocTo.Path).GetResults();
|
||||
return;
|
||||
}
|
||||
#if ModernKeePassLibPCL
|
||||
if (!iocFrom.IsLocalFile()) return;
|
||||
iocFrom.StorageFile?.RenameAsync(iocTo.Path).GetResults();
|
||||
var file = FileSystem.Current.GetFileFromPathAsync(iocFrom.Path).Result;
|
||||
file.MoveAsync(iocTo.Path).RunSynchronously();
|
||||
#else
|
||||
if(iocFrom.IsLocalFile()) { File.Move(iocFrom.Path, iocTo.Path); return; }
|
||||
#endif
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
WebRequest req = CreateWebRequest(iocFrom);
|
||||
if(req != null)
|
||||
{
|
||||
@@ -642,7 +642,7 @@ namespace ModernKeePassLib.Serialization
|
||||
// DeleteFile(iocFrom);
|
||||
}
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
|
||||
private static bool SendCommand(IOConnectionInfo ioc, string strMethod)
|
||||
{
|
||||
try
|
||||
|
@@ -25,15 +25,15 @@ using System.Net;
|
||||
using System.ComponentModel;
|
||||
using System.Xml.Serialization;
|
||||
using System.Diagnostics;
|
||||
|
||||
#if PCL
|
||||
using Windows.Storage;
|
||||
#if ModernKeePassLibPCL
|
||||
using PCLStorage;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Interfaces;
|
||||
using ModernKeePassLib.Utility;
|
||||
using ModernKeePassLibPCL.Interfaces;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
public enum IOCredSaveMode
|
||||
{
|
||||
@@ -67,11 +67,9 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
public sealed class IOConnectionInfo : IDeepCloneable<IOConnectionInfo>
|
||||
{
|
||||
// private IOFileFormatHint m_ioHint = IOFileFormatHint.None;
|
||||
// private IOFileFormatHint m_ioHint = IOFileFormatHint.None;
|
||||
|
||||
public StorageFile StorageFile { get; set; }
|
||||
|
||||
private string m_strUrl = string.Empty;
|
||||
private string m_strUrl = string.Empty;
|
||||
public string Path
|
||||
{
|
||||
get { return m_strUrl; }
|
||||
@@ -295,23 +293,24 @@ namespace ModernKeePassLib.Serialization
|
||||
return ioc;
|
||||
}
|
||||
|
||||
public static IOConnectionInfo FromFile(StorageFile file)
|
||||
{
|
||||
IOConnectionInfo ioc = new IOConnectionInfo();
|
||||
public static IOConnectionInfo FromFile(StorageFile file)
|
||||
{
|
||||
IOConnectionInfo ioc = new IOConnectionInfo();
|
||||
|
||||
ioc.Path = file.Path;
|
||||
ioc.CredSaveMode = IOCredSaveMode.NoSave;
|
||||
ioc.StorageFile = file;
|
||||
ioc.StorageFile = file;
|
||||
ioc.Path = file.Path;
|
||||
ioc.CredSaveMode = IOCredSaveMode.NoSave;
|
||||
|
||||
return ioc;
|
||||
}
|
||||
return ioc;
|
||||
}
|
||||
|
||||
public StorageFile StorageFile { get; set; }
|
||||
|
||||
public bool CanProbablyAccess()
|
||||
{
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
if(IsLocalFile())
|
||||
return (StorageFile.IsAvailable);
|
||||
return (FileSystem.Current.GetFileFromPathAsync(m_strUrl).Result != null);
|
||||
#else
|
||||
if(IsLocalFile()) return File.Exists(m_strUrl);
|
||||
#endif
|
||||
|
@@ -21,20 +21,21 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Security;
|
||||
using System.Drawing;
|
||||
using System.Xml;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
|
||||
using ModernKeePassLib;
|
||||
using ModernKeePassLib.Collections;
|
||||
using ModernKeePassLib.Cryptography;
|
||||
using ModernKeePassLib.Cryptography.Cipher;
|
||||
using ModernKeePassLib.Interfaces;
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Security;
|
||||
using ModernKeePassLib.Utility;
|
||||
using ModernKeePassLibPCL;
|
||||
using ModernKeePassLibPCL.Collections;
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
using ModernKeePassLibPCL.Cryptography.Cipher;
|
||||
using ModernKeePassLibPCL.Interfaces;
|
||||
using ModernKeePassLibPCL.Resources;
|
||||
using ModernKeePassLibPCL.Security;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialization to KeePass KDBX files.
|
||||
@@ -98,7 +99,7 @@ namespace ModernKeePassLib.Serialization
|
||||
xrs.IgnoreProcessingInstructions = true;
|
||||
xrs.IgnoreWhitespace = true;
|
||||
|
||||
#if !PCL
|
||||
#if !ModernKeePassLibPCL
|
||||
// these are default values, so no need to set them
|
||||
#if !KeePassRT
|
||||
#if !KeePassLibSD
|
||||
@@ -241,8 +242,8 @@ namespace ModernKeePassLib.Serialization
|
||||
else if(xr.Name == ElemDbColor)
|
||||
{
|
||||
string strColor = ReadString(xr);
|
||||
/*if(!string.IsNullOrEmpty(strColor))
|
||||
m_pwDatabase.Color = ColorTranslator.FromHtml(strColor);*/
|
||||
if(!string.IsNullOrEmpty(strColor))
|
||||
m_pwDatabase.Color = ColorTranslator.FromHtml(strColor);
|
||||
}
|
||||
else if(xr.Name == ElemDbKeyChanged)
|
||||
m_pwDatabase.MasterKeyChanged = ReadTime(xr);
|
||||
@@ -413,14 +414,14 @@ namespace ModernKeePassLib.Serialization
|
||||
else if(xr.Name == ElemFgColor)
|
||||
{
|
||||
string strColor = ReadString(xr);
|
||||
/*if(!string.IsNullOrEmpty(strColor))
|
||||
m_ctxEntry.ForegroundColor = ColorTranslator.FromHtml(strColor);*/
|
||||
if(!string.IsNullOrEmpty(strColor))
|
||||
m_ctxEntry.ForegroundColor = ColorTranslator.FromHtml(strColor);
|
||||
}
|
||||
else if(xr.Name == ElemBgColor)
|
||||
{
|
||||
string strColor = ReadString(xr);
|
||||
/*if(!string.IsNullOrEmpty(strColor))
|
||||
m_ctxEntry.BackgroundColor = ColorTranslator.FromHtml(strColor);*/
|
||||
if(!string.IsNullOrEmpty(strColor))
|
||||
m_ctxEntry.BackgroundColor = ColorTranslator.FromHtml(strColor);
|
||||
}
|
||||
else if(xr.Name == ElemOverrideUrl)
|
||||
m_ctxEntry.OverrideUrl = ReadString(xr);
|
||||
@@ -689,7 +690,7 @@ namespace ModernKeePassLib.Serialization
|
||||
}
|
||||
|
||||
m_bReadNextNode = false; // ReadElementString skips end tag
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
return xr.ReadElementContentAsString();
|
||||
#else
|
||||
return xr.ReadElementString();
|
||||
@@ -699,7 +700,7 @@ namespace ModernKeePassLib.Serialization
|
||||
private string ReadStringRaw(XmlReader xr)
|
||||
{
|
||||
m_bReadNextNode = false; // ReadElementString skips end tag
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
return xr.ReadElementContentAsString();
|
||||
#else
|
||||
return xr.ReadElementString();
|
||||
|
@@ -22,29 +22,28 @@ using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Security;
|
||||
#if PCL
|
||||
using Windows.Security.Cryptography;
|
||||
#if ModernKeePassLibPCL
|
||||
using PCLCrypto;
|
||||
#else
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
using System.Xml;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
|
||||
#if !KeePassLibSD
|
||||
using System.IO.Compression;
|
||||
#else
|
||||
using KeePassLibSD;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Cryptography;
|
||||
using ModernKeePassLib.Cryptography.Cipher;
|
||||
using ModernKeePassLib.Interfaces;
|
||||
using ModernKeePassLib.Keys;
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Utility;
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
using ModernKeePassLibPCL.Cryptography.Cipher;
|
||||
using ModernKeePassLibPCL.Interfaces;
|
||||
using ModernKeePassLibPCL.Keys;
|
||||
using ModernKeePassLibPCL.Resources;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialization to KeePass KDBX files.
|
||||
@@ -155,7 +154,7 @@ namespace ModernKeePassLib.Serialization
|
||||
// GC.KeepAlive(br);
|
||||
// GC.KeepAlive(brDecrypted);
|
||||
}
|
||||
#if !PCL
|
||||
#if !ModernKeePassLibPCL
|
||||
catch(CryptographicException) // Thrown on invalid padding
|
||||
{
|
||||
throw new CryptographicException(KLRes.FileCorrupted);
|
||||
@@ -221,9 +220,9 @@ namespace ModernKeePassLib.Serialization
|
||||
byte[] pbHeader = msHeader.ToArray();
|
||||
msHeader.Dispose();
|
||||
|
||||
#if PCL
|
||||
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
|
||||
m_pbHashOfHeader = sha256.HashData(pbHeader.AsBuffer()).ToArray();
|
||||
#if ModernKeePassLibPCL
|
||||
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
|
||||
m_pbHashOfHeader = sha256.HashData(pbHeader);
|
||||
#else
|
||||
SHA256Managed sha256 = new SHA256Managed();
|
||||
m_pbHashOfHeader = sha256.ComputeHash(pbHeader);
|
||||
@@ -333,39 +332,46 @@ namespace ModernKeePassLib.Serialization
|
||||
m_craInnerRandomStream = (CrsAlgorithm)uID;
|
||||
}
|
||||
|
||||
private Stream AttachStreamDecryptor(Stream s)
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
Debug.Assert(m_pbMasterSeed.Length == 32);
|
||||
if (m_pbMasterSeed.Length != 32)
|
||||
throw new FormatException(KLRes.MasterSeedLengthInvalid);
|
||||
ms.Write(m_pbMasterSeed, 0, 32);
|
||||
private Stream AttachStreamDecryptor(Stream s)
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
|
||||
byte[] pKey32 = m_pwDatabase.MasterKey.GenerateKey32(m_pbTransformSeed,
|
||||
m_pwDatabase.KeyEncryptionRounds).ReadData();
|
||||
if (pKey32 == null || pKey32.Length != 32)
|
||||
throw new SecurityException(KLRes.InvalidCompositeKey);
|
||||
ms.Write(pKey32, 0, 32);
|
||||
Debug.Assert(m_pbMasterSeed.Length == 32);
|
||||
if(m_pbMasterSeed.Length != 32)
|
||||
throw new FormatException(KLRes.MasterSeedLengthInvalid);
|
||||
ms.Write(m_pbMasterSeed, 0, 32);
|
||||
|
||||
#if PCL
|
||||
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
|
||||
var aesKey = sha256.HashData(ms.GetWindowsRuntimeBuffer()).ToArray();
|
||||
byte[] pKey32 = m_pwDatabase.MasterKey.GenerateKey32(m_pbTransformSeed,
|
||||
m_pwDatabase.KeyEncryptionRounds).ReadData();
|
||||
if((pKey32 == null) || (pKey32.Length != 32))
|
||||
throw new SecurityException(KLRes.InvalidCompositeKey);
|
||||
ms.Write(pKey32, 0, 32);
|
||||
|
||||
#if ModernKeePassLibPCL
|
||||
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
|
||||
var aesKey = sha256.HashData(ms.ToArray());
|
||||
#else
|
||||
SHA256Managed sha256 = new SHA256Managed();
|
||||
byte[] aesKey = sha256.ComputeHash(ms.ToArray());
|
||||
#endif
|
||||
Array.Clear(pKey32, 0, 32);
|
||||
|
||||
if (aesKey == null || aesKey.Length != 32)
|
||||
throw new SecurityException(KLRes.FinalKeyCreationFailed);
|
||||
ms.Dispose();
|
||||
Array.Clear(pKey32, 0, 32);
|
||||
|
||||
if((aesKey == null) || (aesKey.Length != 32))
|
||||
throw new SecurityException(KLRes.FinalKeyCreationFailed);
|
||||
|
||||
ICipherEngine iEngine = CipherPool.GlobalPool.GetCipher(m_pwDatabase.DataCipherUuid);
|
||||
if(iEngine == null) throw new SecurityException(KLRes.FileUnknownCipher);
|
||||
return iEngine.DecryptStream(s, aesKey, m_pbEncryptionIV);
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
public static List<PwEntry> ReadEntries(PwDatabase pwDatabase, Stream msData)
|
||||
{
|
||||
return ReadEntries(msData);
|
||||
}
|
||||
|
||||
ICipherEngine iEngine = CipherPool.GlobalPool.GetCipher(m_pwDatabase.DataCipherUuid);
|
||||
if (iEngine == null) throw new SecurityException(KLRes.FileUnknownCipher);
|
||||
return iEngine.DecryptStream(s, aesKey, m_pbEncryptionIV);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read entries from a stream.
|
||||
/// </summary>
|
||||
|
@@ -23,32 +23,32 @@ using System.Text;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Security;
|
||||
#if PCL
|
||||
using Windows.Security.Cryptography;
|
||||
#if ModernKeePassLibPCL
|
||||
using PCLCrypto;
|
||||
#else
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
|
||||
#if !KeePassLibSD
|
||||
using System.IO.Compression;
|
||||
#else
|
||||
using KeePassLibSD;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Collections;
|
||||
using ModernKeePassLib.Cryptography;
|
||||
using ModernKeePassLib.Cryptography.Cipher;
|
||||
using ModernKeePassLib.Delegates;
|
||||
using ModernKeePassLib.Interfaces;
|
||||
using ModernKeePassLib.Keys;
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Security;
|
||||
using ModernKeePassLib.Utility;
|
||||
using ModernKeePassLibPCL.Collections;
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
using ModernKeePassLibPCL.Cryptography.Cipher;
|
||||
using ModernKeePassLibPCL.Delegates;
|
||||
using ModernKeePassLibPCL.Interfaces;
|
||||
using ModernKeePassLibPCL.Keys;
|
||||
using ModernKeePassLibPCL.Resources;
|
||||
using ModernKeePassLibPCL.Security;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialization to KeePass KDBX files.
|
||||
@@ -124,7 +124,7 @@ namespace ModernKeePassLib.Serialization
|
||||
writerStream = hashedStream;
|
||||
else { Debug.Assert(false); throw new FormatException("KdbFormat"); }
|
||||
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
var settings = new XmlWriterSettings() {
|
||||
Encoding = encNoBom,
|
||||
Indent = true,
|
||||
@@ -188,9 +188,9 @@ namespace ModernKeePassLib.Serialization
|
||||
byte[] pbHeader = ms.ToArray();
|
||||
ms.Dispose();
|
||||
|
||||
#if PCL
|
||||
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
|
||||
m_pbHashOfHeader = sha256.HashData(pbHeader.AsBuffer()).ToArray();
|
||||
#if ModernKeePassLibPCL
|
||||
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
|
||||
m_pbHashOfHeader = sha256.HashData(pbHeader);
|
||||
#else
|
||||
SHA256Managed sha256 = new SHA256Managed();
|
||||
m_pbHashOfHeader = sha256.ComputeHash(pbHeader);
|
||||
@@ -235,9 +235,9 @@ namespace ModernKeePassLib.Serialization
|
||||
throw new SecurityException(KLRes.InvalidCompositeKey);
|
||||
ms.Write(pKey32, 0, 32);
|
||||
|
||||
#if PCL
|
||||
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
|
||||
var aesKey = sha256.HashData(ms.GetWindowsRuntimeBuffer()).ToArray();
|
||||
#if ModernKeePassLibPCL
|
||||
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
|
||||
var aesKey = sha256.HashData(ms.ToArray());
|
||||
#else
|
||||
SHA256Managed sha256 = new SHA256Managed();
|
||||
byte[] aesKey = sha256.ComputeHash(ms.ToArray());
|
||||
@@ -264,7 +264,7 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
BinPoolBuild(pgRoot);
|
||||
|
||||
#if !PCL
|
||||
#if !ModernKeePassLibPCL
|
||||
m_xmlWriter.Formatting = Formatting.Indented;
|
||||
m_xmlWriter.IndentChar = '\t';
|
||||
m_xmlWriter.Indentation = 1;
|
||||
|
@@ -29,18 +29,18 @@ using System.Diagnostics;
|
||||
using System.IO.Compression;
|
||||
#endif
|
||||
|
||||
#if PCL
|
||||
using Windows.Storage;
|
||||
#if ModernKeePassLibPCL
|
||||
using PCLStorage;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Collections;
|
||||
using ModernKeePassLib.Cryptography;
|
||||
using ModernKeePassLib.Delegates;
|
||||
using ModernKeePassLib.Interfaces;
|
||||
using ModernKeePassLib.Security;
|
||||
using ModernKeePassLib.Utility;
|
||||
using ModernKeePassLibPCL.Collections;
|
||||
using ModernKeePassLibPCL.Cryptography;
|
||||
using ModernKeePassLibPCL.Delegates;
|
||||
using ModernKeePassLibPCL.Interfaces;
|
||||
using ModernKeePassLibPCL.Security;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// The <c>KdbxFile</c> class supports saving the data to various
|
||||
@@ -195,7 +195,7 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
private PwDatabase m_pwDatabase; // Not null, see constructor
|
||||
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
private XmlWriter m_xmlWriter = null;
|
||||
#else
|
||||
private XmlTextWriter m_xmlWriter = null;
|
||||
@@ -383,16 +383,16 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
++iTry;
|
||||
}
|
||||
#if PCL
|
||||
while(ApplicationData.Current.RoamingFolder.GetFileAsync(strPath).GetResults() != null);
|
||||
#if ModernKeePassLibPCL
|
||||
while(FileSystem.Current.GetFileFromPathAsync(strPath).Result != null);
|
||||
#else
|
||||
while(File.Exists(strPath));
|
||||
#endif
|
||||
|
||||
#if PCL
|
||||
#if ModernKeePassLibPCL
|
||||
byte[] pbData = pb.ReadData();
|
||||
var file = ApplicationData.Current.RoamingFolder.GetFileAsync(strPath).GetResults();
|
||||
using (var stream = file.OpenAsync(FileAccessMode.ReadWrite).GetResults().AsStream()) {
|
||||
var file = FileSystem.Current.GetFileFromPathAsync(strPath).Result;
|
||||
using (var stream = file.OpenAsync(FileAccess.ReadAndWrite).Result) {
|
||||
stream.Write (pbData, 0, pbData.Length);
|
||||
}
|
||||
MemUtil.ZeroByteArray(pbData);
|
||||
|
@@ -21,10 +21,10 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Utility;
|
||||
using ModernKeePassLibPCL.Resources;
|
||||
using ModernKeePassLibPCL.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
namespace ModernKeePassLibPCL.Serialization
|
||||
{
|
||||
public sealed class OldFormatException : Exception
|
||||
{
|
||||
|
Reference in New Issue
Block a user