Files
Dec2017-Script-Dump/mscorlib/Mono/Security/X509/X509Certificate.cs
T
2026-06-04 11:42:34 +02:00

714 lines
18 KiB
C#

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
namespace Mono.Security.X509
{
// Token: 0x020000BC RID: 188
internal class X509Certificate : ISerializable
{
// Token: 0x06000A29 RID: 2601 RVA: 0x0002D4B8 File Offset: 0x0002B6B8
public X509Certificate(byte[] data)
{
if (data != null)
{
if (data.Length > 0 && data[0] != 48)
{
try
{
data = X509Certificate.PEM("CERTIFICATE", data);
}
catch (Exception ex)
{
throw new CryptographicException(X509Certificate.encoding_error, ex);
}
}
this.Parse(data);
}
}
// Token: 0x06000A2A RID: 2602 RVA: 0x0002D52C File Offset: 0x0002B72C
protected X509Certificate(SerializationInfo info, StreamingContext context)
{
this.Parse((byte[])info.GetValue("raw", typeof(byte[])));
}
// Token: 0x06000A2C RID: 2604 RVA: 0x0002D574 File Offset: 0x0002B774
private void Parse(byte[] data)
{
try
{
this.decoder = new ASN1(data);
if (this.decoder.Tag != 48)
{
throw new CryptographicException(X509Certificate.encoding_error);
}
if (this.decoder[0].Tag != 48)
{
throw new CryptographicException(X509Certificate.encoding_error);
}
ASN1 asn = this.decoder[0];
int num = 0;
ASN1 asn2 = this.decoder[0][num];
this.version = 1;
if (asn2.Tag == 160 && asn2.Count > 0)
{
this.version += (int)asn2[0].Value[0];
num++;
}
ASN1 asn3 = this.decoder[0][num++];
if (asn3.Tag != 2)
{
throw new CryptographicException(X509Certificate.encoding_error);
}
this.serialnumber = asn3.Value;
Array.Reverse(this.serialnumber, 0, this.serialnumber.Length);
num++;
this.issuer = asn.Element(num++, 48);
this.m_issuername = X501.ToString(this.issuer);
ASN1 asn4 = asn.Element(num++, 48);
ASN1 asn5 = asn4[0];
this.m_from = ASN1Convert.ToDateTime(asn5);
ASN1 asn6 = asn4[1];
this.m_until = ASN1Convert.ToDateTime(asn6);
this.subject = asn.Element(num++, 48);
this.m_subject = X501.ToString(this.subject);
ASN1 asn7 = asn.Element(num++, 48);
ASN1 asn8 = asn7.Element(0, 48);
ASN1 asn9 = asn8.Element(0, 6);
this.m_keyalgo = ASN1Convert.ToOid(asn9);
ASN1 asn10 = asn8[1];
this.m_keyalgoparams = ((asn8.Count <= 1) ? null : asn10.GetBytes());
ASN1 asn11 = asn7.Element(1, 3);
int num2 = asn11.Length - 1;
this.m_publickey = new byte[num2];
Buffer.BlockCopy(asn11.Value, 1, this.m_publickey, 0, num2);
byte[] value = this.decoder[2].Value;
this.signature = new byte[value.Length - 1];
Buffer.BlockCopy(value, 1, this.signature, 0, this.signature.Length);
asn8 = this.decoder[1];
asn9 = asn8.Element(0, 6);
this.m_signaturealgo = ASN1Convert.ToOid(asn9);
asn10 = asn8[1];
if (asn10 != null)
{
this.m_signaturealgoparams = asn10.GetBytes();
}
else
{
this.m_signaturealgoparams = null;
}
ASN1 asn12 = asn.Element(num, 129);
if (asn12 != null)
{
num++;
this.issuerUniqueID = asn12.Value;
}
ASN1 asn13 = asn.Element(num, 130);
if (asn13 != null)
{
num++;
this.subjectUniqueID = asn13.Value;
}
ASN1 asn14 = asn.Element(num, 163);
if (asn14 != null && asn14.Count == 1)
{
this.extensions = new X509ExtensionCollection(asn14[0]);
}
else
{
this.extensions = new X509ExtensionCollection(null);
}
this.m_encodedcert = (byte[])data.Clone();
}
catch (Exception ex)
{
throw new CryptographicException(X509Certificate.encoding_error, ex);
}
}
// Token: 0x06000A2D RID: 2605 RVA: 0x0002D904 File Offset: 0x0002BB04
private byte[] GetUnsignedBigInteger(byte[] integer)
{
if (integer[0] == 0)
{
int num = integer.Length - 1;
byte[] array = new byte[num];
Buffer.BlockCopy(integer, 1, array, 0, num);
return array;
}
return integer;
}
// Token: 0x1700013A RID: 314
// (get) Token: 0x06000A2E RID: 2606 RVA: 0x0002D934 File Offset: 0x0002BB34
// (set) Token: 0x06000A2F RID: 2607 RVA: 0x0002DA78 File Offset: 0x0002BC78
public DSA DSA
{
get
{
if (this.m_keyalgoparams == null)
{
throw new CryptographicException("Missing key algorithm parameters.");
}
if (this._dsa == null)
{
DSAParameters dsaparameters = default(DSAParameters);
ASN1 asn = new ASN1(this.m_publickey);
if (asn == null || asn.Tag != 2)
{
return null;
}
dsaparameters.Y = this.GetUnsignedBigInteger(asn.Value);
ASN1 asn2 = new ASN1(this.m_keyalgoparams);
if (asn2 == null || asn2.Tag != 48 || asn2.Count < 3)
{
return null;
}
if (asn2[0].Tag != 2 || asn2[1].Tag != 2 || asn2[2].Tag != 2)
{
return null;
}
dsaparameters.P = this.GetUnsignedBigInteger(asn2[0].Value);
dsaparameters.Q = this.GetUnsignedBigInteger(asn2[1].Value);
dsaparameters.G = this.GetUnsignedBigInteger(asn2[2].Value);
this._dsa = new DSACryptoServiceProvider(dsaparameters.Y.Length << 3);
this._dsa.ImportParameters(dsaparameters);
}
return this._dsa;
}
set
{
this._dsa = value;
if (value != null)
{
this._rsa = null;
}
}
}
// Token: 0x1700013B RID: 315
// (get) Token: 0x06000A30 RID: 2608 RVA: 0x0002DA90 File Offset: 0x0002BC90
public X509ExtensionCollection Extensions
{
get
{
return this.extensions;
}
}
// Token: 0x1700013C RID: 316
// (get) Token: 0x06000A31 RID: 2609 RVA: 0x0002DA98 File Offset: 0x0002BC98
public byte[] Hash
{
get
{
if (this.certhash == null)
{
string signaturealgo = this.m_signaturealgo;
if (signaturealgo != null)
{
if (X509Certificate.<>f__switch$map13 == null)
{
X509Certificate.<>f__switch$map13 = new Dictionary<string, int>(5)
{
{ "1.2.840.113549.1.1.2", 0 },
{ "1.2.840.113549.1.1.4", 1 },
{ "1.2.840.113549.1.1.5", 2 },
{ "1.3.14.3.2.29", 2 },
{ "1.2.840.10040.4.3", 2 }
};
}
int num;
if (X509Certificate.<>f__switch$map13.TryGetValue(signaturealgo, out num))
{
HashAlgorithm hashAlgorithm;
switch (num)
{
case 0:
hashAlgorithm = HashAlgorithm.Create("MD2");
break;
case 1:
hashAlgorithm = MD5.Create();
break;
case 2:
hashAlgorithm = SHA1.Create();
break;
default:
goto IL_BD;
}
if (this.decoder == null || this.decoder.Count < 1)
{
return null;
}
byte[] bytes = this.decoder[0].GetBytes();
this.certhash = hashAlgorithm.ComputeHash(bytes, 0, bytes.Length);
goto IL_100;
}
}
IL_BD:
return null;
}
IL_100:
return (byte[])this.certhash.Clone();
}
}
// Token: 0x1700013D RID: 317
// (get) Token: 0x06000A32 RID: 2610 RVA: 0x0002DBB8 File Offset: 0x0002BDB8
public virtual string IssuerName
{
get
{
return this.m_issuername;
}
}
// Token: 0x1700013E RID: 318
// (get) Token: 0x06000A33 RID: 2611 RVA: 0x0002DBC0 File Offset: 0x0002BDC0
public virtual string KeyAlgorithm
{
get
{
return this.m_keyalgo;
}
}
// Token: 0x1700013F RID: 319
// (get) Token: 0x06000A34 RID: 2612 RVA: 0x0002DBC8 File Offset: 0x0002BDC8
// (set) Token: 0x06000A35 RID: 2613 RVA: 0x0002DBE8 File Offset: 0x0002BDE8
public virtual byte[] KeyAlgorithmParameters
{
get
{
if (this.m_keyalgoparams == null)
{
return null;
}
return (byte[])this.m_keyalgoparams.Clone();
}
set
{
this.m_keyalgoparams = value;
}
}
// Token: 0x17000140 RID: 320
// (get) Token: 0x06000A36 RID: 2614 RVA: 0x0002DBF4 File Offset: 0x0002BDF4
public virtual byte[] PublicKey
{
get
{
if (this.m_publickey == null)
{
return null;
}
return (byte[])this.m_publickey.Clone();
}
}
// Token: 0x17000141 RID: 321
// (get) Token: 0x06000A37 RID: 2615 RVA: 0x0002DC14 File Offset: 0x0002BE14
// (set) Token: 0x06000A38 RID: 2616 RVA: 0x0002DCC0 File Offset: 0x0002BEC0
public virtual RSA RSA
{
get
{
if (this._rsa == null)
{
RSAParameters rsaparameters = default(RSAParameters);
ASN1 asn = new ASN1(this.m_publickey);
ASN1 asn2 = asn[0];
if (asn2 == null || asn2.Tag != 2)
{
return null;
}
ASN1 asn3 = asn[1];
if (asn3.Tag != 2)
{
return null;
}
rsaparameters.Modulus = this.GetUnsignedBigInteger(asn2.Value);
rsaparameters.Exponent = asn3.Value;
int num = rsaparameters.Modulus.Length << 3;
this._rsa = new RSACryptoServiceProvider(num);
this._rsa.ImportParameters(rsaparameters);
}
return this._rsa;
}
set
{
if (value != null)
{
this._dsa = null;
}
this._rsa = value;
}
}
// Token: 0x17000142 RID: 322
// (get) Token: 0x06000A39 RID: 2617 RVA: 0x0002DCD8 File Offset: 0x0002BED8
public virtual byte[] RawData
{
get
{
if (this.m_encodedcert == null)
{
return null;
}
return (byte[])this.m_encodedcert.Clone();
}
}
// Token: 0x17000143 RID: 323
// (get) Token: 0x06000A3A RID: 2618 RVA: 0x0002DCF8 File Offset: 0x0002BEF8
public virtual byte[] SerialNumber
{
get
{
if (this.serialnumber == null)
{
return null;
}
return (byte[])this.serialnumber.Clone();
}
}
// Token: 0x17000144 RID: 324
// (get) Token: 0x06000A3B RID: 2619 RVA: 0x0002DD18 File Offset: 0x0002BF18
public virtual byte[] Signature
{
get
{
if (this.signature == null)
{
return null;
}
string signaturealgo = this.m_signaturealgo;
if (signaturealgo != null)
{
if (X509Certificate.<>f__switch$map14 == null)
{
X509Certificate.<>f__switch$map14 = new Dictionary<string, int>(5)
{
{ "1.2.840.113549.1.1.2", 0 },
{ "1.2.840.113549.1.1.4", 0 },
{ "1.2.840.113549.1.1.5", 0 },
{ "1.3.14.3.2.29", 0 },
{ "1.2.840.10040.4.3", 1 }
};
}
int num;
if (X509Certificate.<>f__switch$map14.TryGetValue(signaturealgo, out num))
{
if (num == 0)
{
return (byte[])this.signature.Clone();
}
if (num == 1)
{
ASN1 asn = new ASN1(this.signature);
if (asn == null || asn.Count != 2)
{
return null;
}
byte[] value = asn[0].Value;
byte[] value2 = asn[1].Value;
byte[] array = new byte[40];
int num2 = Math.Max(0, value.Length - 20);
int num3 = Math.Max(0, 20 - value.Length);
Buffer.BlockCopy(value, num2, array, num3, value.Length - num2);
int num4 = Math.Max(0, value2.Length - 20);
int num5 = Math.Max(20, 40 - value2.Length);
Buffer.BlockCopy(value2, num4, array, num5, value2.Length - num4);
return array;
}
}
}
throw new CryptographicException("Unsupported hash algorithm: " + this.m_signaturealgo);
}
}
// Token: 0x17000145 RID: 325
// (get) Token: 0x06000A3C RID: 2620 RVA: 0x0002DE88 File Offset: 0x0002C088
public virtual string SignatureAlgorithm
{
get
{
return this.m_signaturealgo;
}
}
// Token: 0x17000146 RID: 326
// (get) Token: 0x06000A3D RID: 2621 RVA: 0x0002DE90 File Offset: 0x0002C090
public virtual byte[] SignatureAlgorithmParameters
{
get
{
if (this.m_signaturealgoparams == null)
{
return this.m_signaturealgoparams;
}
return (byte[])this.m_signaturealgoparams.Clone();
}
}
// Token: 0x17000147 RID: 327
// (get) Token: 0x06000A3E RID: 2622 RVA: 0x0002DEC0 File Offset: 0x0002C0C0
public virtual string SubjectName
{
get
{
return this.m_subject;
}
}
// Token: 0x17000148 RID: 328
// (get) Token: 0x06000A3F RID: 2623 RVA: 0x0002DEC8 File Offset: 0x0002C0C8
public virtual DateTime ValidFrom
{
get
{
return this.m_from;
}
}
// Token: 0x17000149 RID: 329
// (get) Token: 0x06000A40 RID: 2624 RVA: 0x0002DED0 File Offset: 0x0002C0D0
public virtual DateTime ValidUntil
{
get
{
return this.m_until;
}
}
// Token: 0x1700014A RID: 330
// (get) Token: 0x06000A41 RID: 2625 RVA: 0x0002DED8 File Offset: 0x0002C0D8
public int Version
{
get
{
return this.version;
}
}
// Token: 0x1700014B RID: 331
// (get) Token: 0x06000A42 RID: 2626 RVA: 0x0002DEE0 File Offset: 0x0002C0E0
public bool IsCurrent
{
get
{
return this.WasCurrent(DateTime.UtcNow);
}
}
// Token: 0x06000A43 RID: 2627 RVA: 0x0002DEF0 File Offset: 0x0002C0F0
public bool WasCurrent(DateTime instant)
{
return instant > this.ValidFrom && instant <= this.ValidUntil;
}
// Token: 0x1700014C RID: 332
// (get) Token: 0x06000A44 RID: 2628 RVA: 0x0002DF20 File Offset: 0x0002C120
public byte[] IssuerUniqueIdentifier
{
get
{
if (this.issuerUniqueID == null)
{
return null;
}
return (byte[])this.issuerUniqueID.Clone();
}
}
// Token: 0x1700014D RID: 333
// (get) Token: 0x06000A45 RID: 2629 RVA: 0x0002DF40 File Offset: 0x0002C140
public byte[] SubjectUniqueIdentifier
{
get
{
if (this.subjectUniqueID == null)
{
return null;
}
return (byte[])this.subjectUniqueID.Clone();
}
}
// Token: 0x06000A46 RID: 2630 RVA: 0x0002DF60 File Offset: 0x0002C160
internal bool VerifySignature(DSA dsa)
{
DSASignatureDeformatter dsasignatureDeformatter = new DSASignatureDeformatter(dsa);
dsasignatureDeformatter.SetHashAlgorithm("SHA1");
return dsasignatureDeformatter.VerifySignature(this.Hash, this.Signature);
}
// Token: 0x06000A47 RID: 2631 RVA: 0x0002DF94 File Offset: 0x0002C194
internal string GetHashNameFromOID(string oid)
{
switch (oid)
{
case "1.2.840.113549.1.1.2":
return "MD2";
case "1.2.840.113549.1.1.4":
return "MD5";
case "1.2.840.113549.1.1.5":
case "1.3.14.3.2.29":
return "SHA1";
}
return null;
}
// Token: 0x06000A48 RID: 2632 RVA: 0x0002E02C File Offset: 0x0002C22C
internal bool VerifySignature(RSA rsa)
{
RSAPKCS1SignatureDeformatter rsapkcs1SignatureDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
string hashNameFromOID = this.GetHashNameFromOID(this.m_signaturealgo);
if (hashNameFromOID == null)
{
throw new CryptographicException("Unsupported hash algorithm: " + this.m_signaturealgo);
}
rsapkcs1SignatureDeformatter.SetHashAlgorithm(hashNameFromOID);
return rsapkcs1SignatureDeformatter.VerifySignature(this.Hash, this.Signature);
}
// Token: 0x06000A49 RID: 2633 RVA: 0x0002E084 File Offset: 0x0002C284
public bool VerifySignature(AsymmetricAlgorithm aa)
{
if (aa == null)
{
throw new ArgumentNullException("aa");
}
if (aa is RSA)
{
return this.VerifySignature(aa as RSA);
}
if (aa is DSA)
{
return this.VerifySignature(aa as DSA);
}
throw new NotSupportedException("Unknown Asymmetric Algorithm " + aa.ToString());
}
// Token: 0x06000A4A RID: 2634 RVA: 0x0002E0E8 File Offset: 0x0002C2E8
public bool CheckSignature(byte[] hash, string hashAlgorithm, byte[] signature)
{
RSACryptoServiceProvider rsacryptoServiceProvider = (RSACryptoServiceProvider)this.RSA;
return rsacryptoServiceProvider.VerifyHash(hash, hashAlgorithm, signature);
}
// Token: 0x1700014E RID: 334
// (get) Token: 0x06000A4B RID: 2635 RVA: 0x0002E10C File Offset: 0x0002C30C
public bool IsSelfSigned
{
get
{
return this.m_issuername == this.m_subject && this.VerifySignature(this.RSA);
}
}
// Token: 0x06000A4C RID: 2636 RVA: 0x0002E140 File Offset: 0x0002C340
public ASN1 GetIssuerName()
{
return this.issuer;
}
// Token: 0x06000A4D RID: 2637 RVA: 0x0002E148 File Offset: 0x0002C348
public ASN1 GetSubjectName()
{
return this.subject;
}
// Token: 0x06000A4E RID: 2638 RVA: 0x0002E150 File Offset: 0x0002C350
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("raw", this.m_encodedcert);
}
// Token: 0x06000A4F RID: 2639 RVA: 0x0002E164 File Offset: 0x0002C364
private static byte[] PEM(string type, byte[] data)
{
string @string = Encoding.ASCII.GetString(data);
string text = string.Format("-----BEGIN {0}-----", type);
string text2 = string.Format("-----END {0}-----", type);
int num = @string.IndexOf(text) + text.Length;
int num2 = @string.IndexOf(text2, num);
string text3 = @string.Substring(num, num2 - num);
return Convert.FromBase64String(text3);
}
// Token: 0x04000292 RID: 658
private ASN1 decoder;
// Token: 0x04000293 RID: 659
private byte[] m_encodedcert;
// Token: 0x04000294 RID: 660
private DateTime m_from;
// Token: 0x04000295 RID: 661
private DateTime m_until;
// Token: 0x04000296 RID: 662
private ASN1 issuer;
// Token: 0x04000297 RID: 663
private string m_issuername;
// Token: 0x04000298 RID: 664
private string m_keyalgo;
// Token: 0x04000299 RID: 665
private byte[] m_keyalgoparams;
// Token: 0x0400029A RID: 666
private ASN1 subject;
// Token: 0x0400029B RID: 667
private string m_subject;
// Token: 0x0400029C RID: 668
private byte[] m_publickey;
// Token: 0x0400029D RID: 669
private byte[] signature;
// Token: 0x0400029E RID: 670
private string m_signaturealgo;
// Token: 0x0400029F RID: 671
private byte[] m_signaturealgoparams;
// Token: 0x040002A0 RID: 672
private byte[] certhash;
// Token: 0x040002A1 RID: 673
private RSA _rsa;
// Token: 0x040002A2 RID: 674
private DSA _dsa;
// Token: 0x040002A3 RID: 675
private int version;
// Token: 0x040002A4 RID: 676
private byte[] serialnumber;
// Token: 0x040002A5 RID: 677
private byte[] issuerUniqueID;
// Token: 0x040002A6 RID: 678
private byte[] subjectUniqueID;
// Token: 0x040002A7 RID: 679
private X509ExtensionCollection extensions;
// Token: 0x040002A8 RID: 680
private static string encoding_error = Locale.GetText("Input data cannot be coded as a valid certificate.");
}
}