448 lines
16 KiB
C#
448 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Mono.Security.X509;
|
|
|
|
namespace System.Security.Cryptography.X509Certificates
|
|
{
|
|
/// <summary>Represents an X.509 store, which is a physical store where certificates are persisted and managed. This class cannot be inherited.</summary>
|
|
// Token: 0x0200029F RID: 671
|
|
public sealed class X509Store
|
|
{
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Store" /> class using the personal certificates of the current user store.</summary>
|
|
// Token: 0x06001881 RID: 6273 RVA: 0x0005394C File Offset: 0x00051B4C
|
|
public X509Store()
|
|
: this("MY", StoreLocation.CurrentUser)
|
|
{
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Store" /> class using the specified store name.</summary>
|
|
/// <param name="storeName">A string value representing the store name. See <see cref="T:System.Security.Cryptography.X509Certificates.StoreName" /> for more information. </param>
|
|
// Token: 0x06001882 RID: 6274 RVA: 0x0005395C File Offset: 0x00051B5C
|
|
public X509Store(string storeName)
|
|
: this(storeName, StoreLocation.CurrentUser)
|
|
{
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Store" /> class using the specified <see cref="T:System.Security.Cryptography.X509Certificates.StoreName" /> value.</summary>
|
|
/// <param name="storeName">One of the <see cref="T:System.Security.Cryptography.X509Certificates.StoreName" /> values. </param>
|
|
// Token: 0x06001883 RID: 6275 RVA: 0x00053968 File Offset: 0x00051B68
|
|
public X509Store(StoreName storeName)
|
|
: this(storeName, StoreLocation.CurrentUser)
|
|
{
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Store" /> class using the specified <see cref="T:System.Security.Cryptography.X509Certificates.StoreLocation" /> value.</summary>
|
|
/// <param name="storeLocation">One of the <see cref="T:System.Security.Cryptography.X509Certificates.StoreLocation" /> values. </param>
|
|
// Token: 0x06001884 RID: 6276 RVA: 0x00053974 File Offset: 0x00051B74
|
|
public X509Store(StoreLocation storeLocation)
|
|
: this("MY", storeLocation)
|
|
{
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Store" /> class using the specified <see cref="T:System.Security.Cryptography.X509Certificates.StoreName" /> and <see cref="T:System.Security.Cryptography.X509Certificates.StoreLocation" /> values.</summary>
|
|
/// <param name="storeName">One of the <see cref="T:System.Security.Cryptography.X509Certificates.StoreName" /> values. </param>
|
|
/// <param name="storeLocation">One of the <see cref="T:System.Security.Cryptography.X509Certificates.StoreLocation" /> values. </param>
|
|
/// <exception cref="T:System.ArgumentException">
|
|
/// <paramref name="storeLocation" /> is not a valid location or <paramref name="storeName" /> is not a valid name. </exception>
|
|
// Token: 0x06001885 RID: 6277 RVA: 0x00053984 File Offset: 0x00051B84
|
|
public X509Store(StoreName storeName, StoreLocation storeLocation)
|
|
{
|
|
if (storeName < StoreName.AddressBook || storeName > StoreName.TrustedPublisher)
|
|
{
|
|
throw new ArgumentException("storeName");
|
|
}
|
|
if (storeLocation < StoreLocation.CurrentUser || storeLocation > StoreLocation.LocalMachine)
|
|
{
|
|
throw new ArgumentException("storeLocation");
|
|
}
|
|
if (storeName != StoreName.CertificateAuthority)
|
|
{
|
|
this._name = storeName.ToString();
|
|
}
|
|
else
|
|
{
|
|
this._name = "CA";
|
|
}
|
|
this._location = storeLocation;
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Store" /> class using an Intptr handle to an HCERTSTORE store.</summary>
|
|
/// <param name="storeHandle">An <see cref="T:System.IntPtr" /> handle to an HCERTSTORE store.</param>
|
|
/// <exception cref="T:System.ArgumentNullException">The <paramref name="storeHandle" /> parameter is null.</exception>
|
|
/// <exception cref="T:System.Security.Cryptography.CryptographicException">The <paramref name="storeHandle" /> parameter points to an invalid context.</exception>
|
|
// Token: 0x06001886 RID: 6278 RVA: 0x00053A04 File Offset: 0x00051C04
|
|
[global::System.MonoTODO("Mono's stores are fully managed. All handles are invalid.")]
|
|
public X509Store(IntPtr storeHandle)
|
|
{
|
|
if (storeHandle == IntPtr.Zero)
|
|
{
|
|
throw new ArgumentNullException("storeHandle");
|
|
}
|
|
throw new CryptographicException("Invalid handle.");
|
|
}
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.X509Certificates.X509Store" /> class using a string representing a value from the <see cref="T:System.Security.Cryptography.X509Certificates.StoreName" /> enumeration and a value from the <see cref="T:System.Security.Cryptography.X509Certificates.StoreLocation" /> enumeration.</summary>
|
|
/// <param name="storeName">A string representing a value from the <see cref="T:System.Security.Cryptography.X509Certificates.StoreName" /> enumeration. </param>
|
|
/// <param name="storeLocation">One of the <see cref="T:System.Security.Cryptography.X509Certificates.StoreLocation" /> values. </param>
|
|
/// <exception cref="T:System.ArgumentException">
|
|
/// <paramref name="storeLocation" /> contains invalid values. </exception>
|
|
// Token: 0x06001887 RID: 6279 RVA: 0x00053A34 File Offset: 0x00051C34
|
|
public X509Store(string storeName, StoreLocation storeLocation)
|
|
{
|
|
if (storeLocation < StoreLocation.CurrentUser || storeLocation > StoreLocation.LocalMachine)
|
|
{
|
|
throw new ArgumentException("storeLocation");
|
|
}
|
|
this._name = storeName;
|
|
this._location = storeLocation;
|
|
}
|
|
|
|
/// <summary>Returns a collection of certificates located in an X.509 certificate store.</summary>
|
|
/// <returns>An <see cref="T:System.Security.Cryptography.X509Certificates.X509Certificate2Collection" /> object.</returns>
|
|
// Token: 0x170007B2 RID: 1970
|
|
// (get) Token: 0x06001888 RID: 6280 RVA: 0x00053A64 File Offset: 0x00051C64
|
|
public X509Certificate2Collection Certificates
|
|
{
|
|
get
|
|
{
|
|
if (this.list == null)
|
|
{
|
|
this.list = new X509Certificate2Collection();
|
|
}
|
|
else if (this.store == null)
|
|
{
|
|
this.list.Clear();
|
|
}
|
|
return this.list;
|
|
}
|
|
}
|
|
|
|
/// <summary>Gets the location of the X.509 certificate store.</summary>
|
|
/// <returns>One of the <see cref="T:System.Security.Cryptography.X509Certificates.StoreLocation" /> values.</returns>
|
|
// Token: 0x170007B3 RID: 1971
|
|
// (get) Token: 0x06001889 RID: 6281 RVA: 0x00053AA0 File Offset: 0x00051CA0
|
|
public StoreLocation Location
|
|
{
|
|
get
|
|
{
|
|
return this._location;
|
|
}
|
|
}
|
|
|
|
/// <summary>Gets the name of the X.509 certificate store.</summary>
|
|
/// <returns>One of the <see cref="T:System.Security.Cryptography.X509Certificates.StoreName" /> values.</returns>
|
|
// Token: 0x170007B4 RID: 1972
|
|
// (get) Token: 0x0600188A RID: 6282 RVA: 0x00053AA8 File Offset: 0x00051CA8
|
|
public string Name
|
|
{
|
|
get
|
|
{
|
|
return this._name;
|
|
}
|
|
}
|
|
|
|
// Token: 0x170007B5 RID: 1973
|
|
// (get) Token: 0x0600188B RID: 6283 RVA: 0x00053AB0 File Offset: 0x00051CB0
|
|
private X509Stores Factory
|
|
{
|
|
get
|
|
{
|
|
if (this._location == StoreLocation.CurrentUser)
|
|
{
|
|
return X509StoreManager.CurrentUser;
|
|
}
|
|
return X509StoreManager.LocalMachine;
|
|
}
|
|
}
|
|
|
|
// Token: 0x170007B6 RID: 1974
|
|
// (get) Token: 0x0600188C RID: 6284 RVA: 0x00053ACC File Offset: 0x00051CCC
|
|
private bool IsOpen
|
|
{
|
|
get
|
|
{
|
|
return this.store != null;
|
|
}
|
|
}
|
|
|
|
// Token: 0x170007B7 RID: 1975
|
|
// (get) Token: 0x0600188D RID: 6285 RVA: 0x00053ADC File Offset: 0x00051CDC
|
|
private bool IsReadOnly
|
|
{
|
|
get
|
|
{
|
|
return Environment.UnityWebSecurityEnabled || (this._flags & OpenFlags.ReadWrite) == OpenFlags.ReadOnly;
|
|
}
|
|
}
|
|
|
|
// Token: 0x170007B8 RID: 1976
|
|
// (get) Token: 0x0600188E RID: 6286 RVA: 0x00053AF8 File Offset: 0x00051CF8
|
|
internal X509Store Store
|
|
{
|
|
get
|
|
{
|
|
return this.store;
|
|
}
|
|
}
|
|
|
|
/// <summary>Gets an <see cref="T:System.IntPtr" /> handle to an HCERTSTORE store. </summary>
|
|
/// <returns>An <see cref="T:System.IntPtr" /> handle to an HCERTSTORE store.</returns>
|
|
// Token: 0x170007B9 RID: 1977
|
|
// (get) Token: 0x0600188F RID: 6287 RVA: 0x00053B00 File Offset: 0x00051D00
|
|
[global::System.MonoTODO("Mono's stores are fully managed. Always returns IntPtr.Zero.")]
|
|
public IntPtr StoreHandle
|
|
{
|
|
get
|
|
{
|
|
return IntPtr.Zero;
|
|
}
|
|
}
|
|
|
|
/// <summary>Adds a certificate to an X.509 certificate store.</summary>
|
|
/// <param name="certificate">An <see cref="T:System.Security.Cryptography.X509Certificates.X509Certificate2" /> object. </param>
|
|
/// <exception cref="T:System.ArgumentNullException">
|
|
/// <paramref name="certificate" /> is null. </exception>
|
|
/// <exception cref="T:System.Security.Cryptography.CryptographicException">The certificate could not be added to the store.</exception>
|
|
// Token: 0x06001890 RID: 6288 RVA: 0x00053B08 File Offset: 0x00051D08
|
|
public void Add(X509Certificate2 certificate)
|
|
{
|
|
if (certificate == null)
|
|
{
|
|
throw new ArgumentNullException("certificate");
|
|
}
|
|
if (!this.IsOpen)
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Store isn't opened."));
|
|
}
|
|
if (this.IsReadOnly)
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Store is read-only."));
|
|
}
|
|
if (!this.Exists(certificate))
|
|
{
|
|
try
|
|
{
|
|
this.store.Import(new X509Certificate(certificate.RawData));
|
|
}
|
|
finally
|
|
{
|
|
this.Certificates.Add(certificate);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Adds a collection of certificates to an X.509 certificate store.</summary>
|
|
/// <param name="certificates">An <see cref="T:System.Security.Cryptography.X509Certificates.X509Certificate2Collection" /> object. </param>
|
|
/// <exception cref="T:System.ArgumentNullException">
|
|
/// <paramref name="certificates" /> is null. </exception>
|
|
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
|
|
// Token: 0x06001891 RID: 6289 RVA: 0x00053BB0 File Offset: 0x00051DB0
|
|
[global::System.MonoTODO("Method isn't transactional (like documented)")]
|
|
public void AddRange(X509Certificate2Collection certificates)
|
|
{
|
|
if (certificates == null)
|
|
{
|
|
throw new ArgumentNullException("certificates");
|
|
}
|
|
if (certificates.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
if (!this.IsOpen)
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Store isn't opened."));
|
|
}
|
|
if (this.IsReadOnly)
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Store is read-only."));
|
|
}
|
|
foreach (X509Certificate2 x509Certificate in certificates)
|
|
{
|
|
if (!this.Exists(x509Certificate))
|
|
{
|
|
try
|
|
{
|
|
this.store.Import(new X509Certificate(x509Certificate.RawData));
|
|
}
|
|
finally
|
|
{
|
|
this.Certificates.Add(x509Certificate);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Closes an X.509 certificate store.</summary>
|
|
// Token: 0x06001892 RID: 6290 RVA: 0x00053C80 File Offset: 0x00051E80
|
|
public void Close()
|
|
{
|
|
this.store = null;
|
|
if (this.list != null)
|
|
{
|
|
this.list.Clear();
|
|
}
|
|
}
|
|
|
|
/// <summary>Opens an X.509 certificate store or creates a new store, depending on <see cref="T:System.Security.Cryptography.X509Certificates.OpenFlags" /> flag settings.</summary>
|
|
/// <param name="flags">One the <see cref="T:System.Security.Cryptography.X509Certificates.OpenFlags" /> values. </param>
|
|
/// <exception cref="T:System.Security.Cryptography.CryptographicException">The store is unreadable. </exception>
|
|
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
|
|
/// <exception cref="T:System.ArgumentException">The store contains invalid values.</exception>
|
|
// Token: 0x06001893 RID: 6291 RVA: 0x00053CA0 File Offset: 0x00051EA0
|
|
public void Open(OpenFlags flags)
|
|
{
|
|
if (string.IsNullOrEmpty(this._name))
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Invalid store name (null or empty)."));
|
|
}
|
|
string name = this._name;
|
|
string text;
|
|
if (name != null)
|
|
{
|
|
if (X509Store.<>f__switch$mapF == null)
|
|
{
|
|
X509Store.<>f__switch$mapF = new Dictionary<string, int>(1) { { "Root", 0 } };
|
|
}
|
|
int num;
|
|
if (X509Store.<>f__switch$mapF.TryGetValue(name, out num))
|
|
{
|
|
if (num == 0)
|
|
{
|
|
text = "Trust";
|
|
goto IL_8B;
|
|
}
|
|
}
|
|
}
|
|
text = this._name;
|
|
IL_8B:
|
|
bool flag = (flags & OpenFlags.OpenExistingOnly) != OpenFlags.OpenExistingOnly;
|
|
this.store = this.Factory.Open(text, flag);
|
|
if (this.store == null)
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Store {0} doesn't exists.", new object[] { this._name }));
|
|
}
|
|
this._flags = flags;
|
|
foreach (X509Certificate x509Certificate in this.store.Certificates)
|
|
{
|
|
this.Certificates.Add(new X509Certificate2(x509Certificate.RawData));
|
|
}
|
|
}
|
|
|
|
/// <summary>Removes a certificate from an X.509 certificate store.</summary>
|
|
/// <param name="certificate">The certificate to remove.</param>
|
|
/// <exception cref="T:System.ArgumentNullException">
|
|
/// <paramref name="certificate" /> is null. </exception>
|
|
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
|
|
// Token: 0x06001894 RID: 6292 RVA: 0x00053DFC File Offset: 0x00051FFC
|
|
public void Remove(X509Certificate2 certificate)
|
|
{
|
|
if (certificate == null)
|
|
{
|
|
throw new ArgumentNullException("certificate");
|
|
}
|
|
if (!this.IsOpen)
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Store isn't opened."));
|
|
}
|
|
if (!this.Exists(certificate))
|
|
{
|
|
return;
|
|
}
|
|
if (this.IsReadOnly)
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Store is read-only."));
|
|
}
|
|
try
|
|
{
|
|
this.store.Remove(new X509Certificate(certificate.RawData));
|
|
}
|
|
finally
|
|
{
|
|
this.Certificates.Remove(certificate);
|
|
}
|
|
}
|
|
|
|
/// <summary>Removes a range of certificates from an X.509 certificate store.</summary>
|
|
/// <param name="certificates">A range of certificates to remove.</param>
|
|
/// <exception cref="T:System.ArgumentNullException">
|
|
/// <paramref name="certificates" /> is null. </exception>
|
|
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
|
|
// Token: 0x06001895 RID: 6293 RVA: 0x00053EA4 File Offset: 0x000520A4
|
|
[global::System.MonoTODO("Method isn't transactional (like documented)")]
|
|
public void RemoveRange(X509Certificate2Collection certificates)
|
|
{
|
|
if (certificates == null)
|
|
{
|
|
throw new ArgumentNullException("certificates");
|
|
}
|
|
if (certificates.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
if (!this.IsOpen)
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Store isn't opened."));
|
|
}
|
|
bool flag = false;
|
|
foreach (X509Certificate2 x509Certificate in certificates)
|
|
{
|
|
if (this.Exists(x509Certificate))
|
|
{
|
|
flag = true;
|
|
}
|
|
}
|
|
if (!flag)
|
|
{
|
|
return;
|
|
}
|
|
if (this.IsReadOnly)
|
|
{
|
|
throw new CryptographicException(global::Locale.GetText("Store is read-only."));
|
|
}
|
|
try
|
|
{
|
|
foreach (X509Certificate2 x509Certificate2 in certificates)
|
|
{
|
|
this.store.Remove(new X509Certificate(x509Certificate2.RawData));
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
this.Certificates.RemoveRange(certificates);
|
|
}
|
|
}
|
|
|
|
// Token: 0x06001896 RID: 6294 RVA: 0x00053FA0 File Offset: 0x000521A0
|
|
private bool Exists(X509Certificate2 certificate)
|
|
{
|
|
if (this.store == null || this.list == null || certificate == null)
|
|
{
|
|
return false;
|
|
}
|
|
foreach (X509Certificate2 x509Certificate in this.list)
|
|
{
|
|
if (certificate.Equals(x509Certificate))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Token: 0x0400137A RID: 4986
|
|
private string _name;
|
|
|
|
// Token: 0x0400137B RID: 4987
|
|
private StoreLocation _location;
|
|
|
|
// Token: 0x0400137C RID: 4988
|
|
private X509Certificate2Collection list;
|
|
|
|
// Token: 0x0400137D RID: 4989
|
|
private OpenFlags _flags;
|
|
|
|
// Token: 0x0400137E RID: 4990
|
|
private X509Store store;
|
|
}
|
|
}
|