System.OverflowException: Arithmetic operation resulted in an overflow when signing PDF with timestamp

When I try sign a PDF using EnableLtv and TimeStampServer I got the error:

System.OverflowException: Arithmetic operation resulted in an overflow.

   at Syncfusion.Pdf.Security.PdfSignatureDictionary.DocumentSaved(Object sender, DocumentSavedEventArgs e)

   at Syncfusion.Pdf.PdfDocumentBase.OnDocumentSaved(DocumentSavedEventArgs args)

   at Syncfusion.Pdf.Parsing.PdfLoadedDocument.Save(Stream stream)


My code:

public void Sign(Stream pdfStream, Stream certificateStream, string password) {
PdfLoadedDocument document = new PdfLoadedDocument(pdfStream);
PdfCertificate certificate = new PdfCertificate(certificateStream, password);

PdfSignature signature = new PdfSignature(document, document.Pages[0], certificate, "DigitalSignature");
signature.TimeStampServer = new TimeStampServer(new Uri("http://timestamp.digicert.com"), "user", "password");
signature.EnableLtv = true;

using (FileStream fileStream = new FileStream("signed.pdf", FileMode.Create, FileAccess.ReadWrite)) {
    document.Save(fileStream);
}
document.Close(true);
}


What I'm doing wrong?


3 Replies 1 reply marked as answer

RB Ravikumar Baladhandapani Syncfusion Team August 7, 2023 03:42 PM UTC

We tried to replicate the reported behavior with our test document based on the provided details and we are unable to reproduce this issue and we suspect the reported issue occurs with a specific document.

Could you please share the complete code snippet and input pdf document and it will be helpful for our further investigation.



PG Promedico Gestao Hospitalar August 8, 2023 12:57 PM UTC

Hi, I attached a sample project with the issue.


Attachment: SyncFusionSample_12bc33c6.zip


SN Santhiya Narayanan Syncfusion Team August 10, 2023 12:46 PM UTC

The problem that might happen is when the expected size for the signature content limit exists together with the certificate and adding a time stamp. We ask you to make the expected signature size bigger to fix this issue.

Please make the changes we've highlighted below in your example to solve this problem. If the problem continues, please give us your certificate file and password details so we can look into the issue more deeply on our end..


    public void Sign(Stream pdfStream, Stream certificateStream, string password)

    {

        PdfLoadedDocument document = new PdfLoadedDocument(pdfStream);

        PdfCertificate certificate = new PdfCertificate(certificateStream, password);

 

        PdfSignature signature = new PdfSignature(document, document.Pages[0], certificate, "Signature");

        signature.Settings.CryptographicStandard = CryptographicStandard.CADES;

        signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA512;

        signature.EstimatedSignatureSize = 200000;

        signature.TimeStampServer = new TimeStampServer(new Uri(http://timestamp.digicert.com), "USERNAME", "PASSWORD");

        signature.EnableLtv = true;

 

        using (FileStream fileStream = new FileStream("signed.pdf", FileMode.Create, FileAccess.ReadWrite))

        {

            document.Save(fileStream);

        }

        //Close the document

        document.Close(true);

    }


Marked as answer
Loader.
Up arrow icon