Hello everybody, I'm looking into applying a signature to a pdf using iTextSharp, when the certificate is a *non-exportable* Windows Certificate. Since I can't get the PublicKey for this type of Certificate, I'm basing it on the code in Appendix D of the book. The code runs, but the signature does not validate. Adobe Reader gives the message "There are errors in the formatting or information contained in this signature (support information: SigDict /Contents illegal data)".
To generate the actual signature bytes, I'm using:
System.Security.Cryptography.RSACryptoServiceProvider.SignHash()
on the bytes returned by hashing with a SHA1 hash.
For the signing certificate, I'm using:
new org.bouncycastle.x509.X509Certificate( cert.GetRawCertData() );
Other than that, the code is very close to what's in the book. Based
on the error message, I compared the /Contents of this file with a
valid signature (generated with the same cert, but using Adobe
Acrobat). The one that doesn't verify is much shorter (132 bytes vs
1000+ bytes).
Has anyone used iTextSharp to sign a pdf with a non-exportable
certificate? Does anyone see what I'm doing wrong?
Thanks in advance,
Brian
Attachments: goodsig.pdf (output from Acrobat), badsig.pdf (my output)
Here is the code:
public static void WindowsSign(Stream sourceFile, Stream
destinationFile)
{
X509Store myStore = new X509Store();
myStore.Open(OpenFlags.ReadOnly);
X509Certificate2 cert = (X509Certificate2)myStore.Certificates[0];
System.Security.Cryptography.AsymmetricAlgorithm alg =
cert.PrivateKey;
System.Security.Cryptography.RSACryptoServiceProvider rsa
= (System.Security.Cryptography.RSACryptoServiceProvider)alg;
org.bouncycastle.x509.X509Certificate bcCert = new
org.bouncycastle.x509.X509Certificate( cert.GetRawCertData() );
PdfReader reader = new PdfReader(sourceFile);
PdfStamper stamper = PdfStamper.CreateSignature(reader,
destinationFile, '\0');
PdfSignatureAppearance sap = stamper.SignatureAppearance;
sap.SetCrypto(null, new
org.bouncycastle.x509.X509Certificate[]{bcCert}, null,
PdfSignatureAppearance.WINCER_SIGNED);
sap.Reason = "Wincert demo";
sap.Location = "LBC";
sap.SetVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
sap.SetExternalDigest(new byte[128], new byte[20], "RSA");
sap.PreClose();
System.Security.Cryptography.SHA1 messageDigest =
System.Security.Cryptography.SHA1.Create();
byte[] hash = messageDigest.ComputeHash(sap.RangeStream);
PdfSigGenericPKCS sg = sap.SigStandard;
PdfLiteral slit = (PdfLiteral)sg.Get(PdfName.CONTENTS);
PdfPKCS7 sig = sg.Signer;
byte[] signedHash = rsa.SignHash(hash, "SHA1");
sig.SetExternalDigest(signedHash, hash, "RSA");
PdfDictionary dic = new PdfDictionary();
dic.Put(PdfName.CONTENTS, new
PdfString(sig.GetEncodedPKCS1()).SetHexWriting(true));
sap.Close(dic);
}
goodsig.pdf
Description: Adobe PDF document
badsig.pdf
Description: Adobe PDF document
------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://itext.ugent.be/itext-in-action/
