285 lines
6.8 KiB
C#
285 lines
6.8 KiB
C#
using System;
|
|
using Mono.Security.X509.Extensions;
|
|
|
|
namespace Mono.Security.X509
|
|
{
|
|
// Token: 0x020000BF RID: 191
|
|
internal class X509Chain
|
|
{
|
|
// Token: 0x06000A68 RID: 2664 RVA: 0x0002E464 File Offset: 0x0002C664
|
|
public X509Chain()
|
|
{
|
|
this.certs = new X509CertificateCollection();
|
|
}
|
|
|
|
// Token: 0x06000A69 RID: 2665 RVA: 0x0002E478 File Offset: 0x0002C678
|
|
public X509Chain(X509CertificateCollection chain)
|
|
: this()
|
|
{
|
|
this._chain = new X509CertificateCollection();
|
|
this._chain.AddRange(chain);
|
|
}
|
|
|
|
// Token: 0x17000152 RID: 338
|
|
// (get) Token: 0x06000A6A RID: 2666 RVA: 0x0002E498 File Offset: 0x0002C698
|
|
public X509CertificateCollection Chain
|
|
{
|
|
get
|
|
{
|
|
return this._chain;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000153 RID: 339
|
|
// (get) Token: 0x06000A6B RID: 2667 RVA: 0x0002E4A0 File Offset: 0x0002C6A0
|
|
public X509Certificate Root
|
|
{
|
|
get
|
|
{
|
|
return this._root;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000154 RID: 340
|
|
// (get) Token: 0x06000A6C RID: 2668 RVA: 0x0002E4A8 File Offset: 0x0002C6A8
|
|
public X509ChainStatusFlags Status
|
|
{
|
|
get
|
|
{
|
|
return this._status;
|
|
}
|
|
}
|
|
|
|
// Token: 0x17000155 RID: 341
|
|
// (get) Token: 0x06000A6D RID: 2669 RVA: 0x0002E4B0 File Offset: 0x0002C6B0
|
|
// (set) Token: 0x06000A6E RID: 2670 RVA: 0x0002E4E8 File Offset: 0x0002C6E8
|
|
public X509CertificateCollection TrustAnchors
|
|
{
|
|
get
|
|
{
|
|
if (this.roots == null)
|
|
{
|
|
this.roots = new X509CertificateCollection();
|
|
this.roots.AddRange(X509StoreManager.TrustedRootCertificates);
|
|
return this.roots;
|
|
}
|
|
return this.roots;
|
|
}
|
|
set
|
|
{
|
|
this.roots = value;
|
|
}
|
|
}
|
|
|
|
// Token: 0x06000A6F RID: 2671 RVA: 0x0002E4F4 File Offset: 0x0002C6F4
|
|
public void LoadCertificate(X509Certificate x509)
|
|
{
|
|
this.certs.Add(x509);
|
|
}
|
|
|
|
// Token: 0x06000A70 RID: 2672 RVA: 0x0002E504 File Offset: 0x0002C704
|
|
public void LoadCertificates(X509CertificateCollection collection)
|
|
{
|
|
this.certs.AddRange(collection);
|
|
}
|
|
|
|
// Token: 0x06000A71 RID: 2673 RVA: 0x0002E514 File Offset: 0x0002C714
|
|
public X509Certificate FindByIssuerName(string issuerName)
|
|
{
|
|
foreach (X509Certificate x509Certificate in this.certs)
|
|
{
|
|
if (x509Certificate.IssuerName == issuerName)
|
|
{
|
|
return x509Certificate;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Token: 0x06000A72 RID: 2674 RVA: 0x0002E594 File Offset: 0x0002C794
|
|
public bool Build(X509Certificate leaf)
|
|
{
|
|
this._status = X509ChainStatusFlags.NoError;
|
|
if (this._chain == null)
|
|
{
|
|
this._chain = new X509CertificateCollection();
|
|
X509Certificate x509Certificate = leaf;
|
|
X509Certificate x509Certificate2 = x509Certificate;
|
|
while (x509Certificate != null && !x509Certificate.IsSelfSigned)
|
|
{
|
|
x509Certificate2 = x509Certificate;
|
|
this._chain.Add(x509Certificate);
|
|
x509Certificate = this.FindCertificateParent(x509Certificate);
|
|
}
|
|
this._root = this.FindCertificateRoot(x509Certificate2);
|
|
}
|
|
else
|
|
{
|
|
int count = this._chain.Count;
|
|
if (count > 0)
|
|
{
|
|
if (this.IsParent(leaf, this._chain[0]))
|
|
{
|
|
int i;
|
|
for (i = 1; i < count; i++)
|
|
{
|
|
if (!this.IsParent(this._chain[i - 1], this._chain[i]))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
if (i == count)
|
|
{
|
|
this._root = this.FindCertificateRoot(this._chain[count - 1]);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this._root = this.FindCertificateRoot(leaf);
|
|
}
|
|
}
|
|
if (this._chain != null && this._status == X509ChainStatusFlags.NoError)
|
|
{
|
|
foreach (X509Certificate x509Certificate3 in this._chain)
|
|
{
|
|
if (!this.IsValid(x509Certificate3))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
if (!this.IsValid(leaf))
|
|
{
|
|
if (this._status == X509ChainStatusFlags.NotTimeNested)
|
|
{
|
|
this._status = X509ChainStatusFlags.NotTimeValid;
|
|
}
|
|
return false;
|
|
}
|
|
if (this._root != null && !this.IsValid(this._root))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
IL_1A6:
|
|
return this._status == X509ChainStatusFlags.NoError;
|
|
}
|
|
|
|
// Token: 0x06000A73 RID: 2675 RVA: 0x0002E770 File Offset: 0x0002C970
|
|
public void Reset()
|
|
{
|
|
this._status = X509ChainStatusFlags.NoError;
|
|
this.roots = null;
|
|
this.certs.Clear();
|
|
if (this._chain != null)
|
|
{
|
|
this._chain.Clear();
|
|
}
|
|
}
|
|
|
|
// Token: 0x06000A74 RID: 2676 RVA: 0x0002E7A4 File Offset: 0x0002C9A4
|
|
private bool IsValid(X509Certificate cert)
|
|
{
|
|
if (!cert.IsCurrent)
|
|
{
|
|
this._status = X509ChainStatusFlags.NotTimeNested;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Token: 0x06000A75 RID: 2677 RVA: 0x0002E7BC File Offset: 0x0002C9BC
|
|
private X509Certificate FindCertificateParent(X509Certificate child)
|
|
{
|
|
foreach (X509Certificate x509Certificate in this.certs)
|
|
{
|
|
if (this.IsParent(child, x509Certificate))
|
|
{
|
|
return x509Certificate;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Token: 0x06000A76 RID: 2678 RVA: 0x0002E838 File Offset: 0x0002CA38
|
|
private X509Certificate FindCertificateRoot(X509Certificate potentialRoot)
|
|
{
|
|
if (potentialRoot == null)
|
|
{
|
|
this._status = X509ChainStatusFlags.PartialChain;
|
|
return null;
|
|
}
|
|
if (this.IsTrusted(potentialRoot))
|
|
{
|
|
return potentialRoot;
|
|
}
|
|
foreach (X509Certificate x509Certificate in this.TrustAnchors)
|
|
{
|
|
if (this.IsParent(potentialRoot, x509Certificate))
|
|
{
|
|
return x509Certificate;
|
|
}
|
|
}
|
|
if (potentialRoot.IsSelfSigned)
|
|
{
|
|
this._status = X509ChainStatusFlags.UntrustedRoot;
|
|
return potentialRoot;
|
|
}
|
|
this._status = X509ChainStatusFlags.PartialChain;
|
|
return null;
|
|
}
|
|
|
|
// Token: 0x06000A77 RID: 2679 RVA: 0x0002E8F4 File Offset: 0x0002CAF4
|
|
private bool IsTrusted(X509Certificate potentialTrusted)
|
|
{
|
|
return this.TrustAnchors.Contains(potentialTrusted);
|
|
}
|
|
|
|
// Token: 0x06000A78 RID: 2680 RVA: 0x0002E904 File Offset: 0x0002CB04
|
|
private bool IsParent(X509Certificate child, X509Certificate parent)
|
|
{
|
|
if (child.IssuerName != parent.SubjectName)
|
|
{
|
|
return false;
|
|
}
|
|
if (parent.Version > 2 && !this.IsTrusted(parent))
|
|
{
|
|
X509Extension x509Extension = parent.Extensions["2.5.29.19"];
|
|
if (x509Extension != null)
|
|
{
|
|
BasicConstraintsExtension basicConstraintsExtension = new BasicConstraintsExtension(x509Extension);
|
|
if (!basicConstraintsExtension.CertificateAuthority)
|
|
{
|
|
this._status = X509ChainStatusFlags.InvalidBasicConstraints;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this._status = X509ChainStatusFlags.InvalidBasicConstraints;
|
|
}
|
|
}
|
|
if (!child.VerifySignature(parent.RSA))
|
|
{
|
|
this._status = X509ChainStatusFlags.NotSignatureValid;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Token: 0x040002AD RID: 685
|
|
private X509CertificateCollection roots;
|
|
|
|
// Token: 0x040002AE RID: 686
|
|
private X509CertificateCollection certs;
|
|
|
|
// Token: 0x040002AF RID: 687
|
|
private X509Certificate _root;
|
|
|
|
// Token: 0x040002B0 RID: 688
|
|
private X509CertificateCollection _chain;
|
|
|
|
// Token: 0x040002B1 RID: 689
|
|
private X509ChainStatusFlags _status;
|
|
}
|
|
}
|