The current procedure is following.
1. The document to be signed is sent to application hosting Spire.PDF library.
2. Spire.PDF should create digital signatures block to the PDF if it did not exist.
3. Spire.PDF should reserve space for the digital signature ~20kb per signature.
4. Then the pdf document bytes to be signed are returned to the client ( block1 and block2 ).
- block1
<reserved block for digital signature>
- block2
]
5. Client will connect to local SCS device which creates digital signature and sends signature bytes back to the application hosting Spire.PDF library
6. Spire.PDF modifies one of the reserved block with the bytes coming from the client.
I started implementing it following way, but the spire.pdf throws exception:
- Code: Select all
System.Exception
HResult=0x80131500
Message=string parameter Filter required.
Source=Spire.Pdf
StackTrace:
at spr颺.솶()
- Code: Select all
public void Sign(byte[] pdf_bytes)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromBytes(pdf_bytes);
// Create a new instance of the CustomPKCS7SignatureFormatter
CustomPKCS7SignatureFormatter customPKCS7SignatureFormatter = new CustomPKCS7SignatureFormatter();
// Create a new instance of the PdfSignature
PdfSignature signature = new PdfSignature(doc, doc.Pages[0], customPKCS7SignatureFormatter, "signature0");
doc.SaveToFile("test.pdf", FileFormat.PDF);
doc.Close();
}
class CustomPKCS7SignatureFormatter : IPdfSignatureFormatter
{
public Dictionary<string, object> Parameters => new(StringComparer.Ordinal);
///
/// If encapsulate is true, a copy of the message will be included in the signature.
///
private bool m_encapsulate = true;
///
/// Construct a new instance.
///
/// The signing certificate.
///
/// If encapsulate is true, a copy of the message will be included in the signature.
///
public CustomPKCS7SignatureFormatter()
{
Parameters.Add("Filter", "Adobe.PPKLite");
Parameters.Add("SubFilter", "adbe.pkcs7.detached");
}
///
/// Sign.
///
/// The data that contains signature content.
/// The signature
public byte[] Sign(byte[] content)
{
// Code never comes here?
return content;
}
}
}
This digital signature flow is different to traditional flow in a way that we do not have access to the client certificate private key, but the device uses the certificate internally to create the signature.