I followed Acrobat's document "Signing and the Byte Range" :

1) I hash PDF ByteStream data using SHA1
2) this hash is being encrypted using SmartCard internals (mechanism
SHA1_RSA)
"The hash value is encrypted with the signer’s private key using a
supported RSA or DSA signature algorithm. A signature object is
generated. By default, it is a PKCS#7 object."

And now I am wondering is there any PdfPKCS7 API function I can use to
SET this encrypted hash value ???


public static void SignUsingMartCard(string filename, string outfile)
        {
            X509Certificate2 card = GetCertificate();
            Org.BouncyCastle.X509.X509CertificateParser cp = new
Org.BouncyCastle.X509.X509CertificateParser();
            Org.BouncyCastle.X509.X509Certificate[] chain = new
Org.BouncyCastle.X509.X509Certificate[] {
cp.ReadCertificate(card.RawData) };

            PdfReader reader = new PdfReader(filename);
            PdfStamper stp = PdfStamper.CreateSignature(reader, new
FileStream(outfile, FileMode.Create), '\0');
            PdfSignatureAppearance sap = stp.SignatureAppearance;
            sap.SetVisibleSignature(new Rectangle(100, 100, 300, 200),
1, null);
            sap.SignDate = DateTime.Now;
            sap.SetCrypto(null, chain, null, null);
            sap.Reason = "I like to sign using C#";
            sap.Location = "Universe";
            sap.Acro6Layers = true;
            sap.Render =
PdfSignatureAppearance.SignatureRender.NameAndDescription;
            PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE,
PdfName.ADBE_PKCS7_DETACHED);
            dic.Date = new PdfDate(sap.SignDate);
            dic.Name =
PdfPKCS7.GetSubjectFields(chain[0]).GetField("CN");
            if (sap.Reason != null)
                dic.Reason = sap.Reason;
            if (sap.Location != null)
                dic.Location = sap.Location;
            sap.CryptoDictionary = dic;
            
            int csize = 2048;
            Hashtable exc = new Hashtable();
            exc[PdfName.CONTENTS] = csize * 2 + 2;
            sap.PreClose(exc);

            Stream s = sap.RangeStream;
            MemoryStream ss = new MemoryStream();
            int read = 0;
            byte[] buff = new byte[8192];
            while ((read = s.Read(buff, 0, 8192)) > 0)
            {
                ss.Write(buff, 0, read);
            }

            GetExternalBytes(ss.ToArray(), ref digest, ref signature);

            PdfPKCS7 pk7 = new PdfPKCS7(null, chain, null, "SHA1",
false);
            pk7.SetExternalDigest(digest, null, "RSA");
            byte[] pk = pk7.GetEncodedPKCS7();

            byte[] outc = new byte[csize];

            PdfDictionary dic2 = new PdfDictionary();

            Array.Copy(pk, 0, outc, 0, pk.Length);

            dic2.Put(PdfName.CONTENTS, new
PdfString(outc).SetHexWriting(true));
            sap.Close(dic2);
        }

------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to