TimeStampResponseGenerator.cs
6.43 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
using System;
using System.Collections;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cmp;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.Tsp;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Date;
namespace Org.BouncyCastle.Tsp
{
/**
* Generator for RFC 3161 Time Stamp Responses.
*/
public class TimeStampResponseGenerator
{
private PkiStatus status;
private Asn1EncodableVector statusStrings;
private int failInfo;
private TimeStampTokenGenerator tokenGenerator;
private IList acceptedAlgorithms;
private IList acceptedPolicies;
private IList acceptedExtensions;
public TimeStampResponseGenerator(
TimeStampTokenGenerator tokenGenerator,
IList acceptedAlgorithms)
: this(tokenGenerator, acceptedAlgorithms, null, null)
{
}
public TimeStampResponseGenerator(
TimeStampTokenGenerator tokenGenerator,
IList acceptedAlgorithms,
IList acceptedPolicy)
: this(tokenGenerator, acceptedAlgorithms, acceptedPolicy, null)
{
}
public TimeStampResponseGenerator(
TimeStampTokenGenerator tokenGenerator,
IList acceptedAlgorithms,
IList acceptedPolicies,
IList acceptedExtensions)
{
this.tokenGenerator = tokenGenerator;
this.acceptedAlgorithms = acceptedAlgorithms;
this.acceptedPolicies = acceptedPolicies;
this.acceptedExtensions = acceptedExtensions;
statusStrings = new Asn1EncodableVector();
}
private void AddStatusString(string statusString)
{
statusStrings.Add(new DerUtf8String(statusString));
}
private void SetFailInfoField(int field)
{
failInfo |= field;
}
private PkiStatusInfo GetPkiStatusInfo()
{
Asn1EncodableVector v = new Asn1EncodableVector(
new DerInteger((int)status));
if (statusStrings.Count > 0)
{
v.Add(new PkiFreeText(new DerSequence(statusStrings)));
}
if (failInfo != 0)
{
v.Add(new FailInfo(failInfo));
}
return new PkiStatusInfo(new DerSequence(v));
}
public TimeStampResponse Generate(
TimeStampRequest request,
BigInteger serialNumber,
DateTime genTime)
{
return Generate(request, serialNumber, new DateTimeObject(genTime));
}
/**
* Return an appropriate TimeStampResponse.
* <p>
* If genTime is null a timeNotAvailable error response will be returned.
*
* @param request the request this response is for.
* @param serialNumber serial number for the response token.
* @param genTime generation time for the response token.
* @param provider provider to use for signature calculation.
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws TSPException
* </p>
*/
public TimeStampResponse Generate(
TimeStampRequest request,
BigInteger serialNumber,
DateTimeObject genTime)
{
TimeStampResp resp;
try
{
if (genTime == null)
throw new TspValidationException("The time source is not available.",
PkiFailureInfo.TimeNotAvailable);
request.Validate(acceptedAlgorithms, acceptedPolicies, acceptedExtensions);
this.status = PkiStatus.Granted;
this.AddStatusString("Operation Okay");
PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
ContentInfo tstTokenContentInfo;
try
{
TimeStampToken token = tokenGenerator.Generate(request, serialNumber, genTime.Value);
byte[] encoded = token.ToCmsSignedData().GetEncoded();
tstTokenContentInfo = ContentInfo.GetInstance(Asn1Object.FromByteArray(encoded));
}
catch (IOException e)
{
throw new TspException("Timestamp token received cannot be converted to ContentInfo", e);
}
resp = new TimeStampResp(pkiStatusInfo, tstTokenContentInfo);
}
catch (TspValidationException e)
{
status = PkiStatus.Rejection;
this.SetFailInfoField(e.FailureCode);
this.AddStatusString(e.Message);
PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
resp = new TimeStampResp(pkiStatusInfo, null);
}
try
{
return new TimeStampResponse(resp);
}
catch (IOException e)
{
throw new TspException("created badly formatted response!", e);
}
}
class FailInfo
: DerBitString
{
internal FailInfo(int failInfoValue)
: base(failInfoValue)
{
}
}
/**
* Generate a TimeStampResponse with chosen status and FailInfoField.
*
* @param status the PKIStatus to set.
* @param failInfoField the FailInfoField to set.
* @param statusString an optional string describing the failure.
* @return a TimeStampResponse with a failInfoField and optional statusString
* @throws TSPException in case the response could not be created
*/
public TimeStampResponse GenerateFailResponse(PkiStatus status, int failInfoField, string statusString)
{
this.status = status;
this.SetFailInfoField(failInfoField);
if (statusString != null)
{
this.AddStatusString(statusString);
}
PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
TimeStampResp resp = new TimeStampResp(pkiStatusInfo, null);
try
{
return new TimeStampResponse(resp);
}
catch (IOException e)
{
throw new TspException("created badly formatted response!", e);
}
}
}
}