How to build a RFC 3161 time stamp using Bouncy Castle
Asked Answered
F

1

5

we need to implement our own PDF timestamping mechanism based on X509 certificate (including private key of course) and RFC 3161. I've googled and asked here on SO and proper solution would be to re-implement TSAClient class to do timestamping locally for us (without online TSA). However I didn't find any implementation of RFC 3161 except SecureBlackbox components. It should be possible with Bouncy Castle libraries but I don't know how.

Can you please point me to right direction?

Flagstad answered 27/1, 2014 at 8:46 Comment(0)
I
8

It is possible to generate a RFC3161 timestamp token with Bouncycastle libraries.

First create a TimestampRequest. For your case it is only a wrapper for the digest algorithm and the digest value.

byte[] document = /* ... */
byte[] digest = MessageDigest.getInstance("SHA256").digest(document);
TimeStampRequestGenerator tsReqGen = new TimeStampRequestGenerator();
TimeStampRequest tsReq = tsReqGen.generate(CMSAlgorithm.SHA256, digest);

Then generate the token

DigestCalculator dgCalc = new JcaDigestCalculatorProviderBuilder().build();
ContentSigner signer = new JcaContentSignerBuilder().build(getPrivateKey());
SignerInfoGenerator siGen = new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder()).build(signer, getCertificate());
ASN1ObjectIdentifier policy = new ASN1ObjectIdentifier("1.2.3.4.5.6"); // Replace by your timestamping policy OID
TimeStampTokenGenerator tstGen = new TimeStampTokenGenerator(siGen, dgCalc, policy);
/* Set the parameters e.g. set the accuracy or include the signing certificate */
TimeStampToken tst = tstGen.generate(tsReq, generateSerialNumber(), new Date());
byte[] encoding = tst.getEncoded();
Ireful answered 5/2, 2014 at 15:5 Comment(1)
This is not an adequate code. Many missing and incorrect elements. do provide a working example or at least inform where placeholders are (e.g. variables that are not bound)Methylnaphthalene

© 2022 - 2024 — McMap. All rights reserved.