Communication-Systems-Group / pdfsign.js

95 stars 26 forks source link

Alive? #3

Open WindowsNT opened 7 years ago

WindowsNT commented 7 years ago

I 'm struggling to sign a PDF in javascript and I found this. However, it always outputs an invalid PDF. Either unreadable (header incorrect), or unsigned with invalid signature entry as read by adobe reader.

I am attaching the two pdf files I 'm trying. The 00 one is generated by TCPDF.

0.pdf 00.pdf

WindowsNT commented 7 years ago

After some hassling I managed to do 2 fixes:

  1. To fix the forge-calling sign() which is not valid and,
  2. In newSig() I changed

    var offsetForm = find(pdf.stream.bytes, '<<', root.offset) + 2;

removing the 3rd parameter... it can now sign some pdf files, but not the one from TCPDF.

gsmachado commented 7 years ago

Hello @WindowsNT, Can you propose a pull request for the project? :-) Then we would test it and probably incorporate to the repo.

WindowsNT commented 7 years ago

0.pdf 1.pdf

I haven't fixed it yet; So far, I 've only managed to produce a valid PDF file, but the signature is still not valid in case of a TCPDF generation.

If I take another PDF (Generated e.g. by a printer driver), it signs OK.

Check it, 0.pdf signs OK, 1 not.

anfuca commented 7 years ago

Hi guys... @WindowsNT any advance?

-A PDF created directly from last version of Adobe Acrobat ---> Invalid PDF -The same PDF but after, opened it with Firefox -> Print -> Save as PDF ---> Valid PDF

WindowsNT commented 7 years ago

I have fixed my issue, but it is tcpdf pdf files I am generating, not any pdf.

shebik commented 7 years ago

Hi, I was able to sign pdf with your mycert-1.p12 certificate, but when I try to sign same pdf with my (czech) p12 cert or with my sample kazakh cert acrobat reader says that signature has errors in formatting or in signature data. When I sign same pdf with these certificates in Java using iText library no errors are displayed. Has PDFSign any limitations? Eg encoding of diacritics in key properties? Or do you have any clue what is causing this problem?

argapramudianto commented 7 years ago

Hi @shebik What does your (czech) p12 cert look likes?? In order to use this lib, your p12 cert should only contain your cert and priv key. If your p12 cert contains another cert (e.g. CA cert), you should repack your p12 cert which only contain your cert and priv key on it. Then it must be working now.

Regards,

rsouthpaw commented 7 years ago

Hi Everyone, @WindowsNT @gsmachado @shebik @anfuca @argapramudianto

I am able to sign pdf but I get invisible signature on the page. I am trying to bring that sign (watermark). Even example pdf of this repository have that watermark for signed pdfs. Do I have to change the code for this. Is this invisible by default? Can you please help me on this?

Thanks

jos1337 commented 7 years ago

Would like to use this. Have you guys been able to make it work?

grisenthwaite commented 7 years ago

I'm also interested in this. Any success in getting the signature to appear?

jos1337 commented 7 years ago

No progress on my side. I want to use this in a node.js Environment. Are there any alternatives?

tbocek commented 7 years ago

Unfortunately, at the moment, I don't have plenty of time to provide the fix. However, I'm more than welcome to receive and analyze a pull request.

jjbenitez026 commented 6 years ago

Hi @argapramudianto ! it is possible to extract my certificate from the PKCS12 file and create a new PKCS12 file (with the private key and only my certificate) to sign, from javascript.

tbocek commented 6 years ago

In theory it is, I'm using forge, which seems to have this feature: https://github.com/digitalbazaar/forge#pkcs12

jjbenitez026 commented 6 years ago

@tbocek: I add this inside the method sign in pdfsign.js:

function sign(data, rawpdf, password, date) {
...
var p7 = forge.pkcs7.createSignedData();
        p7.content = forge.util.createBuffer(rawpdf);
        var last = bags[certBag][0];
        var first = pki.certificateToPem(bags[certBag][0].cert);
        p7.addCertificate(bags[certBag][0].cert);

        // get key bags
        var bags = p12.getBags({bagType: keyBag});
        // get key
        var bag = bags[keyBag][0];
        var key = bag.key;

        // Generate new P12
        var newP12asn1 = forge.pkcs12.toPkcs12Asn1(
            key, first, 'vTemBJHYMd');
        var newp12 = forge.pkcs12.pkcs12FromAsn1(newP12asn1, false, 'vTemBJHYMd');
...
}

I try use the newP12 file to sign but does not work. After i download it and i try to open the newP12 file and is corrupt. Do you have any idea about? Thanks in advance.