Commit Graph

55 Commits

Author SHA1 Message Date
Miklos Vajna
972c1e9e72 xmlsecurity PDF sign: handle AdES when writing SubFilter
Page 21 of "PAdES baseline signatures" specification from
<http://www.etsi.org/deliver/etsi_en/319100_319199/31914201/01.01.01_60/en_31914201v010101p.pdf>
says:

"The Signature Dictionary shall contain a value of ETSI.CAdES.detached
for the key SubFilter."

So in case the UI has the adescompliant checkbox enabled, write that
value instead of the Adobe default.

Change-Id: I69e606a32fb09bebd5e9b25b32150d1b8672f544
2016-11-17 15:54:06 +01:00
Miklos Vajna
49f65d5114 xmlsecurity: extract parts of PDFDocument::Sign() into separate functions
Hopefully it's easier to read this way.

Change-Id: I145e00f8e57e20f2663d1c9ee494af5d93c014c7
2016-11-11 12:10:10 +01:00
Miklos Vajna
3b3fb8cfc8 xmlsecurity PDF sign: support non-compressed AcroForm objects
This was the last problem to be able to counter-sign Acrobat-created PDF
1.6 signatures unlimited number of times.

Change-Id: I24ab80c8516b6fe9c08d57c08907bec70384dc28
Reviewed-on: https://gerrit.libreoffice.org/30757
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-10 16:47:25 +00:00
Miklos Vajna
c21329fa90 xmlsecurity PDF verify: fix incremental updates vs object streams
The problem: an object stream provies obj#1 and obj#2, then an
incremental updates provides an updated obj#1'. Then we look up obj#2,
parse the stored objects on-demand, so at the end when later we look up
the first object, we find obj#1, not obj#1'.

An easy workaround would be to never update already existing objects
from object streams, but that would break when an incremental update
provides an object stream.

Fix the problem by parsing stored objects right after tokenizing the
object stream, and not later, on-demand, when we no longer have the
context what objects should be ignored.

This is needed (but not enough) to correctly append a signature at the
end of a PDF file that has both object streams and incremental updates.

Change-Id: I3c1fae5ac26804c8e8cc1984511f43cfa881c97b
2016-11-10 17:46:27 +01:00
Miklos Vajna
bec9e673ad xmlsecurity PDF verify: fix reading multiple subsections from an xref stream
This is especially needed, as we don't bother compressing updated
objects into sections on signing, we simply use a separate section for
each updated object.

Work towards supporting xref streams and incremental updates at the same
time.

Change-Id: Ie9759edbba816991615fafc6602cdd440141b989
2016-11-10 15:09:27 +01:00
Miklos Vajna
9357e99450 xmlsecurity PDF sign: use a predictor when compressing the xref stream
With this our xref stream output is close enough to Acrobat so that the
existing signature verifier runs without any problems.

Change-Id: I6eca7966890365759c269b465e4bf4d86d335219
2016-11-10 10:29:14 +01:00
Miklos Vajna
bf64b4f145 xmlsecurity PDF sign: compress the xref stream
This way it's a bit smaller for large files and our output is closer to
what Acrobat produces.

Change-Id: Ide5f7b58a74a9d6ad7d806814eb57cb6931023cc
Reviewed-on: https://gerrit.libreoffice.org/30726
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-09 19:22:18 +00:00
Miklos Vajna
f68b1541ea xmlsecurity PDF sign: improve suggested signature ID
So that when we have a single signature with ID="Signature2", then we
use "Signature3" for the next ID, not "Signature2". (Acrobat uses that ID
for the first signature.)

Change-Id: I7032fbf014184da2a5be24730a92abc32a9a1258
Reviewed-on: https://gerrit.libreoffice.org/30725
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-09 19:21:59 +00:00
Miklos Vajna
2a7e39eac2 xmlsecurity PDF sign: conditionally write xref stream
In case the input document used a PDF 1.5 xref stream, not an old xref
table, then write that as part of the incremental update. Acrobat seems
to require this.

Change-Id: I9f1f73140c26308f8720aa1ffe1b905d0e60ede0
Reviewed-on: https://gerrit.libreoffice.org/30724
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-09 19:21:46 +00:00
Miklos Vajna
05ad6dfd4e xmlsecurity PDF sign: handle when Catalog's AcroForm is an indirect dictionary
Normally it's a direct dictionary, but it's OK to have it as a reference, and
then the referenced object is a dictionary.

Change-Id: If09edaf23501883be68148e430c42e721ec68247
Reviewed-on: https://gerrit.libreoffice.org/30719
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-09 19:21:38 +00:00
Miklos Vajna
05bdc6739c xmlsecurity PDF sign: handle when Page object's Annots is an indirect array
Normally it's a direct array, but it's OK to have it as a reference, and
then the referenced object is an array.

Change-Id: I191150632c2d8317ee6fd8c8169a90996298faa4
Reviewed-on: https://gerrit.libreoffice.org/30718
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-09 16:07:09 +00:00
Miklos Vajna
5d34de5f27 xmlsecurity PDF verify: fix handling of xref stream free objects
In case our xref table doesn't have an entry for "free" object types,
then the table size won't provide a valid id for a next object. That
resulted in creating all new objects with the same ID.

With this, our verifier at least can see the new signature when
appending one to a signed PDF 1.6 file.

Change-Id: Iac39a400706cfcd23dd814d2b81cb8b950c69fc6
Reviewed-on: https://gerrit.libreoffice.org/30704
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-08 16:47:58 +00:00
Miklos Vajna
3f213154e2 xmlsecurity PDF sign: handle xref stream when reading trailer
Don't give up signing just because PDF 1.4 trailer is missing, provided
that PDF 1.5 xref stream is available.

Change-Id: I03360d428346537583a4398aa3a94b195b428713
Reviewed-on: https://gerrit.libreoffice.org/30703
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-08 16:47:32 +00:00
Miklos Vajna
62401ba3c9 xmlsecurity PDF sign: don't crash on missing trailer
This will need cross-reference stream write support, just don't crash
for now.

Change-Id: Id48c131b22d4ed96174693f3e96b14c273d596a8
Reviewed-on: https://gerrit.libreoffice.org/30702
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-08 16:46:51 +00:00
Andrea Gelmini
b9b03c7383 Fix typos (also in the code)
Change-Id: I45d45513b102f4fdcb55e8de20b95b37f66ea463
Reviewed-on: https://gerrit.libreoffice.org/30658
Reviewed-by: Michael Stahl <mstahl@redhat.com>
Tested-by: Michael Stahl <mstahl@redhat.com>
2016-11-07 15:44:24 +00:00
Miklos Vajna
d0edff60c7 xmlsecurity PDF NSS verify: handle SHA1_WITH_RSA
SHA1_WITH_RSA is a signing algorithm, not a digest one, but let's
accept it, so LO on Linux can verify a signature generated by LO on
Windows.

It's annoying that equivalent mapping in NSS is not part of their public
API.

Change-Id: I97186fcc1d118f922e5ee3cb472aa5b52bc4b5ca
2016-11-04 12:32:40 +01:00
Miklos Vajna
3e610f8496 vcl: extract PDFWriter::GetDateTime() from PDFWriterImpl
And use it in xmlsecurity when signing an existing PDF. This is
especially important on Windows, where the PKCS#7 blob doesn't have an
(unsigned) timestamp.

Change-Id: I4051dc19a43f8f8114d9f4d02309f28d6754e9ae
2016-11-04 09:13:32 +01:00
Miklos Vajna
a8aab44d75 xmlsecurity PDF sign: enable unit tests on Windows
Now that the mscrypto part of PDFDocument::ValidateSignature() is
implemented it's possible to run these tests on Windows as well,
provided the machine has at least one signing certificate installed.

Also fix a race, where the workdir of the signing test was used by the
pdfsigning test.

Change-Id: I80bbfbb5dc4baa400f9a6b85961883a247b0f22b
2016-11-03 13:55:01 +01:00
Miklos Vajna
8d1fb35f5c xmlsecurity PDF verify: initial Windows support
The timestamp isn't extracted yet, but the digest match is already
checked correctly and the certificate is exposed.

Change-Id: Ieca002a5c4ca0b96f4dc397c460adb7f88f5ffc7
Reviewed-on: https://gerrit.libreoffice.org/30499
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-11-03 08:09:39 +00:00
Miklos Vajna
b0d1a39e99 xmlsecurity PDF verify: add support for object streams
Adobe Acrobat uses object streams (PDF 1.6) when it signs a PDF exported
from LO (PDF 1.4), with this we can verify that signature.

If the PDF had at least one signature in LO, then the doc is not
upgraded from PDF 1.4, so that was working already.

Change-Id: I54b4447ca965a8ba1ffc69bde228ab6f0bda59ee
2016-11-02 15:01:02 +01:00
Jochen Nitschke
8826771e83 style fixes for cppcheck noExplicitConstructor
Change-Id: I2a690caea7656f2a18beb6d09f53154178a30f34
Reviewed-on: https://gerrit.libreoffice.org/30460
Reviewed-by: Jochen Nitschke <j.nitschke+logerrit@ok.de>
Tested-by: Jochen Nitschke <j.nitschke+logerrit@ok.de>
2016-11-01 18:44:20 +00:00
Caolán McNamara
b696781f03 crashtesting: piles of pdf import crashes
e.g. ooo112863-2.pdf

Change-Id: I359f7e50f416f0a756e16099cdf47be3b1c32d90
2016-10-29 15:36:59 +01:00
Miklos Vajna
5c33253daf xmlsecurity PDF verify: start using offsets from xref streams
This is needed (but not enough) to verify PDF 1.5 signatures. What's
missing next is support for object streams.

Change-Id: I5afec0a77839ffabe0aaa07e367064210535a1a9
2016-10-28 18:34:14 +02:00
Miklos Vajna
56cc352d82 xmlsecurity PDF verify: initial support of cross-reference streams
This adds support for cross-reference streams (which can be used instead
of plain-text cross-reference tables) + also one stream predictor.

The actual parsed data is still not used, though.

Change-Id: Ia806abd8a97636a1bd25dfdafea377b088800f00
2016-10-28 18:20:20 +02:00
Miklos Vajna
4beac67a2b xmlsecurity PDF verify: handle missing trailer
Support for Cross-Reference Streams is still missing, but this avoids
a crash at least.

Change-Id: I8c5d16531f8603feeebe8feddb7c3bdfa61943cb
2016-10-27 20:21:02 +02:00
Miklos Vajna
9df81ea775 xmlsecurity PDF verify: import out-of-signature date
The signature date can be placed as the value of the "M" key, and also
inside the signed PKCS#7 binary. When the later is missing show what's
described in the previous.

Change-Id: Idb40d91adb70486bc1f19d4755a3f8e17d35e9e9
2016-10-27 17:01:09 +02:00
Miklos Vajna
3e12ef2cbb xmlsecurity PDF verify: support array ref annotations
Each pdf signature is mentioned in the Annots key of a page object.
Usually the key contains an array value. But it's valid for the key to
contain a reference to an object, where the object contains the actual
array, so support this case as well.

Also:

- stop parsing name tokens on the first seen '(' character (usually
  there is a whitespace between the two, but that's not required)
- handle \0 characters in the last 1024 bytes of the document by using
  std::search() instead of strstr().

Change-Id: I3a167b23340230f99f1ae4112473ed10e1c96b09
2016-10-27 15:27:59 +02:00
Miklos Vajna
b6f98b7115 xmlsecurity PDF verify: support SHA-256
And various other minor fixes.

Change-Id: Ifcccebf48aac8ad526406f2d7a402a840d3c91cd
2016-10-26 20:09:38 +02:00
Miklos Vajna
fc56d31c09 xmlsecurity PDF verify: fix handling of non-imported certs
Previously we only managed to verify a signature in case the certificate
was already imported in the local NSS db. Don't depend on that by
(temporarily) importing certificates from the PDF signature.

Also adjust a test file that failed previously (the test DB has only an
"Alice" cert imported, intentionally sign the file as "Bob" as well).

Change-Id: Id8440acc31915f5a1718ea48129b950bb67e7486
2016-10-26 20:09:38 +02:00
Miklos Vajna
f45ace2897 xmlsecurity PDF signing: fix byte range check for multiple signatures
We can mandate that the byte range end is the end of the file for the
last signature only.

With this, signing a previously unsigned file multiple times works, so
add a matching testcase for that as well.

Change-Id: I8fe5482890fca4dab8da6305aa7fc7f60df612d8
2016-10-26 20:09:38 +02:00
Miklos Vajna
a324099538 xmlsecurity PDF signing: only write incremental xref in an incremental update
We used to just dump all the object offsets in the xref of the
incremental update, but Adobe Acrobat doesn't like that, and considers
that a second signature invalidates the first. If we properly only
mention new and changed objects in the xref, then this doesn't happen.

This requires actually parsing incremental updates, the previous code
depended on LO writing not-really-incremental xrefs at the end of
incremental updates.

Change-Id: Icdd73fe0a3eab16f8c5a62f1355edbb49f6e73de
Reviewed-on: https://gerrit.libreoffice.org/30288
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-26 12:20:26 +00:00
Miklos Vajna
b1434b95d1 xmlsecurity PDF sign: use unique ID for a new signature
Otherwise Adobe Acrobat thinks they are different versions of the same
signature.

Change-Id: I454c7d87106e348fd70f18fad83f2c3aeda29dff
Reviewed-on: https://gerrit.libreoffice.org/30278
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-26 07:04:49 +00:00
Miklos Vajna
a3b3e336e5 xmlsecurity PDF sign: append reference to the Catalog's AcroForm key
Similar to the Page object's Annots key, but here we want to append our
reference to the nested AcroForm/Fields key, so that needs more
infrastructure.

This is also needed (but not enough) to be able to sign a PDF document
multiple times.

Change-Id: I4d5e2aa8f49d2181a15cbf7c4e27577fc98b547d
Reviewed-on: https://gerrit.libreoffice.org/30267
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-25 17:53:30 +00:00
Miklos Vajna
d34a8993e3 xmlsecurity PDF sign: append reference to the Page's Annots key
Previously we assumed that the Page object's dictionary has no Annots
key. Now detect if that's not true, and in that case don't just copy of
the whole dictionary (as part of the incremental update), instead copy
it in two steps, so we can insert our reference in the middle.

This is needed (but not enough alone) to be able to sign a PDF document
multiple times.

Change-Id: Ia5bf993320428eef80551e7e9cc7bfb2b858db7f
Reviewed-on: https://gerrit.libreoffice.org/30257
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-25 12:03:58 +00:00
Miklos Vajna
c758ad4a8b CppunitTest_xmlsecurity_pdfsigning: fix this on RHEL6
The pdfdocument problem is present only on 32bit.

The pdfsigning problem is present on RHEL6, but not on RHEL7, for some
reason NSS fails to parse the provided profile (generated by a bit newer
NSS). Just return early in that case, we want to test the PDF code
there, not NSS.

Change-Id: I1123865d4b2176676a8fdaf648222fda8ca0b923
Reviewed-on: https://gerrit.libreoffice.org/30229
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-10-25 07:02:29 +00:00
Julien Nabet
330b1d8b1e Revert "Blind fix to TBs"
This reverts commit f736c71dde.

Change-Id: I83df90a5fc6cefb4e0c4fdbb37fa46a546809b83
Reviewed-on: https://gerrit.libreoffice.org/30223
Reviewed-by: Julien Nabet <serval2412@yahoo.fr>
Tested-by: Julien Nabet <serval2412@yahoo.fr>
2016-10-24 09:47:56 +00:00
Andrea Gelmini
525311b0d6 Fix typos
Change-Id: Ib7b17f85c7b6a1937c3f6e1617ceec58074643b4
Reviewed-on: https://gerrit.libreoffice.org/30040
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: jan iversen <jani@documentfoundation.org>
Tested-by: jan iversen <jani@documentfoundation.org>
2016-10-23 17:17:01 +00:00
Julien Nabet
f736c71dde Blind fix to TBs
Change-Id: I74165378255e3c844ee7f91a5d4eb09a573784bd
Reviewed-on: https://gerrit.libreoffice.org/30179
Reviewed-by: Julien Nabet <serval2412@yahoo.fr>
Tested-by: Julien Nabet <serval2412@yahoo.fr>
2016-10-23 15:20:37 +00:00
Miklos Vajna
5819448023 xmlsecurity: implement removal of a signature in pdfverify
It's not exactly clear how one should guess what was file end before
signing, for now assume the followings:

- the file ended with a %%EOF, an optional \r, and a \n
- the number of incremental updates is the same as the number of
  signatures

When the later is not the case, don't attempt to remove the signature.

Change-Id: I203a7b0605fc061ec6aacfde3a8eedc4736379f2
Reviewed-on: https://gerrit.libreoffice.org/30140
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-21 16:19:49 +00:00
Miklos Vajna
834abca71b xmlsecurity PDF export: fix missing /Prev key in the trailer dictionary
"In addition, the added trailer dictionary shall contain a Prev entry
giving the location of the previous cross-reference section."
(ISO-32000-1, section 7.5.6). Add it, even if it seems Adobe Acrobat can
live with not writing it.

Change-Id: I1f53e75ebe7dba4b45b3cf1908b2d3b031ef6b02
Reviewed-on: https://gerrit.libreoffice.org/30133
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-21 13:18:00 +00:00
Miklos Vajna
81bbede18f cid#1374093 unreachable code
Change-Id: Ib34aa7af7bdba304fe1f212d39c3c6de24e87a2d
2016-10-21 08:13:06 +02:00
Miklos Vajna
f7a6867d6a xmlsecurity: export description in PDF signature
Extract appendUnicodeTextString() from the PDF writer impl, and reuse it
in xmlsecurity, to share code.

Change-Id: Icdc2f89132cd29e07280001e30bad97e0a644654
Reviewed-on: https://gerrit.libreoffice.org/30110
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-21 06:05:13 +00:00
Miklos Vajna
4c76fd79b7 Extract vcl::PDFWriter::Sign() from vcl and xmlsecurity
The use case is different in vcl and xmlsecurity: vcl creates a new PDF
(possibly with a signature), while xmlsecurity signs an existing PDF,
but this part can be shared between the two.

So far in vcl only the nss part is moved, not touching mscrypto yet.

Change-Id: Ie776f622c1a4a3a18e79e78f68722a2fa219a83b
Reviewed-on: https://gerrit.libreoffice.org/30063
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-20 09:00:44 +00:00
Miklos Vajna
e9834a3eaf xmlsecurity: fix the PDF incremental update's cross-references table
The VCL pdf export writes a space after the in-use entry, and turns out
Adobe Acrobat starts to "repair" the file if there is no such space.
Which means the signature is validated against the repaired document,
resulting in a "The signature byte range is invalid" error message,
hiding the root cause.

Not that ISO-32000 7.5.4 "Cross-References Table" would mention the need
for such whitespace at the end of the lines.

Change-Id: I165b57809550f184f374c00f28426a3cd813c63f
2016-10-19 16:46:20 +02:00
Miklos Vajna
365f2e9054 cid#1374076 uncaught exception
Change-Id: Ifdbfc9f905f2ed7778830a0f2cc114d27feb36da
2016-10-19 08:32:23 +02:00
Miklos Vajna
c70eeb2be7 xmlsecurity: use NSS on Linux/macOS to generate pdf signature
This is just the minimum so that our own PDF signature validation is
happy.

Change-Id: I1148817c11174fd4f9184d0ce2c0511e9d6bd11c
Reviewed-on: https://gerrit.libreoffice.org/30018
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-18 18:30:40 +00:00
Miklos Vajna
a747ed3ca4 xmlsecurity: add signing stub in pdfverify
It does (almost?) everything to add a valid signature, except the actual
signature blob.

Change-Id: I727770caef2335473a8dba29d60daf5a049129b9
Reviewed-on: https://gerrit.libreoffice.org/30001
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
2016-10-18 13:00:51 +00:00
Miklos Vajna
9a3b752756 xmlsecurity: initial incremental update support in pdfverify
I plan to use this for signing purposes, but so far what's implemented
just writes out an incremental update at the end of the file, without
actually updating much (just an unreferenced appearance object).

Change-Id: I1cb40430ade6af0a25ff914ba4df670a77fcf457
2016-10-18 10:58:57 +02:00
Miklos Vajna
d19cb7f597 xmlsecurity: detect if PDF signature doesn't sign the whole file
For ODF signatures we require that all streams of the storage are
signed.  The PDF equivalent of this is to ensure that the byte range is
the entire file, including the signature dictionary but excluding the
signature value itself.

Change-Id: Ie47f42913e2aa960f35079eb981768cd47fb9f92
Reviewed-on: https://gerrit.libreoffice.org/29890
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-10-17 08:26:37 +00:00
Miklos Vajna
f3d9249ca6 xmlsecurity: check file header when reading PDF signature
Currently the only non-ZIP-based import filter that declares the
SUPPORTSSIGNING flag is PDF, so if we get a stream without a storage, we
assume it's PDF.

If any other non-ZIP-based format would add that flag in the future,
that would mean PDFDocument::Read() gets that as an input. That means it
makes sense to at least check the file header early in the tokenizer,
and return early when that doesn't match.

Change-Id: I8760d130c4211f37be705e03b22814825042cac8
Reviewed-on: https://gerrit.libreoffice.org/29888
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2016-10-17 06:08:59 +00:00