向 PDF 添加签名到 PDF
原标题:Add Signature to PDF
I know there are already a lot of different threads about this topic, but I really couldn t find what I am looking for.
There is a service in my country which provides a digital signature for PDFs using a phone number and a password (you have to be registered for this service). Anyways, there is a WebService - which is very poorly documented - and I was able to use this service. After sending the request using my phone number and password, I receive the response for the digitital signature.
But the problem is that I don t receive the signed PDF, I just receive the original PDF, a X.509 certificate and a digest etc.
My question is, how can I add the digest and certificate to the PDF, so that the signature is recognized by common PDF readers?
Here is a short description of how the digital signature service is working:
I convert the PDF I want to sign to base64
I authenticate using my phone number and my password
the service returns the unsigned PDF and also a X.509 certificate and digest
I need to sign the PDF myself
I m stuck at step 4.
Here is the XMLResposne of an example request using a test PDF (the digest and certificate are cut out):
id( signed-data-1-1 )/node()
[some digest]
[some digest]
[some base64 value]
[some base64 value]
[the original PDF as base64, not signed]
2020-06-04T14:00:22Z
[some digest]
[issuer name]
[serial no]
application/pdf
问题回答
In order to make my answer less like spamming(:-)), I will tell a little about what happens when signing a PDF, you can decide to do that or your own or use my code.
A PDF signature is always contained in a signature field in the PDF’s form structure.
The signature object itself contains some PDF-specific metadata, such as
the byte range of the file that it covers;
the hash function used to compute the document hash to be signed;
a modification policy that indicates the ways in which the file can still be modified.
The actual cryptographic signature is embedded as a CMS object. General CMS objects are defined in RFC 5652, but only a limited subset is meaningful in PDF. When creating a signature, the signer is authenticated using the private key associated with an X.509 certificate, as issued by most common PKI authorities nowadays.(In your case, you already have it.)
To sign it mannually may need some professional knowledge that I am not familiar with. You can have a look at ISO 32000 standard(s).
And as you may know, certifications can expire, if you want to valid your documents after expiration, you should supply two additional pieces of data:
a trusted timestamp signed by a time stamping authority (TSA), to prove the time of signing to the validator;
revocation information (relevant CRLs or OCSP responses) for all certificates in the chain of trust of the signer’s certificate, and of the TSA.
About how to do so, maybe that s beyond my ability.
I build one such tool to sign a PDF using specific X.509 certification. By the way, you don t have to apply for an X.509 certification every time. You can generate it yourself.
Here is the step:
install openssl,python3,pip
run the command: pip install pyhanko[opentype] in console.
download the code from https://github.com/Micraow/pdf_signer or just save the stamp.py to your computer. There are some fields near the beginning of the file. Change them.
run python stamp.py
I am sorry for linking my blog here, but I was just trying to add some background information about x.509 and PDF that may be helpful for you, and demonstrate its result.
If you want to use your own certification, specify it in the pfx variable. Please pay attention to Escape Characters if you use Windows.( should be written as ).
What s more, if you are required to use a specified timestamp server, you can also replace the https://freetsa.org/tsr in the code.
Also, this "stamp" is an extra field in the PDF, meaning that it can also be removed.
相关问题
how to represent it in dtd?
I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file.
How to represent this in dtd ?
.Net application configuration add xml-data
I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection?
Is it possible to add the xml ...
XStream serializing collections
I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...
MS Word splits words in its XML format
I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content.
But, the problem is that Word ...
Using CDATA element in XML is vulnerable or not?
Is it a vulnerable using CDATA element in XML documents? If so what happens if we use CDATA element in XML documents?
Merging an XML file with a list of changes
I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first.
Main ...
How do I check if a node has no siblings?
I have a org.w3c.dom.Node object.
I would like to see if it has any other siblings.
Here s what I have tried:
Node sibling = node.getNextSibling();
if(sibling == null)
return true;
else
...
Ordering a hash to xml: Rails
I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished?
hash.to_xml