The signing and verification works fine until the file is signed for the 2nd time (with the user's certificate). After that the first signature somehow becomes invalid
As already clarified in comments, if one wants the first signature to remain valid, the second must be applied in append mode (iText lingo) / as an incremental update (PDF specification lingo). Otherwise the signed bytes very likely change and the signed hash value does not match anymore.
- Is it at all possible to check whether the document was modified between the 2 signatures were applied?
More exactly your question should be whether it is possible to check if the document modifications between the two signatures are in excess of the changes plausible for embedding the second signature.
In short: It is possible in your use case but still means quite some work and juggling with iText low-level APIs. In detail:
In general this is not trivial because adding a new signature with visualization plausibly
- changes the annotations of the page in question as the signature visualization is a widget annotation;
- changes the PDF form definition as the signature is anchored as a form field;
- may create appearance streams for other form fields to fix their exact appearance in the signed file;
- may change metadata streams to document the act of signing;
- may change the digital security store of the document;
- may change probably even more.
Thus, it is non-trivial to check whether the changes fall in any of those categories or not.
Furthermore you have to check for other cheats, e.g. the signature visualization might cover the whole page and show a manipulated version of that content...
But you say your users
have to sign with Adobe Acrobat
This may make your task somewhat feasible: If you use Adobe Acrobat to add sample signatures to a number of documents you signed first before, you can analyze what Adobe Acrobat usually changes in documents when signing them.
Using this knowledge you can implement a class which checks whether only those changes are present.
- If not - what's the alternate way to programmatically (using itextsharp) check whether the PDF file was changed after it was generated and before it was signed?
Without a first signature applied by you the situation becomes much more difficult as there is no reason for the user's PDF signing software to limit the internal structure changes in the document as long as they have no impact on its outward behavior. So I would try working with double signatures.
Alternatively you might try and render the original document and the version signed by your user as bitmaps and compare them. There should only be differences in the area where the user signature visualization is placed. This does not verify the interactive PDF features but at least the integrity of a print output.
Rendering is not yet a feature of iText but the parsing framework meanwhile has evolved far enough to serve as the base for a rendering feature.
Applying a first signature can even help preventing accidental changes: If you provide an empty signature field for the user and use a certification signature yourself, you can limit the "allowed changes" to little more than filling that empty signature field, and Adobe Acrobat usually respects such restrictions unless explicitly told otherwise.
For backgrounds on integrated PDF signatures have a look at this answer on Information Security Stack Exchange.