0

How to signed PDF with token RFC3161 use itext and BouncyCastle ?

Thanks in advance!!

String token = "my jeton ";
ASN1InputStream in  = new ASN1InputStream(Base64.getDecoder().decode(token));
ASN1Sequence seq = (ASN1Sequence)in.readObject();

convert token to TimeStampToken

TimeStampToken stp = new TimeStampToken(new ContentInfo((ASN1Sequence) seq.getObjectAt(seq.getObjectAt(0) instanceof DERTaggedObject ? 3 : 1)));
CollectionStore store =  (CollectionStore) stp.getCertificates();
Iterator itCert = store.iterator();
JcaX509CertificateConverter jcaConvertor = new JcaX509CertificateConverter();
X509Certificate[] cert = new X509Certificate[1] ;
while(itCert.hasNext()){
    X509CertificateHolder certH = (X509CertificateHolder)itCert.next();
    cert[0] = jcaConvertor.getCertificate((certH));
    System.err.println(cert);
}

file to sign

String SRC = "original.pdf";
String DEST  = "signed.pdf";
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);

generate private key

KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName());
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
Base64Encoder b64 = new Base64Encoder();
generator.initialize(1024);
KeyPair pair = generator.generateKeyPair();
PrivateKey pk = (PrivateKey)pair.getPrivate();

itext signing method with token

sign(SRC, DEST, cert, pk, DigestAlgorithms.SHA1, provider.getName(), CryptoStandard.CMS, "", "", null, null, null, 0);
kava
  • 3
  • 4
  • *"How to get the privateKey of the token for the signature?"* - How do you mean that? Private keys are private. Thus, if it is not your own, you likely won't get it. – mkl Mar 08 '17 at 09:55
  • i want to sign a pdf with a token RFC 3161 using itext. – kava Mar 08 '17 at 10:07
  • Do you want to sign the PDF with an embedded time stamp token according to ISO 32000-1? Or do you want to add a document time stamp to the PDF according to PAdES-4? – mkl Mar 08 '17 at 11:18
  • It is according to the standard PAdES – kava Mar 08 '17 at 12:44
  • You have already chosen the time stamp authority from which you want to retrieve the time stamps? – mkl Mar 08 '17 at 13:05
  • Yes, I have TSA that sends me a token that I converted to TimeStampToken, as the code above. My TSA does not use the rfc 3161 interface but the token is generated according to RFC 3161 – kava Mar 08 '17 at 13:14
  • I'm going to ask again mkl question since your answer points me to think you haven't completelly understood the question. Do you want to add a timestamp to a single signature or do you want to add a timestamp to the full PDF document ? – Egl Mar 08 '17 at 14:16
  • I want to add the token to PDF document – kava Mar 08 '17 at 14:17

0 Answers0