Skip to content

Commit 326817d

Browse files
committed
Updated TimeStampTokenGenerator
1 parent 6bda937 commit 326817d

File tree

5 files changed

+472
-230
lines changed

5 files changed

+472
-230
lines changed

crypto/src/cms/SignerInfoGenerator.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ internal void setAssociatedCertificate(X509Certificate certificate)
5858
{
5959
this.certificate = certificate;
6060
}
61+
62+
public SignerInfoGeneratorBuilder NewBuilder()
63+
{
64+
SignerInfoGeneratorBuilder builder = new SignerInfoGeneratorBuilder();
65+
builder.WithSignedAttributeGenerator(signedGen);
66+
builder.WithUnsignedAttributeGenerator(unsignedGen);
67+
builder.SetDirectSignature(isDirectSignature);
68+
return builder;
69+
}
70+
6171
}
6272

6373
public class SignerInfoGeneratorBuilder
@@ -69,6 +79,7 @@ public class SignerInfoGeneratorBuilder
6979
public SignerInfoGeneratorBuilder()
7080
{
7181
}
82+
7283

7384
/**
7485
* If the passed in flag is true, the signer signature will be based on the data, not

crypto/src/crypto/IDigestFactory.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ namespace Org.BouncyCastle.Crypto
22
{
33
/// <summary>
44
/// Base interface for operator factories that create stream-based digest calculators.
5-
/// </summary>
6-
/// <typeparam name="A">The algorithm details/parameter type for the digest factory.</typeparam>
7-
public interface IDigestFactory<out A>
5+
/// </summary>
6+
public interface IDigestFactory
87
{
98
/// <summary>The algorithm details object for calculators made by this factory.</summary>
10-
A AlgorithmDetails { get ; }
9+
object AlgorithmDetails { get ; }
1110

1211
/// <summary>Return the size of the digest associated with this factory.</summary>
1312
/// <returns>The length of the digest produced by this calculators from this factory in bytes.</returns>
@@ -19,6 +18,6 @@ public interface IDigestFactory<out A>
1918
/// and producing the digest block.
2019
/// </summary>
2120
/// <returns>A calculator producing an IBlockResult with the final digest in it.</returns>
22-
IStreamCalculator<IBlockResult> CreateCalculator();
21+
IStreamCalculator CreateCalculator();
2322
}
2423
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Org.BouncyCastle.Asn1;
2+
using Org.BouncyCastle.Asn1.Nist;
3+
using Org.BouncyCastle.Asn1.Oiw;
4+
using Org.BouncyCastle.Asn1.Pkcs;
5+
using Org.BouncyCastle.Asn1.X509;
6+
using Org.BouncyCastle.Crypto.Digests;
7+
using Org.BouncyCastle.Crypto.IO;
8+
using Org.BouncyCastle.Crypto.Paddings;
9+
using Org.BouncyCastle.Crypto.Tests;
10+
using Org.BouncyCastle.Security;
11+
using System;
12+
using System.Collections.Generic;
13+
using System.ComponentModel;
14+
using System.IO;
15+
using System.Linq;
16+
using System.Text;
17+
18+
namespace Org.BouncyCastle.Crypto.Operators
19+
{
20+
21+
22+
23+
public class DigestFactory : IDigestFactory
24+
{
25+
26+
public static DigestFactory Get(DerObjectIdentifier oid)
27+
{
28+
return new DigestFactory(DigestUtilities.GetDigest(oid), oid);
29+
}
30+
31+
public static DigestFactory Get(String mechanism)
32+
{
33+
DerObjectIdentifier oid = DigestUtilities.GetObjectIdentifier(mechanism);
34+
return new DigestFactory(DigestUtilities.GetDigest(oid), oid);
35+
}
36+
37+
38+
private IDigest digest;
39+
private DerObjectIdentifier oid;
40+
41+
public DigestFactory(IDigest digest, DerObjectIdentifier oid)
42+
{
43+
this.digest = digest;
44+
this.oid = oid;
45+
}
46+
47+
public object AlgorithmDetails => new AlgorithmIdentifier(oid);
48+
49+
public int DigestLength => digest.GetDigestSize();
50+
51+
public IStreamCalculator CreateCalculator() => new DfDigestStream(digest);
52+
53+
}
54+
55+
56+
internal class DfDigestStream : IStreamCalculator
57+
{
58+
59+
private DigestSink stream;
60+
61+
public DfDigestStream(IDigest digest)
62+
{
63+
stream = new DigestSink(digest);
64+
}
65+
66+
public Stream Stream => stream;
67+
68+
public object GetResult()
69+
{
70+
byte[] result = new byte[stream.Digest.GetDigestSize()];
71+
stream.Digest.DoFinal(result, 0);
72+
return new SimpleBlockResult(result);
73+
}
74+
75+
}
76+
77+
78+
79+
}

crypto/src/tsp/TimeStampToken.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,17 @@ public TimeStampToken(
8585

8686
if (attr != null)
8787
{
88-
SigningCertificate signCert = SigningCertificate.GetInstance(attr.AttrValues[0]);
8988

90-
this.certID = new CertID(EssCertID.GetInstance(signCert.GetCerts()[0]));
89+
if (attr.AttrValues[0] is SigningCertificateV2)
90+
{
91+
SigningCertificateV2 signCert = SigningCertificateV2.GetInstance(attr.AttrValues[0]);
92+
this.certID = new CertID(EssCertIDv2.GetInstance(signCert.GetCerts()[0]));
93+
}
94+
else
95+
{
96+
SigningCertificate signCert = SigningCertificate.GetInstance(attr.AttrValues[0]);
97+
this.certID = new CertID(EssCertID.GetInstance(signCert.GetCerts()[0]));
98+
}
9199
}
92100
else
93101
{

0 commit comments

Comments
 (0)