mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathAttributeCertificateIssuer.cs
More file actions
148 lines (121 loc) · 4.07 KB
/
Copy pathAttributeCertificateIssuer.cs
File metadata and controls
148 lines (121 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities.Collections;
namespace Org.BouncyCastle.X509
{
/// <summary>Carrying class for an attribute certificate issuer.</summary>
public class AttributeCertificateIssuer
: ISelector<X509Certificate>
{
internal readonly Asn1Encodable form;
/// <summary>Set the issuer directly with the ASN.1 structure.</summary>
public AttributeCertificateIssuer(AttCertIssuer issuer)
{
form = issuer.Issuer;
}
public AttributeCertificateIssuer(X509Name principal)
{
form = new V2Form(new GeneralNames(new GeneralName(principal)));
}
private object[] GetNames()
{
GeneralNames name;
if (form is V2Form v2Form)
{
name = v2Form.IssuerName;
}
else
{
name = (GeneralNames)form;
}
GeneralName[] names = name.GetNames();
int count = 0;
for (int i = 0; i != names.Length; i++)
{
if (names[i].TagNo == GeneralName.DirectoryName)
{
++count;
}
}
object[] result = new object[count];
int pos = 0;
for (int i = 0; i != names.Length; i++)
{
if (names[i].TagNo == GeneralName.DirectoryName)
{
result[pos++] = X509Name.GetInstance(names[i].Name);
}
}
return result;
}
/// <summary>Return any principal objects inside the attribute certificate issuer object.</summary>
/// <returns>An array of IPrincipal objects (usually X509Principal).</returns>
public X509Name[] GetPrincipals()
{
object[] p = this.GetNames();
int count = 0;
for (int i = 0; i != p.Length; i++)
{
if (p[i] is X509Name)
{
++count;
}
}
X509Name[] result = new X509Name[count];
int pos = 0;
for (int i = 0; i != p.Length; i++)
{
if (p[i] is X509Name)
{
result[pos++] = (X509Name)p[i];
}
}
return result;
}
private bool MatchesDN(X509Name subject, GeneralNames targets)
{
GeneralName[] names = targets.GetNames();
for (int i = 0; i != names.Length; i++)
{
GeneralName gn = names[i];
if (gn.TagNo == GeneralName.DirectoryName)
{
try
{
if (X509Name.GetInstance(gn.Name).Equivalent(subject))
return true;
}
catch (Exception)
{
}
}
}
return false;
}
public object Clone() => new AttributeCertificateIssuer(AttCertIssuer.GetInstance(form));
public bool Match(X509Certificate x509Cert)
{
if (x509Cert == null)
return false;
if (form is V2Form issuer)
{
if (issuer.BaseCertificateID != null)
{
return issuer.BaseCertificateID.Serial.HasValue(x509Cert.SerialNumber)
&& MatchesDN(x509Cert.IssuerDN, issuer.BaseCertificateID.Issuer);
}
return MatchesDN(x509Cert.SubjectDN, issuer.IssuerName);
}
return MatchesDN(x509Cert.SubjectDN, (GeneralNames)form);
}
public override bool Equals(object obj)
{
if (obj == this)
return true;
return obj is AttributeCertificateIssuer that
&& this.form.Equals(that.form);
}
public override int GetHashCode() => form.GetHashCode();
}
}