Skip to content

Commit b290b56

Browse files
committed
Finished NewTspTests
1 parent 6cc9728 commit b290b56

File tree

7 files changed

+561
-34
lines changed

7 files changed

+561
-34
lines changed

crypto/src/asn1/x509/X509ExtensionsGenerator.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,16 @@ public X509Extensions Generate()
7777
{
7878
return new X509Extensions(extOrdering, extensions);
7979
}
80-
}
80+
81+
internal void AddExtension(DerObjectIdentifier oid, X509Extension x509Extension)
82+
{
83+
if (extensions.Contains(oid))
84+
{
85+
throw new ArgumentException ("extension " + oid + " already added");
86+
}
87+
88+
extOrdering.Add(oid);
89+
extensions.Add(oid, x509Extension);
90+
}
91+
}
8192
}

crypto/src/cms/CMSSignedData.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,16 @@ public int Version
150150
get { return signedData.Version.IntValueExact; }
151151
}
152152

153-
/**
153+
internal IX509Store GetCertificates()
154+
{
155+
return Helper.GetCertificates(signedData.Certificates);
156+
}
157+
158+
/**
154159
* return the collection of signers that are associated with the
155160
* signatures for the message.
156161
*/
157-
public SignerInformationStore GetSignerInfos()
162+
public SignerInformationStore GetSignerInfos()
158163
{
159164
if (signerInfoStore == null)
160165
{
@@ -217,7 +222,7 @@ public IX509Store GetCertificates(
217222
string type)
218223
{
219224
if (certificateStore == null)
220-
{
225+
{
221226
certificateStore = Helper.CreateCertificateStore(type, signedData.Certificates);
222227
}
223228

crypto/src/cms/CMSSignedHelper.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Org.BouncyCastle.X509.Store;
2121
using Org.BouncyCastle.Crypto.Parameters;
2222
using Org.BouncyCastle.Utilities.Collections;
23+
using Org.BouncyCastle.Crypto.Tls;
2324

2425
namespace Org.BouncyCastle.Cms
2526
{
@@ -127,7 +128,9 @@ static CmsSignedHelper()
127128
ecAlgorithms.Add(CmsSignedGenerator.DigestSha512, EncryptionECDsaWithSha512);
128129
}
129130

130-
/**
131+
132+
133+
/**
131134
* Return the digest algorithm using one of the standard JCA string
132135
* representations rather than the algorithm identifier (if possible).
133136
*/
@@ -422,5 +425,18 @@ internal string GetEncOid(
422425

423426
return encOID;
424427
}
425-
}
428+
429+
public IX509Store GetCertificates(Asn1Set certificates)
430+
{
431+
ArrayList certList = new ArrayList();
432+
if (certificates != null)
433+
{
434+
foreach (Asn1Encodable enc in certificates)
435+
{
436+
certList.Add(X509CertificateStructure.GetInstance(enc));
437+
}
438+
}
439+
return new X509CollectionStore(certList);
440+
}
441+
}
426442
}

crypto/src/tsp/TimeStampResponseGenerator.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Org.BouncyCastle.Asn1.Cmp;
77
using Org.BouncyCastle.Asn1.Cms;
88
using Org.BouncyCastle.Asn1.Tsp;
9+
using Org.BouncyCastle.Asn1.X509;
910
using Org.BouncyCastle.Math;
1011
using Org.BouncyCastle.Utilities.Date;
1112

@@ -163,6 +164,68 @@ public TimeStampResponse Generate(
163164
}
164165
}
165166

167+
168+
public TimeStampResponse GenerateGrantedResponse(
169+
TimeStampRequest request,
170+
BigInteger serialNumber,
171+
DateTimeObject genTime,
172+
String statusString,
173+
X509Extensions additionalExtensions)
174+
{
175+
TimeStampResp resp;
176+
177+
try
178+
{
179+
if (genTime == null)
180+
throw new TspValidationException("The time source is not available.",
181+
PkiFailureInfo.TimeNotAvailable);
182+
183+
request.Validate(acceptedAlgorithms, acceptedPolicies, acceptedExtensions);
184+
185+
this.status = PkiStatus.Granted;
186+
this.AddStatusString(statusString);
187+
188+
PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
189+
190+
ContentInfo tstTokenContentInfo;
191+
try
192+
{
193+
TimeStampToken token = tokenGenerator.Generate(request, serialNumber, genTime.Value,additionalExtensions);
194+
byte[] encoded = token.ToCmsSignedData().GetEncoded();
195+
196+
tstTokenContentInfo = ContentInfo.GetInstance(Asn1Object.FromByteArray(encoded));
197+
}
198+
catch (IOException e)
199+
{
200+
throw new TspException("Timestamp token received cannot be converted to ContentInfo", e);
201+
}
202+
203+
resp = new TimeStampResp(pkiStatusInfo, tstTokenContentInfo);
204+
}
205+
catch (TspValidationException e)
206+
{
207+
status = PkiStatus.Rejection;
208+
209+
this.SetFailInfoField(e.FailureCode);
210+
this.AddStatusString(e.Message);
211+
212+
PkiStatusInfo pkiStatusInfo = GetPkiStatusInfo();
213+
214+
resp = new TimeStampResp(pkiStatusInfo, null);
215+
}
216+
217+
try
218+
{
219+
return new TimeStampResponse(resp);
220+
}
221+
catch (IOException e)
222+
{
223+
throw new TspException("created badly formatted response!", e);
224+
}
225+
}
226+
227+
228+
166229
class FailInfo
167230
: DerBitString
168231
{

crypto/src/tsp/TimeStampToken.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ public IX509Store GetCrls(
146146
return tsToken.GetCrls(type);
147147
}
148148

149-
public IX509Store GetAttributeCertificates(
149+
public IX509Store GetCertificates()
150+
{
151+
return tsToken.GetCertificates();
152+
}
153+
154+
public IX509Store GetAttributeCertificates(
150155
string type)
151156
{
152157
return tsToken.GetAttributeCertificates(type);

crypto/src/tsp/TimeStampTokenGenerator.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,18 @@ public void SetTsa(
253253
//------------------------------------------------------------------------------
254254

255255
public TimeStampToken Generate(
256+
TimeStampRequest request,
257+
BigInteger serialNumber,
258+
DateTime genTime)
259+
{
260+
return Generate(request, serialNumber, genTime, null);
261+
}
262+
263+
264+
public TimeStampToken Generate(
256265
TimeStampRequest request,
257266
BigInteger serialNumber,
258-
DateTime genTime)
267+
DateTime genTime, X509Extensions additionalExtensions)
259268
{
260269
DerObjectIdentifier digestAlgOID = new DerObjectIdentifier(request.MessageImprintAlgOid);
261270

@@ -304,6 +313,33 @@ public TimeStampToken Generate(
304313
tsaPolicy = new DerObjectIdentifier(request.ReqPolicy);
305314
}
306315

316+
317+
X509Extensions respExtensions = request.Extensions;
318+
if (additionalExtensions != null)
319+
{
320+
X509ExtensionsGenerator extGen = new X509ExtensionsGenerator();
321+
322+
if (respExtensions != null)
323+
{
324+
foreach(object oid in respExtensions.ExtensionOids)
325+
{
326+
DerObjectIdentifier id = DerObjectIdentifier.GetInstance(oid);
327+
extGen.AddExtension(id, respExtensions.GetExtension(DerObjectIdentifier.GetInstance(id)));
328+
}
329+
}
330+
331+
foreach (object oid in additionalExtensions.ExtensionOids)
332+
{
333+
DerObjectIdentifier id = DerObjectIdentifier.GetInstance(oid);
334+
extGen.AddExtension(id, additionalExtensions.GetExtension(DerObjectIdentifier.GetInstance(id)));
335+
336+
}
337+
338+
respExtensions = extGen.Generate();
339+
}
340+
341+
342+
307343
DerGeneralizedTime generalizedTime;
308344
if (resolution != Resolution.R_SECONDS)
309345
{
@@ -316,7 +352,7 @@ public TimeStampToken Generate(
316352

317353
TstInfo tstInfo = new TstInfo(tsaPolicy, messageImprint,
318354
new DerInteger(serialNumber), generalizedTime, accuracy,
319-
derOrdering, nonce, tsa, request.Extensions);
355+
derOrdering, nonce, tsa, respExtensions);
320356

321357
try
322358
{

0 commit comments

Comments
 (0)