Aspose.Pdf getting exception while executing

We are using Aspose.PDF v. 19.9.0
We are getting exception while executing

Aspose.Pdf.Facades.PdfFileSignature.Save()

#=zScqqsz$X9TnI4nJ8UHfuKMBLFAjvHJulSVJ4lO8=
  HResult=0x80131500
  Message=Timestamp validation failed, see inner exception for details
  Source=Aspose.Pdf
  StackTrace:
   at #=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=z6usfUtc=(Byte[] #=zDbQvZ20=, #=zp65$JdKj7p5g2OK7W8C5qXyd8poSEdpl5x4BAVM= #=zuZ1aO18=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv, Boolean #=zNqFOBCY=, X509Certificate2 #=zI$RjxMrJjQrU)
   at #=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=zhbuFI48=(Byte[] #=zDbQvZ20=, #=zp65$JdKj7p5g2OK7W8C5qXyd8poSEdpl5x4BAVM= #=zuZ1aO18=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv, Boolean #=zNqFOBCY=)
   at #=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=zhbuFI48=(Byte[] #=zDbQvZ20=, Stream #=zDwgrxZBnr0XC, String #=z2bQ7pz0=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv, Boolean #=zNqFOBCY=)
   at #=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=zhbuFI48=(Byte[] #=zDbQvZ20=, Stream #=zDwgrxZBnr0XC, String #=z2bQ7pz0=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv)
   at #=zEk2IRZuW1zZ_U$4HC3jNrUvwG1Gz.#=zhbuFI48=(String #=zzkLBjVY=, #=z5uPK7AABS5WOK5e5SXO9DIIj4CwuvnPpYg== #=zFCA7FbA=, #=zvHAkhUtk1XfW4H89bQDiDTLM7ISHHzw1KA== #=zNIY$WC0=, Stream #=zuZ1aO18=, String #=zo8ZCmK0=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv, X509Certificate2 #=zI$RjxMrJjQrU)
   at #=zEk2IRZuW1zZ_U$4HC3jNrUvwG1Gz.#=zhbuFI48=(String #=zzkLBjVY=, #=z5uPK7AABS5WOK5e5SXO9DIIj4CwuvnPpYg== #=zFCA7FbA=, #=zvHAkhUtk1XfW4H89bQDiDTLM7ISHHzw1KA== #=zNIY$WC0=, Stream #=zuZ1aO18=, String #=zo8ZCmK0=)
   at Aspose.Pdf.Forms.Signature.#=zhbuFI48=(String #=zzkLBjVY=, Stream #=zuZ1aO18=, String #=zo8ZCmK0=)
   at Aspose.Pdf.Forms.SignatureField.Sign(Signature signature, Stream pfx, String pass)
   at Aspose.Pdf.Forms.SignatureField.Sign(Signature signature)
   at Aspose.Pdf.Facades.PdfFileSignature.#=zJUktKo5llvDT(Stream #=zyXPGQKRAxLv_, Stream #=zKKmv7q1iSK9V, String #=z2bQ7pz0=)
   at Aspose.Pdf.Facades.PdfFileSignature.Save(Stream outputStream)
   at EY.TAX.eTFV.Common.PDFHandler.DoDigitalSign(Stream templatefile, IDictionary`2 signvalues, String webRootPath, String signDate, Boolean isAffidavit) in C:\App\Repos\eTFV\EY.TAX.eTFV.Common\PDFHandler.cs:line 160

  This exception was originally thrown at this call stack:
#=zpUYnJjujKKxzZuY$rMGvCA4ZNoGJeVxC7Tz3$0U=.#=zpUYnJjujKKxzZuY$rMGvCA4ZNoGJeVxC7Tz3$0U=(#=z2NGdkvUKxDHC7r2A6C_cJ1p_ln$glMUsUw==)
#=zCK7U1xQ9VgyRlhrpC$AUXFmt2ALWty8Lyg2x2B0bOvc7.#=zCK7U1xQ9VgyRlhrpC$AUXFmt2ALWty8Lyg2x2B0bOvc7(#=zP9LIG9zYynQPZ81n8wG3CKyRFYGmrMd0jlMi_7_LZ6Bv2k6qWg==)
#=zCK7U1xQ9VgyRlhrpC$AUXFmt2ALWty8Lyg2x2B0bOvc7.#=zCK7U1xQ9VgyRlhrpC$AUXFmt2ALWty8Lyg2x2B0bOvc7(System.IO.Stream)
   #=z7rqwLjr4lM_pmPi2qsPGOi1YZ$u3u8K7urJmgyg=.#=zF4phUBc=(byte[])
#=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=z6usfUtc=(byte[], #=zp65$JdKj7p5g2OK7W8C5qXyd8poSEdpl5x4BAVM=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD, Aspose.Pdf.TimestampSettings, bool, System.Security.Cryptography.X509Certificates.X509Certificate2)

Inner Exception 1:

#=zQ0kY3vR848_sgV8Ggw1MeGd6N24Vv6B4SpNqTmI=: Index was outside the bounds of the array.

aspose connect to http://timestamp.digicert.com

and the exception message is saying something about the timestamp

@sghavare1272,
Can you please share the working sample code and any source file reproducing the issue.

Here is the code snippet.

Below function gets called to sign the pdf. I have removed the pfx file name and password from the code.

public static MemoryStream DoDigitalSign(Stream templatefile, IDictionary<string, string> signvalues, string webRootPath,string signDate=null, bool isAffidavit = false)
        {
            DateTime dt_signDate = (signDate == null) ? DateTime.Now : DateTime.ParseExact(signDate, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
            string pfxFile = webRootPath + @"/pfx/XXXX.pfx";
            PKCS7 signature = new PKCS7(pfxFile, "");
            //System.Drawing.Bitmap bmp = CreateBitmapImage_Try6("Electronically Signed By " + signvalues["FirstName"] + " " + signvalues["Surname"], DateTime.Now.ToString("dd MMMM yyyy, HH:MM:ss"));
            System.Drawing.Bitmap bmp = CreateBitmapImage_Try6("Electronically Signed By " + signvalues["FirstName"] + " " + signvalues["Surname"], dt_signDate.ToString("dd MMMM yyyy, HH:mm:ss",CultureInfo.InvariantCulture));

            MemoryStream msIn = new MemoryStream();
            bmp.Save(msIn, System.Drawing.Imaging.ImageFormat.Png);
            PdfFileSignature pdfSign = new PdfFileSignature();
            pdfSign.BindPdf(templatefile);
            System.Drawing.Rectangle rect;
            if (isAffidavit)
            {
                rect = new System.Drawing.Rectangle(Convert.ToInt32(signvalues["AffidavitSignXAxis"]), Convert.ToInt32(signvalues["AffidavitSignYAxis"]), bmp.Width, bmp.Height - 5);
            }
            else
            {
                rect = new System.Drawing.Rectangle(Convert.ToInt32(signvalues["SignXAxis"]), Convert.ToInt32(signvalues["SignYAxis"]), bmp.Width, bmp.Height - 5);
            }
            signature.ShowProperties = false;
            TimestampSettings timestampSettings = new TimestampSettings("http://timestamp.digicert.com", null);
            signature.TimestampSettings = timestampSettings;
            pdfSign.SignatureAppearanceStream = msIn;
            MemoryStream stream = new MemoryStream();
            if (isAffidavit)
            {
                pdfSign.Sign(Convert.ToInt32(signvalues["SignPageNumber"]) + 1, null, null, null, true, rect, signature);
            }
            else
            {
                pdfSign.Sign(Convert.ToInt32(signvalues["SignPageNumber"]), null, null, null, true, rect, signature);
            }
            pdfSign.Save(stream);

            stream.Seek(0, SeekOrigin.Begin);
            msIn.Close();
            return stream;
        }

isAffidavit flag is used internally.
When you call pdfSign.Save(stream) method it throws below error.

#=zScqqsz$X9TnI4nJ8UHfuKMBLFAjvHJulSVJ4lO8=: Timestamp validation failed, see inner exception for details\r\n ---> #=zQ0kY3vR848_sgV8Ggw1MeGd6N24Vv6B4SpNqTmI=: Index was outside the bounds of the array.\r\n   at #=zpUYnJjujKKxzZuY$rMGvCA4ZNoGJeVxC7Tz3$0U=..ctor(#=z2NGdkvUKxDHC7r2A6C_cJ1p_ln$glMUsUw== #=zCegtawY=)\r\n   at #=zCK7U1xQ9VgyRlhrpC$AUXFmt2ALWty8Lyg2x2B0bOvc7..ctor(#=zP9LIG9zYynQPZ81n8wG3CKyRFYGmrMd0jlMi_7_LZ6Bv2k6qWg== #=zC$Bz4O8=)\r\n   at #=zCK7U1xQ9VgyRlhrpC$AUXFmt2ALWty8Lyg2x2B0bOvc7..ctor(Stream #=zTtUYiRg=)\r\n   at #=z7rqwLjr4lM_pmPi2qsPGOi1YZ$u3u8K7urJmgyg=.#=zF4phUBc=(Byte[] #=z0SjZ4W0r4ddW)\r\n   at #=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=z6usfUtc=(Byte[] #=zDbQvZ20=, #=zp65$JdKj7p5g2OK7W8C5qXyd8poSEdpl5x4BAVM= #=zuZ1aO18=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv, Boolean #=zNqFOBCY=, X509Certificate2 #=zI$RjxMrJjQrU)\r\n   --- End of inner exception stack trace ---\r\n   at #=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=z6us
fUtc=(Byte[] #=zDbQvZ20=, #=zp65$JdKj7p5g2OK7W8C5qXyd8poSEdpl5x4BAVM= #=zuZ1aO18=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv, Boolean #=zNqFOBCY=, X509Certificate2 #=zI$RjxMrJjQrU)\r\n   at #=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=zhbuFI48=(Byte[] #=zDbQvZ20=, #=zp65$JdKj7p5g2OK7W8C5qXyd8poSEdpl5x4BAVM= #=zuZ1aO18=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv, Boolean #=zNqFOBCY=)\r\n   at #=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=zhbuFI48=(Byte[] #=zDbQvZ20=, Stream #=zDwgrxZBnr0XC, String #=z2bQ7pz0=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv, Boolean #=zNqFOBCY=)\r\n   at #=zhdwOcYagvydi1QrSzuMkfi6Uab22IEMRKg==.#=zkIMQLTs=.#=zhbuFI48=(Byte[] #=zDbQvZ20=, Stream #=zDwgrxZBnr0XC, String #=z2bQ7pz0=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv)\r\n   at #=zEk2IRZuW1zZ_U$4HC3jNrUvwG1Gz.#=zhbuFI48=(String #=zzkLBjVY=, #=z5uPK7AABS
5WOK5e5SXO9DIIj4CwuvnPpYg== #=zFCA7FbA=, #=zvHAkhUtk1XfW4H89bQDiDTLM7ISHHzw1KA== #=zNIY$WC0=, Stream #=zuZ1aO18=, String #=zo8ZCmK0=, #=zRNO80ikPHdBmYNGv1Ose24dvD6uD #=zVy4ufqUXqzFA, TimestampSettings #=zWojgFtK5Drcv, X509Certificate2 #=zI$RjxMrJjQrU)\r\n   at #=zEk2IRZuW1zZ_U$4HC3jNrUvwG1Gz.#=zhbuFI48=(String #=zzkLBjVY=, #=z5uPK7AABS5WOK5e5SXO9DIIj4CwuvnPpYg== #=zFCA7FbA=, #=zvHAkhUtk1XfW4H89bQDiDTLM7ISHHzw1KA== #=zNIY$WC0=, Stream #=zuZ1aO18=, String #=zo8ZCmK0=)\r\n   at Aspose.Pdf.Forms.Signature.#=zhbuFI48=(String #=zzkLBjVY=, Stream #=zuZ1aO18=, String #=zo8ZCmK0=)\r\n   at Aspose.Pdf.Forms.SignatureField.Sign(Signature signature, Stream pfx, String pass)\r\n   at Aspose.Pdf.Forms.SignatureField.Sign(Signature signature)\r\n   at Aspose.Pdf.Facades.PdfFileSignature.#=zJUktKo5llvDT(Stream #=zyXPGQKRAxLv_, Stream #=zKKmv7q1iSK9V, String #=z2bQ7pz0=)\r\n   at Aspose.Pdf.Facades.PdfFileSignature.Save(Stream outputStream)\r\n   at EY.TAX.eTFV.Common.PDFHandler.DoDigitalSign(Stream templatefile, IDictionary`2
 signvalues, String webRootPath, String signDate, Boolean isAffidavit) in C:\\Work\\EY\\Project\\TFV\\Repo\\FrontEnd\\EY.TAX.eTFV.Common\\PDFHandler.cs:line 160\r\n   at EY.TAX.eTFV.Common.PDFHandler.CreatePDF(IDictionary`2 fields, String formName, String signDate, Boolean isSign, IDictionary`2 signvalues, String webRootPath, Boolean showAffidavit, DateTime affidavitDate, IDictionary`2 formAffidavitfields) in C:\\Work\\EY\\Project\\TFV\\Repo\\FrontEnd\\EY.TAX.eTFV.Common\\PDFHandler.cs:line 91\r\n   at EY.TAX.TFV.Services.Concrete.CaseService.UploadCaseForm(Int64 caseId, String signDate, String clientName, String dbName, Boolean Issigned, String webRootPath) in C:\\Work\\EY\\Project\\TFV\\Repo\\FrontEnd\\EY.TAX.TFV.Services\\Concrete\\CaseService.cs:line 141\r\n   at EY.TAX.eTFV.WebApp.Controllers.CustomerController.PDFForm(Int64 caseid, String signDate, Boolean isUpload, Boolean Issigned)

@andrewdemmon,
Please share your template file and details of unassigned properties like signvalues and the method CreateBitmapImage_Try6. We will reproduce the issue with your runnable code. Moreover, you may please try using latest version on your end and let us know if the issue persists.

The issue has been resolved and its not with the ASPOSE but it is with
http://timestamp.digicert.com/

Till now we were using digicert url but now its stopped working without the certificate. After replacing it with "https://freetsa.org/tsr". It started working. Again freetsa.org its a free provider. Is there an other method to exclude timestamp url.

@andrewdemmon

It is good to know that the issue has been resolved. If you want to add timestamp then TimestampSettings Constructor necessarily needs the timestamp url. If you want to avoid timestamp then I suggest you to please visit How to digitally sign PDF documentation article.