External signature on a smart card
NickName:GLC Ask DateTime:2017-05-15T16:13:28

External signature on a smart card

I am using a smart card that is signing a SHA-1 hash of a document, and compute a 256 bytes digital signature.

I am using the code posted on this question - iText signing PDF using external signature with smart card.

My problem is that I get the error:" The document has been altered or corrupted since the signature was applied".

I am using a GUI to create the hash and then send the signed 256 bytes that is computed on the card to the signing functions .

Here is my code:

hash creating code of filepath pdf document:

SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); 
SHA256 sha2 = SHA256.Create(); 
//sha2.ComputeHash 
byte[] pdfBytes = System.IO.File.ReadAllBytes(filePath); 
byte[] hash = null; 
hash=  sha1.ComputeHash(pdfBytes); 

the above code is used in one of the GUI functions to create the hash of the document

namespace EIDSmartCardSign
{
    class PdfSignature
    {
        private string outputPdfPath;
        private string certPath;
        byte[] messageDigest;

        private string inputPdfPath;
        public PdfSignature(byte[] messageDigest, string inputPdfPath,string outputPdfPath)
        {
            this.messageDigest = messageDigest;
            this.outputPdfPath = outputPdfPath;
            this.inputPdfPath = inputPdfPath;
        }

        public void setCertPath(string certPath)
        {
            this.certPath = certPath;
        }

        public void signPdf()
        {
            X509Certificate2 cert = new X509Certificate2();

            cert.Import(certPath); // .cer file certificate obtained from smart card

            X509CertificateParser certParse = new Org.BouncyCastle.X509.X509CertificateParser();
                       Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[1] ;
            chain[0] = certParse.ReadCertificate(cert.RawData);

            X509Certificate2[] certs;

            PdfReader reader  = new PdfReader(inputPdfPath);
            FileStream fout = new FileStream(outputPdfPath,FileMode.Create);
            PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0',null,true);

            PdfSignatureAppearance appearance = stamper.SignatureAppearance;
            appearance.SignatureCreator = "Me";
            appearance.Reason = "Testing iText";
            appearance.Location = "On my Laptop";
            iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(50, 50, 250, 100);
            appearance.SetVisibleSignature(rec, 1, "Signature");
            IExternalSignature extSignature= new MyExternalSignature("SHA-1",this.messageDigest);


            MakeSignature.SignDetached(appearance, extSignature, chain, null, null, null, 0, CryptoStandard.CMS);
            //MakeSignature.
        }
    }
}

Copyright Notice:Content Author:「GLC」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43974413/external-signature-on-a-smart-card

More about “External signature on a smart card” related questions

External signature on a smart card

I am using a smart card that is signing a SHA-1 hash of a document, and compute a 256 bytes digital signature. I am using the code posted on this question - iText signing PDF using external signa...

Show Detail

iText signing PDF using external signature with smart card

I've been toying with iTextSharp 5.5.7 for a while and can't find the right way to make a valid digital signature for PDF from Smart Card - Adobe Reader always says its signed by and unknown and ca...

Show Detail

Smart card selection for digital signature

I am mantaining a VB6 Windows application which digitally signs PDF documents by launching a JS file, located in the Javascripts subfolder of Acrobat 9.0. Now my Customer wants to plug another smar...

Show Detail

Simulate smart card for digital signature

I'm working on a project and I need digital signature, in order to confirm identity (read smart cards, a3). But, the point is I have no card or card reader at the moment. Does anyone know a way I c...

Show Detail

Load a smart card or other private certificate in CryptoServiceProvider for signature

I'm trying to develop a simple C# signature API to be used by a web intranet procedure. This signature API must allow a subject connected to the web application to use his certificate (on a smart c...

Show Detail

Tachograph smart card .ddd file signature

I have made android app that reads truck driver card (from tachograph) via otg smart card reader. I can read all files on card from tree structure on image bellow. But when I compare data that I r...

Show Detail

PDF Signing With Smart Card

I have to digitally sign PDF from a smart card . The problem is that smart card is at the client and Files are on the server . I have searched a lot but found nothing helpful . Now I want to do it...

Show Detail

Java applet to implement Digital signature using smart card

How to sign a post(any document or text) in browser using smart card. What I have investigated so far: ActiveX - IE only Silverlight - no access to certificates at all and as a plugin faces the s...

Show Detail

add an applet to a smart card which does not support Java Card technology

This is the first time I work on a smart card and I am not able to understand perfectly how to use it... My situation : I have an ACOS5-64 smart card which supports cryptographic functions like RSA

Show Detail

How to add an e-signature to a PDF File from a Smart Card?

I have a Java EE app that needs to implement e-signing of PDF Files. The user needs to be able to click a link that will generate a PDF File based on data in the database and see the PDF in their b...

Show Detail