CarotDav_decompile/CarotDAV/XmlSettingManager.cs

366 lines
14 KiB
C#

// Decompiled with JetBrains decompiler
// Type: CarotDAV.XmlSettingManager
// Assembly: CarotDAV, Version=1.13.2.18337, Culture=neutral, PublicKeyToken=null
// MVID: C31F2651-A4A8-4D09-916A-8C6106F5E7C8
// Assembly location: F:\Eigene Dateien\Dropbox\portable Collection\Progs\CarotDAV\CarotDAV.exe
using Microsoft.VisualBasic.CompilerServices;
using Rei.Fs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace CarotDAV
{
public class XmlSettingManager
{
private static string pSettingFolder = (string) null;
private const string FormatName = "CarotDAV Encryption 1.0 ";
private const string SettingFileName = "Setting.xml";
private const string FaviconFolderName = "Favicon";
private static bool pIsPortable;
private static XmlAttributeOverrides attroverrides;
static XmlSettingManager()
{
Assembly entryAssembly = Assembly.GetEntryAssembly();
string directoryName = Path.GetDirectoryName(entryAssembly.Location);
XmlSettingManager.pIsPortable = !File.Exists(Path.Combine(directoryName, "Installed.txt"));
if (XmlSettingManager.pIsPortable)
{
XmlSettingManager.pSettingFolder = directoryName;
}
else
{
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string str1 = "Rei Software";
string str2 = "CarotDAV";
object[] customAttributes = entryAssembly.GetCustomAttributes(typeof (AssemblyCompanyAttribute), false);
if (customAttributes != null && customAttributes.Length > 0)
str1 = ((AssemblyCompanyAttribute) customAttributes[0]).Company;
XmlSettingManager.pSettingFolder = string.Join(Conversions.ToString(Path.DirectorySeparatorChar), new string[3]
{
folderPath,
str1,
str2
});
}
}
public static bool IsPortable
{
get
{
return XmlSettingManager.pIsPortable;
}
}
public static string SettingFolder
{
get
{
return XmlSettingManager.pSettingFolder;
}
}
public static string FaviconFolder
{
get
{
return Path.Combine(XmlSettingManager.SettingFolder, "Favicon");
}
}
private static SymmetricAlgorithm CreateCrypto(string password)
{
Rijndael rijndael = Rijndael.Create();
rijndael.Mode = CipherMode.CBC;
rijndael.Padding = PaddingMode.None;
rijndael.BlockSize = 128;
rijndael.KeySize = 256;
byte[] bytes = Encoding.UTF8.GetBytes("CarotDAV Encryption 1.0 ");
Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, bytes, 1024);
rijndael.Key = rfc2898DeriveBytes.GetBytes(checked ((int) Math.Round(unchecked ((double) rijndael.KeySize / 8.0))));
rijndael.IV = rfc2898DeriveBytes.GetBytes(checked ((int) Math.Round(unchecked ((double) rijndael.BlockSize / 8.0))));
return (SymmetricAlgorithm) rijndael;
}
private static XmlAttributeOverrides GetSerializerAttributes()
{
if (XmlSettingManager.attroverrides == null)
{
XmlAttributes attributes = new XmlAttributes();
int num1 = 0;
int num2 = checked (FsManager.FsTypes.Length - 1);
int index = num1;
while (index <= num2)
{
attributes.XmlArrayItems.Add(new XmlArrayItemAttribute()
{
Type = FsManager.FsTypes[index]
});
checked { ++index; }
}
XmlSettingManager.attroverrides = new XmlAttributeOverrides();
XmlSettingManager.attroverrides.Add(typeof (WebDAVClientSetting), "ConnectionSettings", attributes);
}
return XmlSettingManager.attroverrides;
}
public static void SaveSetting(WebDAVClientSetting setting)
{
XmlSerializer xmls = new XmlSerializer(typeof (WebDAVClientSetting), XmlSettingManager.GetSerializerAttributes());
string settingFolder = XmlSettingManager.SettingFolder;
if (!Directory.Exists(settingFolder))
Directory.CreateDirectory(settingFolder);
XmlSettingManager.InnerSaveSetting(Path.Combine(settingFolder, "Setting.xml"), xmls, setting);
}
private static void InnerSaveSetting(string filepath, XmlSerializer xmls, WebDAVClientSetting setting)
{
byte[] array;
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter streamWriter = new StreamWriter((Stream) memoryStream))
xmls.Serialize((TextWriter) streamWriter, (object) setting);
array = memoryStream.ToArray();
}
XmlDocument document = new XmlDocument();
using (MemoryStream memoryStream = new MemoryStream(array))
document.Load((Stream) memoryStream);
IEnumerator enumerator1;
try
{
enumerator1 = document.GetElementsByTagName("ConnectionSettings").GetEnumerator();
if (enumerator1.MoveNext())
{
XmlNode current = (XmlNode) enumerator1.Current;
int num1 = 0;
int num2 = checked (setting.UnsupportedConections.Count - 1);
int index = num1;
while (index <= num2)
{
current.AppendChild(document.ImportNode((XmlNode) setting.UnsupportedConections[index], true));
checked { ++index; }
}
}
}
finally
{
if (enumerator1 is IDisposable)
(enumerator1 as IDisposable).Dispose();
}
if (!string.IsNullOrEmpty(setting.MasterPassword))
{
List<XmlElement> xmlElementList = new List<XmlElement>();
try
{
foreach (XmlNode xmlNode in document.GetElementsByTagName("ConnectionSettings"))
xmlElementList.Add((XmlElement) xmlNode);
}
finally
{
IEnumerator enumerator2;
if (enumerator2 is IDisposable)
(enumerator2 as IDisposable).Dispose();
}
try
{
foreach (XmlNode xmlNode in document.GetElementsByTagName("LastConnectionSetting"))
xmlElementList.Add((XmlElement) xmlNode);
}
finally
{
IEnumerator enumerator2;
if (enumerator2 is IDisposable)
(enumerator2 as IDisposable).Dispose();
}
SymmetricAlgorithm crypto = XmlSettingManager.CreateCrypto(setting.MasterPassword);
int num1 = 0;
int num2 = checked (xmlElementList.Count - 1);
int index = num1;
while (index <= num2)
{
EncryptedXml encryptedXml = new EncryptedXml(document);
EncryptedData encryptedData = new EncryptedData();
encryptedData.Type = "http://www.w3.org/2001/04/xmlenc#Element";
encryptedData.EncryptionMethod = new EncryptionMethod("http://www.w3.org/2001/04/xmlenc#aes256-cbc");
encryptedData.CipherData.CipherValue = encryptedXml.EncryptData(xmlElementList[index], crypto, false);
EncryptedXml.ReplaceElement(xmlElementList[index], encryptedData, false);
checked { ++index; }
}
}
using (MemoryStream memoryStream = new MemoryStream())
{
document.Save((Stream) memoryStream);
array = memoryStream.ToArray();
}
using (FileStream fileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write))
fileStream.Write(array, 0, array.Length);
}
public static WebDAVClientSetting LoadSetting(string masterpassword)
{
XmlAttributeOverrides attributeOverrides = new XmlAttributeOverrides();
XmlSerializer xmls = new XmlSerializer(typeof (WebDAVClientSetting), XmlSettingManager.GetSerializerAttributes());
XmlSettingManager.UnknownElementManager unknownElementManager = new XmlSettingManager.UnknownElementManager();
xmls.UnknownElement += new XmlElementEventHandler(unknownElementManager.OnUnknownElement);
string settingFolder = XmlSettingManager.SettingFolder;
if (!Directory.Exists(settingFolder))
Directory.CreateDirectory(settingFolder);
WebDAVClientSetting davClientSetting = XmlSettingManager.InnerLoadSetting(Path.Combine(settingFolder, "Setting.xml"), xmls, masterpassword) ?? new WebDAVClientSetting();
int index = 0;
while (index < checked (davClientSetting.ConnectionSettings.Count - 1))
{
if ((object) davClientSetting.ConnectionSettings[index].TargetUri == null)
davClientSetting.ConnectionSettings.RemoveAt(index);
else
checked { ++index; }
}
davClientSetting.UnsupportedConections = unknownElementManager.UnsupportedConections;
return davClientSetting;
}
private static WebDAVClientSetting InnerLoadSetting(string filepath, XmlSerializer xmls, string masterpassword)
{
WebDAVClientSetting davClientSetting = (WebDAVClientSetting) null;
if (!File.Exists(filepath))
return (WebDAVClientSetting) null;
XmlDocument document = new XmlDocument();
document.Load(filepath);
XmlNodeList elementsByTagName = document.GetElementsByTagName("EncryptedData");
if (elementsByTagName.Count > 0)
{
if (string.IsNullOrEmpty(masterpassword))
throw new MasterPasswordException(false);
SymmetricAlgorithm crypto = XmlSettingManager.CreateCrypto(masterpassword);
try
{
int num1 = 0;
int num2 = checked (elementsByTagName.Count - 1);
int num3 = num1;
while (num3 <= num2)
{
EncryptedData encryptedData = new EncryptedData();
EncryptedXml encryptedXml = new EncryptedXml(document);
encryptedData.LoadXml((XmlElement) elementsByTagName[0]);
encryptedXml.ReplaceData((XmlElement) elementsByTagName[0], encryptedXml.DecryptData(encryptedData, crypto));
checked { ++num3; }
}
}
catch (CryptographicException ex)
{
ProjectData.SetProjectError((Exception) ex);
throw new MasterPasswordException(true);
}
}
byte[] array;
using (MemoryStream memoryStream = new MemoryStream())
{
document.Save((Stream) memoryStream);
array = memoryStream.ToArray();
}
using (MemoryStream memoryStream = new MemoryStream(array))
davClientSetting = (WebDAVClientSetting) xmls.Deserialize((Stream) memoryStream);
davClientSetting.MasterPassword = masterpassword;
return davClientSetting;
}
public static void ExportSetting(ConnectionSettingBase connectionsetting, string filename)
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter streamWriter = new StreamWriter((Stream) memoryStream))
new XmlSerializer(connectionsetting.GetType()).Serialize((TextWriter) streamWriter, (object) connectionsetting);
using (FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
byte[] array = memoryStream.ToArray();
fileStream.Write(array, 0, array.Length);
}
}
}
public static ConnectionSettingBase ImportSetting(string filename)
{
ConnectionSettingBase connectionSettingBase1;
ConnectionSettingBase connectionSettingBase2;
try
{
using (TextReader textReader = (TextReader) new StreamReader(filename))
connectionSettingBase1 = (ConnectionSettingBase) new XmlSerializer(typeof (ConnectionSettingBase), FsManager.FsTypes).Deserialize(textReader);
connectionSettingBase2 = connectionSettingBase1;
}
catch (Exception ex1)
{
ProjectData.SetProjectError(ex1);
int num1 = 0;
int num2 = checked (FsManager.FsTypes.Length - 1);
int index = num1;
while (index <= num2)
{
try
{
using (TextReader textReader = (TextReader) new StreamReader(filename))
connectionSettingBase1 = (ConnectionSettingBase) new XmlSerializer(FsManager.FsTypes[index]).Deserialize(textReader);
connectionSettingBase2 = connectionSettingBase1;
ProjectData.ClearProjectError();
goto label_17;
}
catch (Exception ex2)
{
ProjectData.SetProjectError(ex2);
ProjectData.ClearProjectError();
}
checked { ++index; }
}
throw;
}
label_17:
return connectionSettingBase2;
}
public static ConnectionSettingBase CopyConnectionSetting(ConnectionSettingBase cs)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof (ConnectionSettingBase), FsManager.FsTypes);
StringBuilder sb = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sb))
xmlSerializer.Serialize((TextWriter) stringWriter, (object) cs);
using (StringReader stringReader = new StringReader(sb.ToString()))
return (ConnectionSettingBase) xmlSerializer.Deserialize((TextReader) stringReader);
}
public static string XmlSerializeConnectionSetting(ConnectionSettingBase cs)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof (ConnectionSettingBase), FsManager.FsTypes);
StringBuilder sb = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sb))
xmlSerializer.Serialize((TextWriter) stringWriter, (object) cs);
return sb.ToString();
}
private class UnknownElementManager
{
public List<XmlElement> UnsupportedConections;
public UnknownElementManager()
{
this.UnsupportedConections = new List<XmlElement>();
}
public void OnUnknownElement(object sender, XmlElementEventArgs e)
{
if (!e.ExpectedElements.Contains("ConnectionSetting"))
return;
this.UnsupportedConections.Add(e.Element);
}
}
}
}