I am getting "Document has been altered or corrupted since it was
signed" error when opening signed pdf in Reader using first method.
I created 2 PDF's. First using opensc-pkcs11.dll and second using MS API
ComputeSignature.
Method I
------------
1) calulate SHA1 hash from ss.ToArray() in function SmartCardSign - see
below
2) this SHA1 hash is enrypted on SmartCard using SHA1_RSA, output is
digest which is 128 bytes long
Method II
------------
For testing purposes I used Paulo's SignDetached example described here:
http://itextpdf.sourceforge.net/howtosign.html#signextitextsharp2There are 3 differences in both PDFs:
1) timestamp field /M(D:20090703162907+03'00')
2) the digest of 128 bytes.
3) trailer in the end of PDF: trailer
<</Info 40 0 R/Root 27 0 R/Size 41/ID
[<115a158e7e38fc64ce62f3efd16f19b4><6f0db7989f0096942ed2c06fa2bba064>]>>
First and second differences are Ok, aren't they ? What aboud trailer
???
Can someone please explain me what am I doing wrong...
public void SignDetached(string filename, string outfile)
{
X509Certificate2 card = GetCertificate();
if (card == null)
throw new ArgumentNullException("Missing certificate");
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');
if (metadata != null)
stp.XmpMetadata = metadata.getStreamedMetaData();
PdfSignatureAppearance sap = stp.SignatureAppearance;
if (appearance.Visible)
sap.SetVisibleSignature(new Rectangle(100, 100, 300, 200), 1,
null);
sap.SignDate = DateTime.Now;
sap.SetCrypto(null, chain, null, null);
sap.Reason = appearance.Reason;
sap.Location = appearance.Location;
sap.Contact = appearance.Contact;
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;
if (sap.Contact != null)
dic.Contact = sap.Contact;
sap.CryptoDictionary = dic;
int csize = 1536;
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);
}
bool success = SmartCardSign(ss.ToArray(), ref rsadata);
if (success == false)
throw new ArgumentNullException("Failed to sign using
SmartCard");
PdfPKCS7 pk7 = new PdfPKCS7(null, chain, null, "SHA1", true);
pk7.SetExternalDigest(rsadata, 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);
}
------------------------------------------------------------------------------
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions