Files
2026-06-04 11:42:34 +02:00

399 lines
12 KiB
C#

using System;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using Mono.Xml;
namespace Mono.Security.Cryptography
{
// Token: 0x020000A6 RID: 166
internal class KeyPairPersistence
{
// Token: 0x06000913 RID: 2323 RVA: 0x00024114 File Offset: 0x00022314
public KeyPairPersistence(CspParameters parameters)
: this(parameters, null)
{
}
// Token: 0x06000914 RID: 2324 RVA: 0x00024120 File Offset: 0x00022320
public KeyPairPersistence(CspParameters parameters, string keyPair)
{
if (parameters == null)
{
throw new ArgumentNullException("parameters");
}
this._params = this.Copy(parameters);
this._keyvalue = keyPair;
}
// Token: 0x170000FA RID: 250
// (get) Token: 0x06000916 RID: 2326 RVA: 0x00024168 File Offset: 0x00022368
public string Filename
{
get
{
if (this._filename == null)
{
this._filename = string.Format(CultureInfo.InvariantCulture, "[{0}][{1}][{2}].xml", new object[]
{
this._params.ProviderType,
this.ContainerName,
this._params.KeyNumber
});
if (this.UseMachineKeyStore)
{
this._filename = Path.Combine(KeyPairPersistence.MachinePath, this._filename);
}
else
{
this._filename = Path.Combine(KeyPairPersistence.UserPath, this._filename);
}
}
return this._filename;
}
}
// Token: 0x170000FB RID: 251
// (get) Token: 0x06000917 RID: 2327 RVA: 0x0002420C File Offset: 0x0002240C
// (set) Token: 0x06000918 RID: 2328 RVA: 0x00024214 File Offset: 0x00022414
public string KeyValue
{
get
{
return this._keyvalue;
}
set
{
if (this.CanChange)
{
this._keyvalue = value;
}
}
}
// Token: 0x170000FC RID: 252
// (get) Token: 0x06000919 RID: 2329 RVA: 0x00024228 File Offset: 0x00022428
public CspParameters Parameters
{
get
{
return this.Copy(this._params);
}
}
// Token: 0x0600091A RID: 2330 RVA: 0x00024238 File Offset: 0x00022438
public bool Load()
{
if (Environment.SocketSecurityEnabled)
{
return false;
}
bool flag = File.Exists(this.Filename);
if (flag)
{
using (StreamReader streamReader = File.OpenText(this.Filename))
{
this.FromXml(streamReader.ReadToEnd());
}
}
return flag;
}
// Token: 0x0600091B RID: 2331 RVA: 0x000242AC File Offset: 0x000224AC
public void Save()
{
if (Environment.SocketSecurityEnabled)
{
return;
}
using (FileStream fileStream = File.Open(this.Filename, FileMode.Create))
{
StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8);
streamWriter.Write(this.ToXml());
streamWriter.Close();
}
if (this.UseMachineKeyStore)
{
KeyPairPersistence.ProtectMachine(this.Filename);
}
else
{
KeyPairPersistence.ProtectUser(this.Filename);
}
}
// Token: 0x0600091C RID: 2332 RVA: 0x00024348 File Offset: 0x00022548
public void Remove()
{
if (Environment.SocketSecurityEnabled)
{
return;
}
File.Delete(this.Filename);
}
// Token: 0x170000FD RID: 253
// (get) Token: 0x0600091D RID: 2333 RVA: 0x00024360 File Offset: 0x00022560
private static string UserPath
{
get
{
object obj = KeyPairPersistence.lockobj;
lock (obj)
{
if (KeyPairPersistence._userPath == null || !KeyPairPersistence._userPathExists)
{
KeyPairPersistence._userPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ".mono");
KeyPairPersistence._userPath = Path.Combine(KeyPairPersistence._userPath, "keypairs");
KeyPairPersistence._userPathExists = Directory.Exists(KeyPairPersistence._userPath);
if (!KeyPairPersistence._userPathExists)
{
try
{
Directory.CreateDirectory(KeyPairPersistence._userPath);
KeyPairPersistence.ProtectUser(KeyPairPersistence._userPath);
KeyPairPersistence._userPathExists = true;
}
catch (Exception ex)
{
string text = Locale.GetText("Could not create user key store '{0}'.");
throw new CryptographicException(string.Format(text, KeyPairPersistence._userPath), ex);
}
}
}
}
if (!KeyPairPersistence.IsUserProtected(KeyPairPersistence._userPath))
{
string text2 = Locale.GetText("Improperly protected user's key pairs in '{0}'.");
throw new CryptographicException(string.Format(text2, KeyPairPersistence._userPath));
}
return KeyPairPersistence._userPath;
}
}
// Token: 0x170000FE RID: 254
// (get) Token: 0x0600091E RID: 2334 RVA: 0x00024484 File Offset: 0x00022684
private static string MachinePath
{
get
{
object obj = KeyPairPersistence.lockobj;
lock (obj)
{
if (KeyPairPersistence._machinePath == null || !KeyPairPersistence._machinePathExists)
{
KeyPairPersistence._machinePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), ".mono");
KeyPairPersistence._machinePath = Path.Combine(KeyPairPersistence._machinePath, "keypairs");
KeyPairPersistence._machinePathExists = Directory.Exists(KeyPairPersistence._machinePath);
if (!KeyPairPersistence._machinePathExists)
{
try
{
Directory.CreateDirectory(KeyPairPersistence._machinePath);
KeyPairPersistence.ProtectMachine(KeyPairPersistence._machinePath);
KeyPairPersistence._machinePathExists = true;
}
catch (Exception ex)
{
string text = Locale.GetText("Could not create machine key store '{0}'.");
throw new CryptographicException(string.Format(text, KeyPairPersistence._machinePath), ex);
}
}
}
}
if (!KeyPairPersistence.IsMachineProtected(KeyPairPersistence._machinePath))
{
string text2 = Locale.GetText("Improperly protected machine's key pairs in '{0}'.");
throw new CryptographicException(string.Format(text2, KeyPairPersistence._machinePath));
}
return KeyPairPersistence._machinePath;
}
}
// Token: 0x0600091F RID: 2335
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool _CanSecure(string root);
// Token: 0x06000920 RID: 2336
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool _ProtectUser(string path);
// Token: 0x06000921 RID: 2337
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool _ProtectMachine(string path);
// Token: 0x06000922 RID: 2338
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool _IsUserProtected(string path);
// Token: 0x06000923 RID: 2339
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool _IsMachineProtected(string path);
// Token: 0x06000924 RID: 2340 RVA: 0x000245A8 File Offset: 0x000227A8
private static bool CanSecure(string path)
{
int platform = (int)Environment.OSVersion.Platform;
return platform == 4 || platform == 128 || platform == 6 || KeyPairPersistence._CanSecure(Path.GetPathRoot(path));
}
// Token: 0x06000925 RID: 2341 RVA: 0x000245E8 File Offset: 0x000227E8
private static bool ProtectUser(string path)
{
return !KeyPairPersistence.CanSecure(path) || KeyPairPersistence._ProtectUser(path);
}
// Token: 0x06000926 RID: 2342 RVA: 0x00024600 File Offset: 0x00022800
private static bool ProtectMachine(string path)
{
return !KeyPairPersistence.CanSecure(path) || KeyPairPersistence._ProtectMachine(path);
}
// Token: 0x06000927 RID: 2343 RVA: 0x00024618 File Offset: 0x00022818
private static bool IsUserProtected(string path)
{
return !KeyPairPersistence.CanSecure(path) || KeyPairPersistence._IsUserProtected(path);
}
// Token: 0x06000928 RID: 2344 RVA: 0x00024630 File Offset: 0x00022830
private static bool IsMachineProtected(string path)
{
return !KeyPairPersistence.CanSecure(path) || KeyPairPersistence._IsMachineProtected(path);
}
// Token: 0x170000FF RID: 255
// (get) Token: 0x06000929 RID: 2345 RVA: 0x00024648 File Offset: 0x00022848
private bool CanChange
{
get
{
return this._keyvalue == null;
}
}
// Token: 0x17000100 RID: 256
// (get) Token: 0x0600092A RID: 2346 RVA: 0x00024654 File Offset: 0x00022854
private bool UseDefaultKeyContainer
{
get
{
return (this._params.Flags & CspProviderFlags.UseDefaultKeyContainer) == CspProviderFlags.UseDefaultKeyContainer;
}
}
// Token: 0x17000101 RID: 257
// (get) Token: 0x0600092B RID: 2347 RVA: 0x00024668 File Offset: 0x00022868
private bool UseMachineKeyStore
{
get
{
return (this._params.Flags & CspProviderFlags.UseMachineKeyStore) == CspProviderFlags.UseMachineKeyStore;
}
}
// Token: 0x17000102 RID: 258
// (get) Token: 0x0600092C RID: 2348 RVA: 0x0002467C File Offset: 0x0002287C
private string ContainerName
{
get
{
if (this._container == null)
{
if (this.UseDefaultKeyContainer)
{
this._container = "default";
}
else if (this._params.KeyContainerName == null || this._params.KeyContainerName.Length == 0)
{
this._container = Guid.NewGuid().ToString();
}
else
{
byte[] bytes = Encoding.UTF8.GetBytes(this._params.KeyContainerName);
MD5 md = MD5.Create();
byte[] array = md.ComputeHash(bytes);
Guid guid = new Guid(array);
this._container = guid.ToString();
}
}
return this._container;
}
}
// Token: 0x0600092D RID: 2349 RVA: 0x0002472C File Offset: 0x0002292C
private CspParameters Copy(CspParameters p)
{
return new CspParameters(p.ProviderType, p.ProviderName, p.KeyContainerName)
{
KeyNumber = p.KeyNumber,
Flags = p.Flags
};
}
// Token: 0x0600092E RID: 2350 RVA: 0x0002476C File Offset: 0x0002296C
private void FromXml(string xml)
{
SecurityParser securityParser = new SecurityParser();
securityParser.LoadXml(xml);
SecurityElement securityElement = securityParser.ToXml();
if (securityElement.Tag == "KeyPair")
{
SecurityElement securityElement2 = securityElement.SearchForChildByTag("KeyValue");
if (securityElement2.Children.Count > 0)
{
this._keyvalue = securityElement2.Children[0].ToString();
}
}
}
// Token: 0x0600092F RID: 2351 RVA: 0x000247D8 File Offset: 0x000229D8
private string ToXml()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("<KeyPair>{0}\t<Properties>{0}\t\t<Provider ", Environment.NewLine);
if (this._params.ProviderName != null && this._params.ProviderName.Length != 0)
{
stringBuilder.AppendFormat("Name=\"{0}\" ", this._params.ProviderName);
}
stringBuilder.AppendFormat("Type=\"{0}\" />{1}\t\t<Container ", this._params.ProviderType, Environment.NewLine);
stringBuilder.AppendFormat("Name=\"{0}\" />{1}\t</Properties>{1}\t<KeyValue", this.ContainerName, Environment.NewLine);
if (this._params.KeyNumber != -1)
{
stringBuilder.AppendFormat(" Id=\"{0}\" ", this._params.KeyNumber);
}
stringBuilder.AppendFormat(">{1}\t\t{0}{1}\t</KeyValue>{1}</KeyPair>{1}", this.KeyValue, Environment.NewLine);
return stringBuilder.ToString();
}
// Token: 0x040001F2 RID: 498
private static bool _userPathExists = false;
// Token: 0x040001F3 RID: 499
private static string _userPath;
// Token: 0x040001F4 RID: 500
private static bool _machinePathExists = false;
// Token: 0x040001F5 RID: 501
private static string _machinePath;
// Token: 0x040001F6 RID: 502
private CspParameters _params;
// Token: 0x040001F7 RID: 503
private string _keyvalue;
// Token: 0x040001F8 RID: 504
private string _filename;
// Token: 0x040001F9 RID: 505
private string _container;
// Token: 0x040001FA RID: 506
private static object lockobj = new object();
}
}