Support for Digital Signatures #165
Replies: 15 comments 23 replies
-
Ping @earthlingworks |
Beta Was this translation helpful? Give feedback.
-
I have just force-pushed the latest changes to the devel branch which include support for signing a PDF document (multiple times) using a locally available certificate and key. The code should easily be adaptable to sign a PDF using an external service, e.g. using a government API to a sign a PDF using your citizen ID certificate. Feedback welcome! |
Beta Was this translation helpful? Give feedback.
-
Also, just noticed that I missed your ping from a couple of weeks somehow, sorry about that. I get a ton of Github messages so probably got buried in between some of those. |
Beta Was this translation helpful? Give feedback.
-
Hi, I can't wait to try this new feature. Could you give me a sample snippet of writing using a certificate? |
Beta Was this translation helpful? Give feedback.
-
@earthlingworks @marcomd Do you need the following use-case implemented where hashing the document and then signing it are done in separate steps?
|
Beta Was this translation helpful? Give feedback.
-
Thomas I miss which hash you are referring to. |
Beta Was this translation helpful? Give feedback.
-
I tried to use the snipped above but the output pdf does not contain any revision with the signature. I can send you a pdf example with a dummy certificate applied with origami to show you what i expected. As secondary issues:
|
Beta Was this translation helpful? Give feedback.
-
I created a script with which to quickly execute the snippet. The certificate_output.pdf file is already present. To run it:
|
Beta Was this translation helpful? Give feedback.
-
Then I have another question: how to assign the signature to a clickable and visible area on the document? # Example how i did with origami
text_annotation = Origami::Annotation::AppearanceStream.new
text_annotation.Type = Origami::Name.new("XObject")
signature_annotation = Origami::Annotation::Widget::Signature.new
signature_annotation.Rect = Origami::Rectangle[llx: x, lly: y+height, urx: x+width, ury: y]
signature_annotation.F = Origami::Annotation::Flags::PRINT
signature_annotation.set_normal_appearance(text_annotation) |
Beta Was this translation helpful? Give feedback.
-
Thank you Thomas, your work is always precious. doc.acro_form(create: true).signature_flag(:append_only) # optional The new code, the more complex one, inserts the box but acrobat reader indicates "Signature field without a signature" and then offers me to sign it. This is interesting and I will consider this possibility but I was wondering how to connect the box to the signature created by code. Consider the scenario where the document has already been signed and I'm just pointing where. I think here we should refer to the signature above sig_field = form.create_signature_field ('my signature') |
Beta Was this translation helpful? Give feedback.
-
Question 1. link the signature field to the signature created by codeNow it is more clear to me, thanks, but I don't understand how to link sig = doc.add({Type: :Sig}) # set an empty signature and apply it to the field Question 2. using a valid certificateThis is not a hexapdf issue but it's generic and related to self-signed certificate. pkcs12 = OpenSSL::PKCS12.new(File.read("your_identity.p12"), "your_password!")
private_key = OpenSSL::PKey::RSA.new(pkcs12.key.to_pem)
certificate = OpenSSL::X509::Certificate.new(pkcs12.certificate.to_pem) Using that certificate your signature will be valid |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
No, the section showing the details of the signatures is empty. In fact it is strange because the signature should be in the list even if "invisible". |
Beta Was this translation helpful? Give feedback.
-
@earthlingworks @marcomd I have pushed some more changes to the devel branch. Basic code now: doc = HexaPDF::Document.open(ARGV[0])
sig_field = doc.acro_form(create: true).create_signature_field('my signature')
widget = sig_field.create_widget(doc.pages[0], Rect: [20, 500, 120, 600])
widget.create_appearance.canvas.
stroke_color("red").rectangle(1, 1, 99, 99).stroke.
font("Helvetica", size: 10).
text("Certified by signer", at: [10, 10])
handler = doc.signatures.handler(reason: 'Reason', certificate: CERTIFICATES.signer_certificate,
key: CERTIFICATES.signer_key,
certificate_chain: [CERTIFICATES.ca_certificate])
doc.signatures.add('/tmp/signed.pdf', handler, signature: sig_field)
|
Beta Was this translation helpful? Give feedback.
-
@earthlingworks @marcomd I have just released version 0.20.0 with digital signature support. See |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have pushed the first part of the digital signature support to the devel branch.
Reading / Valdiating
The current implementation allows reading and validating digital signatures but not writing them; this will be done next.
The easiest way to test the implementation is by using the
hexapdf info
command as it has been enhanced to read all digital signatures and output general information about the signature as well as validation information.As for validating digital signatures: The current implementation only checks a few standard things (e.g. self-signed certificate?) and makes sure that the actual signature is valid. It does not provide a full validation stack as this will differ from use-case to use-case. One can implement the needed validation steps by simply sub-classing an existing signature algorithm handler.
It would help greatly if you could run the
hexapdf info
command on digitally signed PDFs to see whether HexaPDF's implementation is complete.Writing
As for writing digital signatures: Since PDF 2.0 deprecated some signature algorithms, HexaPDF will only support the most used algorithm for signing. This should not be a problem in practice.
I'm still unsure how much of the legwork will be implemented in HexaPDF itself and how much one has to do oneself. What I can say is that there will be at least a simple method for signing a document in a standard way where the user just has to provide the certificate+key and the needed information.
If HexaPDF should cover special use-cases, I really would like to hear about them.
Help Needed!
As already said I would greatly appreciate it if you could run the
hexapdf info
command on digitally signed PDFs and provide input regarding the read/write support - thank you! 🙏Beta Was this translation helpful? Give feedback.
All reactions