mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathX509CrlEntry.cs
More file actions
200 lines (170 loc) · 6.79 KB
/
Copy pathX509CrlEntry.cs
File metadata and controls
200 lines (170 loc) · 6.79 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Text;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Utilities;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security.Certificates;
using Org.BouncyCastle.X509.Extension;
namespace Org.BouncyCastle.X509
{
/// <remarks>
/// The following extensions are listed in RFC 2459 as relevant to CRL Entries:
/// <list type="bullet">
/// <item>Reason Code</item>
/// <item>Hold Instruction Code</item>
/// <item>Invalidity Date</item>
/// <item>Certificate Issuer (critical)</item>
/// </list>
/// </remarks>
public class X509CrlEntry
: X509ExtensionBase
{
private CrlEntry c;
private bool isIndirect;
private X509Name previousCertificateIssuer;
private X509Name certificateIssuer;
private volatile bool hashValueSet;
private volatile int hashValue;
public X509CrlEntry(CrlEntry c)
{
this.c = c;
this.certificateIssuer = LoadCertificateIssuer();
}
/// <summary>
/// Constructor for CRLEntries of indirect CRLs.
/// </summary>
/// <remarks>
/// If <paramref name="isIndirect"/> is <c>false</c> <see cref="GetCertificateIssuer"/> will always return
/// <c>null</c>, <paramref name="previousCertificateIssuer"/> is ignored. If <c>true</c> and this CRL entry
/// has no Certificate Issuer extension, then <paramref name="previousCertificateIssuer"/> is returned by
/// <see cref="GetCertificateIssuer"/>.
/// </remarks>
/// <param name="c">The ASN.1 <see cref="CrlEntry"/> object.</param>
/// <param name="isIndirect"><c>true</c> if the corresponding CRL is an indirect CRL.</param>
/// <param name="previousCertificateIssuer">Certificate issuer of the previous CRL entry.</param>
public X509CrlEntry(CrlEntry c, bool isIndirect, X509Name previousCertificateIssuer)
{
this.c = c;
this.isIndirect = isIndirect;
this.previousCertificateIssuer = previousCertificateIssuer;
this.certificateIssuer = LoadCertificateIssuer();
}
public virtual CrlEntry CrlEntry => c;
private X509Name LoadCertificateIssuer()
{
if (!isIndirect)
return null;
var certificateIssuer = this.GetExtension(X509Extensions.CertificateIssuer, GeneralNames.GetInstance);
if (certificateIssuer == null)
return previousCertificateIssuer;
try
{
foreach (var name in certificateIssuer.GetNames())
{
if (name.TagNo == GeneralName.DirectoryName)
return X509Name.GetInstance(name.Name);
}
}
catch (Exception)
{
}
return null;
}
public X509Name GetCertificateIssuer() => certificateIssuer;
protected override X509Extensions GetX509Extensions() => c.Extensions;
public byte[] GetEncoded()
{
try
{
return c.GetEncoded(Asn1Encodable.Der);
}
catch (Exception e)
{
throw new CrlException(e.ToString());
}
}
public BigInteger SerialNumber => c.UserCertificate.Value;
public DateTime RevocationDate => c.RevocationDate.ToDateTime();
public bool HasExtensions => c.Extensions != null;
public override bool Equals(object other)
{
if (this == other)
return true;
if (!(other is X509CrlEntry that))
return false;
if (this.hashValueSet && that.hashValueSet)
{
if (this.hashValue != that.hashValue)
return false;
}
return this.c.Equals(that.c);
}
public override int GetHashCode()
{
if (!hashValueSet)
{
hashValue = this.c.GetHashCode();
hashValueSet = true;
}
return hashValue;
}
public override string ToString()
{
StringBuilder buf = new StringBuilder();
buf.Append(" userCertificate: ").Append(this.SerialNumber).AppendLine();
buf.Append(" revocationDate: ").Append(this.RevocationDate).AppendLine();
buf.Append(" certificateIssuer: ").Append(this.GetCertificateIssuer()).AppendLine();
X509Extensions extensions = c.Extensions;
if (extensions != null)
{
var e = extensions.ExtensionOids.GetEnumerator();
if (e.MoveNext())
{
buf.AppendLine(" crlEntryExtensions:");
do
{
DerObjectIdentifier oid = e.Current;
X509Extension ext = extensions.GetExtension(oid);
if (ext.Value != null)
{
Asn1Object obj = X509ExtensionUtilities.FromExtensionValue(ext.Value);
buf.Append(" critical(")
.Append(ext.IsCritical)
.Append(") ");
try
{
if (oid.Equals(X509Extensions.ReasonCode))
{
buf.Append(new CrlReason(DerEnumerated.GetInstance(obj)));
}
else if (oid.Equals(X509Extensions.CertificateIssuer))
{
buf.Append("Certificate issuer: ").Append(
GeneralNames.GetInstance((Asn1Sequence)obj));
}
else
{
buf.Append(oid.Id);
buf.Append(" value = ").Append(Asn1Dump.DumpAsString(obj));
}
buf.AppendLine();
}
catch (Exception)
{
buf.Append(oid.Id);
buf.Append(" value = ").Append("*****").AppendLine();
}
}
else
{
buf.AppendLine();
}
}
while (e.MoveNext());
}
}
return buf.ToString();
}
}
}