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 pathTimeStampTokenGenerator.cs
More file actions
379 lines (317 loc) · 14 KB
/
Copy pathTimeStampTokenGenerator.cs
File metadata and controls
379 lines (317 loc) · 14 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.Collections.Generic;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Asn1.Ess;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Tsp;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Cms;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Operators;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Collections;
using Org.BouncyCastle.Utilities.Date;
using Org.BouncyCastle.X509;
namespace Org.BouncyCastle.Tsp
{
public enum Resolution
{
R_SECONDS, R_TENTHS_OF_SECONDS, R_HUNDREDTHS_OF_SECONDS, R_MILLISECONDS
}
public class TimeStampTokenGenerator
{
private int accuracySeconds = -1;
private int accuracyMillis = -1;
private int accuracyMicros = -1;
private bool ordering = false;
private GeneralName tsa = null;
private DerObjectIdentifier m_tsaPolicyOid;
private IStore<X509Certificate> x509Certs;
private IStore<X509Crl> x509Crls;
private IStore<X509V2AttributeCertificate> x509AttrCerts;
private Dictionary<DerObjectIdentifier, IStore<Asn1Encodable>> otherRevoc =
new Dictionary<DerObjectIdentifier, IStore<Asn1Encodable>>();
private SignerInfoGenerator signerInfoGenerator;
private Resolution resolution = Resolution.R_SECONDS;
public Resolution Resolution
{
get { return resolution; }
set { resolution = value; }
}
/**
* basic creation - only the default attributes will be included here.
*/
public TimeStampTokenGenerator(AsymmetricKeyParameter key, X509Certificate cert, string digestOID,
string tsaPolicyOID)
: this(key, cert, digestOID, tsaPolicyOID, null, null)
{
}
public TimeStampTokenGenerator(SignerInfoGenerator signerInfoGen, IDigestFactory digestCalculator,
DerObjectIdentifier tsaPolicy, bool isIssuerSerialIncluded)
{
this.signerInfoGenerator = signerInfoGen;
m_tsaPolicyOid = tsaPolicy;
X509Certificate assocCert = signerInfoGen.Certificate ??
throw new ArgumentException("SignerInfoGenerator must have an associated certificate");
TspUtil.ValidateCertificate(assocCert);
var digestAlgID = (AlgorithmIdentifier)digestCalculator.AlgorithmDetails;
var digestAlgOid = digestAlgID.Algorithm;
try
{
var certHash = DerOctetString.WithContents(
X509.X509Utilities.CalculateDigest(digestCalculator, assocCert.GetEncoded()));
var issuerSerial = isIssuerSerialIncluded ? X509.X509Utilities.CreateIssuerSerial(assocCert) : null;
CmsAttributeTableGenerator signedGen;
if (OiwObjectIdentifiers.IdSha1.Equals(digestAlgOid))
{
signedGen = new TableGen(signerInfoGen, new EssCertID(certHash, issuerSerial));
}
else
{
// TODO Why reconstruct this? (if normalizing the parameters should use DigestAlgorithmFinder??)
digestAlgID = new AlgorithmIdentifier(digestAlgOid);
signedGen = new TableGen2(signerInfoGen, new EssCertIDv2(digestAlgID, certHash, issuerSerial));
}
this.signerInfoGenerator = signerInfoGen.NewBuilder()
.WithSignedAttributeGenerator(signedGen)
.Build(signerInfoGen.SignatureFactory, signerInfoGen.Certificate);
}
catch (Exception ex)
{
throw new TspException("Exception processing certificate", ex);
}
}
/**
* create with a signer with extra signed/unsigned attributes.
*/
public TimeStampTokenGenerator(AsymmetricKeyParameter key, X509Certificate cert, string digestOID,
string tsaPolicyOID, Asn1.Cms.AttributeTable signedAttr, Asn1.Cms.AttributeTable unsignedAttr)
: this(
MakeInfoGenerator(key, cert, new DerObjectIdentifier(digestOID), signedAttr, unsignedAttr),
Asn1DigestFactory.Get(OiwObjectIdentifiers.IdSha1),
tsaPolicyOID != null ? new DerObjectIdentifier(tsaPolicyOID) : null,
false)
{
}
internal static SignerInfoGenerator MakeInfoGenerator(AsymmetricKeyParameter key, X509Certificate cert,
DerObjectIdentifier digestOid, Asn1.Cms.AttributeTable signedAttr, Asn1.Cms.AttributeTable unsignedAttr)
{
TspUtil.ValidateCertificate(cert);
//
// Add the ESSCertID attribute
//
IDictionary<DerObjectIdentifier, object> signedAttrs;
if (signedAttr != null)
{
signedAttrs = signedAttr.ToDictionary();
}
else
{
signedAttrs = new Dictionary<DerObjectIdentifier, object>();
}
string digestName = CmsSignedHelper.GetDigestAlgName(digestOid);
DerObjectIdentifier encOid = CmsSignedHelper.GetEncOid(key, digestOid.Id);
string signatureName = digestName + "with" + CmsSignedHelper.GetEncryptionAlgName(encOid);
var contentSigner = new Asn1SignatureFactory(signatureName, key);
var signedGen = new DefaultSignedAttributeTableGenerator(new Asn1.Cms.AttributeTable(signedAttrs));
var unsignedGen = new SimpleAttributeTableGenerator(unsignedAttr);
return new SignerInfoGeneratorBuilder()
.WithSignedAttributeGenerator(signedGen)
.WithUnsignedAttributeGenerator(unsignedGen)
.Build(contentSigner, cert);
}
public void SetAttributeCertificates(IStore<X509V2AttributeCertificate> attributeCertificates)
{
this.x509AttrCerts = attributeCertificates;
}
public void SetCertificates(IStore<X509Certificate> certificates)
{
this.x509Certs = certificates;
}
public void SetCrls(IStore<X509Crl> crls)
{
this.x509Crls = crls;
}
public void AddOtherRevocationInfos(DerObjectIdentifier otherRevInfoFormat,
IStore<Asn1Encodable> otherRevInfoStore)
{
otherRevoc[otherRevInfoFormat] = otherRevInfoStore;
}
public void SetAccuracySeconds(
int accuracySeconds)
{
this.accuracySeconds = accuracySeconds;
}
public void SetAccuracyMillis(
int accuracyMillis)
{
this.accuracyMillis = accuracyMillis;
}
public void SetAccuracyMicros(
int accuracyMicros)
{
this.accuracyMicros = accuracyMicros;
}
public void SetOrdering(
bool ordering)
{
this.ordering = ordering;
}
public void SetTsa(
GeneralName tsa)
{
this.tsa = tsa;
}
public TimeStampToken Generate(TimeStampRequest request, BigInteger serialNumber, DateTime genTime) =>
Generate(request, serialNumber, genTime, null);
public TimeStampToken Generate(TimeStampRequest request, BigInteger serialNumber, DateTime genTime,
X509Extensions additionalExtensions)
{
var timeStampReq = request.TimeStampReq;
var messageImprint = timeStampReq.MessageImprint;
Accuracy accuracy = null;
if (accuracySeconds > 0 || accuracyMillis > 0 || accuracyMicros > 0)
{
DerInteger seconds = null;
if (accuracySeconds > 0)
{
seconds = DerInteger.ValueOf(accuracySeconds);
}
DerInteger millis = null;
if (accuracyMillis > 0)
{
millis = DerInteger.ValueOf(accuracyMillis);
}
DerInteger micros = null;
if (accuracyMicros > 0)
{
micros = DerInteger.ValueOf(accuracyMicros);
}
accuracy = new Accuracy(seconds, millis, micros);
}
DerBoolean derOrdering = null;
if (ordering)
{
derOrdering = DerBoolean.GetInstance(ordering);
}
DerInteger nonce = timeStampReq.Nonce;
DerObjectIdentifier tsaPolicy = timeStampReq.ReqPolicy ?? m_tsaPolicyOid ??
throw new TspValidationException("request contains no policy", PkiFailureInfo.UnacceptedPolicy);
X509Extensions respExtensions = request.Extensions;
if (additionalExtensions != null)
{
X509ExtensionsGenerator extGen = new X509ExtensionsGenerator();
if (respExtensions != null)
{
foreach(var oid in respExtensions.ExtensionOids)
{
extGen.AddExtension(oid, respExtensions.GetExtension(oid));
}
}
foreach (var oid in additionalExtensions.ExtensionOids)
{
extGen.AddExtension(oid, additionalExtensions.GetExtension(oid));
}
respExtensions = extGen.Generate();
}
/*
* RFC 3161. The ASN.1 GeneralizedTime syntax can include fraction-of-second details. Such syntax, without
* the restrictions from RFC 2459 Section 4.1.2.5.2, where GeneralizedTime is limited to represent the
* time with a granularity of one second, may be used here.
*/
var timeStampTime = new DerGeneralizedTime(WithResolution(genTime, resolution));
TstInfo tstInfo = new TstInfo(tsaPolicy, messageImprint,
new DerInteger(serialNumber), timeStampTime, accuracy,
derOrdering, nonce, tsa, respExtensions);
try
{
CmsSignedDataGenerator signedDataGenerator = new CmsSignedDataGenerator();
byte[] derEncodedTstInfo = tstInfo.GetDerEncoded();
if (request.CertReq)
{
signedDataGenerator.AddCertificates(x509Certs);
signedDataGenerator.AddAttributeCertificates(x509AttrCerts);
}
signedDataGenerator.AddCrls(x509Crls);
foreach (KeyValuePair<DerObjectIdentifier, IStore<Asn1Encodable>> entry in otherRevoc)
{
signedDataGenerator.AddOtherRevocationInfos(
otherRevInfoFormat: entry.Key,
otherRevInfoStore: entry.Value);
}
signedDataGenerator.AddSignerInfoGenerator(signerInfoGenerator);
CmsSignedData signedData = signedDataGenerator.Generate(
new CmsProcessableByteArray(PkcsObjectIdentifiers.IdCTTstInfo, derEncodedTstInfo),
encapsulate: true);
return new TimeStampToken(signedData);
}
catch (CmsException cmsEx)
{
throw new TspException("Error generating time-stamp token", cmsEx);
}
catch (IOException e)
{
throw new TspException("Exception encoding info", e);
}
}
private static DateTime WithResolution(DateTime dateTime, Resolution resolution)
{
switch (resolution)
{
case Resolution.R_SECONDS:
return DateTimeUtilities.WithPrecisionSecond(dateTime);
case Resolution.R_TENTHS_OF_SECONDS:
return DateTimeUtilities.WithPrecisionDecisecond(dateTime);
case Resolution.R_HUNDREDTHS_OF_SECONDS:
return DateTimeUtilities.WithPrecisionCentisecond(dateTime);
case Resolution.R_MILLISECONDS:
return DateTimeUtilities.WithPrecisionMillisecond(dateTime);
default:
throw new ArgumentException("Invalid enum value", nameof(resolution));
}
}
private class TableGen
: CmsAttributeTableGenerator
{
private readonly SignerInfoGenerator infoGen;
private readonly EssCertID essCertID;
internal TableGen(SignerInfoGenerator infoGen, EssCertID essCertID)
{
this.infoGen = infoGen;
this.essCertID = essCertID;
}
public Asn1.Cms.AttributeTable GetAttributes(IDictionary<CmsAttributeTableParameter, object> parameters)
{
Asn1.Cms.AttributeTable tab = infoGen.SignedAttributeTableGenerator.GetAttributes(parameters);
if (tab[PkcsObjectIdentifiers.IdAASigningCertificate] == null)
{
return tab.Add(PkcsObjectIdentifiers.IdAASigningCertificate, new SigningCertificate(essCertID));
}
return tab;
}
}
private class TableGen2
: CmsAttributeTableGenerator
{
private readonly SignerInfoGenerator infoGen;
private readonly EssCertIDv2 essCertIDv2;
internal TableGen2(SignerInfoGenerator infoGen, EssCertIDv2 essCertIDv2)
{
this.infoGen = infoGen;
this.essCertIDv2 = essCertIDv2;
}
public Asn1.Cms.AttributeTable GetAttributes(IDictionary<CmsAttributeTableParameter, object> parameters)
{
Asn1.Cms.AttributeTable tab = infoGen.SignedAttributeTableGenerator.GetAttributes(parameters);
if (tab[PkcsObjectIdentifiers.IdAASigningCertificateV2] == null)
{
return tab.Add(PkcsObjectIdentifiers.IdAASigningCertificateV2, new SigningCertificateV2(essCertIDv2));
}
return tab;
}
}
}
}