Ejemplo n.º 1
0
        public static Org.BouncyCastle.Asn1.Asn1EncodableVector GetTimestamp(byte[] signature, string url, string user, string pass)
        {
            byte[] tsImprint = PdfEncryption.DigestComputeHash("SHA256", signature);

            int        size = 6500;
            ITSAClient tsc  = new TSAClientBouncyCastle(url, user, pass, size, "SHA256");
            String     ID_TIME_STAMP_TOKEN = "1.2.840.113549.1.9.16.2.14";         // RFC 3161 id-aa-timeStampToken

            Asn1InputStream tempstream = new Asn1InputStream(new MemoryStream(tsc.GetTimeStampToken(tsImprint)));

            Asn1EncodableVector unauthAttributes = new Asn1EncodableVector();

            Asn1EncodableVector v = new Asn1EncodableVector();

            v.Add(new DerObjectIdentifier(ID_TIME_STAMP_TOKEN));             // id-aa-timeStampToken
            Asn1Sequence seq = (Asn1Sequence)tempstream.ReadObject();

            v.Add(new DerSet(seq));

            unauthAttributes.Add(new DerSequence(v));
            return(unauthAttributes);
        }
Ejemplo n.º 2
0
        public byte[] signPdf(byte[] inputPdf, byte[] sigImg, string signatureField)
        {
            this.getCRLList();
            Console.WriteLine("Read PDF");
            PdfReader    reader = new PdfReader(inputPdf);
            MemoryStream output = new MemoryStream();

            PdfStamper stamper = PdfStamper.CreateSignature(reader, output, '\0', null, true);

            PdfSignatureAppearance sap = stamper.SignatureAppearance;

            sap.Reason   = "test";
            sap.Location = "Bangkok";
            // Set Signature Image
            if (sigImg != null)
            {
                sap.SignatureGraphic       = Image.GetInstance(sigImg);
                sap.ImageScale             = -1;
                sap.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC;
            }
            // Set Signature Field
            if (signatureField.Equals("") || signatureField == null)
            {
                Rectangle location = new Rectangle(10, 10, 300, 100);
                sap.SetVisibleSignature(location, 1, "signatureField");
            }
            else
            {
                sap.SetVisibleSignature(signatureField);
            }

            sap.CertificationLevel = PdfSignatureAppearance.NOT_CERTIFIED;

            //Create TSA server
            ITSAClient tsaClient      = null;
            Boolean    isTsaConnected = false;

            if (tsa)
            {
                tsaClient = new TSAClientBouncyCastle(tsaUrl, tsaUsername, tsaPassword);
                for (int retry = 0; retry < 5; retry++)
                {
                    try
                    {
                        //int hash = tsaClient.GetHashCode();
                        string testString = "test";
                        byte[] digest;
                        using (SHA256Managed sha256 = new SHA256Managed())
                        {
                            digest = sha256.ComputeHash(Encoding.UTF8.GetBytes(testString));
                        }
                        tsaClient.GetTimeStampToken(digest);
                        isTsaConnected = true;
                        break;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.StackTrace);
                    }
                    Console.WriteLine("retry " + (retry + 1));
                }
            }
            //Do Signing Check not null timestamp and crl
            if (tsaClient != null && crlList != null && isTsaConnected)
            {
                try
                {
                    MakeSignature.SignDetached(sap, this.signature, chain, this.crlList, null, tsaClient, 0, CryptoStandard.CADES);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
            }
            else
            {
                Console.WriteLine("Cannot sign the PDF file.");
                return(null);
            }
            reader.Close();
            stamper.Close();
            signature = null;


            return(output.ToArray());
        }