Paulo Soares wrote:
 >> -----Original Message-----
 >> From: [EMAIL PROTECTED] 
[mailto:[EMAIL PROTECTED] On Behalf Of 
Aiken Sam
 >> Sent: Tuesday, April 24, 2007 8:05 AM
 >> To: itext-questions@lists.sourceforge.net
 >> Subject: Re: [iText-questions] Timestamping
 >>
 >> I've posted an example about timestamping with RFC 3161 TSA.
 >> http://article.gmane.org/gmane.comp.java.lib.itext.general/26813
 >> You must use external signature with custom PKCS7. You can build a 
timestamped PKCS7 CMS using BouncyCastle library.
 >>
 >
 > Now that iText uses the BouncyCastle library it's perhaps time to
 > revisit timestamping. Can you prepare code to incorporate timestamping
 > in the latest iText release?
 >
 >> Paulo, the timestamping example in howtosign.html is too old.
 >> The nCipher DSE200 example is based on obsolete plug-in 
(nCipher.TimeSeal) which is for acrobat Reader 5. Since Adobe supports 
RFC 3161 Timestamping on Acrobat Reader 6, nCipher has withdrawed the 
plugin from their website.
 >>
 >
 > That example was essentially to show how to use external signatures but
 > I'll put a note about its obsolescence.
 >
 > Paulo

Hello

I'm trying to change the portablesigner

(http://portablesigner.sourceforge.net/)

to add timestamping:

So i get the source form portable signer
get the libs, needed, get the example in howto sign.

Try to compile and never get anyting to work.

There is to problems:

TimeStampResponse tsr = new TimeStampResponse(tsre);
raise a exception, but if i comment it out there is another problem

there something strange about preclose

this to lines
sap.preclose(exc);
sap.close(dic2);

if I preclose, it claims that was already preclosed.
if I comment out preclose, it claims that is not preclosed.

Whats wrong with the code bellow?
Any help?

By the way, im not a java programer.
Its my first contact with java.

the timestampclient class was taken from opensignature, and it works i 
can get a timestamp response from the server.
The the ts server works with the ascertia pdf sign&seal.

Regards




/*
  * DoSignPDF.java
  *
  * Created on 21. September 2006, 15:25
  */




package at.gv.wien.PortableSigner;

import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary; // add by gunfas
import com.lowagie.text.pdf.PdfName; // add by gunfas
import com.lowagie.text.pdf.PdfString; // add by gunfas
import com.lowagie.text.pdf.PdfDate; // add by gunfas
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
import java.util.HashMap; // add by gunfas
import java.security.MessageDigest; // add by gunfas
import java.io.InputStream; // add by gunfas
import org.bouncycastle.tsp.TSPAlgorithms; // add by gunfas
import org.bouncycastle.tsp.TimeStampRequest; // add by gunfas
import org.bouncycastle.tsp.TimeStampRequestGenerator; // add by gunfas
import org.bouncycastle.tsp.TimeStampResponse; // add by gunfas
import org.bouncycastle.tsp.TimeStampToken; // add by gunfas
import org.bouncycastle.tsp.TimeStampTokenInfo; // add by gunfas
import java.util.GregorianCalendar; // add by gunfas
import java.util.TimeZone; // add by gunfas
import java.net.URL; // add by gunfas
import java.io.FileInputStream; // add by gunfas
import java.security.DigestInputStream; // add by gunfas
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.util.Date;
import java.util.ResourceBundle;

import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfSignatureAppearance;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
/**
  *
  * @author [EMAIL PROTECTED]
  */

public class DoSignPDF {
     private static PrivateKey privateKey;

     private static Certificate[] certificateChain;

         private static GetPKCS12 pkcs12;

     /** Creates a new instance of DoSignPDF */
     public DoSignPDF(String pdfInputFileName, String pdfOutputFileName,
             String pkcs12FileName, String password, Boolean signText, 
String signLanguage,
                         String sigLogo, String signreason) {
         try {
                         //System.out.println("-> DoSignPDF <-");
                         //System.out.println("Eingabedatei: " + 
pdfInputFileName);
                         //System.out.println("Ausgabedatei: " + 
pdfOutputFileName);
                         //System.out.println("Signaturdatei: " + 
pkcs12FileName);
                         //System.out.println("Signaturblock?: " + 
signText);
                         //System.out.println("Sprache der Blocks: " + 
signLanguage);
                         //System.out.println("Signaturlogo: " + sigLogo);

             java.security.Security.insertProviderAt(
                             new 
org.bouncycastle.jce.provider.BouncyCastleProvider(),2);

                         pkcs12 = new GetPKCS12(pkcs12FileName, password);

             PdfReader reader = null;
             try {
                 reader = new PdfReader(pdfInputFileName);
             } catch (IOException e) {
                 Main.setResult(
                                         java.util.ResourceBundle.getBundle(

"at/gv/wien/PortableSigner/i18n").getString(
                                                 "CouldNotBeOpened"),
                                         true,
                                         e.getLocalizedMessage());
             }
             FileOutputStream fout = null;
             try {
                 fout = new FileOutputStream(pdfOutputFileName);
             } catch (FileNotFoundException e) {
                 Main.setResult(

java.util.ResourceBundle.getBundle("at/gv/wien/PortableSigner/i18n").getString("CouldNotBeWritten"),
                                         true,
                                         e.getLocalizedMessage());
             }

             PdfStamper stp = null;
             try {
                                 Date datum = new 
Date(System.currentTimeMillis());

                                 int pages = reader.getNumberOfPages();

                                 Rectangle size = reader.getPageSize(pages);
                                 stp = 
PdfStamper.createSignature(reader, fout, '\0');

                                 PdfSignatureAppearance sap = 
stp.getSignatureAppearance();

                                 // bof: add by gunfas

                                 MessageDigest messageDigest = 
MessageDigest.getInstance("MD5");
                                 byte buf[] = new byte[8192];
                                 int n;
                                 InputStream inp = sap.getRangeStream();
                                 while ((n = inp.read(buf)) > 0) {
                                     messageDigest.update(buf, 0, n);
                                 }
                                 byte hash[] = messageDigest.digest();

                                 FileInputStream fis = new 
FileInputStream(pdfInputFileName);

                                 // from dse 200 example
                                 // The DataImprint object is the "data 
to be time-stamped"
                                 //DataImprint dataImprint = new 
DataImprint();
                                 //dataImprint.setHashAlgorithm(new 
AlgorithmIdentifier(AlgorithmIdentifier.md5));
                                 //dataImprint.setHashedData(hash);
                                 //TimeStampRequest tsq = new 
TimeStampRequest("");
                                 //tsq.setDataImprint( dataImprint );

                                 // all the removed code above is 
substituted by next lines (reason: dataImprint not found anyhere lets be 
creative)
                                 TimeStampRequestGenerator tsqr = new 
TimeStampRequestGenerator();
                                 tsqr.setCertReq(true);
                                 //tsqr.setReqPolicy("string value");
                                 TimeStampRequest tsq = 
tsqr.generate(TSPAlgorithms.MD5, hash);

                                 byte[] tsqe = tsq.getEncoded();

                                 URL TimeStampServer = new 
URL("http://timestamper/tsa";);
                                 TimeStampClient tsc = new 
TimeStampClient();
                                 byte[] tsre = 
tsc.getHttpTSResponse(tsqe, TimeStampServer);


                                 TimeStampResponse tsr = new 
TimeStampResponse(tsre);
                                 //tsr.validate(tsq);

                                 TimeStampToken tst = 
tsr.getTimeStampToken();
                                 //tst.validate();
                                 TimeStampTokenInfo tsti = 
tst.getTimeStampInfo();

                                 GregorianCalendar cal = new 
GregorianCalendar(TimeZone.getTimeZone("GMT"));
                                 cal.setTime(tsti.getGenTime());


                                 PdfDictionary dic = new PdfDictionary();

                                 dic.put(PdfName.FT, PdfName.SIG);
                                 dic.put(PdfName.SUBFILTER, new 
PdfName("adbe.pkcs7.detached"));
                                 dic.put(PdfName.FILTER, new 
PdfName("Adobe.PPKLite"));
                                 sap.setCryptoDictionary(dic);
                                 HashMap exc = new HashMap();
                                 exc.put(PdfName.CONTENTS, new 
Integer(0x1802));
                                 exc.put(PdfName.M, new Integer(0x19));
                                 exc.put(PdfName.NAME, new Integer(0x1f));

                                 sap.preClose(exc);

                                 PdfDictionary dic2 = new PdfDictionary();
                                 dic2.put(PdfName.CONTENTS, new 
PdfString(tsre).setHexWriting(true));
                                 dic2.put(PdfName.M, new PdfDate(cal));
                                 dic2.put(PdfName.NAME, new 
PdfString("tsa.signed"));
                                 sap.close(dic2);

                                 // eof: add by gunfas

                                 sap.setCrypto(pkcs12.privateKey, 
pkcs12.certificateChain, null, PdfSignatureAppearance.WINCER_SIGNED);
                                 sap.setReason(signreason);
                                 //sap.setLocation("Vienna");


                                   //sap.setCertified(true); // removed 
by gunfas (reason not found anyhere replaced by next line)

sap.setCertificationLevel(sap.CERTIFIED_NO_CHANGES_ALLOWED); // added by 
gunfas
                 stp.close();
                 Main.setResult(

java.util.ResourceBundle.getBundle("at/gv/wien/PortableSigner/i18n").getString("IsGeneratedAndSigned"),
                                         false,
                                         "");
             } catch (Exception e) {
                 Main.setResult(

java.util.ResourceBundle.getBundle("at/gv/wien/PortableSigner/i18n").getString("ErrorWhileSigningFile"),
                                         true,
                                         e.getLocalizedMessage());
             }
         } catch (KeyStoreException kse) {
 
Main.setResult(java.util.ResourceBundle.getBundle("at/gv/wien/PortableSigner/i18n").getString("ErrorCreatingKeystore"),
                     true, kse.getLocalizedMessage());
         }
     }

}


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to