I have a smart card which I got from the cert.body.I have an installed
middlewere.I made some code on signing the pdf file,but when the program is
done I do not get an option in genereted pdf to see certificate -the button
Show Certificate is not enabled.Here is my code:
package spanija1;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import javax.swing.JOptionPane;
/**
*
* @author Jovo
*/
public class Spanija1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws KeyStoreException,
IOException, NoSuchAlgorithmException, CertificateException,
UnrecoverableKeyException, DocumentException, SignatureException,
InvalidKeyException, NoSuchProviderException {
KeyStore ks = KeyStore.getInstance("Windows-MY");
// KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null) ;
//JOptionPane.showMessageDialog(null, ks.aliases().toString());
//deo koji dodajem
//dodajem
//ovde su nazivi samih sertifikata ,stampaju se radi provere
Enumeration en1 = ks.aliases();
while(en1.hasMoreElements()){
System.out.println((String)en1.nextElement());
}
//ovde su nazivi samih sertifikata
//String alias = (String)ks.aliases().nextElement();
Enumeration en = ks.aliases();
String alias = (String)en.nextElement();
alias = (String)en.nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias, "password".toCharArray());
//chain su sami podaci sa kartice-Compensate me Online,,,
java.security.cert.Certificate[] chain = ks.getCertificateChain(alias);
//java.security.cert.Certificate[] chain = new Certificate[1];
//chain[0] = ks.getCertificate(alias);
//java.security.cert.Certificate[] chain = new
java.security.cert.Certificate[1];
//JOptionPane.showMessageDialog(null, chain[0].toString());
//System.out.println(chain[0].toString());
PdfReader reader = new PdfReader("Compensate.pdf");
FileOutputStream fout = new FileOutputStream("signed.pdf");
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0',null,true);
PdfSignatureAppearance sap = stp.getSignatureAppearance();
//sap.setVisibleSignature(alias);
sap.setVisibleSignature(new Rectangle(100 , 100, 200, 200), 1, "CN");
sap.setReason("I like to sign");
sap.setLocation("Universe");
//sap.setAcro6Layers(true);
sap.setExternalDigest(new byte[128], null, "RSA");
sap.setLayer2Text("Ovde ce da bude potpis.\n\nDate: some date");
sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
Calendar cal = Calendar.getInstance();
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.FT, PdfName.SIG);
//dic.put(PdfName.FILTER, new PdfName("SAFE.PPKSF"));
//dic.put(PdfName.SUBFILTER, new PdfName("adbe.pkcs7.detached"));
//skinut komentar
dic.put(PdfName.FILTER, PdfName.ADOBE_PPKLITE);
dic.put(PdfName.SUBFILTER, PdfName.ADBE_PKCS7_DETACHED);
//skinut komentar
dic.put(PdfName.M, new PdfDate(cal));
//dodajem nesto
//dic.put(PdfName.NAME, new PdfString("Neko ime"));
//dodajem nesto
//dic.put(PdfName.NAME, new
PdfString(PdfPKCS7.getSubjectFields((X509Certificate)chain[0]).getField("CN")));
dic.put(PdfName.NAME, new
PdfString(PdfPKCS7.getSubjectFields((X509Certificate)chain[0]).getField("CN")));
//sap.setCrypto(null, chain, null, null);
//sap.setExternalDigest(new byte[128], null, "RSA"); ovo je ok ne
pravi gresku
//dic.putAll(dic);
sap.setCryptoDictionary(dic);//deo koji naknadno moze da se stavi
HashMap exc = new HashMap();
exc.put(PdfName.CONTENTS, new Integer(0x2502));
sap.preClose(exc);
//dodajem ok je
PdfPKCS7 pk7 = new PdfPKCS7(key, chain, null,"SHA1", null, false);
//dodajem
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
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();
byte sh[] = pk7.getAuthenticatedAttributeBytes(hash, cal,null);
pk7.update(sh, 0, sh.length);
PdfDictionary dic2 = new PdfDictionary();
byte sg[] = pk7.getEncodedPKCS7(hash, cal);
byte out[] = new byte[0x2500 / 2];
System.arraycopy(sg, 0, out, 0, sg.length);
dic2.put(PdfName.CONTENTS, new PdfString(out).setHexWriting(true));
sap.close(dic2);
}}
--
View this message in context:
http://itext-general.2136553.n4.nabble.com/Sign-pdf-with-smart-card-on-desktop-tp4655615.html
Sent from the iText - General mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php