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 pathTimeStampToken.cs
More file actions
181 lines (150 loc) · 5.89 KB
/
Copy pathTimeStampToken.cs
File metadata and controls
181 lines (150 loc) · 5.89 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
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Ess;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Tsp;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Cms;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Security.Certificates;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Collections;
using Org.BouncyCastle.X509;
namespace Org.BouncyCastle.Tsp
{
public class TimeStampToken
{
private readonly CmsSignedData m_tsToken;
private readonly SignerInformation m_tsaSignerInfo;
private readonly TimeStampTokenInfo m_tstInfo;
private readonly EssCertIDv2 m_certID;
public TimeStampToken(Asn1.Cms.ContentInfo contentInfo)
: this(new CmsSignedData(contentInfo))
{
}
public TimeStampToken(CmsSignedData signedData)
{
m_tsToken = signedData;
if (!PkcsObjectIdentifiers.IdCTTstInfo.Equals(m_tsToken.SignedContentType))
throw new TspValidationException("ContentInfo object not for a time stamp.");
var signers = m_tsToken.GetSignerInfos().GetSigners();
if (signers.Count != 1)
{
throw new ArgumentException("Time-stamp token signed by "
+ signers.Count
+ " signers, but it must contain just the TSA signature.");
}
m_tsaSignerInfo = signers[0];
try
{
m_tstInfo = new TimeStampTokenInfo(TstInfo.GetInstance(CmsUtilities.GetByteArray(m_tsToken.SignedContent)));
Asn1.Cms.Attribute attr = m_tsaSignerInfo.SignedAttributes[PkcsObjectIdentifiers.IdAASigningCertificate];
if (attr != null)
{
SigningCertificate signCert = SigningCertificate.GetInstance(attr.AttrValues[0]);
m_certID = EssCertIDv2.From(EssCertID.GetInstance(signCert.GetCerts()[0]));
}
else
{
attr = m_tsaSignerInfo.SignedAttributes[PkcsObjectIdentifiers.IdAASigningCertificateV2] ??
throw new TspValidationException("no signing certificate attribute found, time stamp invalid.");
SigningCertificateV2 signCertV2 = SigningCertificateV2.GetInstance(attr.AttrValues[0]);
m_certID = EssCertIDv2.GetInstance(signCertV2.GetCerts()[0]);
}
}
catch (CmsException e)
{
throw new TspException(e.Message, e.InnerException);
}
}
public TimeStampTokenInfo TimeStampInfo => m_tstInfo;
public SignerID SignerID => m_tsaSignerInfo.SignerID;
public Asn1.Cms.AttributeTable SignedAttributes => m_tsaSignerInfo.SignedAttributes;
public Asn1.Cms.AttributeTable UnsignedAttributes => m_tsaSignerInfo.UnsignedAttributes;
public IStore<X509V2AttributeCertificate> GetAttributeCertificates() => m_tsToken.GetAttributeCertificates();
public IStore<X509Certificate> GetCertificates() => m_tsToken.GetCertificates();
public IStore<X509Crl> GetCrls() => m_tsToken.GetCrls();
/**
* Validate the time stamp token.
* <p>
* To be valid the token must be signed by the passed in certificate and
* the certificate must be the one referred to by the SigningCertificate
* attribute included in the hashed attributes of the token. The
* certificate must also have the ExtendedKeyUsageExtension with only
* KeyPurposeID.IdKPTimeStamping and have been valid at the time the
* timestamp was created.
* </p>
* <p>
* A successful call to validate means all the above are true.
* </p>
*/
public void Validate(X509Certificate cert)
{
try
{
// TODO Compare digest calculation to bc-java
byte[] hash = DigestUtilities.CalculateDigest(m_certID.HashAlgorithm.Algorithm, cert.GetEncoded());
if (!Arrays.FixedTimeEquals(m_certID.CertHash.GetOctets(), hash))
throw new TspValidationException("certificate hash does not match certID hash.");
var issuerSerial = m_certID.IssuerSerial;
if (issuerSerial != null)
{
var c = cert.CertificateStructure;
if (!issuerSerial.Serial.Equals(c.SerialNumber))
throw new TspValidationException("certificate serial number does not match certID for signature.");
if (!ValidateIssuer(issuerSerial.Issuer, c.Issuer))
throw new TspValidationException("certificate name does not match certID for signature. ");
}
TspUtil.ValidateCertificate(cert);
if (!cert.IsValid(m_tstInfo.GenTime))
throw new TspValidationException("certificate not valid when time stamp created.");
if (!m_tsaSignerInfo.Verify(cert))
throw new TspValidationException("signature not created by certificate.");
}
catch (CmsException e)
{
if (e.InnerException != null)
throw new TspException(e.Message, e.InnerException);
throw new TspException("CMS exception: " + e, e);
}
catch (CertificateEncodingException e)
{
throw new TspException("problem processing certificate: " + e, e);
}
catch (SecurityUtilityException e)
{
throw new TspException("cannot find algorithm: " + e.Message, e);
}
}
/**
* Return the underlying CmsSignedData object.
*
* @return the underlying CMS structure.
*/
public CmsSignedData ToCmsSignedData() => m_tsToken;
/**
* Return a ASN.1 encoded byte stream representing the encoded object.
*
* @throws IOException if encoding fails.
*/
public byte[] GetEncoded() => m_tsToken.GetEncoded(Asn1Encodable.DL);
/**
* return the ASN.1 encoded representation of this object using the specified encoding.
*
* @param encoding the ASN.1 encoding format to use ("BER" or "DER").
*/
public byte[] GetEncoded(string encoding) => m_tsToken.GetEncoded(encoding);
private static bool ValidateIssuer(GeneralNames issuerNames, X509Name issuer)
{
foreach (GeneralName issuerName in issuerNames.GetNames())
{
if (GeneralName.DirectoryName == issuerName.TagNo &&
X509Name.GetInstance(issuerName.Name).Equivalent(issuer))
{
return true;
}
}
return false;
}
}
}